usb: typec: Fix double free in typec_altmode_put_partner

Message ID 20240102192948.42392-1-lk@c--e.de
State New
Headers
Series usb: typec: Fix double free in typec_altmode_put_partner |

Commit Message

Christian A. Ehrhardt Jan. 2, 2024, 7:29 p.m. UTC
  The altmode device nodes of a port partner and of cable
plugs hold a reference to the altmode of a port. The port's
altmode contains various back pointers but these do not
contribute to the reference count.

Thus, free the port's altmode device instead of doing a
double free on ourself.

Reported-By: Chris Bainbridge <chris.bainbridge@gmail.com>
Fixes: b17b7fe6dd5c (usb: typec: class: fix typec_altmode_put_partner to put plugs)
Cc: RD Babiera <rdbabiera@google.com>
Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: stable@vger.kernel.org
Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
---
 drivers/usb/typec/class.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  

Comments

RD Babiera Jan. 2, 2024, 8:56 p.m. UTC | #1
Hi Christian,

I believe commit 9c6b789e954fae73c548f39332bcc56bdf0d4373 would
need to be reverted to apply this patch, but I'm not sure if that's preferred
over submitting a new version of the problematic patch that combines this
solution instead. Regardless I was able to verify the refcount on my setup.

On Tue, Jan 2, 2024 at 11:30 AM Christian A. Ehrhardt <lk@c--e.de> wrote:
>
> The altmode device nodes of a port partner and of cable
> plugs hold a reference to the altmode of a port. The port's
> altmode contains various back pointers but these do not
> contribute to the reference count.
>
> Thus, free the port's altmode device instead of doing a
> double free on ourself.
>
> Reported-By: Chris Bainbridge <chris.bainbridge@gmail.com>
> Fixes: b17b7fe6dd5c (usb: typec: class: fix typec_altmode_put_partner to put plugs)
> Cc: RD Babiera <rdbabiera@google.com>
> Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: stable@vger.kernel.org
> Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>

Tested-by: RD Babiera <rdbabiera@google.com>

> ---
>  drivers/usb/typec/class.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 16a670828dde..2da19feacd91 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -263,11 +263,13 @@ static void typec_altmode_put_partner(struct altmode *altmode)
>  {
>         struct altmode *partner = altmode->partner;
>         struct typec_altmode *adev;
> +       struct typec_altmode *partner_adev;
>
>         if (!partner)
>                 return;
>
>         adev = &altmode->adev;
> +       partner_adev = &partner->adev;
>
>         if (is_typec_plug(adev->dev.parent)) {
>                 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
> @@ -276,7 +278,7 @@ static void typec_altmode_put_partner(struct altmode *altmode)
>         } else {
>                 partner->partner = NULL;
>         }
> -       put_device(&adev->dev);
> +       put_device(&partner_adev->dev);
>  }
>
>  /**
> --
> 2.40.1
>

Thanks,
---
rd
  

Patch

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 16a670828dde..2da19feacd91 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -263,11 +263,13 @@  static void typec_altmode_put_partner(struct altmode *altmode)
 {
 	struct altmode *partner = altmode->partner;
 	struct typec_altmode *adev;
+	struct typec_altmode *partner_adev;
 
 	if (!partner)
 		return;
 
 	adev = &altmode->adev;
+	partner_adev = &partner->adev;
 
 	if (is_typec_plug(adev->dev.parent)) {
 		struct typec_plug *plug = to_typec_plug(adev->dev.parent);
@@ -276,7 +278,7 @@  static void typec_altmode_put_partner(struct altmode *altmode)
 	} else {
 		partner->partner = NULL;
 	}
-	put_device(&adev->dev);
+	put_device(&partner_adev->dev);
 }
 
 /**