All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pei Xiao" <xiaopei01@kylinos.cn>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH] PCI: switchtec: Fix use-after-free in switchtec_pci_remove due to race condition
Date: Tue, 04 Aug 2026 03:44:06 +0000	[thread overview]
Message-ID: <20260804034406.C5D631F000E9@smtp.kernel.org> (raw)
In-Reply-To: <6fdcbfa869f707cad783c7f92d1681f0a2bd9a7c.1785813784.git.xiaopei01@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Incomplete fix for Use-After-Free: `cancel_work_sync()` is called without synchronizing or freeing active IRQ handlers, allowing ISRs to execute after `stdev` is freed and re-schedule works.
- [High] Incorrect cancellation ordering allows `mrpc_timeout` delayed work to be re-armed by concurrent userspace IO after `cancel_delayed_work_sync()`, leading to Use-After-Free.
--

--- Patch [1]: [PATCH] PCI: switchtec: Fix use-after-free in switchtec_pci_remove due to race condition ---
commit e4d2baee32009f95ba28f6ef409e3f3b5a2a15e3
Author: Pei Xiao <xiaopei01@kylinos.cn>
PCI: switchtec: Fix use-after-free in switchtec_pci_remove due to race condition

This commit attempts to fix a use-after-free bug by canceling the mrpc_work
and link_event_work works during stdev_kill(). It ensures works are canceled
after pci_clear_master() stops the endpoint from issuing further interrupts.

> diff --git a/drivers/pci/switch/switchtec.c b/drivers/pci/switch/switchtec.c
> index 41fc4b5127081..8426d5a9c1c7d 100644
> --- a/drivers/pci/switch/switchtec.c
> +++ b/drivers/pci/switch/switchtec.c
> @@ -1319,6 +1319,8 @@ static void stdev_kill(struct switchtec_dev *stdev)
>  	pci_clear_master(stdev->pdev);
>  
>  	cancel_delayed_work_sync(&stdev->mrpc_timeout);

[Severity: High]
This is a pre-existing issue, but does canceling this delayed work here leave
a race window where concurrent userspace operations can re-arm it?

Since cancel_delayed_work_sync() is called before the mrpc_mutex is acquired
and stdev->alive is set to false, can a concurrent switchtec_dev_write()
verify stdev->alive == true and call mrpc_cmd_submit()?

mrpc_cmd_submit()
  schedule_delayed_work(&stdev->mrpc_timeout, msecs_to_jiffies(500));

If userspace then closes the file descriptor, dropping the final kobject
reference and freeing stdev, won't the delayed work timer expire and execute
mrpc_timeout_work() on the freed stdev?

> +	cancel_work_sync(&stdev->mrpc_work);
> +	cancel_work_sync(&stdev->link_event_work);

[Severity: High]
This isn't a bug introduced by this patch, but does pci_clear_master() wait
for currently executing interrupt handlers to finish?

During device unbind, switchtec_pci_remove() calls stdev_kill(). While
pci_clear_master() prevents new interrupts from being initiated, it doesn't
flush pending interrupts or wait for executing handlers like
switchtec_event_isr().

Could a handler continue running concurrently, and after these works are
canceled, schedule the work again?

switchtec_event_isr()
  schedule_work(&stdev->mrpc_work);

If put_device(&stdev->dev) then frees stdev before the driver's devres (which
owns the interrupt) runs, won't this result in a use-after-free when the
handler places the freed work struct onto the workqueue?

>  
>  	/* Mark the hardware as unavailable and complete all completions */
>  	scoped_guard (mutex, &stdev->mrpc_mutex) {

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/6fdcbfa869f707cad783c7f92d1681f0a2bd9a7c.1785813784.git.xiaopei01@kylinos.cn?part=1

  reply	other threads:[~2026-08-04  3:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04  3:24 [PATCH] PCI: switchtec: Fix use-after-free in switchtec_pci_remove due to race condition Pei Xiao
2026-08-04  3:44 ` sashiko-bot [this message]
2026-08-04 19:48 ` Logan Gunthorpe
2026-08-05  1:28   ` Pei Xiao
2026-08-05 16:05     ` Logan Gunthorpe

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=20260804034406.C5D631F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xiaopei01@kylinos.cn \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.