Skip to contentSkip to conversation

usb: typec: qcom-pmic: fix use-after-free on late probe errors

PATCH In review Johan Hovold 4 participants 9 messages Apr 18, 2024 – Apr 22, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover usb: typec: qcom-pmic: fix use-after-free on late probe errors

When reviewing a patch updating the qcom-pmic typec driver, I noticed that the error handling is broken and can lead to use-after-free.

This series addresses the use-after-free and also fixes the error handling in the pdphy_start() callback which failed to disable its supply in all error paths.

The latter fix is not marked for stable on purpose as its not a critical fix (I'm sure autosel will disagree).

Johan

Johan Hovold (2):
usb: typec: qcom-pmic: fix use-after-free on late probe errors
usb: typec: qcom-pmic: fix pdphy start() error handling

 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c       |  8 ++++++--
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 11 +++++++----
 2 files changed, 13 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.2
#1 lore raw

PATCH 1/2 usb: typec: qcom-pmic: fix use-after-free on late probe errors

Make sure to stop and deregister the port in case of late probe errors to avoid use-after-free issues when the underlying memory is released by devres.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Cc Bryan O'Donoghue <[email protected]>
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +6 -2
index e48412cdcb0f..d3958c061a97 100644
@@ -104,14 +104,18 @@ static int qcom_pmic_typec_probe(struct platform_device *pdev)
 
 	ret = tcpm->port_start(tcpm, tcpm->tcpm_port);
 	if (ret)
-		goto fwnode_remove;
+		goto port_unregister;
 
 	ret = tcpm->pdphy_start(tcpm, tcpm->tcpm_port);
 	if (ret)
-		goto fwnode_remove;
+		goto port_stop;
 
 	return 0;
 
+port_stop:
+	tcpm->port_stop(tcpm);
+port_unregister:
+	tcpm_unregister_port(tcpm->tcpm_port);
 fwnode_remove:
 	fwnode_remove_software_node(tcpm->tcpc.fwnode);
 
Signature · 1 line
2.43.2
Reviewed-by on v1 1/2
#2 lore raw
Quoted 4 lines · Johan Hovold

Make sure to stop and deregister the port in case of late probe errors to avoid use-after-free issues when the underlying memory is released by devres.

Quoted 8 lines
Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Cc Bryan O'Donoghue <[email protected]>
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Signature · 2 lines
With best wishes
Dmitry
maintainer Reviewed-by on v1 1/2
#3 lore raw
In reply to Johan Hovold
Quoted 27 lines · Johan Hovold

Make sure to stop and deregister the port in case of late probe errors to avoid use-after-free issues when the underlying memory is released by devres.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Cc Bryan O'Donoghue <[email protected]>
---
  drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +2 -2
index e48412cdcb0f..d3958c061a97 100644
@@ -104,14 +104,18 @@ static int qcom_pmic_typec_probe(struct platform_device *pdev)
 
  	ret = tcpm->port_start(tcpm, tcpm->tcpm_port);
  	if (ret)
-		goto fwnode_remove;
+		goto port_unregister;
 
  	ret = tcpm->pdphy_start(tcpm, tcpm->tcpm_port);
  	if (ret)
-		goto fwnode_remove;
+		goto port_stop;

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c static int qcom_pmic_typec_probe(struct platform_device *pdev) · line 121

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +4 -0
 
  	return 0;
 
+port_stop:
+	tcpm->port_stop(tcpm);
+port_unregister:
+	tcpm_unregister_port(tcpm->tcpm_port);
  fwnode_remove:
  	fwnode_remove_software_node(tcpm->tcpc.fwnode);
 
maintainer Reviewed-by on v1 1/2
#4 lore raw
In reply to Johan Hovold
Quoted 8 lines · Johan Hovold

Make sure to stop and deregister the port in case of late probe errors to avoid use-after-free issues when the underlying memory is released by devres.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Cc Bryan O'Donoghue <[email protected]>
Quoted 31 lines
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +6 -2
index e48412cdcb0f..d3958c061a97 100644
@@ -104,14 +104,18 @@ static int qcom_pmic_typec_probe(struct platform_device *pdev)
 
 	ret = tcpm->port_start(tcpm, tcpm->tcpm_port);
 	if (ret)
-		goto fwnode_remove;
+		goto port_unregister;
 
 	ret = tcpm->pdphy_start(tcpm, tcpm->tcpm_port);
 	if (ret)
-		goto fwnode_remove;
+		goto port_stop;
 
 	return 0;
 
+port_stop:
+	tcpm->port_stop(tcpm);
+port_unregister:
+	tcpm_unregister_port(tcpm->tcpm_port);
 fwnode_remove:
 	fwnode_remove_software_node(tcpm->tcpc.fwnode);
 
Signature · 1 line
2.43.2
Signature · 1 line
heikki
#5 lore raw

PATCH 2/2 usb: typec: qcom-pmic: fix pdphy start() error handling

In reply to Johan Hovold

Move disabling of the vdd-pdphy supply to the start() function which enabled it for symmetry and to make sure that it is disabled as intended in all error paths of pmic_typec_pdphy_reset() (i.e. not just when qcom_pmic_typec_pdphy_enable() fails).

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +7 -4
index 6560f4fc98d5..5b7f52b74a40 100644
@@ -475,10 +475,8 @@ static int qcom_pmic_typec_pdphy_enable(struct pmic_typec_pdphy *pmic_typec_pdph
 
 	qcom_pmic_typec_pdphy_reset_off(pmic_typec_pdphy);
 done:
-	if (ret) {
-		regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+	if (ret)
 		dev_err(dev, "pdphy_enable fail %d\n", ret);
-	}
 
 	return ret;
 }
@@ -524,12 +522,17 @@ static int qcom_pmic_typec_pdphy_start(struct pmic_typec *tcpm,
 
 	ret = pmic_typec_pdphy_reset(pmic_typec_pdphy);
 	if (ret)
-		return ret;
+		goto err_disable_vdd_pdhy;
 
 	for (i = 0; i < pmic_typec_pdphy->nr_irqs; i++)
 		enable_irq(pmic_typec_pdphy->irq_data[i].irq);
 
 	return 0;
+
+err_disable_vdd_pdhy:
+	regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+
+	return ret;
 }
 
 static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm)
Signature · 1 line
2.43.2
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic: fix pdphy start() error handling

Quoted 10 lines · Johan Hovold

Move disabling of the vdd-pdphy supply to the start() function which enabled it for symmetry and to make sure that it is disabled as intended in all error paths of pmic_typec_pdphy_reset() (i.e. not just when qcom_pmic_typec_pdphy_enable() fails).

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 2 lines
With best wishes
Dmitry
maintainer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic: fix pdphy start() error handling

In reply to Johan Hovold
Quoted 36 lines · Johan Hovold

Move disabling of the vdd-pdphy supply to the start() function which enabled it for symmetry and to make sure that it is disabled as intended in all error paths of pmic_typec_pdphy_reset() (i.e. not just when qcom_pmic_typec_pdphy_enable() fails).

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
---
  drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +2 -4
index 6560f4fc98d5..5b7f52b74a40 100644
@@ -475,10 +475,8 @@ static int qcom_pmic_typec_pdphy_enable(struct pmic_typec_pdphy *pmic_typec_pdph
 
  	qcom_pmic_typec_pdphy_reset_off(pmic_typec_pdphy);
  done:
-	if (ret) {
-		regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+	if (ret)
  		dev_err(dev, "pdphy_enable fail %d\n", ret);
-	}
 
  	return ret;
  }
@@ -524,12 +522,17 @@ static int qcom_pmic_typec_pdphy_start(struct pmic_typec *tcpm,
 
  	ret = pmic_typec_pdphy_reset(pmic_typec_pdphy);
  	if (ret)
-		return ret;
+		goto err_disable_vdd_pdhy;
 
  	for (i = 0; i < pmic_typec_pdphy->nr_irqs; i++)
  		enable_irq(pmic_typec_pdphy->irq_data[i].irq);

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c static int qcom_pmic_typec_pdphy_start(struct pmic_typec *tcpm, · line 538

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +5 -0
 
  	return 0;
+
+err_disable_vdd_pdhy:
+	regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+
+	return ret;
  }
 
  static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm)
maintainer Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic: fix pdphy start() error handling

In reply to Johan Hovold
Quoted 7 lines · Johan Hovold

Move disabling of the vdd-pdphy supply to the start() function which enabled it for symmetry and to make sure that it is disabled as intended in all error paths of pmic_typec_pdphy_reset() (i.e. not just when qcom_pmic_typec_pdphy_enable() fails).

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Quoted 41 lines
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +7 -4
index 6560f4fc98d5..5b7f52b74a40 100644
@@ -475,10 +475,8 @@ static int qcom_pmic_typec_pdphy_enable(struct pmic_typec_pdphy *pmic_typec_pdph
 
 	qcom_pmic_typec_pdphy_reset_off(pmic_typec_pdphy);
 done:
-	if (ret) {
-		regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+	if (ret)
 		dev_err(dev, "pdphy_enable fail %d\n", ret);
-	}
 
 	return ret;
 }
@@ -524,12 +522,17 @@ static int qcom_pmic_typec_pdphy_start(struct pmic_typec *tcpm,
 
 	ret = pmic_typec_pdphy_reset(pmic_typec_pdphy);
 	if (ret)
-		return ret;
+		goto err_disable_vdd_pdhy;
 
 	for (i = 0; i < pmic_typec_pdphy->nr_irqs; i++)
 		enable_irq(pmic_typec_pdphy->irq_data[i].irq);
 
 	return 0;
+
+err_disable_vdd_pdhy:
+	regulator_disable(pmic_typec_pdphy->vdd_pdphy);
+
+	return ret;
 }
 
 static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm)
Signature · 1 line
2.43.2
Signature · 1 line
heikki