From: Jiri Olsa <olsajiri@gmail.com>
To: sashiko-reviews@lists.linux.dev
Cc: bpf@vger.kernel.org
Subject: Re: [PATCHv2 bpf-next 06/11] bpf: Factor callchain_store function from __bpf_get_stack
Date: Thu, 30 Jul 2026 14:04:37 +0200 [thread overview]
Message-ID: <ams91eMxUuXsJd7P@krava> (raw)
In-Reply-To: <20260729085751.96C341F000E9@smtp.kernel.org>
On Wed, Jul 29, 2026 at 08:57:51AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>
> Pre-existing issues:
> - [Critical] Preemption can cause `trace->nr` to be modified concurrently, leading to an integer underflow in `trace_nr = trace->nr - skip` and a massive out-of-bounds `memcpy`.
> --
>
> --- Patch [1]: [PATCHv2 bpf-next 06/11] bpf: Factor callchain_store function from __bpf_get_stack ---
> commit 41f5133836d848295ac407c006940d4f0641b50f
> Author: Jiri Olsa <jolsa@kernel.org>
>
> bpf: Factor callchain_store function from __bpf_get_stack
>
> This commit factors out the callchain storage logic from __bpf_get_stack
> into a new callchain_store function. The new function handles both IP and
> build ID data copying.
>
> > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> > index 41fe87d7302f2..18d1a10bf76e5 100644
> > --- a/kernel/bpf/stackmap.c
> > +++ b/kernel/bpf/stackmap.c
> > @@ -680,6 +680,29 @@ const struct bpf_func_proto bpf_get_stackid_proto_pe = {
> > .arg3_type = ARG_ANYTHING,
> > };
> >
> > +static u32 callchain_store(struct perf_callchain_entry *trace, void *buf,
> > + u32 elem_size, u64 flags)
> > +{
> > + bool user_build_id = flags & BPF_F_USER_BUILD_ID;
> > + u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
> > + u32 trace_nr, copy_len;
> > + u64 *ips;
> > +
> > + trace_nr = trace->nr - skip;
>
> [Severity: Critical]
> This is a pre-existing issue, but could this calculation underflow if
> trace->nr is modified concurrently?
>
> Looking at __bpf_get_stack(), when called from a sleepable BPF program via
> bpf_get_stack_sleepable(), it calls get_perf_callchain(). This allocates
> a per-CPU recursion buffer but unpins it by calling put_callchain_entry()
> before returning.
>
> __bpf_get_stack() then checks if trace->nr is less than skip:
>
> if (unlikely(!trace) || trace->nr < skip) {
> if (may_fault)
> rcu_read_unlock();
> goto err_fault;
> }
>
> If the task is preempted immediately after this check (which is possible for
> sleepable BPF programs under CONFIG_PREEMPT_RCU, as rcu_read_lock() does not
> prevent preemption), could a new task on the same CPU trigger a BPF program
> that reacquires the same level-0 recursion buffer and overwrites trace->nr
> to 0?
>
> When the original task resumes and calls callchain_store(), it re-reads
> trace->nr. If it is now 0, wouldn't 0 - skip underflow to a massive unsigned
> value?
>
> > + copy_len = trace_nr * elem_size;
> > +
> > + ips = trace->ip + skip;
> > + if (user_build_id) {
> > + struct bpf_stack_build_id *id_offs = buf;
> > +
> > + for (u32 i = 0; i < trace_nr; i++)
> > + id_offs[i].ip = ips[i];
> > + } else {
> > + memcpy(buf, ips, copy_len);
>
> If the underflow occurs, would this result in a massive out-of-bounds memcpy
> that could overwrite the BPF stack buffer or map value and cause memory
> corruption?
the preemption fix comes later in the patchset
jirka
next prev parent reply other threads:[~2026-07-30 12:04 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 8:37 [PATCHv2 bpf-next 00/11] bpf: Disable preemption in stack map code Jiri Olsa
2026-07-29 8:37 ` [PATCHv2 bpf-next 01/11] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-07-29 8:51 ` sashiko-bot
2026-07-30 12:04 ` Jiri Olsa
2026-07-29 8:37 ` [PATCHv2 bpf-next 02/11] bpf: Factor stackid_fastpath " Jiri Olsa
2026-07-29 8:37 ` [PATCHv2 bpf-next 03/11] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-07-29 8:38 ` [PATCHv2 bpf-next 04/11] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-07-29 9:42 ` bot+bpf-ci
2026-07-29 8:38 ` [PATCHv2 bpf-next 05/11] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-07-29 10:30 ` Leon Hwang
2026-07-30 11:38 ` Jiri Olsa
2026-07-29 8:38 ` [PATCHv2 bpf-next 06/11] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-07-29 8:57 ` sashiko-bot
2026-07-30 12:04 ` Jiri Olsa [this message]
2026-07-29 8:38 ` [PATCHv2 bpf-next 07/11] bpf: Factor callchain_finalize " Jiri Olsa
2026-07-29 9:41 ` bot+bpf-ci
2026-07-29 8:38 ` [PATCHv2 bpf-next 08/11] bpf: Restore trace->nr value properly in bpf_get_stack_pe Jiri Olsa
2026-07-29 8:38 ` [PATCHv2 bpf-next 09/11] bpf: Remove trace_in argument from __bpf_get_stack Jiri Olsa
2026-07-29 9:58 ` bot+bpf-ci
2026-07-29 8:38 ` [PATCHv2 bpf-next 10/11] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-07-29 8:38 ` [PATCHv2 bpf-next 11/11] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-07-29 9:57 ` bot+bpf-ci
2026-07-30 12:04 ` Jiri Olsa
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=ams91eMxUuXsJd7P@krava \
--to=olsajiri@gmail.com \
--cc=bpf@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