Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/virtio: UAF of ctrlq.vq in virtio_gpu_execbuffer_ioctl()
  on unplug
--

commit 03e41369bd07f481a8b25bccdf4ab65b96d730a3
Author: Seojin An <[email protected]>

drm/virtio: bound execbuffer commands to control queue size

This commit rejects execbuffer commands whose page count plus the request and
response descriptors exceeds the control virtqueue size.

> diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c 
> b/drivers/gpu/drm/virtio/virtgpu_submit.c
> index 6580ece658704..9aff7f8d5195f 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_submit.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
> @@ -482,6 +482,8 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, 
> void *data,
>       u64 fence_ctx = vgdev->fence_drv.context;
>       struct drm_virtgpu_execbuffer *exbuf = data;
>       struct virtio_gpu_submit submit;
> +     u64 command_sgs;
> +     unsigned int max_sgs;
>       u32 ring_idx = 0;
>       int ret = -EINVAL;
>  
> @@ -491,6 +493,17 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, 
> void *data,
>       if (exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS)
>               return ret;
>  
> +     /*
> +      * The command contributes one SG entry per vmalloc page in addition
> +      * to the fixed request and response entries.  Virtio requires both
> +      * direct and indirect descriptor chains to fit the device queue.
> +      * Reject oversized commands before copying or queueing them.
> +      */
> +     max_sgs = virtqueue_get_vring_size(vgdev->ctrlq.vq);
> +     command_sgs = DIV_ROUND_UP((u64)exbuf->size, PAGE_SIZE);
> +     if (max_sgs < 2 || command_sgs > max_sgs - 2)
> +             return -E2BIG;
> +
>       if (exbuf->flags & VIRTGPU_EXECBUF_RING_IDX) {
>               if (exbuf->ring_idx >= vfpriv->num_rings)
>                       return ret;

[Severity: Medium]
Can virtqueue_get_vring_size() dereference vgdev->ctrlq.vq after it has been
freed on device unplug?

drm_ioctl_kernel() checks drm_dev_is_unplugged(), which enters and immediately
exits its SRCU section before calling virtio_gpu_execbuffer_ioctl().  Because
other virtqueue paths such as virtio_gpu_queue_ctrl_sgs() guard vgdev->ctrlq.vq
with drm_dev_enter(), an in-flight ioctl can race with virtio_gpu_remove():

CPU0                                  CPU1
drm_ioctl_kernel()
  drm_dev_is_unplugged()
                                      virtio_gpu_remove()
                                        drm_dev_unplug()
                                        virtio_gpu_deinit()
                                          del_vqs()
                                            kfree(vq)
  virtio_gpu_execbuffer_ioctl()
    virtqueue_get_vring_size(ctrlq.vq)  <-- use-after-free

Guarding the virtqueue_get_vring_size() call with drm_dev_enter() and
drm_dev_exit() would prevent this race.

[Severity: High]
This problem wasn't introduced by this patch, but virtio_gpu_cmd_submit() has
a void return type and drops errors from [1]
virtio_gpu_queue_fenced_ctrl_buffer().

When queueing fails due to memory pressure or device unplug,
virtio_gpu_fence_emit() is never called and the fence stays at seqno 0, yet
virtio_gpu_execbuffer_ioctl() continues to virtio_gpu_install_out_fence_fd()
and exports the uninitialized fence to userspace.  Any client waiting on that
fence file descriptor hangs indefinitely, and checking the fence triggers
WARN_ON_ONCE(f->seqno == 0) in virtio_gpu_fence_signaled().

[1] https://sashiko.dev/#/bug/linux-c280d0dd-d017-4ef7-a36a-c0606306f02c

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to