Skip to content

Commit 4fc2f8f

Browse files
2045geminigregkh
authored andcommitted
hwmon: (w83l786ng) Convert macros to functions to avoid TOCTOU
commit 07272e8 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>
1 parent c8cf0c2 commit 4fc2f8f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

drivers/hwmon/w83l786ng.c

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

79-
#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
80-
((val) == 255 ? 0 : \
81-
1350000 / ((val) * (div))))
79+
static int fan_from_reg(int val, int div)
80+
{
81+
if (val == 0)
82+
return -1;
83+
if (val == 255)
84+
return 0;
85+
return 1350000 / (val * div);
86+
}
8287

8388
/* for temp */
8489
#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
8590
: (val)) / 1000, 0, 0xff))
86-
#define TEMP_FROM_REG(val) (((val) & 0x80 ? \
87-
(val) - 0x100 : (val)) * 1000)
91+
92+
static int temp_from_reg(int val)
93+
{
94+
if (val & 0x80)
95+
return (val - 0x100) * 1000;
96+
return val * 1000;
97+
}
8898

8999
/*
90100
* The analog voltage inputs have 8mV LSB. Since the sysfs output is
@@ -280,7 +290,7 @@ static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
280290
int nr = to_sensor_dev_attr(attr)->index; \
281291
struct w83l786ng_data *data = w83l786ng_update_device(dev); \
282292
return sprintf(buf, "%d\n", \
283-
FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
293+
fan_from_reg(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
284294
}
285295

286296
show_fan_reg(fan);
@@ -347,7 +357,7 @@ store_fan_div(struct device *dev, struct device_attribute *attr,
347357

348358
/* Save fan_min */
349359
mutex_lock(&data->update_lock);
350-
min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
360+
min = fan_from_reg(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
351361

352362
data->fan_div[nr] = DIV_TO_REG(val);
353363

@@ -409,7 +419,7 @@ show_temp(struct device *dev, struct device_attribute *attr, char *buf)
409419
int nr = sensor_attr->nr;
410420
int index = sensor_attr->index;
411421
struct w83l786ng_data *data = w83l786ng_update_device(dev);
412-
return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
422+
return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr][index]));
413423
}
414424

415425
static ssize_t

0 commit comments

Comments
 (0)