All of lore.kernel.org
 help / color / mirror / Atom feed
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 v11 5/6] media: virtio: Add MMAP buffer mapping support
Date: Fri, 20 Nov 2026 22:16:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-226238@kernel.org> (raw)
In-Reply-To: <20261120215916.3149345-6-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] media: virtio: missing vm_ops->open() and pgoff corruption in
  vma close
- [High] media: virtio: use-after-free of commandq in
  virtio_media_vma_close()
- [High] media: virtio: mmap_lock and vlock inversion in
  virtio_media_device_mmap()
- [High] media: virtio: use-after-free in virtio_media_vma_close() after
  remove
- [Medium] media: virtio: missing SHM bounds check in
  virtio_media_device_mmap()
- [Medium] media: virtio: VM_MAYWRITE left set on RO mapping in
  virtio_media_device_mmap()
- [Medium] media: virtio: TOCTOU race on session->error in
  virtio_media_device_mmap()
- [Low] media: virtio: 32-bit offset truncation in
  virtio_media_device_mmap()
--

commit ca0abfda91649fedfc5e57137d16c8a739721fdb
Author: Alexandre Courbot <gnurou@gmail.com>

media: virtio: Add MMAP buffer mapping support

This commit adds V4L2_MEMORY_MMAP buffer mapping support to the virtio-media
driver by querying shared memory region 0 during probe and implementing mmap
and VMA close callbacks 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 7483577256bd0..eb6b31d7e370c 100644
> --- a/drivers/media/virtio/virtio_media_driver.c
> +++ b/drivers/media/virtio/virtio_media_driver.c
[ ... ]
> @@ -697,12 +705,155 @@ 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 = VIRTIO_MEDIA_CMD_MUNMAP;
> +	cmd_munmap->hdr.__reserved = 0;
> +	cmd_munmap->driver_addr =
> +		(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 on vv->commandq if
a mapping is unmapped after virtio_media_remove()?

An active VMA holds a reference on vma->vm_file, which keeps vv alive until
all mappings are unmapped.  If the device is removed while a buffer is mapped,
virtio_media_remove() calls virtio_dev->config->del_vqs(virtio_dev), freeing
vv->commandq.  Unlike V4L2 file operations gated by video_is_registered(), the
MM subsystem invokes vma->vm_ops->close() directly on munmap() or process
exit, so virtio_media_send_command() dereferences 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]
When a mapped VMA outlives virtio_media_remove(), can closing that VMA trigger
both a use-after-free on vv->commandq and a use-after-free and double free of
the session?

drivers/media/virtio/virtio_media_driver.c:virtio_media_remove() {
	...
	/* Delete the virtqueues */
	virtio_dev->config->del_vqs(virtio_dev);

	mutex_unlock(&vv->vlock);

	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);
	}
}

Because vma->vm_file pins the open struct file while the VMA exists, unmapping
the VMA after virtio_media_remove() first calls virtio_media_vma_close(),
which accesses the deleted vv->commandq, and then drops the final file
reference via fput(vma->vm_file).  That invokes v4l2_release() ->
virtio_media_device_close(), which dereferences and frees the session in
file->private_data that virtio_media_remove() already freed.

> +
> +static const struct vm_operations_struct virtio_media_vm_ops = {
> +	.close = virtio_media_vma_close,
> +};

[Severity: High]
Does omitting .open in virtio_media_vm_ops cause premature host unmapping and
corrupted VIRTIO_MEDIA_CMD_MUNMAP offsets when a VMA is duplicated or split?

When a process with a mapped buffer calls fork(), moves the mapping with
mremap(), or splits the VMA via partial munmap() or mprotect(), the kernel
duplicates the VMA and calls vm_ops->open() without calling
virtio_media_device_mmap().  With no .open callback or reference-counted
mapping state in vma->vm_private_data (such as struct vb2_vmarea_handler),
closing one VMA sends VIRTIO_MEDIA_CMD_MUNMAP to the host while the remaining
VMA still maps those shared memory pages in guest userspace.

In addition, __split_vma() advances the upper VMA's vm_pgoff by the split
offset, so when the upper half is later closed,
(vma->vm_pgoff << PAGE_SHIFT) - vv->mmap_region.addr computes an offset inside
the buffer rather than 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;

[Severity: Medium]
Can a VIRTIO_MEDIA_EVT_ERROR event arriving before mutex_lock(&vv->vlock)
cause virtio_media_device_mmap() to send VIRTIO_MEDIA_CMD_MMAP for an already
closed session?

CPU0 (virtio_media_device_mmap)   CPU1 (virtio_media_process_events)
mutex_lock(&session->error_lock)
session_error = session->error
mutex_unlock(&session->error_lock)
                                  mutex_lock(&session->error_lock)
                                  session->error = true
                                  mutex_unlock(&session->error_lock)
                                  virtio_media_session_close(vv, session)
                                    mutex_lock(&vv->vlock)
                                    /* sends VIRTIO_MEDIA_CMD_CLOSE */
                                    mutex_unlock(&vv->vlock)
mutex_lock(&vv->vlock)
/* sends VIRTIO_MEDIA_CMD_MMAP
   on closed session->id */

Re-checking session->error after acquiring vv->vlock (or holding vv->vlock
across the check) would prevent using session->id after
VIRTIO_MEDIA_CMD_CLOSE.

> +	if (!(vma->vm_flags & VM_SHARED))
> +		return -EINVAL;
> +	if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
> +		return -EINVAL;
> +
> +	mutex_lock(&vv->vlock);

[Severity: High]
Does acquiring vv->vlock inside virtio_media_device_mmap() and
virtio_media_vma_close() create an ABBA deadlock with mm->mmap_lock?

CPU0                              CPU1
----                              ----
lock(&mm->mmap_lock)
                                  lock(&vv->vlock)
lock(&vv->vlock)
                                  lock(&mm->mmap_lock)

 *** DEADLOCK ***

CPU0: mmap_region() -> v4l2_mmap() -> virtio_media_device_mmap()
      (or remove_vma() -> virtio_media_vma_close())
CPU1: virtio_media_device_ioctl() -> copy_from_user() / video_ioctl2() /
      pin_user_pages_fast()

virtio_media_device_ioctl() holds vv->vlock across user memory copies and
pin_user_pages_fast(), which acquire mm->mmap_lock on page faults or the GUP
slowpath, while mmap() and munmap() invoke virtio_media_device_mmap() and
virtio_media_vma_close() with mm->mmap_lock already write-locked.

> +
> +	cmd_mmap->hdr.cmd = VIRTIO_MEDIA_CMD_MMAP;
> +	cmd_mmap->hdr.__reserved = 0;
> +	cmd_mmap->session_id = session->id;
> +	cmd_mmap->flags =
> +		(vma->vm_flags & VM_WRITE) ? VIRTIO_MEDIA_MMAP_FLAG_RW_MASK : 0;

[Severity: Medium]
When a buffer is mapped read-only on an O_RDWR file descriptor, can userspace
later gain write access via mprotect() despite the host mapping being created
without VIRTIO_MEDIA_MMAP_FLAG_RW_MASK?

For a shared mapping on an O_RDWR file, do_mmap() sets VM_MAYWRITE in
vma->vm_flags even when PROT_WRITE was not requested.  Because VM_MAYWRITE is
left set when VM_WRITE is clear, a subsequent mprotect(..., PROT_READ |
PROT_WRITE) installs writable guest PTEs backed by a read-only host shared
memory mapping.  Clearing VM_MAYWRITE via vm_flags_clear(vma, VM_MAYWRITE) on
read-only mappings (or checking VM_MAYWRITE when setting cmd_mmap->flags)
would prevent this mismatch.

> +	cmd_mmap->offset = vma->vm_pgoff << PAGE_SHIFT;

[Severity: Low]
Can a 64-bit file offset >= 4 GiB be silently truncated when assigned to the
32-bit cmd_mmap->offset?

On 64-bit kernels, vma->vm_pgoff << PAGE_SHIFT is 64 bits wide while
cmd_mmap->offset is __le32, so an out-of-range mmap() offset wraps modulo 4
GiB and can alias a valid 32-bit plane mem_offset instead of failing with
-EINVAL.

> +	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 = (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(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]
Can an invalid or out-of-bounds resp_mmap->driver_addr cause
io_remap_pfn_range() to remap guest physical memory outside vv->mmap_region?

virtio_media_device_mmap() does not check that resp_mmap->driver_addr and the
mapped range fit within vv->mmap_region.len (without overflow), nor that
resp_mmap->driver_addr is page-aligned before shifting it into vma->vm_pgoff.
If the host returns an unaligned or out-of-range offset, io_remap_pfn_range()
can map arbitrary guest physical addresses outside the shared memory region or
lose the sub-page bits needed to pass the matching driver_addr back in
VIRTIO_MEDIA_CMD_MUNMAP.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120215916.3149345-1-briandaniels@google.com?part=5

  reply	other threads:[~2026-11-20 22:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 21:59 [PATCH v11 0/6] media: add virtio-media driver Brian Daniels
2026-11-20 21:59 ` [PATCH v11 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-11-20 21:59 ` [PATCH v11 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-11-20 21:59 ` [PATCH v11 3/6] media: virtio: Add extended controls support Brian Daniels
2026-11-20 21:59 ` [PATCH v11 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-11-20 21:59 ` [PATCH v11 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-11-20 22:16   ` sashiko-bot [this message]
2026-11-20 21:59 ` [PATCH v11 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels

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-226238@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.