Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Amir Ayupov <aaupov@fb.com>, <linux-perf-users@vger.kernel.org>,
	<coresight@lists.linaro.org>,
	<linux-arm-kernel@lists.infradead.org>,
	"Suzuki K Poulose" <suzuki.poulose@arm.com>,
	James Clark <james.clark@linaro.org>, "Leo Yan" <leo.yan@arm.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	John Garry <john.g.garry@oracle.com>,
	"Will Deacon" <will@kernel.org>
Cc: <linux-doc@vger.kernel.org>, Mike Leach <mike.leach@arm.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	"Swapnil Sapkal" <swapnil.sapkal@amd.com>
Subject: Re: [PATCH 3/9] perf thread-stack: Bound wrapped branch stack copy
Date: Tue, 11 Aug 2026 18:31:22 +0300	[thread overview]
Message-ID: <b1547fe7-255d-43e4-8b0d-34318c331118@intel.com> (raw)
In-Reply-To: <20260803090640.2412336-3-aaupov@fb.com>

On 03/08/2026 12:06, Amir Ayupov wrote:
> When the internal branch ring has wrapped, thread_stack__br_sample()
> computes the number of entries that still fit in the destination:
> 
> 	nr = min(ts->br_stack_pos, sz);
> 
> but then copies ts->br_stack_pos entries regardless, overrunning the
> destination whenever sz is smaller than ts->br_stack_pos.
> 
> No caller can trigger this today: both intel-pt and cs-etm size the
> thread stack ring and the output buffer from the same
> synth_opts.last_branch_sz, so sz is never less than ts->br_stack_sz and
> the two values always agree. It becomes reachable as soon as a caller
> keeps a larger reconstruction ring than the requested output depth,
> which is what --itrace=L does for late branch sampling.
> 
> Copy nr entries instead, so the destination bound is honoured whatever
> the caller asks for.
> 
> Signed-off-by: Amir Ayupov <aaupov@fb.com>
> ---
>  tools/perf/util/thread-stack.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
> index 1a3dffa83bde2..51eaedb47bb1d 100644
> --- a/tools/perf/util/thread-stack.c
> +++ b/tools/perf/util/thread-stack.c
> @@ -643,7 +643,7 @@ void thread_stack__br_sample(struct thread *thread, int cpu,
>  		sz -= nr;
>  		be = &dst->entries[nr];
>  		nr = min(ts->br_stack_pos, sz);
> -		memcpy(be, &src->entries[0], bsz * ts->br_stack_pos);
> +		memcpy(be, &src->entries[0], bsz * nr);
>  	}
>  }
>  

Has since been done:

commit ab9c84d1cd59e6b3b73de34982a35a76e3a9b032
Author: Arnaldo Carvalho de Melo <acme@redhat.com>
Date:   Mon Jul 27 13:17:01 2026 -0300

    perf thread-stack: Fix heap buffer overflow on branch stack wrap copy
    
    thread_stack__br_sample() copies the wrap-around portion of the branch
    stack ring buffer with:
    
      nr = min(ts->br_stack_pos, sz);
      memcpy(be, &src->entries[0], bsz * ts->br_stack_pos);
    
    'nr' is correctly bounded to min(br_stack_pos, sz) but the memcpy uses
    the unbounded ts->br_stack_pos directly.  When br_stack_pos exceeds
    the remaining destination space 'sz', this writes past the destination
    buffer.
    
    Use 'nr' (the bounded value) in the memcpy size, matching the pattern
    of the first memcpy in the same function.
    
    Fixes: 86d67180b920 ("perf thread-stack: Add branch stack support")
    Reported-by: sashiko-bot <sashiko-bot@kernel.org>
    Assisted-by: Claude:claude-opus-4.6
    Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
    Reviewed-by: James Clark <james.clark@linaro.org>
    Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
    Signed-off-by: Namhyung Kim <namhyung@kernel.org>

diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
index c5ce741b0744..1360f44421ef 100644
--- a/tools/perf/util/thread-stack.c
+++ b/tools/perf/util/thread-stack.c
@@ -642,7 +642,7 @@ void thread_stack__br_sample(struct thread *thread, int cpu,
                sz -= nr;
                be = &dst->entries[nr];
                nr = min(ts->br_stack_pos, sz);
-               memcpy(be, &src->entries[0], bsz * ts->br_stack_pos);
+               memcpy(be, &src->entries[0], bsz * nr);
        }
 }



  reply	other threads:[~2026-08-11 15:31 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03  9:06 [PATCH 1/9] perf header: Tolerate inconsistent HEADER_GROUP_DESC Amir Ayupov
2026-08-03  9:06 ` [PATCH 2/9] perf thread-stack: Report branch stack hw_idx as not available Amir Ayupov
2026-08-11 15:58   ` Adrian Hunter
2026-08-12 15:48     ` Adrian Hunter
2026-08-12 16:04       ` Arnaldo Carvalho de Melo
2026-08-12 13:16   ` James Clark
2026-08-03  9:06 ` [PATCH 3/9] perf thread-stack: Bound wrapped branch stack copy Amir Ayupov
2026-08-11 15:31   ` Adrian Hunter [this message]
2026-08-03  9:06 ` [PATCH 4/9] perf dlfilter: Add non-empty branch stack filter Amir Ayupov
2026-08-03  9:06 ` [PATCH 5/9] perf cs-etm: Split up cs_etm__process_timestamped_queues() Amir Ayupov
2026-08-03  9:06 ` [PATCH 6/9] perf cs-etm: Add branch history to existing samples Amir Ayupov
2026-08-03  9:06 ` [PATCH 7/9] perf test cs-etm: Test branch history on " Amir Ayupov
2026-08-12 14:13   ` James Clark
2026-08-12 14:15   ` James Clark
2026-08-03  9:06 ` [PATCH 8/9] perf cs-etm: Consume branch history when attaching it to a sample Amir Ayupov
2026-08-03  9:06 ` [PATCH 9/9] Documentation: coresight: Document context-sensitive PGO workflow Amir Ayupov
2026-08-11 14:38 ` [PATCH 1/9] perf header: Tolerate inconsistent HEADER_GROUP_DESC Adrian Hunter

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=b1547fe7-255d-43e4-8b0d-34318c331118@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=aaupov@fb.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=corbet@lwn.net \
    --cc=coresight@lists.linaro.org \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=john.g.garry@oracle.com \
    --cc=jolsa@kernel.org \
    --cc=leo.yan@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mike.leach@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=suzuki.poulose@arm.com \
    --cc=swapnil.sapkal@amd.com \
    --cc=will@kernel.org \
    /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