tracing: probes: Fix a possible race in trace_probe_log APIs
authorMasami Hiramatsu (Google) <mhiramat@kernel.org>
Sat, 10 May 2025 03:44:41 +0000 (12:44 +0900)
committerMasami Hiramatsu (Google) <mhiramat@kernel.org>
Tue, 13 May 2025 13:23:34 +0000 (22:23 +0900)
Since the shared trace_probe_log variable can be accessed and
modified via probe event create operation of kprobe_events,
uprobe_events, and dynamic_events, it should be protected.
In the dynamic_events, all operations are serialized by
`dyn_event_ops_mutex`. But kprobe_events and uprobe_events
interfaces are not serialized.

To solve this issue, introduces dyn_event_create(), which runs
create() operation under the mutex, for kprobe_events and
uprobe_events. This also uses lockdep to check the mutex is
held when using trace_probe_log* APIs.

Link: https://lore.kernel.org/all/174684868120.551552.3068655787654268804.stgit@devnote2/
Reported-by: Paul Cacheux <paulcacheux@gmail.com>
Closes: https://lore.kernel.org/all/20250510074456.805a16872b591e2971a4d221@kernel.org/
Fixes: ab105a4fb894 ("tracing: Use tracing error_log with probe events")
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
kernel/trace/trace_dynevent.c
kernel/trace/trace_dynevent.h
kernel/trace/trace_kprobe.c
kernel/trace/trace_probe.c
kernel/trace/trace_uprobe.c

index a322e4f..5d64a18 100644 (file)
@@ -16,7 +16,7 @@
 #include "trace_output.h"      /* for trace_event_sem */
 #include "trace_dynevent.h"
 
-static DEFINE_MUTEX(dyn_event_ops_mutex);
+DEFINE_MUTEX(dyn_event_ops_mutex);
 static LIST_HEAD(dyn_event_ops_list);
 
 bool trace_event_dyn_try_get_ref(struct trace_event_call *dyn_call)
@@ -116,6 +116,20 @@ int dyn_event_release(const char *raw_command, struct dyn_event_operations *type
        return ret;
 }
 
+/*
+ * Locked version of event creation. The event creation must be protected by
+ * dyn_event_ops_mutex because of protecting trace_probe_log.
+ */
+int dyn_event_create(const char *raw_command, struct dyn_event_operations *type)
+{
+       int ret;
+
+       mutex_lock(&dyn_event_ops_mutex);
+       ret = type->create(raw_command);
+       mutex_unlock(&dyn_event_ops_mutex);
+       return ret;
+}
+
 static int create_dyn_event(const char *raw_command)
 {
        struct dyn_event_operations *ops;
index 936477a..beee3f8 100644 (file)
@@ -100,6 +100,7 @@ void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
 void dyn_event_seq_stop(struct seq_file *m, void *v);
 int dyn_events_release_all(struct dyn_event_operations *type);
 int dyn_event_release(const char *raw_command, struct dyn_event_operations *type);
+int dyn_event_create(const char *raw_command, struct dyn_event_operations *type);
 
 /*
  * for_each_dyn_event  -       iterate over the dyn_event list
index 2703b96..3e5c47b 100644 (file)
@@ -1089,7 +1089,7 @@ static int create_or_delete_trace_kprobe(const char *raw_command)
        if (raw_command[0] == '-')
                return dyn_event_release(raw_command, &trace_kprobe_ops);
 
-       ret = trace_kprobe_create(raw_command);
+       ret = dyn_event_create(raw_command, &trace_kprobe_ops);
        return ret == -ECANCELED ? -EINVAL : ret;
 }
 
index 2eeecb6..424751c 100644 (file)
@@ -154,9 +154,12 @@ fail:
 }
 
 static struct trace_probe_log trace_probe_log;
+extern struct mutex dyn_event_ops_mutex;
 
 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {
+       lockdep_assert_held(&dyn_event_ops_mutex);
+
        trace_probe_log.subsystem = subsystem;
        trace_probe_log.argc = argc;
        trace_probe_log.argv = argv;
@@ -165,11 +168,15 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 
 void trace_probe_log_clear(void)
 {
+       lockdep_assert_held(&dyn_event_ops_mutex);
+
        memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 }
 
 void trace_probe_log_set_index(int index)
 {
+       lockdep_assert_held(&dyn_event_ops_mutex);
+
        trace_probe_log.index = index;
 }
 
@@ -178,6 +185,8 @@ void __trace_probe_log_err(int offset, int err_type)
        char *command, *p;
        int i, len = 0, pos = 0;
 
+       lockdep_assert_held(&dyn_event_ops_mutex);
+
        if (!trace_probe_log.argv)
                return;
 
index 3386439..35cf76c 100644 (file)
@@ -741,7 +741,7 @@ static int create_or_delete_trace_uprobe(const char *raw_command)
        if (raw_command[0] == '-')
                return dyn_event_release(raw_command, &trace_uprobe_ops);
 
-       ret = trace_uprobe_create(raw_command);
+       ret = dyn_event_create(raw_command, &trace_uprobe_ops);
        return ret == -ECANCELED ? -EINVAL : ret;
 }