Skip to contentSkip to conversation

Replace assert with bound checks in PnvPHB3

PATCH In review Aditya Gupta 4 participants 8 messages Mar 26 – Jun 23

lore
Share & tools
lore
#0 lore raw

cover Replace assert with bound checks in PnvPHB3

Overview
========

PHB in Power8 supports 8 byte registers, and hence the ops structure allows accessing of 8 bytes in 'pnv_phb3_reg_ops'

Both 'pnv_phb3_reg_read' & 'pnv_phb3_reg_write' pass the arguments as is to 'pnv_phb3_config_{read,write}', if offset is PHB_CONFIG_DATA.

This when called with size as 8, causes following assert failure in 'pci_host_config_read_common' & 'pci_host_config_write_common':

    assert(len <= 4);

This patch series splits it into two patches:
1. Fix PnvPHB3 code so that it never calls pci_host_config_{read,write}_common with read/write size of >4, thus fixing the assert problem
2. Replace the assert with bounds checking

Patch #2 should not have any functional change, since the issue is already fixed by patch #1

Testing
=======

1. Git repo with the patches: https://gitlab.com/adi-g15-ibm/qemu/tree/fix_assert_phb3

2. Gitlab pipeline: https://gitlab.com/adi-g15-ibm/qemu/-/pipelines/2410981612

Quoted 2 lines

Note: One test is failing in the pipeline, but the log shows some cargo error, unrelated to the patch series

3. Testcase provided by Zexiang Zhang, this will fail without the patches:

  cat << "EOF" | ./qemu-system-ppc64 \
    -M powernv8 -accel tcg -cpu POWER8 \
    -display none \
    -serial none -monitor none \
    -qtest stdio -S \
    -d guest_errors -D /tmp/pci_len_pnv.log
  writeq 0x3fc009013c000 0x140
  writeq 0x3fc009013c010 0x8000000000000000
  writeq 0x3fc009013c000 0x130
  readq 0x3fc009013c010
  EOF

Aditya Gupta (2):
ppc/pnv_phb3: Error out on invalid config access hw/pci: Replace assert with bounds check and return

 hw/pci-host/pnv_phb3.c | 10 ++++++++++
 hw/pci/pci_host.c      | 14 ++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 ppc/pnv_phb3: Error out on invalid config access

PHB in Power8 supports 8 byte registers, and hence the ops structure allows accessing of 8 bytes in 'pnv_phb3_reg_ops'

Both 'pnv_phb3_reg_read' & 'pnv_phb3_reg_write' pass the arguments as is to 'pnv_phb3_config_{read,write}', if offset is PHB_CONFIG_DATA.

This when called with size as 8, causes following assert failure in 'pci_host_config_read_common' & 'pci_host_config_write_common':

    assert(len <= 4);

Validate that size is <=4, before jumping to pci_host_config_{read,write}_common

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3334

Fixes 9ae1329ee2fe ("ppc/pnv: Add models for POWER8 PHB3 PCIe Host bridge")
---
 hw/pci-host/pnv_phb3.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/pci-host/pnv_phb3.c +10 -0
index d6ab5153374f..4ffdb4ce31ae 100644
@@ -475,6 +475,11 @@ void pnv_phb3_reg_write(void *opaque, hwaddr off, uint64_t val, unsigned size)
 
     /* Special case configuration data */
     if ((off & 0xfffc) == PHB_CONFIG_DATA) {
+        if (size > 4) {
+            phb3_error(phb, "Invalid config access, offset: 0x%"PRIx64" size: %d",
+                      off, size);
+            return;
+        }
         pnv_phb3_config_write(phb, off & 0x3, size, val);
         return;
     }
@@ -597,6 +602,11 @@ uint64_t pnv_phb3_reg_read(void *opaque, hwaddr off, unsigned size)
     uint64_t val;
 
     if ((off & 0xfffc) == PHB_CONFIG_DATA) {
+        if (size > 4) {
+            phb3_error(phb, "Invalid config access, offset: 0x%"PRIx64" size: %d",
+                      off, size);
+            return ~0ull;
+        }
         return pnv_phb3_config_read(phb, off & 0x3, size);
     }
 
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] ppc/pnv_phb3: Error out on invalid config access

Hi Aditya,

hw/pci-host/pnv_phb3.c void pnv_phb3_reg_write(void *opaque, hwaddr off, uint64_t val, unsigned size) · line 482

Quoted 7 lines · Aditya Gupta
     /* Special case configuration data */
     if ((off & 0xfffc) == PHB_CONFIG_DATA) {
hw/pci-host/pnv_phb3.c +5 -0
+        if (size > 4) {
+            phb3_error(phb, "Invalid config access, offset: 0x%"PRIx64" size: %d",
+                      off, size);
+            return;
+        }

As per my understanding, this patch moves the check on assert early for pnv case. If the pci_host_config_xxx_common doesnt support this >4 size access, can we apply the logging change there directly, and maybe lift the assert?

~Shivang.

#3 lore raw

Re: [PATCH 1/2] ppc/pnv_phb3: Error out on invalid config access

In reply to Aditya Gupta
Quoted 26 lines · Aditya Gupta

PHB in Power8 supports 8 byte registers, and hence the ops structure allows accessing of 8 bytes in 'pnv_phb3_reg_ops'

Both 'pnv_phb3_reg_read' & 'pnv_phb3_reg_write' pass the arguments as is to 'pnv_phb3_config_{read,write}', if offset is PHB_CONFIG_DATA.

This when called with size as 8, causes following assert failure in 'pci_host_config_read_common' & 'pci_host_config_write_common':

     assert(len <= 4);

Validate that size is <=4, before jumping to pci_host_config_{read,write}_common

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3334

Fixes 9ae1329ee2fe ("ppc/pnv: Add models for POWER8 PHB3 PCIe Host bridge")
---
  hw/pci-host/pnv_phb3.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
hw/pci-host/pnv_phb3.c +0 -0
index d6ab5153374f..4ffdb4ce31ae 100644

hw/pci-host/pnv_phb3.c void pnv_phb3_reg_write(void *opaque, hwaddr off, uint64_t val, unsigned size) · line 478

Quoted 4 lines
hw/pci-host/pnv_phb3.c +1 -0
@@ -475,6 +475,11 @@ void pnv_phb3_reg_write(void *opaque, hwaddr off, uint64_t val, unsigned size)
 
      /* Special case configuration data */
      if ((off & 0xfffc) == PHB_CONFIG_DATA) {
+        if (size > 4) {

Shouldn't we be doing write/read in chunks of 4 bytes when requested size is 8 instead of returning from here?

regards,
Harsh

Quoted 19 lines
hw/pci-host/pnv_phb3.c +9 -0
+            phb3_error(phb, "Invalid config access, offset: 0x%"PRIx64" size: %d",
+                      off, size);
+            return;
+        }
          pnv_phb3_config_write(phb, off & 0x3, size, val);
          return;
      }
@@ -597,6 +602,11 @@ uint64_t pnv_phb3_reg_read(void *opaque, hwaddr off, unsigned size)
      uint64_t val;
 
      if ((off & 0xfffc) == PHB_CONFIG_DATA) {
+        if (size > 4) {
+            phb3_error(phb, "Invalid config access, offset: 0x%"PRIx64" size: %d",
+                      off, size);
+            return ~0ull;
+        }
          return pnv_phb3_config_read(phb, off & 0x3, size);
      }
 
#4 lore raw

PATCH 2/2 hw/pci: Replace assert with bounds check and return

In reply to Aditya Gupta

As reported in https://gitlab.com/qemu-project/qemu/-/work_items/3334,
callers of 'pci_host_config_{read,write}_common' can pass length as 8, causing an assert failure

The original issue with pnv_phb3 triggering the assert was fixed in a previous commit

Instead of asserting on invalid length, check if the length is valid (<=4), otherwise return (with the failure error code in read)

---
 hw/pci/pci_host.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
hw/pci/pci_host.c +12 -2
index 91e3885c7f62..2a7fdfa5636e 100644
@@ -81,7 +81,12 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
         return;
     }
 
-    assert(len <= 4);
+    if (len > 4) {
+        PCI_DPRINTF("%s: invalid length access: addr " HWADDR_FMT_plx " \
+            len %d val %"PRIx32"\n", __func__, addr, len, val);
+        return;
+    }
+
     /* non-zero functions are only exposed when function 0 is present,
      * allowing direct removal of unexposed functions.
      */
@@ -106,7 +111,12 @@ uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,
         return ~0x0;
     }
 
-    assert(len <= 4);
+    if (len > 4) {
+        PCI_DPRINTF("%s: invalid length access: addr " HWADDR_FMT_plx " \
+            len %d val %"PRIx32"\n", __func__, addr, len, val);
+        return ~0x0;
+    }
+
     /* non-zero functions are only exposed when function 0 is present,
      * allowing direct removal of unexposed functions.
      */
Signature · 1 line
2.53.0
#5 lore raw
In reply to Aditya Gupta

Ping,

Hello Zexiang, can you see if this fixes the issue you reported ?

Also adding shivang and nikhil to cc.

- Aditya G

Quoted 53 lines · Aditya Gupta

Overview
========

PHB in Power8 supports 8 byte registers, and hence the ops structure allows accessing of 8 bytes in 'pnv_phb3_reg_ops'

Both 'pnv_phb3_reg_read' & 'pnv_phb3_reg_write' pass the arguments as is to 'pnv_phb3_config_{read,write}', if offset is PHB_CONFIG_DATA.

This when called with size as 8, causes following assert failure in 'pci_host_config_read_common' & 'pci_host_config_write_common':

     assert(len <= 4);

This patch series splits it into two patches:
1. Fix PnvPHB3 code so that it never calls pci_host_config_{read,write}_common with read/write size of >4, thus fixing the assert problem
2. Replace the assert with bounds checking

Patch #2 should not have any functional change, since the issue is already fixed by patch #1

Testing
=======

1. Git repo with the patches: https://gitlab.com/adi-g15-ibm/qemu/tree/fix_assert_phb3

2. Gitlab pipeline: https://gitlab.com/adi-g15-ibm/qemu/-/pipelines/2410981612

Quoted 2 lines

Note: One test is failing in the pipeline, but the log shows some cargo error, unrelated to the patch series

3. Testcase provided by Zexiang Zhang, this will fail without the patches:

   cat << "EOF" | ./qemu-system-ppc64 \
     -M powernv8 -accel tcg -cpu POWER8 \
     -display none \
     -serial none -monitor none \
     -qtest stdio -S \
     -d guest_errors -D /tmp/pci_len_pnv.log
   writeq 0x3fc009013c000 0x140
   writeq 0x3fc009013c010 0x8000000000000000
   writeq 0x3fc009013c000 0x130
   readq 0x3fc009013c010
   EOF

Aditya Gupta (2):
ppc/pnv_phb3: Error out on invalid config access hw/pci: Replace assert with bounds check and return

  hw/pci-host/pnv_phb3.c | 10 ++++++++++
  hw/pci/pci_host.c      | 14 ++++++++++++--
  2 files changed, 22 insertions(+), 2 deletions(-)
#6 lore raw
In reply to Aditya Gupta
Quoted 14 lines · Aditya Gupta

Overview
========

PHB in Power8 supports 8 byte registers, and hence the ops structure allows accessing of 8 bytes in 'pnv_phb3_reg_ops'

Both 'pnv_phb3_reg_read' & 'pnv_phb3_reg_write' pass the arguments as is to 'pnv_phb3_config_{read,write}', if offset is PHB_CONFIG_DATA.

This when called with size as 8, causes following assert failure in 'pci_host_config_read_common' & 'pci_host_config_write_common':

     assert(len <= 4);
Quoted 7 lines

This patch series splits it into two patches:
1. Fix PnvPHB3 code so that it never calls pci_host_config_{read,write}_common with read/write size of >4, thus fixing the assert problem
2. Replace the assert with bounds checking

Patch #2 should not have any functional change, since the issue is already fixed by patch #1

Hi!

Is this a qemu-stable material?

Thanks,

/mjt

#7 lore raw
Quoted 6 lines · Michael Tokarev
Quoted 1 line · Aditya Gupta

<...snip...>

Hi!

Is this a qemu-stable material?

This isn't a critical fix, as I believe this can't be triggered with usual skiboot/kernel, as they don't do this invalid read.

This assert was originally triggered by a fuzzy tester, using qtest commands to send the read/write commands with size as 8, as reported in
https://gitlab.com/qemu-project/qemu/-/work_items/3334

Though since the fix is small and safe, I don't have any resistance to it, and okay with it going to qemu-stable.

Thanks,
- Aditya G

Quoted 3 lines

Thanks,

/mjt