Skip to content

Commit b817d2b

Browse files
laoargregkh
authored andcommitted
bpf: Reject attaching fexit/fmod_ret to __noreturn functions
[ Upstream commit cfe816d ] If we attach fexit/fmod_ret to __noreturn functions, it will cause an issue that the bpf trampoline image will be left over even if the bpf link has been destroyed. Take attaching do_exit() with fexit for example. The fexit works as follows, bpf_trampoline + __bpf_tramp_enter + percpu_ref_get(&tr->pcref); + call do_exit() + __bpf_tramp_exit + percpu_ref_put(&tr->pcref); Since do_exit() never returns, the refcnt of the trampoline image is never decremented, preventing it from being freed. That can be verified with as follows, $ bpftool link show <<<< nothing output $ grep "bpf_trampoline_[0-9]" /proc/kallsyms ffffffffc04cb000 t bpf_trampoline_6442526459 [bpf] <<<< leftover In this patch, all functions annotated with __noreturn are rejected, except for the following cases: - Functions that result in a system reboot, such as panic, machine_real_restart and rust_begin_unwind - Functions that are never executed by tasks, such as rest_init and cpu_startup_entry - Functions implemented in assembly, such as rewind_stack_and_make_dead and xen_cpu_bringup_again, lack an associated BTF ID. With this change, attaching fexit probes to functions like do_exit() will be rejected. $ ./fexit libbpf: prog 'fexit': BPF program load failed: -EINVAL libbpf: prog 'fexit': -- BEGIN PROG LOAD LOG -- Attaching fexit/fmod_ret to __noreturn functions is rejected. Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Link: https://lore.kernel.org/r/20250318114447.75484-2-laoar.shao@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4131411 commit b817d2b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22289,6 +22289,33 @@ BTF_ID(func, __rcu_read_unlock)
2228922289
#endif
2229022290
BTF_SET_END(btf_id_deny)
2229122291

22292+
/* fexit and fmod_ret can't be used to attach to __noreturn functions.
22293+
* Currently, we must manually list all __noreturn functions here. Once a more
22294+
* robust solution is implemented, this workaround can be removed.
22295+
*/
22296+
BTF_SET_START(noreturn_deny)
22297+
#ifdef CONFIG_IA32_EMULATION
22298+
BTF_ID(func, __ia32_sys_exit)
22299+
BTF_ID(func, __ia32_sys_exit_group)
22300+
#endif
22301+
#ifdef CONFIG_KUNIT
22302+
BTF_ID(func, __kunit_abort)
22303+
BTF_ID(func, kunit_try_catch_throw)
22304+
#endif
22305+
#ifdef CONFIG_MODULES
22306+
BTF_ID(func, __module_put_and_kthread_exit)
22307+
#endif
22308+
#ifdef CONFIG_X86_64
22309+
BTF_ID(func, __x64_sys_exit)
22310+
BTF_ID(func, __x64_sys_exit_group)
22311+
#endif
22312+
BTF_ID(func, do_exit)
22313+
BTF_ID(func, do_group_exit)
22314+
BTF_ID(func, kthread_complete_and_exit)
22315+
BTF_ID(func, kthread_exit)
22316+
BTF_ID(func, make_task_dead)
22317+
BTF_SET_END(noreturn_deny)
22318+
2229222319
static bool can_be_sleepable(struct bpf_prog *prog)
2229322320
{
2229422321
if (prog->type == BPF_PROG_TYPE_TRACING) {
@@ -22377,6 +22404,11 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
2237722404
} else if (prog->type == BPF_PROG_TYPE_TRACING &&
2237822405
btf_id_set_contains(&btf_id_deny, btf_id)) {
2237922406
return -EINVAL;
22407+
} else if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
22408+
prog->expected_attach_type == BPF_MODIFY_RETURN) &&
22409+
btf_id_set_contains(&noreturn_deny, btf_id)) {
22410+
verbose(env, "Attaching fexit/fmod_ret to __noreturn functions is rejected.\n");
22411+
return -EINVAL;
2238022412
}
2238122413

2238222414
key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);

0 commit comments

Comments
 (0)