From: Alan Stern <stern@rowland.harvard.edu>
To: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
Cc: gregkh@linuxfoundation.org, oneukum@suse.com,
linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org,
syzbot+9760fbbd535cee131f81@syzkaller.appspotmail.com,
syzkaller-bugs@googlegroups.com
Subject: Re: [PATCH v4] usb/cdc-wdm: fix memory info leak in wdm_read
Date: Tue, 12 Nov 2024 10:52:30 -0500 [thread overview]
Message-ID: <824e839d-ee72-4923-bc88-e9cc58201b07@rowland.harvard.edu> (raw)
In-Reply-To: <20241112132931.3504749-1-snovitoll@gmail.com>
On Tue, Nov 12, 2024 at 06:29:31PM +0500, Sabyrzhan Tasbolatov wrote:
> syzbot reported "KMSAN: kernel-infoleak in wdm_read", though there is no
> reproducer and the only report for this issue.
>
> The check:
>
> if (cntr > count)
> cntr = count;
>
> only limits `cntr` to `count` (the number of bytes requested by
> userspace), but it doesn't verify that `desc->ubuf` actually has `count`
> bytes. This oversight can lead to situations where `copy_to_user` reads
> uninitialized data from `desc->ubuf`.
>
> This patch makes sure `cntr` respects` both the `desc->length` and the
> `count` requested by userspace, preventing any uninitialized memory from
> leaking into userspace.
> ---
> drivers/usb/class/cdc-wdm.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
> index 86ee39db013f..5a500973b463 100644
> --- a/drivers/usb/class/cdc-wdm.c
> +++ b/drivers/usb/class/cdc-wdm.c
> @@ -598,8 +598,9 @@ static ssize_t wdm_read
> spin_unlock_irq(&desc->iuspin);
> }
Note that the code immediately before the "if" statement which ends here
does:
cntr = READ_ONCE(desc->length);
And the code at the end of the "if" block does:
cntr = desc->length;
(while holding the spinlock). Thus it is guaranteed that either way,
cntr is equal to desc->length when we reach this point.
>
> - if (cntr > count)
> - cntr = count;
> + /* Ensure cntr does not exceed available data in ubuf. */
> + cntr = min_t(size_t, count, desc->length);
And therefore this line does exactly the same computation as the code
you removed. Except for one thing: At this point the spinlock is not
held, and your new code does not call READ_ONCE(). That is an
oversight.
Since the new code does the same thing as the old code, it cannot
possibly fix any bugs.
(Actually there is one other thing to watch out for: the difference
between signed and unsigned values. Here cntr and desc->length are
signed whereas count is unsigned. In theory that could cause problems
-- it might even be related to the cause of the original bug report.
Can you prove that desc->length will never be negative?)
Alan Stern
next prev parent reply other threads:[~2024-11-12 15:52 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-20 22:48 [syzbot] [usb?] KMSAN: kernel-infoleak " syzbot
2024-11-09 15:28 ` [PATCH] usb/cdc-wdm: fix memory leak of wdm_device Sabyrzhan Tasbolatov
2024-11-10 7:07 ` Greg KH
2024-11-10 8:21 ` [PATCH v2] usb/cdc-wdm: fix memory info leak in wdm_read Sabyrzhan Tasbolatov
2024-11-11 9:44 ` [PATCH] usb/cdc-wdm: fix memory leak of wdm_device Oliver Neukum
2024-11-11 10:40 ` Sabyrzhan Tasbolatov
2024-11-11 11:46 ` Greg KH
2024-11-11 12:01 ` [PATCH v3] usb/cdc-wdm: fix memory info leak in wdm_read Sabyrzhan Tasbolatov
2024-11-12 11:13 ` Greg KH
2024-11-12 13:29 ` [PATCH v4] " Sabyrzhan Tasbolatov
2024-11-12 15:52 ` Alan Stern [this message]
2024-11-12 19:30 ` Sabyrzhan Tasbolatov
2024-11-12 20:25 ` Alan Stern
2024-11-14 5:58 ` Sabyrzhan Tasbolatov
2024-11-11 14:29 ` [PATCH] usb/cdc-wdm: fix memory leak of wdm_device Alan Stern
2024-11-12 9:34 ` Sabyrzhan Tasbolatov
2024-11-12 15:38 ` Alan Stern
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=824e839d-ee72-4923-bc88-e9cc58201b07@rowland.harvard.edu \
--to=stern@rowland.harvard.edu \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=oneukum@suse.com \
--cc=snovitoll@gmail.com \
--cc=syzbot+9760fbbd535cee131f81@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®