Skip to contentSkip to conversation

vhost-user: fix crashes on disconnect when iommu is on

PATCH In review Maxime Coquelin 2 participants 5 messages Jun 30, 2017

lore
Share & tools
lore
#0 lore raw

cover vhost-user: fix crashes on disconnect when iommu is on

This two patches series aims at fixing a couple of crashes that happens when the vhost-user socket is closed and iommu enabled.

Maxime Coquelin (2):
vhost: ensure vhost_ops are set before calling iotlb callback
vhost-user: unregister slave req handler at cleanup time

 hw/virtio/vhost-backend.c | 10 ++++++++--
 hw/virtio/vhost-user.c    |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.9.4
#1 lore raw

PATCH 1/2 vhost: ensure vhost_ops are set before calling iotlb callback

This patch fixes a crash that happens when vhost-user iommu support is enabled and vhost-user socket is closed.

When it happens, if an IOTLB invalidation notification is sent by the IOMMU, vhost_ops's NULL pointer is dereferenced.

---
 hw/virtio/vhost-backend.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/virtio/vhost-backend.c +8 -2
index 4e31de1..cb055e8 100644
@@ -309,7 +309,10 @@ int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
         return -EINVAL;
     }
 
-    return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+    if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
+        return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+
+    return -ENODEV;
 }
 
 int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
@@ -321,7 +324,10 @@ int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
     imsg.size = len;
     imsg.type = VHOST_IOTLB_INVALIDATE;
 
-    return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+    if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
+        return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+
+    return -ENODEV;
 }
 
 int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
Signature · 1 line
2.9.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] vhost: ensure vhost_ops are set before calling iotlb callback

----- Original Message -----

Quoted 7 lines

This patch fixes a crash that happens when vhost-user iommu support is enabled and vhost-user socket is closed.

When it happens, if an IOTLB invalidation notification is sent by the IOMMU, vhost_ops's NULL pointer is dereferenced.

looks fine to me,

Quoted 38 lines
---
 hw/virtio/vhost-backend.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/virtio/vhost-backend.c +4 -1
index 4e31de1..cb055e8 100644
@@ -309,7 +309,10 @@ int vhost_backend_update_device_iotlb(struct vhost_dev
 *dev,
         return -EINVAL;
     }
 
-    return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+    if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
+        return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+
+    return -ENODEV;
 }
 

int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,

hw/virtio/vhost-backend.c +4 -1
@@ -321,7 +324,10 @@ int vhost_backend_invalidate_device_iotlb(struct
 vhost_dev *dev,
     imsg.size = len;
     imsg.type = VHOST_IOTLB_INVALIDATE;
 
-    return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+    if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
+        return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
+
+    return -ENODEV;
 }
 

int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
--
2.9.4

#3 lore raw

PATCH 2/2 vhost-user: unregister slave req handler at cleanup time

In reply to Maxime Coquelin

If the backend sends a request just before closing the socket, the aio dispatcher might schedule its reading after the vhost device has been cleaned, leading to a NULL pointer dereference in slave_read();

vhost_user_cleanup() already closes the socket but it is not enough, the handler has to be unregistered.

---
 hw/virtio/vhost-user.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/vhost-user.c +1 -0
index 958ee09..2203011 100644
@@ -779,6 +779,7 @@ static int vhost_user_cleanup(struct vhost_dev *dev)
 
     u = dev->opaque;
     if (u->slave_fd >= 0) {
+        qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
         close(u->slave_fd);
         u->slave_fd = -1;
     }
Signature · 1 line
2.9.4
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] vhost-user: unregister slave req handler at cleanup time

----- Original Message -----

Quoted 9 lines

If the backend sends a request just before closing the socket, the aio dispatcher might schedule its reading after the vhost device has been cleaned, leading to a NULL pointer dereference in slave_read();

vhost_user_cleanup() already closes the socket but it is not enough, the handler has to be unregistered.

Quoted 20 lines
---
 hw/virtio/vhost-user.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/vhost-user.c +1 -1
index 958ee09..2203011 100644
@@ -779,6 +779,7 @@ static int vhost_user_cleanup(struct vhost_dev *dev)
 
     u = dev->opaque;
     if (u->slave_fd >= 0) {
+        qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
         close(u->slave_fd);
         u->slave_fd = -1;
     }
--

2.9.4