From: Alan Stern <stern@rowland.harvard.edu> To: Eric Biggers <ebiggers@kernel.org> Cc: syzbot <syzbot+a9fefd18c7b240f19c54@syzkaller.appspotmail.com>, andreyknvl@google.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, rafael@kernel.org, syzkaller-bugs@googlegroups.com Subject: KASAN: slab-out-of-bounds Read in hex_string Date: Tue, 30 Apr 2019 10:13:38 -0400 (EDT) [thread overview] Message-ID: <Pine.LNX.4.44L0.1904301007320.1465-100000@iolanthe.rowland.org> (raw) On Mon, 29 Apr 2019, Eric Biggers wrote: > On Mon, Apr 29, 2019 at 04:07:04PM -0400, Alan Stern wrote: > > Accessing beyond the end of the descriptor. > > > > #syz test: https://github.com/google/kasan.git usb-fuzzer > > > > --- a/drivers/video/fbdev/udlfb.c > > +++ b/drivers/video/fbdev/udlfb.c > > @@ -1511,6 +1511,7 @@ static int dlfb_parse_vendor_descriptor( > > char *buf; > > char *desc_end; > > int total_len; > > + int width; > > > > buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); > > if (!buf) > > @@ -1529,9 +1530,10 @@ static int dlfb_parse_vendor_descriptor( > > } > > > > if (total_len > 5) { > > + width = min(total_len, 11); > > dev_info(&intf->dev, > > - "vendor descriptor length: %d data: %11ph\n", > > - total_len, desc); > > + "vendor descriptor length: %d data: %*ph\n", > > + total_len, width, desc); > > > > if ((desc[0] != total_len) || /* descriptor length */ > > (desc[1] != 0x5f) || /* vendor descriptor type */ > > > > > > Why not write just: > > dev_info(&intf->dev, > "vendor descriptor length: %d data: %*ph\n", > total_len, min(total_len, 11), desc); I did consider doing that. In the end I decided adding an extra temporary variable made the code a little more readable. (For some reason, extra recursion -- a function call embedded in a function argument -- seems to demand more mental effort than an extra temporary. Maybe my brain is just getting old...) > Also, aren't there more out-of-bounds reads in the code just after? It only > checks for at least 1 byte available, but then it reads up to 7 bytes: > > while (desc < desc_end) { > u8 length; > u16 key; > > key = *desc++; > key |= (u16)*desc++ << 8; > length = *desc++; > > switch (key) { > case 0x0200: { /* max_area */ > u32 max_area = *desc++; > max_area |= (u32)*desc++ << 8; > max_area |= (u32)*desc++ << 16; > max_area |= (u32)*desc++ << 24; > dev_warn(&intf->dev, > "DL chip limited to %d pixel modes\n", > max_area); > dlfb->sku_pixel_limit = max_area; > break; > } > default: > break; > } > desc += length; > } Quite right. Please feel free to submit a patch fixing all these problems. > Also I couldn't help but notice it's also using 'char' rather than 'u8', > so bytes >= 0x80 are read incorrectly as they're sign extended... As I recall, the C standard doesn't specify whether char is signed or unsigned; it can vary with the implementation. However you are certainly correct that to ensure there is no sign extension, the code should use unsigned char or u8. Alan Stern
WARNING: multiple messages have this Message-ID (diff)
From: Alan Stern <stern@rowland.harvard.edu> To: Eric Biggers <ebiggers@kernel.org> Cc: syzbot <syzbot+a9fefd18c7b240f19c54@syzkaller.appspotmail.com>, <andreyknvl@google.com>, <gregkh@linuxfoundation.org>, <linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>, <rafael@kernel.org>, <syzkaller-bugs@googlegroups.com> Subject: Re: KASAN: slab-out-of-bounds Read in hex_string Date: Tue, 30 Apr 2019 10:13:38 -0400 (EDT) [thread overview] Message-ID: <Pine.LNX.4.44L0.1904301007320.1465-100000@iolanthe.rowland.org> (raw) Message-ID: <20190430141338.ePiNn-xvu0g7by5eGmZT6zy533-ErBv5IF7nQYM5DIM@z> (raw) In-Reply-To: <20190429210917.GA251866@gmail.com> On Mon, 29 Apr 2019, Eric Biggers wrote: > On Mon, Apr 29, 2019 at 04:07:04PM -0400, Alan Stern wrote: > > Accessing beyond the end of the descriptor. > > > > #syz test: https://github.com/google/kasan.git usb-fuzzer > > > > --- a/drivers/video/fbdev/udlfb.c > > +++ b/drivers/video/fbdev/udlfb.c > > @@ -1511,6 +1511,7 @@ static int dlfb_parse_vendor_descriptor( > > char *buf; > > char *desc_end; > > int total_len; > > + int width; > > > > buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); > > if (!buf) > > @@ -1529,9 +1530,10 @@ static int dlfb_parse_vendor_descriptor( > > } > > > > if (total_len > 5) { > > + width = min(total_len, 11); > > dev_info(&intf->dev, > > - "vendor descriptor length: %d data: %11ph\n", > > - total_len, desc); > > + "vendor descriptor length: %d data: %*ph\n", > > + total_len, width, desc); > > > > if ((desc[0] != total_len) || /* descriptor length */ > > (desc[1] != 0x5f) || /* vendor descriptor type */ > > > > > > Why not write just: > > dev_info(&intf->dev, > "vendor descriptor length: %d data: %*ph\n", > total_len, min(total_len, 11), desc); I did consider doing that. In the end I decided adding an extra temporary variable made the code a little more readable. (For some reason, extra recursion -- a function call embedded in a function argument -- seems to demand more mental effort than an extra temporary. Maybe my brain is just getting old...) > Also, aren't there more out-of-bounds reads in the code just after? It only > checks for at least 1 byte available, but then it reads up to 7 bytes: > > while (desc < desc_end) { > u8 length; > u16 key; > > key = *desc++; > key |= (u16)*desc++ << 8; > length = *desc++; > > switch (key) { > case 0x0200: { /* max_area */ > u32 max_area = *desc++; > max_area |= (u32)*desc++ << 8; > max_area |= (u32)*desc++ << 16; > max_area |= (u32)*desc++ << 24; > dev_warn(&intf->dev, > "DL chip limited to %d pixel modes\n", > max_area); > dlfb->sku_pixel_limit = max_area; > break; > } > default: > break; > } > desc += length; > } Quite right. Please feel free to submit a patch fixing all these problems. > Also I couldn't help but notice it's also using 'char' rather than 'u8', > so bytes >= 0x80 are read incorrectly as they're sign extended... As I recall, the C standard doesn't specify whether char is signed or unsigned; it can vary with the implementation. However you are certainly correct that to ensure there is no sign extension, the code should use unsigned char or u8. Alan Stern
next prev reply other threads:[~2019-04-30 14:13 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-04-29 12:06 KASAN: slab-out-of-bounds Read in hex_string syzbot 2019-04-29 20:07 ` Alan Stern 2019-04-29 20:07 ` Alan Stern 2019-04-29 20:24 ` Andrey Konovalov 2019-04-29 20:24 ` Andrey Konovalov 2019-04-29 20:52 ` syzbot 2019-04-29 20:52 ` syzbot 2019-04-29 21:09 ` Eric Biggers 2019-04-29 21:09 ` Eric Biggers 2019-04-30 14:13 ` Alan Stern [this message] 2019-04-30 14:13 ` Alan Stern 2019-08-13 12:55 ` Andrey Konovalov 2020-03-18 15:46 ` Andrey Konovalov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=Pine.LNX.4.44L0.1904301007320.1465-100000@iolanthe.rowland.org \ --to=stern@rowland.harvard.edu \ --cc=andreyknvl@google.com \ --cc=ebiggers@kernel.org \ --cc=gregkh@linuxfoundation.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-usb@vger.kernel.org \ --cc=rafael@kernel.org \ --cc=syzbot+a9fefd18c7b240f19c54@syzkaller.appspotmail.com \ --cc=syzkaller-bugs@googlegroups.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).