Skip to contentSkip to conversation

intel_iommu: Replace assert(size == 4) with guest error in MMIO handlers

PATCH In review Junjie Cao 2 participants 6 messages Apr 20 – Apr 24

lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

PATCH 1/2 intel_iommu: Replace assert(size == 4) with guest error in MMIO handlers

vtd_mem_read() and vtd_mem_write() use assert(size == 4) for 32-bit-only VT-d registers. Since MemoryRegionOps .valid/.impl accepts sizes 4 through 8, an 8-byte guest access passes framework validation and reaches the handler, where the assertion aborts QEMU.

Narrowing .impl.max_access_size to 4 is not viable because VT-d has 64-bit registers (e.g. DMAR_CCMD_REG, DMAR_IQA_REG) that require atomic 8-byte access to trigger command execution.

Replace all 22 asserts in vtd_mem_write() and 3 in vtd_mem_read() with a size check that logs via qemu_log_mask(LOG_GUEST_ERROR) and returns without performing the register access. The asymmetry (22 vs 3) is because vtd_mem_read() handles most registers through its default branch, so only three explicit cases carried an assert.

Found by fuzzing with QEMU's generic-fuzz framework; crashes reproduced at DMAR_FECTL_REG, DMAR_IECTL_REG, DMAR_IEADDR_REG and DMAR_PECTL_REG; all 25 sites share the same root cause.

---
 hw/i386/intel_iommu.c | 114 +++++++++++++++++++++++++++++++++---------
 1 file changed, 89 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +89 -25
index f395fa248c..1b91055eef 100644
@@ -21,6 +21,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/error-report.h"
+#include "qemu/log.h"
 #include "qemu/main-loop.h"
 #include "qapi/error.h"
 #include "hw/core/sysbus.h"
@@ -3713,7 +3714,9 @@ static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
         break;
 
     case DMAR_RTADDR_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         val = vtd_get_quad_raw(s, DMAR_RTADDR_REG) >> 32;
         break;
 
@@ -3728,12 +3731,16 @@ static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
         break;
 
     case DMAR_IQA_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         val = s->iq >> 32;
         break;
 
     case DMAR_PEUADDR_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         val = vtd_get_long_raw(s, DMAR_PEUADDR_REG);
         break;
 
@@ -3746,6 +3753,12 @@ static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
     }
 
     return val;
+
+err_width:
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "%s: invalid read size %u at offset 0x%" PRIx64 "\n",
+                  __func__, size, addr);
+    return (uint64_t)-1;
 }
 
 static void vtd_mem_write(void *opaque, hwaddr addr,
@@ -3779,7 +3792,9 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_CCMD_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_ccmd_write(s);
         break;
@@ -3795,13 +3810,17 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_IOTLB_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_iotlb_write(s);
         break;
 
     case DMAR_PEUADDR_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3815,27 +3834,35 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_IVA_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     /* Fault Status Register, 32-bit */
     case DMAR_FSTS_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_fsts_write(s);
         break;
 
     /* Fault Event Control Register, 32-bit */
     case DMAR_FECTL_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_fectl_write(s);
         break;
 
     /* Fault Event Data Register, 32-bit */
     case DMAR_FEDATA_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3854,13 +3881,17 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
 
     /* Fault Event Upper Address Register, 32-bit */
     case DMAR_FEUADDR_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     /* Protected Memory Enable Register, 32-bit */
     case DMAR_PMEN_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3874,7 +3905,9 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_RTADDR_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3889,7 +3922,9 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_IQT_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         /* 19:63 of IQT_REG is RsvdZ, do nothing here */
         break;
@@ -3905,39 +3940,51 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_IQA_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     /* Invalidation Completion Status Register, 32-bit */
     case DMAR_ICS_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_ics_write(s);
         break;
 
     /* Invalidation Event Control Register, 32-bit */
     case DMAR_IECTL_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_iectl_write(s);
         break;
 
     /* Invalidation Event Data Register, 32-bit */
     case DMAR_IEDATA_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     /* Invalidation Event Address Register, 32-bit */
     case DMAR_IEADDR_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     /* Invalidation Event Upper Address Register, 32-bit */
     case DMAR_IEUADDR_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3951,7 +3998,9 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_FRCD_REG_0_1:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
@@ -3966,7 +4015,9 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_FRCD_REG_0_3:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         /* May clear bit 127 (Fault), update PPF */
         vtd_update_fsts_ppf(s);
@@ -3981,18 +4032,24 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
         break;
 
     case DMAR_IRTA_REG_HI:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         break;
 
     case DMAR_PRS_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_prs_write(s);
         break;
 
     case DMAR_PECTL_REG:
-        assert(size == 4);
+        if (size != 4) {
+            goto err_width;
+        }
         vtd_set_long(s, addr, val);
         vtd_handle_pectl_write(s);
         break;
@@ -4004,6 +4061,13 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
             vtd_set_quad(s, addr, val);
         }
     }
+
+    return;
+
+err_width:
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "%s: invalid write size %u at offset 0x%" PRIx64 "\n",
+                  __func__, size, addr);
 }
 
 static void vtd_prepare_identity_entry(hwaddr addr, IOMMUAccessFlags perm,
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/2 tests/qtest: Add regression test for intel-iommu 8-byte MMIO access

Sweep every 4-byte-aligned offset in the VT-d MMIO register space with 8-byte reads and writes to verify that no register handler aborts on an oversized access.

---
 tests/qtest/intel-iommu-test.c | 36 ++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
tests/qtest/intel-iommu-test.c +36 -0
index e5cc6acaf0..87a26b6910 100644
@@ -17,11 +17,45 @@
 #define ECAP_STAGE_1_FIXED1   (VTD_ECAP_QI |  VTD_ECAP_IR | VTD_ECAP_IRO | \
                               VTD_ECAP_MHMV | VTD_ECAP_SMTS | VTD_ECAP_FSTS)
 
+static inline uint32_t vtd_reg_readl(QTestState *s, uint64_t offset)
+{
+    return qtest_readl(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
+}
+
 static inline uint64_t vtd_reg_readq(QTestState *s, uint64_t offset)
 {
     return qtest_readq(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset);
 }
 
+static inline void vtd_reg_writeq(QTestState *s, uint64_t offset,
+                                  uint64_t value)
+{
+    qtest_writeq(s, Q35_HOST_BRIDGE_IOMMU_ADDR + offset, value);
+}
+
+/*
+ * Sweep every 4-byte-aligned offset in the VT-d register space with
+ * an 8-byte read and write to verify that no 32-bit-only register
+ * handler aborts on an oversized access.
+ */
+static void test_intel_iommu_8byte_access_sweep(void)
+{
+    QTestState *s;
+    uint64_t off;
+
+    s = qtest_init("-M q35 -device intel-iommu");
+
+    for (off = 0; off < DMAR_REG_SIZE; off += 4) {
+        vtd_reg_readq(s, off);
+        vtd_reg_writeq(s, off, 0);
+    }
+
+    /* Liveness check: QEMU must still respond after the sweep */
+    g_assert_cmpuint(vtd_reg_readl(s, DMAR_VER_REG), !=, 0);
+
+    qtest_quit(s);
+}
+
 static void test_intel_iommu_stage_1(void)
 {
     uint8_t init_csr[DMAR_REG_SIZE];     /* register values */
@@ -58,6 +92,8 @@ static void test_intel_iommu_stage_1(void)
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
+    qtest_add_func("/q35/intel-iommu/8byte-access-sweep",
+                   test_intel_iommu_8byte_access_sweep);
     qtest_add_func("/q35/intel-iommu/stage-1", test_intel_iommu_stage_1);
 
     return g_test_run();
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] tests/qtest: Add regression test for intel-iommu 8-byte MMIO access

Quoted 8 lines · Junjie Cao

Sweep every 4-byte-aligned offset in the VT-d MMIO register space with 8-byte reads and writes to verify that no register handler aborts on an oversized access.

---
  tests/qtest/intel-iommu-test.c | 36 ++++++++++++++++++++++++++++++++++
  1 file changed, 36 insertions(+)
#3 lore raw
In reply to Junjie Cao
Quoted 8 lines · Junjie Cao

vtd_mem_read() and vtd_mem_write() use assert(size == 4) for 32-bit-only VT-d registers. Since MemoryRegionOps .valid/.impl accepts sizes 4 through 8, an 8-byte guest access passes framework validation and reaches the handler, where the assertion aborts QEMU.

Narrowing .impl.max_access_size to 4 is not viable because VT-d has 64-bit registers (e.g. DMAR_CCMD_REG, DMAR_IQA_REG) that require atomic 8-byte access to trigger command execution.

So what about going the other way around, having a pure 64-bit implementation, so 64-bit accesses are atomic, and 32-bit ones are handled as 64-bit discarding one half:

-- >8 --
@@ -3916,13 +3916,13 @@ static const VMStateDescription vtd_vmstate = {
static const MemoryRegionOps vtd_mem_ops = {
.read = vtd_mem_read,
.write = vtd_mem_write,
.endianness = DEVICE_LITTLE_ENDIAN,
.impl = {
- .min_access_size = 4,
+ .min_access_size = 8,

         .max_access_size = 8,
     },
     .valid = {
         .min_access_size = 4,
         .max_access_size = 8,
     },

};
---

Quoted 15 lines

Replace all 22 asserts in vtd_mem_write() and 3 in vtd_mem_read() with a size check that logs via qemu_log_mask(LOG_GUEST_ERROR) and returns without performing the register access. The asymmetry (22 vs 3) is because vtd_mem_read() handles most registers through its default branch, so only three explicit cases carried an assert.

Found by fuzzing with QEMU's generic-fuzz framework; crashes reproduced at DMAR_FECTL_REG, DMAR_IECTL_REG, DMAR_IEADDR_REG and DMAR_PECTL_REG; all 25 sites share the same root cause.

---
  hw/i386/intel_iommu.c | 114 +++++++++++++++++++++++++++++++++---------
  1 file changed, 89 insertions(+), 25 deletions(-)
author
#4 lore raw

Hi all,

Thanks to Philippe for the suggestion - this direction makes the overall logic much cleaner.

I have drafted v2 locally: raising .impl.min_access_size from 4 to 8 so that the handler always sees size == 8, which eliminates all 25 assert(size == 4) call sites and every size-based branch. The low half of each 64-bit register pair now uses vtd_set_quad(), while the _HI cases stay with vtd_set_long(); for 32-bit registers, the wmask ensures that zero-extended 8-byte writes cannot clobber adjacent registers. The default branch falls back to vtd_set_long() where an 8-byte access would exceed the register file.

Currently replaying all 24 fuzzer-found crash inputs, will also run an 8-hour generic-fuzz, and testing with a qtest that sweeps every 4-byte-aligned offset with 8-byte reads and writes. Will send v2 once everything checks out.

Feedback is very welcome, whether on this thread or on v2.

Many thanks,
Junjie

#5 lore raw
Quoted 5 lines · Junjie Cao

Hi all,

Thanks to Philippe for the suggestion - this direction makes the overall logic much cleaner.

Quoted 8 lines

I have drafted v2 locally: raising .impl.min_access_size from 4 to 8 so that the handler always sees size == 8, which eliminates all 25 assert(size == 4) call sites and every size-based branch. The low half of each 64-bit register pair now uses vtd_set_quad(), while the _HI cases stay with vtd_set_long(); for 32-bit registers, the wmask ensures that zero-extended 8-byte writes cannot clobber adjacent registers. The default branch falls back to vtd_set_long() where an 8-byte access would exceed the register file.

Great, this is exactly what I had in mind :)

Quoted 6 lines

Currently replaying all 24 fuzzer-found crash inputs, will also run an 8-hour generic-fuzz, and testing with a qtest that sweeps every 4-byte-aligned offset with 8-byte reads and writes. Will send v2 once everything checks out.

Feedback is very welcome, whether on this thread or on v2.

Sure, keep me Cc'ed.

Regards,

Phil.

Quoted 3 lines

Many thanks,
Junjie