In ivtvfb_prep_frame(), called from the IVTVFB_IOC_DMA_FRAME ioctl with user-controlled source, dest_offset, and signed int count: 1. Only count == 0 is rejected, allowing negative count values to pass. 2. (dest_offset + count) > oi->video_buffer_size can overflow unsigned long when dest_offset is large (or count is negative and sign-extended), bypassing the framebuffer size limit and passing an unbounded count to ivtv_udma_setup() -> pin_user_pages_unlocked(), which overflows the fixed-size dma->map[IVTV_DMA_SG_OSD_ENT] array. 3. access_ok(source + dest_offset, count) validates the wrong user address (source + dest_offset instead of source, which is what ivtvfb_prep_dec_dma_to_device() actually pins). Reject count <= 0, perform an overflow-safe bounds check against oi->video_buffer_size, and validate access_ok(source, count). Found by LLM-assisted static analysis. Compile-tested on x86_64. Fixes: 32db77545281 ("V4L/DVB (5902): Add ivtv-fb framebuffer driver.") Cc: stable@vger.kernel.org Signed-off-by: Ziheng Wei --- drivers/media/pci/ivtv/ivtvfb.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/media/pci/ivtv/ivtvfb.c b/drivers/media/pci/ivtv/ivtvfb.c index 4f55b4473..a5d7d538b 100644 --- a/drivers/media/pci/ivtv/ivtvfb.c +++ b/drivers/media/pci/ivtv/ivtvfb.c @@ -326,15 +326,18 @@ static int ivtvfb_prep_frame(struct ivtv *itv, int cmd, void __user *source, struct osd_info *oi = itv->osd_info; /* Nothing to do */ - if (count == 0) { - IVTVFB_DEBUG_WARN("ivtvfb_prep_frame: Nothing to do. count = 0\n"); + if (count <= 0) { + IVTVFB_DEBUG_WARN("%s: Nothing to do. count = %d\n", + __func__, count); return -EINVAL; } /* Check Total FB Size */ - if ((dest_offset + count) > oi->video_buffer_size) { - IVTVFB_WARN("ivtvfb_prep_frame: Overflowing the framebuffer %ld, only %d available\n", - dest_offset + count, oi->video_buffer_size); + if (dest_offset > oi->video_buffer_size || + (unsigned long)count > oi->video_buffer_size - dest_offset) { + IVTVFB_WARN("%s: Overflowing framebuffer (offset %lu, count %d, size %u)\n", + __func__, dest_offset, count, + oi->video_buffer_size); return -E2BIG; } @@ -350,7 +353,7 @@ static int ivtvfb_prep_frame(struct ivtv *itv, int cmd, void __user *source, IVTVFB_WARN("ivtvfb_prep_frame: Count not a multiple of 4 (%d)\n", count); /* Check Source */ - if (!access_ok(source + dest_offset, count)) { + if (!access_ok(source, count)) { IVTVFB_WARN("Invalid userspace pointer %p\n", source); IVTVFB_DEBUG_WARN("access_ok() failed for offset 0x%08lx source %p count %d\n", base-commit: 37760be5a04b538bbd25099000e37d3208c27efe -- 2.43.0