Skip to content

Commit fd837de

Browse files
committed
tracing: probes: Fix a possible race in trace_probe_log APIs
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 <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Fixes: ab105a4 ("tracing: Use tracing error_log with probe events") Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent e41b5af commit fd837de

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

kernel/trace/trace_dynevent.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "trace_output.h" /* for trace_event_sem */
1717
#include "trace_dynevent.h"
1818

19-
static DEFINE_MUTEX(dyn_event_ops_mutex);
19+
DEFINE_MUTEX(dyn_event_ops_mutex);
2020
static LIST_HEAD(dyn_event_ops_list);
2121

2222
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
116116
return ret;
117117
}
118118

119+
/*
120+
* Locked version of event creation. The event creation must be protected by
121+
* dyn_event_ops_mutex because of protecting trace_probe_log.
122+
*/
123+
int dyn_event_create(const char *raw_command, struct dyn_event_operations *type)
124+
{
125+
int ret;
126+
127+
mutex_lock(&dyn_event_ops_mutex);
128+
ret = type->create(raw_command);
129+
mutex_unlock(&dyn_event_ops_mutex);
130+
return ret;
131+
}
132+
119133
static int create_dyn_event(const char *raw_command)
120134
{
121135
struct dyn_event_operations *ops;

kernel/trace/trace_dynevent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
100100
void dyn_event_seq_stop(struct seq_file *m, void *v);
101101
int dyn_events_release_all(struct dyn_event_operations *type);
102102
int dyn_event_release(const char *raw_command, struct dyn_event_operations *type);
103+
int dyn_event_create(const char *raw_command, struct dyn_event_operations *type);
103104

104105
/*
105106
* for_each_dyn_event - iterate over the dyn_event list

kernel/trace/trace_kprobe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ static int create_or_delete_trace_kprobe(const char *raw_command)
10891089
if (raw_command[0] == '-')
10901090
return dyn_event_release(raw_command, &trace_kprobe_ops);
10911091

1092-
ret = trace_kprobe_create(raw_command);
1092+
ret = dyn_event_create(raw_command, &trace_kprobe_ops);
10931093
return ret == -ECANCELED ? -EINVAL : ret;
10941094
}
10951095

kernel/trace/trace_probe.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
154154
}
155155

156156
static struct trace_probe_log trace_probe_log;
157+
extern struct mutex dyn_event_ops_mutex;
157158

158159
void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
159160
{
161+
lockdep_assert_held(&dyn_event_ops_mutex);
162+
160163
trace_probe_log.subsystem = subsystem;
161164
trace_probe_log.argc = argc;
162165
trace_probe_log.argv = argv;
@@ -165,11 +168,15 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
165168

166169
void trace_probe_log_clear(void)
167170
{
171+
lockdep_assert_held(&dyn_event_ops_mutex);
172+
168173
memset(&trace_probe_log, 0, sizeof(trace_probe_log));
169174
}
170175

171176
void trace_probe_log_set_index(int index)
172177
{
178+
lockdep_assert_held(&dyn_event_ops_mutex);
179+
173180
trace_probe_log.index = index;
174181
}
175182

@@ -178,6 +185,8 @@ void __trace_probe_log_err(int offset, int err_type)
178185
char *command, *p;
179186
int i, len = 0, pos = 0;
180187

188+
lockdep_assert_held(&dyn_event_ops_mutex);
189+
181190
if (!trace_probe_log.argv)
182191
return;
183192

kernel/trace/trace_uprobe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ static int create_or_delete_trace_uprobe(const char *raw_command)
741741
if (raw_command[0] == '-')
742742
return dyn_event_release(raw_command, &trace_uprobe_ops);
743743

744-
ret = trace_uprobe_create(raw_command);
744+
ret = dyn_event_create(raw_command, &trace_uprobe_ops);
745745
return ret == -ECANCELED ? -EINVAL : ret;
746746
}
747747

0 commit comments

Comments
 (0)