Fix stale register bounds on LSM retval context load
Subsystems (5)
v3 cover Fix stale register bounds on LSM retval context load
From: Tristan Madani <[email protected]>
check_mem_access() calls __mark_reg_s32_range() to narrow a register to the LSM hook retval range, but the intersection preserves stale bounds from prior instructions. Add mark_reg_unknown() before narrowing (same pattern as the else branch) and a selftest that catches the mismatch.
Changes in v3:
- Add selftest demonstrating the issue (Eduard Zingerman)
- No code change in patch 1 from v2
Tristan Madani (2):
bpf: Reset register bounds before narrowing retval range in
check_mem_access()
selftests/bpf: Add test for stale bounds on LSM retval context load
kernel/bpf/verifier.c | 1 + tools/testing/selftests/bpf/progs/verifier_lsm.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+)
Signature · 1 line
2.47.3
PATCH v3 1/2 bpf: Reset register bounds before narrowing retval range in check_mem_access()
From: Tristan Madani <[email protected]>
When the BPF verifier processes a context load of an LSM hook return value, it calls __mark_reg_s32_range() to narrow the register to the hook's valid range. However, __mark_reg_s32_range() intersects the new range with the register's existing bounds using max_t()/min_t() rather than replacing them.
If the destination register carries stale bounds from a prior instruction (e.g. BPF_MOV64_IMM), the intersection can produce a range narrower than reality. The verifier then believes it knows the register's exact value, while at runtime the actual hook return value is loaded, creating a verifier/runtime mismatch that can be used to bypass BPF memory safety checks.
The else branch already calls mark_reg_unknown() to reset register state before any narrowing. Apply the same reset in the is_retval path so stale bounds are cleared before __mark_reg_s32_range() intersects.
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
@@ -6201,6 +6201,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b*/ if (info.reg_type ==SCALAR_VALUE ) {if (info.is_retval &&get_func_retval_range (env->prog, &range)) { +mark_reg_unknown (env, regs, value_regno); err =__mark_reg_s32_range (env, regs, value_regno, range.minval, range.maxval);if (err)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH bpf v3 1/2] bpf: Reset register bounds before narrowing retval range in check_mem_access()
In the v2 review thread (https://lore.kernel.org/bpf/[email protected]/), Eduard Zingerman requested that a selftest be added to demonstrate the stale bounds…
kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b · line 6207
Quoted 12 lines
kernel/bpf/verifier.c +1
-0@@ -6201,6 +6201,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b*/ if (info.reg_type ==SCALAR_VALUE ) {if (info.is_retval &&get_func_retval_range (env->prog, &range)) { +mark_reg_unknown (env, regs, value_regno); err =__mark_reg_s32_range (env, regs, value_regno, range.minval, range.maxval);if (err)
In the v2 review thread
(https://lore.kernel.org/bpf/[email protected]/),
Eduard Zingerman requested that a selftest be added to demonstrate the stale bounds issue. While the v3 cover letter claims the selftest was added as patch 2/2, the selftest is missing from the current commit being reviewed.
The suggested test should verify that loading an LSM hook return value into a register with stale bounds (e.g., from r6 = 0) properly resets the bounds, preventing the verifier from incorrectly pruning branches based on assumed constant values.
Could the selftest be added as described in the v2 review feedback?
---
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/27990633425
Re: [PATCH bpf v3 1/2] bpf: Reset register bounds before narrowing retval range in check_mem_access()
In reply to Tristan MadaniQuoted 15 lines · Tristan Madani
From: Tristan Madani <[email protected]>
When the BPF verifier processes a context load of an LSM hook return value, it calls
__mark_reg_s32_range()to narrow the register to the hook's valid range. However,__mark_reg_s32_range()intersects the new range with the register's existing bounds using max_t()/min_t() rather than replacing them.If the destination register carries stale bounds from a prior instruction (e.g.
BPF_MOV64_IMM), the intersection can produce a range narrower than reality. The verifier then believes it knows the register's exact value, while at runtime the actual hook return value is loaded, creating a verifier/runtime mismatch that can be used to bypass BPF memory safety checks.
Quoted 8 lines
The else branch already calls
mark_reg_unknown()to reset register state before any narrowing. Apply the same reset in theis_retvalpath so stale bounds are cleared before__mark_reg_s32_range()intersects.Fixes 5d99e198be27 ("bpf, lsm: Add check for BPF LSM return value")Signed-off-by Tristan Madani <[email protected]>---
[...]
PATCH v3 2/2 selftests/bpf: Add test for stale bounds on LSM retval context load
In reply to Tristan MadaniFrom: Tristan Madani <[email protected]>
Add a verifier test that catches the stale-bounds issue fixed in the previous patch. The test sets r6 = 0 to create known bounds, then loads the LSM hook return value into r6 from the context. Without the fix, the verifier intersects the retval range with the stale bounds and incorrectly narrows r6 to a single value, pruning the fall-through branch as dead code and missing the div-by-zero.
--- tools/testing/selftests/bpf/progs/verifier_lsm.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/progs/verifier_lsm.c +15 -0
@@ -197,4 +197,19 @@ int BPF_PROG(sleepable_lsm_cgroup)return 0 ; } +SEC("lsm/file_mprotect") +__description("lsm retval load must reset stale register bounds") +__failure __msg("div by zero") +__naked int retval_load_resets_bounds(void *ctx) +{ + asmvolatile ( +"r6 = 0;" +"r6 = *(u64 *)(r1 + 24);" +"if r6 == 0 goto +1;" +"r6 /= 0;" +"r0 = 0;" +"exit;" + ::: __clobber_all); +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.3
Re: [PATCH bpf v3 2/2] selftests/bpf: Add test for stale bounds on LSM retval context load
Quoted 9 lines · Tristan Madani
From: Tristan Madani <[email protected]>
Add a verifier test that catches the stale-bounds issue fixed in the previous patch. The test sets r6 = 0 to create known bounds, then loads the LSM hook return value into r6 from the context. Without the fix, the verifier intersects the retval range with the stale bounds and incorrectly narrows r6 to a single value, pruning the fall-through branch as dead code and missing the div-by-zero.
Quoted 3 lines
Suggested-by Eduard Zingerman <[email protected]>Signed-off-by Tristan Madani <[email protected]>---
[...]
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Mon, 22 Jun 2026 23:01:21 +0000 you wrote:
From: Tristan Madani <[email protected]>
check_mem_access()calls__mark_reg_s32_range()to narrow a register to the LSM hook retval range, but the intersection preserves stale bounds from prior instructions. Addmark_reg_unknown()before narrowing (same pattern as the else branch) and a selftest that catches the mismatch.[...]
Here is the summary with links:
- [bpf,v3,1/2] bpf: Reset register bounds before narrowing retval range in check_mem_access()
https://git.kernel.org/bpf/bpf/c/5e0b273e0a62 - [bpf,v3,2/2] selftests/bpf: Add test for stale bounds on LSM retval context load https://git.kernel.org/bpf/bpf/c/644332f48fc2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html