The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] tracing: Harden synthetic event string validation and copying
@ 2026-12-01 14:29 Lee Jones
  2026-12-01 14:29 ` [PATCH 1/2] tracing/histograms: Reject non-string fields for synthetic string fields Lee Jones
  2026-12-01 14:29 ` [PATCH 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-01 14:29 UTC (permalink / raw)
  To: lee
  Cc: linux-kernel, linux-trace-kernel, Masami Hiramatsu,
	Mathieu Desnoyers, Steven Rostedt, Tom Zanussi

These two follow-up patches harden synthetic event string handling on
top of the pending read-fault fix ("tracing/synthetic: Handle dynamic
string read faults"):

 1. Reject non-string histogram fields (scalars, constants, and raw
    FILTER_PTR_STRING pointers) when matching synthetic string fields
    in check_synth_field(), mirroring the stacktrace validation fix in
    commit a244c241593b ("tracing/histograms: Reject non-stacktrace
    fields for synthetic stacktrace fields").  Note that syscall
    FILTER_PTR_STRING fields (such as sys_enter_openat's filename) are
    intentionally rejected here because user-controlled pointers can
    carry arbitrary kernel addresses into fetch_store_string(); passing
    them as u64 and reading them with a :ustring eprobe remains
    unaffected.

 2. Eliminate the TOCTOU ring-buffer and perf-buffer overflow in
    trace_string() (reported by Sashiko) by measuring each dynamic
    string once during the sizing pass and bounding fetch_store_string()
    to the per-field reserved length.

Patch 1 applies independently to trace/for-next; patch 2 is based on
"[RESEND] tracing/synthetic: Handle dynamic string read faults".

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/histograms: Reject non-string fields for synthetic string
    fields
  tracing/synthetic: Bound dynamic string copy to reserved length

 kernel/trace/trace_events_hist.c  | 11 ++++--
 kernel/trace/trace_events_synth.c | 66 +++++++++++++++++++++----------
 2 files changed, 53 insertions(+), 24 deletions(-)

-- 
2.56.1.839.g22a8290db9-goog

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

* [PATCH 1/2] tracing/histograms: Reject non-string fields for synthetic string fields
  2026-12-01 14:29 [PATCH 0/2] tracing: Harden synthetic event string validation and copying Lee Jones
@ 2026-12-01 14:29 ` Lee Jones
  2026-12-01 14:29 ` [PATCH 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-01 14:29 UTC (permalink / raw)
  To: lee, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Tom Zanussi, linux-kernel, linux-trace-kernel
  Cc: stable

In check_synth_field(), when a synthetic event field is a string
(field->is_string) and the histogram field does not match the "char["
dynamic string shortcut, validation falls through to the generic size
comparison (which also skips signedness checks for string fields).

Because a dynamic string synthetic field ("__data_loc char[]") has a
size of sizeof(u64), any 8-byte scalar histogram field, constant, or
pointer field (including FILTER_PTR_STRING fields that
create_hist_field() intentionally excludes from HIST_FIELD_FL_STRING)
passes the fallback check.  Its scalar value is then dereferenced as a
string pointer in get_field_size() and trace_string().  Likewise, an
8-byte scalar can match a char[8] static string synthetic field, or a
char[8] static string field can match an 8-byte scalar synthetic field
and leak a kernel pointer address.

Propagate HIST_FIELD_FL_STRING to variable references and field
variables (matching HIST_FIELD_FL_STACKTRACE, with variable aliases
already excluded from per-element string capture), and reject any
mismatch between HIST_FIELD_FL_STRING and field->is_string in
check_synth_field().

Fixes: b05e89ae7cf3 ("tracing: Accept different type for synthetic event fields")
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 kernel/trace/trace_events_hist.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d4702ad4e003..7f5ec9ff19e5 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -2138,7 +2138,7 @@ static int init_var_ref(struct hist_field *ref_field,
 	ref_field->is_signed = var_field->is_signed;
 	ref_field->flags |= var_field->flags &
 		(HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS |
-		 HIST_FIELD_FL_STACKTRACE);
+		 HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE);
 
 	if (system) {
 		ref_field->system = kstrdup(system, GFP_KERNEL);
@@ -3331,7 +3331,8 @@ static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
 		ret = PTR_ERR(var);
 		goto err;
 	}
-	var->flags |= val->flags & HIST_FIELD_FL_STACKTRACE;
+	var->flags |= val->flags &
+		(HIST_FIELD_FL_STRING | HIST_FIELD_FL_STACKTRACE);
 
 	field_var = kzalloc_obj(struct field_var);
 	if (!field_var) {
@@ -3951,13 +3952,15 @@ static int check_synth_field(struct synth_event *event,
 
 	field = event->fields[field_pos];
 
+	if (!!(hist_field->flags & HIST_FIELD_FL_STRING) != field->is_string)
+		return -EINVAL;
+
 	/*
 	 * A dynamic string synth field can accept static or
 	 * dynamic. A static string synth field can only accept a
 	 * same-sized static string, which is checked for later.
 	 */
-	if (strstr(hist_field->type, "char[") && field->is_string
-	    && field->is_dynamic)
+	if (field->is_string && field->is_dynamic)
 		return 0;
 
 	if (!!(hist_field->flags & HIST_FIELD_FL_STACKTRACE) != field->is_stack)
-- 
2.56.1.839.g22a8290db9-goog


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

* [PATCH 2/2] tracing/synthetic: Bound dynamic string copy to reserved length
  2026-12-01 14:29 [PATCH 0/2] tracing: Harden synthetic event string validation and copying Lee Jones
  2026-12-01 14:29 ` [PATCH 1/2] tracing/histograms: Reject non-string fields for synthetic string fields Lee Jones
@ 2026-12-01 14:29 ` Lee Jones
  1 sibling, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-12-01 14:29 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>
---
 kernel/trace/trace_events_synth.c | 66 +++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 20 deletions(-)

diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
index 8492a5f285c4..bdbfcecae6fa 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;
@@ -556,10 +567,12 @@ static __always_inline void write_synth_entry(struct synth_event *event,
 		val_idx = var_ref_idx[i];
 		if (event->fields[i]->is_string) {
 			char *str_val = (char *)(long)var_ref_vals[val_idx];
+			bool is_dynamic = event->fields[i]->is_dynamic;
 
 			len = trace_string(entry, event, str_val,
-					   event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   is_dynamic, data_size,
+					   is_dynamic ? dyn_lens[i] : 0,
+					   &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 +613,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 +623,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 +637,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 +649,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 +664,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 +673,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 +1849,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 +1871,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;
 			}
 		}
 
@@ -1881,10 +1899,12 @@ 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;
+			bool is_dynamic = state.event->fields[i]->is_dynamic;
 
 			len = trace_string(state.entry, state.event, str_val,
-					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   is_dynamic, data_size,
+					   is_dynamic ? dyn_lens[i] : 0,
+					   &n_u64);
 			data_size += len; /* only dynamic string increments */
 		} else {
 			struct synth_field *field = state.event->fields[i];
@@ -1939,6 +1959,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 +1973,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;
 		}
 	}
@@ -1973,10 +1997,12 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
 	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];
+			bool is_dynamic = state.event->fields[i]->is_dynamic;
 
 			len = trace_string(state.entry, state.event, str_val,
-					   state.event->fields[i]->is_dynamic,
-					   data_size, &n_u64);
+					   is_dynamic, data_size,
+					   is_dynamic ? dyn_lens[i] : 0,
+					   &n_u64);
 			data_size += len; /* only dynamic string increments */
 		} else {
 			struct synth_field *field = state.event->fields[i];
-- 
2.56.1.839.g22a8290db9-goog


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

end of thread, other threads:[~2026-12-01 14:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 14:29 [PATCH 0/2] tracing: Harden synthetic event string validation and copying Lee Jones
2026-12-01 14:29 ` [PATCH 1/2] tracing/histograms: Reject non-string fields for synthetic string fields Lee Jones
2026-12-01 14:29 ` [PATCH 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