Skip to contentSkip to conversation

pseries: Fix possible out-of-bounds error

PATCH In review Stefan Weil 2 participants 3 messages Nov 21, 2011 – Nov 29, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pseries: Fix possible out-of-bounds error

PCI_NUM_REGIONS is 7, ARRAY_SIZE(bars) is 6. Either bars must be extended by a 7th array element, or the loop which reads bars[i] must terminate before it tries to read bars[6].

For dev->io_regions[6].size == 0, the old code also works, but it is not obvious whether this is always true.

This bug was detected by cppcheck.

---
 hw/spapr_pci.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/spapr_pci.c +1 -1
index 7162588..cefde38 100644
@@ -454,7 +454,7 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,
         reg[0].size = 0;
 
         n = 0;
-        for (i = 0; i < PCI_NUM_REGIONS; ++i) {
+        for (i = 0; i < PCI_NUM_REGIONS && i < ARRAY_SIZE(bars); ++i) {
             if (0 == dev->io_regions[i].size) {
                 continue;
             }
Signature · 1 line
1.7.2.5
#1 lore raw

Am 21.11.2011 20:25, schrieb Stefan Weil:

Quoted 21 lines

PCI_NUM_REGIONS is 7, ARRAY_SIZE(bars) is 6. Either bars must be extended by a 7th array element, or the loop which reads bars[i] must terminate before it tries to read bars[6].

For dev->io_regions[6].size == 0, the old code also works, but it is not obvious whether this is always true.

This bug was detected by cppcheck.

---
  hw/spapr_pci.c |    2 +-
  1 files changed, 1 insertions(+), 1 deletions(-)
hw/spapr_pci.c +0 -0
index 7162588..cefde38 100644

hw/spapr_pci.c int spapr_populate_pci_devices(sPAPRPHBState *phb, · line 460

Quoted 8 lines
hw/spapr_pci.c +1 -1
@@ -454,7 +454,7 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,
          reg[0].size = 0;
 
          n = 0;
-        for (i = 0; i<  PCI_NUM_REGIONS; ++i) {
+        for (i = 0; i<  PCI_NUM_REGIONS&&  i<  ARRAY_SIZE(bars); ++i) {
              if (0 == dev->io_regions[i].size) {
                  continue;
              }

Ping? Can this be fixed in QEMU 1.0?

#2 lore raw
Quoted 23 lines · Stefan Weil

Am 21.11.2011 20:25, schrieb Stefan Weil:

Quoted 22 lines

PCI_NUM_REGIONS is 7, ARRAY_SIZE(bars) is 6. Either bars must be extended by a 7th array element, or the loop which reads bars[i] must terminate before it tries to read bars[6].

For dev->io_regions[6].size == 0, the old code also works, but it is not obvious whether this is always true.

This bug was detected by cppcheck.

---
 hw/spapr_pci.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/spapr_pci.c +0 -0
index 7162588..cefde38 100644
@@ -454,7 +454,7 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,
         reg[0].size = 0;
 
Quoted 8 lines

hw/spapr_pci.c int spapr_populate_pci_devices(sPAPRPHBState *phb, · line 460

Quoted 6 lines
hw/spapr_pci.c +1 -1
         n = 0;
-        for (i = 0; i<  PCI_NUM_REGIONS; ++i) {
+        for (i = 0; i<  PCI_NUM_REGIONS&&  i<  ARRAY_SIZE(bars); ++i) {
             if (0 == dev->io_regions[i].size) {
                 continue;
             }

Ping? Can this be fixed in QEMU 1.0?

Sorry, for some reason I missed the initial post of this patch.

Alexey who wrote most of the spapr_pci code actually found and fixed this already, but we had a bit of a miscommunication so I hadn't realised to send it upstream yet. I'll send a fix shortly for 1.0.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson