Re: [PATCH] hwmon: (vt8231) Convert macros to functions to avoid TOCTOU
From: Guenter Roeck
Date: Mon Nov 24 2025 - 14:49:24 EST
On Tue, Nov 25, 2025 at 12:59:00AM +0800, Gui-Dong Han wrote:
> 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@xxxxxxxxxxxxxx/
> Signed-off-by: Gui-Dong Han <hanguidong02@xxxxxxxxx>
> ---
Applied.
Thanks,
Guenter
...
> + old = vt8231_read_value(data, VT8231_REG_FANDIV);
> + min = fan_from_reg(data->fan_min[nr],
> + DIV_FROM_REG(data->fan_div[nr]));
That line split was now unnecessary. Dropped while applying.