clk: sophgo: Cast an operand to u64 to prevent potential unsigned long overflow on 32-bit machine in sg2042_pll_recalc_rate()
Subsystems (2)
PATCH v3 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. 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.
---
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(-)
drivers/clk/sophgo/clk-sg2042-pll.c +1 -1
@@ -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
Quoted 14 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
There may be a potential integer overflow issue insg2042_pll_recalc_rate(). numerator is defined as u64 whileparent_rateis 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.
Quoted 2 lines
Fixes 48cf7e01386e ("clk: sophgo: Add SG2042 clock driver")Signed-off-by Zichen Xie <[email protected]>
Thanks.
Quoted 20 lines
---
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(-)drivers/clk/sophgo/clk-sg2042-pll.c +1
-1@@ -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;
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()
Quoted 13 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
There may be a potential integer overflow issue insg2042_pll_recalc_rate(). numerator is defined as u64 whileparent_rateis 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
Quoted 19 lines · Dan Carpenter
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()Quoted 13 lines · Gax-c
From: Zichen Xie <[email protected]>
This was found by a static analyzer.
There may be a potential integer overflow issue insg2042_pll_recalc_rate(). numerator is defined as u64 whileparent_rateis 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.
Quoted 2 lines
On second though, could we just leave this out. Let's only mention the numerator.
Fine. I'll do it later.
Quoted 4 lines
regards,
dan carpenter