phy: tegra: xusb: Fix use-after-free issue

Message ID 20230504055446.2039372-1-haotienh@nvidia.com
State New
Headers
Series phy: tegra: xusb: Fix use-after-free issue |

Commit Message

HaoTien Hsu May 4, 2023, 5:54 a.m. UTC
  From: EJ Hsu <ejh@nvidia.com>

For the dual-role port, it will assign the phy dev to usb-phy dev and
use the port dev driver as the dev driver of usb-phy.

When we try to destroy the port dev, it will destroy its dev driver
as well. But we did not remove the reference from usb-phy dev. This
might cause the use-after-free issue in KASAN.

Fixes: e8f7d2f409a1 ("phy: tegra: xusb: Add usb-phy support")
Cc: stable@vger.kernel.org

Signed-off-by: EJ Hsu <ejh@nvidia.com>
Signed-off-by: Haotien Hsu <haotienh@nvidia.com>
---
 drivers/phy/tegra/xusb.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
  

Comments

Thierry Reding May 4, 2023, 9:15 a.m. UTC | #1
On Thu, May 04, 2023 at 01:54:46PM +0800, Haotien Hsu wrote:
> From: EJ Hsu <ejh@nvidia.com>
> 
> For the dual-role port, it will assign the phy dev to usb-phy dev and
> use the port dev driver as the dev driver of usb-phy.
> 
> When we try to destroy the port dev, it will destroy its dev driver
> as well. But we did not remove the reference from usb-phy dev. This
> might cause the use-after-free issue in KASAN.
> 
> Fixes: e8f7d2f409a1 ("phy: tegra: xusb: Add usb-phy support")
> Cc: stable@vger.kernel.org
> 
> Signed-off-by: EJ Hsu <ejh@nvidia.com>
> Signed-off-by: Haotien Hsu <haotienh@nvidia.com>
> ---
>  drivers/phy/tegra/xusb.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/phy/tegra/xusb.c b/drivers/phy/tegra/xusb.c
> index 78045bd6c214..515298a9a433 100644
> --- a/drivers/phy/tegra/xusb.c
> +++ b/drivers/phy/tegra/xusb.c
> @@ -563,13 +563,6 @@ static int tegra_xusb_port_init(struct tegra_xusb_port *port,
>  
>  static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
>  {
> -	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
> -		of_platform_depopulate(&port->dev);
> -		usb_role_switch_unregister(port->usb_role_sw);
> -		cancel_work_sync(&port->usb_phy_work);
> -		usb_remove_phy(&port->usb_phy);
> -	}
> -
>  	if (port->ops->remove)
>  		port->ops->remove(port);
>  
> @@ -832,6 +825,14 @@ void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
>  {
>  	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
>  
> +	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
> +		of_platform_depopulate(&port->dev);
> +		usb_role_switch_unregister(port->usb_role_sw);
> +		cancel_work_sync(&port->usb_phy_work);
> +		usb_remove_phy(&port->usb_phy);
> +		port->usb_phy.dev->driver = NULL;
> +	}
> +

It's a bit difficult to tell what exactly changed because of the
additional move. The move looks correct because this is only valid for
USB2 ports, but I think it'd be easier to review if the move was done
prior to the addition of the ->driver clearing. Or perhaps a mention in
the commit message would be nice.

It's obviously up to Vinod and Kishon if they want to see this
clarified. Either way is fine with me, so:

Acked-by: Thierry Reding <treding@nvidia.com>
  
Vinod Koul May 8, 2023, 8:57 a.m. UTC | #2
On 04-05-23, 11:15, Thierry Reding wrote:
> On Thu, May 04, 2023 at 01:54:46PM +0800, Haotien Hsu wrote:
> > From: EJ Hsu <ejh@nvidia.com>
> > 
> > For the dual-role port, it will assign the phy dev to usb-phy dev and
> > use the port dev driver as the dev driver of usb-phy.
> > 
> > When we try to destroy the port dev, it will destroy its dev driver
> > as well. But we did not remove the reference from usb-phy dev. This
> > might cause the use-after-free issue in KASAN.
> > 
> > Fixes: e8f7d2f409a1 ("phy: tegra: xusb: Add usb-phy support")
> > Cc: stable@vger.kernel.org
> > 
> > Signed-off-by: EJ Hsu <ejh@nvidia.com>
> > Signed-off-by: Haotien Hsu <haotienh@nvidia.com>
> > ---
> >  drivers/phy/tegra/xusb.c | 15 ++++++++-------
> >  1 file changed, 8 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/phy/tegra/xusb.c b/drivers/phy/tegra/xusb.c
> > index 78045bd6c214..515298a9a433 100644
> > --- a/drivers/phy/tegra/xusb.c
> > +++ b/drivers/phy/tegra/xusb.c
> > @@ -563,13 +563,6 @@ static int tegra_xusb_port_init(struct tegra_xusb_port *port,
> >  
> >  static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
> >  {
> > -	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
> > -		of_platform_depopulate(&port->dev);
> > -		usb_role_switch_unregister(port->usb_role_sw);
> > -		cancel_work_sync(&port->usb_phy_work);
> > -		usb_remove_phy(&port->usb_phy);
> > -	}
> > -
> >  	if (port->ops->remove)
> >  		port->ops->remove(port);
> >  
> > @@ -832,6 +825,14 @@ void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
> >  {
> >  	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
> >  
> > +	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
> > +		of_platform_depopulate(&port->dev);
> > +		usb_role_switch_unregister(port->usb_role_sw);
> > +		cancel_work_sync(&port->usb_phy_work);
> > +		usb_remove_phy(&port->usb_phy);
> > +		port->usb_phy.dev->driver = NULL;
> > +	}
> > +
> 
> It's a bit difficult to tell what exactly changed because of the
> additional move. The move looks correct because this is only valid for
> USB2 ports, but I think it'd be easier to review if the move was done
> prior to the addition of the ->driver clearing. Or perhaps a mention in
> the commit message would be nice.
> 
> It's obviously up to Vinod and Kishon if they want to see this
> clarified. Either way is fine with me, so:

I agree changes need to be clarified, expecting updated v2 :-)

> 
> Acked-by: Thierry Reding <treding@nvidia.com>
  

Patch

diff --git a/drivers/phy/tegra/xusb.c b/drivers/phy/tegra/xusb.c
index 78045bd6c214..515298a9a433 100644
--- a/drivers/phy/tegra/xusb.c
+++ b/drivers/phy/tegra/xusb.c
@@ -563,13 +563,6 @@  static int tegra_xusb_port_init(struct tegra_xusb_port *port,
 
 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
 {
-	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
-		of_platform_depopulate(&port->dev);
-		usb_role_switch_unregister(port->usb_role_sw);
-		cancel_work_sync(&port->usb_phy_work);
-		usb_remove_phy(&port->usb_phy);
-	}
-
 	if (port->ops->remove)
 		port->ops->remove(port);
 
@@ -832,6 +825,14 @@  void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
 {
 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
 
+	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
+		of_platform_depopulate(&port->dev);
+		usb_role_switch_unregister(port->usb_role_sw);
+		cancel_work_sync(&port->usb_phy_work);
+		usb_remove_phy(&port->usb_phy);
+		port->usb_phy.dev->driver = NULL;
+	}
+
 	regulator_put(usb2->supply);
 }