From: Jiri Olsa <olsajiri@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
stable@vger.kernel.org, Tao Chen <chen.dylane@linux.dev>,
STAR Labs SG <info@starlabs.sg>,
bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
Quentin Monnet <qmo@kernel.org>,
Arnaud Lecomte <contact@arnaud-lcm.com>
Subject: Re: [PATCHv4 bpf-next 10/12] bpf: Disable preemption in __bpf_get_stack
Date: Wed, 5 Aug 2026 22:29:55 +0200 [thread overview]
Message-ID: <anOdQzvkS_JZG2Op@krava> (raw)
In-Reply-To: <CAEf4BzZQXq5CNSkD4-gMOhs5+GU9Sf9Q7sHvLu1BkpqDTQHa5g@mail.gmail.com>
On Wed, Aug 05, 2026 at 11:34:58AM -0700, Andrii Nakryiko wrote:
> On Wed, Aug 5, 2026 at 2:30 AM Jiri Olsa <jolsa@kernel.org> wrote:
> >
> > From: Daniel Borkmann <daniel@iogearbox.net>
> >
> > get_perf_callchain() returns a per-CPU perf_callchain_entry buffer and
> > releases its recursion slot via put_callchain_entry() before returning,
> > so nothing keeps the entry reserved while __bpf_get_stack() consumes
> > it below.
> >
> > A preemptible BPF program (e.g. a non-sleepable raw tracepoint program
> > on a PREEMPT kernel, which runs under migrate_disable() but not
> > preempt_disable()) can be scheduled out between obtaining the entry
> > and the copy. Another task scheduled on the same CPU then reuses the
> > same per-CPU buffer and overwrites trace->nr with a larger value.
> > copy_len is then computed from the inflated trace->nr and can exceed
> > the caller's buffer, causing an out-of-bounds write in the memcpy()
> > and in the build_id path.
> >
> > The rcu_read_lock() taken here alone does not prevent this. It is
> > only taken on the may_fault path, and under CONFIG_PREEMPT_RCU it does
> > not disable preemption; it merely keeps perf's callchain buffer array
> > alive (freed via call_rcu()) and does nothing to stop another task
> > from reusing the entry.
> >
> > Disable preemption around obtaining the callchain entry and copying
> > it into the caller's buffer, so the entry cannot be reused underneath
> > us and trace->nr stays bounded by max_depth. Build ID resolution may
> > fault and is therefore deferred until after preemption is re-enabled;
> > by then the instruction pointers have already been copied into buf,
> > so it operates only on that private copy. Note, preempt_disable() also
> > subsumes the buffer-lifetime guarantee the rcu_read_lock() provided,
> > since a preempt-disabled section is an RCU read-side critical section
> > for the callchain buffers' call_rcu() reclaim.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: c195651e565a ("bpf: add bpf_get_stack helper")
> > Reported-by: Tao Chen <chen.dylane@linux.dev>
> > Closes: https://lore.kernel.org/bpf/20260206090653.1336687-1-chen.dylane@linux.dev/
> > Reported-by: STAR Labs SG <info@starlabs.sg>
> > Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> > [ changed Fixes: commit ]
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> > kernel/bpf/stackmap.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> > index eabeaef31b63..789fe35b893a 100644
> > --- a/kernel/bpf/stackmap.c
> > +++ b/kernel/bpf/stackmap.c
> > @@ -817,6 +817,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
> >
> > if (may_fault)
> > rcu_read_lock(); /* need RCU for perf's callchain below */
> > + preempt_disable();
>
> nit: asymmetrical to preempt_enable, I'll move it to before
> rcu_read_lock, so we can have proper nesting
ugh, nice.. thanks
jirka
>
>
>
> >
> > if (kernel && task) {
> > trace = get_callchain_entry_for_task(task, max_depth);
> > @@ -828,6 +829,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
> > if (unlikely(!trace) || trace->nr < skip) {
> > if (may_fault)
> > rcu_read_unlock();
> > + preempt_enable();
> > goto err_fault;
> > }
> >
> > @@ -836,6 +838,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
> > /* trace should not be dereferenced after this point */
> > if (may_fault)
> > rcu_read_unlock();
> > + preempt_enable();
> >
> > return callchain_finalize(buf, size, trace_nr, elem_size, flags, may_fault);
> >
> > --
> > 2.54.0
> >
next prev parent reply other threads:[~2026-08-05 20:29 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-05 9:28 [PATCHv4 bpf-next 00/12] bpf: Disable preemption in stack map code Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-08-05 9:40 ` sashiko-bot
2026-08-05 9:28 ` [PATCHv4 bpf-next 02/12] bpf: Factor stackid_fastpath " Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 03/12] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 04/12] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 05/12] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 07/12] bpf: Factor callchain_finalize " Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 08/12] bpf: Remove trace_in argument " Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 09/12] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 10/12] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-08-05 18:34 ` Andrii Nakryiko
2026-08-05 20:29 ` Jiri Olsa [this message]
2026-08-05 9:28 ` [PATCHv4 bpf-next 11/12] bpf: Avoid changing callchain in bpf_get_stack_pe Jiri Olsa
2026-08-05 9:28 ` [PATCHv4 bpf-next 12/12] bpf: Avoid changing callchain in bpf_get_stackid_pe Jiri Olsa
2026-08-05 18:36 ` [PATCHv4 bpf-next 00/12] bpf: Disable preemption in stack map code Andrii Nakryiko
2026-08-05 18:40 ` patchwork-bot+netdevbpf
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=anOdQzvkS_JZG2Op@krava \
--to=olsajiri@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chen.dylane@linux.dev \
--cc=contact@arnaud-lcm.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=info@starlabs.sg \
--cc=martin.lau@linux.dev \
--cc=qmo@kernel.org \
--cc=songliubraving@fb.com \
--cc=stable@vger.kernel.org \
--cc=yhs@fb.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.