[PATCH v2] iommufd: reject mmap() of not-yet-finalized objects with iommufd_try_inc_users()

From: YanXin Li via B4 Relay

Date: Sun Nov 22 2026 - 07:47:28 EST


From: YanXin Li <fadouse@xxxxx>

iommufd_fops_mmap() pins the owner of an mmap region with
refcount_inc_not_zero(&immap->owner->users). This can also pin an
object still being constructed: IOMMU_VIOMMU_ALLOC publishes its
immap in ictx->mt_mmap before the object is finalized, while
ictx->objects still holds XA_ZERO_ENTRY and obj->users is 1.

A concurrent mmap() can take the reference. If the creation ioctl
then fails, for example when iommufd_ucmd_respond() faults on a
read-only command buffer, iommufd_object_abort_and_destroy() erases
and frees the immap. iommufd_object_abort() warns about the extra
users reference; a later VMA close dereferences the freed immap. On
the unpatched kernel, a CONFIG_IOMMUFD_TEST reproducer reported:

WARNING: drivers/iommu/iommufd/main.c:126 at iommufd_object_abort
BUG: KASAN: slab-use-after-free in iommufd_fops_vma_close

The reproducer needs no hardware IOMMU. On production kernels, this
requires a driver that publishes an mmap region before its creation
ioctl can still fail, such as tegra241-cmdqv on real hardware.

Use iommufd_try_inc_users() to check under the objects xarray lock
that the owner is published before increasing users. It returns
-EBUSY for an unfinalized object, so mmap() returns -ENXIO. The mmap
path takes the objects lock under the mt_mmap lock; object teardown
releases the objects lock before erasing the mmap entry.

With this change, a finalized viommu can still be mmap()ed and
unmapped. In an x86-64 QEMU/KVM guest running mainline after
v7.4-rc3 with Generic KASAN, UBSAN, lockdep, and DEBUG_OBJECTS, 3000
aborted allocations racing two mmap()/munmap() loops produced no
kernel splats.

Fixes: 56e9a0d8e53f ("iommufd: Add mmap interface")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: YanXin Li <fadouse@xxxxx>
---
Changes in v2:
- Resend via b4 because the archived v1 email gained a blank line in
the diff context and cannot be applied.
- Shorten the commit message and correct the tested kernel version wording.
- Link to v1: https://lore.kernel.org/r/20261122032416.11746-1-fadouse@xxxxx

Test baseline: c44104f3642d
---
drivers/iommu/iommufd/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 9a921b15316..f0b4308fe34 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -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;
}

---
base-commit: c44104f3642da720164042623e4832836aefdd8e
change-id: 20261122-b4-04-iommufd-mmap-uaf-v2-a67fd4229347-adb0d1f511c0

Best regards,
--
YanXin Li <fadouse@xxxxx>