hwmon: (sht3x) Fix overflow when writing alert limits
PATCH 1/1 hwmon: (sht3x) Fix overflow when writing alert limits
limit_write() encodes the limits with 32-bit arithmetic, which overflows for the largest accepted values:
(130000 + 45000) * 24543 = 4295025000 > U32_MAX
100000 * 42950 = 4295000000 > U32_MAX
Writing 130000 to temp1_max therefore programs -45 degrees C, and writing 100000 to humidity1_max programs 0 %RH, so a limit set to its maximum makes the alert fire instead of never firing. Reading the attributes back returns the cached values, so this is not visible from sysfs.
Use 64-bit arithmetic and clamp each result to its field width. Even with 64-bit arithmetic, 130000 encodes to 0x200, which overflows the 9-bit temperature field, and 100000 encodes to 0x10000, which is still 0 after masking.
Prepared with the help of an AI coding assistant (Claude); I reproduced the bug and tested the fix on hardware.
---
Notes:
Tested on an STM32MP215F-DK (SHT30) with this change backported to a
v6.6 based kernel, reading the high alert set limit register (0xE11F)
back with i2ctransfer (before -> after):
humidity1_max = 100000 0x0133 -> 0xff33
temp1_max = 130000 (humidity1_max is 100000) 0x0000 -> 0xffff
All other accepted values encode as before (checked exhaustively in
userspace). On hwmon-next, this was build-tested only.drivers/hwmon/sht3x.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/sht3x.c +2 -2
@@ -388,8 +388,8 @@ static size_t limit_write(struct device *dev,* adapted for fixed point arithmetic and packed the same as * in limit_read() */ - raw = ((u32)(temperature +45000 ) *24543 ) >> (16 +7 ); - raw |= ((humidity *42950 ) >>16 ) &0xfe00 ; + raw =min (((u64)(temperature +45000 ) *24543 ) >> (16 +7 ),0x1ff ); + raw |=min (((u64 )humidity *42950 ) >>16 ,0xffff ) &0xfe00 ;put_unaligned_be16 (raw, position); position +=SHT3X_WORD_LEN ;
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 17 lines
limit_write()encodes the limits with 32-bit arithmetic, which overflows for the largest accepted values:(130000 + 45000) * 24543 = 4295025000 >
U32_MAX
100000 * 42950 = 4295000000 >U32_MAXWriting 130000 to
temp1_maxtherefore programs -45 degrees C, and writing 100000 tohumidity1_maxprograms 0 %RH, so a limit set to its maximum makes the alert fire instead of never firing. Reading the attributes back returns the cached values, so this is not visible from sysfs.Use 64-bit arithmetic and clamp each result to its field width. Even with 64-bit arithmetic, 130000 encodes to
0x200, which overflows the 9-bit temperature field, and 100000 encodes to0x10000, which is still 0 after masking.
Quoted 7 lines
Prepared with the help of an AI coding assistant (Claude); I reproduced the bug and tested the fix on hardware.
Fixes 7c84f7f80d6f ("hwmon: add support for Sensirion SHT3x sensors")Reported-by Sashiko <[email protected]>Signed-off-by Wayne Ho <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerQuoted 17 lines · Wayne Ho
limit_write()encodes the limits with 32-bit arithmetic, which overflows for the largest accepted values:(130000 + 45000) * 24543 = 4295025000 >
U32_MAX
100000 * 42950 = 4295000000 >U32_MAXWriting 130000 to
temp1_maxtherefore programs -45 degrees C, and writing 100000 tohumidity1_maxprograms 0 %RH, so a limit set to its maximum makes the alert fire instead of never firing. Reading the attributes back returns the cached values, so this is not visible from sysfs.Use 64-bit arithmetic and clamp each result to its field width. Even with 64-bit arithmetic, 130000 encodes to
0x200, which overflows the 9-bit temperature field, and 100000 encodes to0x10000, which is still 0 after masking.
Quoted 7 lines
Prepared with the help of an AI coding assistant (Claude); I reproduced the bug and tested the fix on hardware.
Fixes 7c84f7f80d6f ("hwmon: add support for Sensirion SHT3x sensors")Reported-by Sashiko <[email protected]>Signed-off-by Wayne Ho <[email protected]>
Appied.
Thanks,
Guenter