In bcm_vk_write(), a VK_FID_TRANS_BUF message embeds an array of num_planes struct _vk_data descriptors at the end of the message. Their start is computed as: data = (struct _vk_data *)&entry->to_v_msg[msg_size + 1]; data -= num_planes; num_planes (0..15) comes from the message cmd field and msg_size (a u8) from the size field; both are fully controlled by user space and are only checked with 'msg_size > to_v_blks'. Nothing verifies that the plane count is consistent with the message size, so a crafted message (for example size 0 with a non-zero plane count) makes 'data' underflow below the copied buffer. bcm_vk_sg_alloc() then reads num_planes descriptors from out-of-bounds memory and interprets them as DMA addresses/sizes. Reject any message whose descriptor range does not fall entirely within the buffer copied from user space. Cc: stable@vger.kernel.org Signed-off-by: Hyunjung Ko --- drivers/misc/bcm-vk/bcm_vk_msg.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/misc/bcm-vk/bcm_vk_msg.c b/drivers/misc/bcm-vk/bcm_vk_msg.c index 17114092a284..e0d57f99a569 100644 --- a/drivers/misc/bcm-vk/bcm_vk_msg.c +++ b/drivers/misc/bcm-vk/bcm_vk_msg.c @@ -1192,6 +1192,21 @@ ssize_t bcm_vk_write(struct file *p_file, /* Now back up to the start of the pointers */ data -= num_planes; + /* + * 'data' and the num_planes descriptors that follow must lie + * within the message buffer copied from user space. A crafted + * message with a plane count inconsistent with its size (e.g. + * size 0 with a non-zero plane count) makes 'data' underflow + * below the buffer (or run past its end), leading to an + * out-of-bounds read in bcm_vk_sg_alloc(). + */ + if ((void *)data < (void *)&entry->to_v_msg[0] || + (void *)(data + num_planes) > + (void *)&entry->to_v_msg[entry->to_v_blks]) { + rc = -EINVAL; + goto write_free_msgid; + } + /* Convert user addresses to DMA SG List */ rc = bcm_vk_sg_alloc(dev, entry->dma, dir, data, num_planes); if (rc) -- 2.43.0