Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Neil Armstrong" <neil.armstrong@linaro.org>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 3/3] usb: typec: tcpm: destroy worker after reset_port() in unregister_port()
Date: Wed, 25 Nov 2026 10:59:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-233181@kernel.org> (raw)
In-Reply-To: <20261125-topic-tcpm-core-fixes-v2-3-aede2a3471f4@linaro.org>

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

  reply	other threads:[~2026-11-25 10:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-25 10:59 ` [PATCH v2 0/3] usb: tcpm: partner sink capabilities and altmodes leak fixes sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233181@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox