Skip to contentSkip to conversation

iommufd: Validate hw_queue dependency against concurrent destroy

PATCH New Qihang 1 participant 1 message Nov 25

Subsystems (2)
lore
Share & tools
lore

Qihang

author
#0 lore raw

PATCH 1/1 iommufd: Validate hw_queue dependency against concurrent destroy

_iommufd_object_depend() takes a raw pointer that the IOMMU driver kept in its private structure and does refcount_inc() on it. The driver lock keeps the object memory readable while depend() runs, but nothing prevents IOMMU_DESTROY from removing the object concurrently: it drops the users count to zero, erases the id from ictx->objects and releases the xarray lock before calling the driver destroy op and kfree().

refcount_inc() on a zero count warns and saturates, but depend() still returns success and the caller stores the dangling pointer. A later undepend then reads the freed queue.

Revalidate the depended object under the xarray lock before taking the reference. If the id no longer resolves to the same object, return -EBUSY and do not store the dependency. This mirrors iommufd_try_inc_users(); the check is open-coded because iommufd_driver may be built-in while iommufd itself is a module.

This also rejects a queue visible to the driver before finalization, while its creator can still abort the allocation.

Without the patch the mock vIOMMU destroy/alloc test reported "refcount_t: addition on 0" and a KASAN slab-use-after-free. With the patch the same test observed -EBUSY and completed without refcount warnings or KASAN reports. Tegra241 CMDQV hardware was not tested.

Fixes 0b37d892d042 ("iommufd/driver: Add iommufd_hw_queue_depend/undepend() helpers")
Cc <[email protected]> # v6.17+
---
 drivers/iommu/iommufd/driver.c | 19 +++++++++++++++++--
 include/linux/iommufd.h        | 17 +++++++++++++----
 2 files changed, 30 insertions(+), 6 deletions(-)
drivers/iommu/iommufd/driver.c +17 -2
index e4d17a748178..31d8c79c4f4d 100644
@@ -4,9 +4,12 @@
 #include "iommufd_private.h"
 
 /* Driver should use a per-structure helper in include/linux/iommufd.h */
-int _iommufd_object_depend(struct iommufd_object *obj_dependent,
+int _iommufd_object_depend(struct iommufd_ctx *ictx,
+			   struct iommufd_object *obj_dependent,
 			   struct iommufd_object *obj_depended)
 {
+	struct iommufd_object *cur;
+
 	/* Reject self dependency that dead locks */
 	if (obj_dependent == obj_depended)
 		return -EINVAL;
@@ -14,7 +17,19 @@ int _iommufd_object_depend(struct iommufd_object *obj_dependent,
 	if (obj_dependent->type != obj_depended->type)
 		return -EINVAL;
 
-	refcount_inc(&obj_depended->users);
+	/*
+	 * As in iommufd_try_inc_users(), take the reference under the xarray
+	 * lock only if the object is still installed. Keep this check local
+	 * because iommufd_driver can be built-in while iommufd is a module.
+	 */
+	xa_lock(&ictx->objects);
+	cur = xa_load(&ictx->objects, obj_depended->id);
+	if (cur == obj_depended)
+		refcount_inc(&obj_depended->users);
+	xa_unlock(&ictx->objects);
+	if (cur != obj_depended)
+		return -EBUSY;
+
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(_iommufd_object_depend, "IOMMUFD");
include/linux/iommufd.h +13 -4
index 3087f5b2def8..b0b382beed2f 100644
@@ -259,7 +259,8 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)
 #endif /* CONFIG_IOMMUFD */
 
 #if IS_ENABLED(CONFIG_IOMMUFD_DRIVER_CORE)
-int _iommufd_object_depend(struct iommufd_object *obj_dependent,
+int _iommufd_object_depend(struct iommufd_ctx *ictx,
+			   struct iommufd_object *obj_dependent,
 			   struct iommufd_object *obj_depended);
 void _iommufd_object_undepend(struct iommufd_object *obj_dependent,
 			      struct iommufd_object *obj_depended);
@@ -277,7 +278,8 @@ int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
 				enum iommu_veventq_type type, void *event_data,
 				size_t data_len);
 #else /* !CONFIG_IOMMUFD_DRIVER_CORE */
-static inline int _iommufd_object_depend(struct iommufd_object *obj_dependent,
+static inline int _iommufd_object_depend(struct iommufd_ctx *ictx,
+					 struct iommufd_object *obj_dependent,
 					 struct iommufd_object *obj_depended)
 {
 	return -EOPNOTSUPP;
@@ -351,6 +353,11 @@ static inline int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
 /*
  * Helpers for IOMMU driver to build/destroy a dependency between two sibling
  * structures created by one of the allocators above
+ *
+ * Hold the driver lock protecting @depended from lookup until
+ * iommufd_hw_queue_depend() returns. A queue that has not yet been
+ * published or has already been removed yields -EBUSY. Call
+ * iommufd_hw_queue_undepend() only after a successful depend.
  */
 #define iommufd_hw_queue_depend(dependent, depended, member)                   \
 	({                                                                     \
@@ -361,8 +368,10 @@ static inline int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
 		static_assert(__same_type(typeof(*dependent), *depended));     \
 		if (!WARN_ON_ONCE(dependent->member.viommu !=                  \
 				  depended->member.viommu))                    \
-			ret = _iommufd_object_depend(&dependent->member.obj,   \
-						     &depended->member.obj);   \
+			ret = _iommufd_object_depend(                          \
+				dependent->member.viommu->ictx,                \
+				&dependent->member.obj,                        \
+				&depended->member.obj);                        \
 		ret;                                                           \
 	})
 
Signature · 1 line
2.54.0 (Apple Git-157)