Skip to contentSkip to conversation

vfio: Fix null pointer dereference bug in vfio_bars_finalize()

PATCH In review Avihai Horon 4 participants 5 messages Jul 4, 2023 – Jul 6, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vfio: Fix null pointer dereference bug in vfio_bars_finalize()

vfio_realize() has the following flow:
1. vfio_bars_prepare() -- sets VFIOBAR->size.
2. msix_early_setup().
3. vfio_bars_register() -- allocates VFIOBAR->mr.

After vfio_bars_prepare() is called msix_early_setup() can fail. If it does fail, vfio_bars_register() is never called and VFIOBAR->mr is not allocated.

In this case, vfio_bars_finalize() is called as part of the error flow to free the bars' resources. However, vfio_bars_finalize() calls object_unparent() for VFIOBAR->mr after checking only VFIOBAR->size, and thus we get a null pointer dereference.

Fix it by checking VFIOBAR->mr in vfio_bars_finalize().

Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")

---

Changes from v1:
* Assert VFIOBAR->size and set VFIOBAR->mr to NULL to make the code more accurate. (Philippe)
* Small reword in the last paragraph of the commit message.

 hw/vfio/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/pci.c +3 -1
index ab6645ba60..bc98791cbb 100644
@@ -1752,9 +1752,11 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)
 
         vfio_bar_quirk_finalize(vdev, i);
         vfio_region_finalize(&bar->region);
-        if (bar->size) {
+        if (bar->mr) {
+            assert(bar->size);
             object_unparent(OBJECT(bar->mr));
             g_free(bar->mr);
+            bar->mr = NULL;
         }
     }
 
Signature · 1 line
2.26.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 20 lines · Avihai Horon

vfio_realize() has the following flow:
1. vfio_bars_prepare() -- sets VFIOBAR->size.
2. msix_early_setup().
3. vfio_bars_register() -- allocates VFIOBAR->mr.

After vfio_bars_prepare() is called msix_early_setup() can fail. If it does fail, vfio_bars_register() is never called and VFIOBAR->mr is not allocated.

In this case, vfio_bars_finalize() is called as part of the error flow to free the bars' resources. However, vfio_bars_finalize() calls object_unparent() for VFIOBAR->mr after checking only VFIOBAR->size, and thus we get a null pointer dereference.

Fix it by checking VFIOBAR->mr in vfio_bars_finalize().

Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")

---

Quoted 7 lines

Changes from v1:
* Assert VFIOBAR->size and set VFIOBAR->mr to NULL to make the code more accurate. (Philippe)
* Small reword in the last paragraph of the commit message.

  hw/vfio/pci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by on v2 1/1
#2 lore raw
In reply to Avihai Horon

Hello Avihai

Quoted 9 lines · Avihai Horon

vfio_realize() has the following flow:
1. vfio_bars_prepare() -- sets VFIOBAR->size.
2. msix_early_setup().
3. vfio_bars_register() -- allocates VFIOBAR->mr.

After vfio_bars_prepare() is called msix_early_setup() can fail. If it does fail, vfio_bars_register() is never called and VFIOBAR->mr is not allocated.

Quoted 6 lines

In this case, vfio_bars_finalize() is called as part of the error flow to free the bars' resources. However, vfio_bars_finalize() calls object_unparent() for VFIOBAR->mr after checking only VFIOBAR->size, and thus we get a null pointer dereference.

Fix it by checking VFIOBAR->mr in vfio_bars_finalize().

Did you see the issue by reading the code or did you actually crash QEMU with a test case ?

Quoted 3 lines
Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")

Thanks,

C.

Quoted 27 lines

---

Changes from v1:
* Assert VFIOBAR->size and set VFIOBAR->mr to NULL to make the code more accurate. (Philippe)
* Small reword in the last paragraph of the commit message.

  hw/vfio/pci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/pci.c +3 -1
index ab6645ba60..bc98791cbb 100644
@@ -1752,9 +1752,11 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)
 
          vfio_bar_quirk_finalize(vdev, i);
          vfio_region_finalize(&bar->region);
-        if (bar->size) {
+        if (bar->mr) {
+            assert(bar->size);
              object_unparent(OBJECT(bar->mr));
              g_free(bar->mr);
+            bar->mr = NULL;
          }
      }
 
#3 lore raw
Quoted 20 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello Avihai

Quoted 14 lines · Avihai Horon

vfio_realize() has the following flow:
1. vfio_bars_prepare() -- sets VFIOBAR->size.
2. msix_early_setup().
3. vfio_bars_register() -- allocates VFIOBAR->mr.

After vfio_bars_prepare() is called msix_early_setup() can fail. If it does fail, vfio_bars_register() is never called and VFIOBAR->mr is not allocated.

In this case, vfio_bars_finalize() is called as part of the error flow to free the bars' resources. However, vfio_bars_finalize() calls object_unparent() for VFIOBAR->mr after checking only VFIOBAR->size, and thus we get a null pointer dereference.

Quoted 4 lines
Quoted 1 line

Fix it by checking VFIOBAR->mr in vfio_bars_finalize().

Did you see the issue by reading the code or did you actually crash QEMU with a test case ?

I actually got a segmentation fault after msix_early_setup() failed (due to some other misconfiguration from my side).

Quoted 42 lines
Quoted 4 lines
Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")

Thanks,

C.

Quoted 28 lines

---

  Changes from v1:
  * Assert VFIOBAR->size and set VFIOBAR->mr to NULL to make the code more accurate. (Philippe)
  * Small reword in the last paragraph of the commit message.

  hw/vfio/pci.c | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/pci.c +3 -1
index ab6645ba60..bc98791cbb 100644
@@ -1752,9 +1752,11 @@ static void vfio_bars_finalize(VFIOPCIDevice 
 *vdev)
 
           vfio_bar_quirk_finalize(vdev, i);
           vfio_region_finalize(&bar->region);
-        if (bar->size) {
+        if (bar->mr) {
+            assert(bar->size);
               object_unparent(OBJECT(bar->mr));
               g_free(bar->mr);
+            bar->mr = NULL;
           }
       }
Reviewed-by on v2 1/1
#4 lore raw
In reply to Avihai Horon
Quoted 35 lines · On Tue, 4 Jul 2023 16:39:27 +0300 Avihai Horon <[email protected]> wrote:

vfio_realize() has the following flow:
1. vfio_bars_prepare() -- sets VFIOBAR->size.
2. msix_early_setup().
3. vfio_bars_register() -- allocates VFIOBAR->mr.

After vfio_bars_prepare() is called msix_early_setup() can fail. If it does fail, vfio_bars_register() is never called and VFIOBAR->mr is not allocated.

In this case, vfio_bars_finalize() is called as part of the error flow to free the bars' resources. However, vfio_bars_finalize() calls object_unparent() for VFIOBAR->mr after checking only VFIOBAR->size, and thus we get a null pointer dereference.

Fix it by checking VFIOBAR->mr in vfio_bars_finalize().

Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")

---

Changes from v1:
* Assert VFIOBAR->size and set VFIOBAR->mr to NULL to make the code more accurate. (Philippe)
* Small reword in the last paragraph of the commit message.

 hw/vfio/pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/vfio/pci.c +0 -0
index ab6645ba60..bc98791cbb 100644
@@ -1752,9 +1752,11 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)
 
         vfio_bar_quirk_finalize(vdev, i);

hw/vfio/pci.c static void vfio_bars_finalize(VFIOPCIDevice *vdev) · line 1762

Quoted 10 lines
hw/vfio/pci.c +3 -1
         vfio_region_finalize(&bar->region);
-        if (bar->size) {
+        if (bar->mr) {
+            assert(bar->size);
             object_unparent(OBJECT(bar->mr));
             g_free(bar->mr);
+            bar->mr = NULL;
         }
     }