* [PATCH] bpf: Fix integer overflow in prealloc_elems_and_freelist()
@ 2021-09-25 5:31 Tatsuhiko Yasumatsu
2021-09-28 12:29 ` Daniel Borkmann
0 siblings, 1 reply; 3+ messages in thread
From: Tatsuhiko Yasumatsu @ 2021-09-25 5:31 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: th.yasumatsu, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, netdev, bpf, linux-kernel
In prealloc_elems_and_freelist(), the multiplication to calculate the
size passed to bpf_map_area_alloc() could lead to an integer overflow.
As a result, out-of-bounds write could occur in pcpu_freelist_populate()
as reported by KASAN:
[...]
[ 16.968613] BUG: KASAN: slab-out-of-bounds in pcpu_freelist_populate+0xd9/0x100
[ 16.969408] Write of size 8 at addr ffff888104fc6ea0 by task crash/78
[ 16.970038]
[ 16.970195] CPU: 0 PID: 78 Comm: crash Not tainted 5.15.0-rc2+ #1
[ 16.970878] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
[ 16.972026] Call Trace:
[ 16.972306] dump_stack_lvl+0x34/0x44
[ 16.972687] print_address_description.constprop.0+0x21/0x140
[ 16.973297] ? pcpu_freelist_populate+0xd9/0x100
[ 16.973777] ? pcpu_freelist_populate+0xd9/0x100
[ 16.974257] kasan_report.cold+0x7f/0x11b
[ 16.974681] ? pcpu_freelist_populate+0xd9/0x100
[ 16.975190] pcpu_freelist_populate+0xd9/0x100
[ 16.975669] stack_map_alloc+0x209/0x2a0
[ 16.976106] __sys_bpf+0xd83/0x2ce0
[...]
The possibility of this overflow was originally discussed in [0], but
was overlooked.
Fix the integer overflow by casting one operand to u64.
[0] https://lore.kernel.org/bpf/728b238e-a481-eb50-98e9-b0f430ab01e7@gmail.com/
Fixes: 557c0c6e7df8 ("bpf: convert stackmap to pre-allocation")
Signed-off-by: Tatsuhiko Yasumatsu <th.yasumatsu@gmail.com>
---
kernel/bpf/stackmap.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 09a3fd97d329..8941dc83a769 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -66,7 +66,7 @@ static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
int err;
- smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
+ smap->elems = bpf_map_area_alloc((u64)elem_size * smap->map.max_entries,
smap->map.numa_node);
if (!smap->elems)
return -ENOMEM;
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] bpf: Fix integer overflow in prealloc_elems_and_freelist()
2021-09-25 5:31 [PATCH] bpf: Fix integer overflow in prealloc_elems_and_freelist() Tatsuhiko Yasumatsu
@ 2021-09-28 12:29 ` Daniel Borkmann
2021-09-30 12:14 ` Tatushiko Yasumatsu
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Borkmann @ 2021-09-28 12:29 UTC (permalink / raw)
To: Tatsuhiko Yasumatsu, Alexei Starovoitov, Andrii Nakryiko
Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, netdev, bpf, linux-kernel
On 9/25/21 7:31 AM, Tatsuhiko Yasumatsu wrote:
> In prealloc_elems_and_freelist(), the multiplication to calculate the
> size passed to bpf_map_area_alloc() could lead to an integer overflow.
> As a result, out-of-bounds write could occur in pcpu_freelist_populate()
> as reported by KASAN:
>
> [...]
> [ 16.968613] BUG: KASAN: slab-out-of-bounds in pcpu_freelist_populate+0xd9/0x100
> [ 16.969408] Write of size 8 at addr ffff888104fc6ea0 by task crash/78
> [ 16.970038]
> [ 16.970195] CPU: 0 PID: 78 Comm: crash Not tainted 5.15.0-rc2+ #1
> [ 16.970878] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
> [ 16.972026] Call Trace:
> [ 16.972306] dump_stack_lvl+0x34/0x44
> [ 16.972687] print_address_description.constprop.0+0x21/0x140
> [ 16.973297] ? pcpu_freelist_populate+0xd9/0x100
> [ 16.973777] ? pcpu_freelist_populate+0xd9/0x100
> [ 16.974257] kasan_report.cold+0x7f/0x11b
> [ 16.974681] ? pcpu_freelist_populate+0xd9/0x100
> [ 16.975190] pcpu_freelist_populate+0xd9/0x100
> [ 16.975669] stack_map_alloc+0x209/0x2a0
> [ 16.976106] __sys_bpf+0xd83/0x2ce0
> [...]
>
> The possibility of this overflow was originally discussed in [0], but
> was overlooked.
>
> Fix the integer overflow by casting one operand to u64.
>
> [0] https://lore.kernel.org/bpf/728b238e-a481-eb50-98e9-b0f430ab01e7@gmail.com/
>
> Fixes: 557c0c6e7df8 ("bpf: convert stackmap to pre-allocation")
> Signed-off-by: Tatsuhiko Yasumatsu <th.yasumatsu@gmail.com>
> ---
> kernel/bpf/stackmap.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 09a3fd97d329..8941dc83a769 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -66,7 +66,7 @@ static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
> u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
Thanks a lot for the fix, Tatsuhiko! Could we just change the above elem_size to u64 instead?
> int err;
>
> - smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
> + smap->elems = bpf_map_area_alloc((u64)elem_size * smap->map.max_entries,
> smap->map.numa_node);
> if (!smap->elems)
> return -ENOMEM;
>
Best,
Daniel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] bpf: Fix integer overflow in prealloc_elems_and_freelist()
2021-09-28 12:29 ` Daniel Borkmann
@ 2021-09-30 12:14 ` Tatushiko Yasumatsu
0 siblings, 0 replies; 3+ messages in thread
From: Tatushiko Yasumatsu @ 2021-09-30 12:14 UTC (permalink / raw)
To: Daniel Borkmann
Cc: Alexei Starovoitov, Andrii Nakryiko, Martin KaFai Lau, Song Liu,
Yonghong Song, John Fastabend, KP Singh, netdev, bpf,
linux-kernel, Tatushiko Yasumatsu
On Tue, Sep 28, 2021 at 02:29:43PM +0200, Daniel Borkmann wrote:
> On 9/25/21 7:31 AM, Tatsuhiko Yasumatsu wrote:
> > In prealloc_elems_and_freelist(), the multiplication to calculate the
> > size passed to bpf_map_area_alloc() could lead to an integer overflow.
> > As a result, out-of-bounds write could occur in pcpu_freelist_populate()
> > as reported by KASAN:
> >
> > [...]
> > [ 16.968613] BUG: KASAN: slab-out-of-bounds in pcpu_freelist_populate+0xd9/0x100
> > [ 16.969408] Write of size 8 at addr ffff888104fc6ea0 by task crash/78
> > [ 16.970038]
> > [ 16.970195] CPU: 0 PID: 78 Comm: crash Not tainted 5.15.0-rc2+ #1
> > [ 16.970878] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
> > [ 16.972026] Call Trace:
> > [ 16.972306] dump_stack_lvl+0x34/0x44
> > [ 16.972687] print_address_description.constprop.0+0x21/0x140
> > [ 16.973297] ? pcpu_freelist_populate+0xd9/0x100
> > [ 16.973777] ? pcpu_freelist_populate+0xd9/0x100
> > [ 16.974257] kasan_report.cold+0x7f/0x11b
> > [ 16.974681] ? pcpu_freelist_populate+0xd9/0x100
> > [ 16.975190] pcpu_freelist_populate+0xd9/0x100
> > [ 16.975669] stack_map_alloc+0x209/0x2a0
> > [ 16.976106] __sys_bpf+0xd83/0x2ce0
> > [...]
> >
> > The possibility of this overflow was originally discussed in [0], but
> > was overlooked.
> >
> > Fix the integer overflow by casting one operand to u64.
> >
> > [0] https://lore.kernel.org/bpf/728b238e-a481-eb50-98e9-b0f430ab01e7@gmail.com/
> >
> > Fixes: 557c0c6e7df8 ("bpf: convert stackmap to pre-allocation")
> > Signed-off-by: Tatsuhiko Yasumatsu <th.yasumatsu@gmail.com>
> > ---
> > kernel/bpf/stackmap.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> > index 09a3fd97d329..8941dc83a769 100644
> > --- a/kernel/bpf/stackmap.c
> > +++ b/kernel/bpf/stackmap.c
> > @@ -66,7 +66,7 @@ static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
> > u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
>
> Thanks a lot for the fix, Tatsuhiko! Could we just change the above elem_size to u64 instead?
Thank you for your review, Daniel!
Yes, I think it's possible to just change elem_size to u64.
We just have to be careful to cast one operand (smap->map.value_size)
to u64, so that the integer overflow won't happen in 32-bit
architectures.
This is necessary because in 32-bit architectures, the result of
sizeof() is a 32-bit integer.
I will update the patch.
>
> > int err;
> > - smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
> > + smap->elems = bpf_map_area_alloc((u64)elem_size * smap->map.max_entries,
> > smap->map.numa_node);
> > if (!smap->elems)
> > return -ENOMEM;
> >
>
> Best,
> Daniel
Best regards,
Tatsuhiko Yasumatsu
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-09-30 12:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-25 5:31 [PATCH] bpf: Fix integer overflow in prealloc_elems_and_freelist() Tatsuhiko Yasumatsu
2021-09-28 12:29 ` Daniel Borkmann
2021-09-30 12:14 ` Tatushiko Yasumatsu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).