Skip to contentSkip to conversation

pci: fix pci_default_read_config().

PATCH In review Isaku Yamahata 2 participants 3 messages May 27, 2010 – May 28, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: fix pci_default_read_config().

address and config_size are both unsigned.
So check which is bigger before minus operation.
Otherwise the result of minus can be unexpected
big value.

---
 hw/pci.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
hw/pci.c +7 -2
index 3362842..39a6206 100644
@@ -988,9 +988,14 @@ uint32_t pci_default_read_config(PCIDevice *d,
                                  uint32_t address, int len)
 {
     uint32_t val = 0;
+    uint32_t config_size = pci_config_size(d);
     assert(len == 1 || len == 2 || len == 4);
-    len = MIN(len, pci_config_size(d) - address);
-    memcpy(&val, d->config + address, len);
+    if (address < config_size) {
+        len = MIN(len, config_size - address);
+        memcpy(&val, d->config + address, len);
+    } else {
+        val = ~0;
+    }
     return le32_to_cpu(val);
 }
 
Signature · 1 line
1.6.6.1
#1 lore raw
Quoted 6 lines · Isaku Yamahata

address and config_size are both unsigned.
So check which is bigger before minus operation.
Otherwise the result of minus can be unexpected
big value.

An this happen in practice? If yes, how?

Quoted 28 lines
---
 hw/pci.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
hw/pci.c +7 -2
index 3362842..39a6206 100644
@@ -988,9 +988,14 @@ uint32_t pci_default_read_config(PCIDevice *d,
                                  uint32_t address, int len)
 {
     uint32_t val = 0;
+    uint32_t config_size = pci_config_size(d);
     assert(len == 1 || len == 2 || len == 4);
-    len = MIN(len, pci_config_size(d) - address);
-    memcpy(&val, d->config + address, len);
+    if (address < config_size) {
+        len = MIN(len, config_size - address);
+        memcpy(&val, d->config + address, len);
+    } else {
+        val = ~0;
+    }
     return le32_to_cpu(val);
 }
 
Signature · 1 line
1.6.6.1
#2 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 lines · Isaku Yamahata

address and config_size are both unsigned.
So check which is bigger before minus operation.
Otherwise the result of minus can be unexpected
big value.

An this happen in practice? If yes, how?

Yes, it happens.

When MMCONFIG is enabled, Linux tries to determine the configuration space size of pci host bridget by accessing express extended area without checking pci express capability.
See pci_cfg_space_size_ext()/pci_cfg_space_size()
in linux/drivers/pci/probes.c

If the pci host bridge is not express capable, pci_default_read_config() accesses dev->config beyond its size.

Quoted 30 lines
Quoted 29 lines
---
 hw/pci.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
hw/pci.c +7 -2
index 3362842..39a6206 100644
@@ -988,9 +988,14 @@ uint32_t pci_default_read_config(PCIDevice *d,
                                  uint32_t address, int len)
 {
     uint32_t val = 0;
+    uint32_t config_size = pci_config_size(d);
     assert(len == 1 || len == 2 || len == 4);
-    len = MIN(len, pci_config_size(d) - address);
-    memcpy(&val, d->config + address, len);
+    if (address < config_size) {
+        len = MIN(len, config_size - address);
+        memcpy(&val, d->config + address, len);
+    } else {
+        val = ~0;
+    }
     return le32_to_cpu(val);
 }
 
Signature · 1 line
1.6.6.1
Signature · 1 line
yamahata