* [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
@ 2023-07-04 13:39 Avihai Horon
2023-07-04 14:07 ` Philippe Mathieu-Daudé
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Avihai Horon @ 2023-07-04 13:39 UTC (permalink / raw)
To: qemu-devel
Cc: Alex Williamson, Cédric Le Goater,
Philippe Mathieu-Daudé, 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")
Signed-off-by: Avihai Horon <avihaih@nvidia.com>
---
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(-)
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index ab6645ba60..bc98791cbb 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -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;
}
}
--
2.26.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
2023-07-04 13:39 [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
@ 2023-07-04 14:07 ` Philippe Mathieu-Daudé
2023-07-04 15:54 ` Cédric Le Goater
2023-07-06 20:31 ` Alex Williamson
2 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-04 14:07 UTC (permalink / raw)
To: Avihai Horon, qemu-devel; +Cc: Alex Williamson, Cédric Le Goater
On 4/7/23 15:39, Avihai Horon 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")
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
> ---
>
> 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: Philippe Mathieu-Daudé <philmd@linaro.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
2023-07-04 13:39 [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
2023-07-04 14:07 ` Philippe Mathieu-Daudé
@ 2023-07-04 15:54 ` Cédric Le Goater
2023-07-05 6:16 ` Avihai Horon
2023-07-06 20:31 ` Alex Williamson
2 siblings, 1 reply; 5+ messages in thread
From: Cédric Le Goater @ 2023-07-04 15:54 UTC (permalink / raw)
To: Avihai Horon, qemu-devel; +Cc: Alex Williamson, Philippe Mathieu-Daudé
Hello Avihai
On 7/4/23 15:39, Avihai Horon 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().
Did you see the issue by reading the code or did you actually crash
QEMU with a test case ?
>
> Fixes: 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Thanks,
C.
> ---
>
> 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(-)
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index ab6645ba60..bc98791cbb 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -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;
> }
> }
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
2023-07-04 15:54 ` Cédric Le Goater
@ 2023-07-05 6:16 ` Avihai Horon
0 siblings, 0 replies; 5+ messages in thread
From: Avihai Horon @ 2023-07-05 6:16 UTC (permalink / raw)
To: Cédric Le Goater, qemu-devel
Cc: Alex Williamson, Philippe Mathieu-Daudé
On 04/07/2023 18:54, Cédric Le Goater wrote:
> External email: Use caution opening links or attachments
>
>
> Hello Avihai
>
> On 7/4/23 15:39, Avihai Horon 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().
>
> 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).
>
>>
>> Fixes: 89d5202edc50 ("vfio/pci: Allow relocating MSI-X MMIO")
>> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
>
>
> Reviewed-by: Cédric Le Goater <clg@redhat.com>
>
> Thanks,
>
> C.
>
>
>> ---
>>
>> 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(-)
>>
>> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
>> index ab6645ba60..bc98791cbb 100644
>> --- a/hw/vfio/pci.c
>> +++ b/hw/vfio/pci.c
>> @@ -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;
>> }
>> }
>>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
2023-07-04 13:39 [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
2023-07-04 14:07 ` Philippe Mathieu-Daudé
2023-07-04 15:54 ` Cédric Le Goater
@ 2023-07-06 20:31 ` Alex Williamson
2 siblings, 0 replies; 5+ messages in thread
From: Alex Williamson @ 2023-07-06 20:31 UTC (permalink / raw)
To: Avihai Horon
Cc: qemu-devel, Cédric Le Goater, Philippe Mathieu-Daudé
On Tue, 4 Jul 2023 16:39:27 +0300
Avihai Horon <avihaih@nvidia.com> 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")
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
> ---
>
> 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(-)
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index ab6645ba60..bc98791cbb 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -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: Alex Williamson <alex.williamson@redhat.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-07-06 20:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-04 13:39 [PATCH v2] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
2023-07-04 14:07 ` Philippe Mathieu-Daudé
2023-07-04 15:54 ` Cédric Le Goater
2023-07-05 6:16 ` Avihai Horon
2023-07-06 20:31 ` Alex Williamson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).