Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Maoyi Xie <maoyixie.tju@gmail.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org, stable@vger.kernel.org,
	Runyu Xiao <runyu.xiao@seu.edu.cn>
Subject: [PATCH] hwmon: (occ) Check sensor_length against the sensor struct size
Date: Fri, 27 Nov 2026 17:08:37 +0800	[thread overview]
Message-ID: <20261127090837.164246-1-maoyixie.tju@gmail.com> (raw)

occ_parse_poll_response() bounds each sensor block by
num_sensors * sensor_length. The show functions index the block with
fixed structs, such as the 68-byte struct power_sensor_a0. A smaller
sensor_length passes this check. The reads then run up to 17340 bytes
past the block.

Ignore a block whose sensor_length is smaller than the entry read by the
show functions, like a block of unknown type. A larger sensor_length is
still accepted, because the reads stay inside the block.

Found with a CodeQL-based static analysis tool that we are developing.
Reproduced in a userspace harness under AddressSanitizer. Not tested
on hardware.

Fixes: c10e753d43eb ("hwmon (occ): Add sensor types and versions")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260617201822.3B46E1F000E9@smtp.kernel.org/
Reported-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
Link: https://lore.kernel.org/all/20260619023019.1064970-1-runyu.xiao@seu.edu.cn/
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
I used an LLM to help write the harness and this patch.

 drivers/hwmon/occ/common.c | 48 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
index 5836054c5c25..4ec954728222 100644
--- a/drivers/hwmon/occ/common.c
+++ b/drivers/hwmon/occ/common.c
@@ -1049,6 +1049,45 @@ static int occ_setup_sensor_attrs(struct occ *occ)
 	return 0;
 }
 
+/*
+ * Size of one sensor entry, as the show functions read it.
+ * Keep in sync with occ_setup_sensor_attrs().
+ */
+static const struct {
+	const char *type;
+	u8 format;
+	u8 size;
+} occ_sensor_sizes[] = {
+	{ "TEMP", 0x01, sizeof(struct temp_sensor_1) },
+	{ "TEMP", 0x02, sizeof(struct temp_sensor_2) },
+	{ "TEMP", 0x10, sizeof(struct temp_sensor_10) },
+	{ "FREQ", 0x01, sizeof(struct freq_sensor_1) },
+	{ "FREQ", 0x02, sizeof(struct freq_sensor_2) },
+	{ "POWR", 0x01, sizeof(struct power_sensor_1) },
+	{ "POWR", 0x02, sizeof(struct power_sensor_2) },
+	{ "POWR", 0xA0, sizeof(struct power_sensor_a0) },
+	/* format 1 has no user_source */
+	{ "CAPS", 0x01, offsetof(struct caps_sensor_2, user_source) },
+	{ "CAPS", 0x02, sizeof(struct caps_sensor_2) },
+	{ "CAPS", 0x03, sizeof(struct caps_sensor_3) },
+	{ "EXTN", 0x01, sizeof(struct extended_sensor) },
+};
+
+static unsigned int occ_sensor_size(const struct occ_sensor_data_block *block)
+{
+	const struct occ_sensor_data_block_header *header = &block->header;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(occ_sensor_sizes); i++) {
+		if (header->sensor_format == occ_sensor_sizes[i].format &&
+		    !strncmp(header->eye_catcher, occ_sensor_sizes[i].type, 4))
+			return occ_sensor_sizes[i].size;
+	}
+
+	/* unknown formats are not used, see occ_setup_sensor_attrs() */
+	return 0;
+}
+
 /* only need to do this once at startup, as OCC won't change sensors on us */
 static int occ_parse_poll_response(struct occ *occ)
 {
@@ -1094,6 +1133,15 @@ static int occ_parse_poll_response(struct occ *occ)
 		}
 		size += offset;
 
+		if (block->header.num_sensors &&
+		    block->header.sensor_length < occ_sensor_size(block)) {
+			dev_warn(occ->bus_dev,
+				 "ignoring OCC %.4s sensors of length %u\n",
+				 block->header.eye_catcher,
+				 block->header.sensor_length);
+			continue;
+		}
+
 		dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
 			old_offset, offset - 1, block->header.eye_catcher,
 			block->header.num_sensors);
-- 
2.34.1


             reply	other threads:[~2026-11-27  9:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  9:08 Maoyi Xie [this message]
2026-11-27  9:13 ` [PATCH] hwmon: (occ) Check sensor_length against the sensor struct size 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=20261127090837.164246-1-maoyixie.tju@gmail.com \
    --to=maoyixie.tju@gmail.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=runyu.xiao@seu.edu.cn \
    --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