The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] tracing: Reject event references on coming modules
@ 2026-12-04 15:03 Chris Mason
  0 siblings, 0 replies; only message in thread
From: Chris Mason @ 2026-12-04 15:03 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-trace-kernel
  Cc: mathieu.desnoyers, jolsa, petr.pavlu, mjambigi, bpf,
	linux-modules, linux-kernel, mason

perf_trace_destroy() hits a use-after-free on p_event->tp_event if the
trace event's module fails to load after the perf event was opened.

tp_event points into the module's data, and the perf event holds a
module reference to keep it valid. But a failed load frees the module
without checking refcnt, and try_module_get() in
trace_event_try_get_ref() succeeds for a module in MODULE_STATE_COMING:

    module load                       perf event
    ===========                       ==========
    prepare_coming_module()
      trace_module_notify()
        trace_module_add_events()
                                      perf_trace_init()
                                        trace_event_try_get_ref()
                                        p_event->tp_event = tp_event
    do_init_module()
      mod->init() fails
      free_module()
                                      perf_trace_destroy()
                                        tp_event->class
                                          /* use after free */

The fix used here is to take the module reference only once the module
is live.

Fixes: 1c024eca51fd ("perf, trace: Optimize tracepoints by using per-tracepoint-per-cpu hlist to track events")
Signed-off-by: Chris Mason <mason@kernel.org>
---
This is an AI generated fix, on top of v7.4-rc5.  Jiri flagged this
as a potential bug in a different thread:

  https://lore.kernel.org/all/axKQe6swZjR5d65A@krava/

AI generated tests reproduce it with KASAN, and it's gone with this patch.  The
ftrace selftests give the same results as before.

While a module is loading, perf_event_open() on its trace events now fails with
EINVAL.

 include/linux/trace_events.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 7a0aa23825de4..93fe5ffc43e3f 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -428,10 +428,16 @@ static inline bool trace_event_dyn_busy(struct trace_event_call *call)
 
 static inline bool trace_event_try_get_ref(struct trace_event_call *call)
 {
+	struct module *mod;
+
 	if (call->flags & TRACE_EVENT_FL_DYNAMIC)
 		return trace_event_dyn_try_get_ref(call);
-	else
-		return try_module_get(call->module);
+
+	mod = call->module;
+	/* A failed load frees the module despite references */
+	if (mod && mod->state != MODULE_STATE_LIVE)
+		return false;
+	return try_module_get(mod);
 }
 
 static inline void trace_event_put_ref(struct trace_event_call *call)

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 15:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:03 [PATCH] tracing: Reject event references on coming modules Chris Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox