Skip to contentSkip to conversation

tracing: Bound synthetic event dynamic string and stack stores to reserved size

PATCH In review Dominik Maier 3 participants 4 messages Dec 3 – Dec 4 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 tracing: Bound synthetic event dynamic string and stack stores to reserved size

Synthetic events measure dynamic string and stack fields before reserving ring-buffer space, then copy from unlocked histogram variables without re-checking the reserved size.

If another CPU stores a longer string or deeper stack trace before the copy runs, trace_string() and trace_stack() write past the reserved entry and corrupt adjacent kernel memory. Truncated or empty dynamic fields can also cause out-of-bounds reads when formatting or keying events.

Reserve at least one NUL byte per dynamic string, bound string and stack copies to the remaining budget while NUL-terminating every string, and guard empty fields and stack depth in readers.

Fixes bd82631d7ccd ("tracing: Add support for dynamic strings to synthetic events")
Fixes 00cf3d672a9d ("tracing: Allow synthetic events to pass around stacktraces")

---
v3:
- Always NUL-terminate dynamic strings, guard zero-length strings in print_synth_event(), and use "tracing: Bound ..." subject (Steven Rostedt, Sashiko).
- v2: https://lore.kernel.org/linux-trace-kernel/[email protected]/
- v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Found with an AI-assisted code review tool (ARTIST) and verified in QEMU/KVM with KASAN, lockdep, and synth_event_gen_test.

 kernel/trace/trace_events_hist.c  |  10 ++-
 kernel/trace/trace_events_synth.c | 141 +++++++++++++++++++++---------
 kernel/trace/trace_synth.h        |   1 +
 3 files changed, 109 insertions(+), 43 deletions(-)
kernel/trace/trace_events_hist.c +9 -1
index d4702ad..a3a093d 100644
@@ -251,6 +251,9 @@ static u64 hist_field_dynstring(struct hist_field *hist_field,
 	int str_loc = str_item & 0xffff;
 	char *addr = (char *)(event + str_loc);
 
+	if (!(str_item >> 16))
+		return (u64)(unsigned long)"";
+
 	return (u64)(unsigned long)addr;
 }
 
@@ -4384,10 +4387,14 @@ static u64 hist_field_stack(struct hist_field *hist_field,
 			    struct ring_buffer_event *rbe,
 			    void *event)
 {
+	static const unsigned long empty_stack;
 	u32 str_item = *(u32 *)(event + hist_field->field->offset);
 	int str_loc = str_item & 0xffff;
 	char *addr = (char *)(event + str_loc);
 
+	if ((str_item >> 16) < sizeof(unsigned long))
+		return (u64)(unsigned long)&empty_stack;
+
 	return (u64)(unsigned long)addr;
 }
 
@@ -5451,7 +5458,8 @@ static void event_hist_trigger(struct event_trigger_data *data,
 
 				field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
 				stack = (unsigned long *)(long)field_contents;
-				n_entries = *stack;
+				n_entries = min_t(unsigned long, *stack,
+						  HIST_STACKTRACE_DEPTH);
 				memcpy(entries, ++stack, n_entries * sizeof(unsigned long));
 			} else {
 				stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
kernel/trace/trace_events_synth.c +99 -42

283 lines not on this page · raw message

kernel/trace/trace_synth.h +1 -0
index d4ebf01..bf40742 100644
@@ -29,6 +29,7 @@ struct synth_event {
 	unsigned int				n_fields;
 	struct synth_field			**dynamic_fields;
 	unsigned int				n_dynamic_fields;
+	unsigned int				n_dynamic_strings;
 	unsigned int				n_u64;
 	struct trace_event_class		class;
 	struct trace_event_call			call;

base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790

Signature · 1 line
2.56.1.911.g883d8bcb20-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] tracing: unhandled is_stack fields in synth_event_trace_array() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] tracing: unhandled is_stack fields in synth_event_trace_array()
--

commit 60ba01049fe709a42cf69d5ea5a710f9f24b88c0
Author: Dominik Maier <[email protected]>

tracing: Bound synthetic event dynamic string and stack stores to reserved size

This commit bounds dynamic string and stacktrace copies in synthetic events to the reserved ring-buffer space, ensures dynamic strings are always NUL-terminated, and guards readers against empty dynamic fields and excessive stack depths.

Quoted 4 lines
kernel/trace/trace_events_synth.c +0 -0
index e2870212e58d4..ba9ee84cba036 100644

[ ... ]

Quoted 39 lines
kernel/trace/trace_events_synth.c +11 -3
@@ -1925,7 +1973,7 @@ EXPORT_SYMBOL_GPL(synth_event_trace);
 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 			    unsigned int n_vals)
 {
-	unsigned int i, n_u64, field_pos, len, data_size = 0;
+	unsigned int i, n_u64, field_pos, len, maxlen, rem_strings, data_size = 0;
 	struct synth_event_trace_state state;
 	char *str_val;
 	int ret;
@@ -1939,10 +1987,12 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 
 	if (state.event->n_dynamic_fields) {
 		for (i = 0; i < state.event->n_dynamic_fields; i++) {
+			if (state.event->dynamic_fields[i]->is_stack)
+				continue;
+
 			field_pos = state.event->dynamic_fields[i]->field_pos;
 			str_val = (char *)(long)vals[field_pos];
-			len = strlen(str_val) + 1;
-			data_size += len;
+			data_size += max(fetch_store_strlen((unsigned long)str_val), 1);
 		}
 	}
 
@@ -1955,16 +2005,23 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 		goto out;
 	}
 
+	maxlen = data_size;
+	rem_strings = state.event->n_dynamic_strings;
 	data_size = 0;
 
 	for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
 		if (state.event->fields[i]->is_string) {
 			char *str_val = (char *)(long)vals[i];
+			unsigned int str_max = 0;
+
+			if (state.event->fields[i]->is_dynamic)
+				str_max = synth_dyn_budget(maxlen, --rem_strings);

kernel/trace/trace_events_synth.c int synth_event_trace_array(struct trace_event_file *file, u64 *vals, · line 2027

Quoted 10 lines
kernel/trace/trace_events_synth.c +2 -1
 
 			len = trace_string(state.entry, state.event, str_val,
 					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   data_size, str_max, &n_u64);
 			data_size += len; /* only dynamic string increments */
+			maxlen -= len;
 		} else {
 			struct synth_field *field = state.event->fields[i];
 			u64 val = vals[i];

[Severity: Medium]
Can an is_stack field in synth_event_trace_array() or synth_event_trace() corrupt state.entry->fields[n_u64].as_dynamic and trigger an out-of-bounds read?

Skipping is_stack fields in the reservation loop reserves zero dynamic bytes for them, but the field-writing loop below has no is_stack branch and falls into the scalar switch, where field->size == sizeof(u64) writes vals[i] into state.entry->fields[n_u64].as_u64. That overwrites as_dynamic.offset and as_dynamic.len with raw bits from vals[i], so print_synth_event() and hist_field_stack() read past the end of the ring buffer entry.

Should synth_event_trace_array() and synth_event_trace() either reserve and store stack fields via synth_stack_size() and trace_stack(), or reject events with is_stack fields with -ENOTSUPP like synth_event_trace_start()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 39 lines · On Thu, 03 Dec 2026 02:24:33 +0000 [email protected] wrote:
Quoted 15 lines
kernel/trace/trace_events_synth.c +6 -0
@@ -1955,16 +2005,23 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 		goto out;
 	}
 
+	maxlen = data_size;
+	rem_strings = state.event->n_dynamic_strings;
 	data_size = 0;
 
 	for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
 		if (state.event->fields[i]->is_string) {
 			char *str_val = (char *)(long)vals[i];
+			unsigned int str_max = 0;
+
+			if (state.event->fields[i]->is_dynamic)
+				str_max = synth_dyn_budget(maxlen, --rem_strings);

kernel/trace/trace_events_synth.c int synth_event_trace_array(struct trace_event_file *file, u64 *vals, · line 2027

Quoted 10 lines
kernel/trace/trace_events_synth.c +2 -1
 
 			len = trace_string(state.entry, state.event, str_val,
 					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   data_size, str_max, &n_u64);
 			data_size += len; /* only dynamic string increments */
+			maxlen -= len;
 		} else {
 			struct synth_field *field = state.event->fields[i];
 			u64 val = vals[i];  

[Severity: Medium]
Can an is_stack field in synth_event_trace_array() or synth_event_trace() corrupt state.entry->fields[n_u64].as_dynamic and trigger an out-of-bounds read?

Skipping is_stack fields in the reservation loop reserves zero dynamic bytes for them, but the field-writing loop below has no is_stack branch and falls into the scalar switch, where field->size == sizeof(u64) writes vals[i] into state.entry->fields[n_u64].as_u64. That overwrites as_dynamic.offset and as_dynamic.len with raw bits from vals[i], so print_synth_event() and hist_field_stack() read past the end of the ring buffer entry.

Quoted 4 lines

Should synth_event_trace_array() and synth_event_trace() either reserve and store stack fields via synth_stack_size() and trace_stack(), or reject events with is_stack fields with -ENOTSUPP like synth_event_trace_start()?

Can you address this as well?

-- Steve

maintainer
#3 lore raw
In reply to Dominik Maier
Quoted 39 lines · On Thu, 3 Dec 2026 02:04:20 +0000 Dominik Maier <[email protected]> wrote:

Synthetic events measure dynamic string and stack fields before reserving ring-buffer space, then copy from unlocked histogram variables without re-checking the reserved size.

If another CPU stores a longer string or deeper stack trace before the copy runs, trace_string() and trace_stack() write past the reserved entry and corrupt adjacent kernel memory. Truncated or empty dynamic fields can also cause out-of-bounds reads when formatting or keying events.

Reserve at least one NUL byte per dynamic string, bound string and stack copies to the remaining budget while NUL-terminating every string, and guard empty fields and stack depth in readers.

Fixes bd82631d7ccd ("tracing: Add support for dynamic strings to synthetic events")
Fixes 00cf3d672a9d ("tracing: Allow synthetic events to pass around stacktraces")

---
v3:
- Always NUL-terminate dynamic strings, guard zero-length strings in print_synth_event(), and use "tracing: Bound ..." subject (Steven Rostedt, Sashiko).
- v2: https://lore.kernel.org/linux-trace-kernel/[email protected]/
- v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Found with an AI-assisted code review tool (ARTIST) and verified in QEMU/KVM with KASAN, lockdep, and synth_event_gen_test.

 kernel/trace/trace_events_hist.c  |  10 ++-
 kernel/trace/trace_events_synth.c | 141 +++++++++++++++++++++---------
 kernel/trace/trace_synth.h        |   1 +
 3 files changed, 109 insertions(+), 43 deletions(-)
kernel/trace/trace_events_hist.c +0 -0
index d4702ad..a3a093d 100644

kernel/trace/trace_events_hist.c static u64 hist_field_dynstring(struct hist_field *hist_field, · line 251

Quoted 1 line
kernel/trace/trace_events_hist.c +0 -0
@@ -251,6 +251,9 @@ static u64 hist_field_dynstring(struct hist_field *hist_field,
 	int str_loc = str_item & 0xffff;

Please add here:

	int str_len = str_item >> 16;

and use that, so we know what the hell that shift is for.

Quoted 9 lines
kernel/trace/trace_events_hist.c +3 -0
 	char *addr = (char *)(event + str_loc);
 
+	if (!(str_item >> 16))
+		return (u64)(unsigned long)"";
+
 	return (u64)(unsigned long)addr;
 }
 

kernel/trace/trace_events_hist.c static u64 hist_field_stack(struct hist_field *hist_field, · line 4392

Quoted 6 lines
kernel/trace/trace_events_hist.c +1 -0
@@ -4384,10 +4387,14 @@ static u64 hist_field_stack(struct hist_field *hist_field,
 			    struct ring_buffer_event *rbe,
 			    void *event)
 {
+	static const unsigned long empty_stack;
 	u32 str_item = *(u32 *)(event + hist_field->field->offset);
 	int str_loc = str_item & 0xffff;

Same here.

-- Steve

Quoted 7 lines
kernel/trace/trace_events_hist.c +3 -0
 	char *addr = (char *)(event + str_loc);
 
+	if ((str_item >> 16) < sizeof(unsigned long))
+		return (u64)(unsigned long)&empty_stack;
+
 	return (u64)(unsigned long)addr;
 }