]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iommufd: Test attach before detaching pasid
authorYi Liu <yi.l.liu@intel.com>
Fri, 28 Mar 2025 13:34:48 +0000 (06:34 -0700)
committerJason Gunthorpe <jgg@nvidia.com>
Fri, 28 Mar 2025 14:40:41 +0000 (11:40 -0300)
Check if the pasid has been attached before going further in the detach
path. This fixes a crash found by syzkaller. Add a selftest as well.

   Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASI
   KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
   CPU: 1 UID: 0 PID: 668 Comm: repro Not tainted 6.14.0-next-20250325-eb4bc4b07f66 #1 PREEMPT(voluntary)
   Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org4
   RIP: 0010:iommufd_hw_pagetable_detach+0x8a/0x4d0
   Code: 00 00 00 44 89 ee 48 89 c7 48 89 75 c8 48 89 45 c0 e8 ca 55 17 02 48 89 c2 49 89 c4 48 b8 00 00 00b
   RSP: 0018:ffff888021b17b78 EFLAGS: 00010246
   RAX: dffffc0000000000 RBX: ffff888014b5a000 RCX: ffff888021b17a64
   RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff88801dad07fc
   RBP: ffff888021b17bc8 R08: 0000000000000001 R09: 0000000000000001
   R10: 0000000000000001 R11: ffff88801dad0e58 R12: 0000000000000000
   R13: 0000000000000001 R14: ffff888021b17e18 R15: ffff8880132d3008
   FS:  00007fca52013600(0000) GS:ffff8880e3684000(0000) knlGS:0000000000000000
   CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
   CR2: 00000000200006c0 CR3: 00000000112d0005 CR4: 0000000000770ef0
   DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
   DR3: 0000000000000000 DR6: 00000000ffff07f0 DR7: 0000000000000400
   PKRU: 55555554
   Call Trace:
    <TASK>
    iommufd_device_detach+0x2a/0x2e0
    iommufd_test+0x2f99/0x5cd0
    iommufd_fops_ioctl+0x38e/0x520
    __x64_sys_ioctl+0x1ba/0x220
    x64_sys_call+0x122e/0x2150
    do_syscall_64+0x6d/0x150
    entry_SYSCALL_64_after_hwframe+0x76/0x7e

Link: https://patch.msgid.link/r/20250328133448.22052-1-yi.l.liu@intel.com
Reported-by: Lai Yi <yi1.lai@linux.intel.com>
Closes: https://lore.kernel.org/linux-iommu/Z+X0tzxhiaupJT7b@ly-workstation
Fixes: c0e301b2978d ("iommufd/device: Add pasid_attach array to track per-PASID attach")
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/iommu/iommufd/device.c
tools/testing/selftests/iommu/iommufd.c

index 2307daad65c0fac6e5a4e7267ac8752b7ca67db9..2111bad72c720f77a8c0b109064020c9867a6a70 100644 (file)
@@ -644,6 +644,11 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid)
 
        mutex_lock(&igroup->lock);
        attach = xa_load(&igroup->pasid_attach, pasid);
+       if (!attach) {
+               mutex_unlock(&igroup->lock);
+               return NULL;
+       }
+
        hwpt = attach->hwpt;
        hwpt_paging = find_hwpt_paging(hwpt);
 
@@ -1001,6 +1006,8 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)
        struct iommufd_hw_pagetable *hwpt;
 
        hwpt = iommufd_hw_pagetable_detach(idev, pasid);
+       if (!hwpt)
+               return;
        iommufd_hw_pagetable_put(idev->ictx, hwpt);
        refcount_dec(&idev->obj.users);
 }
index 7eb7ee149f2bc4b4500a4a79094fe5c6a1f46dae..1a8e85afe9aa51f9519172f17f7c3e9b758b30f5 100644 (file)
@@ -3074,6 +3074,12 @@ TEST_F(iommufd_device_pasid, pasid_attach)
        uint32_t pasid = 100;
        uint32_t viommu_id;
 
+       /*
+        * Negative, detach pasid without attaching, this is not expected.
+        * But it should not result in failure anyway.
+        */
+       test_cmd_pasid_detach(pasid);
+
        /* Allocate two nested hwpts sharing one common parent hwpt */
        test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
                            IOMMU_HWPT_ALLOC_NEST_PARENT,