[PATCH bpf-next v2 1/2] bpf: Check byte type when validating helper stack reads
From: Yiyang Chen
Date: Fri Jul 17 2026 - 06:13:28 EST
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 <chenyy23@xxxxxxxxxxxxxxxxxxxxx>
---
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