DRM_IOCTL_VIRTGPU_EXECBUFFER accepts a user-controlled command size and
stores the command in vmalloc memory. virtio-gpu maps each command page to
one SG entry, then adds a request and response entry before submitting the
chain to the control virtqueue.
With indirect descriptors negotiated, the driver accounts for the chain as
one free ring descriptor. An unprivileged render client can therefore
submit more SG entries than the device Queue Size. A sufficiently large
command also exceeds the u16 index range of a split indirect table, causing
the final SG entries to replace the request and first command descriptors
after the next index wraps.
Reject commands whose page count plus the fixed request and response
exceeds the actual control Queue Size. Perform the check before command
copy, object reservation, fence allocation or queue publication.
This also avoids relying on the current queue error path:
virtio_gpu_queue_ctrl_sgs() only warns when virtqueue_add_sgs() fails, then
advances sequence and pending-command state and returns success.
In a no-vIOMMU QEMU/virgl guest, the malformed chain let a render client
substitute RESOURCE_ATTACH_BACKING, obtain guest physical read/write and
gain initial-namespace uid/euid 0. On a 256-entry control queue, 254
command pages continue to complete and 255 pages now return E2BIG.
AI tools assisted the source audit, reproducer construction and patch
drafting.
Fixes: 5edbb5608256 ("drm/virtio: fix ring free check")
Reported-by: Hyeonjun Hong <[email protected]>
Reported-by: GEONWOO HAN <[email protected]>
Cc: [email protected]
Signed-off-by: Seojin An <[email protected]>
---
drivers/gpu/drm/virtio/virtgpu_submit.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
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;
base-commit: 479e381e77dcde0dfcc9d51e89ff3273da199804
--
2.43.0