The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH bpf 0/2] bpf: don't attach links to modules that are still loading
@ 2026-12-03 20:26 Chris Mason
  2026-12-03 20:26 ` [PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules Chris Mason
  2026-12-03 20:26 ` [PATCH bpf 2/2] bpf: Reject kprobe_multi " Chris Mason
  0 siblings, 2 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-03 20:26 UTC (permalink / raw)
  To: bpf, ast, daniel, andrii, jolsa, song
  Cc: eddyz87, memxor, martin.lau, yonghong.song, emil, ihor.solodrai,
	rostedt, mhiramat, mathieu.desnoyers, mjambigi, petr.pavlu,
	linux-trace-kernel, linux-modules, linux-kernel, mason

Hi everyone,

These are AI generated fixes for two use-after-frees in BPF links.

When a module fails to load, we free it without checking for references.
If somebody creates a link while it's still loading, the link is left
pointing at freed memory.  With these applied, the link fails until the
module is live.

Mahanta Jambigi has a patch that makes the module loader wait for those
references instead:

  [PATCH v2] module: drain external refs before freeing a failed-init module
  https://lore.kernel.org/all/20261201103922.1480284-1-mjambigi@linux.ibm.com/

It doesn't cover all of the ways a load can fail, and an open link could
make it wait forever, so I think we need both.

This is on top of v7.4-rc5.

Chris Mason (2):
  bpf: Reject raw tracepoint links on coming modules
  bpf: Reject kprobe_multi links on coming modules

 kernel/trace/bpf_trace.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.55.0


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

* [PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules
  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
  2026-12-03 20:26 ` [PATCH bpf 2/2] bpf: Reject kprobe_multi " Chris Mason
  1 sibling, 0 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-03 20:26 UTC (permalink / raw)
  To: bpf, ast, daniel, andrii, jolsa, song
  Cc: eddyz87, memxor, martin.lau, yonghong.song, emil, ihor.solodrai,
	rostedt, mhiramat, mathieu.desnoyers, mjambigi, petr.pavlu,
	linux-trace-kernel, linux-modules, linux-kernel, mason

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


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

* [PATCH bpf 2/2] bpf: Reject kprobe_multi links on coming modules
  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 ` [PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules Chris Mason
@ 2026-12-03 20:26 ` Chris Mason
  1 sibling, 0 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-03 20:26 UTC (permalink / raw)
  To: bpf, ast, daniel, andrii, jolsa, song
  Cc: eddyz87, memxor, martin.lau, yonghong.song, emil, ihor.solodrai,
	rostedt, mhiramat, mathieu.desnoyers, mjambigi, petr.pavlu,
	linux-trace-kernel, linux-modules, linux-kernel, mason

bpf_kprobe_multi_link_release() hits a use-after-free on struct module
if a probed module fails to load.

get_modules_for_addrs() pins each probed module with try_module_get(),
which also succeeds in MODULE_STATE_COMING. But the error paths of
load_module() and do_init_module() free the module without checking
refcnt:

    module load                         kprobe_multi link
    ===========                         =================
    complete_formation()
      mod->state = MODULE_STATE_COMING
                                        bpf_kprobe_multi_link_attach()
                                          get_modules_for_addrs()
                                            try_module_get()
    do_init_module()
      mod->init() fails
      free_module()
                                        bpf_kprobe_multi_link_release()
                                          module_put()
                                            /* use after free */

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

Fixes: e22061b2d309 ("bpf: Take module reference on kprobe_multi link")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 kernel/trace/bpf_trace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 7ad178c3d04db..dbe68a0805db9 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2780,7 +2780,10 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3
 				skip_add = true;
 				break; /* scoped_guard */
 			}
-			if (!try_module_get(mod))
+			/* A failed load frees the module despite references */
+			if (module_is_coming(mod))
+				err = -EBUSY;
+			else if (mod->state != MODULE_STATE_LIVE || !try_module_get(mod))
 				err = -EINVAL;
 		}
 		if (skip_add)
-- 
2.55.0


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

end of thread, other threads:[~2026-12-03 20:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules Chris Mason
2026-12-03 20:26 ` [PATCH bpf 2/2] bpf: Reject kprobe_multi " Chris Mason

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