Skip to contentSkip to conversation

pinctrl: starfive: jh7100: Reject malformed pin properties

PATCH In review Zhiqi Zhang 2 participants 3 messages Nov 25 – Dec 3

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

PATCH 1/1 pinctrl: starfive: jh7100: Reject malformed pin properties

The JH7100 binding allows each pin group to specify either "pinmux" or "pins", but not both. starfive_dt_node_to_map() uses of_property_count_u32_elems() to decide which property is present, so a negative parser error is handled the same way as an absent property.

A group that contains a malformed "pinmux" property and a valid "pins" property is therefore accepted as a "pins" group. Similarly, a malformed "pins" property can be ignored when a valid "pinmux" property is present. Both cases silently accept a device tree node that the binding rejects.

Check property presence separately from array length. Reject groups that specify both or neither property, and require the selected property's u32 array to contain at least one element before sizing and filling the maps.

I found this issue. OpenAI Codex helped draft the fix, and I reviewed the result.

Fixes ec648f6b7686 ("pinctrl: starfive: Add pinctrl driver for StarFive SoCs")
---
 .../starfive/pinctrl-starfive-jh7100.c        | 41 ++++++++++++++-----
 1 file changed, 30 insertions(+), 11 deletions(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c +30 -11
index d89f6a4722..97c96c92d3 100644
@@ -491,24 +491,34 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
 	nmaps = 0;
 	ngroups = 0;
 	for_each_available_child_of_node_scoped(np, child) {
-		int npinmux = of_property_count_u32_elems(child, "pinmux");
-		int npins   = of_property_count_u32_elems(child, "pins");
+		bool has_pinmux = of_property_present(child, "pinmux");
+		bool has_pins = of_property_present(child, "pins");
+		int npins;
 
-		if (npinmux > 0 && npins > 0) {
+		if (has_pinmux && has_pins) {
 			dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: both pinmux and pins set\n",
 				np, child);
 			return -EINVAL;
 		}
-		if (npinmux == 0 && npins == 0) {
+		if (!has_pinmux && !has_pins) {
 			dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: neither pinmux nor pins set\n",
 				np, child);
 			return -EINVAL;
 		}
 
-		if (npinmux > 0)
+		if (has_pinmux) {
+			npins = of_property_count_u32_elems(child, "pinmux");
+			if (npins < 1)
+				return npins ?: -EINVAL;
+
 			nmaps += 2;
-		else
+		} else {
+			npins = of_property_count_u32_elems(child, "pins");
+			if (npins < 1)
+				return npins ?: -EINVAL;
+
 			nmaps += 1;
+		}
 		ngroups += 1;
 	}
 
@@ -535,7 +545,13 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
 
 		pgnames[ngroups++] = grpname;
 
-		if ((npins = of_property_count_u32_elems(child, "pinmux")) > 0) {
+		if (of_property_present(child, "pinmux")) {
+			npins = of_property_count_u32_elems(child, "pinmux");
+			if (npins < 1) {
+				ret = npins ?: -EINVAL;
+				goto free_map;
+			}
+
 			pins = devm_kzalloc_objs(dev, *pins, npins);
 			if (!pins) {
 				ret = -ENOMEM;
@@ -562,7 +578,13 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
 			map[nmaps].data.mux.function = np->name;
 			map[nmaps].data.mux.group = grpname;
 			nmaps += 1;
-		} else if ((npins = of_property_count_u32_elems(child, "pins")) > 0) {
+		} else {
+			npins = of_property_count_u32_elems(child, "pins");
+			if (npins < 1) {
+				ret = npins ?: -EINVAL;
+				goto free_map;
+			}
+
 			pins = devm_kzalloc_objs(dev, *pins, npins);
 			if (!pins) {
 				ret = -ENOMEM;
@@ -579,9 +601,6 @@ static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
 					goto free_map;
 				pins[i] = v;
 			}
-		} else {
-			ret = -EINVAL;
-			goto free_map;
 		}
 
 		ret = pinctrl_generic_add_group(pctldev, grpname, pins, npins, pinmux);
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#1 lore raw
Quoted 14 lines · Zhiqi Zhang

The JH7100 binding allows each pin group to specify either "pinmux" or "pins", but not both. starfive_dt_node_to_map() uses of_property_count_u32_elems() to decide which property is present, so a negative parser error is handled the same way as an absent property.

A group that contains a malformed "pinmux" property and a valid "pins" property is therefore accepted as a "pins" group. Similarly, a malformed "pins" property can be ignored when a valid "pinmux" property is present. Both cases silently accept a device tree node that the binding rejects.

Check property presence separately from array length. Reject groups that specify both or neither property, and require the selected property's u32 array to contain at least one element before sizing and filling the maps.

Quoted 5 lines

I found this issue. OpenAI Codex helped draft the fix, and I reviewed the result.

Fixes ec648f6b7686 ("pinctrl: starfive: Add pinctrl driver for StarFive SoCs")

Can one of the Starfive maintainers/authors review this patch?

Yours,
Linus Walleij

maintainer
#2 lore raw
In reply to Zhiqi Zhang
Quoted 14 lines · Zhiqi Zhang

The JH7100 binding allows each pin group to specify either "pinmux" or "pins", but not both. starfive_dt_node_to_map() uses of_property_count_u32_elems() to decide which property is present, so a negative parser error is handled the same way as an absent property.

A group that contains a malformed "pinmux" property and a valid "pins" property is therefore accepted as a "pins" group. Similarly, a malformed "pins" property can be ignored when a valid "pinmux" property is present. Both cases silently accept a device tree node that the binding rejects.

Check property presence separately from array length. Reject groups that specify both or neither property, and require the selected property's u32 array to contain at least one element before sizing and filling the maps.

Quoted 5 lines

I found this issue. OpenAI Codex helped draft the fix, and I reviewed the result.

Fixes ec648f6b7686 ("pinctrl: starfive: Add pinctrl driver for StarFive SoCs")

What I want to know is if such a malformed pinmux property and pins present condition passes the device tree schema validation for this driver?

I guess we should check for this kind of stuff to some extent but it can become overzealous and bloat the kernel with runtime checks of device trees that wouldn't pass validation in the first place, and the boot chain should protect against attackers inserting invalid device trees so it's not really an attack vector.

Quoted 13 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c +10 -2
-               if (npinmux > 0)
+               if (has_pinmux) {
+                       npins = of_property_count_u32_elems(child, "pinmux");
+                       if (npins < 1)
+                               return npins ?: -EINVAL;
+
                        nmaps += 2;
-               else
+               } else {
+                       npins = of_property_count_u32_elems(child, "pins");
+                       if (npins < 1)
+                               return npins ?: -EINVAL;
+

(...)

drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev, · line 586

Quoted 6 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c +6 -0
+               } else {
+                       npins = of_property_count_u32_elems(child, "pins");
+                       if (npins < 1) {
+                               ret = npins ?: -EINVAL;
+                               goto free_map;
+                       }

Three copies of very similar code?

Create a static helper function to check the property count.

Yours,
Linus Walleij