From: Edward Adam Davis <eadavis@qq.com>
To: stern@rowland.harvard.edu
Cc: eadavis@qq.com, gregkh@linuxfoundation.org,
linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org,
syzbot+9d34f80f841e948c3fdb@syzkaller.appspotmail.com,
syzkaller-bugs@googlegroups.com
Subject: Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Date: Sun, 8 Sep 2024 10:01:52 +0800 [thread overview]
Message-ID: <tencent_D062C96FE3AC045EF77E5779C6C4B36D4305@qq.com> (raw)
In-Reply-To: <3b4b8e7f-57b2-4ca1-8dc1-63faef573df3@rowland.harvard.edu>
On Sat, 7 Sep 2024 21:32:28 -0400, Alan Stern wrote:
> On Sun, Sep 08, 2024 at 08:59:48AM +0800, Edward Adam Davis wrote:
> > On Sat, 7 Sep 2024 10:45:52 -0400, Alan Stern wrote:
> > > On Sat, Sep 07, 2024 at 10:08:57AM +0800, Edward Adam Davis wrote:
> > > > On Fri, 6 Sep 2024 10:28:11 -0400, Alan Stern wrote:
> > > > > On Fri, Sep 06, 2024 at 10:11:03PM +0800, Edward Adam Davis wrote:
> > > > > > The syzbot reported a kernel-usb-infoleak in usbtmc_write.
> > > > > >
> > > > > > The expression "aligned = (transfersize + (USBTMC_HEADER_SIZE + 3)) & ~3;"
> > > > > > in usbtmcw_write() follows the following pattern:
> > > > > >
> > > > > > aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized
> > > > > > aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized
> > > > > > aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized
> > > > > > aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized
> > > > > > aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized
> > > > > > aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized
> > > > > > aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized
> > > > > > aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initialized
> > > > > > aligned = (9 + 12 + 3) & ~3 = 24
> > > > > > ...
> > > > >
> > > > > What is the purpose of aligned? Why doesn't the driver simply use
> > > > > USBTMC_HEADER_SIZE + transfersize instead of rounding it up to a
> > > > > multiple of 4?
> > > > I just found out that the logic of aligned calculation is like this.
> > > > As for why it is calculated like this, perhaps Guido Kiener can provide
> > > > a clearer explanation.
> > > > It was introduced by commit 4d5e18d9ed93 ("usb: usbtmc: Optimize usbtmc_write").
> > > > >
> > > > > > Note: #define USBTMC_HEADER_SIZE 12
> > > > > >
> > > > > > This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its
> > > > > > subsequent memory not being initialized.
> > > > > >
> > > > > > The condition aligned < buflen is used to avoid out of bounds access to
> > > > > > the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize =
> > > > > > buflen - USBTMC_HEADER_SIZE".
> > > > > >
> > > > > > Fixes: 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")
> > > > > > Reported-and-tested-by: syzbot+9d34f80f841e948c3fdb@syzkaller.appspotmail.com
> > > > > > Closes: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb
> > > > > > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > > > > > ---
> > > > > > drivers/usb/class/usbtmc.c | 4 ++++
> > > > > > 1 file changed, 4 insertions(+)
> > > > > >
> > > > > > diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
> > > > > > index 6bd9fe565385..faf8c5508997 100644
> > > > > > --- a/drivers/usb/class/usbtmc.c
> > > > > > +++ b/drivers/usb/class/usbtmc.c
> > > > > > @@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
> > > > > > goto exit;
> > > > > > }
> > > > > >
> > > > > > + if (aligned < buflen && (transfersize % 4))
> > > > >
> > > > > Shouldn't this be
> > > > >
> > > > > if (USBTMC_HEADER_SIZE + transfersize < aligned)
> > > > Logically, it seems possible to write it this way.
> > >
> > > In fact, what you wrote is wrong. Consider the case where buflen is 32
> > > and transfersize is 17. Then aligned = (12 + 17 + 3) & ~3 = 32, so your
> > > condition would fail to initialize the extra 3 bytes.
> > The buflen is equal to USBTMC_BUFSIZE and can not equal to any other value.
> > You can find it in usbtmc_create_urb() and usbtmc_write().
> >
> > Note: #define USBTMC_BUFSIZE (4096)
>
> All right, so what happens if transfersize is 4081? Then aligned will
> be equal to 4096, so your condition would fail to initialize the extra 3
> bytes.
Not bad, when the transfersize values are 4081, 4082, and 4083, the aligned
value is 4096, which will result in 1, 2, and 3 bytes not being initialized,
respectively. I will update my patch.
BR,
Edward
next prev parent reply other threads:[~2024-09-08 2:20 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-25 9:14 [syzbot] [usb?] KMSAN: kernel-usb-infoleak in usbtmc_write syzbot
2024-09-04 12:00 ` syzbot
2024-09-04 12:47 ` Edward Adam Davis
2024-09-04 13:33 ` syzbot
2024-09-04 13:55 ` [PATCH] USB: usbtmc: prevent kernel-infoleak Edward Adam Davis
2024-09-04 14:09 ` Greg KH
2024-09-04 14:13 ` Greg KH
2024-09-05 13:56 ` Edward Adam Davis
2024-09-05 14:04 ` Greg KH
2024-09-05 14:16 ` Edward Adam Davis
2024-09-06 14:11 ` [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak Edward Adam Davis
2024-09-06 14:28 ` Alan Stern
2024-09-07 2:08 ` Edward Adam Davis
2024-09-07 14:45 ` Alan Stern
2024-09-08 0:59 ` Edward Adam Davis
2024-09-08 1:32 ` Alan Stern
2024-09-08 2:01 ` Edward Adam Davis [this message]
2024-09-08 2:20 ` [PATCH V3] " Edward Adam Davis
2024-09-08 5:20 ` Greg KH
2024-09-08 7:35 ` Edward Adam Davis
2024-09-08 7:54 ` Greg KH
2024-09-08 8:16 ` Edward Adam Davis
2024-09-08 8:33 ` Greg KH
2024-09-08 9:17 ` [PATCH v4] " Edward Adam Davis
2024-09-05 11:27 ` [syzbot] [usb?] KMSAN: kernel-usb-infoleak in usbtmc_write Edward Adam Davis
2024-09-05 15:42 ` syzbot
2024-09-05 14:21 ` Edward Adam Davis
2024-09-05 16:11 ` syzbot
2024-09-06 11:55 ` Edward Adam Davis
2024-09-06 12:29 ` syzbot
2024-09-06 12:37 ` Edward Adam Davis
2024-09-06 13:07 ` syzbot
2024-09-06 13:06 ` Edward Adam Davis
2024-09-06 13:51 ` syzbot
2024-09-06 13:52 ` Edward Adam Davis
2024-09-06 16:59 ` syzbot
2024-09-05 7:11 ` [syzbot] " syzbot
2024-09-05 8:29 ` syzbot
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=tencent_D062C96FE3AC045EF77E5779C6C4B36D4305@qq.com \
--to=eadavis@qq.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
--cc=syzbot+9d34f80f841e948c3fdb@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: link
Be 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
all inboxes | Powered by JetHome®