Skip to contentSkip to conversation

PCI: Fix potential double-free and add error logging

PATCH New Jinhui Guo 1 participant 3 messages Oct 10, 2025 PCI

lore
Share & tools
lore

Jinhui Guo

author
#0 lore raw

cover PCI: Fix potential double-free and add error logging

Hi all,

Two patches fix pci sysfs/procfs issues:

1. Avoid a double-free in pci_remove_resource_files() by clearing pdev->res_attr[i] and pdev->res_attr_wc[i] to NULL after kfree().
2. To make PCI bring-up failures easier to diagnose, log the errno returned by pci_create_sysfs_dev_files() and pci_proc_attach_device() in pci_bus_add_device().

Thanks,
Jinhui

----
Jinhui Guo (2):
PCI/sysfs: Fix potential double-free in pci_remove_resource_files()
PCI: Print the error code when PCI sysfs or procfs creation fails

 drivers/pci/bus.c       | 9 +++++++--
 drivers/pci/pci-sysfs.c | 2 ++
 2 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/2 PCI/sysfs: Fix potential double-free in pci_remove_resource_files()

Avoid a double-free in pci_remove_resource_files() by clearing pdev->res_attr[i] and pdev->res_attr_wc[i] to NULL after kfree(). If pci_create_resource_files() fails it immediately calls pci_remove_resource_files() to clean up, and the same function is invoked again when the device is later removed from the PCI tree. Without zeroing the pointers the second free would operate on stale addresses, causing use-after-free or a double-free panic.

---
 drivers/pci/pci-sysfs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/pci-sysfs.c +2 -0
index 9d6f74bd95f8..a8a27d6c62bb 100644
@@ -1222,12 +1222,14 @@ static void pci_remove_resource_files(struct pci_dev *pdev)
 		if (res_attr) {
 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
 			kfree(res_attr);
+			pdev->res_attr[i] = NULL;
 		}
 
 		res_attr = pdev->res_attr_wc[i];
 		if (res_attr) {
 			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
 			kfree(res_attr);
+			pdev->res_attr_wc[i] = NULL;
 		}
 	}
 }
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 2/2 PCI: Print the error code when PCI sysfs or procfs creation fails

In reply to Jinhui Guo

pci_bus_add_device() ignores the return values of pci_create_sysfs_dev_files() and pci_proc_attach_device(), so any sysfs/procfs creation errors are silently lost. To make PCI bring-up failures easier to diagnose, log the errno returned by pci_create_sysfs_dev_files() and pci_proc_attach_device() in pci_bus_add_device().

---
 drivers/pci/bus.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/bus.c +7 -2
index f26aec6ff588..395a1c7bb4c7 100644
@@ -344,6 +344,7 @@ void pci_bus_add_device(struct pci_dev *dev)
 {
 	struct device_node *dn = dev->dev.of_node;
 	struct platform_device *pdev;
+	int ret;
 
 	/*
 	 * Can not put in pci_device_add yet because resources
@@ -353,8 +354,12 @@ void pci_bus_add_device(struct pci_dev *dev)
 	pci_fixup_device(pci_fixup_final, dev);
 	if (pci_is_bridge(dev))
 		of_pci_make_dev_node(dev);
-	pci_create_sysfs_dev_files(dev);
-	pci_proc_attach_device(dev);
+	ret = pci_create_sysfs_dev_files(dev);
+	if (ret)
+		pci_err(dev, "created sysfs files error %d\n", ret);
+	ret = pci_proc_attach_device(dev);
+	if (ret)
+		pci_err(dev, "created procfs files error %d\n", ret);
 	pci_bridge_d3_update(dev);
 
 	/*
Signature · 1 line
2.20.1