The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Chris Mason <mason@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, jolsa@kernel.org, song@kernel.org
Cc: eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
	yonghong.song@linux.dev, emil@etsalapatis.com,
	ihor.solodrai@linux.dev, rostedt@goodmis.org,
	mhiramat@kernel.org, mathieu.desnoyers@efficios.com,
	mjambigi@linux.ibm.com, petr.pavlu@suse.com,
	linux-trace-kernel@vger.kernel.org,
	linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org,
	mason@kernel.org
Subject: [PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules
Date: Thu,  3 Dec 2026 15:26:16 -0500	[thread overview]
Message-ID: <20261203202617.31741-2-mason@kernel.org> (raw)
In-Reply-To: <20261203202617.31741-1-mason@kernel.org>

bpf_raw_tp_link_release() hits a use-after-free on link->btp if the
tracepoint's module fails to load after the link was created.

link->btp points into the module's data, and the link holds a module
reference to keep it valid. But a failed load frees the module without
checking refcnt, and try_module_get() in bpf_get_raw_tracepoint_module()
succeeds for a module in MODULE_STATE_COMING:

    module load                       raw tracepoint link
    ===========                       ===================
    prepare_coming_module()
      bpf_event_notify()
        list_add()
                                      bpf_raw_tp_link_attach()
                                        bpf_get_raw_tracepoint()
                                          try_module_get()
                                        link->btp = btp
    do_init_module()
      mod->init() fails
      free_module()
                                      bpf_raw_tp_link_release()
                                        bpf_probe_unregister()
                                          btp->tp
                                            /* use after free */

The fix used here is to pin a module only once it is live, as
btf_try_get_module() does.

Fixes: a38d1107f937 ("bpf: support raw tracepoints in modules")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 kernel/trace/bpf_trace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 19e6b51bb2f00..7ad178c3d04db 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -65,7 +65,9 @@ static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
 		for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
 			btp = &btm->module->bpf_raw_events[i];
 			if (!strcmp(btp->tp->name, name)) {
-				if (try_module_get(btm->module))
+				/* A failed load frees the module despite references */
+				if (btm->module->state == MODULE_STATE_LIVE &&
+				    try_module_get(btm->module))
 					ret = btp;
 				goto out;
 			}
-- 
2.55.0


  reply	other threads:[~2026-12-03 20:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 20:26 [PATCH bpf 0/2] bpf: don't attach links to modules that are still loading Chris Mason
2026-12-03 20:26 ` Chris Mason [this message]
2026-12-03 20:26 ` [PATCH bpf 2/2] bpf: Reject kprobe_multi links on coming modules Chris Mason

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=20261203202617.31741-2-mason@kernel.org \
    --to=mason@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mjambigi@linux.ibm.com \
    --cc=petr.pavlu@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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