hwmon: Fix underflows seen when writing limit attributes
Guenter Roeck
maintainercover hwmon: Fix underflows seen when writing limit attributes
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
The problem was fixed using the following a coccinelle script. The python arithmetic is used to fold constant multiplications.
@lc@
expression range, val, err, buf, res;
position p;
expression divisor, l1, l2;
@@
err = \( kstrtol \| kstrtoint \| kstrtos64 \)(buf, range, &val); ... when != val res = clamp_val(DIV_ROUND_CLOSEST@p(val, divisor), l1, l2);
@script:python mult@
l1 << lc.l1;
l2 << lc.l2;
d << lc.divisor;
m1;
m2;
de;
l1e;
l2e;
@@
coccinelle.dn = d.isnumeric()
coccinelle.l1e = l1.replace(' ', '')
coccinelle.l2e = l2.replace(' ', '')
coccinelle.l1n = l1.isnumeric() or (coccinelle.l1e[0] == '-' and coccinelle.l1e[1:].isnumeric()) coccinelle.l2n = l2.isnumeric() or (coccinelle.l2e[0] == '-' and coccinelle.l2e[1:].isnumeric())
if coccinelle.dn and coccinelle.l1n and coccinelle.l2n:
coccinelle.m1 = str(eval(l1) * int(d))
coccinelle.m2 = str(eval(l2) * int(d))
elif coccinelle.dn and coccinelle.l1n:
coccinelle.m1 = str(eval(l1) * int(d))
coccinelle.m2 = l2 + ' * ' + d
elif coccinelle.dn and coccinelle.l2n:
coccinelle.m1 = l1 + ' * ' + d
coccinelle.m2 = str(eval(l2) * int(d))
else:
coccinelle.m1 = l1 + ' * ' + d
coccinelle.m2 = l2 + ' * ' + d@@
identifier mult.m1, mult.m2;
position lc.p;
expression res, val;
expression divisor, l1, l2;
@@
- res = clamp_val(DIV_ROUND_CLOSEST@p(val, divisor), l1, l2);
+ res = DIV_ROUND_CLOSEST(clamp_val(val, m1, m2), divisor);
----------------------------------------------------------------
Guenter Roeck (4):
hwmon: (adc128d818) Fix underflows seen when writing limit attributes
hwmon: (lm95234) Fix underflows seen when writing limit attributes
hwmon: (nct6775-core) Fix underflows seen when writing limit attributes
hwmon: (w83627ehf) Fix underflows seen when writing limit attributes
drivers/hwmon/adc128d818.c | 4 ++-- drivers/hwmon/lm95234.c | 9 +++++---- drivers/hwmon/nct6775-core.c | 2 +- drivers/hwmon/w83627ehf.c | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-)
Guenter Roeck
maintainerPATCH 1/4 hwmon: (adc128d818) Fix underflows seen when writing limit attributes
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
--- drivers/hwmon/adc128d818.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/adc128d818.c +2 -2
@@ -175,7 +175,7 @@ static ssize_t adc128_in_store(struct device *dev,mutex_lock (&data->update_lock);/* 10 mV LSB on limit registers */ - regval =clamp_val (DIV_ROUND_CLOSEST (val,10 ),0 ,255 ); + regval =DIV_ROUND_CLOSEST (clamp_val (val,0 ,2550 ),10 ); data->in[index][nr] = regval <<4 ; reg = index ==1 ?ADC128_REG_IN_MIN (nr) :ADC128_REG_IN_MAX (nr);i2c_smbus_write_byte_data (data->client, reg, regval);
@@ -213,7 +213,7 @@ static ssize_t adc128_temp_store(struct device *dev,return err;mutex_lock (&data->update_lock); - regval =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),-128 ,127 ); + regval =DIV_ROUND_CLOSEST (clamp_val (val,-128000 ,127000 ),1000 ); data->temp[index] = regval <<1 ; i2c_smbus_write_byte_data(data->client, index == 1 ? ADC128_REG_TEMP_MAX
Signature · 1 line
2.39.2
Guenter Roeck
maintainerPATCH 2/4 hwmon: (lm95234) Fix underflows seen when writing limit attributes
In reply to Guenter RoeckDIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
--- drivers/hwmon/lm95234.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/lm95234.c +5 -4
@@ -301,7 +301,8 @@ static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr,if (ret <0 )return ret; - val =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),0 , index ?255 :127 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,0 , (index ?255 :127 ) *1000 ), +1000 );mutex_lock (&data->update_lock); data->tcrit2[index] = val;
@@ -350,7 +351,7 @@ static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,if (ret <0 )return ret; - val =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),0 ,255 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,0 ,255000 ),1000 );mutex_lock (&data->update_lock); data->tcrit1[index] = val;
@@ -391,7 +392,7 @@ static ssize_t tcrit1_hyst_store(struct device *dev,if (ret <0 )return ret; - val =DIV_ROUND_CLOSEST (val,1000 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,-255000 ,255000 ),1000 ); val =clamp_val ((int )data->tcrit1[index] - val,0 ,31 );mutex_lock (&data->update_lock);
@@ -431,7 +432,7 @@ static ssize_t offset_store(struct device *dev, struct device_attribute *attr,return ret;/* Accuracy is 1/2 degrees C */ - val =clamp_val (DIV_ROUND_CLOSEST (val,500 ),-128 ,127 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,-64000 ,63500 ),500 );mutex_lock (&data->update_lock); data->toffset[index] = val;
Signature · 1 line
2.39.2
Guenter Roeck
maintainerPATCH 3/4 hwmon: (nct6775-core) Fix underflows seen when writing limit attributes
In reply to Guenter RoeckDIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
--- drivers/hwmon/nct6775-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nct6775-core.c +1 -1
@@ -2262,7 +2262,7 @@ store_temp_offset(struct device *dev, struct device_attribute *attr,if (err <0 )return err; - val =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),-128 ,127 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,-128000 ,127000 ),1000 );mutex_lock (&data->update_lock); data->temp_offset[nr] = val;
Signature · 1 line
2.39.2
Guenter Roeck
maintainerPATCH 4/4 hwmon: (w83627ehf) Fix underflows seen when writing limit attributes
In reply to Guenter RoeckDIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large negative number such as -9223372036854775808 is provided by the user. Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
--- drivers/hwmon/w83627ehf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/w83627ehf.c +2 -2
@@ -895,7 +895,7 @@ store_target_temp(struct device *dev, struct device_attribute *attr,if (err <0 )return err; - val =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),0 ,127 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,0 ,127000 ),1000 );mutex_lock (&data->update_lock); data->target_temp[nr] = val;
@@ -920,7 +920,7 @@ store_tolerance(struct device *dev, struct device_attribute *attr,return err;/* Limit the temp to 0C - 15C */ - val =clamp_val (DIV_ROUND_CLOSEST (val,1000 ),0 ,15 ); + val =DIV_ROUND_CLOSEST (clamp_val (val,0 ,15000 ),1000 );mutex_lock (&data->update_lock); reg =w83627ehf_read_value (data,W83627EHF_REG_TOLERANCE [nr]);
Signature · 1 line
2.39.2