All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] tracing: Bound synthetic event dynamic string and stack stores to reserved size
@ 2026-12-03  2:04 Dominik Maier
  2026-12-03  2:24 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-12-03  2:04 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
  Cc: Tom Zanussi, Lee Jones, linux-trace-kernel, dmnk+artist

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")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
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/20261130160412.2637358-1-dmnk@google.com/
- v1: https://lore.kernel.org/linux-trace-kernel/20261126035553.406453-1-dmnk@google.com/

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(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d4702ad..a3a093d 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -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,
diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
index e287021..ba9ee84 100644
--- a/kernel/trace/trace_events_synth.c
+++ b/kernel/trace/trace_events_synth.c
@@ -376,7 +376,8 @@ static enum print_line_t print_synth_event(struct trace_iterator *iter,
 				union trace_synth_field *data = &entry->fields[n_u64];
 
 				trace_seq_printf(s, print_fmt, se->fields[i]->name,
-						 (char *)entry + data->as_dynamic.offset,
+						 data->as_dynamic.len ?
+						 (char *)entry + data->as_dynamic.offset : "",
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64++;
 			} else {
@@ -424,11 +425,31 @@ static struct trace_event_functions synth_event_funcs = {
 	.trace		= print_synth_event
 };
 
+static __always_inline unsigned int synth_stack_size(const long *stack)
+{
+	unsigned long n_entries;
+
+	if (!stack)
+		return 0;
+
+	n_entries = min_t(unsigned long,
+			  READ_ONCE(*(const unsigned long *)stack),
+			  HIST_STACKTRACE_DEPTH);
+	return (n_entries + 1) * sizeof(unsigned long);
+}
+
+static __always_inline unsigned int synth_dyn_budget(unsigned int maxlen,
+						     unsigned int rem_strings)
+{
+	return maxlen > rem_strings ? maxlen - rem_strings : 0;
+}
+
 static unsigned int trace_string(struct synth_trace_event *entry,
 				 struct synth_event *event,
 				 char *str_val,
 				 bool is_dynamic,
 				 unsigned int data_size,
+				 unsigned int maxlen,
 				 unsigned int *n_u64)
 {
 	unsigned int len = 0;
@@ -437,12 +458,20 @@ static unsigned int trace_string(struct synth_trace_event *entry,
 
 	if (is_dynamic) {
 		union trace_synth_field *data = &entry->fields[*n_u64];
-
-		len = fetch_store_strlen((unsigned long)str_val);
-		data->as_dynamic.offset = struct_size(entry, fields, event->n_u64) + data_size;
-		data->as_dynamic.len = len;
-
-		ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
+		unsigned int offset;
+
+		offset = struct_size(entry, fields, event->n_u64) + data_size;
+		data->as_dynamic.offset = offset;
+		data->as_dynamic.len = min_t(unsigned int, maxlen, MAX_STRING_SIZE);
+
+		ret = fetch_store_string((unsigned long)str_val, data, entry);
+		if (ret > 0) {
+			len = ret;
+		} else if (maxlen) {
+			*((char *)entry + offset) = '\0';
+			data->as_dynamic.len = 1;
+			len = 1;
+		}
 
 		(*n_u64)++;
 	} else {
@@ -468,6 +497,7 @@ static unsigned int trace_stack(struct synth_trace_event *entry,
 				 struct synth_event *event,
 				 long *stack,
 				 unsigned int data_size,
+				 unsigned int maxlen,
 				 unsigned int *n_u64)
 {
 	union trace_synth_field *data = &entry->fields[*n_u64];
@@ -478,16 +508,18 @@ static unsigned int trace_stack(struct synth_trace_event *entry,
 	data_offset = struct_size(entry, fields, event->n_u64);
 	data_offset += data_size;
 
-	for (len = 0; len < HIST_STACKTRACE_DEPTH; len++) {
-		if (!stack[len])
-			break;
-	}
-
-	len *= sizeof(long);
+	len = min_t(unsigned int, synth_stack_size(stack),
+		    rounddown(maxlen, sizeof(long)));
 
 	/* Find the dynamic section to copy the stack into. */
 	data_loc = (void *)entry + data_offset;
-	memcpy(data_loc, stack, len);
+	if (len) {
+		unsigned long n_entries = len / sizeof(long) - 1;
+
+		memcpy(data_loc, &n_entries, sizeof(n_entries));
+		memcpy(data_loc + sizeof(n_entries), stack + 1,
+		       len - sizeof(n_entries));
+	}
 
 	/* Fill in the field that holds the offset/len combo */
 
@@ -516,13 +548,10 @@ static __always_inline int get_field_size(struct synth_event *event,
 		val_idx = var_ref_idx[field_pos];
 		str_val = (char *)(long)var_ref_vals[val_idx];
 
-		if (event->dynamic_fields[i]->is_stack) {
-			/* reserve one extra element for size */
-			len = *((unsigned long *)str_val) + 1;
-			len *= sizeof(unsigned long);
-		} else {
-			len = fetch_store_strlen((unsigned long)str_val);
-		}
+		if (event->dynamic_fields[i]->is_stack)
+			len = synth_stack_size((const long *)str_val);
+		else
+			len = max(fetch_store_strlen((unsigned long)str_val), 1);
 
 		fields_size += len;
 	}
@@ -532,28 +561,39 @@ static __always_inline int get_field_size(struct synth_event *event,
 static __always_inline void write_synth_entry(struct synth_event *event,
 					      struct synth_trace_event *entry,
 					      u64 *var_ref_vals,
-					      unsigned int *var_ref_idx)
+					      unsigned int *var_ref_idx,
+					      unsigned int fields_size)
 {
-	int data_size = 0;
+	unsigned int fixed_size = event->n_u64 * sizeof(u64);
+	unsigned int maxlen = fields_size > fixed_size ? fields_size - fixed_size : 0;
+	unsigned int rem_strings = event->n_dynamic_strings;
+	unsigned int data_size = 0;
+	unsigned int len;
 	int i, n_u64;
 	int val_idx;
-	int len;
 
 	for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
 		val_idx = var_ref_idx[i];
 		if (event->fields[i]->is_string) {
 			char *str_val = (char *)(long)var_ref_vals[val_idx];
+			unsigned int str_max = 0;
+
+			if (event->fields[i]->is_dynamic)
+				str_max = synth_dyn_budget(maxlen, --rem_strings);
 
 			len = trace_string(entry, event, str_val,
 					   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 if (event->fields[i]->is_stack) {
 			long *stack = (long *)(long)var_ref_vals[val_idx];
 
-			len = trace_stack(entry, event, stack,
-					   data_size, &n_u64);
+			len = trace_stack(entry, event, stack, data_size,
+					  synth_dyn_budget(maxlen, rem_strings),
+					  &n_u64);
 			data_size += len;
+			maxlen -= len;
 		} else {
 			struct synth_field *field = event->fields[i];
 			u64 val = var_ref_vals[val_idx];
@@ -610,7 +650,7 @@ static void trace_event_raw_event_synth(void *__data,
 	if (!entry)
 		return;
 
-	write_synth_entry(event, entry, var_ref_vals, var_ref_idx);
+	write_synth_entry(event, entry, var_ref_vals, var_ref_idx, fields_size);
 
 	trace_event_buffer_commit(&fbuffer);
 }
@@ -645,7 +685,7 @@ static void perf_event_raw_event_synth(void *__data,
 	if (unlikely(!entry))
 		return;
 
-	write_synth_entry(event, entry, var_ref_vals, var_ref_idx);
+	write_synth_entry(event, entry, var_ref_vals, var_ref_idx, fields_size);
 
 	perf_fetch_caller_regs(regs);
 
@@ -1072,9 +1112,13 @@ static struct synth_event *alloc_synth_event(const char *name, int n_fields,
 		goto out;
 	}
 
-	for (i = 0; i < n_fields; i++)
-		if (fields[i]->is_dynamic)
+	for (i = 0; i < n_fields; i++) {
+		if (fields[i]->is_dynamic) {
 			n_dynamic_fields++;
+			if (fields[i]->is_string)
+				event->n_dynamic_strings++;
+		}
+	}
 
 	if (n_dynamic_fields) {
 		event->dynamic_fields = kzalloc_objs(*event->dynamic_fields,
@@ -1820,7 +1864,7 @@ __synth_event_trace_end(struct synth_event_trace_state *trace_state)
  */
 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 {
-	unsigned int i, n_u64, len, data_size = 0;
+	unsigned int i, n_u64, len, maxlen, rem_strings, data_size = 0;
 	struct synth_event_trace_state state;
 	va_list args;
 	int ret;
@@ -1839,11 +1883,8 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 			u64 val = va_arg(args, u64);
 
 			if (state.event->fields[i]->is_string &&
-			    state.event->fields[i]->is_dynamic) {
-				char *str_val = (char *)(long)val;
-
-				data_size += strlen(str_val) + 1;
-			}
+			    state.event->fields[i]->is_dynamic)
+				data_size += max(fetch_store_strlen((unsigned long)val), 1);
 		}
 
 		va_end(args);
@@ -1858,6 +1899,8 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 		goto out;
 	}
 
+	maxlen = data_size;
+	rem_strings = state.event->n_dynamic_strings;
 	data_size = 0;
 
 	va_start(args, n_vals);
@@ -1868,11 +1911,16 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 
 		if (state.event->fields[i]->is_string) {
 			char *str_val = (char *)(long)val;
+			unsigned int str_max = 0;
+
+			if (state.event->fields[i]->is_dynamic)
+				str_max = synth_dyn_budget(maxlen, --rem_strings);
 
 			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];
 
@@ -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);
 
 			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];
diff --git a/kernel/trace/trace_synth.h b/kernel/trace/trace_synth.h
index d4ebf01..bf40742 100644
--- a/kernel/trace/trace_synth.h
+++ b/kernel/trace/trace_synth.h
@@ -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
-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] tracing: Bound synthetic event dynamic string and stack stores to reserved size
  2026-12-03  2:04 [PATCH v3] tracing: Bound synthetic event dynamic string and stack stores to reserved size Dominik Maier
@ 2026-12-03  2:24 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03  2:24 UTC (permalink / raw)
  To: Dominik Maier; +Cc: linux-trace-kernel

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 <dmnk@google.com>

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.

> diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
> index e2870212e58d4..ba9ee84cba036 100644
> --- a/kernel/trace/trace_events_synth.c
> +++ b/kernel/trace/trace_events_synth.c

[ ... ]

> @@ -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);
>  
>  			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()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203020420.3573008-1-dmnk@google.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  2:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  2:04 [PATCH v3] tracing: Bound synthetic event dynamic string and stack stores to reserved size Dominik Maier
2026-12-03  2:24 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.