cros_ec_sensorhub_ring_handler() processes number_data entries of the EC reply to MOTIONSENSE_CMD_FIFO_READ. It checks number_data against the FIFO count and the ring space, but not against the reply length. A malfunctioning EC can send fewer entries than number_data. The loop then uses stale bytes as samples. Past max_response bytes, it reads beyond the message buffer. Stop reading when the reply does not hold all number_data entries. Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware. Fixes: 145d59baff59 ("platform/chrome: cros_ec_sensorhub: Add FIFO support") Cc: stable@vger.kernel.org Signed-off-by: Maoyi Xie --- I used an LLM to help write the harness and this patch. drivers/platform/chrome/cros_ec_sensorhub_ring.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c index cbaec9ff8fbd..1a4ad45ad804 100644 --- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c +++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c @@ -969,6 +969,13 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub) number_data, fifo_info->count - i); break; } + if (ret < struct_size(&sensorhub->resp->fifo_read, data, + number_data)) { + dev_warn_ratelimited(sensorhub->dev, + "Invalid EC data: %d entries in %d bytes\n", + number_data, ret); + break; + } if (out + number_data > sensorhub->ring + fifo_info->count) { dev_warn(sensorhub->dev, -- 2.34.1