Skip to content

Commit 6830e20

Browse files
acmelnamhyung
authored andcommitted
perf header: Sanity check HEADER_GROUP_DESC
Add upper bound check on nr_groups in process_group_desc() to harden against malformed perf.data files (max 32768), and move the env assignment after validation. Cc: Namhyung Kim <namhyung@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 f613a6d commit 6830e20

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

tools/perf/util/header.c

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

66+
#define MAX_GROUP_DESC 32768
6667
#define MAX_NUMA_NODES 4096
6768
#define MAX_PMU_MAPPINGS 4096
6869
#define MAX_SCHED_DOMAINS 64
@@ -3132,12 +3133,25 @@ static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
31323133
if (do_read_u32(ff, &nr_groups))
31333134
return -1;
31343135

3135-
env->nr_groups = nr_groups;
31363136
if (!nr_groups) {
31373137
pr_debug("group desc not available\n");
31383138
return 0;
31393139
}
31403140

3141+
if (nr_groups > MAX_GROUP_DESC) {
3142+
pr_err("Invalid HEADER_GROUP_DESC: nr_groups (%u) > %u\n",
3143+
nr_groups, MAX_GROUP_DESC);
3144+
return -1;
3145+
}
3146+
3147+
if (ff->size < sizeof(u32) + nr_groups * 3 * sizeof(u32)) {
3148+
pr_err("Invalid HEADER_GROUP_DESC: section too small (%zu) for %u groups\n",
3149+
ff->size, nr_groups);
3150+
return -1;
3151+
}
3152+
3153+
env->nr_groups = nr_groups;
3154+
31413155
desc = calloc(nr_groups, sizeof(*desc));
31423156
if (!desc)
31433157
return -1;

0 commit comments

Comments
 (0)