Skip to contentSkip to conversation

pci: add checking whether the device is realized before unlinking the capability

PATCH In review SeokYeon Hwang 3 participants 6 messages Jun 25, 2014 – Jun 28, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: add checking whether the device is realized before unlinking the capability

In case of the unrealized "pdev", memory can be illegally accessed and corrupted. Refer to device_unparent() in the commit 5c21ce77d7e5643089ceec556c0408445d017f32.

Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/pci/pci.c +6 -1
index 49eca95..bb7f0c5 100644
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
Signature · 1 line
1.9.1
#1 lore raw
Quoted 5 lines · SeokYeon Hwang

In case of the unrealized "pdev", memory can be illegally accessed and corrupted. Refer to device_unparent() in the commit 5c21ce77d7e5643089ceec556c0408445d017f32.

Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae

Another case of qemu mailing list dropping patches :( I will bounce it now so people can see the original message.

Quoted 24 lines
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/pci/pci.c +6 -1
index 49eca95..bb7f0c5 100644
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
Signature · 1 line
1.9.1
#2 lore raw
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · SeokYeon Hwang

In case of the unrealized "pdev", memory can be illegally accessed and corrupted. Refer to device_unparent() in the commit 5c21ce77d7e5643089ceec556c0408445d017f32.

Hi,
Thank you for submitting the patch.

Can you please send to the list how to reproduce the issue? Is this a regression? Before the above commit did it work?

Quoted 13 lines
Quoted 3 lines
Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae

Another case of qemu mailing list dropping patches :( I will bounce it now so people can see the original message.

Quoted 4 lines
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, · line 2061

Quoted 12 lines
hw/pci/pci.c +3 -1
index 49eca95..bb7f0c5 100644
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {

The 'qdev' field and 'realize' property are private, you should use one of QOM's 'property_get' methods.

I am also concerned about adding the realize check here, it seems too "deep" in implementation, seeing this checks everywhere doesn't seem a good idea.
I think we should first understand the root cause and try making this change in a higher level.

I hope I helped,
Marcel

Quoted 9 lines
Quoted 8 lines
hw/pci/pci.c +3 -0
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
Signature · 1 line
1.9.1
#3 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 2 lines · SeokYeon Hwang
Quoted 1 line

In case of the unrealized "pdev", memory can be illegally accessed and

corrupted.

Quoted 1 line
Quoted 1 line

Refer to device_unparent() in the commit

5c21ce77d7e5643089ceec556c0408445d017f32.
Hi,
Thank you for submitting the patch.

Can you please send to the list how to reproduce the issue? Is this a regression? Before the above commit did it work?

Hi,

When VirtIO device is hot-unplugged, Qemu unrealizes the device first then child_bus by device_unparent(). (After the commit 5c21ce77d7e5643089ceec556c0408445d017f32.)

For example, if I suppose to hot-unplug "virtio block" device, unparent "virtio-blk-pci" first, then "virtio-blk".

virtio_bus_device_unplugged() function in virtio-bus.c calls "msix_uninit()" with the parameter of its parent PCI device. The problem is that it accesses already freed member of pdev, such as "pdev->config".
This illegal access causes the heap corruption, and it kills Qemu process suddenly.

You can detect this illegal access by using "electric fence" or "valgrind".

Quoted 29 lines
Quoted 13 lines
Quoted 4 lines
Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae

Another case of qemu mailing list dropping patches :( I will bounce it now so people can see the original message.

Quoted 4 lines
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 9 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2059

Quoted 9 lines
hw/pci/pci.c index 49eca95..bb7f0c5 renamed from hw/pci/pci.c +0 -0

100644

hw/pci/pci.c +0 -1
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev,
 uint8_t cap_id,
 /* Unlink capability from the pci config space. */  void
 pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)  {
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id,

&prev);

Quoted 3 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, · line 2061

Quoted 3 lines
hw/pci/pci.c +3 -0
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {

The 'qdev' field and 'realize' property are private, you should use one of QOM's 'property_get' methods.

Quoted 8 lines

I am also concerned about adding the realize check here, it seems too "deep" in implementation, seeing this checks everywhere doesn't seem a good idea.
I think we should first understand the root cause and try making this change in a higher level.

I hope I helped,
Marcel

Ok, I see. I think you're right.
If needed, I will send an another patch using "object_property_get_bool()".

Thanks.

Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
hw/pci/pci.c +3 -1
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
--

1.9.1

#4 lore raw
Quoted 17 lines · SeokYeon Hwang
Quoted 11 lines · Michael S. Tsirkin
Quoted 2 lines · SeokYeon Hwang
Quoted 1 line

In case of the unrealized "pdev", memory can be illegally accessed and

corrupted.

Quoted 1 line
Quoted 1 line

Refer to device_unparent() in the commit

5c21ce77d7e5643089ceec556c0408445d017f32.
Hi,
Thank you for submitting the patch.

Can you please send to the list how to reproduce the issue? Is this a regression? Before the above commit did it work?

Hi,

When VirtIO device is hot-unplugged, Qemu unrealizes the device first then child_bus by device_unparent(). (After the commit 5c21ce77d7e5643089ceec556c0408445d017f32.)

Quoted 7 lines

For example, if I suppose to hot-unplug "virtio block" device, unparent "virtio-blk-pci" first, then "virtio-blk".

virtio_bus_device_unplugged() function in virtio-bus.c calls "msix_uninit()" with the parameter of its parent PCI device. The problem is that it accesses already freed member of pdev, such as "pdev->config".
This illegal access causes the heap corruption, and it kills Qemu process suddenly.

You can detect this illegal access by using "electric fence" or "valgrind".

Does this still trigger for you with latest master? I tried this:

hw/pci/pci.c +5 -0
index 17ed510..fc81803 100644
@@ -791,6 +791,11 @@ static void pci_config_free(PCIDevice *pci_dev)
     g_free(pci_dev->wmask);
     g_free(pci_dev->w1cmask);
     g_free(pci_dev->used);
+    pci_dev->config = NULL;
+    pci_dev->cmask = NULL;
+    pci_dev->wmask = NULL;
+    pci_dev->w1cmask = NULL;
+    pci_dev->used = NULL;
 }
 
 static void do_pci_unregister_device(PCIDevice *pci_dev)

does not trigger crash after hot-unplug of virtio-net-pci on master.

Quoted 56 lines
Quoted 29 lines
Quoted 13 lines
Quoted 4 lines
Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae

Another case of qemu mailing list dropping patches :( I will bounce it now so people can see the original message.

Quoted 4 lines
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 9 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2059

Quoted 9 lines
hw/pci/pci.c index 49eca95..bb7f0c5 renamed from hw/pci/pci.c +0 -0

100644

hw/pci/pci.c +0 -1
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev,
 uint8_t cap_id,
 /* Unlink capability from the pci config space. */  void
 pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)  {
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id,

&prev);

Quoted 3 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2061

Quoted 3 lines
hw/pci/pci.c +3 -0
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {

The 'qdev' field and 'realize' property are private, you should use one of QOM's 'property_get' methods.

Quoted 8 lines

I am also concerned about adding the realize check here, it seems too "deep" in implementation, seeing this checks everywhere doesn't seem a good idea.
I think we should first understand the root cause and try making this change in a higher level.

I hope I helped,
Marcel

Ok, I see. I think you're right.
If needed, I will send an another patch using "object_property_get_bool()".

Thanks.

Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
hw/pci/pci.c +3 -1
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
--

1.9.1

#5 lore raw

When I check it the latest master branch, the issue is already resolved. It is resolved by commit 5942a19040fed313b316ab7b6e3d2d8e7b1625bb (28, June).
I'm sorry and thank you for your help.

Quoted 115 lines · SeokYeon Hwang
Quoted 10 lines
Quoted 10 lines · Michael S. Tsirkin
Quoted 3 lines · SeokYeon Hwang
Quoted 2 lines

In case of the unrealized "pdev", memory can be illegally accessed and

corrupted.

Quoted 1 line
Quoted 1 line

Refer to device_unparent() in the commit

5c21ce77d7e5643089ceec556c0408445d017f32.
Hi,
Thank you for submitting the patch.

Quoted 6 lines
Quoted 2 lines

Can you please send to the list how to reproduce the issue? Is this a regression? Before the above commit did it work?

Hi,

When VirtIO device is hot-unplugged, Qemu unrealizes the device first

then child_bus by device_unparent().

Quoted 3 lines

(After the commit 5c21ce77d7e5643089ceec556c0408445d017f32.)

For example, if I suppose to hot-unplug "virtio block" device, unparent

"virtio-blk-pci" first, then "virtio-blk".

Quoted 2 lines

virtio_bus_device_unplugged() function in virtio-bus.c calls

"msix_uninit()" with the parameter of its parent PCI device.

Quoted 1 line

The problem is that it accesses already freed member of pdev, such as

"pdev->config".

Quoted 1 line

This illegal access causes the heap corruption, and it kills Qemu

process suddenly.

Quoted 2 lines

You can detect this illegal access by using "electric fence" or

"valgrind".

Does this still trigger for you with latest master? I tried this:

hw/pci/pci.c +5 -0
@@ -791,6 +791,11 @@ static void pci_config_free(PCIDevice *pci_dev)
     g_free(pci_dev->wmask);
     g_free(pci_dev->w1cmask);
     g_free(pci_dev->used);
+    pci_dev->config = NULL;
+    pci_dev->cmask = NULL;
+    pci_dev->wmask = NULL;
+    pci_dev->w1cmask = NULL;
+    pci_dev->used = NULL;
 }
 
 static void do_pci_unregister_device(PCIDevice *pci_dev)

does not trigger crash after hot-unplug of virtio-net-pci on master.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Change-Id Iacb195a092c86d4c677ad0404582af104b2251ae

Another case of qemu mailing list dropping patches :( I will bounce it now so people can see the original message.

Quoted 4 lines
---
 hw/pci/pci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2058

Quoted 8 lines
hw/pci/pci.c index 49eca95..bb7f0c5 renamed from hw/pci/pci.c +0 -0

100644

hw/pci/pci.c +0 -0
@@ -2056,7 +2056,12 @@ int pci_add_capability(PCIDevice *pdev,
 uint8_t cap_id,
 /* Unlink capability from the pci config space. */  void
 pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)

{

Quoted 14 lines
Quoted 14 lines
Quoted 1 line

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2059

Quoted 1 line
hw/pci/pci.c +0 -1
-    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id,

&prev);

Quoted 3 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, · line 2061

Quoted 3 lines
hw/pci/pci.c +3 -0
+    uint8_t prev, offset;
+    /* Check whether the device is realized or not */
+    if (!pdev->qdev.realized) {

The 'qdev' field and 'realize' property are private, you should use one of QOM's 'property_get' methods.

I am also concerned about adding the realize check here, it seems too "deep" in implementation, seeing this checks everywhere doesn't seem a good idea.
I think we should first understand the root cause and try making this change in a higher level.

Quoted 5 lines
Quoted 2 lines

I hope I helped,
Marcel

Ok, I see. I think you're right.
If needed, I will send an another patch using

"object_property_get_bool()".

Quoted 17 lines

Thanks.

Quoted 13 lines
Quoted 12 lines
Quoted 11 lines
hw/pci/pci.c +3 -0
+        return;
+    }
+    offset = pci_find_capability_list(pdev, cap_id, &prev);
     if (!offset)
         return;
     pdev->config[prev] = pdev->config[offset +

PCI_CAP_LIST_NEXT];
--
1.9.1