[PATCH 07/12] pinctrl: nuvoton: ma35: Avoid double free of shared pin configs

From: Lee Jones

Date: Wed Dec 02 2026 - 04:32:41 EST


ma35_pinctrl_parse_groups() allocates a single configs array via
pinconf_generic_parse_dt_config() and assigns the same pointer to every
pin in the group. ma35_pinctrl_dt_node_to_map_func() then copies that
shared pointer into every PIN_MAP_TYPE_CONFIGS_PIN map entry. Because
ma35_pctrl_ops uses pinconf_generic_dt_free_map(), tearing down the
pinctrl mapping frees the same configs pointer once per pin, causing an
immediate double free and leaving dangling pointers in the group data
for subsequent mappings. The configs array is also leaked on error
paths in ma35_pinctrl_parse_groups().

Manage the lifetime of the shared configs array with a devm action
registered right after pinconf_generic_parse_dt_config(), and use a
dedicated dt_free_map callback that frees only the map array.

Fixes: f805e356313b ("pinctrl: nuvoton: Add ma35d1 pinctrl and GPIO driver")
Closes: https://sashiko.dev/#/bug/linux-ec1cf664-4937-4cb3-831c-e2c7838a1aae
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/pinctrl/nuvoton/pinctrl-ma35.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/pinctrl/nuvoton/pinctrl-ma35.c b/drivers/pinctrl/nuvoton/pinctrl-ma35.c
index c20350bc1cdb..b9b34b8b84c6 100644
--- a/drivers/pinctrl/nuvoton/pinctrl-ma35.c
+++ b/drivers/pinctrl/nuvoton/pinctrl-ma35.c
@@ -17,6 +17,7 @@
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/regmap.h>
+#include <linux/slab.h>

#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
@@ -236,12 +237,18 @@ static int ma35_pinctrl_dt_node_to_map_func(struct pinctrl_dev *pctldev,
return 0;
}

+static void ma35_dt_free_map(struct pinctrl_dev *pctldev,
+ struct pinctrl_map *map, unsigned int num_maps)
+{
+ kfree(map);
+}
+
static const struct pinctrl_ops ma35_pctrl_ops = {
.get_groups_count = ma35_get_groups_count,
.get_group_name = ma35_get_group_name,
.get_group_pins = ma35_get_group_pins,
.dt_node_to_map = ma35_pinctrl_dt_node_to_map_func,
- .dt_free_map = pinconf_generic_dt_free_map,
+ .dt_free_map = ma35_dt_free_map,
};

static int ma35_pinmux_get_func_count(struct pinctrl_dev *pctldev)
@@ -1028,6 +1035,11 @@ static const struct pinconf_ops ma35_pinconf_ops = {
.is_generic = true,
};

+static void ma35_free_configs(void *data)
+{
+ kfree(data);
+}
+
static int ma35_pinctrl_parse_groups(struct fwnode_handle *fwnode, struct group_desc *grp,
struct ma35_pinctrl *npctl, u32 index)
{
@@ -1043,6 +1055,13 @@ static int ma35_pinctrl_parse_groups(struct fwnode_handle *fwnode, struct group_
if (ret)
return ret;

+ if (configs) {
+ ret = devm_add_action_or_reset(npctl->dev, ma35_free_configs,
+ configs);
+ if (ret)
+ return ret;
+ }
+
count = fwnode_property_count_u32(fwnode, "nuvoton,pins");
if (!count || count % 3)
return -EINVAL;
--
2.56.1.911.g883d8bcb20-goog