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 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure
Date: Tue, 01 Dec 2026 19:53:17 +0000 [thread overview]
Message-ID: <sashiko-outbox-244130@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-3-briandaniels@google.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] media: virtio: DMA from stack in scatterlist_builder_add_data()
- [High] media: virtio: bypassed V4L2 checks and race in
virtio_media_device_ioctl()
- [High] media: virtio: UAF and NULL deref on remove with active ioctl
or open fd
- [Medium] media: virtio: missing NUL-termination of card in
virtio_media_querycap()
- [Medium] media: virtio: uninitialized stack vars in
virtio_media_g_input()/g_output()
--
commit f60ebbdd7d1cb93400c55ed40c7ea0c14f198b90
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, implementing VIDIOC_QUERYCAP and
fixed-size V4L2 ioctls for virtio-media devices.
> diff --git a/drivers/media/virtio/scatterlist_builder.c b/drivers/media/virtio/scatterlist_builder.c
> new file mode 100644
> index 0000000000000..69aeb4737f1e4
> --- /dev/null
> +++ b/drivers/media/virtio/scatterlist_builder.c
[ ... ]
> +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]
Can scatterlist_builder_add_data() map kernel stack memory directly for
DMA when always_use_shadow_buffer is false?
Several callers pass stack buffers as data: virtio_media_g_input(),
virtio_media_s_input(), virtio_media_g_output(), virtio_media_s_output(),
virtio_media_s_std(), and virtio_media_device_ioctl() pass local stack
variables, and video_usercopy() passes its 128-byte stack buffer sbuf for
any V4L2 ioctl whose argument fits in 128 bytes. Because neither branch
excludes object_is_on_stack(data), direct-mapped stacks match
virt_addr_valid() and vmap stacks match is_vmalloc_addr() instead of
falling back to builder->shadow_buffer. DMA on the kernel stack violates
the DMA API contract, triggers check_for_stack() warnings under
CONFIG_DMA_API_DEBUG, and risks stack corruption on non-cache-coherent
architectures while the caller sleeps waiting for the virtqueue response.
Also, virt_addr_valid(data + len) checks one byte past the end of the
buffer rather than virt_addr_valid(data + len - 1).
[ ... ]
> 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
[ ... ]
> +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));
[Severity: Medium]
Can virtio_media_querycap() return a non-NUL-terminated cap->card string
to userspace?
struct virtio_media_config in include/uapi/linux/virtio_media.h specifies
that card[32] does not need to be NUL-terminated when all 32 bytes are
used, whereas V4L2 requires struct v4l2_capability.card to always be
NUL-terminated. Reading all 32 bytes without setting
cap->card[sizeof(cap->card) - 1] = '\0' leaves cap->card unterminated when
the host uses the full field.
> +
> + cap->capabilities = video_dev->device_caps | V4L2_CAP_DEVICE_CAPS;
> + cap->device_caps = video_dev->device_caps;
> +
> + return 0;
> +}
> +
> +/*
> + * 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;
> +}
> +
> +static int virtio_media_s_input(struct file *file, void *fh, unsigned int i)
> +{
> + u32 input = i;
> +
> + return virtio_media_send_wr_ioctl(file_to_v4l2_fh(file),
> + VIDIOC_S_INPUT, &input,
> + sizeof(input), sizeof(input));
> +}
> +
> +static int virtio_media_g_output(struct file *file, void *fh, unsigned int *o)
> +{
> + u32 output;
> +
> + int ret = virtio_media_send_r_ioctl(file_to_v4l2_fh(file),
> + VIDIOC_G_OUTPUT, &output,
> + sizeof(output));
[Severity: Medium]
Does passing uninitialized input and output variables in
virtio_media_g_input() and virtio_media_g_output() copy uninitialized
kernel stack bytes into the shadow buffer?
When scatterlist_builder_add_data() takes the shadow buffer path, it runs
memcpy(shadow_buffer, data, len) even for device-writable response
buffers. Because input and output are not initialized, 4 bytes of kernel
stack memory are copied into session->shadow_buf (and returned to
userspace via *i or *o if the device reports a sufficient response length
without overwriting all 4 bytes).
[ ... ]
> +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);
[Severity: High]
Can virtio_media_device_ioctl() race with virtio_media_remove() and cause
a use-after-free or NULL pointer dereference?
v4l2_ioctl() checks video_is_registered(vdev) before calling
virtio_media_device_ioctl(), which then blocks on mutex_lock(&vv->vlock)
without re-checking video_is_registered(video_dev) once the lock is
acquired:
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)
virtio_media_session_fh_del(s)
sg_free_table(&s->command_sgs)
kfree(s->shadow_buf)
kfree(s)
mutex_lock(&vv->vlock)
virtio_media_s_std()
virtio_media_send_w_ioctl() <-- NULL deref / use-after-free
Once virtio_media_remove() releases vv->vlock and frees the sessions, the
waiting ioctl proceeds with file->private_data cleared by v4l2_fh_del(),
session->command_sgs and session->shadow_buf freed, and vv->commandq
deleted. Furthermore, when userspace later closes the open file
descriptor, v4l2_release() invokes virtio_media_device_close(), which
dereferences file->private_data again.
> +
> + /*
> + * 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) {\n> + 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;
[Severity: High]
Does intercepting VIDIOC_S_STD, VIDIOC_ENUMSTD, and VIDIOC_QUERYSTD before
video_ioctl2() bypass V4L2 core validation and break 32-bit compat
VIDIOC_ENUMSTD32?
Handling these commands directly skips several checks that video_usercopy()
and __video_do_ioctl() perform:
1. test_bit(_IOC_NR(cmd), video_dev->valid_ioctls) is skipped, so these
ioctls are forwarded to the host even on device types or directions
where determine_valid_ioctls() disabled them and expects -ENOTTY.
2. v4l2_prio_check(video_dev->prio, vfh->prio) is skipped for
VIDIOC_S_STD (which carries INFO_FL_PRIO), allowing a lower-priority
file descriptor to change the standard while another holds
V4L2_PRIORITY_RECORD.
3. VIDIOC_ENUMSTD has INFO_FL_CLEAR(v4l2_standard, index), so
video_get_user() zeroes all fields after index. Copying the entire
struct v4l2_standard from userspace forwards uninitialized userspace
bytes in the output and reserved fields to the host.
4. Under CONFIG_COMPAT, v4l2_compat_ioctl32() passes VIDIOC_ENUMSTD32 to
unlocked_ioctl, relying on video_usercopy() to translate it to
VIDIOC_ENUMSTD. Because the switch only matches VIDIOC_ENUMSTD,
32-bit compat calls fall through to video_ioctl2() -> v4l_enumstd() and
fail with -EINVAL because video_dev->tvnorms is 0.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201192957.790894-1-briandaniels@google.com?part=2
next prev parent reply other threads:[~2026-12-01 19:53 UTC|newest]
Thread overview: 14+ 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 [this message]
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
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
[not found] ` <6b0f2dd7.af12d3bd.2ac852.b195@mx.google.com>
2026-12-01 21:10 ` [v13,0/6] media: add virtio-media driver 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-244130@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.