Skip to content

Commit 7be11d3

Browse files
yiliu1765jgunthorpe
authored andcommitted
iommufd: Test attach before detaching pasid
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: c0e301b ("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>
1 parent 858c9c1 commit 7be11d3

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,11 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid)
644644

645645
mutex_lock(&igroup->lock);
646646
attach = xa_load(&igroup->pasid_attach, pasid);
647+
if (!attach) {
648+
mutex_unlock(&igroup->lock);
649+
return NULL;
650+
}
651+
647652
hwpt = attach->hwpt;
648653
hwpt_paging = find_hwpt_paging(hwpt);
649654

@@ -1001,6 +1006,8 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)
10011006
struct iommufd_hw_pagetable *hwpt;
10021007

10031008
hwpt = iommufd_hw_pagetable_detach(idev, pasid);
1009+
if (!hwpt)
1010+
return;
10041011
iommufd_hw_pagetable_put(idev->ictx, hwpt);
10051012
refcount_dec(&idev->obj.users);
10061013
}

tools/testing/selftests/iommu/iommufd.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,12 @@ TEST_F(iommufd_device_pasid, pasid_attach)
30743074
uint32_t pasid = 100;
30753075
uint32_t viommu_id;
30763076

3077+
/*
3078+
* Negative, detach pasid without attaching, this is not expected.
3079+
* But it should not result in failure anyway.
3080+
*/
3081+
test_cmd_pasid_detach(pasid);
3082+
30773083
/* Allocate two nested hwpts sharing one common parent hwpt */
30783084
test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
30793085
IOMMU_HWPT_ALLOC_NEST_PARENT,

0 commit comments

Comments
 (0)