All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tracing/probes: fix race between probe execution and event file removal
@ 2026-12-02 16:23 Antoine Tenart
  2026-12-02 16:33 ` Steven Rostedt
  2026-12-02 16:45 ` sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Antoine Tenart @ 2026-12-02 16:23 UTC (permalink / raw)
  To: rostedt, mhiramat, mathieu.desnoyers
  Cc: Antoine Tenart, vmalik, linux-kernel, linux-trace-kernel,
	Uladzislau Rezki (Sony)

A probe being executed can race with its unregistration happening on a
different CPU and lead to a UaF, which can trigger KASAN or crash the
kernel.

  BUG: KASAN: slab-use-after-free in kretprobe_trace_func+0x68d/0x7e0
  Read of size 8 at addr ffff888173328390 by task hammer/1619
  CPU: 32 UID: 0 PID: 1619 Comm: hammer Tainted: G S   7.4.0-rc1+ #1 PREEMPT(lazy)
  Call Trace:
   kretprobe_trace_func+0x68d/0x7e0
   kretprobe_dispatcher+0x128/0x220
   kretprobe_rethook_handler+0x10f/0x1d0
   rethook_trampoline_handler+0x181/0x510
   arch_rethook_trampoline_callback+0xcd/0x160
   arch_rethook_trampoline+0x2c/0x60
   entry_SYSCALL_64_after_hwframe+0x76/0x7e

The race is the following:

          CPU 0                             CPU 1

  kretprobe_dispatcher
    kretprobe_trace_func
      file = link->file
                              disable_trace_kprobe
                               trace_probe_remove_file
                                 // Frees link after an RCU grace period.
                                 kvfree_rcu_mightsleep(link)
                              unregister_trace_kprobe
                                unregister_kprobe_event
                                  probe_remove_event_call
                                    event_remove
                                      remove_event_from_tracers
                                        // Frees link->file.
                                        event_file_put
      // UaF.
      file->...

Looking back at history, disable_trace_kprobe used to have the following
snippet:

  /* Synchronize with kprobe_trace_func/kretprobe_trace_func
   * to ensure disabled (all running handlers are finished).
   * This is not only for kfree(), but also the caller,
   * trace_remove_event_call() supposes it for releasing
   * event_call related objects, which will be accessed in
   * the kprobe_trace_func/kretprobe_trace_func.
   */
   synchronize_rcu();
   kfree(link);

It made clear synchronize_rcu was not only there for link users, but
also related objects. The comment was lost during refactoring in
b5f935ee1339 ("tracing/probe: Add trace_event_file access APIs for
trace_probe") but synchronize_rcu stayed, in trace_probe_remove_file.

Later, a6ed2aee5464 ("tracing: Switch to kvfree_rcu() API") removed the
synchronize_rcu/kfree pair into a single kvfree_rcu call. This works for
freeing the link but the side effect of protecting related objects was
lost, leading to the UaF described here.

We do not want to put back the synchronize_rcu call as it has a
noticeable impact when removing probes. Instead, defer the freeing of
the link after an RCU grace period.

The other snippet of the commit mentioned in the Fixes tag was fixed in commit
fe58f457ad8d ("tracing/osnoise: Call synchronize_rcu() when unregistering").

Fixes: a6ed2aee5464 ("tracing: Switch to kvfree_rcu() API")
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Antoine Tenart <atenart@kernel.org>
---

Since v1:
- Do not add back the synchronize_rcu() call and use Steven's suggestion
  instead (freeing the object after an RCU grace period).

 kernel/trace/trace_probe.c | 11 ++++++++++-
 kernel/trace/trace_probe.h |  1 +
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
index cff55dcae36f..c249256dea80 100644
--- a/kernel/trace/trace_probe.c
+++ b/kernel/trace/trace_probe.c
@@ -2812,6 +2812,7 @@ int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
 		return -ENOMEM;
 
 	link->file = file;
+	event_file_get(file);
 	INIT_LIST_HEAD(&link->list);
 	list_add_tail_rcu(&link->list, &tp->event->files);
 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
@@ -2831,6 +2832,14 @@ struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
 	return NULL;
 }
 
+static void free_link_rcu(struct rcu_head *rcu)
+{
+	struct event_file_link *link = container_of(rcu, struct event_file_link, rcu);
+
+	event_file_put(link->file);
+	kvfree(link);
+}
+
 int trace_probe_remove_file(struct trace_probe *tp,
 			    struct trace_event_file *file)
 {
@@ -2841,7 +2850,7 @@ int trace_probe_remove_file(struct trace_probe *tp,
 		return -ENOENT;
 
 	list_del_rcu(&link->list);
-	kvfree_rcu_mightsleep(link);
+	call_rcu(&link->rcu, free_link_rcu);
 
 	if (list_empty(&tp->event->files))
 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
index d1fb3520700f..aecc9f24951d 100644
--- a/kernel/trace/trace_probe.h
+++ b/kernel/trace/trace_probe.h
@@ -281,6 +281,7 @@ struct trace_probe {
 struct event_file_link {
 	struct trace_event_file		*file;
 	struct list_head		list;
+	struct rcu_head			rcu;
 };
 
 static inline unsigned int trace_probe_load_flag(struct trace_probe *tp)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] tracing/probes: fix race between probe execution and event file removal
  2026-12-02 16:23 [PATCH v2] tracing/probes: fix race between probe execution and event file removal Antoine Tenart
@ 2026-12-02 16:33 ` Steven Rostedt
  2026-12-02 16:45 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: Steven Rostedt @ 2026-12-02 16:33 UTC (permalink / raw)
  To: Antoine Tenart
  Cc: mhiramat, mathieu.desnoyers, vmalik, linux-kernel,
	linux-trace-kernel, Uladzislau Rezki (Sony)

On Wed,  2 Dec 2026 17:23:07 +0100
Antoine Tenart <atenart@kernel.org> wrote:


> Since v1:
> - Do not add back the synchronize_rcu() call and use Steven's suggestion
>   instead (freeing the object after an RCU grace period).

Masami,

If you ack this, I can take this and my patch through the trace/urgent push
I'm working on. Otherwise, you can push this through your branch (if you
have something already brewing) and I'll push my patch through my branch.

-- Steve

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] tracing/probes: fix race between probe execution and event file removal
  2026-12-02 16:23 [PATCH v2] tracing/probes: fix race between probe execution and event file removal Antoine Tenart
  2026-12-02 16:33 ` Steven Rostedt
@ 2026-12-02 16:45 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 16:45 UTC (permalink / raw)
  To: Antoine Tenart; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] tracing/probes: UAF on file->filter and file->tr after
  free_link_rcu()
--

commit e46b0b6beaf53f9316361be522e07400739512b0
Author: Antoine Tenart <atenart@kernel.org>

tracing/probes: fix race between probe execution and event file removal

This commit takes an event_file_get() reference on link->file in
trace_probe_add_file() and releases it via call_rcu() in
trace_probe_remove_file() so link->file is not freed while a probe handler
is still running.

> diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
> index cff55dcae36f2..c249256dea80d 100644
> --- a/kernel/trace/trace_probe.c
> +++ b/kernel/trace/trace_probe.c
> @@ -2812,6 +2812,7 @@ int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
>  		return -ENOMEM;
>  
>  	link->file = file;
> +	event_file_get(file);
>  	INIT_LIST_HEAD(&link->list);
>  	list_add_tail_rcu(&link->list, &tp->event->files);
>  	trace_probe_set_flag(tp, TP_FLAG_TRACE);
> @@ -2831,6 +2832,14 @@ struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
>  	return NULL;
>  }
>  
> +static void free_link_rcu(struct rcu_head *rcu)
> +{
> +	struct event_file_link *link = container_of(rcu, struct event_file_link, rcu);
> +
> +	event_file_put(link->file);
> +	kvfree(link);
> +}
> +
>  int trace_probe_remove_file(struct trace_probe *tp,
>  			    struct trace_event_file *file)
>  {
> @@ -2841,7 +2850,7 @@ int trace_probe_remove_file(struct trace_probe *tp,
>  		return -ENOENT;
>  
>  	list_del_rcu(&link->list);
> -	kvfree_rcu_mightsleep(link);
> +	call_rcu(&link->rcu, free_link_rcu);

[Severity: High]
Can an in-flight probe handler still hit a use-after-free on file->filter
or file->tr even with event_file_get() holding a reference on file?

event_file_get() only delays freeing the struct trace_event_file slab
object itself; it does not delay the teardown of sub-objects hanging off
file when trace_probe_remove_file() returns without waiting for an RCU
grace period.

CPU0                              CPU1
kretprobe_trace_func()
  file = link->file
                                  disable_trace_kprobe()
                                    trace_probe_remove_file()
                                      list_del_rcu(&link->list)
                                      call_rcu(&link->rcu, free_link_rcu)
                                  unregister_trace_kprobe()
                                    unregister_kprobe_event()
                                      ...
                                        remove_event_file_dir()
                                          free_event_filter(file->filter)
                                          file->flags |= EVENT_FILE_FL_FREED
                                          event_file_put(file)
  __kretprobe_trace_func()
    trace_event_buffer_commit()
      __event_trigger_test_discard()
        filter_match_preds(file->filter, ...)
          <-- use-after-free on file->filter

remove_event_file_dir() frees file->filter synchronously before calling
event_file_put() and leaves EVENT_FILE_FL_FILTERED set in file->flags.

Similarly, during trace instance removal (__remove_instance() ->
event_trace_del_tracer()), tracepoint_synchronize_unregister() only waits
on tracepoint_srcu and rcu_tasks_trace rather than standard RCU, so
__remove_instance() can free tr->array_buffer.buffer and tr while an
in-flight probe handler is still accessing link->file->tr.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202162333.168965-1-atenart@kernel.org?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 16:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:23 [PATCH v2] tracing/probes: fix race between probe execution and event file removal Antoine Tenart
2026-12-02 16:33 ` Steven Rostedt
2026-12-02 16:45 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.