]> 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:08:21 +0000 (14:08 +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 d4f71372332349599271252cb025b1a6fa1890a7..6d0e9f869ad688436e34f20a3996ecea0ec69a14 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 3a1c54c9918b44fe73c7f02ea2ba1d866a06cdc0..e062f4efec8d0a56b976f949d4f2c82a3ca7c82f 100644 (file)
@@ -971,7 +971,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 d2a1b7f03068558aaf74c29edc27c944060beecb..38fa6cc118dafa8f327728999e1766532b361e11 100644 (file)
@@ -143,9 +143,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;
@@ -154,11 +157,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;
 }
 
@@ -167,6 +174,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 720b46b34ab9414d768f744439616acaeeacd12e..322d56661d04ad6c800ed0a7312163fe80432614 100644 (file)
@@ -729,7 +729,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;
 }