bpf: Enforce regsafe base id consistency for BPF_ADD_CONST scalars
Subsystems (4)
Daniel Borkmann
maintainerPATCH 1/2 bpf: Enforce regsafe base id consistency for BPF_ADD_CONST scalars
When regsafe() compares two scalar registers that both carry BPF_ADD_CONST, check_scalar_ids() maps their full compound id (aka base | BPF_ADD_CONST flag) as one idmap entry. However, it never verifies that the underlying base ids, that is, with the flag stripped are consistent with existing idmap mappings.
This allows construction of two verifier states where the old state has R3 = R2 + 10 (both sharing base id A) while the current state has R3 = R4 + 10 (base id C, unrelated to R2). The idmap creates two independent entries: A->B (for R2) and A|flag->C|flag (for R3), without catching that A->C conflicts with A->B. State pruning then incorrectly succeeds.
Fix this by additionally verifying base ID mapping consistency whenever BPF_ADD_CONST is set: after mapping the compound ids, also invoke check_ids() on the base IDs (flag bits stripped). This ensures that if A was already mapped to B from comparing the source register, any ADD_CONST derivative must also derive from B, not an unrelated C.
--- kernel/bpf/verifier.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +16 -1
@@ -20130,6 +20130,13 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)* and r7.id=0 (both independent), without temp IDs both would map old_id=X * to cur_id=0 and pass. With temp IDs: r6 maps X->temp1, r7 tries to map * X->temp2, but X is already mapped to temp1, so the check fails correctly. +* +* When old_id has BPF_ADD_CONST set, the compound id (base | flag) and the +* base id (flag stripped) must both map consistently. Example: old has +* r2.id=A, r3.id=A|flag (r3 = r2 + delta), cur has r2.id=B, r3.id=C|flag +* (r3 derived from unrelated r4). Without the base check, idmap gets two +* independent entries A->B and A|flag->C|flag, missing that A->C conflicts +* with A->B. The base ID cross-check catches this. */ static bool check_scalar_ids (u32 old_id,u32 cur_id,struct bpf_idmap *idmap) {
@@ -20138,7 +20145,15 @@ static bool check_scalar_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap) cur_id = cur_id ? cur_id : ++idmap->tmp_id_gen; -return check_ids (old_id, cur_id, idmap); +if (!check_ids (old_id, cur_id, idmap)) +return false; +if (old_id &BPF_ADD_CONST ) { + old_id &= ~BPF_ADD_CONST ; + cur_id &= ~BPF_ADD_CONST ; +if (!check_ids (old_id, cur_id, idmap)) +return false; + } +return true; } static void __clean_func_state(struct bpf_verifier_env *env,
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH 2/2 selftests/bpf: Add test for add_const base_id consistency
Add a test to verifier_linked_scalars that exercises the base_id consistency check for BPF_ADD_CONST linked scalars during state pruning.
With the fix, pruning fails and the verifier discovers the true branch's R3 is too wide for the stack access.
# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t verifier_linked_scalars [...]
#613/22 verifier_linked_scalars/scalars_stale_delta_from_cleared_id:OK #613/23 verifier_linked_scalars/scalars_stale_delta_from_cleared_id_alu32:OK #613/24 verifier_linked_scalars/linked scalars: add_const base_id must be consistent for pruning:OK #613 verifier_linked_scalars:OK
Summary: 1/24 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/progs/verifier_linked_scalars.c | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+)
tools/testing/selftests/bpf/progs/verifier_linked_scalars.c +63 -0
@@ -647,4 +647,67 @@ l_exit_%=: \ : __clobber_all); } +/* +* Test that regsafe() verifies base_id consistency for BPF_ADD_CONST +* linked scalars during state pruning. +* +* The false branch (explored first) links R3 to R2 via ADD_CONST. +* The true branch (runtime path) links R3 to R4 (unrelated base_id). +* At the merge point, pruning must fail because the linkage topology +* differs. +*/ +SEC("socket") +__description("linked scalars: add_const base_id must be consistent for pruning") +__failure __msg("invalid variable-offset") +__flag (BPF_F_TEST_STATE_FREQ) +__naked void add_const_base_id_pruning(void) +{ + asmvolatile (" \ +r1 = 0; \ +*(u64*)(r10 - 16) = r1; \ +call %[bpf_get_prandom_u32]; \ +r6 = r0; \ +r6 &= 1; \ +if r6 >= 1 goto l_true_%=; \ +\ +/* False branch (explored first, old state) */ \ +call %[bpf_get_prandom_u32]; \ +r2 = r0; \ +r2 &= 0xff; /* R2 = scalar(id=A) [0,255] */ \ +r3 = r2; /* R3 linked to R2 (id=A) */ \ +r3 += 10; /* R3 id=A|ADD_CONST, delta=10 */\ +r6 = 0; \ +goto l_merge_%=; \ +\ +l_true_%=: \ +/* True branch (runtime path, cur state) */ \ +call %[bpf_get_prandom_u32]; \ +r2 = r0; \ +r2 &= 0xff; /* R2 = scalar [0,255], id=0 */ \ +r4 = r0; \ +r4 &= 0xff; /* R4 = scalar [0,255], id=0 */ \ +r3 = r4; /* R3 linked to R4 (new id=C) */\ +r3 += 10; /* R3 id=C|ADD_CONST, delta=10 */\ +r6 = 0; \ +\ +l_merge_%=: \ +/* At merge, old R3 linked to R2, cur R3 linked to R4. */\ +/* Pruning must fail: base_ids A vs C inconsistent. */ \ +if r2 >= 6 goto l_exit_%=; \ +/* sync_linked_regs: R2<6 => R3<16 in old state. */ \ +/* Without fix: R3 in [10,15] from incorrect pruning. */\ +/* With fix: R3 in [10,265], not synced from R2. */ \ +r3 -= 10; /* [0,5] vs [0,255] */ \ +r9 = r10; \ +r9 += -16; \ +r9 += r3; /* fp-16+[0,5] vs fp-16+[0,255] */\ +*(u8*)(r9 + 0) = r6; /* within 16B vs past fp */ \ +l_exit_%=: \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_get_prandom_u32) + : __clobber_all); +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 6 lines · On Sat, 11 Apr 2026 01:26:50 +0200 you wrote:
When
regsafe()compares two scalar registers that both carryBPF_ADD_CONST,check_scalar_ids()maps their full compound id (aka base |BPF_ADD_CONSTflag) as one idmap entry. However, it never verifies that the underlying base ids, that is, with the flag stripped are consistent with existing idmap mappings.
Quoted 8 lines
This allows construction of two verifier states where the old state has R3 = R2 + 10 (both sharing base id A) while the current state has R3 = R4 + 10 (base id C, unrelated to R2). The idmap creates two independent entries:
A->B(for R2) and A|flag->C|flag (for R3), without catching thatA->Cconflicts withA->B. State pruning then incorrectly succeeds.[...]
Here is the summary with links:
- [bpf-next,1/2] bpf: Enforce regsafe base id consistency for BPF_ADD_CONST scalars
https://git.kernel.org/bpf/bpf-next/c/2f2ec8e7730e - [bpf-next,2/2] selftests/bpf: Add test for add_const base_id consistency https://git.kernel.org/bpf/bpf-next/c/497fa510ee46
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html