Skip to contentSkip to conversation

fix two virtio queues memleak

PATCH Superseded Pan Nengyuan 2 participants 5 messages Mar 27, 2020 – Mar 28, 2020

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover fix two virtio queues memleak

This series fix two vqs leak:
1. Do delete vqs on the error path in virtio_blk_device_realize().
2. Do delete vqs in virtio_iommu_device_unrealize() to fix another leaks.

v2->v1:
- Fix incorrect free in virtio_blk_device_realize, it will cause a uaf.

v3->v2:
- Aslo clean 's->as_by_busptr' hash table in virtio_iommu_device_unrealize.(Suggested by Stefano Garzarella)

Pan Nengyuan (2):
virtio-blk: delete vqs on the error path in realize()
virtio-iommu: delete vqs in unrealize to fix memleak

 hw/block/virtio-blk.c    | 3 +++
 hw/virtio/virtio-iommu.c | 3 +++
 2 files changed, 6 insertions(+)
Signature · 1 line
2.18.2
#1 lore raw

PATCH v3 1/2 virtio-blk: delete vqs on the error path in realize()

virtio_vqs forgot to free on the error path in realize(). Fix that.

The asan stack:
Direct leak of 14336 byte(s) in 1 object(s) allocated from:

    #0 0x7f58b93fd970 in __interceptor_calloc (/lib64/libasan.so.5+0xef970)
    #1 0x7f58b858249d in g_malloc0 (/lib64/libglib-2.0.so.0+0x5249d)
    #2 0x5562cc627f49 in virtio_add_queue /mnt/sdb/qemu/hw/virtio/virtio.c:2413
    #3 0x5562cc4b524a in virtio_blk_device_realize /mnt/sdb/qemu/hw/block/virtio-blk.c:1202
    #4 0x5562cc613050 in virtio_device_realize /mnt/sdb/qemu/hw/virtio/virtio.c:3615
    #5 0x5562ccb7a568 in device_set_realized /mnt/sdb/qemu/hw/core/qdev.c:891
    #6 0x5562cd39cd45 in property_set_bool /mnt/sdb/qemu/qom/object.c:2238

---
v2->v1:
- Fix incorrect free in v1, it will cause a uaf.
---
Cc: Stefan Hajnoczi <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Max Reitz <[email protected]>
Cc: [email protected]

---
 hw/block/virtio-blk.c | 3 +++
 1 file changed, 3 insertions(+)
hw/block/virtio-blk.c +3 -0
index 142863a3b2..97ba8a2187 100644
@@ -1204,6 +1204,9 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
     virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
     if (err != NULL) {
         error_propagate(errp, err);
+        for (i = 0; i < conf->num_queues; i++) {
+            virtio_del_queue(vdev, i);
+        }
         virtio_cleanup(vdev);
         return;
     }
Signature · 1 line
2.18.2
#2 lore raw

PATCH v3 2/2 virtio-iommu: delete vqs in unrealize to fix memleak

In reply to Pan Nengyuan

req_vq/event_vq forgot to free in unrealize. Fix that.
And aslo do clean 's->as_by_busptr' hash table in unrealize to fix another leak.

---
Cc: Eric Auger <[email protected]>
---
v3->v1/v2:
- Aslo clean 's->as_by_busptr' hash table in unrealize.(Suggested by Stefano Garzarella)

---
 hw/virtio/virtio-iommu.c | 3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-iommu.c +3 -0
index 4cee8083bc..694698fa0f 100644
@@ -696,7 +696,10 @@ static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)
     g_tree_destroy(s->domains);
     g_tree_destroy(s->endpoints);
 
+    virtio_delete_queue(s->req_vq);
+    virtio_delete_queue(s->event_vq);
     virtio_cleanup(vdev);
+    g_hash_table_destroy(s->as_by_busptr);
 }
 
 static void virtio_iommu_device_reset(VirtIODevice *vdev)
Signature · 1 line
2.18.2
#3 lore raw

Re: [PATCH v3 2/2] virtio-iommu: delete vqs in unrealize to fix memleak

Quoted 2 lines · Pan Nengyuan

req_vq/event_vq forgot to free in unrealize. Fix that.
And aslo do clean 's->as_by_busptr' hash table in unrealize to fix another leak.

s/aslo/also

Maybe we can also update the subject in something like this:
"virtio-iommu: avoid memleak in the unrealize"

Quoted 17 lines

---
Cc: Eric Auger <[email protected]>
---
v3->v1/v2:
- Aslo clean 's->as_by_busptr' hash table in unrealize.(Suggested by Stefano Garzarella)

---
 hw/virtio/virtio-iommu.c | 3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-iommu.c +0 -0
index 4cee8083bc..694698fa0f 100644

hw/virtio/virtio-iommu.c static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) · line 702

Quoted 7 lines
hw/virtio/virtio-iommu.c +3 -0
@@ -696,7 +696,10 @@ static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)
     g_tree_destroy(s->domains);
     g_tree_destroy(s->endpoints);
 
+    virtio_delete_queue(s->req_vq);
+    virtio_delete_queue(s->event_vq);
     virtio_cleanup(vdev);
+    g_hash_table_destroy(s->as_by_busptr);

If you need to respin, you could move g_hash_table_destroy() at the beggining of unrealize(), just to follow a reverse order of realize().

Thanks,
Stefano

Quoted 7 lines
hw/virtio/virtio-iommu.c +0 -0
 }
 
 static void virtio_iommu_device_reset(VirtIODevice *vdev)
Signature · 1 line
2.18.2
#4 lore raw

Re: [PATCH v3 2/2] virtio-iommu: delete vqs in unrealize to fix memleak

Quoted 8 lines · Stefano Garzarella
Quoted 2 lines · Pan Nengyuan

req_vq/event_vq forgot to free in unrealize. Fix that.
And aslo do clean 's->as_by_busptr' hash table in unrealize to fix another leak.

s/aslo/also

Maybe we can also update the subject in something like this:
"virtio-iommu: avoid memleak in the unrealize"

OK.

Quoted 21 lines
Quoted 21 lines

---
Cc: Eric Auger <[email protected]>
---
v3->v1/v2:
- Aslo clean 's->as_by_busptr' hash table in unrealize.(Suggested by Stefano Garzarella)

---
 hw/virtio/virtio-iommu.c | 3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-iommu.c +0 -0
index 4cee8083bc..694698fa0f 100644
@@ -696,7 +696,10 @@ static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)
     g_tree_destroy(s->domains);
     g_tree_destroy(s->endpoints);
 
Quoted 8 lines

hw/virtio/virtio-iommu.c static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) · line 702

Quoted 4 lines
hw/virtio/virtio-iommu.c +3 -0
+    virtio_delete_queue(s->req_vq);
+    virtio_delete_queue(s->event_vq);
     virtio_cleanup(vdev);
+    g_hash_table_destroy(s->as_by_busptr);

If you need to respin, you could move g_hash_table_destroy() at the beggining of unrealize(), just to follow a reverse order of realize().

OK.

Thanks.

Quoted 12 lines

Thanks,
Stefano

Quoted 7 lines
hw/virtio/virtio-iommu.c +0 -0
 }
 
 static void virtio_iommu_device_reset(VirtIODevice *vdev)
Signature · 1 line
2.18.2