Skip to content

Commit 9f88b29

Browse files
puranjaymohangregkh
authored andcommitted
bpf: Support negative offsets, BPF_SUB, and alu32 for linked register tracking
[ Upstream commit 7a433e5 ] Previously, the verifier only tracked positive constant deltas between linked registers using BPF_ADD. This limitation meant patterns like: r1 = r0; r1 += -4; if r1 s>= 0 goto l0_%=; // r1 >= 0 implies r0 >= 4 // verifier couldn't propagate bounds back to r0 if r0 != 0 goto l0_%=; r0 /= 0; // Verifier thinks this is reachable l0_%=: Similar limitation exists for 32-bit registers. With this change, the verifier can now track negative deltas in reg->off enabling bound propagation for the above pattern. For alu32, we make sure the destination register has the upper 32 bits as 0s before creating the link. BPF_ADD_CONST is split into BPF_ADD_CONST64 and BPF_ADD_CONST32, the latter is used in case of alu32 and sync_linked_regs uses this to zext the result if known_reg has this flag. Signed-off-by: Puranjay Mohan <puranjay@kernel.org> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20260204151741.2678118-2-puranjay@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Stable-dep-of: d7f1417 ("bpf: Fix linked reg delta tracking when src_reg == dst_reg") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2546335 commit 9f88b29

3 files changed

Lines changed: 45 additions & 13 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
@@ -15787,6 +15787,13 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1578715787
verbose(env, "verifier internal error: no src_reg\n");
1578815788
return -EFAULT;
1578915789
}
15790+
/*
15791+
* For alu32 linked register tracking, we need to check dst_reg's
15792+
* umax_value before the ALU operation. After adjust_scalar_min_max_vals(),
15793+
* alu32 ops will have zero-extended the result, making umax_value <= U32_MAX.
15794+
*/
15795+
u64 dst_umax = dst_reg->umax_value;
15796+
1579015797
err = adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
1579115798
if (err)
1579215799
return err;
@@ -15796,26 +15803,44 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
1579615803
* r1 += 0x1
1579715804
* if r2 < 1000 goto ...
1579815805
* use r1 in memory access
15799-
* So for 64-bit alu remember constant delta between r2 and r1 and
15800-
* update r1 after 'if' condition.
15806+
* So remember constant delta between r2 and r1 and update r1 after
15807+
* 'if' condition.
1580115808
*/
1580215809
if (env->bpf_capable &&
15803-
BPF_OP(insn->code) == BPF_ADD && !alu32 &&
15804-
dst_reg->id && is_reg_const(src_reg, false)) {
15805-
u64 val = reg_const_value(src_reg, false);
15810+
(BPF_OP(insn->code) == BPF_ADD || BPF_OP(insn->code) == BPF_SUB) &&
15811+
dst_reg->id && is_reg_const(src_reg, alu32)) {
15812+
u64 val = reg_const_value(src_reg, alu32);
15813+
s32 off;
15814+
15815+
if (!alu32 && ((s64)val < S32_MIN || (s64)val > S32_MAX))
15816+
goto clear_id;
15817+
15818+
if (alu32 && (dst_umax > U32_MAX))
15819+
goto clear_id;
1580615820

15807-
if ((dst_reg->id & BPF_ADD_CONST) ||
15808-
/* prevent overflow in sync_linked_regs() later */
15809-
val > (u32)S32_MAX) {
15821+
off = (s32)val;
15822+
15823+
if (BPF_OP(insn->code) == BPF_SUB) {
15824+
/* Negating S32_MIN would overflow */
15825+
if (off == S32_MIN)
15826+
goto clear_id;
15827+
off = -off;
15828+
}
15829+
15830+
if (dst_reg->id & BPF_ADD_CONST) {
1581015831
/*
1581115832
* If the register already went through rX += val
1581215833
* we cannot accumulate another val into rx->off.
1581315834
*/
15835+
clear_id:
1581415836
dst_reg->off = 0;
1581515837
dst_reg->id = 0;
1581615838
} else {
15817-
dst_reg->id |= BPF_ADD_CONST;
15818-
dst_reg->off = val;
15839+
if (alu32)
15840+
dst_reg->id |= BPF_ADD_CONST32;
15841+
else
15842+
dst_reg->id |= BPF_ADD_CONST64;
15843+
dst_reg->off = off;
1581915844
}
1582015845
} else {
1582115846
/*
@@ -16888,7 +16913,7 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_s
1688816913
u32 saved_id = reg->id;
1688916914

1689016915
fake_reg.type = SCALAR_VALUE;
16891-
__mark_reg_known(&fake_reg, (s32)reg->off - (s32)known_reg->off);
16916+
__mark_reg_known(&fake_reg, (s64)reg->off - (s64)known_reg->off);
1689216917

1689316918
/* reg = known_reg; reg += delta */
1689416919
copy_register_state(reg, known_reg);
@@ -16903,6 +16928,9 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_s
1690316928
scalar32_min_max_add(reg, &fake_reg);
1690416929
scalar_min_max_add(reg, &fake_reg);
1690516930
reg->var_off = tnum_add(reg->var_off, fake_reg.var_off);
16931+
if (known_reg->id & BPF_ADD_CONST32)
16932+
zext_32_to_64(reg);
16933+
reg_bounds_sync(reg);
1690616934
}
1690716935
}
1690816936
}

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)