Commit
3cc117514d69067a94d6c7479730dce41e678f50
tracing: Fix crash in print_graph_function_event()
tracing_set_tracer() assigns the flags of the newly selected tracer
with:
tr->current_trace_flags = t->flags ? : t->tracer->flags;
If the new tracer defines neither static flags nor default flags, like
hwlat, irqsoff or wakeup, add_tracer() has no per instance flags copy
for it, and both operands are NULL, so tr->current_trace_flags ends up
being NULL as well.
The ring buffer is not cleared when switching tracers: it is reset
inside tracer_init(), after current_trace_flags was already assigned.
If the buffer still holds function_graph entries of the previous
tracer, printing one of them dereferences the NULL pointer. This was
hit by cat trace during an ftrace stress test:
BUG: kernel NULL pointer dereference, address: 0000000000000000
#PF: supervisor read access in kernel mode
RIP: 0010:print_graph_function_event+0xe/0x20
Call Trace:
s_show+0x4d/0x110
seq_read_iter+0x2d4/0x490
seq_read+0x130/0x170
vfs_read+0xb5/0x370
ksys_read+0x6d/0xe0
do_syscall_64+0xbc/0x450
The snapshot buffer is not reset at all, so this can be reproduced
without a race:
echo function_graph > /sys/kernel/tracing/current_tracer
echo 1 > /sys/kernel/tracing/snapshot
echo hwlat > /sys/kernel/tracing/current_tracer
cat /sys/kernel/tracing/snapshot
Rather than teaching every user of tr->current_trace_flags about this
corner case, make sure it is never NULL: when the newly selected
tracer has no flags at all, tracing_set_tracer() falls back to a
static empty no_tracer_flags. run_tracer_selftest() can hit the same
case, so the same fallback is added there too. The existing users
already handle opts == NULL, so these tracers keep behaving as before.
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20261015130658.221198-1-hao.ge@linux.dev
Fixes: e29aa918a928 ("tracing: Have function graph tracer define options per instance")
Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Hao Ge <hao.ge@linux.dev>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Hao Ge <hao.ge@linux.dev> · 2026-10-15 13:06 UTC · committed by Steven Rostedt
1 file changed,
+18
−2
modified kernel/trace/trace.c
| @@ -99,6 +99,12 @@ struct tracers { | |||
| 99 | 99 | struct tracer_flags *flags; | |
| 100 | 100 | }; | |
| 101 | 101 | ||
| 102 | + | /* | |
| 103 | + | * Tracers that do not define their own flags use these empty flags, | |
| 104 | + | * as tr->current_trace_flags must never be NULL. | |
| 105 | + | */ | |
| 106 | + | static struct tracer_flags no_tracer_flags; | |
| 107 | + | ||
| 102 | 108 | /* | |
| 103 | 109 | * To prevent the comm cache from being overwritten when no | |
| 104 | 110 | * tracing is active, only save the comm when a trace event | |
| @@ -1318,7 +1324,12 @@ static int run_tracer_selftest(struct tracer *type) | |||
| 1318 | 1324 | tracing_reset_online_cpus(&tr->array_buffer); | |
| 1319 | 1325 | ||
| 1320 | 1326 | tr->current_trace = type; | |
| 1321 | - | tr->current_trace_flags = type->flags ? : type->default_flags; | |
| 1327 | + | if (type->flags) | |
| 1328 | + | tr->current_trace_flags = type->flags; | |
| 1329 | + | else if (type->default_flags) | |
| 1330 | + | tr->current_trace_flags = type->default_flags; | |
| 1331 | + | else | |
| 1332 | + | tr->current_trace_flags = &no_tracer_flags; | |
| 1322 | 1333 | ||
| 1323 | 1334 | #ifdef CONFIG_TRACER_MAX_TRACE | |
| 1324 | 1335 | if (tracer_uses_snapshot(type)) { | |
| @@ -5089,7 +5100,12 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf) | |||
| 5089 | 5100 | return ret; | |
| 5090 | 5101 | } | |
| 5091 | 5102 | ||
| 5092 | - | tr->current_trace_flags = t->flags ? : t->tracer->flags; | |
| 5103 | + | if (t->flags) | |
| 5104 | + | tr->current_trace_flags = t->flags; | |
| 5105 | + | else if (t->tracer->flags) | |
| 5106 | + | tr->current_trace_flags = t->tracer->flags; | |
| 5107 | + | else | |
| 5108 | + | tr->current_trace_flags = &no_tracer_flags; | |
| 5093 | 5109 | ||
| 5094 | 5110 | if (trace->init) { | |
| 5095 | 5111 | ret = tracer_init(trace, tr); | |