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

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