Skip to contentSkip to conversation

usb: gadget: use after free in dev_config

PATCH Superseded Hangyu Hua 2 participants 5 messages Dec 30, 2021 – Dec 31, 2021 USB

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

v2 cover usb: gadget: use after free in dev_config

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.

v2:
1. break one patch up into two separate patches.
2. use "fail:" to clear all members.

Hangyu Hua (2):
usb: gadget: don't release an existing dev->buf
usb: gadget: clear related members when goto fail

 drivers/usb/gadget/legacy/inode.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/2 usb: gadget: don't release an existing dev->buf

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

---
 drivers/usb/gadget/legacy/inode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/gadget/legacy/inode.c +2 -1
index 3b58f4fc0a80..564352035ac1 100644
@@ -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;
 
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] usb: gadget: don't release an existing dev->buf

Quoted 4 lines · Hangyu Hua

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

I missed this, where did Alan ack this patch?

thanks,

greg k-h

author
#3 lore raw

Re: [PATCH v2 1/2] usb: gadget: don't release an existing dev->buf

I misunderstood the meaning of Alan. He didn't ack this patch. We stil disscuss about this. I am sorry.

Quoted 12 lines · Greg KH
Quoted 4 lines · Hangyu Hua

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

I missed this, where did Alan ack this patch?

thanks,

greg k-h

author
#4 lore raw

PATCH v2 2/2 usb: gadget: clear related members when goto fail

In reply to Hangyu Hua

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

---
 drivers/usb/gadget/legacy/inode.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/usb/gadget/legacy/inode.c +11 -3
index 564352035ac1..c03ac0272e7d 100644
@@ -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;
 	} 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;
 fail:
+	dev->config = NULL;
 	spin_unlock_irq (&dev->lock);
 	pr_debug ("%s: %s fail %zd, %p\n", shortname, __func__, value, dev);
 	kfree (dev->buf);
Signature · 1 line
2.25.1