Re: [PATCH 5/5] perf arm-spe: Reject zero nr_cpu in metadata to prevent division by zero

Adrian Hunter <[email protected]> Mon, 3 Aug 2026 21:46:49 +0300
Newsgroups org.kernel.vger.linux-perf-users,org.kernel.vger.linux-kernel
Organization Intel Finland Oy, Registered Address: c/o Alberga Business Park, 6 krs, Bertel Jungin Aukio 5, 02600 Espoo, Business Identity Code: 0357606 - 4, Domiciled in Helsinki
Message-ID <[email protected]>
On 27/07/2026 19:17, Arnaldo Carvalho de Melo wrote:
> From: Arnaldo Carvalho de Melo <[email protected]>
> 
> arm_spe__alloc_metadata() reads nr_cpu from the auxtrace_info priv
> array without validation.  When a crafted perf.data provides nr_cpu=0,
> the per_cpu_sz calculation divides by zero:
> 
>   per_cpu_sz = (metadata_size - (hdr_sz * sizeof(u64))) / (*nr_cpu);
> 
> Reject nr_cpu <= 0 early, before the division.  The caller already
> treats NULL return with metadata_ver != 1 as a parse failure.
> 
> Fixes: e52abceb4b6c2723 ("perf arm-spe: Dump metadata with version 2")

git blame shows 7842a4b6ff698 "perf arm-spe: Support metadata version 2"
for the relevant lines

> Reported-by: sashiko-bot <[email protected]>
> Cc: Leo Yan <[email protected]>
> Cc: James Clark <[email protected]>
> Cc: Adrian Hunter <[email protected]>
> Assisted-by: Claude:claude-opus-4.6
> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
> ---
>  tools/perf/util/arm-spe.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> index 552f063f126e6769..401aab529309cbd6 100644
> --- a/tools/perf/util/arm-spe.c
> +++ b/tools/perf/util/arm-spe.c
> @@ -1605,6 +1605,10 @@ static u64 **arm_spe__alloc_metadata(struct perf_record_auxtrace_info *info,
>  	hdr_sz = ptr[ARM_SPE_HEADER_SIZE];
>  	*nr_cpu = ptr[ARM_SPE_CPUS_NUM];
>  
> +	/* nr_cpu is used as a divisor below */
> +	if (*nr_cpu <= 0)
> +		return NULL;
> +
>  	metadata = calloc(*nr_cpu, sizeof(*metadata));
>  	if (!metadata)
>  		return NULL;