mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Sabyrzhan Tasbolatov <snovitoll@gmail.com>
Cc: oneukum@suse.com, gregkh@linuxfoundation.org,
	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 15:25:53 -0500	[thread overview]
Message-ID: <4ea9e56b-0941-4ea4-8cf3-b62facdbff53@rowland.harvard.edu> (raw)
In-Reply-To: <CACzwLxgVJ2jROr8RWHXv++2m2tD9fvskp_MqTL7VhCPr-Eeeiw@mail.gmail.com>

On Wed, Nov 13, 2024 at 12:30:08AM +0500, Sabyrzhan Tasbolatov wrote:
> I've re-read your and Oliver's comments and come up with this diff,
> which is the same as v4 except it is within a spinlock.
> 
> diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
> index 86ee39db013f..47b299e03e11 100644
> --- a/drivers/usb/class/cdc-wdm.c
> +++ b/drivers/usb/class/cdc-wdm.c
> @@ -598,8 +598,11 @@ static ssize_t wdm_read
>                 spin_unlock_irq(&desc->iuspin);
>         }
> 
> -       if (cntr > count)
> -               cntr = count;
> +       spin_lock_irq(&desc->iuspin);
> +       /* Ensure cntr does not exceed available data in ubuf. */
> +       cntr = min_t(size_t, count, desc->length);
> +       spin_unlock_irq(&desc->iuspin);
> +
>         rv = copy_to_user(buffer, desc->ubuf, cntr);
>         if (rv > 0) {
>                 rv = -EFAULT;

You seem to be stuck in a rut, doing the same thing over and over again 
and not realizing that it accomplishes nothing.  The spinlock here 
doesn't help; it merely allows you to avoid calling READ_ONCE.

> > Since the new code does the same thing as the old code, it cannot
> > possibly fix any bugs.
> 
> Without the reproducer I can not confirm that this fixes the hypothetical bug,
> however here is my understand how the diff above can fix the memory info leak:
> 
> static ssize_t wdm_read() {
>         cntr = READ_ONCE(desc->length);
>         if (cntr == 0) {
>                 spin_lock_irq(&desc->iuspin);
> 
>                 /* can remain 0 if not increased in wdm_in_callback() */
>                 cntr = desc->length;
> 
>                 spin_unlock_irq(&desc->iuspin);
>         }
> 
>         spin_lock_irq(&desc->iuspin);
>         /* take the minimum of whatever user requests `count` and
> desc->length = 0 */
>         cntr = min_t(size_t, count, desc->length);
>         spin_lock_irq(&desc->iuspin);
> 
>         /* cntr is 0, nothing to copy to the user space. */
>         rv = copy_to_user(buffer, desc->ubuf, cntr);

This does not explain anything.  How do you think your change will avoid 
the memory info leak?  That is, what differences between the old code 
and the new code will cause the leak to happen with the old code and not 
to happen with your new code?

Note that if cntr is 0 then nothing is copied to user space so there is 
no info leak.

> > (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?)
> 
> desc->length can not be negative if I understand the following correctly:
> 
> static void wdm_in_callback(struct urb *urb)
> {
>         ...
>         int length = urb->actual_length;
>        ...
>        if (length + desc->length > desc->wMaxCommand) {
>               /* The buffer would overflow */
>              ...
>        } else {
>               /* we may already be in overflow */
>               if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
>                      ...
>                      desc->length += length;
>                      desc->reslength = length;
>        }
> }
> 
> urb->actual_length is u32, actually, need to change `int length` to
> `u32 length` though.

You don't really need to change it.  urb->actual_length can never be 
larger than urb->length.

Alan Stern

  reply	other threads:[~2024-11-12 20:25 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
2024-11-12 19:30               ` Sabyrzhan Tasbolatov
2024-11-12 20:25                 ` Alan Stern [this message]
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=4ea9e56b-0941-4ea4-8cf3-b62facdbff53@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®