From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796154222; b=sVxEXIMVRQDEV6u+QhL2itdufInAYuOcpjFBFy62J+ZT3gxEs0Tvawz2t2rZtsi2e05y fJL7y2JoMRujyTMEZXkJCni2UUvnEpWDAE9PYy+lv5eeXMyqExF74lQECiZg085K+Zb6C hPAPUVNMXUc+A3HUTfeHWIUUVKz3hc3FeWa4tZz4bOZnxXMIh+kzWzMttL/9GYC5ei3gQ Ip5V1kAhNqJviABQijVYgk0i0OjJlOPuUSaYvidb9/hoZRQsDoVKwcbvxKRcUVSpcPqTH 58do2ph4tfZb/nyqYivfzymXRU257E1DoFSLYdBD8thLPwbI6aRC4sHM/U/clbFu8Sw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796154222; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=IxXieZxJXm3j6PQ75lxQ9U/OY3y3ukEWuw+Mqj9+CjI=; b=Ng9FzvNwpOpm0U6dRQKVJQF2aOL0L8ttYIOGNnGQTrgmA6axFgCJkHJwIhw6/2FP40at z1wGRPnGZIgsUjQUGiy03+iJiZqSl5BdkDi+c0AAnX8OL58DQ2blSBzzhG7XoxyJxxkl9 E9kt1l2HlHY86V1b74t8tTLUGlrZyKe9NSgpTxjGp/Nd97HcTTLHWWBxVuu08bUDvb0ra OdsNsAOiskio4co/D6dJ6MM+CAibNValI4Dg1mGDshkU4b6+FeFxpjWcDMB+TR5GWW2Cp h5rwsPOvqEVaTBc+s0MBG91QToy+lnpuyUn9a9ynZndvSP0ahdrirBe/Oy5VLL5ZaHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UstdAovp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B71E6606A31A for ; Tue, 1 Dec 2026 19:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UstdAovp Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8F521F00893; Tue, 1 Dec 2026 19:43:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796154222; bh=IxXieZxJXm3j6PQ75lxQ9U/OY3y3ukEWuw+Mqj9+CjI=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=UstdAovpk0KKXecUi6g22twMEtYYMEY+MIGToIR104dsx0o/ve+nBPM4oKeBCof4W +kdI+GXD66R/8zV0ZN+Xo7ssmDo2+TIZB1oQ9/xIzxSG9xrz7rKMiTTXiFoGOcMN/a OPB/fo8JnJf2/AKSG+LBJ9AjlUQVggk1X2y8KHiebxwxPS22CzoLI+7ymliFqQWeUV 8GMJ0r/fRJmAy/coYn7xe6OWJ9VmTW4Xe7OisJS1d2CLZJ5kN7CXdoKFbeYJkFd0yK hIQOXFx57jbtsLK8035g3iMza9ghpwHTbAOqLEtRuPpY0Jeme1KVS9QIxdn0FTQzxx cG84qTd5X6wbQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support Reply-To: sashiko-reviews@lists.linux.dev To: "Brian Daniels" Cc: virtualization@lists.linux.dev, media-ci@linuxtv.org, "Michael S. Tsirkin" , "Eugenio Perez" In-Reply-To: <20261201192957.790894-6-briandaniels@google.com> References: <20261201192957.790894-1-briandaniels@google.com> <20261201192957.790894-6-briandaniels@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 19:43:41 +0000 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] media: virtio: missing VMA open op and broken pgoff munmap on fork and split - [High] media: virtio: use-after-free of vv->commandq in virtio_media_vma_close() - [High] media: virtio: AB-BA deadlock between mm->mmap_lock and vv->vlock - [High] media: virtio: session use-after-free and double-free on close after remove - [Medium] media: virtio: missing SHM bounds and alignment checks in virtio_media_device_mmap() - [Medium] media: virtio: VM_MAYWRITE left set on read-only mmap in virtio_media_device_mmap() - [Medium] media: virtio: TOCTOU race on session->error in virtio_media_device_mmap() - [Low] media: virtio: silent 32-bit offset truncation in virtio_media_device_mmap() -- commit 492cd7eb7e4ab2089e069f26dcda9fc7209ca755 Author: Alexandre Courbot media: virtio: Add MMAP buffer mapping support This commit adds support for mapping V4L2_MEMORY_MMAP buffers into guest userspace via virtio shared memory region 0 using VIRTIO_MEDIA_CMD_MMAP and VIRTIO_MEDIA_CMD_MUNMAP. > diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/v= irtio/virtio_media_driver.c > index 406d6ffb70f9e..72c234a7325bb 100644 > --- a/drivers/media/virtio/virtio_media_driver.c > +++ b/drivers/media/virtio/virtio_media_driver.c [ ... ] > @@ -703,12 +711,157 @@ static __poll_t virtio_media_device_poll(struct fi= le *file, poll_table *wait) > return rc; > } > =20 > +static void virtio_media_vma_close_locked(struct vm_area_struct *vma) > +{ > + struct virtio_media *vv =3D vma->vm_private_data; > + struct virtio_media_cmd_munmap *cmd_munmap =3D &vv->cmd.munmap; > + struct virtio_media_resp_munmap *resp_munmap =3D &vv->resp.munmap; > + struct scatterlist cmd_sg =3D {}, resp_sg =3D {}; > + struct scatterlist *sgs[2] =3D { &cmd_sg, &resp_sg }; > + int ret; > + > + sg_set_buf(&cmd_sg, cmd_munmap, sizeof(*cmd_munmap)); > + sg_mark_end(&cmd_sg); > + > + sg_set_buf(&resp_sg, resp_munmap, sizeof(*resp_munmap)); > + sg_mark_end(&resp_sg); > + > + cmd_munmap->hdr.cmd =3D cpu_to_le32(VIRTIO_MEDIA_CMD_MUNMAP); > + cmd_munmap->hdr.__reserved =3D 0; > + cmd_munmap->driver_addr =3D > + cpu_to_le64((vma->vm_pgoff << PAGE_SHIFT) - > + vv->mmap_region.addr); > + ret =3D virtio_media_send_command(vv, sgs, 1, 1, sizeof(*resp_munmap), > + NULL); > + if (ret < 0) { > + v4l2_err(&vv->v4l2_dev, "host failed to unmap buffer: %d\n", > + ret); > + } > +} [Severity: High] Can virtio_media_vma_close_locked() cause a use-after-free of vv->commandq if the VMA is unmapped after the virtio device has been removed? When the device is unbound while a buffer is mapped in userspace, virtio_media_remove() deletes the virtqueues while the VMA still holds a reference to vma->vm_file (which keeps struct virtio_media allocated until the last VMA and file descriptor close): drivers/media/virtio/virtio_media_driver.c:virtio_media_remove() { ... /* Delete the virtqueues */ virtio_dev->config->del_vqs(virtio_dev); ... } Unlike v4l2_mmap() and v4l2_ioctl(), which check video_is_registered() before calling into the driver, the MM subsystem invokes vm_ops->close directly when the process later calls munmap() or exits. virtio_media_vma_close_locked() then calls virtio_media_send_command(), which accesses the freed vv->commandq. > + > +/** > + * virtio_media_vma_close() - Close a MMAP buffer mapping. > + * @vma: VMA of the mapping to close. > + * > + * Inform the host that a previously created MMAP mapping is no longer n= eeded > + * and can be removed. > + */ > +static void virtio_media_vma_close(struct vm_area_struct *vma) > +{ > + struct virtio_media *vv =3D vma->vm_private_data; > + > + mutex_lock(&vv->vlock); > + virtio_media_vma_close_locked(vma); > + mutex_unlock(&vv->vlock); > +} [Severity: High] Does acquiring vv->vlock in virtio_media_vma_close() and virtio_media_device_mmap() introduce an ABBA deadlock with mm->mmap_lock? The MM core calls f_op->mmap and vm_ops->close with mm->mmap_lock held in write mode, taking mm->mmap_lock -> vv->vlock. However, virtio_media_device_ioctl() holds vv->vlock across copy_from_user(), copy_to_user(), video_ioctl2(), and userptr page pinning via vb2_create_framevec() -> pin_user_pages_fast(), any of which can fault or fall back to acquiring mm->mmap_lock: CPU0 CPU1 ---- ---- lock(&vv->vlock) down_write(&mm->mmap_lock) lock(&vv->vlock) down_read(&mm->mmap_lock) *** DEADLOCK *** CPU0: virtio_media_device_ioctl() -> copy_from_user() -> do_user_addr_fault= () CPU1: vm_munmap() -> ... -> virtio_media_vma_close() > + > +static const struct vm_operations_struct virtio_media_vm_ops =3D { > + .close =3D virtio_media_vma_close, > +}; [Severity: High] What happens when a VMA using virtio_media_vm_ops is duplicated on fork() or split by a partial munmap() or mprotect()? Because virtio_media_vm_ops does not implement .open (or reference-count a per-mapping object in vma->vm_private_data like vb2_common_vm_ops does), dup_mmap(), copy_vma(), and __split_vma() create additional VMAs backed by a single VIRTIO_MEDIA_CMD_MMAP call. When the first VMA closes (for example, when a forked child exits or one half of a split VMA is unmapped), virtio_media_vma_close() sends VIRTIO_MEDIA_CMD_MUNMAP and unmaps the host shared memory while the remaining VMA is still mapped in guest userspace. In addition, when __split_vma() splits a VMA, it advances vma->vm_pgoff on the upper VMA by the split delta. When that upper VMA closes, virtio_media_vma_close_locked() computes cmd_munmap->driver_addr from (vma->vm_pgoff << PAGE_SHIFT) - vv->mmap_region.addr, sending a shifted mid-buffer offset to the host instead of the base driver_addr returned by VIRTIO_MEDIA_CMD_MMAP. > + > +/** > + * virtio_media_device_mmap() - Perform a mmap request from userspace. > + * @file: opened file of the session to map for. > + * @vma: VM area struct describing the desired mapping. > + * > + * This requests the host to map a MMAP buffer for us, so we can then ma= ke that > + * mapping visible into user-space address space. > + */ > +static int virtio_media_device_mmap(struct file *file, > + struct vm_area_struct *vma) > +{ > + struct video_device *video_dev =3D video_devdata(file); > + struct virtio_media *vv =3D to_virtio_media(video_dev); > + struct virtio_media_session *session =3D > + fh_to_session(file->private_data); > + struct virtio_media_cmd_mmap *cmd_mmap =3D &session->cmd.mmap; > + struct virtio_media_resp_mmap *resp_mmap =3D &session->resp.mmap; > + struct scatterlist cmd_sg =3D {}, resp_sg =3D {}; > + struct scatterlist *sgs[2] =3D { &cmd_sg, &resp_sg }; > + int ret; > + > + mutex_lock(&session->error_lock); > + bool session_error =3D session->error; > + > + mutex_unlock(&session->error_lock); > + > + if (session_error) > + return -EIO; > + if (!(vma->vm_flags & VM_SHARED)) > + return -EINVAL; > + if (!(vma->vm_flags & (VM_READ | VM_WRITE))) > + return -EINVAL; > + > + mutex_lock(&vv->vlock); [Severity: Medium] Can a VIRTIO_MEDIA_EVT_ERROR event race with virtio_media_device_mmap() after session->error_lock is dropped and before vv->vlock is acquired? CPU0 CPU1 (eventq_work) virtio_media_device_mmap() virtio_media_process_events() lock(&session->error_lock) session_error =3D session->error unlock(&session->error_lock) lock(&session->error_lock) session->error =3D true unlock(&session->error_lock) virtio_media_session_close(vv, session) lock(&vv->vlock) /* closes session->id on host */ unlock(&vv->vlock) lock(&vv->vlock) /* sends CMD_MMAP for closed session->id */ Checking session->error after acquiring vv->vlock would avoid sending VIRTIO_MEDIA_CMD_MMAP for a session that has already been closed on the host. > + > + cmd_mmap->hdr.cmd =3D cpu_to_le32(VIRTIO_MEDIA_CMD_MMAP); > + cmd_mmap->hdr.__reserved =3D 0; > + cmd_mmap->session_id =3D cpu_to_le32(session->id); > + cmd_mmap->flags =3D cpu_to_le32((vma->vm_flags & VM_WRITE) ? > + VIRTIO_MEDIA_MMAP_FLAG_RW_MASK : 0); [Severity: Medium] When a buffer is mapped read-only (!(vma->vm_flags & VM_WRITE)) on a file opened with O_RDWR, should VM_MAYWRITE be cleared on the VMA? The MM core sets VM_MAYWRITE on shared mappings of O_RDWR files even when mmap() is called with PROT_READ. Because cmd_mmap->flags omits VIRTIO_MEDIA_MMAP_FLAG_RW_MASK when VM_WRITE is not set, leaving VM_MAYWRITE set allows userspace to later call mprotect(..., PROT_WRITE) and make the guest PTEs writable without requesting a writable mapping from the host. > + cmd_mmap->offset =3D cpu_to_le32(vma->vm_pgoff << PAGE_SHIFT); [Severity: Low] Can vma->vm_pgoff << PAGE_SHIFT silently truncate a 64-bit mmap offset to 32 bits in cmd_mmap->offset? If userspace passes an offset >=3D 4 GiB (vma->vm_pgoff > (U32_MAX >> PAGE_SHIFT)), the upper 32 bits are discarded instead of returning -EINVAL, aliasing a valid 32-bit plane mem_offset. Also, in virtio_media_vma_close_locked(), (vma->vm_pgoff << PAGE_SHIFT) is evaluated in unsigned long rather than u64 or PFN_PHYS(vma->vm_pgoff). > + cmd_mmap->__reserved =3D 0; > + > + sg_set_buf(&cmd_sg, cmd_mmap, sizeof(*cmd_mmap)); > + sg_mark_end(&cmd_sg); > + > + sg_set_buf(&resp_sg, resp_mmap, sizeof(*resp_mmap)); > + sg_mark_end(&resp_sg); > + > + /* > + * The host performs reference counting and is smart enough to return > + * the same guest physical address if this is called several times on > + * the same > + * buffer. > + */ > + ret =3D virtio_media_send_command(vv, sgs, 1, 1, sizeof(*resp_mmap), > + NULL); > + if (ret < 0) > + goto end; > + > + vma->vm_private_data =3D vv; > + /* > + * Keep the guest address at which the buffer is mapped since we will > + * use that to unmap. > + */ > + vma->vm_pgoff =3D (le64_to_cpu(resp_mmap->driver_addr) + > + vv->mmap_region.addr) >> PAGE_SHIFT; > + > + /* > + * We cannot let the mapping be larger than the buffer. > + */ > + if (vma->vm_end - vma->vm_start > > + PAGE_ALIGN(le64_to_cpu(resp_mmap->len))) { > + dev_dbg(&video_dev->dev, > + "invalid MMAP, as it would overflow buffer length\n"); > + ret =3D -EINVAL; > + goto cleanup; > + } > + > + ret =3D io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, > + vma->vm_end - vma->vm_start, > + vma->vm_page_prot); [Severity: Medium] Should virtio_media_device_mmap() validate that resp_mmap->driver_addr is page-aligned and that the mapped range fits within vv->mmap_region.len? virtio_media_probe() populates vv->mmap_region.len via virtio_get_shm_region(), but virtio_media_device_mmap() never checks that [driver_addr, driver_addr + (vma->vm_end - vma->vm_start)) lies within [0, vv->mmap_region.len) without overflowing. An out-of-range driver_addr from the device would cause io_remap_pfn_range() to map arbitrary guest physical memory outside the shared memory region into userspace, and a non-page-aligned driver_addr would lose its sub-page bits when shifted into vma->vm_pgoff, causing VIRTIO_MEDIA_CMD_MUNMAP to send a different address. > + if (ret) { > + dev_dbg(&video_dev->dev, "io_remap_pfn_range() failed\n"); > + goto cleanup; > + } > + > + vma->vm_ops =3D &virtio_media_vm_ops; > + goto end; > +cleanup: > + virtio_media_vma_close_locked(vma); > +end: > + mutex_unlock(&vv->vlock); > + return ret; > +} > + > static const struct v4l2_file_operations virtio_media_fops =3D { > .owner =3D THIS_MODULE, > .open =3D virtio_media_device_open, > .release =3D virtio_media_device_close, > .poll =3D virtio_media_device_poll, > .unlocked_ioctl =3D virtio_media_device_ioctl, > + .mmap =3D virtio_media_device_mmap, > }; [Severity: High] When a VMA mapping or open file descriptor outlives virtio_media_remove(), does virtio_media_device_close() trigger a use-after-free and double free of struct virtio_media_session? An active VMA holds a reference to vma->vm_file, delaying v4l2_release() -> virtio_media_device_close() until the VMA is unmapped and the file closes. However, virtio_media_remove() unconditionally frees all active sessions on vv->sessions when the device is removed: drivers/media/virtio/virtio_media_driver.c:virtio_media_remove() { ... list_for_each_safe(p, n, &vv->sessions) { struct virtio_media_session *s =3D list_entry(p, struct virtio_media_session, list); virtio_media_session_remove(vv, s); virtio_media_session_free(s); } ... } When the file is subsequently released, virtio_media_device_close() retrieves the freed session from file->private_data, dereferences session->error_lock, sends VIRTIO_MEDIA_CMD_CLOSE on the deleted vv->commandq, and calls virtio_media_session_remove() and virtio_media_session_free() a second time. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201192957.7908= 94-1-briandaniels@google.com?part=3D5