Skip to contentSkip to conversation

memory: assert and define MemoryRegionOps callbacks

PATCH Applied P J P 6 participants 29 messages Aug 11, 2020 – Feb 2, 2021 Arm i.MX and Layerscape

lore
Share & tools
lore

P J P

author
#0 lore raw

v4 cover memory: assert and define MemoryRegionOps callbacks

From: Prasad J Pandit <[email protected]>

Hello,

* This series asserts that MemoryRegionOps objects define read/write
callback methods. Thus avoids potential NULL pointer dereference.
ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2

* Also adds various undefined MemoryRegionOps read/write functions to avoid potential assert failure.

Thank you.
--
Prasad J Pandit (9):
hw/pci-host: add pci-intack write method
pci-host: designware: add pcie-msi read method
vfio: add quirk device write method
prep: add ppc-parity write method
nvram: add nrf51_soc flash read method
spapr_pci: add spapr msi read method
tz-ppc: add dummy read/write methods
imx7-ccm: add digprog mmio write method
memory: assert MemoryRegionOps callbacks are defined

 hw/misc/imx7_ccm.c       |  8 ++++++++
 hw/misc/tz-ppc.c         | 14 ++++++++++++++
 hw/nvram/nrf51_nvm.c     | 10 ++++++++++
 hw/pci-host/designware.c | 19 +++++++++++++++++++
 hw/pci-host/prep.c       |  8 ++++++++
 hw/ppc/prep_systemio.c   |  8 ++++++++
 hw/ppc/spapr_pci.c       | 14 ++++++++++++--
 hw/vfio/pci-quirks.c     |  8 ++++++++
 softmmu/memory.c         | 10 +++++++++-
 9 files changed, 96 insertions(+), 3 deletions(-)

--
2.26.2

author
#1 lore raw

PATCH v4 1/9 hw/pci-host: add pci-intack write method

From: Prasad J Pandit <[email protected]>

Add pci-intack mmio write method to avoid NULL pointer dereference issue.

---
 hw/pci-host/prep.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Update v4: add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05325.html

hw/pci-host/prep.c +8 -0
index 4b93fd2b01..64b33af6c0 100644
@@ -26,6 +26,7 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 #include "qemu/units.h"
+#include "qemu/log.h"
 #include "qapi/error.h"
 #include "hw/pci/pci.h"
 #include "hw/pci/pci_bus.h"
@@ -119,8 +120,15 @@ static uint64_t raven_intack_read(void *opaque, hwaddr addr,
     return pic_read_irq(isa_pic);
 }
 
+static void raven_intack_write(void *opaque, hwaddr addr,
+                                        uint64_t data, unsigned size)
+{
+    qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
+}
+
 static const MemoryRegionOps raven_intack_ops = {
     .read = raven_intack_read,
+    .write = raven_intack_write,
     .valid = {
         .max_access_size = 1,
     },
Signature · 1 line
2.26.2
author
#2 lore raw

PATCH v4 2/9 pci-host: designware: add pcie-msi read method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add pcie-msi mmio read method to avoid NULL pointer dereference issue.

---
 hw/pci-host/designware.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Update v4: add explanatory comment and Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05315.html

hw/pci-host/designware.c +19 -0
index 8492c18991..db036aac71 100644
@@ -21,6 +21,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
+#include "qemu/log.h"
 #include "hw/pci/msi.h"
 #include "hw/pci/pci_bridge.h"
 #include "hw/pci/pci_host.h"
@@ -63,6 +64,23 @@ designware_pcie_root_to_host(DesignwarePCIERoot *root)
     return DESIGNWARE_PCIE_HOST(bus->parent);
 }
 
+static uint64_t designware_pcie_root_msi_read(void *opaque, hwaddr addr,
+                                              unsigned size)
+{
+    /*
+     * Attempts to read from the MSI address are undefined in
+     * the PCI specifications. For this hardware, the datasheet
+     * specifies that a read from the magic address is simply not
+     * intercepted by the MSI controller, and will go out to the
+     * AHB/AXI bus like any other PCI-device-initiated DMA read.
+     * This is not trivial to implement in QEMU, so since
+     * well-behaved guests won't ever ask a PCI device to DMA from
+     * this address we just log the missing functionality.
+     */
+    qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
+    return 0;
+}
+
 static void designware_pcie_root_msi_write(void *opaque, hwaddr addr,
                                            uint64_t val, unsigned len)
 {
@@ -77,6 +95,7 @@ static void designware_pcie_root_msi_write(void *opaque, hwaddr addr,
 }
 
 static const MemoryRegionOps designware_pci_host_msi_ops = {
+    .read = designware_pcie_root_msi_read,
     .write = designware_pcie_root_msi_write,
     .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
Signature · 1 line
2.26.2
author
#3 lore raw

PATCH v4 3/9 vfio: add quirk device write method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add vfio quirk device mmio write method to avoid NULL pointer dereference issue.

---
 hw/vfio/pci-quirks.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Update v4: log guest_error and add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05326.html

hw/vfio/pci-quirks.c +8 -0
index 3a14b7c303..9b162cc3f9 100644
@@ -14,6 +14,7 @@
 #include "config-devices.h"
 #include "exec/memop.h"
 #include "qemu/units.h"
+#include "qemu/log.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 #include "qemu/module.h"
@@ -264,8 +265,15 @@ static uint64_t vfio_ati_3c3_quirk_read(void *opaque,
     return data;
 }
 
+static void vfio_ati_3c3_quirk_write(void *opaque, hwaddr addr,
+                                        uint64_t data, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
+}
+
 static const MemoryRegionOps vfio_ati_3c3_quirk = {
     .read = vfio_ati_3c3_quirk_read,
+    .write = vfio_ati_3c3_quirk_write,
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
Signature · 1 line
2.26.2
author
#4 lore raw

PATCH v4 4/9 prep: add ppc-parity write method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add ppc-parity mmio write method to avoid NULL pointer dereference issue.

---
 hw/ppc/prep_systemio.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Update v4: No change, v3 was acked
-> https://lists.gnu.org/archive/html/qemu-devel/2020-06/msg09965.html

hw/ppc/prep_systemio.c +8 -0
index bbc51b6e9a..e583222a1b 100644
@@ -23,6 +23,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/log.h"
 #include "hw/irq.h"
 #include "hw/isa/isa.h"
 #include "hw/qdev-properties.h"
@@ -235,8 +236,15 @@ static uint64_t ppc_parity_error_readl(void *opaque, hwaddr addr,
     return val;
 }
 
+static void ppc_parity_error_writel(void *opaque, hwaddr addr,
+                                    uint64_t data, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
+}
+
 static const MemoryRegionOps ppc_parity_error_ops = {
     .read = ppc_parity_error_readl,
+    .write = ppc_parity_error_writel,
     .valid = {
         .min_access_size = 4,
         .max_access_size = 4,
Signature · 1 line
2.26.2
Reviewed-by on v4 4/9
#5 lore raw

Re: [PATCH v4 4/9] prep: add ppc-parity write method

Quoted 9 lines · P J P <[email protected]> 于2020年8月11日周二 下午7:44写道:

From: Prasad J Pandit <[email protected]>

Add ppc-parity mmio write method to avoid NULL pointer dereference issue.

Quoted 38 lines
---
 hw/ppc/prep_systemio.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Update v4: No change, v3 was acked
-> https://lists.gnu.org/archive/html/qemu-devel/2020-06/msg09965.html

hw/ppc/prep_systemio.c +8 -1
index bbc51b6e9a..e583222a1b 100644
@@ -23,6 +23,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/log.h"
 #include "hw/irq.h"
 #include "hw/isa/isa.h"
 #include "hw/qdev-properties.h"
@@ -235,8 +236,15 @@ static uint64_t ppc_parity_error_readl(void *opaque, hwaddr addr,
     return val;
 }
 
+static void ppc_parity_error_writel(void *opaque, hwaddr addr,
+                                    uint64_t data, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
+}
+
 static const MemoryRegionOps ppc_parity_error_ops = {
     .read = ppc_parity_error_readl,
+    .write = ppc_parity_error_writel,
     .valid = {
         .min_access_size = 4,
         .max_access_size = 4,
--

2.26.2

author
#6 lore raw

PATCH v4 5/9 nvram: add nrf51_soc flash read method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add nrf51_soc mmio read method to avoid NULL pointer dereference issue.

---
 hw/nvram/nrf51_nvm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Update v4: add explanatory comment and Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05309.html

hw/nvram/nrf51_nvm.c +10 -0
index f2283c1a8d..7b3460d52d 100644
@@ -273,6 +273,15 @@ static const MemoryRegionOps io_ops = {
         .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
+static uint64_t flash_read(void *opaque, hwaddr offset, unsigned size)
+{
+    /*
+     * This is a rom_device MemoryRegion which is always in
+     * romd_mode (we never put it in MMIO mode), so reads always
+     * go directly to RAM and never come here.
+     */
+    g_assert_not_reached();
+}
 
 static void flash_write(void *opaque, hwaddr offset, uint64_t value,
         unsigned int size)
@@ -300,6 +309,7 @@ static void flash_write(void *opaque, hwaddr offset, uint64_t value,
 
 
 static const MemoryRegionOps flash_ops = {
+    .read = flash_read,
     .write = flash_write,
     .valid.min_access_size = 4,
     .valid.max_access_size = 4,
Signature · 1 line
2.26.2
Reviewed-by on v4 5/9
#7 lore raw

Re: [PATCH v4 5/9] nvram: add nrf51_soc flash read method

Quoted 9 lines · P J P <[email protected]> 于2020年8月11日周二 下午7:44写道:

From: Prasad J Pandit <[email protected]>

Add nrf51_soc mmio read method to avoid NULL pointer dereference issue.

Quoted 38 lines
---
 hw/nvram/nrf51_nvm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Update v4: add explanatory comment and Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05309.html

hw/nvram/nrf51_nvm.c +10 -1
index f2283c1a8d..7b3460d52d 100644
@@ -273,6 +273,15 @@ static const MemoryRegionOps io_ops = {
         .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
+static uint64_t flash_read(void *opaque, hwaddr offset, unsigned size)
+{
+    /*
+     * This is a rom_device MemoryRegion which is always in
+     * romd_mode (we never put it in MMIO mode), so reads always
+     * go directly to RAM and never come here.
+     */
+    g_assert_not_reached();
+}
 
 static void flash_write(void *opaque, hwaddr offset, uint64_t value,
         unsigned int size)
@@ -300,6 +309,7 @@ static void flash_write(void *opaque, hwaddr offset, uint64_t value,
 
 
 static const MemoryRegionOps flash_ops = {
+    .read = flash_read,
     .write = flash_write,
     .valid.min_access_size = 4,
     .valid.max_access_size = 4,
--

2.26.2

author
#8 lore raw

PATCH v4 6/9 spapr_pci: add spapr msi read method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add spapr msi mmio read method to avoid NULL pointer dereference issue.

---
 hw/ppc/spapr_pci.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Update v4: fix multi-line comment and log guest_error
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05311.html

hw/ppc/spapr_pci.c +12 -2
index 363cdb3f7b..53dfd3d8c6 100644
@@ -52,6 +52,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/hostmem.h"
 #include "sysemu/numa.h"
+#include "qemu/log.h"
 
 /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
 #define RTAS_QUERY_FN           0
@@ -738,6 +739,12 @@ static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
     return route;
 }
 
+static uint64_t spapr_msi_read(void *opaque, hwaddr addr, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
+    return 0;
+}
+
 /*
  * MSI/MSIX memory region implementation.
  * The handler handles both MSI and MSIX.
@@ -755,8 +762,11 @@ static void spapr_msi_write(void *opaque, hwaddr addr,
 }
 
 static const MemoryRegionOps spapr_msi_ops = {
-    /* There is no .read as the read result is undefined by PCI spec */
-    .read = NULL,
+    /*
+     * .read result is undefined by PCI spec.
+     * define .read method to avoid assert failure in memory_region_init_io
+     */
+    .read = spapr_msi_read,
     .write = spapr_msi_write,
     .endianness = DEVICE_LITTLE_ENDIAN
 };
Signature · 1 line
2.26.2
#9 lore raw

Re: [PATCH v4 6/9] spapr_pci: add spapr msi read method

Cc'ing PCI maintainers

Quoted 42 lines · P J P

From: Prasad J Pandit <[email protected]>

Add spapr msi mmio read method to avoid NULL pointer dereference issue.

---
 hw/ppc/spapr_pci.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Update v4: fix multi-line comment and log guest_error
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05311.html

hw/ppc/spapr_pci.c +7 -0
index 363cdb3f7b..53dfd3d8c6 100644
@@ -52,6 +52,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/hostmem.h"
 #include "sysemu/numa.h"
+#include "qemu/log.h"
 
 /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
 #define RTAS_QUERY_FN           0
@@ -738,6 +739,12 @@ static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
     return route;
 }
 
+static uint64_t spapr_msi_read(void *opaque, hwaddr addr, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
+    return 0;
+}
+
 /*
  * MSI/MSIX memory region implementation.
  * The handler handles both MSI and MSIX.

hw/ppc/spapr_pci.c static void spapr_msi_write(void *opaque, hwaddr addr, · line 769

Quoted 10 lines
hw/ppc/spapr_pci.c +5 -2
@@ -755,8 +762,11 @@ static void spapr_msi_write(void *opaque, hwaddr addr,
 }
 
 static const MemoryRegionOps spapr_msi_ops = {
-    /* There is no .read as the read result is undefined by PCI spec */
-    .read = NULL,
+    /*
+     * .read result is undefined by PCI spec.
+     * define .read method to avoid assert failure in memory_region_init_io
+     */
+    .read = spapr_msi_read,

Shouldn't this be a read_with_attrs handler returning MEMTX_ERROR instead? Maybe we need another MemTxResult which does not yet exist.

Quoted 4 lines
hw/ppc/spapr_pci.c +0 -0
     .write = spapr_msi_write,
     .endianness = DEVICE_LITTLE_ENDIAN
 };
author
#10 lore raw

Re: [PATCH v4 6/9] spapr_pci: add spapr msi read method

+-- On Tue, 11 Aug 2020, Philippe Mathieu-Daudé wrote --+
| Cc'ing PCI maintainers
... 
| > +    .read = spapr_msi_read,
|
| Shouldn't this be a read_with_attrs handler returning MEMTX_ERROR
| instead? Maybe we need another MemTxResult which does not yet exist.

Could this be a subsequent new patch? This patch series is reviewed/ack'd, could be included in the v5.1.0 release.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#11 lore raw

PATCH v4 7/9 tz-ppc: add dummy read/write methods

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add tz-ppc-dummy mmio read/write methods to avoid assert failure during initialisation.

---
 hw/misc/tz-ppc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

Update v4: add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05303.html

hw/misc/tz-ppc.c +14 -0
index 6431257b52..36495c68e7 100644
@@ -196,7 +196,21 @@ static bool tz_ppc_dummy_accepts(void *opaque, hwaddr addr,
     g_assert_not_reached();
 }
 
+static uint64_t tz_ppc_dummy_read(void *opaque, hwaddr addr, unsigned size)
+{
+    g_assert_not_reached();
+}
+
+static void tz_ppc_dummy_write(void *opaque, hwaddr addr,
+                                        uint64_t data, unsigned size)
+{
+    g_assert_not_reached();
+}
+
 static const MemoryRegionOps tz_ppc_dummy_ops = {
+    /* define r/w methods to avoid assert failure in memory_region_init_io */
+    .read = tz_ppc_dummy_read,
+    .write = tz_ppc_dummy_write,
     .valid.accepts = tz_ppc_dummy_accepts,
 };
 
Signature · 1 line
2.26.2
Reviewed-by on v4 7/9
#12 lore raw

Re: [PATCH v4 7/9] tz-ppc: add dummy read/write methods

Quoted 8 lines · P J P <[email protected]> 于2020年8月11日周二 下午7:44写道:

From: Prasad J Pandit <[email protected]>

Add tz-ppc-dummy mmio read/write methods to avoid assert failure during initialisation.

Quoted 36 lines
---
 hw/misc/tz-ppc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

Update v4: add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05303.html

hw/misc/tz-ppc.c +14 -1
index 6431257b52..36495c68e7 100644
@@ -196,7 +196,21 @@ static bool tz_ppc_dummy_accepts(void *opaque, hwaddr addr,
     g_assert_not_reached();
 }
 
+static uint64_t tz_ppc_dummy_read(void *opaque, hwaddr addr, unsigned size)
+{
+    g_assert_not_reached();
+}
+
+static void tz_ppc_dummy_write(void *opaque, hwaddr addr,
+                                        uint64_t data, unsigned size)
+{
+    g_assert_not_reached();
+}
+
 static const MemoryRegionOps tz_ppc_dummy_ops = {
+    /* define r/w methods to avoid assert failure in memory_region_init_io */
+    .read = tz_ppc_dummy_read,
+    .write = tz_ppc_dummy_write,
     .valid.accepts = tz_ppc_dummy_accepts,
 };
 
--

2.26.2

author
#13 lore raw

PATCH v4 8/9 imx7-ccm: add digprog mmio write method

In reply to P J P

From: Prasad J Pandit <[email protected]>

Add digprog mmio write method to avoid assert failure during initialisation.

---
 hw/misc/imx7_ccm.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Update v4: revise log message to guest_error
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05306.html

hw/misc/imx7_ccm.c +8 -0
index 02fc1ae8d0..075159e497 100644
@@ -131,8 +131,16 @@ static const struct MemoryRegionOps imx7_set_clr_tog_ops = {
     },
 };
 
+static void imx7_digprog_write(void *opaque, hwaddr addr,
+                                        uint64_t data, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "Guest write to read-only ANALOG_DIGPROG register\n");
+}
+
 static const struct MemoryRegionOps imx7_digprog_ops = {
     .read = imx7_set_clr_tog_read,
+    .write = imx7_digprog_write,
     .endianness = DEVICE_NATIVE_ENDIAN,
     .impl = {
         .min_access_size = 4,
Signature · 1 line
2.26.2
author
#14 lore raw

PATCH v4 9/9 memory: assert MemoryRegionOps callbacks are defined

In reply to P J P

From: Prasad J Pandit <[email protected]>

When registering a MemoryRegionOps object, assert that its read/write callback methods are defined. This avoids potential guest crash via a NULL pointer dereference.

---
 softmmu/memory.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Update v4: add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05324.html

softmmu/memory.c +9 -1
index af25987518..1f4b37b3a6 100644
@@ -1485,7 +1485,13 @@ void memory_region_init_io(MemoryRegion *mr,
                            uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops ? ops : &unassigned_mem_ops;
+    if (ops) {
+        assert(ops->read || ops->read_with_attrs);
+        assert(ops->write || ops->write_with_attrs);
+        mr->ops = ops;
+    } else {
+        mr->ops = &unassigned_mem_ops;
+    }
     mr->opaque = opaque;
     mr->terminates = true;
 }
@@ -1663,6 +1669,8 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
 {
     Error *err = NULL;
     assert(ops);
+    assert(ops->read || ops->read_with_attrs);
+    assert(ops->write || ops->write_with_attrs);
     memory_region_init(mr, owner, name, size);
     mr->ops = ops;
     mr->opaque = opaque;
Signature · 1 line
2.26.2
Reviewed-by on v4 9/9
#15 lore raw

Re: [PATCH v4 9/9] memory: assert MemoryRegionOps callbacks are defined

Quoted 10 lines · P J P

From: Prasad J Pandit <[email protected]>

When registering a MemoryRegionOps object, assert that its read/write callback methods are defined. This avoids potential guest crash via a NULL pointer dereference.

Quoted 36 lines
---
 softmmu/memory.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Update v4: add Reviewed-by tag
-> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05324.html

softmmu/memory.c +9 -1
index af25987518..1f4b37b3a6 100644
@@ -1485,7 +1485,13 @@ void memory_region_init_io(MemoryRegion *mr,
                            uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops ? ops : &unassigned_mem_ops;
+    if (ops) {
+        assert(ops->read || ops->read_with_attrs);
+        assert(ops->write || ops->write_with_attrs);
+        mr->ops = ops;
+    } else {
+        mr->ops = &unassigned_mem_ops;
+    }
     mr->opaque = opaque;
     mr->terminates = true;
 }
@@ -1663,6 +1669,8 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
 {
     Error *err = NULL;
     assert(ops);
+    assert(ops->read || ops->read_with_attrs);
+    assert(ops->write || ops->write_with_attrs);
     memory_region_init(mr, owner, name, size);
     mr->ops = ops;
     mr->opaque = opaque;
author
#16 lore raw

Re: [PATCH v4 9/9] memory: assert MemoryRegionOps callbacks are defined

+-- On Wed, 16 Sep 2020, Philippe Mathieu-Daudé wrote --+
| On 8/11/20 1:41 PM, P J P wrote:
| > When registering a MemoryRegionOps object, assert that its
| > read/write callback methods are defined. This avoids potential
| > guest crash via a NULL pointer dereference.
| > 
| > Suggested-by: Peter Maydell <[email protected]>
| > Reviewed-by: Li Qiang <[email protected]>
| > Reviewed-by: Peter Maydell <[email protected]>
| > Signed-off-by: Prasad J Pandit <[email protected]>
| 
| Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
| 
| > ---
| >  softmmu/memory.c | 10 +++++++++-
| >  1 file changed, 9 insertions(+), 1 deletion(-)
| > 
| > Update v4: add Reviewed-by tag
| >   -> https://lists.nongnu.org/archive/html/qemu-devel/2020-07/msg05324.html
| > 
| > diff --git a/softmmu/memory.c b/softmmu/memory.c
| > index af25987518..1f4b37b3a6 100644
| > --- a/softmmu/memory.c
| > +++ b/softmmu/memory.c
| > @@ -1485,7 +1485,13 @@ void memory_region_init_io(MemoryRegion *mr,
| >                             uint64_t size)
| >  {
| >      memory_region_init(mr, owner, name, size);
| > -    mr->ops = ops ? ops : &unassigned_mem_ops;
| > +    if (ops) {
| > +        assert(ops->read || ops->read_with_attrs);
| > +        assert(ops->write || ops->write_with_attrs);
| > +        mr->ops = ops;
| > +    } else {
| > +        mr->ops = &unassigned_mem_ops;
| > +    }
| >      mr->opaque = opaque;
| >      mr->terminates = true;
| >  }
| > @@ -1663,6 +1669,8 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
| >  {
| >      Error *err = NULL;
| >      assert(ops);
| > +    assert(ops->read || ops->read_with_attrs);
| > +    assert(ops->write || ops->write_with_attrs);
| >      memory_region_init(mr, owner, name, size);
| >      mr->ops = ops;
| >      mr->opaque = opaque;
| > 

@Paolo...ping!

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#17 lore raw
In reply to P J P
Quoted 10 lines · P J P

From: Prasad J Pandit <[email protected]>

Hello,

* This series asserts that MemoryRegionOps objects define read/write
callback methods. Thus avoids potential NULL pointer dereference.
ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2

* Also adds various undefined MemoryRegionOps read/write functions to avoid potential assert failure.

The overall idea seems fine. Looks like we could avoid a fair bit of boilerplate - and slightly reduce our binary size - by introducing a global unimplemented_write() function.

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
author
#18 lore raw
+-- On Thu, 13 Aug 2020, David Gibson wrote --+
| The overall idea seems fine.  Looks like we could avoid a fair bit of 
| boilerplate - and slightly reduce our binary size - by introducing a global 
| unimplemented_write() function.

* You mean for after this assert(3) in memory_region_init_io change is merged? This series attempts to define all missing r/w calls.

* There are also unassigned_mem_read/write functions, maybe those can be reused?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#19 lore raw
Quoted 4 lines · P J P
+-- On Thu, 13 Aug 2020, David Gibson wrote --+
| The overall idea seems fine.  Looks like we could avoid a fair bit of 
| boilerplate - and slightly reduce our binary size - by introducing a global 
| unimplemented_write() function.

IIRC in v2 or v3 it was mentioned each device has to be handled differently, as it behaves differently, it sits on a particular bus, and so on. So the preferred way is to have a device-specific handler.

(This explains v4 and why these patches took so long).

Quoted 13 lines

* You mean for after this assert(3) in memory_region_init_io change is merged? This series attempts to define all missing r/w calls.

* There are also unassigned_mem_read/write functions, maybe those can be reused?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#20 lore raw
In reply to P J P
Quoted 10 lines · P J P

From: Prasad J Pandit <[email protected]>

Hello,

* This series asserts that MemoryRegionOps objects define read/write
callback methods. Thus avoids potential NULL pointer dereference.
ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2

* Also adds various undefined MemoryRegionOps read/write functions to avoid potential assert failure.

What about read_with_attrs()/write_with_attrs()?
It seems they are part of the same problem.

Quoted 28 lines

Thank you.
--
Prasad J Pandit (9):
hw/pci-host: add pci-intack write method
pci-host: designware: add pcie-msi read method
vfio: add quirk device write method
prep: add ppc-parity write method
nvram: add nrf51_soc flash read method
spapr_pci: add spapr msi read method
tz-ppc: add dummy read/write methods
imx7-ccm: add digprog mmio write method
memory: assert MemoryRegionOps callbacks are defined

 hw/misc/imx7_ccm.c       |  8 ++++++++
 hw/misc/tz-ppc.c         | 14 ++++++++++++++
 hw/nvram/nrf51_nvm.c     | 10 ++++++++++
 hw/pci-host/designware.c | 19 +++++++++++++++++++
 hw/pci-host/prep.c       |  8 ++++++++
 hw/ppc/prep_systemio.c   |  8 ++++++++
 hw/ppc/spapr_pci.c       | 14 ++++++++++++--
 hw/vfio/pci-quirks.c     |  8 ++++++++
 softmmu/memory.c         | 10 +++++++++-
 9 files changed, 96 insertions(+), 3 deletions(-)

--
2.26.2

author
#21 lore raw
+-- On Sun, 16 Aug 2020, Philippe Mathieu-Daudé wrote --+
| On 8/11/20 1:41 PM, P J P wrote:
| > From: Prasad J Pandit <[email protected]>
| > * This series asserts that MemoryRegionOps objects define read/write
| >   callback methods. Thus avoids potential NULL pointer dereference.
| >   ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2
| > 
| > * Also adds various undefined MemoryRegionOps read/write functions
| >   to avoid potential assert failure.
| 
| What about read_with_attrs()/write_with_attrs()? It seems they are part of 
| the same problem.

* read/write_with_attrs function is called if read/write callback is not defined

  ../softmmu/memory.c
    if (mr->ops->write) {
                    ... memory_region_write_accessor, mr,
    } else {
                    ... memory_region_write_with_attrs_accessor,

So, defining read/write methods may also address read/write_with_attrs issue?

* $ grep -Eri -A 5 -B 5 '(\.read_with_attrs|\.write_with_attrs)' . | fpaste

-> https://paste.centos.org/view/386c9597

It doesn't show an occurrence where one of the read/write_with_attrs is missing.

* Nevertheless, if we need to define read/write_with_attrs routines, because memory_region_init_io() would assert(3) for them

could that be a subsequent patch series please?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#22 lore raw
Quoted 29 lines · P J P
+-- On Sun, 16 Aug 2020, Philippe Mathieu-Daudé wrote --+
| On 8/11/20 1:41 PM, P J P wrote:
| > From: Prasad J Pandit <[email protected]>
| > * This series asserts that MemoryRegionOps objects define read/write
| >   callback methods. Thus avoids potential NULL pointer dereference.
| >   ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2
| > 
| > * Also adds various undefined MemoryRegionOps read/write functions
| >   to avoid potential assert failure.
| 
| What about read_with_attrs()/write_with_attrs()? It seems they are part of 
| the same problem.

* read/write_with_attrs function is called if read/write callback is not defined

  ../softmmu/memory.c
    if (mr->ops->write) {
                    ... memory_region_write_accessor, mr,
    } else {
                    ... memory_region_write_with_attrs_accessor,

So, defining read/write methods may also address read/write_with_attrs issue?

* $ grep -Eri -A 5 -B 5 '(\.read_with_attrs|\.write_with_attrs)' . | fpaste

   -> https://paste.centos.org/view/386c9597
Quoted 7 lines

It doesn't show an occurrence where one of the read/write_with_attrs is missing.

* Nevertheless, if we need to define read/write_with_attrs routines, because memory_region_init_io() would assert(3) for them

could that be a subsequent patch series please?

Yes no problem, I was just wondering and wasn't sure.

author
#23 lore raw
+-- On Mon, 17 Aug 2020, Philippe Mathieu-Daudé wrote --+
| On 8/17/20 7:02 AM, P J P wrote:
| > +-- On Sun, 16 Aug 2020, Philippe Mathieu-Daudé wrote --+
| > | What about read_with_attrs()/write_with_attrs()? It seems they are part 
| > | of the same problem.
| > 
| > * read/write_with_attrs function is called if read/write callback is not 
| >   defined
| > 
| >   ../softmmu/memory.c
| >     if (mr->ops->write) {
| >                     ... memory_region_write_accessor, mr,
| >     } else {
| >                     ... memory_region_write_with_attrs_accessor,
| > 
| >   So, defining read/write methods may also address read/write_with_attrs 
| >   issue?
| > 
| > * $ grep -Eri -A 5 -B 5 '(\.read_with_attrs|\.write_with_attrs)' . | fpaste
| > 
| >    -> https://paste.centos.org/view/386c9597
| > 
| >   It doesn't show an occurrence where one of the read/write_with_attrs is 
| >   missing.
| > 
| > * Nevertheless, if we need to define read/write_with_attrs routines, because 
| >   memory_region_init_io() would assert(3) for them
| > 
| >   could that be a subsequent patch series please?
| 
| Yes no problem, I was just wondering and wasn't sure.

@Peter, @Paolo: (To confirm)
Do we ping/reach out to respective maintainers for merging this series?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#24 lore raw
+-- On Mon, 17 Aug 2020, P J P wrote --+
| +-- On Mon, 17 Aug 2020, Philippe Mathieu-Daudé wrote --+
| | On 8/17/20 7:02 AM, P J P wrote:
| | > +-- On Sun, 16 Aug 2020, Philippe Mathieu-Daudé wrote --+
| | > | What about read_with_attrs()/write_with_attrs()? It seems they are part 
| | > | of the same problem.
| | > 
| | > * read/write_with_attrs function is called if read/write callback is not 
| | >   defined
| | > 
| | >   ../softmmu/memory.c
| | >     if (mr->ops->write) {
| | >                     ... memory_region_write_accessor, mr,
| | >     } else {
| | >                     ... memory_region_write_with_attrs_accessor,
| | > 
| | >   So, defining read/write methods may also address read/write_with_attrs 
| | >   issue?
| | > 
| | > * $ grep -Eri -A 5 -B 5 '(\.read_with_attrs|\.write_with_attrs)' . | fpaste
| | > 
| | >    -> https://paste.centos.org/view/386c9597
| | > 
| | >   It doesn't show an occurrence where one of the read/write_with_attrs is 
| | >   missing.
| | > 
| | > * Nevertheless, if we need to define read/write_with_attrs routines, because 
| | >   memory_region_init_io() would assert(3) for them
| | > 
| | >   could that be a subsequent patch series please?
| | 
| | Yes no problem, I was just wondering and wasn't sure.
| 
| @Peter, @Paolo: (To confirm)
|    Do we ping/reach out to respective maintainers for merging this series?

Ping...!
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#25 lore raw
+-- On Thu, 27 Aug 2020, P J P wrote --+
| +-- On Mon, 17 Aug 2020, P J P wrote --+
| | +-- On Mon, 17 Aug 2020, Philippe Mathieu-Daudé wrote --+
| | | On 8/17/20 7:02 AM, P J P wrote:
| | | > +-- On Sun, 16 Aug 2020, Philippe Mathieu-Daudé wrote --+
| | | > | What about read_with_attrs()/write_with_attrs()? It seems they are part 
| | | > | of the same problem.
| | | > 
| | | > * read/write_with_attrs function is called if read/write callback is not 
| | | >   defined
| | | > 
| | | >   ../softmmu/memory.c
| | | >     if (mr->ops->write) {
| | | >                     ... memory_region_write_accessor, mr,
| | | >     } else {
| | | >                     ... memory_region_write_with_attrs_accessor,
| | | > 
| | | >   So, defining read/write methods may also address read/write_with_attrs 
| | | >   issue?
| | | > 
| | | > * $ grep -Eri -A 5 -B 5 '(\.read_with_attrs|\.write_with_attrs)' . | fpaste
| | | > 
| | | >    -> https://paste.centos.org/view/386c9597
| | | > 
| | | >   It doesn't show an occurrence where one of the read/write_with_attrs is 
| | | >   missing.
| | | > 
| | | > * Nevertheless, if we need to define read/write_with_attrs routines, because 
| | | >   memory_region_init_io() would assert(3) for them
| | | > 
| | | >   could that be a subsequent patch series please?
| | | 
| | | Yes no problem, I was just wondering and wasn't sure.
| | 
| | @Peter, @Paolo: (To confirm)
| |    Do we ping/reach out to respective maintainers for merging this series?
| 

Ping..!
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#26 lore raw
In reply to P J P
+-- On Tue, 11 Aug 2020, P J P wrote --+
| * This series asserts that MemoryRegionOps objects define read/write
|   callback methods. Thus avoids potential NULL pointer dereference.
|   ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2
| 
| * Also adds various undefined MemoryRegionOps read/write functions
|   to avoid potential assert failure.
| 
| Thank you.
| --
| Prasad J Pandit (9):
|   hw/pci-host: add pci-intack write method
|   pci-host: designware: add pcie-msi read method
|   vfio: add quirk device write method
|   prep: add ppc-parity write method
|   nvram: add nrf51_soc flash read method
|   spapr_pci: add spapr msi read method
|   tz-ppc: add dummy read/write methods
|   imx7-ccm: add digprog mmio write method
|   memory: assert MemoryRegionOps callbacks are defined
| 
|  hw/misc/imx7_ccm.c       |  8 ++++++++
|  hw/misc/tz-ppc.c         | 14 ++++++++++++++
|  hw/nvram/nrf51_nvm.c     | 10 ++++++++++
|  hw/pci-host/designware.c | 19 +++++++++++++++++++
|  hw/pci-host/prep.c       |  8 ++++++++
|  hw/ppc/prep_systemio.c   |  8 ++++++++
|  hw/ppc/spapr_pci.c       | 14 ++++++++++++--
|  hw/vfio/pci-quirks.c     |  8 ++++++++
|  softmmu/memory.c         | 10 +++++++++-
|  9 files changed, 96 insertions(+), 3 deletions(-)

Ping...@Peter, @David, @Paolo, @Herve, @Alex,

* This patch series is not pulled/merged yet, could you please help with it? (Pinging respective maintainers, as didn't get reply for last pings, not sure how to go about it.)

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#27 lore raw
+-- On Tue, 15 Sep 2020, P J P wrote --+
| +-- On Tue, 11 Aug 2020, P J P wrote --+
| | * This series asserts that MemoryRegionOps objects define read/write
| |   callback methods. Thus avoids potential NULL pointer dereference.
| |   ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2
| | 
| | * Also adds various undefined MemoryRegionOps read/write functions
| |   to avoid potential assert failure.
| | 
| | Thank you.
| | --
| | Prasad J Pandit (9):
| |   hw/pci-host: add pci-intack write method
| |   pci-host: designware: add pcie-msi read method
| |   vfio: add quirk device write method
| |   prep: add ppc-parity write method
| |   nvram: add nrf51_soc flash read method
| |   spapr_pci: add spapr msi read method
| |   tz-ppc: add dummy read/write methods
| |   imx7-ccm: add digprog mmio write method
| |   memory: assert MemoryRegionOps callbacks are defined
| | 
| |  hw/misc/imx7_ccm.c       |  8 ++++++++
| |  hw/misc/tz-ppc.c         | 14 ++++++++++++++
| |  hw/nvram/nrf51_nvm.c     | 10 ++++++++++
| |  hw/pci-host/designware.c | 19 +++++++++++++++++++
| |  hw/pci-host/prep.c       |  8 ++++++++
| |  hw/ppc/prep_systemio.c   |  8 ++++++++
| |  hw/ppc/spapr_pci.c       | 14 ++++++++++++--
| |  hw/vfio/pci-quirks.c     |  8 ++++++++
| |  softmmu/memory.c         | 10 +++++++++-
| |  9 files changed, 96 insertions(+), 3 deletions(-)
| 
| Ping...@Peter, @David, @Paolo, @Herve, @Alex,
| 
| * This patch series is not pulled/merged yet, could you please help with it?  

Ping..!
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#28 lore raw
Quoted 38 lines · P J P
+-- On Tue, 15 Sep 2020, P J P wrote --+
| +-- On Tue, 11 Aug 2020, P J P wrote --+
| | * This series asserts that MemoryRegionOps objects define read/write
| |   callback methods. Thus avoids potential NULL pointer dereference.
| |   ex. -> https://git.qemu.org/?p=qemu.git;a=commit;h=bb15013ef34617eb1344f5276292cadd326c21b2
| |
| | * Also adds various undefined MemoryRegionOps read/write functions
| |   to avoid potential assert failure.
| |
| | Thank you.
| | --
| | Prasad J Pandit (9):
| |   hw/pci-host: add pci-intack write method
| |   pci-host: designware: add pcie-msi read method
| |   vfio: add quirk device write method
| |   prep: add ppc-parity write method
| |   nvram: add nrf51_soc flash read method
| |   spapr_pci: add spapr msi read method
| |   tz-ppc: add dummy read/write methods
| |   imx7-ccm: add digprog mmio write method
| |   memory: assert MemoryRegionOps callbacks are defined
| |
| |  hw/misc/imx7_ccm.c       |  8 ++++++++
| |  hw/misc/tz-ppc.c         | 14 ++++++++++++++
| |  hw/nvram/nrf51_nvm.c     | 10 ++++++++++
| |  hw/pci-host/designware.c | 19 +++++++++++++++++++
| |  hw/pci-host/prep.c       |  8 ++++++++
| |  hw/ppc/prep_systemio.c   |  8 ++++++++
| |  hw/ppc/spapr_pci.c       | 14 ++++++++++++--
| |  hw/vfio/pci-quirks.c     |  8 ++++++++
| |  softmmu/memory.c         | 10 +++++++++-
| |  9 files changed, 96 insertions(+), 3 deletions(-)
|
| Ping...@Peter, @David, @Paolo, @Herve, @Alex,
|
| * This patch series is not pulled/merged yet, could you please help with it?
Quoted 5 lines

Ping..!
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

Queued, thanks!

Paolo