Skip to contentSkip to conversation

clk: renesas: fix a double free on error

PATCH In review Dan Carpenter 3 participants 6 messages Jun 17, 2021 – Jun 18, 2021

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

PATCH v2 1/2 clk: renesas: fix a double free on error

The "pll_clk" and "clock" pointers are allocated with devm_kzalloc() so freeing them with kfree() will lead to a double free. This would only happen if probe failed, and the system is not bootable.

Fixes ef3c613ccd68 ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC")

---
V2: Fix "pll_clk" as well.

 drivers/clk/renesas/renesas-rzg2l-cpg.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/clk/renesas/renesas-rzg2l-cpg.c +1 -6
index 5009b9e48b13..7ba36f19896f 100644
@@ -199,11 +199,7 @@ rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
 	pll_clk->priv = priv;
 	pll_clk->type = core->type;
 
-	clk = clk_register(NULL, &pll_clk->hw);
-	if (IS_ERR(clk))
-		kfree(pll_clk);
-
-	return clk;
+	return clk_register(NULL, &pll_clk->hw);
 }
 
 static struct clk
@@ -473,7 +469,6 @@ rzg2l_cpg_register_mod_clk(const struct rzg2l_mod_clk *mod,
 fail:
 	dev_err(dev, "Failed to register %s clock %s: %ld\n", "module",
 		mod->name, PTR_ERR(clk));
-	kfree(clock);
 }
 
 #define rcdev_to_priv(x)	container_of(x, struct rzg2l_cpg_priv, rcdev)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 clk: renesas: Avoid mixing error pointers and NULL

These functions accidentally return both error pointers and NULL when there is an error. It doesn't cause a problem but it is confusing and seems unintentional.

---
 drivers/clk/renesas/renesas-rzg2l-cpg.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/clk/renesas/renesas-rzg2l-cpg.c +3 -6
index 7ba36f19896f..83b58e1cb78f 100644
@@ -124,7 +124,7 @@ rzg2l_cpg_div_clk_register(const struct cpg_core_clk *core,
 						 core->flag, &priv->rmw_lock);
 
 	if (IS_ERR(clk_hw))
-		return NULL;
+		return ERR_CAST(clk_hw);
 
 	return clk_hw->clk;
 }
@@ -174,17 +174,14 @@ rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
 	struct clk_init_data init;
 	const char *parent_name;
 	struct pll_clk *pll_clk;
-	struct clk *clk;
 
 	parent = clks[core->parent & 0xffff];
 	if (IS_ERR(parent))
 		return ERR_CAST(parent);
 
 	pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
-	if (!pll_clk) {
-		clk = ERR_PTR(-ENOMEM);
-		return NULL;
-	}
+	if (!pll_clk)
+		return ERR_PTR(-ENOMEM);
 
 	parent_name = __clk_get_name(parent);
 	init.name = core->name;
Signature · 1 line
2.30.2
Reviewed-by on v2 1/2
#4 lore raw
In reply to Dan Carpenter

Hi Dan,

Thank you for the fix.

Quoted 12 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: 17 June 2021 15:14
To: Geert Uytterhoeven <[email protected]>
Cc: Michael Turquette <[email protected]>; Stephen Boyd <[email protected]>; Prabhakar Mahadev Lad <[email protected]>; Biju Das <[email protected]>; linux-renesas- [email protected]; [email protected]; [email protected]
Subject: [PATCH v2 1/2] clk: renesas: fix a double free on error

The "pll_clk" and "clock" pointers are allocated with devm_kzalloc() so freeing them with kfree() will lead to a double free. This would only happen if probe failed, and the system is not bootable.

Quoted 8 lines
Fixes ef3c613ccd68 ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC")

---
V2: Fix "pll_clk" as well.

 drivers/clk/renesas/renesas-rzg2l-cpg.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

Also Reported-by tag,

Cheers,
Prabhakar

Quoted 27 lines
drivers/clk/renesas/renesas-rzg2l-cpg.c +1 -7
index 5009b9e48b13..7ba36f19896f 100644
@@ -199,11 +199,7 @@ rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
 	pll_clk->priv = priv;
 	pll_clk->type = core->type;
 
-	clk = clk_register(NULL, &pll_clk->hw);
-	if (IS_ERR(clk))
-		kfree(pll_clk);
-
-	return clk;
+	return clk_register(NULL, &pll_clk->hw);
 }
 
 static struct clk
@@ -473,7 +469,6 @@ rzg2l_cpg_register_mod_clk(const struct rzg2l_mod_clk *mod,
 fail:
 	dev_err(dev, "Failed to register %s clock %s: %ld\n", "module",
 		mod->name, PTR_ERR(clk));
-	kfree(clock);
 }
 
 #define rcdev_to_priv(x)	container_of(x, struct rzg2l_cpg_priv, rcdev)
--

2.30.2

Reviewed-by on v1 2/2
#2 lore raw

RE: [PATCH 2/2] clk: renesas: Avoid mixing error pointers and NULL

In reply to Dan Carpenter

Hi Dan,

Thank you for the patch.

Quoted 10 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: 17 June 2021 15:15
To: Geert Uytterhoeven <[email protected]>; Prabhakar Mahadev Lad <prabhakar.mahadev- [email protected]>
Cc: Michael Turquette <[email protected]>; Stephen Boyd <[email protected]>; linux-renesas- [email protected]; [email protected]; [email protected]; kernel- [email protected]
Subject: [PATCH 2/2] clk: renesas: Avoid mixing error pointers and NULL

Quoted 8 lines

These functions accidentally return both error pointers and NULL when there is an error. It doesn't cause a problem but it is confusing and seems unintentional.

---
 drivers/clk/renesas/renesas-rzg2l-cpg.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Cheers,
Prabhakar

Quoted 35 lines
drivers/clk/renesas/renesas-rzg2l-cpg.c +3 -7
index 7ba36f19896f..83b58e1cb78f 100644
@@ -124,7 +124,7 @@ rzg2l_cpg_div_clk_register(const struct cpg_core_clk *core,
 						 core->flag, &priv->rmw_lock);
 
 	if (IS_ERR(clk_hw))
-		return NULL;
+		return ERR_CAST(clk_hw);
 
 	return clk_hw->clk;
 }
@@ -174,17 +174,14 @@ rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
 	struct clk_init_data init;
 	const char *parent_name;
 	struct pll_clk *pll_clk;
-	struct clk *clk;
 
 	parent = clks[core->parent & 0xffff];
 	if (IS_ERR(parent))
 		return ERR_CAST(parent);
 
 	pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
-	if (!pll_clk) {
-		clk = ERR_PTR(-ENOMEM);
-		return NULL;
-	}
+	if (!pll_clk)
+		return ERR_PTR(-ENOMEM);
 
 	parent_name = __clk_get_name(parent);
 	init.name = core->name;
--

2.30.2

Reviewed-by on v2 1/2
#5 lore raw
In reply to Dan Carpenter
Quoted 8 lines · Dan Carpenter

The "pll_clk" and "clock" pointers are allocated with devm_kzalloc() so freeing them with kfree() will lead to a double free. This would only happen if probe failed, and the system is not bootable.

Fixes ef3c613ccd68 ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC")

---
V2: Fix "pll_clk" as well.

i.e. will queue in renesas-clk for v5.15.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] clk: renesas: Avoid mixing error pointers and NULL

In reply to Dan Carpenter

Hi Dan,

Quoted 5 lines · Dan Carpenter

These functions accidentally return both error pointers and NULL when there is an error. It doesn't cause a problem but it is confusing and seems unintentional.

i.e. will queue in renesas-clk-for-v5.15.

Quoted 18 lines
drivers/clk/renesas/renesas-rzg2l-cpg.c +1 -2
@@ -124,7 +124,7 @@ rzg2l_cpg_div_clk_register(const struct cpg_core_clk *core,
                                                 core->flag, &priv->rmw_lock);
 
        if (IS_ERR(clk_hw))
-               return NULL;
+               return ERR_CAST(clk_hw);
 
        return clk_hw->clk;
 }
@@ -174,17 +174,14 @@ rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
        struct clk_init_data init;
        const char *parent_name;
        struct pll_clk *pll_clk;
-       struct clk *clk;
 
        parent = clks[core->parent & 0xffff];

drivers/clk/renesas/renesas-rzg2l-cpg.c rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core, · line 184

Quoted 10 lines
drivers/clk/renesas/renesas-rzg2l-cpg.c +2 -4
        if (IS_ERR(parent))
                return ERR_CAST(parent);
 
        pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
-       if (!pll_clk) {
-               clk = ERR_PTR(-ENOMEM);
-               return NULL;
-       }
+       if (!pll_clk)
+               return ERR_PTR(-ENOMEM);

This part I already have, by virtue of
https://lore.kernel.org/r/[email protected]

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds