Skip to content

Commit bc21e74

Browse files
Denis Nikitinacmel
authored andcommitted
perf session: Remap buf if there is no space for event
If a perf event doesn't fit into remaining buffer space return NULL to remap buf and fetch the event again. Keep the logic to error out on inadequate input from fuzzing. This fixes perf failing on ChromeOS (with 32b userspace): $ perf report -v -i perf.data ... prefetch_event: head=0x1fffff8 event->header_size=0x30, mmap_size=0x2000000: fuzzed or compressed perf.data? Error: failed to process sample Fixes: 57fc032 ("perf session: Avoid infinite loop when seeing invalid header.size") Reviewed-by: James Clark <james.clark@arm.com> Signed-off-by: Denis Nikitin <denik@chromium.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexey Budankov <alexey.budankov@linux.intel.com> Cc: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/r/20220330031130.2152327-1-denik@chromium.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 299687e commit bc21e74

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tools/perf/util/session.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,7 @@ prefetch_event(char *buf, u64 head, size_t mmap_size,
20952095
bool needs_swap, union perf_event *error)
20962096
{
20972097
union perf_event *event;
2098+
u16 event_size;
20982099

20992100
/*
21002101
* Ensure we have enough space remaining to read
@@ -2107,15 +2108,23 @@ prefetch_event(char *buf, u64 head, size_t mmap_size,
21072108
if (needs_swap)
21082109
perf_event_header__bswap(&event->header);
21092110

2110-
if (head + event->header.size <= mmap_size)
2111+
event_size = event->header.size;
2112+
if (head + event_size <= mmap_size)
21112113
return event;
21122114

21132115
/* We're not fetching the event so swap back again */
21142116
if (needs_swap)
21152117
perf_event_header__bswap(&event->header);
21162118

2117-
pr_debug("%s: head=%#" PRIx64 " event->header_size=%#x, mmap_size=%#zx:"
2118-
" fuzzed or compressed perf.data?\n",__func__, head, event->header.size, mmap_size);
2119+
/* Check if the event fits into the next mmapped buf. */
2120+
if (event_size <= mmap_size - head % page_size) {
2121+
/* Remap buf and fetch again. */
2122+
return NULL;
2123+
}
2124+
2125+
/* Invalid input. Event size should never exceed mmap_size. */
2126+
pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:"
2127+
" fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size);
21192128

21202129
return error;
21212130
}

0 commit comments

Comments
 (0)