From: Song Liu <song@kernel.org>
To: Arnaud Lecomte <contact@arnaud-lcm.com>,
alexei.starovoitov@gmail.com, yonghong.song@linux.dev
Cc: andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org,
daniel@iogearbox.net, eddyz87@gmail.com, haoluo@google.com,
john.fastabend@gmail.com, jolsa@kernel.org, kpsingh@kernel.org,
linux-kernel@vger.kernel.org, martin.lau@linux.dev,
sdf@fomichev.me,
syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com,
syzkaller-bugs@googlegroups.com
Subject: Re: [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
Date: Thu, 4 Sep 2025 15:45:30 -0700 [thread overview]
Message-ID: <f6e9710a-a5bf-4af9-8c0d-d81d28c3040c@kernel.org> (raw)
In-Reply-To: <20250903234325.30212-1-contact@arnaud-lcm.com>
On 9/3/25 4:43 PM, Arnaud Lecomte wrote:
> Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stackid()
> when copying stack trace data. The issue occurs when the perf trace
> contains more stack entries than the stack map bucket can hold,
> leading to an out-of-bounds write in the bucket's data array.
>
> Changes in v2:
> - Fixed max_depth names across get stack id
>
> Changes in v4:
> - Removed unnecessary empty line in __bpf_get_stackid
>
> Changs in v6:
> - Added back trace_len computation in __bpf_get_stackid
>
> Link to v6: https://lore.kernel.org/all/20250903135348.97884-1-contact@arnaud-lcm.com/
>
> Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
> Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
For future patches, please keep the "Changes in vX.." at the end of
your commit log and after a "---". IOW, something like
Acked-by: Yonghong Song <yonghong.song@linux.dev>
---
changes in v2:
...
---
kernel/bpf/stackmap.c | 8 ++++++++
In this way, the "changes in vXX" part will be removed by git-am.
> ---
> kernel/bpf/stackmap.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 9f3ae426ddc3..29e05c9ff1bd 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -369,6 +369,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
> {
> struct perf_event *event = ctx->event;
> struct perf_callchain_entry *trace;
> + u32 elem_size, max_depth;
> bool kernel, user;
> __u64 nr_kernel;
> int ret;
> @@ -390,11 +391,15 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
> return -EFAULT;
>
> nr_kernel = count_kernel_ip(trace);
> + elem_size = stack_map_data_size(map);
>
> if (kernel) {
> __u64 nr = trace->nr;
>
> trace->nr = nr_kernel;
this trace->nr = is useless.
> + max_depth =
> + stack_map_calculate_max_depth(map->value_size, elem_size, flags);
> + trace->nr = min_t(u32, nr_kernel, max_depth);
> ret = __bpf_get_stackid(map, trace, flags);
>
> /* restore nr */
> @@ -407,6 +412,9 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
> return -EFAULT;
>
> flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
> + max_depth =
> + stack_map_calculate_max_depth(map->value_size, elem_size, flags);
> + trace->nr = min_t(u32, trace->nr, max_depth);
> ret = __bpf_get_stackid(map, trace, flags);
I missed this part earlier. Here we need to restore trace->nr, just like
we did
in the "if (kernel)" branch.
Thanks,
Song
> }
> return ret;
next prev parent reply other threads:[~2025-09-04 22:45 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-03 23:39 [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud Lecomte
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
2025-09-03 23:43 ` [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
2025-09-04 22:45 ` Song Liu [this message]
2025-09-04 22:53 ` Lecomte, Arnaud
2025-09-04 22:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Song Liu
2025-09-04 22:52 ` Lecomte, Arnaud
2025-09-04 22:41 ` [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Song Liu
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=f6e9710a-a5bf-4af9-8c0d-d81d28c3040c@kernel.org \
--to=song@kernel.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=contact@arnaud-lcm.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com \
--cc=syzkaller-bugs@googlegroups.com \
--cc=yonghong.song@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