Skip to contentSkip to conversation

vdpa: fix use-after-free of vqs in vhost_vdpa_device_unrealize

PATCH New Li Zhaoxin 1 participant 1 message Jun 22

lore
Share & tools
lore

Li Zhaoxin

author
#0 lore raw

PATCH 1/1 vdpa: fix use-after-free of vqs in vhost_vdpa_device_unrealize

From: lizhaoxin04 <[email protected]>

vhost_vdpa_device_unrealize() frees s->dev.vqs before vhost_dev_cleanup(), but vhost_dev_cleanup() still accesses hdev->vqs while tearing down virtqueues. This leads to a use-after-free and may crash QEMU with SIGSEGV during vDPA hot-unplug.

Save the vqs pointer in a local variable, call vhost_dev_cleanup(), and free it afterward. This matches the cleanup pattern used by vhost-scsi.

Fixes b430a2bd23 ("vdpa: add vdpa-dev support")
---
 hw/virtio/vdpa-dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/vdpa-dev.c +2 -1
index 94188d37bb..089a77f4d0 100644
@@ -172,6 +172,7 @@ static void vhost_vdpa_device_unrealize(DeviceState *dev)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
+    struct vhost_virtqueue *vqs = s->dev.vqs;
     int i;
 
     virtio_set_status(vdev, 0);
@@ -183,8 +184,8 @@ static void vhost_vdpa_device_unrealize(DeviceState *dev)
     virtio_cleanup(vdev);
 
     g_free(s->config);
-    g_free(s->dev.vqs);
     vhost_dev_cleanup(&s->dev);
+    g_free(vqs);
     g_free(s->vdpa.shared);
     qemu_close(s->vhostfd);
     s->vhostfd = -1;
Signature · 1 line
2.17.1