Commit fd837de3 authored by Masami Hiramatsu (Google)'s avatar Masami Hiramatsu (Google)
Browse files

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: default avatarPaul Cacheux <paulcacheux@gmail.com>
Closes: https://lore.kernel.org/all/20250510074456.805a16872b591e2971a4d221@kernel.org/


Fixes: ab105a4f ("tracing: Use tracing error_log with probe events")
Signed-off-by: default avatarMasami Hiramatsu (Google) <mhiramat@kernel.org>
parent e41b5af4
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -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;
+1 −0
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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;
}

+9 −0
Original line number Diff line number Diff line
@@ -154,9 +154,12 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
}

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;

+1 −1
Original line number Diff line number Diff line
@@ -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;
}