Cursor pixel data is copied as 32bpp, but the resource format is never
checked. virgl_renderer_get_cursor_data() sizes its buffer for the
resource's own format, while vg_virgl_update_cursor_data() copies
64 * 64 * 4 bytes from it: a 64x64 R8_UNORM resource gives a 4096 byte
buffer, so 12 KiB past its end is read and handed to the display
backend as cursor pixels.

Look up the resource format and refuse anything that is not 32bpp,
matching the virtio-gpu backends.

While at it, let g_autofree release the buffer: the 64x64 check returned
before it was freed, letting a guest grow the backend's memory with
repeated UPDATE_CURSOR.

Fixes: CVE-2026-122027
Fixes: d52c454aadcd ("contrib: add vhost-user-gpu")
Reported-by: Fernando Bortotti <https://gitlab.com/fernando.bortotti>
Signed-off-by: Marc-André Lureau <[email protected]>
---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 31 ++++++++++++++++++++-----------
 contrib/vhost-user-gpu/virgl.c          | 25 ++++++++++++++++++-------
 contrib/vhost-user-gpu/virgl.h          |  2 +-
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/contrib/vhost-user-gpu/vhost-user-gpu.c 
b/contrib/vhost-user-gpu/vhost-user-gpu.c
index f70b34c179f3..b34a1e013b8b 100644
--- a/contrib/vhost-user-gpu/vhost-user-gpu.c
+++ b/contrib/vhost-user-gpu/vhost-user-gpu.c
@@ -949,19 +949,22 @@ vg_handle_ctrl(VuDev *dev, int qidx)
     }
 }
 
-static void
+static bool
 update_cursor_data_simple(VuGpu *g, uint32_t resource_id, gpointer data)
 {
     struct virtio_gpu_simple_resource *res;
 
     res = virtio_gpu_find_resource(g, resource_id);
-    g_return_if_fail(res != NULL);
-    g_return_if_fail(pixman_image_get_width(res->image) == 64);
-    g_return_if_fail(pixman_image_get_height(res->image) == 64);
-    g_return_if_fail(
-        PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) == 32);
+    if (!res || !res->image ||
+        pixman_image_get_width(res->image) != 64 ||
+        pixman_image_get_height(res->image) != 64 ||
+        PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) != 32) {
+        g_debug("%s: invalid cursor resource %u", G_STRFUNC, resource_id);
+        return false;
+    }
 
     memcpy(data, pixman_image_get_data(res->image), 64 * 64 * 
sizeof(uint32_t));
+    return true;
 }
 
 static void
@@ -984,6 +987,7 @@ vg_process_cursor_cmd(VuGpu *g, struct 
virtio_gpu_update_cursor *cursor)
         break;
     }
     case VIRTIO_GPU_CMD_UPDATE_CURSOR: {
+        bool ok = true;
         VhostUserGpuMsg msg = {
             .request = VHOST_USER_GPU_CURSOR_UPDATE,
             .size = sizeof(VhostUserGpuCursorUpdate),
@@ -998,12 +1002,17 @@ vg_process_cursor_cmd(VuGpu *g, struct 
virtio_gpu_update_cursor *cursor)
             }
         };
         g_debug("%s: update", G_STRFUNC);
-        if (g->virgl) {
-            vg_virgl_update_cursor_data(g, cursor->resource_id,
-                                        msg.payload.cursor_update.data);
+        if (!cursor->resource_id) {
+            /* no resource: send a transparent cursor */
+        } else if (g->virgl) {
+            ok = vg_virgl_update_cursor_data(g, cursor->resource_id,
+                                             msg.payload.cursor_update.data);
         } else {
-            update_cursor_data_simple(g, cursor->resource_id,
-                                      msg.payload.cursor_update.data);
+            ok = update_cursor_data_simple(g, cursor->resource_id,
+                                           msg.payload.cursor_update.data);
+        }
+        if (!ok) {
+            break;
         }
         vg_send_msg(g, &msg, -1);
         break;
diff --git a/contrib/vhost-user-gpu/virgl.c b/contrib/vhost-user-gpu/virgl.c
index 0ef4b9d8c903..8af5a456ee5b 100644
--- a/contrib/vhost-user-gpu/virgl.c
+++ b/contrib/vhost-user-gpu/virgl.c
@@ -14,24 +14,35 @@
 
 #include "qemu/osdep.h"
 #include <virglrenderer.h>
+#include "hw/virtio/virtio-gpu-pixman.h"
 #include "virgl.h"
 
 #include <epoxy/gl.h>
 
-void
+bool
 vg_virgl_update_cursor_data(VuGpu *g, uint32_t resource_id,
                             gpointer data)
 {
-    uint32_t width, height;
-    uint32_t *cursor;
+    struct virgl_renderer_resource_info info = { 0 };
+    g_autofree uint32_t *cursor = NULL;
+    uint32_t width = 0, height = 0;
+
+    virgl_renderer_force_ctx_0();
+    if (virgl_renderer_resource_get_info(resource_id, &info) != 0 ||
+        !virtio_gpu_format_is_cursor_capable(info.virgl_format)) {
+        g_debug("%s: invalid cursor resource %u", G_STRFUNC, resource_id);
+        return false;
+    }
 
     cursor = virgl_renderer_get_cursor_data(resource_id, &width, &height);
-    g_return_if_fail(cursor != NULL);
-    g_return_if_fail(width == 64);
-    g_return_if_fail(height == 64);
+    if (!cursor || width != 64 || height != 64) {
+        g_debug("%s: failed to read cursor resource %u (%ux%u)",
+                G_STRFUNC, resource_id, width, height);
+        return false;
+    }
 
     memcpy(data, cursor, 64 * 64 * sizeof(uint32_t));
-    free(cursor);
+    return true;
 }
 
 static void
diff --git a/contrib/vhost-user-gpu/virgl.h b/contrib/vhost-user-gpu/virgl.h
index 17078783a50f..40caa766d3fe 100644
--- a/contrib/vhost-user-gpu/virgl.h
+++ b/contrib/vhost-user-gpu/virgl.h
@@ -20,7 +20,7 @@
 bool vg_virgl_init(VuGpu *g);
 uint32_t vg_virgl_get_num_capsets(void);
 void vg_virgl_process_cmd(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd);
-void vg_virgl_update_cursor_data(VuGpu *g, uint32_t resource_id,
+bool vg_virgl_update_cursor_data(VuGpu *g, uint32_t resource_id,
                                  gpointer data);
 
 #endif

-- 
2.56.0.116.g6de20f6092dc


Reply via email to