check_stack_range_initialized() accepts a STACK_INVALID byte only when allow_uninit_stack is set. It then has a separate path for spilled registers: scalar spills are accepted directly, while pointer spills require allow_ptr_leaks. bpf_is_spilled_reg() tests slot_type[BPF_REG_SIZE - 1], the canonical top byte of the slot. After a sub-register scalar spill, such as a one-byte store to fp-8, save_register_state() marks only the covered byte STACK_SPILL. mark_stack_slot_misc() leaves the untouched sibling bytes STACK_INVALID. When the helper range reaches fp-7, the STACK_INVALID case correctly declines it if allow_uninit_stack is false, but the following scalar-spill path accepts it based on fp-8's slot-wide spill marker. For a CAP_BPF load without CAP_PERFMON, allow_uninit_stack is false, so an untouched sibling byte such as fp-7 should reach -EACCES. Instead, passing fp-7 as the value to bpf_map_update_elem() after a one-byte spill to fp-8 is accepted, and the helper copies a byte the program never wrote. A direct read of fp-7 rejects; only the helper-argument path is affected. Require the current byte to be STACK_SPILL before taking the spilled-register path. Full-width spills still mark every byte STACK_SPILL. A permitted STACK_INVALID read is still handled by the preceding allow_uninit_stack case. This mirrors the producer-side fix in mark_stack_slot_misc(), which preserves STACK_INVALID siblings, and applies the same invariant to helper arguments. The existing direct-read selftest does not cover this path. Fixes: 354e8f1970f8 ("bpf: Support <8-byte scalar spill and refill") Signed-off-by: Yiyang Chen --- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a78cdabf85607..7c61ade27d2a6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6735,7 +6735,8 @@ static int check_stack_range_initialized( goto mark; } - if (bpf_is_spilled_reg(&state->stack[spi]) && + if (*stype == STACK_SPILL && + bpf_is_spilled_reg(&state->stack[spi]) && (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || env->allow_ptr_leaks)) { if (clobber) { -- 2.34.1 Add a verifier_spill_fill case that passes an untouched byte next to a one-byte scalar spill to bpf_map_update_elem(). The full-capability load retains the existing uninitialized-stack behavior, while the CAP_BPF-only load must reject the STACK_INVALID helper read. Fixes: 354e8f1970f8 ("bpf: Support <8-byte scalar spill and refill") Signed-off-by: Yiyang Chen --- .../selftests/bpf/progs/verifier_spill_fill.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_spill_fill.c b/tools/testing/selftests/bpf/progs/verifier_spill_fill.c index 8b166c42c4e0e..2e62a7ddcbce2 100644 --- a/tools/testing/selftests/bpf/progs/verifier_spill_fill.c +++ b/tools/testing/selftests/bpf/progs/verifier_spill_fill.c @@ -11,6 +11,13 @@ struct { __uint(max_entries, 4096); } map_ringbuf SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u8); +} map_partial_spill SEC(".maps"); + SEC("socket") __description("check valid spill/fill") __success __failure_unpriv __msg_unpriv("R0 leaks addr") @@ -1307,6 +1314,33 @@ __naked void stack_noperfmon_spill_32bit_onto_64bit_slot(void) : __clobber_all); } +SEC("socket") +__description("helper read must reject an invalid sibling of a narrow spill") +__success +__caps_unpriv(CAP_BPF) +__failure_unpriv __msg_unpriv("invalid read from stack R3 off -7+0 size 1") +__naked void helper_read_invalid_partial_spill_sibling(void) +{ + asm volatile (" \ + r5 = 1; \ + *(u8 *)(r10 - 8) = r5; \ + r2 = 0; \ + *(u32 *)(r10 - 16) = r2; \ + r1 = %[map_partial_spill] ll; \ + r2 = r10; \ + r2 += -16; \ + r3 = r10; \ + r3 += -7; \ + r4 = 0; \ + call %[bpf_map_update_elem]; \ + r0 = 0; \ + exit; \ +" : + : __imm(bpf_map_update_elem), + __imm_addr(map_partial_spill) + : __clobber_all); +} + /* * stacksafe(): check if 32-bit scalar spill in old state is considered * equivalent to STACK_MISC in cur state. -- 2.34.1