Skip to contentSkip to conversation

bpf: Reset register bounds before narrowing retval range in check_mem_access()

PATCH Superseded Tristan Madani 2 participants 2 messages Jun 19

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

Fixes 5d99e198be27 ("bpf, lsm: Add check for BPF LSM return value")

---
Changes in v2:
- Use [PATCH bpf] subject prefix per Jiri Olsa review
- Rebased on bpf/master
- No code change from v1

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
index 2abc79dbf281..6de9af4115dd 100644
@@ -6196,6 +6196,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
maintainer
#1 lore raw
Quoted 36 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.

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.

Fixes 5d99e198be27 ("bpf, lsm: Add check for BPF LSM return value")

---
Changes in v2:
- Use [PATCH bpf] subject prefix per Jiri Olsa review
- Rebased on bpf/master
- No code change from v1

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +0 -0
index 2abc79dbf281..6de9af4115dd 100644

kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b · line 6199

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -6196,6 +6196,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);

This seem to be a real bug, could you please add a selftest demonstrating the issue and post v3?
Something like this in the tools/testing/selftests/bpf/progs/verifier_lsm.c:

/* lsm bpf prog retval load must reset stale register bounds */ SEC("lsm/file_mprotect")
__failure __msg("div by zero")
__naked int retval_load_resets_bounds(void)

{
        asm volatile (
        "r6 = 0;"
        "r6 = *(u64 *)(r1 + 24);"	/* return value of the LSM_HOOK(...file_mprotect...) */
        "if r6 == 0 goto +1;"           /* r6 value should not be predictable here */
        "r6 /= 0;"
        "r0 = 0;"
        "exit;"
        ::: __clobber_all);
}
Quoted 3 lines
kernel/bpf/verifier.c +0 -0
 					err = __mark_reg_s32_range(env, regs, value_regno,
 								   range.minval, range.maxval);
 					if (err)