* [PATCH v3 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races
@ 2026-12-04 6:06 Masami Hiramatsu (Google)
2026-12-04 6:06 ` [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref Masami Hiramatsu (Google)
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04 6:06 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
Hi,
This is v3 of the series fixing read-only backup trace instance read/splice
and autoremove issues, rebased onto linux-trace/trace/fixes (a6a40ab36abb,
which already includes v2 Patch 2 "tracing: Reject backup instance opens
only after autoremove is queued").
The previous version is here:
https://lore.kernel.org/all/179629163198.2691692.6206990932650792224.stgit@mhiramat.c.googlers.com/
Changes in v3:
- Rebase onto linux-trace/trace/fixes (a6a40ab36abb) and drop v2 [2/3]
which is already applied.
- [1/3] Newly added to hold a file reference in struct buffer_ref and
prevent use-after-free when an instance is destroyed while spliced
pages remain in a pipe.
- [2/3] Move update_last_data_if_empty() into tracing_wait_pipe() and
call it in tracing_buffers_splice_read() so draining via splice()
schedules backup autoremove (reported by Sashiko).
- [3/3] Unchanged from v2.
1. struct buffer_ref attached to pipe buffers by
tracing_buffers_splice_read() holds a raw pointer to trace_buffer
without pinning the source file. If trace_pipe_raw is closed while
spliced pages remain in the pipe and the instance is destroyed (by
rmdir or backup autoremove), buffer_ref_release() triggers a UAF.
Patch 1 holds a get_file(file) reference in struct buffer_ref and
releases it via fput() in buffer_ref_release().
2. Empty read-only backup instances block indefinitely (or return -EAGAIN
on non-blocking reads/splices, or never wake in poll()) when
update_last_data_if_empty() returns false, and draining via splice()
never schedules backup autoremove. Patch 2 returns EOF (or -ENODEV for
TRACE_MMAP_IOCTL_GET_READER), calls update_last_data_if_empty() on the
splice EOF paths, and returns EPOLLIN | EPOLLRDNORM in trace_poll().
3. __remove_instance() calls cancel_work_sync() on autoremove_work while
holding event_mutex and trace_types_lock, causing an ABBA deadlock or
UAF/ABA race when rmdir or trace_array_destroy() races with
autoremove. Patch 3 lets trace_array_autoremove() exclusively destroy
tr once queued, and consolidates trace_array_destroy() and
instance_rmdir() so external callers drop the locks, flush
autoremove_wq, and return -ENODEV.
Thank you,
Masami Hiramatsu (Google) (3):
tracing: Hold file reference in splice buffer_ref
tracing: Return EOF on empty readonly instances
tracing: Fix deadlock and UAF when rmdir races with backup autoremove
kernel/trace/trace.c | 111 ++++++++++++++++++++++++++++++++++-----------------
1 file changed, 74 insertions(+), 37 deletions(-)
base-commit: a6a40ab36abb91e698b08cc7a0d3dc9455fcfaf4
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref
2026-12-04 6:06 [PATCH v3 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
@ 2026-12-04 6:06 ` Masami Hiramatsu (Google)
2026-12-04 6:19 ` sashiko-bot
2026-12-04 6:07 ` [PATCH v3 2/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
2026-12-04 6:07 ` [PATCH v3 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove Masami Hiramatsu (Google)
2 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04 6:06 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
tracing_buffers_splice_read() stores a raw pointer to
iter->array_buffer->buffer in each struct buffer_ref attached to the pipe
buffers, and buffer_ref_release() later dereferences ref->buffer in
ring_buffer_free_read_page() when the pipe drains or closes.
However, struct buffer_ref does not hold a reference to the source file
(and thus the owning struct trace_array). If the trace_pipe_raw file
descriptor is closed while spliced pages are still sitting in the pipe
and the trace instance is then destroyed (via manual rmdir or backup
instance autoremove), ring_buffer_free_read_page() dereferences the freed
trace_buffer, causing a use-after-free.
Store a get_file(file) reference in struct buffer_ref for each spliced
page and release it via fput() in buffer_ref_release() after
ring_buffer_free_read_page(). Holding the file reference keeps the
trace_array (including tr->trace_ref, so tracer switches and instance
removal return -EBUSY until the pipe drains) and its ring buffer alive
without taking trace_types_lock on the splice data path.
Fixes: 0c8916c34203 ("tracing: Add rmdir to remove multibuffer instances")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v3:
- Newly added.
---
kernel/trace/trace.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index f153ffedb9d2..e657b01fca37 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -42,6 +42,7 @@
#include <linux/init.h>
#include <linux/panic_notifier.h>
#include <linux/poll.h>
+#include <linux/file.h>
#include <linux/nmi.h>
#include <linux/fs.h>
#include <linux/trace.h>
@@ -7228,6 +7229,7 @@ int tracing_buffers_release(struct inode *inode, struct file *file)
}
struct buffer_ref {
+ struct file *file;
struct trace_buffer *buffer;
struct buffer_data_read_page *rpage;
int cpu;
@@ -7239,6 +7241,7 @@ static void buffer_ref_release(struct buffer_ref *ref)
if (!refcount_dec_and_test(&ref->refcount))
return;
ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->rpage);
+ fput(ref->file);
kfree(ref);
}
@@ -7357,6 +7360,7 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
break;
}
+ ref->file = get_file(file);
page = virt_to_page(ring_buffer_read_page_data(ref->rpage));
spd.pages[i] = page;
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v3 2/3] tracing: Return EOF on empty readonly instances
2026-12-04 6:06 [PATCH v3 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
2026-12-04 6:06 ` [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref Masami Hiramatsu (Google)
@ 2026-12-04 6:07 ` Masami Hiramatsu (Google)
2026-12-04 6:07 ` [PATCH v3 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove Masami Hiramatsu (Google)
2 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04 6:07 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Commit 2c79da099aac ("tracing: Make the backup instance non-reusable")
marked backup instances with TRACE_ARRAY_FL_RDONLY so that no new events
can ever be written to them.
However, when no data is available and update_last_data_if_empty()
returns false, blocking pipe readers still hang indefinitely in
wait_on_pipe() / ring_buffer_wait(), non-blocking readers return -EAGAIN
as if more events might arrive later, and poll() waits indefinitely (or
fails to report readable when unread data is below buffer_percent). This
happens in several cases:
- Reading a per-CPU trace_pipe or trace_pipe_raw on a CPU whose buffer
is empty while another CPU still holds previous-boot data.
- Calling splice() on trace_pipe or trace_pipe_raw when the CPU buffer
is empty or only contains a partial commit page (which cannot be
spliced as a full sub-buffer).
- Reading an empty readonly instance after TRACE_ARRAY_FL_LAST_BOOT has
already been cleared (for example, while another file descriptor still
keeps the backup instance alive).
- Issuing a blocking TRACE_MMAP_IOCTL_GET_READER ioctl on a readonly
trace_pipe_raw file.
Check trace_array_is_readonly() before the non-blocking checks and
blocking waits in the pipe read, splice, and mmap reader ioctl paths so
they return EOF (or -ENODEV for the ioctl) instead of blocking or
returning -EAGAIN. Also call update_last_data_if_empty() on the splice
EOF paths so draining a backup instance via splice() schedules
autoremove, and return EPOLLIN | EPOLLRDNORM in trace_poll() for
readonly instances since their buffers are static.
Fixes: 2c79da099aac ("tracing: Make the backup instance non-reusable")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v3:
- Move update_last_data_if_empty() into tracing_wait_pipe() and call it
in tracing_buffers_splice_read() so draining via splice() schedules
backup autoremove (reported by Sashiko).
Changes in v2:
- Check trace_array_is_readonly() before O_NONBLOCK / SPLICE_F_NONBLOCK
so non-blocking reads and splices also return EOF instead of -EAGAIN.
- Return EPOLLIN | EPOLLRDNORM in trace_poll() for readonly instances
since their buffers are static.
---
kernel/trace/trace.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index e657b01fca37..918cc463b23f 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -5346,8 +5346,8 @@ trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_tabl
{
struct trace_array *tr = iter->tr;
- /* Iterators are static, they should be filled or empty */
- if (trace_buffer_iter(iter, iter->cpu_file))
+ /* Iterators and readonly instances are static, they are filled or empty */
+ if (trace_buffer_iter(iter, iter->cpu_file) || trace_array_is_readonly(tr))
return EPOLLIN | EPOLLRDNORM;
if (tr->trace_flags & TRACE_ITER(BLOCK))
@@ -5376,6 +5376,13 @@ static int tracing_wait_pipe(struct file *filp)
while (trace_empty(iter)) {
+ if (update_last_data_if_empty(iter->tr))
+ return 0;
+
+ /* Readonly instances can never receive new events. */
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
if ((filp->f_flags & O_NONBLOCK)) {
return -EAGAIN;
}
@@ -5464,9 +5471,6 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
}
waitagain:
- if (update_last_data_if_empty(iter->tr))
- return 0;
-
sret = tracing_wait_pipe(filp);
if (sret <= 0)
return sret;
@@ -7166,6 +7170,9 @@ ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
if (update_last_data_if_empty(iter->tr))
return 0;
+ if (trace_array_is_readonly(iter->tr))
+ return 0;
+
if ((filp->f_flags & O_NONBLOCK))
return -EAGAIN;
@@ -7385,6 +7392,12 @@ ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
if (woken)
goto out;
+ if (!iter->snapshot && update_last_data_if_empty(iter->tr))
+ goto out;
+
+ if (trace_array_is_readonly(iter->tr))
+ goto out;
+
ret = -EAGAIN;
if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
goto out;
@@ -7417,7 +7430,7 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned
int err;
if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
- if (!(file->f_flags & O_NONBLOCK)) {
+ if (!(file->f_flags & O_NONBLOCK) && !trace_array_is_readonly(iter->tr)) {
err = ring_buffer_wait(iter->array_buffer->buffer,
iter->cpu_file,
iter->tr->buffer_percent,
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v3 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove
2026-12-04 6:06 [PATCH v3 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
2026-12-04 6:06 ` [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref Masami Hiramatsu (Google)
2026-12-04 6:07 ` [PATCH v3 2/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
@ 2026-12-04 6:07 ` Masami Hiramatsu (Google)
2 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04 6:07 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-kernel, linux-trace-kernel
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
__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. Consolidate
trace_array_destroy() and instance_rmdir() so that if tr->autoremove_queued
is set, they 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@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v2:
- Consolidate trace_array_destroy() and instance_rmdir() via
try_remove_instance() and drop nested scoped_guard().
- Keep returning -ENODEV after flush_workqueue() so vfs_rmdir()
does not run d_delete_notify() on an already-removed dentry.
---
kernel/trace/trace.c | 82 +++++++++++++++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 31 deletions(-)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 918cc463b23f..bfe405f1fbe9 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -575,11 +575,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, const char *name,
+ 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, NULL, true);
}
static struct workqueue_struct *autoremove_wq;
@@ -594,16 +597,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);
@@ -8947,7 +8940,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);
@@ -8977,39 +8969,67 @@ static int __remove_instance(struct trace_array *tr)
return 0;
}
-int trace_array_destroy(struct trace_array *this_tr)
+static int try_remove_instance(struct trace_array *this_tr, const char *name,
+ bool from_autoremove)
{
struct trace_array *tr;
- if (!this_tr)
- return -EINVAL;
-
+ /* Making sure trace array exists before destroying it. */
guard(mutex)(&event_mutex);
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)
- return __remove_instance(tr);
+ if (name) {
+ tr = trace_array_find(name);
+ if (!tr)
+ return -ENODEV;
+ } else {
+ list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+ if (tr == this_tr)
+ break;
+ }
+ if (list_entry_is_head(tr, &ftrace_trace_arrays, list))
+ return -ENODEV;
}
- return -ENODEV;
+ if (!tr->autoremove_queued || from_autoremove)
+ return __remove_instance(tr);
+
+ /* Return positive to flush autoremove work queue */
+ return 1;
}
-EXPORT_SYMBOL_GPL(trace_array_destroy);
-static int instance_rmdir(const char *name)
+static int __trace_array_destroy(struct trace_array *this_tr, const char *name,
+ bool from_autoremove)
{
- struct trace_array *tr;
+ int ret;
- guard(mutex)(&event_mutex);
- guard(mutex)(&trace_types_lock);
+ if ((!this_tr && !name) || (this_tr && name))
+ return -EINVAL;
- tr = trace_array_find(name);
- if (!tr)
- return -ENODEV;
+ ret = try_remove_instance(this_tr, name, from_autoremove);
+ if (ret <= 0)
+ return ret;
+
+ /*
+ * Wait outside event_mutex and trace_types_lock for
+ * trace_array_autoremove() to finish destroying this instance,
+ * then return -ENODEV so vfs_rmdir() will not run d_delete_notify()
+ * on the dentry already removed by tracefs_remove().
+ */
+ flush_workqueue(autoremove_wq);
+
+ return -ENODEV;
+}
- return __remove_instance(tr);
+int trace_array_destroy(struct trace_array *this_tr)
+{
+ return __trace_array_destroy(this_tr, NULL, false);
+}
+EXPORT_SYMBOL_GPL(trace_array_destroy);
+
+static int instance_rmdir(const char *name)
+{
+ return __trace_array_destroy(NULL, name, false);
}
static __init void create_trace_instances(struct dentry *d_tracer)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref
2026-12-04 6:06 ` [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref Masami Hiramatsu (Google)
@ 2026-12-04 6:19 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 6:19 UTC (permalink / raw)
To: Masami Hiramatsu (Google); +Cc: linux-trace-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] tracing: Fixes trailer cites wrong commit for
ring_buffer_free_read_page() UAF
--
commit d437840eacea8fdaf6388c3810c823a11b795543
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> tracing: Hold file reference in splice buffer_ref
>
> tracing_buffers_splice_read() stores a raw pointer to
> iter->array_buffer->buffer in each struct buffer_ref attached to the pipe
> buffers, and buffer_ref_release() later dereferences ref->buffer in
> ring_buffer_free_read_page() when the pipe drains or closes.
>
> However, struct buffer_ref does not hold a reference to the source file
> (and thus the owning struct trace_array). If the trace_pipe_raw file
> descriptor is closed while spliced pages are still sitting in the pipe
> and the trace instance is then destroyed (via manual rmdir or backup
> instance autoremove), ring_buffer_free_read_page() dereferences the freed
> trace_buffer, causing a use-after-free.
>
> Store a get_file(file) reference in struct buffer_ref for each spliced
> page and release it via fput() in buffer_ref_release() after
> ring_buffer_free_read_page(). Holding the file reference keeps the
> trace_array (including tr->trace_ref, so tracer switches and instance
> removal return -EBUSY until the pipe drains) and its ring buffer alive
> without taking trace_types_lock on the splice data path.
>
> Fixes: 0c8916c34203 ("tracing: Add rmdir to remove multibuffer instances")
> Cc: stable@vger.kernel.org
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
[Severity: Low]
Should the Fixes tag cite 73a757e63114 ("ring-buffer: Return reader page
back into existing ring buffer") instead of 0c8916c34203 ("tracing: Add
rmdir to remove multibuffer instances")?
When instance rmdir was added, ring_buffer_free_read_page() only called
free_page() and ignored the buffer pointer. The buffer->buffers[cpu]
dereference that causes the use-after-free was introduced when reader page
caching was added.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179636440238.1289712.8372580891085333773.stgit@mhiramat.c.googlers.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 6:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 6:06 [PATCH v3 0/3] tracing: Fix readonly backup instance blocking reads and autoremove races Masami Hiramatsu (Google)
2026-12-04 6:06 ` [PATCH v3 1/3] tracing: Hold file reference in splice buffer_ref Masami Hiramatsu (Google)
2026-12-04 6:19 ` sashiko-bot
2026-12-04 6:07 ` [PATCH v3 2/3] tracing: Return EOF on empty readonly instances Masami Hiramatsu (Google)
2026-12-04 6:07 ` [PATCH v3 3/3] tracing: Fix deadlock and UAF when rmdir races with backup autoremove Masami Hiramatsu (Google)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox