public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf()
@ 2024-11-07  0:53 Zijun Hu
  2024-11-07  0:53 ` [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults Zijun Hu
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Zijun Hu @ 2024-11-07  0:53 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Krzysztof Wilczyński,
	Kishon Vijay Abraham I, Bjorn Helgaas, Frank Li,
	Lorenzo Pieralisi
  Cc: Zijun Hu, Krzysztof Wilczyński, linux-pci, linux-kernel,
	Zijun Hu, Jingoo Han, Marek Vasut, Yoshihiro Shimoda, Shawn Lin,
	Heiko Stuebner, stable

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,
-- 
Zijun Hu <quic_zijuhu@quicinc.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults
  2024-11-07  0:53 [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Zijun Hu
@ 2024-11-07  0:53 ` Zijun Hu
  2024-11-12  7:03   ` Manivannan Sadhasivam
  2024-11-07  0:53 ` [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF Zijun Hu
  2024-11-12  7:35 ` [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Manivannan Sadhasivam
  2 siblings, 1 reply; 7+ messages in thread
From: Zijun Hu @ 2024-11-07  0:53 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Krzysztof Wilczyński,
	Kishon Vijay Abraham I, Bjorn Helgaas, Frank Li,
	Lorenzo Pieralisi
  Cc: Zijun Hu, Krzysztof Wilczyński, linux-pci, linux-kernel,
	Zijun Hu, Jingoo Han, Marek Vasut, Yoshihiro Shimoda, Shawn Lin,
	Heiko Stuebner, stable

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);
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF
  2024-11-07  0:53 [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Zijun Hu
  2024-11-07  0:53 ` [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults Zijun Hu
@ 2024-11-07  0:53 ` Zijun Hu
  2024-11-12  7:08   ` Manivannan Sadhasivam
  2024-11-12  7:35 ` [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Manivannan Sadhasivam
  2 siblings, 1 reply; 7+ messages in thread
From: Zijun Hu @ 2024-11-07  0:53 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Krzysztof Wilczyński,
	Kishon Vijay Abraham I, Bjorn Helgaas, Frank Li,
	Lorenzo Pieralisi
  Cc: Zijun Hu, Krzysztof Wilczyński, linux-pci, linux-kernel,
	Zijun Hu, stable

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);

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults
  2024-11-07  0:53 ` [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults Zijun Hu
@ 2024-11-12  7:03   ` Manivannan Sadhasivam
  2024-11-12  7:18     ` quic_zijuhu
  0 siblings, 1 reply; 7+ messages in thread
From: Manivannan Sadhasivam @ 2024-11-12  7:03 UTC (permalink / raw)
  To: Zijun Hu
  Cc: Krzysztof Wilczyński, Kishon Vijay Abraham I, Bjorn Helgaas,
	Frank Li, Lorenzo Pieralisi, Krzysztof Wilczyński, linux-pci,
	linux-kernel, Zijun Hu, Jingoo Han, Marek Vasut,
	Yoshihiro Shimoda, Shawn Lin, Heiko Stuebner, stable

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
> 

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF
  2024-11-07  0:53 ` [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF Zijun Hu
@ 2024-11-12  7:08   ` Manivannan Sadhasivam
  0 siblings, 0 replies; 7+ messages in thread
From: Manivannan Sadhasivam @ 2024-11-12  7:08 UTC (permalink / raw)
  To: Zijun Hu
  Cc: Krzysztof Wilczyński, Kishon Vijay Abraham I, Bjorn Helgaas,
	Frank Li, Lorenzo Pieralisi, Krzysztof Wilczyński, linux-pci,
	linux-kernel, Zijun Hu, stable

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;
> +		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);
> 
> -- 
> 2.34.1
> 

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults
  2024-11-12  7:03   ` Manivannan Sadhasivam
@ 2024-11-12  7:18     ` quic_zijuhu
  0 siblings, 0 replies; 7+ messages in thread
From: quic_zijuhu @ 2024-11-12  7:18 UTC (permalink / raw)
  To: Manivannan Sadhasivam, Zijun Hu
  Cc: Krzysztof Wilczyński, Kishon Vijay Abraham I, Bjorn Helgaas,
	Frank Li, Lorenzo Pieralisi, Krzysztof Wilczyński, linux-pci,
	linux-kernel, Jingoo Han, Marek Vasut, Yoshihiro Shimoda,
	Shawn Lin, Heiko Stuebner, stable

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]

>>
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf()
  2024-11-07  0:53 [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Zijun Hu
  2024-11-07  0:53 ` [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults Zijun Hu
  2024-11-07  0:53 ` [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF Zijun Hu
@ 2024-11-12  7:35 ` Manivannan Sadhasivam
  2 siblings, 0 replies; 7+ messages in thread
From: Manivannan Sadhasivam @ 2024-11-12  7:35 UTC (permalink / raw)
  To: Zijun Hu
  Cc: Krzysztof Wilczyński, Kishon Vijay Abraham I, Bjorn Helgaas,
	Frank Li, Lorenzo Pieralisi, Krzysztof Wilczyński, linux-pci,
	linux-kernel, Zijun Hu, Jingoo Han, Marek Vasut,
	Yoshihiro Shimoda, Shawn Lin, Heiko Stuebner, stable

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>
> 

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-11-12  7:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-07  0:53 [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Zijun Hu
2024-11-07  0:53 ` [PATCH v2 1/2] PCI: endpoint: Fix API pci_epc_destroy() releasing domain_nr ID faults Zijun Hu
2024-11-12  7:03   ` Manivannan Sadhasivam
2024-11-12  7:18     ` quic_zijuhu
2024-11-07  0:53 ` [PATCH v2 2/2] PCI: endpoint: Fix API pci_epc_remove_epf() cleaning up wrong EPC of EPF Zijun Hu
2024-11-12  7:08   ` Manivannan Sadhasivam
2024-11-12  7:35 ` [PATCH v2 0/2] PCI: endpoint: fix bugs for both API pci_epc_destroy() and pci_epc_remove_epf() Manivannan Sadhasivam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox