rtc: stm32: fix clock leak on pinctrl probe failure
Subsystems (2)
PATCH v2 1/1 rtc: stm32: fix clock leak on pinctrl probe failure
A failing devm_pinctrl_register_and_init() or pinctrl_enable() in stm32_rtc_probe() returns directly instead of going through err:, leaking rtc_ck/pclk -- and since -EPROBE_DEFER is routine here, each deferred retry leaks another.
Route both through err:, and move the pinctrl setup earlier (right after stm32_rtc_init(), before the RTC device/IRQ are registered) so disabling the clocks there doesn't race a still-live IRQ.
---
Changes in v2:
- Sashiko found that routing the pinctrl failures through err: (as v1 did) disables the clocks while the still-registered RTC device and alarm IRQ are live. Moved the pinctrl setup earlier instead of just moving where the leak was plugged.
drivers/rtc/rtc-stm32.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/rtc/rtc-stm32.c +14 -10
@@ -1137,6 +1137,20 @@ static int stm32_rtc_probe(struct platform_device *pdev)if (ret) goto err; +stm32_rtc_clean_outs (rtc); + + ret =devm_pinctrl_register_and_init (&pdev->dev, &stm32_rtc_pdesc, rtc, &pctl); +if (ret) { +dev_err_probe (&pdev->dev, ret,"pinctrl register failed" ); + goto err; + } + + ret =pinctrl_enable (pctl); +if (ret) { +dev_err_probe (&pdev->dev, ret,"pinctrl enable failed" ); + goto err; + } + rtc->irq_alarm =platform_get_irq (pdev,0 );if (rtc->irq_alarm <=0 ) { ret = rtc->irq_alarm;
@@ -1172,16 +1186,6 @@ static int stm32_rtc_probe(struct platform_device *pdev) goto err; } -stm32_rtc_clean_outs (rtc); - - ret =devm_pinctrl_register_and_init (&pdev->dev, &stm32_rtc_pdesc, rtc, &pctl); -if (ret) -return dev_err_probe (&pdev->dev, ret,"pinctrl register failed" ); - - ret =pinctrl_enable (pctl); -if (ret) -return dev_err_probe (&pdev->dev, ret,"pinctrl enable failed" ); -/* * If INITS flag is reset (calendar year field set to 0x00), calendar * must be initialized
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] rtc: stm32: NULL pointer dereference in stm32_rtc_pinmux_action_lsco() - [High] rtc: stm32…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] rtc: stm32: NULL pointer dereference in stm32_rtc_pinmux_action_lsco()
- [High] rtc: stm32: clk_lsco and OF provider leak on probe error
--
commit c5ee543f4626bb3238dc601e2915f1d27f3674f2
Author: Cong Nguyen <[email protected]>
rtc: stm32: fix clock leak on pinctrl probe failure
A failing devm_pinctrl_register_and_init() or pinctrl_enable() in stm32_rtc_probe() returns directly instead of going through err:, leaking rtc_ck/pclk -- and since -EPROBE_DEFER is routine here, each deferred retry leaks another.
Quoted 7 lines
drivers/rtc/rtc-stm32.c +0
-0@@ -1137,6 +1137,20 @@ static int stm32_rtc_probe(struct platform_device *pdev)if (ret) goto err;
drivers/rtc/rtc-stm32.c static int stm32_rtc_probe(struct platform_device *pdev) · line 1148
Quoted 10 lines
drivers/rtc/rtc-stm32.c +9
-0+stm32_rtc_clean_outs (rtc); + + ret =devm_pinctrl_register_and_init (&pdev->dev, &stm32_rtc_pdesc, rtc, &pctl); +if (ret) { +dev_err_probe (&pdev->dev, ret,"pinctrl register failed" ); + goto err; + } + + ret =pinctrl_enable (pctl);
[Severity: High]
Does this code trigger a null pointer dereference if the 'lsco' function is configured as a pinmux hog in the device tree?
Calling pinctrl_enable() synchronously claims hogs, which invokes the pinmux callback. If 'lsco' is claimed, stm32_rtc_pinmux_action_lsco() will be called:
stm32_rtc_pinmux_action_lsco() {
struct device *dev = rtc->rtc_dev->dev.parent;Since devm_rtc_device_register() hasn't been called yet in the new probe sequence, rtc->rtc_dev is still NULL, which will cause a kernel panic during probe.
Quoted 9 lines
drivers/rtc/rtc-stm32.c +5
-0+if (ret) { +dev_err_probe (&pdev->dev, ret,"pinctrl enable failed" ); + goto err; + } + rtc->irq_alarm =platform_get_irq (pdev,0 );if (rtc->irq_alarm <=0 ) { ret = rtc->irq_alarm;goto err;
[Severity: High]
Can a failure here leak the manually registered clock gate and OF provider?
If 'lsco' is claimed by pinctrl_enable() above, it registers a clock gate and an OF provider:
stm32_rtc_pinmux_action_lsco() {
...
rtc->clk_lsco = clk_register_gate(dev, "rtc_lsco", ...);
of_clk_add_provider(dev->of_node, of_clk_src_simple_get, rtc->clk_lsco);Because pinctrl setup was moved earlier, any subsequent error such as platform_get_irq() failing will jump to the err: label. The err: cleanup path does not unregister rtc->clk_lsco or the OF provider. This will permanently leak these resources on routine errors like -EPROBE_DEFER, prevent successful re-probing, and can cause a use-after-free later since there is no of_clk_del_provider() call.
Quoted 1 line
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 9 lines · Cong Nguyen
A failing
devm_pinctrl_register_and_init()orpinctrl_enable()instm32_rtc_probe()returns directly instead of going through err:, leaking rtc_ck/pclk -- and since -EPROBE_DEFER is routine here, each deferred retry leaks another.Route both through err:, and move the pinctrl setup earlier (right after
stm32_rtc_init(), before the RTC device/IRQ are registered) so disabling the clocks there doesn't race a still-live IRQ.
Quoted 5 lines
Fixes 16ad2bc09efb ("rtc: stm32: add pinctrl and pinmux interfaces")Reported-by Sashiko AI review <[email protected]>Assisted-by LLMSigned-off-by Cong Nguyen <[email protected]>
Yours,
Linus Walleij