From: Alexei Starovoitov The JIT puts the outgoing stack arguments of a function in its frame on the kernel stack, up to 19 slots. The combined stack size does not have them, so many frames with many slots overflow the kernel stack: BUG: TASK stack guard page was hit at ffffc900005e7f98 Count them. Also for a function with a private stack: arm64 keeps the arguments on the kernel stack. A program that is at the limit and passes more than 5 arguments can be rejected. One selftest expects the new depth. Fixes: 0f6bd5e7a804 ("bpf: Support stack arguments for bpf functions") Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 6 +++--- tools/testing/selftests/bpf/progs/verifier_private_stack.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index e7013ed8cadd..f41f0998f9cd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5712,10 +5712,11 @@ struct bpf_subprog_call_depth_info { static int subprog_stack_size(struct bpf_verifier_env *env, int s) { struct bpf_subprog_info *sub = &env->subprog_info[s]; + int args = (sub->stack_arg_cnt - bpf_in_stack_arg_cnt(sub)) * BPF_REG_SIZE; if (sub->priv_stack_mode == PRIV_STACK_ADAPTIVE) - return 0; - return round_up_stack_depth(env, sub->stack_depth); + return args; + return round_up_stack_depth(env, sub->stack_depth + args); } static void max_call_depth(struct bpf_subprog_call_depth_info *to, @@ -5912,7 +5913,6 @@ static int check_call_depth(struct bpf_verifier_env *env, struct bpf_call_graph s, depth); goto out; } - continue; } depth = dinfo[s].depth + subprog_stack_size(env, s); if (depth > env->max_stack_depth) diff --git a/tools/testing/selftests/bpf/progs/verifier_private_stack.c b/tools/testing/selftests/bpf/progs/verifier_private_stack.c index 41bd0ec54edf..ed2a40475125 100644 --- a/tools/testing/selftests/bpf/progs/verifier_private_stack.c +++ b/tools/testing/selftests/bpf/progs/verifier_private_stack.c @@ -100,7 +100,7 @@ __description("No private stack, stack arguments") __success __load_if_JITed() __arch_x86_64 -__log_level(4) __msg("stack depth max 128") +__log_level(4) __msg("stack depth max 144") __naked void no_private_stack_stack_args(void) { asm volatile (" \ -- 2.55.0 From: Alexei Starovoitov A recursive function is stopped when r10 gets to the middle of the kernel stack. The depth tells how far the first frame is from there: the low 13 bits of r10, with the random offset of the system call in them. A program that has CAP_BPF only can count the calls. Ask for CAP_PERFMON. Programs without it lose recursion, two selftests expect the refusal now. Fixes: 7b7dc9c6e8e9 ("bpf: Limit depth and time of recursive subprogs at run time") Signed-off-by: Alexei Starovoitov --- Documentation/bpf/verifier.rst | 2 +- kernel/bpf/fixups.c | 7 +++++++ tools/testing/selftests/bpf/progs/verifier_barrier.c | 11 ++++------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst index af9429ebe7b0..0bffb2b1476a 100644 --- a/Documentation/bpf/verifier.rst +++ b/Documentation/bpf/verifier.rst @@ -1292,7 +1292,7 @@ can go past the jump that a proof needs: full `Limits`_ ``CAP_PERFMON`` trip count, reads of stack that is not written, pointers as numbers, access to BTF - objects, arena + objects, arena, recursion ``CAP_PERFMON`` or no mitigations equations of pointers, equations that a join makes from constants, the end of infeasible states, skipped first trips, stack access diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index eed9d13f6a6d..aa590efc32d3 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -715,6 +715,13 @@ int bpf_insert_recursion_checks(struct bpf_verifier_env *env) for (s = 1; s < env->subprog_cnt; s++) { if (!si[s].recursive) continue; + /* The depth at which the check stops it tells the low bits of r10. */ + if (!env->allow_ptr_leaks && env->insn_aux_data[si[s].start].seen) { + verbose(env, + "recursive %s() is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", + bpf_subprog_name(env, s)); + return -EPERM; + } n = 0; /* The exit drops the frame in the stack in arena. Take it before the check. */ if (is_arena_sp_adj(&env->prog->insnsi[si[s].start])) { diff --git a/tools/testing/selftests/bpf/progs/verifier_barrier.c b/tools/testing/selftests/bpf/progs/verifier_barrier.c index 7e68fc072e3c..bc12cc17f0f2 100644 --- a/tools/testing/selftests/bpf/progs/verifier_barrier.c +++ b/tools/testing/selftests/bpf/progs/verifier_barrier.c @@ -207,7 +207,8 @@ static __naked __noinline __used int recurse(void) SEC("tc") __description("barrier: a speculative state holds more in a recursion") -__success SPECULATES __success_unpriv +__success SPECULATES +__failure_unpriv __msg_unpriv("recursive recurse() is allowed only to CAP_PERFMON") __naked void recursion(void) { ASM(START @@ -237,12 +238,8 @@ static __naked __noinline __used int recurse_in_frames(void) SEC("tc") __description("barrier: at the call of a recursion in the frames of the callers") -__success SPECULATES __success_unpriv -#ifdef SPEC_V1 -__xlated_unpriv("r1 += -1") -__xlated_unpriv("nospec") -__xlated_unpriv("call pc-") -#endif +__success SPECULATES +__failure_unpriv __msg_unpriv("recursive recurse_in_frames() is allowed only to CAP_PERFMON") __naked void recursion_in_frames(void) { ASM("call bpf_rcu_read_lock;" -- 2.55.0 From: Alexei Starovoitov check_reference_leak() lets a struct_ops program keep a reference in r0. It leaves the rest to check_return_code(), which looks at r0 only for an operator that returns a pointer. The reference is lost at the exit of other operators, of void global functions and of async callbacks, and at a tail call. The target of a tail call in a subprog returns its reference to the caller of the subprog, which takes r0 for a scalar. Skip the reference only at an exit that returns to the caller of an operator that returns a pointer. Reject a tail call in a subprog of such an operator. Fixes: 8d9f547f74c7 ("bpf: Allow struct_ops prog to return referenced kptr") Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f41f0998f9cd..b5c812bea918 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12708,10 +12708,26 @@ record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, return 0; } +static bool st_ops_returns_ptr(const struct bpf_prog *prog) +{ + const struct bpf_prog_aux *aux = prog->aux; + + return prog->type == BPF_PROG_TYPE_STRUCT_OPS && + btf_type_resolve_ptr(aux->attach_btf, aux->attach_func_proto->type, NULL); +} + +/* What leaves frame 0 gets to the caller of the program */ +static bool returns_to_kernel(const struct bpf_verifier_state *state) +{ + const struct bpf_func_state *frame = state->frame[0]; + + return !frame->in_async_callback_fn && + (!state->callers_unknown || frame->in_exception_callback_fn); +} + static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exit) { struct bpf_verifier_state *state = env->cur_state; - enum bpf_prog_type type = resolve_prog_type(env->prog); struct bpf_reg_state *reg = reg_state(env, BPF_REG_0); bool refs_lingering = false; int i; @@ -12725,8 +12741,9 @@ static int check_reference_leak(struct bpf_verifier_env *env, bool exception_exi /* Allow struct_ops programs to return a referenced kptr back to * kernel. Type checks are performed later in check_return_code. */ - if (type == BPF_PROG_TYPE_STRUCT_OPS && !exception_exit && - reg->id == state->refs[i].id) + if (!exception_exit && reg->id == state->refs[i].id && + env->prog->insnsi[env->insn_idx].code == (BPF_JMP | BPF_EXIT) && + st_ops_returns_ptr(env->prog) && returns_to_kernel(state)) continue; verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", state->refs[i].id, state->refs[i].insn_idx); @@ -13465,6 +13482,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn verbose(env, "tail calls are not allowed in fmod_ret programs with restricted return values\n"); return -EINVAL; } + /* It can return a reference. The caller of a subprog takes r0 for a scalar. */ + if (st_ops_returns_ptr(env->prog) && + (env->cur_state->curframe || !returns_to_kernel(env->cur_state))) { + verbose(env, "tail_call in a subprog would lead to reference leak\n"); + return -EINVAL; + } if (env->cur_state->curframe) { struct bpf_verifier_state *branch; -- 2.55.0 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