Skip to content

Commit ef901d2

Browse files
2045geminigregkh
authored andcommitted
hwmon: (max16065) Use local variable to avoid TOCTOU
commit b8d5acd upstream. 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: f5bae26 ("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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5b804b8 commit ef901d2

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

drivers/hwmon/max16065.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,13 @@ static ssize_t max16065_current_show(struct device *dev,
216216
struct device_attribute *da, char *buf)
217217
{
218218
struct max16065_data *data = max16065_update_device(dev);
219+
int curr_sense = data->curr_sense;
219220

220-
if (unlikely(data->curr_sense < 0))
221-
return data->curr_sense;
221+
if (unlikely(curr_sense < 0))
222+
return curr_sense;
222223

223224
return sysfs_emit(buf, "%d\n",
224-
ADC_TO_CURR(data->curr_sense, data->curr_gain));
225+
ADC_TO_CURR(curr_sense, data->curr_gain));
225226
}
226227

227228
static ssize_t max16065_limit_store(struct device *dev,

0 commit comments

Comments
 (0)