Skip to contentSkip to conversation

iommufd: reject mmap() of not-yet-finalized objects with iommufd_try_inc_users()

PATCH Superseded YanXin Li 1 participant 1 message Nov 22

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

YanXin Li

author
#0 lore raw

PATCH 1/1 iommufd: reject mmap() of not-yet-finalized objects with iommufd_try_inc_users()

iommufd_fops_mmap() takes a users reference on the object that owns the mmapped region with refcount_inc_not_zero(&immap->owner->users). The reference is meant to pin the owner across the lifetime of the VMA, but refcount_inc_not_zero() cannot tell an already-created object from an object that is still being constructed.

Objects with an MMIO region are published in ictx->mt_mmap by their creation ioctl before they are finalized: IOMMU_VIOMMU_ALLOC inserts the immap in mock_viommu_init() (or in a driver's viommu_init()) through iommufd_viommu_alloc_mmap(), and at that point the viommu is reserved as XA_ZERO_ENTRY in ictx->objects and obj->users is still the initial 1. A concurrent mmap() on the same /dev/iommu fd therefore succeeds in taking a users reference on the half-constructed object, because refcount_inc_not_zero() only fails once the refcount has dropped to zero. When the ioctl later fails - for IOMMU_VIOMMU_ALLOC this is reachable by simply placing the ioctl
command buffer on a
read-only page so that iommufd_ucmd_respond() faults - the error path runs iommufd_object_abort_and_destroy(), which calls the driver destroy op that erases the immap from mt_mmap and kfree()s it, and then iommufd_object_abort() WARNs because obj->users never returns to zero. The next munmap() (or fork/exit) reaches iommufd_fops_vma_close(), which dereferences the freed immap. On v7.4-rc3 with CONFIG_IOMMUFD_TEST, racing a loop of mmap()/munmap() against aborted IOMMU_VIOMMU_ALLOC yields:

  WARNING: drivers/iommu/iommufd/main.c:126 at iommufd_object_abort+0x11d/0x1a0
  BUG: KASAN: slab-use-after-free in iommufd_fops_vma_close+0x87/0x90
  Read of size 8 at addr ffff888010f4a5c0 by task test_iommufd_mm
  ...
  Oops: 0002 [#1] SMP KASAN

The reproducer uses CONFIG_IOMMUFD_TEST and needs no hardware IOMMU. On production kernels, the bug requires a driver that publishes an mmap region before its creation ioctl can still fail, such as tegra241-cmdqv o
n real hardware.

The kernel already has the correct helper for this invariant: main.c documents that "the users refcount cannot be increased on an already created object unless the object is installed in the xarray, otherwise things are racing with a parallel destruction", and iommufd_try_inc_users() implements exactly that check under the xarray lock. Use it in iommufd_fops_mmap() instead of refcount_inc_not_zero(). Before finalize, xa_load() returns XA_ZERO_ENTRY instead of the object, the helper returns -EBUSY, and the mmap() is rejected with -ENXIO while the ioctl is still constructing the object; after finalize it behaves like the open-coded version. The mmap path now nests the objects xarray lock inside the mt_mmap lock. Object teardown releases the xarray lock before the driver destroy op erases the mmap entry, so it does not acquire these locks in reverse order.

With this change a successful (finalized) viommu can still be mmap()ed and unmapped normally,
while 3000 aborted allocations racing two
mmap()/munmap() loops produced zero kernel splats, versus the use- after-free above on the unmodified kernel. A test case and full validation logs are available on request.

The vulnerable code is present in mainline commit c44104f3642d (post-v7.4-rc3). Runtime verification and fix validation were performed in an x86-64 QEMU/KVM guest built from that baseline with Generic KASAN, UBSAN, lockdep, and DEBUG_OBJECTS.

Fixes 56e9a0d8e53f ("iommufd: Add mmap interface")
---
 drivers/iommu/iommufd/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iommufd/main.c +1 -1
index 9a921b15316..f0b4308fe34 100644
@@ -601,7 +601,7 @@ static int iommufd_fops_mmap(struct file *filp, struct vm_area_struct *vma)
 	mtree_lock(&ictx->mt_mmap);
 
 	/* vma->vm_pgoff carries a page-shifted start position to an immap */
 	immap = mtree_load(&ictx->mt_mmap, vma->vm_pgoff << PAGE_SHIFT);
-	if (!immap || !refcount_inc_not_zero(&immap->owner->users)) {
+	if (!immap || iommufd_try_inc_users(ictx, immap->owner)) {
 		mtree_unlock(&ictx->mt_mmap);
 		return -ENXIO;

}

Signature · 1 line
2.43.0