2021-12-30 04:38:36

by Hangyu Hua

[permalink] [raw]
Subject: [PATCH v2 0/2] 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(-)

--
2.25.1



2021-12-30 04:38:39

by Hangyu Hua

[permalink] [raw]
Subject: [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.

Acked-by: Alan Stern <[email protected]>
Signed-off-by: Hangyu Hua <[email protected]>
---
drivers/usb/gadget/legacy/inode.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index 3b58f4fc0a80..564352035ac1 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;

--
2.25.1


2021-12-30 04:38:42

by Hangyu Hua

[permalink] [raw]
Subject: [PATCH v2 2/2] usb: gadget: clear related members when goto fail

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

Acked-by: Alan Stern <[email protected]>
Signed-off-by: Hangyu Hua <[email protected]>
---
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 564352035ac1..c03ac0272e7d 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;
} 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);
--
2.25.1


2021-12-30 11:04:27

by Greg Kroah-Hartman

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

On Thu, Dec 30, 2021 at 12:38:14PM +0800, Hangyu Hua wrote:
> dev->buf does not need to be released if it already exists before
> executing dev_config.
>
> Acked-by: Alan Stern <[email protected]>

I missed this, where did Alan ack this patch?

thanks,

greg k-h

2021-12-31 01:44:30

by Hangyu Hua

[permalink] [raw]
Subject: 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.

On 2021/12/30 下午7:03, Greg KH wrote:
> On Thu, Dec 30, 2021 at 12:38:14PM +0800, Hangyu Hua wrote:
>> dev->buf does not need to be released if it already exists before
>> executing dev_config.
>>
>> Acked-by: Alan Stern <[email protected]>
>
> I missed this, where did Alan ack this patch?
>
> thanks,
>
> greg k-h
>