Skip to contentSkip to conversation

bpf: Reject attaching fexit/fmod_ret to __noreturn functions

PATCH Superseded Yafang Shao 2 participants 5 messages Mar 17, 2025 – Mar 18, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 bpf: Reject attaching fexit/fmod_ret to __noreturn functions

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.

v3>v4:
- Reject also fmod_ret (Alexei)
- Fix build warnings and remove unnecessary functions (Alexei)

v1->v2: https://lore.kernel.org/bpf/[email protected]/
- 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/[email protected]/

Yafang Shao (2):
bpf: Reject attaching fexit/fmod_ret to __noreturn functions selftests/bpf: Add selftest for attaching fexit to __noreturn
functions

 kernel/bpf/verifier.c                         | 48 +++++++++++++++++++
 .../bpf/prog_tests/fexit_noreturns.c          |  9 ++++
 .../selftests/bpf/progs/fexit_noreturns.c     | 15 ++++++
 3 files changed, 72 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c
 create mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.c
Signature · 1 line
2.43.5
#1 lore raw

PATCH v4 1/1 bpf: Reject attaching fexit/fmod_ret to __noreturn functions

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.
---
 kernel/bpf/verifier.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
kernel/bpf/verifier.c +48 -0
index 9971c03adfd5..b7d7d5c4989f 100644
@@ -22841,6 +22841,49 @@ 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)
+#define NORETURN(fn) BTF_ID(func, fn)
+#ifdef CONFIG_IA32_EMULATION
+NORETURN(__ia32_sys_exit)
+NORETURN(__ia32_sys_exit_group)
+#endif
+#ifdef CONFIG_KUNIT
+NORETURN(__kunit_abort)
+NORETURN(kunit_try_catch_throw)
+#endif
+#ifdef CONFIG_MODULES
+NORETURN(__module_put_and_kthread_exit)
+#endif
+#ifdef CONFIG_X86_64
+NORETURN(__x64_sys_exit)
+NORETURN(__x64_sys_exit_group)
+#endif
+#ifdef CONFIG_XEN_PV_SMP
+NORETURN(cpu_bringup_and_idle)
+#endif
+NORETURN(do_exit)
+NORETURN(do_group_exit)
+#if defined(CONFIG_X86) && defined(CONFIG_SMP)
+NORETURN(hlt_play_dead)
+#endif
+#ifdef CONFIG_HYPERV
+NORETURN(hv_ghcb_terminate)
+#endif
+NORETURN(kthread_complete_and_exit)
+NORETURN(kthread_exit)
+NORETURN(make_task_dead)
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+NORETURN(sev_es_terminate)
+NORETURN(snp_abort)
+#endif
+NORETURN(stop_this_cpu)
+#undef NORETURN
+BTF_SET_END(noreturn_deny)
+
 static bool can_be_sleepable(struct bpf_prog *prog)
 {
 	if (prog->type == BPF_PROG_TYPE_TRACING) {
@@ -22929,6 +22972,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);
Signature · 1 line
2.43.5
#2 lore raw
Quoted 50 lines · Yafang Shao

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.
---
 kernel/bpf/verifier.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
kernel/bpf/verifier.c +0 -0
index 9971c03adfd5..b7d7d5c4989f 100644

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22849

Quoted 9 lines
kernel/bpf/verifier.c +6 -0
@@ -22841,6 +22841,49 @@ 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)
+#define NORETURN(fn) BTF_ID(func, fn)

no need for extra macro. Just use BTF_ID(...) below.

Quoted 8 lines
kernel/bpf/verifier.c +8 -0
+#ifdef CONFIG_IA32_EMULATION
+NORETURN(__ia32_sys_exit)
+NORETURN(__ia32_sys_exit_group)
+#endif
+#ifdef CONFIG_KUNIT
+NORETURN(__kunit_abort)
+NORETURN(kunit_try_catch_throw)
+#endif

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22867

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+#ifdef CONFIG_MODULES
+NORETURN(__module_put_and_kthread_exit)
+#endif
+#ifdef CONFIG_X86_64
+NORETURN(__x64_sys_exit)
+NORETURN(__x64_sys_exit_group)
+#endif
+#ifdef CONFIG_XEN_PV_SMP
+NORETURN(cpu_bringup_and_idle)
+#endif

it's called during bringup. bpf doesn't exist at that time. Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22872

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+NORETURN(do_exit)
+NORETURN(do_group_exit)
+#if defined(CONFIG_X86) && defined(CONFIG_SMP)
+NORETURN(hlt_play_dead)
+#endif

This one is similar to panic.
Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22875

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+#ifdef CONFIG_HYPERV
+NORETURN(hv_ghcb_terminate)
+#endif

Also does 'hlt'.
Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22881

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+NORETURN(kthread_complete_and_exit)
+NORETURN(kthread_exit)
+NORETURN(make_task_dead)
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+NORETURN(sev_es_terminate)
+NORETURN(snp_abort)

drop both for the same reason as above.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22883

Quoted 2 lines
kernel/bpf/verifier.c +2 -0
+#endif
+NORETURN(stop_this_cpu)

and this one as well.

Pls make sure to resend as series of 2 patches
otherwise bpf CI will complain.

pw-bot: cr

#3 lore raw
Quoted 111 lines · Alexei Starovoitov
Quoted 50 lines · Yafang Shao

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.
---
 kernel/bpf/verifier.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
kernel/bpf/verifier.c +0 -0
index 9971c03adfd5..b7d7d5c4989f 100644

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22849

Quoted 9 lines
kernel/bpf/verifier.c +6 -0
@@ -22841,6 +22841,49 @@ 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)
+#define NORETURN(fn) BTF_ID(func, fn)

no need for extra macro. Just use BTF_ID(...) below.

Quoted 8 lines
kernel/bpf/verifier.c +8 -0
+#ifdef CONFIG_IA32_EMULATION
+NORETURN(__ia32_sys_exit)
+NORETURN(__ia32_sys_exit_group)
+#endif
+#ifdef CONFIG_KUNIT
+NORETURN(__kunit_abort)
+NORETURN(kunit_try_catch_throw)
+#endif

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22867

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+#ifdef CONFIG_MODULES
+NORETURN(__module_put_and_kthread_exit)
+#endif
+#ifdef CONFIG_X86_64
+NORETURN(__x64_sys_exit)
+NORETURN(__x64_sys_exit_group)
+#endif
+#ifdef CONFIG_XEN_PV_SMP
+NORETURN(cpu_bringup_and_idle)
+#endif

it's called during bringup. bpf doesn't exist at that time. Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22872

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+NORETURN(do_exit)
+NORETURN(do_group_exit)
+#if defined(CONFIG_X86) && defined(CONFIG_SMP)
+NORETURN(hlt_play_dead)
+#endif

This one is similar to panic.
Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22875

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+#ifdef CONFIG_HYPERV
+NORETURN(hv_ghcb_terminate)
+#endif

Also does 'hlt'.
Drop it.

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22881

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+NORETURN(kthread_complete_and_exit)
+NORETURN(kthread_exit)
+NORETURN(make_task_dead)
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+NORETURN(sev_es_terminate)
+NORETURN(snp_abort)

drop both for the same reason as above.

Quoted 7 lines

kernel/bpf/verifier.c BTF_ID(func, __rcu_read_unlock) · line 22883

Quoted 2 lines
kernel/bpf/verifier.c +2 -0
+#endif
+NORETURN(stop_this_cpu)

and this one as well.

Pls make sure to resend as series of 2 patches
otherwise bpf CI will complain.

will change it.
Thanks for your review.

Signature · 2 lines
Regards
Yafang
#4 lore raw

PATCH v4 1/1 selftests/bpf: Add selftest for attaching fexit to __noreturn functions

In reply to Yafang Shao

The reuslt:

  $ tools/testing/selftests/bpf/test_progs --name=fexit_noreturns
  #99/1    fexit_noreturns/noreturns:OK
  #99      fexit_noreturns:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED
---
 .../selftests/bpf/prog_tests/fexit_noreturns.c    |  9 +++++++++
 .../testing/selftests/bpf/progs/fexit_noreturns.c | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c
 create mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.c
tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c new file +9 -0
new file mode 100644
index 000000000000..568d3aa48a78
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "fexit_noreturns.skel.h"
+
+void test_fexit_noreturns(void)
+{
+	RUN_TESTS(fexit_noreturns);
+}
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +15 -0
new file mode 100644
index 000000000000..54654539f550
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("fexit/do_exit")
+__failure __msg("Attaching fexit/fmod_ret to __noreturn functions is rejected.")
+int BPF_PROG(noreturns)
+{
+	return 0;
+}
Signature · 1 line
2.43.5