[PATCH] pinctrl: starfive: jh7100: Reject malformed pin properties

From: Zhiqi Zhang

Date: Wed Nov 25 2026 - 01:07:55 EST


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")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
.../starfive/pinctrl-starfive-jh7100.c | 41 ++++++++++++++-----
1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c b/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
index d89f6a4722..97c96c92d3 100644
--- a/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
+++ b/drivers/pinctrl/starfive/pinctrl-starfive-jh7100.c
@@ -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);
--
2.54.0 (Apple Git-157)