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