From: Ali Ahmet Memis <ali@iusegentoo.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Inochi Amaoto <inochiama@outlook.com>,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH] hwmon: (sg2042-mcu) reject short uptime reads
Date: Sun, 2 Aug 2026 13:07:28 +0000 [thread overview]
Message-ID: <20260802130728.25721-1-ali@iusegentoo.com> (raw)
uptime_show() asks for two bytes and only rejects a negative return:
u8 time_val[2];
ret = i2c_smbus_read_i2c_block_data(mcu->client, REG_UPTIME,
sizeof(time_val), time_val);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n",
(time_val[0]) | (time_val[1] << 8));
i2c_smbus_read_i2c_block_data() returns the number of bytes the transfer
actually produced, which the device supplies and which can be shorter
than the length asked for:
memcpy(values, &data.block[1], data.block[0]);
return data.block[0];
time_val is not initialised, so a reply of one byte leaves the high half
of the reported uptime as whatever was on the stack, and a reply of zero
bytes leaks both halves. Either way the value ends up in sysfs.
Require the full two bytes. The other registers this driver reads go
through i2c_smbus_read_byte_data(), which returns the byte itself, so
the existing negative-only checks are right there.
Fixes: 758b62e562f2 ("hwmon: Add sophgo SG2042 external hardware monitor support")
Cc: stable@vger.kernel.org
Signed-off-by: Ali Ahmet Memis <ali@iusegentoo.com>
---
drivers/hwmon/sg2042-mcu.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/hwmon/sg2042-mcu.c b/drivers/hwmon/sg2042-mcu.c
index 591f5f572fe4..10292417e766 100644
--- a/drivers/hwmon/sg2042-mcu.c
+++ b/drivers/hwmon/sg2042-mcu.c
@@ -79,6 +79,8 @@ static ssize_t uptime_show(struct device *dev,
sizeof(time_val), time_val);
if (ret < 0)
return ret;
+ if (ret != sizeof(time_val))
+ return -EIO;
return sprintf(buf, "%d\n",
(time_val[0]) | (time_val[1] << 8));
base-commit: 2d2338c93da79b3bfe4b6099a931d9468d539952
--
2.55.0
next reply other threads:[~2026-08-02 13:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 13:07 Ali Ahmet Memis [this message]
2026-08-02 15:00 ` Guenter Roeck
2026-08-02 15:11 ` Ali Ahmet Memis
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=20260802130728.25721-1-ali@iusegentoo.com \
--to=ali@iusegentoo.com \
--cc=inochiama@outlook.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=stable@vger.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 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®