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 6/6] media: virtio: clear the size of legacy controls built on the stack
Date: Tue, 01 Dec 2026 19:58:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-244138@kernel.org> (raw)
In-Reply-To: <20261201192957.790894-7-briandaniels@google.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] media: virtio: leak of framevecs array in
scatterlist_builder_free_framevecs()
- [High] media: virtio: DMA from kernel stack in
scatterlist_builder_add_data()
- [Medium] media: virtio: framevec leak on early error returns in ext
controls ioctl
- [Medium] media: virtio: out-of-bounds read in compat ioctl from
unvalidated ctrls->count
--
commit 6b7a4cc45d72b8a4c84b891deb82533c8d6fb1dd
Author: Nick Rogers <nick@getfieldwork.ai>
media: virtio: clear the size of legacy controls built on the stack
This commit zeroes the size field of stack-allocated v4l2_ext_control
entries passed from the V4L2 core for VIDIOC_G_CTRL and VIDIOC_S_CTRL
before forwarding them as extended control ioctls.
Link: https://lore.kernel.org/all/20260923160936.33445-1-nick@getfieldwork.ai/
> diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c
> index 99a6ef4f26e05..c1d5c9b166406 100644
> --- a/drivers/media/virtio/virtio_media_ioctls.c
> +++ b/drivers/media/virtio/virtio_media_ioctls.c
[ ... ]
> @@ -593,11 +594,31 @@ static int virtio_media_querycap(struct file *file, void *fh,
> * Extended control ioctls are handled mostly identically.
> */
>
> +/*
> + * VIDIOC_G_CTRL and VIDIOC_S_CTRL reach a driver without a control handler
> + * as one extended control the V4L2 core builds on its own stack, with its
> + * size left uninitialized. They carry a 32-bit value and never a payload,
> + * so a size there is stack garbage, which would be sent as a user pointer
> + * to copy and fail the ioctl. An array from userspace is never on the
> + * stack: the core copies it into an allocation.
> + */
> +static void virtio_media_legacy_ctrl(struct v4l2_ext_controls *ctrls)
> +{
> + u32 i;
> +
> + if (!ctrls->count || !object_is_on_stack(ctrls->controls))
> + return;
> + for (i = 0; i < ctrls->count; i++)
> + ctrls->controls[i].size = 0;
> +}
[Severity: High]
Does allowing stack-allocated ctrls and ctrls->controls through to
virtio_media_send_ext_controls_ioctl() cause DMA directly to and from the
kernel stack?
virtio_media_send_ext_controls_ioctl() passes ctrls and ctrls->controls to
scatterlist_builder_add_ext_ctrls() -> scatterlist_builder_add_data() and
reuses those descriptors for the device-writable response:
drivers/media/virtio/scatterlist_builder.c:scatterlist_builder_add_data() {
...
if (!always_use_shadow_buffer && virt_addr_valid(data) &&
virt_addr_valid(data + len)) {
...
} else if (!always_use_shadow_buffer && is_vmalloc_addr(data)) {
...
}
When always_use_shadow_buffer is false, stack addresses match
virt_addr_valid() (with CONFIG_VMAP_STACK=n) or is_vmalloc_addr() (with
CONFIG_VMAP_STACK=y) instead of falling through to builder->shadow_buffer.
Mapping stack pages into virtqueue scatterlists triggers check_for_stack()
errors under CONFIG_DMA_API_DEBUG and risks stack corruption on
non-coherent architectures. Checking !object_is_on_stack(data) in
scatterlist_builder_add_data() would force stack buffers through the
shadow buffer.
> static int virtio_media_g_ext_ctrls(struct file *file, void *fh,
> struct v4l2_ext_controls *ctrls)
> {
> struct v4l2_fh *vfh = file_to_v4l2_fh(file);
>
> + virtio_media_legacy_ctrl(ctrls);
> +
> return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_G_EXT_CTRLS,
> ctrls);
> }
[Severity: High]
Can virtio_media_send_ext_controls_ioctl() leak builder.framevecs when an
error occurs before the first userptr frame_vector is created?
scatterlist_builder_allocate_framevecs() allocates builder->framevecs
while builder->cur_framevec is 0, and builder->cur_framevec is only
incremented in __scatterlist_builder_add_userptr() after
vb2_create_framevec() and scatterlist setup succeed. If an earlier step
or the first vb2_create_framevec() call fails (for example, -EFAULT on an
invalid user pointer), cleanup jumps to free_framevecs:
drivers/media/virtio/scatterlist_builder.c:scatterlist_builder_free_framevecs() {
if (builder->cur_framevec && builder->framevecs) {
struct frame_vector *vec;
while (builder->cur_framevec--) {
vec = builder->framevecs[builder->cur_framevec];
vb2_destroy_framevec(vec, true);
}
kfree(builder->framevecs);
}
builder->framevecs = NULL;
}
Because kfree(builder->framevecs) is gated on builder->cur_framevec, the
framevecs array is leaked whenever cur_framevec is still 0.
> @@ -607,6 +628,8 @@ static int virtio_media_s_ext_ctrls(struct file *file, void *fh,
> {
> struct v4l2_fh *vfh = file_to_v4l2_fh(file);
>
> + virtio_media_legacy_ctrl(ctrls);
> +
> return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_S_EXT_CTRLS,
> ctrls);
> }
[Severity: Medium]
Do the post-command error checks in virtio_media_send_ext_controls_ioctl()
leak builder.framevecs and pinned user pages?
drivers/media/virtio/virtio_media_ioctls.c:virtio_media_send_ext_controls_ioctl() {
...
if (ctrls->count > num_ctrls)
return -ENOSPC;
if (ret < 0) {
...
goto free_framevecs;
}
resp_len -= sizeof(struct virtio_media_resp_ioctl);
/* Make sure that the reply's length covers our v4l2_ext_controls */
if (resp_len < sizeof(*ctrls))
return -EINVAL;
...
}
Returning -ENOSPC or -EINVAL directly bypasses free_framevecs, leaving any
frame_vector instances pinned by scatterlist_builder_add_ext_ctrls_userptrs()
and the builder.framevecs array unreleased.
> @@ -616,6 +639,8 @@ static int virtio_media_try_ext_ctrls(struct file *file, void *fh,
> {
> struct v4l2_fh *vfh = file_to_v4l2_fh(file);
>
> + virtio_media_legacy_ctrl(ctrls);
> +
> return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_TRY_EXT_CTRLS,
> ctrls);
> }
[Severity: Medium]
Can a device response with ctrls->count > num_ctrls trigger an
out-of-bounds read of array_buf during 32-bit compat ioctl copy-back?
In virtio_media_send_ext_controls_ioctl(), the ctrls->count > num_ctrls
check returns -ENOSPC without restoring ctrls->count to num_ctrls, and
when the shadow buffer is used, scatterlist_builder_retrieve_ext_ctrls()
copies ctrls from the shadow buffer after that check has already run.
Because VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, and VIDIOC_TRY_EXT_CTRLS
have INFO_FL_ALWAYS_COPY set, video_usercopy() still invokes
v4l2_compat_put_array_args() on error, which loops up to ctrls->count and
reads past the end of the num_ctrls-sized kernel buffer when copying back
to userspace.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201192957.790894-1-briandaniels@google.com?part=6
prev parent reply other threads:[~2026-12-01 19:58 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
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 [this message]
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-244138@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