hw/virtio: Protect from more DMA re-entrancy bugs
cover hw/virtio: Protect from more DMA re-entrancy bugs
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(-)
Signature · 1 line
2.41.0
PATCH 1/4 hw/virtio: Introduce virtio_bh_new_guarded() helper
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).
--- include/hw/virtio/virtio.h | 7 +++++++ hw/virtio/virtio.c | 10 ++++++++++ 2 files changed, 17 insertions(+)
include/hw/virtio/virtio.h +7 -0
@@ -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
hw/virtio/virtio.c +10 -0
@@ -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); +}
Signature · 1 line
2.41.0
PATCH 2/4 hw/display/virtio-gpu: Protect from DMA re-entrancy bugs
In reply to Philippe Mathieu-Daudé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:18previously 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
--- hw/display/virtio-gpu.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +2 -4
@@ -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);
Signature · 1 line
2.41.0
PATCH 3/4 hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
In reply to Philippe Mathieu-Daudé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.
--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
hw/char/virtio-serial-bus.c +1 -2
@@ -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 ; }
Signature · 1 line
2.41.0
Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
Quoted 16 lines · Philippe Mathieu-Daudé
Replace
qemu_bh_new_guarded()byvirtio_bh_new_guarded()so the bus and device use the same guard. Otherwise the DMA-reentrancy protection can be bypassed.Suggested-by Alexander Bulekov <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/char/virtio-serial-bus.c +0
-0
hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 988
Quoted 6 lines
hw/char/virtio-serial-bus.c +1
-2@@ -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);
---
Quoted 3 lines
hw/char/virtio-serial-bus.c +0
-0port->elem =NULL ; }
Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
Replace
qemu_bh_new_guarded()byvirtio_bh_new_guarded()so the bus and device use the same guard. Otherwise the DMA-reentrancy protection can be bypassed.Suggested-by Alexander Bulekov <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/char/virtio-serial-bus.c +0
-0hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 987
Quoted 5 lines
hw/char/virtio-serial-bus.c +1
-2@@ -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 --
Review
Quoted 4 lines
(quoted diff) +2
-2- 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?
Quoted 2 lines
Quoted 2 lines
hw/char/virtio-serial-bus.c +0
-0port->elem =NULL ; }
Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
Quoted 31 lines · Michael S. Tsirkin
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
Replace
qemu_bh_new_guarded()byvirtio_bh_new_guarded()so the bus and device use the same guard. Otherwise the DMA-reentrancy protection can be bypassed.Suggested-by Alexander Bulekov <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/char/virtio-serial-bus.c +0
-0hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 987
Quoted 5 lines
hw/char/virtio-serial-bus.c +1
-2@@ -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 --Review
Quoted 4 lines
(quoted diff) +2
-2- 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); ---
Quoted 2 lines
I don't get it. vdev is already the correct type. Why do you need
VIRTIO_DEVICEhere?
This function doesn't declare vdev.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
hw/char/virtio-serial-bus.c +0
-0port->elem =NULL ; }
Michael S. Tsirkin
Acked-byRe: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Michael S. Tsirkin
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
Replace
qemu_bh_new_guarded()byvirtio_bh_new_guarded()so the bus and device use the same guard. Otherwise the DMA-reentrancy protection can be bypassed.Suggested-by Alexander Bulekov <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/char/virtio-serial-bus.c +0
-0hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 987
Quoted 5 lines
hw/char/virtio-serial-bus.c +1
-2@@ -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 --(quoted diff) +2
-2- 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); ---
Quoted 9 lines
Quoted 2 lines
I don't get it. vdev is already the correct type. Why do you need
VIRTIO_DEVICEhere?This function doesn't declare vdev.
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
hw/char/virtio-serial-bus.c +0
-0port->elem =NULL ; }
But it seems clear it wasn't really tested, right? Philipe here's my ack:
Feel free to merge these after testing.
Signature · 1 line
MST
Re: [PATCH-for-9.0 3/4] hw/char/virtio-serial-bus: Protect from DMA re-entrancy bugs
Quoted 39 lines · Michael S. Tsirkin
Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Michael S. Tsirkin
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
Replace
qemu_bh_new_guarded()byvirtio_bh_new_guarded()so the bus and device use the same guard. Otherwise the DMA-reentrancy protection can be bypassed.Suggested-by Alexander Bulekov <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/char/virtio-serial-bus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/char/virtio-serial-bus.c +0
-0hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 987
Quoted 5 lines
hw/char/virtio-serial-bus.c +1
-2@@ -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 --Review
Quoted 4 lines
(quoted diff) +2
-2- 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); ---Quoted 2 lines
I don't get it. vdev is already the correct type. Why do you need
VIRTIO_DEVICEhere?This function doesn't declare vdev.
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 990
Quoted 2 lines
hw/char/virtio-serial-bus.c +0
-0port->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.
Quoted 5 lines
Philipe here's my ack:
Feel free to merge these after testing.
Sure, thanks!
Phil.
PATCH 4/4 hw/virtio/virtio-crypto: Protect from DMA re-entrancy bugs
In reply to Philippe Mathieu-Daudé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.
--- hw/virtio/virtio-crypto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/virtio-crypto.c +2 -2
@@ -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; }
Signature · 1 line
2.41.0
Quoted 2 lines · Philippe Mathieu-Daudé
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:
take care,
Gerd
Hi,
Quoted 3 lines · Philippe Mathieu-Daudé
Gerd suggested to use the transport guard to protect the device from DMA re-entrancy abuses.
This was assigned CVE-2024-3446.
Quoted 13 lines
Philippe Mathieu-Daudé (4):
hw/virtio: Introducevirtio_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 bugsinclude/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(-)
Quoted 3 lines
--
2.41.0
Thanks,
--
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0