lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAEf4BzaoCOUofb8xzpuxT0Lg+XwdLiW1uEZURqhy_mZzigx63w@mail.gmail.com>
Date: Mon, 22 Sep 2025 15:38:07 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Arnaud Lecomte <contact@...aud-lcm.com>
Cc: alexei.starovoitov@...il.com, yonghong.song@...ux.dev, song@...nel.org, 
	andrii@...nel.org, ast@...nel.org, bpf@...r.kernel.org, daniel@...earbox.net, 
	eddyz87@...il.com, haoluo@...gle.com, john.fastabend@...il.com, 
	jolsa@...nel.org, kpsingh@...nel.org, linux-kernel@...r.kernel.org, 
	martin.lau@...ux.dev, sdf@...ichev.me, 
	syzbot+c9b724fbb41cf2538b7b@...kaller.appspotmail.com, 
	syzkaller-bugs@...glegroups.com
Subject: Re: [PATCH bpf-next v9 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()

On Fri, Sep 12, 2025 at 4:36 PM Arnaud Lecomte <contact@...aud-lcm.com> 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.
>
> Reported-by: syzbot+c9b724fbb41cf2538b7b@...kaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
> Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
> Acked-by: Yonghong Song <yonghong.song@...ux.dev>
> Acked-by: Song Liu <song@...nel.org>
> Signed-off-by: Arnaud Lecomte <contact@...aud-lcm.com>
> ---
> Changes in v2:
>  - Fixed max_depth names across get stack id
>
> Changes in v4:
>  - Removed unnecessary empty line in __bpf_get_stackid
>
> Changes in v6:
>  - Added back trace_len computation in __bpf_get_stackid
>
> Changes in v7:
>  - Removed usefull trace->nr assignation in bpf_get_stackid_pe
>  - Added restoration of trace->nr for both kernel and user traces
>    in bpf_get_stackid_pe
>
> Changes in v9:
>  - Fixed variable declarations in bpf_get_stackid_pe
>  - Added the missing truncate of trace_nr in __bpf_getstackid
>
> Link to v8: https://lore.kernel.org/all/20250905134833.26791-1-contact@arnaud-lcm.com/
> ---
> ---
>  kernel/bpf/stackmap.c | 21 ++++++++++++++++-----
>  1 file changed, 16 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 9a86b5acac10..ac5ec3253ce6 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -251,8 +251,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
>  {
>         struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
>         struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
> +       u32 hash, id, trace_nr, trace_len, i, max_storable;
>         u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
> -       u32 hash, id, trace_nr, trace_len, i;
>         bool user = flags & BPF_F_USER_STACK;
>         u64 *ips;
>         bool hash_matches;
> @@ -261,7 +261,9 @@ static long __bpf_get_stackid(struct bpf_map *map,
>                 /* skipping more than usable stack trace */
>                 return -EFAULT;
>
> +       max_storable = map->value_size / stack_map_data_size(map);
>         trace_nr = trace->nr - skip;
> +       trace_nr = min_t(u32, trace_nr, max_storable);
>         trace_len = trace_nr * sizeof(u64);
>         ips = trace->ip + skip;
>         hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
> @@ -369,6 +371,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,15 +393,16 @@ 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);
> +       __u64 nr = trace->nr; /* save original */
>
>         if (kernel) {
> -               __u64 nr = trace->nr;
> -
>                 trace->nr = nr_kernel;

unnecessary, you'll be overriding it two lines below

> +               max_depth =
> +                       stack_map_calculate_max_depth(map->value_size, elem_size, flags);

here and below, keep on the same line, it's under 100 characters

> +               trace->nr = min_t(u32, nr_kernel, max_depth);
>                 ret = __bpf_get_stackid(map, trace, flags);
>
> -               /* restore nr */
> -               trace->nr = nr;
>         } else { /* user */
>                 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
>
> @@ -407,8 +411,15 @@ 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);
>         }
> +
> +       /* restore nr */
> +       trace->nr = nr;
> +
>         return ret;
>  }
>
> --
> 2.43.0
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ