Skip to contentSkip to conversation

hw/nvme: fix CVE-2021-3929

PATCH Superseded Klaus Jensen 2 participants 3 messages Jan 20, 2022 – Jan 21, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvme: fix CVE-2021-3929

From: Klaus Jensen <[email protected]>

This fixes CVE-2021-3929 "locally" by denying DMA to the iomem of the device itself. This still allows DMA to MMIO regions of other devices (e.g. doing P2P DMA to the controller memory buffer of another NVMe device).

Fixes CVE-2021-3929
---
 hw/nvme/ctrl.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/nvme/ctrl.c +14 -0
index 5f573c417b3d..9a79f6728867 100644
@@ -357,6 +357,16 @@ static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr)
     return memory_region_get_ram_ptr(&n->pmr.dev->mr) + (addr - n->pmr.cba);
 }
 
+static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr)
+{
+    hwaddr hi, lo;
+
+    lo = n->bar0.addr;
+    hi = lo + int128_get64(n->bar0.size);
+
+    return addr >= lo && addr < hi;
+}
+
 static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
 {
     hwaddr hi = addr + size - 1;
@@ -614,6 +624,10 @@ static uint16_t nvme_map_addr(NvmeCtrl *n, NvmeSg *sg, hwaddr addr, size_t len)
 
     trace_pci_nvme_map_addr(addr, len);
 
+    if (nvme_addr_is_iomem(n, addr)) {
+        return NVME_DATA_TRAS_ERROR;
+    }
+
     if (nvme_addr_is_cmb(n, addr)) {
         cmb = true;
     } else if (nvme_addr_is_pmr(n, addr)) {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

hw/nvme/ctrl.c static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr) · line 367

Quoted 8 lines · Klaus Jensen
hw/nvme/ctrl.c +8 -0
+static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr)
+{
+    hwaddr hi, lo;
+
+    lo = n->bar0.addr;
+    hi = lo + int128_get64(n->bar0.size);
+
+    return addr >= lo && addr < hi;

Looks fine considering this implementation always puts CMB in an exclusive BAR. From a spec consideration though, you can put a CMB at a BAR0 offset. I don't think that's going to happen anytime soon here, but may be worth a comment to notify this function needs to be updated if that assumption ever changes.

#2 lore raw
Quoted 16 lines · Keith Busch

hw/nvme/ctrl.c static inline void *nvme_addr_to_pmr(NvmeCtrl *n, hwaddr addr) · line 367

Quoted 8 lines · Klaus Jensen
hw/nvme/ctrl.c +8 -0
+static inline bool nvme_addr_is_iomem(NvmeCtrl *n, hwaddr addr)
+{
+    hwaddr hi, lo;
+
+    lo = n->bar0.addr;
+    hi = lo + int128_get64(n->bar0.size);
+
+    return addr >= lo && addr < hi;

Looks fine considering this implementation always puts CMB in an exclusive BAR. From a spec consideration though, you can put a CMB at a BAR0 offset. I don't think that's going to happen anytime soon here, but may be worth a comment to notify this function needs to be updated if that assumption ever changes.

Quoted 1 line

Nice catch Keith. A comment would be wise!