Re: usb/gadget: warning in dev_config/memdup_user

From: Greg Kroah-Hartman
Date: Fri Dec 02 2016 - 11:06:52 EST


On Fri, Dec 02, 2016 at 04:56:15PM +0100, Greg Kroah-Hartman wrote:
> On Fri, Dec 02, 2016 at 04:07:38PM +0100, Andrey Konovalov wrote:
> > Hi!
> >
> > I've got the following error report while running the syzkaller fuzzer.
> >
> > The length passed to memdup_user() directly without limitations.
> >
> > On commit 2caceb3294a78c389b462e7e236a4e744a53a474 (Dec 1).
> >
> > WARNING: CPU: 3 PID: 14477 at mm/page_alloc.c:3511
> > __alloc_pages_nodemask+0x159c/0x1e20
> > Kernel panic - not syncing: panic_on_warn set ...
> >
> > CPU: 3 PID: 14477 Comm: syz-executor0 Not tainted 4.9.0-rc7+ #61
> > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
> > ffff880039b67298 ffffffff81f96bba ffffffff00000200 1ffff1000736cde6
> > ffffed000736cdde 0000000000000a06 0000000041b58ab3 ffffffff8598af00
> > ffffffff81f96928 0000000041b58ab3 ffffffff859423c8 ffffffff81432790
> > Call Trace:
> > [< inline >] __dump_stack lib/dump_stack.c:15
> > [<ffffffff81f96bba>] dump_stack+0x292/0x398 lib/dump_stack.c:51
> > [<ffffffff8168c7ae>] panic+0x1cb/0x3a9 kernel/panic.c:179
> > [<ffffffff812b80b4>] __warn+0x1c4/0x1e0 kernel/panic.c:542
> > [<ffffffff812b831c>] warn_slowpath_null+0x2c/0x40 kernel/panic.c:585
> > [< inline >] __alloc_pages_slowpath mm/page_alloc.c:3511
> > [<ffffffff816c07cc>] __alloc_pages_nodemask+0x159c/0x1e20 mm/page_alloc.c:3781
> > [<ffffffff817cdd27>] alloc_pages_current+0x1c7/0x6b0 mm/mempolicy.c:2072
> > [< inline >] alloc_pages include/linux/gfp.h:469
> > [<ffffffff8172fcdf>] kmalloc_order+0x1f/0x70 mm/slab_common.c:1015
> > [<ffffffff8172fd4f>] kmalloc_order_trace+0x1f/0x160 mm/slab_common.c:1026
> > [< inline >] kmalloc_large include/linux/slab.h:422
> > [<ffffffff817e348f>] __kmalloc_track_caller+0x1df/0x290 mm/slub.c:4233
> > [<ffffffff8171bb3c>] memdup_user+0x2c/0xa0 mm/util.c:137
> > [<ffffffff83228ca2>] dev_config+0x2e2/0x1180
> > drivers/usb/gadget/legacy/inode.c:1776
> > [<ffffffff817fde55>] __vfs_write+0x5d5/0x760 fs/read_write.c:510
> > [<ffffffff817ff750>] vfs_write+0x170/0x4e0 fs/read_write.c:560
> > [< inline >] SYSC_write fs/read_write.c:607
> > [<ffffffff81803b5b>] SyS_write+0xfb/0x230 fs/read_write.c:599
> > [<ffffffff84f44cc1>] entry_SYSCALL_64_fastpath+0x1f/0xc2
> > Dumping ftrace buffer:
> > (ftrace buffer empty)
> > Kernel Offset: disabled
>
> Oh how nice, we check to ensure that the buffer is not too small, but no
> one checks if it is too big :(
>
> Felipe, would a patch like the one below solve this? If so, I'll resend
> it in a "proper" format...
>
> thanks,
>
> greg k-h
>
> ---------------------
>
> diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
> index bd82dd12deff..42f9003b1621 100644
> --- a/drivers/usb/gadget/legacy/inode.c
> +++ b/drivers/usb/gadget/legacy/inode.c
> @@ -1765,6 +1765,10 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
> if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
> return -EINVAL;
>
> + /* No one needs more than 64k... */
> + if (len > PAGE_SIZE * 8)
> + return -EINVAL;

That's what I get for trying to be cute with comments right after a long
lunch, the math is wrong... But the main concept should be sane.