BUG: bad usercopy in ld_usb_read

9 views
Skip to first unread message

syzbot

unread,
Aug 8, 2019, 8:38:07 AM8/8/19
to ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, gre...@linuxfoundation.org, kees...@chromium.org, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
Hello,

syzbot found the following crash on:

HEAD commit: e96407b4 usb-fuzzer: main usb gadget fuzzer driver
git tree: https://github.com/google/kasan.git usb-fuzzer
console output: https://syzkaller.appspot.com/x/log.txt?x=13aeaece600000
kernel config: https://syzkaller.appspot.com/x/.config?x=cfa2c18fb6a8068e
dashboard link: https://syzkaller.appspot.com/bug?extid=45b2f40f0778cfa7634e
compiler: gcc (GCC) 9.0.0 20181231 (experimental)

Unfortunately, I don't have any reproducer for this crash yet.

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+45b2f4...@syzkaller.appspotmail.com

ldusb 6-1:0.124: Read buffer overflow, -131383996186150 bytes dropped
usercopy: Kernel memory exposure attempt detected from SLUB
object 'kmalloc-2k' (offset 8, size 65062)!
------------[ cut here ]------------
kernel BUG at mm/usercopy.c:98!
invalid opcode: 0000 [#1] SMP KASAN
CPU: 0 PID: 15185 Comm: syz-executor.2 Not tainted 5.3.0-rc2+ #25
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS
Google 01/01/2011
RIP: 0010:usercopy_abort+0xb9/0xbb mm/usercopy.c:98
Code: e8 c1 f7 d6 ff 49 89 d9 4d 89 e8 4c 89 e1 41 56 48 89 ee 48 c7 c7 e0
f3 cd 85 ff 74 24 08 41 57 48 8b 54 24 20 e8 15 98 c1 ff <0f> 0b e8 95 f7
d6 ff e8 80 9f fd ff 8b 54 24 04 49 89 d8 4c 89 e1
RSP: 0018:ffff8881ccb3fc38 EFLAGS: 00010286
RAX: 0000000000000067 RBX: ffffffff86a659d4 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff8128a0fd RDI: ffffed1039967f79
RBP: ffffffff85cdf2c0 R08: 0000000000000067 R09: fffffbfff11acdaa
R10: fffffbfff11acda9 R11: ffffffff88d66d4f R12: ffffffff86a696e8
R13: ffffffff85cdf180 R14: 000000000000fe26 R15: ffffffff85cdf140
FS: 00007ff6daf91700(0000) GS:ffff8881db200000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f1de6600000 CR3: 00000001ca554000 CR4: 00000000001406f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
__check_heap_object+0xdd/0x110 mm/slub.c:3914
check_heap_object mm/usercopy.c:234 [inline]
__check_object_size mm/usercopy.c:280 [inline]
__check_object_size+0x32d/0x39b mm/usercopy.c:250
check_object_size include/linux/thread_info.h:119 [inline]
check_copy_size include/linux/thread_info.h:150 [inline]
copy_to_user include/linux/uaccess.h:151 [inline]
ld_usb_read+0x304/0x780 drivers/usb/misc/ldusb.c:495
__vfs_read+0x76/0x100 fs/read_write.c:425
vfs_read+0x1ea/0x430 fs/read_write.c:461
ksys_read+0x1e8/0x250 fs/read_write.c:587
do_syscall_64+0xb7/0x580 arch/x86/entry/common.c:296
entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x459829
Code: fd b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 48 89 f8 48 89 f7
48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff
ff 0f 83 cb b7 fb ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007ff6daf90c78 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 0000000000459829
RDX: 000000000000fe26 RSI: 00000000200000c0 RDI: 0000000000000003
RBP: 000000000075bf20 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 00007ff6daf916d4
R13: 00000000004c6c73 R14: 00000000004dbee8 R15: 00000000ffffffff
Modules linked in:
---[ end trace 4fe8dba032d24ceb ]---
RIP: 0010:usercopy_abort+0xb9/0xbb mm/usercopy.c:98
Code: e8 c1 f7 d6 ff 49 89 d9 4d 89 e8 4c 89 e1 41 56 48 89 ee 48 c7 c7 e0
f3 cd 85 ff 74 24 08 41 57 48 8b 54 24 20 e8 15 98 c1 ff <0f> 0b e8 95 f7
d6 ff e8 80 9f fd ff 8b 54 24 04 49 89 d8 4c 89 e1
RSP: 0018:ffff8881ccb3fc38 EFLAGS: 00010286
RAX: 0000000000000067 RBX: ffffffff86a659d4 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff8128a0fd RDI: ffffed1039967f79
RBP: ffffffff85cdf2c0 R08: 0000000000000067 R09: fffffbfff11acdaa
R10: fffffbfff11acda9 R11: ffffffff88d66d4f R12: ffffffff86a696e8
R13: ffffffff85cdf180 R14: 000000000000fe26 R15: ffffffff85cdf140
FS: 00007ff6daf91700(0000) GS:ffff8881db200000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f1de6600000 CR3: 00000001ca554000 CR4: 00000000001406f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400


---
This bug is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzk...@googlegroups.com.

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

Greg KH

unread,
Aug 8, 2019, 8:46:57 AM8/8/19
to syzbot, Michael Hund, ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, kees...@chromium.org, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
On Thu, Aug 08, 2019 at 05:38:06AM -0700, syzbot wrote:
> Hello,
>
> syzbot found the following crash on:
>
> HEAD commit: e96407b4 usb-fuzzer: main usb gadget fuzzer driver
> git tree: https://github.com/google/kasan.git usb-fuzzer
> console output: https://syzkaller.appspot.com/x/log.txt?x=13aeaece600000
> kernel config: https://syzkaller.appspot.com/x/.config?x=cfa2c18fb6a8068e
> dashboard link: https://syzkaller.appspot.com/bug?extid=45b2f40f0778cfa7634e
> compiler: gcc (GCC) 9.0.0 20181231 (experimental)
>
> Unfortunately, I don't have any reproducer for this crash yet.
>
> IMPORTANT: if you fix the bug, please add the following tag to the commit:
> Reported-by: syzbot+45b2f4...@syzkaller.appspotmail.com
>
> ldusb 6-1:0.124: Read buffer overflow, -131383996186150 bytes dropped

That's a funny number :)

Nice overflow found, I see you are now starting to fuzz the char device
nodes of usb drivers...

Michael, care to fix this up?

thanks,

greg k-h

Kees Cook

unread,
Aug 8, 2019, 7:06:35 PM8/8/19
to Greg KH, syzbot, Michael Hund, ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
This looks like the length in the read-from-device buffer is unchecked:

/* actual_buffer contains actual_length + interrupt_in_buffer */
actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
bytes_to_read = min(count, *actual_buffer);
if (bytes_to_read < *actual_buffer)
dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
*actual_buffer-bytes_to_read);

/* copy one interrupt_in_buffer from ring_buffer into userspace */
if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
retval = -EFAULT;
goto unlock_exit;
}

I assume what's stored at actual_buffer is bogus and needs validation
somewhere before it's actually used. (If not here, maybe where ever the
write into the buffer originally happens?)

--
Kees Cook

Greg KH

unread,
Aug 9, 2019, 5:18:22 AM8/9/19
to Kees Cook, syzbot, Michael Hund, ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
I think it should be verified here, as that's when it is parsed. The
data is written to the buffer in ld_usb_interrupt_in_callback() but it
does not "know" how to parse it at that location.

thanks,

greg k-h

Alan Stern

unread,
Aug 9, 2019, 11:13:03 AM8/9/19
to Greg KH, Kees Cook, syzbot, Michael Hund, ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
I looked at this bug report, and it is very puzzling.

Yes, the value stored in *actual_buffer is written in
ld_usb_interrupt_in_callback(), but the value is simply
urb->actual_length and therefore does not need any validation. The
URB's transfer_buffer_length is taken from
dev->interrupt_in_endpoint_size, which comes from usb_endpoint_maxp()
and therefore cannot be larger than 2048.

(On the other hand, actual_buffer might not be aligned on a 32-bit
address. For x86, of course, this doesn't matter, but it can affect
other architectures.)

Furthermore, the computation leading to the dev_warn() involves only
size_t types and therefore is carried out entirely using unsigned
arithmetic. The warning's format string uses %zd instead of %zu;
that's why the number showed up as negative but doesn't explain why it
looks so funny.

In fact, I don't see why any of the computations here should overflow
or wrap around, or even give rise to a negative value. If syzbot had a
reproducer we could get more debugging output -- but it doesn't.

Alan Stern

syzbot

unread,
Aug 10, 2019, 2:15:06 PM8/10/19
to ak...@linux-foundation.org, all...@lohutok.net, andre...@google.com, c...@lca.pw, gre...@linuxfoundation.org, kees...@chromium.org, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, mh...@ld-didactic.de, st...@rowland.harvard.edu, syzkall...@googlegroups.com, tg...@linutronix.de
syzbot has found a reproducer for the following crash on:

HEAD commit: e96407b4 usb-fuzzer: main usb gadget fuzzer driver
git tree: https://github.com/google/kasan.git usb-fuzzer
console output: https://syzkaller.appspot.com/x/log.txt?x=17cf0b16600000
kernel config: https://syzkaller.appspot.com/x/.config?x=cfa2c18fb6a8068e
dashboard link: https://syzkaller.appspot.com/bug?extid=45b2f40f0778cfa7634e
compiler: gcc (GCC) 9.0.0 20181231 (experimental)
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=151bab16600000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=148f8cd2600000

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+45b2f4...@syzkaller.appspotmail.com

ldusb 4-1:0.28: Read buffer overflow, -3222596215958809898 bytes dropped
usercopy: Kernel memory exposure attempt detected from process stack
(offset 0, size 2147479552)!
------------[ cut here ]------------
kernel BUG at mm/usercopy.c:98!
invalid opcode: 0000 [#1] SMP KASAN
CPU: 1 PID: 2023 Comm: syz-executor861 Not tainted 5.3.0-rc2+ #25
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS
Google 01/01/2011
RIP: 0010:usercopy_abort+0xb9/0xbb mm/usercopy.c:98
Code: e8 c1 f7 d6 ff 49 89 d9 4d 89 e8 4c 89 e1 41 56 48 89 ee 48 c7 c7 e0
f3 cd 85 ff 74 24 08 41 57 48 8b 54 24 20 e8 15 98 c1 ff <0f> 0b e8 95 f7
d6 ff e8 80 9f fd ff 8b 54 24 04 49 89 d8 4c 89 e1
RSP: 0018:ffff8881cbda7c40 EFLAGS: 00010282
RAX: 0000000000000061 RBX: ffffffff85cdf100 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff8128a0fd RDI: ffffed10397b4f7a
RBP: ffffffff85cdf2c0 R08: 0000000000000061 R09: fffffbfff11acda1
R10: fffffbfff11acda0 R11: ffffffff88d66d07 R12: ffffffff85cdf4e0
R13: ffffffff85cdf100 R14: 000000007ffff000 R15: ffffffff85cdf100
FS: 00007f10bb76a700(0000) GS:ffff8881db300000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7135a49000 CR3: 00000001d20e8000 CR4: 00000000001406e0
Call Trace:
__check_object_size mm/usercopy.c:276 [inline]
__check_object_size.cold+0x91/0xba mm/usercopy.c:250
check_object_size include/linux/thread_info.h:119 [inline]
check_copy_size include/linux/thread_info.h:150 [inline]
copy_to_user include/linux/uaccess.h:151 [inline]
ld_usb_read+0x304/0x780 drivers/usb/misc/ldusb.c:495
__vfs_read+0x76/0x100 fs/read_write.c:425
vfs_read+0x1ea/0x430 fs/read_write.c:461
ksys_read+0x1e8/0x250 fs/read_write.c:587
do_syscall_64+0xb7/0x580 arch/x86/entry/common.c:296
entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x446e19
Code: e8 ec e7 ff ff 48 83 c4 18 c3 0f 1f 80 00 00 00 00 48 89 f8 48 89 f7
48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff
ff 0f 83 3b 07 fc ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007f10bb769d98 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
RAX: ffffffffffffffda RBX: 00000000006dbc38 RCX: 0000000000446e19
RDX: 00000000ffffffbc RSI: 0000000020000040 RDI: 0000000000000004
RBP: 00000000006dbc30 R08: 0000000000000000 R09: 0000000000000000
R10: 000000000000000f R11: 0000000000000246 R12: 00000000006dbc3c
R13: 0001002402090100 R14: 000048c920200f11 R15: 08983baa00000112
Modules linked in:
---[ end trace 93f3613883c53c00 ]---
RIP: 0010:usercopy_abort+0xb9/0xbb mm/usercopy.c:98
Code: e8 c1 f7 d6 ff 49 89 d9 4d 89 e8 4c 89 e1 41 56 48 89 ee 48 c7 c7 e0
f3 cd 85 ff 74 24 08 41 57 48 8b 54 24 20 e8 15 98 c1 ff <0f> 0b e8 95 f7
d6 ff e8 80 9f fd ff 8b 54 24 04 49 89 d8 4c 89 e1
RSP: 0018:ffff8881cbda7c40 EFLAGS: 00010282
RAX: 0000000000000061 RBX: ffffffff85cdf100 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffffffff8128a0fd RDI: ffffed10397b4f7a
RBP: ffffffff85cdf2c0 R08: 0000000000000061 R09: fffffbfff11acda1
R10: fffffbfff11acda0 R11: ffffffff88d66d07 R12: ffffffff85cdf4e0
R13: ffffffff85cdf100 R14: 000000007ffff000 R15: ffffffff85cdf100
FS: 00007f10bb76a700(0000) GS:ffff8881db300000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7135a49000 CR3: 00000001d20e8000 CR4: 00000000001406e0

Kees Cook

unread,
Aug 10, 2019, 2:23:21 PM8/10/19
to Alan Stern, Greg KH, syzbot, Michael Hund, ak...@linux-foundation.org, andre...@google.com, c...@lca.pw, linux-...@vger.kernel.org, linu...@kvack.org, linu...@vger.kernel.org, syzkall...@googlegroups.com, tg...@linutronix.de
On Fri, Aug 09, 2019 at 11:13:00AM -0400, Alan Stern wrote:
> In fact, I don't see why any of the computations here should overflow
> or wrap around, or even give rise to a negative value. If syzbot had a
> reproducer we could get more debugging output -- but it doesn't.

Yeah, this is odd. The only thing I could see here with more study was
that ring_tail is used/updated outside of the rbsl lock in
ld_usb_read(). I couldn't convince myself there wasn't a race against
the interrupt, but I also couldn't think of a way it could break...

--
Kees Cook

Andrey Konovalov

unread,
Aug 12, 2019, 8:06:20 AM8/12/19
to syzbot, Andrew Morton, Qian Cai, Greg Kroah-Hartman, Kees Cook, LKML, Linux Memory Management List, USB list, syzkaller-bugs, Thomas Gleixner
#syz dup: KASAN: use-after-free Read in ld_usb_release

Johan Hovold

unread,
Oct 18, 2019, 10:39:06 AM10/18/19
to Andrey Konovalov, syzbot, Greg Kroah-Hartman, Kees Cook, LKML, USB list, syzkaller-bugs
This was a different bug. Mark as dup of the latest report:

#syz dup: KASAN: slab-out-of-bounds Read in ld_usb_read (3)

Johan
Reply all
Reply to author
Forward
0 new messages