[PULL 16/44] hw/core/sysbus: Assert memory region index is in range

Laurent Vivier posted 44 patches 5 years, 5 months ago
There is a newer version of this series
[PULL 16/44] hw/core/sysbus: Assert memory region index is in range
Posted by Laurent Vivier 5 years, 5 months ago
From: Philippe Mathieu-Daudé <f4bug@amsat.org>

Devices incorrectly modelled might use invalid index while
calling sysbus_mmio_get_region(), leading to OOB access.
Help developers by asserting the index is in range.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20200806130945.21629-3-f4bug@amsat.org>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 hw/core/sysbus.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 77ab351ce1a8..294f90b7deef 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -199,6 +199,7 @@ void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
 
 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
 {
+    assert(n >= 0 && n < QDEV_MAX_MMIO);
     return dev->mmio[n].memory;
 }
 
-- 
2.26.2