memory: Add function pointers checks to memory_region_read/write()
PATCH 1/1 memory: Add function pointers checks to memory_region_read/write()
The file memory.c directly calls the function pointers provided in
the MemoryRegionOps to handle read and write operations for memory regions. The function pointers are called without checking if the function pointers are initialised, therefore, causing QEMU to SIGSEGV when accessing a memory address for which the operation is not defined (and not initialised)
The patch adds explicit checks to function pointers before issuing the calls.
--- memory.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
memory.c +20 -0
@@ -380,6 +380,9 @@ static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr, {uint64_t tmp; +if (!mr->ops->old_mmio.read) { +return MEMTX_ERROR ; + } tmp = mr->ops->old_mmio.read[ctz32 (size)](mr->opaque, addr);trace_memory_region_ops_read (mr, addr, tmp, size); *value |= (tmp & mask) << shift;
@@ -396,6 +399,9 @@ static MemTxResult memory_region_read_accessor(MemoryRegion *mr, {uint64_t tmp; +if (!mr->ops->read) { +return MEMTX_ERROR ; + } tmp = mr->ops->read (mr->opaque, addr, size);trace_memory_region_ops_read (mr, addr, tmp, size); *value |= (tmp & mask) << shift;
@@ -413,6 +419,9 @@ static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,uint64_t tmp =0 ;MemTxResult r; +if (!mr->ops->read_with_attrs) { +return MEMTX_ERROR ; + } r = mr->ops->read_with_attrs (mr->opaque, addr, &tmp, size, attrs);trace_memory_region_ops_read (mr, addr, tmp, size); *value |= (tmp & mask) << shift;
@@ -429,6 +438,9 @@ static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr, {uint64_t tmp; +if (!mr->ops->old_mmio.write) { +return MEMTX_ERROR ; + } tmp = (*value >> shift) & mask;trace_memory_region_ops_write (mr, addr, tmp, size); mr->ops->old_mmio.write[ctz32 (size)](mr->opaque, addr, tmp);
@@ -447,6 +459,9 @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;trace_memory_region_ops_write (mr, addr, tmp, size); +if (!mr->ops->write) { +return MEMTX_ERROR ; + } mr->ops->write (mr->opaque, addr, tmp, size);return MEMTX_OK ; }
@@ -463,6 +478,9 @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;trace_memory_region_ops_write (mr, addr, tmp, size); +if (!mr->ops->write_with_attrs) { +return MEMTX_ERROR ; + }return mr->ops->write_with_attrs (mr->opaque, addr, tmp, size, attrs); }
@@ -1187,6 +1205,8 @@ void memory_region_init_io(MemoryRegion *mr, uint64_t size) {memory_region_init (mr, owner, name, size); +assert (ops !=NULL ); + mr->ops = ops; mr->opaque = opaque; mr->terminates = true;
Signature · 1 line
2.1.4
Quoted 7 lines · Salva Peiró
The file
memory.cdirectly calls the function pointers provided in
the MemoryRegionOps to handle read and write operations for memory regions. The function pointers are called without checking if the function pointers are initialised, therefore, causing QEMU to SIGSEGV when accessing a memory address for which the operation is not defined (and not initialised)The patch adds explicit checks to function pointers before issuing the calls.
What device are you encountering this for? Perhaps this should be done in memory_region_init_io instead, so that it is detected early.
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 8 lines · Salva Peiró
The file
memory.cdirectly calls the function pointers provided in the MemoryRegionOps to handle read and write operations for memory regions.
The function pointers are called without checking if the function pointers are initialised, therefore, causing QEMU to SIGSEGV when accessing a memory address for which the operation is not defined (and not initialised)
Quoted 8 lines
Quoted 2 lines
The patch adds explicit checks to function pointers before issuing the calls.
What device are you encountering this for? Perhaps this should be done in
memory_region_init_ioinstead, so that it is detected early.Paolo
Rigth, I should have started by providing the scenario where the fault occurs. The problem occurs performing a writeb to the BAR0 of device 1033:194. That is PCI_DEVICE_ID_NEC_UPD720200 0x0194 at hw/usb/hcd-xhci.c
I've attached tests/nec-usb-xhci-test.c that reproduces the scenario.
Best
--
salva
From e66ab9c9b9836f37866605acd2e1efda422b0e31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Salva=20Peir=C3=B3?= <[email protected]>
Date: Tue, 8 Sep 2015 08:40:20 +0200
Subject: [PATCH] tests/nec-usb-xhci-test.c
--- tests/Makefile | 2 + tests/nec-usb-xhci-test.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests/nec-usb-xhci-test.c
tests/Makefile +2 -0
@@ -416,6 +416,8 @@ tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(block-obj-y) libqemuutil.a libqemustub.a +tests/nec-usb-xhci-test$(EXESUF): tests/nec-usb-xhci-test.o $(libqos-pc-obj-y) $(qtest-obj-y) tests/libqtest.o libqemuutil.a libqemustub.a + ifeq ($(CONFIG_POSIX),y) LIBS += -lutil
tests/nec-usb-xhci-test.c new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.1.4
Quoted 5 lines · Salva Peiró
Rigth, I should have started by providing the scenario where the fault occurs. The problem occurs performing a writeb to the BAR0 of device 1033:194. That is
PCI_DEVICE_ID_NEC_UPD7202000x0194athw/usb/hcd-xhci.cI've attached
tests/nec-usb-xhci-test.cthat reproduces the scenario.
Thanks, this is a good addition. Please submit a fix to hcd-xhci.c and the test case!
Paolo