PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
PATCH v2 1/1 PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
pci_proc_attach_device() creates procfs entries for PCI devices and is called from pci_bus_add_device(). It lazily creates the per-bus procfs directory (bus->procdir) via proc_mkdir() on first use, and returns early if proc_initialized is not yet set.
On x86 with ACPI, PCI enumeration occurs at subsys_initcall, before pci_proc_init() sets proc_initialized at device_initcall. The for_each_pci_dev() loop in pci_proc_init() then creates procfs entries for these already-enumerated devices, but runs without holding pci_rescan_remove_lock.
On ARM64 with devicetree, PCI host bridges probe at device_initcall. With async probing enabled, pci_bus_add_device() can run concurrently with pci_proc_init(), and both may call pci_proc_attach_device() for the same device or for different devices on the same bus. As pci_host_probe() holds pci_rescan_remove_lock while pci_proc_init() does not, there is no serialisation between the two paths.
When two threads concurrently call pci_proc_attach_device() for devices on the same bus, both observe bus->procdir as NULL and both call proc_mkdir(). The proc filesystem serialises directory creation internally, so only one caller succeeds. The other receives NULL (duplicate entry) and unconditionally stores it to bus->procdir, corrupting the valid pointer set by the first caller.
Thus, extract the bus procfs directory creation from pci_proc_attach_device() into a new pci_proc_attach_bus() function, and call it from the two bus creation paths: pci_register_host_bridge() for root buses and pci_alloc_child_bus() for child buses. These are the only two callers of pci_alloc_bus(), so for buses created after proc_initialized is set, bus->procdir is in place before any device can be added to the bus.
Therefore, by the time pci_proc_attach_device() runs on these buses, bus->procdir is already set and the racy proc_mkdir() call is never reached.
For buses created before pci_proc_init() sets proc_initialized (the common x86 ACPI case), the bus creation hooks return early. As such, add a fallback call to pci_proc_attach_bus() from pci_proc_attach_device() to handle these pre-init buses.
Additionally, wrap the for_each_pci_dev() loop in pci_proc_init() with pci_lock_rescan_remove() to serialise against concurrent PCI bus operations, add an early return in pci_proc_attach_device() when dev->procent is already set to make the function idempotent, and clear bus->procdir in pci_proc_detach_bus() to prevent use of a dangling pointer after proc_remove().
---
Changes in v2:
https://lore.kernel.org/linux-pci/[email protected]/
- Extracted bus->procdir creation into new pci_proc_attach_bus() function, addressing the suggestion to move proc_mkdir() to a bus creation path, as per Bjorn Helgaas' feedback.
- Added bus->procdir = NULL in pci_proc_detach_bus() for symmetry and to prevent dangling pointer after proc_remove().
drivers/pci/pci.h | 6 +++-- drivers/pci/probe.c | 6 +++++ drivers/pci/proc.c | 63 +++++++++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 22 deletions(-)
drivers/pci/pci.h +4 -2
@@ -343,13 +343,15 @@ void pci_allocate_vc_save_buffers(struct pci_dev *dev);/* PCI /proc functions */ #ifdef CONFIG_PROC_FS +int pci_proc_attach_bus (struct pci_bus *bus); +int pci_proc_detach_bus (struct pci_bus *bus);int pci_proc_attach_device (struct pci_dev *dev);int pci_proc_detach_device (struct pci_dev *dev); -int pci_proc_detach_bus (struct pci_bus *bus);#else +static inline int pci_proc_attach_bus (struct pci_bus *bus) {return 0 ; } +static inline int pci_proc_detach_bus (struct pci_bus *bus) {return 0 ; }static inline int pci_proc_attach_device (struct pci_dev *dev) {return 0 ; }static inline int pci_proc_detach_device (struct pci_dev *dev) {return 0 ; } -static inline int pci_proc_detach_bus (struct pci_bus *bus) {return 0 ; }#endif /* Functions for PCI Hotplug drivers to use */
drivers/pci/probe.c +6 -0
@@ -1071,6 +1071,9 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)/* Create legacy_io and legacy_mem files for this bus */ pci_create_legacy_files (bus); +/* Create procfs directory for this bus */ +pci_proc_attach_bus (bus); +if (parent)dev_info (parent,"PCI host bridge to bus %s\n" , name);else
@@ -1279,6 +1282,9 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,/* Create legacy_io and legacy_mem files for this bus */ pci_create_legacy_files (child); +/* Create procfs directory for this bus */ +pci_proc_attach_bus (child); +return child; }
drivers/pci/proc.c +43 -20
95 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] TOCTOU race leading to Use-After-Free and resource leaks via corrupted procfs pointer --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] TOCTOU race leading to Use-After-Free and resource leaks via corrupted procfs pointer --
commit cd2e3cd5b4869787fb5b78b8cfef90b55821ad2b
Author: Krzysztof Wilczyński <[email protected]>
PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
This commit attempts to fix a race condition between pci_proc_init() and pci_bus_add_device() by lazily attaching procfs directories when buses are created, and by protecting the device iteration in pci_proc_init() with pci_lock_rescan_remove().
Quoted 16 lines
drivers/pci/proc.c +8
-0@@ -416,34 +416,51 @@ static const struct seq_operations proc_bus_pci_devices_op = {static struct proc_dir_entry *proc_bus_pci_dir; +int pci_proc_attach_bus (struct pci_bus *bus) +{ +char name[16 ]; + +if (!proc_initialized) +return -EACCES ; + +if (bus->procdir)
drivers/pci/proc.c static const struct seq_operations proc_bus_pci_devices_op = { · line 436
Quoted 10 lines
drivers/pci/proc.c +10
-0+return 0 ; + +if (pci_proc_domain (bus)) +sprintf (name,"%04x:%02x" ,pci_domain_nr (bus), bus->number); +else +sprintf (name,"%02x" , bus->number); + + bus->procdir =proc_mkdir (name, proc_bus_pci_dir); +if (!bus->procdir) +return -ENOMEM ;
[Severity: High]
Can this direct assignment overwrite a valid pointer with NULL if a concurrent creation fails?
If an asynchronous probe runs concurrently without holding pci_rescan_remove_lock(), two threads might both observe bus->procdir as NULL. If one successfully creates the entry, the second could fail due to a name collision and return NULL, unconditionally overwriting the valid bus->procdir.
Could this cause subsequent child devices to erroneously create their proc files in the root of /proc?
Quoted 4 lines
drivers/pci/proc.c +4
-0+ +return 0 ; +} +
[ ... ]
drivers/pci/proc.c static const struct seq_operations proc_bus_pci_devices_op = { · line 462
Quoted 9 lines
drivers/pci/proc.c +4
-3sprintf (name,"%02x.%x" ,PCI_SLOT (dev->devfn),PCI_FUNC (dev->devfn)); - e =proc_create_data (name,S_IFREG |S_IRUGO |S_IWUSR , bus->procdir, - &proc_bus_pci_ops, dev); -if (!e) + dev->procent =proc_create_data (name,S_IFREG |S_IRUGO |S_IWUSR , + dev->bus->procdir, &proc_bus_pci_ops, + dev); +if (!dev->procent)return -ENOMEM ;
[Severity: High]
Does this assignment introduce a risk of a use-after-free and resource leak?
Similar to the bus directory, if concurrent threads call pci_proc_attach_device() for the same device, one might overwrite a valid dev->procent with NULL when proc_create_data() fails.
Later, pci_proc_detach_device() would call proc_remove(NULL) which does nothing. The procfs file would be leaked, and because the file's data points to the freed pci_dev struct, subsequent reads from userspace could trigger a use-after-free.
Quoted 6 lines
drivers/pci/proc.c +1
-2-proc_set_size (e, dev->cfg_size); - dev->procent = e; +proc_set_size (dev->procent, dev->cfg_size);return 0 ; }
[ ... ]
Quoted 5 lines
static int
__initpci_proc_init(void)
{drivers/pci/proc.c +1
-0struct pci_dev *dev =NULL ; + proc_bus_pci_dir =proc_mkdir ("bus/pci" ,NULL );
drivers/pci/proc.c int pci_proc_detach_device(struct pci_dev *dev) · line 495
Quoted 10 lines
drivers/pci/proc.c +5
-1proc_create_seq ("devices" ,0 , proc_bus_pci_dir, - &proc_bus_pci_devices_op); + &proc_bus_pci_devices_op); + proc_initialized =1 ; + +pci_lock_rescan_remove ();for_each_pci_dev (dev)pci_proc_attach_device (dev); +pci_unlock_rescan_remove ();
[Severity: High]
Is this lock sufficient to serialize against lockless async probe paths?
Paths like the VMD driver (vmd_probe() -> vmd_enable_domain() -> pci_bus_add_devices()) or platform drivers can run asynchronously and do not always hold pci_rescan_remove_lock().
If an asynchronous probe adds devices to the global pci_bus_type list before calling pci_proc_attach_device(), and pci_proc_init() iterates the list under the lock, could both threads still find the new devices and call pci_proc_attach_device() concurrently?
Quoted 3 lines
return 0;
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hello,
Quoted 16 lines
drivers/pci/proc.c static const struct seq_operations proc_bus_pci_devices_op = { · line 436
Quoted 3 lines
drivers/pci/proc.c +3
-0+ bus->procdir =proc_mkdir (name, proc_bus_pci_dir); +if (!bus->procdir) +return -ENOMEM ;[Severity: High]
Can this direct assignment overwrite a valid pointer with NULL if a concurrent creation fails?If an asynchronous probe runs concurrently without holding
pci_rescan_remove_lock(), two threads might both observebus->procdiras NULL. If one successfully creates the entry, the second could fail due to a name collision and return NULL, unconditionally overwriting the validbus->procdir.Could this cause subsequent child devices to erroneously create their proc files in the root of /proc?
[...]
Quoted 13 lines
drivers/pci/proc.c static const struct seq_operations proc_bus_pci_devices_op = { · line 462
Quoted 9 lines
drivers/pci/proc.c +4
-3sprintf (name,"%02x.%x" ,PCI_SLOT (dev->devfn),PCI_FUNC (dev->devfn)); - e =proc_create_data (name,S_IFREG |S_IRUGO |S_IWUSR , bus->procdir, - &proc_bus_pci_ops, dev); -if (!e) + dev->procent =proc_create_data (name,S_IFREG |S_IRUGO |S_IWUSR , + dev->bus->procdir, &proc_bus_pci_ops, + dev); +if (!dev->procent)return -ENOMEM ;[Severity: High]
Does this assignment introduce a risk of a use-after-free and resource leak?
Quoted 8 lines
Similar to the bus directory, if concurrent threads call
pci_proc_attach_device()for the same device, one might overwrite a validdev->procentwith NULL whenproc_create_data()fails.Later,
pci_proc_detach_device()would callproc_remove(NULL)which does nothing. The procfs file would be leaked, and because the file's data points to the freedpci_devstruct, subsequent reads from userspace could trigger a use-after-free.
I will send another revision to address this feedback.
Thank you!
Krzysztof