BUG: sleepable task_file bpf programs trigger suspicious RCU usage in get_callchain_entry()
梅开彦
Reviewed-by Dongliang MuOur fuzzer discovered an RCU usage violation in the BPF subsystem. A sleepable BPF program attached to the task_file iterator calls bpf_get_stack(), which reads the perf callchain buffers via rcu_dereference() in get_callchain_entry() (kernel/events/callchain.c) while holding only rcu_read_lock_trace(), rather than a classic RCU read lock. Lockdep reports "WARNING: suspicious RCU usage" and "suspicious rcu_dereference_check() usage!".
## Root Cause
Sleepable BPF programs run only under rcu_read_lock_trace() (bpf_iter_run_prog(), kernel/bpf/bpf_iter.c); they do not hold a classic RCU read lock. The task_file iterator accepts such
programs: its target registration sets BPF_ITER_RESCHED (kernel/bpf/task_iter.c).
On the TRACING path, tracing_prog_func_proto() (which serves BPF_TRACE_ITER programs) resolves BPF_FUNC_get_stack to bpf_get_stack_proto_raw_tp via raw_tp_prog_func_proto() (kernel/trace/bpf_trace.c). Its bpf_get_stack_raw_tp() calls __bpf_get_stack() with may_fault = false, so __bpf_get_stack() skips the rcu_read_lock() that must be held for rcu_dereference(callchain_cpus_entries) in get_callchain_entry() (kernel/events/callchain.c).
When a sleepable task_file iterator program calls bpf_get_stack(), that rcu_dereference() therefore executes with only rcu_read_lock_trace() held, and lockdep fires.
## Execution Flow
``` |--- 1. Sleepable BPF_TRACING prog, expected_attach_type = BPF_TRACE_ITER, | attach_btf_id = vmlinux BTF FUNC "bpf_iter_task_file" | |--- 2. bpf(BPF_LINK_CREATE) + bpf(BPF_ITER_CREATE) -> iterator fd | |--- 3. read(iter_fd) | | | `-- bpf_seq_read -> task_file_seq_show | `-- bpf_iter_run_prog (sleepable branch) | `-- rcu_read_lock_trace() [trace RCU only, no classic RCU] | `-- bpf_get_stack_raw_tp <-- raw_tp variant, may_fault=false | `-- __bpf_get_stack(..., may_fault=false) | `-- get_perf_callchain | `-- get_callchain_entry (kernel/events/callchain.c) | `-- rcu_dereference(callchain_cpus_entries) | => lockdep: "suspicious RCU usage" ```
## Reproduction Steps
1. Load a sleepable (BPF_F_SLEEPABLE) BPF_PROG_TYPE_TRACING program with expected_attach_type = BPF_TRACE_ITER and attach_btf_id = <vmlinux BTF id of "bpf_iter_task_file">; the program calls bpf_get_stack() (helper 67).
2. Call bpf(BPF_LINK_CREATE) + bpf(BPF_ITER_CREATE) to get the iterator fd.
3. Call read() on the iterator fd: the warning fires on the first program run.
## Crash Report
The crash can be reproduced on bpf-next 4169c9c90532df9585c0a2ae513337a58f5e59ab.
```
WARNING: suspicious RCU usage
[ 85.892137] 7.3.0-g4169c9c90532 #11 Not tainted
[ 85.892681] -----------------------------
[ 85.894506] kernel/events/callchain.c:163 suspicious rcu_dereference_check() usage!
[ 85.895396]
[ 85.896455]
[ 85.897292] locks held by poc_rcu/9434: 2, last CPU#1:
[ 85.897924] #0: ff1100011f5c4ab8 (&p->lock){+.+.}-{4:4}, at: bpf_seq_read+0x63/0x1210
[ 85.898886] #1: ffffffff8f9cbbb8 (rcu_tasks_trace_srcu_struct){....}-{0:0}, at: bpf_iter_run_prog+0xc6/0xfc0
[ 85.900026]
[ 85.900643] CPU: 1 UID: 0 PID: 9434 Comm: poc_rcu Not tainted 7.3.0-g4169c9c90532 #11 PREEMPT(full)
[ 85.901681] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 85.902633] Call Trace:
[ 85.902992] <TASK>
[ 85.903307] dump_stack_lvl+0x180/0x1b0
[ 85.903816] lockdep_rcu_suspicious+0x167/0x270
[ 85.904398] get_callchain_entry+0x274/0x3e0
[ 85.904955] get_perf_callchain+0x279/0xa10
[ 85.905496] ? __pfx_get_perf_callchain+0x10/0x10
[ 85.906091] ? check_irq_usage+0xb5/0xc80
[ 85.906614] ? rcu_is_watching+0x13/0xc0
[ 85.907131] ? __bfs+0xfa/0x2b0
[ 85.907565] __bpf_get_stack+0x408/0x7e0
[ 85.908072] ? __pfx___bpf_get_stack+0x10/0x10
[ 85.908619] ? check_prev_add+0xfd/0xe60
[ 85.909117] ? check_prev_add+0xfd/0xe60
[ 85.909616] bpf_get_stack_raw_tp+0x1da/0x280
[ 85.910157] bpf_prog_a2027975767969e6+0x4e/0x64
[ 85.910718] ? __might_fault+0x13d/0x190
[ 85.911221] ? __might_fault+0xe5/0x190
[ 85.911709] ? __might_fault+0x156/0x190
[ 85.912225] bpf_iter_run_prog+0x300/0xfc0
[ 85.912750] ? __pfx_bpf_iter_run_prog+0x10/0x10
[ 85.913330] ? do_raw_spin_unlock+0x14c/0x1f0
[ 85.913886] ? _raw_spin_unlock+0x2d/0x50
[ 85.914403] ? fget_task_next+0x2c2/0x5a0
[ 85.914928] task_file_seq_show+0x139/0x1b0
[ 85.915463] ? __pfx_task_file_seq_show+0x10/0x10
[ 85.916055] ? __pfx_task_file_seq_get_next+0x10/0x10
[ 85.916690] bpf_seq_read+0x1db/0x1210
[ 85.917184] ? security_file_permission+0x7a/0x230
[ 85.917782] ? __pfx_bpf_seq_read+0x10/0x10
[ 85.918319] vfs_read+0x1eb/0xc80
[ 85.918774] ? __pfx_vfs_read+0x10/0x10
[ 85.919282] ? exc_page_fault+0xbe/0x170
[ 85.919769] ksys_read+0x126/0x240
[ 85.920208] ? __pfx_ksys_read+0x10/0x10
[ 85.920701] do_syscall_64+0x128/0x7b0
[ 85.921173] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 85.921761] RIP: 0033:0x4481cd
[ 85.922158] Code: 28 c3 e8 86 1d 00 00 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
[ 85.924050] RSP: 002b:00007ffe98982348 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
[ 85.924873] RAX: ffffffffffffffda RBX: 00007ffe98982548 RCX: 00000000004481cd
[ 85.925664] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000005
[ 85.926445] RBP: 0000000000000001 R08: 00007ffe98982548 R09: 00007ffe98982548
[ 85.927225] R10: 00007ffe98982548 R11: 0000000000000246 R12: 0000000000000001
[ 85.928010] R13: 00007ffe98982538 R14: 00000000004c17d0 R15: 0000000000000001
[ 85.928797] </TASK>
```## Proof of Concept
```c
#define _GNU_SOURCE
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef __NR_bpf
#define __NR_bpf 321
#endif
#define BITMASK(bf_off,bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type,htobe,addr,val,bf_off,bf_len) *(type*)(addr) = htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
const char* reason;
(void)reason;
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}
*(uint32_t*)0x200000001880 = 0x1a;
*(uint32_t*)0x200000001884 = 0x11;
*(uint64_t*)0x200000001888 = 0x200000001940;
*(uint8_t*)0x200000001940 = 0xbf;
STORE_BY_BITMASK(uint8_t, , 0x200000001941, 6, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001941, 1, 4, 4);
*(uint16_t*)0x200000001942 = 0;
*(uint32_t*)0x200000001944 = 0;
*(uint8_t*)0x200000001948 = 0xbf;
STORE_BY_BITMASK(uint8_t, , 0x200000001949, 1, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001949, 6, 4, 4);
*(uint16_t*)0x20000000194a = 0;
*(uint32_t*)0x20000000194c = 0;
*(uint8_t*)0x200000001950 = 0xbf;
STORE_BY_BITMASK(uint8_t, , 0x200000001951, 2, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001951, 0xa, 4, 4);
*(uint16_t*)0x200000001952 = 0;
*(uint32_t*)0x200000001954 = 0;
*(uint8_t*)0x200000001958 = 7;
STORE_BY_BITMASK(uint8_t, , 0x200000001959, 2, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001959, 0, 4, 4);
*(uint16_t*)0x20000000195a = 0;
*(uint32_t*)0x20000000195c = 0xffffff00;
*(uint8_t*)0x200000001960 = 0xb7;
STORE_BY_BITMASK(uint8_t, , 0x200000001961, 3, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001961, 0, 4, 4);
*(uint16_t*)0x200000001962 = 0;
*(uint32_t*)0x200000001964 = 0x100;
*(uint8_t*)0x200000001968 = 0xb7;
STORE_BY_BITMASK(uint8_t, , 0x200000001969, 4, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001969, 0, 4, 4);
*(uint16_t*)0x20000000196a = 0;
*(uint32_t*)0x20000000196c = 0;
*(uint8_t*)0x200000001970 = 0xb7;
STORE_BY_BITMASK(uint8_t, , 0x200000001971, 0, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001971, 0, 4, 4);
*(uint16_t*)0x200000001972 = 0;
*(uint32_t*)0x200000001974 = 0;
*(uint8_t*)0x200000001978 = 0xb7;
STORE_BY_BITMASK(uint8_t, , 0x200000001979, 5, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001979, 0, 4, 4);
*(uint16_t*)0x20000000197a = 0;
*(uint32_t*)0x20000000197c = 0;
*(uint8_t*)0x200000001980 = 7;
STORE_BY_BITMASK(uint8_t, , 0x200000001981, 3, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001981, 0, 4, 4);
*(uint16_t*)0x200000001982 = 0;
*(uint32_t*)0x200000001984 = 0;
*(uint8_t*)0x200000001988 = 0x57;
STORE_BY_BITMASK(uint8_t, , 0x200000001989, 4, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001989, 0, 4, 4);
*(uint16_t*)0x20000000198a = 0;
*(uint32_t*)0x20000000198c = -1;
*(uint8_t*)0x200000001990 = 0xbf;
STORE_BY_BITMASK(uint8_t, , 0x200000001991, 7, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001991, 6, 4, 4);
*(uint16_t*)0x200000001992 = 0;
*(uint32_t*)0x200000001994 = 0;
*(uint8_t*)0x200000001998 = 0x17;
STORE_BY_BITMASK(uint8_t, , 0x200000001999, 7, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x200000001999, 0, 4, 4);
*(uint16_t*)0x20000000199a = 0;
*(uint32_t*)0x20000000199c = 0;
*(uint8_t*)0x2000000019a0 = 0x85;
STORE_BY_BITMASK(uint8_t, , 0x2000000019a1, 0, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x2000000019a1, 0, 4, 4);
*(uint16_t*)0x2000000019a2 = 0;
*(uint32_t*)0x2000000019a4 = 0x43;
*(uint8_t*)0x2000000019a8 = 0xa5;
STORE_BY_BITMASK(uint8_t, , 0x2000000019a9, 0, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x2000000019a9, 0, 4, 4);
*(uint16_t*)0x2000000019aa = 2;
*(uint32_t*)0x2000000019ac = 0;
*(uint8_t*)0x2000000019b0 = 0x63;
STORE_BY_BITMASK(uint8_t, , 0x2000000019b1, 0xa, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x2000000019b1, 0, 4, 4);
*(uint16_t*)0x2000000019b2 = 0xfefc;
*(uint32_t*)0x2000000019b4 = 0;
*(uint8_t*)0x2000000019b8 = 0xb7;
STORE_BY_BITMASK(uint8_t, , 0x2000000019b9, 0, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x2000000019b9, 0, 4, 4);
*(uint16_t*)0x2000000019ba = 0;
*(uint32_t*)0x2000000019bc = 0;
*(uint8_t*)0x2000000019c0 = 0x95;
STORE_BY_BITMASK(uint8_t, , 0x2000000019c1, 0, 0, 4);
STORE_BY_BITMASK(uint8_t, , 0x2000000019c1, 0, 4, 4);
*(uint16_t*)0x2000000019c2 = 0;
*(uint32_t*)0x2000000019c4 = 0;
*(uint64_t*)0x200000001890 = 0x200000001a00;
memcpy((void*)0x200000001a00, "GPL\000", 4);
*(uint32_t*)0x200000001898 = 0;
*(uint32_t*)0x20000000189c = 0;
*(uint64_t*)0x2000000018a0 = 0;
*(uint32_t*)0x2000000018a8 = 0;
*(uint32_t*)0x2000000018ac = 0x10;
memset((void*)0x2000000018b0, 0, 16);
*(uint32_t*)0x2000000018c0 = 0;
*(uint32_t*)0x2000000018c4 = 0x1c;
*(uint32_t*)0x2000000018c8 = 0;
*(uint32_t*)0x2000000018cc = 0;
*(uint64_t*)0x2000000018d0 = 0;
*(uint32_t*)0x2000000018d8 = 0;
*(uint32_t*)0x2000000018dc = 0;
*(uint64_t*)0x2000000018e0 = 0;
*(uint32_t*)0x2000000018e8 = 0;
*(uint32_t*)0x2000000018ec = 209446; /* attach_btf_id: vmlinux FUNC bpf_iter_task_file */
*(uint32_t*)0x2000000018f0 = 0;
*(uint32_t*)0x2000000018f4 = 0;
*(uint64_t*)0x2000000018f8 = 0;
*(uint64_t*)0x200000001900 = 0;
*(uint32_t*)0x200000001908 = 0;
*(uint32_t*)0x20000000190c = 0;
*(uint32_t*)0x200000001910 = 0;
res = syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0x200000001880ul, /*size=*/0x94ul);
if (res != -1)
r[0] = res;
*(uint32_t*)0x200000001c40 = r[0];
*(uint32_t*)0x200000001c44 = 0;
*(uint32_t*)0x200000001c48 = 0x1c;
*(uint32_t*)0x200000001c4c = 0;
*(uint32_t*)0x200000001c50 = 0;
res = syscall(__NR_bpf, /*cmd=*/0x1cul, /*arg=*/0x200000001c40ul, /*size=*/0x20ul);
if (res != -1)
r[1] = res;
*(uint32_t*)0x200000001cc0 = r[1];
*(uint32_t*)0x200000001cc4 = 0;
res = syscall(__NR_bpf, /*cmd=*/0x21ul, /*arg=*/0x200000001cc0ul, /*size=*/8ul);
if (res != -1)
r[2] = res;
syscall(__NR_read, /*fd=*/r[2], /*buf=*/0ul, /*count=*/0ul);
return 0;
}
```## Kernel Configuration Requirements for Reproduction
The vulnerability can be triggered with the kernel config in the attachment. The kernel must be built with CONFIG_PROVE_LOCKING=y, CONFIG_LOCKDEP=y and CONFIG_DEBUG_ATOMIC_SLEEP=y for the lockdep warning to appear.