Commit cfe816d
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.
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>1 parent f4edc66 commit cfe816d
1 file changed
+32
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23215 | 23215 | | |
23216 | 23216 | | |
23217 | 23217 | | |
| 23218 | + | |
| 23219 | + | |
| 23220 | + | |
| 23221 | + | |
| 23222 | + | |
| 23223 | + | |
| 23224 | + | |
| 23225 | + | |
| 23226 | + | |
| 23227 | + | |
| 23228 | + | |
| 23229 | + | |
| 23230 | + | |
| 23231 | + | |
| 23232 | + | |
| 23233 | + | |
| 23234 | + | |
| 23235 | + | |
| 23236 | + | |
| 23237 | + | |
| 23238 | + | |
| 23239 | + | |
| 23240 | + | |
| 23241 | + | |
| 23242 | + | |
| 23243 | + | |
| 23244 | + | |
23218 | 23245 | | |
23219 | 23246 | | |
23220 | 23247 | | |
| |||
23301 | 23328 | | |
23302 | 23329 | | |
23303 | 23330 | | |
| 23331 | + | |
| 23332 | + | |
| 23333 | + | |
| 23334 | + | |
| 23335 | + | |
23304 | 23336 | | |
23305 | 23337 | | |
23306 | 23338 | | |
| |||
0 commit comments