Skip to content

Commit 110a661

Browse files
acmelnamhyung
authored andcommitted
perf header: Sanity check HEADER_CACHE
Add upper bound check on cache entry count in process_cache() to harden against malformed perf.data files (max 32768). Cc: Jiri Olsa <jolsa@kernel.org> 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 6830e20 commit 110a661

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tools/perf/util/header.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include <event-parse.h>
6464
#endif
6565

66+
#define MAX_CACHE_ENTRIES 32768
6667
#define MAX_GROUP_DESC 32768
6768
#define MAX_NUMA_NODES 4096
6869
#define MAX_PMU_MAPPINGS 4096
@@ -3243,6 +3244,18 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
32433244
if (do_read_u32(ff, &cnt))
32443245
return -1;
32453246

3247+
if (cnt > MAX_CACHE_ENTRIES) {
3248+
pr_err("Invalid HEADER_CACHE: cnt (%u) > %u\n",
3249+
cnt, MAX_CACHE_ENTRIES);
3250+
return -1;
3251+
}
3252+
3253+
if (ff->size < 2 * sizeof(u32) + cnt * 7 * sizeof(u32)) {
3254+
pr_err("Invalid HEADER_CACHE: section too small (%zu) for %u entries\n",
3255+
ff->size, cnt);
3256+
return -1;
3257+
}
3258+
32463259
caches = calloc(cnt, sizeof(*caches));
32473260
if (!caches)
32483261
return -1;

0 commit comments

Comments
 (0)