From: sashiko-bot@kernel.org
To: "Jiri Olsa" <jolsa@kernel.org>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCHv3 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack
Date: Mon, 03 Aug 2026 21:30:50 +0000 [thread overview]
Message-ID: <20260803213050.8EF301F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260803210149.296496-7-jolsa@kernel.org>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] A Time-of-Check to Time-of-Use (TOCTOU) data race exists in `__bpf_get_stack` when used from a sleepable BPF program, allowing concurrent modification of `trace->nr` that leads to a massive Out-of-Bounds (OOB) memory read and write.
--
--- Patch [1]: [PATCHv3 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack ---
commit 1569be7d337e247b4d7eb8620dff965cc57a3fc6
Author: Jiri Olsa <jolsa@kernel.org>
bpf: Factor callchain_store function from __bpf_get_stack
This commit factors out the callchain storage logic into a new
callchain_store() function. It handles extracting both just-ip and buildid
data into a user-supplied buffer.
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 463f94ba1cc47..e51e03c4b60ed 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 unprotected re-read of
trace->nr lead to an integer underflow and out-of-bounds memory access?
If __bpf_get_stack() is called from a sleepable BPF program, preemption
is enabled. Between the bounds check in __bpf_get_stack() and this read,
the task could be preempted. If a preempting task running on the same CPU
executes a BPF program and reallocates the identical per-CPU buffer, it
might overwrite trace->nr with a smaller value.
When the sleepable task resumes, this re-read of trace->nr could result in
a value smaller than skip, causing trace_nr to underflow and resulting in
a massive copy_len during the memcpy() below.
> + 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);
> + }
> + return trace_nr;
> +}
> +
> static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
[ ... ]
> @@ -737,21 +759,10 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
> goto err_fault;
> }
>
> - trace_nr = trace->nr - skip;
> + trace_nr = callchain_store(trace, buf, elem_size, flags);
[Severity: Critical]
This isn't a bug introduced by this patch, but this is the preemption
window mentioned above.
The check if (unlikely(!trace) || trace->nr < skip) happens right before
this call. For sleepable BPF programs, preemption can occur between the
bounds check and the re-reading of trace->nr inside callchain_store().
Could we cache the validated trace->nr value (e.g., using READ_ONCE())
and pass it to callchain_store() to avoid this Time-of-Check to
Time-of-Use race?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260803210149.296496-7-jolsa@kernel.org?part=1
next prev parent reply other threads:[~2026-08-03 21:30 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 21:01 [PATCHv3 bpf-next 00/12] bpf: Disable preemption in stack map code Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 01/12] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 02/12] bpf: Factor stackid_fastpath " Jiri Olsa
2026-08-03 22:21 ` bot+bpf-ci
2026-08-03 21:01 ` [PATCHv3 bpf-next 03/12] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 04/12] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 05/12] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 06/12] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-08-03 21:30 ` sashiko-bot [this message]
2026-08-04 12:02 ` Jiri Olsa
2026-08-03 22:06 ` bot+bpf-ci
2026-08-03 21:01 ` [PATCHv3 bpf-next 07/12] bpf: Factor callchain_finalize " Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 08/12] bpf: Remove trace_in argument " Jiri Olsa
2026-08-03 22:21 ` bot+bpf-ci
2026-08-04 20:05 ` Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 09/12] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 10/12] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 11/12] bpf: Avoid changing callchain in bpf_get_stack_pe Jiri Olsa
2026-08-03 21:01 ` [PATCHv3 bpf-next 12/12] bpf: Avoid changing callchain in bpf_get_stackid_pe Jiri Olsa
2026-08-03 22:06 ` bot+bpf-ci
2026-08-04 20:05 ` 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=20260803213050.8EF301F00A3A@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=jolsa@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