All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free
@ 2022-09-15 17:18 Alex Williamson
  2022-09-17 21:37 ` Philippe Mathieu-Daudé via
  2022-09-19 20:50 ` Nicolin Chen
  0 siblings, 2 replies; 3+ messages in thread
From: Alex Williamson @ 2022-09-15 17:18 UTC (permalink / raw)
  To: qemu-devel; +Cc: alex.williamson, nicolinc, cohuck

On error, vfio_get_iommu_info() frees and clears *info, but
vfio_connect_container() continues to use the pointer regardless
of the return value.  Restructure the code such that a failure
of this function triggers an error and clean up the remainder of
the function, including updating an outdated comment that had
drifted from its relevant line of code and using host page size
for a default for better compatibility on non-4KB systems.

Reported-by: Nicolin Chen <nicolinc@nvidia.com>
Link: https://lore.kernel.org/all/20220910004245.2878-1-nicolinc@nvidia.com/
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 hw/vfio/common.c |   36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

Turns out I had the errno vs ret correct the first time from the
thread in the above Link, vfio_get_iommu_info() returns -errno.

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index ace9562a9ba1..6b5d8c0bf694 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -2111,29 +2111,31 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
     {
         struct vfio_iommu_type1_info *info;
 
-        /*
-         * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
-         * IOVA whatsoever.  That's not actually true, but the current
-         * kernel interface doesn't tell us what it can map, and the
-         * existing Type1 IOMMUs generally support any IOVA we're
-         * going to actually try in practice.
-         */
         ret = vfio_get_iommu_info(container, &info);
+        if (ret) {
+            error_setg_errno(errp, -ret, "Failed to get VFIO IOMMU info");
+            goto enable_discards_exit;
+        }
 
-        if (ret || !(info->flags & VFIO_IOMMU_INFO_PGSIZES)) {
-            /* Assume 4k IOVA page size */
-            info->iova_pgsizes = 4096;
+        if (info->flags & VFIO_IOMMU_INFO_PGSIZES) {
+            container->pgsizes = info->iova_pgsizes;
+        } else {
+            container->pgsizes = qemu_real_host_page_size();
         }
-        vfio_host_win_add(container, 0, (hwaddr)-1, info->iova_pgsizes);
-        container->pgsizes = info->iova_pgsizes;
 
-        /* The default in the kernel ("dma_entry_limit") is 65535. */
-        container->dma_max_mappings = 65535;
-        if (!ret) {
-            vfio_get_info_dma_avail(info, &container->dma_max_mappings);
-            vfio_get_iommu_info_migration(container, info);
+        if (!vfio_get_info_dma_avail(info, &container->dma_max_mappings)) {
+            container->dma_max_mappings = 65535;
         }
+        vfio_get_iommu_info_migration(container, info);
         g_free(info);
+
+        /*
+         * FIXME: We should parse VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE
+         * information to get the actual window extent rather than assume
+         * a 64-bit IOVA address space.
+         */
+        vfio_host_win_add(container, 0, (hwaddr)-1, container->pgsizes);
+
         break;
     }
     case VFIO_SPAPR_TCE_v2_IOMMU:




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

* Re: [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free
  2022-09-15 17:18 [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free Alex Williamson
@ 2022-09-17 21:37 ` Philippe Mathieu-Daudé via
  2022-09-19 20:50 ` Nicolin Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Philippe Mathieu-Daudé via @ 2022-09-17 21:37 UTC (permalink / raw)
  To: Alex Williamson, qemu-devel
  Cc: nicolinc, cohuck, Alberto Faria, Stefan Hajnoczi

On 15/9/22 19:18, Alex Williamson wrote:
> On error, vfio_get_iommu_info() frees and clears *info, but
> vfio_connect_container() continues to use the pointer regardless
> of the return value.  Restructure the code such that a failure
> of this function triggers an error and clean up the remainder of
> the function, including updating an outdated comment that had
> drifted from its relevant line of code and using host page size
> for a default for better compatibility on non-4KB systems.
> 
> Reported-by: Nicolin Chen <nicolinc@nvidia.com>
> Link: https://lore.kernel.org/all/20220910004245.2878-1-nicolinc@nvidia.com/
> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
> ---
>   hw/vfio/common.c |   36 +++++++++++++++++++-----------------
>   1 file changed, 19 insertions(+), 17 deletions(-)
> 
> Turns out I had the errno vs ret correct the first time from the
> thread in the above Link, vfio_get_iommu_info() returns -errno.
> 
> diff --git a/hw/vfio/common.c b/hw/vfio/common.c
> index ace9562a9ba1..6b5d8c0bf694 100644
> --- a/hw/vfio/common.c
> +++ b/hw/vfio/common.c
> @@ -2111,29 +2111,31 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
>       {
>           struct vfio_iommu_type1_info *info;
>   
> -        /*
> -         * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
> -         * IOVA whatsoever.  That's not actually true, but the current
> -         * kernel interface doesn't tell us what it can map, and the
> -         * existing Type1 IOMMUs generally support any IOVA we're
> -         * going to actually try in practice.
> -         */
>           ret = vfio_get_iommu_info(container, &info);
> +        if (ret) {
> +            error_setg_errno(errp, -ret, "Failed to get VFIO IOMMU info");
> +            goto enable_discards_exit;
> +        }
>   
> -        if (ret || !(info->flags & VFIO_IOMMU_INFO_PGSIZES)) {
> -            /* Assume 4k IOVA page size */
> -            info->iova_pgsizes = 4096;
> +        if (info->flags & VFIO_IOMMU_INFO_PGSIZES) {
> +            container->pgsizes = info->iova_pgsizes;
> +        } else {
> +            container->pgsizes = qemu_real_host_page_size();
>           }
> -        vfio_host_win_add(container, 0, (hwaddr)-1, info->iova_pgsizes);
> -        container->pgsizes = info->iova_pgsizes;
>   
> -        /* The default in the kernel ("dma_entry_limit") is 65535. */
> -        container->dma_max_mappings = 65535;
> -        if (!ret) {
> -            vfio_get_info_dma_avail(info, &container->dma_max_mappings);
> -            vfio_get_iommu_info_migration(container, info);
> +        if (!vfio_get_info_dma_avail(info, &container->dma_max_mappings)) {
> +            container->dma_max_mappings = 65535;
>           }
> +        vfio_get_iommu_info_migration(container, info);
>           g_free(info);
> +
> +        /*
> +         * FIXME: We should parse VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE
> +         * information to get the actual window extent rather than assume
> +         * a 64-bit IOVA address space.
> +         */
> +        vfio_host_win_add(container, 0, (hwaddr)-1, container->pgsizes);
> +
>           break;
>       }
>       case VFIO_SPAPR_TCE_v2_IOMMU:
> 

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>




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

* Re: [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free
  2022-09-15 17:18 [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free Alex Williamson
  2022-09-17 21:37 ` Philippe Mathieu-Daudé via
@ 2022-09-19 20:50 ` Nicolin Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Nicolin Chen @ 2022-09-19 20:50 UTC (permalink / raw)
  To: Alex Williamson; +Cc: qemu-devel, cohuck

On Thu, Sep 15, 2022 at 11:18:27AM -0600, Alex Williamson wrote:
> External email: Use caution opening links or attachments
> 
> 
> On error, vfio_get_iommu_info() frees and clears *info, but
> vfio_connect_container() continues to use the pointer regardless
> of the return value.  Restructure the code such that a failure
> of this function triggers an error and clean up the remainder of
> the function, including updating an outdated comment that had
> drifted from its relevant line of code and using host page size
> for a default for better compatibility on non-4KB systems.
> 
> Reported-by: Nicolin Chen <nicolinc@nvidia.com>
> Link: https://lore.kernel.org/all/20220910004245.2878-1-nicolinc@nvidia.com/
> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>

Thanks!


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

end of thread, other threads:[~2022-09-19 20:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-15 17:18 [PATCH] vfio/common: Fix vfio_iommu_type1_info use after free Alex Williamson
2022-09-17 21:37 ` Philippe Mathieu-Daudé via
2022-09-19 20:50 ` Nicolin Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.