Skip to contentSkip to conversation

hwmon: Fix underflows seen when writing limit attributes

PATCH New Guenter Roeck 1 participant 5 messages Jul 7, 2024 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

cover 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(-)
author maintainer
#1 lore raw

PATCH 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
index 8ac6e735ec5c..5e805d4ee76a 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/4 hwmon: (lm95234) Fix underflows seen when writing limit attributes

In reply to Guenter Roeck

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/lm95234.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/lm95234.c +5 -4
index 67b9d7636ee4..37e8e9679aeb 100644
@@ -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
author maintainer
#3 lore raw

PATCH 3/4 hwmon: (nct6775-core) Fix underflows seen when writing limit attributes

In reply to Guenter Roeck

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/nct6775-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nct6775-core.c +1 -1
index 9fbab8f02334..934fed3dd586 100644
@@ -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
author maintainer
#4 lore raw

PATCH 4/4 hwmon: (w83627ehf) Fix underflows seen when writing limit attributes

In reply to Guenter Roeck

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/w83627ehf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/w83627ehf.c +2 -2
index fe960c0a624f..7d7d70afde65 100644
@@ -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