public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 04/13] perf header: Sanity check HEADER_CPU_TOPOLOGY
Date: Fri, 10 Apr 2026 22:38:19 +0000	[thread overview]
Message-ID: <20260410223820.6CB18C19421@smtp.kernel.org> (raw)
In-Reply-To: <20260410220905.200051-5-acme@kernel.org>

Sashiko AI review found 2 potential issue(s):
- [High] Regression: Freeing `env->cpu` on error paths creates a state inconsistency leading to a guaranteed NULL pointer dereference.
- [High] Pre-existing: Heap out-of-bounds read and information leak due to missing null-termination guarantees in `do_read_string()`.
--

commit b6aa3d453035257912e7800f950abadb5a643531
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf header: Sanity check HEADER_CPU_TOPOLOGY

This commit adds validation to process_cpu_topology() to harden against
malformed perf.data files. It bounds checks sibling counts and changes bare
returns to goto statements that free the env->cpu structure.

> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c

[ ... ]

> @@ -2887,7 +2898,13 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
>  	env->sibling_cores = strbuf_detach(&sb, NULL);
>  
>  	if (do_read_u32(ff, &nr))
> -		return -1;
> +		goto free_cpu;
> +
> +	if (nr > (u32)cpu_nr) {
> +		pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_threads (%u) > nr_cpus_avail (%d)\n",
> +		       nr, cpu_nr);
> +		goto free_cpu;
> +	}

Does freeing env->cpu on error paths here create a state inconsistency?

If perf_session__read_header() ignores the return value of
perf_header__process_sections() and proceeds, env->nr_cpus_avail will still
be greater than 0, but env->cpu will be NULL because it was freed.

Later consumers like perf_env__get_socket_aggr_by_cpu() might blindly
dereference env->cpu based on nr_cpus_avail being greater than zero:

perf_env__get_socket_aggr_by_cpu()
    env->cpu[cpu.cpu].socket_id

Before this change, the partially initialized array was left intact and
safely zero-initialized, which avoided a null pointer dereference. 

The memory leak this attempts to address appears to be safely handled already,
as perf_env__exit() properly frees env->cpu during session cleanup. Could
these new error paths just return directly to avoid the crash?

A check of subsequent commits in this series indicates this resulting state
inconsistency remains unaddressed.

[ ... ]

> @@ -2936,7 +2953,13 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
>  		return 0;
>  
>  	if (do_read_u32(ff, &nr))
> -		return -1;
> +		goto free_cpu;
> +
> +	if (nr > (u32)cpu_nr) {
> +		pr_err("Invalid HEADER_CPU_TOPOLOGY: nr_sibling_dies (%u) > nr_cpus_avail (%d)\n",
> +		       nr, cpu_nr);
> +		goto free_cpu;
> +	}

This isn't a regression from this patch, but looking at the surrounding code
that processes these sibling strings:

str = do_read_string(ff);
if (!str)
    goto error;

if (strbuf_add(&sb, str, strlen(str) + 1) < 0)

Is it possible for do_read_string() to return a string without a null
terminator? 

If a malformed file provides a string without a null byte, could strlen()
read past the end of the heap allocation until it finds one, potentially
leaking heap data when the topology information is dumped?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260410220905.200051-1-acme@kernel.org?part=4

  reply	other threads:[~2026-04-10 22:38 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-10 22:08 [PATCHES perf-tools-next v2 00/13] Sanity check perf.data headers Arnaldo Carvalho de Melo
2026-04-10 22:08 ` [PATCH 01/13] perf header: Validate nr_domains when reading HEADER_CPU_DOMAIN_INFO Arnaldo Carvalho de Melo
2026-04-10 22:08 ` [PATCH 02/13] perf header: Bump up the max number of command line args allowed Arnaldo Carvalho de Melo
2026-04-10 22:34   ` sashiko-bot
2026-04-10 22:08 ` [PATCH 03/13] perf header: Sanity check HEADER_NRCPUS and HEADER_CPU_DOMAIN_INFO Arnaldo Carvalho de Melo
2026-04-10 22:45   ` sashiko-bot
2026-04-10 22:08 ` [PATCH 04/13] perf header: Sanity check HEADER_CPU_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:38   ` sashiko-bot [this message]
2026-04-10 22:08 ` [PATCH 05/13] perf header: Sanity check HEADER_NUMA_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:28   ` sashiko-bot
2026-04-10 22:08 ` [PATCH 06/13] perf header: Sanity check HEADER_MEM_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:32   ` sashiko-bot
2026-04-10 22:08 ` [PATCH 07/13] perf header: Sanity check HEADER_PMU_MAPPINGS Arnaldo Carvalho de Melo
2026-04-10 22:33   ` sashiko-bot
2026-04-10 22:09 ` [PATCH 08/13] perf header: Sanity check HEADER_GROUP_DESC Arnaldo Carvalho de Melo
2026-04-10 22:28   ` sashiko-bot
2026-04-10 22:09 ` [PATCH 09/13] perf header: Sanity check HEADER_CACHE Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 10/13] perf header: Sanity check HEADER_HYBRID_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 11/13] perf header: Sanity check HEADER_PMU_CAPS Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 12/13] perf header: Sanity check HEADER_BPF_PROG_INFO Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 13/13] perf header: Add sanity checks to HEADER_BPF_BTF processing Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2026-04-10  0:39 [PATCHES perf-tools-next v1 00/13] Sanity check perf.data headers Arnaldo Carvalho de Melo
2026-04-10  0:39 ` [PATCH 04/13] perf header: Sanity check HEADER_CPU_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10  1:14   ` sashiko-bot

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=20260410223820.6CB18C19421@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko@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