vfio: Fix null pointer dereference bug in vfio_bars_finalize()
PATCH 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 unconditionally and thus we get a null pointer dereference.
Fix it by checking VFIOBAR->mr in vfio_bars_finalize().
--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/pci.c +1 -1
@@ -1752,7 +1752,7 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)vfio_bar_quirk_finalize (vdev, i);vfio_region_finalize (&bar->region); -if (bar->size) { +if (bar->size && bar->mr) {object_unparent (OBJECT (bar->mr));g_free (bar->mr); }
Signature · 1 line
2.26.3
Quoted 27 lines · Avihai Horon
vfio_realize()has the following flow:
1.vfio_bars_prepare()-- setsVFIOBAR->size.
2.msix_early_setup().
3.vfio_bars_register()-- allocatesVFIOBAR->mr.After
vfio_bars_prepare()is calledmsix_early_setup()can fail. If it does fail,vfio_bars_register()is never called andVFIOBAR->mris 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()callsobject_unparent()forVFIOBAR->mrunconditionally and thus we get a null pointer dereference.Fix it by checking
VFIOBAR->mrinvfio_bars_finalize().Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")Signed-off-by Avihai Horon <[email protected]>--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/pci.c +0
-0
hw/vfio/pci.c static void vfio_bars_finalize(VFIOPCIDevice *vdev) · line 1758
Quoted 8 lines
hw/vfio/pci.c +1
-1@@ -1752,7 +1752,7 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)vfio_bar_quirk_finalize (vdev, i);vfio_region_finalize (&bar->region); -if (bar->size) { +if (bar->size && bar->mr) {object_unparent (OBJECT (bar->mr));g_free (bar->mr); }
What about:
if (bar->mr) {
assert(bar->size);
object_unparent(OBJECT(bar->mr));
g_free(bar->mr);
bar->mr = NULL;
}?
Quoted 43 lines · Philippe Mathieu-Daudé
External email: Use caution opening links or attachments
Quoted 35 lines · Avihai Horon
vfio_realize()has the following flow:
1.vfio_bars_prepare()-- setsVFIOBAR->size.
2.msix_early_setup().
3.vfio_bars_register()-- allocatesVFIOBAR->mr.After
vfio_bars_prepare()is calledmsix_early_setup()can fail. If it does fail,vfio_bars_register()is never called andVFIOBAR->mris 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()callsobject_unparent()forVFIOBAR->mrunconditionally and thus we get a null pointer dereference.Fix it by checking
VFIOBAR->mrinvfio_bars_finalize().Fixes 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")Signed-off-by Avihai Horon <[email protected]>---
hw/vfio/pci.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/pci.c +1
-1@@ -1752,7 +1752,7 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev) vfio_bar_quirk_finalize(vdev, i); vfio_region_finalize(&bar->region); - if (bar->size) { + if (bar->size && bar->mr) { object_unparent(OBJECT(bar->mr)); g_free(bar->mr);Quoted 1 line
}
What about:
Quoted 8 lines
if (
bar->mr) {
assert(bar->size);
object_unparent(OBJECT(bar->mr));
g_free(bar->mr);
bar->mr= NULL;
}?
Nice touch, it's indeed more accurate.
Will change and post v2.
Thanks!