Skip to content

Commit 6e951a9

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-improve-linked-register-tracking'
Puranjay Mohan says: ==================== bpf: Improve linked register tracking V3: https://lore.kernel.org/all/20260203222643.994713-1-puranjay@kernel.org/ Changes in v3->v4: - Add a call to reg_bounds_sync() in sync_linked_regs() to sync bounds after alu op. - Add a __sink(path[0]); in the C selftest so compiler doesn't say "error: variable 'path' set but not used" V2: https://lore.kernel.org/all/20260113152529.3217648-1-puranjay@kernel.org/ Changes in v2->v3: - Added another selftest showing a real usage pattern - Rebased on bpf-next/master v1: https://lore.kernel.org/bpf/20260107203941.1063754-1-puranjay@kernel.org/ Changes in v1->v2: - Add support for alu32 operations in linked register tracking (Alexei) - Squash the selftest fix with the first patch (Eduard) - Add more selftests to detect edge cases This series extends the BPF verifier's linked register tracking to handle negative offsets, BPF_SUB operations, and alu32 operations, enabling better bounds propagation for common arithmetic patterns. The verifier previously only tracked positive constant deltas between linked registers using BPF_ADD. This meant patterns using negative offsets or subtraction couldn't benefit from bounds propagation: void alu32_negative_offset(void) { volatile char path[5]; volatile int offset = bpf_get_prandom_u32(); int off = offset; if (off >= 5 && off < 10) path[off - 5] = '.'; } this gets compiled to: 0000000000000478 <alu32_negative_offset>: 143: call 0x7 144: *(u32 *)(r10 - 0xc) = w0 145: w1 = *(u32 *)(r10 - 0xc) 146: w2 = w1 // w2 and w1 share the same id 147: w2 += -0x5 // verifier knows w1 = w2 + 5 148: if w2 > 0x4 goto +0x5 <L0> // in fall-through: verifier knows w2 ∈ [0,4] => w1 ∈ [5, 9] 149: r2 = r10 150: r2 += -0x5 // r2 = fp - 5 151: r2 += r1 // r2 = fp - 5 + r1 (∈ [5, 9]) => r2 ∈ [fp, fp + 4] 152: w1 = 0x2e 153: *(u8 *)(r2 - 0x5) = w1 // r2 ∈ [fp, fp + 4] => r2 - 5 ∈ [fp - 5, fp - 1] <L0>: 154: exit After the changes, the verifier could link 32-bit scalars and also supported -ve offsets for linking: 146: w2 = w1 147: w2 += -0x5 It allowed the verifier to correctly propagate bounds, without the changes in this patchset, verifier would reject this program with: invalid unbounded variable-offset write to stack R2 This program has been added as a selftest in the second patch. Veristat comparison on programs from sched_ext, selftests, and some meta internal programs: Scx Progs File Program Verdict (A) Verdict (B) Verdict (DIFF) Insns (A) Insns (B) Insns (DIFF) ----------------- ---------------- ----------- ----------- -------------- --------- --------- ------------- scx_layered.bpf.o layered_runnable success success MATCH 5674 6077 +403 (+7.10%) FB Progs File Program Verdict (A) Verdict (B) Verdict (DIFF) Insns (A) Insns (B) Insns (DIFF) ------------ ---------------- ----------- ----------- -------------- --------- --------- ----------------- bpf232.bpf.o layered_dump success success MATCH 1151 1218 +67 (+5.82%) bpf257.bpf.o layered_runnable success success MATCH 5743 6143 +400 (+6.97%) bpf252.bpf.o layered_runnable success success MATCH 5677 6075 +398 (+7.01%) bpf227.bpf.o layered_dump success success MATCH 915 982 +67 (+7.32%) bpf239.bpf.o layered_runnable success success MATCH 5459 5861 +402 (+7.36%) bpf246.bpf.o layered_runnable success success MATCH 5562 6008 +446 (+8.02%) bpf229.bpf.o layered_runnable success success MATCH 2559 3011 +452 (+17.66%) bpf231.bpf.o layered_runnable success success MATCH 2559 3011 +452 (+17.66%) bpf234.bpf.o layered_runnable success success MATCH 2549 3001 +452 (+17.73%) bpf019.bpf.o do_sendmsg success success MATCH 124823 153523 +28700 (+22.99%) bpf019.bpf.o do_parse success success MATCH 124809 153509 +28700 (+23.00%) bpf227.bpf.o layered_runnable success success MATCH 1915 2356 +441 (+23.03%) bpf228.bpf.o layered_runnable success success MATCH 1700 2152 +452 (+26.59%) bpf232.bpf.o layered_runnable success success MATCH 1499 1951 +452 (+30.15%) bpf312.bpf.o mount_exit success success MATCH 19253 62883 +43630 (+226.61%) bpf312.bpf.o umount_exit success success MATCH 19253 62883 +43630 (+226.61%) bpf311.bpf.o mount_exit success success MATCH 19226 62863 +43637 (+226.97%) bpf311.bpf.o umount_exit success success MATCH 19226 62863 +43637 (+226.97%) The above four programs have specific patters that make the verifier explore a lot more states: for (; depth < MAX_DIR_DEPTH; depth++) { const unsigned char* name = BPF_CORE_READ(dentry, d_name.name); if (offset >= MAX_PATH_LEN - MAX_DIR_LEN) { return depth; } int len = bpf_probe_read_kernel_str(&path[offset], MAX_DIR_LEN, name); offset += len; if (len == MAX_DIR_LEN) { if (offset - 2 < MAX_PATH_LEN) { // <---- (a) path[offset - 2] = '.'; } if (offset - 3 < MAX_PATH_LEN) { // <---- (b) path[offset - 3] = '.'; } if (offset - 4 < MAX_PATH_LEN) { // <---- (c) path[offset - 4] = '.'; } } } When at some depth == N false branches of conditions (a), (b) and (c) are scheduled for verification, constraints for offset at depth == N+1 are: 1. offset >= MAX_PATH_LEN + 2 2. offset >= MAX_PATH_LEN + 3 3. offset >= MAX_PATH_LEN + 4 (visited before others) And after offset += len it becomes: 1. offset >= MAX_PATH_LEN - 4093 2. offset >= MAX_PATH_LEN - 4092 3. offset >= MAX_PATH_LEN - 4091 (visited before others) Because of the DFS states exploration logic, the states above are visited in order 3, 2, 1; 3 is not a subset of 2 and 1 is not a subset of 2, so pruning logic does not kick in. Previously this was not a problem, because range for offset was not propagated through the statements (a), (b), (c). As the root cause of this regression is understood, this is not a blocker for this change. Selftest Progs File Program Verdict (A) Verdict (B) Verdict (DIFF) Insns (A) Insns (B) Insns (DIFF) ---------------------------------- ------------------------ ----------- ----------- -------------- --------- --------- -------------- linked_list_peek.bpf.o list_peek success success MATCH 152 88 -64 (-42.11%) verifier_iterating_callbacks.bpf.o cond_break2 success success MATCH 110 88 -22 (-20.00%) These are the added selftests that failed earlier but are passing now: verifier_linked_scalars.bpf.o alu32_negative_offset failure success MISMATCH 11 13 +2 (+18.18%) verifier_linked_scalars.bpf.o scalars_alu32_big_offset failure success MISMATCH 7 10 +3 (+42.86%) verifier_linked_scalars.bpf.o scalars_neg_alu32_add failure success MISMATCH 7 10 +3 (+42.86%) verifier_linked_scalars.bpf.o scalars_neg_alu32_sub failure success MISMATCH 7 10 +3 (+42.86%) verifier_linked_scalars.bpf.o scalars_neg failure success MISMATCH 7 10 +3 (+42.86%) verifier_linked_scalars.bpf.o scalars_neg_sub failure success MISMATCH 7 10 +3 (+42.86%) verifier_linked_scalars.bpf.o scalars_sub_neg_imm failure success MISMATCH 7 10 +3 (+42.86%) iters.bpf.o iter_obfuscate_counter success success MATCH 83 119 +36 (+43.37%) bpf_cubic.bpf.o bpf_cubic_acked success success MATCH 243 430 +187 (+76.95%) ==================== Link: https://patch.msgid.link/20260204151741.2678118-1-puranjay@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents b282131 + 47fcf4d commit 6e951a9

4 files changed

Lines changed: 346 additions & 15 deletions

File tree

include/linux/bpf_verifier.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@ struct bpf_reg_state {
147147
* registers. Example:
148148
* r1 = r2; both will have r1->id == r2->id == N
149149
* r1 += 10; r1->id == N | BPF_ADD_CONST and r1->off == 10
150+
* r3 = r2; both will have r3->id == r2->id == N
151+
* w3 += 10; r3->id == N | BPF_ADD_CONST32 and r3->off == 10
150152
*/
151-
#define BPF_ADD_CONST (1U << 31)
153+
#define BPF_ADD_CONST64 (1U << 31)
154+
#define BPF_ADD_CONST32 (1U << 30)
155+
#define BPF_ADD_CONST (BPF_ADD_CONST64 | BPF_ADD_CONST32)
152156
u32 id;
153157
/* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned
154158
* from a pointer-cast helper, bpf_sk_fullsock() and

kernel/bpf/verifier.c

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16209,6 +16209,13 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1620916209
verbose(env, "verifier internal error: no src_reg\n");
1621016210
return -EFAULT;
1621116211
}
16212+
/*
16213+
* For alu32 linked register tracking, we need to check dst_reg's
16214+
* umax_value before the ALU operation. After adjust_scalar_min_max_vals(),
16215+
* alu32 ops will have zero-extended the result, making umax_value <= U32_MAX.
16216+
*/
16217+
u64 dst_umax = dst_reg->umax_value;
16218+
1621216219
err = adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
1621316220
if (err)
1621416221
return err;
@@ -16218,26 +16225,44 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1621816225
* r1 += 0x1
1621916226
* if r2 < 1000 goto ...
1622016227
* use r1 in memory access
16221-
* So for 64-bit alu remember constant delta between r2 and r1 and
16222-
* update r1 after 'if' condition.
16228+
* So remember constant delta between r2 and r1 and update r1 after
16229+
* 'if' condition.
1622316230
*/
1622416231
if (env->bpf_capable &&
16225-
BPF_OP(insn->code) == BPF_ADD && !alu32 &&
16226-
dst_reg->id && is_reg_const(src_reg, false)) {
16227-
u64 val = reg_const_value(src_reg, false);
16232+
(BPF_OP(insn->code) == BPF_ADD || BPF_OP(insn->code) == BPF_SUB) &&
16233+
dst_reg->id && is_reg_const(src_reg, alu32)) {
16234+
u64 val = reg_const_value(src_reg, alu32);
16235+
s32 off;
16236+
16237+
if (!alu32 && ((s64)val < S32_MIN || (s64)val > S32_MAX))
16238+
goto clear_id;
16239+
16240+
if (alu32 && (dst_umax > U32_MAX))
16241+
goto clear_id;
1622816242

16229-
if ((dst_reg->id & BPF_ADD_CONST) ||
16230-
/* prevent overflow in sync_linked_regs() later */
16231-
val > (u32)S32_MAX) {
16243+
off = (s32)val;
16244+
16245+
if (BPF_OP(insn->code) == BPF_SUB) {
16246+
/* Negating S32_MIN would overflow */
16247+
if (off == S32_MIN)
16248+
goto clear_id;
16249+
off = -off;
16250+
}
16251+
16252+
if (dst_reg->id & BPF_ADD_CONST) {
1623216253
/*
1623316254
* If the register already went through rX += val
1623416255
* we cannot accumulate another val into rx->off.
1623516256
*/
16257+
clear_id:
1623616258
dst_reg->off = 0;
1623716259
dst_reg->id = 0;
1623816260
} else {
16239-
dst_reg->id |= BPF_ADD_CONST;
16240-
dst_reg->off = val;
16261+
if (alu32)
16262+
dst_reg->id |= BPF_ADD_CONST32;
16263+
else
16264+
dst_reg->id |= BPF_ADD_CONST64;
16265+
dst_reg->off = off;
1624116266
}
1624216267
} else {
1624316268
/*
@@ -17334,7 +17359,7 @@ static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_s
1733417359
u32 saved_id = reg->id;
1733517360

1733617361
fake_reg.type = SCALAR_VALUE;
17337-
__mark_reg_known(&fake_reg, (s32)reg->off - (s32)known_reg->off);
17362+
__mark_reg_known(&fake_reg, (s64)reg->off - (s64)known_reg->off);
1733817363

1733917364
/* reg = known_reg; reg += delta */
1734017365
copy_register_state(reg, known_reg);
@@ -17349,6 +17374,9 @@ static void sync_linked_regs(struct bpf_verifier_env *env, struct bpf_verifier_s
1734917374
scalar32_min_max_add(reg, &fake_reg);
1735017375
scalar_min_max_add(reg, &fake_reg);
1735117376
reg->var_off = tnum_add(reg->var_off, fake_reg.var_off);
17377+
if (known_reg->id & BPF_ADD_CONST32)
17378+
zext_32_to_64(reg);
17379+
reg_bounds_sync(reg);
1735217380
}
1735317381
if (e->is_reg)
1735417382
mark_reg_scratched(env, e->regno);

tools/testing/selftests/bpf/progs/verifier_bounds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ __naked void sub64_full_overflow(void)
14771477
SEC("socket")
14781478
__description("64-bit subtraction, partial overflow, result in unbounded reg")
14791479
__success __log_level(2)
1480-
__msg("3: (1f) r3 -= r2 {{.*}} R3=scalar()")
1480+
__msg("3: (1f) r3 -= r2 {{.*}} R3=scalar(id=1-1)")
14811481
__retval(0)
14821482
__naked void sub64_partial_overflow(void)
14831483
{

0 commit comments

Comments
 (0)