Skip to content

Commit a2cce54

Browse files
Tengda Wugregkh
authored andcommitted
tracing: Fix use-after-free in print_graph_function_flags during tracer switching
commit 7f81f27b1093e4895e87b74143c59c055c3b1906 upstream. Kairui reported a UAF issue in print_graph_function_flags() during ftrace stress testing [1]. This issue can be reproduced if puting a 'mdelay(10)' after 'mutex_unlock(&trace_types_lock)' in s_start(), and executing the following script: $ echo function_graph > current_tracer $ cat trace > /dev/null & $ sleep 5 # Ensure the 'cat' reaches the 'mdelay(10)' point $ echo timerlat > current_tracer The root cause lies in the two calls to print_graph_function_flags within print_trace_line during each s_show(): * One through 'iter->trace->print_line()'; * Another through 'event->funcs->trace()', which is hidden in print_trace_fmt() before print_trace_line returns. Tracer switching only updates the former, while the latter continues to use the print_line function of the old tracer, which in the script above is print_graph_function_flags. Moreover, when switching from the 'function_graph' tracer to the 'timerlat' tracer, s_start only calls graph_trace_close of the 'function_graph' tracer to free 'iter->private', but does not set it to NULL. This provides an opportunity for 'event->funcs->trace()' to use an invalid 'iter->private'. To fix this issue, set 'iter->private' to NULL immediately after freeing it in graph_trace_close(), ensuring that an invalid pointer is not passed to other tracers. Additionally, clean up the unnecessary 'iter->private = NULL' during each 'cat trace' when using wakeup and irqsoff tracers. [1] https://lore.kernel.org/all/[email protected]/ Cc: [email protected] Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Zheng Yejian <[email protected]> Link: https://lore.kernel.org/[email protected] Fixes: eecb91b9f98d ("tracing: Fix memleak due to race between current_tracer and trace") Closes: https://lore.kernel.org/all/CAMgjq7BW79KDSCyp+tZHjShSzHsScSiJxn5ffskp-QzVM06fxw@mail.gmail.com/ Reported-by: Kairui Song <[email protected]> Signed-off-by: Tengda Wu <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3ac65de commit a2cce54

File tree

3 files changed

+1
-4
lines changed

3 files changed

+1
-4
lines changed

kernel/trace/trace_functions_graph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ void graph_trace_close(struct trace_iterator *iter)
12441244
if (data) {
12451245
free_percpu(data->cpu_data);
12461246
kfree(data);
1247+
iter->private = NULL;
12471248
}
12481249
}
12491250

kernel/trace/trace_irqsoff.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ static void irqsoff_trace_open(struct trace_iterator *iter)
231231
{
232232
if (is_graph(iter->tr))
233233
graph_trace_open(iter);
234-
else
235-
iter->private = NULL;
236234
}
237235

238236
static void irqsoff_trace_close(struct trace_iterator *iter)

kernel/trace/trace_sched_wakeup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ static void wakeup_trace_open(struct trace_iterator *iter)
168168
{
169169
if (is_graph(iter->tr))
170170
graph_trace_open(iter);
171-
else
172-
iter->private = NULL;
173171
}
174172

175173
static void wakeup_trace_close(struct trace_iterator *iter)

0 commit comments

Comments
 (0)