Skip to contentSkip to conversation

memory: drop guest writes to read-only ram device regions

PATCH Superseded Yan Zhao 3 participants 7 messages Apr 16, 2020 – Apr 27, 2020

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

PATCH v4 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.

Cc Philippe Mathieu-Daudé <[email protected]>
---
 memory.c | 7 +++++++
 1 file changed, 7 insertions(+)
memory.c +7 -0
index 601b749906..9576dd6807 100644
@@ -34,6 +34,7 @@
 #include "sysemu/accel.h"
 #include "hw/boards.h"
 #include "migration/vmstate.h"
+#include "qemu/log.h"
 
 //#define DEBUG_UNASSIGNED
 
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
     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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }
 
     switch (size) {
     case 1:
Signature · 1 line
2.17.1
Reviewed-by on v4 1/3
#1 lore raw
Quoted 25 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
  memory.c | 7 +++++++
  1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -34,6 +34,7 @@
  #include "sysemu/accel.h"
  #include "hw/boards.h"
  #include "migration/vmstate.h"
+#include "qemu/log.h"
 
  //#define DEBUG_UNASSIGNED
 
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
      MemoryRegion *mr = opaque;
 

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1325

Quoted 10 lines
memory.c +6 -0
      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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }
 
      switch (size) {
      case 1:
#2 lore raw
In reply to Yan Zhao
Quoted 22 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
 memory.c | 7 +++++++
 1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -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 void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1322

Quoted 9 lines
memory.c +6 -0
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
     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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }

As mentioned in the review of v1, memory_region_ram_device_write should be changed to a .write_with_attrs operation, so that it can return MEMTX_ERROR.

Otherwise this looks good.

Paolo

author
#3 lore raw
Quoted 37 lines · Paolo Bonzini
Quoted 22 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
 memory.c | 7 +++++++
 1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -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 void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1322

Quoted 9 lines
memory.c +6 -0
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
     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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }

As mentioned in the review of v1, memory_region_ram_device_write should be changed to a .write_with_attrs operation, so that it can return MEMTX_ERROR.

Quoted 2 lines

Otherwise this looks good.

hi Paolo,
thanks for pointing it out again!
I didn't get your meaning in v1. will update the patch!

Thanks
Yan

Quoted 1 line
author
#4 lore raw
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Paolo Bonzini
Quoted 24 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
 memory.c | 7 +++++++
 1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -34,6 +34,7 @@
 #include "sysemu/accel.h"
 #include "hw/boards.h"
 #include "migration/vmstate.h"
+#include "qemu/log.h"
 
 //#define DEBUG_UNASSIGNED
 
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
     MemoryRegion *mr = opaque;
 
Quoted 12 lines
Quoted 8 lines

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1322

Quoted 7 lines
memory.c +6 -0
     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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }
Quoted 4 lines

As mentioned in the review of v1, memory_region_ram_device_write should be changed to a .write_with_attrs operation, so that it can return MEMTX_ERROR.

hi Paolo and Alex,
need I also change vfio_region_write() in patch 2 to a .write_with_attrs operation?
vfio_region_read() is also possible to fail, so should I change it to a .read_with_attrs, too?

Thanks
Yan

Quoted 10 lines
Quoted 2 lines

Otherwise this looks good.

hi Paolo,
thanks for pointing it out again!
I didn't get your meaning in v1. will update the patch!

Thanks
Yan

Quoted 1 line
#5 lore raw
Quoted 39 lines · Yan Zhao
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Paolo Bonzini
Quoted 24 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
  memory.c | 7 +++++++
  1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -34,6 +34,7 @@
  #include "sysemu/accel.h"
  #include "hw/boards.h"
  #include "migration/vmstate.h"
+#include "qemu/log.h"
 
  //#define DEBUG_UNASSIGNED
 
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
      MemoryRegion *mr = opaque;
 
Quoted 12 lines
Quoted 8 lines

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1322

Quoted 7 lines
memory.c +6 -0
      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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }
Quoted 4 lines

As mentioned in the review of v1, memory_region_ram_device_write should be changed to a .write_with_attrs operation, so that it can return MEMTX_ERROR.

Quoted 5 lines

hi Paolo and Alex,
need I also change vfio_region_write() in patch 2 to a .write_with_attrs operation?
vfio_region_read() is also possible to fail, so should I change it to a .read_with_attrs, too?

Yes.

Please submit your series as a thread, with a cover letter:
https://wiki.qemu.org/Contribute/SubmitAPatch#Include_a_meaningful_cover_letter

Quoted 15 lines

Thanks
Yan

Quoted 10 lines
Quoted 2 lines

Otherwise this looks good.

hi Paolo,
thanks for pointing it out again!
I didn't get your meaning in v1. will update the patch!

Thanks
Yan

Quoted 1 line
author
#6 lore raw
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 39 lines · Yan Zhao
Quoted 26 lines · Yan Zhao
Quoted 25 lines · Paolo Bonzini
Quoted 24 lines · Yan Zhao

for ram device regions, drop guest writes if the regions is read-only.

Cc Philippe Mathieu-Daudé <[email protected]>
---
  memory.c | 7 +++++++
  1 file changed, 7 insertions(+)
memory.c +1 -0
index 601b749906..9576dd6807 100644
@@ -34,6 +34,7 @@
  #include "sysemu/accel.h"
  #include "hw/boards.h"
  #include "migration/vmstate.h"
+#include "qemu/log.h"
 
  //#define DEBUG_UNASSIGNED
 
@@ -1313,6 +1314,12 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
      MemoryRegion *mr = opaque;
 
Quoted 12 lines
Quoted 8 lines

memory.c static void memory_region_ram_device_write(void *opaque, hwaddr addr, · line 1322

Quoted 7 lines
memory.c +6 -0
      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 0x%"
+                       HWADDR_PRIx" size %u\n", addr, size);
+        return;
+    }
Quoted 4 lines

As mentioned in the review of v1, memory_region_ram_device_write should be changed to a .write_with_attrs operation, so that it can return MEMTX_ERROR.

Quoted 5 lines

hi Paolo and Alex,
need I also change vfio_region_write() in patch 2 to a .write_with_attrs operation?
vfio_region_read() is also possible to fail, so should I change it to a .read_with_attrs, too?

Quoted 5 lines

Yes.

Please submit your series as a thread, with a cover letter:
https://wiki.qemu.org/Contribute/SubmitAPatch#Include_a_meaningful_cover_letter

hi Philippe
thanks for pointing out this issue.
I just realized this version of patches were sent separately, though I did send a cover letter. not sure what happened. maybe I just forgot to add an -in-reply-to before sending out.
will pay attention to it next time.

Thanks
Yan

Quoted 16 lines
Quoted 15 lines

Thanks
Yan

Quoted 10 lines
Quoted 2 lines

Otherwise this looks good.

hi Paolo,
thanks for pointing it out again!
I didn't get your meaning in v1. will update the patch!

Thanks
Yan

Quoted 1 line