]> Gentwo Git Trees - linux/.git/commitdiff
hwmon: (max16065) Use local variable to avoid TOCTOU
authorGui-Dong Han <hanguidong02@gmail.com>
Fri, 28 Nov 2025 12:47:09 +0000 (20:47 +0800)
committerGuenter Roeck <linux@roeck-us.net>
Fri, 28 Nov 2025 16:36:25 +0000 (08:36 -0800)
In max16065_current_show, data->curr_sense is read twice: once for the
error check and again for the calculation. Since
i2c_smbus_read_byte_data returns negative error codes on failure, if the
data changes to an error code between the check and the use, ADC_TO_CURR
results in an incorrect calculation.

Read data->curr_sense into a local variable to ensure consistency. Note
that data->curr_gain is constant and safe to access directly.

This aligns max16065_current_show with max16065_input_show, which
already uses a local variable for the same reason.

Link: https://lore.kernel.org/all/CALbr=LYJ_ehtp53HXEVkSpYoub+XYSTU8Rg=o1xxMJ8=5z8B-g@mail.gmail.com/
Fixes: f5bae2642e3d ("hwmon: Driver for MAX16065 System Manager and compatibles")
Cc: stable@vger.kernel.org
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
Link: https://lore.kernel.org/r/20251128124709.3876-1-hanguidong02@gmail.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/max16065.c

index 0ccb5eb596fc40be6e5fa130624c417214ccb0d9..4c9e7892a73c1b7e9d630c42d33bf0348ac63c2a 100644 (file)
@@ -216,12 +216,13 @@ static ssize_t max16065_current_show(struct device *dev,
                                     struct device_attribute *da, char *buf)
 {
        struct max16065_data *data = max16065_update_device(dev);
+       int curr_sense = data->curr_sense;
 
-       if (unlikely(data->curr_sense < 0))
-               return data->curr_sense;
+       if (unlikely(curr_sense < 0))
+               return curr_sense;
 
        return sysfs_emit(buf, "%d\n",
-                         ADC_TO_CURR(data->curr_sense, data->curr_gain));
+                         ADC_TO_CURR(curr_sense, data->curr_gain));
 }
 
 static ssize_t max16065_limit_store(struct device *dev,