From: Alexei Starovoitov When loops are widened all states that enter the loop can end up going around it. If none of the states that came from the state that entered the loop left it, the program never leaves the loop either: l1: r0 += 1 goto l1 Remember in bpf_scc_visit that some state left the loop: the walk took the jump out of SCC, got to exit or bpf_throw(), or the state was pruned by the state that is fully explored. When the state that entered the loop is done, there are backedges and nothing left the loop fail the walk. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 3 +++ kernel/bpf/states.c | 40 ++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 20 ++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 981902cd5b71..60f483d7e4a9 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -900,6 +900,8 @@ struct bpf_scc_visit { struct bpf_verifier_state *entry_state; struct bpf_scc_backedge *backedges; /* list of backedges */ u32 num_backedges; + /* some state left the SCC since entry_state was set */ + bool exited; }; /* An array of bpf_scc_visit structs sharing tht same bpf_scc_callchain->scc @@ -1230,6 +1232,7 @@ int mark_chain_precision(struct bpf_verifier_env *env, int regno); int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx); bool bpf_same_loop(struct bpf_verifier_state *old, struct bpf_verifier_state *cur); +void bpf_scc_mark_exit(struct bpf_verifier_env *env, struct bpf_verifier_state *st, int insn_idx); int bpf_widen_loop_head(struct bpf_verifier_env *env, int insn_idx, bool backedge, struct bpf_verifier_state *old, struct bpf_verifier_state *cur); int bpf_update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st); diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c index 22987ce070e7..39e6052922e5 100644 --- a/kernel/bpf/states.c +++ b/kernel/bpf/states.c @@ -170,6 +170,7 @@ static int maybe_enter_scc(struct bpf_verifier_env *env, struct bpf_verifier_sta return -ENOMEM; if (!visit->entry_state) { visit->entry_state = st; + visit->exited = false; if (env->log.level & BPF_LOG_LEVEL2) verbose(env, "SCC enter %s\n", format_callchain(env, callchain)); } @@ -212,6 +213,16 @@ static int maybe_exit_scc(struct bpf_verifier_env *env, struct bpf_verifier_stat } if (visit->entry_state != st) return 0; + /* + * All states that came from @st are done. If some of them went + * around the loop and none left it, nothing that enters the loop + * the way @st did ever leaves it. + */ + if (env->widen_loops && !st->speculative && visit->backedges && !visit->exited) { + verbose(env, "loop at insn %d never exits\n", st->insn_idx); + env->widen_used = true; + return -EINVAL; + } if (env->log.level & BPF_LOG_LEVEL2) verbose(env, "SCC exit %s\n", format_callchain(env, callchain)); visit->entry_state = NULL; @@ -221,6 +232,32 @@ static int maybe_exit_scc(struct bpf_verifier_env *env, struct bpf_verifier_stat return propagate_backedges(env, visit); } +/* + * @st left the loop that @insn_idx of its current frame is in. The loop is + * left when the frame that bpf_scc_visit is for leaves it. Loops in callees + * of that frame are part of one trip around the loop of the caller. + */ +void bpf_scc_mark_exit(struct bpf_verifier_env *env, struct bpf_verifier_state *st, int insn_idx) +{ + struct bpf_scc_callchain *callchain = &env->callchain_buf; + struct bpf_scc_visit *visit; + u32 i, callsite; + + if (st->speculative || !env->insn_aux_data[insn_idx].scc) + return; + memset(callchain, 0, sizeof(*callchain)); + for (i = 0; i < st->curframe; i++) { + callsite = bpf_frame_insn_idx(st, i); + if (env->insn_aux_data[callsite].scc) + return; + callchain->callsites[i] = callsite; + } + callchain->scc = env->insn_aux_data[insn_idx].scc; + visit = scc_visit_lookup(env, callchain); + if (visit) + visit->exited = true; +} + /* Lookup an bpf_scc_visit instance corresponding to @st callchain * and add @backedge to visit->backedges. @st callchain must exist. */ @@ -1462,6 +1499,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx) if (states_equal(env, &sl->state, cur, loop ? RANGE_WITHIN : NOT_EXACT)) { hit: sl->hit_cnt++; + /* what follows the old state was seen, it may leave the loop */ + if (env->widen_loops && !sl->state.branches) + bpf_scc_mark_exit(env, cur, insn_idx); /* if previous state reached the exit with precision and * current state is equivalent to it (except precision marks) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 85b2691f81b1..06dc269991b2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19192,6 +19192,21 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state) return -EFAULT; } +/* Did the jump at @prev take the walk out of the loop @prev is in? */ +static bool left_loop(struct bpf_verifier_env *env, int prev, int insn_idx) +{ + struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_insn *insn; + + if (prev < 0 || prev >= env->prog->len || !aux[prev].scc || + aux[prev].scc == aux[insn_idx].scc) + return false; + /* the callee is not in SCC of the call, the insn after the call is */ + insn = &env->prog->insnsi[prev]; + return !((BPF_CLASS(insn->code) == BPF_JMP || BPF_CLASS(insn->code) == BPF_JMP32) && + BPF_OP(insn->code) == BPF_CALL); +} + static int do_check(struct bpf_verifier_env *env) { bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); @@ -19230,6 +19245,8 @@ static int do_check(struct bpf_verifier_env *env) state->last_insn_idx = env->prev_insn_idx; state->insn_idx = env->insn_idx; + if (env->widen_loops && left_loop(env, prev_insn_idx, env->insn_idx)) + bpf_scc_mark_exit(env, state, prev_insn_idx); /* * Record the incoming edge so active and queued paths use the same * branch-recording path. A zero-offset conditional has identical @@ -19358,6 +19375,9 @@ static int do_check(struct bpf_verifier_env *env) } else if (err < 0) { return err; } else if (err == PROCESS_BPF_EXIT) { + /* exit or bpf_throw() inside of a loop */ + if (env->widen_loops) + bpf_scc_mark_exit(env, state, env->insn_idx); goto process_bpf_exit; } else if (err == INSN_IDX_UPDATED) { } else if (err == 0) { -- 2.55.0