Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH] hwmon: (occ) Check sensor_length against the sensor struct size
@ 2026-11-27  9:08 Maoyi Xie
  2026-11-27  9:13 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Maoyi Xie @ 2026-11-27  9:08 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: linux-hwmon, stable, Runyu Xiao

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27  9:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  9:08 [PATCH] hwmon: (occ) Check sensor_length against the sensor struct size Maoyi Xie
2026-11-27  9:13 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox