Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] perf trace: guard free(new_filter) to avoid double-free on OOM
@ 2026-11-12  3:18 Gu Bin
  2026-11-12  3:25 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Gu Bin @ 2026-11-12  3:18 UTC (permalink / raw)
  To: acme, namhyung
  Cc: peterz, mingo, mark.rutland, alexander.shishkin, jolsa, irogers,
	adrian.hunter, linux-perf-users, linux-kernel

On the asprintf() failure path in trace__expand_filter(), free(new_filter)
is called unconditionally. On the first iteration new_filter aliases
evsel->filter, so this frees the evsel's own filter string; when the
error propagates and the evsel is later deleted, evsel->filter is freed
a second time.

The success path, right below, guards the free with
"if (new_filter != evsel->filter)". Do the same on the error path.

Fixes: 90df0249c2ea ("perf trace: Expand strings in filters to integers")
Signed-off-by: Gu Bin <gubin@cmss.chinamobile.com>
---
 tools/perf/builtin-trace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index 4094141461f91..f4a67491f5be1 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -4679,7 +4679,8 @@ static int trace__expand_filter(struct trace *trace, struct evsel *evsel)
 
 					if (asprintf(&n, "%.*s%s%s", expansion_offset, new_filter, expansion, right_end) < 0) {
 						pr_debug(" out of memory!\n");
-						free(new_filter);
+						if (new_filter != evsel->filter)
+							free(new_filter);
 						return -1;
 					}
 					if (new_filter != evsel->filter)
-- 
2.50.1.windows.1




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

* Re: [PATCH] perf trace: guard free(new_filter) to avoid double-free on OOM
  2026-11-12  3:18 [PATCH] perf trace: guard free(new_filter) to avoid double-free on OOM Gu Bin
@ 2026-11-12  3:25 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-12  3:25 UTC (permalink / raw)
  To: Gu Bin; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Medium] perf trace: unhandled -ENOMEM in trace__expand_filter() [1]
--

commit e8aa93f0bab0450d21dc93c231c980b9474cb264
Author: Gu Bin <gubin@cmss.chinamobile.com>

perf trace: guard free(new_filter) to avoid double-free on OOM

This commit guards free(new_filter) on the asprintf() failure path in
trace__expand_filter() so that evsel->filter is not freed when new_filter
still aliases it.

> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index 4094141461f91..f4a67491f5be1 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
> @@ -4679,7 +4679,8 @@ static int trace__expand_filter(struct trace *trace, struct evsel *evsel)
>  
>  					if (asprintf(&n, "%.*s%s%s", expansion_offset, new_filter, expansion, right_end) < 0) {
>  						pr_debug(" out of memory!\n");
> -						free(new_filter);
> +						if (new_filter != evsel->filter)
> +							free(new_filter);
>  						return -1;
>  					}
>  					if (new_filter != evsel->filter)

[Severity: Medium]
This problem wasn't introduced by this patch, but at the end of
trace__expand_filter(), should the return value of evsel__set_filter() be
checked [1]?

tools/perf/builtin-trace.c:trace__expand_filter() {
	...
	if (new_filter != evsel->filter) {
		pr_debug("New filter for %s: %s\n", evsel->name, new_filter);
		evsel__set_filter(evsel, new_filter);
		free(new_filter);
	}

	return 0;
}

If strdup() inside evsel__set_filter() fails with -ENOMEM, the failure is
silently ignored and trace__expand_filter() returns 0, leaving the
unexpanded filter in place.

[1] https://sashiko.dev/#/bug/linux-cbf4bb01-a981-47dc-b440-68e5c9c3e81c

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261112031810.1899-1-gubin@cmss.chinamobile.com?part=1

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

end of thread, other threads:[~2026-11-12  3:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12  3:18 [PATCH] perf trace: guard free(new_filter) to avoid double-free on OOM Gu Bin
2026-11-12  3:25 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox