* [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions
2020-04-30 5:15 [PATCH v5 0/3] drop writes to read-only ram device & vfio regions Yan Zhao
@ 2020-04-30 5:19 ` Yan Zhao
2020-04-30 7:07 ` Philippe Mathieu-Daudé
2020-04-30 5:23 ` [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions Yan Zhao
2020-04-30 5:24 ` [PATCH v5 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Yan Zhao
2 siblings, 1 reply; 8+ messages in thread
From: Yan Zhao @ 2020-04-30 5:19 UTC (permalink / raw)
To: pbonzini, alex.williamson; +Cc: Yan Zhao, xin.zeng, philmd, qemu-devel
for ram device regions, drop guest writes if the regions is read-only.
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
Signed-off-by: Xin Zeng <xin.zeng@intel.com>
---
memory.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/memory.c b/memory.c
index 601b749906..90a748912f 100644
--- a/memory.c
+++ b/memory.c
@@ -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,
--
2.17.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions
2020-04-30 5:19 ` [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions Yan Zhao
@ 2020-04-30 7:07 ` Philippe Mathieu-Daudé
2020-04-30 7:02 ` Yan Zhao
0 siblings, 1 reply; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-04-30 7:07 UTC (permalink / raw)
To: Yan Zhao, pbonzini, alex.williamson; +Cc: xin.zeng, qemu-devel
On 4/30/20 7:19 AM, Yan Zhao wrote:
> for ram device regions, drop guest writes if the regions is read-only.
>
> Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> Signed-off-by: Xin Zeng <xin.zeng@intel.com>
> ---
> memory.c | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/memory.c b/memory.c
> index 601b749906..90a748912f 100644
> --- a/memory.c
> +++ b/memory.c
> @@ -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)
Style alignment is now of and can be adjusted easily.
> {
> 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:
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> + 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,
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions
2020-04-30 7:07 ` Philippe Mathieu-Daudé
@ 2020-04-30 7:02 ` Yan Zhao
0 siblings, 0 replies; 8+ messages in thread
From: Yan Zhao @ 2020-04-30 7:02 UTC (permalink / raw)
To: Philippe Mathieu-Daudé
Cc: pbonzini, alex.williamson, Zeng, Xin, qemu-devel
On Thu, Apr 30, 2020 at 03:07:21PM +0800, Philippe Mathieu-Daudé wrote:
> On 4/30/20 7:19 AM, Yan Zhao wrote:
> > for ram device regions, drop guest writes if the regions is read-only.
> >
> > Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
> > Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> > Signed-off-by: Xin Zeng <xin.zeng@intel.com>
> > ---
> > memory.c | 15 ++++++++++++---
> > 1 file changed, 12 insertions(+), 3 deletions(-)
> >
> > diff --git a/memory.c b/memory.c
> > index 601b749906..90a748912f 100644
> > --- a/memory.c
> > +++ b/memory.c
> > @@ -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)
>
> Style alignment is now of and can be adjusted easily.
>
> > {
> > 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:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Thanks! I'll update it right now!
>
> > + 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,
> >
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions
2020-04-30 5:15 [PATCH v5 0/3] drop writes to read-only ram device & vfio regions Yan Zhao
2020-04-30 5:19 ` [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions Yan Zhao
@ 2020-04-30 5:23 ` Yan Zhao
2020-04-30 7:02 ` Philippe Mathieu-Daudé
2020-04-30 5:24 ` [PATCH v5 3/3] hw/vfio: let read-only flag take effect for mmap'd regions Yan Zhao
2 siblings, 1 reply; 8+ messages in thread
From: Yan Zhao @ 2020-04-30 5:23 UTC (permalink / raw)
To: pbonzini, alex.williamson; +Cc: Yan Zhao, xin.zeng, philmd, qemu-devel
for vfio regions that are without write permission,
drop guest writes to those regions.
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.co>
Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
Signed-off-by: Xin Zeng <xin.zeng@intel.com>
---
hw/vfio/common.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 0b3593b3c0..2a4fedfeaa 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -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
--
2.17.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions
2020-04-30 5:23 ` [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions Yan Zhao
@ 2020-04-30 7:02 ` Philippe Mathieu-Daudé
2020-04-30 7:01 ` Yan Zhao
0 siblings, 1 reply; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-04-30 7:02 UTC (permalink / raw)
To: Yan Zhao, pbonzini, alex.williamson; +Cc: xin.zeng, qemu-devel
On 4/30/20 7:23 AM, Yan Zhao wrote:
> for vfio regions that are without write permission,
> drop guest writes to those regions.
>
> Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.co>
The full domain name is redhat.com.
> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> Signed-off-by: Xin Zeng <xin.zeng@intel.com>
> ---
> hw/vfio/common.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/hw/vfio/common.c b/hw/vfio/common.c
> index 0b3593b3c0..2a4fedfeaa 100644
> --- a/hw/vfio/common.c
> +++ b/hw/vfio/common.c
> @@ -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
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions
2020-04-30 7:02 ` Philippe Mathieu-Daudé
@ 2020-04-30 7:01 ` Yan Zhao
0 siblings, 0 replies; 8+ messages in thread
From: Yan Zhao @ 2020-04-30 7:01 UTC (permalink / raw)
To: Philippe Mathieu-Daudé
Cc: pbonzini, alex.williamson, Zeng, Xin, qemu-devel
On Thu, Apr 30, 2020 at 03:02:36PM +0800, Philippe Mathieu-Daudé wrote:
> On 4/30/20 7:23 AM, Yan Zhao wrote:
> > for vfio regions that are without write permission,
> > drop guest writes to those regions.
> >
> > Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
> > Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.co>
>
> The full domain name is redhat.com.
>
oops. really sorry....
> > Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> > Signed-off-by: Xin Zeng <xin.zeng@intel.com>
> > ---
> > hw/vfio/common.c | 13 +++++++++++--
> > 1 file changed, 11 insertions(+), 2 deletions(-)
> >
> > diff --git a/hw/vfio/common.c b/hw/vfio/common.c
> > index 0b3593b3c0..2a4fedfeaa 100644
> > --- a/hw/vfio/common.c
> > +++ b/hw/vfio/common.c
> > @@ -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
> >
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v5 3/3] hw/vfio: let read-only flag take effect for mmap'd regions
2020-04-30 5:15 [PATCH v5 0/3] drop writes to read-only ram device & vfio regions Yan Zhao
2020-04-30 5:19 ` [PATCH v5 1/3] memory: drop guest writes to read-only ram device regions Yan Zhao
2020-04-30 5:23 ` [PATCH v5 2/3] hw/vfio: drop guest writes to ro regions Yan Zhao
@ 2020-04-30 5:24 ` Yan Zhao
2 siblings, 0 replies; 8+ messages in thread
From: Yan Zhao @ 2020-04-30 5:24 UTC (permalink / raw)
To: pbonzini, alex.williamson; +Cc: Yan Zhao, xin.zeng, philmd, qemu-devel
along 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.
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
Signed-off-by: Xin Zeng <xin.zeng@intel.com>
---
hw/vfio/common.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 2a4fedfeaa..bf510e66c0 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -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);
--
2.17.1
^ permalink raw reply related [flat|nested] 8+ messages in thread