From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Tom Zanussi <tom.zanussi@linux.intel.com>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 2/2] tracing/synthetic: Bound dynamic string copy to reserved length
Date: Tue, 1 Dec 2026 14:29:07 +0000 [thread overview]
Message-ID: <20261201142918.1849179-3-lee@kernel.org> (raw)
In-Reply-To: <20261201142918.1849179-1-lee@kernel.org>
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
next prev parent reply other threads:[~2026-12-01 14:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-01 22:39 ` [PATCH 2/2] tracing/synthetic: Bound dynamic string copy to reserved length Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201142918.1849179-3-lee@kernel.org \
--to=lee@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.org \
--cc=stable@vger.kernel.org \
--cc=tom.zanussi@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.