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 <[email protected]>

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/[email protected]?part=6

Reply via email to