iommufd_test_pasid_check_hwpt() calls iommu_attach_handle_get() and dereferences handle->domain without synchronizing against concurrent PASID detach or replace. Since iommu_attach_handle_get() only holds xa_lock during the lookup and takes no reference on the handle, a concurrent IOMMU_TEST_OP_PASID_DETACH or IOMMU_TEST_OP_PASID_REPLACE can erase the entry and free struct iommufd_attach_handle before handle->domain is read, causing a UAF. Hold idev->igroup->lock across iommu_attach_handle_get() and the handle->domain dereference, matching the locking used by iommufd_device_get_attach_handle(). Also pass idev->igroup->group directly to iommu_attach_handle_get(). Fixes: c1b52b0a97ae ("iommufd/selftest: Add test ops to test pasid attach/detach") Cc: stable@vger.kernel.org Signed-off-by: Peiyang He --- Found by our research tool based on Syzkaller when fuzzing Linux 7.1; we manually write the PoC, confirm this bug exists in the latest Linux and the fix is valid: BUG: KASAN: slab-use-after-free in iommufd_test_pasid_check_hwpt drivers/iommu/iommufd/selftest.c:1830 [inline] BUG: KASAN: slab-use-after-free in iommufd_test+0x1e17/0x5710 drivers/iommu/iommufd/selftest.c:2138 Read of size 8 at addr ffff888047f98f60 by task syz.1.250/11772 CPU: 1 UID: 0 PID: 11772 Comm: syz.1.250 Not tainted 7.1.0 #14 PREEMPT(full) Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description+0x55/0x1e0 mm/kasan/report.c:378 print_report+0x58/0x70 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 iommufd_test_pasid_check_hwpt drivers/iommu/iommufd/selftest.c:1830 [inline] iommufd_test+0x1e17/0x5710 drivers/iommu/iommufd/selftest.c:2138 iommufd_fops_ioctl+0x469/0x600 drivers/iommu/iommufd/main.c:533 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl+0x101/0x170 fs/ioctl.c:583 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0x15f/0x560 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fee771a710d Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fee78138018 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00007fee77435fa0 RCX: 00007fee771a710d RDX: 00002000000004c0 RSI: 0000000000003ba0 RDI: 0000000000000003 RBP: 00007fee7724ec67 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fee77436038 R14: 00007fee77435fa0 R15: 00007ffe5695f2c0 Allocated by task 11772: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 poison_kmalloc_redzone mm/kasan/common.c:398 [inline] __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415 kasan_kmalloc include/linux/kasan.h:263 [inline] __kmalloc_cache_noprof+0x33b/0x680 mm/slub.c:5420 kmalloc_noprof include/linux/slab.h:950 [inline] kzalloc_noprof include/linux/slab.h:1188 [inline] iommufd_hwpt_attach_device drivers/iommu/iommufd/device.c:511 [inline] iommufd_hw_pagetable_attach+0x841/0xf20 drivers/iommu/iommufd/device.c:658 iommufd_device_do_attach+0x27/0x40 drivers/iommu/iommufd/device.c:726 iommufd_device_change_pt+0xfa/0xaf0 drivers/iommu/iommufd/device.c:957 iommufd_device_attach+0x32/0xd0 drivers/iommu/iommufd/device.c:1007 iommufd_test_pasid_attach drivers/iommu/iommufd/selftest.c:1864 [inline] iommufd_test+0x1335/0x5710 drivers/iommu/iommufd/selftest.c:2132 iommufd_fops_ioctl+0x469/0x600 drivers/iommu/iommufd/main.c:533 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl+0x101/0x170 fs/ioctl.c:583 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0x15f/0x560 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 11775: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 kasan_save_free_info+0x40/0x50 mm/kasan/generic.c:584 poison_slab_object mm/kasan/common.c:253 [inline] __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285 kasan_slab_free include/linux/kasan.h:235 [inline] slab_free_hook mm/slub.c:2689 [inline] slab_free mm/slub.c:6251 [inline] kfree+0x1ca/0x640 mm/slub.c:6566 iommufd_hwpt_detach_device drivers/iommu/iommufd/device.c:558 [inline] iommufd_hw_pagetable_detach+0x354/0x6b0 drivers/iommu/iommufd/device.c:706 iommufd_device_detach+0x21/0x80 drivers/iommu/iommufd/device.c:1059 iommufd_test_pasid_detach drivers/iommu/iommufd/selftest.c:1909 [inline] iommufd_test+0x169a/0x5710 drivers/iommu/iommufd/selftest.c:2136 iommufd_fops_ioctl+0x469/0x600 drivers/iommu/iommufd/main.c:533 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:597 [inline] __se_sys_ioctl+0x101/0x170 fs/ioctl.c:583 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0x15f/0x560 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f The buggy address belongs to the object at ffff888047f98f60 which belongs to the cache kmalloc-16 of size 16 The buggy address is located 0 bytes inside of freed 16-byte region [ffff888047f98f60, ffff888047f98f70) ================================================================== drivers/iommu/iommufd/selftest.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 25387969ab75..7565ed083087 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -1830,22 +1830,24 @@ static int iommufd_test_pasid_check_hwpt(struct iommufd_ucmd *ucmd, struct iommu_domain *attached_domain; struct iommu_attach_handle *handle; struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; struct selftest_obj *sobj; - struct mock_dev *mdev; int rc = 0; sobj = iommufd_test_get_selftest_obj(ucmd->ictx, cmd->id); if (IS_ERR(sobj)) return PTR_ERR(sobj); - mdev = sobj->idev.mock_dev; + idev = sobj->idev.idev; - handle = iommu_attach_handle_get(mdev->dev.iommu_group, + mutex_lock(&idev->igroup->lock); + handle = iommu_attach_handle_get(idev->igroup->group, cmd->pasid_check.pasid, 0); if (IS_ERR(handle)) attached_domain = NULL; else attached_domain = handle->domain; + mutex_unlock(&idev->igroup->lock); /* hwpt_id == 0 means to check if pasid is detached */ if (!hwpt_id) { base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790 -- 2.43.0