Linux virtualization list
 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 v12 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure
Date: Sat, 21 Nov 2026 03:15:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-226565@kernel.org> (raw)
In-Reply-To: <20261121025007.3445723-3-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] media: virtio: stack buffers mapped for DMA in
  scatterlist_builder_add_data()
- [High] media: virtio: V4L2 core and remove race bypass in
  virtio_media_device_ioctl()
- [Medium] media: virtio: const discard in
  scatterlist_builder_retrieve_data() on ppc64le
- [Medium] media: virtio: missing NUL-termination of card in
  virtio_media_querycap()
- [Medium] media: virtio: uninitialized stack payload in
  virtio_media_g_input()
--

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

media: virtio: Add V4L2 ioctl forwarding infrastructure

This commit introduces a scatterlist builder and synchronous V4L2 ioctl
forwarding via VIRTIO_MEDIA_CMD_IOCTL, along with handlers for
VIDIOC_QUERYCAP and fixed-size V4L2 ioctls.

> diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/virtio/scatterlist_builder.c
> new file mode 100644
> index 0000000000000..ae8ffee82fa6e
> --- /dev/null
> +++ b/drivers/media/virtio/scatterlist_builder.c
> @@ -0,0 +1,222 @@

[ ... ]

> +int scatterlist_builder_add_data(struct scatterlist_builder *builder,
> +				 void *data, size_t len)
> +{
> +	const size_t cur_desc = builder->cur_desc;
> +
> +	if (len == 0)
> +		return 0;
> +
> +	if (builder->cur_desc >= builder->num_descs)
> +		return -ENOSPC;
> +
> +	if (!always_use_shadow_buffer && virt_addr_valid(data) &&
> +	    virt_addr_valid(data + len)) {
> +		/*
> +		 * If "data" is in the 1:1 physical memory mapping then we can
> +		 * use a single SG entry and avoid copying.
> +		 */
> +		struct page *page = virt_to_page(data);
> +		size_t offset = (((size_t)data) & ~PAGE_MASK);
> +		struct scatterlist *next_desc =
> +			&builder->descs[builder->cur_desc];
> +
> +		memset(next_desc, 0, sizeof(*next_desc));
> +		sg_set_page(next_desc, page, len, offset);
> +		builder->cur_desc++;
> +	} else if (!always_use_shadow_buffer && is_vmalloc_addr(data)) {

[Severity: High]
Does scatterlist_builder_add_data() map kernel stack buffers directly into
scatterlists for DMA instead of bouncing them through the shadow buffer?

Most ioctl payloads passed here live on the kernel stack: video_usercopy()
uses a 128-byte stack buffer (sbuf) for small ioctls, and handlers such as
virtio_media_g_input(), virtio_media_s_std(), and
virtio_media_device_ioctl() pass addresses of local stack variables.
Because neither branch checks !object_is_on_stack(data), stack buffers
match virt_addr_valid() when CONFIG_VMAP_STACK=n and is_vmalloc_addr()
when CONFIG_VMAP_STACK=y.  Mapping stack memory into virtio scatterlists
violates kernel DMA rules (triggering CONFIG_DMA_API_DEBUG warnings when
the virtqueue uses the DMA API) and risks corrupting adjacent stack frames
sharing a cache line on non-coherent DMA platforms while the caller sleeps
in wait_event().

Also, virt_addr_valid(data + len) checks one byte past the end of the
buffer instead of data + len - 1.

[ ... ]

> +int scatterlist_builder_retrieve_data(struct scatterlist_builder *builder,
> +				      size_t sg_index, void *data)
> +{
> +	void *shadow_buf = builder->shadow_buffer;
> +	struct scatterlist *sg;
> +	void *kaddr;
> +
> +	/* We can only retrieve from the range of sgs currently set. */
> +	if (sg_index >= builder->cur_sg)
> +		return -ERANGE;
> +
> +	sg = builder->sgs[sg_index];
> +	kaddr = pfn_to_kaddr(page_to_pfn(sg_page(sg))) + sg->offset;

[Severity: Medium]
Does assigning pfn_to_kaddr() to void *kaddr fail to build on 64-bit
little-endian PowerPC with CONFIG_WERROR=y?  On PowerPC, pfn_to_kaddr()
returns const void *, so this assignment triggers -Wdiscarded-qualifiers.
Using sg_virt(sg) avoids the qualifier mismatch.

> diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c
> new file mode 100644
> index 0000000000000..e92032bce7dbe
> --- /dev/null
> +++ b/drivers/media/virtio/virtio_media_ioctls.c
> @@ -0,0 +1,606 @@

[ ... ]

> +static int virtio_media_querycap(struct file *file, void *fh,
> +				 struct v4l2_capability *cap)
> +{
> +	struct video_device *video_dev = video_devdata(file);
> +	struct virtio_media *vv = to_virtio_media(video_dev);
> +
> +	strscpy(cap->bus_info, "platform:virtio-media");
> +	strscpy(cap->driver, VIRTIO_MEDIA_DEFAULT_DRIVER_NAME);
> +
> +	virtio_cread_bytes(vv->virtio_dev, 8, cap->card, sizeof(cap->card));
> +
> +	cap->capabilities = video_dev->device_caps | V4L2_CAP_DEVICE_CAPS;
> +	cap->device_caps = video_dev->device_caps;
> +
> +	return 0;
> +}

[Severity: Medium]
Can virtio_media_querycap() return a non-NUL-terminated cap->card string to
userspace?

In include/uapi/linux/virtio_media.h, struct virtio_media_config documents
that its 32-byte card field does not need to be NUL-terminated when all
characters are used, whereas V4L2 requires v4l2_capability.card to always
be NUL-terminated.  Ensuring cap->card[sizeof(cap->card) - 1] = '\0' (and
using offsetof(struct virtio_media_config, card) instead of the hardcoded
8) would prevent unterminated strings from reaching userspace.

> +
> +/*
> + * s/g_input/output work with an unsigned int - recast this to a u32 so the
> + * size is unambiguous.
> + */
> +
> +static int virtio_media_g_input(struct file *file, void *fh, unsigned int *i)
> +{
> +	u32 input;
> +
> +	int ret = virtio_media_send_r_ioctl(file_to_v4l2_fh(file),
> +					 VIDIOC_G_INPUT, &input,
> +					 sizeof(input));
> +	if (ret)
> +		return ret;
> +
> +	*i = input;
> +
> +	return 0;
> +}

[Severity: Medium]
Can virtio_media_g_input() and virtio_media_g_output() copy uninitialized
kernel stack memory into the DMA shadow buffer or back to userspace?

Both functions leave their local u32 variable uninitialized before calling
virtio_media_send_r_ioctl() -> scatterlist_builder_add_data().  When the
shadow buffer path is used, scatterlist_builder_add_data() unconditionally
runs memcpy(shadow_buffer, data, len) even for _IOR response buffers,
copying the uninitialized stack value into session->shadow_buf, and if the
host does not overwrite the buffer (or returns a status with bit 31 set,
which virtio_media_kick_command() negates as unsigned __le32 into a
positive value that virtio_media_send_command() treats as success), that
uninitialized value is returned to userspace.

[ ... ]

> +long virtio_media_device_ioctl(struct file *file, unsigned int cmd,
> +			       unsigned long arg)
> +{
> +	struct video_device *video_dev = video_devdata(file);
> +	struct virtio_media *vv = to_virtio_media(video_dev);
> +	struct v4l2_fh *vfh = NULL;
> +	struct v4l2_standard standard;
> +	v4l2_std_id std_id = 0;
> +	int ret;
> +
> +	if (test_bit(V4L2_FL_USES_V4L2_FH, &video_dev->flags))
> +		vfh = file_to_v4l2_fh(file);
> +
> +	mutex_lock(&vv->vlock);
> +
> +	/*
> +	 * We need to handle a few ioctls manually because their results
> +	 * rely on vfd->tvnorms, which is normally updated by the driver
> +	 * as S_INPUT is called. Since we want to just pass these ioctls
> +	 * through, we have to hijack them from here.
> +	 */
> +	switch (cmd) {
> +	case VIDIOC_S_STD:
> +		ret = copy_from_user(&std_id, (void __user *)arg,
> +				     sizeof(std_id));
> +		if (ret) {
> +			ret = -EFAULT;
> +			break;
> +		}
> +		ret = virtio_media_s_std(file, vfh, std_id);
> +		break;
> +	case VIDIOC_ENUMSTD:
> +		ret = copy_from_user(&standard, (void __user *)arg,
> +				     sizeof(standard));
> +		if (ret) {
> +			ret = -EFAULT;
> +			break;
> +		}
> +		ret = virtio_media_enumstd(file, vfh, &standard);
> +		if (ret)
> +			break;
> +		ret = copy_to_user((void __user *)arg, &standard,
> +				   sizeof(standard));
> +		if (ret)
> +			ret = -EFAULT;
> +		break;
> +	case VIDIOC_QUERYSTD:
> +		ret = virtio_media_querystd(file, vfh, &std_id);
> +		if (ret)
> +			break;
> +		ret = copy_to_user((void __user *)arg, &std_id, sizeof(std_id));
> +		if (ret)
> +			ret = -EFAULT;
> +		break;
> +	default:
> +		ret = video_ioctl2(file, cmd, arg);
> +		break;
> +	}
> +
> +	mutex_unlock(&vv->vlock);
> +
> +	return ret;
> +}

[Severity: High]
Can intercepting VIDIOC_S_STD, VIDIOC_ENUMSTD, and VIDIOC_QUERYSTD before
video_ioctl2() cause a use-after-free or NULL pointer dereference when
racing with virtio_media_remove()?

CPU0                                  CPU1
v4l2_ioctl()
  video_is_registered(vdev) -> true
  virtio_media_device_ioctl()
                                      virtio_media_remove()
                                        video_unregister_device()
                                        mutex_lock(&vv->vlock)
                                        virtio_dev->config->del_vqs()
                                        mutex_unlock(&vv->vlock)
                                        virtio_media_session_free(s)
    mutex_lock(&vv->vlock)
    virtio_media_querystd()
      virtio_media_send_r_ioctl()     <-- NULL fh->vdev / UAF on session
                                          and deleted commandq

Unlike __video_do_ioctl(), which re-checks video_is_registered() after
taking the lock, these manual cases proceed directly into the handlers
after virtio_media_remove() has deleted vv->commandq and freed the
sessions.

Bypassing video_ioctl2() here also skips 32-bit compat translation for
VIDIOC_ENUMSTD32 (so 32-bit compat callers fall through to default and
fail because video_dev->tvnorms is 0), skips is_valid_ioctl() and
v4l2_prio_check() on VIDIOC_S_STD, and does not zero the output fields of
struct v4l2_standard after index on VIDIOC_ENUMSTD.

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

  reply	other threads:[~2026-11-21  3:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  2:50 [PATCH v12 0/6] media: add virtio-media driver Brian Daniels
2026-11-21  2:50 ` [PATCH v12 1/6] media: virtio: Add core driver and session management Brian Daniels
2026-11-21  3:13   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-11-21  3:15   ` sashiko-bot [this message]
2026-11-21  2:50 ` [PATCH v12 3/6] media: virtio: Add extended controls support Brian Daniels
2026-11-21  3:14   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-11-21  3:20   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 5/6] media: virtio: Add MMAP buffer mapping support Brian Daniels
2026-11-21  3:07   ` sashiko-bot
2026-11-21  2:50 ` [PATCH v12 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-11-21  3:17   ` 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-226565@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