]> www.infradead.org Git - users/willy/xarray.git/commitdiff
perf pmu: Handle memory failure in tool_pmu__new()
authorThomas Richter <tmricht@linux.ibm.com>
Wed, 19 Mar 2025 12:28:20 +0000 (13:28 +0100)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 20 Mar 2025 00:00:16 +0000 (17:00 -0700)
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")
Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Link: https://lore.kernel.org/r/20250319122820.2898333-1-tmricht@linux.ibm.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/pmus.c
tools/perf/util/tool_pmu.c

index 9b5a63ecb24934ba19a36715330d937a6d4fb948..b99292de76693dbb79dcdc596104de686b9867b1 100644 (file)
@@ -265,7 +265,8 @@ skip_pe_pmus:
        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)
index b60ac390d52d27459881bb60031e767423cb906f..97b327d1ce4a0175bf6da725f51d5c05636e1039 100644 (file)
@@ -495,12 +495,20 @@ 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);
+               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;
 }