hw/block/nvme: harden cmb access
authorKlaus Jensen <k.jensen@samsung.com>
Mon, 30 Mar 2020 21:23:15 +0000 (23:23 +0200)
committerKlaus Jensen <k.jensen@samsung.com>
Tue, 27 Oct 2020 06:24:47 +0000 (07:24 +0100)
Since the controller has only supported PRPs so far it has not been
required to check the ending address (addr + len - 1) of the CMB access
for validity since it has been guaranteed to be in range of the CMB.

This changes when the controller adds support for SGLs (next patch), so
add that check.

Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
hw/block/nvme.c

index 0e916d48d763f68f9fe9779a677d2003ab351854..c0f1f8ccd473b53d40e7d241aa90104fe46ed1bf 100644 (file)
@@ -142,7 +142,12 @@ static inline void *nvme_addr_to_cmb(NvmeCtrl *n, hwaddr addr)
 
 static int nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
 {
-    if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr)) {
+    hwaddr hi = addr + size - 1;
+    if (hi < addr) {
+        return 1;
+    }
+
+    if (n->bar.cmbsz && nvme_addr_is_cmb(n, addr) && nvme_addr_is_cmb(n, hi)) {
         memcpy(buf, nvme_addr_to_cmb(n, addr), size);
         return 0;
     }