From: Alexei Starovoitov bpf_throw() does not return, and neither do the subprogs between it and the main program. With fexit or fsession on such a subprog the walk of the stack ends at the trampoline. Allow only fentry, as for subprogs that unwind. might_throw of the CFG does not see callx and counts dead code, take what the main pass found. Two selftests expect the refusal now. Fixes: f18b03fabaa9 ("bpf: Implement BPF exceptions") Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 1 + kernel/bpf/fixups.c | 1 + kernel/bpf/verifier.c | 13 ++++++++++++- tools/testing/selftests/bpf/prog_tests/exceptions.c | 12 ++++++------ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4e9a4d15af0e..92c314065d58 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1937,6 +1937,7 @@ struct bpf_prog_aux { bool changes_pkt_data; bool has_prologue; bool might_sleep; + bool might_throw; bool can_unwind; /* frames of recursive subprogs can be on the stack when it is called */ bool in_recursion; diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 151b5781606d..dc8796ee8cd5 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1181,6 +1181,7 @@ struct bpf_subprog_info { s16 fastcall_stack_off; bool has_tail_call: 1; bool might_throw: 1; + bool throws: 1; /* as the main pass found it, callx included */ bool can_unwind: 1; /* has unwind_exit */ bool recursive: 1; /* on a cycle of the call graph */ bool in_recursion: 1; /* recursive, or a recursive subprog gets to it */ diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index aa590efc32d3..7af8cb886f7d 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -1772,6 +1772,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) func[i]->aux->num_exentries = num_exentries; func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb; + func[i]->aux->might_throw = env->subprog_info[i].throws; func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data; func[i]->aux->might_sleep = env->subprog_info[i].might_sleep; func[i]->aux->can_unwind = env->subprog_info[i].can_unwind; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index b5c812bea918..967cedec6fad 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5477,6 +5477,7 @@ enum { CG_ARENA_SP = BIT(5), /* subprogs that get to r12 */ CG_CB = BIT(6), CG_GLOBAL = BIT(7), + CG_THROW = BIT(8), /* subprogs that get to bpf_throw() */ }; #define for_each_marked_subprog(cg, s, mark) \ @@ -5520,10 +5521,13 @@ static int build_call_graph(struct bpf_verifier_env *env, struct bpf_call_graph (si[s].is_async_cb ? CG_IN_ASYNC_CB : 0) | (bpf_call_graph_recursive(cg, s) ? CG_IN_RECURSION : 0) | (si[s].has_tail_call ? CG_TAIL_CALL : 0) | + (si[s].throws ? CG_THROW : 0) | (si[s].uses_arena_sp ? CG_ARENA_SP : 0) | (bpf_subprog_is_global(env, s) ? CG_GLOBAL : 0); bpf_call_graph_mark_callees(cg, CG_IN_CB | CG_IN_ASYNC_CB | CG_IN_RECURSION); - bpf_call_graph_mark_callers(cg, CG_TAIL_CALL, 0); + bpf_call_graph_mark_callers(cg, CG_TAIL_CALL | CG_THROW, 0); + for_each_marked_subprog(cg, s, CG_THROW) + si[s].throws = true; return 0; } @@ -16068,6 +16072,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, return -ENOTSUPP; } env->seen_exception = true; + env->subprog_info[cur_func(env)->subprogno].throws = true; /* In the case of the default callback, the cookie value passed * to bpf_throw becomes the return value of the program. @@ -24410,6 +24415,12 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, bpf_log(log, "Only fentry can attach to %s, which can unwind\n", tname); return -EINVAL; } + /* bpf_throw() would have to skip the trampoline that called the target */ + if (subprog && aux->func && aux->func[subprog]->aux->might_throw && + !prog_extension && prog->expected_attach_type != BPF_TRACE_FENTRY) { + bpf_log(log, "Only fentry can attach to %s, which can throw\n", tname); + return -EINVAL; + } /* What it takes of the stack is not counted, see bpf_insert_recursion_checks() */ if (aux->func && aux->func[subprog]->aux->in_recursion) { bpf_log(log, "Cannot attach to %s, which is called in a recursion\n", tname); diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c index 1bd15c4c125b..fc36cd226fb1 100644 --- a/tools/testing/selftests/bpf/prog_tests/exceptions.c +++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c @@ -266,26 +266,26 @@ static void test_exceptions_success(void) }), "", 0); if (test__start_subtest("non-throwing fexit -> throwing subprog")) - /* non-throwing fexit -> throwing subprog : OK */ - RUN_EXT(0, false, ({ + /* non-throwing fexit -> throwing subprog : BAD */ + RUN_EXT(-EINVAL, true, ({ prog = eskel->progs.pfexit; bpf_program__set_autoload(prog, true); if (!ASSERT_OK(bpf_program__set_attach_target(prog, bpf_program__fd(skel->progs.exception_throw_subprog), "throwing_subprog"), "set_attach_target")) goto done; - }), "", 0); + }), "Only fentry can attach to throwing_subprog, which can throw", 0); if (test__start_subtest("throwing fexit -> throwing subprog")) - /* throwing fexit -> throwing subprog : OK */ - RUN_EXT(0, false, ({ + /* throwing fexit -> throwing subprog : BAD */ + RUN_EXT(-EINVAL, true, ({ prog = eskel->progs.throwing_fexit; bpf_program__set_autoload(prog, true); if (!ASSERT_OK(bpf_program__set_attach_target(prog, bpf_program__fd(skel->progs.exception_throw_subprog), "throwing_subprog"), "set_attach_target")) goto done; - }), "", 0); + }), "Only fentry can attach to throwing_subprog, which can throw", 0); /* fmod_ret not allowed for subprog - Check so we remember to handle its * throwing specification compatibility with target when supported. -- 2.55.0