Skip to contentSkip to conversation

perf: do not call zfree() on uninitialized pointer

PATCH Superseded Jiri Slaby (SUSE) 2 participants 4 messages Nov 4 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf: do not call zfree() on uninitialized pointer

In add_drm_pmu(), the check for type > PERF_PMU_TYPE_DRM_END happens before "drm" is allocated by zalloc(). The error path nevertheless called zfree(&drm), i.e. free() on an uninitialized pointer, which can crash. Remove this bogus zfree() -- nothing has been allocated yet at this point, so there is nothing to free.

Fixes this compiler warning:
In function ‘__zfree’,
inlined from ‘add_drm_pmu’ at util/drm_pmu.c:110:3,
inlined from ‘read_drm_pmus_cb’ at util/drm_pmu.c:174:10:
../lib/zalloc.c:13:9: warning: ‘drm’ may be used uninitialized [-Wmaybe-uninitialized]

   13 |         free(*ptr);
      |         ^
Assisted-by LLM
Fixes 28917cb17f9d ("perf drm_pmu: Add a tool like PMU to expose DRM information")
Cc Ian Rogers <[email protected]>

---
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: James Clark <[email protected]>
Cc: [email protected]

---
 tools/perf/util/drm_pmu.c | 1 -
 1 file changed, 1 deletion(-)
tools/perf/util/drm_pmu.c +0 -1
index b8badae7015c..5e4bdd8e7fa8 100644
@@ -107,7 +107,6 @@ static struct drm_pmu *add_drm_pmu(struct list_head *pmus, char *line, size_t li
 		type = PERF_PMU_TYPE_DRM_START;
 
 	if (type > PERF_PMU_TYPE_DRM_END) {
-		zfree(&drm);
 		pr_err("Unable to encode DRM PMU type for %s\n", name);
 		return NULL;
 	}
Signature · 1 line
2.56.0
#1 lore raw

PATCH 2/2 perf: do not call list_for_each_entry_safe() on uninitalized list

In test__pmu_config_helpers(), when evlist__new() fails, the code jumped to the err_out label, which calls parse_events_terms__exit(&terms). At that point parse_events_terms__init(&terms) has not run yet, so the terms list head is uninitialized and list_for_each_entry_safe() walks a garbage pointer.

Add a separate err_out_pmu label after the evlist/terms cleanup and use it for the early evlist allocation failure, so the uninitialized terms are never touched.

Fixes this compiler warning:
In function ‘parse_events_terms__exit’,
inlined from ‘test__pmu_config_helpers’ at tests/pmu.c:296:2:
util/parse-events.c:2849:2: warning: ‘terms.terms.next’ may be used uninitialized [-Wmaybe-uninitialized] tests/pmu.c: In function ‘test__pmu_config_helpers’:
tests/pmu.c:209:28: note: ‘terms’ declared here

Assisted-by LLM
Fixes 8e2ef85c66dc ("perf tests: Test evsel__set_config_if_unset() and config change tracking")
Cc James Clark <[email protected]>

---
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Ian Rogers <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: [email protected]

---
 tools/perf/tests/pmu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/tests/pmu.c +2 -1
index 13e8d7fa80af..be984e1385df 100644
@@ -223,7 +223,7 @@ static int test__pmu_config_helpers(struct test_suite *test __maybe_unused,
 	evlist = evlist__new();
 	if (evlist == NULL) {
 		pr_err("Failed allocation");
-		goto err_out;
+		goto err_out_pmu;
 	}
 
 	parse_events_terms__init(&terms);
@@ -295,6 +295,7 @@ static int test__pmu_config_helpers(struct test_suite *test __maybe_unused,
 err_out:
 	parse_events_terms__exit(&terms);
 	evlist__put(evlist);
+err_out_pmu:
 	test_pmu_put(dir, pmu);
 	return ret;
 }
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 2/2] perf: do not call list_for_each_entry_safe() on uninitalized list

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf tests: uninitialized err list in test__pmu_config_helpers() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf tests: uninitialized err list in test__pmu_config_helpers() --

commit 779f6d6b7e775ad26417497420edb0cda7bafa6c
Author: Jiri Slaby (SUSE) <[email protected]>

perf: do not call list_for_each_entry_safe() on uninitalized list

This commit adds an err_out_pmu cleanup label in test__pmu_config_helpers() so that an early evlist__new() allocation failure skips calling parse_events_terms__exit(&terms) before parse_events_terms__init(&terms) has run.

Quoted 14 lines
tools/perf/tests/pmu.c +1 -1
index 13e8d7fa80af2..be984e1385df2 100644
@@ -223,7 +223,7 @@ static int test__pmu_config_helpers(struct test_suite *test __maybe_unused,
 	evlist = evlist__new();
 	if (evlist == NULL) {
 		pr_err("Failed allocation");
-		goto err_out;
+		goto err_out_pmu;
 	}
 
 	parse_events_terms__init(&terms);

tools/perf/tests/pmu.c static int test__pmu_config_helpers(struct test_suite *test __maybe_unused, · line 301

Quoted 7 lines
tools/perf/tests/pmu.c +1 -0
@@ -295,6 +295,7 @@ static int test__pmu_config_helpers(struct test_suite *test __maybe_unused,
 err_out:
 	parse_events_terms__exit(&terms);
 	evlist__put(evlist);
+err_out_pmu:
 	test_pmu_put(dir, pmu);
 	return ret;
 }

[Severity: Medium]
Should test__pmu_config_helpers() in tools/perf/tests/pmu.c be initializing and cleaning up err instead of terms?

Looking at test__pmu_config_helpers(), both terms and config_terms appear to be unused remnants, whereas err is passed to parse_events(), parse_events_error__print(), and parse_events_error__contains() without parse_events_error__init(&err) or parse_events_error__exit(&err) ever being called:

	struct parse_events_terms terms;
	struct parse_events_error err;
	LIST_HEAD(config_terms);
	...
	parse_events_terms__init(&terms);
	ret = parse_events(evlist, event, &err);
	if (ret) {
		pr_debug("failed to parse event '%s', err %d\n", event, ret);
		parse_events_error__print(&err, event);
		if (parse_events_error__contains(&err, "can't access trace events"))
			ret = TEST_SKIP;
		goto err_out;
	}
	...
err_out:
	parse_events_terms__exit(&terms);
	evlist__put(evlist);

When parse_events() fails and records an error via parse_events_error__handle(), list_add(&entry->list, &err->list) and the subsequent list_for_each_entry() walks in parse_events_error__print() and parse_events_error__contains() will dereference the uninitialized err.list pointers on the stack, and any allocated parse_events_error_entry objects will be leaked.

Instead of keeping the unused terms variable, should terms and config_terms be removed and parse_events_terms__init(&terms) / parse_events_terms__exit(&terms) be replaced with parse_events_error__init(&err) and parse_events_error__exit(&err)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw
In reply to Jiri Slaby (SUSE)
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

In add_drm_pmu(), the check for type > PERF_PMU_TYPE_DRM_END happens before "drm" is allocated by zalloc(). The error path nevertheless called zfree(&drm), i.e. free() on an uninitialized pointer, which can crash. Remove this bogus zfree() -- nothing has been allocated yet at this point, so there is nothing to free.

Fixes this compiler warning:
In function ‘__zfree’,
inlined from ‘add_drm_pmu’ at util/drm_pmu.c:110:3,
inlined from ‘read_drm_pmus_cb’ at util/drm_pmu.c:174:10:
../lib/zalloc.c:13:9: warning: ‘drm’ may be used uninitialized [-Wmaybe-uninitialized]

Quoted 6 lines
   13 |         free(*ptr);
      |         ^
Assisted-by LLM
Fixes 28917cb17f9d ("perf drm_pmu: Add a tool like PMU to expose DRM information")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1