Skip to content

Commit e4bbd35

Browse files
kkdwvdgregkh
authored andcommitted
bpf: Don't mark STACK_INVALID as STACK_MISC in mark_stack_slot_misc
[ Upstream commit 69772f5 ] 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>
1 parent aa71ab2 commit e4bbd35

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,17 @@ static bool is_spilled_scalar_reg(const struct bpf_stack_state *stack)
13501350
/* Mark stack slot as STACK_MISC, unless it is already STACK_INVALID, in which
13511351
* case they are equivalent, or it's STACK_ZERO, in which case we preserve
13521352
* more precise STACK_ZERO.
1353-
* Note, in uprivileged mode leaving STACK_INVALID is wrong, so we take
1354-
* env->allow_ptr_leaks into account and force STACK_MISC, if necessary.
1353+
* Regardless of allow_ptr_leaks setting (i.e., privileged or unprivileged
1354+
* mode), we won't promote STACK_INVALID to STACK_MISC. In privileged case it is
1355+
* unnecessary as both are considered equivalent when loading data and pruning,
1356+
* in case of unprivileged mode it will be incorrect to allow reads of invalid
1357+
* slots.
13551358
*/
13561359
static void mark_stack_slot_misc(struct bpf_verifier_env *env, u8 *stype)
13571360
{
13581361
if (*stype == STACK_ZERO)
13591362
return;
1360-
if (env->allow_ptr_leaks && *stype == STACK_INVALID)
1363+
if (*stype == STACK_INVALID)
13611364
return;
13621365
*stype = STACK_MISC;
13631366
}

0 commit comments

Comments
 (0)