All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wayne Ho <wayneho219@gmail.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Pascal Sachs <pascal.sachs@sensirion.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
	Wayne Ho <wayneho219@gmail.com>, Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH] hwmon: (sht3x) Fix overflow when writing alert limits
Date: Fri, 13 Nov 2026 13:48:57 +0800	[thread overview]
Message-ID: <20261113054857.97048-1-wayneho219@gmail.com> (raw)

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.

Fixes: 7c84f7f80d6f ("hwmon: add support for Sensirion SHT3x sensors")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/linux-hwmon/20260725222229.EA69B1F000E9@smtp.kernel.org/
Signed-off-by: Wayne Ho <wayneho219@gmail.com>
---

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(-)

diff --git a/drivers/hwmon/sht3x.c b/drivers/hwmon/sht3x.c
index 866e9113f..68ab1cf1e 100644
--- a/drivers/hwmon/sht3x.c
+++ b/drivers/hwmon/sht3x.c
@@ -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;
-- 
2.43.0


             reply	other threads:[~2026-11-13  5:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  5:48 Wayne Ho [this message]
2026-11-13  5:54 ` [PATCH] hwmon: (sht3x) Fix overflow when writing alert limits sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113054857.97048-1-wayneho219@gmail.com \
    --to=wayneho219@gmail.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=pascal.sachs@sensirion.com \
    --cc=sashiko-bot@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.