summaryrefslogtreecommitdiff
path: root/kernel/bpf/verifier.c
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@kernel.org>2025-03-19 05:07:18 +0300
committerAlexei Starovoitov <ast@kernel.org>2025-03-19 05:07:24 +0300
commit6ca21620b475e9d359c80a5db9ede7ce4abc4d03 (patch)
treef99c75ef5e0aa594a31a2d5376db8512df9620aa /kernel/bpf/verifier.c
parentf4edc66e48a694b3e6d164cc71f059de542dfaec (diff)
parentbe16ddeaae960f8d03472e8b62aae50fc469f1c9 (diff)
downloadlinux-6ca21620b475e9d359c80a5db9ede7ce4abc4d03.tar.xz
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>
Diffstat (limited to 'kernel/bpf/verifier.c')
-rw-r--r--kernel/bpf/verifier.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 3303a3605ee8..19ece8893d38 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -23215,6 +23215,33 @@ BTF_ID(func, __rcu_read_unlock)
#endif
BTF_SET_END(btf_id_deny)
+/* fexit and fmod_ret can't be used to attach to __noreturn functions.
+ * Currently, we must manually list all __noreturn functions here. Once a more
+ * robust solution is implemented, this workaround can be removed.
+ */
+BTF_SET_START(noreturn_deny)
+#ifdef CONFIG_IA32_EMULATION
+BTF_ID(func, __ia32_sys_exit)
+BTF_ID(func, __ia32_sys_exit_group)
+#endif
+#ifdef CONFIG_KUNIT
+BTF_ID(func, __kunit_abort)
+BTF_ID(func, kunit_try_catch_throw)
+#endif
+#ifdef CONFIG_MODULES
+BTF_ID(func, __module_put_and_kthread_exit)
+#endif
+#ifdef CONFIG_X86_64
+BTF_ID(func, __x64_sys_exit)
+BTF_ID(func, __x64_sys_exit_group)
+#endif
+BTF_ID(func, do_exit)
+BTF_ID(func, do_group_exit)
+BTF_ID(func, kthread_complete_and_exit)
+BTF_ID(func, kthread_exit)
+BTF_ID(func, make_task_dead)
+BTF_SET_END(noreturn_deny)
+
static bool can_be_sleepable(struct bpf_prog *prog)
{
if (prog->type == BPF_PROG_TYPE_TRACING) {
@@ -23301,6 +23328,11 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
} else if (prog->type == BPF_PROG_TYPE_TRACING &&
btf_id_set_contains(&btf_id_deny, btf_id)) {
return -EINVAL;
+ } else if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
+ prog->expected_attach_type == BPF_MODIFY_RETURN) &&
+ btf_id_set_contains(&noreturn_deny, btf_id)) {
+ verbose(env, "Attaching fexit/fmod_ret to __noreturn functions is rejected.\n");
+ return -EINVAL;
}
key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id);