* [PATCH v2] tracing/synthetic: bound dynamic string and stack stores to reserved size
@ 2026-11-30 16:04 Dominik Maier
2026-11-30 16:22 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-30 16:04 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
Cc: Tom Zanussi, linux-trace-kernel, dmnk+artist
Synthetic events can write past their reserved ring buffer space when a
dynamic string or stack trace grows between sizing and copying.
Histogram string and stack variables are updated across CPUs without
per-element locking, so re-measuring the source while writing the entry
can copy more bytes than were reserved. Downstream histograms on the
event can also dereference zero-length dynamic fields or read past the
maximum stack depth.
Pass the remaining reserved byte budget into the string and stack copy
helpers so writes never exceed the entry, store the copied frame count,
and guard empty dynamic fields and stack depth in histogram 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>
---
v2:
- Store copied frame count at data_loc[0] in trace_stack(), return empty
fallbacks for 0-length dynamic fields in hist_field_dynstring() and
hist_field_stack(), and clamp n_entries in event_hist_trigger() (Sashiko).
- Base directly on v7.4-rc5 without external patch prerequisites and drop
unrelated goto churn in synth_event_trace{,_array}().
- 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 | 81 +++++++++++++++++++++----------
2 files changed, 64 insertions(+), 27 deletions(-)
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d4702ad4e003..a3a093d36f85 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 e2870212e58d..4989b5947366 100644
--- a/kernel/trace/trace_events_synth.c
+++ b/kernel/trace/trace_events_synth.c
@@ -424,11 +424,25 @@ 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 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;
@@ -438,11 +452,12 @@ 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;
+ data->as_dynamic.len = min_t(unsigned int, maxlen, MAX_STRING_SIZE);
ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
+ if (ret > 0)
+ len = ret;
(*n_u64)++;
} else {
@@ -468,6 +483,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 +494,15 @@ 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) {
+ memcpy(data_loc, stack, len);
+ *(unsigned long *)data_loc = len / sizeof(long) - 1;
+ }
/* Fill in the field that holds the offset/len combo */
@@ -518,10 +533,11 @@ static __always_inline int get_field_size(struct synth_event *event,
if (event->dynamic_fields[i]->is_stack) {
/* reserve one extra element for size */
- len = *((unsigned long *)str_val) + 1;
- len *= sizeof(unsigned long);
+ len = synth_stack_size((const long *)str_val);
} else {
len = fetch_store_strlen((unsigned long)str_val);
+ if (len < 0)
+ len = 0;
}
fields_size += len;
@@ -532,12 +548,15 @@ 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 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];
@@ -546,14 +565,16 @@ 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, maxlen, &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);
+ data_size, maxlen, &n_u64);
data_size += len;
+ maxlen -= len;
} else {
struct synth_field *field = event->fields[i];
u64 val = var_ref_vals[val_idx];
@@ -610,7 +631,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 +666,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);
@@ -1820,7 +1841,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, data_size = 0;
struct synth_event_trace_state state;
va_list args;
int ret;
@@ -1840,9 +1861,10 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
if (state.event->fields[i]->is_string &&
state.event->fields[i]->is_dynamic) {
- char *str_val = (char *)(long)val;
+ int slen = fetch_store_strlen((unsigned long)val);
- data_size += strlen(str_val) + 1;
+ if (slen > 0)
+ data_size += slen;
}
}
@@ -1858,6 +1880,7 @@ int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
goto out;
}
+ maxlen = data_size;
data_size = 0;
va_start(args, n_vals);
@@ -1871,8 +1894,9 @@ 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, maxlen, &n_u64);
data_size += len; /* only dynamic string increments */
+ maxlen -= len;
} else {
struct synth_field *field = state.event->fields[i];
@@ -1925,7 +1949,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, data_size = 0;
struct synth_event_trace_state state;
char *str_val;
int ret;
@@ -1939,10 +1963,13 @@ 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++) {
+ int slen;
+
field_pos = state.event->dynamic_fields[i]->field_pos;
str_val = (char *)(long)vals[field_pos];
- len = strlen(str_val) + 1;
- data_size += len;
+ slen = fetch_store_strlen((unsigned long)str_val);
+ if (slen > 0)
+ data_size += slen;
}
}
@@ -1955,6 +1982,7 @@ int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
goto out;
}
+ maxlen = data_size;
data_size = 0;
for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
@@ -1963,8 +1991,9 @@ 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, maxlen, &n_u64);
data_size += len; /* only dynamic string increments */
+ maxlen -= len;
} else {
struct synth_field *field = state.event->fields[i];
u64 val = vals[i];
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] tracing/synthetic: bound dynamic string and stack stores to reserved size
2026-11-30 16:04 [PATCH v2] tracing/synthetic: bound dynamic string and stack stores to reserved size Dominik Maier
@ 2026-11-30 16:22 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 16:22 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/synthetic: out-of-bounds read on empty dynstring in
print_synth_event()
--
commit fd194d819ad6073b2273ec9552d1b33827c2073f
Author: Dominik Maier <dmnk@google.com>
tracing/synthetic: bound dynamic string and stack stores to reserved size
This commit bounds synthetic event dynamic string and stack trace stores
to the remaining reserved ring buffer budget and adds length and depth
guards to downstream histogram readers.
> diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
> index e2870212e58d4..4989b59473667 100644
> --- a/kernel/trace/trace_events_synth.c
> +++ b/kernel/trace/trace_events_synth.c
> @@ -424,11 +424,25 @@ 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 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;
> @@ -438,11 +452,12 @@ 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;
> + data->as_dynamic.len = min_t(unsigned int, maxlen, MAX_STRING_SIZE);
>
> ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
> + if (ret > 0)
> + len = ret;
>
> (*n_u64)++;
> } else {
[Severity: Medium]
Can print_synth_event() read out of bounds when formatting a dynamic
string with data->as_dynamic.len == 0?
If an earlier dynamic field grows between sizing and copying and consumes
the reserved budget, maxlen becomes 0 for a subsequent dynamic string.
trace_string() then sets data->as_dynamic.offset to the end of the
reserved entry and data->as_dynamic.len to 0, and fetch_store_string()
returns -ENOMEM without writing a NUL terminator. Unlike
hist_field_dynstring(), print_synth_event() passes
(char *)entry + data->as_dynamic.offset directly to trace_seq_printf()
without checking data->as_dynamic.len:
kernel/trace/trace_events_synth.c:print_synth_event() {
...
if (se->fields[i]->is_string) {
if (se->fields[i]->is_dynamic) {
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,
i == se->n_fields - 1 ? "" : " ");
...
}
Reading the trace output (or formatting via tracepoint_printk) will then
read past the end of the reserved ring buffer entry. Checking
data->as_dynamic.len in print_synth_event() and falling back to "" when it
is 0 (matching hist_field_dynstring()) would avoid this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130160412.2637358-1-dmnk@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 16:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:04 [PATCH v2] tracing/synthetic: bound dynamic string and stack stores to reserved size Dominik Maier
2026-11-30 16:22 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox