Skip to contentSkip to conversation

pci: check address before reading configuration bytes

PATCH Superseded P J P 3 participants 3 messages Jun 3, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 pci: check address before reading configuration bytes

From: Prasad J Pandit <[email protected]>

While reading PCI configuration bytes, a guest may send an address towards the end of the configuration space. It may lead to an OOB access issue. Add check to ensure 'address + len' is within PCI configuration space.

---
 hw/pci/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/pci/pci.c +3 -1
index 70c66965f5..4429fa9401 100644
@@ -1385,7 +1385,9 @@ uint32_t pci_default_read_config(PCIDevice *d,
         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
         pcie_sync_bridge_lnk(d);
     }
-    memcpy(&val, d->config + address, len);
+    if (address + len <= pci_config_size(d)) {
+        memcpy(&val, d->config + address, len);
+    }
     return le32_to_cpu(val);
 }
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 6 lines · P J P

From: Prasad J Pandit <[email protected]>

While reading PCI configuration bytes, a guest may send an address towards the end of the configuration space. It may lead to an OOB access issue. Add check to ensure 'address + len' is within PCI configuration space.

A malicious guest triggering an OOB access in the host QEMU sounds like a significant security flaw. Do we have a CVE assigned for this ?

Quoted 28 lines
---
 hw/pci/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/pci/pci.c +3 -1
index 70c66965f5..4429fa9401 100644
@@ -1385,7 +1385,9 @@ uint32_t pci_default_read_config(PCIDevice *d,
         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
         pcie_sync_bridge_lnk(d);
     }
-    memcpy(&val, d->config + address, len);
+    if (address + len <= pci_config_size(d)) {
+        memcpy(&val, d->config + address, len);
+    }
     return le32_to_cpu(val);
 }
 
Signature · 1 line
2.26.2

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
In reply to P J P
Quoted 20 lines · P J P

From: Prasad J Pandit <[email protected]>

While reading PCI configuration bytes, a guest may send an address towards the end of the configuration space. It may lead to an OOB access issue. Add check to ensure 'address + len' is within PCI configuration space.

---
 hw/pci/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/pci/pci.c +0 -0
index 70c66965f5..4429fa9401 100644

hw/pci/pci.c uint32_t pci_default_read_config(PCIDevice *d, · line 1388

Quoted 5 lines
hw/pci/pci.c +1 -1
@@ -1385,7 +1385,9 @@ uint32_t pci_default_read_config(PCIDevice *d,
         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
         pcie_sync_bridge_lnk(d);
     }
-    memcpy(&val, d->config + address, len);
+    if (address + len <= pci_config_size(d)) {

We don't want to hide/ignore earlier bugs, so IMO the caller should check for access in range, and this function abort() as it should never been called with such arguments.

Quoted 6 lines
hw/pci/pci.c +2 -0
+        memcpy(&val, d->config + address, len);
+    }
     return le32_to_cpu(val);
 }