[PATCH] iommufd: Keep group state in the attach handle

From: Qihang

Date: Fri Nov 20 2026 - 22:41:34 EST


The attach handle is created per iommu_group and PASID by the device
that attaches, and it is freed when the attachment is replaced or its
last device detaches. It stores a struct iommufd_device pointer,
which is the device that created the current handle.

That device can be unbound while the handle is still in use. If one
device detaches while other devices in the group stay attached, the
handle is kept but its device pointer is left dangling. A later MSI
setup on a remaining device reaches iommufd_sw_msi(), which reads
handle->idev->igroup and dereferences freed memory:

BUG: KASAN: slab-use-after-free in iommufd_sw_msi
Read of size 8 at addr ...
Allocated by iommufd_device_bind
Freed by iommufd_object_remove <- iommufd_device_unbind

The fault read path has the same problem when it composes
hwpt_fault::dev_id from the same pointer.

Store the iommufd_group instead. It is reference counted for as long
as any device in the group is bound, and iommufd_sw_msi() runs under
the iommu group mutex, which also keeps the handle from being replaced
or freed during the call.

The fault path needs a device ID and there is no longer a device to
take it from. Cache the ID of the device that created the current
handle at attach or replace time. This preserves the existing choice
of ID while allowing the fault read path to use it after that device
has been unbound, so this patch alone removes every use of the stale
pointer.

Fixes: b7d8833677ba ("iommufd: Fault-capable hwpt attach/detach/replace")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Qihang <q.h.hack.winter@xxxxxxxxx>
---
drivers/iommu/iommufd/device.c | 6 ++++--
drivers/iommu/iommufd/driver.c | 11 +++++------
drivers/iommu/iommufd/eventq.c | 14 ++++++--------
drivers/iommu/iommufd/iommufd_private.h | 9 ++++++++-
4 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 1b0bdf57ac8a..43ee9610e6d1 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -581,7 +581,8 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
if (!handle)
return -ENOMEM;

- handle->idev = idev;
+ handle->igroup = idev->igroup;
+ handle->dev_id = idev->obj.id;
if (pasid == IOMMU_NO_PASID)
rc = iommu_attach_group_handle(hwpt->domain, idev->igroup->group,
&handle->handle);
@@ -654,7 +655,8 @@ static int iommufd_hwpt_replace_device(struct iommufd_device *idev,
if (!handle)
return -ENOMEM;

- handle->idev = idev;
+ handle->igroup = idev->igroup;
+ handle->dev_id = idev->obj.id;
if (pasid == IOMMU_NO_PASID)
rc = iommu_replace_group_handle(idev->igroup->group,
hwpt->domain, &handle->handle);
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index e4d17a748178..601a5aeced95 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -274,26 +274,25 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,

handle = to_iommufd_handle(raw_handle);
/* No IOMMU_RESV_SW_MSI means no change to the msi_msg */
- if (handle->idev->igroup->sw_msi_start == PHYS_ADDR_MAX)
+ if (handle->igroup->sw_msi_start == PHYS_ADDR_MAX)
return 0;

- ictx = handle->idev->ictx;
+ ictx = handle->igroup->ictx;
guard(mutex)(&ictx->sw_msi_lock);
/*
* The input msi_addr is the exact byte offset of the MSI doorbell, we
* assume the caller has checked that it is contained with a MMIO region
* that is secure to map at PAGE_SIZE.
*/
- msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
- msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
+ msi_map = iommufd_sw_msi_get_map(ictx, msi_addr & PAGE_MASK,
+ handle->igroup->sw_msi_start);
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);

rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
if (rc)
return rc;
- __set_bit(msi_map->id, handle->idev->igroup->required_sw_msi.bitmap);
+ __set_bit(msi_map->id, handle->igroup->required_sw_msi.bitmap);

iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
msi_desc_set_iommu_msi_iova(desc, iova, PAGE_SHIFT);
diff --git a/drivers/iommu/iommufd/eventq.c b/drivers/iommu/iommufd/eventq.c
index b8a54454dd17..3c3282fd5b93 100644
--- a/drivers/iommu/iommufd/eventq.c
+++ b/drivers/iommu/iommufd/eventq.c
@@ -83,11 +83,10 @@ void iommufd_fault_destroy(struct iommufd_object *obj)

static void iommufd_compose_fault_message(struct iommu_fault *fault,
struct iommu_hwpt_pgfault *hwpt_fault,
- struct iommufd_device *idev,
- u32 cookie)
+ u32 dev_id, u32 cookie)
{
hwpt_fault->flags = fault->prm.flags;
- hwpt_fault->dev_id = idev->obj.id;
+ hwpt_fault->dev_id = dev_id;
hwpt_fault->pasid = fault->prm.pasid;
hwpt_fault->grpid = fault->prm.grpid;
hwpt_fault->perm = fault->prm.perm;
@@ -128,7 +127,7 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
struct iommufd_eventq *eventq = filep->private_data;
struct iommufd_fault *fault = eventq_to_fault(eventq);
struct iommu_hwpt_pgfault data = {};
- struct iommufd_device *idev;
+ u32 dev_id;
struct iopf_group *group;
struct iopf_fault *iopf;
size_t done = 0;
@@ -157,11 +156,10 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
break;
}

- idev = to_iommufd_handle(group->attach_handle)->idev;
+ dev_id = to_iommufd_handle(group->attach_handle)->dev_id;
list_for_each_entry(iopf, &group->faults, list) {
- iommufd_compose_fault_message(&iopf->fault,
- &data, idev,
- group->cookie);
+ iommufd_compose_fault_message(&iopf->fault, &data,
+ dev_id, group->cookie);
if (copy_to_user(buf + group_done, &data, fault_size)) {
xa_erase(&fault->response, group->cookie);
iommufd_fault_deliver_restore(fault, group);
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index eb2e85b27e42..6d02599ebd9e 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -589,9 +589,16 @@ struct iommufd_eventq {
struct wait_queue_head wait_queue;
};

+/*
+ * Devices attached to the same PASID in an IOMMU group share this
+ * handle until the attachment is replaced or its last device detaches.
+ * Each bound device holds an igroup reference, so the group remains
+ * valid even if the device that created the handle is unbound.
+ */
struct iommufd_attach_handle {
struct iommu_attach_handle handle;
- struct iommufd_device *idev;
+ struct iommufd_group *igroup;
+ u32 dev_id;
};

/* Convert an iommu attach handle to iommufd handle. */
--
2.54.0 (Apple Git-157)