Skip to contentSkip to conversation

clk: Fix OOB writes, UAFs, ERR_PTR dereferences, and rate-calc bugs (batch 2)

PATCH New Lee Jones 2 participants 14 messages Nov 27

Subsystems (11)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover clk: Fix OOB writes, UAFs, ERR_PTR dereferences, and rate-calc bugs (batch 2)

These issues were reported by Sashiko.

This is batch 2 of Common Clock Framework fixes, based on clk/clk-next. It addresses output array OOB writes in ti/fapll, ERR_PTR dereferences and double-unregister in tegra/bpmp, notifier UAFs and global array mutation in starfive/jh7110 and rockchip/cpu, rate and parent lookup bugs in rp1, and allocation error handling in sci-clk and icst.

Cc "Thomas Richard (TI)" <[email protected]>
Cc Andrea della Porta <[email protected]>
Cc Brian Masney <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Dominique Belhachemi <[email protected]>
Cc Doug Anderson <[email protected]>
Cc Emil Renner Berthing <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Hal Feng <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Jonathan Hunter <[email protected]>
Cc Joshua Peisach <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Michael Turquette <[email protected]>
Cc Michal Wilczynski <[email protected]>
Cc Naushir Patuck <[email protected]>
Cc Nishanth Menon <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Prashant Gaikwad <[email protected]>
Cc Rob Herring <[email protected]>
Cc Rosen Penev <[email protected]>
Cc Santosh Shilimkar <[email protected]>
Cc Stephen Boyd <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Thomas Abraham <[email protected]>
Cc Timo Alho <[email protected]>
Cc Tony Lindgren <[email protected]>
Cc Wentao Liang <[email protected]>
Cc Xingyu Wu <[email protected]>

Lee Jones (10):
clk: ti: fapll: Fix output clock array allocation and bounds check
clk: tegra: bpmp: Drop manual clock unregister on provider failure
clk: starfive: jh7110-sys: Use devres for PLL0 clock and notifier
clk: starfive: jh7110: Duplicate top clock bulk arrays per device
clk: rockchip: cpu: Allocate rate_table before registering notifier
clk: tegra: bpmp: Skip ERR_PTR entries in tegra_bpmp_clk_of_xlate()
clk: rp1: Propagate determine_rate() return value for PLL dividers
clk: rp1: Reference secondary PLL divider clk_hw in parent tables
clk: keystone: sci-clk: Check allocation failure before copying clock
array
clk: versatile: icst: Check kasprintf() and put parent node on error

 drivers/clk/clk-rp1.c                         | 32 +++++++++----------
 drivers/clk/keystone/sci-clk.c                |  8 +++--
 drivers/clk/rockchip/clk-cpu.c                | 26 +++++++--------
 .../clk/starfive/clk-starfive-jh7110-isp.c    |  9 ++++--
 .../clk/starfive/clk-starfive-jh7110-sys.c    |  5 +--
 .../clk/starfive/clk-starfive-jh7110-vout.c   |  9 ++++--
 drivers/clk/tegra/clk-bpmp.c                  | 14 +-------
 drivers/clk/ti/fapll.c                        | 13 +++++---
 drivers/clk/versatile/clk-icst.c              |  5 +++
 9 files changed, 66 insertions(+), 55 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH 1/10 clk: ti: fapll: Fix output clock array allocation and bounds check

ti_fapll_setup() allocates fd->outputs.clks as sizeof(struct clk *) * MAX_FAPLL_OUTPUTS + 1, adding a single byte rather than an extra pointer slot for the 1-based child synthesisers stored at indices 1..MAX_FAPLL_OUTPUTS. It also reads output_instance into a signed int via of_property_read_u32_index() and indexes fd->outputs.clks[output_instance] without bounds validation, allowing out-of-bounds heap writes.

Allocate MAX_FAPLL_OUTPUTS + 1 pointer slots with kzalloc_objs(), store output_instance as u32, and skip entries exceeding MAX_FAPLL_OUTPUTS.

Fixes 163152cbbe32 ("clk: ti: Add support for FAPLL on dm816x")
---
 drivers/clk/ti/fapll.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/clk/ti/fapll.c +9 -4
index 7d6a4dd4fc8c..d5e95e6489cd 100644
@@ -549,9 +549,8 @@ static void __init ti_fapll_setup(struct device_node *node)
 	if (!fd)
 		return;
 
-	fd->outputs.clks = kzalloc(sizeof(struct clk *) *
-				   MAX_FAPLL_OUTPUTS + 1,
-				   GFP_KERNEL);
+	fd->outputs.clks = kzalloc_objs(*fd->outputs.clks,
+					MAX_FAPLL_OUTPUTS + 1);
 	if (!fd->outputs.clks)
 		goto free;
 
@@ -614,7 +613,7 @@ static void __init ti_fapll_setup(struct device_node *node)
 		const char *output_name;
 		void __iomem *freq, *div;
 		struct clk *synth_clk;
-		int output_instance;
+		u32 output_instance;
 		u32 v;
 
 		if (of_property_read_string_index(node, "clock-output-names",
@@ -625,6 +624,12 @@ static void __init ti_fapll_setup(struct device_node *node)
 					       &output_instance))
 			output_instance = i;
 
+		if (output_instance > MAX_FAPLL_OUTPUTS) {
+			pr_err("%pOFn invalid output_instance %u\n",
+			       node, output_instance);
+			continue;
+		}
+
 		freq = fd->base + (output_instance * 8);
 		div = freq + 4;
 
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#3 lore raw

PATCH 2/10 clk: tegra: bpmp: Drop manual clock unregister on provider failure

In reply to Lee Jones

tegra_bpmp_clk_register() registers every clock with devm_clk_hw_register(), and tegra_bpmp_register_clocks_one() records ERR_PTR values in bpmp->clocks[i] whenever an individual clock fails to register. If of_clk_add_hw_provider() fails, tegra_bpmp_unregister_clocks() unconditionally dereferences bpmp->clocks[i] and calls clk_hw_unregister(), crashing on any error pointer and double-unregistering clocks when devres subsequently unwinds on probe failure.

Remove tegra_bpmp_unregister_clocks() and rely on devres to unregister the clocks cleanly.

Fixes ca6f2796eef7 ("clk: tegra: Add BPMP clock driver")
---
 drivers/clk/tegra/clk-bpmp.c | 12 ------------
 1 file changed, 12 deletions(-)
drivers/clk/tegra/clk-bpmp.c +0 -12
index 3649324e2e1f..e4b4f3eec777 100644
@@ -648,14 +648,6 @@ static int tegra_bpmp_register_clocks(struct tegra_bpmp *bpmp,
 	return 0;
 }
 
-static void tegra_bpmp_unregister_clocks(struct tegra_bpmp *bpmp)
-{
-	unsigned int i;
-
-	for (i = 0; i < bpmp->num_clocks; i++)
-		clk_hw_unregister(&bpmp->clocks[i]->hw);
-}
-
 static struct clk_hw *tegra_bpmp_clk_of_xlate(struct of_phandle_args *clkspec,
 					      void *data)
 {
@@ -696,10 +688,6 @@ int tegra_bpmp_init_clocks(struct tegra_bpmp *bpmp)
 	err = of_clk_add_hw_provider(bpmp->dev->of_node,
 				     tegra_bpmp_clk_of_xlate,
 				     bpmp);
-	if (err < 0) {
-		tegra_bpmp_unregister_clocks(bpmp);
-		goto free;
-	}
 
 free:
 	kfree(clocks);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#5 lore raw

PATCH 3/10 clk: starfive: jh7110-sys: Use devres for PLL0 clock and notifier

In reply to Lee Jones

jh7110_syscrg_probe() allocates priv with devm_kzalloc_flex() but acquires pll0_out with clk_get() and registers &priv->pll_clk_nb with unmanaged clk_notifier_register(). If any subsequent step in probe fails, devres frees priv while the notifier stays registered on pll0_out, resulting in a use-after-free on the next rate change and a leaked clock reference.

Use devm_clk_get() and devm_clk_notifier_register() so both the notifier and clock reference are released automatically in LIFO order before priv is freed.

Fixes 538d5477b252 ("clk: starfive: jh7110-sys: Add notifier for PLL0 clock")
---
 drivers/clk/starfive/clk-starfive-jh7110-sys.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/clk/starfive/clk-starfive-jh7110-sys.c +3 -2
index 44766b094b60..02ecf32fffdc 100644
@@ -422,7 +422,7 @@ static int __init jh7110_syscrg_probe(struct platform_device *pdev)
 		return PTR_ERR(priv->base);
 
 	/* Use fixed factor clocks if can not get the PLL clocks from DTS */
-	pllclk = clk_get(priv->dev, "pll0_out");
+	pllclk = devm_clk_get(priv->dev, "pll0_out");
 	if (IS_ERR(pllclk)) {
 		/* 24MHz -> 1000.0MHz */
 		priv->pll[0] = devm_clk_hw_register_fixed_factor(priv->dev, "pll0_out",
@@ -431,7 +431,8 @@ static int __init jh7110_syscrg_probe(struct platform_device *pdev)
 			return PTR_ERR(priv->pll[0]);
 	} else {
 		priv->pll_clk_nb.notifier_call = jh7110_pll0_clk_notifier_cb;
-		ret = clk_notifier_register(pllclk, &priv->pll_clk_nb);
+		ret = devm_clk_notifier_register(priv->dev, pllclk,
+						 &priv->pll_clk_nb);
 		if (ret)
 			return ret;
 		priv->pll[0] = NULL;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#6 lore raw

PATCH 4/10 clk: starfive: jh7110: Duplicate top clock bulk arrays per device

In reply to Lee Jones

Both the JH7110 ISP and Video-Output clock drivers point top->top_clks directly at static global struct clk_bulk_data arrays and pass them to devm_clk_bulk_get(), mutating shared global state in place across probes or unbind/rebind cycles.

Mark the template arrays const and allocate per-device copies with devm_kmemdup_array().

Fixes 81279f5d0812 ("clk: starfive: Add StarFive JH7110 Image-Signal-Process clock driver")
Fixes dae5448a327e ("clk: starfive: Add StarFive JH7110 Video-Output clock driver")
---
 drivers/clk/starfive/clk-starfive-jh7110-isp.c  | 9 +++++++--
 drivers/clk/starfive/clk-starfive-jh7110-vout.c | 9 +++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)
drivers/clk/starfive/clk-starfive-jh7110-isp.c +7 -2
index d39ce4a2a8e9..9395de01d42b 100644
@@ -23,7 +23,7 @@
 #define JH7110_ISPCLK_DVP_CLK			(JH7110_ISPCLK_END + 3)
 #define JH7110_ISPCLK_EXT_END			(JH7110_ISPCLK_END + 4)
 
-static struct clk_bulk_data jh7110_isp_top_clks[] = {
+static const struct clk_bulk_data jh7110_isp_top_clks[] = {
 	{ .id = "isp_top_core" },
 	{ .id = "isp_top_axi" }
 };
@@ -119,8 +119,13 @@ static int jh7110_ispcrg_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->base))
 		return PTR_ERR(priv->base);
 
-	top->top_clks = jh7110_isp_top_clks;
 	top->top_clks_num = ARRAY_SIZE(jh7110_isp_top_clks);
+	top->top_clks = devm_kmemdup_array(priv->dev, jh7110_isp_top_clks,
+					   top->top_clks_num,
+					   sizeof(*top->top_clks), GFP_KERNEL);
+	if (!top->top_clks)
+		return -ENOMEM;
+
 	ret = devm_clk_bulk_get(priv->dev, top->top_clks_num, top->top_clks);
 	if (ret)
 		return dev_err_probe(priv->dev, ret, "failed to get main clocks\n");
drivers/clk/starfive/clk-starfive-jh7110-vout.c +7 -2
index d4181cce9c15..8b4dbae372ee 100644
@@ -25,7 +25,7 @@
 #define JH7110_VOUTCLK_HDMITX0_PIXELCLK		(JH7110_VOUTCLK_END + 5)
 #define JH7110_VOUTCLK_EXT_END			(JH7110_VOUTCLK_END + 6)
 
-static struct clk_bulk_data jh7110_vout_top_clks[] = {
+static const struct clk_bulk_data jh7110_vout_top_clks[] = {
 	{ .id = "vout_src" },
 	{ .id = "vout_top_ahb" }
 };
@@ -126,8 +126,13 @@ static int jh7110_voutcrg_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->base))
 		return PTR_ERR(priv->base);
 
-	top->top_clks = jh7110_vout_top_clks;
 	top->top_clks_num = ARRAY_SIZE(jh7110_vout_top_clks);
+	top->top_clks = devm_kmemdup_array(priv->dev, jh7110_vout_top_clks,
+					   top->top_clks_num,
+					   sizeof(*top->top_clks), GFP_KERNEL);
+	if (!top->top_clks)
+		return -ENOMEM;
+
 	ret = devm_clk_bulk_get(priv->dev, top->top_clks_num, top->top_clks);
 	if (ret)
 		return dev_err_probe(priv->dev, ret, "failed to get top clocks\n");
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#7 lore raw

PATCH 5/10 clk: rockchip: cpu: Allocate rate_table before registering notifier

In reply to Lee Jones

rockchip_clk_register_cpuclk() registers cpuclk->clk_nb before allocating cpuclk->rate_table, and on clk_register() failure frees cpuclk->rate_table before unregistering the notifier. A concurrent parent rate change can therefore observe a NULL rate_table while rate_count is already non-zero, or dereference the freed rate_table before clk_notifier_unregister() completes.

Allocate and populate cpuclk->rate_table before calling clk_notifier_register(), and reverse the error-path teardown so the notifier is unregistered before cpuclk->rate_table is freed.

Fixes f6fba5f6967d ("clk: rockchip: add new clock-type for the cpuclk")
---
 drivers/clk/rockchip/clk-cpu.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/clk/rockchip/clk-cpu.c +13 -13
index eb6b8b878e4d..27d5b6f6987d 100644
@@ -360,36 +360,36 @@ struct clk *rockchip_clk_register_cpuclk(const char *name,
 		goto free_alt_parent;
 	}
 
-	ret = clk_notifier_register(clk, &cpuclk->clk_nb);
-	if (ret) {
-		pr_err("%s: failed to register clock notifier for %s\n",
-				__func__, name);
-		goto free_alt_parent;
-	}
-
 	if (nrates > 0) {
-		cpuclk->rate_count = nrates;
 		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
 						   GFP_KERNEL);
 		if (!cpuclk->rate_table) {
 			ret = -ENOMEM;
-			goto unregister_notifier;
+			goto free_alt_parent;
 		}
+		cpuclk->rate_count = nrates;
+	}
+
+	ret = clk_notifier_register(clk, &cpuclk->clk_nb);
+	if (ret) {
+		pr_err("%s: failed to register clock notifier for %s\n",
+		       __func__, name);
+		goto free_rate_table;
 	}
 
 	cclk = clk_register(NULL, &cpuclk->hw);
 	if (IS_ERR(cclk)) {
-		pr_err("%s: could not register cpuclk %s\n", __func__,	name);
+		pr_err("%s: could not register cpuclk %s\n", __func__, name);
 		ret = PTR_ERR(cclk);
-		goto free_rate_table;
+		goto unregister_notifier;
 	}
 
 	return cclk;
 
-free_rate_table:
-	kfree(cpuclk->rate_table);
 unregister_notifier:
 	clk_notifier_unregister(clk, &cpuclk->clk_nb);
+free_rate_table:
+	kfree(cpuclk->rate_table);
 free_alt_parent:
 	clk_disable_unprepare(cpuclk->alt_parent);
 free_cpuclk:
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#9 lore raw

PATCH 6/10 clk: tegra: bpmp: Skip ERR_PTR entries in tegra_bpmp_clk_of_xlate()

In reply to Lee Jones

tegra_bpmp_register_clocks_one() intentionally stores ERR_PTR values in bpmp->clocks[i] when registering a clock fails. However, tegra_bpmp_clk_of_xlate() only checks for NULL before dereferencing clk->id, crashing on any failed clock slot.

Use IS_ERR_OR_NULL() when skipping unpopulated clock entries.

Fixes 2db12b15c6f3 ("clk: tegra: Register clocks from root to leaf")
---
 drivers/clk/tegra/clk-bpmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/tegra/clk-bpmp.c +1 -1
index e4b4f3eec777..7cbdd7797623 100644
@@ -657,7 +657,7 @@ static struct clk_hw *tegra_bpmp_clk_of_xlate(struct of_phandle_args *clkspec,
 	for (i = 0; i < bpmp->num_clocks; i++) {
 		struct tegra_bpmp_clk *clk = bpmp->clocks[i];
 
-		if (!clk)
+		if (IS_ERR_OR_NULL(clk))
 			continue;
 
 		if (clk->id == id)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#10 lore raw

PATCH 7/10 clk: rp1: Propagate determine_rate() return value for PLL dividers

In reply to Lee Jones

rp1_pll_divider_determine_rate() assigns the return value of clk_divider_ops.determine_rate() to req->rate and always returns 0. Because determine_rate() updates req->rate in place and returns 0 on success or a negative error code on failure, every call overwrites req->rate with zero or a cast errno and discards errors.

Return the result of clk_divider_ops.determine_rate() directly.

Fixes 41c9570e4178 ("clk: rp1: convert from round_rate() to determine_rate()")
---
 drivers/clk/clk-rp1.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/clk/clk-rp1.c +1 -3
index 1dd276dd2bb3..cfa7cbefad2e 100644
@@ -770,9 +770,7 @@ static unsigned long rp1_pll_divider_recalc_rate(struct clk_hw *hw,
 static int rp1_pll_divider_determine_rate(struct clk_hw *hw,
 					  struct clk_rate_request *req)
 {
-	req->rate = clk_divider_ops.determine_rate(hw, req);
-
-	return 0;
+	return clk_divider_ops.determine_rate(hw, req);
 }
 
 static int rp1_clock_is_on(struct clk_hw *hw)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#11 lore raw

PATCH 8/10 clk: rp1: Reference secondary PLL divider clk_hw in parent tables

In reply to Lee Jones

rp1_register_pll_divider() registers &desc->div.hw rather than &desc->hw, leaving desc->hw.core NULL for the secondary PLL dividers pll_sys_sec_desc, pll_video_sec_desc, and pll_audio_sec_desc. Fourteen clock parent table entries still reference &pll_*_sec_desc.hw instead of &pll_*_sec_desc.div.hw, causing parent resolution to fail for those clocks.

Point all fourteen parent entries at .div.hw, matching clk_eth_parents.

Fixes a7713a7dd159 ("clk: rp1: Implement remaining clock tree")
---
 drivers/clk/clk-rp1.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/clk/clk-rp1.c +14 -14
index cfa7cbefad2e..d5d94d699829 100644
@@ -1419,7 +1419,7 @@ static struct rp1_clk_desc pll_video_sec_desc = REGISTER_PLL_DIV(
 
 static const struct clk_parent_data clk_eth_tsu_parents[] = {
 	{ .index = 0 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
@@ -1450,7 +1450,7 @@ static struct rp1_clk_desc clk_eth_tsu_desc = REGISTER_CLK(
 static const struct clk_parent_data clk_eth_parents[] = {
 	{ .hw = &pll_sys_sec_desc.div.hw },
 	{ .hw = &pll_sys_desc.hw },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 };
 
 static struct rp1_clk_desc clk_eth_desc = REGISTER_CLK(
@@ -1651,7 +1651,7 @@ static struct rp1_clk_desc clk_uart_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_pwm0_parents[] = {
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = 0 },
 };
 
@@ -1677,7 +1677,7 @@ static struct rp1_clk_desc clk_pwm0_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_pwm1_parents[] = {
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = 0 },
 };
 
@@ -1705,7 +1705,7 @@ static const struct clk_parent_data clk_audio_in_parents[] = {
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = 0 },
 };
 
@@ -1731,7 +1731,7 @@ static struct rp1_clk_desc clk_audio_in_desc = REGISTER_CLK(
 static const struct clk_parent_data clk_audio_out_parents[] = {
 	{ .index = -1 },
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = 0 },
 };
 
@@ -1757,7 +1757,7 @@ static struct rp1_clk_desc clk_audio_out_desc = REGISTER_CLK(
 static const struct clk_parent_data clk_i2s_parents[] = {
 	{ .index = 0 },
 	{ .hw = &pll_audio_desc.hw },
-	{ .hw = &pll_audio_sec_desc.hw },
+	{ .hw = &pll_audio_sec_desc.div.hw },
 };
 
 static struct rp1_clk_desc clk_i2s_desc = REGISTER_CLK(
@@ -1879,7 +1879,7 @@ static struct rp1_clk_desc clk_sdio_alt_src_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_dpi_parents[] = {
 	{ .hw = &pll_sys_desc.hw },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .hw = &pll_video_desc.hw },
 	{ .index = -1 },
 	{ .index = -1 },
@@ -2015,7 +2015,7 @@ static struct rp1_clk_desc clksrc_mipi1_dsi_byteclk_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_mipi0_dpi_parents[] = {
 	{ .hw = &pll_sys_desc.hw },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .hw = &pll_video_desc.hw },
 	{ .hw = &clksrc_mipi0_dsi_byteclk_desc.hw },
 	{ .index = -1 },
@@ -2046,7 +2046,7 @@ static struct rp1_clk_desc clk_mipi0_dpi_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_mipi1_dpi_parents[] = {
 	{ .hw = &pll_sys_desc.hw },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .hw = &pll_video_desc.hw },
 	{ .hw = &clksrc_mipi1_dsi_byteclk_desc.hw },
 	{ .index = -1 },
@@ -2082,7 +2082,7 @@ static const struct clk_parent_data clk_gp2_parents[] = {
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
-	{ .hw = &pll_sys_sec_desc.hw },
+	{ .hw = &pll_sys_sec_desc.div.hw },
 	{ .index = -1 },
 	{ .hw = &pll_video_desc.hw },
 	{ .hw = &clk_audio_in_desc.hw },
@@ -2163,7 +2163,7 @@ static const struct clk_parent_data clk_gp4_parents[] = {
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
@@ -2197,7 +2197,7 @@ static struct rp1_clk_desc clk_gp4_desc = REGISTER_CLK(
 
 static const struct clk_parent_data clk_vec_parents[] = {
 	{ .hw = &pll_sys_pri_ph_desc.hw },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .hw = &pll_video_desc.hw },
 	{ .index = -1 },
 	{ .index = -1 },
@@ -2233,7 +2233,7 @@ static const struct clk_parent_data clk_gp5_parents[] = {
 	{ .index = -1 },
 	{ .index = -1 },
 	{ .index = -1 },
-	{ .hw = &pll_video_sec_desc.hw },
+	{ .hw = &pll_video_sec_desc.div.hw },
 	{ .hw = &clk_eth_tsu_desc.hw },
 	{ .index = -1 },
 	{ .hw = &clk_vec_desc.hw },
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#12 lore raw

PATCH 9/10 clk: keystone: sci-clk: Check allocation failure before copying clock array

In reply to Lee Jones

ti_sci_scan_clocks_from_fw() calls devm_kmalloc_objs() without checking for NULL before passing tmp_clks into memcpy(), and invokes memcpy() with a NULL source pointer on the initial iteration when max_clks is zero.

Return -ENOMEM if devm_kmalloc_objs() fails, and only copy and free the previous array when max_clks is non-zero.

Fixes 3c13933c6033 ("clk: keystone: sci-clk: add support for dynamically probing clocks")
---
 drivers/clk/keystone/sci-clk.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/clk/keystone/sci-clk.c +6 -2
index 967f77900358..4cb1299cd49e 100644
@@ -495,9 +495,13 @@ static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
 		if (num_clks == max_clks) {
 			tmp_clks = devm_kmalloc_objs(dev, sci_clk,
 						     max_clks + 64);
-			memcpy(tmp_clks, clks, max_clks * sizeof(sci_clk));
-			if (max_clks)
+			if (!tmp_clks)
+				return -ENOMEM;
+			if (max_clks) {
+				memcpy(tmp_clks, clks,
+				       max_clks * sizeof(sci_clk));
 				devm_kfree(dev, clks);
+			}
 			max_clks += 64;
 			clks = tmp_clks;
 		}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#13 lore raw

PATCH 10/10 clk: versatile: icst: Check kasprintf() and put parent node on error

In reply to Lee Jones

of_syscon_icst_setup() does not check the return value of kasprintf() before passing name into icst_clk_setup(), causing registration to fail with -ENOMEM after redundant allocations and logging a NULL name while leaking the parent device node reference. The IS_ERR(regclk) error path also omits of_node_put().

Release the parent node and return early when kasprintf() fails, and release the parent node when icst_clk_setup() returns an error.

Fixes 1b2189f3aa50 ("clk: versatile: clk-icst: Ensure clock names are unique")
Fixes d430819d69a5 ("clk: versatile-icst: add device tree support")
---
 drivers/clk/versatile/clk-icst.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/clk/versatile/clk-icst.c +5 -0
index e9c7353aa5d5..10cb34a3e8ea 100644
@@ -566,11 +566,16 @@ static void __init of_syscon_icst_setup(struct device_node *np)
 	/* Parent clock name is not the same as node parent */
 	parent_name = of_clk_get_parent_name(np, 0);
 	name = kasprintf(GFP_KERNEL, "%pOFP", np);
+	if (!name) {
+		of_node_put(parent);
+		return;
+	}
 
 	regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map, ctype);
 	if (IS_ERR(regclk)) {
 		pr_err("error setting up syscon ICST clock %s\n", name);
 		kfree(name);
+		of_node_put(parent);
 		return;
 	}
 	of_clk_add_provider(np, of_clk_src_simple_get, regclk);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#8 lore raw

Re: [PATCH 05/10] clk: rockchip: cpu: Allocate rate_table before registering notifier

In reply to Lee Jones
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] clk: rockchip: leaked notifier in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] clk: rockchip: leaked notifier in rockchip_clk_register_cpuclk_multi_pll() [1]
--

commit 87431f3421888170ed6b1c77970113743924cab9
Author: Lee Jones <[email protected]>

clk: rockchip: cpu: Allocate rate_table before registering notifier

This commit allocates and populates cpuclk->rate_table before calling clk_notifier_register() in rockchip_clk_register_cpuclk(), and reverses the error teardown order so the notifier is unregistered before rate_table is freed.

Quoted 44 lines
drivers/clk/rockchip/clk-cpu.c +11 -11
index eb6b8b878e4db..27d5b6f6987db 100644
@@ -360,36 +360,36 @@ struct clk *rockchip_clk_register_cpuclk(const char *name,
 		goto free_alt_parent;
 	}
 
-	ret = clk_notifier_register(clk, &cpuclk->clk_nb);
-	if (ret) {
-		pr_err("%s: failed to register clock notifier for %s\n",
-				__func__, name);
-		goto free_alt_parent;
-	}
-
 	if (nrates > 0) {
-		cpuclk->rate_count = nrates;
 		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
 						   GFP_KERNEL);
 		if (!cpuclk->rate_table) {
 			ret = -ENOMEM;
-			goto unregister_notifier;
+			goto free_alt_parent;
 		}
+		cpuclk->rate_count = nrates;
+	}
+
+	ret = clk_notifier_register(clk, &cpuclk->clk_nb);
+	if (ret) {
+		pr_err("%s: failed to register clock notifier for %s\n",
+		       __func__, name);
+		goto free_rate_table;
 	}
 
 	cclk = clk_register(NULL, &cpuclk->hw);
 	if (IS_ERR(cclk)) {
-		pr_err("%s: could not register cpuclk %s\n", __func__,	name);
+		pr_err("%s: could not register cpuclk %s\n", __func__, name);
 		ret = PTR_ERR(cclk);
-		goto free_rate_table;
+		goto unregister_notifier;
 	}
 
 	return cclk;

drivers/clk/rockchip/clk-cpu.c struct clk *rockchip_clk_register_cpuclk(const char *name, · line 395

Quoted 10 lines
drivers/clk/rockchip/clk-cpu.c +2 -2
 
-free_rate_table:
-	kfree(cpuclk->rate_table);
 unregister_notifier:
 	clk_notifier_unregister(clk, &cpuclk->clk_nb);
+free_rate_table:
+	kfree(cpuclk->rate_table);
 free_alt_parent:
 	clk_disable_unprepare(cpuclk->alt_parent);
 free_cpuclk:

[Severity: High]
This problem wasn't introduced by this patch, but does rockchip_clk_register_cpuclk_multi_pll() in the same file also register cpuclk->clk_nb before allocating cpuclk->rate_table, and leak the notifier
[1] if kmemdup() fails?

drivers/clk/rockchip/clk-cpu.c:rockchip_clk_register_cpuclk_multi_pll() {
    ...
	ret = clk_notifier_register(hw->clk, &cpuclk->clk_nb);
	if (ret) {
		pr_err("%s: failed to register clock notifier for %s\n",
		       __func__, name);
		goto free_cpuclk;
	}

	if (nrates > 0) {
		cpuclk->rate_count = nrates;
		cpuclk->rate_table = kmemdup(rates,
					     sizeof(*rates) * nrates,
					     GFP_KERNEL);
		if (!cpuclk->rate_table) {
			ret = -ENOMEM;
			goto free_cpuclk;
		}
	}

	return hw->clk;

free_cpuclk:

	kfree(cpuclk);
    ...
}

Jumping directly to free_cpuclk without calling clk_notifier_unregister() leaves the freed cpuclk->clk_nb linked in clk_notifier_list, which can cause a use-after-free or list corruption when the notifier chain is later traversed or modified.

[1] https://sashiko.dev/#/bug/linux-1bafb3ff-a5b6-4219-9489-1455c756f137

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

Re: [PATCH 01/10] clk: ti: fapll: Fix output clock array allocation and bounds check

In reply to Lee Jones
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] clk: ti: fapll: missing lower-bound check for index 0 in ti_fapll_setup() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] clk: ti: fapll: missing lower-bound check for index 0 in ti_fapll_setup()
--

commit a8867d96413dfd436f2d0dca33e4c637a2d0da77
Author: Lee Jones <[email protected]>

clk: ti: fapll: Fix output clock array allocation and bounds check

This commit allocates MAX_FAPLL_OUTPUTS + 1 pointer slots for fd->outputs.clks and bounds-checks output_instance against MAX_FAPLL_OUTPUTS.

Quoted 4 lines
drivers/clk/ti/fapll.c +0 -0
index 7d6a4dd4fc8ce..d5e95e6489cda 100644

[ ... ]

drivers/clk/ti/fapll.c static void __init ti_fapll_setup(struct device_node *node) · line 634

Quoted 12 lines
drivers/clk/ti/fapll.c +6 -0
@@ -625,6 +624,12 @@ static void __init ti_fapll_setup(struct device_node *node)
 					       &output_instance))
 			output_instance = i;
 
+		if (output_instance > MAX_FAPLL_OUTPUTS) {
+			pr_err("%pOFn invalid output_instance %u\n",
+			       node, output_instance);
+			continue;
+		}
+
 		freq = fd->base + (output_instance * 8);
 		div = freq + 4;

[Severity: Medium]
Should this check also reject output_instance == 0?

Child synthesizers are 1-based, while slot 0 of fd->outputs.clks is populated with the parent pll_clk before this loop and register offsets 0 and 4 are the main PLL control and powerdown registers. If clock-indices contains 0 or of_property_read_u32_index() fails on i == 0 and falls back to output_instance = i, synth_clk aliases the main PLL registers and overwrites fd->outputs.clks[0].

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

Re: [PATCH 02/10] clk: tegra: bpmp: Drop manual clock unregister on provider failure

In reply to Lee Jones
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] clk: tegra: error pointer dereference…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] clk: tegra: error pointer dereference in tegra_bpmp_clk_of_xlate() [1]
--

commit 44882aa0bdd34b806a054776ef80a0aa5e952e08
Author: Lee Jones <[email protected]>

clk: tegra: bpmp: Drop manual clock unregister on provider failure

This commit removes tegra_bpmp_unregister_clocks() and its call on of_clk_add_hw_provider() failure so devres handles clock cleanup without dereferencing error pointers or double-unregistering clocks.

Quoted 9 lines
drivers/clk/tegra/clk-bpmp.c +0 -1
index 3649324e2e1fd..e4b4f3eec7773 100644
@@ -648,14 +648,6 @@ static int tegra_bpmp_register_clocks(struct tegra_bpmp *bpmp,
 	return 0;
 }
 
-static void tegra_bpmp_unregister_clocks(struct tegra_bpmp *bpmp)

drivers/clk/tegra/clk-bpmp.c static int tegra_bpmp_register_clocks(struct tegra_bpmp *bpmp, · line 653

Quoted 10 lines
drivers/clk/tegra/clk-bpmp.c +0 -7
-{
-	unsigned int i;
-
-	for (i = 0; i < bpmp->num_clocks; i++)
-		clk_hw_unregister(&bpmp->clocks[i]->hw);
-}
-
 static struct clk_hw *tegra_bpmp_clk_of_xlate(struct of_phandle_args *clkspec,
 					      void *data)
 {

[Severity: High]
This problem wasn't introduced by this patch, but can tegra_bpmp_clk_of_xlate() also dereference an error pointer in bpmp->clocks[i] [1]?

drivers/clk/tegra/clk-bpmp.c:tegra_bpmp_clk_of_xlate() {
    ...
	for (i = 0; i < bpmp->num_clocks; i++) {
		struct tegra_bpmp_clk *clk = bpmp->clocks[i];

		if (!clk)
			continue;

		if (clk->id == id)
			return &clk->hw;
	}
    ...
}

Because tegra_bpmp_register_clocks_one() stores ERR_PTR values in bpmp->clocks[i] when a clock fails to register, checking only !clk before reading clk->id will crash during device tree clock lookup. Should this check IS_ERR_OR_NULL(clk)?

[1] https://sashiko.dev/#/bug/linux-3d54138a-c3d2-4466-952a-f7e44259adfe

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