Skip to contentSkip to conversation

usb: typec: qcom-pmic-typec: Avoid reading uninitialized state on error paths

PATCH In review Ian Rogers 3 participants 4 messages Nov 19 – Nov 20

Subsystems (4)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 usb: typec: qcom-pmic-typec: Avoid reading uninitialized state on error paths

In qcom_pmic_typec_port_set_cc(), error paths from regmap_read() and the default switch branch jump to 'done:', which logs 'currsrc', 'mode', and 'misc' via dev_dbg() and passes uninitialized 'currsrc' into rp_sel_to_name(), indexing rp_sel_name[] out-of-bounds when dynamic debug is enabled. Similarly, early error paths in qcom_pmic_typec_port_get_cc() jump to 'done:' before 'misc', 'attached', '*cc1', and '*cc2' are initialized.

Return early on error paths in both functions instead of jumping to the post-success dev_dbg() trace.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not tested on Qualcomm PMIC Type-C hardware.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
---
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 25 +++++++++----------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +12 -13
index b2ad31e85b5e..a1595b8b53e7 100644
@@ -372,14 +372,12 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 	ret = regmap_read(pmic_typec_port->regmap,
 			  pmic_typec_port->base + TYPEC_MISC_STATUS_REG, &misc);
 	if (ret)
-		goto done;
+		return ret;
 
 	attached = !!(misc & CC_ATTACHED);
 
-	if (pmic_typec_port->debouncing_cc) {
-		ret = -EBUSY;
-		goto done;
-	}
+	if (pmic_typec_port->debouncing_cc)
+		return -EBUSY;
 
 	*cc1 = TYPEC_CC_OPEN;
 	*cc2 = TYPEC_CC_OPEN;
@@ -392,7 +390,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 				  pmic_typec_port->base + TYPEC_SRC_STATUS_REG,
 				  &val);
 		if (ret)
-			goto done;
+			return ret;
 		switch (val & DETECTED_SRC_TYPE_MASK) {
 		case AUDIO_ACCESS_RA_RA:
 			val = TYPEC_CC_RA;
@@ -417,7 +415,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 				  pmic_typec_port->base + TYPEC_SNK_STATUS_REG,
 				  &val);
 		if (ret)
-			goto done;
+			return ret;
 		switch (val & DETECTED_SNK_TYPE_MASK) {
 		case SNK_RP_STD:
 			val = TYPEC_CC_RP_DEF;
@@ -472,7 +470,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 			  pmic_typec_port->base + TYPEC_MISC_STATUS_REG,
 			  &misc);
 	if (ret)
-		goto done;
+		goto err_unlock;
 
 	mode = EN_SRC_ONLY;
 
@@ -496,7 +494,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 	default:
 		dev_warn(dev, "unexpected set_cc %d\n", cc);
 		ret = -EINVAL;
-		goto done;
+		goto err_unlock;
 	}
 
 	if (mode == EN_SRC_ONLY) {
@@ -504,14 +502,11 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 				   pmic_typec_port->base + TYPEC_CURRSRC_CFG_REG,
 				   currsrc);
 		if (ret)
-			goto done;
+			goto err_unlock;
 	}
 
 	pmic_typec_port->cc = cc;
 	qcom_pmic_set_cc_debounce(pmic_typec_port);
-	ret = 0;
-
-done:
 	spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
 
 	dev_dbg(dev, "set_cc: currsrc=%x %s mode %s debounce %d attached %d cc=%s\n",
@@ -520,6 +515,10 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 		pmic_typec_port->debouncing_cc, !!(misc & CC_ATTACHED),
 		misc_to_cc(misc));
 
+	return 0;
+
+err_unlock:
+	spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
 	return ret;
 }
 
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

In qcom_pmic_typec_port_set_cc(), error paths from regmap_read() and the default switch branch jump to 'done:', which logs 'currsrc', 'mode', and 'misc' via dev_dbg() and passes uninitialized 'currsrc' into rp_sel_to_name(), indexing rp_sel_name[] out-of-bounds when dynamic debug is enabled. Similarly, early error paths in qcom_pmic_typec_port_get_cc() jump to 'done:' before 'misc', 'attached', '*cc1', and '*cc2' are initialized.

Return early on error paths in both functions instead of jumping to the post-success dev_dbg() trace.

Quoted 7 lines

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not tested on Qualcomm PMIC Type-C hardware.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Ian Rogers
Quoted 11 lines · Ian Rogers

In qcom_pmic_typec_port_set_cc(), error paths from regmap_read() and the default switch branch jump to 'done:', which logs 'currsrc', 'mode', and 'misc' via dev_dbg() and passes uninitialized 'currsrc' into rp_sel_to_name(), indexing rp_sel_name[] out-of-bounds when dynamic debug is enabled. Similarly, early error paths in qcom_pmic_typec_port_get_cc() jump to 'done:' before 'misc', 'attached', '*cc1', and '*cc2' are initialized.

Return early on error paths in both functions instead of jumping to the post-success dev_dbg() trace.

Quoted 7 lines

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not tested on Qualcomm PMIC Type-C hardware.

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")

No stable here either?

Thanks,

Quoted 91 lines
---
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 25 +++++++++----------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +12 -13
index b2ad31e85b5e..a1595b8b53e7 100644
@@ -372,14 +372,12 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 	ret = regmap_read(pmic_typec_port->regmap,
 			  pmic_typec_port->base + TYPEC_MISC_STATUS_REG, &misc);
 	if (ret)
-		goto done;
+		return ret;
 
 	attached = !!(misc & CC_ATTACHED);
 
-	if (pmic_typec_port->debouncing_cc) {
-		ret = -EBUSY;
-		goto done;
-	}
+	if (pmic_typec_port->debouncing_cc)
+		return -EBUSY;
 
 	*cc1 = TYPEC_CC_OPEN;
 	*cc2 = TYPEC_CC_OPEN;
@@ -392,7 +390,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 				  pmic_typec_port->base + TYPEC_SRC_STATUS_REG,
 				  &val);
 		if (ret)
-			goto done;
+			return ret;
 		switch (val & DETECTED_SRC_TYPE_MASK) {
 		case AUDIO_ACCESS_RA_RA:
 			val = TYPEC_CC_RA;
@@ -417,7 +415,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
 				  pmic_typec_port->base + TYPEC_SNK_STATUS_REG,
 				  &val);
 		if (ret)
-			goto done;
+			return ret;
 		switch (val & DETECTED_SNK_TYPE_MASK) {
 		case SNK_RP_STD:
 			val = TYPEC_CC_RP_DEF;
@@ -472,7 +470,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 			  pmic_typec_port->base + TYPEC_MISC_STATUS_REG,
 			  &misc);
 	if (ret)
-		goto done;
+		goto err_unlock;
 
 	mode = EN_SRC_ONLY;
 
@@ -496,7 +494,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 	default:
 		dev_warn(dev, "unexpected set_cc %d\n", cc);
 		ret = -EINVAL;
-		goto done;
+		goto err_unlock;
 	}
 
 	if (mode == EN_SRC_ONLY) {
@@ -504,14 +502,11 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 				   pmic_typec_port->base + TYPEC_CURRSRC_CFG_REG,
 				   currsrc);
 		if (ret)
-			goto done;
+			goto err_unlock;
 	}
 
 	pmic_typec_port->cc = cc;
 	qcom_pmic_set_cc_debounce(pmic_typec_port);
-	ret = 0;
-
-done:
 	spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
 
 	dev_dbg(dev, "set_cc: currsrc=%x %s mode %s debounce %d attached %d cc=%s\n",
@@ -520,6 +515,10 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
 		pmic_typec_port->debouncing_cc, !!(misc & CC_ATTACHED),
 		misc_to_cc(misc));
 
+	return 0;
+
+err_unlock:
+	spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
 	return ret;
 }
 
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Signature · 1 line
heikki
author
#3 lore raw
Quoted 18 lines · Heikki Krogerus
Quoted 16 lines · Ian Rogers

In qcom_pmic_typec_port_set_cc(), error paths from regmap_read() and the default switch branch jump to 'done:', which logs 'currsrc', 'mode', and 'misc' via dev_dbg() and passes uninitialized 'currsrc' into rp_sel_to_name(), indexing rp_sel_name[] out-of-bounds when dynamic debug is enabled. Similarly, early error paths in qcom_pmic_typec_port_get_cc() jump to 'done:' before 'misc', 'attached', '*cc1', and '*cc2' are initialized.

Return early on error paths in both functions instead of jumping to the post-success dev_dbg() trace.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not tested on Qualcomm PMIC Type-C hardware.

Quoted 4 lines
Quoted 2 lines
Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")

No stable here either?

Agreed, 'Cc: [email protected]' makes sense here too (especially for the rp_sel_name[] out-of-bounds read under dynamic debug). I'm happy to send a v2 adding Cc: [email protected], unless you prefer to add it when applying.

Thanks,
Ian

Quoted 96 lines

Thanks,

Quoted 90 lines
---
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 25 +++++++++----------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +12 -14
index b2ad31e85b5e..a1595b8b53e7 100644
@@ -372,14 +372,12 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
      ret = regmap_read(pmic_typec_port->regmap,
                        pmic_typec_port->base + TYPEC_MISC_STATUS_REG, &misc);
      if (ret)
-             goto done;
+             return ret;
 
      attached = !!(misc & CC_ATTACHED);
 
-     if (pmic_typec_port->debouncing_cc) {
-             ret = -EBUSY;
-             goto done;
-     }
+     if (pmic_typec_port->debouncing_cc)
+             return -EBUSY;
 
      *cc1 = TYPEC_CC_OPEN;
      *cc2 = TYPEC_CC_OPEN;
@@ -392,7 +390,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
                                pmic_typec_port->base + TYPEC_SRC_STATUS_REG,
                                &val);
              if (ret)
-                     goto done;
+                     return ret;
              switch (val & DETECTED_SRC_TYPE_MASK) {
              case AUDIO_ACCESS_RA_RA:
                      val = TYPEC_CC_RA;
@@ -417,7 +415,7 @@ static int qcom_pmic_typec_port_get_cc(struct tcpc_dev *tcpc,
                                pmic_typec_port->base + TYPEC_SNK_STATUS_REG,
                                &val);
              if (ret)
-                     goto done;
+                     return ret;
              switch (val & DETECTED_SNK_TYPE_MASK) {
              case SNK_RP_STD:
                      val = TYPEC_CC_RP_DEF;
@@ -472,7 +470,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
                        pmic_typec_port->base + TYPEC_MISC_STATUS_REG,
                        &misc);
      if (ret)
-             goto done;
+             goto err_unlock;
 
      mode = EN_SRC_ONLY;
 
@@ -496,7 +494,7 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
      default:
              dev_warn(dev, "unexpected set_cc %d\n", cc);
              ret = -EINVAL;
-             goto done;
+             goto err_unlock;
      }
 
      if (mode == EN_SRC_ONLY) {
@@ -504,14 +502,11 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
                                 pmic_typec_port->base + TYPEC_CURRSRC_CFG_REG,
                                 currsrc);
              if (ret)
-                     goto done;
+                     goto err_unlock;
      }
 
      pmic_typec_port->cc = cc;
      qcom_pmic_set_cc_debounce(pmic_typec_port);
-     ret = 0;
-
-done:
      spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
 
      dev_dbg(dev, "set_cc: currsrc=%x %s mode %s debounce %d attached %d cc=%s\n",
@@ -520,6 +515,10 @@ static int qcom_pmic_typec_port_set_cc(struct tcpc_dev *tcpc,
              pmic_typec_port->debouncing_cc, !!(misc & CC_ATTACHED),
              misc_to_cc(misc));
 
+     return 0;
+
+err_unlock:
+     spin_unlock_irqrestore(&pmic_typec_port->lock, flags);
      return ret;
 }
 
--
Quoted 1 line

2.56.1.708.g77570a6a1d-goog

--
heikki