Blame SOURCES/kvm-hw-display-qxl-Avoid-buffer-overrun-in-qxl_phys2virt.patch

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