* [PATCH] usb: typec: ucsi: Reset connector state in ucsi_unregister()
@ 2026-12-05 10:55 Zhendong Liu via B4 Relay
2026-12-05 11:15 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Zhendong Liu via B4 Relay @ 2026-12-05 10:55 UTC (permalink / raw)
To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck
Cc: Jameson Thies, Vladyslav Tymoshchuk, Bertrand Jacquin, linux-usb,
linux-kernel, stable, Sashiko, Zhendong Liu
From: Zhendong Liu <yzddmr6@gmail.com>
ucsi_unregister() frees the connector array but leaves ucsi->connector
pointing at it and ucsi->ntfy as ucsi_init() set it. When it cancels a
connector change work that has not run yet, it also leaves EVENT_PENDING
set, as only the work acknowledges the change and clears it.
That state outlives the registration. Three drivers unregister and then
register the same struct ucsi again: ucsi_glink when the remote
processor restarts, ucsi_ccg after flashing new firmware, and
ucsi_stm32g0 after a firmware update. The new registration then starts
with:
- A dangling ucsi->connector. If ucsi_init() fails before it publishes
a new array, for example because the PPM does not come back from the
reset, the next ucsi_unregister() walks the freed array and frees it
again. ucsi_resume() and the power debugfs files also dereference it.
- Connector change notifications that look enabled. A connector change
reported before ucsi_init() has registered the new connectors passes
the check in ucsi_connector_change() and indexes the freed array.
- EVENT_PENDING possibly still set, so ucsi_connector_change() drops
every later connector change, including the one that ucsi_init() reads
from the CCI after it enables the notifications.
Clear ucsi->ntfy before tearing down the connectors, clear EVENT_PENDING
when a connector work is cancelled before it ran, and set
ucsi->connector to NULL once the array is freed, so that a new
registration starts from the same state as the first one.
Notifications that race with ucsi_unregister() itself are not covered.
As before, the glue driver has to stop delivering them before it
unregisters.
This patch was prepared with the help of an AI coding assistant.
Fixes: e32fd989ac1c ("usb: typec: ucsi: ccg: Move to the new API")
Cc: stable <stable@kernel.org>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-239672@kernel.org/
Closes: https://lore.kernel.org/all/sashiko-outbox-248918@kernel.org/
Signed-off-by: Zhendong Liu <yzddmr6@gmail.com>
---
Found by reading the code, and reported by the Sashiko AI review on two
other UCSI patches (the Closes: links). I have not reproduced the
use-after-free and have no reproducer. Build-tested only:
drivers/usb/typec/ucsi/ with W=1 (arm64, all UCSI glue drivers as
modules) on the base commit below, no new warnings.
This neither depends on nor conflicts with Jameson's "usb: typec: ucsi:
Enable notifications before registering connectors" v2 [1]. With that
patch ucsi_connector_change() checks ucsi->connector, so the NULL store
here is what keeps an early notification out, and clearing ucsi->ntfy
becomes redundant but harmless.
After this patch, a read of the power debugfs files between a new
ucsi_register() and the end of ucsi_init() finds a NULL ucsi->connector
instead of the freed array. Bertrand's pending NULL check [2] covers
that case.
I used an AI coding assistant for this work. It traced the leftover
state through the glue drivers and drafted this patch and changelog. I
have reviewed the change.
[1] https://lore.kernel.org/all/20261204012637.2469388-1-jthies@google.com/
[2] https://lore.kernel.org/all/20261010120357.4937-1-bertrand@jacquin.bzh/
---
drivers/usb/typec/ucsi/ucsi.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index c605501aac5a..899708dccb62 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -2436,16 +2436,22 @@ void ucsi_unregister(struct ucsi *ucsi)
/* Disable notifications */
ucsi->ops->async_control(ucsi, cmd);
+ /* Ignore connector changes until ucsi_init() runs again */
+ ucsi->ntfy = 0;
+
if (!ucsi->connector)
return;
for (i = 0; i < ucsi->cap.num_connectors; i++) {
- cancel_work_sync(&ucsi->connector[i].work);
+ /* A cancelled connector change was never acknowledged */
+ if (cancel_work_sync(&ucsi->connector[i].work))
+ clear_bit(EVENT_PENDING, &ucsi->flags);
ucsi_unregister_port(&ucsi->connector[i]);
lockdep_unregister_key(&ucsi->connector[i].lock_key);
}
kfree(ucsi->connector);
+ ucsi->connector = NULL;
}
EXPORT_SYMBOL_GPL(ucsi_unregister);
---
base-commit: 8e6abce7f32e9ef6b4300f461cdc004a8997d93e
change-id: 20261205-upstream3-ucsi-unregister-reset-718f426b54f2
Best regards,
--
Zhendong Liu <yzddmr6@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 11:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:55 [PATCH] usb: typec: ucsi: Reset connector state in ucsi_unregister() Zhendong Liu via B4 Relay
2026-12-05 11:15 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox