Skip to contentSkip to conversation

PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf()

PATCH Applied Zijun Hu 3 participants 7 messages Nov 7, 2024 – Nov 12, 2024

Subsystems (3)
lore
Share & tools
lore

Zijun Hu

author
#0 lore raw

v2 cover PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf()

This patch series is to fix bugs for below 2 APIs:
pci_epc_destroy()
pci_epc_remove_epf()

---
Changes in v2:
- Correct title and commit messages, and remove RFC tag
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Zijun Hu (2):
PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults
PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF

 drivers/pci/endpoint/pci-epc-core.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

---
base-commit: ad5df4a631fa7eeb8eb212d21ab3f6979fd1926e
change-id: 20241102-epc_rfc-e1d9d03d5101

Best regards,

Signature · 1 line
Zijun Hu <[email protected]>
author
#1 lore raw

PATCH v2 1/2 PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults

From: Zijun Hu <[email protected]>

pci_epc_destroy() invokes pci_bus_release_domain_nr() to release domain_nr ID, but the invocation has below 2 faults:

- The later accesses device @epc->dev which has been kfree()ed by previous device_unregister(), namely, it is a UAF issue.

- The later frees the domain_nr ID into @epc->dev, but the ID is actually allocated from @epc->dev.parent, so it will destroy domain_nr IDA.

Fix by freeing the ID to @epc->dev.parent before unregistering @epc->dev.

The file(s) affected are shown below since they indirectly use the API. drivers/pci/controller/cadence/pcie-cadence-ep.c drivers/pci/controller/dwc/pcie-designware-ep.c drivers/pci/controller/pcie-rockchip-ep.c drivers/pci/controller/pcie-rcar-ep.c

Fixes 0328947c5032 ("PCI: endpoint: Assign PCI domain number for endpoint controllers")
Cc Lorenzo Pieralisi <[email protected]>
Cc Jingoo Han <[email protected]>
Cc Marek Vasut <[email protected]>
Cc Yoshihiro Shimoda <[email protected]>
Cc Shawn Lin <[email protected]>
Cc Heiko Stuebner <[email protected]>
---
 drivers/pci/endpoint/pci-epc-core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/pci/endpoint/pci-epc-core.c +2 -3
index 17f007109255..bcc9bc3d6df5 100644
@@ -837,11 +837,10 @@ EXPORT_SYMBOL_GPL(pci_epc_bus_master_enable_notify);
 void pci_epc_destroy(struct pci_epc *epc)
 {
 	pci_ep_cfs_remove_epc_group(epc->group);
-	device_unregister(&epc->dev);
-
 #ifdef CONFIG_PCI_DOMAINS_GENERIC
-	pci_bus_release_domain_nr(&epc->dev, epc->domain_nr);
+	pci_bus_release_domain_nr(epc->dev.parent, epc->domain_nr);
 #endif
+	device_unregister(&epc->dev);
 }
 EXPORT_SYMBOL_GPL(pci_epc_destroy);
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults

Quoted 11 lines · Zijun Hu

From: Zijun Hu <[email protected]>

pci_epc_destroy() invokes pci_bus_release_domain_nr() to release domain_nr ID, but the invocation has below 2 faults:

- The later accesses device @epc->dev which has been kfree()ed by previous device_unregister(), namely, it is a UAF issue.

- The later frees the domain_nr ID into @epc->dev, but the ID is actually allocated from @epc->dev.parent, so it will destroy domain_nr IDA.

Quoted 7 lines

Fix by freeing the ID to @epc->dev.parent before unregistering @epc->dev.

The file(s) affected are shown below since they indirectly use the API. drivers/pci/controller/cadence/pcie-cadence-ep.c drivers/pci/controller/dwc/pcie-designware-ep.c drivers/pci/controller/pcie-rockchip-ep.c drivers/pci/controller/pcie-rcar-ep.c

No need to mention the callers.

Quoted 10 lines
Fixes 0328947c5032 ("PCI: endpoint: Assign PCI domain number for endpoint controllers")
Cc Lorenzo Pieralisi <[email protected]>
Cc Jingoo Han <[email protected]>
Cc Marek Vasut <[email protected]>
Cc Yoshihiro Shimoda <[email protected]>
Cc Shawn Lin <[email protected]>
Cc Heiko Stuebner <[email protected]>

Good catch! (not sure how I messed up in first place).

- Mani

Quoted 26 lines
---
 drivers/pci/endpoint/pci-epc-core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/pci/endpoint/pci-epc-core.c +2 -3
index 17f007109255..bcc9bc3d6df5 100644
@@ -837,11 +837,10 @@ EXPORT_SYMBOL_GPL(pci_epc_bus_master_enable_notify);
 void pci_epc_destroy(struct pci_epc *epc)
 {
 	pci_ep_cfs_remove_epc_group(epc->group);
-	device_unregister(&epc->dev);
-
 #ifdef CONFIG_PCI_DOMAINS_GENERIC
-	pci_bus_release_domain_nr(&epc->dev, epc->domain_nr);
+	pci_bus_release_domain_nr(epc->dev.parent, epc->domain_nr);
 #endif
+	device_unregister(&epc->dev);
 }
 EXPORT_SYMBOL_GPL(pci_epc_destroy);
 
Signature · 2 lines
-- 
2.34.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#3 lore raw

Re: [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults

Quoted 14 lines · Manivannan Sadhasivam
Quoted 13 lines · Zijun Hu

From: Zijun Hu <[email protected]>

pci_epc_destroy() invokes pci_bus_release_domain_nr() to release domain_nr ID, but the invocation has below 2 faults:

- The later accesses device @epc->dev which has been kfree()ed by previous device_unregister(), namely, it is a UAF issue.

- The later frees the domain_nr ID into @epc->dev, but the ID is actually allocated from @epc->dev.parent, so it will destroy domain_nr IDA.

Fix by freeing the ID to @epc->dev.parent before unregistering @epc->dev.

Quoted 8 lines
Quoted 5 lines

The file(s) affected are shown below since they indirectly use the API. drivers/pci/controller/cadence/pcie-cadence-ep.c drivers/pci/controller/dwc/pcie-designware-ep.c drivers/pci/controller/pcie-rockchip-ep.c drivers/pci/controller/pcie-rcar-ep.c

No need to mention the callers.

thank you Manivannan for code review.
good suggestions, i will take them for further similar patches.(^^)

Quoted 18 lines
Quoted 10 lines
Fixes 0328947c5032 ("PCI: endpoint: Assign PCI domain number for endpoint controllers")
Cc Lorenzo Pieralisi <[email protected]>
Cc Jingoo Han <[email protected]>
Cc Marek Vasut <[email protected]>
Cc Yoshihiro Shimoda <[email protected]>
Cc Shawn Lin <[email protected]>
Cc Heiko Stuebner <[email protected]>

Good catch! (not sure how I messed up in first place).

- Mani

Quoted 1 line

---

[snip]

Quoted 2 lines
Quoted 1 line
author
#4 lore raw

PATCH v2 2/2 PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF

In reply to Zijun Hu

From: Zijun Hu <[email protected]>

It is wrong for pci_epc_remove_epf(..., epf, SECONDARY_INTERFACE) to clean up @epf->epc obviously.

Fix by cleaning up @epf->sec_epc instead of @epf->epc for SECONDARY_INTERFACE.

Fixes 63840ff53223 ("PCI: endpoint: Add support to associate secondary EPC with EPF")
---
 drivers/pci/endpoint/pci-epc-core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/endpoint/pci-epc-core.c +3 -3
index bcc9bc3d6df5..62f7dff43730 100644
@@ -660,18 +660,18 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
 	if (IS_ERR_OR_NULL(epc) || !epf)
 		return;
 
+	mutex_lock(&epc->list_lock);
 	if (type == PRIMARY_INTERFACE) {
 		func_no = epf->func_no;
 		list = &epf->list;
+		epf->epc = NULL;
 	} else {
 		func_no = epf->sec_epc_func_no;
 		list = &epf->sec_epc_list;
+		epf->sec_epc = NULL;
 	}
-
-	mutex_lock(&epc->list_lock);
 	clear_bit(func_no, &epc->function_num_map);
 	list_del(list);
-	epf->epc = NULL;
 	mutex_unlock(&epc->list_lock);
 }
 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF

Quoted 11 lines · Zijun Hu

From: Zijun Hu <[email protected]>

It is wrong for pci_epc_remove_epf(..., epf, SECONDARY_INTERFACE) to clean up @epf->epc obviously.

Fix by cleaning up @epf->sec_epc instead of @epf->epc for SECONDARY_INTERFACE.

Fixes 63840ff53223 ("PCI: endpoint: Add support to associate secondary EPC with EPF")

- Mani

Quoted 34 lines
---
 drivers/pci/endpoint/pci-epc-core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/endpoint/pci-epc-core.c +3 -3
index bcc9bc3d6df5..62f7dff43730 100644
@@ -660,18 +660,18 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
 	if (IS_ERR_OR_NULL(epc) || !epf)
 		return;
 
+	mutex_lock(&epc->list_lock);
 	if (type == PRIMARY_INTERFACE) {
 		func_no = epf->func_no;
 		list = &epf->list;
+		epf->epc = NULL;
 	} else {
 		func_no = epf->sec_epc_func_no;
 		list = &epf->sec_epc_list;
+		epf->sec_epc = NULL;
 	}
-
-	mutex_lock(&epc->list_lock);
 	clear_bit(func_no, &epc->function_num_map);
 	list_del(list);
-	epf->epc = NULL;
 	mutex_unlock(&epc->list_lock);
 }
 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
Signature · 2 lines
-- 
2.34.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#6 lore raw
In reply to Zijun Hu
Quoted 5 lines · Zijun Hu

This patch series is to fix bugs for below 2 APIs:
pci_epc_destroy()
pci_epc_remove_epf()

Applied to pci/endpoint!

- Mani

Quoted 20 lines

---
Changes in v2:
- Correct title and commit messages, and remove RFC tag
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Zijun Hu (2):
PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults
PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF

 drivers/pci/endpoint/pci-epc-core.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

---
base-commit: ad5df4a631fa7eeb8eb212d21ab3f6979fd1926e
change-id: 20241102-epc_rfc-e1d9d03d5101

Best regards,

Signature · 1 line
Zijun Hu <[email protected]>
Signature · 1 line
மணிவண்ணன் சதாசிவம்