Linux clock framework development
 help / color / mirror / Atom feed
* [PATCH] clk: microchip: Unregister the PIC32MZDA OF clock provider
@ 2026-11-28  9:40 Guangshuo Li
  2026-11-28  9:53 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-11-28  9:40 UTC (permalink / raw)
  To: sboyd, bmasney+clk, jbrunet+clk, purna.mandal; +Cc: linux-clk

The PIC32MZDA OF clock provider points into the device-managed clock
data. It survives unbind and failure to install the failsafe notifier,
allowing a clock lookup to access the freed table.

Remove the provider on both paths before devres releases its data.

This issue was found by our static analysis tool under development.

Fixes: ce6e11884659 ("CLK: microchip: Add Microchip PIC32 clock driver.")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/clk/microchip/clk-pic32mzda.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/microchip/clk-pic32mzda.c b/drivers/clk/microchip/clk-pic32mzda.c
index fc4357ec7103..e889cb813016 100644
--- a/drivers/clk/microchip/clk-pic32mzda.c
+++ b/drivers/clk/microchip/clk-pic32mzda.c
@@ -241,7 +241,16 @@ static int pic32mzda_clk_probe(struct platform_device *pdev)
 
 	/* register NMI for failsafe clock monitor */
 	cd->failsafe_notifier.notifier_call = pic32_fscm_nmi;
-	return register_nmi_notifier(&cd->failsafe_notifier);
+	ret = register_nmi_notifier(&cd->failsafe_notifier);
+	if (ret)
+		of_clk_del_provider(np);
+
+	return ret;
+}
+
+static void pic32mzda_clk_remove(struct platform_device *pdev)
+{
+	of_clk_del_provider(pdev->dev.of_node);
 }
 
 static const struct of_device_id pic32mzda_clk_match_table[] = {
@@ -252,6 +261,7 @@ MODULE_DEVICE_TABLE(of, pic32mzda_clk_match_table);
 
 static struct platform_driver pic32mzda_clk_driver = {
 	.probe		= pic32mzda_clk_probe,
+	.remove		= pic32mzda_clk_remove,
 	.driver		= {
 		.name	= "clk-pic32mzda",
 		.of_match_table = pic32mzda_clk_match_table,

base-commit: d8f05ec001e838cdb94fc14efa7c411e676f9ab9
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] clk: microchip: Unregister the PIC32MZDA OF clock provider
  2026-11-28  9:40 [PATCH] clk: microchip: Unregister the PIC32MZDA OF clock provider Guangshuo Li
@ 2026-11-28  9:53 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28  9:53 UTC (permalink / raw)
  To: Guangshuo Li; +Cc: linux-clk

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] clk: microchip: UAF and incomplete teardown in
  pic32mzda_clk_remove()
- [Medium] clk: microchip: incomplete error unwind in
  pic32mzda_clk_probe()
--

commit 6d31002c60a71977bbe0f0b53f73dd0c80ec6ed3
Author: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

clk: microchip: Unregister the PIC32MZDA OF clock provider

This commit removes the OF clock provider when register_nmi_notifier()
fails during probe and adds a platform driver remove callback to delete the
provider on unbind.

> diff --git a/drivers/clk/microchip/clk-pic32mzda.c b/drivers/clk/microchip/clk-pic32mzda.c
> index fc4357ec7103d..e889cb8130169 100644
> --- a/drivers/clk/microchip/clk-pic32mzda.c
> +++ b/drivers/clk/microchip/clk-pic32mzda.c
> @@ -241,7 +241,16 @@ static int pic32mzda_clk_probe(struct platform_device *pdev)
>  
>  	/* register NMI for failsafe clock monitor */
>  	cd->failsafe_notifier.notifier_call = pic32_fscm_nmi;
> -	return register_nmi_notifier(&cd->failsafe_notifier);
> +	ret = register_nmi_notifier(&cd->failsafe_notifier);
> +	if (ret)
> +		of_clk_del_provider(np);
> +
> +	return ret;
> +}

[Severity: Medium]
Does returning an error here leave non-devm resources and dangling
references behind when devres frees cd and the devm-registered clocks?

pic32mzda_clk_probe() allocates cd with devm_kzalloc_obj() and registers
most clocks via devm_clk_register(), but also performs several non-devm
registrations:

drivers/clk/microchip/clk-pic32mzda.c:pic32mzda_clk_probe() {
	...
	core->iobase = of_io_request_and_map(np, 0, of_node_full_name(np));
	...
	clks[FRCDIVCLK] = clk_register_divider(&pdev->dev, "frcdiv_clk",
					       "frc_clk", 0,
					       core->iobase,
					       OSC_FRCDIV_SHIFT,
					       OSC_FRCDIV_MASK,
					       CLK_DIVIDER_POWER_OF_TWO,
					       &core->reg_lock);
	/* PLL ICLK mux */
	pll_mux_clk = clk_register_mux(&pdev->dev, "spll_mux_clk",
				       pll_mux_parents, 2, 0,
				       core->iobase + 0x020,
				       PLL_ICLK_SHIFT, 1, 0, &core->reg_lock);
	...
	for (i = 0; i < MAXCLKS; i++) {
		if (IS_ERR(clks[i]))
			continue;
		clk_register_clkdev(clks[i], NULL, __clk_get_name(clks[i]));
	}
}

If register_nmi_notifier() fails, devres frees cd and the devm-allocated
clk_hw objects while the clk_lookup entries registered with
clk_register_clkdev() remain on the global clkdev list pointing to freed
clk_hw memory (causing a use-after-free on clk_get()), and frcdiv_clk and
spll_mux_clk remain registered with spinlock pointers to &cd->core.reg_lock
in freed cd.  In addition, the critical clocks enabled just above are not
disabled/unprepared, the fixed-rate clocks are not unregistered, and
core->iobase from of_io_request_and_map() is neither unmapped nor released.

> +
> +static void pic32mzda_clk_remove(struct platform_device *pdev)
> +{
> +	of_clk_del_provider(pdev->dev.of_node);
>  }

[Severity: High]
Can unbinding this driver corrupt the global MIPS nmi_chain and cause a
use-after-free?

pic32mzda_clk_probe() registers &cd->failsafe_notifier with
register_nmi_notifier(), which links the embedded notifier_block into the
global nmi_chain in arch/mips/kernel/traps.c (and MIPS provides no
unregister_nmi_notifier() helper).  When pic32mzda_clk_remove() returns,
devres frees cd while &cd->failsafe_notifier is still linked in nmi_chain,
so a subsequent NMI or register_nmi_notifier() call walks freed memory.

Unbinding also suffers from the same incomplete teardown as the probe error
path above: the non-devm clkdev lookup entries, non-devm clocks referencing
&cd->core.reg_lock, enabled critical clocks, and the of_io_request_and_map()
mapping and memory region are all left behind.  Since this is a core SoC
clock controller, should pic32mzda_clk_driver set .suppress_bind_attrs =
true instead of providing an incomplete .remove callback?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-f81b700d6c-u16-0@mail.sdu.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28  9:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  9:40 [PATCH] clk: microchip: Unregister the PIC32MZDA OF clock provider Guangshuo Li
2026-11-28  9:53 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox