thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
62547e
From 4e1bfbe3a0a113fe3cf39336a9d7da4e8c2a21ea 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 4/5] hw/display/qxl: Avoid buffer overrun in qxl_phys2virt
62547e
 (CVE-2022-4144)
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: [4/5] afe53f8d9b31c6fd8211fe172173151f3255e67c (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 6dbbf055148c6f1b7d8a3251a65bd6f3d1e1f622
62547e
Author: Philippe Mathieu-Daudé <philmd@linaro.org>
62547e
Date:   Mon Nov 28 21:27:40 2022 +0100
62547e
62547e
    hw/display/qxl: Avoid buffer overrun in qxl_phys2virt (CVE-2022-4144)
62547e
62547e
    Have qxl_get_check_slot_offset() return false if the requested
62547e
    buffer size does not fit within the slot memory region.
62547e
62547e
    Similarly qxl_phys2virt() now returns NULL in such case, and
62547e
    qxl_dirty_one_surface() aborts.
62547e
62547e
    This avoids buffer overrun in the host pointer returned by
62547e
    memory_region_get_ram_ptr().
62547e
62547e
    Fixes: CVE-2022-4144 (out-of-bounds read)
62547e
    Reported-by: Wenxu Yin (@awxylitol)
62547e
    Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1336
62547e
    Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
62547e
    Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
62547e
    Message-Id: <20221128202741.4945-5-philmd@linaro.org>
62547e
62547e
(cherry picked from commit 6dbbf055148c6f1b7d8a3251a65bd6f3d1e1f622)
62547e
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
62547e
---
62547e
 hw/display/qxl.c | 27 +++++++++++++++++++++++----
62547e
 hw/display/qxl.h |  2 +-
62547e
 2 files changed, 24 insertions(+), 5 deletions(-)
62547e
62547e
diff --git a/hw/display/qxl.c b/hw/display/qxl.c
62547e
index aa9065183e..2a4b2d4158 100644
62547e
--- a/hw/display/qxl.c
62547e
+++ b/hw/display/qxl.c
62547e
@@ -1412,11 +1412,13 @@ static void qxl_reset_surfaces(PCIQXLDevice *d)
62547e
 
62547e
 /* can be also called from spice server thread context */
62547e
 static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
62547e
-                                      uint32_t *s, uint64_t *o)
62547e
+                                      uint32_t *s, uint64_t *o,
62547e
+                                      size_t size_requested)
62547e
 {
62547e
     uint64_t phys   = le64_to_cpu(pqxl);
62547e
     uint32_t slot   = (phys >> (64 -  8)) & 0xff;
62547e
     uint64_t offset = phys & 0xffffffffffff;
62547e
+    uint64_t size_available;
62547e
 
62547e
     if (slot >= NUM_MEMSLOTS) {
62547e
         qxl_set_guest_bug(qxl, "slot too large %d >= %d", slot,
62547e
@@ -1440,6 +1442,23 @@ static bool qxl_get_check_slot_offset(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
62547e
                           slot, offset, qxl->guest_slots[slot].size);
62547e
         return false;
62547e
     }
62547e
+    size_available = memory_region_size(qxl->guest_slots[slot].mr);
62547e
+    if (qxl->guest_slots[slot].offset + offset >= size_available) {
62547e
+        qxl_set_guest_bug(qxl,
62547e
+                          "slot %d offset %"PRIu64" > region size %"PRIu64"\n",
62547e
+                          slot, qxl->guest_slots[slot].offset + offset,
62547e
+                          size_available);
62547e
+        return false;
62547e
+    }
62547e
+    size_available -= qxl->guest_slots[slot].offset + offset;
62547e
+    if (size_requested > size_available) {
62547e
+        qxl_set_guest_bug(qxl,
62547e
+                          "slot %d offset %"PRIu64" size %zu: "
62547e
+                          "overrun by %"PRIu64" bytes\n",
62547e
+                          slot, offset, size_requested,
62547e
+                          size_requested - size_available);
62547e
+        return false;
62547e
+    }
62547e
 
62547e
     *s = slot;
62547e
     *o = offset;
62547e
@@ -1459,7 +1478,7 @@ void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id,
62547e
         offset = le64_to_cpu(pqxl) & 0xffffffffffff;
62547e
         return (void *)(intptr_t)offset;
62547e
     case MEMSLOT_GROUP_GUEST:
62547e
-        if (!qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset)) {
62547e
+        if (!qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset, size)) {
62547e
             return NULL;
62547e
         }
62547e
         ptr = memory_region_get_ram_ptr(qxl->guest_slots[slot].mr);
62547e
@@ -1925,9 +1944,9 @@ static void qxl_dirty_one_surface(PCIQXLDevice *qxl, QXLPHYSICAL pqxl,
62547e
     uint32_t slot;
62547e
     bool rc;
62547e
 
62547e
-    rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset);
62547e
-    assert(rc == true);
62547e
     size = (uint64_t)height * abs(stride);
62547e
+    rc = qxl_get_check_slot_offset(qxl, pqxl, &slot, &offset, size);
62547e
+    assert(rc == true);
62547e
     trace_qxl_surfaces_dirty(qxl->id, offset, size);
62547e
     qxl_set_dirty(qxl->guest_slots[slot].mr,
62547e
                   qxl->guest_slots[slot].offset + offset,
62547e
diff --git a/hw/display/qxl.h b/hw/display/qxl.h
62547e
index c784315daa..89ca832cf9 100644
62547e
--- a/hw/display/qxl.h
62547e
+++ b/hw/display/qxl.h
62547e
@@ -157,7 +157,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(PCIQXLDevice, PCI_QXL)
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
- * the @qxl device. If the slot is inactive, or the offset is out
62547e
+ * the @qxl device. If the slot is inactive, or the offset + size are out
62547e
  * of the memory region, returns NULL.
62547e
  *
62547e
  * Use with care; by the time this function returns, the returned pointer is
62547e
-- 
62547e
2.37.3
62547e