Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Dominik Maier" <dmnk@google.com>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] tracing/synthetic: bound dynamic string and stack stores to reserved size
Date: Thu, 26 Nov 2026 04:10:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-234724@kernel.org> (raw)
In-Reply-To: <20261126035553.406453-1-dmnk@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tracing/synthetic: unclamped stack frame count in
  trace_stack() causes OOB read
--

commit 1ad0830d62adf86896f0b684ccf1009777546151
Author: Dominik Maier <dmnk@google.com>

tracing/synthetic: bound dynamic string and stack stores to reserved size

This commit passes the remaining reserved dynamic buffer budget into
trace_string() and trace_stack() so that concurrent updates to histogram
variable buffers cannot write past the reserved ring-buffer or perf-buffer
entry.

> diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
> index 8492a5f285c4e..ccec67813e41f 100644
> --- a/kernel/trace/trace_events_synth.c
> +++ b/kernel/trace/trace_events_synth.c
[ ... ]
> @@ -479,6 +492,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];
> @@ -489,16 +503,13 @@ 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);

[Severity: Medium]
When len is clamped by rounddown(maxlen, sizeof(long)), or when len is 0,
can a histogram trigger keying on this synthetic event's stacktrace field
read past the end of the reserved ring-buffer entry?

If another CPU grows stack[] or an earlier dynamic string field between
get_field_size() and trace_stack(), memcpy() copies the truncated slice
with the original frame count in ((unsigned long *)data_loc)[0] (or leaves
data_loc unwritten at the end of the entry when len == 0).  A histogram
trigger on the synthetic event reads that field through hist_field_stack(),
which uses only the 16-bit offset and ignores data->as_dynamic.len:

kernel/trace/trace_events_hist.c:hist_field_stack() {
	u32 str_item = *(u32 *)(event + hist_field->field->offset);
	int str_loc = str_item & 0xffff;
	char *addr = (char *)(event + str_loc);

	return (u64)(unsigned long)addr;
}

kernel/trace/trace_events_hist.c:event_hist_trigger() {
    ...
			if (key_field->field) {
				unsigned long *stack, n_entries;

				field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
				stack = (unsigned long *)(long)field_contents;
				n_entries = *stack;
				memcpy(entries, ++stack, n_entries * sizeof(unsigned long));
			}
    ...
}

event_hist_trigger() then copies n_entries frames from ++stack, reading
past the reserved entry and, if *stack is uninitialized or concurrently
corrupted, overflowing hist_pad->entries[HIST_STACKTRACE_DEPTH].  Clamping
((unsigned long *)data_loc)[0] to (len / sizeof(long)) - 1 when len > 0
and checking the dynamic length in hist_field_stack() would prevent this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126035553.406453-1-dmnk@google.com?part=1

      reply	other threads:[~2026-11-26  4:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  3:55 [PATCH] tracing/synthetic: bound dynamic string and stack stores to reserved size Dominik Maier
2026-11-26  4:10 ` sashiko-bot [this message]

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=sashiko-outbox-234724@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmnk@google.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox