Linux USB
 help / color / mirror / Atom feed
* [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes
@ 2026-11-25 10:40 Neil Armstrong
  2026-11-25 10:40 ` [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery Neil Armstrong
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Neil Armstrong @ 2026-11-25 10:40 UTC (permalink / raw)
  To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
	Guenter Roeck, Igor Paunovic
  Cc: linux-usb, Neil Armstrong, stable, Sashiko

Two fixes for potential leaks where neither the partner
sink capabilities and altmodes were unregistered properly.

Signed-off-by: Neil Armstrong <neil.armstrong@linaro.org>
---
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/20261120-topic-tcpm-core-fixes-v1-0-c753d1d286bb@linaro.org

To: Badhri Jagan Sridharan <badhri@google.com>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Guenter Roeck <groeck@chromium.org>
To: Igor Paunovic <royalnet026@gmail.com>
Cc: linux-usb@vger.kernel.org

---
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,
--  
Neil Armstrong <neil.armstrong@linaro.org>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery
  2026-11-25 10:40 [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes Neil Armstrong
@ 2026-11-25 10:40 ` Neil Armstrong
  2026-11-25 10:57   ` sashiko-bot
  2026-11-25 10:40 ` [PATCH v2 2/3] usb: typec: tcpm: unregister stale partner sink capabilities Neil Armstrong
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Neil Armstrong @ 2026-11-25 10:40 UTC (permalink / raw)
  To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
	Guenter Roeck, Igor Paunovic
  Cc: linux-usb, Neil Armstrong, stable

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().

Cc: stable@vger.kernel.org
Fixes: f0690a25a140 ("staging: typec: USB Type-C Port Manager (tcpm)")
Assisted-by: LLM
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Neil Armstrong <neil.armstrong@linaro.org>
---
 drivers/usb/typec/tcpm/tcpm.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index ca38e05f831a..20e18bca649e 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -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;

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] usb: typec: tcpm: unregister stale partner sink capabilities
  2026-11-25 10:40 [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes Neil Armstrong
  2026-11-25 10:40 ` [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery Neil Armstrong
@ 2026-11-25 10:40 ` Neil Armstrong
  2026-11-25 10:40 ` [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port() Neil Armstrong
  2026-11-25 10:59 ` [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: Neil Armstrong @ 2026-11-25 10:40 UTC (permalink / raw)
  To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
	Guenter Roeck, Igor Paunovic
  Cc: linux-usb, Neil Armstrong, stable

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

Cc: stable@vger.kernel.org
Fixes: 8203d26905ee ("usb: typec: tcpm: Register USB Power Delivery Capabilities")
Assisted-by: LLM
Signed-off-by: Neil Armstrong <neil.armstrong@linaro.org>
---
 drivers/usb/typec/tcpm/tcpm.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 20e18bca649e..d0abe8100742 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -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))

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
  2026-11-25 10:40 [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes Neil Armstrong
  2026-11-25 10:40 ` [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery Neil Armstrong
  2026-11-25 10:40 ` [PATCH v2 2/3] usb: typec: tcpm: unregister stale partner sink capabilities Neil Armstrong
@ 2026-11-25 10:40 ` Neil Armstrong
  2026-11-25 10:59   ` sashiko-bot
  2026-11-25 10:59 ` [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes sashiko-bot
  3 siblings, 1 reply; 7+ messages in thread
From: Neil Armstrong @ 2026-11-25 10:40 UTC (permalink / raw)
  To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
	Guenter Roeck, Igor Paunovic
  Cc: linux-usb, Neil Armstrong, stable, Sashiko

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

Cc: stable@vger.kernel.org
Fixes: 98d00deb6ae4 ("usb: typec: tcpm: fix use-after-free of the kthread worker on port unregister")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-224916@kernel.org/
Signed-off-by: Neil Armstrong <neil.armstrong@linaro.org>
---
 drivers/usb/typec/tcpm/tcpm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index d0abe8100742..5e68ce2ea1ac 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -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++)

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery
  2026-11-25 10:40 ` [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery Neil Armstrong
@ 2026-11-25 10:57   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-25 10:57 UTC (permalink / raw)
  To: Neil Armstrong; +Cc: linux-usb

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 <neil.armstrong@linaro.org>

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().

> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index ca38e05f831a7..20e18bca649eb 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -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));

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-topic-tcpm-core-fixes-v2-0-aede2a3471f4@linaro.org?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
  2026-11-25 10:40 ` [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port() Neil Armstrong
@ 2026-11-25 10:59   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-25 10:59 UTC (permalink / raw)
  To: Neil Armstrong; +Cc: linux-usb

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 <neil.armstrong@linaro.org>

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.

> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index d0abe8100742f..5e68ce2ea1ace 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -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-free

Conversely, 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.

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-topic-tcpm-core-fixes-v2-0-aede2a3471f4@linaro.org?part=3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes
  2026-11-25 10:40 [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes Neil Armstrong
                   ` (2 preceding siblings ...)
  2026-11-25 10:40 ` [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port() Neil Armstrong
@ 2026-11-25 10:59 ` sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-25 10:59 UTC (permalink / raw)
  To: Neil Armstrong; +Cc: linux-usb

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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-topic-tcpm-core-fixes-v2-0-aede2a3471f4@linaro.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-25 10:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 10:40 [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes Neil Armstrong
2026-11-25 10:40 ` [PATCH v2 1/3] usb: typec: tcpm: unregister partner altmodes before re-discovery Neil Armstrong
2026-11-25 10:57   ` sashiko-bot
2026-11-25 10:40 ` [PATCH v2 2/3] usb: typec: tcpm: unregister stale partner sink capabilities Neil Armstrong
2026-11-25 10:40 ` [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port() Neil Armstrong
2026-11-25 10:59   ` sashiko-bot
2026-11-25 10:59 ` [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox