hwmon: (fam15h_power) Fix unintentional integer overflow
Guenter Roeck
maintainerPATCH 2/3 hwmon: (fam15h_power) Fix unintentional integer overflow
Expression with two unsigned integer variables is calculated as unsigned integer before it is converted to u64. This may result in an integer overflow. Fix by typecasting the left operand to u64 before performing the left shift.
This patch addresses Coverity #402320: Unintentional integer overflow.
--- drivers/hwmon/fam15h_power.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/fam15h_power.c +2 -1
@@ -67,7 +67,8 @@ static ssize_t show_power(struct device *dev, REG_TDP_LIMIT3, &val); tdp_limit = val >>16 ; - curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; + curr_pwr_watts = ((u64)(tdp_limit + + data->base_tdp)) << running_avg_range; curr_pwr_watts -= running_avg_capture; curr_pwr_watts *= data->tdp_to_watts;
Signature · 7 lines
1.7.9.7 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Hi Guenter,
Adding Andreas to Cc...
Quoted 16 lines · Guenter Roeck
Expression with two unsigned integer variables is calculated as unsigned integer before it is converted to u64. This may result in an integer overflow. Fix by typecasting the left operand to u64 before performing the left shift.
This patch addresses Coverity #402320: Unintentional integer overflow.
Signed-off-by Guenter Roeck <[email protected]>--- drivers/hwmon/fam15h_power.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/hwmon/fam15h_power.c +0
-0
drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 71
Quoted 6 lines
drivers/hwmon/fam15h_power.c +2
-1@@ -67,7 +67,8 @@ static ssize_t show_power(struct device *dev, REG_TDP_LIMIT3, &val); tdp_limit = val >>16 ; - curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; + curr_pwr_watts = ((u64)(tdp_limit + + data->base_tdp)) << running_avg_range;
Even then, "tdp_limit + data->base_tdp" could overflow already, on 32-bit architectures [1]. So I think what you really want is:
curr_pwr_watts = ((u64)tdp_limit + data->base_tdp) << running_avg_range;
[1] I'm not quite sure why one would run a 32-bit Linux on an AMD Family 15h machine, but you never know...
drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 73
Quoted 2 lines
drivers/hwmon/fam15h_power.c +0
-0curr_pwr_watts -= running_avg_capture; curr_pwr_watts *= data->tdp_to_watts;
And BTW, this might be good to push to stable trees, as we've had real problems with overflows in this driver in the past. I don't know the realistic values for tdp_limit, base_tdp and running_avg_range so I'm not sure.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerQuoted 1 line · Jean Delvare
Hi Guenter,
Hi Jean,
Quoted 30 lines
Adding Andreas to Cc...
Quoted 16 lines · Guenter Roeck
Expression with two unsigned integer variables is calculated as unsigned integer before it is converted to u64. This may result in an integer overflow. Fix by typecasting the left operand to u64 before performing the left shift.
This patch addresses Coverity #402320: Unintentional integer overflow.
Signed-off-by Guenter Roeck <[email protected]>--- drivers/hwmon/fam15h_power.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/hwmon/fam15h_power.c +0
-0drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 71
Quoted 6 lines
drivers/hwmon/fam15h_power.c +2
-1@@ -67,7 +67,8 @@ static ssize_t show_power(struct device *dev, REG_TDP_LIMIT3, &val); tdp_limit = val >>16 ; - curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; + curr_pwr_watts = ((u64)(tdp_limit + + data->base_tdp)) << running_avg_range;Even then, "
tdp_limit+data->base_tdp" could overflow already, on 32-bit architectures [1]. So I think what you really want is:
Quoted 2 lines
curr_pwr_watts = ((u64)tdp_limit + data->base_tdp) << running_avg_range;
Both tdp_limit and data->base_tdp can not be larger than 65535, so I figured that the "inner" overflow is not an issue.
Quoted 11 lines
[1] I'm not quite sure why one would run a 32-bit Linux on an AMD Family 15h machine, but you never know...
drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 73
Quoted 2 lines
drivers/hwmon/fam15h_power.c +0
-0curr_pwr_watts -= running_avg_capture; curr_pwr_watts *= data->tdp_to_watts;And BTW, this might be good to push to stable trees, as we've had real problems with overflows in this driver in the past. I don't know the realistic values for
tdp_limit,base_tdpandrunning_avg_rangeso I'm not sure.
Max: (65535 + 65535) << 16 = 8589803520 = 0x1FFFE0000. No idea if this is can happen in the real world.
Thanks,
Guenter
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 13 lines · Guenter Roeck
Quoted 12 lines · Jean Delvare
drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 71
Quoted 7 lines · Guenter Roeck
drivers/hwmon/fam15h_power.c +2
-1@@ -67,7 +67,8 @@ static ssize_t show_power(struct device *dev, REG_TDP_LIMIT3, &val); tdp_limit = val >>16 ; - curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; + curr_pwr_watts = ((u64)(tdp_limit + + data->base_tdp)) << running_avg_range;Even then, "
tdp_limit+data->base_tdp" could overflow already, on 32-bit architectures [1]. So I think what you really want is:
Quoted 4 lines
Quoted 2 lines
curr_pwr_watts = ((u64)tdp_limit + data->base_tdp) << running_avg_range;Both
tdp_limitanddata->base_tdpcan not be larger than 65535, so I figured that the "inner" overflow is not an issue.
Ah, right, I could have easily figured that out by myself, had I taken the time to read the code. Sorry for the noise, and this patch of yours is:
Quoted 7 lines
Quoted 4 lines
And BTW, this might be good to push to stable trees, as we've had real problems with overflows in this driver in the past. I don't know the realistic values for
tdp_limit,base_tdpandrunning_avg_rangeso I'm not sure.Max: (65535 + 65535) << 16 = 8589803520 =
0x1FFFE0000. No idea if this is can happen in the real world.
No idea either. Maybe Andreas can tell. But if we have no reason to believe this can't happen, I'd say we play it safe and push the fix to stable trees.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 30 lines · Jean Delvare
Quoted 13 lines · Guenter Roeck
Quoted 12 lines · Jean Delvare
drivers/hwmon/fam15h_power.c static ssize_t show_power(struct device *dev, · line 71
Quoted 7 lines · Guenter Roeck
drivers/hwmon/fam15h_power.c +2
-1@@ -67,7 +67,8 @@ static ssize_t show_power(struct device *dev, REG_TDP_LIMIT3, &val); tdp_limit = val >>16 ; - curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range; + curr_pwr_watts = ((u64)(tdp_limit + + data->base_tdp)) << running_avg_range;Even then, "
tdp_limit+data->base_tdp" could overflow already, on 32-bit architectures [1]. So I think what you really want is:Quoted 4 lines
Quoted 2 lines
curr_pwr_watts = ((u64)tdp_limit + data->base_tdp) << running_avg_range;Both
tdp_limitanddata->base_tdpcan not be larger than 65535, so I figured that the "inner" overflow is not an issue.Ah, right, I could have easily figured that out by myself, had I taken the time to read the code. Sorry for the noise, and this patch of yours is:
Acked-by Jean Delvare <[email protected]>Quoted 5 lines
Quoted 4 lines
And BTW, this might be good to push to stable trees, as we've had real problems with overflows in this driver in the past. I don't know the realistic values for
tdp_limit,base_tdpandrunning_avg_rangeso I'm not sure.
Quoted 6 lines
Quoted 2 lines
Max: (65535 + 65535) << 16 = 8589803520 =
0x1FFFE0000. No idea if this is can happen in the real world.No idea either. Maybe Andreas can tell. But if we have no reason to believe this can't happen, I'd say we play it safe and push the fix to stable trees.
A typical value for base_tdp is 0x3f which would translate to about 14-15 Watts.
Yes in theory the computation can overflow. But that would mean hardware reports senseless values for base_tdp.
I don't see an issue with current hardware but of course the computation should be fixed. (I don't think that this fix is required for stable trees but feel free to push it there.)
Andreas
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors