Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation
@ 2026-08-21 23:58 hanzhijian
  2026-08-24 11:30 ` Markus Elfring
  0 siblings, 1 reply; 4+ messages in thread
From: hanzhijian @ 2026-08-21 23:58 UTC (permalink / raw)
  To: Daniel Palmer, Romain Perier, Michael Turquette, Stephen Boyd
  Cc: Brian Masney, linux-arm-kernel, linux-clk, linux-kernel,
	hanzhijian

The hws array of mpll->clk_data is allocated with struct_size() using
ARRAY_SIZE(output_dividers) as the element count, giving it 7 elements.
But the probe function stores the MPLL clock at hws[0] and one
fixed-factor clock for each output divider at hws[i + 1] for i in
[0, ARRAY_SIZE(output_dividers)), writing 8 elements in total.  The
final write to hws[7] is past the end of the allocation.

clk_data->num is also set to NUMOUTPUTS (8), so the clock framework
reads hws[0..7], again accessing hws[7] out of bounds.

Use NUMOUTPUTS as the element count so the allocation matches the
number of clocks actually stored and exposed.

Found by smatch:
  drivers/clk/mstar/clk-msc313-mpll.c:134 msc313_mpll_probe()
  error: buffer overflow 'mpll->clk_data->hws' 7 <= 7

Signed-off-by: hanzhijian <hanzhijian1991@gmail.com>
---
 drivers/clk/mstar/clk-msc313-mpll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/mstar/clk-msc313-mpll.c b/drivers/clk/mstar/clk-msc313-mpll.c
index 61beb4e87..bd45aa0ae 100644
--- a/drivers/clk/mstar/clk-msc313-mpll.c
+++ b/drivers/clk/mstar/clk-msc313-mpll.c
@@ -105,7 +105,7 @@ static int msc313_mpll_probe(struct platform_device *pdev)
 		return PTR_ERR(mpll->loop_div_second);
 
 	mpll->clk_data = devm_kzalloc(dev, struct_size(mpll->clk_data, hws,
-			ARRAY_SIZE(output_dividers)), GFP_KERNEL);
+				NUMOUTPUTS), GFP_KERNEL);
 	if (!mpll->clk_data)
 		return -ENOMEM;
 
-- 
2.43.0



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

* Re: [PATCH] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation
  2026-08-21 23:58 [PATCH] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation hanzhijian
@ 2026-08-24 11:30 ` Markus Elfring
  2026-08-24 14:28   ` [PATCH v2] " hanzhijian
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2026-08-24 11:30 UTC (permalink / raw)
  To: hanzhijian1991, linux-clk, linux-arm-kernel, Daniel Palmer,
	Michael Turquette, Romain Perier, Stephen Boyd
  Cc: LKML, Brian Masney

…
> Use NUMOUTPUTS as the element count so the allocation matches the
> number of clocks actually stored and exposed.
> 
> Found by smatch:
>   drivers/clk/mstar/clk-msc313-mpll.c:134 msc313_mpll_probe()
>   error: buffer overflow 'mpll->clk_data->hws' 7 <= 7

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?

See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2#n34


Regards,
Markus


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

* [PATCH v2] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation
  2026-08-24 11:30 ` Markus Elfring
@ 2026-08-24 14:28   ` hanzhijian
  2026-08-24 16:00     ` Brian Masney
  0 siblings, 1 reply; 4+ messages in thread
From: hanzhijian @ 2026-08-24 14:28 UTC (permalink / raw)
  To: Daniel Palmer, Romain Perier, Michael Turquette, Stephen Boyd
  Cc: Brian Masney, linux-arm-kernel, linux-clk, linux-kernel,
	hanzhijian, stable

The hws array of mpll->clk_data is allocated with struct_size() using
ARRAY_SIZE(output_dividers) as the element count, giving it 7 elements.
But the probe function stores the MPLL clock at hws[0] and one
fixed-factor clock for each output divider at hws[i + 1] for i in
[0, ARRAY_SIZE(output_dividers)), writing 8 elements in total.  The
final write to hws[7] is past the end of the allocation.

clk_data->num is also set to NUMOUTPUTS (8), so the clock framework
reads hws[0..7], again accessing hws[7] out of bounds.

Use NUMOUTPUTS as the element count so the allocation matches the
number of clocks actually stored and exposed.

Found by smatch:
  drivers/clk/mstar/clk-msc313-mpll.c:134 msc313_mpll_probe()
  error: buffer overflow 'mpll->clk_data->hws' 7 <= 7

Fixes: bef7a78da716 ("clk: mstar: MStar/SigmaStar MPLL driver")
Cc: stable@vger.kernel.org
Signed-off-by: hanzhijian <hanzhijian1991@gmail.com>
---
v2: add Fixes: and Cc: stable tags

 drivers/clk/mstar/clk-msc313-mpll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/mstar/clk-msc313-mpll.c b/drivers/clk/mstar/clk-msc313-mpll.c
index 61beb4e87..bd45aa0ae 100644
--- a/drivers/clk/mstar/clk-msc313-mpll.c
+++ b/drivers/clk/mstar/clk-msc313-mpll.c
@@ -105,7 +105,7 @@ static int msc313_mpll_probe(struct platform_device *pdev)
 		return PTR_ERR(mpll->loop_div_second);
 
 	mpll->clk_data = devm_kzalloc(dev, struct_size(mpll->clk_data, hws,
-			ARRAY_SIZE(output_dividers)), GFP_KERNEL);
+				NUMOUTPUTS), GFP_KERNEL);
 	if (!mpll->clk_data)
 		return -ENOMEM;
 
-- 
2.43.0



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

* Re: [PATCH v2] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation
  2026-08-24 14:28   ` [PATCH v2] " hanzhijian
@ 2026-08-24 16:00     ` Brian Masney
  0 siblings, 0 replies; 4+ messages in thread
From: Brian Masney @ 2026-08-24 16:00 UTC (permalink / raw)
  To: hanzhijian
  Cc: Daniel Palmer, Romain Perier, Michael Turquette, Stephen Boyd,
	linux-arm-kernel, linux-clk, linux-kernel, stable

On Mon, Aug 24, 2026 at 10:28:00PM +0800, hanzhijian wrote:
> The hws array of mpll->clk_data is allocated with struct_size() using
> ARRAY_SIZE(output_dividers) as the element count, giving it 7 elements.
> But the probe function stores the MPLL clock at hws[0] and one
> fixed-factor clock for each output divider at hws[i + 1] for i in
> [0, ARRAY_SIZE(output_dividers)), writing 8 elements in total.  The
> final write to hws[7] is past the end of the allocation.
> 
> clk_data->num is also set to NUMOUTPUTS (8), so the clock framework
> reads hws[0..7], again accessing hws[7] out of bounds.
> 
> Use NUMOUTPUTS as the element count so the allocation matches the
> number of clocks actually stored and exposed.
> 
> Found by smatch:
>   drivers/clk/mstar/clk-msc313-mpll.c:134 msc313_mpll_probe()
>   error: buffer overflow 'mpll->clk_data->hws' 7 <= 7
> 
> Fixes: bef7a78da716 ("clk: mstar: MStar/SigmaStar MPLL driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: hanzhijian <hanzhijian1991@gmail.com>

Reviewed-by: Brian Masney <bmasney@redhat.com>



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

end of thread, other threads:[~2026-08-24 16:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-21 23:58 [PATCH] clk: mstar: msc313-mpll: fix off-by-one in clock array allocation hanzhijian
2026-08-24 11:30 ` Markus Elfring
2026-08-24 14:28   ` [PATCH v2] " hanzhijian
2026-08-24 16:00     ` Brian Masney

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