All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hw/nvme: fix CVE-2021-3929
@ 2022-01-20  8:01 Klaus Jensen
  2022-01-20 15:10 ` Keith Busch
  0 siblings, 1 reply; 3+ messages in thread
From: Klaus Jensen @ 2022-01-20  8:01 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Klaus Jensen, Qiuhao Li, Klaus Jensen, Keith Busch,
	Philippe Mathieu-Daudé

From: Klaus Jensen <k.jensen@samsung.com>

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
Reported-by: Qiuhao Li <Qiuhao.Li@outlook.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 5f573c417b3d..9a79f6728867 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -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)) {
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] hw/nvme: fix CVE-2021-3929
  2022-01-20  8:01 [PATCH] hw/nvme: fix CVE-2021-3929 Klaus Jensen
@ 2022-01-20 15:10 ` Keith Busch
  2022-01-20 20:13   ` Klaus Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Keith Busch @ 2022-01-20 15:10 UTC (permalink / raw)
  To: Klaus Jensen
  Cc: Klaus Jensen, Philippe Mathieu-Daudé,
	qemu-devel, qemu-block, Qiuhao Li

On Thu, Jan 20, 2022 at 09:01:55AM +0100, Klaus Jensen wrote:
> +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.

Reviewed-by: Keith Busch <kbusch@kernel.org>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] hw/nvme: fix CVE-2021-3929
  2022-01-20 15:10 ` Keith Busch
@ 2022-01-20 20:13   ` Klaus Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Klaus Jensen @ 2022-01-20 20:13 UTC (permalink / raw)
  To: Keith Busch
  Cc: Klaus Jensen, Philippe Mathieu-Daudé,
	qemu-devel, qemu-block, Qiuhao Li

[-- Attachment #1: Type: text/plain, Size: 765 bytes --]

On Jan 20 07:10, Keith Busch wrote:
> On Thu, Jan 20, 2022 at 09:01:55AM +0100, Klaus Jensen wrote:
> > +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.
> 
> Reviewed-by: Keith Busch <kbusch@kernel.org>

Nice catch Keith. A comment would be wise!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-21  0:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-20  8:01 [PATCH] hw/nvme: fix CVE-2021-3929 Klaus Jensen
2022-01-20 15:10 ` Keith Busch
2022-01-20 20:13   ` Klaus Jensen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.