Skip to contentSkip to conversation

clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()

PATCH Superseded Gax-c 3 participants 5 messages Oct 21, 2024 – Oct 22, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH 1/1 clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()

From: Zichen Xie <[email protected]>

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. For the same reason, adding a cast to denominator could be better. So, we recommend adding an extra cast to prevent potential integer overflow.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
---
 drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sophgo/clk-sg2042-pll.c +2 -2
index ff9deeef509b..e0f92f7a21bd 100644
@@ -153,8 +153,8 @@ 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;
-	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;
+	denominator = (u64)ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
 	do_div(numerator, denominator);
 	return numerator;
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Gax-c

From: Zichen Xie <[email protected]>

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. For the same reason, adding a cast to denominator could be better. So, we recommend adding an extra cast to prevent potential integer overflow.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
---
  drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
index ff9deeef509b..e0f92f7a21bd 100644

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 160

Quoted 10 lines
drivers/clk/sophgo/clk-sg2042-pll.c +2 -2
@@ -153,8 +153,8 @@ 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;
-	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;
+	denominator = (u64)ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
  	do_div(numerator, denominator);
  	return numerator;
  }

LGTM, thanks.

#2 lore raw
In reply to Gax-c
Quoted 24 lines · Gax-c

From: Zichen Xie <[email protected]>

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. For the same reason, adding a cast to denominator could be better. So, we recommend adding an extra cast to prevent potential integer overflow.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
---
 drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
index ff9deeef509b..e0f92f7a21bd 100644

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 154

Quoted 2 lines
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
@@ -153,8 +153,8 @@ static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value,
 
 	sg2042_pll_ctrl_decode(reg_value, &ctrl_table);
                                          ^^^^^^^^^^^

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 156

Quoted 4 lines
drivers/clk/sophgo/clk-sg2042-pll.c +1 -2
-	numerator = parent_rate * ctrl_table.fbdiv;
-	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;

This seems reasonable.

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 157

Quoted 1 line
drivers/clk/sophgo/clk-sg2042-pll.c +1 -0
+	denominator = (u64)ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;

These values from sg2042_pll_ctrl_decode() and there is no way they can overflow. The highest they can be is 63 * 7 * 7 = 3087.

regards,
dan carpenter

Quoted 5 lines
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
 	do_div(numerator, denominator);
 	return numerator;
 }
Signature · 1 line
2.34.1
author
#3 lore raw
Quoted 29 lines · Dan Carpenter
Quoted 24 lines · Gax-c

From: Zichen Xie <[email protected]>

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. For the same reason, adding a cast to denominator could be better. So, we recommend adding an extra cast to prevent potential integer overflow.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
---
  drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
index ff9deeef509b..e0f92f7a21bd 100644

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 154

Quoted 2 lines
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
@@ -153,8 +153,8 @@ static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value,
 
  	sg2042_pll_ctrl_decode(reg_value, &ctrl_table);
                                           ^^^^^^^^^^^
Quoted 1 line
Quoted 8 lines

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 156

Quoted 3 lines
drivers/clk/sophgo/clk-sg2042-pll.c +1 -2
-	numerator = parent_rate * ctrl_table.fbdiv;
-	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;

This seems reasonable.

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 157

Quoted 1 line
drivers/clk/sophgo/clk-sg2042-pll.c +1 -0
+	denominator = (u64)ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;

These values from sg2042_pll_ctrl_decode() and there is no way they can overflow. The highest they can be is 63 * 7 * 7 = 3087.

You are right. But I think it could be better to add this cast to demonstrate that developers have realized the potential of integer overflow.

And it can also prevent some static analyzers from reporting such bugs.

Anyway, I can still provide a patch with "numerator" cast only if it's better.

Best,

Zichen

Quoted 9 lines

regards,
dan carpenter

Quoted 5 lines

do_div(numerator, denominator);
return numerator;
}

Signature · 1 line
2.34.1
#4 lore raw
Quoted 38 lines · Zichen Xie
Quoted 27 lines · Dan Carpenter
Quoted 24 lines · Gax-c

From: Zichen Xie <[email protected]>

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. For the same reason, adding a cast to denominator could be better. So, we recommend adding an extra cast to prevent potential integer overflow.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")
---
  drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
index ff9deeef509b..e0f92f7a21bd 100644

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 153

Quoted 1 line
drivers/clk/sophgo/clk-sg2042-pll.c +0 -0
@@ -153,8 +153,8 @@ static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value,
  	sg2042_pll_ctrl_decode(reg_value, &ctrl_table);
                                           ^^^^^^^^^^^
Quoted 8 lines

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 156

Quoted 3 lines
drivers/clk/sophgo/clk-sg2042-pll.c +1 -2
-	numerator = parent_rate * ctrl_table.fbdiv;
-	denominator = ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;
+	numerator = (u64)parent_rate * ctrl_table.fbdiv;

This seems reasonable.

drivers/clk/sophgo/clk-sg2042-pll.c static unsigned long sg2042_pll_recalc_rate(unsigned int reg_value, · line 157

Quoted 1 line
drivers/clk/sophgo/clk-sg2042-pll.c +1 -0
+	denominator = (u64)ctrl_table.refdiv * ctrl_table.postdiv1 * ctrl_table.postdiv2;

These values from sg2042_pll_ctrl_decode() and there is no way they can overflow. The highest they can be is 63 * 7 * 7 = 3087.

Quoted 5 lines

You are right. But I think it could be better to add this cast to demonstrate that
developers have realized the potential of integer overflow.

And it can also prevent some static analyzers from reporting such bugs.

Pointless casting is just confusing and harmful. The static checker could be fixed to parse that code correctly.

Quoted 4 lines

Anyway, I can still provide a patch with "numerator" cast only if it's better.

Yes, please.

regards,
dan carpenter