Re: [syzbot] [kernel?] linux-next test error: UBSAN: array-index-out-of-bounds in alloc_pid

From: Christian Brauner
Date: Wed May 17 2023 - 04:25:18 EST


On Wed, May 17, 2023 at 12:40:03AM -0700, syzbot wrote:
> Hello,
>
> syzbot found the following issue on:
>
> HEAD commit: 065efa589871 Add linux-next specific files for 20230517
> git tree: linux-next
> console output: https://syzkaller.appspot.com/x/log.txt?x=17f27bb2280000
> kernel config: https://syzkaller.appspot.com/x/.config?x=821eeb02ef201bcc
> dashboard link: https://syzkaller.appspot.com/bug?extid=ac3b41786a2d0565b6d5
> compiler: gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2
>
> Downloadable assets:
> disk image: https://storage.googleapis.com/syzbot-assets/dbbd691e9e5a/disk-065efa58.raw.xz
> vmlinux: https://storage.googleapis.com/syzbot-assets/e5b9541c3979/vmlinux-065efa58.xz
> kernel image: https://storage.googleapis.com/syzbot-assets/44cf3f3aaabb/bzImage-065efa58.xz
>
> IMPORTANT: if you fix the issue, please add the following tag to the commit:
> Reported-by: syzbot+ac3b41786a2d0565b6d5@xxxxxxxxxxxxxxxxxxxxxxxxx
>
> ================================================================================
> UBSAN: array-index-out-of-bounds in kernel/pid.c:244:15

Only way I see this happening is if the logic in
kernel/pid_namespace.c:create_pid_cachep() which sets the object size
for the struct pid allocation of this pid namespace based on
parent_pid_namespace->level + 1 is broken. The way this works is:

struct pid
{
[snip]
struct upid numbers[1];
};

create_pid_namespace()
{
unsigned int level = parent_pid_ns->level + 1;
ns->pid_cachep = create_pid_cachep(level);
}

and then during fork:

alloc_pid()
{
pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
}

So effectively, the wrong level must've been set in
create_pid_namespace() so that the flexible array allocation is too
small.

I don't have time to debug this tbh. Ccing Kees maybe there's some
flexible array stuff going on I'm unaware of.