[PATCH bpf 1/2] bpf: Reject raw tracepoint links on coming modules
From: Chris Mason
Date: Thu Dec 03 2026 - 15:26:32 EST
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@xxxxxxxxxx>
---
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