Skip to content

Commit a7c0aaa

Browse files
Sanman Pradhangroeck
authored andcommitted
hwmon: (isl28022) Fix integer overflow in power calculation on 32-bit
isl28022_read_power() computes: *val = ((51200000L * ((long)data->gain)) / (long)data->shunt) * (long)regval; On 32-bit platforms, 'long' is 32 bits. With gain=8 and shunt=10000 (the default configuration): (51200000 * 8) / 10000 = 40960 40960 * 65535 = 2,684,313,600 This exceeds LONG_MAX (2,147,483,647), resulting in signed integer overflow. Additionally, dividing before multiplying by regval loses precision unnecessarily. Use u64 arithmetic with div_u64() and multiply before dividing to retain precision. The intermediate product cannot overflow u64 (worst case: 51200000 * 8 * 65535 = 26843136000000). Power is inherently non-negative, so unsigned types are the natural fit. Cap the result to LONG_MAX before returning it through the hwmon callback. Fixes: 39671a1 ("hwmon: (isl28022) new driver for ISL28022 power monitor") Cc: stable@vger.kernel.org Signed-off-by: Sanman Pradhan <psanman@juniper.net> Link: https://lore.kernel.org/r/20260410002613.424557-1-sanman.pradhan@hpe.com Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 24c73e9 commit a7c0aaa

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

drivers/hwmon/isl28022.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/err.h>
1010
#include <linux/hwmon.h>
1111
#include <linux/i2c.h>
12+
#include <linux/math64.h>
1213
#include <linux/module.h>
1314
#include <linux/regmap.h>
1415

@@ -185,8 +186,8 @@ static int isl28022_read_power(struct device *dev, u32 attr, long *val)
185186
ISL28022_REG_POWER, &regval);
186187
if (err < 0)
187188
return err;
188-
*val = ((51200000L * ((long)data->gain)) /
189-
(long)data->shunt) * (long)regval;
189+
*val = min(div_u64(51200000ULL * data->gain * regval,
190+
data->shunt), LONG_MAX);
190191
break;
191192
default:
192193
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)