Skip to contentSkip to conversation

tracing: fix race when creating trace probe log error message

PATCH Superseded Paul Cacheux 3 participants 8 messages May 2, 2025 – May 9, 2025 Tracing

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tracing: fix race when creating trace probe log error message

Hello,

As reported in [1] a race exists in the shared trace probe log used to build error messages. This can cause kernel crashes when building the actual error message, but the race happens even for non-error tracefs uses, it's just not visible.

Reproducer first reported that is still crashing:

# 'p4' is invalid command which make kernel run into trace_probe_log_err()
cd /sys/kernel/debug/tracing
while true; do
echo 'p4:myprobe1 do_sys_openat2 dfd=%ax filename=%dx flags=%cx mode=+4($stack)' >> kprobe_events & echo 'p4:myprobe2 do_sys_openat2' >> kprobe_events &
echo 'p4:myprobe3 do_sys_openat2 dfd=%ax filename=%dx' >> kprobe_events &
done;

The original email suggested to use a mutex or to allocate the trace_probe_log on the stack. This patch implements a simpler solution suggest during the review of the v1 where we only protect access to the shared trace_probe_log with a mutex. This will prevent any crash from happening.

[1] https://lore.kernel.org/all/[email protected]/T/

---
Changes in v2:
- change approach, and use the mutex based solution suggested during
review
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Paul Cacheux (2):
tracing: add missing trace_probe_log_clear for eprobes
tracing: protect trace_probe_log with mutex

 kernel/trace/trace_eprobe.c | 3 +++
 kernel/trace/trace_probe.c  | 6 ++++++
 2 files changed, 9 insertions(+)

---
base-commit: ebd297a2affadb6f6f4d2e5d975c1eda18ac762d
change-id: 20250420-fix-trace-probe-log-race-cc89d8e5fb15

Best regards,

Signature · 1 line
Paul Cacheux <[email protected]>
#1 lore raw

PATCH v2 1/2 tracing: add missing trace_probe_log_clear for eprobes

Make sure trace_probe_log_clear is called in the tracing eprobe code path, matching the trace_probe_log_init call.

---
 kernel/trace/trace_eprobe.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_eprobe.c +3 -0
index c08355c3ef32b4124ac944d7e3a03efb66492269..916555f0de811f03feed9d923c63078b0e4c993b 100644
@@ -969,10 +969,13 @@ static int __trace_eprobe_create(int argc, const char *argv[])
 			goto error;
 		}
 	}
+	trace_probe_log_clear();
 	return ret;
+
 parse_error:
 	ret = -EINVAL;
 error:
+	trace_probe_log_clear();
 	trace_event_probe_cleanup(ep);
 	return ret;
 }
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH v2 1/2] tracing: add missing trace_probe_log_clear for eprobes

Quoted 6 lines · On Fri, 02 May 2025 15:15:52 +0200 Paul Cacheux via B4 Relay…

From: Paul Cacheux <[email protected]>

Make sure trace_probe_log_clear is called in the tracing eprobe code path, matching the trace_probe_log_init call.

Looks good to me.

Fixes 7491e2c44278 ("tracing: Add a probe that attaches to trace events")

Thank you,

Quoted 27 lines
---
 kernel/trace/trace_eprobe.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_eprobe.c +3 -0
index c08355c3ef32b4124ac944d7e3a03efb66492269..916555f0de811f03feed9d923c63078b0e4c993b 100644
@@ -969,10 +969,13 @@ static int __trace_eprobe_create(int argc, const char *argv[])
 			goto error;
 		}
 	}
+	trace_probe_log_clear();
 	return ret;
+
 parse_error:
 	ret = -EINVAL;
 error:
+	trace_probe_log_clear();
 	trace_event_probe_cleanup(ep);
 	return ret;
 }
Signature · 2 lines
-- 
2.49.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#3 lore raw

PATCH v2 2/2 tracing: protect trace_probe_log with mutex

In reply to Paul Cacheux

The shared trace_probe_log variable can be accessed and modified by multiple processes using tracefs at the same time, this new mutex will guarantee it's always in a coherent state.

There is no guarantee that multiple errors happening at the same time will each have the correct error message, but at least this won't crash.

Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe events")
---
 kernel/trace/trace_probe.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +6 -0
index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..14a7a0b59cd20a8bc43e3e7c653e986081f924c8 100644
@@ -154,9 +154,11 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
 }
 
 static struct trace_probe_log trace_probe_log;
+static DEFINE_MUTEX(trace_probe_log_lock);
 
 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.subsystem = subsystem;
 	trace_probe_log.argc = argc;
 	trace_probe_log.argv = argv;
@@ -165,11 +167,13 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 
 void trace_probe_log_clear(void)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 }
 
 void trace_probe_log_set_index(int index)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.index = index;
 }
 
@@ -178,6 +182,8 @@ void __trace_probe_log_err(int offset, int err_type)
 	char *command, *p;
 	int i, len = 0, pos = 0;
 
+	guard(mutex)(&trace_probe_log_lock);
+
 	if (!trace_probe_log.argv)
 		return;
 
Signature · 1 line
2.49.0
maintainer
#4 lore raw

Re: [PATCH v2 2/2] tracing: protect trace_probe_log with mutex

Quoted 6 lines · On Fri, 02 May 2025 15:15:53 +0200 Paul Cacheux via B4 Relay…

From: Paul Cacheux <[email protected]>

The shared trace_probe_log variable can be accessed and modified by multiple processes using tracefs at the same time, this new mutex will guarantee it's always in a coherent state.

Quoted 6 lines

There is no guarantee that multiple errors happening at the same time will each have the correct error message, but at least this won't crash.

Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe events")

No space needed between Fixes and SOB.

Quoted 10 lines
---
 kernel/trace/trace_probe.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +0 -0
index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..14a7a0b59cd20a8bc43e3e7c653e986081f924c8 100644

kernel/trace/trace_probe.c static const struct fetch_type *find_fetch_type(const char *type, unsigned long · line 157

Quoted 4 lines
kernel/trace/trace_probe.c +1 -0
@@ -154,9 +154,11 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
 }
 
 static struct trace_probe_log trace_probe_log;
+static DEFINE_MUTEX(trace_probe_log_lock);

Probably should add a comment here saying something like:

/*
* The trace_probe_log_lock only protects against the individual
* modification of the trace_probe_log. It does not protect against
* the log from producing garbage if two probes use it at the same
* time. That would only happen if two admins were trying to add
* probes simultaneously which they shouldn't be doing. */

-- Steve

Quoted 31 lines
kernel/trace/trace_probe.c +5 -0
 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.subsystem = subsystem;
 	trace_probe_log.argc = argc;
 	trace_probe_log.argv = argv;
@@ -165,11 +167,13 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 
 void trace_probe_log_clear(void)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 }
 
 void trace_probe_log_set_index(int index)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.index = index;
 }
 
@@ -178,6 +182,8 @@ void __trace_probe_log_err(int offset, int err_type)
 	char *command, *p;
 	int i, len = 0, pos = 0;
 
+	guard(mutex)(&trace_probe_log_lock);
+
 	if (!trace_probe_log.argv)
 		return;
 
#5 lore raw

Re: [PATCH v2 2/2] tracing: protect trace_probe_log with mutex

Quoted 14 lines · Steven Rostedt
Quoted 10 lines · On Fri, 02 May 2025 15:15:53 +0200 Paul Cacheux via B4 Relay…

From: Paul Cacheux <[email protected]>

The shared trace_probe_log variable can be accessed and modified by multiple processes using tracefs at the same time, this new mutex will guarantee it's always in a coherent state.

There is no guarantee that multiple errors happening at the same time will each have the correct error message, but at least this won't crash.

Quoted 5 lines
Quoted 3 lines
Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe

events")

No space needed between Fixes and SOB.

Sorry about that, fixed in v3

Quoted 22 lines
Quoted 16 lines

---
 kernel/trace/trace_probe.c | 6 ++++++

 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +0 -0

index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..14a7a0b59cd20a8bc43e3e7c6 53e986081f924c8 100644

kernel/trace/trace_probe.c +0 -0
@@ -154,9 +154,11 @@ static const struct fetch_type
 *find_fetch_type(const char *type, unsigned long
  }
Quoted 3 lines


 static struct trace_probe_log trace_probe_log;
+static DEFINE_MUTEX(trace_probe_log_lock);

Probably should add a comment here saying something like:

Quoted 7 lines

/*
 * The trace_probe_log_lock only protects against the individual
 * modification of the trace_probe_log. It does not protect against
 * the log from producing garbage if two probes use it at the same
 * time. That would only happen if two admins were trying to add
 * probes simultaneously which they shouldn't be doing. */

I sent a v3 with the space removed and this comment added, thank you for the review.

Quoted 30 lines

-- Steve

Quoted 26 lines


 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {

kernel/trace/trace_probe.c +1 -0
+	guard(mutex)(&trace_probe_log_lock);

  trace_probe_log.subsystem = subsystem;
  trace_probe_log.argc = argc;
  trace_probe_log.argv = argv;

kernel/trace/trace_probe.c +0 -0
@@ -165,11 +167,13 @@ void trace_probe_log_init(const char
 *subsystem, int argc, const char **argv)


 void trace_probe_log_clear(void)
 {

kernel/trace/trace_probe.c +1 -0
+	guard(mutex)(&trace_probe_log_lock);

  memset(&trace_probe_log, 0, sizeof(trace_probe_log)); }

 void trace_probe_log_set_index(int index)
 {

kernel/trace/trace_probe.c +1 -0
+	guard(mutex)(&trace_probe_log_lock);

  trace_probe_log.index = index;
 }

kernel/trace/trace_probe.c +0 -0
@@ -178,6 +182,8 @@ void __trace_probe_log_err(int offset, int
 err_type)
  	char *command, *p;
Quoted 8 lines
Quoted 8 lines
kernel/trace/trace_probe.c +0 -0
  	int i, len = 0, pos = 0;

kernel/trace/trace_probe.c +2 -0
+	guard(mutex)(&trace_probe_log_lock);
+

  if (!trace_probe_log.argv)
  return;

Best regards,

Paul Cacheux

#6 lore raw

Re: [PATCH v2 2/2] tracing: protect trace_probe_log with mutex

In reply to Paul Cacheux
Quoted 5 lines · On Fri, 02 May 2025 15:15:53 +0200 Paul Cacheux via B4 Relay…

From: Paul Cacheux <[email protected]>

The shared trace_probe_log variable can be accessed and modified by multiple processes using tracefs at the same time, this new mutex will guarantee it's always in a coherent state.

Actually that's not happen. For the create part (and currently the event_log is used only in the creation), dyn_event_ops_mutex is already held in create_dyn_event(). Thus, it is already serialized.

And even if there are multiple events are done in parallel, this is not enough because trace_probe_log::argc/argv can be changed before other user finishs to use the log.

Thank you,

Quoted 56 lines

There is no guarantee that multiple errors happening at the same time will each have the correct error message, but at least this won't crash.

Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe events")
---
 kernel/trace/trace_probe.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +6 -0
index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..14a7a0b59cd20a8bc43e3e7c653e986081f924c8 100644
@@ -154,9 +154,11 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
 }
 
 static struct trace_probe_log trace_probe_log;
+static DEFINE_MUTEX(trace_probe_log_lock);
 
 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.subsystem = subsystem;
 	trace_probe_log.argc = argc;
 	trace_probe_log.argv = argv;
@@ -165,11 +167,13 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 
 void trace_probe_log_clear(void)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 }
 
 void trace_probe_log_set_index(int index)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.index = index;
 }
 
@@ -178,6 +182,8 @@ void __trace_probe_log_err(int offset, int err_type)
 	char *command, *p;
 	int i, len = 0, pos = 0;
 
+	guard(mutex)(&trace_probe_log_lock);
+
 	if (!trace_probe_log.argv)
 		return;
 
Signature · 2 lines
-- 
2.49.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#7 lore raw

Re: [PATCH v2 2/2] tracing: protect trace_probe_log with mutex

Quoted 5 lines · On Sat, 10 May 2025 07:44:56 +0900 Masami Hiramatsu (Google)…
Quoted 2 lines · On Fri, 02 May 2025 15:15:53 +0200 Paul Cacheux via B4 Relay…

From: Paul Cacheux <[email protected]>

Quoted 7 lines
Quoted 3 lines

The shared trace_probe_log variable can be accessed and modified by multiple processes using tracefs at the same time, this new mutex will guarantee it's always in a coherent state.

Actually that's not happen. For the create part (and currently the event_log is used only in the creation), dyn_event_ops_mutex is already held in create_dyn_event(). Thus, it is already serialized.

Oops, if user writes some commands not directly from dynevent, (e.g. tracefs/{kprobe_events,uprobe_events} )the mutex is not held. Hmm, we need to make another patch.

Quoted 4 lines

And even if there are multiple events are done in parallel, this is not enough because trace_probe_log::argc/argv can be changed before other user finishs to use the log.

But this is still valid. We need to reroute the creation to dynamic_events.

Thank you,

Quoted 63 lines

Thank you,

Quoted 56 lines

There is no guarantee that multiple errors happening at the same time will each have the correct error message, but at least this won't crash.

Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe events")
---
 kernel/trace/trace_probe.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_probe.c +6 -0
index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..14a7a0b59cd20a8bc43e3e7c653e986081f924c8 100644
@@ -154,9 +154,11 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
 }
 
 static struct trace_probe_log trace_probe_log;
+static DEFINE_MUTEX(trace_probe_log_lock);
 
 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.subsystem = subsystem;
 	trace_probe_log.argc = argc;
 	trace_probe_log.argv = argv;
@@ -165,11 +167,13 @@ void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 
 void trace_probe_log_clear(void)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 }
 
 void trace_probe_log_set_index(int index)
 {
+	guard(mutex)(&trace_probe_log_lock);
 	trace_probe_log.index = index;
 }
 
@@ -178,6 +182,8 @@ void __trace_probe_log_err(int offset, int err_type)
 	char *command, *p;
 	int i, len = 0, pos = 0;
 
+	guard(mutex)(&trace_probe_log_lock);
+
 	if (!trace_probe_log.argv)
 		return;
 
Signature · 2 lines
-- 
2.49.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>