Skip to content

Commit 7d033a1

Browse files
2045castorUlrich Hecht
authored andcommitted
hwmon: (w83l786ng) Convert macros to functions to avoid TOCTOU
commit 07272e883fc61574b8367d44de48917f622cdd83 upstream. The macros FAN_FROM_REG and TEMP_FROM_REG evaluate their arguments multiple times. When used in lockless contexts involving shared driver data, this causes Time-of-Check to Time-of-Use (TOCTOU) race conditions. Convert the macros to static functions. This guarantees that arguments are evaluated only once (pass-by-value), preventing the race conditions. Adhere to the principle of minimal changes by only converting macros that evaluate arguments multiple times and are used in lockless contexts. Link: https://lore.kernel.org/all/CALbr=LYJ_ehtp53HXEVkSpYoub+XYSTU8Rg=o1xxMJ8=5z8B-g@mail.gmail.com/ Fixes: 85f03bc ("hwmon: Add support for Winbond W83L786NG/NR") Cc: stable@vger.kernel.org Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com> Link: https://lore.kernel.org/r/20251128123816.3670-1-hanguidong02@gmail.com Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Ulrich Hecht <uli@kernel.org>
1 parent b9b4590 commit 7d033a1

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

drivers/hwmon/w83l786ng.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,25 @@ FAN_TO_REG(long rpm, int div)
9090
return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
9191
}
9292

93-
#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
94-
((val) == 255 ? 0 : \
95-
1350000 / ((val) * (div))))
93+
static int fan_from_reg(int val, int div)
94+
{
95+
if (val == 0)
96+
return -1;
97+
if (val == 255)
98+
return 0;
99+
return 1350000 / (val * div);
100+
}
96101

97102
/* for temp */
98103
#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
99104
: (val)) / 1000, 0, 0xff))
100-
#define TEMP_FROM_REG(val) (((val) & 0x80 ? \
101-
(val) - 0x100 : (val)) * 1000)
105+
106+
static int temp_from_reg(int val)
107+
{
108+
if (val & 0x80)
109+
return (val - 0x100) * 1000;
110+
return val * 1000;
111+
}
102112

103113
/*
104114
* The analog voltage inputs have 8mV LSB. Since the sysfs output is
@@ -294,7 +304,7 @@ static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
294304
int nr = to_sensor_dev_attr(attr)->index; \
295305
struct w83l786ng_data *data = w83l786ng_update_device(dev); \
296306
return sprintf(buf, "%d\n", \
297-
FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
307+
fan_from_reg(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
298308
}
299309

300310
show_fan_reg(fan);
@@ -361,7 +371,7 @@ store_fan_div(struct device *dev, struct device_attribute *attr,
361371

362372
/* Save fan_min */
363373
mutex_lock(&data->update_lock);
364-
min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
374+
min = fan_from_reg(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
365375

366376
data->fan_div[nr] = DIV_TO_REG(val);
367377

@@ -423,7 +433,7 @@ show_temp(struct device *dev, struct device_attribute *attr, char *buf)
423433
int nr = sensor_attr->nr;
424434
int index = sensor_attr->index;
425435
struct w83l786ng_data *data = w83l786ng_update_device(dev);
426-
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
436+
return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr][index]));
427437
}
428438

429439
static ssize_t

0 commit comments

Comments
 (0)