All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: use after free in dev_config
@ 2021-12-28  9:21 Hangyu Hua
  2021-12-29 21:11 ` Alan Stern
  0 siblings, 1 reply; 3+ messages in thread
From: Hangyu Hua @ 2021-12-28  9:21 UTC (permalink / raw)
  To: balbi, gregkh, axboe, dan.carpenter, jj251510319013
  Cc: linux-usb, linux-kernel, Hangyu Hua

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: gadget: use after free in dev_config
  2021-12-28  9:21 [PATCH] usb: gadget: use after free in dev_config Hangyu Hua
@ 2021-12-29 21:11 ` Alan Stern
  2021-12-30  2:15   ` Hangyu Hua
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Stern @ 2021-12-29 21:11 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: balbi, gregkh, axboe, dan.carpenter, jj251510319013, linux-usb,
	linux-kernel

On Tue, Dec 28, 2021 at 05:21:26PM +0800, Hangyu Hua wrote:
> There are two bugs:

You should break this up into two separate patches, one for each bug.

> dev->buf does not need to be released if it already exists before
> executing dev_config.

That's right.  The call to dev_config should fail without changing any 
of the stored values.

> dev->config and dev->hs_config and dev->dev need to be cleaned if
> dev_config fails to avoid UAF.

Do they really need to be cleared?  I think if dev_config fails then 
those pointers never get used, so it doesn't matter what they contain.

Of course, clearing them doesn't hurt, but it would be best to clear 
all of them in the "fail:" part of the routine.  And then you would 
want to change the pathway where usb_gadget_probe_driver fails, to make 
it go to "fail:" also.

Alan Stern

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: gadget: use after free in dev_config
  2021-12-29 21:11 ` Alan Stern
@ 2021-12-30  2:15   ` Hangyu Hua
  0 siblings, 0 replies; 3+ messages in thread
From: Hangyu Hua @ 2021-12-30  2:15 UTC (permalink / raw)
  To: Alan Stern
  Cc: balbi, gregkh, axboe, dan.carpenter, jj251510319013, linux-usb,
	linux-kernel

Thank for your suggesions. I will break this up into two separate 
patches later. And i think it's necessary to clear up dev->config and
dev->hs_config and dev->dev. For example, dev->hs_config is used in
ep0_read() which may lead a UAF.

Thanks.

On 2021/12/30 上午5:11, Alan Stern wrote:
> On Tue, Dec 28, 2021 at 05:21:26PM +0800, Hangyu Hua wrote:
>> There are two bugs:
> 
> You should break this up into two separate patches, one for each bug.
> 
>> dev->buf does not need to be released if it already exists before
>> executing dev_config.
> 
> That's right.  The call to dev_config should fail without changing any
> of the stored values.
> 
>> dev->config and dev->hs_config and dev->dev need to be cleaned if
>> dev_config fails to avoid UAF.
> 
> Do they really need to be cleared?  I think if dev_config fails then
> those pointers never get used, so it doesn't matter what they contain.
> 
> Of course, clearing them doesn't hurt, but it would be best to clear
> all of them in the "fail:" part of the routine.  And then you would
> want to change the pathway where usb_gadget_probe_driver fails, to make
> it go to "fail:" also.
> 
> Alan Stern
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-30  2:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-28  9:21 [PATCH] usb: gadget: use after free in dev_config Hangyu Hua
2021-12-29 21:11 ` Alan Stern
2021-12-30  2:15   ` Hangyu Hua

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.