bpf: Fixes for the verifier that joins states
Subsystems (4)
cover bpf: Fixes for the verifier that joins states
From: Alexei Starovoitov <[email protected]>
Fixes for commit 2d0b457611c0 ("Merge branch 'bpf-a-verifier-that-joins-states'"), from review and from BPF CI.
1-2 A number that wraps in front of the jump that counts a loop gets
a wrong residue behind the jump. The verifier then checks numbers that the register never has.
3-4 Two of the new tests fail in BPF CI: one with GCC BPF, one on s390x. 5-7 The same kind, no job shows it today: a message with GCC, a target of
fentry that is not in BTF, five tests with an arena that run at the same time under KASAN.
8-9 Valid progs that throw one of several error codes are rejected.
Alexei Starovoitov (9):
bpf: Leave a number alone that wraps in front of the jump that counts selftests/bpf: Test a number that wraps in front of the jump that
counts
selftests/bpf: Skip the stack walker of verifier_loop_latch with GCC selftests/bpf: Skip plain_c_test_wakeup_source without the kfuncs selftests/bpf: Expect GCC's first error in test_alloc_double_release selftests/bpf: Skip plain_c_arena_race without zap_vma_range() in BTF selftests/bpf: Run the plain C tests with an arena serially
bpf: A join must leave the cookie of bpf_throw() alone selftests/bpf: Test a flag that goes with the cookie of bpf_throw()
kernel/bpf/flow.c | 4 +- kernel/bpf/scev.c | 8 ++- kernel/bpf/verifier.c | 3 + .../selftests/bpf/prog_tests/plain_c.c | 34 ++++++++--- .../selftests/bpf/progs/cpumask_failure.c | 5 ++ .../bpf/progs/verifier_jumps_ahead.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/verifier_loop_latch.c | 26 +++++++++ 7 files changed, 126 insertions(+), 11 deletions(-)
PATCH 1/9 bpf: Leave a number alone that wraps in front of the jump that counts
From: Alexei Starovoitov <[email protected]>
for (i = 0, n = first; ; i++, n -= 97) {
n += 100;
if (i == 2)
break;
use(n);
}n is linear at the jump that counts: 'first + 100 + 3 * k'. Behind the jump bpf_clamp_scev_regs() gives it the range of one iteration less. When first is a constant it gets the step 3 and the residue '(first + 100) mod 3' too. When first + 100 goes over S64_MAX the signed value is 2^64 less. 2^64 is not a multiple of 3, so the residue is another one. reg_bounds_sync() rounds the range to numbers that n never is and drops the numbers that it is. What takes n as an index is checked for the wrong numbers.
scalar_step_add() drops the step of a sum that can wrap. Set no range at all in this case. The register keeps what the state has. That went through scalar_step_add(), and it has the low bits of n, which bpf_set_reg_range() forgets.
'bounds.base + moved' can overflow s64 when n itself doesn't wrap. Add the residues, as scalar_step_add() does.
--- kernel/bpf/scev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/scev.c +6 -2
@@ -3992,7 +3992,7 @@ int bpf_clamp_scev_regs(struct bpf_verifier_env *env, struct bpf_func_state *curstruct env *header_env;struct bounds bounds;u32 r, r_scev; -s64 moved; +s64 moved, sum;int err;if (entry_state->curframe != cur_func_state->frameno) {
@@ -4044,8 +4044,12 @@ int bpf_clamp_scev_regs(struct bpf_verifier_env *env, struct bpf_func_state *curif (coefs.base_reg == entry->iv_reg && !moved)iv_bounds (&bounds, entry->iv_range);if (moved) { +/* a sum that can wrap has another residue, see scalar_step_add() */ +if (check_add_overflow (cnum64_smin (bounds.r64), moved, &sum) || +check_add_overflow (cnum64_smax (bounds.r64), moved, &sum)) +continue ; bounds.r64 =cnum64_add (bounds.r64,cnum64_from_srange (moved, moved)); - bounds.base =imod (bounds.base + moved, bounds.step); + bounds.base = (bounds.base +imod (moved, bounds.step)) % bounds.step; }if (behind) {/* jumps on the way may have told more */
PATCH 2/9 selftests/bpf: Test a number that wraps in front of the jump that counts
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A number goes up by 3. In front of the jump that counts the loop it moves over S64_MAX in one pair of progs and by S64_MAX in the other. The store behind the jump takes it as an index. In the progs that have to be rejected the store is one byte behind the value at run time.
--- .../selftests/bpf/progs/verifier_loop_latch.c | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+)
tools/testing/selftests/bpf/progs/verifier_loop_latch.c +23 -0
@@ -117,6 +117,29 @@ LOOP(behind_moved, "w8 = 0; r9 = r6; r9 += 2043;", MOVED(4) "goto 1b;") BAD("a store behind the jump that counts, the pointer has moved too far") LOOP(behind_moved_bad, "w8 = 0; r9 = r6; r9 += 2044;", MOVED(4) "goto 1b;") +/* +* A number goes up by 3 and has moved in front of the jump. At the store it is @first or +* @first + 3. Moved over S64_MAX it has another residue: 2^64 is not a multiple of 3. It keeps +* the range of all three iterations then. + */ +#define NUM (by, back, first, off) \ + "r3 = " #by " ll; r9 += r3; if w8 == 2 goto 9f; r2 = r9; r3 = " #first " ll; r2 -= r3;" \ + "r2 += r5; *(u8 *)(r2 + " #off ") = r1; r3 = " #back " ll; r9 += r3; w8 += 1; goto 1b;" +#define OVER (off) NUM(100, -97, 0x8000000000000031, off) +#define FAR (off) NUM(0x7fffffffffffffff, 0x8000000000000004, 0x7ffffffffffffffa, off) + +OK("behind the jump that counts, a number has moved over S64_MAX", "3") +LOOP(moved_over, "w8 = 0; r5 = r6; r9 = 0x7fffffffffffffcd ll;", OVER(2041)) + +BAD("behind the jump that counts, a number has moved over S64_MAX, too far") +LOOP(moved_over_bad, "w8 = 0; r5 = r6; r9 = 0x7fffffffffffffcd ll;", OVER(2045)) + +OK("behind the jump that counts, a number has moved by S64_MAX", "3") +LOOP(moved_far, "w8 = 0; r5 = r6; r9 = -5;", FAR(2044)) + +BAD("behind the jump that counts, a number has moved by S64_MAX, one too far") +LOOP(moved_far_bad, "w8 = 0; r5 = r6; r9 = -5;", FAR(2045)) + OK("a store behind the jump that counts two back edges", "5") LOOP(behind_two, "w8 = 0; r9 = r6; r9 += 2044;", AT_TOP(4) "if r7 == 7 goto 1b; goto 1b;")
PATCH 3/9 selftests/bpf: Skip the stack walker of verifier_loop_latch with GCC
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
The GCC BPF jobs of BPF CI fail in:
verifier_loop_latch/latch: frames of a stack walker
The test is about the outer loop: 'if (frames == 8) break;' counts it, so out[idx + j] stays within the 256 words of the value. GCC unrolls the inner loop
for (j = 0; j < n; j++)
out[idx + j] = b + c + d + e;by 8, walks 'out' with a pointer and ends the loop with 'if r9 != r5', where both registers point into the map value. The verifier finds no count for that and says:
invalid access to map value, value_size=2048 off=2048 size=8
Skip the prog with GCC, as plain_c_verifier_loop_as_is.c does for the same reason. The prog with nine frames is rejected with both compilers:
it stays.
--- tools/testing/selftests/bpf/progs/verifier_loop_latch.c | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/bpf/progs/verifier_loop_latch.c +3 -0
@@ -206,6 +206,9 @@ int name(void *ctx) \ } OK("frames of a stack walker", "[0..9]") __retval(256) +#ifndef __clang__ +__skip("GCC unrolls the inner loop") +#endif FRAMES(eight_frames, 8)BAD ("frames of a stack walker, one too many" )
PATCH 4/9 selftests/bpf: Skip plain_c_test_wakeup_source without the kfuncs
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
The s390x jobs of BPF CI fail in:
plain_c_test_wakeup_source/iterate_wakeupsources
The prog calls bpf_wakeup_sources_get_head(), _read_lock() and _read_unlock(). They are in drivers/base/power/wakeup.c, which is built with CONFIG_PM_SLEEP. s390 has no suspend and no hibernation, so its kernel does not have the kfuncs and the load fails:
libbpf: extern (func ksym) 'bpf_wakeup_sources_get_head': not found in kernel or module BTFs
prog_tests/wakeup_source.c skips when bpf_wakeup_sources_get_head() is not in the BTF of vmlinux. Do the same for the plain C form of its prog.
--- tools/testing/selftests/bpf/prog_tests/plain_c.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/plain_c.c +11 -1
@@ -152,7 +152,17 @@ void test_plain_c_tailcall_bpf2bpf_hierarchy1(void) { RUN(tailcall_bpf2bpf_hieravoid test_plain_c_tailcall_bpf2bpf_hierarchy2 (void ) {RUN (tailcall_bpf2bpf_hierarchy2); }void test_plain_c_tailcall_bpf2bpf_hierarchy3 (void ) {RUN (tailcall_bpf2bpf_hierarchy3); }void test_plain_c_tailcall_callback (void ) {RUN (tailcall_callback); } -void test_plain_c_test_wakeup_source (void ) {RUN (test_wakeup_source); } + +void test_plain_c_test_wakeup_source (void ) +{ +/* prog_tests/wakeup_source.c: the kfuncs are there with CONFIG_PM_SLEEP */ +if (libbpf_find_vmlinux_btf_id ("bpf_wakeup_sources_get_head" ,BPF_TRACE_FENTRY ) <=0 ) { +test__skip (); +return ; + } +RUN (test_wakeup_source); +} +void test_plain_c_tracing_stack_arg_tailcall_tgt (void ) {RUN (tracing_stack_arg_tailcall_tgt); }void test_plain_c_verifier_arena_large (void ) {RUN (verifier_arena_large); }void test_plain_c_verifier_iterating_callbacks (void ) {RUN (verifier_iterating_callbacks); }
PATCH 5/9 selftests/bpf: Expect GCC's first error in test_alloc_double_release
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
test_alloc_double_release has two errors: it releases the cpumask twice and it does not check create_cpumask() for NULL. The test expects the message for the first one:
R1 type=scalar expected=ptr_, trusted_ptr_, rcu_ptr_
GCC gives the path with NULL its own copy of the two calls. The verifier gets there first and says:
Possibly NULL pointer passed to trusted R1
The prog is rejected with both compilers. Expect GCC's message with GCC.
--- tools/testing/selftests/bpf/progs/cpumask_failure.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/testing/selftests/bpf/progs/cpumask_failure.c +5 -0
@@ -45,7 +45,12 @@ int BPF_PROG(test_alloc_no_release, struct task_struct *task, u64 clone_flags) } SEC("tp_btf/task_newtask") +#ifdef __clang__ __failure __msg("R1 type=scalar expected=ptr_, trusted_ptr_, rcu_ptr_") +#else +/* no check for NULL either: in GCC's layout the verifier gets there first */ +__failure __msg("NULL pointer passed to trusted R1") +#endif int BPF_PROG (test_alloc_double_release ,struct task_struct *task,u64 clone_flags) {struct bpf_cpumask *cpumask;
PATCH 6/9 selftests/bpf: Skip plain_c_arena_race without zap_vma_range() in BTF
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
plain_c_arena_race has the fentry prog of arena_race.c. libbpf looks for its target, zap_vma_range(), in the BTF of vmlinux. When the function is not there the load fails:
failed to find kernel BTF type ID of 'zap_vma_range': -ESRCH
prog_tests/arena_race.c skips the subtests that need the prog in this case. Do the same for the plain C form. The kernels of BPF CI have the
function: no job fails today.
--- tools/testing/selftests/bpf/prog_tests/plain_c.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/plain_c.c +10 -1
@@ -119,7 +119,16 @@ void test_plain_c_arena_list(void)arena_list_add_del (); } -void test_plain_c_arena_race (void ) {RUN (arena_race); } +void test_plain_c_arena_race (void ) +{ +/* prog_tests/arena_race.c: the target of fentry */ +if (libbpf_find_vmlinux_btf_id ("zap_vma_range" ,BPF_TRACE_FENTRY ) <=0 ) { +test__skip (); +return ; + } +RUN (arena_race); +} +void test_plain_c_arena_strsearch (void ) {RUN (arena_strsearch); }void test_plain_c_bpf_ma_ttrace (void )
PATCH 7/9 selftests/bpf: Run the plain C tests with an arena serially
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
Commit a14f9ba080c1 ("selftests/bpf: Run arena tests serially") made arena_htab, arena_list, arena_strsearch and the other arena tests serial. With KASAN every arena takes 512 MB of shadow memory while it lives, and several arenas at a time exhaust the memory of a small CI VM.
The plain C forms of these tests make the same arenas. Their names are next to each other, so test_progs -j gives them to its workers at the same time. In a KASAN VM with 4 cpus and 5G
./test_progs -j -t plain_c
leaves 0.4G of memory available at its lowest point. Make the five tests whose prog file has an arena map serial: 2.4G stay available.
plain_c_arena_race has no arena. It stays parallel.
--- tools/testing/selftests/bpf/prog_tests/plain_c.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/plain_c.c +6 -5
@@ -109,10 +109,11 @@ static void dynptr_run(void)plain_c_dynptr_success__destroy (skel); } -void test_plain_c_arena_htab (void ) {RUN (arena_htab); } -void test_plain_c_arena_htab_asm (void ) {RUN (arena_htab_asm); } +/* Files with an arena map are serial, as prog_tests/arena_*.c: 512 MB of KASAN shadow per arena */ +void serial_test_plain_c_arena_htab (void ) {RUN (arena_htab); } +void serial_test_plain_c_arena_htab_asm (void ) {RUN (arena_htab_asm); } -void test_plain_c_arena_list (void ) +void serial_test_plain_c_arena_list (void ) {RUN (arena_list);if (test__start_subtest ("run" ))
@@ -129,7 +130,7 @@ void test_plain_c_arena_race(void)RUN (arena_race); } -void test_plain_c_arena_strsearch (void ) {RUN (arena_strsearch); } +void serial_test_plain_c_arena_strsearch (void ) {RUN (arena_strsearch); }void test_plain_c_bpf_ma_ttrace (void ) {
@@ -173,7 +174,7 @@ void test_plain_c_test_wakeup_source(void) }void test_plain_c_tracing_stack_arg_tailcall_tgt (void ) {RUN (tracing_stack_arg_tailcall_tgt); } -void test_plain_c_verifier_arena_large (void ) {RUN (verifier_arena_large); } +void serial_test_plain_c_verifier_arena_large (void ) {RUN (verifier_arena_large); }void test_plain_c_verifier_iterating_callbacks (void ) {RUN (verifier_iterating_callbacks); }void test_plain_c_verifier_loop_as_is (void ) {RUN (verifier_loop_as_is); }void test_plain_c_xdp_lb_bench (void ) {RUN (xdp_lb_bench); }
PATCH 8/9 bpf: A join must leave the cookie of bpf_throw() alone
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
An LSM prog without an exception callback:
err = policy(file); /* 1: no rule, 2: audit, 0, -EPERM, -EACCES */
count(a); count(b); count(c);
if (err == -EPERM || err == -EACCES)
bpf_throw(err);
return 0;The compiler decides the 'if' where err is set, and keeps a flag. All paths meet, then the flag is tested. In the join err is [-13, 2]:
At program exit the register R1 has .. should have been in [-4095, 0]
The prog is valid, and it loaded when the verifier followed each path. It has the shape of cil_sock4_connect() of Cilium, with bpf_throw() in place of bpf_set_retval().
Without an exception callback the prog returns the cookie. Thus check_kfunc_call() compares r1 of bpf_throw() with the range of the prog type, like r0 at exit. That check is not in check_func_arg() either. Add r1 to bpf_call_checked_arg_regs() by hand, like r1 of bpf_set_retval(). States in which the flag and the cookie differ are not joined then.
The same table tells whether something looks at a number that a short loop counts. This prog loads too:
for (i = 0; i < 3; i++)
if (hit(i))
n++; bpf_throw(n); /* cgroup_skb/egress takes [0, 3] */
--- kernel/bpf/flow.c | 4 ++-- kernel/bpf/verifier.c | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-)
kernel/bpf/flow.c +2 -2
@@ -549,8 +549,8 @@ static u16 flow_stack_off_regs(struct bpf_verifier_env *env, int idx)/* * A jump that compares a register with a constant, which may be in the other register. -* A call does the same: the verifier compares a size, or what bpf_set_retval() takes, -* with what is allowed. So does the exit of the prog, see flow_compute_need_const(). +* A call does the same: the verifier compares a size, or what bpf_set_retval() or bpf_throw() +* takes, with what is allowed. So does the exit of the prog, see flow_compute_need_const(). */ static u16 flow_tested_regs (struct bpf_verifier_env *env,int idx) {
kernel/bpf/verifier.c +3 -0
@@ -15614,6 +15614,9 @@ u16 bpf_call_checked_arg_regs(struct bpf_verifier_env *env, struct bpf_insn *ins {if (bpf_helper_call (insn) && insn->imm == BPF_FUNC_set_retval)return BIT (BPF_REG_1 ); +/* without an exception callback the prog returns what bpf_throw() takes */ +if (bpf_is_throw_kfunc (insn) && !env->exception_callback_subprog) +return BIT (BPF_REG_1 );return call_arg_regs (env, insn, arg_type_is_mem_size); }
PATCH 9/9 selftests/bpf: Test a flag that goes with the cookie of bpf_throw()
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
if (err == -EPERM || err == -EACCES)
bpf_throw(err);An LSM prog, no exception callback. The compiler keeps the condition in a flag. Two jumps are between the place where the paths meet and the test of the flag. That is too far for the rule that keeps 0 and 1 of a flag apart. In the second prog the flag goes with the number that the prog must not return.
--- .../bpf/progs/verifier_jumps_ahead.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+)
tools/testing/selftests/bpf/progs/verifier_jumps_ahead.c +57 -0
@@ -523,6 +523,63 @@ __naked void flag_and_retval(void) ); } +extern void bpf_throw(__u64 cookie) __ksym; + +/* BTF FUNC records are not generated for kfuncs referenced from inline assembly */ +void __kfunc_btf_root (void ) +{ +bpf_throw (0 ); +} + +/* +* err = policy(); +* count(a); count(b); +* if (err == -EPERM || err == -EACCES) +* bpf_throw(err); +* return 0; +* There is no exception callback: the prog returns err. The jump that looks at the flag is too +* far ahead to keep 0 and 1 of the flag apart. +*/ +#define FLAG_AND_COOKIE (name, no, yes) \ +__arch_x86_64 __arch_arm64 __arch_s390x \ +__naked void name(void) \ +{ \ + asm volatile ( \ + "call %[bpf_get_prandom_u32];" \ + "w8 = " #no ";" \ + "r9 = 1;" \ + "if r0 == 0 goto 1f;" \ + "w8 = " #yes ";" \ + "r9 = -13;" \ + "1:" \ + "call %[bpf_get_prandom_u32];" \ + "if r0 == 0 goto +1;" \ + "r7 = r0;" \ + "call %[bpf_get_prandom_u32];" \ + "if r0 == 0 goto +1;" \ + "r7 = r0;" \ + "if w8 == 0 goto 2f;" \ + "r1 = r9;" \ + "call %[bpf_throw];" \ + "2:" \ + "r0 = 0;" \ + "exit;" \ + : \ + : __imm(bpf_get_prandom_u32), __imm(bpf_throw) \ + : __clobber_all \ + ); \ +} + +SEC("lsm/file_permission") +__description("jumps ahead: a flag goes together with what bpf_throw() takes") +__success +FLAG_AND_COOKIE(flag_and_cookie, 0, 1) + +SEC("lsm/file_permission") +__description("jumps ahead: a flag goes together with what bpf_throw() does not take") +__failure __msg("At program exit the register R1 has smin=1 smax=1 should have been in [-4095, 0]") +FLAG_AND_COOKIE(flag_and_bad_cookie, 1, 0) + char buf[8];/* As in test_seg6_loop.c without 32-bit insns: the copy is tested, the original is used */
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 14 lines · On Sat, 7 Nov 2026 17:02:57 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
Fixes for commit
2d0b457611c0("Merge branch 'bpf-a-verifier-that-joins-states'"), from review and from BPF CI.1-2 A number that wraps in front of the jump that counts a loop gets
a wrong residue behind the jump. The verifier then checks numbers that the register never has.
3-4 Two of the new tests fail in BPF CI: one with GCC BPF, one on s390x. 5-7 The same kind, no job shows it today: a message with GCC, a target of
fentry that is not in BTF, five tests with an arena that run at the same time under KASAN.
8-9 Valid progs that throw one of several error codes are rejected.
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,1/9] bpf: Leave a number alone that wraps in front of the jump that counts
https://git.kernel.org/bpf/bpf-next/c/914dd83543fc - [bpf-next,2/9] selftests/bpf: Test a number that wraps in front of the jump that counts https://git.kernel.org/bpf/bpf-next/c/589b23cbc6c6 - [bpf-next,3/9] selftests/bpf: Skip the stack walker of verifier_loop_latch with GCC https://git.kernel.org/bpf/bpf-next/c/ce4b64ed8b22 - [bpf-next,4/9] selftests/bpf: Skip plain_c_test_wakeup_source without the kfuncs https://git.kernel.org/bpf/bpf-next/c/f567c4b7af18 - [bpf-next,5/9] selftests/bpf: Expect GCC's first error in test_alloc_double_release https://git.kernel.org/bpf/bpf-next/c/98cc0b224608 - [bpf-next,6/9] selftests/bpf: Skip plain_c_arena_race without zap_vma_range() in BTF https://git.kernel.org/bpf/bpf-next/c/57c57a40807a - [bpf-next,7/9] selftests/bpf: Run the plain C tests with an arena serially https://git.kernel.org/bpf/bpf-next/c/b992d6ed7531 - [bpf-next,8/9] bpf: A join must leave the cookie of bpf_throw() alone https://git.kernel.org/bpf/bpf-next/c/ed10b8a49c39 - [bpf-next,9/9] selftests/bpf: Test a flag that goes with the cookie of bpf_throw() https://git.kernel.org/bpf/bpf-next/c/d877bd038cc6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html