linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event()
@ 2025-08-15  7:20 zhaoguohan
  2025-08-18 21:28 ` Ian Rogers
  2025-08-25 12:55 ` Markus Elfring
  0 siblings, 2 replies; 3+ messages in thread
From: zhaoguohan @ 2025-08-15  7:20 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung
  Cc: mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	kan.liang, thomas.falcon, open list:PERFORMANCE EVENTS SUBSYSTEM,
	open list:PERFORMANCE EVENTS SUBSYSTEM, GuoHan Zhao

From: GuoHan Zhao <zhaoguohan@kylinos.cn>

In the error handling path of __add_event(), if evsel__new_idx() fails
and returns NULL, the subsequent calls to zfree(&evsel->name) and
zfree(&evsel->metric_id) will cause null pointer dereference.

Extend the goto chain to properly handle the case where evsel allocation
fails, avoiding unnecessary cleanup operations on a NULL pointer.

Fixes: cd63c2216825 ("perf parse-events: Minor __add_event refactoring")
Signed-off-by: GuoHan Zhao <zhaoguohan@kylinos.cn>

Changes in V2:
- Extended the goto chain with separate error handling labels instead of using null pointer check
- Reordered jump targets to avoid accessing NULL evsel members
- Added Fixes tag
- Updated commit subject to use "Prevent" instead of "Fix"
---
 tools/perf/util/parse-events.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 8282ddf68b98..8a1fc5d024bf 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -277,18 +277,18 @@ __add_event(struct list_head *list, int *idx,
 
 	evsel = evsel__new_idx(attr, *idx);
 	if (!evsel)
-		goto out_err;
+		goto out_free_cpus;
 
 	if (name) {
 		evsel->name = strdup(name);
 		if (!evsel->name)
-			goto out_err;
+			goto out_free_evsel;
 	}
 
 	if (metric_id) {
 		evsel->metric_id = strdup(metric_id);
 		if (!evsel->metric_id)
-			goto out_err;
+			goto out_free_evsel;
 	}
 
 	(*idx)++;
@@ -310,12 +310,15 @@ __add_event(struct list_head *list, int *idx,
 		evsel__warn_user_requested_cpus(evsel, user_cpus);
 
 	return evsel;
-out_err:
-	perf_cpu_map__put(cpus);
-	perf_cpu_map__put(pmu_cpus);
+
+out_free_evsel:
 	zfree(&evsel->name);
 	zfree(&evsel->metric_id);
 	free(evsel);
+out_free_cpus:
+	perf_cpu_map__put(cpus);
+	perf_cpu_map__put(pmu_cpus);
+
 	return NULL;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event()
  2025-08-15  7:20 [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event() zhaoguohan
@ 2025-08-18 21:28 ` Ian Rogers
  2025-08-25 12:55 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Rogers @ 2025-08-18 21:28 UTC (permalink / raw)
  To: zhaoguohan
  Cc: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, adrian.hunter, kan.liang, thomas.falcon,
	open list:PERFORMANCE EVENTS SUBSYSTEM,
	open list:PERFORMANCE EVENTS SUBSYSTEM

On Fri, Aug 15, 2025 at 12:20 AM <zhaoguohan@kylinos.cn> wrote:
>
> From: GuoHan Zhao <zhaoguohan@kylinos.cn>
>
> In the error handling path of __add_event(), if evsel__new_idx() fails
> and returns NULL, the subsequent calls to zfree(&evsel->name) and
> zfree(&evsel->metric_id) will cause null pointer dereference.
>
> Extend the goto chain to properly handle the case where evsel allocation
> fails, avoiding unnecessary cleanup operations on a NULL pointer.
>
> Fixes: cd63c2216825 ("perf parse-events: Minor __add_event refactoring")
> Signed-off-by: GuoHan Zhao <zhaoguohan@kylinos.cn>

Reviewed-by: Ian Rogers <irogers@google.com>

> Changes in V2:
> - Extended the goto chain with separate error handling labels instead of using null pointer check
> - Reordered jump targets to avoid accessing NULL evsel members
> - Added Fixes tag
> - Updated commit subject to use "Prevent" instead of "Fix"
> ---
>  tools/perf/util/parse-events.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index 8282ddf68b98..8a1fc5d024bf 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -277,18 +277,18 @@ __add_event(struct list_head *list, int *idx,
>
>         evsel = evsel__new_idx(attr, *idx);
>         if (!evsel)
> -               goto out_err;
> +               goto out_free_cpus;

nit: can we call this out_put_cpus rather than free?

Thanks,
Ian

>
>         if (name) {
>                 evsel->name = strdup(name);
>                 if (!evsel->name)
> -                       goto out_err;
> +                       goto out_free_evsel;
>         }
>
>         if (metric_id) {
>                 evsel->metric_id = strdup(metric_id);
>                 if (!evsel->metric_id)
> -                       goto out_err;
> +                       goto out_free_evsel;
>         }
>
>         (*idx)++;
> @@ -310,12 +310,15 @@ __add_event(struct list_head *list, int *idx,
>                 evsel__warn_user_requested_cpus(evsel, user_cpus);
>
>         return evsel;
> -out_err:
> -       perf_cpu_map__put(cpus);
> -       perf_cpu_map__put(pmu_cpus);
> +
> +out_free_evsel:
>         zfree(&evsel->name);
>         zfree(&evsel->metric_id);
>         free(evsel);
> +out_free_cpus:
> +       perf_cpu_map__put(cpus);
> +       perf_cpu_map__put(pmu_cpus);
> +
>         return NULL;
>  }
>
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event()
  2025-08-15  7:20 [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event() zhaoguohan
  2025-08-18 21:28 ` Ian Rogers
@ 2025-08-25 12:55 ` Markus Elfring
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2025-08-25 12:55 UTC (permalink / raw)
  To: GuoHan Zhao, linux-perf-users
  Cc: LKML, Adrian Hunter, Alexander Shishkin, Arnaldo Carvalho de Melo,
	Ian Rogers, Ingo Molnar, Jiri Olsa, Kan Liang, Mark Rutland,
	Namhyung Kim, Peter Zijlstra, Thomas Falcon

…
> Signed-off-by: GuoHan Zhao <zhaoguohan@kylinos.cn>
> 
> Changes in V2:
> - Extended the goto chain with separate error handling labels …
> ---
>  tools/perf/util/parse-events.c | 15 +++++++++------
…

Please move your patch version descriptions behind the marker line.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc3#n784

Regards,
Markus

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-25 12:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-15  7:20 [PATCH v2] perf parse-events: Prevent null pointer dereference in __add_event() zhaoguohan
2025-08-18 21:28 ` Ian Rogers
2025-08-25 12:55 ` Markus Elfring

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).