[v3] usbip: vudc: Fix use after free bug in vudc_remove due to race condition

Message ID 20230316174432.1592087-1-zyytlz.wz@163.com
State New
Headers
Series [v3] usbip: vudc: Fix use after free bug in vudc_remove due to race condition |

Commit Message

Zheng Wang March 16, 2023, 5:44 p.m. UTC
  In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.

When it calls usbip_sockfd_store, it will call v_start_timer to start the 
timer work.

When we call vudc_remove to remove the driver, theremay be a sequence as 
follows:

Fix it by shutdown the timer work before cleanup in vudc_remove.

Note that removing a driver is a root-only operation, and should never
happen.

CPU0                  CPU1

                     |v_timer
vudc_remove          |
kfree(udc);          |
//free shost         |
                     |udc->gadget
                     |//use

Fixes: b6a0ca111867 ("usbip: vudc: Add UDC specific ops")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
 drivers/usb/usbip/vudc_dev.c | 1 +
 1 file changed, 1 insertion(+)
  

Comments

Zheng Hacker March 16, 2023, 5:47 p.m. UTC | #1
Sorry this is not v3 but v1, I will resubmit v1 again. Sorry again for
my mistake.

Thanks,
Zheng

Zheng Wang <zyytlz.wz@163.com> 于2023年3月17日周五 01:44写道:
>
> In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.
>
> When it calls usbip_sockfd_store, it will call v_start_timer to start the
> timer work.
>
> When we call vudc_remove to remove the driver, theremay be a sequence as
> follows:
>
> Fix it by shutdown the timer work before cleanup in vudc_remove.
>
> Note that removing a driver is a root-only operation, and should never
> happen.
>
> CPU0                  CPU1
>
>                      |v_timer
> vudc_remove          |
> kfree(udc);          |
> //free shost         |
>                      |udc->gadget
>                      |//use
>
> Fixes: b6a0ca111867 ("usbip: vudc: Add UDC specific ops")
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
>  drivers/usb/usbip/vudc_dev.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/usb/usbip/vudc_dev.c b/drivers/usb/usbip/vudc_dev.c
> index 2bc428f2e261..33d0991755bb 100644
> --- a/drivers/usb/usbip/vudc_dev.c
> +++ b/drivers/usb/usbip/vudc_dev.c
> @@ -633,6 +633,7 @@ int vudc_remove(struct platform_device *pdev)
>  {
>         struct vudc *udc = platform_get_drvdata(pdev);
>
> +       timer_shutdown_sync(&udc->timer);
>         usb_del_gadget_udc(&udc->gadget);
>         cleanup_vudc_hw(udc);
>         kfree(udc);
> --
> 2.25.1
>
  
Shuah Khan March 16, 2023, 5:57 p.m. UTC | #2
On 3/16/23 11:44, Zheng Wang wrote:
> In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.
> 
> When it calls usbip_sockfd_store, it will call v_start_timer to start the
> timer work.
> 
> When we call vudc_remove to remove the driver, theremay be a sequence as
> follows:
> 

When you resend the patch as you indicated would, please add details
on how you found this proble,m.

> Fix it by shutdown the timer work before cleanup in vudc_remove.
> 

thanks,
-- Shuah
  
Zheng Hacker March 16, 2023, 6:02 p.m. UTC | #3
Shuah Khan <skhan@linuxfoundation.org> 于2023年3月17日周五 01:57写道:
>
> On 3/16/23 11:44, Zheng Wang wrote:
> > In vudc_probe, it calls init_vudc_hw, which bound &udc->timer with v_timer.
> >
> > When it calls usbip_sockfd_store, it will call v_start_timer to start the
> > timer work.
> >
> > When we call vudc_remove to remove the driver, theremay be a sequence as
> > follows:
> >
>
> When you resend the patch as you indicated would, please add details
> on how you found this proble,m.
>

Get it, I found it by static analysis. So this might be false postive.

Best regards,
Zheng

> > Fix it by shutdown the timer work before cleanup in vudc_remove.
> >
>
> thanks,
> -- Shuah
>
  

Patch

diff --git a/drivers/usb/usbip/vudc_dev.c b/drivers/usb/usbip/vudc_dev.c
index 2bc428f2e261..33d0991755bb 100644
--- a/drivers/usb/usbip/vudc_dev.c
+++ b/drivers/usb/usbip/vudc_dev.c
@@ -633,6 +633,7 @@  int vudc_remove(struct platform_device *pdev)
 {
 	struct vudc *udc = platform_get_drvdata(pdev);
 
+	timer_shutdown_sync(&udc->timer);
 	usb_del_gadget_udc(&udc->gadget);
 	cleanup_vudc_hw(udc);
 	kfree(udc);