Skip to contentSkip to conversation

bpf: Preserve scalar zero spills for var-offset stack reads

PATCH Superseded Woojin Ji 5 participants 12 messages Jun 13 – Jun 25

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Woojin Ji

author
#0 lore raw

v2 cover bpf: Preserve scalar zero spills for var-offset stack reads

Variable-offset stack reads currently lose the known-zero fact when the loaded byte comes from a spilled scalar constant zero rather than from a STACK_ZERO byte. This series teaches the var-offset stack read path to preserve that zero fact while marking the contributing spill slots precise, and adds verifier_var_off coverage for the new behaviour and the pruning-sensitive negative case.

I don't have a confirmed deployed-program regression, so this is targeted at bpf-next. I did confirm the pattern is reachable from normal C codegen with a small helper-based BPF C reproducer: clang 22.1.6 -O2/-O3 can produce a spilled scalar-zero plus variable-offset stack byte load pattern that the unpatched verifier rejects and the patched verifier accepts.

Changes in v2:
- Rebased onto bpf-next.
- Split verifier and selftests changes into separate patches.
- Added bpf_bt_set_frame_slot_mask() instead of open-coding a slot loop.
- Kept the new eager precision marking on the variable-offset read path.
- Added verifier log assertions for the zero result and mark_precise trail.
- Added sub-8-byte spill coverage with STACK_ZERO and STACK_MISC neighbours.
- Cleaned up inline asm formatting and local labels.

Tested with:
- make O=../../out/kernel olddefconfig
- make O=../../out/kernel -j$(nproc) kernel/bpf/verifier.o
- make O=../../out/kernel LLVM=1 -j$(nproc) bzImage
- ./test_progs -t verifier_var_off -v
Summary: 1/30 PASSED, 0 SKIPPED, 0 FAILED
- ./test_progs -t verifier_spill_fill -t verifier_live_stack \
-t verifier_search_pruning -v
Summary: 3/127 PASSED, 0 SKIPPED, 0 FAILED
- veristat -o csv verifier_var_off.bpf.o

Assisted-by opencode:gpt-5.5

---
Woojin Ji (2):
bpf: Preserve scalar zero spills for var-offset stack reads selftests/bpf: Cover var-offset stack reads from zero spills

 include/linux/bpf_verifier.h                       |   5 +
 kernel/bpf/verifier.c                              |  55 ++++++--
 .../testing/selftests/bpf/progs/verifier_var_off.c | 148 +++++++++++++++++++++
 3 files changed, 198 insertions(+), 10 deletions(-)

---
base-commit: 7bfb93e3475be9de894f1cecd3a727d3e1649b03
change-id: 20260610-bpf-stack-var-off-zero-v1-34ad1bc3b533

Best regards,

Signature · 1 line
Woojin Ji <[email protected]>
author
#1 lore raw

PATCH v2 1/2 bpf: Preserve scalar zero spills for var-offset stack reads

Variable-offset stack reads can read back bytes that belong to a previously spilled scalar constant zero. Today mark_reg_stack_read() only treats STACK_ZERO bytes as known zero bytes, so the destination register becomes unknown for such reads even though every byte in the read range is known to be zero. This can lead to rejecting otherwise valid programs once the loaded byte is used as a pointer offset.

This pattern is not limited to hand-written verifier tests. clang 22.1.6 at -O2 and -O3 emits it from a small helper-based BPF C reproducer; the resulting program is rejected before this change and accepted afterwards. No deployed-program regression is currently known, so target bpf-next rather than bpf.

Teach the variable-offset stack read path to also consider STACK_SPILL bytes backed by a spilled scalar constant zero as zero bytes. When a zero result depends on such a spill, mark the contributing stack slots precise before accepting the const-zero result so pruning cannot reuse a zero-spill state for a later non-zero spill state.

Keep fixed-offset stack reads out of this eager precision marking path, as the new behaviour is only needed when filling a register from a variable-offset stack byte range.

Assisted-by opencode:gpt-5.5
---
 include/linux/bpf_verifier.h |  5 ++++
 kernel/bpf/verifier.c        | 55 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 10 deletions(-)
include/linux/bpf_verifier.h +5 -0
index d57b339a8..c2e665044 100644
@@ -1242,6 +1242,11 @@ static inline void bpf_bt_set_frame_slot(struct backtrack_state *bt, u32 frame,
 	bt->stack_masks[frame] |= 1ull << slot;
 }
 
+static inline void bpf_bt_set_frame_slot_mask(struct backtrack_state *bt, u32 frame, u64 mask)
+{
+	bt->stack_masks[frame] |= mask;
+}
+
 static inline void bt_set_frame_stack_arg_slot(struct backtrack_state *bt, u32 frame, u32 slot)
 {
 	bt->stack_arg_masks[frame] |= 1 << slot;
kernel/bpf/verifier.c +45 -10

99 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw
This looks like a bug fix for the variable-offset stack access feature. The commit message describes that mark_reg_stack_read() only treated STACK_ZERO bytes as known zero bytes, causing the…
Quoted 19 lines

Variable-offset stack reads can read back bytes that belong to a previously spilled scalar constant zero. Today mark_reg_stack_read() only treats STACK_ZERO bytes as known zero bytes, so the destination register becomes unknown for such reads even though every byte in the read range is known to be zero. This can lead to rejecting otherwise valid programs once the loaded byte is used as a pointer offset.

This pattern is not limited to hand-written verifier tests. clang 22.1.6 at -O2 and -O3 emits it from a small helper-based BPF C reproducer; the resulting program is rejected before this change and accepted afterwards. No deployed-program regression is currently known, so target bpf-next rather than bpf.

Teach the variable-offset stack read path to also consider STACK_SPILL bytes backed by a spilled scalar constant zero as zero bytes. When a zero result depends on such a spill, mark the contributing stack slots precise before accepting the const-zero result so pruning cannot reuse a zero-spill state for a later non-zero spill state.

Quoted 6 lines

Keep fixed-offset stack reads out of this eager precision marking path, as the new behaviour is only needed when filling a register from a variable-offset stack byte range.

Assisted-by opencode:gpt-5.5

This looks like a bug fix for the variable-offset stack access feature. The commit message describes that mark_reg_stack_read() only treated STACK_ZERO bytes as known zero bytes, causing the verifier to incorrectly reject valid programs. This behavior appears to have been introduced when the variable-offset stack access feature was added in commit 01f810ace9ed3.

Should this include:

Fixes 01f810ace9ed3 ("bpf: Allow variable-offset stack access")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27465403255

maintainer Acked-by on v2 1/2
#3 lore raw
In reply to Woojin Ji
Quoted 19 lines · Woojin Ji

Variable-offset stack reads can read back bytes that belong to a previously spilled scalar constant zero. Today mark_reg_stack_read() only treats STACK_ZERO bytes as known zero bytes, so the destination register becomes unknown for such reads even though every byte in the read range is known to be zero. This can lead to rejecting otherwise valid programs once the loaded byte is used as a pointer offset.

This pattern is not limited to hand-written verifier tests. clang 22.1.6 at -O2 and -O3 emits it from a small helper-based BPF C reproducer; the resulting program is rejected before this change and accepted afterwards. No deployed-program regression is currently known, so target bpf-next rather than bpf.

Teach the variable-offset stack read path to also consider STACK_SPILL bytes backed by a spilled scalar constant zero as zero bytes. When a zero result depends on such a spill, mark the contributing stack slots precise before accepting the const-zero result so pruning cannot reuse a zero-spill state for a later non-zero spill state.

Quoted 7 lines

Keep fixed-offset stack reads out of this eager precision marking path, as the new behaviour is only needed when filling a register from a variable-offset stack byte range.

Assisted-by opencode:gpt-5.5

---

[...]

#4 lore raw
In reply to Woojin Ji
Quoted 5 lines · Woojin Ji

Keep fixed-offset stack reads out of this eager precision marking path, as the new behaviour is only needed when filling a register from a variable-offset stack byte range.

...

kernel/bpf/verifier.c static int check_stack_write_var_off(struct bpf_verifier_env *env, · line 3711

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+static int mark_reg_stack_read(struct bpf_verifier_env *env,
+                              /* func where src register points to */
+                              struct bpf_func_state *ptr_state,
+                              int min_off, int max_off, int dst_regno,
+                              bool mark_zero_spills)

why differentiate fixed vs var-off with this flag?

It seems the fixed offset reads would benefit as well?

maintainer
#5 lore raw
Quoted 15 lines · Alexei Starovoitov
Quoted 5 lines · Woojin Ji

Keep fixed-offset stack reads out of this eager precision marking path, as the new behaviour is only needed when filling a register from a variable-offset stack byte range.

...

kernel/bpf/verifier.c static int check_stack_write_var_off(struct bpf_verifier_env *env, · line 3711

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+static int mark_reg_stack_read(struct bpf_verifier_env *env,
+                              /* func where src register points to */
+                              struct bpf_func_state *ptr_state,
+                              int min_off, int max_off, int dst_regno,
+                              bool mark_zero_spills)
Quoted 3 lines

why differentiate fixed vs var-off with this flag?

It seems the fixed offset reads would benefit as well?

Hm, I though we handle this for fixed offset case via the flags in the jump history, but apparently not the mixed case of '0000ssss'.

author
#6 lore raw
In reply to Alexei Starovoitov

Yes, good point.

I kept the eager precision marking limited to var-off reads because that was the original reproducer, but fixed-offset reads have the same mixed case as well. Pure scalar-zero spill reads are already handled, but a fixed read spanning both STACK_ZERO and scalar const-zero STACK_SPILL bytes, e.g. 0000ssss, still falls back to unknown.

I removed the flag in v3, made the zero-spill handling common to both paths, kept the existing pure register-fill behavior intact, and added a fixed-offset selftest for the mixed case.

Thanks,
Woojin

author
#7 lore raw

PATCH v2 2/2 selftests/bpf: Cover var-offset stack reads from zero spills

In reply to Woojin Ji

Add verifier_var_off coverage for variable-offset stack reads from spilled scalar constant zero values.

Cover single-slot and cross-slot spilled zero reads, a sub-8-byte spill with neighbouring STACK_ZERO bytes, and a sub-8-byte spill with neighbouring STACK_MISC bytes that must not be treated as zero. Add a state-pruning negative test to ensure the verifier marks the spilled zero stack slot precise before using it to derive a const-zero register.

Use verifier log assertions to check both the zero result and the precision backtracking trail.

Assisted-by opencode:gpt-5.5
---
 .../testing/selftests/bpf/progs/verifier_var_off.c | 148 +++++++++++++++++++++
 1 file changed, 148 insertions(+)
tools/testing/selftests/bpf/progs/verifier_var_off.c +148 -0
index f345466bc..f0052d831 100644
@@ -59,6 +59,154 @@ __naked void stack_read_priv_vs_unpriv(void)
 "	::: __clobber_all);
 }
 
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves spilled zero")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__retval(0)
+__naked void stack_read_var_off_preserves_spilled_zero(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u64*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves spilled zero across slots")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8,-16")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__retval(0)
+__naked void stack_read_var_off_preserves_spilled_zero_across_slots(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u64*)(r10 - 8) = r0;\
+	*(u64*)(r10 - 16) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 15;\
+	r2 -= 16;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves partial spilled zero")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__retval(0)
+__naked void stack_read_var_off_preserves_partial_spilled_zero(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u8*)(r10 - 9) = r0;\
+	*(u8*)(r10 - 10) = r0;\
+	*(u8*)(r10 - 11) = r0;\
+	*(u8*)(r10 - 12) = r0;\
+	*(u8*)(r10 - 13) = r0;\
+	*(u8*)(r10 - 14) = r0;\
+	*(u8*)(r10 - 15) = r0;\
+	*(u32*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 15;\
+	if r2 > 10 goto l0_%=;\
+	r2 -= 15;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+l0_%=: r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read partial spill with misc data")
+__failure
+__msg("invalid variable-offset write to stack R1")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__naked void stack_read_var_off_partial_spill_with_misc_data(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u32*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = 0;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read tracks spilled zero precisely")
+__failure
+__flag(BPF_F_TEST_STATE_FREQ)
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("invalid variable-offset write to stack R1")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__naked void stack_read_var_off_tracks_spilled_zero_precisely(void)
+{
+	asm volatile ("\
+	r6 = *(u32*)(r1 + 0);\
+	r6 &= 1;\
+	r0 = 0;\
+	if r6 != 0 goto l0_%=;\
+	*(u64*)(r10 - 8) = r0;\
+	goto l1_%=;\
+l0_%=: r0 = 1;\
+	*(u64*)(r10 - 8) = r0;\
+l1_%=: r0 = 0;\
+	r2 = *(u32*)(r1 + 4);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = 0;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
 SEC("cgroup/skb")
 __description("variable-offset stack read, uninitialized")
 __success
Signature · 1 line
2.54.0
maintainer Reviewed-by on v2 2/2
#8 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover var-offset stack reads from zero spills

Quoted 9 lines · Woojin Ji

Add verifier_var_off coverage for variable-offset stack reads from spilled scalar constant zero values.

Cover single-slot and cross-slot spilled zero reads, a sub-8-byte spill with neighbouring STACK_ZERO bytes, and a sub-8-byte spill with neighbouring STACK_MISC bytes that must not be treated as zero. Add a state-pruning negative test to ensure the verifier marks the spilled zero stack slot precise before using it to derive a const-zero register.

Quoted 5 lines

Use verifier log assertions to check both the zero result and the precision backtracking trail.

Assisted-by opencode:gpt-5.5

Could you please fix a few nits below?
pw-bot: cr.

Quoted 9 lines
---
 .../testing/selftests/bpf/progs/verifier_var_off.c | 148 +++++++++++++++++++++
 1 file changed, 148 insertions(+)
tools/testing/selftests/bpf/progs/verifier_var_off.c +0 -0
index f345466bc..f0052d831 100644

tools/testing/selftests/bpf/progs/verifier_var_off.c __naked void stack_read_priv_vs_unpriv(void) · line 68

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_var_off.c +7 -0
@@ -59,6 +59,154 @@ __naked void stack_read_priv_vs_unpriv(void)
 "	::: __clobber_all);
 }
 
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves spilled zero")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")

Nit: can drop the __failure_unpriv check

tools/testing/selftests/bpf/progs/verifier_var_off.c __naked void stack_read_priv_vs_unpriv(void) · line 72

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_var_off.c +4 -0
+__retval(0)
+__naked void stack_read_var_off_preserves_spilled_zero(void)
+{
+	asm volatile ("\

^^^ please keep these similar to other tests in the file.

Quoted 102 lines
tools/testing/selftests/bpf/progs/verifier_var_off.c +102 -0
+	r0 = 0;\
+	*(u64*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves spilled zero across slots")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8,-16")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__retval(0)
+__naked void stack_read_var_off_preserves_spilled_zero_across_slots(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u64*)(r10 - 8) = r0;\
+	*(u64*)(r10 - 16) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 15;\
+	r2 -= 16;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read preserves partial spilled zero")
+__success
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("R3=0")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__retval(0)
+__naked void stack_read_var_off_preserves_partial_spilled_zero(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u8*)(r10 - 9) = r0;\
+	*(u8*)(r10 - 10) = r0;\
+	*(u8*)(r10 - 11) = r0;\
+	*(u8*)(r10 - 12) = r0;\
+	*(u8*)(r10 - 13) = r0;\
+	*(u8*)(r10 - 14) = r0;\
+	*(u8*)(r10 - 15) = r0;\
+	*(u32*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 15;\
+	if r2 > 10 goto l0_%=;\
+	r2 -= 15;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = r3;\
+l0_%=: r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
+SEC("cgroup/skb")
+__description("variable-offset stack read partial spill with misc data")
+__failure
+__msg("invalid variable-offset write to stack R1")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__naked void stack_read_var_off_partial_spill_with_misc_data(void)
+{
+	asm volatile ("\
+	r0 = 0;\
+	*(u32*)(r10 - 8) = r0;\
+	r2 = *(u32*)(r1 + 0);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = 0;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}

tools/testing/selftests/bpf/progs/verifier_var_off.c __naked void stack_read_priv_vs_unpriv(void) · line 184

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_var_off.c +10 -0
+
+SEC("cgroup/skb")
+__description("variable-offset stack read tracks spilled zero precisely")
+__failure
+__flag(BPF_F_TEST_STATE_FREQ)
+__log_level(2)
+__msg("mark_precise: frame0: regs= stack=-8")
+__msg("invalid variable-offset write to stack R1")
+__failure_unpriv __msg_unpriv("R2 variable stack access prohibited for !root")
+__naked void stack_read_var_off_tracks_spilled_zero_precisely(void)

I don't think this test is necessary, tbh.

Quoted 28 lines
tools/testing/selftests/bpf/progs/verifier_var_off.c +25 -0
+{
+	asm volatile ("\
+	r6 = *(u32*)(r1 + 0);\
+	r6 &= 1;\
+	r0 = 0;\
+	if r6 != 0 goto l0_%=;\
+	*(u64*)(r10 - 8) = r0;\
+	goto l1_%=;\
+l0_%=: r0 = 1;\
+	*(u64*)(r10 - 8) = r0;\
+l1_%=: r0 = 0;\
+	r2 = *(u32*)(r1 + 4);\
+	r2 &= 7;\
+	r2 -= 8;\
+	r2 += r10;\
+	r3 = *(u8*)(r2 + 0);\
+	r1 = r10;\
+	r1 += -1;\
+	r1 += r3;\
+	*(u8*)(r1 + 0) = 0;\
+	r0 = 0;\
+	exit;\
+"	::: __clobber_all);
+}
+
 SEC("cgroup/skb")
 __description("variable-offset stack read, uninitialized")
 __success
author
#9 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover var-offset stack reads from zero spills

Thanks, fixed these in v3.

I dropped the extra unpriv checks from the new tests, matched the existing asm formatting in the file, and removed the pruning-sensitive test.

I also added fixed-offset mixed zero/spill-zero coverage after Alexei's comment on patch 1.

Thanks,
Woojin

reviewer
#10 lore raw
In reply to Woojin Ji
Quoted 7 lines · Woojin Ji

Variable-offset stack reads currently lose the known-zero fact when the loaded byte comes from a spilled scalar constant zero rather than from a STACK_ZERO byte. This series teaches the var-offset stack read path to preserve that zero fact while marking the contributing spill slots precise, and adds verifier_var_off coverage for the new behaviour and the pruning-sensitive negative case.

Quoted 5 lines

I don't have a confirmed deployed-program regression, so this is targeted at bpf-next. I did confirm the pattern is reachable from normal C codegen with a small helper-based BPF C reproducer: clang 22.1.6 -O2/-O3 can produce a spilled scalar-zero plus variable-offset stack byte load pattern that the unpatched verifier rejects and the patched verifier accepts.

Could you share your C code to desmonstrate patch 1? We can then check whether the pattern is common and whether the code can be easliy worked around with source code (e.g. barrier_var() etc.).

Quoted 41 lines

Changes in v2:
- Rebased onto bpf-next.
- Split verifier and selftests changes into separate patches.
- Added bpf_bt_set_frame_slot_mask() instead of open-coding a slot loop.
- Kept the new eager precision marking on the variable-offset read path.
- Added verifier log assertions for the zero result and mark_precise trail.
- Added sub-8-byte spill coverage with STACK_ZERO and STACK_MISC neighbours.
- Cleaned up inline asm formatting and local labels.

Tested with:
- make O=../../out/kernel olddefconfig
- make O=../../out/kernel -j$(nproc) kernel/bpf/verifier.o
- make O=../../out/kernel LLVM=1 -j$(nproc) bzImage
- ./test_progs -t verifier_var_off -v
Summary: 1/30 PASSED, 0 SKIPPED, 0 FAILED
- ./test_progs -t verifier_spill_fill -t verifier_live_stack \
-t verifier_search_pruning -v
Summary: 3/127 PASSED, 0 SKIPPED, 0 FAILED
- veristat -o csv verifier_var_off.bpf.o

Assisted-by opencode:gpt-5.5

---
Woojin Ji (2):
bpf: Preserve scalar zero spills for var-offset stack reads selftests/bpf: Cover var-offset stack reads from zero spills

  include/linux/bpf_verifier.h                       |   5 +
  kernel/bpf/verifier.c                              |  55 ++++++--
  .../testing/selftests/bpf/progs/verifier_var_off.c | 148 +++++++++++++++++++++
  3 files changed, 198 insertions(+), 10 deletions(-)

---
base-commit: 7bfb93e3475be9de894f1cecd3a727d3e1649b03
change-id: 20260610-bpf-stack-var-off-zero-v1-34ad1bc3b533

Best regards,
--
Woojin Ji <[email protected]>

author
#11 lore raw
Quoted 3 lines · Yonghong Song

Could you share your C code to desmonstrate patch 1? We can then check whether the pattern is common and whether the code can be easliy worked around with source code (e.g. barrier_var() etc.).

Sure. I don't have a confirmed production program hitting this, and I don't want to claim that the pattern is common. The reason I mentioned it in the cover letter is that it is reachable from normal C codegen, not only from hand-written verifier asm.

This is the minimized single-slot reproducer I used:

  #include <linux/bpf.h>
  #include <bpf/bpf_helpers.h>

  struct {
          __uint(type, BPF_MAP_TYPE_HASH);
          __uint(max_entries, 1);
          __type(key, __u64);
          __type(value, __u8);
  } single_some_map SEC(".maps");

  struct {
          __uint(type, BPF_MAP_TYPE_HASH);
          __uint(max_entries, 1);
          __type(key, __u32);
          __type(value, __u8);
  } single_one_byte_map SEC(".maps");

  SEC("tc")
  int helper_zero_single(struct __sk_buff *skb)
  {
          __u64 key = 0;
          __u32 out_key = 0;
          __u32 idx = skb->mark;
          __u8 *out;
          __u8 value;
          if (idx > 7)
                  return 0;

          out = bpf_map_lookup_elem(&single_one_byte_map, &out_key);
          if (!out)
                  return 0;

          bpf_map_lookup_elem(&single_some_map, &key);
          value = ((__u8 *)&key)[idx];
          out[value] = value;
          return 0;

}

  char LICENSE[] SEC("license") = "GPL";

With clang 22.1.6, both -O2 and -O3 produce the relevant pattern:

*(u64 *)(r10 - 8) = r2
...
call 1
r7 += r8
w1 = *(u8 *)(r7 + 0)
r6 += r1
*(u8 *)(r6 + 0) = w1

The unpatched verifier rejects the object because the byte loaded from the spilled-zero key becomes var_off=(0x0; 0xff), so the following one-byte map-value write is considered possibly out of bounds. With the patch, the load is kept as const zero and the program is accepted.

I also used a two-slot variant:

  __u64 key[2] = {};
  ...
  if (idx > 15)
          return 0;
  ...
  bpf_map_lookup_elem(&single_some_map, &key[0]);
  value = ((__u8 *)key)[idx];
  out[value] = value;

That produces the same kind of variable-offset byte load over two spilled scalar-zero slots and shows the same base reject / patched accept behavior.

I agree that this may be possible to work around at the C source level, depending on the program, for example by avoiding the variable byte read from the stack key or by otherwise forcing different codegen. That is why I kept this as bpf-next and did not claim a production regression.

Thanks,
Woojin