Linux Modules
 help / color / mirror / Atom feed
From: "Chris Mason" <mason@kernel.org>
To: rostedt@goodmis.org, mhiramat@kernel.org,
	linux-trace-kernel@vger.kernel.org
Cc: mathieu.desnoyers@efficios.com, jolsa@kernel.org,
	petr.pavlu@suse.com, mjambigi@linux.ibm.com, bpf@vger.kernel.org,
	linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org,
	mason@kernel.org
Subject: [PATCH] tracing: Reject event references on coming modules
Date: Fri, 04 Dec 2026 10:03:49 -0500	[thread overview]
Message-ID: <20261204130200.15208-1-mason@kernel.org> (raw)

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

                 reply	other threads:[~2026-12-04 15:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204130200.15208-1-mason@kernel.org \
    --to=mason@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=mjambigi@linux.ibm.com \
    --cc=petr.pavlu@suse.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox