Skip to contentSkip to conversation

PCI: fix reference leak in pci_alloc_child_bus()

PATCH Superseded Ma Ke 2 participants 2 messages Jan 7, 2025 PCI

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH 1/1 PCI: fix reference leak in pci_alloc_child_bus()

When device_register(&child->dev) failed, calling put_device() to explicitly release child->dev. Otherwise, it could cause double free problem.

Found by code review.

---
 drivers/pci/probe.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/probe.c +4 -1
index 2e81ab0f5a25..a61070ce5f88 100644
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 add_dev:
 	pci_set_bus_msi_domain(child);
 	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return ERR_PTR(ret);
+	}
 
 	pcibios_add_bus(child);
 
Signature · 1 line
2.25.1
#1 lore raw

Le 07/01/2025 à 12:07, Ma Ke a écrit :

Quoted 16 lines

When device_register(&child->dev) failed, calling put_device() to explicitly release child->dev. Otherwise, it could cause double free problem.

Found by code review.

---
  drivers/pci/probe.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/probe.c +0 -0
index 2e81ab0f5a25..a61070ce5f88 100644

drivers/pci/probe.c static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent, · line 1179

Quoted 7 lines
drivers/pci/probe.c +3 -1
@@ -1174,7 +1174,10 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  add_dev:
  	pci_set_bus_msi_domain(child);
  	ret = device_register(&child->dev);
-	WARN_ON(ret < 0);
+	if (WARN_ON(ret < 0)) {
+		put_device(&child->dev);
+		return ERR_PTR(ret);

Previously, the code was continuing the execution. I don't know it is correct to return at this point.

Anyway, returning ERR_PTR(ret) just looks wrong, because pci_add_new_bus() expect NULL in case of error. Should ERR_PTR(ret) be returned, it is likely basd thing would happen there.

CJ

Quoted 4 lines
drivers/pci/probe.c +1 -0
+	}
 
  	pcibios_add_bus(child);