All of lore.kernel.org
 help / color / mirror / Atom feed
From: Klaus Jensen <its@irrelevant.dk>
To: qemu-devel@nongnu.org
Cc: "Fam Zheng" <fam@euphon.net>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Thomas Huth" <thuth@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	qemu-block@nongnu.org, "Laurent Vivier" <lvivier@redhat.com>,
	"Klaus Jensen" <k.jensen@samsung.com>,
	"Gollu Appalanaidu" <anaidu.gollu@samsung.com>,
	"Max Reitz" <mreitz@redhat.com>,
	"Keith Busch" <kbusch@kernel.org>,
	"Kevin Wolf" <kwolf@redhat.com>,
	"Klaus Jensen" <its@irrelevant.dk>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>
Subject: [PATCH v3 3/5] hw/nvme: fix out-of-bounds reads
Date: Wed, 14 Jul 2021 08:01:23 +0200	[thread overview]
Message-ID: <20210714060125.994882-4-its@irrelevant.dk> (raw)
In-Reply-To: <20210714060125.994882-1-its@irrelevant.dk>

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

Peter noticed that mmio access may read into the NvmeParams member in
the NvmeCtrl struct.

Fix the bounds check.

Reported-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
 hw/nvme/ctrl.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 8c305315f41c..0449cc4dee9b 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -5968,23 +5968,26 @@ static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
         /* should RAZ, fall through for now */
     }
 
-    if (addr < sizeof(n->bar)) {
-        /*
-         * When PMRWBM bit 1 is set then read from
-         * from PMRSTS should ensure prior writes
-         * made it to persistent media
-         */
-        if (addr == NVME_REG_PMRSTS &&
-            (NVME_PMRCAP_PMRWBM(n->bar.pmrcap) & 0x02)) {
-            memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
-        }
-        memcpy(&val, ptr + addr, size);
-    } else {
+    if (addr > sizeof(n->bar) - size) {
         NVME_GUEST_ERR(pci_nvme_ub_mmiord_invalid_ofs,
                        "MMIO read beyond last register,"
                        " offset=0x%"PRIx64", returning 0", addr);
+
+        return 0;
     }
 
+    /*
+     * When PMRWBM bit 1 is set then read from
+     * from PMRSTS should ensure prior writes
+     * made it to persistent media
+     */
+    if (addr == NVME_REG_PMRSTS &&
+        (NVME_PMRCAP_PMRWBM(n->bar.pmrcap) & 0x02)) {
+        memory_region_msync(&n->pmr.dev->mr, 0, n->pmr.dev->size);
+    }
+
+    memcpy(&val, ptr + addr, size);
+
     return val;
 }
 
-- 
2.32.0



  parent reply	other threads:[~2021-07-14  6:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14  6:01 [PATCH v3 0/5] hw/nvme: fix mmio read Klaus Jensen
2021-07-14  6:01 ` [PATCH v3 1/5] hw/nvme: split pmrmsc register into upper and lower Klaus Jensen
2021-07-14 11:35   ` Gollu Appalanaidu
2021-07-19  9:13   ` Peter Maydell
2021-07-19  9:32     ` Klaus Jensen
2021-07-14  6:01 ` [PATCH v3 2/5] hw/nvme: use symbolic names for registers Klaus Jensen
2021-07-14  9:08   ` Philippe Mathieu-Daudé
2021-07-14 11:42   ` Gollu Appalanaidu
2021-07-14  6:01 ` Klaus Jensen [this message]
2021-07-19  8:50   ` [PATCH v3 3/5] hw/nvme: fix out-of-bounds reads Stefan Hajnoczi
2021-07-19  9:15   ` Peter Maydell
2021-07-14  6:01 ` [PATCH v3 4/5] hw/nvme: fix mmio read Klaus Jensen
2021-07-19  9:52   ` Peter Maydell
2021-07-14  6:01 ` [PATCH v3 5/5] tests/qtest/nvme-test: add mmio read test Klaus Jensen
2021-07-14 11:58   ` Gollu Appalanaidu
2021-07-19  6:43 ` [PATCH v3 0/5] hw/nvme: fix mmio read Klaus Jensen
2021-07-19  8:52   ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210714060125.994882-4-its@irrelevant.dk \
    --to=its@irrelevant.dk \
    --cc=anaidu.gollu@samsung.com \
    --cc=fam@euphon.net \
    --cc=k.jensen@samsung.com \
    --cc=kbusch@kernel.org \
    --cc=kwolf@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.