* [PATCH bpf-next v2 0/2] bpf: Validate byte type for helper stack reads
@ 2026-07-17 10:05 Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 1/2] bpf: Check byte type when validating " Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 2/2] selftests/bpf: Cover helper reads of invalid spill siblings Yiyang Chen
0 siblings, 2 replies; 3+ messages in thread
From: Yiyang Chen @ 2026-07-17 10:05 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Yiyang Chen, John Fastabend, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, bpf,
linux-kselftest, linux-kernel
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/cover.1783920884.git.chenyy23@mails.tsinghua.edu.cn/
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
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH bpf-next v2 1/2] bpf: Check byte type when validating helper stack reads
2026-07-17 10:05 [PATCH bpf-next v2 0/2] bpf: Validate byte type for helper stack reads Yiyang Chen
@ 2026-07-17 10:05 ` Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 2/2] selftests/bpf: Cover helper reads of invalid spill siblings Yiyang Chen
1 sibling, 0 replies; 3+ messages in thread
From: Yiyang Chen @ 2026-07-17 10:05 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Yiyang Chen, John Fastabend, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, bpf,
linux-kselftest, linux-kernel
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@mails.tsinghua.edu.cn>
---
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
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH bpf-next v2 2/2] selftests/bpf: Cover helper reads of invalid spill siblings
2026-07-17 10:05 [PATCH bpf-next v2 0/2] bpf: Validate byte type for helper stack reads Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 1/2] bpf: Check byte type when validating " Yiyang Chen
@ 2026-07-17 10:05 ` Yiyang Chen
1 sibling, 0 replies; 3+ messages in thread
From: Yiyang Chen @ 2026-07-17 10:05 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Yiyang Chen, John Fastabend, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan, bpf,
linux-kselftest, linux-kernel
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 <chenyy23@mails.tsinghua.edu.cn>
---
.../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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-17 10:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-17 10:05 [PATCH bpf-next v2 0/2] bpf: Validate byte type for helper stack reads Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 1/2] bpf: Check byte type when validating " Yiyang Chen
2026-07-17 10:05 ` [PATCH bpf-next v2 2/2] selftests/bpf: Cover helper reads of invalid spill siblings Yiyang Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®