Skip to contentSkip to conversation

tracing: fix race when creating trace probe log error message

PATCH In review Paul Cacheux 2 participants 6 messages May 4, 2025 – May 7, 2025 Tracing

lore
Share & tools
lore
#0 lore raw

v3 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 v3:
- add some comment around the new mutex definition
- Link to v2: https://lore.kernel.org/r/[email protected]

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  | 13 +++++++++++++
 2 files changed, 16 insertions(+)

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

Best regards,

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

PATCH v3 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
maintainer Acked-by on v3 1/2
#2 lore raw

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

Quoted 9 lines · On Sun, 04 May 2025 20:27: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.

---
 kernel/trace/trace_eprobe.c | 3 +++
 1 file changed, 3 insertions(+)

-- Steve

#3 lore raw

PATCH v3 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 | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/trace/trace_probe.c +13 -0
index 2eeecb6c95eea55502b83af6775b7b6f0cc5ab94..d538964c87a52b06646ec1b8c3469be7399f04fa 100644
@@ -153,10 +153,19 @@ static const struct fetch_type *find_fetch_type(const char *type, unsigned long
 	return NULL;
 }
 
+/*
+ * 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.
+ */
+static DEFINE_MUTEX(trace_probe_log_lock);
 static struct trace_probe_log trace_probe_log;
 
 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 +174,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 +189,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 Acked-by on v3 2/2
#4 lore raw

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

Quoted 10 lines · On Sun, 04 May 2025 20:27: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
Fixes ab105a4fb894 ("tracing: Use tracing error_log with probe events")
---
 kernel/trace/trace_probe.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

-- Steve

maintainer
#5 lore raw
In reply to Paul Cacheux

Masami,

Do you want to take this?

-- Steve

Quoted 49 lines · On Sun, 04 May 2025 20:27:51 +0200 Paul Cacheux via B4 Relay…

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 v3:
- add some comment around the new mutex definition
- Link to v2: https://lore.kernel.org/r/[email protected]

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  | 13 +++++++++++++
 2 files changed, 16 insertions(+)

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

Best regards,