bpf: Reject attaching fexit to __noreturn functions
Subsystems (5)
cover bpf: Reject attaching fexit 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. Since objtool already handles this, we will leverage its implementation.
Once a more robust solution is found, this workaround can be removed.
Yafang Shao (3):
objtool: Move noreturns.h to a common location
bpf: Reject attaching fexit to functions annotated with __noreturn selftests/bpf: Add selftest for attaching fexit to __noreturn
functions
{tools/objtool => include/linux}/noreturns.h | 0
kernel/bpf/verifier.c | 10 ++++
tools/include/linux/noreturns.h | 52 +++++++++++++++++++
tools/objtool/Documentation/objtool.txt | 3 +-
tools/objtool/check.c | 2 +-
.../bpf/prog_tests/fexit_noreturns.c | 13 +++++
.../selftests/bpf/progs/fexit_noreturns.c | 13 +++++
7 files changed, 91 insertions(+), 2 deletions(-)
rename {tools/objtool => include/linux}/noreturns.h (100%)
create mode 100644 tools/include/linux/noreturns.h
create mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c
create mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.cSignature · 1 line
2.43.5
PATCH 1/3 objtool: Move noreturns.h to a common location
It will used by bpf to reject attaching fexit prog to functions annotated with __noreturn.
---
{tools/objtool => include/linux}/noreturns.h | 0
tools/include/linux/noreturns.h | 52 ++++++++++++++++++++
tools/objtool/Documentation/objtool.txt | 3 +-
tools/objtool/check.c | 2 +-
4 files changed, 55 insertions(+), 2 deletions(-)
rename {tools/objtool => include/linux}/noreturns.h (100%)
create mode 100644 tools/include/linux/noreturns.hinclude/linux/noreturns.h renamed from tools/objtool/noreturns.h +0 -0
tools/include/linux/noreturns.h new file +52 -0
52 lines not on this page · raw message
tools/objtool/Documentation/objtool.txt +2 -1
@@ -326,7 +326,8 @@ the objtool maintainers. The call from foo() to bar() doesn't return, but bar() is missing the __noreturn annotation. NOTE: In addition to annotating the function - with __noreturn, please also add it to tools/objtool/noreturns.h. + with __noreturn, please also add it to tools/include/linux/noreturns.h and + include/linux/noreturns.h. 4. file.o: warning: objtool: func(): can't find starting instruction or
tools/objtool/check.c +1 -1
@@ -250,7 +250,7 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,#define NORETURN (func) __stringify(func), static const char * const global_noreturns[] = { -#include "noreturns.h" +#include <linux/noreturns.h> };#undef NORETURN
Signature · 1 line
2.43.5
Eduard Zingerman
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
On Tue, 2025-02-11 at 10:33 +0800, Yafang Shao wrote:
[...]
Quoted 6 lines
tools/include/linux/noreturns.h new file +0
-0
tools/include/linux/noreturns.h line 8
Quoted 8 lines
tools/include/linux/noreturns.h new file +8
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* This is a (sorted!) list of all known __noreturn functions in the kernel. +* It's needed for objtool to properly reverse-engineer the control flow graph. +* +* Yes, this is unfortunate. A better solution is in the works. +*/
I'm probably out of context for this discussion, sorry if I'm raising points already discussed.
The DW_AT_noreturn attribute is defined for DWARF. A simple script like [1] could be used to find all functions with this attribute known to DWARF. Using this script I see several functions present in my kernel but not present in the NORETURN list from this patch:
- abort
- devtmpfs_work_loop
- play_dead
- rcu_gp_kthread
- rcu_tasks_kthread
All these are marked as FUNC symbols when doing 'readelf --symbols vmlinux'.
'pahole' could be modified to look for DW_AT_noreturn attributes and add this information in BTF. E.g. by adding special btf_decl_tag to corresponding FUNC definitions. This won't work if kernel is compiled w/o BTF, of-course.
[1] https://gist.github.com/eddyz87/d8513a731dfe7e2be52b346aef1de353
Quoted 44 lines
tools/include/linux/noreturns.h new file +44
-0+NORETURN(__fortify_panic) +NORETURN(__ia32_sys_exit) +NORETURN(__ia32_sys_exit_group) +NORETURN(__kunit_abort) +NORETURN(__module_put_and_kthread_exit) +NORETURN(__stack_chk_fail) +NORETURN(__tdx_hypercall_failed) +NORETURN(__ubsan_handle_builtin_unreachable) +NORETURN(__x64_sys_exit) +NORETURN(__x64_sys_exit_group) +NORETURN(arch_cpu_idle_dead) +NORETURN(bch2_trans_in_restart_error) +NORETURN(bch2_trans_restart_error) +NORETURN(bch2_trans_unlocked_error) +NORETURN(cpu_bringup_and_idle) +NORETURN(cpu_startup_entry) +NORETURN(do_exit) +NORETURN(do_group_exit) +NORETURN(do_task_dead) +NORETURN(ex_handler_msr_mce) +NORETURN(hlt_play_dead) +NORETURN(hv_ghcb_terminate) +NORETURN(kthread_complete_and_exit) +NORETURN(kthread_exit) +NORETURN(kunit_try_catch_throw) +NORETURN(machine_real_restart) +NORETURN(make_task_dead) +NORETURN(mpt_halt_firmware) +NORETURN(nmi_panic_self_stop) +NORETURN(panic) +NORETURN(panic_smp_self_stop) +NORETURN(rest_init) +NORETURN(rewind_stack_and_make_dead) +NORETURN(rust_begin_unwind) +NORETURN(rust_helper_BUG) +NORETURN(sev_es_terminate) +NORETURN(snp_abort) +NORETURN(start_kernel) +NORETURN(stop_this_cpu) +NORETURN(usercopy_abort) +NORETURN(x86_64_start_kernel) +NORETURN(x86_64_start_reservations) +NORETURN(xen_cpu_bringup_again) +NORETURN(xen_start_kernel)
[...]
Re: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 14 lines · Eduard Zingerman
On Tue, 2025-02-11 at 10:33 +0800, Yafang Shao wrote:
[...]
Quoted 9 lines
tools/include/linux/noreturns.h new file +3
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/*
Quoted 8 lines
tools/include/linux/noreturns.h line 8
Quoted 5 lines
tools/include/linux/noreturns.h new file +5
-0+* This is a (sorted!) list of all known __noreturn functions in the kernel. +* It's needed for objtool to properly reverse-engineer the control flow graph. +* +* Yes, this is unfortunate. A better solution is in the works. +*/ I'm probably out of context for this discussion, sorry if I'm raising points already discussed.
Please refer to the discussion at
https://lore.kernel.org/bpf/CAADnVQKXgPTQsjUDB3tjZ46aPWvoEcxBCnDXro8WPtNhkGNFyg@mail.gmail.com/
for more details.
Quoted 11 lines
The
DW_AT_noreturnattribute is defined for DWARF. A simple script like [1] could be used to find all functions with this attribute known to DWARF. Using this script I see several functions present in my kernel but not present in the NORETURN list from this patch:
- abort
-devtmpfs_work_loop
-play_dead
-rcu_gp_kthread
-rcu_tasks_kthread
Quoted 8 lines
All these are marked as FUNC symbols when doing 'readelf --symbols vmlinux'.
'pahole' could be modified to look for
DW_AT_noreturnattributes and add this information in BTF. E.g. by adding specialbtf_decl_tagto corresponding FUNC definitions. This won't work if kernel is compiled w/o BTF, of-course.[1] https://gist.github.com/eddyz87/d8513a731dfe7e2be52b346aef1de353
Thank you for your example—it’s been really helpful. Following Alexei's suggestion, we’ll address this in the next step. We need to apply the fix to the current kernel and backport it, so for now, the simplest approach is to implement the current solution.
--
Regards
Yafang
Josh Poimboeuf
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
In reply to Eduard ZingermanQuoted 13 lines · Eduard Zingerman
I'm probably out of context for this discussion, sorry if I'm raising points already discussed.
The
DW_AT_noreturnattribute is defined for DWARF. A simple script like [1] could be used to find all functions with this attribute known to DWARF. Using this script I see several functions present in my kernel but not present in the NORETURN list from this patch:
- abort
-devtmpfs_work_loop
-play_dead
-rcu_gp_kthread
-rcu_tasks_kthread
Quoted 8 lines
All these are marked as FUNC symbols when doing 'readelf --symbols vmlinux'.
'pahole' could be modified to look for
DW_AT_noreturnattributes and add this information in BTF. E.g. by adding specialbtf_decl_tagto corresponding FUNC definitions. This won't work if kernel is compiled w/o BTF, of-course.[1] https://gist.github.com/eddyz87/d8513a731dfe7e2be52b346aef1de353
I also suggested this, I agree this is a much better way to go. noreturns.h is manually maintained based on objtool warnings and I'm not surprised it has missing entries.
Alexei mentioned 30k+ noreturns, but when I eliminate dups and __compiletime_assert_* it's a much smaller list:
$ ./noreturn_printer vmlinux |sort |uniq |grep -v compiletime_assert arch_cpu_idle_dead external idle.c arch_cpu_idle_dead external process.c cpu_startup_entry external cpu.h cpu_startup_entry external idle.c do_exit external exit.c do_exit external kernel.h do_group_exit external exit.c do_group_exit external task.h do_task_dead external core.c do_task_dead external task.h doublefault_shim external doublefault_32.c ex_handler_msr_mce external core.c ex_handler_msr_mce external extable.h __fortify_panic external fortify-string.h __fortify_panic external string_helpers.c i386_start_kernel external head32.c __ia32_sys_exit external syscalls_32.h __ia32_sys_exit_group external syscalls_32.h kthread_complete_and_exit external kthread.c kthread_exit external kthread.c kthread_exit external kthread.h machine_real_restart external reboot.c make_task_dead external exit.c __module_put_and_kthread_exit external main.c __module_put_and_kthread_exit external module.h nmi_panic_self_stop external panic.c panic external panic.c panic external panic.h panic_smp_self_stop external panic.c play_dead process.c rcu_gp_kthread tree.c rcu_tasks_kthread tasks.h rest_init main.c rewind_stack_and_make_dead external dumpstack.c start_kernel external main.c start_kernel external start_kernel.h stop_this_cpu external process.c stop_this_cpu external processor.h
Also, for objtool we could use something based on your program to autogenerate noreturns.h. Only problem is, objtool doesn't currently have a dependency on CONFIG_DEBUG_INFO. Another option we've considered is compiler annotations (or compiler plugins).
Signature · 1 line
Josh
Re: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 71 lines · Josh Poimboeuf
Quoted 13 lines · Eduard Zingerman
I'm probably out of context for this discussion, sorry if I'm raising points already discussed.
The
DW_AT_noreturnattribute is defined for DWARF. A simple script like [1] could be used to find all functions with this attribute known to DWARF. Using this script I see several functions present in my kernel but not present in the NORETURN list from this patch:
- abort
-devtmpfs_work_loop
-play_dead
-rcu_gp_kthread
-rcu_tasks_kthreadQuoted 8 lines
All these are marked as FUNC symbols when doing 'readelf --symbols vmlinux'.
'pahole' could be modified to look for
DW_AT_noreturnattributes and add this information in BTF. E.g. by adding specialbtf_decl_tagto corresponding FUNC definitions. This won't work if kernel is compiled w/o BTF, of-course.[1] https://gist.github.com/eddyz87/d8513a731dfe7e2be52b346aef1de353
I also suggested this, I agree this is a much better way to go.
noreturns.his manually maintained based on objtool warnings and I'm not surprised it has missing entries.Alexei mentioned 30k+ noreturns, but when I eliminate dups and __compiletime_assert_* it's a much smaller list:
$ ./noreturn_printer vmlinux |sort |uniq |grep -v compiletime_assert arch_cpu_idle_dead external idle.c arch_cpu_idle_dead external process.c cpu_startup_entry external cpu.h cpu_startup_entry external idle.c do_exit external exit.c do_exit external kernel.h do_group_exit external exit.c do_group_exit external task.h do_task_dead external core.c do_task_dead external task.h doublefault_shim external doublefault_32.c ex_handler_msr_mce external core.c ex_handler_msr_mce external extable.h __fortify_panic external fortify-string.h __fortify_panic external string_helpers.c i386_start_kernel external head32.c __ia32_sys_exit external syscalls_32.h __ia32_sys_exit_group external syscalls_32.h kthread_complete_and_exit external kthread.c kthread_exit external kthread.c kthread_exit external kthread.h machine_real_restart external reboot.c make_task_dead external exit.c __module_put_and_kthread_exit external main.c __module_put_and_kthread_exit external module.h nmi_panic_self_stop external panic.c panic external panic.c panic external panic.h panic_smp_self_stop external panic.c play_dead process.c rcu_gp_kthread tree.c rcu_tasks_kthread tasks.h rest_init main.c rewind_stack_and_make_dead external dumpstack.c start_kernel external main.c start_kernel external start_kernel.h stop_this_cpu external process.c stop_this_cpu external processor.h
Quoted 2 lines
Also, for objtool we could use something based on your program to autogenerate
noreturns.h.
Automatically generating noreturns.h is a great idea, as it would make the file accessible for use by others as well.
Quoted 2 lines
Only problem is, objtool doesn't currently
have a dependency onCONFIG_DEBUG_INFO.
Is there any reason we can't make it dependent on CONFIG_DEBUG_INFO?"
Quoted 2 lines
Another option we've considered
is compiler annotations (or compiler plugins).
Signature · 2 lines
Regards Yafang
Josh Poimboeuf
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 5 lines · Yafang Shao
Quoted 2 lines · Josh Poimboeuf
Only problem is, objtool doesn't currently
have a dependency onCONFIG_DEBUG_INFO.Is there any reason we can't make it dependent on
CONFIG_DEBUG_INFO?"
Objtool is enabled by default on x86 and is pretty much required at this point. We definitely don't want to force enable CONFIG_DEBUG_INFO as that will slow the build down considerably.
Signature · 1 line
Josh
Peter Zijlstra
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 9 lines · Josh Poimboeuf
Quoted 5 lines · Yafang Shao
Quoted 2 lines · Josh Poimboeuf
Only problem is, objtool doesn't currently
have a dependency onCONFIG_DEBUG_INFO.Is there any reason we can't make it dependent on
CONFIG_DEBUG_INFO?"Objtool is enabled by default on x86 and is pretty much required at this point.
x86_64, but yeah, disabling objtool for x86_64 is going to be *really* hard.
i386 is still limping along without -- mostly because nobody cared enough to make it work.
Quoted 2 lines
We definitely don't want to force enable
CONFIG_DEBUG_INFOas that will slow the build down considerably.
This, very much this. Same reason I hardly every use clang to build a kernel -- just too slow.
Peter Zijlstra
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
In reply to Josh PoimboeufQuoted 4 lines · Josh Poimboeuf
Also, for objtool we could use something based on your program to autogenerate
noreturns.h. Only problem is, objtool doesn't currently have a dependency onCONFIG_DEBUG_INFO. Another option we've considered is compiler annotations (or compiler plugins).
But we don't need to re-generate the file on every build, right? We can have every DBUG_INFO build verify the file is still complete.
Josh Poimboeuf
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Josh Poimboeuf
Also, for objtool we could use something based on your program to autogenerate
noreturns.h. Only problem is, objtool doesn't currently have a dependency onCONFIG_DEBUG_INFO. Another option we've considered is compiler annotations (or compiler plugins).But we don't need to re-generate the file on every build, right? We can have every
DBUG_INFObuild verify the file is still complete.
Hm, yeah we could do that. It's still not ideal, as a human now has to go fix those warnings...
But it's probably better than the current situation.
Signature · 1 line
Josh
Josh Poimboeuf
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
In reply to Yafang ShaoQuoted 9 lines · Yafang Shao
It will used by bpf to reject attaching fexit prog to functions annotated with
__noreturn.Signed-off-by Yafang Shao <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Peter Zijlstra <[email protected]>--- {tools/objtool => include/linux}/noreturns.h | 0 tools/include/linux/noreturns.h | 52 ++++++++++++++++++++
Instead of moving the file, please make a copy and add it to tools/objtool/sync-check.sh.
Signature · 1 line
Josh
Re: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 5 lines · Josh Poimboeuf
Quoted 3 lines · Yafang Shao
It will used by bpf to reject attaching fexit prog to functions annotated with
__noreturn.
Quoted 8 lines
Quoted 6 lines
Signed-off-by Yafang Shao <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Peter Zijlstra <[email protected]>--- {tools/objtool => include/linux}/noreturns.h | 0 tools/include/linux/noreturns.h | 52 ++++++++++++++++++++Instead of moving the file, please make a copy
Are you suggesting we keep tools/objtool/noreturns.h as is and simply copy it to include/linux/noreturns.h? In other words, the two files would be tools/objtool/noreturns.h and include/linux/noreturns.h.
Quoted 2 lines
and add it to
tools/objtool/sync-check.sh.
Sure.
Signature · 2 lines
Regards Yafang
Josh Poimboeuf
maintainerRe: [PATCH bpf-next 1/3] objtool: Move noreturns.h to a common location
Quoted 13 lines · Yafang Shao
Quoted 12 lines · Josh Poimboeuf
Quoted 9 lines · Yafang Shao
It will used by bpf to reject attaching fexit prog to functions annotated with
__noreturn.Signed-off-by Yafang Shao <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Peter Zijlstra <[email protected]>--- {tools/objtool => include/linux}/noreturns.h | 0 tools/include/linux/noreturns.h | 52 ++++++++++++++++++++
Quoted 5 lines
Quoted 1 line
Instead of moving the file, please make a copy
Are you suggesting we keep
tools/objtool/noreturns.has is and simply copy it toinclude/linux/noreturns.h? In other words, the two files would betools/objtool/noreturns.handinclude/linux/noreturns.h.
Right.
Signature · 1 line
Josh
PATCH 2/3 bpf: Reject attaching fexit to functions annotated with __noreturn
In reply to Yafang ShaoIf we attach fexit to a function annotated with __noreturn, 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() 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. This 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
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 -- processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'fexit': failed to load: -EINVAL libbpf: failed to load object 'fexit_bpf' libbpf: failed to load BPF skeleton 'fexit_bpf': -EINVAL failed to load BPF object -22
--- kernel/bpf/verifier.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
kernel/bpf/verifier.c +10 -0
@@ -22841,6 +22841,13 @@ BTF_ID(func, __rcu_read_unlock)#endif BTF_SET_END(btf_id_deny) +/* The functions annotated with __noreturn are denied. */ +BTF_SET_START(fexit_deny) +#define NORETURN(fn) BTF_ID(func, fn) +#include <linux/noreturns.h> +#undef NORETURN +BTF_SET_END (fexit_deny) + static bool can_be_sleepable(struct bpf_prog *prog) {if (prog->type ==BPF_PROG_TYPE_TRACING ) {
@@ -22929,6 +22936,9 @@ 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 && +btf_id_set_contains (&fexit_deny, btf_id)) { +return -EINVAL ; } key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id);
Signature · 1 line
2.43.5
Song Liu
reviewerRe: [PATCH bpf-next 2/3] bpf: Reject attaching fexit to functions annotated with __noreturn
Quoted 60 lines · Yafang Shao
If we attach fexit to a function annotated with
__noreturn, it will cause an issue that the bpf trampoline image will be left over even if the bpf link has been destroyed. Take attachingdo_exit()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. This can be verified with as follows,$ bpftool link show <<<< nothing output $ grep "bpf_trampoline_[0-9]" /proc/kallsyms ffffffffc04cb000 t bpf_trampoline_6442526459 [bpf] <<<< leftoverWith 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 -- processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'fexit': failed to load: -EINVAL libbpf: failed to load object 'fexit_bpf' libbpf: failed to load BPF skeleton 'fexit_bpf': -EINVAL failed to load BPF object -22Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/verifier.c | 10 ++++++++++ 1 file changed, 10 insertions(+)kernel/bpf/verifier.c +7
-0@@ -22841,6 +22841,13 @@ BTF_ID(func, __rcu_read_unlock)#endif BTF_SET_END(btf_id_deny) +/* The functions annotated with __noreturn are denied. */ +BTF_SET_START(fexit_deny) +#define NORETURN(fn) BTF_ID(func, fn) +#include <linux/noreturns.h> +#undef NORETURN +BTF_SET_END (fexit_deny) + static bool can_be_sleepable(struct bpf_prog *prog) {if (prog->type ==BPF_PROG_TYPE_TRACING ) {
kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 22940
Quoted 5 lines
kernel/bpf/verifier.c +2
-0@@ -22929,6 +22936,9 @@ 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 && +btf_id_set_contains (&fexit_deny, btf_id)) {
Please add a verifier log (with verbose()) here, so that the user knows why the program failed to attach.
Thanks,
Song
Quoted 7 lines
kernel/bpf/verifier.c +1
-1+return -EINVAL ; } key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); --2.43.5
Re: [PATCH bpf-next 2/3] bpf: Reject attaching fexit to functions annotated with __noreturn
Quoted 62 lines · Song Liu
Quoted 60 lines · Yafang Shao
If we attach fexit to a function annotated with
__noreturn, it will cause an issue that the bpf trampoline image will be left over even if the bpf link has been destroyed. Take attachingdo_exit()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. This can be verified with as follows,$ bpftool link show <<<< nothing output $ grep "bpf_trampoline_[0-9]" /proc/kallsyms ffffffffc04cb000 t bpf_trampoline_6442526459 [bpf] <<<< leftoverWith 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 -- processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'fexit': failed to load: -EINVAL libbpf: failed to load object 'fexit_bpf' libbpf: failed to load BPF skeleton 'fexit_bpf': -EINVAL failed to load BPF object -22Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/verifier.c | 10 ++++++++++ 1 file changed, 10 insertions(+)kernel/bpf/verifier.c +7
-0@@ -22841,6 +22841,13 @@ BTF_ID(func, __rcu_read_unlock)#endif BTF_SET_END(btf_id_deny) +/* The functions annotated with __noreturn are denied. */ +BTF_SET_START(fexit_deny) +#define NORETURN(fn) BTF_ID(func, fn) +#include <linux/noreturns.h> +#undef NORETURN +BTF_SET_END (fexit_deny) + static bool can_be_sleepable(struct bpf_prog *prog) {if (prog->type ==BPF_PROG_TYPE_TRACING ) {@@ -22929,6 +22936,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
Quoted 8 lines
kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 22940
Quoted 5 lines
kernel/bpf/verifier.c +2
-0}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 && +btf_id_set_contains (&fexit_deny, btf_id)) {Please add a verifier log (with
verbose()) here, so that the user knows why the program failed to attach.
Good suggestion. I will add it.
Signature · 2 lines
Regards Yafang
PATCH 3/3 selftests/bpf: Add selftest for attaching fexit to __noreturn functions
In reply to Yafang ShaoThe reuslt:
$ tools/testing/selftests/bpf/test_progs --name=fexit_noreturns #99 fexit_noreturns:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--- .../selftests/bpf/prog_tests/fexit_noreturns.c | 13 +++++++++++++ tools/testing/selftests/bpf/progs/fexit_noreturns.c | 13 +++++++++++++ 2 files changed, 26 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 +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "fexit_noreturns.skel.h" + +void test_fexit_noreturns (void ) +{ +struct fexit_noreturns *fexit_skel; + + fexit_skel =fexit_noreturns__open_and_load (); +ASSERT_NULL (fexit_skel,"fexit_load" ); +ASSERT_EQ (errno,EINVAL ,"can't load fexit_noreturns" ); +}
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +SEC("fexit/do_exit") +int BPF_PROG (noreturns ) +{ +return 0 ; +}
Signature · 1 line
2.43.5
Song Liu
reviewerRe: [PATCH bpf-next 3/3] selftests/bpf: Add selftest for attaching fexit to __noreturn functions
Quoted 40 lines · Yafang Shao
The reuslt:
$ tools/testing/selftests/bpf/test_progs --name=fexit_noreturns #99 fexit_noreturns:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by Yafang Shao <[email protected]>--- .../selftests/bpf/prog_tests/fexit_noreturns.c | 13 +++++++++++++ tools/testing/selftests/bpf/progs/fexit_noreturns.c | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c create mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.ctools/testing/selftests/bpf/prog_tests/fexit_noreturns.c new file +13
-0@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "fexit_noreturns.skel.h" + +void test_fexit_noreturns (void ) +{ +struct fexit_noreturns *fexit_skel; + + fexit_skel =fexit_noreturns__open_and_load (); +ASSERT_NULL (fexit_skel,"fexit_load" ); +ASSERT_EQ (errno,EINVAL ,"can't load fexit_noreturns" ); +}tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +0
-0
tools/testing/selftests/bpf/progs/fexit_noreturns.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +9
-0@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +SEC ("fexit/do_exit" )
We can add:
__failure __msg(<verifier log added in 2/3>)
here.
Quoted 7 lines
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +4
-1+int BPF_PROG (noreturns ) +{ +return 0 ; +} --2.43.5
Then, test_fexit_noreturns above can simply be:
void test_fexit_noreturns(void)
{
RUN_TESTS(fexit_noreturns);
}Thanks,
Song
Re: [PATCH bpf-next 3/3] selftests/bpf: Add selftest for attaching fexit to __noreturn functions
Quoted 67 lines · Song Liu
Quoted 40 lines · Yafang Shao
The reuslt:
$ tools/testing/selftests/bpf/test_progs --name=fexit_noreturns #99 fexit_noreturns:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by Yafang Shao <[email protected]>--- .../selftests/bpf/prog_tests/fexit_noreturns.c | 13 +++++++++++++ tools/testing/selftests/bpf/progs/fexit_noreturns.c | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fexit_noreturns.c create mode 100644 tools/testing/selftests/bpf/progs/fexit_noreturns.ctools/testing/selftests/bpf/prog_tests/fexit_noreturns.c new file +13
-0@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "fexit_noreturns.skel.h" + +void test_fexit_noreturns (void ) +{ +struct fexit_noreturns *fexit_skel; + + fexit_skel =fexit_noreturns__open_and_load (); +ASSERT_NULL (fexit_skel,"fexit_load" ); +ASSERT_EQ (errno,EINVAL ,"can't load fexit_noreturns" ); +}tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +0
-0tools/testing/selftests/bpf/progs/fexit_noreturns.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +9
-0@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +SEC ("fexit/do_exit" )We can add:
__failure__msg(<verifier log added in 2/3>)here.
Quoted 7 lines
tools/testing/selftests/bpf/progs/fexit_noreturns.c new file +4
-1+int BPF_PROG (noreturns ) +{ +return 0 ; +} --2.43.5
Then,
test_fexit_noreturnsabove can simply be:
Quoted 5 lines
void
test_fexit_noreturns(void){ RUN_TESTS(fexit_noreturns); }
Thanks for your suggestion; it really helps simplify the code.
Signature · 2 lines
Regards Yafang