62547e
From 0e6bd3911c4971f575aac7e9cd726467b52fe544 Mon Sep 17 00:00:00 2001
62547e
From: Jon Maloy <jmaloy@redhat.com>
62547e
Date: Mon, 5 Dec 2022 15:32:55 -0500
62547e
Subject: [PATCH 3/5] hw/display/qxl: Pass requested buffer size to
62547e
 qxl_phys2virt()
62547e
MIME-Version: 1.0
62547e
Content-Type: text/plain; charset=UTF-8
62547e
Content-Transfer-Encoding: 8bit
62547e
62547e
RH-Author: Jon Maloy <jmaloy@redhat.com>
62547e
RH-MergeRequest: 240: hw/display/qxl: Have qxl_log_command Return early if no log_cmd handler
62547e
RH-Bugzilla: 2148545
62547e
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
62547e
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
62547e
RH-Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
RH-Commit: [3/5] 8e362d67fe7fef9eb457cfb15d75b298fed725c3 (jmaloy/jons-qemu-kvm)
62547e
62547e
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=2148545
62547e
CVE: CVE-2022-4144
62547e
Upstream: Merged
62547e
62547e
commit 8efec0ef8bbc1e75a7ebf6e325a35806ece9b39f
62547e
Author: Philippe Mathieu-Daudé <philmd@linaro.org>
62547e
Date:   Mon Nov 28 21:27:39 2022 +0100
62547e
62547e
    hw/display/qxl: Pass requested buffer size to qxl_phys2virt()
62547e
62547e
    Currently qxl_phys2virt() doesn't check for buffer overrun.
62547e
    In order to do so in the next commit, pass the buffer size
62547e
    as argument.
62547e
62547e
    For QXLCursor in qxl_render_cursor() -> qxl_cursor() we
62547e
    verify the size of the chunked data ahead, checking we can
62547e
    access 'sizeof(QXLCursor) + chunk->data_size' bytes.
62547e
    Since in the SPICE_CURSOR_TYPE_MONO case the cursor is
62547e
    assumed to fit in one chunk, no change are required.
62547e
    In SPICE_CURSOR_TYPE_ALPHA the ahead read is handled in
62547e
    qxl_unpack_chunks().
62547e
62547e
    Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
62547e
    Acked-by: Gerd Hoffmann <kraxel@redhat.com>
62547e
    Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
62547e
    Message-Id: <20221128202741.4945-4-philmd@linaro.org>
62547e
62547e
(cherry picked from commit 8efec0ef8bbc1e75a7ebf6e325a35806ece9b39f)
62547e
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
62547e
---
62547e
 hw/display/qxl-logger.c | 11 ++++++++---
62547e
 hw/display/qxl-render.c | 20 ++++++++++++++++----
62547e
 hw/display/qxl.c        | 14 +++++++++-----
62547e
 hw/display/qxl.h        |  4 +++-
62547e
 4 files changed, 36 insertions(+), 13 deletions(-)
62547e
62547e
diff --git a/hw/display/qxl-logger.c b/hw/display/qxl-logger.c
62547e
index 1bcf803db6..35c38f6252 100644
62547e
--- a/hw/display/qxl-logger.c
62547e
+++ b/hw/display/qxl-logger.c
62547e
@@ -106,7 +106,7 @@ static int qxl_log_image(PCIQXLDevice *qxl, QXLPHYSICAL addr, int group_id)
62547e
     QXLImage *image;
62547e
     QXLImageDescriptor *desc;
62547e
 
62547e
-    image = qxl_phys2virt(qxl, addr, group_id);
62547e
+    image = qxl_phys2virt(qxl, addr, group_id, sizeof(QXLImage));
62547e
     if (!image) {
62547e
         return 1;
62547e
     }
62547e
@@ -214,7 +214,8 @@ int qxl_log_cmd_cursor(PCIQXLDevice *qxl, QXLCursorCmd *cmd, int group_id)
62547e
                 cmd->u.set.position.y,
62547e
                 cmd->u.set.visible ? "yes" : "no",
62547e
                 cmd->u.set.shape);
62547e
-        cursor = qxl_phys2virt(qxl, cmd->u.set.shape, group_id);
62547e
+        cursor = qxl_phys2virt(qxl, cmd->u.set.shape, group_id,
62547e
+                               sizeof(QXLCursor));
62547e
         if (!cursor) {
62547e
             return 1;
62547e
         }
62547e
@@ -236,6 +237,7 @@ int qxl_log_command(PCIQXLDevice *qxl, const char *ring, QXLCommandExt *ext)
62547e
 {
62547e
     bool compat = ext->flags & QXL_COMMAND_FLAG_COMPAT;
62547e
     void *data;
62547e
+    size_t datasz;
62547e
     int ret;
62547e
 
62547e
     if (!qxl->cmdlog) {
62547e
@@ -249,15 +251,18 @@ int qxl_log_command(PCIQXLDevice *qxl, const char *ring, QXLCommandExt *ext)
62547e
 
62547e
     switch (ext->cmd.type) {
62547e
     case QXL_CMD_DRAW:
62547e
+        datasz = compat ? sizeof(QXLCompatDrawable) : sizeof(QXLDrawable);
62547e
         break;
62547e
     case QXL_CMD_SURFACE:
62547e
+        datasz = sizeof(QXLSurfaceCmd);
62547e
         break;
62547e
     case QXL_CMD_CURSOR:
62547e
+        datasz = sizeof(QXLCursorCmd);
62547e
         break;
62547e
     default:
62547e
         goto out;
62547e
     }
62547e
-    data = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
62547e
+    data = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id, datasz);
62547e
     if (!data) {
62547e
         return 1;
62547e
     }
62547e
diff --git a/hw/display/qxl-render.c b/hw/display/qxl-render.c
62547e
index ca217004bf..fcfd40c3ac 100644
62547e
--- a/hw/display/qxl-render.c
62547e
+++ b/hw/display/qxl-render.c
62547e
@@ -107,7 +107,9 @@ static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
62547e
         qxl->guest_primary.resized = 0;
62547e
         qxl->guest_primary.data = qxl_phys2virt(qxl,
62547e
                                                 qxl->guest_primary.surface.mem,
62547e
-                                                MEMSLOT_GROUP_GUEST);
62547e
+                                                MEMSLOT_GROUP_GUEST,
62547e
+                                                qxl->guest_primary.abs_stride
62547e
+                                                * height);
62547e
         if (!qxl->guest_primary.data) {
62547e
             goto end;
62547e
         }
62547e
@@ -228,7 +230,8 @@ static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
62547e
         if (offset == size) {
62547e
             return;
62547e
         }
62547e
-        chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
62547e
+        chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id,
62547e
+                              sizeof(QXLDataChunk) + chunk->data_size);
62547e
         if (!chunk) {
62547e
             return;
62547e
         }
62547e
@@ -295,7 +298,8 @@ fail:
62547e
 /* called from spice server thread context only */
62547e
 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
62547e
 {
62547e
-    QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
62547e
+    QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
62547e
+                                      sizeof(QXLCursorCmd));
62547e
     QXLCursor *cursor;
62547e
     QEMUCursor *c;
62547e
 
62547e
@@ -314,7 +318,15 @@ int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
62547e
     }
62547e
     switch (cmd->type) {
62547e
     case QXL_CURSOR_SET:
62547e
-        cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
62547e
+        /* First read the QXLCursor to get QXLDataChunk::data_size ... */
62547e
+        cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
62547e
+                               sizeof(QXLCursor));
62547e
+        if (!cursor) {
62547e
+            return 1;
62547e
+        }
62547e
+        /* Then read including the chunked data following QXLCursor. */
62547e
+        cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
62547e
+                               sizeof(QXLCursor) + cursor->chunk.data_size);
62547e
         if (!cursor) {
62547e
             return 1;
62547e
         }
62547e
diff --git a/hw/display/qxl.c b/hw/display/qxl.c
62547e
index 29c80b4289..aa9065183e 100644
62547e
--- a/hw/display/qxl.c
62547e
+++ b/hw/display/qxl.c
62547e
@@ -274,7 +274,8 @@ static void qxl_spice_monitors_config_async(PCIQXLDevice *qxl, int replay)
62547e
                                           QXL_IO_MONITORS_CONFIG_ASYNC));
62547e
     }
62547e
 
62547e
-    cfg = qxl_phys2virt(qxl, qxl->guest_monitors_config, MEMSLOT_GROUP_GUEST);
62547e
+    cfg = qxl_phys2virt(qxl, qxl->guest_monitors_config, MEMSLOT_GROUP_GUEST,
62547e
+                        sizeof(QXLMonitorsConfig));
62547e
     if (cfg != NULL && cfg->count == 1) {
62547e
         qxl->guest_primary.resized = 1;
62547e
         qxl->guest_head0_width  = cfg->heads[0].width;
62547e
@@ -459,7 +460,8 @@ static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
62547e
     switch (le32_to_cpu(ext->cmd.type)) {
62547e
     case QXL_CMD_SURFACE:
62547e
     {
62547e
-        QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
62547e
+        QXLSurfaceCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
62547e
+                                           sizeof(QXLSurfaceCmd));
62547e
 
62547e
         if (!cmd) {
62547e
             return 1;
62547e
@@ -494,7 +496,8 @@ static int qxl_track_command(PCIQXLDevice *qxl, struct QXLCommandExt *ext)
62547e
     }
62547e
     case QXL_CMD_CURSOR:
62547e
     {
62547e
-        QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
62547e
+        QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
62547e
+                                          sizeof(QXLCursorCmd));
62547e
 
62547e
         if (!cmd) {
62547e
             return 1;
62547e
@@ -1444,7 +1447,8 @@ static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
62547e
 }
62547e
 
62547e
 /* can be also called from spice server thread context */
62547e
-void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)
62547e
+void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id,
62547e
+                    size_t size)
62547e
 {
62547e
     uint64_t offset;
62547e
     uint32_t slot;
62547e
@@ -1952,7 +1956,7 @@ static void qxl_dirty_surfaces(PCIQXLDevice *qxl)
62547e
         }
62547e
 
62547e
         cmd = qxl_phys2virt(qxl, qxl->guest_surfaces.cmds[i],
62547e
-                            MEMSLOT_GROUP_GUEST);
62547e
+                            MEMSLOT_GROUP_GUEST, sizeof(QXLSurfaceCmd));
62547e
         assert(cmd);
62547e
         assert(cmd->type == QXL_SURFACE_CMD_CREATE);
62547e
         qxl_dirty_one_surface(qxl, cmd->u.surface_create.data,
62547e
diff --git a/hw/display/qxl.h b/hw/display/qxl.h
62547e
index c938f88a2f..c784315daa 100644
62547e
--- a/hw/display/qxl.h
62547e
+++ b/hw/display/qxl.h
62547e
@@ -153,6 +153,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(PCIQXLDevice, PCI_QXL)
62547e
  * @qxl: QXL device
62547e
  * @phys: physical offset of buffer within the VRAM
62547e
  * @group_id: memory slot group
62547e
+ * @size: size of the buffer
62547e
  *
62547e
  * Returns a host pointer to a buffer placed at offset @phys within the
62547e
  * active slot @group_id of the PCI VGA RAM memory region associated with
62547e
@@ -166,7 +167,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(PCIQXLDevice, PCI_QXL)
62547e
  * the incoming ram_addr_t.
62547e
  *
62547e
  */
62547e
-void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL phys, int group_id);
62547e
+void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL phys, int group_id,
62547e
+                    size_t size);
62547e
 void qxl_set_guest_bug(PCIQXLDevice *qxl, const char *msg, ...)
62547e
     GCC_FMT_ATTR(2, 3);
62547e
 
62547e
-- 
62547e
2.37.3
62547e