* [PATCH v3 0/2] usb: gadget: use after free in dev_config @ 2021-12-30 5:11 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 0 siblings, 2 replies; 8+ messages in thread From: Hangyu Hua @ 2021-12-30 5:11 UTC (permalink / raw) To: balbi, gregkh, axboe, stern, jj251510319013, dan.carpenter 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. v2: 1. break one patch up into two separate patches. 2. use "fail:" to clear all members. v3: fix a mistake in [PATCH v3 2/2] 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/2] usb: gadget: don't release an existing dev->buf 2021-12-30 5:11 [PATCH v3 0/2] usb: gadget: use after free in dev_config Hangyu Hua @ 2021-12-30 5:11 ` Hangyu Hua 2021-12-30 5:11 ` [PATCH v3 2/2] usb: gadget: clear related members when goto fail Hangyu Hua 1 sibling, 0 replies; 8+ messages in thread From: Hangyu Hua @ 2021-12-30 5:11 UTC (permalink / raw) To: balbi, gregkh, axboe, stern, jj251510319013, dan.carpenter Cc: linux-usb, linux-kernel, Hangyu Hua dev->buf does not need to be released if it already exists before executing dev_config. Acked-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Hangyu Hua <hbh25y@gmail.com> --- 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..eaad03c0252f 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 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 2/2] usb: gadget: clear related members when goto fail 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 ` Hangyu Hua 2021-12-30 19:46 ` Alan Stern 1 sibling, 1 reply; 8+ messages in thread From: Hangyu Hua @ 2021-12-30 5:11 UTC (permalink / raw) To: balbi, gregkh, axboe, stern, jj251510319013, dan.carpenter Cc: linux-usb, linux-kernel, Hangyu Hua 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> 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; } 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 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail 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 2021-12-30 19:50 ` Alan Stern 2021-12-31 2:31 ` Hangyu Hua 0 siblings, 2 replies; 8+ messages in thread From: Alan Stern @ 2021-12-30 19:46 UTC (permalink / raw) To: Hangyu Hua Cc: balbi, gregkh, axboe, jj251510319013, dan.carpenter, linux-usb, linux-kernel 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail 2021-12-30 19:46 ` Alan Stern @ 2021-12-30 19:50 ` Alan Stern 2021-12-31 2:31 ` Hangyu Hua 1 sibling, 0 replies; 8+ messages in thread From: Alan Stern @ 2021-12-30 19:50 UTC (permalink / raw) To: Hangyu Hua Cc: balbi, gregkh, axboe, jj251510319013, dan.carpenter, linux-usb, linux-kernel On Thu, Dec 30, 2021 at 02:46:00PM -0500, Alan Stern wrote: > 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. > > @@ -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? Wrong word: I meant to write "grab", not "grep". Alan Stern ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail 2021-12-30 19:46 ` Alan Stern 2021-12-30 19:50 ` Alan Stern @ 2021-12-31 2:31 ` Hangyu Hua 2021-12-31 15:57 ` Alan Stern 1 sibling, 1 reply; 8+ messages in thread From: Hangyu Hua @ 2021-12-31 2:31 UTC (permalink / raw) To: Alan Stern Cc: balbi, gregkh, axboe, jj251510319013, dan.carpenter, linux-usb, linux-kernel On 2021/12/31 上午3:46, Alan Stern wrote: > You must not do this. I never sent you an Acked-by for this patch; you > shouldn't claim that I did. I am sorry about this. I should read the linux kernel community rules more carefully. >> 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? You are right. I will modify my patch later. >> } 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 > I don't think so. It is not necessary to clean all three pointers if some of them aren't kbuf. I think it may be better to keep their own pointers. Thanks. Happy new year. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail 2021-12-31 2:31 ` Hangyu Hua @ 2021-12-31 15:57 ` Alan Stern 2021-12-31 16:15 ` Hangyu Hua 0 siblings, 1 reply; 8+ messages in thread From: Alan Stern @ 2021-12-31 15:57 UTC (permalink / raw) To: Hangyu Hua Cc: balbi, gregkh, axboe, jj251510319013, dan.carpenter, linux-usb, linux-kernel On Fri, Dec 31, 2021 at 10:31:51AM +0800, Hangyu Hua wrote: > On 2021/12/31 上午3:46, Alan Stern wrote: > >> @@ -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. > I don't think so. It is not necessary to clean all three pointers if > some of them aren't kbuf. I think it may be better to keep their own > pointers. If the pointers aren't set to a region inside kbuf then they are meaningless. There is no reason to keep the old values. It is better to avoid multiple unnecessary statement labels. Alan Stern ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] usb: gadget: clear related members when goto fail 2021-12-31 15:57 ` Alan Stern @ 2021-12-31 16:15 ` Hangyu Hua 0 siblings, 0 replies; 8+ messages in thread From: Hangyu Hua @ 2021-12-31 16:15 UTC (permalink / raw) To: Alan Stern Cc: balbi, gregkh, axboe, jj251510319013, dan.carpenter, linux-usb, linux-kernel I get that. I will fix and resubmit my patch. Thanks again. On 2021/12/31 下午11:57, Alan Stern wrote: > On Fri, Dec 31, 2021 at 10:31:51AM +0800, Hangyu Hua wrote: >> On 2021/12/31 上午3:46, Alan Stern wrote: > >>>> @@ -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. > >> I don't think so. It is not necessary to clean all three pointers if >> some of them aren't kbuf. I think it may be better to keep their own >> pointers. > > If the pointers aren't set to a region inside kbuf then they are > meaningless. There is no reason to keep the old values. It is better > to avoid multiple unnecessary statement labels. > > Alan Stern > ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2021-12-31 16:20 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox