vfio: fix partial unmap
authorAnatoly Burakov <anatoly.burakov@intel.com>
Tue, 26 Oct 2021 13:26:44 +0000 (13:26 +0000)
committerDavid Marchand <david.marchand@redhat.com>
Thu, 28 Oct 2021 07:51:55 +0000 (09:51 +0200)
Partial unmap support was introduced in commit c13ca4e81cac
("vfio: fix DMA mapping granularity for IOVA as VA"), and with it
was added a check that dereferenced the IOMMU type to determine whether
partial ummapping is supported for currently configured IOMMU type. In
certain circumstances (such as when VFIO is supported, but no devices
were bound to the VFIO driver), the IOMMU type pointer can be NULL.

However, dereferencing of IOMMU type was guarded by access to the user
maps list - that is, we were always checking the user map list first,
and then, if we found a memory region that encloses the one we're trying
to unmap, we would have performed the IOMMU type check.

This ensured that the IOMMU type check will not cause any NULL pointer
dereferences, because in order for an IOMMU type check to have been
performed, there necessarily must have been at least one memory region
that was previously mapped successfully, and that implies having a
defined IOMMU type.

When commit 56259f7fc010 ("vfio: allow partially unmapping adjacent
memory") was introduced, the IOMMU type check was moved to
before we were traversing the user mem maps list, thereby introducing a
potential NULL dereference, because the IOMMU type access was no longer
guarded by the user mem maps list traversal.

Fix the issue by moving the IOMMU type check to after the user mem maps
traversal, thereby ensuring that by the time the check happens, the
IOMMU type is always valid.

Fixes: 56259f7fc010 ("vfio: allow partially unmapping adjacent memory")

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
Reviewed-by: David Marchand <david.marchand@redhat.com>
Tested-by: Xuan Ding <xuan.ding@intel.com>
lib/eal/linux/eal_vfio.c

index 657c89c..aa2087a 100644 (file)
@@ -1943,9 +1943,6 @@ container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
         * mappings, let's just rebuild them using information we have.
         */
 
-       /* do we have partial unmap capability? */
-       has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
-
        /*
         * first thing to do is check if there exists a mapping that includes
         * the start and the end of our requested unmap. We need to collect all
@@ -1961,6 +1958,9 @@ container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
                goto out;
        }
 
+       /* do we have partial unmap capability? */
+       has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
+
        /*
         * if we don't support partial unmap, we must check if start and end of
         * current unmap region are chunk-aligned.