qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
@ 2023-07-03 16:39 Avihai Horon
  2023-07-03 16:56 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 3+ messages in thread
From: Avihai Horon @ 2023-07-03 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Williamson, Cédric Le Goater, 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 unconditionally 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>
---
 hw/vfio/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index ab6645ba60..95e077082b 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -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);
         }
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
  2023-07-03 16:39 [PATCH] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
@ 2023-07-03 16:56 ` Philippe Mathieu-Daudé
  2023-07-04 10:51   ` Avihai Horon
  0 siblings, 1 reply; 3+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-03 16:56 UTC (permalink / raw)
  To: Avihai Horon, qemu-devel; +Cc: Alex Williamson, Cédric Le Goater

On 3/7/23 18: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 unconditionally 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>
> ---
>   hw/vfio/pci.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index ab6645ba60..95e077082b 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -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;
             }

?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] vfio: Fix null pointer dereference bug in vfio_bars_finalize()
  2023-07-03 16:56 ` Philippe Mathieu-Daudé
@ 2023-07-04 10:51   ` Avihai Horon
  0 siblings, 0 replies; 3+ messages in thread
From: Avihai Horon @ 2023-07-04 10:51 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Alex Williamson, Cédric Le Goater


On 03/07/2023 19:56, Philippe Mathieu-Daudé wrote:
> External email: Use caution opening links or attachments
>
>
> On 3/7/23 18: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 unconditionally 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>
>> ---
>>   hw/vfio/pci.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
>> index ab6645ba60..95e077082b 100644
>> --- a/hw/vfio/pci.c
>> +++ b/hw/vfio/pci.c
>> @@ -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;
>             }
>
> ?

Nice touch, it's indeed more accurate.

Will change and post v2.

Thanks!



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-04 10:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-03 16:39 [PATCH] vfio: Fix null pointer dereference bug in vfio_bars_finalize() Avihai Horon
2023-07-03 16:56 ` Philippe Mathieu-Daudé
2023-07-04 10:51   ` Avihai Horon

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).