Skip to contentSkip to conversation

physmem: Have flaview API check bus permission from MemTxAttrs argument

RFC PATCH Applied Philippe Mathieu-Daudé 6 participants 15 messages Dec 15, 2021 – Jan 25, 2022

lore
Share & tools
lore
#0 lore raw

v3 cover physmem: Have flaview API check bus permission from MemTxAttrs argument

This series aim to kill a recent class of bug, the infamous "DMA reentrancy" issues found by Alexander while fuzzing.

Introduce the 'memory' field in MemTxAttrs, allowing to restrict a controller to memories (and not devices).

If a transaction permission is not allowed (for example access to device), we return the specific MEMTX_BUS_ERROR.

Permissions are checked in after the flatview is resolved, and before the access is done, in a new function: flatview_access_allowed().

Since v2 [1]:
- Addressed review comments:
- reword arm_gicv3 description (pm215)
- merged patches 3/4/5 (peterx & dhildenb)
- simplify flatview_access_allowed() logic (stefanha)
- drop MEMTXPERM enum and reword following AMBA terminology (edgar)

Since v1 ("hw: Forbid DMA write accesses to MMIO regions") [2]:
- rewrite based on Peter / Stefan feedbacks

Based on "hw: Let the DMA API take a MemTxAttrs argument" [3].

Based-on: <[email protected]>

[1] https://www.mail-archive.com/[email protected]/msg831168.html
[2] https://www.mail-archive.com/[email protected]/msg72924.html
[3] https://www.mail-archive.com/[email protected]/msg820359.html

Philippe Mathieu-Daudé (3):
hw/intc/arm_gicv3: Check for !MEMTX_OK instead of MEMTX_ERROR softmmu/physmem: Simplify flatview_write and
address_space_access_valid
softmmu/physmem: Introduce MemTxAttrs::memory field and
MEMTX_BUS_ERROR

 include/exec/memattrs.h    |  9 +++++++
 hw/intc/arm_gicv3_redist.c |  4 +--
 softmmu/physmem.c          | 54 +++++++++++++++++++++++++++++++-------
 3 files changed, 55 insertions(+), 12 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

RFC PATCH v3 1/3 hw/intc/arm_gicv3: Check for !MEMTX_OK instead of MEMTX_ERROR

Quoting Peter Maydell:

"These MEMTX_* aren't from the memory transaction
API functions; they're just being used by gicd_readl() and friends as a way to indicate a success/failure so that the actual MemoryRegionOps read/write fns like gicv3_dist_read() can log a guest error."

We are going to introduce more MemTxResult bits, so it is safer to check for !MEMTX_OK rather than MEMTX_ERROR.

---
 hw/intc/arm_gicv3_redist.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/arm_gicv3_redist.c +2 -2
index c8ff3eca085..99b11ca5eee 100644
@@ -462,7 +462,7 @@ MemTxResult gicv3_redist_read(void *opaque, hwaddr offset, uint64_t *data,
         break;
     }
 
-    if (r == MEMTX_ERROR) {
+    if (r != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: invalid guest read at offset " TARGET_FMT_plx
                       " size %u\n", __func__, offset, size);
@@ -521,7 +521,7 @@ MemTxResult gicv3_redist_write(void *opaque, hwaddr offset, uint64_t data,
         break;
     }
 
-    if (r == MEMTX_ERROR) {
+    if (r != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: invalid guest write at offset " TARGET_FMT_plx
                       " size %u\n", __func__, offset, size);
Signature · 1 line
2.33.1
#2 lore raw

Re: [RFC PATCH v3 1/3] hw/intc/arm_gicv3: Check for !MEMTX_OK instead of MEMTX_ERROR

Hi Peter,

Can you take this single patch via your arm tree?

Thanks,

Phil.

Quoted 42 lines · Philippe Mathieu-Daudé

Quoting Peter Maydell:

"These MEMTX_* aren't from the memory transaction
API functions; they're just being used by gicd_readl() and friends as a way to indicate a success/failure so that the actual MemoryRegionOps read/write fns like gicv3_dist_read() can log a guest error."

We are going to introduce more MemTxResult bits, so it is safer to check for !MEMTX_OK rather than MEMTX_ERROR.

---
 hw/intc/arm_gicv3_redist.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/arm_gicv3_redist.c +2 -2
index c8ff3eca085..99b11ca5eee 100644
@@ -462,7 +462,7 @@ MemTxResult gicv3_redist_read(void *opaque, hwaddr offset, uint64_t *data,
         break;
     }
 
-    if (r == MEMTX_ERROR) {
+    if (r != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: invalid guest read at offset " TARGET_FMT_plx
                       " size %u\n", __func__, offset, size);
@@ -521,7 +521,7 @@ MemTxResult gicv3_redist_write(void *opaque, hwaddr offset, uint64_t data,
         break;
     }
 
-    if (r == MEMTX_ERROR) {
+    if (r != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: invalid guest write at offset " TARGET_FMT_plx
                       " size %u\n", __func__, offset, size);
#3 lore raw

Re: [RFC PATCH v3 1/3] hw/intc/arm_gicv3: Check for !MEMTX_OK instead of MEMTX_ERROR

Quoted 4 lines · Philippe Mathieu-Daudé

Hi Peter,

Can you take this single patch via your arm tree?

Sure.

Applied to target-arm.next, thanks.

-- PMM

#4 lore raw

RFC PATCH v3 2/3 softmmu/physmem: Simplify flatview_write and address_space_access_valid

In reply to Philippe Mathieu-Daudé

Remove unuseful local 'result' variables.

---
 softmmu/physmem.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
softmmu/physmem.c +3 -8
index 007e7526f0a..6c97a20107a 100644
@@ -2815,14 +2815,11 @@ static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
     hwaddr l;
     hwaddr addr1;
     MemoryRegion *mr;
-    MemTxResult result = MEMTX_OK;
 
     l = len;
     mr = flatview_translate(fv, addr, &addr1, &l, true, attrs);
-    result = flatview_write_continue(fv, addr, attrs, buf, len,
-                                     addr1, l, mr);
-
-    return result;
+    return flatview_write_continue(fv, addr, attrs, buf, len,
+                                   addr1, l, mr);
 }
 
 /* Called within RCU critical section.  */
@@ -3119,12 +3116,10 @@ bool address_space_access_valid(AddressSpace *as, hwaddr addr,
                                 MemTxAttrs attrs)
 {
     FlatView *fv;
-    bool result;
 
     RCU_READ_LOCK_GUARD();
     fv = address_space_to_flatview(as);
-    result = flatview_access_valid(fv, addr, len, is_write, attrs);
-    return result;
+    return flatview_access_valid(fv, addr, len, is_write, attrs);
 }
 
 static hwaddr
Signature · 1 line
2.33.1
#5 lore raw

RFC PATCH v3 3/3 softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

In reply to Philippe Mathieu-Daudé

Add the 'memory' bit to the memory attributes to restrict bus controller accesses to memories.

Introduce flatview_access_allowed() to check bus permission before running any bus transaction.

Have read/write accessors return MEMTX_BUS_ERROR if an access is restricted.

There is no change for the default case where 'memory' is not set.

---
 include/exec/memattrs.h |  9 +++++++++
 softmmu/physmem.c       | 43 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 50 insertions(+), 2 deletions(-)
include/exec/memattrs.h +9 -0
index 95f2d20d55b..f0063583ee2 100644
@@ -35,6 +35,14 @@ typedef struct MemTxAttrs {
     unsigned int secure:1;
     /* Memory access is usermode (unprivileged) */
     unsigned int user:1;
+    /*
+     * Bus interconnect and peripherals can access anything (memories,
+     * devices) by default. By setting the 'memory' bit, bus transaction
+     * are restricted to "normal" memories (per the AMBA documentation)
+     * versus devices. Access to devices will be logged and rejected
+     * (see MEMTX_BUS_ERROR).
+     */
+    unsigned int memory:1;
     /* Requester ID (for MSI for example) */
     unsigned int requester_id:16;
     /* Invert endianness for this page */
@@ -66,6 +74,7 @@ typedef struct MemTxAttrs {
 #define MEMTX_OK 0
 #define MEMTX_ERROR             (1U << 0) /* device returned an error */
 #define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
+#define MEMTX_BUS_ERROR         (1U << 2) /* bus returned an error */
 typedef uint32_t MemTxResult;
 
 #endif
softmmu/physmem.c +41 -2

73 lines not on this page · raw message

Signature · 1 line
2.33.1
Reviewed-by on v3 3/3
#6 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

softmmu/physmem.c static bool prepare_mmio_access(MemoryRegion *mr) · line 2774

Quoted 2 lines · Philippe Mathieu-Daudé
softmmu/physmem.c +2 -0
+static inline bool flatview_access_allowed(MemoryRegion *mr, MemTxAttrs attrs,
+                                           hwaddr addr, hwaddr len)

There's no need to mark this inline. Otherwise,

r~

#7 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

In reply to Philippe Mathieu-Daudé
Quoted 38 lines · Philippe Mathieu-Daudé

Add the 'memory' bit to the memory attributes to restrict bus controller accesses to memories.

Introduce flatview_access_allowed() to check bus permission before running any bus transaction.

Have read/write accessors return MEMTX_BUS_ERROR if an access is restricted.

There is no change for the default case where 'memory' is not set.

---
 include/exec/memattrs.h |  9 +++++++++
 softmmu/physmem.c       | 43 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 50 insertions(+), 2 deletions(-)
include/exec/memattrs.h +8 -0
index 95f2d20d55b..f0063583ee2 100644
@@ -35,6 +35,14 @@ typedef struct MemTxAttrs {
     unsigned int secure:1;
     /* Memory access is usermode (unprivileged) */
     unsigned int user:1;
+    /*
+     * Bus interconnect and peripherals can access anything (memories,
+     * devices) by default. By setting the 'memory' bit, bus transaction
+     * are restricted to "normal" memories (per the AMBA documentation)
+     * versus devices. Access to devices will be logged and rejected
+     * (see MEMTX_BUS_ERROR).
+     */
+    unsigned int memory:1;
     /* Requester ID (for MSI for example) */
     unsigned int requester_id:16;
     /* Invert endianness for this page */

include/exec/memattrs.h typedef struct MemTxAttrs { · line 77

Quoted 4 lines
include/exec/memattrs.h +1 -0
@@ -66,6 +74,7 @@ typedef struct MemTxAttrs {
 #define MEMTX_OK 0
 #define MEMTX_ERROR             (1U << 0) /* device returned an error */
 #define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
+#define MEMTX_BUS_ERROR         (1U << 2) /* bus returned an error */

This is kind of odd naming, because MEMTX_DECODE_ERROR already means "bus/interconnect returned an error" and it generally translates into what at the OS level gets called a "bus error"...

-- PMM

#8 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

Quoted 39 lines · Peter Maydell
Quoted 38 lines · Philippe Mathieu-Daudé

Add the 'memory' bit to the memory attributes to restrict bus controller accesses to memories.

Introduce flatview_access_allowed() to check bus permission before running any bus transaction.

Have read/write accessors return MEMTX_BUS_ERROR if an access is restricted.

There is no change for the default case where 'memory' is not set.

---
 include/exec/memattrs.h |  9 +++++++++
 softmmu/physmem.c       | 43 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 50 insertions(+), 2 deletions(-)
include/exec/memattrs.h +8 -0
index 95f2d20d55b..f0063583ee2 100644
@@ -35,6 +35,14 @@ typedef struct MemTxAttrs {
     unsigned int secure:1;
     /* Memory access is usermode (unprivileged) */
     unsigned int user:1;
+    /*
+     * Bus interconnect and peripherals can access anything (memories,
+     * devices) by default. By setting the 'memory' bit, bus transaction
+     * are restricted to "normal" memories (per the AMBA documentation)
+     * versus devices. Access to devices will be logged and rejected
+     * (see MEMTX_BUS_ERROR).
+     */
+    unsigned int memory:1;
     /* Requester ID (for MSI for example) */
     unsigned int requester_id:16;
     /* Invert endianness for this page */
@@ -66,6 +74,7 @@ typedef struct MemTxAttrs {
Quoted 8 lines

include/exec/memattrs.h typedef struct MemTxAttrs { · line 77

Quoted 4 lines
include/exec/memattrs.h +1 -0
 #define MEMTX_OK 0
 #define MEMTX_ERROR             (1U << 0) /* device returned an error */
 #define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
+#define MEMTX_BUS_ERROR         (1U << 2) /* bus returned an error */

This is kind of odd naming, because MEMTX_DECODE_ERROR already means "bus/interconnect returned an error" and it generally translates into what at the OS level gets called a "bus error"...

MEMTX_DECODE_ERROR is "nothing at that address". We want a name for "there is something, but you don't have access to it". Maybe MEMTX_ILLEGAL_ERROR?

#9 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 5 lines
Quoted 4 lines

#define MEMTX_OK 0
#define MEMTX_ERROR (1U << 0) /* device returned an error */
#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
+#define MEMTX_BUS_ERROR (1U << 2) /* bus returned an error */

Quoted 7 lines
Quoted 3 lines

This is kind of odd naming, because MEMTX_DECODE_ERROR already means "bus/interconnect returned an error" and it generally translates into what at the OS level gets called a "bus error"...

MEMTX_DECODE_ERROR is "nothing at that address". We want a name for "there is something, but you don't have access to it". Maybe MEMTX_ILLEGAL_ERROR?

ILLEGAL doesn't convey much. MEMTX_ACCESS_ERROR?

r~

#10 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

Quoted 12 lines · Richard Henderson
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines
Quoted 6 lines

  #define MEMTX_OK 0
  #define MEMTX_ERROR             (1U << 0) /* device returned an error */
  #define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
+#define MEMTX_BUS_ERROR         (1U << 2) /* bus returned an error */

This is kind of odd naming, because MEMTX_DECODE_ERROR already means "bus/interconnect returned an error" and it generally translates into what at the OS level gets called a "bus error"...

Quoted 5 lines
Quoted 3 lines

MEMTX_DECODE_ERROR is "nothing at that address". We want a name for "there is something, but you don't have access to it". Maybe MEMTX_ILLEGAL_ERROR?

ILLEGAL doesn't convey much.  MEMTX_ACCESS_ERROR?

OK, such:

  #define MEMTX_ACCESS_ERROR      (1U << 2) /* access denied */

alternatively MEMTX_PERM_ERROR.

Reviewed-by on v3 3/3
#11 lore raw

Re: [RFC PATCH v3 3/3] softmmu/physmem: Introduce MemTxAttrs::memory field and MEMTX_BUS_ERROR

In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

Add the 'memory' bit to the memory attributes to restrict bus controller accesses to memories.

Introduce flatview_access_allowed() to check bus permission before running any bus transaction.

Have read/write accessors return MEMTX_BUS_ERROR if an access is restricted.

Quoted 7 lines

There is no change for the default case where 'memory' is not set.

---
 include/exec/memattrs.h |  9 +++++++++
 softmmu/physmem.c       | 43 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 50 insertions(+), 2 deletions(-)
#12 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 2 lines · Philippe Mathieu-Daudé

This series aim to kill a recent class of bug, the infamous "DMA reentrancy" issues found by Alexander while fuzzing.

I took a look at how to protect DMA transactions in VIRTIO devices. It will require setting the MemTxAttrs for address_space_ld/st_le/be_cached calls. Errors on write (store) can be ignored. Errors on read (load) are a bit more questionable since the device performs some operation based on the loaded value, but at this point the driver has already caused the device to do something no correct driver does (as of today, it could change in the future...) so undefined device behavior might be okay.

It would be easier to be confident if there was a single place to disable DMA re-entrancy for a device. The currently proposed API requires per-device code audits and fixes. It leaves decisions to the developer of each device. This will be a lot of work to fix and we cannot be confident that everything has been covered since this is an opt-in mechanism.

For these reasons it seems likely that DMA re-entrancy issues will continue to creep in. I think the only way to rule out this class of bugs is to implement a centralized change that doesn't involve fixing every DMA access in QEMU.

Thoughts?

Stefan

#13 lore raw
Quoted 19 lines · Stefan Hajnoczi
Quoted 2 lines · Philippe Mathieu-Daudé

This series aim to kill a recent class of bug, the infamous "DMA reentrancy" issues found by Alexander while fuzzing.

I took a look at how to protect DMA transactions in VIRTIO devices. It will require setting the MemTxAttrs for address_space_ld/st_le/be_cached calls. Errors on write (store) can be ignored. Errors on read (load) are a bit more questionable since the device performs some operation based on the loaded value, but at this point the driver has already caused the device to do something no correct driver does (as of today, it could change in the future...) so undefined device behavior might be okay.

It would be easier to be confident if there was a single place to disable DMA re-entrancy for a device. The currently proposed API requires per-device code audits and fixes. It leaves decisions to the developer of each device. This will be a lot of work to fix and we cannot be confident that everything has been covered since this is an opt-in mechanism.

Quoted 6 lines

For these reasons it seems likely that DMA re-entrancy issues will continue to creep in. I think the only way to rule out this class of bugs is to implement a centralized change that doesn't involve fixing every DMA access in QEMU.

Thoughts?

Hi Stefan,
Do you have some ideas about how to do this centrally?
There were at least two attempts to do this in a centralized way, but it seems there is some worry that edge cases will break. However, I'm not sure there were any concrete examples of such breakages.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
(AFAIK Neither handles the BH->DMA->MMIO case, at the moment)

-Alex

Quoted 2 lines

Stefan

#14 lore raw
Quoted 33 lines · Alexander Bulekov
Quoted 19 lines · Stefan Hajnoczi
Quoted 2 lines · Philippe Mathieu-Daudé

This series aim to kill a recent class of bug, the infamous "DMA reentrancy" issues found by Alexander while fuzzing.

I took a look at how to protect DMA transactions in VIRTIO devices. It will require setting the MemTxAttrs for address_space_ld/st_le/be_cached calls. Errors on write (store) can be ignored. Errors on read (load) are a bit more questionable since the device performs some operation based on the loaded value, but at this point the driver has already caused the device to do something no correct driver does (as of today, it could change in the future...) so undefined device behavior might be okay.

It would be easier to be confident if there was a single place to disable DMA re-entrancy for a device. The currently proposed API requires per-device code audits and fixes. It leaves decisions to the developer of each device. This will be a lot of work to fix and we cannot be confident that everything has been covered since this is an opt-in mechanism.

Quoted 6 lines

For these reasons it seems likely that DMA re-entrancy issues will continue to creep in. I think the only way to rule out this class of bugs is to implement a centralized change that doesn't involve fixing every DMA access in QEMU.

Thoughts?

Hi Stefan,
Do you have some ideas about how to do this centrally?
There were at least two attempts to do this in a centralized way, but it seems there is some worry that edge cases will break. However, I'm not sure there were any concrete examples of such breakages.

Quoted 3 lines

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
(AFAIK Neither handles the BH->DMA->MMIO case, at the moment)

Regressions are the problem with defaulting to RAM-only DMA. There's no way to avoid the risk if we change the default. On the other hand, it's the only way to squash this class of bugs - most existing devices just aren't written to cope with DMA re-entrancy.

The approach in your patch sounds good to me, but I haven't followed the discussions so maybe there were valid reasons to look for alternatives.

Stefan