From: sashiko-bot@kernel.org
To: "Brian Daniels" <briandaniels@google.com>
Cc: virtualization@lists.linux.dev, media-ci@linuxtv.org,
"Michael S. Tsirkin" <mst@redhat.com>,
"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support
Date: Tue, 01 Dec 2026 19:43:41 +0000 [thread overview]
Message-ID: <sashiko-outbox-244103@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-6-briandaniels@google.com>
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 <gnurou@gmail.com>
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/virtio/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 file *file, poll_table *wait)
> return rc;
> }
>
> +static void virtio_media_vma_close_locked(struct vm_area_struct *vma)
> +{
> + struct virtio_media *vv = vma->vm_private_data;
> + struct virtio_media_cmd_munmap *cmd_munmap = &vv->cmd.munmap;
> + struct virtio_media_resp_munmap *resp_munmap = &vv->resp.munmap;
> + struct scatterlist cmd_sg = {}, resp_sg = {};
> + struct scatterlist *sgs[2] = { &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 = cpu_to_le32(VIRTIO_MEDIA_CMD_MUNMAP);
> + cmd_munmap->hdr.__reserved = 0;
> + cmd_munmap->driver_addr =
> + cpu_to_le64((vma->vm_pgoff << PAGE_SHIFT) -
> + vv->mmap_region.addr);
> + ret = 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 needed
> + * and can be removed.
> + */
> +static void virtio_media_vma_close(struct vm_area_struct *vma)
> +{
> + struct virtio_media *vv = 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 = {
> + .close = 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 make 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 = video_devdata(file);
> + struct virtio_media *vv = to_virtio_media(video_dev);
> + struct virtio_media_session *session =
> + fh_to_session(file->private_data);
> + struct virtio_media_cmd_mmap *cmd_mmap = &session->cmd.mmap;
> + struct virtio_media_resp_mmap *resp_mmap = &session->resp.mmap;
> + struct scatterlist cmd_sg = {}, resp_sg = {};
> + struct scatterlist *sgs[2] = { &cmd_sg, &resp_sg };
> + int ret;
> +
> + mutex_lock(&session->error_lock);
> + bool session_error = 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 = session->error
unlock(&session->error_lock)
lock(&session->error_lock)
session->error = 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 = cpu_to_le32(VIRTIO_MEDIA_CMD_MMAP);
> + cmd_mmap->hdr.__reserved = 0;
> + cmd_mmap->session_id = cpu_to_le32(session->id);
> + cmd_mmap->flags = 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 = 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 >= 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 = 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 = virtio_media_send_command(vv, sgs, 1, 1, sizeof(*resp_mmap),
> + NULL);
> + if (ret < 0)
> + goto end;
> +
> + vma->vm_private_data = vv;
> + /*
> + * Keep the guest address at which the buffer is mapped since we will
> + * use that to unmap.
> + */
> + vma->vm_pgoff = (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 = -EINVAL;
> + goto cleanup;
> + }
> +
> + ret = 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 = &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 = {
> .owner = THIS_MODULE,
> .open = virtio_media_device_open,
> .release = virtio_media_device_close,
> .poll = virtio_media_device_poll,
> .unlocked_ioctl = virtio_media_device_ioctl,
> + .mmap = 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 =
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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201192957.790894-1-briandaniels@google.com?part=5
next prev parent reply other threads:[~2026-12-01 19:43 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 19:29 [PATCH v13 0/6] media: add virtio-media driver Brian Daniels
2026-12-01 19:29 ` [PATCH v13 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-12-01 19:49 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-12-01 19:53 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 3/6] media: virtio: Add extended controls support Brian Daniels
2026-12-01 19:55 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-12-01 19:59 ` sashiko-bot
2026-12-01 19:29 ` [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-12-01 19:43 ` sashiko-bot [this message]
2026-12-01 19:29 ` [PATCH v13 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-12-01 19:58 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-244103@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=briandaniels@google.com \
--cc=eperezma@redhat.com \
--cc=media-ci@linuxtv.org \
--cc=mst@redhat.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox