Skip to contentSkip to conversation

virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers

PATCH New Lei Xiang 3 participants 9 messages Aug 9, 2022 – Dec 27, 2022

lore
Share & tools
lore

Lei Xiang

author
#0 lore raw

PATCH 1/1 virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers

From: Lei Xiang <[email protected]>

proxy->vector_irqfd did not free when set guest notifier failed.

---
 hw/virtio/virtio-pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index 45327f0..75188a9 100644
@@ -1058,6 +1058,10 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+    g_free(proxy->vector_irqfd);
+    proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 1 line
1.9.1
#1 lore raw

PATCH RESEND 1/1 virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers

proxy->vector_irqfd did not free when set guest notifier failed.

---
 hw/virtio/virtio-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+   g_free(proxy->vector_irqfd);
+   proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 2 lines
No virus found
		Checked by Hillstone Network AntiVirus
#2 lore raw

PATCH RESEND 1/1 virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers

proxy->vector_irqfd did not free when set guest notifier failed.

---
 hw/virtio/virtio-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+   g_free(proxy->vector_irqfd);
+   proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 2 lines
No virus found
		Checked by Hillstone Network AntiVirus
#3 lore raw

PATCH RESEND 1/1 virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers

In reply to leixiang

proxy->vector_irqfd did not free when set guest notifier failed.

---
 hw/virtio/virtio-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+   g_free(proxy->vector_irqfd);
+   proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 2 lines
No virus found
		Checked by Hillstone Network AntiVirus
#4 lore raw
Quoted 5 lines · leixiang

proxy->vector_irqfd did not free when set guest notifier failed.

Thanks a lot! Fixes tag? When was the leak introduced?

Quoted 24 lines
---
 hw/virtio/virtio-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+   g_free(proxy->vector_irqfd);
+   proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 2 lines
No virus found
		Checked by Hillstone Network AntiVirus
#5 lore raw

As we found, it will cause guest os crash when reboot guest os. because in 'virtio_pci_set_guest_notifiers' function, if it failed to set guest notifier, the error handler did not release the 'proxy->vector_irqfd' which already alloced. when reboot guest os, into 'virtio_pci_set_guest_notifiers' function again, it will jump into  'msix_unset_vector_notifiers' function, this function will check device notifier hooks, cause hooks were null, it will cause a assert error, then the guest os crash down. We put the error message in our test below:

2022-07-29 07:49:46.591+0000: Domain id=30 is tainted: host-cpu char device redirected to /dev/pts/4 (label charserial0)
2022-07-29T07:49:48.319622Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:49:57.974985Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:50:01.834092Z qemu-4.1: nvqs:2,pci_dev name:virtio-net-pci
2022-07-29T07:50:01.834207Z qemu-4.1: Error binding guest notifier: 11
2022-07-29T07:50:01.834218Z qemu-4.1: unable to start vhost net: 11: falling back on userspace virtio
2022-07-29T07:50:01.865367Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:50:01.865506Z qemu-4.1: virtio-blk failed to set guest notifier (-11), ensure -accel kvm is set.
2022-07-29T07:50:01.865518Z qemu-4.1: virtio_bus_start_ioeventfd: failed. Fallback to userspace (slower).
2022-07-29T07:50:02.005930Z qemu-4.1: nvqs:3,pci_dev name:virtio-scsi-pci
2022-07-29T07:50:02.006079Z qemu-4.1: virtio-scsi: Failed to set guest notifiers (-11), ensure -accel kvm is set.
2022-07-29T07:50:02.006091Z qemu-4.1: virtio_bus_start_ioeventfd: failed. Fallback to userspace (slower).
2022-07-29T07:52:44.108820Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:52:52.886792Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
qemu-4.1: hw/pci/msix.c:612: msix_unset_vector_notifiers: Assertion `dev->msix_vector_use_notifier && dev->msix_vector_release_notifier' failed.
2022-07-29 07:52:53.377+0000: shutting down, reason=crashed

----

主 题:Re: [RESEND PATCH] virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers
日 期:2022-11-30 14:59
发件人:Michael S. Tsirkin
收件人:雷翔;

Quoted 5 lines · leixiang

proxy->vector_irqfd did not free when set guest notifier failed.

Signed-off-by Lei Xiang
Tested-by Zeng Chi
Suggested-by Xie Ming

Thanks a lot! Fixes tag? When was the leak introduced?

Quoted 24 lines

---
hw/virtio/virtio-pci.c | 6 ++++++

1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -1
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
 while (--n >= 0) {
 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
 }
+
+ g_free(proxy->vector_irqfd);
+ proxy->vector_irqfd = NULL;
+
 return r;
 }
 
--

No virus found
Checked by Hillstone Network AntiVirus

#6 lore raw
Quoted 25 lines · 雷翔

As we found, it will cause guest os crash when reboot guest os. because in 'virtio_pci_set_guest_notifiers' function, if it failed to set guest notifier, the error handler did not release the 'proxy->vector_irqfd' which already alloced. when reboot guest os, into 'virtio_pci_set_guest_notifiers' function again, it will jump into 'msix_unset_vector_notifiers' function, this function will check device notifier hooks, cause hooks were null, it will cause a assert error, then the guest os crash down.

We put the error message in our test below:

2022-07-29 07:49:46.591+0000: Domain id=30 is tainted: host-cpu char device
redirected to /dev/pts/4 (label charserial0)
2022-07-29T07:49:48.319622Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:49:57.974985Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:50:01.834092Z qemu-4.1: nvqs:2,pci_dev name:virtio-net-pci
2022-07-29T07:50:01.834207Z qemu-4.1: Error binding guest notifier: 11
2022-07-29T07:50:01.834218Z qemu-4.1: unable to start vhost net: 11: falling
back on userspace virtio
2022-07-29T07:50:01.865367Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:50:01.865506Z qemu-4.1: virtio-blk failed to set guest notifier
(-11), ensure -accel kvm is set.
2022-07-29T07:50:01.865518Z qemu-4.1: virtio_bus_start_ioeventfd: failed.
Fallback to userspace (slower).
2022-07-29T07:50:02.005930Z qemu-4.1: nvqs:3,pci_dev name:virtio-scsi-pci
2022-07-29T07:50:02.006079Z qemu-4.1: virtio-scsi: Failed to set guest
Quoted 12 lines

notifiers (-11), ensure -accel kvm is set.

2022-07-29T07:50:02.006091Z qemu-4.1: virtio_bus_start_ioeventfd: failed.
Fallback to userspace (slower).
2022-07-29T07:52:44.108820Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
2022-07-29T07:52:52.886792Z qemu-4.1: nvqs:1,pci_dev name:virtio-blk-pci
qemu-4.1: hw/pci/msix.c:612: msix_unset_vector_notifiers: Assertion `dev->
msix_vector_use_notifier && dev->msix_vector_release_notifier' failed.
2022-07-29 07:52:53.377+0000: shutting down, reason=crashed

Thanks! My question is, does this also reproduce with qemu 7.1 or is the problem new in 7.2?

Quoted 49 lines

----

主 题:Re: [RESEND PATCH] virtio-pci: fix vector_irqfd leak in virtio_pci_set_guest_notifiers
日 期:2022-11-30 14:59
发件人:Michael S. Tsirkin
收件人:雷翔;

Quoted 5 lines · leixiang

proxy->vector_irqfd did not free when set guest notifier failed.

Signed-off-by Lei Xiang
Tested-by Zeng Chi
Suggested-by Xie Ming

Thanks a lot! Fixes tag? When was the leak introduced?

Quoted 24 lines

---
hw/virtio/virtio-pci.c | 6 ++++++

1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -1
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
 while (--n >= 0) {
 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
 }
+
+ g_free(proxy->vector_irqfd);
+ proxy->vector_irqfd = NULL;
+
 return r;
 }
 
--

No virus found
Checked by Hillstone Network AntiVirus

#7 lore raw
In reply to leixiang
Quoted 1 line · leixiang

proxy->vector_irqfd did not free when set guest notifier failed.

Can you pls add a Fixes tag so people know where to backport this?

Quoted 3 lines

Looking at the code I see this:

    /* Must set vector notifier after guest notifier has been assigned */
    if ((with_irqfd ||
         (vdev->use_guest_notifier_mask && k->guest_notifier_mask)) &&
        assign) {
        if (with_irqfd) {
            proxy->vector_irqfd =
                g_malloc0(sizeof(*proxy->vector_irqfd) *
                          msix_nr_vectors_allocated(&proxy->pci_dev));
            r = kvm_virtio_pci_vector_vq_use(proxy, nvqs);
            if (r < 0) {
                goto config_assign_error;
            }
            r = kvm_virtio_pci_vector_config_use(proxy);
            if (r < 0) {
                goto config_error;
            }
        }

        r = msix_set_vector_notifiers(&proxy->pci_dev, virtio_pci_vector_unmask,
                                      virtio_pci_vector_mask,
                                      virtio_pci_vector_poll);
        if (r < 0) {
            goto notifiers_error;
        }
    }

doesn't this mean g_free belongs at the label config_assign_error?

Quoted 24 lines
---
 hw/virtio/virtio-pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/virtio/virtio-pci.c +4 -0
index c6b47a9c..4862f83b 100644
@@ -1038,6 +1038,12 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+
+   g_free(proxy->vector_irqfd);
+   proxy->vector_irqfd = NULL;
+
     return r;
 }
 
Signature · 2 lines
No virus found
		Checked by Hillstone Network AntiVirus

The patch is corrupted. Line counts are wrong, and your antivirus added trash at the end.

Signature · 1 line
MST
#8 lore raw

PATCH RESEND 1/1 virtio-pci: fix proxy->vector_irqfd leak in virtio_pci_set_guest_notifiers

proxy->vector_irqfd did not free when kvm_virtio_pci_vector_use or msix_set_vector_notifiers failed in virtio_pci_set_guest_notifiers.

Fixes 7d37d351
---
 hw/virtio/virtio-pci.c | 2 ++
 1 file changed, 2 insertions(+)
hw/virtio/virtio-pci.c +2 -0
index a50c5a57d7..5339be4e01 100644
@@ -1063,6 +1063,8 @@ assign_error:
     while (--n >= 0) {
         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
     }
+    g_free(proxy->vector_irqfd);
+    proxy->vector_irqfd = NULL;
     return r;
 }
 
Signature · 1 line
2.25.1