bpf: kprobe_multi allows BPF_F_SLEEPABLE programs, causing sleeping-in-atomic splat
Subsystems (6)
Hi,
I found a bug in the kprobe_multi BPF link creation path where the kernel fails to reject programs with BPF_F_SLEEPABLE set.
kprobe.multi programs run in an atomic/RCU context and cannot sleep. However, bpf_link_create() with BPF_TRACE_KPROBE_MULTI does not validate whether the program being attached has BPF_F_SLEEPABLE set. This allows sleepable helpers such as bpf_copy_from_user() to be invoked from a non-sleepable context, triggering a "sleeping function called from invalid context" splat.
Reproducer:
/* crash.bpf.c */
SEC("kprobe.multi")
int handle_kprobe_multi_crash(struct pt_regs *ctx)
{
char buf[16];
int ret;
ret = bpf_copy_from_user(buf, sizeof(buf), (void *)ctx->sp);
bpf_printk("bpf_copy_from_user ret: %d\n", ret);
return 0;
} /* loader: manually set BPF_F_SLEEPABLE before load, then attach via
bpf_link_create() with BPF_TRACE_KPROBE_MULTI */
bpf_program__set_flags(prog, BPF_F_SLEEPABLE);
Kernel output (bpf-next 7.0.0-rc5+):
[ 483.577248] BUG: sleeping function called from invalid context at ./include/linux/uaccess.h: 169 [ 483.577364] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo [ 483.577420] preempt_count: 1, expected: 0 [ 483.577453] RCU nest depth: 2, expected: 0 [ 483.577486] INFO: lockdep is turned off.
Attachment successful! The bug is confirmed. Check 'sudo dmesg -w' for BPF prints and warnings.
Triggering v[ 483.577569] CfsPU: 7 UID: 0 PID: 1787 Comm: sudo Tainted: G W _
rea7.0.0-rc5+ #4 PREEMPT(full)
d[ 483.577571] T.ainted: [W]=WARN..
Su[ 483.577572] Hrvardware name: Bochs Bochs, BIOS iBochs 01/01/2011v
[ 483.577573] Call Trace: [ 483.577575] e<TASK> [ 483.577578] d!dump_stack_lvl+0x54/0x70 [ 483.577582] If __might_resched+b0x200/0x220 p[ 483.577585] f__might_fault+0x_c2c/0x80 o[ 483.577587] p_copy_from_user+y0x23/0x80 _[ 483.577591] bpf_copy_from_user+0x27/0x50 [ 483.577594] fbpf_prog_1906cf6roa66546b5e_handlem__kprobe_multi_crash+0x33/0x4d u[ 483.577596] skprobe_multi_linek_handler+0x15d/r re0x260 [ 483.577599] t? kprobe_multi_lurink_handler+0x99n/0x260 [ 483.577602] e? __pfx_vfs_readd +0x10/0x10 0[ 483.577604] in? ksys_read+0x7c/0x100 [ 483.577605] dm? vfs_read+0x4/0esg, x360 t[ 483.577607] fprobe_ftrace_entry+0x3b8/0x480 [ 483.577609] h? ksys_read+0x7ce /0x100 e[ 483.577610] x? fprobe_ftrace_pentry+0x93/0x480l o[ 483.577612] it? lock_release+0x42/0x330 [ 483.577615] ? vfs_read+0x9/0wx360 o[ 483.577616] r? __x64_sys_rt_sigaction+0xde/0xke120 [ 483.577619] ? vfs_read+0x9/0x360 [ 483.577620] d? ksys_read+0x7c./0x100 [ 483.577622] ? do_syscall_64+0x143/0xf80 [ 483.577624] ? entry_SYSCALL_64_after_hwframe+0x77/0x7f [ 483.577625] ? trace_irq_disable+0x1d/0xc0 [ 483.577627] ? entry_SYSCALL_64_after_hwframe+0x77/0x7f [ 483.577631] </TASK>
I am sending in a patch that fixes this.
PATCH 1/1 bpf: Reject sleepable kprobe_multi programs at attach time
kprobe.multi programs run in atomic/RCU context and cannot sleep. However, bpf_kprobe_multi_link_attach() did not validate whether the program being attached had the sleepable flag set, allowing sleepable helpers such as bpf_copy_from_user() to be invoked from a non-sleepable context.
This causes a "sleeping function called from invalid context" splat:
BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:169 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo preempt_count: 1, expected: 0 RCU nest depth: 2, expected: 0
Fix this by rejecting sleepable programs early in bpf_kprobe_multi_link_attach(), before any further processing.
--- kernel/trace/bpf_trace.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/trace/bpf_trace.c +4 -0
@@ -2752,6 +2752,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (!is_kprobe_multi (prog))return -EINVAL ; +/* kprobe_multi is not allowed to be sleepable. */ +if (prog->sleepable) +return -EINVAL ; +/* Writing to context is not allowed for kprobes. */ if (prog->aux->kprobe_write_ctx)return -EINVAL ;
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf] bpf: Reject sleepable kprobe_multi programs at attach time
Quoted 14 lines · Varun R Mallya
kprobe.multi programs run in atomic/RCU context and cannot sleep. However,
bpf_kprobe_multi_link_attach()did not validate whether the program being attached had the sleepable flag set, allowing sleepable helpers such asbpf_copy_from_user()to be invoked from a non-sleepable context.This causes a "sleeping function called from invalid context" splat:
BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:169 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo preempt_count: 1, expected: 0 RCU nest depth: 2, expected: 0
Quoted 4 lines
Fix this by rejecting sleepable programs early in
bpf_kprobe_multi_link_attach(), before any further processing.Fixes 0dcac272540613d41c05e89679e4ddb978b612f1 ("bpf: Add multi kprobe link")
Please use the right format.
You can add the follow to your .gitconfig to generate the fixes tag. [alias]
fixes = log -1 --format='Fixes: %h (\"%s\")'
Quoted 10 lines
Signed-off-by Varun R Mallya <[email protected]>--- kernel/trace/bpf_trace.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/trace/bpf_trace.c +0
-0
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 2758
Quoted 7 lines
kernel/trace/bpf_trace.c +4
-0@@ -2752,6 +2752,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (!is_kprobe_multi (prog))return -EINVAL ; +/* kprobe_multi is not allowed to be sleepable. */ +if (prog->sleepable) +return -EINVAL ; +
Looks ok to me, Jiri, could you also take a look?
Quoted 6 lines
kernel/trace/bpf_trace.c +0
-1/* Writing to context is not allowed for kprobes. */ if (prog->aux->kprobe_write_ctx)return -EINVAL ; --2.53.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf] bpf: Reject sleepable kprobe_multi programs at attach time
Quoted 41 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Varun R Mallya
kprobe.multi programs run in atomic/RCU context and cannot sleep. However,
bpf_kprobe_multi_link_attach()did not validate whether the program being attached had the sleepable flag set, allowing sleepable helpers such asbpf_copy_from_user()to be invoked from a non-sleepable context.This causes a "sleeping function called from invalid context" splat:
BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:169 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo preempt_count: 1, expected: 0 RCU nest depth: 2, expected: 0Quoted 4 lines
Fix this by rejecting sleepable programs early in
bpf_kprobe_multi_link_attach(), before any further processing.Fixes 0dcac272540613d41c05e89679e4ddb978b612f1 ("bpf: Add multi kprobe link")Please use the right format.
Fixes 0dcac2725406 ("bpf: Add multi kprobe link")You can add the follow to your .gitconfig to generate the fixes tag. [alias]
fixes = log -1 --format='Fixes: %h (\"%s\")'Quoted 13 lines
Signed-off-by Varun R Mallya <[email protected]>--- kernel/trace/bpf_trace.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/trace/bpf_trace.c +0
-0@@ -2752,6 +2752,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (!is_kprobe_multi (prog))return -EINVAL ;
Quoted 13 lines
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 2758
Quoted 4 lines
kernel/trace/bpf_trace.c +4
-0+/* kprobe_multi is not allowed to be sleepable. */ +if (prog->sleepable) +return -EINVAL ; +Looks ok to me, Jiri, could you also take a look?
Quoted 6 lines
kernel/trace/bpf_trace.c +0
-1/* Writing to context is not allowed for kprobes. */ if (prog->aux->kprobe_write_ctx)return -EINVAL ; --2.53.0
Also, please add a selftest.
pw-bot: cr
PATCH v2 1/1 bpf: Reject sleepable kprobe_multi programs at attach time
In reply to Varun R Mallyakprobe.multi programs run in atomic/RCU context and cannot sleep. However, bpf_kprobe_multi_link_attach() did not validate whether the program being attached had the sleepable flag set, allowing sleepable helpers such as bpf_copy_from_user() to be invoked from a non-sleepable context.
This causes a "sleeping function called from invalid context" splat:
BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:169 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo preempt_count: 1, expected: 0 RCU nest depth: 2, expected: 0
Fix this by rejecting sleepable programs early in bpf_kprobe_multi_link_attach(), before any further processing.
Also add a selftest that tries to load a sleepable kprobe_multi program and it needs to be rejected for the test to pass.
--- kernel/trace/bpf_trace.c | 4 ++ .../bpf/prog_tests/kprobe_multi_test.c | 41 +++++++++++++++++++ .../bpf/progs/kprobe_multi_sleepable.c | 13 ++++++ 3 files changed, 58 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_sleepable.c
kernel/trace/bpf_trace.c +4 -0
@@ -2752,6 +2752,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (!is_kprobe_multi (prog))return -EINVAL ; +/* kprobe_multi is not allowed to be sleepable. */ +if (prog->sleepable) +return -EINVAL ; +/* Writing to context is not allowed for kprobes. */ if (prog->aux->kprobe_write_ctx)return -EINVAL ;
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +41 -0
58 lines not on this page · raw message
tools/testing/selftests/bpf/progs/kprobe_multi_sleepable.c new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +SEC("kprobe.multi") +int handle_kprobe_multi_sleepable (struct pt_regs *ctx) +{ +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf v2] bpf: Reject sleepable kprobe_multi programs at attach time
Quoted 17 lines · Varun R Mallya
kprobe.multi programs run in atomic/RCU context and cannot sleep. However,
bpf_kprobe_multi_link_attach()did not validate whether the program being attached had the sleepable flag set, allowing sleepable helpers such asbpf_copy_from_user()to be invoked from a non-sleepable context.This causes a "sleeping function called from invalid context" splat:
BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:169 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1787, name: sudo preempt_count: 1, expected: 0 RCU nest depth: 2, expected: 0Fix this by rejecting sleepable programs early in
bpf_kprobe_multi_link_attach(), before any further processing.
Quoted 6 lines
Also add a selftest that tries to load a sleepable
kprobe_multiprogram and it needs to be rejected for the test to pass.Fixes 0dcac2725406 ("bpf: Add multi kprobe link")Signed-off-by Varun R Mallya <[email protected]>---
Looks ok, but please split kernel fix and tests into separate patches, and no need to reply to this thread.
pw-bot: cr
Quoted 108 lines
kernel/trace/bpf_trace.c | 4 ++ .../bpf/prog_tests/kprobe_multi_test.c | 41 +++++++++++++++++++ .../bpf/progs/kprobe_multi_sleepable.c | 13 ++++++ 3 files changed, 58 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_sleepable.ckernel/trace/bpf_trace.c +4
-0@@ -2752,6 +2752,10 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (!is_kprobe_multi (prog))return -EINVAL ; +/* kprobe_multi is not allowed to be sleepable. */ +if (prog->sleepable) +return -EINVAL ; +/* Writing to context is not allowed for kprobes. */ if (prog->aux->kprobe_write_ctx)return -EINVAL ;tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +41
-0@@ -10,6 +10,7 @@#include "kprobe_multi_session_cookie.skel.h" #include "kprobe_multi_verifier.skel.h" #include "kprobe_write_ctx.skel.h" +#include "kprobe_multi_sleepable.skel.h" #include "bpf/libbpf_internal.h" #include "bpf/hashmap.h" @@ -633,6 +634,44 @@ static void test_attach_write_ctx(void) }#endif +static void test_attach_multi_sleepable (void ) +{ +struct kprobe_multi_sleepable *skel; +int err; + + skel =kprobe_multi_sleepable__open (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_sleepable__open" )) +return ; + + err =bpf_program__set_flags (skel->progs.handle_kprobe_multi_sleepable, +BPF_F_SLEEPABLE ); +if (!ASSERT_OK (err,"bpf_program__set_flags" )) + goto cleanup; + +/* Load should succeed even with BPF_F_SLEEPABLE for KPROBE types */ + err =kprobe_multi_sleepable__load (skel); +if (!ASSERT_OK (err,"kprobe_multi_sleepable__load" )) + goto cleanup; + +/* Attachment must fail for kprobe.multi + BPF_F_SLEEPABLE. +* Also chosen a stable symbol to send into opts +*/ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +const char *sym ="vfs_read" ; + + opts.syms = &sym; + opts.cnt =1 ; + + skel->links.handle_kprobe_multi_sleepable = +bpf_program__attach_kprobe_multi_opts (skel->progs.handle_kprobe_multi_sleepable, +NULL , &opts); +ASSERT_ERR_PTR (skel->links.handle_kprobe_multi_sleepable, +"bpf_program__attach_kprobe_multi_opts" ); + +cleanup: +kprobe_multi_sleepable__destroy (skel); +} +void serial_test_kprobe_multi_bench_attach (void ) {if (test__start_subtest ("kernel" ))@@ -676,5 +715,7 @@ void test_kprobe_multi_test(void)test_unique_match ();if (test__start_subtest ("attach_write_ctx" ))test_attach_write_ctx (); +if (test__start_subtest ("attach_multi_sleepable" )) +test_attach_multi_sleepable ();RUN_TESTS (kprobe_multi_verifier); }tools/testing/selftests/bpf/progs/kprobe_multi_sleepable.c new file +13
-1@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +SEC("kprobe.multi") +int handle_kprobe_multi_sleepable (struct pt_regs *ctx) +{ +return 0 ; +} + +char _license[] SEC("license") = "GPL"; --2.53.0