* [PATCH] hwmon (tmp401): fix overflow caused by default conversion rate value
@ 2025-12-11 16:43 Alexey Simakov
2025-12-14 17:49 ` Guenter Roeck
0 siblings, 1 reply; 2+ messages in thread
From: Alexey Simakov @ 2025-12-11 16:43 UTC (permalink / raw)
To: Guenter Roeck; +Cc: Alexey Simakov, linux-hwmon, linux-kernel, lvc-project
The driver computes conversion intervals using the formula:
interval = (1 << (7 - rate)) * 125ms
where 'rate' is the sensor's conversion rate register value. According to
the datasheet, the power-on reset value of this register is 0x8, which
could be assigned to the register, after handling i2c general call.
Using this default value causes a result greater than the bit width of
left operand and an undefined behaviour in the calculation above, since
shifting by values larger than the bit width is undefined behaviour as
per C language standard.
Limit the maximum usable 'rate' value to 7 to prevent undefined
behaviour in calculations.
Found by Linux Verification Center (linuxtesting.org) with Svace.
Fixes: ca53e7640de7 ("hwmon: (tmp401) Convert to _info API")
Signed-off-by: Alexey Simakov <bigalex934@gmail.com>
---
drivers/hwmon/tmp401.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/hwmon/tmp401.c b/drivers/hwmon/tmp401.c
index 02c5a3bb1071..84aaf817144c 100644
--- a/drivers/hwmon/tmp401.c
+++ b/drivers/hwmon/tmp401.c
@@ -401,7 +401,7 @@ static int tmp401_chip_read(struct device *dev, u32 attr, int channel, long *val
ret = regmap_read(data->regmap, TMP401_CONVERSION_RATE, ®val);
if (ret < 0)
return ret;
- *val = (1 << (7 - regval)) * 125;
+ *val = (1 << (7 - min(regval, 7))) * 125;
break;
case hwmon_chip_temp_reset_history:
*val = 0;
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] hwmon (tmp401): fix overflow caused by default conversion rate value
2025-12-11 16:43 [PATCH] hwmon (tmp401): fix overflow caused by default conversion rate value Alexey Simakov
@ 2025-12-14 17:49 ` Guenter Roeck
0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2025-12-14 17:49 UTC (permalink / raw)
To: Alexey Simakov; +Cc: linux-hwmon, linux-kernel, lvc-project
On Thu, Dec 11, 2025 at 07:43:43PM +0300, Alexey Simakov wrote:
> The driver computes conversion intervals using the formula:
>
> interval = (1 << (7 - rate)) * 125ms
>
> where 'rate' is the sensor's conversion rate register value. According to
> the datasheet, the power-on reset value of this register is 0x8, which
> could be assigned to the register, after handling i2c general call.
> Using this default value causes a result greater than the bit width of
> left operand and an undefined behaviour in the calculation above, since
> shifting by values larger than the bit width is undefined behaviour as
> per C language standard.
>
> Limit the maximum usable 'rate' value to 7 to prevent undefined
> behaviour in calculations.
>
> Found by Linux Verification Center (linuxtesting.org) with Svace.
>
> Fixes: ca53e7640de7 ("hwmon: (tmp401) Convert to _info API")
> Signed-off-by: Alexey Simakov <bigalex934@gmail.com>
Applied.
It does not matter in practice, though, unless someone overwrites the chip
configuration from outside the driver. The register is initialized with a
value of 5 (500ms) when the driver is loaded, and the driver never writes
a bad value. I added that note to the commit message.
Thanks,
Guenter
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-12-14 17:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-11 16:43 [PATCH] hwmon (tmp401): fix overflow caused by default conversion rate value Alexey Simakov
2025-12-14 17:49 ` Guenter Roeck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®