public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()
@ 2024-10-22 20:12 Gax-c
  2024-10-23  0:22 ` Chen Wang
  2024-10-23  7:41 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Gax-c @ 2024-10-22 20:12 UTC (permalink / raw)
  To: mturquette, sboyd, unicorn_wang, inochiama, nathan, dan.carpenter
  Cc: linux-clk, zzjas98, chenyuan0y, Zichen Xie

From: Zichen Xie <zichenxie0106@gmail.com>

This was found by a static analyzer.
There may be a potential integer overflow issue in
sg2042_pll_recalc_rate(). numerator is defined as u64 while
parent_rate is defined as unsigned long and ctrl_table.fbdiv
is defined as unsigned int. On 32-bit machine, the result of
the calculation will be limited to "u32" without correct casting.
Integer overflow may occur on high-performance systems.
We recommended that we cast the denominator as well but
Dan Carpenter said that it was a max of 3087 and was not even
vaguely close to the 4 billion mark needed to overflow a u32.
So, we only cast the numerator here.

Fixes: 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>
---
v2: modified patch to numerator casting only.
v3: modified wrapping to make it clear.
---
 drivers/clk/sophgo/clk-sg2042-pll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/sophgo/clk-sg2042-pll.c b/drivers/clk/sophgo/clk-sg2042-pll.c
index ff9deeef509b..1537f4f05860 100644
--- a/drivers/clk/sophgo/clk-sg2042-pll.c
+++ b/drivers/clk/sophgo/clk-sg2042-pll.c
@@ -153,7 +153,7 @@ static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value,
 
 	sg2042_pll_ctrl_decode(reg_value, &ctrl_table);
 
-	numerator = parent_rate * ctrl_table.fbdiv;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;
 	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
 	do_div(numerator, denominator);
 	return numerator;
-- 
2.34.1


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

* Re: [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()
  2024-10-22 20:12 [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate() Gax-c
@ 2024-10-23  0:22 ` Chen Wang
  2024-10-23  7:41 ` Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Chen Wang @ 2024-10-23  0:22 UTC (permalink / raw)
  To: Gax-c, mturquette, sboyd, inochiama, nathan, dan.carpenter
  Cc: linux-clk, zzjas98, chenyuan0y


On 2024/10/23 4:12, Gax-c wrote:
> From: Zichen Xie <zichenxie0106@gmail.com>
>
> This was found by a static analyzer.
> There may be a potential integer overflow issue in
> sg2042_pll_recalc_rate(). numerator is defined as u64 while
> parent_rate is defined as unsigned long and ctrl_table.fbdiv
> is defined as unsigned int. On 32-bit machine, the result of
> the calculation will be limited to "u32" without correct casting.
> Integer overflow may occur on high-performance systems.
> We recommended that we cast the denominator as well but
> Dan Carpenter said that it was a max of 3087 and was not even
> vaguely close to the 4 billion mark needed to overflow a u32.
> So, we only cast the numerator here.
>
> Fixes: 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
> Signed-off-by: Zichen Xie <zichenxie0106@gmail.com>

Reviewed-by: Chen Wang <unicorn_wang@outlook.com>

Thanks.

> ---
> v2: modified patch to numerator casting only.
> v3: modified wrapping to make it clear.
> ---
>   drivers/clk/sophgo/clk-sg2042-pll.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/clk/sophgo/clk-sg2042-pll.c b/drivers/clk/sophgo/clk-sg2042-pll.c
> index ff9deeef509b..1537f4f05860 100644
> --- a/drivers/clk/sophgo/clk-sg2042-pll.c
> +++ b/drivers/clk/sophgo/clk-sg2042-pll.c
> @@ -153,7 +153,7 @@ static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value,
>   
>   	sg2042_pll_ctrl_decode(reg_value, &ctrl_table);
>   
> -	numerator = parent_rate * ctrl_table.fbdiv;
> +	numerator = (u64)parent_rate * ctrl_table.fbdiv;
>   	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
>   	do_div(numerator, denominator);
>   	return numerator;

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

* Re: [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()
  2024-10-22 20:12 [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate() Gax-c
  2024-10-23  0:22 ` Chen Wang
@ 2024-10-23  7:41 ` Dan Carpenter
  2024-10-23 13:59   ` Zichen Xie
  1 sibling, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2024-10-23  7:41 UTC (permalink / raw)
  To: Gax-c
  Cc: mturquette, sboyd, unicorn_wang, inochiama, nathan, linux-clk,
	zzjas98, chenyuan0y

I'm sorry, I have let you down in some ways.  This subject line is too long.
It's 126 characters long.  Please change it so something like:

clk: sophgo: avoid integer overflow in sg2042_pll_recalc_rate()

On Tue, Oct 22, 2024 at 03:12:45PM -0500, Gax-c wrote:
> From: Zichen Xie <zichenxie0106@gmail.com>
> 
> This was found by a static analyzer.
> There may be a potential integer overflow issue in
> sg2042_pll_recalc_rate(). numerator is defined as u64 while
> parent_rate is defined as unsigned long and ctrl_table.fbdiv
> is defined as unsigned int. On 32-bit machine, the result of
> the calculation will be limited to "u32" without correct casting.
> Integer overflow may occur on high-performance systems.
> We recommended that we cast the denominator as well but
> Dan Carpenter said that it was a max of 3087 and was not even
> vaguely close to the 4 billion mark needed to overflow a u32.
> So, we only cast the numerator here.

On second though, could we just leave this out.  Let's only mention the
numerator.

regards,
dan carpenter


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

* Re: [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()
  2024-10-23  7:41 ` Dan Carpenter
@ 2024-10-23 13:59   ` Zichen Xie
  0 siblings, 0 replies; 4+ messages in thread
From: Zichen Xie @ 2024-10-23 13:59 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: mturquette, sboyd, unicorn_wang, inochiama, nathan, linux-clk,
	zzjas98, chenyuan0y


On 2024/10/23 2:41, Dan Carpenter wrote:
> I'm sorry, I have let you down in some ways.  This subject line is too long.
> It's 126 characters long.  Please change it so something like:
>
> clk: sophgo: avoid integer overflow in sg2042_pll_recalc_rate()
>
> On Tue, Oct 22, 2024 at 03:12:45PM -0500, Gax-c wrote:
>> From: Zichen Xie <zichenxie0106@gmail.com>
>>
>> This was found by a static analyzer.
>> There may be a potential integer overflow issue in
>> sg2042_pll_recalc_rate(). numerator is defined as u64 while
>> parent_rate is defined as unsigned long and ctrl_table.fbdiv
>> is defined as unsigned int. On 32-bit machine, the result of
>> the calculation will be limited to "u32" without correct casting.
>> Integer overflow may occur on high-performance systems.
>> We recommended that we cast the denominator as well but
>> Dan Carpenter said that it was a max of 3087 and was not even
>> vaguely close to the 4 billion mark needed to overflow a u32.
>> So, we only cast the numerator here.
> On second though, could we just leave this out.  Let's only mention the
> numerator.

Fine. I'll do it later.


>
> regards,
> dan carpenter
>

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

end of thread, other threads:[~2024-10-23 13:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-22 20:12 [PATCH v3] clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate() Gax-c
2024-10-23  0:22 ` Chen Wang
2024-10-23  7:41 ` Dan Carpenter
2024-10-23 13:59   ` Zichen Xie

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