Skip to contentSkip to conversation

mfd: syscon: Put the clock after regmap_exit()

PATCH New Arun Iyengar 2 participants 2 messages Nov 19

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

PATCH 1/1 mfd: syscon: Put the clock after regmap_exit()

On the error path, of_syscon_register() puts the clock before regmap_exit(), which then unprepares the freed clock. This happens when the reset lookup fails, e.g. with -EPROBE_DEFER.

Put the clock after regmap_exit().

Fixes a00406b71c5f ("mfd: syscon: Add optional clock support")
Fixes 7d1e3bd94828 ("mfd: syscon: Allow reset control for syscon devices")

---
A related regmap patch ("regmap: mmio: Don't attach the clock if clk_prepare() fails") is sent separately; neither depends on the other.

Tested on an Orange Pi 5 (RK3588S), 7.3 with KASAN, together with the regmap patch: with a forced reset lookup failure, a use-after-free in clk_unprepare() is reported without the two patches and nothing with both.

Developed with Claude (Anthropic), which wrote the code and the commit message. I reviewed every change.

---
 drivers/mfd/syscon.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mfd/syscon.c +4 -5
index 21a7fcdd2737..3ff4e024542b 100644
@@ -132,13 +132,13 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_res)
 		} else {
 			ret = regmap_mmio_attach_clk(regmap, clk);
 			if (ret)
-				goto err_attach_clk;
+				goto err_clk;
 		}
 
 		reset = of_reset_control_get_optional_exclusive(np, NULL);
 		if (IS_ERR(reset)) {
 			ret = PTR_ERR(reset);
-			goto err_attach_clk;
+			goto err_clk;
 		}
 
 		ret = reset_control_deassert(reset);
@@ -155,11 +155,10 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_res)
 
 err_reset:
 	reset_control_put(reset);
-err_attach_clk:
-	if (!IS_ERR(clk))
-		clk_put(clk);
 err_clk:
 	regmap_exit(regmap);
+	if (!IS_ERR(clk))
+		clk_put(clk);
 err_regmap:
 	iounmap(base);
 	return ERR_PTR(ret);

---
base-commit: e5bcc8e02e570e3eee46d4cebefd552cc7c9e843
change-id: 20261119-syscon-clk-uaf-5ff259e0fa15

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

From: Arun Iyengar <[email protected]>

On the error path, of_syscon_register() puts the clock before regmap_exit(), which then unprepares the freed clock. This happens when the reset lookup fails, e.g. with -EPROBE_DEFER.

Quoted 8 lines

Put the clock after regmap_exit().

Fixes a00406b71c5f ("mfd: syscon: Add optional clock support")
Fixes 7d1e3bd94828 ("mfd: syscon: Allow reset control for syscon devices")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1