Skip to content

Commit 8c30bad

Browse files
kkdwivedismb49
authored andcommitted
bpf: Don't mark STACK_INVALID as STACK_MISC in mark_stack_slot_misc
BugLink: https://bugs.launchpad.net/bugs/2102118 [ Upstream commit 69772f509e084ec6bca12dbcdeeeff41b0103774 ] Inside mark_stack_slot_misc, we should not upgrade STACK_INVALID to STACK_MISC when allow_ptr_leaks is false, since invalid contents shouldn't be read unless the program has the relevant capabilities. The relaxation only makes sense when env->allow_ptr_leaks is true. However, such conversion in privileged mode becomes unnecessary, as invalid slots can be read without being upgraded to STACK_MISC. Currently, the condition is inverted (i.e. checking for true instead of false), simply remove it to restore correct behavior. Fixes: eaf18fe ("bpf: preserve STACK_ZERO slots on partial reg spills") Acked-by: Andrii Nakryiko <andrii@kernel.org> Reported-by: Tao Lyu <tao.lyu@epfl.ch> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20241204044757.1483141-2-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Koichiro Den <koichiro.den@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 14bef7c commit 8c30bad

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

kernel/bpf/verifier.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,14 +1158,17 @@ static bool is_spilled_scalar_reg(const struct bpf_stack_state *stack)
11581158
/* Mark stack slot as STACK_MISC, unless it is already STACK_INVALID, in which
11591159
* case they are equivalent, or it's STACK_ZERO, in which case we preserve
11601160
* more precise STACK_ZERO.
1161-
* Note, in uprivileged mode leaving STACK_INVALID is wrong, so we take
1162-
* env->allow_ptr_leaks into account and force STACK_MISC, if necessary.
1161+
* Regardless of allow_ptr_leaks setting (i.e., privileged or unprivileged
1162+
* mode), we won't promote STACK_INVALID to STACK_MISC. In privileged case it is
1163+
* unnecessary as both are considered equivalent when loading data and pruning,
1164+
* in case of unprivileged mode it will be incorrect to allow reads of invalid
1165+
* slots.
11631166
*/
11641167
static void mark_stack_slot_misc(struct bpf_verifier_env *env, u8 *stype)
11651168
{
11661169
if (*stype == STACK_ZERO)
11671170
return;
1168-
if (env->allow_ptr_leaks && *stype == STACK_INVALID)
1171+
if (*stype == STACK_INVALID)
11691172
return;
11701173
*stype = STACK_MISC;
11711174
}

0 commit comments

Comments
 (0)