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

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

Reply via email to