This patch series is to fix bugs for below 2 APIs: pci_epc_destroy() pci_epc_remove_epf()
Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com --- Changes in v2: - Correct title and commit messages, and remove RFC tag - Link to v1: https://lore.kernel.org/r/20241102-epc_rfc-v1-0-5026322df5bc@quicinc.com
--- 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,
From: Zijun Hu quic_zijuhu@quicinc.com
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 lpieralisi@kernel.org Cc: Jingoo Han jingoohan1@gmail.com Cc: Marek Vasut marek.vasut+renesas@gmail.com Cc: Yoshihiro Shimoda yoshihiro.shimoda.uh@renesas.com Cc: Shawn Lin shawn.lin@rock-chips.com Cc: Heiko Stuebner heiko@sntech.de Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com --- drivers/pci/endpoint/pci-epc-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index 17f007109255..bcc9bc3d6df5 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -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);
On Thu, Nov 07, 2024 at 08:53:08AM +0800, Zijun Hu wrote:
From: Zijun Hu quic_zijuhu@quicinc.com
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
No need to mention the callers.
Fixes: 0328947c5032 ("PCI: endpoint: Assign PCI domain number for endpoint controllers") Cc: Lorenzo Pieralisi lpieralisi@kernel.org Cc: Jingoo Han jingoohan1@gmail.com Cc: Marek Vasut marek.vasut+renesas@gmail.com Cc: Yoshihiro Shimoda yoshihiro.shimoda.uh@renesas.com Cc: Shawn Lin shawn.lin@rock-chips.com Cc: Heiko Stuebner heiko@sntech.de Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com
Good catch! (not sure how I messed up in first place).
Reviewed-by: Manivannan Sadhasivam manivannan.sadhasivam@linaro.org
- Mani
drivers/pci/endpoint/pci-epc-core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index 17f007109255..bcc9bc3d6df5 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -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);
-- 2.34.1
On 11/12/2024 3:03 PM, Manivannan Sadhasivam wrote:
On Thu, Nov 07, 2024 at 08:53:08AM +0800, Zijun Hu wrote:
From: Zijun Hu quic_zijuhu@quicinc.com
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
No need to mention the callers.
thank you Manivannan for code review. good suggestions, i will take them for further similar patches.(^^)
Fixes: 0328947c5032 ("PCI: endpoint: Assign PCI domain number for endpoint controllers") Cc: Lorenzo Pieralisi lpieralisi@kernel.org Cc: Jingoo Han jingoohan1@gmail.com Cc: Marek Vasut marek.vasut+renesas@gmail.com Cc: Yoshihiro Shimoda yoshihiro.shimoda.uh@renesas.com Cc: Shawn Lin shawn.lin@rock-chips.com Cc: Heiko Stuebner heiko@sntech.de Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com
Good catch! (not sure how I messed up in first place).
Reviewed-by: Manivannan Sadhasivam manivannan.sadhasivam@linaro.org
- Mani
[snip]
From: Zijun Hu quic_zijuhu@quicinc.com
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") Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com --- drivers/pci/endpoint/pci-epc-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index bcc9bc3d6df5..62f7dff43730 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -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);
On Thu, Nov 07, 2024 at 08:53:09AM +0800, Zijun Hu wrote:
From: Zijun Hu quic_zijuhu@quicinc.com
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") Cc: stable@vger.kernel.org Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com
Reviewed-by: Manivannan Sadhasivam manivannan.sadhasivam@linaro.org
- Mani
drivers/pci/endpoint/pci-epc-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index bcc9bc3d6df5..62f7dff43730 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -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;
} else { func_no = epf->sec_epc_func_no; list = &epf->sec_epc_list;epf->epc = NULL;
}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);
-- 2.34.1
On Thu, Nov 07, 2024 at 08:53:07AM +0800, Zijun Hu wrote:
This patch series is to fix bugs for below 2 APIs: pci_epc_destroy() pci_epc_remove_epf()
Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com
Applied to pci/endpoint!
- Mani
Changes in v2:
- Correct title and commit messages, and remove RFC tag
- Link to v1: https://lore.kernel.org/r/20241102-epc_rfc-v1-0-5026322df5bc@quicinc.com
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,
Zijun Hu quic_zijuhu@quicinc.com
linux-stable-mirror@lists.linaro.org