Blame SOURCES/kvm-softmmu-physmem-Introduce-MemTxAttrs-memory-field-an.patch

4841a6
From fe4e22b9ccf2eb55d61eccf5050fb7aeafb5fe20 Mon Sep 17 00:00:00 2001
6443c2
From: Jon Maloy <jmaloy@redhat.com>
6443c2
Date: Wed, 13 Apr 2022 14:51:06 -0400
4841a6
Subject: [PATCH 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and
6443c2
 MEMTX_ACCESS_ERROR
6443c2
MIME-Version: 1.0
6443c2
Content-Type: text/plain; charset=UTF-8
6443c2
Content-Transfer-Encoding: 8bit
6443c2
6443c2
RH-Author: Jon Maloy <jmaloy@redhat.com>
4841a6
RH-MergeRequest: 151: hw/intc/arm_gicv3: Check for !MEMTX_OK instead of MEMTX_ERROR
4841a6
RH-Commit: [3/3] b1ebc1e99f21ba0b9eccb284e260b56c7a8e64d8 (jmaloy/qemu-kvm)
4841a6
RH-Bugzilla: 1999236
6443c2
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
6443c2
RH-Acked-by: Peter Xu <peterx@redhat.com>
6443c2
4841a6
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1999236
6443c2
Upstream: Merged
6443c2
CVE: CVE-2021-3750
6443c2
Conflicts: memalign.h has not been introduced in this version. Instead,
6443c2
           we include osdep.h where the function prototypes are to be
6443c2
           found.
6443c2
6443c2
commit 3ab6fdc91b72e156da22848f0003ff4225690ced
6443c2
Author: Philippe Mathieu-Daudé <philmd@redhat.com>
6443c2
Date:   Wed Dec 15 19:24:21 2021 +0100
6443c2
6443c2
    softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_ACCESS_ERROR
6443c2
6443c2
    Add the 'memory' bit to the memory attributes to restrict bus
6443c2
    controller accesses to memories.
6443c2
6443c2
    Introduce flatview_access_allowed() to check bus permission
6443c2
    before running any bus transaction.
6443c2
6443c2
    Have read/write accessors return MEMTX_ACCESS_ERROR if an access is
6443c2
    restricted.
6443c2
6443c2
    There is no change for the default case where 'memory' is not set.
6443c2
6443c2
    Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6443c2
    Message-Id: <20211215182421.418374-4-philmd@redhat.com>
6443c2
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
6443c2
    Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
6443c2
    [thuth: Replaced MEMTX_BUS_ERROR with MEMTX_ACCESS_ERROR, remove "inline"]
6443c2
    Signed-off-by: Thomas Huth <thuth@redhat.com>
6443c2
6443c2
(cherry picked from commit 3ab6fdc91b72e156da22848f0003ff4225690ced)
6443c2
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
6443c2
---
6443c2
 include/exec/memattrs.h |  9 +++++++++
6443c2
 softmmu/physmem.c       | 45 +++++++++++++++++++++++++++++++++++++++--
6443c2
 2 files changed, 52 insertions(+), 2 deletions(-)
6443c2
6443c2
diff --git a/include/exec/memattrs.h b/include/exec/memattrs.h
6443c2
index 95f2d20d55..9fb98bc1ef 100644
6443c2
--- a/include/exec/memattrs.h
6443c2
+++ b/include/exec/memattrs.h
6443c2
@@ -35,6 +35,14 @@ typedef struct MemTxAttrs {
6443c2
     unsigned int secure:1;
6443c2
     /* Memory access is usermode (unprivileged) */
6443c2
     unsigned int user:1;
6443c2
+    /*
6443c2
+     * Bus interconnect and peripherals can access anything (memories,
6443c2
+     * devices) by default. By setting the 'memory' bit, bus transaction
6443c2
+     * are restricted to "normal" memories (per the AMBA documentation)
6443c2
+     * versus devices. Access to devices will be logged and rejected
6443c2
+     * (see MEMTX_ACCESS_ERROR).
6443c2
+     */
6443c2
+    unsigned int memory:1;
6443c2
     /* Requester ID (for MSI for example) */
6443c2
     unsigned int requester_id:16;
6443c2
     /* Invert endianness for this page */
6443c2
@@ -66,6 +74,7 @@ typedef struct MemTxAttrs {
6443c2
 #define MEMTX_OK 0
6443c2
 #define MEMTX_ERROR             (1U << 0) /* device returned an error */
6443c2
 #define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
6443c2
+#define MEMTX_ACCESS_ERROR      (1U << 2) /* access denied */
6443c2
 typedef uint32_t MemTxResult;
6443c2
 
6443c2
 #endif
6443c2
diff --git a/softmmu/physmem.c b/softmmu/physmem.c
6443c2
index 483a31be81..4d0ef5f92f 100644
6443c2
--- a/softmmu/physmem.c
6443c2
+++ b/softmmu/physmem.c
6443c2
@@ -41,6 +41,8 @@
6443c2
 #include "qemu/config-file.h"
6443c2
 #include "qemu/error-report.h"
6443c2
 #include "qemu/qemu-print.h"
6443c2
+#include "qemu/log.h"
6443c2
+#include "qemu/osdep.h"
6443c2
 #include "exec/memory.h"
6443c2
 #include "exec/ioport.h"
6443c2
 #include "sysemu/dma.h"
6443c2
@@ -2759,6 +2761,33 @@ static bool prepare_mmio_access(MemoryRegion *mr)
6443c2
     return release_lock;
6443c2
 }
6443c2
 
6443c2
+/**
6443c2
+ * flatview_access_allowed
6443c2
+ * @mr: #MemoryRegion to be accessed
6443c2
+ * @attrs: memory transaction attributes
6443c2
+ * @addr: address within that memory region
6443c2
+ * @len: the number of bytes to access
6443c2
+ *
6443c2
+ * Check if a memory transaction is allowed.
6443c2
+ *
6443c2
+ * Returns: true if transaction is allowed, false if denied.
6443c2
+ */
6443c2
+static bool flatview_access_allowed(MemoryRegion *mr, MemTxAttrs attrs,
6443c2
+                                    hwaddr addr, hwaddr len)
6443c2
+{
6443c2
+    if (likely(!attrs.memory)) {
6443c2
+        return true;
6443c2
+    }
6443c2
+    if (memory_region_is_ram(mr)) {
6443c2
+        return true;
6443c2
+    }
6443c2
+    qemu_log_mask(LOG_GUEST_ERROR,
6443c2
+                  "Invalid access to non-RAM device at "
6443c2
+                  "addr 0x%" HWADDR_PRIX ", size %" HWADDR_PRIu ", "
6443c2
+                  "region '%s'\n", addr, len, memory_region_name(mr));
6443c2
+    return false;
6443c2
+}
6443c2
+
6443c2
 /* Called within RCU critical section.  */
6443c2
 static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
6443c2
                                            MemTxAttrs attrs,
6443c2
@@ -2773,7 +2802,10 @@ static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
6443c2
     const uint8_t *buf = ptr;
6443c2
 
6443c2
     for (;;) {
6443c2
-        if (!memory_access_is_direct(mr, true)) {
6443c2
+        if (!flatview_access_allowed(mr, attrs, addr1, l)) {
6443c2
+            result |= MEMTX_ACCESS_ERROR;
6443c2
+            /* Keep going. */
6443c2
+        } else if (!memory_access_is_direct(mr, true)) {
6443c2
             release_lock |= prepare_mmio_access(mr);
6443c2
             l = memory_access_size(mr, l, addr1);
6443c2
             /* XXX: could force current_cpu to NULL to avoid
6443c2
@@ -2818,6 +2850,9 @@ static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
6443c2
 
6443c2
     l = len;
6443c2
     mr = flatview_translate(fv, addr, &addr1, &l, true, attrs);
6443c2
+    if (!flatview_access_allowed(mr, attrs, addr, len)) {
6443c2
+        return MEMTX_ACCESS_ERROR;
6443c2
+    }
6443c2
     return flatview_write_continue(fv, addr, attrs, buf, len,
6443c2
                                    addr1, l, mr);
6443c2
 }
6443c2
@@ -2836,7 +2871,10 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
6443c2
 
6443c2
     fuzz_dma_read_cb(addr, len, mr);
6443c2
     for (;;) {
6443c2
-        if (!memory_access_is_direct(mr, false)) {
6443c2
+        if (!flatview_access_allowed(mr, attrs, addr1, l)) {
6443c2
+            result |= MEMTX_ACCESS_ERROR;
6443c2
+            /* Keep going. */
6443c2
+        } else if (!memory_access_is_direct(mr, false)) {
6443c2
             /* I/O case */
6443c2
             release_lock |= prepare_mmio_access(mr);
6443c2
             l = memory_access_size(mr, l, addr1);
6443c2
@@ -2879,6 +2917,9 @@ static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
6443c2
 
6443c2
     l = len;
6443c2
     mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
6443c2
+    if (!flatview_access_allowed(mr, attrs, addr, len)) {
6443c2
+        return MEMTX_ACCESS_ERROR;
6443c2
+    }
6443c2
     return flatview_read_continue(fv, addr, attrs, buf, len,
6443c2
                                   addr1, l, mr);
6443c2
 }
6443c2
-- 
6443c2
2.27.0
6443c2