Skip to contentSkip to conversation

ppc/e500_pci: Fix an array overflow issue

PATCH Superseded Liu Yu 4 participants 7 messages Sep 27, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Liu Yu

author
#0 lore raw

PATCH 1/1 ppc/e500_pci: Fix an array overflow issue

---
 hw/ppce500_pci.c |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)
hw/ppce500_pci.c +16 -10
index 2db365d..3e24e85 100644
@@ -108,15 +108,18 @@ static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
 
     case PPCE500_PCI_IW3:
     case PPCE500_PCI_IW2:
-    case PPCE500_PCI_IW1:
+    case PPCE500_PCI_IW1: {
+        int idx = ((addr >> 5) & 0x3) - 1;
+
         switch (addr & 0xC) {
-        case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
-        case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
-        case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
-        case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
+        case PCI_PITAR: value = pci->pib[idx].pitar; break;
+        case PCI_PIWBAR: value = pci->pib[idx].piwbar; break;
+        case PCI_PIWBEAR: value = pci->pib[idx].piwbear; break;
+        case PCI_PIWAR: value = pci->pib[idx].piwar; break;
         default: break;
         };
         break;
+    }
 
     case PPCE500_PCI_GASKET_TIMR:
         value = pci->gasket_time;
@@ -164,15 +167,18 @@ static void pci_reg_write4(void *opaque, target_phys_addr_t addr,
 
     case PPCE500_PCI_IW3:
     case PPCE500_PCI_IW2:
-    case PPCE500_PCI_IW1:
+    case PPCE500_PCI_IW1: {
+        int idx = ((addr >> 5) & 0x3) - 1;
+
         switch (addr & 0xC) {
-        case PCI_PITAR: pci->pib[(addr >> 5) & 0x3].pitar = value; break;
-        case PCI_PIWBAR: pci->pib[(addr >> 5) & 0x3].piwbar = value; break;
-        case PCI_PIWBEAR: pci->pib[(addr >> 5) & 0x3].piwbear = value; break;
-        case PCI_PIWAR: pci->pib[(addr >> 5) & 0x3].piwar = value; break;
+        case PCI_PITAR: pci->pib[idx].pitar = value; break;
+        case PCI_PIWBAR: pci->pib[idx].piwbar = value; break;
+        case PCI_PIWBEAR: pci->pib[idx].piwbear = value; break;
+        case PCI_PIWAR: pci->pib[idx].piwar = value; break;
         default: break;
         };
         break;
+    }
 
     case PPCE500_PCI_GASKET_TIMR:
         pci->gasket_time = value;
Signature · 1 line
1.6.4
#1 lore raw
Quoted 1 line · Liu Yu

Patch description missing.

Also, please always CC [email protected] for patches concerning ppc.

Quoted 9 lines

---
hw/ppce500_pci.c | 26 ++++++++++++++++----------

1 files changed, 16 insertions(+), 10 deletions(-)
hw/ppce500_pci.c +0 -0
index 2db365d..3e24e85 100644

hw/ppce500_pci.c static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr) · line 112

Quoted 6 lines
hw/ppce500_pci.c +2 -1
@@ -108,15 +108,18 @@ static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
 
    case PPCE500_PCI_IW3:
    case PPCE500_PCI_IW2:
-    case PPCE500_PCI_IW1:
+    case PPCE500_PCI_IW1: {
+        int idx = ((addr >> 5) & 0x3) - 1;

So this is the main change, right? Why the -1? A guest could potentially access pib[-1] using this, no?

hw/ppce500_pci.c static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr) · line 118

Quoted 10 lines
hw/ppce500_pci.c +5 -4
+
        switch (addr & 0xC) {
-        case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
-        case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
-        case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
-        case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
+        case PCI_PITAR: value = pci->pib[idx].pitar; break;
+        case PCI_PIWBAR: value = pci->pib[idx].piwbar; break;
+        case PCI_PIWBEAR: value = pci->pib[idx].piwbear; break;
+        case PCI_PIWAR: value = pci->pib[idx].piwar; break;

I'm fairly sure this breaks checkpatch.pl.

Alex

#2 lore raw
Quoted 11 lines · Alexander Graf
Quoted 10 lines · Liu Yu

---
hw/ppce500_pci.c | 26 ++++++++++++++++----------

1 files changed, 16 insertions(+), 10 deletions(-)
hw/ppce500_pci.c +0 -0
index 2db365d..3e24e85 100644
@@ -108,15 +108,18 @@ static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
 
Quoted 7 lines

hw/ppce500_pci.c static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr) · line 112

Quoted 5 lines
hw/ppce500_pci.c +2 -1
    case PPCE500_PCI_IW3:
    case PPCE500_PCI_IW2:
-    case PPCE500_PCI_IW1:
+    case PPCE500_PCI_IW1: {
+        int idx = ((addr >> 5) & 0x3) - 1;

So this is the main change, right? Why the -1? A guest could potentially access pib[-1] using this, no?

Not with the values of addr that lead to this code. The -1 is because IW1/2/3 are 0x1e0/0x1c0/0x1a0. Previously IW1 would overflow the array.

Quoted 11 lines

hw/ppce500_pci.c static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr) · line 118

Quoted 9 lines
hw/ppce500_pci.c +4 -4
        switch (addr & 0xC) {
-        case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
-        case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
-        case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
-        case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
+        case PCI_PITAR: value = pci->pib[idx].pitar; break;
+        case PCI_PIWBAR: value = pci->pib[idx].piwbar; break;
+        case PCI_PIWBEAR: value = pci->pib[idx].piwbear; break;
+        case PCI_PIWAR: value = pci->pib[idx].piwar; break;

I'm fairly sure this breaks checkpatch.pl.

So does the original code...

If this is to be fixed, the outbound window switch should be fixed too (and made to use idx, for consistency).

-Scott

#3 lore raw
Quoted 20 lines · Scott Wood
Quoted 11 lines · Alexander Graf
Quoted 10 lines · Liu Yu

---
hw/ppce500_pci.c | 26 ++++++++++++++++----------

1 files changed, 16 insertions(+), 10 deletions(-)
hw/ppce500_pci.c +0 -0
index 2db365d..3e24e85 100644
@@ -108,15 +108,18 @@ static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
 
Quoted 7 lines

hw/ppce500_pci.c static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr) · line 112

Quoted 5 lines
hw/ppce500_pci.c +2 -1
   case PPCE500_PCI_IW3:
   case PPCE500_PCI_IW2:
-    case PPCE500_PCI_IW1:
+    case PPCE500_PCI_IW1: {
+        int idx = ((addr >> 5) & 0x3) - 1;

So this is the main change, right? Why the -1? A guest could potentially access pib[-1] using this, no?

Quoted 2 lines

Not with the values of addr that lead to this code. The -1 is because IW1/2/3 are 0x1e0/0x1c0/0x1a0. Previously IW1 would overflow the array.

We're matching on addr & 0xfe0 and do the switch based on that. Possible values are:

0x1a0
0x1c0
0x1e0

Then we >> 5 them.

0xd
0xe
0xf

... and & 0x3 them

0x1
0x2
0x0

... and apply -1:

0x0
0x1
-1

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
hw/ppce500_pci.c +4 -4
       switch (addr & 0xC) {
-        case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
-        case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
-        case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
-        case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
+        case PCI_PITAR: value = pci->pib[idx].pitar; break;
+        case PCI_PIWBAR: value = pci->pib[idx].piwbar; break;
+        case PCI_PIWBEAR: value = pci->pib[idx].piwbear; break;
+        case PCI_PIWAR: value = pci->pib[idx].piwar; break;
Quoted 6 lines
Quoted 1 line

I'm fairly sure this breaks checkpatch.pl.

So does the original code...

If this is to be fixed, the outbound window switch should be fixed too (and made to use idx, for consistency).

Yes, please. My preferred way to do this would be to send a cleanup patch that fixes the coding style issues first (can be in the same patch set) and then another patch for functional changes on top. Makes it easier to review.

Alex

#4 lore raw
Quoted 9 lines · Alexander Graf

0xd
0xe
0xf

... and & 0x3 them

0x1
0x2
0x0

That's a remarkably different AND function...

r~

#5 lore raw
Quoted 5 lines · Richard Henderson
Quoted 4 lines · Alexander Graf

0xd
0xe
0xf

Quoted 7 lines
Quoted 5 lines

... and & 0x3 them

0x1
0x2
0x0

That's a remarkably different AND function...

No, it's a typo. I typed % instead of & and didn't realize it. Bleks.

:)

Alex

#6 lore raw
In reply to Alexander Graf
Quoted 21 lines · Alexander Graf
Quoted 5 lines · Scott Wood
Quoted 1 line · Alexander Graf

So this is the main change, right? Why the -1? A guest could potentially access pib[-1] using this, no?

Not with the values of addr that lead to this code. The -1 is because IW1/2/3 are 0x1e0/0x1c0/0x1a0. Previously IW1 would overflow the array.

We're matching on addr & 0xfe0 and do the switch based on that. Possible values are:

0x1a0
0x1c0
0x1e0

Then we >> 5 them.

0xd
0xe
0xf

Quoted 5 lines

... and & 0x3 them

0x1
0x2
0x0

0xd & 0x3 = 1
0xe & 0x3 = 2
0xf & 0x3 = 3

-Scott