Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mohamad Raizudeen" <raizudeen.kerneldev@gmail.com>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH] PCI: Fix use-after-free race in pci_find_bus()
Date: Wed, 12 Aug 2026 02:59:32 +0000	[thread overview]
Message-ID: <20260812025933.1E9DF1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260812024713.4958-1-raizudeen.kerneldev@gmail.com>

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

Pre-existing issues:
- [High] The fundamentally vulnerable pci_find_next_bus() iterator is left unmodified, retaining a use-after-free race.
- [High] pci_find_bus() returns a non-refcounted bus pointer after dropping pci_bus_sem, exposing callers to a caller-side use-after-free.
--

--- Patch [1]: [PATCH] PCI: Fix use-after-free race in pci_find_bus() ---
commit 7750909a991524c7f4e93f423648abcd167d3923
Author: Mohamad Raizudeen <raizudeen.kerneldev@gmail.com>

PCI: Fix use-after-free race in pci_find_bus()
    
pci_find_bus() iterates over the list of PCI root buses using
pci_find_next_bus(). If a PCI bus is concurrently removed via hotplug
between loop iterations, the from pointer passed back into
pci_find_next_bus() becomes stale. This commit fixes this by iterating
pci_root_buses list directly while holding the pci_bus_sem read lock.

> diff --git a/drivers/pci/search.c b/drivers/pci/search.c
> index e3d3177fce549..f50e83061b769 100644
> --- a/drivers/pci/search.c
> +++ b/drivers/pci/search.c
> @@ -142,17 +142,19 @@ static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
>   */
>  struct pci_bus *pci_find_bus(int domain, int busnr)
>  {
> -	struct pci_bus *bus = NULL;
> -	struct pci_bus *tmp_bus;
> +	struct pci_bus *bus;
> +	struct pci_bus *tmp_bus = NULL;
>  
> -	while ((bus = pci_find_next_bus(bus)) != NULL)  {

[Severity: High]
This is a pre-existing issue, but while you are removing the use of
pci_find_next_bus() here, the pci_find_next_bus() function itself remains
vulnerable to the same use-after-free race.

Other lockless callers like uncore_die_to_segment() in
arch/x86/events/intel/uncore.c still iterate over buses using
pci_find_next_bus() without holding pci_lock_rescan_remove(). Because
pci_find_next_bus() drops pci_bus_sem before returning, a concurrent PCI
hot-remove can free the bus. 

When the caller passes the freed bus back into pci_find_next_bus() for the
next iteration, from->node.next is dereferenced, causing a use-after-free.

Should pci_find_next_bus() also be fixed to prevent memory corruption for
other callers?

> -		if (pci_domain_nr(bus) != domain)
> -			continue;
> -		tmp_bus = pci_do_find_bus(bus, busnr);
> -		if (tmp_bus)
> -			return tmp_bus;
> +	down_read(&pci_bus_sem);
> +	list_for_each_entry(bus, &pci_root_buses, node) {
> +		if (pci_domain_nr(bus) == domain) {
> +			tmp_bus = pci_do_find_bus(bus, busnr);
> +			if (tmp_bus)
> +				break;
> +		}
>  	}
> -	return NULL;
> +	up_read(&pci_bus_sem);
> +	return tmp_bus;

[Severity: High]
This is a pre-existing issue, but does this expose the caller to a
use-after-free race?

By dropping pci_bus_sem before returning tmp_bus, the returned bus pointer
is un-refcounted and no longer protected by the lock. If the caller does
not hold pci_lock_rescan_remove(), a concurrent hot-remove thread could
immediately acquire pci_bus_sem for write, remove the bus, and free its
memory before the caller has a chance to safely use it.

Would it be safer to increment the reference count of the returned bus
with pci_bus_get() before releasing pci_bus_sem?

>  }
>  EXPORT_SYMBOL(pci_find_bus);

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

  reply	other threads:[~2026-08-12  2:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-12  2:47 [PATCH] PCI: Fix use-after-free race in pci_find_bus() Mohamad Raizudeen
2026-08-12  2:59 ` sashiko-bot [this message]
2026-08-12  3:00 ` Greg KH
2026-08-12  7:26   ` Mohamad Raizudeen
2026-08-12  7:45     ` Greg KH

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=20260812025933.1E9DF1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=raizudeen.kerneldev@gmail.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