Skip to content

Commit 69772f5

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Don't mark STACK_INVALID as STACK_MISC in mark_stack_slot_misc
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 <[email protected]> Reported-by: Tao Lyu <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5a6ea70 commit 69772f5

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
@@ -1202,14 +1202,17 @@ static bool is_spilled_scalar_reg64(const struct bpf_stack_state *stack)
12021202
/* Mark stack slot as STACK_MISC, unless it is already STACK_INVALID, in which
12031203
* case they are equivalent, or it's STACK_ZERO, in which case we preserve
12041204
* more precise STACK_ZERO.
1205-
* Note, in uprivileged mode leaving STACK_INVALID is wrong, so we take
1206-
* env->allow_ptr_leaks into account and force STACK_MISC, if necessary.
1205+
* Regardless of allow_ptr_leaks setting (i.e., privileged or unprivileged
1206+
* mode), we won't promote STACK_INVALID to STACK_MISC. In privileged case it is
1207+
* unnecessary as both are considered equivalent when loading data and pruning,
1208+
* in case of unprivileged mode it will be incorrect to allow reads of invalid
1209+
* slots.
12071210
*/
12081211
static void mark_stack_slot_misc(struct bpf_verifier_env *env, u8 *stype)
12091212
{
12101213
if (*stype == STACK_ZERO)
12111214
return;
1212-
if (env->allow_ptr_leaks && *stype == STACK_INVALID)
1215+
if (*stype == STACK_INVALID)
12131216
return;
12141217
*stype = STACK_MISC;
12151218
}

0 commit comments

Comments
 (0)