drop writes to read-only ram device & vfio regions
v5 cover drop writes to read-only ram device & vfio regions
guest writes to read-only memory regions need to be dropped.
patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions
patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions
patch 3 set read-only flag to mmap'd read-only vfio regions, so that guest writes to those regions would be trapped.
without patch 1, host qemu would then crash on guest write to those read-only regions.
with patch 1, host qemu would drop the writes.
Changelog:
v5:
-changed write handler of ram device memory region from .write to .write_with_attrs in patch 1 (Paolo)
(for vfio region in patch 2, I still keep the operations as .read & .write. the reasons are:
1. vfio_region_ops are for mmio/pio regions. the top level read/write dispatcher in kvm just ignores their return values. (the return value of address_space_rw() is just ignored)
2. there are a lot of callers to vfio_region_read() and vfio_region_write(), who actually do not care about the return values )
-minor changes on text format in error logs.
v4:
-instead of modifying tracing log, added qemu_log_mask(LOG_GUEST_ERROR...) to log guest writes to read-only regions (Philippe)
for
v3:
-refreshed and Cc Stefan for reviewing of tracing part
v2:
-split one big patches into smaller ones (Philippe)
-modify existing trace to record guest writes to read-only memory (Alex) -modify vfio_region_write() to drop guest writes to non-mmap'd read-only
region (Alex)
Yan Zhao (3):
memory: drop guest writes to read-only ram device regions hw/vfio: drop guest writes to ro regions hw/vfio: let read-only flag take effect for mmap'd regions
hw/vfio/common.c | 17 +++++++++++++++-- memory.c | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-)
Signature · 1 line
2.17.1
PATCH v5 1/3 memory: drop guest writes to read-only ram device regions
for ram device regions, drop guest writes if the regions is read-only.
--- memory.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
memory.c +12 -3
@@ -34,6 +34,7 @@#include "sysemu/accel.h" #include "hw/boards.h" #include "migration/vmstate.h" +#include "qemu/log.h" //#define DEBUG_UNASSIGNED
@@ -1307,12 +1308,19 @@ static uint64_t memory_region_ram_device_read(void *opaque,return data; } -static void memory_region_ram_device_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size) +static MemTxResult memory_region_ram_device_write (void *opaque,hwaddr addr, +uint64_t data,unsigned size, +MemTxAttrs attrs) {MemoryRegion *mr = opaque;trace_memory_region_ram_device_write (get_cpu_index (), mr, addr, data, size); +if (mr->readonly) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only ram device region addr 0x%" + HWADDR_PRIx" size %u\n" , addr, size); +return MEMTX_ERROR ; + }switch (size) {case 1 :
@@ -1328,11 +1336,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr, *(uint64_t *)(mr->ram_block->host + addr) = data;break ; } +return MEMTX_OK ; }static const MemoryRegionOps ram_device_mem_ops = { .read = memory_region_ram_device_read, - .write = memory_region_ram_device_write, + .write_with_attrs = memory_region_ram_device_write, .endianness =DEVICE_HOST_ENDIAN , .valid = { .min_access_size =1 ,
Signature · 1 line
2.17.1
Re: [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions
Quoted 22 lines · Yan Zhao
for ram device regions, drop guest writes if the regions is read-only.
Cc Philippe Mathieu-Daudé <[email protected]>Signed-off-by Yan Zhao <[email protected]>Signed-off-by Xin Zeng <[email protected]>--- memory.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)memory.c +1
-0@@ -34,6 +34,7 @@#include "sysemu/accel.h" #include "hw/boards.h" #include "migration/vmstate.h" +#include "qemu/log.h" //#define DEBUG_UNASSIGNED
memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1313
Quoted 8 lines
memory.c +3
-2@@ -1307,12 +1308,19 @@ static uint64_t memory_region_ram_device_read(void *opaque,return data; } -static void memory_region_ram_device_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size) +static MemTxResult memory_region_ram_device_write (void *opaque,hwaddr addr, +uint64_t data,unsigned size, +MemTxAttrs attrs)
Style alignment is now of and can be adjusted easily.
memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1321
Quoted 8 lines
memory.c +4
-0{MemoryRegion *mr = opaque;trace_memory_region_ram_device_write (get_cpu_index (), mr, addr, data, size); +if (mr->readonly) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only ram device region addr 0x%" + HWADDR_PRIx" size %u\n" , addr, size);
Style alignment of here too.
Otherwise:
Quoted 20 lines
memory.c +4
-1+return MEMTX_ERROR ; + }switch (size) {case 1 :@@ -1328,11 +1336,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr, *(uint64_t *)(mr->ram_block->host + addr) = data;break ; } +return MEMTX_OK ; }static const MemoryRegionOps ram_device_mem_ops = { .read = memory_region_ram_device_read, - .write = memory_region_ram_device_write, + .write_with_attrs = memory_region_ram_device_write, .endianness =DEVICE_HOST_ENDIAN , .valid = { .min_access_size =1 ,
Re: [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions
Quoted 43 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Yan Zhao
for ram device regions, drop guest writes if the regions is read-only.
Cc Philippe Mathieu-Daudé <[email protected]>Signed-off-by Yan Zhao <[email protected]>Signed-off-by Xin Zeng <[email protected]>--- memory.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)memory.c +1
-0@@ -34,6 +34,7 @@#include "sysemu/accel.h" #include "hw/boards.h" #include "migration/vmstate.h" +#include "qemu/log.h" //#define DEBUG_UNASSIGNED memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1313
Quoted 8 lines
memory.c +3
-2@@ -1307,12 +1308,19 @@ static uint64_t memory_region_ram_device_read(void *opaque,return data; } -static void memory_region_ram_device_write (void *opaque,hwaddr addr, -uint64_t data,unsigned size) +static MemTxResult memory_region_ram_device_write (void *opaque,hwaddr addr, +uint64_t data,unsigned size, +MemTxAttrs attrs)Style alignment is now of and can be adjusted easily.
memory.c static uint64_t memory_region_ram_device_read(void *opaque, · line 1321
Quoted 8 lines
memory.c +4
-0{MemoryRegion *mr = opaque;trace_memory_region_ram_device_write (get_cpu_index (), mr, addr, data, size); +if (mr->readonly) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only ram device region addr 0x%" + HWADDR_PRIx" size %u\n" , addr, size);
Quoted 4 lines
Style alignment of here too.
Otherwise:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks! I'll update it right now!
Quoted 22 lines
Quoted 21 lines
memory.c +4
-1+return MEMTX_ERROR ; + }switch (size) {case 1 :@@ -1328,11 +1336,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr, *(uint64_t *)(mr->ram_block->host + addr) = data;break ; } +return MEMTX_OK ; }static const MemoryRegionOps ram_device_mem_ops = { .read = memory_region_ram_device_read, - .write = memory_region_ram_device_write, + .write_with_attrs = memory_region_ram_device_write, .endianness =DEVICE_HOST_ENDIAN , .valid = { .min_access_size =1 ,
PATCH v5 2/3 hw/vfio: drop guest writes to ro regions
In reply to Yan Zhaofor vfio regions that are without write permission, drop guest writes to those regions.
--- hw/vfio/common.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
hw/vfio/common.c +11 -2
@@ -38,6 +38,7 @@#include "sysemu/reset.h" #include "trace.h" #include "qapi/error.h" +#include "qemu/log.h" VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list);
@@ -190,6 +191,16 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t qword; } buf; +trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); +if (!(region->flags &VFIO_REGION_INFO_FLAG_WRITE )) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only vfio region (%s:region%d" +"+0x%" HWADDR_PRIx" size %d)\n" , vbasedev->name, + region->nr, addr, size); + +return ; + } +switch (size) {case 1 : buf.byte = data;
@@ -215,8 +226,6 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } -trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); -/* * A read or write to a BAR always signals an INTx EOI. This will * do nothing if not pending (including not in INTx mode). We assume
Signature · 1 line
2.17.1
Re: [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions
Quoted 5 lines · Yan Zhao
for vfio regions that are without write permission, drop guest writes to those regions.
Cc Philippe Mathieu-Daudé <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>
The full domain name is redhat.com.
Quoted 45 lines
Signed-off-by Yan Zhao <[email protected]>Signed-off-by Xin Zeng <[email protected]>--- hw/vfio/common.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)hw/vfio/common.c +11
-2@@ -38,6 +38,7 @@#include "sysemu/reset.h" #include "trace.h" #include "qapi/error.h" +#include "qemu/log.h" VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list);@@ -190,6 +191,16 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t qword; } buf; +trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); +if (!(region->flags &VFIO_REGION_INFO_FLAG_WRITE )) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only vfio region (%s:region%d" +"+0x%" HWADDR_PRIx" size %d)\n" , vbasedev->name, + region->nr, addr, size); + +return ; + } +switch (size) {case 1 : buf.byte = data;@@ -215,8 +226,6 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } -trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); -/* * A read or write to a BAR always signals an INTx EOI. This will * do nothing if not pending (including not in INTx mode). We assume
Re: [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Yan Zhao
for vfio regions that are without write permission, drop guest writes to those regions.
Cc Philippe Mathieu-Daudé <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>The full domain name is redhat.com.
oops. really sorry....
Quoted 46 lines
Quoted 45 lines
Signed-off-by Yan Zhao <[email protected]>Signed-off-by Xin Zeng <[email protected]>--- hw/vfio/common.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)hw/vfio/common.c +11
-2@@ -38,6 +38,7 @@#include "sysemu/reset.h" #include "trace.h" #include "qapi/error.h" +#include "qemu/log.h" VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list);@@ -190,6 +191,16 @@ void vfio_region_write(void *opaque, hwaddr addr,uint64_t qword; } buf; +trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); +if (!(region->flags &VFIO_REGION_INFO_FLAG_WRITE )) { +qemu_log_mask (LOG_GUEST_ERROR , +"Invalid write to read only vfio region (%s:region%d" +"+0x%" HWADDR_PRIx" size %d)\n" , vbasedev->name, + region->nr, addr, size); + +return ; + } +switch (size) {case 1 : buf.byte = data;@@ -215,8 +226,6 @@ void vfio_region_write(void *opaque, hwaddr addr, addr, data, size); } -trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size); -/* * A read or write to a BAR always signals an INTx EOI. This will * do nothing if not pending (including not in INTx mode). We assume
PATCH v5 3/3 hw/vfio: let read-only flag take effect for mmap'd regions
In reply to Yan Zhaoalong side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called.
--- hw/vfio/common.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/vfio/common.c +4 -0
@@ -980,6 +980,10 @@ int vfio_region_mmap(VFIORegion *region) name, region->mmaps[i].size, region->mmaps[i].mmap);g_free (name); + +if (!(region->flags &VFIO_REGION_INFO_FLAG_WRITE )) { +memory_region_set_readonly (®ion->mmaps[i].mem, true); + }memory_region_add_subregion (region->mem, region->mmaps[i].offset, ®ion->mmaps[i].mem);
Signature · 1 line
2.17.1