bpf: Fixes for the verifier passes
v2 cover bpf: Fixes for the verifier passes
From: Alexei Starovoitov <[email protected]>
Fixes for the series "bpf: Verify in passes, with equations in the state". None of the bugs makes the verifier accept an unsafe program.
Patch 1: the build fails without CONFIG_NET.
Patches 2, 3 and 5: the path-sensitive run finds a failing path that the program does not have. It cuts the budget, and a valid program is rejected. Patch 4: the log shows only the last basic block, not the path to the error. Patch 6: after a failed allocation a load gets -E2BIG in place of -ENOMEM. Patch 7: nfp can refuse to attach a valid offloaded program.
385572 programs of cilium, tetragon, tracee, scx, Meta, systemd, rust-bpf and other projects have the same verdict and the same number of processed insns.
v1 -> v2:
- patch 1: add the Closes tag.
- patch 2: do not skip if the bound differs, in place of a guess. A bound that is a range gave a false guess, and a rejected program took much more insns. Truncate the bound to u32 for jmp32.
- patch 3: describe a case that is not fixed.
- patch 5: drop only a path that is out of frames, not each -E2BIG.
Alexei Starovoitov (7):
bpf: Fix the build without CONFIG_NET
bpf: Do not skip trips if the bound moves
bpf: Mark a skip of trips as a guess if a 32-bit value wraps
bpf: Do not lose the failing path if only an equation differs
bpf: Drop a path of a path-sensitive run that is out of frames
bpf: Return -ENOMEM if the next partition has no memory
bpf: Replay an offloaded program with the precision of the good pass
include/linux/bpf_verifier.h | 42 ++++++++++++++++-- kernel/bpf/equation_join.c | 82 +++++++++++++++++++++++++++--------- kernel/bpf/precision.c | 22 ++++++---- kernel/bpf/verifier.c | 19 +++++++-- 4 files changed, 130 insertions(+), 35 deletions(-)
base-commit: 9867c61abbd44f7053a871566b3d982f62c6d8dc
Signature · 1 line
2.55.0
PATCH v2 1/7 bpf: Fix the build without CONFIG_NET
From: Alexei Starovoitov <[email protected]>
offload.o is built only with CONFIG_NET. Without it the verifier links only if the compiler removes each call of an offload function as dead code. It does not do that for two calls:
undefined reference to bpf_prog_offload_verify_insn' undefined reference to bpf_prog_offload_verifier_prep'
Add stubs for the offload functions that bpf_verifier.h declares.
--- include/linux/bpf_verifier.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
include/linux/bpf_verifier.h +27 -0
@@ -1663,6 +1663,7 @@ static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)return cur_func (env)->regs; } +#ifdef CONFIG_NET int bpf_prog_offload_verifier_prep (struct bpf_prog *prog);int bpf_prog_offload_verify_insn (struct bpf_verifier_env *env,int insn_idx,int prev_insn_idx);
@@ -1672,6 +1673,32 @@ bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, struct bpf_insn *insn);void bpf_prog_offload_remove_insns (struct bpf_verifier_env *env,u32 off,u32 cnt); +#else +static inline int bpf_prog_offload_verifier_prep (struct bpf_prog *prog) +{ +return -EOPNOTSUPP ; +} + +static inline int bpf_prog_offload_verify_insn (struct bpf_verifier_env *env, +int insn_idx,int prev_insn_idx) +{ +return -EOPNOTSUPP ; +} + +static inline int bpf_prog_offload_finalize (struct bpf_verifier_env *env) +{ +return -EOPNOTSUPP ; +} + +static inline void bpf_prog_offload_replace_insn (struct bpf_verifier_env *env,u32 off, +struct bpf_insn *insn) +{ +} + +static inline void bpf_prog_offload_remove_insns (struct bpf_verifier_env *env,u32 off,u32 cnt) +{ +} +#endif /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
Signature · 1 line
2.55.0
PATCH v2 2/7 bpf: Do not skip trips if the bound moves
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A valid program can be rejected. No unsafe program is accepted, because the path-sensitive run accepts nothing.
After a pass with an error, the run looks for one path to the error. It skips trips of a loop. It gets the number of trips from the step of the iv and the bound, as if the bound stays:
i = 0; n = 60;
do { end = flag ? 60 : --n; } while (++i < end);
buf[n - 30];If flag is 0, the loop ends at i = 30, n = 30. At i = 5, n = 55 the run counts 50 more trips and skips to i = 54, n = 6. No path has this state, but the access to buf fails with it. The error counts as real, so the budget of the next passes is cut. If the program needs more than that budget, it is rejected.
Keep the bound in exit_threshold, which this run does not use, and compare it with the bound of the saved trip. If it moved, do not skip. If the bound is a range, the jump moves one end of it. Keep the other end. If the jump refines that end too, a later saved trip has the new value, and the run skips then.
--- include/linux/bpf_verifier.h | 6 +++++- kernel/bpf/equation_join.c | 14 +++++++++++--- kernel/bpf/verifier.c | 5 ++++- 3 files changed, 20 insertions(+), 5 deletions(-)
include/linux/bpf_verifier.h +5 -1
@@ -456,7 +456,11 @@ struct loop_stack_entry {/* an xreg: how many times the program came back to the header */ struct scalar trip_index;bool trip_widened;/* it was moved to a threshold, or it does not grow */ -u64 exit_threshold;/* by the jumps that the state passed and that leave the loop */ +/* +* The trip index at which a jump that the state passed leaves the loop. +* Path-sensitive run: the bound that the jump on the iv saw. +*/ +u64 exit_threshold;/* Path-sensitive run: the jump that tests the iv, the value it saw, the trips left */ u32 iv_jump_idx;u64 iv_prev, trips_left;
kernel/bpf/equation_join.c +11 -3
@@ -756,11 +756,19 @@ static void note_trips_left(struct bpf_verifier_env *env, struct loop_stack_entr far_end = value + (step <0 ?0 : jmp32 ? x->r32.size : x->r64.size);if (e->iv_jump_idx) e->trips_left = step ?compute_trip_count (step, &far_end, op, jmp32, *bound) :0 ; -if (e->iv_jump_idx && step && is_body_jump && !e->trips_left) - e->trips_left =compute_trip_count (step, &far_end,bpf_rev_opcode (op), - jmp32, *bound); +if (e->iv_jump_idx && step && is_body_jump && !e->trips_left) { + op =bpf_rev_opcode (op); + e->trips_left =compute_trip_count (step, &far_end, op, jmp32, *bound); + } e->iv_jump_idx = env->insn_idx; e->iv_prev = value; +/* the jump moves the other end of the bound */ + op =bpf_signed_opcode (op); + e->exit_threshold = jmp32 ? bound->r32.base : bound->r64.base; +if (op !=BPF_JSGT && op !=BPF_JSGE && (op !=BPF_JNE || step >0 )) + e->exit_threshold += jmp32 ? bound->r32.size : bound->r64.size; +if (jmp32) + e->exit_threshold = (u32 )e->exit_threshold; }static void move_bounds (struct bpf_verifier_env *env,struct scalar *s,bool is_ptr,u64 off)
kernel/bpf/verifier.c +4 -1
@@ -21046,18 +21046,21 @@ static int check_held_in_leftover(struct bpf_verifier_env *env,* for (i = 0; i < 100000; i++) { if (rnd() && n < 8) n++; } * Two trips show a step also for 'sum += i', and more than 2^32 trips also for a ring index. * If the steps are not constant, the skip or "infinite" is a guess. +* If the bound moved, there is no skip. A later saved trip can have the same bound. * Returns 1 if it skipped these trips. */ static int fast_forward_trips (struct bpf_verifier_env *env,struct loop_stack_entry *entry) {struct bpf_verifier_state *cur = env->cur_state, *old = &entry->header_state->st; -u32 period = entry->trip -bpf_top_loop (old->frame[old->curframe])->trip; +const struct loop_stack_entry *saved =bpf_top_loop (old->frame[old->curframe]); +u32 period = entry->trip - saved->trip;bool infinite = entry->trips_left >BPF_MAX_TRIPS , guess; DECLARE_BITMAP(iv_xregs, MAX_BPF_XREG) = {};int err;/* the answer needs a walk over the program */ if ((infinite ? entry->loop->infinite : entry->trips_left <= period) || + (!infinite && saved->exit_threshold != entry->exit_threshold) || (u64 )entry->trip * entry->loop->nr_insns < env->prog->len)return 0 ;bpf_fast_forward_xregs (env, old, cur,0 , iv_xregs);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/7] bpf: Do not skip trips if the bound moves
TL;DR: The new bound check in fast_forward_trips() compares only the period's two ends, so a bound that changes mid-period may still be skipped. That could give wrong loop-exit states and spurious…
Quoted 1 line
bpf: Do not skip trips if the bound moves
TL;DR: The new bound check in fast_forward_trips() compares only the period's two ends, so a bound that changes mid-period may still be skipped. That could give wrong loop-exit states and spurious verifier rejections.
Quoted 3 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 7 lines
kernel/bpf/verifier.c +1
-0@@ -21046,18 +21046,21 @@ static int check_held_in_leftover(struct bpf_verifier_env *env,* If the steps are not constant, the skip or "infinite" is a guess. +* If the bound moved, there is no skip. A later saved trip can have the same bound. * Returns 1 if it skipped these trips. */ static int fast_forward_trips (struct bpf_verifier_env *env,struct loop_stack_entry *entry) {
[ ... ]
kernel/bpf/verifier.c static int check_held_in_leftover(struct bpf_verifier_env *env, · line 21065
Quoted 5 lines
kernel/bpf/verifier.c +1
-0/* the answer needs a walk over the program */ if ((infinite ? entry->loop->infinite : entry->trips_left <= period) || + (!infinite && saved->exit_threshold != entry->exit_threshold) || (u64 )entry->trip * entry->loop->nr_insns < env->prog->len)return 0 ;
Is this check enough when the period is longer than one trip? Header states are saved at trips 0, 1, 2, 4, ..., so at trip 4 the comparison is against trip 2 with period = 2. Only the bound seen in the trip before the saved header and the bound seen in the last trip are compared, and note_trips_left() overwrites trips_left on every call. A bound that changes inside the period passes if the two ends match.
For example:
i = 0; t = 0;
do { end = t ? 100 : 50; t ^= 1; } while (++i < end);
buf[i - 40]; /* buf has 16 bytes */The bound alternates between 50 and 100. At trip 4 vs trip 2, saved->exit_threshold is 100 (from trip 1) and entry->exit_threshold is 100 (from trip 3), so the new check passes. trips_left comes from trip 3's jump (i = 4 vs end = 100, about 96 trips), so the skip adds
(96 - 1) / 2 * 2 = 94 to i and lands on i = 98. The loop then leaves with i = 99, while every real path leaves with i = 51.
buf[i - 40] then fails only on this unreal state. The skip is not marked as a guess, so the error counts as real and the budget of the next passes is cut, which is the situation the commit message describes. The new comment "If the bound moved, there is no skip." also overstates what is checked.
Could the skip be refused if any trip of the period saw a different bound, or could trips_left keep the tightest value over the period? Otherwise, should the changelog say that only the ends of the period are compared?
---
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/44956799894
PATCH v2 3/7 bpf: Mark a skip of trips as a guess if a 32-bit value wraps
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A valid program can be rejected. No unsafe program is accepted, because the path-sensitive run accepts nothing.
After a pass with an error, the run looks for one path to the error. It skips trips of a loop: it adds step * trips to each xreg that moves. It adds in 64 bits, but a 32-bit insn wraps:
r7 = 0;
for (i = 0; i < 100; i++) {
x = r7;
if (!flag) w7 += 0x4000000;
}
if (!flag) buf[x - 0x8c000000];If flag is 0, x is 0x8c000000 after the loop. The skip gives 0x18c000000. No path has this value, but the access to buf fails with it. The error counts as real, so the budget of the next passes is cut. If the program needs more than that budget, it is rejected.
If the skip moves a number past a multiple of 2^31, a 32-bit insn can wrap. If such an insn writes an xreg that the skip moves, and the upper half is live, mark the skip as a guess. A guess does not cut the budget.
The test sees the values at the header. It can miss a wrap if a 64-bit insn adds to the result in the same trip: 'w7 += 1; r7 += 5'. Such a program is handled as before.
--- include/linux/bpf_verifier.h | 2 +- kernel/bpf/equation_join.c | 45 ++++++++++++++++++++++++++++-------- kernel/bpf/verifier.c | 5 ++-- 3 files changed, 40 insertions(+), 12 deletions(-)
include/linux/bpf_verifier.h +1 -1
@@ -2593,7 +2593,7 @@ void bpf_collect_iv_thresholds(struct bpf_verifier_env *env);int bpf_guess_first_trips (struct bpf_verifier_env *env,struct bpf_verifier_state *st,struct bpf_loop *loop);bool bpf_steps_are_constant (struct bpf_verifier_env *env,const struct loop_stack_entry *entry, -const unsigned long *iv_xregs,bool infinite); +const unsigned long *iv_xregs,bool infinite,u64 factor);void bpf_fast_forward_xregs (struct bpf_verifier_env *env,const struct bpf_verifier_state *old,struct bpf_verifier_state *cur,u64 factor,unsigned long *iv_xregs);
kernel/bpf/equation_join.c +36 -9
97 lines not on this page · raw message
kernel/bpf/verifier.c +3 -2
@@ -21055,6 +21055,7 @@ static int fast_forward_trips(struct bpf_verifier_env *env, struct loop_stack_enconst struct loop_stack_entry *saved =bpf_top_loop (old->frame[old->curframe]);u32 period = entry->trip - saved->trip;bool infinite = entry->trips_left >BPF_MAX_TRIPS , guess; +u64 factor =div_u64 (entry->trips_left -1 , period); DECLARE_BITMAP(iv_xregs, MAX_BPF_XREG) = {};int err;
@@ -21067,14 +21068,14 @@ static int fast_forward_trips(struct bpf_verifier_env *env, struct loop_stack_en err =bpf_jumps_see_same (env, old, cur, entry->iv_jump_idx, infinite ?NULL : iv_xregs);if (err <=0 )return err; - guess = !bpf_steps_are_constant (env, entry, iv_xregs, infinite); + guess = !bpf_steps_are_constant (env, entry, iv_xregs, infinite, factor);if (infinite) { entry->loop->infinite = true; entry->loop->infinite_is_guess = guess || env->run_has_guess;return 0 ; } env->run_has_guess |= guess; -bpf_fast_forward_xregs (env, old, cur,div_u64 (entry->trips_left -1 , period), iv_xregs); +bpf_fast_forward_xregs (env, old, cur, factor, iv_xregs); entry->iv_jump_idx =0 ; entry->trips_left =1 ;return 1 ;
Signature · 1 line
2.55.0
PATCH v2 4/7 bpf: Do not lose the failing path if only an equation differs
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
This fixes the log of a rejected program. It is not a safety bug: only the path-sensitive run uses bpf_equations_hash(), and that run accepts nothing.
After a pass with an error, the run looks for one path to the error and puts it in the log. It drops a state if a state with the same hash was at the insn. The hash has the xregs that share a sym, but not their relation:
r1 = unknown; r6 = r1; if (rnd) r6 += 1; if (r6 != r1) bad access;
r6 == r1 and r6 == r1 + 1 have the same bounds and the same hash. The first state does not get to the bad access. The second one is dropped, so the run finds no failing path. The program is still rejected, but the log has only the last basic block.
Hash the relation too: both coefs, and the offset between the xreg and the first xreg of its sym. Now the log has the path from insn 0 through 'r6 += 1' to the bad access.
--- kernel/bpf/equation_join.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
kernel/bpf/equation_join.c +16 -7
@@ -622,12 +622,13 @@ bool bpf_equations_join_forgets(struct bpf_verifier_env *env, const struct bpf_vreturn false; } -/* Which xregs of @xregs in frame @fr have one sym? */ +/* Which xregs of @xregs in frame @fr have one sym, and how does one give the other? */ u32 bpf_equations_hash (struct bpf_verifier_env *env,const struct bpf_verifier_state *st,u32 fr,const unsigned long *xregs,u32 bits) { -const struct bpf_xreg_equation *xreg_eq; -u32 i, hash =0 , *first; +const struct bpf_xreg_equation *xreg_eq, *first; +u32 i, hash =0 , *first_pos; +u64 key[5 ];for (i =0 ; i < st->equations.sym_cnt; i++) env->eq_env->scratch[i].new_symno =U32_MAX ;
@@ -635,10 +636,18 @@ u32 bpf_equations_hash(struct bpf_verifier_env *env, const struct bpf_verifier_sif (xreg_eq->xreg >>32 != fr || (u32 )xreg_eq->xreg >= bits || !test_bit ((u32 )xreg_eq->xreg, xregs))continue ; - first = &env->eq_env->scratch[xreg_eq->sym].new_symno; -if (*first !=U32_MAX ) - hash =jhash_2words (*first, xreg_eq->xreg, hash); - *first =min_t (u32, *first, xreg_eq->xreg); + first_pos = &env->eq_env->scratch[xreg_eq->sym].new_symno; +if (*first_pos ==U32_MAX ) { + *first_pos = xreg_eq - st->equations.xreg_equations; +continue ; + } + first = &st->equations.xreg_equations[*first_pos]; + key[0 ] = first->xreg; + key[1 ] = xreg_eq->xreg; + key[2 ] = first->coef; + key[3 ] = xreg_eq->coef; + key[4 ] = first->coef * xreg_eq->off - xreg_eq->coef * first->off; + hash =jhash (key,sizeof (key), hash); }return hash; }
Signature · 1 line
2.55.0
PATCH v2 5/7 bpf: Drop a path of a path-sensitive run that is out of frames
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A valid program with recursion can be rejected with "the call stack of 33 frames is too deep". No unsafe program is accepted, because the path-sensitive run accepts nothing.
A pass can check a recursive function on its own, with no frame for each call. The run has each call of a static function in frames. It can run out of frames and get -E2BIG where no pass does. If the last pass had another error at that call, for example a callx with an unknown target, the run takes -E2BIG for the failing path. The budget of the next passes is cut. If the program needs more, it is rejected.
Drop such a path. If each pass is out of frames too, the program is still rejected. Now its log has only the last basic block, not the full path.
--- include/linux/bpf_verifier.h | 5 ++++- kernel/bpf/verifier.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/bpf_verifier.h +4 -1
@@ -586,7 +586,10 @@ struct bpf_verifier_state {struct bpf_reference_state *refs;u32 insn_idx;bool speculative; -/* an xreg has no value: unreachable. Only a path-sensitive run drops such a state. */ +/* +* An xreg has no value: unreachable. Only a path-sensitive run drops such a state. +* The run also sets it when it is out of frames. +*/ bool infeasible;/* an error only in a leftover, see check_held_in_leftover() */ bool holds_too_much;
kernel/bpf/verifier.c +2 -0
@@ -11240,6 +11240,8 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int callsif (state->curframe +1 >=MAX_CALL_FRAMES ) {verbose (env,"the call stack of %d frames is too deep\n" , state->curframe +2 ); +/* drop the path: a pass possibly does not have these calls in frames */ + env->cur_state->infeasible |= env->path_sensitive;return -E2BIG ; }/* in the frames of the callers it has no end */
Signature · 1 line
2.55.0
PATCH v2 6/7 bpf: Return -ENOMEM if the next partition has no memory
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
This fixes the error code after a failed allocation. No unsafe program is accepted.
If a pass is over its budget, bpf_undo_precision() undoes the last partition and calls partition_next() to select another one. That call can fail with -ENOMEM. bpf_undo_precision() returns bool, so the error becomes "there is no other partition". If the pass had no other error, the load fails with -E2BIG. The log has an error of a less precise pass and "the limit on cost or memory ended pass N". If the program is valid, the user looks for a problem that is not there.
Return the value of partition_next(), and end the load with -ENOMEM. Do that also if the pass had another error. Before, the verifier tried the allocation again, and the program could load. If an earlier pass was good, the program still loads with that pass.
--- include/linux/bpf_verifier.h | 2 +- kernel/bpf/precision.c | 19 +++++++++++-------- kernel/bpf/verifier.c | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-)
include/linux/bpf_verifier.h +1 -1
@@ -1792,7 +1792,7 @@ int bpf_partition_for_pessimized(struct bpf_verifier_env *env);void bpf_restore_precision (struct bpf_verifier_env *env);bool bpf_same_insns_unproven (struct bpf_verifier_env *env);int bpf_partition_more (struct bpf_verifier_env *env); -bool bpf_undo_precision (struct bpf_verifier_env *env); +int bpf_undo_precision (struct bpf_verifier_env *env);void bpf_precision_new_pass (struct bpf_verifier_env *env);u64 bpf_pass_max_cost (struct bpf_verifier_env *env);u64 bpf_precision_budget (struct bpf_verifier_env *env);
kernel/bpf/precision.c +11 -8
@@ -1440,7 +1440,10 @@ static void keep_precision(struct bpf_precision *prec, bool keep) } } -/* The first partition that adds something. If all others are too costly, start again. */ +/* +* The first partition that adds something. If all others are too costly, start again. +* Returns 1 if the next pass has a new partition, barrier or budget, else 0 or an error. +*/ static int partition_next (struct bpf_verifier_env *env) {struct bpf_precision *prec = env->precision;
@@ -1500,8 +1503,8 @@ static bool partition_helped(struct bpf_verifier_env *env) !prec->this_pass.over_budget)); } -/* The pass is over its budget. Returns true if another partition replaces the last one. */ -bool bpf_undo_precision (struct bpf_verifier_env *env) +/* If the pass is over its budget, undo the last partition. Returns as partition_next(). */ +int bpf_undo_precision (struct bpf_verifier_env *env) {struct bpf_precision *prec = env->precision;bool over_budget = false, again = false;
@@ -1511,11 +1514,11 @@ bool bpf_undo_precision(struct bpf_verifier_env *env) prec->this_pass.over_budget = env->over_limit; prec->this_pass.calls_joined = env->calls_joined;if (!env->over_limit || env->cost > env->max_cost) -return false; +return 0 ;if (prec->partitioned_by <=BIT (PARTITION_ADD_DEPS ) || (env->err && (prec->last_pass.over_budget ||partition_helped (env)))) { env->over_limit = !env->err; -return false; +return 0 ; } env->over_limit = false; prec->live_types_over = false;
@@ -1540,7 +1543,7 @@ bool bpf_undo_precision(struct bpf_verifier_env *env) }if (partition_by[__ffs (prec->partitioned_by)] != partition_suspect_loops && !again) prec->too_costly |= prec->partitioned_by; -return partition_next (env) >0 ; +return partition_next (env); }/*
@@ -1569,7 +1572,7 @@ bool bpf_undo_precision(struct bpf_verifier_env *env)* | v * +--- budget *= 4, undo all since one helped, see keep_precision() * -* Returns 1 if more is kept apart. +* Returns as partition_next(). */ int bpf_partition_more (struct bpf_verifier_env *env) {
@@ -1629,7 +1632,7 @@ int bpf_partition_more(struct bpf_verifier_env *env)/* * The pass has no error. But an imprecise state can raise a bound that is checked at attach * time or at run time. It can also ask for a barrier. Partition as if such an insn is -* unproven. Returns 1 if more is kept apart. +* unproven. Returns as partition_next(). */ int bpf_partition_for_pessimized (struct bpf_verifier_env *env) {
kernel/bpf/verifier.c +6 -1
@@ -23684,8 +23684,13 @@ static int do_check_all(struct bpf_verifier_env *env) err = ret;break ; } -if (bpf_undo_precision (env)) + more =bpf_undo_precision (env); +if (more >0 )continue ; +if (more == -ENOMEM && !good_pass) { + err = more; +break ; + }if (ret && good_pass) {restore_snapshot (env, good_pass); err =0 ;
Signature · 1 line
2.55.0
PATCH v2 7/7 bpf: Replay an offloaded program with the precision of the good pass
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
Only offloaded programs are affected. A valid program loads, but nfp can refuse to attach it. No unsafe program is accepted, because the pass that the driver sees is a full pass with all checks.
The driver must see only a pass without errors. So the verifier runs the good pass once more with the hooks of the driver. This is the replay. Before it, bpf_partition_for_pessimized() tries more precision. If that fails, bpf_restore_precision() takes the precision masks back from prec->undo.
partition_next() makes that copy. If find_calls_to_unproven() is over the cost limit, partition_next() does not run, and prec->undo has the masks of an older pass. The limit is low after the first good pass, so this happens if a later good pass ends just below it. Then the replay can join more states than that pass did. In a test with a stub driver max_pkt_offset was 448 in place of 56. If that value and the MTU are above the packet split boundary, nfp refuses to attach the program.
Make the copy in bpf_partition_for_pessimized() too.
--- kernel/bpf/precision.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/bpf/precision.c +3 -0
@@ -1645,6 +1645,9 @@ int bpf_partition_for_pessimized(struct bpf_verifier_env *env) prec->good_budget = prec->budget;for (i =0 ; i < prec->loop_cnt; i++) prec->loops[i]->good = prec->loops[i]->precision; +/* bpf_partition_more() can fail before partition_next() makes this copy */ +if (prec->data_deps) +memcpy (prec->undo, prec->data_deps,5 * prec->words *sizeof (long ));for (i =0 ; i < env->prog->len; i++)if (env->insn_aux_data[i].pessimized)note_unproven_after_pass (env, i);
Signature · 1 line
2.55.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 5 lines · On Sat, 21 Nov 2026 22:13:37 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
Fixes for the series "bpf: Verify in passes, with equations in the state". None of the bugs makes the verifier accept an unsafe program.
Quoted 8 lines
Patch 1: the build fails without
CONFIG_NET.
Patches 2, 3 and 5: the path-sensitive run finds a failing path that the program does not have. It cuts the budget, and a valid program is rejected. Patch 4: the log shows only the last basic block, not the path to the error. Patch 6: after a failed allocation a load gets-E2BIGin place of-ENOMEM. Patch 7: nfp can refuse to attach a valid offloaded program.[...]
Here is the summary with links:
- [bpf-next,v2,1/7] bpf: Fix the build without CONFIG_NET
https://git.kernel.org/bpf/bpf-next/c/625e1bdc1db8 - [bpf-next,v2,2/7] bpf: Do not skip trips if the bound moves https://git.kernel.org/bpf/bpf-next/c/d7db96e1c61b - [bpf-next,v2,3/7] bpf: Mark a skip of trips as a guess if a 32-bit value wraps https://git.kernel.org/bpf/bpf-next/c/e247bf610bee - [bpf-next,v2,4/7] bpf: Do not lose the failing path if only an equation differs https://git.kernel.org/bpf/bpf-next/c/e42c915f6a9f - [bpf-next,v2,5/7] bpf: Drop a path of a path-sensitive run that is out of frames https://git.kernel.org/bpf/bpf-next/c/5f2775434dc2 - [bpf-next,v2,6/7] bpf: Return -ENOMEM if the next partition has no memory https://git.kernel.org/bpf/bpf-next/c/9ec090cadfe0 - [bpf-next,v2,7/7] bpf: Replay an offloaded program with the precision of the good pass https://git.kernel.org/bpf/bpf-next/c/695ce5ca70fc
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html