diff mbox series

[2/2] USB: dwc3: fix use-after-free on core driver unbind

Message ID 20230607100540.31045-3-johan+linaro@kernel.org (mailing list archive)
State Accepted
Commit e3dbb657571509044be15184a13134fa7c1fdca1
Headers show
Series USB: dwc3: qcom: fix NULL-deref on suspend | expand

Commit Message

Johan Hovold June 7, 2023, 10:05 a.m. UTC
Some dwc3 glue drivers are currently accessing the driver data of the
child core device directly, which is clearly a bad idea as the child may
not have probed yet or may have been unbound from its driver.

As a workaround until the glue drivers have been fixed, clear the driver
data pointer before allowing the glue parent device to runtime suspend
to prevent its driver from accessing data that has been freed during
unbind.

Fixes: 6dd2565989b4 ("usb: dwc3: add imx8mp dwc3 glue layer driver")
Fixes: 6895ea55c385 ("usb: dwc3: qcom: Configure wakeup interrupts during suspend")
Cc: stable@vger.kernel.org      # 5.12
Cc: Li Jun <jun.li@nxp.com>
Cc: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
Cc: Krishna Kurapati <quic_kriskura@quicinc.com>
Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
---
 drivers/usb/dwc3/core.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

'Manivannan Sadhasivam' June 8, 2023, 1:02 p.m. UTC | #1
On Wed, Jun 07, 2023 at 12:05:40PM +0200, Johan Hovold wrote:
> Some dwc3 glue drivers are currently accessing the driver data of the
> child core device directly, which is clearly a bad idea as the child may
> not have probed yet or may have been unbound from its driver.
> 
> As a workaround until the glue drivers have been fixed, clear the driver
> data pointer before allowing the glue parent device to runtime suspend
> to prevent its driver from accessing data that has been freed during
> unbind.
> 
> Fixes: 6dd2565989b4 ("usb: dwc3: add imx8mp dwc3 glue layer driver")
> Fixes: 6895ea55c385 ("usb: dwc3: qcom: Configure wakeup interrupts during suspend")
> Cc: stable@vger.kernel.org      # 5.12
> Cc: Li Jun <jun.li@nxp.com>
> Cc: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> Cc: Krishna Kurapati <quic_kriskura@quicinc.com>
> Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
> ---
>  drivers/usb/dwc3/core.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 7b2ce013cc5b..d68958e151a7 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
>  	pm_runtime_disable(&pdev->dev);
>  	pm_runtime_dont_use_autosuspend(&pdev->dev);
>  	pm_runtime_put_noidle(&pdev->dev);
> +	/*
> +	 * HACK: Clear the driver data, which is currently accessed by parent
> +	 * glue drivers, before allowing the parent to suspend.
> +	 */
> +	platform_set_drvdata(pdev, NULL);

This is required because you have seen the glue driver going to runtime suspend
once the below pm_runtime_set_suspended() is completed?

- Mani

>  	pm_runtime_set_suspended(&pdev->dev);
>  
>  	dwc3_free_event_buffers(dwc);
> -- 
> 2.39.3
>
Johan Hovold June 8, 2023, 1:09 p.m. UTC | #2
On Thu, Jun 08, 2023 at 06:32:46PM +0530, Manivannan Sadhasivam wrote:
> On Wed, Jun 07, 2023 at 12:05:40PM +0200, Johan Hovold wrote:
> > Some dwc3 glue drivers are currently accessing the driver data of the
> > child core device directly, which is clearly a bad idea as the child may
> > not have probed yet or may have been unbound from its driver.
> > 
> > As a workaround until the glue drivers have been fixed, clear the driver
> > data pointer before allowing the glue parent device to runtime suspend
> > to prevent its driver from accessing data that has been freed during
> > unbind.

> > @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
> >  	pm_runtime_disable(&pdev->dev);
> >  	pm_runtime_dont_use_autosuspend(&pdev->dev);
> >  	pm_runtime_put_noidle(&pdev->dev);
> > +	/*
> > +	 * HACK: Clear the driver data, which is currently accessed by parent
> > +	 * glue drivers, before allowing the parent to suspend.
> > +	 */
> > +	platform_set_drvdata(pdev, NULL);
> 
> This is required because you have seen the glue driver going to runtime suspend
> once the below pm_runtime_set_suspended() is completed?

This is based on analysis of the code. The parent (glue) can not suspend
while the child (core) is in the active state, but once we set the
suspended state that could happen.

> >  
> >  	dwc3_free_event_buffers(dwc);

Johan
'Manivannan Sadhasivam' June 8, 2023, 1:23 p.m. UTC | #3
On Thu, Jun 08, 2023 at 03:09:27PM +0200, Johan Hovold wrote:
> On Thu, Jun 08, 2023 at 06:32:46PM +0530, Manivannan Sadhasivam wrote:
> > On Wed, Jun 07, 2023 at 12:05:40PM +0200, Johan Hovold wrote:
> > > Some dwc3 glue drivers are currently accessing the driver data of the
> > > child core device directly, which is clearly a bad idea as the child may
> > > not have probed yet or may have been unbound from its driver.
> > > 
> > > As a workaround until the glue drivers have been fixed, clear the driver
> > > data pointer before allowing the glue parent device to runtime suspend
> > > to prevent its driver from accessing data that has been freed during
> > > unbind.
> 
> > > @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
> > >  	pm_runtime_disable(&pdev->dev);
> > >  	pm_runtime_dont_use_autosuspend(&pdev->dev);
> > >  	pm_runtime_put_noidle(&pdev->dev);
> > > +	/*
> > > +	 * HACK: Clear the driver data, which is currently accessed by parent
> > > +	 * glue drivers, before allowing the parent to suspend.
> > > +	 */
> > > +	platform_set_drvdata(pdev, NULL);
> > 
> > This is required because you have seen the glue driver going to runtime suspend
> > once the below pm_runtime_set_suspended() is completed?
> 
> This is based on analysis of the code. The parent (glue) can not suspend
> while the child (core) is in the active state, but once we set the
> suspended state that could happen.
> 

I could see that the driver core is setting drvdata to NULL during
device_unbind_cleanup(), so not sure if this scenario could be met otherwise it
will be redundant.

- Mani


> > >  
> > >  	dwc3_free_event_buffers(dwc);
> 
> Johan
Johan Hovold June 8, 2023, 1:49 p.m. UTC | #4
On Thu, Jun 08, 2023 at 06:53:13PM +0530, Manivannan Sadhasivam wrote:
> On Thu, Jun 08, 2023 at 03:09:27PM +0200, Johan Hovold wrote:
> > On Thu, Jun 08, 2023 at 06:32:46PM +0530, Manivannan Sadhasivam wrote:
> > > On Wed, Jun 07, 2023 at 12:05:40PM +0200, Johan Hovold wrote:
> > > > Some dwc3 glue drivers are currently accessing the driver data of the
> > > > child core device directly, which is clearly a bad idea as the child may
> > > > not have probed yet or may have been unbound from its driver.
> > > > 
> > > > As a workaround until the glue drivers have been fixed, clear the driver
> > > > data pointer before allowing the glue parent device to runtime suspend
> > > > to prevent its driver from accessing data that has been freed during
> > > > unbind.
> > 
> > > > @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
> > > >  	pm_runtime_disable(&pdev->dev);
> > > >  	pm_runtime_dont_use_autosuspend(&pdev->dev);
> > > >  	pm_runtime_put_noidle(&pdev->dev);
> > > > +	/*
> > > > +	 * HACK: Clear the driver data, which is currently accessed by parent
> > > > +	 * glue drivers, before allowing the parent to suspend.
> > > > +	 */
> > > > +	platform_set_drvdata(pdev, NULL);
> > > 
> > > This is required because you have seen the glue driver going to runtime suspend
> > > once the below pm_runtime_set_suspended() is completed?
> > 
> > This is based on analysis of the code. The parent (glue) can not suspend
> > while the child (core) is in the active state, but once we set the
> > suspended state that could happen.
> 
> I could see that the driver core is setting drvdata to NULL during
> device_unbind_cleanup(), so not sure if this scenario could be met otherwise it
> will be redundant.

If this was redundant I wouldn't have added it. ;)

The parent driver has no business accessing the driver data of the child
in the first place, but it must absolutely not do so after the child has
been unbound from its driver and the driver data is getting freed.

Relying on the clean up in driver core that resets this pointer does not
work as that would still leave a window where the parent could access
this stale data.

Johan
'Manivannan Sadhasivam' June 8, 2023, 3:16 p.m. UTC | #5
On Thu, Jun 08, 2023 at 03:49:58PM +0200, Johan Hovold wrote:
> On Thu, Jun 08, 2023 at 06:53:13PM +0530, Manivannan Sadhasivam wrote:
> > On Thu, Jun 08, 2023 at 03:09:27PM +0200, Johan Hovold wrote:
> > > On Thu, Jun 08, 2023 at 06:32:46PM +0530, Manivannan Sadhasivam wrote:
> > > > On Wed, Jun 07, 2023 at 12:05:40PM +0200, Johan Hovold wrote:
> > > > > Some dwc3 glue drivers are currently accessing the driver data of the
> > > > > child core device directly, which is clearly a bad idea as the child may
> > > > > not have probed yet or may have been unbound from its driver.
> > > > > 
> > > > > As a workaround until the glue drivers have been fixed, clear the driver
> > > > > data pointer before allowing the glue parent device to runtime suspend
> > > > > to prevent its driver from accessing data that has been freed during
> > > > > unbind.
> > > 
> > > > > @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
> > > > >  	pm_runtime_disable(&pdev->dev);
> > > > >  	pm_runtime_dont_use_autosuspend(&pdev->dev);
> > > > >  	pm_runtime_put_noidle(&pdev->dev);
> > > > > +	/*
> > > > > +	 * HACK: Clear the driver data, which is currently accessed by parent
> > > > > +	 * glue drivers, before allowing the parent to suspend.
> > > > > +	 */
> > > > > +	platform_set_drvdata(pdev, NULL);
> > > > 
> > > > This is required because you have seen the glue driver going to runtime suspend
> > > > once the below pm_runtime_set_suspended() is completed?
> > > 
> > > This is based on analysis of the code. The parent (glue) can not suspend
> > > while the child (core) is in the active state, but once we set the
> > > suspended state that could happen.
> > 
> > I could see that the driver core is setting drvdata to NULL during
> > device_unbind_cleanup(), so not sure if this scenario could be met otherwise it
> > will be redundant.
> 
> If this was redundant I wouldn't have added it. ;)
> 
> The parent driver has no business accessing the driver data of the child
> in the first place, but it must absolutely not do so after the child has
> been unbound from its driver and the driver data is getting freed.
> 
> Relying on the clean up in driver core that resets this pointer does not
> work as that would still leave a window where the parent could access
> this stale data.
> 

Well, I agree with the small window here.

Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

- Mani

> Johan
Thinh Nguyen June 9, 2023, 12:33 a.m. UTC | #6
On Wed, Jun 07, 2023, Johan Hovold wrote:
> Some dwc3 glue drivers are currently accessing the driver data of the
> child core device directly, which is clearly a bad idea as the child may
> not have probed yet or may have been unbound from its driver.
> 
> As a workaround until the glue drivers have been fixed, clear the driver
> data pointer before allowing the glue parent device to runtime suspend
> to prevent its driver from accessing data that has been freed during
> unbind.
> 
> Fixes: 6dd2565989b4 ("usb: dwc3: add imx8mp dwc3 glue layer driver")
> Fixes: 6895ea55c385 ("usb: dwc3: qcom: Configure wakeup interrupts during suspend")
> Cc: stable@vger.kernel.org      # 5.12
> Cc: Li Jun <jun.li@nxp.com>
> Cc: Sandeep Maheswaram <quic_c_sanm@quicinc.com>
> Cc: Krishna Kurapati <quic_kriskura@quicinc.com>
> Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
> ---
>  drivers/usb/dwc3/core.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 7b2ce013cc5b..d68958e151a7 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -1929,6 +1929,11 @@ static int dwc3_remove(struct platform_device *pdev)
>  	pm_runtime_disable(&pdev->dev);
>  	pm_runtime_dont_use_autosuspend(&pdev->dev);
>  	pm_runtime_put_noidle(&pdev->dev);
> +	/*
> +	 * HACK: Clear the driver data, which is currently accessed by parent
> +	 * glue drivers, before allowing the parent to suspend.
> +	 */
> +	platform_set_drvdata(pdev, NULL);
>  	pm_runtime_set_suspended(&pdev->dev);
>  
>  	dwc3_free_event_buffers(dwc);
> -- 
> 2.39.3
> 

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Thanks,
Thinh
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 7b2ce013cc5b..d68958e151a7 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -1929,6 +1929,11 @@  static int dwc3_remove(struct platform_device *pdev)
 	pm_runtime_disable(&pdev->dev);
 	pm_runtime_dont_use_autosuspend(&pdev->dev);
 	pm_runtime_put_noidle(&pdev->dev);
+	/*
+	 * HACK: Clear the driver data, which is currently accessed by parent
+	 * glue drivers, before allowing the parent to suspend.
+	 */
+	platform_set_drvdata(pdev, NULL);
 	pm_runtime_set_suspended(&pdev->dev);
 
 	dwc3_free_event_buffers(dwc);