Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vladyslav Tymoshchuk" <tvlad744@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 1/5] usb: typec: ucsi: glink: Handle a restart of the remote before the work runs
Date: Sun, 29 Nov 2026 14:01:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-239672@kernel.org> (raw)
In-Reply-To: <20261129134442.579-2-tvlad744@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: typec: ucsi: glink: use-after-free on UCSI re-registration
- [Medium] usb: typec: ucsi: glink: stale pd_running used in
  pmic_glink_ucsi_register()
- [Low] usb: typec: ucsi: glink: wrong Fixes commit in changelog
--

commit a32a3e07eca3f17ff775eaeede6e3c77bdeacb36
Author: Vladyslav Tymoshchuk <tvlad744@gmail.com>

> usb: typec: ucsi: glink: Handle a restart of the remote before the work runs
>
> When the remote service goes down and comes back up before the register
> work runs, the work finds the service running and UCSI registered, and
> does nothing. The restarted firmware is then never initialized, and no
> longer reports connector changes.
>
> Remember that the service went down, and register UCSI anew.
>
> Fixes: 62b5412b1f4a ("usb: typec: ucsi: add PMIC Glink UCSI driver")
> Signed-off-by: Vladyslav Tymoshchuk <tvlad744@gmail.com>

[Severity: Low]
Should the Fixes tag reference 11bb2ffb6793 ("usb: typec: ucsi: Move
unregister out of atomic section") instead?

In 62b5412b1f4a ("usb: typec: ucsi: add PMIC Glink UCSI driver"),
pmic_glink_ucsi_pdr_notify() called ucsi_unregister() directly when the
service went down.  11bb2ffb6793 ("usb: typec: ucsi: Move unregister out
of atomic section") deferred unregistration to pmic_glink_ucsi_register()
and introduced the pd_running and ucsi_registered checks.

> diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c
> index 2696e0b978f66..0535710271822 100644
> --- a/drivers/usb/typec/ucsi/ucsi_glink.c
> +++ b/drivers/usb/typec/ucsi/ucsi_glink.c
[ ... ]
> @@ -314,18 +315,22 @@ static void pmic_glink_ucsi_register(struct work_struct *work)
>  {
>  	struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
>  	unsigned long flags;
> -	bool pd_running;
> +	bool pd_running, pd_restarted;
>  
>  	spin_lock_irqsave(&ucsi->state_lock, flags);
>  	pd_running = ucsi->pd_running;
> +	pd_restarted = ucsi->pd_restarted;
> +	ucsi->pd_restarted = false;
>  	spin_unlock_irqrestore(&ucsi->state_lock, flags);
>  
> +	if (ucsi->ucsi_registered && (pd_restarted || !pd_running)) {
> +		ucsi_unregister(ucsi->ucsi);
> +		ucsi->ucsi_registered = false;
> +	}

[Severity: Medium]
Can pd_running become stale while ucsi_unregister() sleeps?

ucsi_unregister() sleeps while cancelling work and waiting on GLINK commands.
If the remote service goes down during that window, pmic_glink_ucsi_register()
continues with the earlier pd_running == true snapshot, calls ucsi_register()
against the down service, and sets ucsi->ucsi_registered = true.

> +
>  	if (!ucsi->ucsi_registered && pd_running) {
>  		ucsi_register(ucsi->ucsi);
>  		ucsi->ucsi_registered = true;
> -	} else if (ucsi->ucsi_registered && !pd_running) {
> -		ucsi_unregister(ucsi->ucsi);
> -		ucsi->ucsi_registered = false;
>  	}
>  }

[Severity: High]
Can re-registering the same ucsi->ucsi instance after ucsi_unregister()
cause a use-after-free or double free on ucsi->connector?

ucsi_unregister() frees ucsi->connector without clearing the pointer,
ucsi->ntfy, or EVENT_PENDING in ucsi->flags:

drivers/usb/typec/ucsi/ucsi.c:ucsi_unregister() {
    ...
	for (i = 0; i < ucsi->cap.num_connectors; i++) {
		cancel_work_sync(&ucsi->connector[i].work);
		ucsi_unregister_port(&ucsi->connector[i]);
		lockdep_unregister_key(&ucsi->connector[i].lock_key);
	}

	kfree(ucsi->connector);
}

Because ucsi_register() queues ucsi->work asynchronously, ucsi->connector
remains a dangling pointer and ucsi->ntfy retains
UCSI_ENABLE_NTFY_CONNECTOR_CHANGE until ucsi_init() assigns a new connector
array:

kworker (register_work / ucsi->work)   kworker (notify_work)
pmic_glink_ucsi_register()
  ucsi_unregister(ucsi->ucsi)
    kfree(ucsi->connector)
  ucsi_register(ucsi->ucsi)
    queue_delayed_work(..., &ucsi->work, 0)
                                       pmic_glink_ucsi_notify()
                                         ucsi_notify_common()
                                           ucsi_connector_change()
                                             con = &ucsi->connector[num - 1];
                                             schedule_work(&con->work)
                                               <-- use-after-free

In addition, if ucsi_register() fails (its return value is not checked), if
ucsi_init() fails before reallocating ucsi->connector, or if another
ucsi_unregister() runs before ucsi_init() finishes, the next
ucsi_unregister() sees the non-NULL ucsi->connector pointer, dereferences
the freed array, and calls kfree(ucsi->connector) a second time.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129134442.579-1-tvlad744@gmail.com?part=1

  reply	other threads:[~2026-11-29 14:01 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:44 [PATCH v2 0/5] soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 1/5] usb: typec: ucsi: glink: Handle a restart of the remote before the work runs Vladyslav Tymoshchuk
2026-11-29 14:01   ` sashiko-bot [this message]
2026-11-29 13:44 ` [PATCH v2 2/5] soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 3/5] soc: qcom: pmic_glink: Report wakeup events on behalf of clients Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 4/5] usb: typec: ucsi: glink: Wake up the system on connector changes Vladyslav Tymoshchuk
2026-11-29 13:44 ` [PATCH v2 5/5] power: supply: qcom_battmgr: Allow charger changes to wake up the system Vladyslav Tymoshchuk
2026-11-29 14:00   ` sashiko-bot
2026-11-29 14:01 ` [PATCH v2 0/5] soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue 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-239672@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tvlad744@gmail.com \
    /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