Re: [PATCH 03/11] perf tools: Use perf_env__get_cpu_topology() in machine__resolve()
From: Ian Rogers
Date: Mon Jun 08 2026 - 18:01:27 EST
On Mon, Jun 8, 2026 at 1:18 PM Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
>
> machine__resolve() accesses env->cpu[al->cpu].socket_id after checking
> al->cpu >= 0 and env->cpu != NULL, but without validating al->cpu
> against env->nr_cpus_avail. Since al->cpu comes from the untrusted
> perf.data sample, a crafted file with a large CPU index causes an
> out-of-bounds heap read.
>
> Use perf_env__get_cpu_topology() which validates both NULL and bounds.
>
> Fixes: 0c4c4debb0adda4c ("perf tools: Add processor socket info to hist_entry and addr_location")
> Reported-by: sashiko-bot <sashiko-bot@xxxxxxxxxx>
> Cc: Kan Liang <kan.liang@xxxxxxxxx>
> Cc: Ian Rogers <irogers@xxxxxxxxxx>
> Assisted-by: Claude Opus 4.6 <noreply@xxxxxxxxxxxxx>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> ---
> tools/perf/util/event.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
> index 66f4843bb235df53..001db00be1073ad4 100644
> --- a/tools/perf/util/event.c
> +++ b/tools/perf/util/event.c
> @@ -14,6 +14,7 @@
> #include <linux/perf_event.h>
> #include "cpumap.h"
> #include "dso.h"
> +#include "env.h"
> #include "event.h"
> #include "debug.h"
> #include "hist.h"
> @@ -836,8 +837,14 @@ int machine__resolve(struct machine *machine, struct addr_location *al,
> if (al->cpu >= 0) {
> struct perf_env *env = machine->env;
>
> - if (env && env->cpu)
> - al->socket = env->cpu[al->cpu].socket_id;
> + /* bounds-check before truncating to struct perf_cpu (int16_t) */
> + if (env && al->cpu < env->nr_cpus_avail) {
Isn't this already covered by the test in perf_env__get_cpu_topology?
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/env.h?h=perf-tools-next#n199
```
if (env->cpu && cpu.cpu >= 0 && cpu.cpu < env->nr_cpus_avail)
return &env->cpu[cpu.cpu];
return NULL;
```
Thanks,
Ian
> + struct cpu_topology_map *topo;
> +
> + topo = perf_env__get_cpu_topology(env, (struct perf_cpu){ al->cpu });
> + if (topo)
> + al->socket = topo->socket_id;
> + }
> }
>
> /* Account for possible out-of-order switch events. */
> --
> 2.54.0
>