From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH v4 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types
Date: Wed, 25 Nov 2026 01:19:10 +0000 [thread overview]
Message-ID: <sashiko-outbox-232323@kernel.org> (raw)
In-Reply-To: <20261125005108.1020003-6-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] perf/core: u16 header_size overflow in perf_prepare_sample()
with AUX
- [Medium] perf/core: unprotected per-CPU entry in
perf_output_sample_build_id()
- [Low] perf/core: wasted kernel IP lookup and uninit read in
perf_prepare_sample()
--
commit 12fd54f1045a93a0b62bd4e4ddb1fe0c0c1107f8
Author: Ian Rogers <irogers@google.com>
perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types
This commit implements synchronous PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET payload generation using
stack_map_get_build_id_offset() and per-CPU callchain build ID scratch
buffers.
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index d67bb825c504c..2a6ab82d617ef 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
[ ... ]
> @@ -8394,6 +8400,78 @@ static void perf_output_read(struct perf_output_handle *handle,
> perf_output_read_one(handle, event, enabled, running);
> }
>
[ ... ]
> +static void perf_output_sample_build_id(struct perf_output_handle *handle,
> + struct perf_event_header *header,
> + struct perf_sample_data *data,
> + struct perf_event *event)
> +{
> + u64 sample_type = data->type;
> + bool sample_bid = sample_type & PERF_SAMPLE_BUILD_ID_OFFSET;
> + bool callchain_bid = sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
> + bool crosstask = event->ctx->task && event->ctx->task != current;
> + bool has_user_ctx = is_user_task(current) && current->mm && !crosstask;
> + bool is_user = (header->misc & PERF_RECORD_MISC_CPUMODE_MASK) ==
> + PERF_RECORD_MISC_USER;
> +
> + if (sample_bid) {
> + if (is_user && has_user_ctx) {
> + if (callchain_bid && data->callchain_build_id &&
> + data->callchain_build_id->has_ip_bid) {
> + perf_output_put(handle,
> + data->callchain_build_id->ip_bid);
> + } else {
> + struct bpf_stack_build_id bpf_bid = {
> + .ip = data->ip,
> + };
> +
> + stack_map_get_build_id_offset(&bpf_bid, 1, true,
> + false);
> + perf_bpf_to_sample_bid_offset(&bpf_bid);
> + perf_output_put(handle, bpf_bid);
> + }
> + } else {
> + struct perf_sample_build_id_offset bid_offset = {
> + .offset = data->ip,
> + };
> +
> + perf_output_put(handle, bid_offset);
> + }
> + }
> +
> + if (callchain_bid) {
> + struct perf_callchain_build_id_entry *entry =
> + data->callchain_build_id;
> +
> + __output_copy(handle, &entry->nr_build_ids,
> + sizeof(u64) + entry->build_ids_len);
> + if (entry->nr_offsets) {
> + __output_copy(handle, entry->offsets,
> + entry->nr_offsets * sizeof(u64));
> + }
> + }
> +}
[Severity: Medium]
Can a nested perf event overwrite data->callchain_build_id during
stack_map_get_build_id_offset() here?
When callchain->nr > 0 but nr_bpf_bids == 0 (for example, when
exclude_callchain_user is set or no user frames follow PERF_CONTEXT_USER),
perf_callchain_build_id() skips resolving ip, leaves entry->has_ip_bid
false, and releases callchain_recursion[rctx] before returning the per-CPU
entry:
kernel/events/callchain.c:perf_callchain_build_id() {
...
if (nr_bpf_bids > 0) {
if (resolve_ip) {
bpf_bids[0].ip = ip;
ip_slot = 1;
}
...
perf_callchain_encode_build_ids(entry, callchain->ip, nr, user_start,
bpf_bids + ip_slot, nr_bpf_bids,
max_size);
put_callchain_entry(rctx);
return entry;
}
If the sampled IP was in user mode, perf_output_sample_build_id() then
falls back to calling stack_map_get_build_id_offset() after
perf_output_begin() has already reserved header->size bytes and while
callchain_recursion[rctx] is no longer held:
__perf_event_output()
perf_prepare_sample()
perf_callchain_build_id()
get_callchain_build_id_entry()
/* nr_bpf_bids == 0: entry->has_ip_bid remains false */
put_callchain_entry(rctx)
perf_output_begin() /* reserves header->size bytes */
perf_output_sample()
perf_output_sample_build_id()
stack_map_get_build_id_offset()
mmap_read_trylock()
<tracepoint / kprobe perf event at same context level>
perf_prepare_sample()
perf_callchain_build_id()
/* reuses same per-CPU entry and increases
* entry->build_ids_len / entry->nr_offsets
*/
__output_copy(..., entry->build_ids_len) <-- writes past reserved size
Resolving ip in perf_callchain_build_id() whenever resolve_ip &&
has_user_ctx (even if nr_bpf_bids == 0, or when !callchain->nr) would avoid
calling stack_map_get_build_id_offset() during output while the per-CPU
entry is unprotected.
[ ... ]
> @@ -8963,6 +9052,24 @@ void perf_prepare_sample(struct perf_sample_data *data,
> data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE;
> }
>
> + if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
> + u32 header_size = perf_sample_data_size(data, event);
> + u32 max_size = 0;
> + struct perf_callchain_build_id_entry *entry;
> +
> + if (header_size < U16_MAX)
> + max_size = round_down(U16_MAX - header_size, sizeof(u64));
> +
> + entry = perf_callchain_build_id(event, data->callchain,
> + data->ip,
> + !!(sample_type & PERF_SAMPLE_BUILD_ID_OFFSET),
> + max_size);
[Severity: Low]
Does this pass resolve_ip = true for kernel-mode samples and read
data->ip when PERF_SAMPLE_IP was not prepared?
When a user task is sampled in kernel mode with both
PERF_SAMPLE_BUILD_ID_OFFSET and PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET
enabled, perf_callchain_build_id() looks up the kernel data->ip in
current->mm via stack_map_get_build_id_offset(), even though
perf_output_sample_build_id() checks is_user and discards entry->ip_bid.
Also, when PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET is used without
PERF_SAMPLE_IP, PERF_SAMPLE_CODE_PAGE_SIZE, or PERF_SAMPLE_BUILD_ID_OFFSET,
data->ip is uninitialized on the stack when passed by value here. Finally,
perf_callchain_build_id() clamps nr_bpf_bids to max_size / sizeof(u64)
without subtracting user_start, so it can resolve up to user_start trailing
user frames that cannot fit in max_size.
> + data->callchain_build_id = entry;
> + data->dyn_size += entry->build_ids_len +
> + entry->nr_offsets * sizeof(u64);
> + data->sample_flags |= PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
> + }
> +
> if (filtered_sample_type & PERF_SAMPLE_AUX) {
> u64 size;
> u16 header_size = perf_sample_data_size(data, event);
[Severity: High]
Can header_size in the PERF_SAMPLE_AUX block wrap u16 to 0 when
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET is also enabled?
max_size above allows the sample up to the end of
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET to reach round_down(U16_MAX, 8)
(65528 bytes) without reserving the 8-byte aux_size field that
PERF_SAMPLE_AUX unconditionally emits. When perf_sample_data_size(data,
event) reaches 65528, the PERF_SAMPLE_AUX block's
u16 header_size = perf_sample_data_size(data, event);
header_size += sizeof(u64); /* size */
wraps header_size to 0. U16_MAX - header_size then becomes 65535,
data->dyn_size exceeds 64 KiB, and perf_prepare_header() truncates u16
header->size, causing perf_output_sample() to write 64 KiB more into the
perf ring buffer than perf_output_begin() reserved.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125005108.1020003-1-irogers@google.com?part=5
next prev parent reply other threads:[~2026-11-25 1:19 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 0:50 [PATCH v4 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
2026-11-25 0:50 ` [PATCH v4 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler() Ian Rogers
2026-11-25 0:50 ` [PATCH v4 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct Ian Rogers
2026-11-25 0:50 ` [PATCH v4 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI Ian Rogers
2026-11-25 0:50 ` [PATCH v4 04/19] bpf: Export stack_map_get_build_id_offset() for perf events Ian Rogers
2026-11-25 0:50 ` [PATCH v4 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types Ian Rogers
2026-11-25 1:19 ` sashiko-bot [this message]
2026-11-25 0:50 ` [PATCH v4 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET " Ian Rogers
2026-11-25 0:50 ` [PATCH v4 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr Ian Rogers
2026-11-25 0:50 ` [PATCH v4 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples Ian Rogers
2026-11-25 1:12 ` sashiko-bot
2026-11-25 0:50 ` [PATCH v4 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample() Ian Rogers
2026-11-25 0:50 ` [PATCH v4 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id Ian Rogers
2026-11-25 0:51 ` [PATCH v4 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET Ian Rogers
2026-11-25 0:51 ` [PATCH v4 12/19] perf: Refactor thread map and symbol APIs to take perf_sample Ian Rogers
2026-11-25 0:51 ` [PATCH v4 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id() Ian Rogers
2026-11-25 0:51 ` [PATCH v4 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types Ian Rogers
2026-11-25 0:51 ` [PATCH v4 15/19] perf symbol: Resolve symbols and callchains using build ID and offset Ian Rogers
2026-11-25 0:51 ` [PATCH v4 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset Ian Rogers
2026-11-25 1:18 ` sashiko-bot
2026-11-25 0:51 ` [PATCH v4 17/19] perf record: Add --buildid-offset option Ian Rogers
2026-11-25 1:19 ` sashiko-bot
2026-11-25 0:51 ` [PATCH v4 18/19] perf test: Add BUILD_ID_OFFSET C unit tests Ian Rogers
2026-11-25 0:51 ` [PATCH v4 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests Ian Rogers
2026-11-25 1:19 ` [PATCH v4 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-232323@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=irogers@google.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox