Skip to content

Commit 6ca2162

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-reject-attaching-fexit-fmod_ret-to-noreturn-functions'
Yafang Shao says: ==================== Attaching fexit probes to functions marked with __noreturn may lead to unpredictable behavior. To avoid this, we will reject attaching probes to such functions. Currently, there is no ideal solution, so we will hardcode a check for all __noreturn functions. Once a more robust solution is implemented, this workaround can be removed. v4->v5: - Remove unnecessary functions (Alexei) - Use BTF_ID directly (Alexei) v3->v4: https://lore.kernel.org/bpf/20250317121735.86515-1-laoar.shao@gmail.com/ - Reject also fmod_ret (Alexei) - Fix build warnings and remove unnecessary functions (Alexei) v1->v2: https://lore.kernel.org/bpf/20250223062735.3341-1-laoar.shao@gmail.com/ - keep tools/objtool/noreturns.h as is (Josh) - Add noreturns.h to objtool/sync-check.sh (Josh) - Add verbose for the reject and simplify the test case (Song) v1: https://lore.kernel.org/bpf/20250211023359.1570-1-laoar.shao@gmail.com/ ==================== Link: https://patch.msgid.link/20250318114447.75484-1-laoar.shao@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents f4edc66 + be16dde commit 6ca2162

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23215,6 +23215,33 @@ BTF_ID(func, __rcu_read_unlock)
2321523215
#endif
2321623216
BTF_SET_END(btf_id_deny)
2321723217

23218+
/* fexit and fmod_ret can't be used to attach to __noreturn functions.
23219+
* Currently, we must manually list all __noreturn functions here. Once a more
23220+
* robust solution is implemented, this workaround can be removed.
23221+
*/
23222+
BTF_SET_START(noreturn_deny)
23223+
#ifdef CONFIG_IA32_EMULATION
23224+
BTF_ID(func, __ia32_sys_exit)
23225+
BTF_ID(func, __ia32_sys_exit_group)
23226+
#endif
23227+
#ifdef CONFIG_KUNIT
23228+
BTF_ID(func, __kunit_abort)
23229+
BTF_ID(func, kunit_try_catch_throw)
23230+
#endif
23231+
#ifdef CONFIG_MODULES
23232+
BTF_ID(func, __module_put_and_kthread_exit)
23233+
#endif
23234+
#ifdef CONFIG_X86_64
23235+
BTF_ID(func, __x64_sys_exit)
23236+
BTF_ID(func, __x64_sys_exit_group)
23237+
#endif
23238+
BTF_ID(func, do_exit)
23239+
BTF_ID(func, do_group_exit)
23240+
BTF_ID(func, kthread_complete_and_exit)
23241+
BTF_ID(func, kthread_exit)
23242+
BTF_ID(func, make_task_dead)
23243+
BTF_SET_END(noreturn_deny)
23244+
2321823245
static bool can_be_sleepable(struct bpf_prog *prog)
2321923246
{
2322023247
if (prog->type == BPF_PROG_TYPE_TRACING) {
@@ -23301,6 +23328,11 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
2330123328
} else if (prog->type == BPF_PROG_TYPE_TRACING &&
2330223329
btf_id_set_contains(&btf_id_deny, btf_id)) {
2330323330
return -EINVAL;
23331+
} else if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
23332+
prog->expected_attach_type == BPF_MODIFY_RETURN) &&
23333+
btf_id_set_contains(&noreturn_deny, btf_id)) {
23334+
verbose(env, "Attaching fexit/fmod_ret to __noreturn functions is rejected.\n");
23335+
return -EINVAL;
2330423336
}
2330523337

2330623338
key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "fexit_noreturns.skel.h"
5+
6+
void test_fexit_noreturns(void)
7+
{
8+
RUN_TESTS(fexit_noreturns);
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "bpf_misc.h"
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
SEC("fexit/do_exit")
11+
__failure __msg("Attaching fexit/fmod_ret to __noreturn functions is rejected.")
12+
int BPF_PROG(noreturns)
13+
{
14+
return 0;
15+
}

0 commit comments

Comments
 (0)