From: Alan Stern <stern@rowland.harvard.edu>
To: Hangyu Hua <hbh25y@gmail.com>
Cc: balbi@kernel.org, gregkh@linuxfoundation.org, axboe@kernel.dk,
jj251510319013@gmail.com, dan.carpenter@oracle.com,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail
Date: Thu, 30 Dec 2021 14:46:00 -0500 [thread overview]
Message-ID: <Yc4MeIt6JygZ6CrY@rowland.harvard.edu> (raw)
In-Reply-To: <20211230051132.21056-3-hbh25y@gmail.com>
On Thu, Dec 30, 2021 at 01:11:32PM +0800, Hangyu Hua wrote:
> dev->config and dev->hs_config and dev->dev need to be cleaned if
> dev_config fails to avoid UAF.
>
> Acked-by: Alan Stern <stern@rowland.harvard.edu>
You must not do this. I never sent you an Acked-by for this patch; you
shouldn't claim that I did.
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> ---
> drivers/usb/gadget/legacy/inode.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
> index eaad03c0252f..d2e88f3b9131 100644
> --- a/drivers/usb/gadget/legacy/inode.c
> +++ b/drivers/usb/gadget/legacy/inode.c
> @@ -1847,7 +1847,7 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
> total = le16_to_cpu(dev->hs_config->wTotalLength);
> if (!is_valid_config(dev->hs_config, total) ||
> total > length - USB_DT_DEVICE_SIZE)
> - goto fail;
> + goto fail1;
> kbuf += total;
> length -= total;
> } else {
> @@ -1858,12 +1858,12 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
>
> /* device descriptor (tweaked for paranoia) */
> if (length != USB_DT_DEVICE_SIZE)
> - goto fail;
> + goto fail1;
> dev->dev = (void *)kbuf;
> if (dev->dev->bLength != USB_DT_DEVICE_SIZE
> || dev->dev->bDescriptorType != USB_DT_DEVICE
> || dev->dev->bNumConfigurations != 1)
> - goto fail;
> + goto fail2;
> dev->dev->bcdUSB = cpu_to_le16 (0x0200);
>
> /* triggers gadgetfs_bind(); then we can enumerate. */
> @@ -1875,6 +1875,9 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
>
> value = usb_gadget_probe_driver(&gadgetfs_driver);
> if (value != 0) {
> + dev->dev = NULL;
> + dev->hs_config = NULL;
> + dev->config = NULL;
> kfree (dev->buf);
> dev->buf = NULL;
Why not just grep the lock and goto fail?
> } else {
> @@ -1892,7 +1895,12 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
> }
> return value;
>
> +fail2:
> + dev->dev = NULL;
> +fail1:
> + dev->hs_config = NULL;
It is not necessary to have all these different statement labels. You
can simply have "fail:" clear all three pointers.
> fail:
> + dev->config = NULL;
> spin_unlock_irq (&dev->lock);
> pr_debug ("%s: %s fail %zd, %p\n", shortname, __func__, value, dev);
> kfree (dev->buf);
Alan Stern
next prev parent reply other threads:[~2021-12-30 19:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-30 5:11 [PATCH v3 0/2] usb: gadget: use after free in dev_config Hangyu Hua
2021-12-30 5:11 ` [PATCH v3 1/2] usb: gadget: don't release an existing dev->buf Hangyu Hua
2021-12-30 5:11 ` [PATCH v3 2/2] usb: gadget: clear related members when goto fail Hangyu Hua
2021-12-30 19:46 ` Alan Stern [this message]
2021-12-30 19:50 ` Alan Stern
2021-12-31 2:31 ` Hangyu Hua
2021-12-31 15:57 ` Alan Stern
2021-12-31 16:15 ` Hangyu Hua
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=Yc4MeIt6JygZ6CrY@rowland.harvard.edu \
--to=stern@rowland.harvard.edu \
--cc=axboe@kernel.dk \
--cc=balbi@kernel.org \
--cc=dan.carpenter@oracle.com \
--cc=gregkh@linuxfoundation.org \
--cc=hbh25y@gmail.com \
--cc=jj251510319013@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
/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®