Skip to contentSkip to conversation

clk: sophgo: avoid integer overflow in sg2042_pll_recalc_rate()

PATCH Applied Gax-c 3 participants 3 messages Oct 23, 2024 – Oct 28, 2024

Subsystems (2)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH v3 1/1 clk: sophgo: avoid integer overflow 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.

Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")

---
v2: Modified patch to numerator casting only.
v3: Shorted subject and wrapping.

---
 drivers/clk/sophgo/clk-sg2042-pll.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/sophgo/clk-sg2042-pll.c +1 -1
index ff9deeef509b..1537f4f05860 100644
@@ -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;
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 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.

Quoted 3 lines
Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")

Thanks so much. Sorry for the extra round of re-writing the commit message.

regards,
dan carpenter

maintainer Applied
#2 lore raw
In reply to Gax-c

Quoting Gax-c (2024-10-23 07:51:47)

Quoted 10 lines

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.

Quoted 4 lines
Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")

---

Applied to clk-next