Skip to content

Commit 66af7e9

Browse files
acmelnamhyung
authored andcommitted
perf header: Sanity check HEADER_BPF_PROG_INFO
Add validation to process_bpf_prog_info() to harden against malformed perf.data files: - Upper bound on BPF program count (max 131072) - Upper bound on per-program data_len (max 256MB) 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 f5722a6 commit 66af7e9

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

tools/perf/util/header.c

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

66+
#define MAX_BPF_DATA_LEN (256 * 1024 * 1024)
67+
#define MAX_BPF_PROGS 131072
6668
#define MAX_CACHE_ENTRIES 32768
6769
#define MAX_GROUP_DESC 32768
6870
#define MAX_NUMA_NODES 4096
@@ -3525,6 +3527,18 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _
35253527
if (do_read_u32(ff, &count))
35263528
return -1;
35273529

3530+
if (count > MAX_BPF_PROGS) {
3531+
pr_err("Invalid HEADER_BPF_PROG_INFO: count (%u) > %u\n",
3532+
count, MAX_BPF_PROGS);
3533+
return -1;
3534+
}
3535+
3536+
if (ff->size < sizeof(u32) + count * (2 * sizeof(u32) + sizeof(u64))) {
3537+
pr_err("Invalid HEADER_BPF_PROG_INFO: section too small (%zu) for %u entries\n",
3538+
ff->size, count);
3539+
return -1;
3540+
}
3541+
35283542
down_write(&env->bpf_progs.lock);
35293543

35303544
for (i = 0; i < count; ++i) {
@@ -3542,6 +3556,12 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _
35423556
goto out;
35433557
}
35443558

3559+
if (data_len > MAX_BPF_DATA_LEN) {
3560+
pr_warning("Invalid HEADER_BPF_PROG_INFO: data_len (%u) too large\n",
3561+
data_len);
3562+
goto out;
3563+
}
3564+
35453565
info_linear = malloc(sizeof(struct perf_bpil) +
35463566
data_len);
35473567
if (!info_linear)

0 commit comments

Comments
 (0)