tracing/synthetic: bound dynamic string and stack stores to reserved size
PATCH v2 1/1 tracing/synthetic: bound dynamic string and stack stores to reserved size
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.
---
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/[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 | 81 +++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 27 deletions(-)
kernel/trace/trace_events_hist.c +9 -1
@@ -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 +55 -26
210 lines not on this page · raw message
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewThank 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() --
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 <[email protected]>
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.
Quoted 35 lines
kernel/trace/trace_events_synth.c +14
-1@@ -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);
kernel/trace/trace_events_synth.c static unsigned int trace_string(struct synth_trace_event *entry, · line 463
Quoted 10 lines
kernel/trace/trace_events_synth.c +3
-1data->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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Steven Rostedt
maintainerQuoted 10 lines · On Mon, 30 Nov 2026 16:22:30 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] tracing/synthetic: out-of-bounds read on empty dynstring inprint_synth_event()
--commit
fd194d819ad6073b2273ec9552d1b33827c2073f
Author: Dominik Maier <[email protected]>tracing/synthetic: bound dynamic string and stack stores to reserved size
FYI, the tracing subsystem has the subject start with a capital letter, and also doesn't split up sub-subsystems like synthetic:
tracing: Bound synthetic event dynamic string and stack stores to reserved size
Quoted 76 lines
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.
Quoted 35 lines
kernel/trace/trace_events_synth.c +14
-1@@ -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);kernel/trace/trace_events_synth.c static unsigned int trace_string(struct synth_trace_event *entry, · line 463
Quoted 10 lines
kernel/trace/trace_events_synth.c +3
-1data->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]
Canprint_synth_event()read out of bounds when formatting a dynamic string withdata->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 setsdata->as_dynamic.offsetto the end of the reserved entry anddata->as_dynamic.lento 0, andfetch_store_string()returns-ENOMEMwithout writing a NUL terminator. Unlikehist_field_dynstring(),print_synth_event()passes
(char *)entry +data->as_dynamic.offsetdirectly totrace_seq_printf()without checkingdata->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 ? "" : " "); ... }
Quoted 5 lines
Reading the trace output (or formatting via
tracepoint_printk) will then read past the end of the reserved ring buffer entry. Checkingdata->as_dynamic.leninprint_synth_event()and falling back to "" when it is 0 (matchinghist_field_dynstring()) would avoid this.
Yes, the string added must always be NULL terminated.
-- Steve