Closing a perf event on a system call tracepoint can crash a task that is entering a system call at the same time. With perf and ftrace both tracing system call entry, the task calls ftrace_syscall_enter() with the perf probe's NULL data instead of its trace_array: BUG: kernel NULL pointer dereference, address: 0000000000000188 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page Oops: Oops: 0000 [#1] SMP NOPTI CPU: 1 UID: 0 PID: 106 Comm: tpvictim Not tainted 7.4.0-rc5-dirty #4 PREEMPT(full) RIP: 0010:ftrace_syscall_enter+0x88/0x310 Call Trace: ? vprintk_emit+0x20c/0x2f0 trace_syscall_enter+0x73/0xc0 do_syscall_64+0x446/0x4f0 entry_SYSCALL_64_after_hwframe+0x77/0x7f When a tracepoint ends up with a single probe (2 -> 1, or 1 -> 0 -> 1), its static call is switched from __traceiter_*() to that probe. Readers load tp->funcs[0].data before the static call, so tp_rcu_cond_sync() first makes sure that an RCU and a tracepoint_srcu grace period have elapsed since the old data was replaced. Since commit a363d27cdbc2 ("tracing: Allow system call tracepoints to handle page faults"), system call tracepoints protect their probes with RCU Tasks Trace only, and neither grace period waits for those readers. A task preempted between the data load and the static call then resumes into the new probe with the old data: CPU 0 CPU 1 __do_trace_sys_enter() guard(rcu_tasks_trace)(); __data = funcs[0].data; // NULL perf_trace_event_unreg() tracepoint_remove_func() tp_rcu_cond_sync(); tracepoint_update_call(); static_call(tp_func_sys_enter)(__data, ...); ftrace_syscall_enter(NULL, ...); Commit 2e8a12b82c40 ("tracing: Fix syscall tracepoint use-after-free") made release_probes() defer freeing old probe arrays until after an RCU Tasks Trace grace period, but left the waits before a static call switch unchanged. Keep the static call of faultable tracepoints on __traceiter_*(), which ignores the data it is passed and loads each probe together with its data from the same tp->funcs entry. Waiting for an RCU Tasks Trace grace period in tp_rcu_cond_sync() would also close the race, but under tracepoints_mutex. System call probes can sleep in their read-side sections while faulting in user memory, and every tracepoint registration would then wait for the fault. The cost is an indirect call per system call while the tracepoint has a single probe, as on kernels without CONFIG_HAVE_STATIC_CALL, which always use __traceiter_*(). The splat above is from x86-64 with CONFIG_PREEMPT=y and a test-only mdelay(), with preemption enabled, between the data load and the static call. A 1 -> 0 -> 1 sequence (perf, then a BPF raw tracepoint) crashes the same way in bpf_trace_run2(). With this patch, neither crashes. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the tests. Fixes: a363d27cdbc2 ("tracing: Allow system call tracepoints to handle page faults") Signed-off-by: Andrea Parri --- Reproduced on v7.4-rc5 in x86-64 KVM guests (CONFIG_PREEMPT=y) with the test-only hook below, which busy-waits once for 2 s, with preemption enabled, between the data load and the static call of one task. Without such a delay the window is a few instructions, and I did not try to hit it by stress alone. Each sequence oopsed in 3 of 3 runs without this patch and in 0 of 3 runs with it. With this patch, the BPF attach in the 1 -> 0 -> 1 case returns while the delayed task still holds the old data, and that task resumes without crashing. An earlier version of this patch waited for an RCU Tasks Trace grace period in tp_rcu_cond_sync() instead. With a sys_enter_write probe parked in a userfaultfd fault, attaching a BPF raw tracepoint to sched_switch then blocked in tracepoint_add_func() until the fault was resolved (tested on v7.4-rc4). With this patch the attach returns after 54 ms, and after 59 ms without it. Calling __traceiter_*() directly from __DECLARE_TRACE_SYSCALL() would also fix the bug. I kept the static call, now always targeting the iterator, so that tracepoint_update_call() remains the one place that picks the target and the fix stays in kernel/tracepoint.c. I can switch to the direct call if that is preferred. With perf_syscall_enter() as the only sys_enter probe, returning early, getppid() takes 155.9 ns with this patch and 153.7 ns without it (median of 5 runs, each the best of 5 batches of 2M calls, in a nested KVM guest). With no probe attached, it takes 124.9 and 124.4 ns. ftracetest, with tools/testing/selftests/ftrace/config plus CONFIG_PREEMPT=y: 164 passed, 1 unresolved (checkbashisms is not installed), 12 unsupported and 2 xfailed, with and without this patch. Test-only hook, applying to v7.4-rc5 with or without this patch: diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index e0d838c9ce938..863231ad0cfe0 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -227,8 +227,23 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) static_call(tp_func_##name)(__data, args); \ } \ } while (0) +/* TEST ONLY: widen the window between loading __data and the static call. */ +extern void tp_syscall_race_delay(void); +#define __DO_TRACE_CALL_SYSCALL(name, args) \ + do { \ + struct tracepoint_func *it_func_ptr; \ + void *__data; \ + it_func_ptr = \ + rcu_dereference_raw((&__tracepoint_##name)->funcs); \ + if (it_func_ptr) { \ + __data = (it_func_ptr)->data; \ + tp_syscall_race_delay(); \ + static_call(tp_func_##name)(__data, args); \ + } \ + } while (0) #else #define __DO_TRACE_CALL(name, args) __traceiter_##name(NULL, args) +#define __DO_TRACE_CALL_SYSCALL(name, args) __DO_TRACE_CALL(name, args) #endif /* CONFIG_HAVE_STATIC_CALL */ /* @@ -336,7 +351,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) { \ TRACEPOINT_CHECK(name) \ guard(rcu_tasks_trace)(); \ - __DO_TRACE_CALL(name, TP_ARGS(args)); \ + __DO_TRACE_CALL_SYSCALL(name, TP_ARGS(args)); \ } \ static inline void trace_##name(proto) \ { \ diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index dffef52a807bc..87d4356078017 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -3,6 +3,8 @@ * Copyright (C) 2008-2014 Mathieu Desnoyers */ #include +#include +#include #include #include #include @@ -66,6 +68,45 @@ static void tp_rcu_cond_sync(enum tp_transition_sync sync) snapshot->ongoing = false; } +/* + * TEST ONLY: one-shot busy-wait inside faultable syscall tracepoints, after + * __data has been loaded and before the static call, for the task whose comm + * matches race_comm. Busy-wait with preemption enabled, so that regular RCU + * (and tracepoint_srcu) grace periods can complete meanwhile, exactly as they + * would if the task were preempted at that point. + * + * /sys/module/tracepoint/parameters/race_comm (write with echo -n) + * /sys/module/tracepoint/parameters/race_delay_ms (cleared once consumed) + * /sys/module/tracepoint/parameters/race_state (0 idle, 1 spinning, 2 done) + */ +static char race_comm[TASK_COMM_LEN]; +module_param_string(race_comm, race_comm, sizeof(race_comm), 0644); +static unsigned int race_delay_ms; +module_param(race_delay_ms, uint, 0644); +static int race_state; +module_param(race_state, int, 0444); + +notrace void tp_syscall_race_delay(void) +{ + unsigned int ms; + + if (likely(!READ_ONCE(race_delay_ms))) + return; + if (strncmp(current->comm, race_comm, TASK_COMM_LEN)) + return; + ms = xchg(&race_delay_ms, 0); + if (!ms) + return; + pr_info("tp-race: %s/%d loaded __data, spinning %u ms (preemptible=%d)\n", + current->comm, current->pid, ms, preemptible()); + WRITE_ONCE(race_state, 1); + mdelay(ms); + WRITE_ONCE(race_state, 2); + pr_info("tp-race: %s/%d resuming into static call\n", + current->comm, current->pid); +} +EXPORT_SYMBOL_GPL(tp_syscall_race_delay); + /* Set to 1 to enable tracepoint debug output */ static const int tracepoint_debug; With the hook, as root, the following crashes v7.4-rc5 in 3 of 3 runs, and does not crash it with this patch. This was run under virtme-ng, with "vng --kconfig" plus CONFIG_PREEMPT=y, CONFIG_FTRACE_SYSCALLS=y, CONFIG_PERF_EVENTS=y and CONFIG_KALLSYMS_ALL=y, booted with preempt=full: P=/sys/module/tracepoint/parameters E=/sys/kernel/tracing/events/syscalls/sys_enter_openat echo -n dd > $P/race_comm # sys_enter probe 1: perf_syscall_enter(), data NULL perf stat -a -e syscalls:sys_enter_openat & PERF=$!; sleep 1 echo 1 > $E/enable # probe 2: ftrace_syscall_enter(), data tr echo 2000 > $P/race_delay_ms dd if=/dev/zero of=/dev/null count=1 & # loads NULL, spins 2 s sleep 0.5; kill -INT $PERF # perf exits: 2 -> 1 sleep 3; dmesg | grep -A12 'BUG:' The trigger script used for the counts above, including the 1 -> 0 -> 1 variant, the userfaultfd test and the benchmark are available on request. kernel/tracepoint.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index dffef52a807bc..11401df67e7b2 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -275,7 +275,14 @@ static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func /* Synthetic events do not have static call sites */ if (!tp->static_call_key) return; - if (nr_func_state(tp_funcs) == TP_FUNC_1) + /* + * Faultable tracepoint readers can be preempted between loading + * funcs[0].data and the static call, and tp_rcu_cond_sync() does not + * wait for them. Keep the iterator, which loads each probe together + * with its data. + */ + if (nr_func_state(tp_funcs) == TP_FUNC_1 && + !tracepoint_is_faultable(tp)) func = tp_funcs[0].func; __static_call_update(tp->static_call_key, tp->static_call_tramp, func); } @@ -319,7 +326,7 @@ static int tracepoint_add_func(struct tracepoint *tp, * 1->0->1 transition sequence. */ tp_rcu_cond_sync(TP_TRANSITION_SYNC_1_0_1); - /* Set static call to first function */ + /* Set static call to first function, unless faultable */ tracepoint_update_call(tp, tp_funcs); /* Both iterator and static call handle NULL tp->funcs */ rcu_assign_pointer(tp->funcs, tp_funcs); @@ -401,7 +408,7 @@ static int tracepoint_remove_func(struct tracepoint *tp, if (tp_funcs[0].data != old[0].data) tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1); tp_rcu_cond_sync(TP_TRANSITION_SYNC_N_2_1); - /* Set static call to first function */ + /* Set static call to first function, unless faultable */ tracepoint_update_call(tp, tp_funcs); break; case TP_FUNC_2: /* N->N-1 (N>2) */ -- 2.53.0