[PATCH 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove
From: Masami Hiramatsu (Google)
Date: Wed Dec 02 2026 - 10:06:05 EST
From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
__remove_instance() is called with event_mutex and trace_types_lock held
and calls trace_array_cancel_autoremove(), which invokes
cancel_work_sync(&tr->autoremove_work) when work_pending() is true.
However, trace_array_autoremove() calls trace_array_destroy(), which
acquires event_mutex and trace_types_lock.
When a manual rmdir (or trace_array_destroy()) races with a queued
autoremove_work, this creates two bugs:
1. ABBA deadlock: if the worker dequeues autoremove_work between
work_pending() and cancel_work_sync() and blocks on event_mutex,
instance_rmdir() blocks forever inside cancel_work_sync() while
holding event_mutex and trace_types_lock. Lockdep also flags the
circular dependency:
WARNING: possible circular locking dependency detected
repro_rmdir/89 is trying to acquire lock:
((work_completion)(&tr->autoremove_work)), at: __flush_work
but task is already holding lock:
(trace_types_lock), at: instance_rmdir
Chain exists of:
(work_completion)(&tr->autoremove_work)
--> event_mutex --> trace_types_lock
2. Use-after-free / ABA race: if autoremove_work has already been
dequeued (so work_pending() returns false) and is waiting on
event_mutex, trace_array_cancel_autoremove() skips cancellation and
__remove_instance() frees tr while the work item is still executing
in the worker. If another trace_array is later allocated at the same
address before the worker acquires event_mutex, trace_array_destroy()
could also destroy the wrong instance.
Remove trace_array_cancel_autoremove() and let trace_array_autoremove()
exclusively destroy tr once tr->autoremove_queued is set. If
trace_array_destroy() or instance_rmdir() sees tr->autoremove_queued,
release the locks, wait for autoremove_wq to flush, and return -ENODEV
(avoiding double dentry removal in vfs_rmdir()).
Fixes: eca33fdab47b ("tracing: Remove the backup instance automatically after read")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
kernel/trace/trace.c | 78 +++++++++++++++++++++++++++++++++-----------------
1 file changed, 51 insertions(+), 27 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 5914b970b34e..f4aa6c06ee8a 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -574,11 +574,14 @@ void trace_set_ring_buffer_expanded(struct trace_array *tr)
tr->ring_buffer_expanded = true;
}
+static int __trace_array_destroy(struct trace_array *this_tr,
+ bool from_autoremove);
+
static void trace_array_autoremove(struct work_struct *work)
{
struct trace_array *tr = container_of(work, struct trace_array, autoremove_work);
- trace_array_destroy(tr);
+ __trace_array_destroy(tr, true);
}
static struct workqueue_struct *autoremove_wq;
@@ -593,16 +596,6 @@ static void trace_array_kick_autoremove(struct trace_array *tr)
}
}
-static void trace_array_cancel_autoremove(struct trace_array *tr)
-{
- /*
- * Since this can be called inside trace_array_autoremove(),
- * it has to avoid deadlock of the workqueue.
- */
- if (work_pending(&tr->autoremove_work))
- cancel_work_sync(&tr->autoremove_work);
-}
-
static void trace_array_init_autoremove(struct trace_array *tr)
{
INIT_WORK(&tr->autoremove_work, trace_array_autoremove);
@@ -8942,7 +8935,6 @@ static int __remove_instance(struct trace_array *tr)
set_tracer_flag(tr, 1ULL << i, 0);
}
- trace_array_cancel_autoremove(tr);
tracing_set_nop(tr);
clear_ftrace_function_probes(tr);
event_trace_del_tracer(tr);
@@ -8972,39 +8964,71 @@ static int __remove_instance(struct trace_array *tr)
return 0;
}
-int trace_array_destroy(struct trace_array *this_tr)
+static int __trace_array_destroy(struct trace_array *this_tr,
+ bool from_autoremove)
{
struct trace_array *tr;
+ bool wait_autoremove = false;
+ int ret = -ENODEV;
if (!this_tr)
return -EINVAL;
- guard(mutex)(&event_mutex);
- guard(mutex)(&trace_types_lock);
-
+ scoped_guard(mutex, &event_mutex) {
+ scoped_guard(mutex, &trace_types_lock) {
+ /* Making sure trace array exists before destroying it. */
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+ if (tr != this_tr)
+ continue;
+ if (!from_autoremove && tr->autoremove_queued)
+ wait_autoremove = true;
+ else
+ ret = __remove_instance(tr);
+ break;
+ }
+ }
+ }
- /* Making sure trace array exists before destroying it. */
- list_for_each_entry(tr, &ftrace_trace_arrays, list) {
- if (tr == this_tr)
- return __remove_instance(tr);
+ if (wait_autoremove) {
+ /*
+ * Wait outside event_mutex and trace_types_lock for
+ * trace_array_autoremove() to finish destroying this instance.
+ */
+ flush_workqueue(autoremove_wq);
}
- return -ENODEV;
+ return ret;
+}
+
+int trace_array_destroy(struct trace_array *this_tr)
+{
+ return __trace_array_destroy(this_tr, false);
}
EXPORT_SYMBOL_GPL(trace_array_destroy);
static int instance_rmdir(const char *name)
{
struct trace_array *tr;
+ bool wait_autoremove = false;
+ int ret = -ENODEV;
- guard(mutex)(&event_mutex);
- guard(mutex)(&trace_types_lock);
+ scoped_guard(mutex, &event_mutex) {
+ scoped_guard(mutex, &trace_types_lock) {
+ tr = trace_array_find(name);
+ if (!tr)
+ return -ENODEV;
- tr = trace_array_find(name);
- if (!tr)
- return -ENODEV;
+ if (tr->autoremove_queued)
+ wait_autoremove = true;
+ else
+ ret = __remove_instance(tr);
+ }
+ }
- return __remove_instance(tr);
+ if (wait_autoremove)
+ flush_workqueue(autoremove_wq);
+
+ return ret;
}
static __init void create_trace_instances(struct dentry *d_tracer)