public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Hangyu Hua <hbh25y@gmail.com>
To: balbi@kernel.org, gregkh@linuxfoundation.org, axboe@kernel.dk,
	dan.carpenter@oracle.com, jj251510319013@gmail.com
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Hangyu Hua <hbh25y@gmail.com>
Subject: [PATCH] usb: gadget: use after free in dev_config
Date: Tue, 28 Dec 2021 17:21:26 +0800	[thread overview]
Message-ID: <20211228092126.29510-1-hbh25y@gmail.com> (raw)

There are two bugs:
dev->buf does not need to be released if it already exists before
executing dev_config.
dev->config and dev->hs_config and dev->dev need to be cleaned if
dev_config fails to avoid UAF.

Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
---
 drivers/usb/gadget/legacy/inode.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index 3b58f4fc0a80..2ea02887025b 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1826,8 +1826,9 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 	spin_lock_irq (&dev->lock);
 	value = -EINVAL;
 	if (dev->buf) {
+		spin_unlock_irq(&dev->lock);
 		kfree(kbuf);
-		goto fail;
+		return value;
 	}
 	dev->buf = kbuf;
 
@@ -1835,8 +1836,10 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 	dev->config = (void *) kbuf;
 	total = le16_to_cpu(dev->config->wTotalLength);
 	if (!is_valid_config(dev->config, total) ||
-			total > length - USB_DT_DEVICE_SIZE)
+			total > length - USB_DT_DEVICE_SIZE) {
+		dev->config = NULL;
 		goto fail;
+	}
 	kbuf += total;
 	length -= total;
 
@@ -1845,8 +1848,11 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 		dev->hs_config = (void *) kbuf;
 		total = le16_to_cpu(dev->hs_config->wTotalLength);
 		if (!is_valid_config(dev->hs_config, total) ||
-				total > length - USB_DT_DEVICE_SIZE)
+				total > length - USB_DT_DEVICE_SIZE) {
+			dev->config = NULL;
+			dev->hs_config = NULL;
 			goto fail;
+		}
 		kbuf += total;
 		length -= total;
 	} else {
@@ -1856,13 +1862,20 @@ dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 	/* could support multiple configs, using another encoding! */
 
 	/* device descriptor (tweaked for paranoia) */
-	if (length != USB_DT_DEVICE_SIZE)
+	if (length != USB_DT_DEVICE_SIZE) {
+		dev->config = NULL;
+		dev->hs_config = NULL;
 		goto fail;
+	}
 	dev->dev = (void *)kbuf;
 	if (dev->dev->bLength != USB_DT_DEVICE_SIZE
 			|| dev->dev->bDescriptorType != USB_DT_DEVICE
-			|| dev->dev->bNumConfigurations != 1)
+			|| dev->dev->bNumConfigurations != 1) {
+		dev->config = NULL;
+		dev->hs_config = NULL;
+		dev->dev = NULL;
 		goto fail;
+	}
 	dev->dev->bcdUSB = cpu_to_le16 (0x0200);
 
 	/* triggers gadgetfs_bind(); then we can enumerate. */
-- 
2.25.1


             reply	other threads:[~2021-12-28  9:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-28  9:21 Hangyu Hua [this message]
2021-12-29 21:11 ` [PATCH] usb: gadget: use after free in dev_config Alan Stern
2021-12-30  2: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=20211228092126.29510-1-hbh25y@gmail.com \
    --to=hbh25y@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=balbi@kernel.org \
    --cc=dan.carpenter@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --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