All of lore.kernel.org
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: "Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Sriram Yagnaraman" <sriram.yagnaraman@ericsson.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Keith Busch" <kbusch@kernel.org>,
	"Klaus Jensen" <its@irrelevant.dk>,
	"Markus Armbruster" <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org,
	 Akihiko Odaki <akihiko.odaki@daynix.com>,
	qemu-stable@nongnu.org
Subject: [PATCH v8 01/15] hw/nvme: Use pcie_sriov_num_vfs()
Date: Wed, 28 Feb 2024 20:33:12 +0900	[thread overview]
Message-ID: <20240228-reuse-v8-1-282660281e60@daynix.com> (raw)
In-Reply-To: <20240228-reuse-v8-0-282660281e60@daynix.com>

nvme_sriov_pre_write_ctrl() used to directly inspect SR-IOV
configurations to know the number of VFs being disabled due to SR-IOV
configuration writes, but the logic was flawed and resulted in
out-of-bound memory access.

It assumed PCI_SRIOV_NUM_VF always has the number of currently enabled
VFs, but it actually doesn't in the following cases:
- PCI_SRIOV_NUM_VF has been set but PCI_SRIOV_CTRL_VFE has never been.
- PCI_SRIOV_NUM_VF was written after PCI_SRIOV_CTRL_VFE was set.
- VFs were only partially enabled because of realization failure.

It is a responsibility of pcie_sriov to interpret SR-IOV configurations
and pcie_sriov does it correctly, so use pcie_sriov_num_vfs(), which it
provides, to get the number of enabled VFs before and after SR-IOV
configuration writes.

Cc: qemu-stable@nongnu.org
Fixes: CVE-2024-26328
Fixes: 11871f53ef8e ("hw/nvme: Add support for the Virtualization Management command")
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 hw/nvme/ctrl.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index f026245d1e9e..7a56e7b79b4d 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -8466,36 +8466,26 @@ static void nvme_pci_reset(DeviceState *qdev)
     nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
 }
 
-static void nvme_sriov_pre_write_ctrl(PCIDevice *dev, uint32_t address,
-                                      uint32_t val, int len)
+static void nvme_sriov_post_write_config(PCIDevice *dev, uint16_t old_num_vfs)
 {
     NvmeCtrl *n = NVME(dev);
     NvmeSecCtrlEntry *sctrl;
-    uint16_t sriov_cap = dev->exp.sriov_cap;
-    uint32_t off = address - sriov_cap;
-    int i, num_vfs;
+    int i;
 
-    if (!sriov_cap) {
-        return;
-    }
-
-    if (range_covers_byte(off, len, PCI_SRIOV_CTRL)) {
-        if (!(val & PCI_SRIOV_CTRL_VFE)) {
-            num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
-            for (i = 0; i < num_vfs; i++) {
-                sctrl = &n->sec_ctrl_list.sec[i];
-                nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
-            }
-        }
+    for (i = pcie_sriov_num_vfs(dev); i < old_num_vfs; i++) {
+        sctrl = &n->sec_ctrl_list.sec[i];
+        nvme_virt_set_state(n, le16_to_cpu(sctrl->scid), false);
     }
 }
 
 static void nvme_pci_write_config(PCIDevice *dev, uint32_t address,
                                   uint32_t val, int len)
 {
-    nvme_sriov_pre_write_ctrl(dev, address, val, len);
+    uint16_t old_num_vfs = pcie_sriov_num_vfs(dev);
+
     pci_default_write_config(dev, address, val, len);
     pcie_cap_flr_write_config(dev, address, val, len);
+    nvme_sriov_post_write_config(dev, old_num_vfs);
 }
 
 static const VMStateDescription nvme_vmstate = {

-- 
2.43.2



  reply	other threads:[~2024-02-28 11:34 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-28 11:33 [PATCH v8 00/15] hw/pci: SR-IOV related fixes and improvements Akihiko Odaki
2024-02-28 11:33 ` Akihiko Odaki [this message]
2024-02-28 11:33 ` [PATCH v8 02/15] pcie_sriov: Validate NumVFs Akihiko Odaki
2024-02-28 16:08   ` Sriram Yagnaraman
2024-02-28 11:33 ` [PATCH v8 03/15] pcie_sriov: Reset SR-IOV extended capability Akihiko Odaki
2024-02-28 16:23   ` Sriram Yagnaraman
2024-02-29  2:27     ` Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 04/15] pcie_sriov: Do not reset NumVFs after disabling VFs Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 05/15] hw/pci: Always call pcie_sriov_pf_reset() Akihiko Odaki
2024-02-28 16:23   ` Sriram Yagnaraman
2024-02-28 11:33 ` [PATCH v8 06/15] hw/pci: Rename has_power to enabled Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 07/15] pcie_sriov: Do not manually unrealize Akihiko Odaki
2024-03-12 19:27   ` Michael S. Tsirkin
2024-02-28 11:33 ` [PATCH v8 08/15] pcie_sriov: Reuse SR-IOV VF device instances Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 09/15] pcie_sriov: Release VFs failed to realize Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 10/15] pcie_sriov: Remove num_vfs from PCIESriovPF Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 11/15] pcie_sriov: Register VFs after migration Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 12/15] hw/pci: Replace -1 with UINT32_MAX for romsize Akihiko Odaki
2024-02-28 11:33 ` [PATCH v8 13/15] hw/pci: Use UINT32_MAX as a default value for rombar Akihiko Odaki
2024-02-28 12:36   ` Markus Armbruster
2024-02-28 11:33 ` [PATCH v8 14/15] hw/pci: Determine if rombar is explicitly enabled Akihiko Odaki
2024-02-28 12:37   ` Markus Armbruster
2024-02-28 11:33 ` [PATCH v8 15/15] hw/qdev: Remove opts member Akihiko Odaki
2024-03-12 19:53 ` [PATCH v8 00/15] hw/pci: SR-IOV related fixes and improvements Michael S. Tsirkin

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=20240228-reuse-v8-1-282660281e60@daynix.com \
    --to=akihiko.odaki@daynix.com \
    --cc=alex.williamson@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=clg@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=its@irrelevant.dk \
    --cc=jasowang@redhat.com \
    --cc=kbusch@kernel.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=sriram.yagnaraman@ericsson.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.