Skip to content

Commit fe598ab

Browse files
2045castorgroeck
authored andcommitted
hwmon: (vt8231) Convert macros to functions to avoid TOCTOU
The macro FAN_FROM_REG evaluates its arguments multiple times. When used with shared driver data, this leads to Time-of-Check to Time-of-Use (TOCTOU) race conditions, potentially causing divide-by-zero errors. Convert the macro to a static function to ensure arguments are evaluated only once. Additionally, in fan_div_store, move the reading of the old register value and the calculation of the minimum limit inside the update lock. This ensures that the read-modify-write sequence operates on consistent data, preventing race conditions during fan divider updates. Link: https://lore.kernel.org/all/CALbr=LYJ_ehtp53HXEVkSpYoub+XYSTU8Rg=o1xxMJ8=5z8B-g@mail.gmail.com/ Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com> Link: https://lore.kernel.org/r/20251124165900.4713-1-hanguidong02@gmail.com [groeck: Dropped unnecessary line split] Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 4faaa77 commit fe598ab

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

drivers/hwmon/vt8231.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ static inline u8 FAN_TO_REG(long rpm, int div)
138138
return clamp_val(1310720 / (rpm * div), 1, 255);
139139
}
140140

141-
#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
141+
static int fan_from_reg(int val, int div)
142+
{
143+
if (val == 0)
144+
return 0;
145+
return 1310720 / (val * div);
146+
}
142147

143148
struct vt8231_data {
144149
unsigned short addr;
@@ -561,7 +566,7 @@ static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
561566
struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
562567
int nr = sensor_attr->index;
563568
struct vt8231_data *data = vt8231_update_device(dev);
564-
return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
569+
return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr],
565570
DIV_FROM_REG(data->fan_div[nr])));
566571
}
567572

@@ -571,7 +576,7 @@ static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
571576
struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572577
int nr = sensor_attr->index;
573578
struct vt8231_data *data = vt8231_update_device(dev);
574-
return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
579+
return sprintf(buf, "%d\n", fan_from_reg(data->fan_min[nr],
575580
DIV_FROM_REG(data->fan_div[nr])));
576581
}
577582

@@ -613,16 +618,17 @@ static ssize_t fan_div_store(struct device *dev,
613618
struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
614619
unsigned long val;
615620
int nr = sensor_attr->index;
616-
int old = vt8231_read_value(data, VT8231_REG_FANDIV);
617-
long min = FAN_FROM_REG(data->fan_min[nr],
618-
DIV_FROM_REG(data->fan_div[nr]));
621+
int old;
622+
long min;
619623
int err;
620624

621625
err = kstrtoul(buf, 10, &val);
622626
if (err)
623627
return err;
624628

625629
mutex_lock(&data->update_lock);
630+
old = vt8231_read_value(data, VT8231_REG_FANDIV);
631+
min = fan_from_reg(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
626632
switch (val) {
627633
case 1:
628634
data->fan_div[nr] = 0;

0 commit comments

Comments
 (0)