All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] USB: gadget: udc: Fix use after free bug in udc_plat_remove due to race condition
@ 2023-03-10  7:00 Zheng Wang
  2023-03-23 16:20 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Zheng Wang @ 2023-03-10  7:00 UTC (permalink / raw)
  To: gregkh
  Cc: linux-usb, linux-kernel, hackerzheng666, 1395428693sheep,
	alex000young, Zheng Wang

In udc_plat_probe, &udc->drd_work is bound with
udc_drd_work. udc_drd_work may be called by
usbd_connect_notify to start the work.

Besides, there is a invoking chain:
udc_plat_probe
->udc_probe
->usb_add_gadget_udc_release
->usb_add_gadget

It will add a new gadget to the udc class driver
 list. In usb_add_gadget, it uses usb_udc_release
 as its release function, which will kfree(udc)
 to when destroying the gadget.

If we remove the module which will call udc_plat_remove
  to make cleanup, there may be a unfinished work.
The possible sequence is as follows:

Fix it by finishing the work before cleanup in the udc_plat_remove

Fixes: 1b9f35adb0ff ("usb: gadget: udc: Add Synopsys UDC Platform driver")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
 drivers/usb/gadget/udc/snps_udc_plat.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/usb/gadget/udc/snps_udc_plat.c b/drivers/usb/gadget/udc/snps_udc_plat.c
index 8bbb89c80348..6228e178cc0a 100644
--- a/drivers/usb/gadget/udc/snps_udc_plat.c
+++ b/drivers/usb/gadget/udc/snps_udc_plat.c
@@ -230,6 +230,7 @@ static int udc_plat_remove(struct platform_device *pdev)
 	struct udc *dev;
 
 	dev = platform_get_drvdata(pdev);
+	cancel_delayed_work_sync(&dev->drd_work);
 
 	usb_del_gadget_udc(&dev->gadget);
 	/* gadget driver must not be registered */
-- 
2.25.1


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

* Re: [PATCH] USB: gadget: udc: Fix use after free bug in udc_plat_remove due to race condition
  2023-03-10  7:00 [PATCH] USB: gadget: udc: Fix use after free bug in udc_plat_remove due to race condition Zheng Wang
@ 2023-03-23 16:20 ` Greg KH
  2023-03-24 15:34   ` Zheng Hacker
  0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2023-03-23 16:20 UTC (permalink / raw)
  To: Zheng Wang
  Cc: linux-usb, linux-kernel, hackerzheng666, 1395428693sheep, alex000young

On Fri, Mar 10, 2023 at 03:00:39PM +0800, Zheng Wang wrote:
> In udc_plat_probe, &udc->drd_work is bound with
> udc_drd_work. udc_drd_work may be called by
> usbd_connect_notify to start the work.
> 
> Besides, there is a invoking chain:
> udc_plat_probe
> ->udc_probe
> ->usb_add_gadget_udc_release
> ->usb_add_gadget
> 
> It will add a new gadget to the udc class driver
>  list. In usb_add_gadget, it uses usb_udc_release
>  as its release function, which will kfree(udc)
>  to when destroying the gadget.
> 
> If we remove the module which will call udc_plat_remove
>   to make cleanup, there may be a unfinished work.
> The possible sequence is as follows:
> 
> Fix it by finishing the work before cleanup in the udc_plat_remove
> 
> Fixes: 1b9f35adb0ff ("usb: gadget: udc: Add Synopsys UDC Platform driver")
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
>  drivers/usb/gadget/udc/snps_udc_plat.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/usb/gadget/udc/snps_udc_plat.c b/drivers/usb/gadget/udc/snps_udc_plat.c
> index 8bbb89c80348..6228e178cc0a 100644
> --- a/drivers/usb/gadget/udc/snps_udc_plat.c
> +++ b/drivers/usb/gadget/udc/snps_udc_plat.c
> @@ -230,6 +230,7 @@ static int udc_plat_remove(struct platform_device *pdev)
>  	struct udc *dev;
>  
>  	dev = platform_get_drvdata(pdev);
> +	cancel_delayed_work_sync(&dev->drd_work);
>  
>  	usb_del_gadget_udc(&dev->gadget);
>  	/* gadget driver must not be registered */
> -- 
> 2.25.1
> 

Please test this to verify that it actually works.

thanks,

greg k-h

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

* Re: [PATCH] USB: gadget: udc: Fix use after free bug in udc_plat_remove due to race condition
  2023-03-23 16:20 ` Greg KH
@ 2023-03-24 15:34   ` Zheng Hacker
  0 siblings, 0 replies; 3+ messages in thread
From: Zheng Hacker @ 2023-03-24 15:34 UTC (permalink / raw)
  To: Greg KH
  Cc: Zheng Wang, linux-usb, linux-kernel, 1395428693sheep, alex000young

Greg KH <gregkh@linuxfoundation.org> 于2023年3月24日周五 00:20写道:
>
> On Fri, Mar 10, 2023 at 03:00:39PM +0800, Zheng Wang wrote:
> > In udc_plat_probe, &udc->drd_work is bound with
> > udc_drd_work. udc_drd_work may be called by
> > usbd_connect_notify to start the work.
> >
> > Besides, there is a invoking chain:
> > udc_plat_probe
> > ->udc_probe
> > ->usb_add_gadget_udc_release
> > ->usb_add_gadget
> >
> > It will add a new gadget to the udc class driver
> >  list. In usb_add_gadget, it uses usb_udc_release
> >  as its release function, which will kfree(udc)
> >  to when destroying the gadget.
> >
> > If we remove the module which will call udc_plat_remove
> >   to make cleanup, there may be a unfinished work.
> > The possible sequence is as follows:
> >
> > Fix it by finishing the work before cleanup in the udc_plat_remove
> >
> > Fixes: 1b9f35adb0ff ("usb: gadget: udc: Add Synopsys UDC Platform driver")
> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> > ---
> >  drivers/usb/gadget/udc/snps_udc_plat.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/usb/gadget/udc/snps_udc_plat.c b/drivers/usb/gadget/udc/snps_udc_plat.c
> > index 8bbb89c80348..6228e178cc0a 100644
> > --- a/drivers/usb/gadget/udc/snps_udc_plat.c
> > +++ b/drivers/usb/gadget/udc/snps_udc_plat.c
> > @@ -230,6 +230,7 @@ static int udc_plat_remove(struct platform_device *pdev)
> >       struct udc *dev;
> >
> >       dev = platform_get_drvdata(pdev);
> > +     cancel_delayed_work_sync(&dev->drd_work);
> >
> >       usb_del_gadget_udc(&dev->gadget);
> >       /* gadget driver must not be registered */
> > --
> > 2.25.1
> >
>
> Please test this to verify that it actually works.
>

Hi,

Sorry for my late reply. I will try to simulate a device in qemu to test.

Best regards,
Zheng

> thanks,
>
> greg k-h

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

end of thread, other threads:[~2023-03-24 15:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-10  7:00 [PATCH] USB: gadget: udc: Fix use after free bug in udc_plat_remove due to race condition Zheng Wang
2023-03-23 16:20 ` Greg KH
2023-03-24 15:34   ` Zheng Hacker

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.