bpf: Validate byte type for helper stack reads
Subsystems (4)
v2 cover bpf: Validate byte type for helper stack reads
The verifier checks helper memory arguments byte by byte, but its scalar spill fast path only checks the slot-wide spill marker. For a CAP_BPF load, a narrow scalar spill can therefore make an untouched sibling byte appear initialized when the sibling is passed to a helper.
Patch 1 makes the fast path require STACK_SPILL for the byte being checked. Patch 2 adds verifier_spill_fill coverage for a CAP_BPF map update helper reading an invalid sibling byte.
Changes in v2:
- Rewrite the patch 1 commit message to explain the uninitialized-stack-read invariant and distinguish it from the pointer-leak check, with references to the related producer-side fix and direct-read selftest.
- Add the regression Fixes tag to patch 2 and use the neighboring inline-asm line-continuation style.
v1: https://lore.kernel.org/bpf/[email protected]/
Yiyang Chen (2):
bpf: Check byte type when validating helper stack reads selftests/bpf: Cover helper reads of invalid spill siblings
kernel/bpf/verifier.c | 3 +- .../selftests/bpf/progs/verifier_spill_fill.c | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-)
base-commit: 1d91ea01185656ac3ee63c5f9f6f8bde3c746b3d
Signature · 1 line
2.34.1
PATCH v2 1/2 bpf: Check byte type when validating helper stack reads
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.
--- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -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) {
Signature · 1 line
2.34.1
PATCH v2 2/2 selftests/bpf: Cover helper reads of invalid spill siblings
In reply to Yiyang ChenAdd 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.
--- .../selftests/bpf/progs/verifier_spill_fill.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)
tools/testing/selftests/bpf/progs/verifier_spill_fill.c +34 -0
@@ -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 ) +{ + asmvolatile (" \ +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.
Signature · 1 line
2.34.1