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 6/6] media: virtio: clear the size of legacy controls built on the stack
Date: Fri, 20 Nov 2026 22:25:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-226259@kernel.org> (raw)
In-Reply-To: <20261120215916.3149345-7-briandaniels@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] media: virtio: incomplete stack initialization in
  virtio_media_legacy_ctrl()
--

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

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

This commit adds virtio_media_legacy_ctrl() to zero out the size field of
stack-allocated v4l2_ext_control entries 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;
> +}
> +
>  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);
>  }
> @@ -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);
>  }
> @@ -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]
Does clearing only controls[i].size in virtio_media_legacy_ctrl() still leave
other fields of ctrls and ctrls->controls[i] uninitialized on the stack?

In v4l_g_ctrl() and v4l_s_ctrl(), both struct v4l2_ext_controls ctrls and
struct v4l2_ext_control ctrl are declared on the stack without
zero-initialization, and only ctrls.which, ctrls.count, ctrls.controls,
ctrl.id, and the 32-bit ctrl.value member are assigned (with check_ext_ctrls()
clearing ctrls.reserved[0] and ctrl.reserved2[0]).  That leaves
ctrls->error_idx, ctrls->request_fd, struct padding, and the upper 32 bits of
the 64-bit union in ctrl.value64 holding stack garbage when
scatterlist_builder_add_ext_ctrls() sends both full structures to the host.
In addition to leaking 16 bytes of guest kernel stack to the host on every
VIDIOC_G_CTRL and VIDIOC_S_CTRL call, a VIDIOC_S_CTRL targeting a 64-bit host
control will pass stack garbage in the upper 32 bits of ctrl.value64.

Also, VIDIOC_G_CTRL and VIDIOC_S_CTRL never call vidioc_try_ext_ctrls, so
virtio_media_try_ext_ctrls() has no legacy stack-allocated caller.  Rather
than detecting stack pointers with object_is_on_stack() in the driver, could
v4l_g_ctrl() and v4l_s_ctrl() in drivers/media/v4l2-core/v4l2-ioctl.c
zero-initialize ctrls and ctrl directly?

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

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

Thread overview: 13+ 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 22:19   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 2/6] media: virtio: Add V4L2 ioctl forwarding infrastructure Brian Daniels
2026-11-20 22:25   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 3/6] media: virtio: Add extended controls support Brian Daniels
2026-11-20 22:23   ` sashiko-bot
2026-11-20 21:59 ` [PATCH v11 4/6] media: virtio: Add buffer streaming, event handling, and poll Brian Daniels
2026-11-20 22:21   ` sashiko-bot
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
2026-11-20 21:59 ` [PATCH v11 6/6] media: virtio: clear the size of legacy controls built on the stack Brian Daniels
2026-11-20 22:25   ` 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-226259@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.