mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-07-21 22:47:51 -04:00
hwmon: occ: validate poll response sensor blocks
The OCC poll response parser walks a counted list of sensor data blocks.
It used the static backing-array capacity as the parse boundary, but a
transport response makes only data_length bytes current and valid. A
truncated response can therefore make the parser consume a block header or
block extent outside the current response.
Use data_length as the parent boundary, prove the fixed poll header and
each current block header before reading them, and prove the complete block
before advancing. Keep parsed sensor metadata local until the complete
response has passed validation, then publish it. Propagate
malformed-response errors before publishing the OCC as active.
Fixes: aa195fe49b ("hwmon (occ): Parse OCC poll response")
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Link: https://lore.kernel.org/r/20260720115826.14813-1-pengpeng@iscas.ac.cn
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
committed by
Guenter Roeck
parent
9813c1f49e
commit
70e76e700f
@@ -1052,32 +1052,49 @@ static int occ_setup_sensor_attrs(struct occ *occ)
|
||||
}
|
||||
|
||||
/* only need to do this once at startup, as OCC won't change sensors on us */
|
||||
static void occ_parse_poll_response(struct occ *occ)
|
||||
static int occ_parse_poll_response(struct occ *occ)
|
||||
{
|
||||
unsigned int i, old_offset, offset = 0, size = 0;
|
||||
u16 data_length;
|
||||
struct occ_sensor *sensor;
|
||||
struct occ_sensors *sensors = &occ->sensors;
|
||||
struct occ_sensors parsed = {};
|
||||
struct occ_sensors *sensors = &parsed;
|
||||
struct occ_response *resp = &occ->resp;
|
||||
struct occ_poll_response *poll =
|
||||
(struct occ_poll_response *)&resp->data[0];
|
||||
struct occ_poll_response_header *header = &poll->header;
|
||||
struct occ_sensor_data_block *block = &poll->block;
|
||||
|
||||
data_length = get_unaligned_be16(&resp->data_length);
|
||||
if (data_length < sizeof(*header) || data_length > OCC_RESP_DATA_BYTES) {
|
||||
dev_err(occ->bus_dev, "invalid OCC poll response length %u\n",
|
||||
data_length);
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
|
||||
header->occ_code_level);
|
||||
|
||||
for (i = 0; i < header->num_sensor_data_blocks; ++i) {
|
||||
block = (struct occ_sensor_data_block *)((u8 *)block + offset);
|
||||
if (size + sizeof(*header) + sizeof(block->header) >
|
||||
data_length) {
|
||||
dev_err(occ->bus_dev,
|
||||
"truncated OCC sensor block header\n");
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
old_offset = offset;
|
||||
offset = (block->header.num_sensors *
|
||||
block->header.sensor_length) + sizeof(block->header);
|
||||
size += offset;
|
||||
|
||||
/* validate all the length/size fields */
|
||||
if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
|
||||
dev_warn(occ->bus_dev, "exceeded response buffer\n");
|
||||
return;
|
||||
if (size + sizeof(*header) + offset > data_length) {
|
||||
dev_err(occ->bus_dev,
|
||||
"exceeded OCC poll response length\n");
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
size += offset;
|
||||
|
||||
dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
|
||||
old_offset, offset - 1, block->header.eye_catcher,
|
||||
@@ -1107,6 +1124,9 @@ static void occ_parse_poll_response(struct occ *occ)
|
||||
|
||||
dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
|
||||
sizeof(*header), size + sizeof(*header));
|
||||
occ->sensors = parsed;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int occ_active(struct occ *occ, bool active)
|
||||
@@ -1138,10 +1158,12 @@ int occ_active(struct occ *occ, bool active)
|
||||
goto unlock;
|
||||
}
|
||||
|
||||
occ->active = true;
|
||||
occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
|
||||
occ_parse_poll_response(occ);
|
||||
rc = occ_parse_poll_response(occ);
|
||||
if (rc)
|
||||
goto unlock;
|
||||
|
||||
occ->active = true;
|
||||
rc = occ_setup_sensor_attrs(occ);
|
||||
if (rc) {
|
||||
dev_err(occ->bus_dev,
|
||||
|
||||
Reference in New Issue
Block a user