Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf header: Fail read if header sections overlap

Buggy perf.data files can have the attributes and data
overlapping.

For example, when processing pipe data the attributes aren't known and
so file offset header calculations can consider them not present.

Later this can cause the attributes to overwrite the data. This can be
seen in:

$ perf record -o - true > a.data
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.059 MB - ]
$ perf inject -i a.data -o b.data
$ perf report --stats -i b.data
0x68 [0]: failed to process type: 510379 [Invalid argument]
Error:
failed to process sample
$

This change makes reading the corrupt file fail:

$ perf report --stats -i b.data
Perf file header corrupt: Attributes and data overlap
incompatible file format (rerun with -v to learn more)
$

Which is more informative.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yanteng Si <siyanteng@loongson.cn>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Link: https://lore.kernel.org/r/20240829150154.37929-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
10df481f d71bbe79

+18
+18
tools/perf/util/header.c
··· 3986 3986 adds_features)); 3987 3987 } 3988 3988 3989 + if (header->size > header->attrs.offset) { 3990 + pr_err("Perf file header corrupt: header overlaps attrs\n"); 3991 + return -1; 3992 + } 3993 + 3994 + if (header->size > header->data.offset) { 3995 + pr_err("Perf file header corrupt: header overlaps data\n"); 3996 + return -1; 3997 + } 3998 + 3999 + if ((header->attrs.offset <= header->data.offset && 4000 + header->attrs.offset + header->attrs.size > header->data.offset) || 4001 + (header->attrs.offset > header->data.offset && 4002 + header->data.offset + header->data.size > header->attrs.offset)) { 4003 + pr_err("Perf file header corrupt: Attributes and data overlap\n"); 4004 + return -1; 4005 + } 4006 + 3989 4007 if (header->size != sizeof(*header)) { 3990 4008 /* Support the previous format */ 3991 4009 if (header->size == offsetof(typeof(*header), adds_features))