qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs
@ 2024-04-04 19:13 Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 1/4] hw/virtio: Introduce virtio_bh_new_guarded() helper Philippe Mathieu-Daudé
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-04 19:13 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, Philippe Mathieu-Daudé

Gerd suggested to use the transport guard to protect the
device from DMA re-entrancy abuses.

Philippe Mathieu-Daudé (4):
  hw/virtio: Introduce virtio_bh_new_guarded() helper
  hw/display/virtio-gpu: Protect from DMA re-entrancy bugs
  hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  hw/virtio/virtio-crypto: Protect from DMA re-entrancy bugs

 include/hw/virtio/virtio.h  |  7 +++++++
 hw/char/virtio-serial-bus.c |  3 +--
 hw/display/virtio-gpu.c     |  6 ++----
 hw/virtio/virtio-crypto.c   |  4 ++--
 hw/virtio/virtio.c          | 10 ++++++++++
 5 files changed, 22 insertions(+), 8 deletions(-)

-- 
2.41.0



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH-for-9.0 1/4] hw/virtio: Introduce virtio_bh_new_guarded() helper
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
@ 2024-04-04 19:13 ` Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 2/4] hw/display/virtio-gpu: Protect from DMA re-entrancy bugs Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-04 19:13 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, Philippe Mathieu-Daudé

Introduce virtio_bh_new_guarded(), similar to qemu_bh_new_guarded()
but using the transport memory guard, instead of the device one
(there can only be one virtio device per virtio bus).

Inspired-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/hw/virtio/virtio.h |  7 +++++++
 hw/virtio/virtio.c         | 10 ++++++++++
 2 files changed, 17 insertions(+)

diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index b3c74a1bca..12419d6355 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -22,6 +22,7 @@
 #include "standard-headers/linux/virtio_config.h"
 #include "standard-headers/linux/virtio_ring.h"
 #include "qom/object.h"
+#include "block/aio.h"
 
 /*
  * A guest should never accept this. It implies negotiation is broken
@@ -527,4 +528,10 @@ static inline bool virtio_device_disabled(VirtIODevice *vdev)
 bool virtio_legacy_allowed(VirtIODevice *vdev);
 bool virtio_legacy_check_disabled(VirtIODevice *vdev);
 
+QEMUBH *virtio_bh_new_guarded_full(VirtIODevice *vdev,
+                                   QEMUBHFunc *cb, void *opaque,
+                                   const char *name);
+#define virtio_bh_new_guarded(vdev, cb, opaque) \
+    virtio_bh_new_guarded_full((vdev), (cb), (opaque), (stringify(cb)))
+
 #endif
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index fb6b4ccd83..e1735cf7fd 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -4176,3 +4176,13 @@ static void virtio_register_types(void)
 }
 
 type_init(virtio_register_types)
+
+QEMUBH *virtio_bh_new_guarded_full(VirtIODevice *vdev,
+                                   QEMUBHFunc *cb, void *opaque,
+                                   const char *name)
+{
+    BusState *virtio_bus = qdev_get_parent_bus(DEVICE(vdev));
+    DeviceState *transport = virtio_bus->parent;
+
+    return qemu_bh_new_full(cb, opaque, name, &transport->mem_reentrancy_guard);
+}
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH-for-9.0 2/4] hw/display/virtio-gpu: Protect from DMA re-entrancy bugs
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 1/4] hw/virtio: Introduce virtio_bh_new_guarded() helper Philippe Mathieu-Daudé
@ 2024-04-04 19:13 ` Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: " Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-04 19:13 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, Philippe Mathieu-Daudé,
	qemu-stable, Yongkang Jia, Xiao Lei, Yiming Tao

Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
so the bus and device use the same guard. Otherwise the
DMA-reentrancy protection can be bypassed:

  $ cat << EOF | qemu-system-i386 -display none -nodefaults \
                                  -machine q35,accel=qtest \
                                  -m 512M \
                                  -device virtio-gpu \
                                  -qtest stdio
  outl 0xcf8 0x80000820
  outl 0xcfc 0xe0004000
  outl 0xcf8 0x80000804
  outw 0xcfc 0x06
  write 0xe0004030 0x4 0x024000e0
  write 0xe0004028 0x1 0xff
  write 0xe0004020 0x4 0x00009300
  write 0xe000401c 0x1 0x01
  write 0x101 0x1 0x04
  write 0x103 0x1 0x1c
  write 0x9301c8 0x1 0x18
  write 0x105 0x1 0x1c
  write 0x107 0x1 0x1c
  write 0x109 0x1 0x1c
  write 0x10b 0x1 0x00
  write 0x10d 0x1 0x00
  write 0x10f 0x1 0x00
  write 0x111 0x1 0x00
  write 0x113 0x1 0x00
  write 0x115 0x1 0x00
  write 0x117 0x1 0x00
  write 0x119 0x1 0x00
  write 0x11b 0x1 0x00
  write 0x11d 0x1 0x00
  write 0x11f 0x1 0x00
  write 0x121 0x1 0x00
  write 0x123 0x1 0x00
  write 0x125 0x1 0x00
  write 0x127 0x1 0x00
  write 0x129 0x1 0x00
  write 0x12b 0x1 0x00
  write 0x12d 0x1 0x00
  write 0x12f 0x1 0x00
  write 0x131 0x1 0x00
  write 0x133 0x1 0x00
  write 0x135 0x1 0x00
  write 0x137 0x1 0x00
  write 0x139 0x1 0x00
  write 0xe0007003 0x1 0x00
  EOF
  ...
  =================================================================
  ==276099==ERROR: AddressSanitizer: heap-use-after-free on address 0x60d000011178
  at pc 0x562cc3b736c7 bp 0x7ffed49dee60 sp 0x7ffed49dee58
  READ of size 8 at 0x60d000011178 thread T0
      #0 0x562cc3b736c6 in virtio_gpu_ctrl_response hw/display/virtio-gpu.c:180:42
      #1 0x562cc3b7c40b in virtio_gpu_ctrl_response_nodata hw/display/virtio-gpu.c:192:5
      #2 0x562cc3b7c40b in virtio_gpu_simple_process_cmd hw/display/virtio-gpu.c:1015:13
      #3 0x562cc3b82873 in virtio_gpu_process_cmdq hw/display/virtio-gpu.c:1050:9
      #4 0x562cc4a85514 in aio_bh_call util/async.c:169:5
      #5 0x562cc4a85c52 in aio_bh_poll util/async.c:216:13
      #6 0x562cc4a1a79b in aio_dispatch util/aio-posix.c:423:5
      #7 0x562cc4a8a2da in aio_ctx_dispatch util/async.c:358:5
      #8 0x7f36840547a8 in g_main_context_dispatch (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x547a8)
      #9 0x562cc4a8b753 in glib_pollfds_poll util/main-loop.c:290:9
      #10 0x562cc4a8b753 in os_host_main_loop_wait util/main-loop.c:313:5
      #11 0x562cc4a8b753 in main_loop_wait util/main-loop.c:592:11
      #12 0x562cc3938186 in qemu_main_loop system/runstate.c:782:9
      #13 0x562cc43b7af5 in qemu_default_main system/main.c:37:14
      #14 0x7f3683a6c189 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
      #15 0x7f3683a6c244 in __libc_start_main csu/../csu/libc-start.c:381:3
      #16 0x562cc2a58ac0 in _start (qemu-system-i386+0x231bac0)

  0x60d000011178 is located 56 bytes inside of 136-byte region [0x60d000011140,0x60d0000111c8)
  freed by thread T0 here:
      #0 0x562cc2adb662 in __interceptor_free (qemu-system-i386+0x239e662)
      #1 0x562cc3b86b21 in virtio_gpu_reset hw/display/virtio-gpu.c:1524:9
      #2 0x562cc416e20e in virtio_reset hw/virtio/virtio.c:2145:9
      #3 0x562cc37c5644 in virtio_pci_reset hw/virtio/virtio-pci.c:2249:5
      #4 0x562cc4233758 in memory_region_write_accessor system/memory.c:497:5
      #5 0x562cc4232eea in access_with_adjusted_size system/memory.c:573:18

  previously allocated by thread T0 here:
      #0 0x562cc2adb90e in malloc (qemu-system-i386+0x239e90e)
      #1 0x7f368405a678 in g_malloc (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x5a678)
      #2 0x562cc4163ffc in virtqueue_split_pop hw/virtio/virtio.c:1612:12
      #3 0x562cc4163ffc in virtqueue_pop hw/virtio/virtio.c:1783:16
      #4 0x562cc3b91a95 in virtio_gpu_handle_ctrl hw/display/virtio-gpu.c:1112:15
      #5 0x562cc4a85514 in aio_bh_call util/async.c:169:5
      #6 0x562cc4a85c52 in aio_bh_poll util/async.c:216:13
      #7 0x562cc4a1a79b in aio_dispatch util/aio-posix.c:423:5

  SUMMARY: AddressSanitizer: heap-use-after-free hw/display/virtio-gpu.c:180:42 in virtio_gpu_ctrl_response

With this change, the same reproducer triggers:

  qemu-system-i386: warning: Blocked re-entrant IO on MemoryRegion: virtio-pci-common-virtio-gpu at addr: 0x6

Cc: qemu-stable@nongnu.org
Reported-by: Alexander Bulekov <alxndr@bu.edu>
Reported-by: Yongkang Jia <kangel@zju.edu.cn>
Reported-by: Xiao Lei <nop.leixiao@gmail.com>
Reported-by: Yiming Tao <taoym@zju.edu.cn>
Buglink: https://bugs.launchpad.net/qemu/+bug/1888606
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/display/virtio-gpu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 78d5a4f164..3ab94a9735 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -1492,10 +1492,8 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
 
     g->ctrl_vq = virtio_get_queue(vdev, 0);
     g->cursor_vq = virtio_get_queue(vdev, 1);
-    g->ctrl_bh = qemu_bh_new_guarded(virtio_gpu_ctrl_bh, g,
-                                     &qdev->mem_reentrancy_guard);
-    g->cursor_bh = qemu_bh_new_guarded(virtio_gpu_cursor_bh, g,
-                                       &qdev->mem_reentrancy_guard);
+    g->ctrl_bh = virtio_bh_new_guarded(vdev, virtio_gpu_ctrl_bh, g);
+    g->cursor_bh = virtio_bh_new_guarded(vdev, virtio_gpu_cursor_bh, g);
     g->reset_bh = qemu_bh_new(virtio_gpu_reset_bh, g);
     qemu_cond_init(&g->reset_cond);
     QTAILQ_INIT(&g->reslist);
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 1/4] hw/virtio: Introduce virtio_bh_new_guarded() helper Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 2/4] hw/display/virtio-gpu: Protect from DMA re-entrancy bugs Philippe Mathieu-Daudé
@ 2024-04-04 19:13 ` Philippe Mathieu-Daudé
  2024-04-08  7:14   ` Philippe Mathieu-Daudé
  2024-04-04 19:13 ` [PATCH-for-9.0 4/4] hw/virtio/virtio-crypto: " Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-04 19:13 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, Philippe Mathieu-Daudé,
	qemu-stable

Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
so the bus and device use the same guard. Otherwise the
DMA-reentrancy protection can be bypassed.

Cc: qemu-stable@nongnu.org
Suggested-by: Alexander Bulekov <alxndr@bu.edu>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/char/virtio-serial-bus.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
index 016aba6374..cd0e3a11f7 100644
--- a/hw/char/virtio-serial-bus.c
+++ b/hw/char/virtio-serial-bus.c
@@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
-                                   &dev->mem_reentrancy_guard);
+    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
     port->elem = NULL;
 }
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH-for-9.0 4/4] hw/virtio/virtio-crypto: Protect from DMA re-entrancy bugs
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2024-04-04 19:13 ` [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: " Philippe Mathieu-Daudé
@ 2024-04-04 19:13 ` Philippe Mathieu-Daudé
  2024-04-05  7:02 ` [PATCH-for-9.0 0/4] hw/virtio: Protect from more " Gerd Hoffmann
  2024-04-08  7:37 ` Mauro Matteo Cascella
  5 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-04 19:13 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, Philippe Mathieu-Daudé,
	qemu-stable

Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
so the bus and device use the same guard. Otherwise the
DMA-reentrancy protection can be bypassed.

Cc: qemu-stable@nongnu.org
Suggested-by: Alexander Bulekov <alxndr@bu.edu>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/virtio/virtio-crypto.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index fe1313f2ad..ac1b67d1fb 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -1080,8 +1080,8 @@ static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
         vcrypto->vqs[i].dataq =
                  virtio_add_queue(vdev, 1024, virtio_crypto_handle_dataq_bh);
         vcrypto->vqs[i].dataq_bh =
-                 qemu_bh_new_guarded(virtio_crypto_dataq_bh, &vcrypto->vqs[i],
-                                     &dev->mem_reentrancy_guard);
+                 virtio_bh_new_guarded(vdev, virtio_crypto_dataq_bh,
+                                       &vcrypto->vqs[i]);
         vcrypto->vqs[i].vcrypto = vcrypto;
     }
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2024-04-04 19:13 ` [PATCH-for-9.0 4/4] hw/virtio/virtio-crypto: " Philippe Mathieu-Daudé
@ 2024-04-05  7:02 ` Gerd Hoffmann
  2024-04-08  7:37 ` Mauro Matteo Cascella
  5 siblings, 0 replies; 12+ messages in thread
From: Gerd Hoffmann @ 2024-04-05  7:02 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini

On Thu, Apr 04, 2024 at 09:13:35PM +0200, Philippe Mathieu-Daudé wrote:
> Gerd suggested to use the transport guard to protect the
> device from DMA re-entrancy abuses.

Thanks for turning that idea into a proper patch series.

Series:
Reviewed-by: Gerd Hoffmann <kraxel@redhat.com>

take care,
  Gerd



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-04 19:13 ` [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: " Philippe Mathieu-Daudé
@ 2024-04-08  7:14   ` Philippe Mathieu-Daudé
  2024-04-08 10:08     ` Michael S. Tsirkin
  0 siblings, 1 reply; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-08  7:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Gerd Hoffmann, Amit Shah, Michael S. Tsirkin, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, qemu-stable

On 4/4/24 21:13, Philippe Mathieu-Daudé wrote:
> Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
> so the bus and device use the same guard. Otherwise the
> DMA-reentrancy protection can be bypassed.
> 
> Cc: qemu-stable@nongnu.org
> Suggested-by: Alexander Bulekov <alxndr@bu.edu>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   hw/char/virtio-serial-bus.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
> index 016aba6374..cd0e3a11f7 100644
> --- a/hw/char/virtio-serial-bus.c
> +++ b/hw/char/virtio-serial-bus.c
> @@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
>           return;
>       }
>   
> -    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
> -                                   &dev->mem_reentrancy_guard);
> +    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);

Missing:
-- >8 --
-    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
+    port->bh = virtio_bh_new_guarded(VIRTIO_DEVICE(dev),
+                                     flush_queued_data_bh, port);
---

>       port->elem = NULL;
>   }
>   



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs
  2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2024-04-05  7:02 ` [PATCH-for-9.0 0/4] hw/virtio: Protect from more " Gerd Hoffmann
@ 2024-04-08  7:37 ` Mauro Matteo Cascella
  5 siblings, 0 replies; 12+ messages in thread
From: Mauro Matteo Cascella @ 2024-04-08  7:37 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Gerd Hoffmann, Amit Shah, Michael S. Tsirkin,
	Alexander Bulekov, Gonglei (Arei), Marc-André Lureau,
	Laurent Vivier, Paolo Bonzini

Hi,

On Thu, Apr 4, 2024 at 9:13 PM Philippe Mathieu-Daudé <philmd@linaro.org> wrote:
>
> Gerd suggested to use the transport guard to protect the
> device from DMA re-entrancy abuses.

This was assigned CVE-2024-3446.

> Philippe Mathieu-Daudé (4):
>   hw/virtio: Introduce virtio_bh_new_guarded() helper
>   hw/display/virtio-gpu: Protect from DMA re-entrancy bugs
>   hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
>   hw/virtio/virtio-crypto: Protect from DMA re-entrancy bugs
>
>  include/hw/virtio/virtio.h  |  7 +++++++
>  hw/char/virtio-serial-bus.c |  3 +--
>  hw/display/virtio-gpu.c     |  6 ++----
>  hw/virtio/virtio-crypto.c   |  4 ++--
>  hw/virtio/virtio.c          | 10 ++++++++++
>  5 files changed, 22 insertions(+), 8 deletions(-)
>
> --
> 2.41.0
>

Thanks,
--
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-08  7:14   ` Philippe Mathieu-Daudé
@ 2024-04-08 10:08     ` Michael S. Tsirkin
  2024-04-08 11:04       ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 12+ messages in thread
From: Michael S. Tsirkin @ 2024-04-08 10:08 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Gerd Hoffmann, Amit Shah, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, qemu-stable

On Mon, Apr 08, 2024 at 09:14:39AM +0200, Philippe Mathieu-Daudé wrote:
> On 4/4/24 21:13, Philippe Mathieu-Daudé wrote:
> > Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
> > so the bus and device use the same guard. Otherwise the
> > DMA-reentrancy protection can be bypassed.
> > 
> > Cc: qemu-stable@nongnu.org
> > Suggested-by: Alexander Bulekov <alxndr@bu.edu>
> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> > ---
> >   hw/char/virtio-serial-bus.c | 3 +--
> >   1 file changed, 1 insertion(+), 2 deletions(-)
> > 
> > diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
> > index 016aba6374..cd0e3a11f7 100644
> > --- a/hw/char/virtio-serial-bus.c
> > +++ b/hw/char/virtio-serial-bus.c
> > @@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
> >           return;
> >       }
> > -    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
> > -                                   &dev->mem_reentrancy_guard);
> > +    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
> 
> Missing:
> -- >8 --
> -    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
> +    port->bh = virtio_bh_new_guarded(VIRTIO_DEVICE(dev),
> +                                     flush_queued_data_bh, port);
> ---

I don't get it. vdev is already the correct type. Why do you need
VIRTIO_DEVICE here?

> >       port->elem = NULL;
> >   }



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-08 10:08     ` Michael S. Tsirkin
@ 2024-04-08 11:04       ` Philippe Mathieu-Daudé
  2024-04-08 15:20         ` Michael S. Tsirkin
  0 siblings, 1 reply; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-08 11:04 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: qemu-devel, Gerd Hoffmann, Amit Shah, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, qemu-stable

On 8/4/24 12:08, Michael S. Tsirkin wrote:
> On Mon, Apr 08, 2024 at 09:14:39AM +0200, Philippe Mathieu-Daudé wrote:
>> On 4/4/24 21:13, Philippe Mathieu-Daudé wrote:
>>> Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
>>> so the bus and device use the same guard. Otherwise the
>>> DMA-reentrancy protection can be bypassed.
>>>
>>> Cc: qemu-stable@nongnu.org
>>> Suggested-by: Alexander Bulekov <alxndr@bu.edu>
>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>> ---
>>>    hw/char/virtio-serial-bus.c | 3 +--
>>>    1 file changed, 1 insertion(+), 2 deletions(-)
>>>
>>> diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
>>> index 016aba6374..cd0e3a11f7 100644
>>> --- a/hw/char/virtio-serial-bus.c
>>> +++ b/hw/char/virtio-serial-bus.c
>>> @@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
>>>            return;
>>>        }
>>> -    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
>>> -                                   &dev->mem_reentrancy_guard);
>>> +    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
>>
>> Missing:
>> -- >8 --
>> -    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
>> +    port->bh = virtio_bh_new_guarded(VIRTIO_DEVICE(dev),
>> +                                     flush_queued_data_bh, port);
>> ---
> 
> I don't get it. vdev is already the correct type. Why do you need
> VIRTIO_DEVICE here?

This function doesn't declare vdev.

> 
>>>        port->elem = NULL;
>>>    }
> 



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-08 11:04       ` Philippe Mathieu-Daudé
@ 2024-04-08 15:20         ` Michael S. Tsirkin
  2024-04-08 18:22           ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 12+ messages in thread
From: Michael S. Tsirkin @ 2024-04-08 15:20 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Gerd Hoffmann, Amit Shah, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, qemu-stable

On Mon, Apr 08, 2024 at 01:04:11PM +0200, Philippe Mathieu-Daudé wrote:
> On 8/4/24 12:08, Michael S. Tsirkin wrote:
> > On Mon, Apr 08, 2024 at 09:14:39AM +0200, Philippe Mathieu-Daudé wrote:
> > > On 4/4/24 21:13, Philippe Mathieu-Daudé wrote:
> > > > Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
> > > > so the bus and device use the same guard. Otherwise the
> > > > DMA-reentrancy protection can be bypassed.
> > > > 
> > > > Cc: qemu-stable@nongnu.org
> > > > Suggested-by: Alexander Bulekov <alxndr@bu.edu>
> > > > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> > > > ---
> > > >    hw/char/virtio-serial-bus.c | 3 +--
> > > >    1 file changed, 1 insertion(+), 2 deletions(-)
> > > > 
> > > > diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
> > > > index 016aba6374..cd0e3a11f7 100644
> > > > --- a/hw/char/virtio-serial-bus.c
> > > > +++ b/hw/char/virtio-serial-bus.c
> > > > @@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
> > > >            return;
> > > >        }
> > > > -    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
> > > > -                                   &dev->mem_reentrancy_guard);
> > > > +    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
> > > 
> > > Missing:
> > > -- >8 --
> > > -    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
> > > +    port->bh = virtio_bh_new_guarded(VIRTIO_DEVICE(dev),
> > > +                                     flush_queued_data_bh, port);
> > > ---
> > 
> > I don't get it. vdev is already the correct type. Why do you need
> > VIRTIO_DEVICE here?
> 
> This function doesn't declare vdev.
> 
> > 
> > > >        port->elem = NULL;
> > > >    }
> > 



But it seems clear it wasn't really tested, right?
Philipe here's my ack:

Acked-by: Michael S. Tsirkin <mst@redhat.com>

Feel free to merge these after testing.

-- 
MST



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
  2024-04-08 15:20         ` Michael S. Tsirkin
@ 2024-04-08 18:22           ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-04-08 18:22 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: qemu-devel, Gerd Hoffmann, Amit Shah, Alexander Bulekov,
	Gonglei (Arei), Marc-André Lureau, Laurent Vivier,
	Mauro Matteo Cascella, Paolo Bonzini, qemu-stable

On 8/4/24 17:20, Michael S. Tsirkin wrote:
> On Mon, Apr 08, 2024 at 01:04:11PM +0200, Philippe Mathieu-Daudé wrote:
>> On 8/4/24 12:08, Michael S. Tsirkin wrote:
>>> On Mon, Apr 08, 2024 at 09:14:39AM +0200, Philippe Mathieu-Daudé wrote:
>>>> On 4/4/24 21:13, Philippe Mathieu-Daudé wrote:
>>>>> Replace qemu_bh_new_guarded() by virtio_bh_new_guarded()
>>>>> so the bus and device use the same guard. Otherwise the
>>>>> DMA-reentrancy protection can be bypassed.
>>>>>
>>>>> Cc: qemu-stable@nongnu.org
>>>>> Suggested-by: Alexander Bulekov <alxndr@bu.edu>
>>>>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>>>> ---
>>>>>     hw/char/virtio-serial-bus.c | 3 +--
>>>>>     1 file changed, 1 insertion(+), 2 deletions(-)
>>>>>
>>>>> diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
>>>>> index 016aba6374..cd0e3a11f7 100644
>>>>> --- a/hw/char/virtio-serial-bus.c
>>>>> +++ b/hw/char/virtio-serial-bus.c
>>>>> @@ -985,8 +985,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)
>>>>>             return;
>>>>>         }
>>>>> -    port->bh = qemu_bh_new_guarded(flush_queued_data_bh, port,
>>>>> -                                   &dev->mem_reentrancy_guard);
>>>>> +    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
>>>>
>>>> Missing:
>>>> -- >8 --
>>>> -    port->bh = virtio_bh_new_guarded(vdev, flush_queued_data_bh, port);
>>>> +    port->bh = virtio_bh_new_guarded(VIRTIO_DEVICE(dev),
>>>> +                                     flush_queued_data_bh, port);
>>>> ---
>>>
>>> I don't get it. vdev is already the correct type. Why do you need
>>> VIRTIO_DEVICE here?
>>
>> This function doesn't declare vdev.
>>
>>>
>>>>>         port->elem = NULL;
>>>>>     }
>>>
> 
> 
> 
> But it seems clear it wasn't really tested, right?

Indeed, I only tested virtio-gpu, then added the other ones Alexander
suggested. I don't have virtio-specific tests, I rely on the GitLab CI
ones. Hope that's enough.

> Philipe here's my ack:
> 
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> 
> Feel free to merge these after testing.

Sure, thanks!

Phil.



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-04-08 18:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-04 19:13 [PATCH-for-9.0 0/4] hw/virtio: Protect from more DMA re-entrancy bugs Philippe Mathieu-Daudé
2024-04-04 19:13 ` [PATCH-for-9.0 1/4] hw/virtio: Introduce virtio_bh_new_guarded() helper Philippe Mathieu-Daudé
2024-04-04 19:13 ` [PATCH-for-9.0 2/4] hw/display/virtio-gpu: Protect from DMA re-entrancy bugs Philippe Mathieu-Daudé
2024-04-04 19:13 ` [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: " Philippe Mathieu-Daudé
2024-04-08  7:14   ` Philippe Mathieu-Daudé
2024-04-08 10:08     ` Michael S. Tsirkin
2024-04-08 11:04       ` Philippe Mathieu-Daudé
2024-04-08 15:20         ` Michael S. Tsirkin
2024-04-08 18:22           ` Philippe Mathieu-Daudé
2024-04-04 19:13 ` [PATCH-for-9.0 4/4] hw/virtio/virtio-crypto: " Philippe Mathieu-Daudé
2024-04-05  7:02 ` [PATCH-for-9.0 0/4] hw/virtio: Protect from more " Gerd Hoffmann
2024-04-08  7:37 ` Mauro Matteo Cascella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).