Skip to contentSkip to conversation

perf pmu: Handle memory failure in tool_pmu__new()

PATCH Superseded Thomas Richter 2 participants 3 messages Mar 19, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf pmu: Handle memory failure in tool_pmu__new()

On linux-next
commit 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU") allocated PMU named "tool" dynamicly. However that allocation can fail and a NULL pointer is returned. That case is currently not handled and would result in an invalid address reference. Add a check for NULL pointer.

Fixes 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
 tools/perf/util/pmus.c     | 3 ++-
 tools/perf/util/tool_pmu.c | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/util/pmus.c +2 -1
index 9b5a63ecb249..b99292de7669 100644
@@ -265,7 +265,8 @@ static void pmu_read_sysfs(unsigned int to_read_types)
 	if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) != 0 &&
 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) == 0) {
 		tool_pmu = tool_pmu__new();
-		list_add_tail(&tool_pmu->list, &other_pmus);
+		if (tool_pmu)
+			list_add_tail(&tool_pmu->list, &other_pmus);
 	}
 	if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) != 0 &&
 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)
tools/perf/util/tool_pmu.c +9 -0
index b60ac390d52d..d764c4734be6 100644
@@ -495,12 +495,21 @@ struct perf_pmu *tool_pmu__new(void)
 {
 	struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu));
 
+	if (!tool)
+		goto out;
 	tool->name = strdup("tool");
+	if (!tool->name) {
+		zfree(tool);
+		tool = NULL;
+		goto out;
+	}
+
 	tool->type = PERF_PMU_TYPE_TOOL;
 	INIT_LIST_HEAD(&tool->aliases);
 	INIT_LIST_HEAD(&tool->caps);
 	INIT_LIST_HEAD(&tool->format);
 	tool->events_table = find_core_events_table("common", "common");
 
+out:
 	return tool;
 }
Signature · 1 line
2.48.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Thomas Richter

On linux-next
commit 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU") allocated PMU named "tool" dynamicly. However that allocation can fail and a NULL pointer is returned. That case is currently not handled and would result in an invalid address reference. Add a check for NULL pointer.

Fixes 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>
---
  tools/perf/util/pmus.c     | 3 ++-
  tools/perf/util/tool_pmu.c | 9 +++++++++
  2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/util/pmus.c +2 -1
index 9b5a63ecb249..b99292de7669 100644
@@ -265,7 +265,8 @@ static void pmu_read_sysfs(unsigned int to_read_types)
  	if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) != 0 &&
  	    (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) == 0) {
  		tool_pmu = tool_pmu__new();
-		list_add_tail(&tool_pmu->list, &other_pmus);
+		if (tool_pmu)
+			list_add_tail(&tool_pmu->list, &other_pmus);
  	}
  	if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) != 0 &&
  	    (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)
tools/perf/util/tool_pmu.c +0 -0
index b60ac390d52d..d764c4734be6 100644

tools/perf/util/tool_pmu.c struct perf_pmu *tool_pmu__new(void) · line 503

Quoted 9 lines
tools/perf/util/tool_pmu.c +5 -0
@@ -495,12 +495,21 @@ struct perf_pmu *tool_pmu__new(void)
  {
  	struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu));
 
+	if (!tool)
+		goto out;
  	tool->name = strdup("tool");
+	if (!tool->name) {
+		zfree(tool);
+		tool = NULL;

Hi Thomas,

zfree() already sets the thing to NULL but you need to pass a pointer to it:

  zfree(&tool);

Without doing that you only free the first u64 of the struct, which happens to be zero in this case so free() does nothing. Then zfree() sets the first u64 of the struct to zero which it already is.

With that fixed:

#2 lore raw
Quoted 51 lines · James Clark
Quoted 35 lines · Thomas Richter

On linux-next
commit 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU") allocated PMU named "tool" dynamicly. However that allocation can fail and a NULL pointer is returned. That case is currently not handled and would result in an invalid address reference. Add a check for NULL pointer.

Fixes 72c6f57a4193 ("perf pmu: Dynamically allocate tool PMU")
Cc Ian Rogers <[email protected]>
Cc James Clark <[email protected]>

---
  tools/perf/util/pmus.c     | 3 ++-
  tools/perf/util/tool_pmu.c | 9 +++++++++

  2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/util/pmus.c +0 -0
index 9b5a63ecb249..b99292de7669 100644
@@ -265,7 +265,8 @@ static void pmu_read_sysfs(unsigned int to_read_types)

      if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) != 0 && (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) == 0) { tool_pmu = tool_pmu__new();
-        list_add_tail(&tool_pmu->list, &other_pmus);
+        if (tool_pmu)
+            list_add_tail(&tool_pmu->list, &other_pmus); }
      if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) != 0 && (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)

tools/perf/util/tool_pmu.c +0 -0
index b60ac390d52d..d764c4734be6 100644

tools/perf/util/tool_pmu.c struct perf_pmu *tool_pmu__new(void) · line 502

Quoted 8 lines
tools/perf/util/tool_pmu.c +4 -0
@@ -495,12 +495,21 @@ struct perf_pmu *tool_pmu__new(void)
   {
       struct perf_pmu *tool = zalloc(sizeof(struct perf_pmu));
   +    if (!tool)
+        goto out;
       tool->name = strdup("tool");
+    if (!tool->name) {
+        zfree(tool);
+        tool = NULL;

Hi Thomas,

zfree() already sets the thing to NULL but you need to pass a pointer to it:

Quoted 9 lines

  zfree(&tool);

Without doing that you only free the first u64 of the struct, which happens to be zero in this case so free() does nothing. Then zfree() sets the first u64 of the struct to zero which it already is.

With that fixed:

Thanks for the finding. I'll post verion 2 soon.

Signature · 9 lines
Thomas Richter, Dept 3303, IBM s390 Linux Development, Boeblingen, Germany
--
IBM Deutschland Research & Development GmbH

Vorsitzender des Aufsichtsrats: Wolfgang Wendt

Geschäftsführung: David Faller

Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294