Skip to contentSkip to conversation

iommu: Hold group references across bus_iommu_probe()

PATCH In review Peter Chen 2 participants 3 messages Nov 12 – Nov 24

Subsystems (3)
lore
Share & tools
lore

Peter Chen

author
#0 lore raw

PATCH v2 1/1 iommu: Hold group references across bus_iommu_probe()

bus_iommu_probe() collects the groups that are linked through group->entry, but the list does not hold a reference on them. Only the member devices do. A device of such a group may be released while the bus walk is still in progress, for example by a BUS_NOTIFY_REMOVED_DEVICE for a platform device that is depopulated concurrently. If the last iommu_group_put() then frees the group while it is still linked on the list, the second loop of bus_iommu_probe() walks and unlinks freed memory.

This is the "iommu_bus_notifier() being triggered during bus_iommu_probe()" case that commit da33e87bd2bf ("iommu: Handle yet another race around registration") left open. The sequence is:

CPU0 (driver's of_platform_populate / of_platform_depopulate() path) CPU1 (arm_smmu_device_probe() -> iommu_device_register())

CPU0 CPU1
of_platform_device_create(T)
bus_iommu_probe()
__iommu_probe_device(T)
allocates group X, queues X on group_list
platform_device_unregister(T)

    iommu_bus_notifier()
      __iommu_group_remove_device(T)
        iommu_group_put(X) -> kfree(X)
                                        list_del_init(&X->entry)
                                          <- use-after-free

The kernel dump is like below:
list_del corruption. next->prev should be ffffff8064a984e8,

                       but was 0000000000000000. (next=ffffff8064a9a6e8)
  kernel BUG at lib/list_debug.c:67!
  Call trace:
   __list_del_entry_valid_or_report+0x148/0x14c (P)
   iommu_device_register+0x1d8/0x268
   arm_smmu_device_probe+0x588/0x624 [arm_smmu]
   platform_probe+0x74/0xb8
   ...
   deferred_probe_work_func+0xa4/0xf4

where next points at ->entry (offset 232) of a freed kmalloc-512 object, the released iommu_group of the depopulated device. That was seen on an Android 6.18-based kernel, but the code involved is unchanged in mainline.

Take a group reference when a group is added to the list, and drop it once bus_iommu_probe() is done with the group. With the group kept alive, a group that has lost all of its devices in the meantime can simply be skipped: there is nothing left to attach a default domain to.

Also drain the list on the error paths. Returning early used to leave the remaining groups linked to the dead on-stack list head, so list_empty(&group->entry) stays false for them and a later bus_iommu_probe() would never queue them for default domain setup again.

Fixes 41df6dcc0a3f ("iommu: Keep a list of allocated groups in __iommu_probe_device()")

---
Changes in v2:
- Replace the step list with a race diagram in the commit message.
- Add Cc: stable.
- Rebase on v7.4-rc1, no code change.
- Delete the LLM Assisted-by tag.
- v1: https://lore.kernel.org/all/[email protected]/

 drivers/iommu/iommu.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)
drivers/iommu/iommu.c +37 -4
index b486b8bbd1fc..6965b89e76bb 100644
@@ -682,10 +682,14 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 		/*
 		 * With a group_list argument we defer the default_domain setup
 		 * to the caller by providing a de-duplicated list of groups
-		 * that need further setup.
+		 * that need further setup. The list holds a reference on each
+		 * group, as its devices may be released before the caller
+		 * gets to it.
 		 */
-		if (list_empty(&group->entry))
+		if (list_empty(&group->entry)) {
+			iommu_group_ref_get(group);
 			list_add_tail(&group->entry, group_list);
+		}
 	}
 
 	if (group->default_domain)
@@ -1951,6 +1955,18 @@ static void iommu_group_do_probe_finalize(struct device *dev)
 		ops->probe_finalize(dev);
 }
 
+static void iommu_group_list_put(struct list_head *group_list)
+{
+	struct iommu_group *group, *next;
+
+	list_for_each_entry_safe(group, next, group_list, entry) {
+		mutex_lock(&group->mutex);
+		list_del_init(&group->entry);
+		mutex_unlock(&group->mutex);
+		iommu_group_put(group);
+	}
+}
+
 static int bus_iommu_probe(const struct bus_type *bus)
 {
 	struct iommu_group *group, *next;
@@ -1959,7 +1975,7 @@ static int bus_iommu_probe(const struct bus_type *bus)
 
 	ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
 	if (ret)
-		return ret;
+		goto err_put_groups;
 
 	list_for_each_entry_safe(group, next, &group_list, entry) {
 		struct group_device *gdev;
@@ -1969,6 +1985,16 @@ static int bus_iommu_probe(const struct bus_type *bus)
 		/* Remove item from the list */
 		list_del_init(&group->entry);
 
+		/*
+		 * The bus notifier may have released every device of the group
+		 * since it was added to the list; there is nothing to set up.
+		 */
+		if (list_empty(&group->devices)) {
+			mutex_unlock(&group->mutex);
+			iommu_group_put(group);
+			continue;
+		}
+
 		/*
 		 * We go to the trouble of deferred default domain creation so
 		 * that the cross-group default domain type and the setup of the
@@ -1977,7 +2003,8 @@ static int bus_iommu_probe(const struct bus_type *bus)
 		ret = iommu_setup_default_domain(group, 0);
 		if (ret) {
 			mutex_unlock(&group->mutex);
-			return ret;
+			iommu_group_put(group);
+			goto err_put_groups;
 		}
 		for_each_group_device(group, gdev)
 			iommu_setup_dma_ops(gdev->dev, group->default_domain);
@@ -1991,9 +2018,15 @@ static int bus_iommu_probe(const struct bus_type *bus)
 		 */
 		for_each_group_device(group, gdev)
 			iommu_group_do_probe_finalize(gdev->dev);
+
+		iommu_group_put(group);
 	}
 
 	return 0;
+
+err_put_groups:
+	iommu_group_list_put(&group_list);
+	return ret;
 }
 
 /**

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.43.0
#1 lore raw
Quoted 15 lines

[ ... 9 lines skipped ... ]
This is the "iommu_bus_notifier() being triggered during bus_iommu_probe()" case that commit da33e87bd2bf ("iommu: Handle yet another race around registration") left open. The sequence is:

CPU0 (driver's of_platform_populate / of_platform_depopulate() path) CPU1 (arm_smmu_device_probe() -> iommu_device_register())

CPU0 CPU1
of_platform_device_create(T)
bus_iommu_probe()
__iommu_probe_device(T)
allocates group X, queues X on group_list
platform_device_unregister(T)

Quoted 5 lines
    iommu_bus_notifier()
      __iommu_group_remove_device(T)
        iommu_group_put(X) -> kfree(X)
                                        list_del_init(&X->entry)
                                          <- use-after-free

This seems odd, like shouldn't something outside the iommu subsystem be serializing these two things? If some driver is calling of_platform_device_create() and racing it with platform_device_unregister() it is broken, IMHO.

Can you explain more how this arises in the first place?

drivers/iommu/iommu.c static void iommu_group_do_probe_finalize(struct device *dev) · line 1966

Quoted 13 lines
drivers/iommu/iommu.c +9 -0
@@ -1951,6 +1955,18 @@ static void iommu_group_do_probe_finalize(struct device *dev)
 		ops->probe_finalize(dev);
 }
 
+static void iommu_group_list_put(struct list_head *group_list)
+{
+	struct iommu_group *group, *next;
+
+	list_for_each_entry_safe(group, next, group_list, entry) {
+		mutex_lock(&group->mutex);
+		list_del_init(&group->entry);
+		mutex_unlock(&group->mutex);
+		iommu_group_put(group);

What is the mutex for? The group_list is a stack head so what are we protecting list del against?

Signature · 1 line
Jason
author
#2 lore raw
Quoted 21 lines · Jason Gunthorpe
Quoted 15 lines

[ ... 9 lines skipped ... ]
This is the "iommu_bus_notifier() being triggered during bus_iommu_probe()" case that commit da33e87bd2bf ("iommu: Handle yet another race around registration") left open. The sequence is:

CPU0 (driver's of_platform_populate / of_platform_depopulate() path) CPU1 (arm_smmu_device_probe() -> iommu_device_register())

CPU0 CPU1
of_platform_device_create(T)
bus_iommu_probe()
__iommu_probe_device(T)
allocates group X, queues X on group_list
platform_device_unregister(T)

Quoted 5 lines
    iommu_bus_notifier()
      __iommu_group_remove_device(T)
        iommu_group_put(X) -> kfree(X)
                                        list_del_init(&X->entry)
                                          <- use-after-free
Quoted 6 lines

This seems odd, like shouldn't something outside the iommu subsystem be serializing these two things? If some driver is calling of_platform_device_create() and racing it with platform_device_unregister() it is broken, IMHO.

Can you explain more how this arises in the first place?

Hi Jason,

Thanks for reviewing.

It is not one driver racing create against unregister. It is the usual glue/core pattern: the parent populates the child, and if the child is not bound (due to IOMMU is not ready at this case) yet, it depopulates and retries later. That runs at the same time as an unrelated IOMMU instance registering.

We hit it on a Qualcomm SoC (Android 6.18 kernel), with a DT like this:

	usb@a600000 {				/* glue */
		compatible = "qcom,...";
		...
		usb@a600000 {			/* dwc3 core */
			compatible = "snps,dwc3";
			iommus = <&apps_smmu 0x40 0x0>;
		};
	};

The glue driver does what upstream dwc3-imx8mp.c does in dwc3_imx8mp_probe():

	of_platform_populate(node, NULL, NULL, dev);
	pdev = of_find_device_by_node(dwc3_np);
	dwc = platform_get_drvdata(pdev);
	if (!dwc) {
		/* core probe deferred, e.g. its SMMU is not there yet */
		ret = -EPROBE_DEFER;
		goto depopulate;	/* -> platform_device_unregister() */
	}

The dwc3 core probe defers because apps_smmu has not registered yet, so the glue depopulates the child. Meanwhile the SMMU driver probes from the deferred probe work and calls iommu_device_register():

glue (kworker) SMMU probe (deferred_probe_work)
-------------- --------------------------------
of_platform_populate()
device_add(T) /* dwc3 core */
T's probe -> -EPROBE_DEFER
iommu_device_register()
bus_iommu_probe(platform)
__iommu_probe_device(T)
dma_configure replay finds the SMMU, allocates group G, adds G to group_list
platform_get_drvdata(T) == NULL
of_platform_depopulate()

    platform_device_unregister(T)
      BUS_NOTIFY_REMOVED_DEVICE
        iommu_release_device(T)
          last ref on G dropped, G freed
                                             list_for_each_entry_safe(G...)
                                               mutex_lock(&G->mutex) <- UAF

The two sides only share the platform bus. Neither driver knows about the other, so I don't see a lock outside the IOMMU core that could order them. The glue does not know the SMMU is in the middle of registering, and the SMMU driver has no relationship to the USB device. Device add/remove can happen at any time while bus_for_each_dev() runs, and that is why the core has iommu_bus_notifier(). Commit da33e87bd2bf already handles this notifier running during bus_iommu_probe(). This patch closes the remaining hole, where a group on the on-stack list loses its last device and gets freed.

I will put this real-world scenario into the commit message for v3.

Quoted 5 lines
Quoted 5 lines
drivers/iommu/iommu.c +0 -0
@@ -1951,6 +1955,18 @@ static void iommu_group_do_probe_finalize(struct device *dev)
 		ops->probe_finalize(dev);
 }
 
Quoted 13 lines

drivers/iommu/iommu.c static void iommu_group_do_probe_finalize(struct device *dev) · line 1966

Quoted 9 lines
drivers/iommu/iommu.c +9 -0
+static void iommu_group_list_put(struct list_head *group_list)
+{
+	struct iommu_group *group, *next;
+
+	list_for_each_entry_safe(group, next, group_list, entry) {
+		mutex_lock(&group->mutex);
+		list_del_init(&group->entry);
+		mutex_unlock(&group->mutex);
+		iommu_group_put(group);

What is the mutex for? The group_list is a stack head so what are we protecting list del against?

Yes, the list head is on the stack, but group->entry is in the shared group. "Is group->entry empty" is the de-dup flag that __iommu_probe_device() tests and sets under group->mutex:

	if (list_empty(&group->entry))
		list_add_tail(&group->entry, group_list);

bus_iommu_probe() can run concurrently for different IOMMU instances, since only the per-device probe is serialised by iommu_probe_device_lock. So the unlink and re-init is done under group->mutex to make it atomic with respect to that check-and-add, the same as the existing loop in bus_iommu_probe().

It is only on the error path, the cost is little, so I'd prefer to keep it. If you think that concurrency can't happen in practice, I'm fine dropping it in v3.

Signature · 2 lines
Thanks,
Peter Chen