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

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