Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sam Agazaryan" <samagazaryan@google.com>, linux-i3c@lists.infradead.org
Cc: Frank.Li@kernel.org, "Alexandre Belloni" <alexandre.belloni@bootlin.com>
Subject: Re: [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA
Date: Wed, 02 Dec 2026 02:48:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-244720@kernel.org> (raw)
In-Reply-To: <20261202022502.3661797-3-samagazaryan@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] i3c: master: use-after-free in i3c_generic_ibi_free_pool() on
  checked-out slot
- [Medium] i3c: master: UAF on async IBI wq teardown in
  __i3c_master_add_i3c_dev_locked()
--

commit 4b74a8ce04bb2a901a13cfbe1ec9e6788a44f476
Author: Sam Agazaryan <samagazaryan@google.com>

i3c: master: Preserve IBI workqueue across DAA

This commit avoids destroying the old device descriptor's IBI workqueue
under the bus maintenance lock during DAA by moving the IBI info and its
workqueue to the new descriptor and deferring workqueue destruction on
failure.

> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index 75c44c45bd08c..22fe970ce3ac1 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
[ ... ]
> @@ -2804,9 +2815,18 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
>  			 * a new address. This will result in NACK or timeout.
>  			 * So, update the olddev->ibi->enabled flag to false
>  			 * to avoid DISEC with OldAddr.
> +			 *
> +			 * Free only the controller IBI resources and hand
> +			 * olddev->ibi, workqueue included, over to newdev.
> +			 * Destroying the workqueue here would wait for any
> +			 * running IBI handler while we hold the bus
> +			 * maintenance lock, and the handler may be waiting
> +			 * for that lock.
>  			 */
>  			olddev->ibi->enabled = false;
> -			i3c_dev_free_ibi_locked(olddev);
> +			i3c_dev_free_ibi_controller_locked(olddev);
> +			old_ibi = olddev->ibi;
> +			olddev->ibi = NULL;
>  		}
>  		mutex_unlock(&olddev->ibi_lock);

[Severity: High]
Can freeing olddev's controller IBI pool here cause a use-after-free on
pool and pool->payload_buf if the controller still holds a slot checked
out via i3c_generic_ibi_get_free_slot()?

Commit "i3c: master: Drain IBI slot pool before freeing" adds a
pool->stopping check in i3c_master_queue_ibi() to recycle a slot that was
checked out before i3c_generic_ibi_free_pool() ran, but
i3c_generic_ibi_free_pool() only drains work items already queued on
pool->wq.  A checked-out slot that has not been passed to
i3c_master_queue_ibi() yet is neither on pool->free_slots nor queued on
pool->wq.  For example, mipi-i3c-hci checks out pio->ibi.slot in
hci_pio_prep_new_ibi() and holds it across interrupts when an IBI spans
multiple segments, while hci_pio_free_ibi() calls
i3c_generic_ibi_free_pool() without clearing pio->ibi.slot:

CPU0                                  CPU1
hci_pio_irq_handler()
  hci_pio_prep_new_ibi()
    slot = i3c_generic_ibi_get_free_slot(pool)
  (awaits next IBI segment)
                                      __i3c_master_add_i3c_dev_locked()
                                        i3c_dev_free_ibi_controller_locked()
                                          ...
                                          i3c_generic_ibi_free_pool(pool)
                                            (pool->wq is NULL, or
                                             free_work runs on pool->wq)
                                            i3c_generic_ibi_destroy_pool(pool)
                                              WARN_ON(nslots != pool->num_slots)
                                              kfree(pool->payload_buf)
                                              kfree(pool)
hci_pio_irq_handler()
  writes into slot->data  <-- use-after-free
  i3c_master_queue_ibi(..., slot)  <-- use-after-free

The same race also exists in svc-i3c-master, where svc_i3c_master_ibi_isr()
checks out and queues a slot under master->xferqueue.lock while
svc_i3c_master_free_ibi() only takes master->ibi.lock before calling
i3c_generic_ibi_free_pool().

> @@ -2860,13 +2880,17 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
>  	 * should request the IBI again instead of trying to hide that from
>  	 * him.
>  	 */
> -	if (ibireq.handler) {
> +	if (old_ibi) {
>  		mutex_lock(&newdev->ibi_lock);
> -		ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
> +		newdev->ibi = old_ibi;
> +		ret = i3c_dev_request_ibi_controller_locked(newdev, &ibireq);
>  		if (ret) {
>  			dev_err(&master->dev,
>  				"Failed to request IBI on device %d-%llx",
>  				master->bus.id, newdev->info.pid);
> +			newdev->ibi = NULL;
> +			/* destroy_workqueue() waits, so not under the bus lock */
> +			queue_work(system_dfl_wq, &old_ibi->destroy_work);
>  		} else if (enable_ibi) {
>  			ret = i3c_dev_enable_ibi_locked(newdev);
>  			if (ret)

[Severity: Medium]
If i3c_dev_request_ibi_controller_locked() fails while an IBI handler is
still running on old_ibi->wq, can setting newdev->ibi = NULL and deferring
destroy_workqueue() to system_dfl_wq cause a use-after-free on driver
teardown?

Once newdev->ibi is cleared to NULL, a subsequent driver unbind or
i3c_master_unregister() calls i3c_device_disable_ibi() and
i3c_device_free_ibi(), both of which see dev->ibi == NULL and return
immediately without waiting for old_ibi->wq to drain:

CPU0 (old_ibi->wq)                CPU1
i3c_master_handle_ibi()
  ibi->handler(i3cdev, &payload)
    mctp_i3c_ibi_handler()
      (blocked on bus lock)
                                  __i3c_master_add_i3c_dev_locked()
                                    i3c_dev_request_ibi_controller_locked()
                                      (fails)
                                    newdev->ibi = NULL
                                    queue_work(..., &old_ibi->destroy_work)
                                  (releases bus lock)
                                  mctp_i3c_remove_device()
                                    i3c_device_disable_ibi() -> no-op
                                    i3c_device_free_ibi() -> no-op
                                    kfree(mi)
      ...
      accesses mi  <-- use-after-free

For the same reason, i3c_master_unregister_i3c_devs() can drop the final
reference on i3cdev->dev while ibi->handler(i3cdev, &payload) is still
using i3cdev.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202022502.3661797-1-samagazaryan@google.com?part=2

-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

      reply	other threads:[~2026-12-02  2:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:25 [PATCH RFC 0/2] i3c: master: Fix IBI teardown when DAA re-adds a device Sam Agazaryan
2026-12-02  2:25 ` [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing Sam Agazaryan
2026-12-02  2:44   ` sashiko-bot
2026-12-02  2:25 ` [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA Sam Agazaryan
2026-12-02  2:48   ` sashiko-bot [this message]

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-244720@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-i3c@lists.infradead.org \
    --cc=samagazaryan@google.com \
    --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