linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Liang, Kan" <kan.liang@linux.intel.com>
To: Fedor Pchelkin <pchelkin@ispras.ru>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	x86@kernel.org,
	Alexander Antonov <alexander.antonov@linux.intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	lvc-project@linuxtesting.org
Subject: Re: [PATCH] perf/x86/uncore: avoid null-ptr-deref on error in pmu_alloc_topology
Date: Mon, 5 Feb 2024 10:08:27 -0500	[thread overview]
Message-ID: <a5e8e3fe-a8c2-45e7-9139-84967cba06eb@linux.intel.com> (raw)
In-Reply-To: <20240204134841.80003-1-pchelkin@ispras.ru>



On 2024-02-04 8:48 a.m., Fedor Pchelkin wrote:
> If topology[die] array allocation fails then topology[die][idx] elements
> can't be accessed on error path.
> 
> Checking this on the error path probably looks more readable than
> decrementing the counter in the allocation loop.
> 
> Found by Linux Verification Center (linuxtesting.org).
> 
> Fixes: 4d13be8ab5d4 ("perf/x86/intel/uncore: Generalize IIO topology support")
> Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
> ---

It seems the code just jumps to the wrong kfree on the error path.
Does the below patch work?

diff --git a/arch/x86/events/intel/uncore_snbep.c
b/arch/x86/events/intel/uncore_snbep.c
index 8250f0f59c2b..5481fd00d861 100644
--- a/arch/x86/events/intel/uncore_snbep.c
+++ b/arch/x86/events/intel/uncore_snbep.c
@@ -3808,7 +3808,7 @@ static int pmu_alloc_topology(struct
intel_uncore_type *type, int topology_type)
 	for (die = 0; die < uncore_max_dies(); die++) {
 		topology[die] = kcalloc(type->num_boxes, sizeof(**topology), GFP_KERNEL);
 		if (!topology[die])
-			goto clear;
+			goto free_topology;
 		for (idx = 0; idx < type->num_boxes; idx++) {
 			topology[die][idx].untyped = kcalloc(type->num_boxes,
 							     topology_size[topology_type],
@@ -3827,6 +3827,7 @@ static int pmu_alloc_topology(struct
intel_uncore_type *type, int topology_type)
 			kfree(topology[die][idx].untyped);
 		kfree(topology[die]);
 	}
+free_topology:
 	kfree(topology);
 err:
 	return -ENOMEM;

Thanks,
Kan

>  arch/x86/events/intel/uncore_snbep.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/events/intel/uncore_snbep.c b/arch/x86/events/intel/uncore_snbep.c
> index a96496bef678..7d4deb9126be 100644
> --- a/arch/x86/events/intel/uncore_snbep.c
> +++ b/arch/x86/events/intel/uncore_snbep.c
> @@ -3831,9 +3831,11 @@ static int pmu_alloc_topology(struct intel_uncore_type *type, int topology_type)
>  	return 0;
>  clear:
>  	for (; die >= 0; die--) {
> -		for (idx = 0; idx < type->num_boxes; idx++)
> -			kfree(topology[die][idx].untyped);
> -		kfree(topology[die]);
> +		if (topology[die]) {
> +			for (idx = 0; idx < type->num_boxes; idx++)
> +				kfree(topology[die][idx].untyped);
> +			kfree(topology[die]);
> +		}
>  	}
>  	kfree(topology);
>  err:

  reply	other threads:[~2024-02-05 15:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-04 13:48 [PATCH] perf/x86/uncore: avoid null-ptr-deref on error in pmu_alloc_topology Fedor Pchelkin
2024-02-05 15:08 ` Liang, Kan [this message]
2024-02-05 15:18   ` Fedor Pchelkin
2024-02-05 15:32     ` Liang, Kan
2024-12-12 14:35 ` Fedor Pchelkin

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=a5e8e3fe-a8c2-45e7-9139-84967cba06eb@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=alexander.antonov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=mingo@redhat.com \
    --cc=pchelkin@ispras.ru \
    --cc=peterz@infradead.org \
    --cc=x86@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;
as well as URLs for NNTP newsgroup(s).