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

From: Kees Cook
Date: Wed May 17 2023 - 11:39:02 EST


On May 17, 2023 1:25:08 AM PDT, Christian Brauner <brauner@xxxxxxxxxx> wrote:
>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];

I was *just* looking at this fake flex array during LSS last week. It was one of two core structs still using the ancient 1-element style.

> };
>
> 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.

Yes, I think it's due to:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=for-next/hardening&id=2d47c6956ab3c8b580a59d7704aab3e2a4882b6c

This makes the sanitizer treat only [0]-arrays as flex arrays.

Though I wonder why Clang hasn't warned about this yet.

Regardless, we'll need to fix struct pid. Since it uses a static initializer for "numbers[0]", this will need a bit of a tweak, but I've got patches for this. I hadn't sent them yet because I was still studying the use of the "levels" member which is off by one for the count of "numbers" elements, which some code already has to work around (using "<=" when iterating and "+ 1" for some outputs)...



--
Kees Cook