]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
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)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 22 May 2025 12:12:12 +0000 (14:12 +0200)
[ Upstream commit fd837de3c9cb1a162c69bc1fb1f438467fe7f2f5 ]

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>
Signed-off-by: Sasha Levin <sashal@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 4376887e0d8aabea0b3ac9fe96282e42cd853c61..c9b0533407edeb098f6ce829de5c4aee647b28ac 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)
@@ -125,6 +125,20 @@ out:
        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 936477a111d3e7d2cc47caf631d9de434e636cad..beee3f8d75444470fea1e8cadd53a72020365e32 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 508c10414a934396371be96507b920daa6f034ab..46491f3c1569cd8a47b46756695e09810695b6cb 100644 (file)
@@ -1004,7 +1004,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 606190239c8776f518091e164361ce4b178c922e..694f32d843d90cb3a3f947104419444fa3fe9254 100644 (file)
@@ -153,9 +153,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;
@@ -164,11 +167,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;
 }
 
@@ -177,6 +184,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 79f8da7e3cd492b3fbfcbfcc8856d8dab0add924..ecf04e81ddf7059184baeaee2020dee8cb2a4443 100644 (file)
@@ -730,7 +730,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;
 }