From: Alexei Starovoitov A function that main programs share has one landing pad per site, and it has to be right for all of them: static int f(bool lock) prog1: f(false) { prog2: f(true) if (lock) bpf_rcu_read_lock(); while (...) <- cancelled here ; if (lock) bpf_rcu_read_unlock(); } Each pass starts with no records of the sites, so the pad would be for the last main program only. Start the passes of a main program with a copy of what the ones before it recorded. Settle the pads after each main program, so that its passes can fix its errors, and undo that until the last one. A main program that is proven is not checked again. Reject the load when a later one changes a site that it went through: - a global function can be cancelled, and its call did not expect that - an access to arena where nothing was held needs a pad that releases - a function cannot be cancelled any more, so its may_goto jumps For the same reason don't change what is handed over to its functions. The pad of a main program returns the value of its struct_ops member and passes the entry to ->cancelled(). An async callback passes the owner. With bpf_throw() pads pass on the value that a shared function leaves with, so reject main programs that have different values. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 9 +++++- kernel/bpf/pads.c | 59 ++++++++++++++++++++++++++------- kernel/bpf/verifier.c | 63 ++++++++++++++++++++++++++++++++---- 3 files changed, 112 insertions(+), 19 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index e0eb2cce3180..619df34f0ced 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1614,6 +1614,12 @@ struct bpf_subprog_info { bool bail_unwinds: 1; /* bail_unwinds as it was before the first pass, see check_funcs() */ bool bail_unwinds_at_first: 1; + /* a main prog that is not checked again gets to it, see check_proven_mains() */ + bool proven: 1; + /* a call of the global subprog took it for one that does not unwind, or is not cancelled */ + bool call_skips_unwind: 1; + bool call_skips_cancel: 1; + bool leaves_unexpected: 1; /* and it does */ /* * It has an unwind_exit, or a state left it with unwind_exit in this pass, or is expected * to, see mark_unwind_above(). For a global subprog the bit goes to the next pass. @@ -1749,6 +1755,7 @@ static inline u32 bpf_stack_depth_of_prog(const struct bpf_subprog_info *sub) struct bpf_main_prog { struct bpf_prog *prog; u32 subprog; /* until dead code is removed */ + int cancelled_retval; }; static inline u16 bpf_in_stack_arg_cnt(const struct bpf_subprog_info *sub) @@ -2526,7 +2533,7 @@ void bpf_pad_note_section(struct bpf_verifier_env *env, enum bpf_pad_class class void bpf_pad_note_return(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, const struct bpf_func_state *caller, int call_idx, bool plain); size_t bpf_pad_plans_free(struct bpf_insn_aux_data *aux); -void bpf_pads_new_round(struct bpf_verifier_env *env); +void bpf_pads_keep(struct bpf_verifier_env *env, struct bpf_pads_moved *kept); void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, bool save); size_t bpf_pads_free_moved(struct bpf_pads_moved *moved, u32 cnt); struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 70a972716385..befe9a729115 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -492,19 +492,52 @@ size_t bpf_pad_note_free(struct bpf_pad_note *note) return bytes; } -/* A pass or a path-sensitive run begins */ -void bpf_pads_new_round(struct bpf_verifier_env *env) +static int copy_plans(struct bpf_verifier_env *env, struct bpf_pad_plan **to, + const struct bpf_pad_plan *plan) +{ + for (; plan; plan = plan->next, to = &(*to)->next) { + *to = kmemdup(plan, sizeof(*plan), BPF_VERIFIER_GFP); + if (!*to) + return -ENOMEM; + env->mem += plan->bytes; + (*to)->next = NULL; + (*to)->ents = !plan->ents ? NULL : + kmemdup_array(plan->ents, BPF_PAD_MAX_ENTS, sizeof(*plan->ents), + BPF_VERIFIER_GFP); + (*to)->callee = !plan->callee ? NULL : + kmemdup_array(plan->callee, roundup_pow_of_two(plan->nr_callees), + sizeof(*plan->callee), BPF_VERIFIER_GFP); + if (!(*to)->ents != !plan->ents || !(*to)->callee != !plan->callee) + return -ENOMEM; + } + return 0; +} + +static int copy_notes(struct bpf_verifier_env *env, struct bpf_pad_note **to, + const struct bpf_pad_note *note) +{ + for (; note; note = note->next, to = &(*to)->next) { + *to = kmemdup(note, sizeof(*note), BPF_VERIFIER_GFP); + if (!*to) + return -ENOMEM; + env->mem += sizeof(*note); + (*to)->next = NULL; + } + return 0; +} + +/* + * A pass adds to what the main progs before this one recorded: a pad is for the states of all of + * them. The records have just moved out of @kept. Put a copy back for the next pass. + */ +void bpf_pads_keep(struct bpf_verifier_env *env, struct bpf_pads_moved *kept) { struct bpf_insn_aux_data *aux = env->insn_aux_data; int i; - env->pads_late = false; - env->pad_skip_gen++; - for (i = 0; i < env->prog->len; i++, aux++) { - env->mem -= bpf_pad_plans_free(aux); - env->mem -= bpf_pad_note_free(aux->pad_note); - aux->pad_note = NULL; - } + for (i = 0; i < env->prog->len && !env->no_mem; i++, aux++, kept++) + env->no_mem = copy_plans(env, &kept->plans, aux->pad_plans) || + copy_notes(env, &kept->note, aux->pad_note); } /* @@ -2346,7 +2379,9 @@ static bool plan_saves_value(struct bpf_verifier_env *env, const struct bpf_pad_ int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, int subprog, struct bpf_insn *buf, struct bpf_insn_aux_data *aux) { - struct bpf_insn ld_aux[2] = { BPF_LD_IMM64(BPF_REG_1, (long)env->prog->aux) }; + u32 main = env->subprog_info[subprog].main; + struct bpf_prog *prog = main ? bpf_subprog_main_prog(env, subprog) : env->prog; + struct bpf_insn ld_aux[2] = { BPF_LD_IMM64(BPF_REG_1, (long)prog->aux) }; int value_off = env->subprog_info[subprog].value_off; int src = bpf_site_value_reg(plan->kind), dst = BPF_REG_0; bool passes_on = bpf_pad_passes_on(env, subprog, plan->kind); @@ -2354,7 +2389,7 @@ int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, int class, i, n = 0; /* the main program hands the value to the exception callback */ - if (bpf_subprog_is_main(env, subprog) && env->exception_callback_subprog) + if (main && env->exception_callback_subprog) dst = BPF_REG_1; if (plan_saves_value(env, plan, subprog)) { buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, src, value_off); @@ -2385,7 +2420,7 @@ int bpf_pad_insns(struct bpf_verifier_env *env, const struct bpf_pad_plan *plan, } else if (passes_on) { /* no call: R0 and R2 are what unwind_exit left */ } else if (!bpf_site_has_value(env, subprog, plan->kind)) { - buf[n++] = BPF_MOV64_IMM(dst, bpf_cancelled_retval(env)); + buf[n++] = BPF_MOV64_IMM(dst, env->mains[main ? main - 1 : 0].cancelled_retval); } else if (src != dst) { buf[n++] = BPF_MOV64_REG(dst, src); } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2c6dc44f4e74..f6fda89be3e6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12685,10 +12685,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, * known to leave without returning: with unwind_exit and with cancel_exit. */ for (i = 0; i < 2; i++) { - const struct bpf_subprog_info *sub = &env->subprog_info[subprog]; + struct bpf_subprog_info *sub = &env->subprog_info[subprog]; struct bpf_verifier_state *branch; bool kernel = i; + sub->call_skips_unwind |= !sub->did_unwind; + sub->call_skips_cancel |= !sub->did_cancel; if (!(kernel ? sub->did_cancel : sub->did_unwind) || !sub->bail_unwinds) continue; err = check_unwound_return(env, *insn_idx, subprog, kernel); @@ -24814,6 +24816,7 @@ static int do_check_main(struct bpf_verifier_env *env) int ret; enter_main(env, env->pass_main); + env->mains[env->pass_main].cancelled_retval = bpf_cancelled_retval(env); env->insn_idx = sub->start; ret = do_check_common(env, sub - env->subprog_info, env->prog->sleepable) ?: do_check_subprogs(env); @@ -25016,18 +25019,39 @@ static int settle_late_unwinds(struct bpf_verifier_env *env, const struct bpf_sn */ static int mark_used_sites(struct bpf_verifier_env *env, const struct bpf_snapshot *initial) { + struct bpf_subprog_info *sub = env->subprog_info; int s, err; bpf_flow_drop_guards(env); err = bpf_mark_subprogs_that_unwind(env, true); bpf_pads_mark_used(env); - for (s = 0; s < env->subprog_cnt; s++) { - env->subprog_info[s].can_unwind = initial->subprogs[s].can_unwind; - env->subprog_info[s].can_cancel = initial->subprogs[s].can_cancel; + for (s = 0; s < env->subprog_cnt; s++, sub++) { + sub->leaves_unexpected = (sub->can_unwind && sub->call_skips_unwind) || + (sub->can_cancel && sub->call_skips_cancel); + sub->can_unwind = initial->subprogs[s].can_unwind; + sub->can_cancel = initial->subprogs[s].can_cancel; } return err; } +/* The next main prog adds to the records. See if they can be settled, and put them back. */ +static int try_pads_settle(struct bpf_verifier_env *env) +{ + struct bpf_snapshot *unsettled = save_snapshot(env, NULL); + int err; + + env->no_mem |= !unsettled; + if (!unsettled) + return -ENOMEM; + restore_snapshot(env, unsettled); + bpf_pads_keep(env, unsettled->pads); + err = bpf_check_limits(env) ?: bpf_pads_settle(env); + if (!err && !pass_needs_restart(env)) + restore_snapshot(env, unsettled); + free_snapshot(env, unsettled); + return err; +} + /* Returns the first error */ static int check_funcs(struct bpf_verifier_env *env, struct bpf_snapshot *initial) { @@ -25067,7 +25091,7 @@ static int check_funcs(struct bpf_verifier_env *env, struct bpf_snapshot *initia kvfree(env->callx_index.slots); env->callx_index = (struct bpf_index){}; bpf_flow_new_pass(env); - bpf_pads_new_round(env); + bpf_pads_keep(env, initial->pads); ret = do_check_main(env) ?: bpf_check_limits(env); if (env->pads_late) { err = settle_late_unwinds(env, initial); @@ -25096,7 +25120,7 @@ static int check_pass(struct bpf_verifier_env *env, struct bpf_snapshot *initial * Only the sites that the program keeps count. Hidden slots add to the stack of a * function, so the landing pads are settled before the stack is checked. */ - ret = mark_used_sites(env, initial) ?: bpf_pads_settle(env); + ret = mark_used_sites(env, initial) ?: last ? bpf_pads_settle(env) : try_pads_settle(env); /* The limit on cost or a signal is no error of the program, see do_check_all(). */ if (ret == -EINTR || (ret && env->over_limit)) return ret; @@ -25243,6 +25267,8 @@ static bool hand_over_more(struct bpf_verifier_env *env, struct bpf_snapshot *in int s; for (s = 1; s < env->subprog_cnt; s++) { + if (initial->subprogs[s].proven) + continue; args = env->subprog_info[s].asks_args & ~initial->subprogs[s].owns_args; initial->subprogs[s].owns_args |= args; more |= args; @@ -25286,6 +25312,8 @@ static bool try_more(struct bpf_verifier_env *env, struct bpf_snapshot *initial) return false; env->handed_back = true; for (s = 1; s < env->subprog_cnt; s++) { + if (initial->subprogs[s].proven) + continue; owned |= initial->subprogs[s].owns_args; initial->subprogs[s].owns_args = 0; } @@ -25491,13 +25519,34 @@ static int do_check_all(struct bpf_verifier_env *env) static int check_proven_mains(struct bpf_verifier_env *env) { struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_subprog_info *sub = env->subprog_info; int i; + /* the pads of the callers pass on the value that a shared function leaves with */ + if (env->has_throw && + env->mains[env->pass_main].cancelled_retval != env->mains[0].cancelled_retval) { + verbose(env, "programs with bpf_throw return different values when cancelled\n"); + return -EINVAL; + } for (i = 0; i < env->prog->len; i++) { if (aux[i].needs_guard && aux[i].no_guard_edges) { verbose(env, "loop at insn %d needs may_goto for some programs only\n", i); return -EINVAL; } + if (aux[i].fault_empty && bpf_pad_plan(&aux[i], BPF_SITE_FAULT)) { + verbose(env, "insn %d needs a landing pad for some programs only\n", i); + return -EINVAL; + } + } + for (i = 0; i < env->subprog_cnt; i++, sub++) { + if (sub->leaves_unexpected || (sub->stuck && sub->soft_unwinds) || + (sub->proven && sub->bail_unwinds != sub->bail_unwinds_at_first)) { + verbose(env, "%s() can be cancelled for some programs only\n", + bpf_subprog_name(env, i)); + return -EINVAL; + } + sub->bail_unwinds_at_first = sub->bail_unwinds; + sub->proven = aux[sub->start].seen; } return 0; } @@ -25513,6 +25562,7 @@ static int do_check_mains(struct bpf_verifier_env *env) env->max_cost = max_cost; env->max_mem = max_mem; bpf_flow_new_main(env); + env->handed_back = false; err = do_check_all(env); if (!err && env->main_cnt > 1) err = check_proven_mains(env); @@ -26762,6 +26812,7 @@ static int finish_entry(struct bpf_verifier_env *env, int s) aux->max_tp_access = sub->max_tp_access; aux->changes_pkt_data = sub->changes_pkt_data; aux->might_sleep = sub->might_sleep; + aux->can_unwind = func->aux->can_unwind; aux->stack_depth = sub->stack_depth; aux->verified_insns = sub->insns_total; aux->subprog_start = start; -- 2.55.0