Skip to content

Commit f5722a6

Browse files
acmelnamhyung
authored andcommitted
perf header: Sanity check HEADER_PMU_CAPS
Add upper bound checks in PMU capabilities processing to harden against malformed perf.data files: - nr_pmu bounded to MAX_PMU_MAPPINGS (4096) in process_pmu_caps() - nr_pmu_caps bounded to MAX_PMU_CAPS (512) in __process_pmu_caps() Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Ian Rogers <irogers@google.com> Assisted-by: Claude Code:claude-opus-4-6 Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 47c68eb commit f5722a6

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

tools/perf/util/header.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#define MAX_CACHE_ENTRIES 32768
6767
#define MAX_GROUP_DESC 32768
6868
#define MAX_NUMA_NODES 4096
69+
#define MAX_PMU_CAPS 512
6970
#define MAX_PMU_MAPPINGS 4096
7071
#define MAX_SCHED_DOMAINS 64
7172

@@ -3677,6 +3678,12 @@ static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
36773678
if (!nr_pmu_caps)
36783679
return 0;
36793680

3681+
if (nr_pmu_caps > MAX_PMU_CAPS) {
3682+
pr_err("Invalid pmu caps: nr_pmu_caps (%u) > %u\n",
3683+
nr_pmu_caps, MAX_PMU_CAPS);
3684+
return -1;
3685+
}
3686+
36803687
*caps = calloc(nr_pmu_caps, sizeof(char *));
36813688
if (!*caps)
36823689
return -1;
@@ -3754,6 +3761,18 @@ static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
37543761
return 0;
37553762
}
37563763

3764+
if (nr_pmu > MAX_PMU_MAPPINGS) {
3765+
pr_err("Invalid HEADER_PMU_CAPS: nr_pmu (%u) > %u\n",
3766+
nr_pmu, MAX_PMU_MAPPINGS);
3767+
return -1;
3768+
}
3769+
3770+
if (ff->size < sizeof(u32) + nr_pmu * sizeof(u32)) {
3771+
pr_err("Invalid HEADER_PMU_CAPS: section too small (%zu) for %u PMUs\n",
3772+
ff->size, nr_pmu);
3773+
return -1;
3774+
}
3775+
37573776
pmu_caps = calloc(nr_pmu, sizeof(*pmu_caps));
37583777
if (!pmu_caps)
37593778
return -ENOMEM;

0 commit comments

Comments
 (0)