Skip to content

Commit 28a1a64

Browse files
mhiramatvijay-suman
authored andcommitted
tracing: probes: Fix a possible race in trace_probe_log APIs
[ 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 <[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]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit 67c90814452951acac7509ba9492262500382046) Signed-off-by: Vijayendra Suman <[email protected]>
1 parent 90d1e50 commit 28a1a64

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)
@@ -125,6 +125,20 @@ int dyn_event_release(const char *raw_command, struct dyn_event_operations *type
125125
return ret;
126126
}
127127

128+
/*
129+
* Locked version of event creation. The event creation must be protected by
130+
* dyn_event_ops_mutex because of protecting trace_probe_log.
131+
*/
132+
int dyn_event_create(const char *raw_command, struct dyn_event_operations *type)
133+
{
134+
int ret;
135+
136+
mutex_lock(&dyn_event_ops_mutex);
137+
ret = type->create(raw_command);
138+
mutex_unlock(&dyn_event_ops_mutex);
139+
return ret;
140+
}
141+
128142
static int create_dyn_event(const char *raw_command)
129143
{
130144
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
@@ -970,7 +970,7 @@ static int create_or_delete_trace_kprobe(const char *raw_command)
970970
if (raw_command[0] == '-')
971971
return dyn_event_release(raw_command, &trace_kprobe_ops);
972972

973-
ret = trace_kprobe_create(raw_command);
973+
ret = dyn_event_create(raw_command, &trace_kprobe_ops);
974974
return ret == -ECANCELED ? -EINVAL : ret;
975975
}
976976

kernel/trace/trace_probe.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ static const struct fetch_type *find_fetch_type(const char *type)
143143
}
144144

145145
static struct trace_probe_log trace_probe_log;
146+
extern struct mutex dyn_event_ops_mutex;
146147

147148
void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
148149
{
150+
lockdep_assert_held(&dyn_event_ops_mutex);
151+
149152
trace_probe_log.subsystem = subsystem;
150153
trace_probe_log.argc = argc;
151154
trace_probe_log.argv = argv;
@@ -154,11 +157,15 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
154157

155158
void trace_probe_log_clear(void)
156159
{
160+
lockdep_assert_held(&dyn_event_ops_mutex);
161+
157162
memset(&trace_probe_log, 0, sizeof(trace_probe_log));
158163
}
159164

160165
void trace_probe_log_set_index(int index)
161166
{
167+
lockdep_assert_held(&dyn_event_ops_mutex);
168+
162169
trace_probe_log.index = index;
163170
}
164171

@@ -167,6 +174,8 @@ void __trace_probe_log_err(int offset, int err_type)
167174
char *command, *p;
168175
int i, len = 0, pos = 0;
169176

177+
lockdep_assert_held(&dyn_event_ops_mutex);
178+
170179
if (!trace_probe_log.argv)
171180
return;
172181

kernel/trace/trace_uprobe.c

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

732-
ret = trace_uprobe_create(raw_command);
732+
ret = dyn_event_create(raw_command, &trace_uprobe_ops);
733733
return ret == -ECANCELED ? -EINVAL : ret;
734734
}
735735

0 commit comments

Comments
 (0)