Skip to content

Commit f5e477a

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
bpf: Fix slot type check in check_stack_write_var_off
For the case where allow_ptr_leaks is false, code is checking whether slot type is STACK_INVALID and STACK_SPILL and rejecting other cases. This is a consequence of incorrectly checking for register type instead of the slot type (NOT_INIT and SCALAR_VALUE respectively). Fix the check. Fixes: 01f810a ("bpf: Allow variable-offset stack access") Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20221103191013.1236066-5-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 261f466 commit f5e477a

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,14 +3181,17 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,
31813181
stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
31823182
mark_stack_slot_scratched(env, spi);
31833183

3184-
if (!env->allow_ptr_leaks
3185-
&& *stype != NOT_INIT
3186-
&& *stype != SCALAR_VALUE) {
3187-
/* Reject the write if there's are spilled pointers in
3188-
* range. If we didn't reject here, the ptr status
3189-
* would be erased below (even though not all slots are
3190-
* actually overwritten), possibly opening the door to
3191-
* leaks.
3184+
if (!env->allow_ptr_leaks && *stype != STACK_MISC && *stype != STACK_ZERO) {
3185+
/* Reject the write if range we may write to has not
3186+
* been initialized beforehand. If we didn't reject
3187+
* here, the ptr status would be erased below (even
3188+
* though not all slots are actually overwritten),
3189+
* possibly opening the door to leaks.
3190+
*
3191+
* We do however catch STACK_INVALID case below, and
3192+
* only allow reading possibly uninitialized memory
3193+
* later for CAP_PERFMON, as the write may not happen to
3194+
* that slot.
31923195
*/
31933196
verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d",
31943197
insn_idx, i);

0 commit comments

Comments
 (0)