* [PATCH] drm/virtio: bound execbuffer commands to control queue size
@ 2026-11-26 8:00 Seojin An
2026-11-26 8:22 ` sashiko-bot
0 siblings, 1 reply; 4+ messages in thread
From: Seojin An @ 2026-11-26 8:00 UTC (permalink / raw)
To: David Airlie, Gerd Hoffmann, Dmitry Osipenko
Cc: Gurchetan Singh, Chia-I Wu, dri-devel, virtualization, security
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 <hyeonjunhong330@gmail.com>
Reported-by: GEONWOO HAN <gunwoo9643@uos.ac.kr>
Cc: stable@vger.kernel.org
Signed-off-by: Seojin An <love09010224@uos.ac.kr>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/virtio: bound execbuffer commands to control queue size
2026-11-26 8:00 [PATCH] drm/virtio: bound execbuffer commands to control queue size Seojin An
@ 2026-11-26 8:22 ` sashiko-bot
2026-11-26 14:38 ` [PATCH v2] " Seojin An
0 siblings, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-26 8:22 UTC (permalink / raw)
To: Seojin An; +Cc: dri-devel, virtualization, Michael S. Tsirkin, Eugenio Perez
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 <love09010224@uos.ac.kr>
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/20261126080044.2898223-1-love09010224@uos.ac.kr?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] drm/virtio: bound execbuffer commands to control queue size
2026-11-26 8:22 ` sashiko-bot
@ 2026-11-26 14:38 ` Seojin An
2026-11-26 14:47 ` sashiko-bot
0 siblings, 1 reply; 4+ messages in thread
From: Seojin An @ 2026-11-26 14:38 UTC (permalink / raw)
To: David Airlie, Gerd Hoffmann, Dmitry Osipenko
Cc: Gurchetan Singh, Chia-I Wu, Michael S. Tsirkin,
Eugenio Perez Martin, dri-devel, virtualization, linux-kernel,
security
DRM_IOCTL_VIRTGPU_EXECBUFFER accepts a user-controlled command size and
copies the command with vmemdup_user(). For a vmalloc-backed command,
virtio-gpu maps each 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
split-ring command wraps the indirect table's u16 next index. The device
then consumes a chain different from the one constructed by the driver.
Reject command sizes whose vmalloc page count plus the fixed request and
response exceeds the actual control Queue Size. This execbuffer check does
not depend on the ring layout and applies to both split and packed
virtqueues. The driver creates one command SG entry per page even when
adjacent pages are physically contiguous. Read the queue size inside a
drm_dev_enter()/drm_dev_exit() section so device unplug cannot free the
virtqueue during the lookup.
virtio_gpu_queue_ctrl_sgs() takes a separate lifetime reference for the
later queue operation.
Perform the size check before command copy, object reservation, fence
allocation or queue publication. Oversized execbuffers then cannot reach a
pre-existing error path where virtio_gpu_queue_ctrl_sgs() warns when
virtqueue_add_sgs() fails, 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 and obtain guest physical read/write.
The client then gained initial-namespace uid/euid 0. On a 256-entry control
queue, 254 command pages continue to complete and 255 pages 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 <hyeonjunhong330@gmail.com>
Link: https://lore.kernel.org/all/20261126080044.2898223-1-love09010224@uos.ac.kr/
Reported-by: GEONWOO HAN <gunwoo9643@uos.ac.kr>
Link: https://lore.kernel.org/all/20261126080044.2898223-1-love09010224@uos.ac.kr/
Suggested-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
Suggested-by: GEONWOO HAN <gunwoo9643@uos.ac.kr>
Cc: stable@vger.kernel.org
Signed-off-by: Seojin An <love09010224@uos.ac.kr>
---
Changes in v2:
- Keep this change in virtio-gpu. Maintainer review concluded with no
virtio-ring change for now.
- Clarify that the driver-side limit covers split and packed virtqueues.
- Protect the control-virtqueue size lookup with
drm_dev_enter()/drm_dev_exit() so it cannot race virtqueue teardown
during device unplug.
v1: https://lore.kernel.org/all/20261126080044.2898223-1-love09010224@uos.ac.kr/
tools/virtio exercises virtio_ring, not virtio_gpu_execbuffer_ioctl(), so it
does not cover this driver-side check. A rejection test in that harness would
instead test the core rule that maintainers chose not to add for now. The
Queue Size boundary and exploit-negative case were tested in a QEMU
virtio-gpu guest.
drivers/gpu/drm/virtio/virtgpu_submit.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c
index 6580ece658704..64d585bfdf626 100644
--- a/drivers/gpu/drm/virtio/virtgpu_submit.c
+++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
@@ -482,7 +482,10 @@ 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 idx;
int ret = -EINVAL;
if (!vgdev->has_virgl_3d)
@@ -491,6 +494,20 @@ 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.
+ */
+ command_sgs = DIV_ROUND_UP((u64)exbuf->size, PAGE_SIZE);
+ if (!drm_dev_enter(dev, &idx))
+ return -ENODEV;
+ max_sgs = virtqueue_get_vring_size(vgdev->ctrlq.vq);
+ drm_dev_exit(idx);
+ 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;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] drm/virtio: bound execbuffer commands to control queue size
2026-11-26 14:38 ` [PATCH v2] " Seojin An
@ 2026-11-26 14:47 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-26 14:47 UTC (permalink / raw)
To: Seojin An; +Cc: virtualization, Michael S. Tsirkin, Eugenio Perez
> DRM_IOCTL_VIRTGPU_EXECBUFFER accepts a user-controlled command size and
> copies the command with vmemdup_user(). For a vmalloc-backed command,
> virtio-gpu maps each 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
> split-ring command wraps the indirect table's u16 next index. The device
> then consumes a chain different from the one constructed by the driver.
>
> Reject command sizes whose vmalloc page count plus the fixed request and
> response exceeds the actual control Queue Size. This execbuffer check does
> not depend on the ring layout and applies to both split and packed
> virtqueues. The driver creates one command SG entry per page even when
> [ ... ]
> Link: https://lore.kernel.org/all/20261126080044.2898223-1-love09010224@uos.ac.kr/
> Suggested-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
> Suggested-by: GEONWOO HAN <gunwoo9643@uos.ac.kr>
> Cc: stable@vger.kernel.org
> Signed-off-by: Seojin An <love09010224@uos.ac.kr>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126143800.3493685-1-love09010224@uos.ac.kr?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 14:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 8:00 [PATCH] drm/virtio: bound execbuffer commands to control queue size Seojin An
2026-11-26 8:22 ` sashiko-bot
2026-11-26 14:38 ` [PATCH v2] " Seojin An
2026-11-26 14:47 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox