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 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.
--- drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
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; }
Signature · 1 line
2.34.1
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 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. 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")Signed-off-by Zichen Xie <[email protected]>--- drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/sophgo/clk-sg2042-pll.c +0
-0
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.
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 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. 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")Signed-off-by Zichen Xie <[email protected]>--- drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/sophgo/clk-sg2042-pll.c +0
-0
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
-0do_div (numerator, denominator);return numerator; }Signature · 1 line
2.34.1
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 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. 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")Signed-off-by Zichen Xie <[email protected]>--- drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/sophgo/clk-sg2042-pll.c +0
-0drivers/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 carpenterQuoted 5 lines
do_div(numerator, denominator);
return numerator;
}Signature · 1 line
2.34.1
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 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. 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")Signed-off-by Zichen Xie <[email protected]>--- drivers/clk/sophgo/clk-sg2042-pll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/sophgo/clk-sg2042-pll.c +0
-0drivers/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