Skip to contentSkip to conversation

bpf: verifier: fix integer wrap in regs_refine_cond_op() BPF_JNE case

PATCH Superseded Ibrahim Zein 2 participants 2 messages Mar 17

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: verifier: fix integer wrap in regs_refine_cond_op() BPF_JNE case

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 same issue exists for all {u,s}{min,max}{32,64} variants. Add explicit boundary guards to prevent wrap-around in all cases.

---

kernel/bpf/verifier.c +16 -8
@@ -17055,22 +17055,30 @@
 			 * 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;
#1 lore raw
The comment above this code (not shown in the diff hunk) says:

kernel/bpf/verifier.c static void regs_refine_cond_op(struct bpf_reg_state *reg1, struct bpf_reg_state · line 17060

Quoted 12 lines
kernel/bpf/verifier.c +2 -1
index 01c18f4268de..2f18cc3e1a78 100644
@@ -17055,22 +17055,30 @@ static void regs_refine_cond_op(struct bpf_reg_state *reg1, struct bpf_reg_state
 			 * 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++;

The comment above this code (not shown in the diff hunk) says:

/* 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.

This reasoning assumes that umin == umax implies the register is constant, but is_reg_const() checks tnum_is_const() which requires mask == 0. As the commit message itself demonstrates, tnum can disagree with bounds (tnum non-const while umin == umax == U64_MAX), so the comment's claim is incorrect.

Should this comment be updated or removed? As written, it says the boundary condition handled by the new guards is impossible, which directly contradicts the guards and could mislead a future reader into removing them.

[ ... ]

This commit appears to fix a verifier bounds tracking bug that could allow BPF programs to bypass safety checks. Should it carry a Fixes: tag? The wrap-around issue was introduced in:

Fixes d028f87517d6 ("bpf: make the verifier tracks the "not equal" for regs")

---
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/23218241458