usb: typec: class: fix typec_altmode_put_partner to put plugs
Subsystems (3)
PATCH 1/1 usb: typec: class: fix typec_altmode_put_partner to put plugs
When releasing an Alt Mode, typec_altmode_release called by a plug device will not release the plug Alt Mode, meaning that a port will hold a reference to a plug Alt Mode even if the port partner is unregistered. As a result, typec_altmode_get_plug() can return an old plug altmode.
Currently, typec_altmode_put_partner does not raise issues when unregistering a partner altmode. Looking at the current implementation:
Quoted 3 lines
static void
typec_altmode_put_partner(struct altmode *altmode){ struct altmode *partner = altmode->partner;
When called by the partner Alt Mode, then partner evaluates to the port's Alt Mode. When called by the plug Alt Mode, this also evaluates to the port's Alt Mode.
Quoted 6 lines
struct typec_altmode *adev; if (!partner) return; adev = &partner->adev;
This always evaluates to the port's typec_altmode
Quoted 4 lines
if (is_typec_plug(adev->dev.parent)) {
struct typec_plug *plug = to_typec_plug(adev->dev.parent);
partner->plug[plug->index] = NULL;If the routine is called to put the plug's Alt mode and altmode refers to the plug, then adev referring to the port can never be a typec_plug. If altmode refers to the port, adev will always refer to the port partner, which runs the block below.
Quoted 5 lines
} else {
partner->partner = NULL;
}
put_device(&adev->dev);
}When calling typec_altmode_set_partner, a registration always calls get_device() on the port partner or the plug being registered, therefore typec_altmode_put_partner should put_device() the same device. By changing adev to altmode->adev, we make sure to put the correct device and properly unregister plugs. The reason port partners are always properly unregistered is because even when adev refers to the port, the port partner gets nullified in the else block. The port device currently gets put().
--- drivers/usb/typec/class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/class.c +1 -1
@@ -267,7 +267,7 @@ static void typec_altmode_put_partner(struct altmode *altmode)if (!partner)return ; - adev = &partner->adev; + adev = &altmode->adev;if (is_typec_plug (adev->dev.parent)) {struct typec_plug *plug =to_typec_plug (adev->dev.parent);
base-commit: b85ea95d086471afb4ad062012a4d73cd328fa86
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog
Heikki Krogerus
maintainerQuoted 63 lines · RD Babiera
When releasing an Alt Mode,
typec_altmode_releasecalled by a plug device will not release the plug Alt Mode, meaning that a port will hold a reference to a plug Alt Mode even if the port partner is unregistered. As a result,typec_altmode_get_plug()can return an old plug altmode.Currently,
typec_altmode_put_partnerdoes not raise issues when unregistering a partner altmode. Looking at the current implementation:Quoted 3 lines
static void
typec_altmode_put_partner(struct altmode *altmode){ struct altmode *partner = altmode->partner;When called by the partner Alt Mode, then partner evaluates to the port's Alt Mode. When called by the plug Alt Mode, this also evaluates to the port's Alt Mode.
Quoted 6 lines
struct typec_altmode *adev; if (!partner) return; adev = &partner->adev;This always evaluates to the port's
typec_altmodeQuoted 4 lines
if (is_typec_plug(adev->dev.parent)) { struct typec_plug *plug = to_typec_plug(adev->dev.parent); partner->plug[plug->index] = NULL;If the routine is called to put the plug's Alt mode and altmode refers to the plug, then adev referring to the port can never be a
typec_plug. If altmode refers to the port, adev will always refer to the port partner, which runs the block below.Quoted 5 lines
} else { partner->partner = NULL; } put_device(&adev->dev); }When calling
typec_altmode_set_partner, a registration always callsget_device()on the port partner or the plug being registered, thereforetypec_altmode_put_partnershouldput_device()the same device. By changing adev toaltmode->adev, we make sure to put the correct device and properly unregister plugs. The reason port partners are always properly unregistered is because even when adev refers to the port, the port partner gets nullified in the else block. The port device currently getsput().Fixes 8a37d87d72f0 ("usb: typec: Bus type for alternate modes")Signed-off-by RD Babiera <[email protected]>--- drivers/usb/typec/class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/usb/typec/class.c +0
-0
drivers/usb/typec/class.c static void typec_altmode_put_partner(struct altmode *altmode) · line 273
Quoted 8 lines
drivers/usb/typec/class.c +1
-1@@ -267,7 +267,7 @@ static void typec_altmode_put_partner(struct altmode *altmode)if (!partner)return ; - adev = &partner->adev; + adev = &altmode->adev;if (is_typec_plug (adev->dev.parent)) {struct typec_plug *plug =to_typec_plug (adev->dev.parent);
Sorry, I may have missed something, but do we need to call this function with ports at all?
static void typec_altmode_release(struct device *dev)
{
struct altmode *alt = to_altmode(to_typec_altmode(dev));- typec_altmode_put_partner(alt);
+ if (!is_typec_port(dev->parent))
+ typec_altmode_put_partner(alt);
altmode_id_remove(alt->adev.dev.parent, alt->id);
kfree(alt);
...thanks,
Signature · 1 line
heikki
Hi,
I found this mail in the archives after looking at a bug report that was bisected to the change that resulted from the following analysis:
https://lore.kernel.org/all/CAP-bSRb3SXpgo_BEdqZB-p1K5625fMegRZ17ZkPE1J8ZYgEHDg@mail.gmail.com/
AFAICS the analysis below is partially flawed
Quoted 29 lines · RD Babiera
When releasing an Alt Mode,
typec_altmode_releasecalled by a plug device will not release the plug Alt Mode, meaning that a port will hold a reference to a plug Alt Mode even if the port partner is unregistered. As a result,typec_altmode_get_plug()can return an old plug altmode.Currently,
typec_altmode_put_partnerdoes not raise issues when unregistering a partner altmode. Looking at the current implementation:Quoted 3 lines
static void
typec_altmode_put_partner(struct altmode *altmode){ struct altmode *partner = altmode->partner;When called by the partner Alt Mode, then partner evaluates to the port's Alt Mode. When called by the plug Alt Mode, this also evaluates to the port's Alt Mode.
Quoted 6 lines
struct typec_altmode *adev; if (!partner) return; adev = &partner->adev;This always evaluates to the port's
typec_altmodeQuoted 3 lines
if (is_typec_plug(adev->dev.parent)) { struct typec_plug *plug = to_typec_plug(adev->dev.parent);
Quoted 12 lines
Quoted 1 line
partner->plug[plug->index] = NULL;If the routine is called to put the plug's Alt mode and altmode refers to the plug, then adev referring to the port can never be a
typec_plug. If altmode refers to the port, adev will always refer to the port partner, which runs the block below.Quoted 5 lines
} else { partner->partner = NULL; } put_device(&adev->dev); }
So far everything is fine.
Quoted 2 lines
When calling
typec_altmode_set_partner, a registration always callsget_device()on the port partner or the plug being registered,
This is wrong. It is the altmode of the plug or partner that holds a reference to the altmode of the port not the other way around. The port's altmode has (back) pointers to the altmodes of its partner and the cable plugs but these are weak references that do not contribute to the refcount.
Quoted 2 lines
therefore
typec_altmode_put_partnershouldput_device()the same device. By changing
Thus this conclusion is wrong. The put_device() used to be correct.
Quoted 5 lines
adev to
altmode->adev, we make sure to put the correct device and properly unregister plugs. The reason port partners are always properly unregistered is because even when adev refers to the port, the port partner gets nullified in the else block. The port device currently getsput().
Please correct me if I missed something.
regards Christian
Heikki Krogerus
maintainerHi Christian,
Quoted 70 lines · Christian A. Ehrhardt
Hi,
I found this mail in the archives after looking at a bug report that was bisected to the change that resulted from the following analysis:
https://lore.kernel.org/all/CAP-bSRb3SXpgo_BEdqZB-p1K5625fMegRZ17ZkPE1J8ZYgEHDg@mail.gmail.com/
AFAICS the analysis below is partially flawed
Quoted 29 lines · RD Babiera
When releasing an Alt Mode,
typec_altmode_releasecalled by a plug device will not release the plug Alt Mode, meaning that a port will hold a reference to a plug Alt Mode even if the port partner is unregistered. As a result,typec_altmode_get_plug()can return an old plug altmode.Currently,
typec_altmode_put_partnerdoes not raise issues when unregistering a partner altmode. Looking at the current implementation:Quoted 3 lines
static void
typec_altmode_put_partner(struct altmode *altmode){ struct altmode *partner = altmode->partner;When called by the partner Alt Mode, then partner evaluates to the port's Alt Mode. When called by the plug Alt Mode, this also evaluates to the port's Alt Mode.
Quoted 6 lines
struct typec_altmode *adev; if (!partner) return; adev = &partner->adev;This always evaluates to the port's
typec_altmodeQuoted 3 lines
if (is_typec_plug(adev->dev.parent)) { struct typec_plug *plug = to_typec_plug(adev->dev.parent);Quoted 12 lines
Quoted 1 line
partner->plug[plug->index] = NULL;If the routine is called to put the plug's Alt mode and altmode refers to the plug, then adev referring to the port can never be a
typec_plug. If altmode refers to the port, adev will always refer to the port partner, which runs the block below.Quoted 5 lines
} else { partner->partner = NULL; } put_device(&adev->dev); }So far everything is fine.
Quoted 2 lines
When calling
typec_altmode_set_partner, a registration always callsget_device()on the port partner or the plug being registered,This is wrong. It is the altmode of the plug or partner that holds a reference to the altmode of the port not the other way around. The port's altmode has (back) pointers to the altmodes of its partner and the cable plugs but these are weak references that do not contribute to the refcount.
Quoted 2 lines
therefore
typec_altmode_put_partnershouldput_device()the same device. By changingThus this conclusion is wrong. The
put_device()used to be correct.
Quoted 7 lines
Quoted 5 lines
adev to
altmode->adev, we make sure to put the correct device and properly unregister plugs. The reason port partners are always properly unregistered is because even when adev refers to the port, the port partner gets nullified in the else block. The port device currently getsput().Please correct me if I missed something.
Thanks for checking this. Your analysis sounds correct to me.
RD, I think we need to revert the commmit. If you still see the original problem, please prepare a new patch.
thanks,
Signature · 1 line
heikki
Hi Heikki,
That sounds good to me. Christian had proposed a fix in another email thread, so I can post the patch after testing on my end.
Thanks again Christian for the analysis and fix.
Best,
---
RD
PATCH 1/1 usb: typec: Fix double free in typec_altmode_put_partner
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.
--- drivers/usb/typec/class.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +3 -1
@@ -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); }/**
Signature · 1 line
2.40.1
Re: [PATCH] usb: typec: Fix double free in typec_altmode_put_partner
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.
Quoted 9 lines · Christian A. Ehrhardt
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.
Quoted 7 lines
Reported-By Chris Bainbridge <[email protected]>Fixes b17b7fe6dd5c (usb: typec: class: fix typec_altmode_put_partner to put plugs)Cc RD Babiera <[email protected]>Cc Heikki Krogerus <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Signed-off-by Christian A. Ehrhardt <[email protected]>
Quoted 32 lines
--- drivers/usb/typec/class.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/usb/typec/class.c +3
-2@@ -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); }/** --
Quoted 2 lines
2.40.1
Thanks,
---
rd