Skip to contentSkip to conversation

vhost-vdpa: cleanup memory maps when closing vdpa fds

PATCH In review Longpeng(Mike) 3 participants 11 messages Jan 31, 2023 – Mar 10, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost-vdpa: cleanup memory maps when closing vdpa fds

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

---
 drivers/vhost/vdpa.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
 		vhost_vdpa_remove_as(v, asid);
 }
 
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+	}
+}
+
 static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 			     struct vhost_iotlb *iotlb,
 			     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
 	vhost_vdpa_clean_irq(v);
 	vhost_vdpa_reset(v);
 	vhost_dev_stop(&v->vdev);
+	vhost_vdpa_clean_map(v);
 	vhost_vdpa_free_domain(v);
 	vhost_vdpa_config_put(v);
 	vhost_vdpa_cleanup(v);
Signature · 1 line
2.23.0
#1 lore raw

Hi guys, any suggestions?

Quoted 42 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

---
  drivers/vhost/vdpa.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
  		vhost_vdpa_remove_as(v, asid);
  }
 
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+	}
+}
+
  static int vhost_vdpa_va_map(struct vhost_vdpa *v,
  			     struct vhost_iotlb *iotlb,
  			     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
  	vhost_vdpa_clean_irq(v);
  	vhost_vdpa_reset(v);
  	vhost_dev_stop(&v->vdev);
+	vhost_vdpa_clean_map(v);
  	vhost_vdpa_free_domain(v);
  	vhost_vdpa_config_put(v);
  	vhost_vdpa_cleanup(v);
#2 lore raw
In reply to Longpeng(Mike)
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

Thanks

Quoted 30 lines
---
  drivers/vhost/vdpa.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +12 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
  		vhost_vdpa_remove_as(v, asid);
  }
 
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+	struct vhost_vdpa_as *as;
+	u32 asid;
+
+	for (asid = 0; asid < v->vdpa->nas; asid++) {
+		as = asid_to_as(v, asid);
+		if (as)
+			vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+	}
+}
+
  static int vhost_vdpa_va_map(struct vhost_vdpa *v,
  			     struct vhost_iotlb *iotlb,
  			     u64 iova, u64 size, u64 uaddr, u32 perm)

drivers/vhost/vdpa.c static int vhost_vdpa_release(struct inode *inode, struct file *filep) · line 1265

Quoted 7 lines
drivers/vhost/vdpa.c +1 -0
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode *inode, struct file *filep)
  	vhost_vdpa_clean_irq(v);
  	vhost_vdpa_reset(v);
  	vhost_dev_stop(&v->vdev);
+	vhost_vdpa_clean_map(v);
  	vhost_vdpa_free_domain(v);
  	vhost_vdpa_config_put(v);
  	vhost_vdpa_cleanup(v);

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

By the way, Jason, can you reproduce the problem in your machine?

Quoted 42 lines

Thanks

Quoted 36 lines

---
  drivers/vhost/vdpa.c | 13 +++++++++++++

  1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
           vhost_vdpa_remove_as(v, asid);
   }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
   static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                    struct vhost_iotlb *iotlb,
                    u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode 
 *inode, struct file *filep)
       vhost_vdpa_clean_irq(v);
       vhost_vdpa_reset(v);
       vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
       vhost_vdpa_free_domain(v);
       vhost_vdpa_config_put(v);
Quoted 1 line

      vhost_vdpa_cleanup(v);

.

#4 lore raw
Quoted 13 lines · Cloud Infrastructure Service Product Dept.)
Quoted 9 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Quoted 8 lines
Quoted 2 lines

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
        struct vhost_vdpa *v = filep->private_data;
        struct vhost_dev *d = &v->vdev;

        mutex_lock(&d->mutex);
        filep->private_data = NULL;
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
        vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
        vhost_vdpa_cleanup(v);
        mutex_unlock(&d->mutex);

        atomic_dec(&v->opened);
        complete(&v->completion);

        return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 34 lines
Quoted 34 lines

Thanks

Quoted 31 lines
---
  drivers/vhost/vdpa.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +12 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
          vhost_vdpa_remove_as(v, asid);
  }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
  static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                   struct vhost_iotlb *iotlb,
                   u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
      vhost_vdpa_clean_irq(v);
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
drivers/vhost/vdpa.c +1 -0
      vhost_vdpa_reset(v);
      vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
      vhost_vdpa_free_domain(v);
      vhost_vdpa_config_put(v);
      vhost_vdpa_cleanup(v);

.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
         struct vhost_vdpa *v = filep->private_data;
         struct vhost_dev *d = &v->vdev;

         mutex_lock(&d->mutex);
         filep->private_data = NULL;
         vhost_vdpa_clean_irq(v);
         vhost_vdpa_reset(v);
         vhost_dev_stop(&v->vdev);
         vhost_vdpa_free_domain(v);
         vhost_vdpa_config_put(v);
         vhost_vdpa_cleanup(v);
         mutex_unlock(&d->mutex);

         atomic_dec(&v->opened);
         complete(&v->completion);

         return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 34 lines
Quoted 34 lines

Thanks

Quoted 31 lines
---
   drivers/vhost/vdpa.c | 13 +++++++++++++
   1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +12 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
           vhost_vdpa_remove_as(v, asid);
   }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
   static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                    struct vhost_iotlb *iotlb,
                    u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
       vhost_vdpa_clean_irq(v);
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
drivers/vhost/vdpa.c +1 -0
       vhost_vdpa_reset(v);
       vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
       vhost_vdpa_free_domain(v);
       vhost_vdpa_config_put(v);
       vhost_vdpa_cleanup(v);

.

.

#6 lore raw
Quoted 44 lines · Cloud Infrastructure Service Product Dept.)
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

Quoted 7 lines

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

We do:

vhost_vdpa_cleanup()
vhost_vdpa_remove_as()
vhost_vdpa_iotlb_unmap()
vhost_vdpa_pa_unmap()
unpin_user_pages()
vhost_vdpa_general_unmap()
iommu_unmap()
?

Btw, it looks like we should call vhost_vdpa_free_domain() *after* vhost_vdpa_cleanup() otherwise it's a UAF?

Thanks

Quoted 75 lines
Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
         struct vhost_vdpa *v = filep->private_data;
         struct vhost_dev *d = &v->vdev;

         mutex_lock(&d->mutex);
         filep->private_data = NULL;
         vhost_vdpa_clean_irq(v);
         vhost_vdpa_reset(v);
         vhost_dev_stop(&v->vdev);
         vhost_vdpa_free_domain(v);
         vhost_vdpa_config_put(v);
         vhost_vdpa_cleanup(v);
         mutex_unlock(&d->mutex);

         atomic_dec(&v->opened);
         complete(&v->completion);

         return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 43 lines
Quoted 42 lines

Thanks

Quoted 36 lines
---
   drivers/vhost/vdpa.c | 13 +++++++++++++
   1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
           vhost_vdpa_remove_as(v, asid);
   }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
   static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                    struct vhost_iotlb *iotlb,
                    u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
       vhost_vdpa_clean_irq(v);
       vhost_vdpa_reset(v);
       vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
       vhost_vdpa_free_domain(v);
       vhost_vdpa_config_put(v);
Quoted 1 line
       vhost_vdpa_cleanup(v);

.

Quoted 2 lines
Quoted 1 line

.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#7 lore raw
Quoted 56 lines · Jason Wang
Quoted 44 lines · Cloud Infrastructure Service Product Dept.)
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

Quoted 7 lines

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

We do:

Quoted 9 lines

vhost_vdpa_cleanup()
vhost_vdpa_remove_as()
vhost_vdpa_iotlb_unmap()
vhost_vdpa_pa_unmap()
unpin_user_pages()
vhost_vdpa_general_unmap()
iommu_unmap()
?

Oh, my codebase is linux-6.2-rc2 and the commit c070c1912a8 (vhost-vdpa: fix an iotlb memory leak) already fixed this bug in linux-6.2-rc3.

Quoted 3 lines

Btw, it looks like we should call vhost_vdpa_free_domain() *after* vhost_vdpa_cleanup() otherwise it's a UAF?

I think so, the v->domain is set to NULL in vhost_vdpa_free_domain(), it seems would trigger null-pointer access in my case.

Quoted 81 lines

Thanks

Quoted 75 lines
Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
          struct vhost_vdpa *v = filep->private_data;
          struct vhost_dev *d = &v->vdev;

          mutex_lock(&d->mutex);
          filep->private_data = NULL;
          vhost_vdpa_clean_irq(v);
          vhost_vdpa_reset(v);
          vhost_dev_stop(&v->vdev);
          vhost_vdpa_free_domain(v);
          vhost_vdpa_config_put(v);
          vhost_vdpa_cleanup(v);
          mutex_unlock(&d->mutex);

          atomic_dec(&v->opened);
          complete(&v->completion);

          return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 43 lines
Quoted 42 lines

Thanks

Quoted 36 lines
---
    drivers/vhost/vdpa.c | 13 +++++++++++++
    1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
            vhost_vdpa_remove_as(v, asid);
    }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
    static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                     struct vhost_iotlb *iotlb,
                     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
        vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
Quoted 1 line
        vhost_vdpa_cleanup(v);

.

Quoted 2 lines
Quoted 1 line

.

.

#8 lore raw
Quoted 68 lines · Cloud Infrastructure Service Product Dept.)
Quoted 56 lines · Jason Wang
Quoted 44 lines · Cloud Infrastructure Service Product Dept.)
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

Quoted 7 lines

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

We do:

Quoted 9 lines

vhost_vdpa_cleanup()
vhost_vdpa_remove_as()
vhost_vdpa_iotlb_unmap()
vhost_vdpa_pa_unmap()
unpin_user_pages()
vhost_vdpa_general_unmap()
iommu_unmap()
?

Quoted 8 lines

Oh, my codebase is linux-6.2-rc2 and the commit c070c1912a8 (vhost-vdpa: fix an iotlb memory leak) already fixed this bug in linux-6.2-rc3.

Quoted 3 lines

Btw, it looks like we should call vhost_vdpa_free_domain() *after* vhost_vdpa_cleanup() otherwise it's a UAF?

I think so, the v->domain is set to NULL in vhost_vdpa_free_domain(), it seems would trigger null-pointer access in my case.

OK I'll drop this patch for now then?

Quoted 77 lines
Quoted 77 lines

Thanks

Quoted 75 lines
Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
          struct vhost_vdpa *v = filep->private_data;
          struct vhost_dev *d = &v->vdev;

          mutex_lock(&d->mutex);
          filep->private_data = NULL;
          vhost_vdpa_clean_irq(v);
          vhost_vdpa_reset(v);
          vhost_dev_stop(&v->vdev);
          vhost_vdpa_free_domain(v);
          vhost_vdpa_config_put(v);
          vhost_vdpa_cleanup(v);
          mutex_unlock(&d->mutex);

          atomic_dec(&v->opened);
          complete(&v->completion);

          return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 43 lines
Quoted 42 lines

Thanks

Quoted 36 lines
---
    drivers/vhost/vdpa.c | 13 +++++++++++++
    1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
            vhost_vdpa_remove_as(v, asid);
    }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
    static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                     struct vhost_iotlb *iotlb,
                     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
        vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
Quoted 1 line
        vhost_vdpa_cleanup(v);

.

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

.

.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#9 lore raw
In reply to Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
Quoted 69 lines · Cloud Infrastructure Service Product Dept.)
Quoted 56 lines · Jason Wang
Quoted 44 lines · Cloud Infrastructure Service Product Dept.)
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

Quoted 7 lines

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

We do:

Quoted 9 lines

vhost_vdpa_cleanup()
vhost_vdpa_remove_as()
vhost_vdpa_iotlb_unmap()
vhost_vdpa_pa_unmap()
unpin_user_pages()
vhost_vdpa_general_unmap()
iommu_unmap()
?

Quoted 8 lines

Oh, my codebase is linux-6.2-rc2 and the commit c070c1912a8 (vhost-vdpa:
fix an iotlb memory leak) already fixed this bug in linux-6.2-rc3.

Quoted 3 lines

Btw, it looks like we should call vhost_vdpa_free_domain() *after* vhost_vdpa_cleanup() otherwise it's a UAF?

I think so, the v->domain is set to NULL in vhost_vdpa_free_domain(), it seems would trigger null-pointer access in my case.

Patch are welcomed.

Thanks

Quoted 78 lines
Quoted 78 lines

Thanks

Quoted 75 lines
Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
          struct vhost_vdpa *v = filep->private_data;
          struct vhost_dev *d = &v->vdev;

          mutex_lock(&d->mutex);
          filep->private_data = NULL;
          vhost_vdpa_clean_irq(v);
          vhost_vdpa_reset(v);
          vhost_dev_stop(&v->vdev);
          vhost_vdpa_free_domain(v);
          vhost_vdpa_config_put(v);
          vhost_vdpa_cleanup(v);
          mutex_unlock(&d->mutex);

          atomic_dec(&v->opened);
          complete(&v->completion);

          return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 43 lines
Quoted 42 lines

Thanks

Quoted 36 lines
---
    drivers/vhost/vdpa.c | 13 +++++++++++++
    1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
            vhost_vdpa_remove_as(v, asid);
    }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
    static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                     struct vhost_iotlb *iotlb,
                     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
        vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
Quoted 1 line
        vhost_vdpa_cleanup(v);

.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

.

.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#10 lore raw
In reply to Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
Quoted 68 lines · Cloud Infrastructure Service Product Dept.)
Quoted 56 lines · Jason Wang
Quoted 44 lines · Cloud Infrastructure Service Product Dept.)
Quoted 21 lines · Jason Wang
Quoted 19 lines · Cloud Infrastructure Service Product Dept.)
Quoted 11 lines · Jason Wang
Quoted 5 lines · Longpeng(Mike)

From: Longpeng <[email protected]>

We must cleanup all memory maps when closing the vdpa fds, otherwise some critical resources (e.g. memory, iommu map) will leaked if the userspace exits unexpectedly (e.g. kill -9).

Sounds like a bug of the kernel, should we fix there?

For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl.

Quoted 8 lines
Quoted 2 lines

So maybe we release these resources in vdpa framework in kernel is a suitable choice?

I think I need understand what does "resources" mean here:

For iommu mapping, it should be freed by vhost_vdpa_free_domain() in vhost_vdpa_release()?

Please consider the following lifecycle of the vdpa device:

1. vhost_vdpa_open
vhost_vdpa_alloc_domain

2. vhost_vdpa_pa_map
pin_user_pages
vhost_vdpa_map
iommu_map

Quoted 7 lines

3. kill QEMU

4. vhost_vdpa_release
vhost_vdpa_free_domain

In this case, we have no opportunity to invoke unpin_user_pages or iommu_unmap to free the memory.

We do:

Quoted 9 lines

vhost_vdpa_cleanup()
vhost_vdpa_remove_as()
vhost_vdpa_iotlb_unmap()
vhost_vdpa_pa_unmap()
unpin_user_pages()
vhost_vdpa_general_unmap()
iommu_unmap()
?

Quoted 2 lines

Oh, my codebase is linux-6.2-rc2 and the commit c070c1912a8 (vhost-vdpa: fix an iotlb memory leak) already fixed this bug in linux-6.2-rc3.

OK I dropped this.

Quoted 87 lines
Quoted 3 lines

Btw, it looks like we should call vhost_vdpa_free_domain() *after* vhost_vdpa_cleanup() otherwise it's a UAF?

I think so, the v->domain is set to NULL in vhost_vdpa_free_domain(), it seems would trigger null-pointer access in my case.

Quoted 81 lines

Thanks

Quoted 75 lines
Quoted 75 lines

static int vhost_vdpa_release(struct inode *inode, struct file *filep)

{
          struct vhost_vdpa *v = filep->private_data;
          struct vhost_dev *d = &v->vdev;

          mutex_lock(&d->mutex);
          filep->private_data = NULL;
          vhost_vdpa_clean_irq(v);
          vhost_vdpa_reset(v);
          vhost_dev_stop(&v->vdev);
          vhost_vdpa_free_domain(v);
          vhost_vdpa_config_put(v);
          vhost_vdpa_cleanup(v);
          mutex_unlock(&d->mutex);

          atomic_dec(&v->opened);
          complete(&v->completion);

          return 0;
}
Quoted 3 lines

By the way, Jason, can you reproduce the problem in your machine?

Haven't got time in doing this but it should be the responsibility of the author to validate this anyhow.

Thanks

Quoted 43 lines
Quoted 42 lines

Thanks

Quoted 36 lines
---
    drivers/vhost/vdpa.c | 13 +++++++++++++
    1 file changed, 13 insertions(+)
drivers/vhost/vdpa.c +13 -0
index a527eeeac637..37477cffa5aa 100644
@@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v,
            vhost_vdpa_remove_as(v, asid);
    }
+static void vhost_vdpa_clean_map(struct vhost_vdpa *v)
+{
+    struct vhost_vdpa_as *as;
+    u32 asid;
+
+    for (asid = 0; asid < v->vdpa->nas; asid++) {
+        as = asid_to_as(v, asid);
+        if (as)
+            vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1);
+    }
+}
+
    static int vhost_vdpa_va_map(struct vhost_vdpa *v,
                     struct vhost_iotlb *iotlb,
                     u64 iova, u64 size, u64 uaddr, u32 perm)
@@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode
 *inode, struct file *filep)
        vhost_vdpa_clean_irq(v);
        vhost_vdpa_reset(v);
        vhost_dev_stop(&v->vdev);
+    vhost_vdpa_clean_map(v);
        vhost_vdpa_free_domain(v);
        vhost_vdpa_config_put(v);
Quoted 1 line
        vhost_vdpa_cleanup(v);

.

Quoted 2 lines
Quoted 1 line

.

.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization