bpf: verifier: fix integer wrap in regs_refine_cond_op() BPF_JNE case
PATCH v3 1/1 bpf: verifier: fix integer wrap in regs_refine_cond_op() BPF_JNE case
From: Ibrahim Zein <[email protected]>
In regs_refine_cond_op(), the BPF_JNE case narrows register bounds without guarding against wrap-around at type boundaries. When umin_value equals U64_MAX, incrementing it wraps to 0, producing an inconsistent bounds state (umin=0, umax=U64_MAX-1).
This can be triggered by constructing a register state where:
r1 = unknown_byte | 0xFFFFFFFFFFFFFFFE
-> var_off = (value=U64_MAX-1, mask=1)
-> constrain with JGE U64_MAX -> umin=umax=U64_MAX, tnum non-const -> compare with BPF_JNE const=U64_MAX -> wrap occurs
The comment above these checks incorrectly claims this boundary condition is impossible. However, is_reg_const() uses tnum_is_const() which requires mask == 0, so a register can have umin == umax while tnum is non-const (mask != 0). Remove the misleading comment and add explicit boundary guards to prevent wrap-around in all {u,s}{min,max}{32,64} variants.
---
kernel/bpf/verifier.c +16 -17
@@ -17046,31 +17046,30 @@*/ val =reg_const_value (reg2, is_jmp32);if (is_jmp32) { -/* u32_min_value is not equal to 0xffffffff at this point, -* because otherwise u32_max_value is 0xffffffff as well, -* in such a case both reg1 and reg2 would be constants, -* jump would be predicted and reg_set_min_max() won't -* be called. -* -* Same reasoning works for all {u,s}{min,max}{32,64} cases -* below. -*/ -if (reg1->u32_min_value == (u32 )val) +if (reg1->u32_min_value == (u32 )val && + reg1->u32_min_value !=U32_MAX ) reg1->u32_min_value++; -if (reg1->u32_max_value == (u32 )val) +if (reg1->u32_max_value == (u32 )val && + reg1->u32_max_value !=0 ) reg1->u32_max_value--; -if (reg1->s32_min_value == (s32 )val) +if (reg1->s32_min_value == (s32 )val && + reg1->s32_min_value !=S32_MAX ) reg1->s32_min_value++; -if (reg1->s32_max_value == (s32 )val) +if (reg1->s32_max_value == (s32 )val && + reg1->s32_max_value !=S32_MIN ) reg1->s32_max_value--; }else { -if (reg1->umin_value == (u64 )val) +if (reg1->umin_value == (u64 )val && + reg1->umin_value !=U64_MAX ) reg1->umin_value++; -if (reg1->umax_value == (u64 )val) +if (reg1->umax_value == (u64 )val && + reg1->umax_value !=0 ) reg1->umax_value--; -if (reg1->smin_value == (s64 )val) +if (reg1->smin_value == (s64 )val && + reg1->smin_value !=S64_MAX ) reg1->smin_value++; -if (reg1->smax_value == (s64 )val) +if (reg1->smax_value == (s64 )val && + reg1->smax_value !=S64_MIN ) reg1->smax_value--; }break ;
[email protected]
AI reviewWhen umin == umax == val and val == U64_MAX, the umin guard prevents the increment (U64_MAX == U64_MAX), but the umax guard allows the decrement (U64_MAX != 0). This leaves umin_value at U64_MAX and…
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 1 line
val = reg_const_value(reg2, is_jmp32);
[ ... ]
Quoted 7 lines
} else {kernel/bpf/verifier.c +2
-0+if (reg1->umin_value == (u64 )val && + reg1->umin_value !=U64_MAX )reg1->umin_value++;kernel/bpf/verifier.c +2
-0+if (reg1->umax_value == (u64 )val && + reg1->umax_value !=0 )reg1->umax_value--;
When umin == umax == val and val == U64_MAX, the umin guard prevents the increment (U64_MAX == U64_MAX), but the umax guard allows the decrement (U64_MAX != 0). This leaves umin_value at U64_MAX and umax_value at U64_MAX-1, creating an inverted range.
This state survives reg_bounds_sync() because__update_reg64_bounds() recomputes:
umin = max(umin, var_off.value) -> still U64_MAX
umax = min(umax, var_off.value | var_off.mask) -> still U64_MAX-1and then reg_bounds_sanity_check() catches umin > umax, triggering verifier_bug() which calls BPF_WARN_ONCE().
The scenario from the commit message (var_off with mask=1 after JGE U64_MAX) reaches this path because is_scalar_branch_taken() returns -1 for BPF_JNE when tnum overlaps but is not const, so both branches are explored.
The same pattern applies symmetrically when val == 0 (umax guard fires, umin proceeds), and to all the s32/s64/u32 variants at their respective type boundaries.
Would it make sense to handle this dead-branch case before the individual adjustments? The BPF_JSET | BPF_X case in the same function already does this:
/* Forget the ranges before narrowing tnums, to avoid invariant
* violations if we're on a dead branch.
*/
__mark_reg_unbounded(reg1);A similar approach here, or skipping adjustments entirely when umin == umax == val, would avoid the verifier_bug() trigger on what is an architecturally expected dead branch.
Quoted 8 lines
kernel/bpf/verifier.c +2
-0+if (reg1->smin_value == (s64 )val && + reg1->smin_value !=S64_MAX )reg1->smin_value++;kernel/bpf/verifier.c +2
-0+if (reg1->smax_value == (s64 )val && + reg1->smax_value !=S64_MIN )reg1->smax_value--; } break;
---
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/23223625211
Quoted 21 lines · Ibrahim Zein
From: Ibrahim Zein <[email protected]>
In
regs_refine_cond_op(), theBPF_JNEcase narrows register bounds without guarding against wrap-around at type boundaries. Whenumin_valueequalsU64_MAX, incrementing it wraps to 0, producing an inconsistent bounds state (umin=0, umax=U64_MAX-1).This can be triggered by constructing a register state where:
r1 =unknown_byte|0xFFFFFFFFFFFFFFFE
->var_off= (value=U64_MAX-1, mask=1)
-> constrain with JGEU64_MAX-> umin=umax=U64_MAX, tnum non-const -> compare withBPF_JNEconst=U64_MAX -> wrap occursThe comment above these checks incorrectly claims this boundary condition is impossible. However,
is_reg_const()usestnum_is_const()which requires mask == 0, so a register can have umin == umax while tnum is non-const (mask != 0). Remove the misleading comment and add explicit boundary guards to prevent wrap-around in all {u,s}{min,max}{32,64} variants.
Quoted 3 lines
Fixes d028f87517d6 ("bpf: make the verifier tracks the "not equal" for regs")Reported-by Ibrahim Zein <[email protected]>Signed-off-by Ibrahim Zein <[email protected]>
Please stop resending incorrect patch.
Instead please create a selftest that demonstrates the issue.
On 2026/3/18 08:41 Ibrahim Zein <[email protected]> write:
Quoted 13 lines
From: Ibrahim Zein <[email protected]>
In
regs_refine_cond_op(), theBPF_JNEcase narrows register bounds without guarding against wrap-around at type boundaries. Whenumin_valueequalsU64_MAX, incrementing it wraps to 0, producing an inconsistent bounds state (umin=0, umax=U64_MAX-1).This can be triggered by constructing a register state where:
r1 =unknown_byte|0xFFFFFFFFFFFFFFFE
->var_off= (value=U64_MAX-1, mask=1)
-> constrain with JGEU64_MAX-> umin=umax=U64_MAX, tnum non-const -> compare withBPF_JNEconst=U64_MAX -> wrap occurs
Quoted 6 lines
The comment above these checks incorrectly claims this boundary condition is impossible. However,
is_reg_const()usestnum_is_const()which requires mask == 0, so a register can have umin == umax while tnum is non-const (mask != 0). Remove the misleading comment and add explicit boundary guards to prevent wrap-around in all {u,s}{min,max}{32,64} variants.
Hi, Ibrahim. As Alexei said, your patch is incorrect, and you should generate it with:
git format-patch --subject-prefix="PATCH bpf-next" -1
How did you find this issue? By AI? I ask this because I don't think this is a issue that can be triggered. As you said, the key problem is that r1 is "umin=umax=U64_MAX, tnum non-const", so it's a known value. Therefore, the return value of is_branch_taken() should always be 0 or 1 in check_cond_jmp_op, as r2 is a const in this case. Then, reg_set_min_max() won't be called, as we don't need to adjust the min/max for a known register. Right?
Thanks!
Menglong Dong
Quoted 57 lines
Fixes d028f87517d6 ("bpf: make the verifier tracks the "not equal" for regs")Reported-by Ibrahim Zein <[email protected]>Signed-off-by Ibrahim Zein <[email protected]>---
kernel/bpf/verifier.c +16
-17@@ -17046,31 +17046,30 @@*/ val =reg_const_value (reg2, is_jmp32);if (is_jmp32) { -/* u32_min_value is not equal to 0xffffffff at this point, -* because otherwise u32_max_value is 0xffffffff as well, -* in such a case both reg1 and reg2 would be constants, -* jump would be predicted and reg_set_min_max() won't -* be called. -* -* Same reasoning works for all {u,s}{min,max}{32,64} cases -* below. -*/ -if (reg1->u32_min_value == (u32 )val) +if (reg1->u32_min_value == (u32 )val && + reg1->u32_min_value !=U32_MAX ) reg1->u32_min_value++; -if (reg1->u32_max_value == (u32 )val) +if (reg1->u32_max_value == (u32 )val && + reg1->u32_max_value !=0 ) reg1->u32_max_value--; -if (reg1->s32_min_value == (s32 )val) +if (reg1->s32_min_value == (s32 )val && + reg1->s32_min_value !=S32_MAX ) reg1->s32_min_value++; -if (reg1->s32_max_value == (s32 )val) +if (reg1->s32_max_value == (s32 )val && + reg1->s32_max_value !=S32_MIN ) reg1->s32_max_value--; }else { -if (reg1->umin_value == (u64 )val) +if (reg1->umin_value == (u64 )val && + reg1->umin_value !=U64_MAX ) reg1->umin_value++; -if (reg1->umax_value == (u64 )val) +if (reg1->umax_value == (u64 )val && + reg1->umax_value !=0 ) reg1->umax_value--; -if (reg1->smin_value == (s64 )val) +if (reg1->smin_value == (s64 )val && + reg1->smin_value !=S64_MAX ) reg1->smin_value++; -if (reg1->smax_value == (s64 )val) +if (reg1->smax_value == (s64 )val && + reg1->smax_value !=S64_MIN ) reg1->smax_value--; }break ;