Skip to content

Commit cc267e4

Browse files
rostedtgregkh
authored andcommitted
tracing: Fix trace_marker copy link list updates
commit 07183aa upstream. When the "copy_trace_marker" option is enabled for an instance, anything written into /sys/kernel/tracing/trace_marker is also copied into that instances buffer. When the option is set, that instance's trace_array descriptor is added to the marker_copies link list. This list is protected by RCU, as all iterations uses an RCU protected list traversal. When the instance is deleted, all the flags that were enabled are cleared. This also clears the copy_trace_marker flag and removes the trace_array descriptor from the list. The issue is after the flags are called, a direct call to update_marker_trace() is performed to clear the flag. This function returns true if the state of the flag changed and false otherwise. If it returns true here, synchronize_rcu() is called to make sure all readers see that its removed from the list. But since the flag was already cleared, the state does not change and the synchronization is never called, leaving a possible UAF bug. Move the clearing of all flags below the updating of the copy_trace_marker option which then makes sure the synchronization is performed. Also use the flag for checking the state in update_marker_trace() instead of looking at if the list is empty. Cc: stable@vger.kernel.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://patch.msgid.link/20260318185512.1b6c7db4@gandalf.local.home Fixes: 7b382ef ("tracing: Allow the top level trace_marker to write into another instances") Reported-by: Sasha Levin <sashal@kernel.org> Closes: https://lore.kernel.org/all/20260225133122.237275-1-sashal@kernel.org/ Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cf74d19 commit cc267e4

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

kernel/trace/trace.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,18 @@ static bool update_marker_trace(struct trace_array *tr, int enabled)
567567
lockdep_assert_held(&event_mutex);
568568

569569
if (enabled) {
570-
if (!list_empty(&tr->marker_list))
570+
if (tr->trace_flags & TRACE_ITER(COPY_MARKER))
571571
return false;
572572

573573
list_add_rcu(&tr->marker_list, &marker_copies);
574574
tr->trace_flags |= TRACE_ITER(COPY_MARKER);
575575
return true;
576576
}
577577

578-
if (list_empty(&tr->marker_list))
578+
if (!(tr->trace_flags & TRACE_ITER(COPY_MARKER)))
579579
return false;
580580

581-
list_del_init(&tr->marker_list);
581+
list_del_rcu(&tr->marker_list);
582582
tr->trace_flags &= ~TRACE_ITER(COPY_MARKER);
583583
return true;
584584
}
@@ -10547,18 +10547,19 @@ static int __remove_instance(struct trace_array *tr)
1054710547

1054810548
list_del(&tr->list);
1054910549

10550-
/* Disable all the flags that were enabled coming in */
10551-
for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
10552-
if ((1ULL << i) & ZEROED_TRACE_FLAGS)
10553-
set_tracer_flag(tr, 1ULL << i, 0);
10554-
}
10555-
1055610550
if (printk_trace == tr)
1055710551
update_printk_trace(&global_trace);
1055810552

10553+
/* Must be done before disabling all the flags */
1055910554
if (update_marker_trace(tr, 0))
1056010555
synchronize_rcu();
1056110556

10557+
/* Disable all the flags that were enabled coming in */
10558+
for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
10559+
if ((1ULL << i) & ZEROED_TRACE_FLAGS)
10560+
set_tracer_flag(tr, 1ULL << i, 0);
10561+
}
10562+
1056210563
tracing_set_nop(tr);
1056310564
clear_ftrace_function_probes(tr);
1056410565
event_trace_del_tracer(tr);

0 commit comments

Comments
 (0)