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 6/6] media: virtio: clear the size of legacy controls built on the stack
Date: Sat, 21 Nov 2026 03:17:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-226567@kernel.org> (raw)
In-Reply-To: <20261121025007.3445723-7-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] media: virtio: framevecs array leak in
  scatterlist_builder_free_framevecs()
- [Medium] media: virtio: incomplete stack control init in
  virtio_media_legacy_ctrl()
- [Medium] media: virtio: framevec and pinned page leaks in
  virtio_media_send_ext_controls_ioctl()
--

commit 623bf5e3612ed0e69ea0cdb41218ea9e8d7b62ef
Author: Nick Rogers <nick@getfieldwork.ai>

media: virtio: clear the size of legacy controls built on the stack

This commit adds a helper that checks if the extended control array is on
the kernel stack and zeroes each control's size field before forwarding
extended control ioctls to the VirtIO host.

> diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c
> index aa56efe9a08c2..ad240073aec9f 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: Medium]
Does clearing only ctrls->controls[i].size still leave uninitialized
kernel stack fields that get sent across the virtqueue to the host?

When v4l_g_ctrl() and v4l_s_ctrl() build struct v4l2_ext_controls and
struct v4l2_ext_control on the stack without zero-initializing them,
ctrls->error_idx, ctrls->request_fd, struct alignment padding, and the
upper 32 bits of the 8-byte value64 union in ctrl remain uninitialized.
scatterlist_builder_add_ext_ctrls() transmits the entire sizeof(*ctrls)
and sizeof(ctrls->controls[0]) * ctrls->count buffers to the host,
leaking guest kernel stack bytes and passing stack garbage in request_fd,
error_idx, and the upper 32 bits of value64.

Also, detecting legacy controls in the driver with object_is_on_stack()
(including in virtio_media_try_ext_ctrls(), which VIDIOC_G_CTRL and
VIDIOC_S_CTRL never call) works around v4l_g_ctrl() and v4l_s_ctrl() in
drivers/media/v4l2-core/v4l2-ioctl.c rather than zero-initializing the
stack structures there.

> +
>  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: Medium]
In virtio_media_send_ext_controls_ioctl() (introduced earlier in this
series in commit "media: virtio: Add extended controls support"), do the
two post-command validation checks leak builder.framevecs and leave user
pages permanently pinned?

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;
    ...
free_framevecs:
	scatterlist_builder_free_framevecs(&builder);
	return ret;
}

When a request includes controls with ctrl->size > 0, returning -ENOSPC
or -EINVAL directly after virtio_media_send_command() skips
free_framevecs, leaking builder.framevecs and never calling
vb2_destroy_framevec() on the pinned user buffers.

>  
>  static int virtio_media_s_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_S_EXT_CTRLS,
>  						    ctrls);
>  }

[Severity: High]
Also in the free_framevecs path of virtio_media_send_ext_controls_ioctl()
(from commit "media: virtio: Add extended controls support"), does
scatterlist_builder_free_framevecs() leak builder->framevecs when an
error occurs before the first framevec is created?

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

scatterlist_builder_allocate_framevecs() allocates builder->framevecs
whenever any control has ctrl->size > 0, while builder->cur_framevec is
still 0.  If an unprivileged user passes an invalid ctrl->ptr on the
first payload control, vb2_create_framevec() fails with cur_framevec == 0
and scatterlist_builder_free_framevecs() skips kfree(builder->framevecs)
before clearing the pointer.

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

      reply	other threads:[~2026-11-21  3:17 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
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 [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-226567@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