hw/display/virtio-gpu: cap submit_3d command buffer allocation
PATCH v2 1/1 hw/display/virtio-gpu: cap submit_3d command buffer allocation
From: Marc-André Lureau <[email protected]>
Both virgl_cmd_submit_3d() and rutabaga_cmd_submit_3d() pass the guest-controlled cs.size directly to malloc() without bounds checking. A malicious guest can set cs.size to an arbitrarily large value, causing an OOM abort that crashes the QEMU process.
Checking cs.size against the descriptor payload length (iov_size) is not
sufficient: indirect descriptor tables can repeat entries aliasing the same guest-physical range, inflating iov_size() to nearly 4 GiB while referring to only a small amount of unique memory.
Instead, cap cs.size at 4 MiB. With 4 KiB pages and QEMU's VIRTQUEUE_MAX_SIZE (1024) mapped-iov limit, the Linux virtio driver cannot carry more than ~4 MiB of inline command data, so legitimate submissions are unaffected.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3776
--- contrib/vhost-user-gpu/vugpu.h | 8 ++++++++ include/hw/virtio/virtio-gpu.h | 8 ++++++++ contrib/vhost-user-gpu/virgl.c | 7 +++++++ hw/display/virtio-gpu-rutabaga.c | 8 ++++++++ hw/display/virtio-gpu-virgl.c | 8 ++++++++ 5 files changed, 39 insertions(+)
contrib/vhost-user-gpu/vugpu.h +8 -0
@@ -163,6 +163,14 @@ struct virtio_gpu_ctrl_command {QTAILQ_ENTRY (virtio_gpu_ctrl_command) next; }; +/* +* With 4 KiB pages and QEMU's VIRTQUEUE_MAX_SIZE (1024) mapped-iov +* limit, the largest inline command is ~4 MiB. Cap submit_3d +* allocations to this value to prevent a malicious guest from +* triggering an OOM abort via an inflated cs.size field. +*/ +#define VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE (4 * 1024 * 1024) +#define VUGPU_FILL_CMD (out) do { \ size_t vugpufillcmd_s_ = \ iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
include/hw/virtio/virtio-gpu.h +8 -0
@@ -299,6 +299,14 @@ struct VirtIOGPURutabaga {struct rutabaga *rutabaga; }; +/* +* With 4 KiB pages and QEMU's VIRTQUEUE_MAX_SIZE (1024) mapped-iov +* limit, the largest inline command is ~4 MiB. Cap submit_3d +* allocations to this value to prevent a malicious guest from +* triggering an OOM abort via an inflated cs.size field. +*/ +#define VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE (4 * 1024 * 1024) +#define VIRTIO_GPU_FILL_CMD (out) do { \ size_t virtiogpufillcmd_s_ = \ iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \
contrib/vhost-user-gpu/virgl.c +7 -0
@@ -202,6 +202,13 @@ virgl_cmd_submit_3d(VuGpu *g,VUGPU_FILL_CMD (cs); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +g_critical ("%s: command buffer too large (%u)" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_malloc (cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);
hw/display/virtio-gpu-rutabaga.c +8 -0
@@ -351,6 +351,14 @@ rutabaga_cmd_submit_3d(VirtIOGPU *g,VIRTIO_GPU_FILL_CMD (cs);trace_virtio_gpu_cmd_ctx_submit (cs.hdr.ctx_id, cs.size); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: command buffer too large (%u)\n" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_new0 (uint8_t, cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);
hw/display/virtio-gpu-virgl.c +8 -0
@@ -607,6 +607,14 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g,VIRTIO_GPU_FILL_CMD (cs);trace_virtio_gpu_cmd_ctx_submit (cs.hdr.ctx_id, cs.size); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: command buffer too large (%u)\n" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_malloc (cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);
Signature · 1 line
2.55.0
Quoted 36 lines · On 2026/07/16 19:50, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Both
virgl_cmd_submit_3d()andrutabaga_cmd_submit_3d()pass the guest-controlled cs.size directly tomalloc()without bounds checking. A malicious guest can set cs.size to an arbitrarily large value, causing an OOM abort that crashes the QEMU process.Checking cs.size against the descriptor payload length (
iov_size) is not
sufficient: indirect descriptor tables can repeat entries aliasing the same guest-physical range, inflatingiov_size()to nearly 4 GiB while referring to only a small amount of unique memory.Instead, cap cs.size at 4 MiB. With 4 KiB pages and QEMU's
VIRTQUEUE_MAX_SIZE(1024) mapped-iov limit, the Linux virtio driver cannot carry more than ~4 MiB of inline command data, so legitimate submissions are unaffected.Fixes 9d9e152136bd ("virtio-gpu: add 3d mode and virgl rendering support.")Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")Fixes d52c454aadc ("contrib: add vhost-user-gpu")Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3776
Reported-by [email protected]Signed-off-by Marc-André Lureau <[email protected]>--- contrib/vhost-user-gpu/vugpu.h | 8 ++++++++ include/hw/virtio/virtio-gpu.h | 8 ++++++++ contrib/vhost-user-gpu/virgl.c | 7 +++++++ hw/display/virtio-gpu-rutabaga.c | 8 ++++++++ hw/display/virtio-gpu-virgl.c | 8 ++++++++ 5 files changed, 39 insertions(+)contrib/vhost-user-gpu/vugpu.h +0
-0
contrib/vhost-user-gpu/vugpu.h struct virtio_gpu_ctrl_command { · line 172
Quoted 10 lines
contrib/vhost-user-gpu/vugpu.h +7
-0@@ -163,6 +163,14 @@ struct virtio_gpu_ctrl_command {QTAILQ_ENTRY (virtio_gpu_ctrl_command) next; }; +/* +* With 4 KiB pages and QEMU's VIRTQUEUE_MAX_SIZE (1024) mapped-iov +* limit, the largest inline command is ~4 MiB. Cap submit_3d +* allocations to this value to prevent a malicious guest from +* triggering an OOM abort via an inflated cs.size field. +*/ +#define VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE (4 * 1024 * 1024)
One nit I failed to point out earlier: please use the "MiB" macro. With that addressed:
Regards,
Akihiko Odaki
Quoted 79 lines
contrib/vhost-user-gpu/vugpu.h +1
-0+#define VUGPU_FILL_CMD (out) do { \ size_t vugpufillcmd_s_ = \ iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \include/hw/virtio/virtio-gpu.h +8
-0@@ -299,6 +299,14 @@ struct VirtIOGPURutabaga {struct rutabaga *rutabaga; }; +/* +* With 4 KiB pages and QEMU's VIRTQUEUE_MAX_SIZE (1024) mapped-iov +* limit, the largest inline command is ~4 MiB. Cap submit_3d +* allocations to this value to prevent a malicious guest from +* triggering an OOM abort via an inflated cs.size field. +*/ +#define VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE (4 * 1024 * 1024) +#define VIRTIO_GPU_FILL_CMD (out) do { \ size_t virtiogpufillcmd_s_ = \ iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, 0, \contrib/vhost-user-gpu/virgl.c +7
-0@@ -202,6 +202,13 @@ virgl_cmd_submit_3d(VuGpu *g,VUGPU_FILL_CMD (cs); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +g_critical ("%s: command buffer too large (%u)" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_malloc (cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);hw/display/virtio-gpu-rutabaga.c +8
-0@@ -351,6 +351,14 @@ rutabaga_cmd_submit_3d(VirtIOGPU *g,VIRTIO_GPU_FILL_CMD (cs);trace_virtio_gpu_cmd_ctx_submit (cs.hdr.ctx_id, cs.size); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: command buffer too large (%u)\n" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_new0 (uint8_t, cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);hw/display/virtio-gpu-virgl.c +8
-0@@ -607,6 +607,14 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g,VIRTIO_GPU_FILL_CMD (cs);trace_virtio_gpu_cmd_ctx_submit (cs.hdr.ctx_id, cs.size); +if (cs.size >VIRTIO_GPU_MAX_CMD_SUBMIT_SIZE ) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: command buffer too large (%u)\n" , + __func__, cs.size); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + buf =g_malloc (cs.size); s =iov_to_buf (cmd->elem.out_sg, cmd->elem.out_num,sizeof (cs), buf, cs.size);