From: Alexei Starovoitov 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. Fixes: ecb43124db39 ("bpf: Partition the states after an error") Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 2 +- kernel/bpf/precision.c | 19 +++++++++++-------- kernel/bpf/verifier.c | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 05a52913ae..151b578160 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -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); diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c index c53099a82a..e6dfb9f4bd 100644 --- a/kernel/bpf/precision.c +++ b/kernel/bpf/precision.c @@ -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) { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index fe6209c166..dea18409f4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -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; -- 2.55.0