Skip to contentSkip to conversation

vfio/pci: Fix crash when BAR has zero size

PATCH In review Cédric Le Goater 2 participants 2 messages Nov 9

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/pci: Fix crash when BAR has zero size

When the host kernel fails to assign a BAR (broken BIOS "Above 4G Decoding" or ReBAR configuration), the VFIO kernel interface reports a zero size. In that case, vfio_region_setup() skips allocating region->mem, leaving it NULL.

Then, vfio_bar_quirk_setup() calls the BAR quirk probes and passes a NULL region->mem pointer to memory_region_add_subregion_overlap(), causing a segfault in memory_region_update_container_subregions().

Skip quirk setup in vfio_realize() for zero size regions.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2732

---
 hw/vfio/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/pci.c +3 -1
index aa03927bb783..765326b755f4 100644
@@ -1852,7 +1852,9 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)
     }
 
     for (i = 0; i < PCI_ROM_SLOT; i++) {
-        vfio_bar_quirk_setup(vdev, i);
+        if (vdev->bars[i].size) {
+            vfio_bar_quirk_setup(vdev, i);
+        }
     }
 
     if (!vfio_pci_irq_setup(vdev, errp)) {
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Cédric Le Goater

When the host kernel fails to assign a BAR (broken BIOS "Above 4G Decoding" or ReBAR configuration), the VFIO kernel interface reports a zero size. In that case, vfio_region_setup() skips allocating region->mem, leaving it NULL.

Then, vfio_bar_quirk_setup() calls the BAR quirk probes and passes a NULL region->mem pointer to memory_region_add_subregion_overlap(), causing a segfault in memory_region_update_container_subregions().

Quoted 7 lines

Skip quirk setup in vfio_realize() for zero size regions.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2732

---
  hw/vfio/pci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)