Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

firmware: arm_scmi: Harden shared memory access in fetch_response

A misbheaving SCMI platform firmware could reply with out-of-spec messages,
shorter than the mimimum size comprising a header and a status field.

Harden shmem_fetch_response to properly truncate such a bad messages.

Fixes: 5c8a47a5a91d ("firmware: arm_scmi: Make scmi core independent of the transport type")
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
Link: https://lore.kernel.org/r/20221222183823.518856-3-cristian.marussi@arm.com
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>

authored by

Cristian Marussi and committed by
Sudeep Holla
ad78b81a f6ca5059

+3 -2
+3 -2
drivers/firmware/arm_scmi/shmem.c
··· 81 81 void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem, 82 82 struct scmi_xfer *xfer) 83 83 { 84 + size_t len = ioread32(&shmem->length); 85 + 84 86 xfer->hdr.status = ioread32(shmem->msg_payload); 85 87 /* Skip the length of header and status in shmem area i.e 8 bytes */ 86 - xfer->rx.len = min_t(size_t, xfer->rx.len, 87 - ioread32(&shmem->length) - 8); 88 + xfer->rx.len = min_t(size_t, xfer->rx.len, len > 8 ? len - 8 : 0); 88 89 89 90 /* Take a copy to the rx buffer.. */ 90 91 memcpy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer->rx.len);