usb: tcpm: partner sink capabilities and altmodes leak fixes
Subsystems (4)
v2 cover usb: tcpm: partner sink capabilities and altmodes leak fixes
Two fixes for potential leaks where neither the partner sink capabilities and altmodes were unregistered properly.
---
Changes in v2:
- Add Fixes tag and review tag
- patch2: Unregister partner_sink_caps before usb_power_delivery_register_capabilities()
- Add new patch to fix a regression when calling tcpm_unregister_port() and the TCPM driver can call back from tcpm_reset_port()
- Link to v1: https://patch.msgid.link/[email protected]
To: Badhri Jagan Sridharan <[email protected]>
To: Heikki Krogerus <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
To: Guenter Roeck <[email protected]>
To: Igor Paunovic <[email protected]>
Cc: [email protected]
---
Neil Armstrong (3):
usb: typec: tcpm: unregister partner altmodes before re-discovery
usb: typec: tcpm: unregister stale partner sink capabilities
usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
drivers/usb/typec/tcpm/tcpm.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-)
---
base-commit: 3dd8c790a7b7027abdd568e36db5e82ffaea7c77
change-id: 20261120-topic-tcpm-core-fixes-5cb1a6a9838c
Best regards,
Signature · 1 line
Neil Armstrong <[email protected]>
PATCH v2 1/3 usb: typec: tcpm: unregister partner altmodes before re-discovery
svdm_consume_identity() clears port->mode_data when a Discover Identity ACK is received. mode_data.altmodes is also the count used to walk port->partner_altmode[] when unregistering the partner alternate modes, so if the partner altmodes from an earlier discovery are still registered, clearing it leaks them and the following Discover Modes round registers a second set for the same partner.
Unregister the partner altmodes before clearing mode_data, and reuse the new helper in tcpm_unregister_altmodes().
--- drivers/usb/typec/tcpm/tcpm.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +13 -4
@@ -2068,6 +2068,17 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,return ret; } +static void tcpm_unregister_partner_altmodes (struct tcpm_port *port) +{ +struct pd_mode_data *modep = &port->mode_data; +int i; + +for (i =0 ; i < modep->altmodes && i <ALTMODE_DISCOVERY_MAX ; i++) { +typec_unregister_altmode (port->partner_altmode[i]); + port->partner_altmode[i] =NULL ; + } +} +static void svdm_consume_identity (struct tcpm_port *port,const u32 *p,int cnt) {u32 vdo = p[VDO_INDEX_IDH ];
@@ -2076,6 +2087,7 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)if (cnt <=VDO_INDEX_PRODUCT )return ; +tcpm_unregister_partner_altmodes (port);memset (&port->mode_data,0 ,sizeof (port->mode_data)); port->partner_ident.id_header = vdo;
@@ -5137,10 +5149,7 @@ static void tcpm_unregister_altmodes(struct tcpm_port *port)struct pd_mode_data *modep_prime = &port->mode_data_prime;int i; -for (i =0 ; i < modep->altmodes && i <ALTMODE_DISCOVERY_MAX ; i++) { -typec_unregister_altmode (port->partner_altmode[i]); - port->partner_altmode[i] =NULL ; - } +tcpm_unregister_partner_altmodes (port);for (i =0 ; i < modep_prime->altmodes && i <ALTMODE_DISCOVERY_MAX ; i++) {typec_unregister_altmode (port->plug_prime_altmode[i]); port->plug_prime_altmode[i] =NULL ;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] usb: typec: tcpm: plug_prime_altmode leak and UAF in svdm_consume_identity() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] usb: typec: tcpm: plug_prime_altmode leak and UAF in svdm_consume_identity()
--
commit e6d007de8d361cfdd76de81da17f0dc6714fe900
Author: Neil Armstrong <[email protected]>
usb: typec: tcpm: unregister partner altmodes before re-discovery
This commit adds a helper to unregister partner alternate modes before svdm_consume_identity() clears port->mode_data, and reuses it in tcpm_unregister_altmodes().
Quoted 23 lines
drivers/usb/typec/tcpm/tcpm.c +11
-0@@ -2068,6 +2068,17 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,return ret; } +static void tcpm_unregister_partner_altmodes (struct tcpm_port *port) +{ +struct pd_mode_data *modep = &port->mode_data; +int i; + +for (i =0 ; i < modep->altmodes && i <ALTMODE_DISCOVERY_MAX ; i++) { +typec_unregister_altmode (port->partner_altmode[i]); + port->partner_altmode[i] =NULL ; + } +} +static void svdm_consume_identity (struct tcpm_port *port,const u32 *p,int cnt) {u32 vdo = p[VDO_INDEX_IDH ];
drivers/usb/typec/tcpm/tcpm.c static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) · line 2091
Quoted 5 lines
drivers/usb/typec/tcpm/tcpm.c +1
-0@@ -2076,6 +2087,7 @@ static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)if (cnt <=VDO_INDEX_PRODUCT )return ; +tcpm_unregister_partner_altmodes (port);memset (&port->mode_data,0 ,sizeof (port->mode_data));
[Severity: High]
Should svdm_consume_identity() call tcpm_unregister_altmodes(port) instead so that port->plug_prime_altmode[] and port->mode_data_prime are also cleaned up before re-discovery?
When tcpm_cable_vdm_supported(port) is true, partner altmode drivers such as dp_altmode_probe() allocate their private state with devm_kzalloc_obj(&alt->dev, *dp) on the partner altmode device and attach it to the plug altmode via typec_altmode_set_drvdata(plug, dp) and plug->cable_ops. Unregistering only port->partner_altmode[] frees that devm state while leaving port->plug_prime_altmode[] registered and reachable through typec_cable_altmode_vdm(), causing a use-after-free.
Once SOP Discover Modes finishes, tcpm_handle_discover_mode() also chains into SOP' CMD_DISCOVER_SVID and CMD_DISCOVER_MODES whenever tcpm_cable_vdm_supported(port) is true:
drivers/usb/typec/tcpm/tcpm.c:tcpm_handle_discover_mode() {
...
if (tcpm_cable_vdm_supported(port)) {
*response_tx_sop_type = TCPC_TX_SOP_PRIME;
response[0] = VDO(USB_SID_PD, 1,
typec_get_cable_svdm_version(typec),
CMD_DISCOVER_SVID);
tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_PARTNER_MODES);
return 1;
}
...
tcpm_register_plug_altmodes(port);
tcpm_register_partner_altmodes(port);
...
}Because port->mode_data_prime was not zeroed and port->plug_prime_altmode[] was not unregistered, SOP' discovery appends to stale mode_data_prime counts and tcpm_register_plug_altmodes() overwrites port->plug_prime_altmode[], leaking the previous plug altmode devices.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/3 usb: typec: tcpm: unregister stale partner sink capabilities
In reply to Neil Armstrongtcpm_register_sink_caps() registers a new "sink-capabilities" device under the partner USB PD object every time a Sink_Capabilities message is received, but the previously registered one is only removed on detach.
sink_cap_done is cleared on Hard Reset, so the sink capabilities are queried and registered again on the same partner: registering a second device with the same name under the same parent fails, and the stale capabilities stay exposed in sysfs.
Unregister the previous capabilities before registering the new ones.
--- drivers/usb/typec/tcpm/tcpm.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/usb/typec/tcpm/tcpm.c +5 -0
@@ -3559,6 +3559,11 @@ static int tcpm_register_sink_caps(struct tcpm_port *port)struct usb_power_delivery_capabilities_desc caps = { };struct usb_power_delivery_capabilities *cap; +if (port->partner_sink_caps) { +usb_power_delivery_unregister_capabilities (port->partner_sink_caps); + port->partner_sink_caps =NULL ; + } +if (!port->partner_pd) port->partner_pd =usb_power_delivery_register (NULL , &desc);if (IS_ERR (port->partner_pd))
Signature · 1 line
2.34.1
Re: [PATCH v2 2/3] usb: typec: tcpm: unregister stale partner sink capabilities
Quoted 10 lines · Neil Armstrong
tcpm_register_sink_caps()registers a new "sink-capabilities" device under the partner USB PD object every time aSink_Capabilitiesmessage is received, but the previously registered one is only removed on detach.
sink_cap_doneis cleared on Hard Reset, so the sink capabilities are queried and registered again on the same partner: registering a second device with the same name under the same parent fails, and the stale capabilities stay exposed in sysfs.
Quoted 6 lines
Unregister the previous capabilities before registering the new ones.
Fixes 8203d26905ee ("usb: typec: tcpm: Register USB Power Delivery Capabilities")Assisted-by LLMSigned-off-by Neil Armstrong <[email protected]>
Quoted 24 lines
--- drivers/usb/typec/tcpm/tcpm.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/usb/typec/tcpm/tcpm.c +5
-0@@ -3559,6 +3559,11 @@ static int tcpm_register_sink_caps(struct tcpm_port *port)struct usb_power_delivery_capabilities_desc caps = { };struct usb_power_delivery_capabilities *cap; +if (port->partner_sink_caps) { +usb_power_delivery_unregister_capabilities (port->partner_sink_caps); + port->partner_sink_caps =NULL ; + } +if (!port->partner_pd) port->partner_pd =usb_power_delivery_register (NULL , &desc);if (IS_ERR (port->partner_pd))Signature · 2 lines
-- 2.34.1
Signature · 1 line
heikki
PATCH v2 3/3 usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
In reply to Neil ArmstrongWhen calling tcpm_reset_port() after destroying the worker and the associated hrtimers, the TCPM implementation may still send some TCPM events and call back into the TCPM core and encounter a NULL port->wq.
Move the power_supply_put_system_batteries() & tcpm_reset_port() earlier, since it's the last function that calls into TCPM callback.
--- drivers/usb/typec/tcpm/tcpm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +4 -3
@@ -9025,6 +9025,10 @@ void tcpm_unregister_port(struct tcpm_port *port)int i; port->registered = false; + +power_supply_put_system_batteries (port->fixed_batt, port->fixed_batt_cnt); +tcpm_reset_port (port); +kthread_destroy_worker (port->wq); port->wq =NULL ;
@@ -9033,9 +9037,6 @@ void tcpm_unregister_port(struct tcpm_port *port)hrtimer_cancel (&port->vdm_state_machine_timer);hrtimer_cancel (&port->state_machine_timer); -power_supply_put_system_batteries (port->fixed_batt, port->fixed_batt_cnt); -tcpm_reset_port (port); -tcpm_port_unregister_pd (port);for (i =0 ; i <ARRAY_SIZE (port->port_altmode); i++)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: typec: tcpm: unlocked tcpm_reset_port() before worker destroy - [High] usb: typec: tcpm: UAF…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] usb: typec: tcpm: unlocked tcpm_reset_port() before worker destroy
- [High] usb: typec: tcpm: UAF and leak of fixed_batt in tcpm_unregister_port()
--
commit 67e3181a0501c55343c7ba43d0c183fa2c194d28
Author: Neil Armstrong <[email protected]>
usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
This commit moves power_supply_put_system_batteries() and tcpm_reset_port() before kthread_destroy_worker() in tcpm_unregister_port() so callbacks invoked during port reset do not encounter a destroyed worker.
drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 9029
Quoted 10 lines
drivers/usb/typec/tcpm/tcpm.c +2
-0@@ -9025,6 +9025,10 @@ void tcpm_unregister_port(struct tcpm_port *port)int i; port->registered = false; + +power_supply_put_system_batteries (port->fixed_batt, port->fixed_batt_cnt);
[Severity: High]
Can calling power_supply_put_system_batteries() before destroying port->wq cause a use-after-free or a leak of port->fixed_batt?
power_supply_put_system_batteries() drops the struct power_supply references and frees port->fixed_batt without holding port->lock or clearing port->fixed_batt_cnt, while port->wq is still running and about to be flushed:
CPU0 kworker (port->wq)tcpm_unregister_port()
power_supply_put_system_batteries()
kfree(port->fixed_batt)
tcpm_state_machine_work()
tcpm_pd_send_batt_status()
tcpm_get_fixed_batt()
/* fixed_batt_cnt > 0: returns */
batt = port->fixed_batt[batt_id];
<-- use-after-freeConversely, if port->fixed_batt_cnt is 0 when power_supply_put_system_batteries() runs, a concurrent or flushed work item calling tcpm_get_fixed_batt() will allocate port->fixed_batt and take battery references that are never released.
drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 9030
Quoted 1 line
drivers/usb/typec/tcpm/tcpm.c +1
-0+tcpm_reset_port (port);
[Severity: High]
Does running tcpm_reset_port() here without port->lock while port->wq is still active race with running works and allow flushed works to run after the reset?
Every other caller of tcpm_reset_port() holds port->lock. Running it unlocked while port->wq is still active allows tcpm_unregister_altmodes(), tcpm_typec_disconnect(), and partner PD teardown in tcpm_reset_port() to race with work items on port->wq, causing concurrent device_unregister() calls and use-after-free on partner, cable, plug, altmode, and PD capability objects:
CPU0 kworker (port->wq)tcpm_unregister_port()
tcpm_reset_port()
tcpm_typec_disconnect()
typec_unregister_partner(port->partner)
tcpm_state_machine_work()
...
tcpm_typec_disconnect()
typec_unregister_partner(port->partner)
<-- double unregister / UAF
port->partner = NULL;In addition, when tcpm_reset_port() triggers a synchronous TCPC callback such as qcom_pmic_typec_port_set_vbus() -> tcpm_vbus_change(), that callback queues port->event_work onto port->wq. kthread_destroy_worker() then flushes port->wq after tcpm_reset_port() has finished; because tcpm_reset_port() does not reset port->state and work handlers do not check port->registered, tcpm_pd_event_handler() -> _tcpm_pd_vbus_off() evaluates the pre-reset state with port->auto_vbus_discharge_enabled cleared and queues port->state_machine with delay 0, either hitting WARN_ON(!list_empty(&worker->work_list)) in kthread_destroy_worker() or re-running the state machine after port reset.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewSashiko has finished reviewing this 3-patch series:
Sashiko has finished reviewing this 3-patch series:
- Patch 2: no issues found.
- Patch 1 ("usb: typec: tcpm: unregister partner altmodes before..."): 1 potential issue (1 High)
- Patch 3 ("usb: typec: tcpm: destroy worker after reset_port()..."): 2 potential issues (2 High)
Detailed reviews have been sent as separate replies to patches 1 and 3.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]