Skip to content

Commit dff56bd

Browse files
acmelnamhyung
authored andcommitted
perf header: Add sanity checks to HEADER_BPF_BTF processing
Validate the BTF entry count and individual data sizes when reading HEADER_BPF_BTF from perf.data files to prevent excessive memory allocation from malformed files. Reuses the MAX_BPF_PROGS (131072) and MAX_BPF_DATA_LEN (256 MB) limits from HEADER_BPF_PROG_INFO processing. Cc: Song Liu <song@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.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 66af7e9 commit dff56bd

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

tools/perf/util/header.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,17 @@ static int process_bpf_btf(struct feat_fd *ff __maybe_unused, void *data __mayb
36223622
if (do_read_u32(ff, &count))
36233623
return -1;
36243624

3625+
if (count > MAX_BPF_PROGS) {
3626+
pr_err("bpf btf count %u too large (max %u)\n", count, MAX_BPF_PROGS);
3627+
return -1;
3628+
}
3629+
3630+
if (ff->size < sizeof(u32) + count * 2 * sizeof(u32)) {
3631+
pr_err("Invalid HEADER_BPF_BTF: section too small (%zu) for %u entries\n",
3632+
ff->size, count);
3633+
return -1;
3634+
}
3635+
36253636
down_write(&env->bpf_progs.lock);
36263637

36273638
for (i = 0; i < count; ++i) {
@@ -3632,6 +3643,12 @@ static int process_bpf_btf(struct feat_fd *ff __maybe_unused, void *data __mayb
36323643
if (do_read_u32(ff, &data_size))
36333644
goto out;
36343645

3646+
if (data_size > MAX_BPF_DATA_LEN) {
3647+
pr_err("bpf btf data size %u too large (max %u)\n",
3648+
data_size, MAX_BPF_DATA_LEN);
3649+
goto out;
3650+
}
3651+
36353652
node = malloc(sizeof(struct btf_node) + data_size);
36363653
if (!node)
36373654
goto out;

0 commit comments

Comments
 (0)