Skip to content

Commit 75f5307

Browse files
tracing: fprobe events: Fix possible UAF on modules
JIRA: https://issues.redhat.com/browse/RHEL-86682 Conflicts: Context change from missing commit 8275637 ("tracing: Adopt __free() and guard() for trace_fprobe.c") commit dd94150 Author: Masami Hiramatsu (Google) <mhiramat@kernel.org> Date: Mon Mar 31 23:05:07 2025 +0900 tracing: fprobe events: Fix possible UAF on modules Commit ac91052 ("tracing: tprobe-events: Fix leakage of module refcount") moved try_module_get() from __find_tracepoint_module_cb() to find_tracepoint() caller, but that introduced a possible UAF because the module can be unloaded before try_module_get(). In this case, the module object should be freed too. Thus, try_module_get() does not only fail but may access to the freed object. To avoid that, try_module_get() in __find_tracepoint_module_cb() again. Link: https://lore.kernel.org/all/174342990779.781946.9138388479067729366.stgit@devnote2/ Fixes: ac91052 ("tracing: tprobe-events: Fix leakage of module refcount") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
1 parent 631b6f5 commit 75f5307

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

kernel/trace/trace_fprobe.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,15 @@ static void __find_tracepoint_module_cb(struct tracepoint *tp, struct module *mo
888888
struct __find_tracepoint_cb_data *data = priv;
889889

890890
if (!data->tpoint && !strcmp(data->tp_name, tp->name)) {
891-
data->tpoint = tp;
892-
if (!data->mod)
891+
/* If module is not specified, try getting module refcount. */
892+
if (!data->mod && mod) {
893+
/* If failed to get refcount, ignore this tracepoint. */
894+
if (!try_module_get(mod))
895+
return;
896+
893897
data->mod = mod;
898+
}
899+
data->tpoint = tp;
894900
}
895901
}
896902

@@ -902,7 +908,11 @@ static void __find_tracepoint_cb(struct tracepoint *tp, void *priv)
902908
data->tpoint = tp;
903909
}
904910

905-
/* Find a tracepoint from kernel and module. */
911+
/*
912+
* Find a tracepoint from kernel and module. If the tracepoint is on the module,
913+
* the module's refcount is incremented and returned as *@tp_mod. Thus, if it is
914+
* not NULL, caller must call module_put(*tp_mod) after used the tracepoint.
915+
*/
906916
static struct tracepoint *find_tracepoint(const char *tp_name,
907917
struct module **tp_mod)
908918
{
@@ -931,7 +941,10 @@ static void reenable_trace_fprobe(struct trace_fprobe *tf)
931941
}
932942
}
933943

934-
/* Find a tracepoint from specified module. */
944+
/*
945+
* Find a tracepoint from specified module. In this case, this does not get the
946+
* module's refcount. The caller must ensure the module is not freed.
947+
*/
935948
static struct tracepoint *find_tracepoint_in_module(struct module *mod,
936949
const char *tp_name)
937950
{
@@ -1167,11 +1180,6 @@ static int __trace_fprobe_create(int argc, const char *argv[])
11671180
if (is_tracepoint) {
11681181
ctx.flags |= TPARG_FL_TPOINT;
11691182
tpoint = find_tracepoint(symbol, &tp_mod);
1170-
/* lock module until register this tprobe. */
1171-
if (tp_mod && !try_module_get(tp_mod)) {
1172-
tpoint = NULL;
1173-
tp_mod = NULL;
1174-
}
11751183
if (tpoint) {
11761184
ctx.funcname = kallsyms_lookup(
11771185
(unsigned long)tpoint->probestub,

0 commit comments

Comments
 (0)