97168e
From 4e1bfbe3a0a113fe3cf39336a9d7da4e8c2a21ea Mon Sep 17 00:00:00 2001
32790f
From: Jon Maloy <jmaloy@redhat.com>
32790f
Date: Mon, 5 Dec 2022 15:32:55 -0500
97168e
Subject: [PATCH 4/5] hw/display/qxl: Avoid buffer overrun in qxl_phys2virt
32790f
 (CVE-2022-4144)
32790f
MIME-Version: 1.0
32790f
Content-Type: text/plain; charset=UTF-8
32790f
Content-Transfer-Encoding: 8bit
32790f
32790f
RH-Author: Jon Maloy <jmaloy@redhat.com>
97168e
RH-MergeRequest: 240: hw/display/qxl: Have qxl_log_command Return early if no log_cmd handler
97168e
RH-Bugzilla: 2148545
32790f
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
32790f
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
97168e
RH-Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>
97168e
RH-Commit: [4/5] afe53f8d9b31c6fd8211fe172173151f3255e67c (jmaloy/jons-qemu-kvm)
32790f
97168e
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=2148545
32790f
CVE: CVE-2022-4144
32790f
Upstream: Merged
32790f
32790f
commit 6dbbf055148c6f1b7d8a3251a65bd6f3d1e1f622
32790f
Author: Philippe Mathieu-Daudé <philmd@linaro.org>
32790f
Date:   Mon Nov 28 21:27:40 2022 +0100
32790f
32790f
    hw/display/qxl: Avoid buffer overrun in qxl_phys2virt (CVE-2022-4144)
32790f
32790f
    Have qxl_get_check_slot_offset() return false if the requested
32790f
    buffer size does not fit within the slot memory region.
32790f
32790f
    Similarly qxl_phys2virt() now returns NULL in such case, and
32790f
    qxl_dirty_one_surface() aborts.
32790f
32790f
    This avoids buffer overrun in the host pointer returned by
32790f
    memory_region_get_ram_ptr().
32790f
32790f
    Fixes: CVE-2022-4144 (out-of-bounds read)
32790f
    Reported-by: Wenxu Yin (@awxylitol)
32790f
    Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1336
32790f
    Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
32790f
    Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
32790f
    Message-Id: <20221128202741.4945-5-philmd@linaro.org>
32790f
32790f
(cherry picked from commit 6dbbf055148c6f1b7d8a3251a65bd6f3d1e1f622)
32790f
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
32790f
---
32790f
 hw/display/qxl.c | 27 +++++++++++++++++++++++----
32790f
 hw/display/qxl.h |  2 +-
32790f
 2 files changed, 24 insertions(+), 5 deletions(-)
32790f
32790f
diff --git a/hw/display/qxl.c b/hw/display/qxl.c
32790f
index aa9065183e..2a4b2d4158 100644
32790f
--- a/hw/display/qxl.c
32790f
+++ b/hw/display/qxl.c
32790f
@@ -1412,11 +1412,13 @@ static void qxl_reset_surfaces(PCIQXLDevice *d)
32790f
 
32790f
 /* can be also called from spice server thread context */
32790f
 static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
32790f
-                                      uint32_t *s, uint64_t *o)
32790f
+                                      uint32_t *s, uint64_t *o,
32790f
+                                      size_t size_requested)
32790f
 {
32790f
     uint64_t phys   = le64_to_cpu(pqxl);
32790f
     uint32_t slot   = (phys >> (64 -  8)) & 0xff;
32790f
     uint64_t offset = phys & 0xffffffffffff;
32790f
+    uint64_t size_available;
32790f
 
32790f
     if (slot >= NUM_MEMSLOTS) {
32790f
         qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
32790f
@@ -1440,6 +1442,23 @@ static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
32790f
                           slot, offset, qxl->guest_slots[slot].size);
32790f
         return false;
32790f
     }
32790f
+    size_available = memory_region_size(qxl->guest_slots[slot].mr);
32790f
+    if (qxl->guest_slots[slot].offset + offset >= size_available) {
32790f
+        qxl_set_guest_bug(qxl,
32790f
+                          "slot %d offset %"PRIu64" > region size %"PRIu64"\n",
32790f
+                          slot, qxl->guest_slots[slot].offset + offset,
32790f
+                          size_available);
32790f
+        return false;
32790f
+    }
32790f
+    size_available -= qxl->guest_slots[slot].offset + offset;
32790f
+    if (size_requested > size_available) {
32790f
+        qxl_set_guest_bug(qxl,
32790f
+                          "slot %d offset %"PRIu64" size %zu: "
32790f
+                          "overrun by %"PRIu64" bytes\n",
32790f
+                          slot, offset, size_requested,
32790f
+                          size_requested - size_available);
32790f
+        return false;
32790f
+    }
32790f
 
32790f
     *s = slot;
32790f
     *o = offset;
32790f
@@ -1459,7 +1478,7 @@ void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id,
32790f
         offset = le64_to_cpu(pqxl) & 0xffffffffffff;
32790f
         return (void *)(intptr_t)offset;
32790f
     case MEMSLOT_GROUP_GUEST:
32790f
-        if (!qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset)) {
32790f
+        if (!qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset, size)) {
32790f
             return NULL;
32790f
         }
32790f
         ptr = memory_region_get_ram_ptr(qxl->guest_slots[slot].mr);
32790f
@@ -1925,9 +1944,9 @@ static void qxl_dirty_one_surface(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
32790f
     uint32_t slot;
32790f
     bool rc;
32790f
 
32790f
-    rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset);
32790f
-    assert(rc == true);
32790f
     size = (uint64_t)height * abs(stride);
32790f
+    rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset, size);
32790f
+    assert(rc == true);
32790f
     trace_qxl_surfaces_dirty(qxl->id, offset, size);
32790f
     qxl_set_dirty(qxl->guest_slots[slot].mr,
32790f
                   qxl->guest_slots[slot].offset + offset,
32790f
diff --git a/hw/display/qxl.h b/hw/display/qxl.h
32790f
index c784315daa..89ca832cf9 100644
32790f
--- a/hw/display/qxl.h
32790f
+++ b/hw/display/qxl.h
32790f
@@ -157,7 +157,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(PCIQXLDevice, PCI_QXL)
32790f
  *
32790f
  * Returns a host pointer to a buffer placed at offset @phys within the
32790f
  * active slot @group_id of the PCI VGA RAM memory region associated with
32790f
- * the @qxl device. If the slot is inactive, or the offset is out
32790f
+ * the @qxl device. If the slot is inactive, or the offset + size are out
32790f
  * of the memory region, returns NULL.
32790f
  *
32790f
  * Use with care; by the time this function returns, the returned pointer is
32790f
-- 
32790f
2.37.3
32790f