Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v2 0/2] tracing/synthetic: Harden dynamic string fault handling and length bounds
@ 2026-12-03 15:23 Lee Jones
  2026-12-03 15:23 ` [PATCH v2 1/2] tracing/synthetic: Handle dynamic string read faults Lee Jones
  2026-12-03 15:23 ` [PATCH v2 2/2] tracing/synthetic: Bound dynamic string copy to reserved length Lee Jones
  0 siblings, 2 replies; 3+ messages in thread
From: Lee Jones @ 2026-12-03 15:23 UTC (permalink / raw)
  To: lee
  Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu,
	Mathieu Desnoyers, Steven Rostedt, Tom Zanussi

This v2 series is based on trace/fixes (which already contains
"tracing/histograms: Reject non-string fields for synthetic string
fields" from v1 as commit c7868604fda5) and includes the pending
dynamic-string read-fault prerequisite so that both patches apply
cleanly together:

 1. tracing/synthetic: Handle dynamic string read faults (previously
    sent as <20261109155758.3330810-1-lee@kernel.org>)
 2. tracing/synthetic: Bound dynamic string copy to reserved length
    (reported by Sashiko)

v1 => v2:
 - Rebase onto trace/fixes and drop the already-applied histogram patch
 - Include the pending read-fault prerequisite as patch 1/2
 - Eliminate the duplicated ternary operators when passing dyn_lens[i]
   to trace_string()

Cc: linux-trace-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Tom Zanussi <tom.zanussi@linux.intel.com>

Lee Jones (2):
  tracing/synthetic: Handle dynamic string read faults
  tracing/synthetic: Bound dynamic string copy to reserved length

 kernel/trace/trace_events_synth.c | 63 +++++++++++++++++++++++--------
 1 file changed, 48 insertions(+), 15 deletions(-)

-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH v2 1/2] tracing/synthetic: Handle dynamic string read faults
  2026-12-03 15:23 [PATCH v2 0/2] tracing/synthetic: Harden dynamic string fault handling and length bounds Lee Jones
@ 2026-12-03 15:23 ` Lee Jones
  2026-12-03 15:23 ` [PATCH v2 2/2] tracing/synthetic: Bound dynamic string copy to reserved length Lee Jones
  1 sibling, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-12-03 15:23 UTC (permalink / raw)
  To: lee, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Tom Zanussi, linux-kernel, linux-trace-kernel
  Cc: stable

Commit 0934ae9977c2 ("tracing: Fix reading strings from synthetic
events") switched synthetic event dynamic strings to use
fetch_store_strlen() and fetch_store_string(), relying on commit
2e9906f84fc7 ("tracing: Add "(fault)" name injection to kernel probes")
to reserve and store FAULT_STRING when a string address faults.

When commit 4ed8f337dee3 ("Revert "tracing: Add "(fault)" name injection
to kernel probes"") and commit 797311bce5c2 ("tracing/probes: Fix to
record 0-length data_loc in fetch_store_string*() if fails") later
reverted FAULT_STRING injection in favour of recording a 0-length
data_loc on fault, synthetic events were left unchanged.

Consequently, a faulting kernel string address causes get_field_size()
to add a negative error code (-EFAULT) to fields_size, under-reserving
the trace buffer entry, while trace_string() casts the negative return
value to an unsigned length and passes it to fetch_store_string() and
write_synth_entry(), corrupting the ring buffer.

Skip non-positive lengths in get_field_size(), record a 0-length
dynamic field in trace_string() when fetching a dynamic string fails,
and print FAULT_STRING in print_synth_event() when a dynamic string has
zero length, matching trace_probe.

Fixes: 4ed8f337dee3 ("Revert "tracing: Add "(fault)" name injection to kernel probes"")
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---

v1 => v2:
 - Rebase cleanly onto trace/fixes and include ahead of patch 2/2
   (unchanged from <20261109155758.3330810-1-lee@kernel.org>)

 kernel/trace/trace_events_synth.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
index e2870212e58d..8492a5f285c4 100644
--- a/kernel/trace/trace_events_synth.c
+++ b/kernel/trace/trace_events_synth.c
@@ -376,7 +376,9 @@ 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 :
+						 FAULT_STRING,
 						 i == se->n_fields - 1 ? "" : " ");
 				n_u64++;
 			} else {
@@ -438,11 +440,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_strlen((unsigned long)str_val);
+		if (ret <= 0) {
+			data->as_dynamic.len = 0;
+			(*n_u64)++;
+			return 0;
+		}
+
+		data->as_dynamic.len = ret;
 
 		ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
+		if (ret > 0)
+			len = ret;
 
 		(*n_u64)++;
 	} else {
@@ -522,6 +533,8 @@ static __always_inline int get_field_size(struct synth_event *event,
 			len *= sizeof(unsigned long);
 		} else {
 			len = fetch_store_strlen((unsigned long)str_val);
+			if (len <= 0)
+				continue;
 		}
 
 		fields_size += len;
-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH v2 2/2] tracing/synthetic: Bound dynamic string copy to reserved length
  2026-12-03 15:23 [PATCH v2 0/2] tracing/synthetic: Harden dynamic string fault handling and length bounds Lee Jones
  2026-12-03 15:23 ` [PATCH v2 1/2] tracing/synthetic: Handle dynamic string read faults Lee Jones
@ 2026-12-03 15:23 ` Lee Jones
  1 sibling, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-12-03 15:23 UTC (permalink / raw)
  To: lee, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Tom Zanussi, linux-kernel, linux-trace-kernel
  Cc: stable

trace_event_raw_event_synth() and perf_event_raw_event_synth() measure
each dynamic string via get_field_size(), reserve a buffer entry of
that size, and then call write_synth_entry() -> trace_string() to copy
the strings into the entry.  Likewise, synth_event_trace() and
synth_event_trace_array() measure dynamic string lengths before
reserving the entry and calling trace_string().

Because trace_string() calls fetch_store_strlen() a second time and
writes the new length into data->as_dynamic.len before invoking
fetch_store_string(), a source string that grows concurrently between
the reservation and copy passes (for example, when another CPU updates
a shared histogram element string buffer) causes fetch_store_string()
to copy past the end of the reserved ring buffer or perf buffer entry.

Record each dynamic string's reserved length (measured once with
fetch_store_strlen() and capped at MAX_STRING_SIZE) in a per-field
array during the sizing pass, and pass that reserved length into
trace_string() so fetch_store_string() never writes beyond the space
reserved for the field.

Fixes: bd82631d7ccd ("tracing: Add support for dynamic strings to synthetic events")
Closes: https://sashiko.dev/#/bug/linux-99ac32b2-e9bc-4529-bfdc-62d0b5345672
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---

v1 => v2:
 - Pass dyn_lens[i] directly to trace_string() at all three call sites
   instead of using the ternary operator (if (!max_len) is already
   inside the if (is_dynamic) branch in trace_string(), so static
   strings never inspect max_len)

 kernel/trace/trace_events_synth.c | 54 +++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 17 deletions(-)

diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
index 8492a5f285c4..0283cace6752 100644
--- a/kernel/trace/trace_events_synth.c
+++ b/kernel/trace/trace_events_synth.c
@@ -426,11 +426,22 @@ static struct trace_event_functions synth_event_funcs = {
 	.trace		= print_synth_event
 };
 
+static unsigned int synth_str_len(const char *str_val)
+{
+	int len = fetch_store_strlen((unsigned long)str_val);
+
+	if (len <= 0)
+		return 0;
+
+	return min_t(unsigned int, len, MAX_STRING_SIZE);
+}
+
 static unsigned int trace_string(struct synth_trace_event *entry,
 				 struct synth_event *event,
 				 char *str_val,
 				 bool is_dynamic,
 				 unsigned int data_size,
+				 u16 max_len,
 				 unsigned int *n_u64)
 {
 	unsigned int len = 0;
@@ -442,14 +453,13 @@ static unsigned int trace_string(struct synth_trace_event *entry,
 
 		data->as_dynamic.offset = struct_size(entry, fields, event->n_u64) + data_size;
 
-		ret = fetch_store_strlen((unsigned long)str_val);
-		if (ret <= 0) {
+		if (!max_len) {
 			data->as_dynamic.len = 0;
 			(*n_u64)++;
 			return 0;
 		}
 
-		data->as_dynamic.len = ret;
+		data->as_dynamic.len = max_len;
 
 		ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
 		if (ret > 0)
@@ -512,7 +522,8 @@ static unsigned int trace_stack(struct synth_trace_event *entry,
 
 static __always_inline int get_field_size(struct synth_event *event,
 					  u64 *var_ref_vals,
-					  unsigned int *var_ref_idx)
+					  unsigned int *var_ref_idx,
+					  u16 *dyn_lens)
 {
 	int fields_size;
 
@@ -532,9 +543,8 @@ static __always_inline int get_field_size(struct synth_event *event,
 			len = *((unsigned long *)str_val) + 1;
 			len *= sizeof(unsigned long);
 		} else {
-			len = fetch_store_strlen((unsigned long)str_val);
-			if (len <= 0)
-				continue;
+			len = synth_str_len(str_val);
+			dyn_lens[field_pos] = len;
 		}
 
 		fields_size += len;
@@ -545,7 +555,8 @@ 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,
+					      const u16 *dyn_lens)
 {
 	int data_size = 0;
 	int i, n_u64;
@@ -559,7 +570,7 @@ static __always_inline void write_synth_entry(struct synth_event *event,
 
 			len = trace_string(entry, event, str_val,
 					   event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   data_size, dyn_lens[i], &n_u64);
 			data_size += len; /* only dynamic string increments */
 		} else if (event->fields[i]->is_stack) {
 			long *stack = (long *)(long)var_ref_vals[val_idx];
@@ -600,6 +611,7 @@ static void trace_event_raw_event_synth(void *__data,
 	struct trace_event_file *trace_file = __data;
 	struct synth_trace_event *entry;
 	struct trace_event_buffer fbuffer;
+	u16 dyn_lens[SYNTH_FIELDS_MAX] = {};
 	struct trace_buffer *buffer;
 	struct synth_event *event;
 	int fields_size;
@@ -609,7 +621,7 @@ static void trace_event_raw_event_synth(void *__data,
 	if (trace_trigger_soft_disabled(trace_file))
 		return;
 
-	fields_size = get_field_size(event, var_ref_vals, var_ref_idx);
+	fields_size = get_field_size(event, var_ref_vals, var_ref_idx, dyn_lens);
 
 	/*
 	 * Avoid ring buffer recursion detection, as this event
@@ -623,7 +635,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, dyn_lens);
 
 	trace_event_buffer_commit(&fbuffer);
 }
@@ -635,6 +647,7 @@ static void perf_event_raw_event_synth(void *__data,
 {
 	struct trace_event_call *call = __data;
 	struct synth_trace_event *entry;
+	u16 dyn_lens[SYNTH_FIELDS_MAX] = {};
 	struct hlist_head *perf_head;
 	struct synth_event *event;
 	struct pt_regs *regs;
@@ -649,7 +662,7 @@ static void perf_event_raw_event_synth(void *__data,
 	if (!perf_head || hlist_empty(perf_head))
 		return;
 
-	fields_size = get_field_size(event, var_ref_vals, var_ref_idx);
+	fields_size = get_field_size(event, var_ref_vals, var_ref_idx, dyn_lens);
 
 	size = ALIGN(sizeof(*entry) + fields_size, 8);
 
@@ -658,7 +671,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, dyn_lens);
 
 	perf_fetch_caller_regs(regs);
 
@@ -1834,6 +1847,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;
+	u16 dyn_lens[SYNTH_FIELDS_MAX] = {};
 	struct synth_event_trace_state state;
 	va_list args;
 	int ret;
@@ -1855,7 +1869,9 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 			    state.event->fields[i]->is_dynamic) {
 				char *str_val = (char *)(long)val;
 
-				data_size += strlen(str_val) + 1;
+				len = synth_str_len(str_val);
+				dyn_lens[i] = len;
+				data_size += len;
 			}
 		}
 
@@ -1884,7 +1900,7 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
 
 			len = trace_string(state.entry, state.event, str_val,
 					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   data_size, dyn_lens[i], &n_u64);
 			data_size += len; /* only dynamic string increments */
 		} else {
 			struct synth_field *field = state.event->fields[i];
@@ -1939,6 +1955,7 @@ 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;
+	u16 dyn_lens[SYNTH_FIELDS_MAX] = {};
 	struct synth_event_trace_state state;
 	char *str_val;
 	int ret;
@@ -1952,9 +1969,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_string)
+				continue;
 			field_pos = state.event->dynamic_fields[i]->field_pos;
 			str_val = (char *)(long)vals[field_pos];
-			len = strlen(str_val) + 1;
+			len = synth_str_len(str_val);
+			dyn_lens[field_pos] = len;
 			data_size += len;
 		}
 	}
@@ -1976,7 +1996,7 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 
 			len = trace_string(state.entry, state.event, str_val,
 					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   data_size, dyn_lens[i], &n_u64);
 			data_size += len; /* only dynamic string increments */
 		} else {
 			struct synth_field *field = state.event->fields[i];
-- 
2.56.1.911.g883d8bcb20-goog


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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 15:23 [PATCH v2 0/2] tracing/synthetic: Harden dynamic string fault handling and length bounds Lee Jones
2026-12-03 15:23 ` [PATCH v2 1/2] tracing/synthetic: Handle dynamic string read faults Lee Jones
2026-12-03 15:23 ` [PATCH v2 2/2] tracing/synthetic: Bound dynamic string copy to reserved length Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox