bpf: Fix sleepable check for tracing/lsm prog
Subsystems (7)
v4 cover bpf: Fix sleepable check for tracing/lsm prog
When CONFIG_FUNCTION_ERROR_INJECTION is disabled, a sleepable tracing prog is allowed to attach to '__x64_'-alike prefix symbols.
It is because the verifier does not verify whether the symbol is a kernel function or a bpf prog. That said, a sleepable tracing prog is allowed to attach to a bpf prog target whose name has '__x64_'-alike prefix.
For example, a sleepable fentry prog attaches to a '__x64_sys_nop' XDP prog, and copies buffer from a user pointer with bpf_copy_from_user() helper. After attaching the XDP prog to lo interface, the kernel BUG could be triggered by 'ping -c 1 -W 1 127.0.0.1':
[ 3.460756] BUG: sleeping function called from invalid context at kernel/bpf/trampoline.c:1324
Similar issue could be applied to lsm prog, when the prog->aux->attach_btf_id is in the LSM allowing list but the attach_btf is a malicious bpf prog's btf.
Fix it by disallowing sleepable tracing/lsm prog always when its target btf is not kernel's btf.
Changes:
v3 -> v4:
* Add Fixes tag for sleepable LSM case.
* Move btf check outside of 'switch (prog->type)'. (per Andrii)
* v3: https://lore.kernel.org/bpf/[email protected]/
v2 -> v3:
* Use btf_is_kernel() instead of passing 'tgt_prog'. (per Andrii)
* v2: https://lore.kernel.org/bpf/[email protected]/
v1 -> v2:
* Drop redundant 'prog->sleepable' check. (per Viktor)
* Collect Acked-by from Viktor, Thanks.
* v1: https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (2):
bpf: Fix sleepable check for tracing/lsm prog selftests/bpf: Verify rejection of sleepable tracing prog
kernel/bpf/verifier.c | 3 + .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/fentry_sleepable.c | 18 ++++++ tools/testing/selftests/bpf/progs/xdp_dummy.c | 6 ++ 4 files changed, 84 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fentry_sleepable.c
--
2.55.0
PATCH v4 1/2 bpf: Fix sleepable check for tracing/lsm prog
When CONFIG_FUNCTION_ERROR_INJECTION is disabled, a sleepable tracing prog is allowed to attach to '__x64_'-alike prefix symbols.
It is because the verifier does not verify whether the symbol is a kernel function or a bpf prog. That said, a sleepable tracing prog is allowed to attach to a bpf prog target whose name has '__x64_'-alike prefix.
For example, a sleepable fentry prog attaches to a '__x64_sys_nop' XDP prog, and copies buffer from a user pointer with bpf_copy_from_user() helper. After attaching the XDP prog to lo interface, the kernel BUG could be triggered by 'ping -c 1 -W 1 127.0.0.1':
[ 3.460756] BUG: sleeping function called from invalid context at kernel/bpf/trampoline.c:1324
Similar issue could be applied to lsm prog, when the prog->aux->attach_btf_id is in the LSM allowing list but the attach_btf is a malicious bpf prog's btf.
Fix it by disallowing sleepable tracing/lsm prog always when its target btf is not kernel's btf.
--- kernel/bpf/verifier.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/bpf/verifier.c +3 -0
@@ -19022,6 +19022,9 @@ static int btf_id_allow_sleepable(u32 btf_id, unsigned long addr, const struct bconst struct btf_type *t;const char *tname; +if (!btf_is_kernel (btf)) +return -EINVAL ; +switch (prog->type) {case BPF_PROG_TYPE_TRACING : t =btf_type_by_id (btf, btf_id);
Signature · 1 line
2.55.0
PATCH v4 2/2 selftests/bpf: Verify rejection of sleepable tracing prog
In reply to Leon HwangAdd a test to verify that the sleepable tracing prog cannot attach to a '__x64_sys' prefix prog target.
When CONFIG_FUNCTION_ERROR_INJECTION is disabled, without the fix, the test would trigger the BUG:
[ 3.460756] BUG: sleeping function called from invalid context at kernel/bpf/trampoline.c:1324
--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/fentry_sleepable.c | 18 ++++++ tools/testing/selftests/bpf/progs/xdp_dummy.c | 6 ++ 3 files changed, 81 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fentry_sleepable.c
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +57 -0
@@ -5,6 +5,7 @@#include <bpf/btf.h> #include "bind4_prog.skel.h" #include "freplace_progmap.skel.h" +#include "fentry_sleepable.skel.h" #include "xdp_dummy.skel.h" typedef int (*test_cb )(struct bpf_object *obj);
@@ -576,6 +577,60 @@ static void test_func_replace_progmap(void)freplace_progmap__destroy (skel); } +static void test_sleepable_fentry_to_xdp (void ) +{ +struct fentry_sleepable *skel =NULL ; +struct xdp_dummy *skel_xdp =NULL ; +int ifindex, prog_fd, err; +char buff[64 ] = {}; + +#ifndef __x86_64__ +test__skip (); +return ; +#endif + + ifindex =if_nametoindex ("lo" ); +if (!ASSERT_GT (ifindex,0 ,"if_nametoindex" )) +return ; + + skel_xdp =xdp_dummy__open_and_load (); +if (!ASSERT_OK_PTR (skel_xdp,"xdp_dummy__open_and_load" )) +return ; + + skel =fentry_sleepable__open (); +if (!ASSERT_OK_PTR (skel,"fentry_sleepable__open" )) + goto out; + + skel->bss->user_ptr = buff; + + prog_fd =bpf_program__fd (skel_xdp->progs.__x64_sys_nop); + err =bpf_program__set_attach_target (skel->progs.fentry_xdp, prog_fd,"__x64_sys_nop" ); +if (!ASSERT_OK (err,"bpf_program__set_attach_target" )) + goto out; + + err =fentry_sleepable__load (skel); +ASSERT_ERR (err,"fentry_sleepable__load" ); +if (err) + goto out; + + skel->links.fentry_xdp =bpf_program__attach_trace (skel->progs.fentry_xdp); +if (!ASSERT_OK_PTR (skel->links.fentry_xdp,"bpf_program__attach_trace" )) + goto out; + + skel_xdp->links.__x64_sys_nop =bpf_program__attach_xdp (skel_xdp->progs.__x64_sys_nop, + ifindex); +if (!ASSERT_OK_PTR (skel_xdp->links.__x64_sys_nop,"bpf_program__attach_xdp" )) + goto out; + + err =system ("ping -q -c 1 -W 1 127.0.0.1 > /dev/null" ); +ASSERT_OK (err,"ping" ); +ASSERT_ERR (skel->bss->retval,"retval" ); + +out: +fentry_sleepable__destroy (skel); +xdp_dummy__destroy (skel_xdp); +} +/* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf (void ) {
@@ -607,4 +662,6 @@ void serial_test_fexit_bpf2bpf(void)test_func_replace_int_with_void ();if (test__start_subtest ("freplace_void" ))test_func_replace_void (); +if (test__start_subtest ("sleepable_fentry_to_xdp" )) +test_sleepable_fentry_to_xdp (); }
tools/testing/selftests/bpf/progs/fentry_sleepable.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char LICENSE[] SEC("license") = "GPL"; + +void *user_ptr; +int retval; + +SEC("fentry.s") +int BPF_PROG (fentry_xdp ) +{ +char buff[64 ]; + + retval =bpf_copy_from_user (buff,sizeof (buff), user_ptr); +return 0 ; +}
tools/testing/selftests/bpf/progs/xdp_dummy.c +6 -0
@@ -10,4 +10,10 @@ int xdp_dummy_prog(struct xdp_md *ctx)return XDP_PASS ; } +SEC("xdp") +int __x64_sys_nop (struct xdp_md *ctx) +{ +return XDP_PASS ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v4 2/2] selftests/bpf: Verify rejection of sleepable tracing prog
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The test deliberately triggers a kernel BUG on a failing run by executing an active…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test deliberately triggers a kernel BUG on a failing run by executing an active kernel-crashing payload when the tested bug is present, disrupting automated CI environments. --
--- Patch [2]: [PATCH bpf-next v4 2/2] selftests/bpf: Verify rejection of sleepable tracing prog --- commit b744623263d5a19a3bd8be3aa0ccd3a94a64c24e
Author: Leon Hwang <[email protected]>
selftests/bpf: Verify rejection of sleepable tracing prog
This commit adds a selftest to verify that a sleepable tracing program cannot attach to a '__x64_sys' prefix prog target. It ensures the kernel properly rejects this to avoid sleeping function bugs in invalid contexts.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +2
-0@@ -576,6 +577,60 @@ static void test_func_replace_progmap(void)freplace_progmap__destroy (skel); } +static void test_sleepable_fentry_to_xdp (void ) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c static void test_func_replace_progmap(void) · line 616
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +10
-0+ err =bpf_program__set_attach_target (skel->progs.fentry_xdp, prog_fd,"__x64_sys_nop" ); +if (!ASSERT_OK (err,"bpf_program__set_attach_target" )) + goto out; + + err =fentry_sleepable__load (skel); +ASSERT_ERR (err,"fentry_sleepable__load" ); +if (err) + goto out; + + skel->links.fentry_xdp =bpf_program__attach_trace (skel->progs.fentry_xdp);
[Severity: Medium]
Will this execution path crash automated CI environments when running on a kernel without the fix?
If fentry_sleepable__load() unexpectedly succeeds and returns 0, ASSERT_ERR() will log the test failure, but the subsequent 'if (err)' check evaluates to false. This causes the test to fall through to the attach and ping payload rather than jumping to 'out'.
Since executing this payload triggers a kernel panic when the bug is present, should this error check jump to 'out' when err == 0 to fail gracefully?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewCan this LSM scenario actually occur? Looking at the code, prog->aux->attach_btf appears to be validated to be kernel BTF at program load time in bpf_prog_load():
Quoted 3 lines
Similar issue could be applied to lsm prog, when the
prog->aux->attach_btf_idis in the LSM allowing list but theattach_btfis a malicious bpf prog's btf.
Can this LSM scenario actually occur? Looking at the code, prog->aux->attach_btf appears to be validated to be kernel BTF at program load time in bpf_prog_load():
kernel/bpf/syscall.c:2991
attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
...
if (!btf_is_kernel(attach_btf)) {
/* attaching through specifying bpf_prog's BTF
* objects directly might be supported eventually
*/
btf_put(attach_btf);
err = -ENOTSUPP;The only other assignment is the bpf_get_btf_vmlinux() fallback at syscall.c:3007, so attach_btf is never a bpf prog's BTF.
The other path for non-kernel BTF reaching btf_id_allow_sleepable() is the tgt_prog branch in bpf_check_attach_target():
kernel/bpf/verifier.c:19149
btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf;
But can an LSM program have a tgt_prog? bpf_prog_load_check_attach() appears to reject it at load:
kernel/bpf/syscall.c:2752
if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && prog_type != BPF_PROG_TYPE_EXT) return -EINVAL;
And bpf_tracing_prog_attach() only accepts tgt_prog_fd for BPF_PROG_TYPE_EXT (syscall.c:3673). So the BPF_PROG_TYPE_LSM arm of btf_id_allow_sleepable() appears to be only ever reached with kernel BTF, making the 'malicious bpf prog's btf' case unreachable.
Quoted 5 lines
Fix it by disallowing sleepable tracing/lsm prog always when its target btf is not kernel's btf.
Fixes 16d9c5660692 ("bpf: Always allow sleepable programs on syscalls")Fixes 1e6c62a88215 ("bpf: Introduce sleepable BPF programs")
Does the second Fixes: tag point at the commit that introduced the bug? Looking at 1e6c62a88215 and up through 16d9c5660692, the tracing arm read:
if (!check_non_sleepable_error_inject(btf_id) && within_error_injection_list(addr))
On the tgt_prog path, addr is JIT text (tgt_prog->bpf_func / aux->func[subprog]->bpf_func, verifier.c:19409-19412), which is never on the error-injection list. With CONFIG_FUNCTION_ERROR_INJECTION=n, within_error_injection_list() is a stub returning false (include/linux/error-injection.h:16).
The fmodret fallback also does not help, because btf_kfunc_is_modify_return() checks __btf_kfunc_is_allowed(), which returns false when btf->kfunc_set_tab is NULL (kernel/bpf/btf.c:9021), and kfunc_set_tab is only populated for vmlinux/module BTF (__register_btf_kfunc_id_set() calls btf_get_module_btf(), btf.c:9160).
So a sleepable tracing prog attaching to another BPF program was already rejected before 16d9c5660692. Only the has_arch_syscall_prefix(tname) path added by 16d9c5660692 opened the hole. Keeping the 1e6c62a88215 tag could direct AUTOSEL and stable at trees back to 5.10 that do not contain the bug.
Would Fixes: 16d9c5660692 alone be more accurate?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31020655174
Quoted 65 lines · On Wed, Aug 5, 2026 at 9:44 AM <[email protected]> wrote:
Quoted 4 lines
Similar issue could be applied to lsm prog, when the
prog->aux->attach_btf_idis in the LSM allowing list but theattach_btfis a malicious bpf prog's btf.Can this LSM scenario actually occur? Looking at the code,
prog->aux->attach_btfappears to be validated to be kernel BTF at program load time inbpf_prog_load():
kernel/bpf/syscall.c:2991attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); ... if (!btf_is_kernel(attach_btf)) { /* attaching through specifying bpf_prog's BTF * objects directly might be supported eventually */ btf_put(attach_btf); err = -ENOTSUPP;The only other assignment is the
bpf_get_btf_vmlinux()fallback at syscall.c:3007, soattach_btfis never a bpf prog's BTF.The other path for non-kernel BTF reaching
btf_id_allow_sleepable()is thetgt_progbranch inbpf_check_attach_target():
kernel/bpf/verifier.c:19149btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf;But can an LSM program have a
tgt_prog?bpf_prog_load_check_attach()appears to reject it at load:
kernel/bpf/syscall.c:2752if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && prog_type != BPF_PROG_TYPE_EXT) return -EINVAL;And
bpf_tracing_prog_attach()only acceptstgt_prog_fdforBPF_PROG_TYPE_EXT(syscall.c:3673). So theBPF_PROG_TYPE_LSMarm ofbtf_id_allow_sleepable()appears to be only ever reached with kernel BTF, making the 'malicious bpf prog's btf' case unreachable.Quoted 5 lines
Fix it by disallowing sleepable tracing/lsm prog always when its target btf is not kernel's btf.
Fixes 16d9c5660692 ("bpf: Always allow sleepable programs on syscalls")Fixes 1e6c62a88215 ("bpf: Introduce sleepable BPF programs")Does the second Fixes: tag point at the commit that introduced the bug? Looking at
1e6c62a88215and up through16d9c5660692, the tracing arm read:if (!check_non_sleepable_error_inject(btf_id) && within_error_injection_list(addr))On the
tgt_progpath, addr is JIT text (tgt_prog->bpf_func/ aux->func[subprog]->bpf_func, verifier.c:19409-19412), which is never on the error-injection list. WithCONFIG_FUNCTION_ERROR_INJECTION=n,within_error_injection_list()is a stub returning false (include/linux/error-injection.h:16).The fmodret fallback also does not help, because
btf_kfunc_is_modify_return()checks__btf_kfunc_is_allowed(), which returns false whenbtf->kfunc_set_tabis NULL (kernel/bpf/btf.c:9021), andkfunc_set_tabis only populated for vmlinux/module BTF (__register_btf_kfunc_id_set()callsbtf_get_module_btf(), btf.c:9160).
Quoted 7 lines
So a sleepable tracing prog attaching to another BPF program was already rejected before
16d9c5660692. Only thehas_arch_syscall_prefix(tname)path added by16d9c5660692opened the hole. Keeping the1e6c62a88215tag could direct AUTOSEL and stable at trees back to 5.10 that do not contain the bug.Would Fixes:
16d9c5660692alone be more accurate?
Yes, I dropped second Fixes and reworded patch #1 and cover letter a tiny bit. pushed to bpf-next
Quoted 7 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/31020655174
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Wed, 5 Aug 2026 23:08:08 +0800 you wrote:
When
CONFIG_FUNCTION_ERROR_INJECTIONis disabled, a sleepable tracing prog is allowed to attach to '__x64_'-alike prefix symbols.It is because the verifier does not verify whether the symbol is a kernel function or a bpf prog. That said, a sleepable tracing prog is allowed to attach to a bpf prog target whose name has '__x64_'-alike prefix.
[...]
Here is the summary with links:
- [bpf-next,v4,1/2] bpf: Fix sleepable check for tracing/lsm prog
https://git.kernel.org/bpf/bpf-next/c/00244bdaa423 - [bpf-next,v4,2/2] selftests/bpf: Verify rejection of sleepable tracing prog https://git.kernel.org/bpf/bpf-next/c/045809795751
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html