Re: [PATCH] usb: typec: Fix double free in typec_altmode_put_partner

From: RD Babiera
Date: Tue Jan 02 2024 - 15:57:20 EST


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@xxxxxxx> 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@xxxxxxxxx>
> Fixes: b17b7fe6dd5c (usb: typec: class: fix typec_altmode_put_partner to put plugs)
> Cc: RD Babiera <rdbabiera@xxxxxxxxxx>
> Cc: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
> Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Christian A. Ehrhardt <lk@xxxxxxx>

Tested-by: RD Babiera <rdbabiera@xxxxxxxxxx>

> ---
> 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