hw/pci/pcie_sriov: register_vfs() called from pcie_sriov_post_load() does not clamp PCI_SRIOV_NUM_VF against PCI_SRIOV_TOTAL_VF

Host environment

  • Operating system: Debian GNU/Linux 12 (bookworm)

  • OS/kernel version: Linux 6.1.0-51-cloud-amd64 x86_64

  • Architecture: x86_64

  • QEMU flavor: qemu-system-x86_64

  • QEMU version: Verified on QEMU 9.0.2, 11.1.1, and current upstream master (73294d096cc591e122fde9e63db4739de6d84f2b, 2026-10-28)

  • QEMU command line:

    ./qemu-system-x86_64 -machine q35,accel=kvm -m 512M -incoming tcp:127.0.0.1:4444

Emulated/Virtualized environment

  • Operating system: Guest-agnostic (device state deserialization / post-migration access)
  • OS/kernel version: N/A
  • Architecture: x86_64 / arm64 (q35, virt with any SR-IOV-capable PCIe PF such as igb or nvme)

Description of problem

Tooling Disclosure (per QEMU bug reporting policy): This issue was identified with the assistance of a static analysis screener checking VMStateDescription deserialization invariants across QEMU device models, and was manually triaged, audited against upstream master (73294d096cc591e122fde9e63db4739de6d84f2b), and paired with a compiler-verified .post_load patch by the reporter prior to submission.

In hw/pci/pcie_sriov.c, pcie_sriov_config_write() checks val > pci_get_word(dev->config + sriov_cap + PCI_SRIOV_TOTAL_VF) only when the guest sets PCI_SRIOV_CTRL_VFE (VF Enable). However, while VF Enable is clear, PCI_SRIOV_NUM_VF is guest-writable in PCI config space, and during live migration dev->config is restored verbatim before pcie_sriov_post_load() invokes register_vfs(dev) (hw/pci/pcie_sriov.c:35-58):

static void register_vfs(PCIDevice *dev)
{
    uint16_t num_vfs;
    uint16_t i;
    uint16_t sriov_cap = dev->exp.sriov_cap;

    assert(sriov_cap > 0);
    num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);

    for (i = 0; i < num_vfs; i++) {
        dev->exp.sriov_pf.vf[i] = register_vf(dev, ...);
    }
}

Because dev->exp.sriov_pf.vf is allocated with total_vfs entries (PCI_SRIOV_TOTAL_VF) at PF realization, restoring a PCI config state where PCI_SRIOV_CTRL_VFE is set and num_vfs > total_vfs causes register_vfs() to write past the end of dev->exp.sriov_pf.vf[] on the destination host heap.

Steps to reproduce

  1. Start QEMU with an SR-IOV-capable PCIe device (e.g., igb or nvme with sriov_max_vfs configured).
  2. Restore a migration state where PCI_SRIOV_CTRL_VFE is set in PCI_SRIOV_CTRL and PCI_SRIOV_NUM_VF > PCI_SRIOV_TOTAL_VF.
  3. pcie_sriov_post_load() -> register_vfs() iterates i = 0 .. num_vfs - 1, overflowing dev->exp.sriov_pf.vf[].

Additional information

Proposed Fix Patch (05-hw-pci-pcie_sriov-numvfs-clamp.patch, verified against master 73294d096cc591e122fde9e63db4739de6d84f2b)

diff --git a/hw/pci/pcie_sriov.c b/hw/pci/pcie_sriov.c
--- a/hw/pci/pcie_sriov.c
+++ b/hw/pci/pcie_sriov.c
@@ -35,11 +35,24 @@ static void register_vfs(PCIDevice *dev)
 static void register_vfs(PCIDevice *dev)
 {
     uint16_t num_vfs;
+    uint16_t total_vfs;
     uint16_t i;
     uint16_t sriov_cap = dev->exp.sriov_cap;
 
     assert(sriov_cap > 0);
     num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
+    total_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_TOTAL_VF);
+
+    /*
+     * NumVFs is guest-writable while VF Enable is clear and is restored
+     * verbatim from the migration stream.  TotalVFs is read-only, so a
+     * malformed stream can otherwise make the loop below walk past the
+     * total_vfs sized dev->exp.sriov_pf.vf[] array.
+     */
+    if (num_vfs > total_vfs) {
+        num_vfs = total_vfs;
+        pci_set_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF, num_vfs);
+    }
 
     trace_sriov_register_vfs(dev->name, PCI_SLOT(dev->devfn),
                              PCI_FUNC(dev->devfn), num_vfs);

05-hw-pci-pcie_sriov-numvfs-clamp.patch