Skip to contentSkip to conversation

pinctrl: nomadik: validate DT pin config property read and range

PATCH New Elkin Cruz 1 participant 1 message Nov 25

Subsystems (4)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 pinctrl: nomadik: validate DT pin config property read and range

In nmk_pinctrl_dt_get_config(), of_property_read_u32() is only checked for (ret != -EINVAL), passing an uninitialized val to nmk_dt_pin_config() on other error returns. In addition, val is signed and nmk_dt_pin_config() returns 0 even when val is out of bounds for nmk_cfg_params[index].choice.

Check (!ret) after of_property_read_u32(), make val unsigned (u32), and return -EINVAL when val >= nmk_cfg_params[index].size.

Fixes e32af8894588 ("pinctrl/nomadik: add device tree support")

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 drivers/pinctrl/nomadik/pinctrl-nomadik.c | 24 +++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/pinctrl/nomadik/pinctrl-nomadik.c +14 -10
index 617ad259f62c..400009929dbd 100644
@@ -693,18 +693,21 @@ static const struct nmk_cfg_param nmk_cfg_params[] = {
 	NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable",	nmk_pin_sleep_pdis_modes),
 };
 
-static int nmk_dt_pin_config(int index, int val, unsigned long *config)
+static int nmk_dt_pin_config(int index, u32 val, unsigned long *config)
 {
 	if (!nmk_cfg_params[index].choice) {
 		*config = nmk_cfg_params[index].config;
-	} else {
-		/* test if out of range */
-		if  (val < nmk_cfg_params[index].size) {
-			*config = nmk_cfg_params[index].config |
-				nmk_cfg_params[index].choice[val];
-		}
+		return 0;
 	}
-	return 0;
+
+	/* test if out of range */
+	if (val < nmk_cfg_params[index].size) {
+		*config = nmk_cfg_params[index].config |
+			nmk_cfg_params[index].choice[val];
+		return 0;
+	}
+
+	return -EINVAL;
 }
 
 static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
@@ -724,11 +727,12 @@ static bool nmk_pinctrl_dt_get_config(struct device_node *np,
 {
 	bool has_config = 0;
 	unsigned long cfg = 0;
-	int i, val, ret;
+	int i, ret;
+	u32 val;
 
 	for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
 		ret = of_property_read_u32(np, nmk_cfg_params[i].property, &val);
-		if (ret != -EINVAL) {
+		if (!ret) {
 			if (nmk_dt_pin_config(i, val, &cfg) == 0) {
 				*configs |= cfg;
 				has_config = 1;
Signature · 1 line
2.56.1.839.g22a8290db9-goog