Commit b817d2b
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
1 file changed
+32
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22289 | 22289 | | |
22290 | 22290 | | |
22291 | 22291 | | |
| 22292 | + | |
| 22293 | + | |
| 22294 | + | |
| 22295 | + | |
| 22296 | + | |
| 22297 | + | |
| 22298 | + | |
| 22299 | + | |
| 22300 | + | |
| 22301 | + | |
| 22302 | + | |
| 22303 | + | |
| 22304 | + | |
| 22305 | + | |
| 22306 | + | |
| 22307 | + | |
| 22308 | + | |
| 22309 | + | |
| 22310 | + | |
| 22311 | + | |
| 22312 | + | |
| 22313 | + | |
| 22314 | + | |
| 22315 | + | |
| 22316 | + | |
| 22317 | + | |
| 22318 | + | |
22292 | 22319 | | |
22293 | 22320 | | |
22294 | 22321 | | |
| |||
22377 | 22404 | | |
22378 | 22405 | | |
22379 | 22406 | | |
| 22407 | + | |
| 22408 | + | |
| 22409 | + | |
| 22410 | + | |
| 22411 | + | |
22380 | 22412 | | |
22381 | 22413 | | |
22382 | 22414 | | |
| |||
0 commit comments