Skip to contentSkip to conversation

Fix QEMU crashes when passing IGD to a guest VM under XEN

PATCH Superseded Grzegorz Uriasz 4 participants 8 messages Apr 28, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix QEMU crashes when passing IGD to a guest VM under XEN

Hi,

This patch series is a small subset of a bigger patch set spanning few projects aiming to isolate the GPU in QUBES OS to a dedicated security domain. I'm doing this together with 3 colleagues as part of our Bachelors thesis.

When passing an Intel Graphic Device to a HVM guest under XEN, QEMU sometimes crashes when starting the VM. It turns out that the code responsible for setting up the legacy VBIOS for the IGD contains a bug which results in a memcpy of an undefined size between the QEMU heap and the physical memory of the guest.

If the size of the memcpy is small enough qemu does not crash - this means that this bug is actually a small security issue - a hostile guest kernel might determine the memory layout of QEMU simply by looking at physical memory beyond 0xdffff - this defeats ASLR and might make exploitation easier if other issues were to be found.

The problem is the current mechanism for obtaining a copy of the ROM of the IGD.
We first allocate a buffer which holds the vbios - the size of which is obtained from sysfs.
We then try to read the rom from sysfs, if we fail then we just return without setting the size of the buffer. This would be ok if the size of the ROM reported by sysfs would be 0, but the size is always 32 pages as this corresponds to legacy memory ranges. It turns out that reading the ROM fails on every single device I've tested(spanning few generations of IGD), which means qemu never sets the size of the buffer and returns a valid pointer to code which basically does a memcpy of an undefined size.

I'm including two patches.
The first one fixes the security issue by making failing to read the ROM from sysfs fatal.
The second patch introduces a better method for obtaining the VBIOS. I've haven't yet seen a single device on which the old code was working, the new code basically creates a shadow copy directly by reading from /dev/mem - this should be fine as a quick grep of the codebase shows that this approach is already being used to handle MSI. I've tested the new code on few different laptops and it works fine and the guest VMS finally stopped complaining that the VBIOS tables are missing.

Grzegorz Uriasz (2):
Fix undefined behaviour
Improve legacy vbios handling

 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c | 13 +++++------
 3 files changed, 57 insertions(+), 12 deletions(-)
Signature · 1 line
2.26.1
#1 lore raw

PATCH 1/2 Fix undefined behaviour

---
 hw/xen/xen_pt_load_rom.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/xen/xen_pt_load_rom.c +5 -6
index a50a80837e..9f100dc159 100644
@@ -38,12 +38,12 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
     fp = fopen(rom_file, "r+");
     if (fp == NULL) {
         if (errno != ENOENT) {
-            error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
+            warn_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
         }
         return NULL;
     }
     if (fstat(fileno(fp), &st) == -1) {
-        error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
+        warn_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
         goto close_rom;
     }
 
@@ -59,10 +59,9 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
     memset(ptr, 0xff, st.st_size);
 
     if (!fread(ptr, 1, st.st_size, fp)) {
-        error_report("pci-assign: Cannot read from host %s", rom_file);
-        error_printf("Device option ROM contents are probably invalid "
-                     "(check dmesg).\nSkip option ROM probe with rombar=0, "
-                     "or load from file with romfile=\n");
+        warn_report("pci-assign: Cannot read from host %s", rom_file);
+        memory_region_unref(&dev->rom);
+        ptr = NULL;
         goto close_rom;
     }
 
Signature · 1 line
2.26.1
#2 lore raw

RE: [PATCH 1/2] Fix undefined behaviour

Quoted 10 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 28 April 2020 07:29
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 1/2] Fix undefined behaviour

I think we need more of a commit comment for both this and patch #2 to explain why you are making the changes.

Paul

#3 lore raw

Re: [PATCH 1/2] Fix undefined behaviour

Quoted 9 lines · Paul Durrant
Quoted 9 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 28 April 2020 07:29
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 1/2] Fix undefined behaviour

Quoted 4 lines
Quoted 1 line

I think we need more of a commit comment for both this and patch #2 to explain why you are making the changes.

Paul

I agree Grzegorz should improve the commit messages. In the mean time see email with subject "[PATCH 0/2] Fix QEMU crashes when passing IGD to a guest VM under XEN", it contains quite detailed explanation for both "Fix undefined behaviour" and "Improve legacy vbios handling" patches.

Artur Puzio

#4 lore raw

RE: [PATCH 1/2] Fix undefined behaviour

Quoted 22 lines

-----Original Message-----
From: Artur Puzio <[email protected]>
Sent: 28 April 2020 10:41
To: [email protected]; 'Grzegorz Uriasz' <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; 'Stefano Stabellini' <[email protected]>; 'Anthony Perard' <[email protected]>; xen- [email protected]
Subject: Re: [PATCH 1/2] Fix undefined behaviour

Quoted 6 lines · Paul Durrant
Quoted 6 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 28 April 2020 07:29
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>;

Anthony

Quoted 5 lines
Quoted 4 lines

Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 1/2] Fix undefined behaviour

I think we need more of a commit comment for both this and patch #2 to explain why you are making

Quoted 9 lines

the changes.

Quoted 2 lines

Paul

I agree Grzegorz should improve the commit messages. In the mean time see email with subject "[PATCH 0/2] Fix QEMU crashes when passing IGD to a guest VM under XEN", it contains quite detailed explanation for both "Fix undefined behaviour" and "Improve legacy vbios handling" patches.

Ok. Can you please make sure maintainers are cc-ed on patch #0 too.

Paul

#5 lore raw

RE: [PATCH 1/2] Fix undefined behaviour

Quoted 35 lines

-----Original Message-----
From: Paul Durrant <[email protected]>
Sent: 28 April 2020 13:33
To: 'Artur Puzio' <[email protected]>; 'Grzegorz Uriasz' <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; 'Stefano Stabellini' <[email protected]>; 'Anthony Perard' <[email protected]>; xen- [email protected]
Subject: RE: [PATCH 1/2] Fix undefined behaviour

Quoted 26 lines

-----Original Message-----
From: Artur Puzio <[email protected]>
Sent: 28 April 2020 10:41
To: [email protected]; 'Grzegorz Uriasz' <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; 'Stefano Stabellini' <[email protected]>; 'Anthony Perard' <[email protected]>; xen- [email protected]
Subject: Re: [PATCH 1/2] Fix undefined behaviour

Quoted 6 lines · Paul Durrant
Quoted 6 lines

-----Original Message-----
From: Grzegorz Uriasz <[email protected]>
Sent: 28 April 2020 07:29
To: [email protected]
Cc: Grzegorz Uriasz <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Stefano Stabellini <[email protected]>;

Anthony

Quoted 5 lines
Quoted 4 lines

Perard <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 1/2] Fix undefined behaviour

I think we need more of a commit comment for both this and patch #2 to explain why you are making

the changes.

Quoted 2 lines

Paul

Quoted 8 lines
Quoted 5 lines

I agree Grzegorz should improve the commit messages. In the mean time see email with subject "[PATCH 0/2] Fix QEMU crashes when passing IGD to a guest VM under XEN", it contains quite detailed explanation for both "Fix undefined behaviour" and "Improve legacy vbios handling" patches.

Ok. Can you please make sure maintainers are cc-ed on patch #0 too.

Actually they are, sorry. My MUA is playing tricks on me.

Paul

#7 lore raw

PATCH 2/2 Improve legacy vbios handling

In reply to Grzegorz Uriasz
---
 hw/xen/xen_pt.c          |  8 +++++--
 hw/xen/xen_pt_graphics.c | 48 +++++++++++++++++++++++++++++++++++++---
 hw/xen/xen_pt_load_rom.c |  2 +-
 3 files changed, 52 insertions(+), 6 deletions(-)
hw/xen/xen_pt.c +6 -2
index b91082cb8b..ffc3559dd4 100644
@@ -483,8 +483,12 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd)
                    i, r->size, r->base_addr, type);
     }
 
-    /* Register expansion ROM address */
-    if (d->rom.base_addr && d->rom.size) {
+    /*
+     * Register expansion ROM address. If we are dealing with a ROM
+     * shadow copy for legacy vga devices then don't bother to map it
+     * as previous code creates a proper shadow copy
+     */
+    if (d->rom.base_addr && d->rom.size && !(is_igd_vga_passthrough(d))) {
         uint32_t bar_data = 0;
 
         /* Re-set BAR reported by OS, otherwise ROM can't be read. */
hw/xen/xen_pt_graphics.c +45 -3

73 lines not on this page · raw message

hw/xen/xen_pt_load_rom.c +1 -1
index 9f100dc159..8cd9aa84dc 100644
@@ -65,7 +65,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
         goto close_rom;
     }
 
-    pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
+    pci_register_bar(dev, PCI_ROM_SLOT, PCI_BASE_ADDRESS_SPACE_MEMORY, &dev->rom);
     dev->has_rom = true;
     *size = st.st_size;
 close_rom:
Signature · 1 line
2.26.1