Skip to contentSkip to conversation

driver core: take parent lock in async device attach

PATCH In review Johan Hovold 3 participants 4 messages Oct 13 – Oct 14

Subsystems (2)
lore
Share & tools
lore

Johan Hovold

author maintainer
#0 lore raw

PATCH v3 1/1 driver core: take parent lock in async device attach

From: Mario Peter <[email protected]>

On buses with need_parent_lock set (only USB), probe() must run with the parent device locked. A synchronous attach after device_add() gets this from the caller, e.g. usb_set_configuration() holds the udev lock while adding interfaces. __device_attach_async_helper() runs the probe from an async worker instead, where that lock isn't held, and only takes device_lock(dev).

With async probing enabled for the hub driver (e.g. driver_async_probe=* or driver_async_probe=hub), this races when a multi-TT hub is configured. For multi-TT hubs, hub_probe() calls usb_set_interface(hdev, 0, 1) to select the TT-per-port altsetting, and usb_set_interface() creates the endpoint devices of the interface a second time:

usb_set_configuration() async worker

    [udev locked]
    device_add(intf)
      queues the probe  ----------> __device_attach_async_helper()
                                      [intf locked, udev not locked]
                                      hub_probe()
                                        usb_set_interface(hdev, 0, 1)
    create_intf_ep_devs(intf)             create_intf_ep_devs(intf)

create_intf_ep_devs() checks and sets intf->ep_devs_created without a lock of its own. If usb_set_configuration() has created ep_81 but not yet set the flag, usb_set_interface() skips removing the endpoint devices of the old altsetting and creates ep_81 again. With a synchronous probe, hub_probe() runs inside device_add() with the udev lock held, and usb_set_configuration() then finds ep_devs_created already set.

Single-TT hubs don't call usb_set_interface() from hub_probe() and don't show this warning, but their probe runs without the udev lock as well.

On an i.MX8MM board with an onboard USB2514 hub this hit 11 of 100 boots:

sysfs: cannot create duplicate filename '.../1-1/1-1:1.0/ep_81'

  Call trace:
   sysfs_warn_dup
   usb_create_ep_devs
   create_intf_ep_devs
   usb_set_interface
   hub_probe
   usb_probe_interface
   really_probe
   __device_attach_async_helper
   async_run_entry_fn

It also shows on v7.3-rc6 in QEMU, with dummy_hcd and a raw-gadget program emulating a multi-TT hub: re-authorizing the hub in a loop, while a SCHED_FIFO task preempts the writing task at random points for 3 ms, hit the warning in 29 of 6000 iterations.

Take the parent lock in __device_attach_async_helper() as well, like __driver_attach_async_helper() does, and move __device_driver_lock/unlock() up for that. With this the warning was gone in 300 boots on the board and in 6000 iterations in QEMU.

Fixes 765230b5f084 ("driver-core: add asynchronous probing support for drivers")
Assisted-by LLM

[ johan: switch to async_schedule_dev_nocall() to avoid deadlock if
async_schedule_dev() runs synchronously when parent lock is held ]

---

Changes in v3:
- fix deadlock if async_schedule_dev() runs synchronously

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 drivers/base/dd.c | 85 ++++++++++++++++++++++++++---------------------
 1 file changed, 47 insertions(+), 38 deletions(-)
drivers/base/dd.c +47 -38
index f6525a7ee8c5..9dc7cce9bd8d 100644
@@ -1029,6 +1029,38 @@ static int __device_attach_driver(struct device_driver *drv, void *_data)
 	return ret == 0;
 }
 
+/*
+ * __device_driver_lock - acquire locks needed to manipulate dev->drv
+ * @dev: Device we will update driver info for
+ * @parent: Parent device. Needed if the bus requires parent lock
+ *
+ * This function will take the required locks for manipulating dev->drv.
+ * Normally this will just be the @dev lock, but when called for a USB
+ * interface, @parent lock will be held as well.
+ */
+static void __device_driver_lock(struct device *dev, struct device *parent)
+{
+	if (parent && dev->bus->need_parent_lock)
+		device_lock(parent);
+	device_lock(dev);
+}
+
+/*
+ * __device_driver_unlock - release locks needed to manipulate dev->drv
+ * @dev: Device we will update driver info for
+ * @parent: Parent device. Needed if the bus requires parent lock
+ *
+ * This function will release the required locks for manipulating dev->drv.
+ * Normally this will just be the @dev lock, but when called for a
+ * USB interface, @parent lock will be released as well.
+ */
+static void __device_driver_unlock(struct device *dev, struct device *parent)
+{
+	device_unlock(dev);
+	if (parent && dev->bus->need_parent_lock)
+		device_unlock(parent);
+}
+
 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 {
 	struct device *dev = _dev;
@@ -1038,7 +1070,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 		.want_async	= true,
 	};
 
-	device_lock(dev);
+	__device_driver_lock(dev, dev->parent);
 
 	/*
 	 * Check if device has already been removed or claimed. This may
@@ -1060,7 +1092,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 	if (dev->parent)
 		pm_runtime_put(dev->parent);
 out_unlock:
-	device_unlock(dev);
+	__device_driver_unlock(dev, dev->parent);
 
 	put_device(dev);
 }
@@ -1117,8 +1149,13 @@ static int __device_attach(struct device *dev, bool allow_async)
 	}
 out_unlock:
 	device_unlock(dev);
-	if (async)
-		async_schedule_dev(__device_attach_async_helper, dev);
+	if (async) {
+		if (!async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
+			put_device(dev);
+			ret = 2;	/* retry synchronously */
+		}
+	}
+
 	return ret;
 }
 
@@ -1145,48 +1182,20 @@ EXPORT_SYMBOL_GPL(device_attach);
 void device_initial_probe(struct device *dev)
 {
 	struct subsys_private *sp = bus_to_subsys(dev->bus);
+	int ret;
 
 	if (!sp)
 		return;
 
-	if (sp->drivers_autoprobe)
-		__device_attach(dev, true);
+	if (sp->drivers_autoprobe) {
+		ret = __device_attach(dev, true);
+		if (ret == 2)
+			__device_attach(dev, false);
+	}
 
 	subsys_put(sp);
 }
 
-/*
- * __device_driver_lock - acquire locks needed to manipulate dev->drv
- * @dev: Device we will update driver info for
- * @parent: Parent device. Needed if the bus requires parent lock
- *
- * This function will take the required locks for manipulating dev->drv.
- * Normally this will just be the @dev lock, but when called for a USB
- * interface, @parent lock will be held as well.
- */
-static void __device_driver_lock(struct device *dev, struct device *parent)
-{
-	if (parent && dev->bus->need_parent_lock)
-		device_lock(parent);
-	device_lock(dev);
-}
-
-/*
- * __device_driver_unlock - release locks needed to manipulate dev->drv
- * @dev: Device we will update driver info for
- * @parent: Parent device. Needed if the bus requires parent lock
- *
- * This function will release the required locks for manipulating dev->drv.
- * Normally this will just be the @dev lock, but when called for a
- * USB interface, @parent lock will be released as well.
- */
-static void __device_driver_unlock(struct device *dev, struct device *parent)
-{
-	device_unlock(dev);
-	if (parent && dev->bus->need_parent_lock)
-		device_unlock(parent);
-}
-
 /**
  * device_driver_attach - attach a specific driver to a specific device
  * @drv: Driver to attach
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] driver-core: UAF on dev->parent in async device attach --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] driver-core: UAF on dev->parent in async device attach
--

commit c5f34060c20ab88f32e94e3a0ebf364f523925ee
Author: Mario Peter <[email protected]>

driver core: take parent lock in async device attach

On buses with need_parent_lock set (only USB), probe() must run with the parent device locked. A synchronous attach after device_add() gets this from the caller, e.g. usb_set_configuration() holds the udev lock while adding interfaces.

Quoted 4 lines
drivers/base/dd.c +0 -0
index f6525a7ee8c54..9dc7cce9bd8d2 100644

[ ... ]

Quoted 7 lines
drivers/base/dd.c +3 -0
@@ -1029,6 +1029,38 @@ static int __device_attach_driver(struct device_driver *drv, void *_data)
 	return ret == 0;
 }
 
+/*
+ * __device_driver_lock - acquire locks needed to manipulate dev->drv
+ * @dev: Device we will update driver info for

drivers/base/dd.c static int __device_attach_driver(struct device_driver *drv, void *_data) · line 1044

Quoted 10 lines
drivers/base/dd.c +10 -0
+ * @parent: Parent device. Needed if the bus requires parent lock
+ *
+ * This function will take the required locks for manipulating dev->drv.
+ * Normally this will just be the @dev lock, but when called for a USB
+ * interface, @parent lock will be held as well.
+ */
+static void __device_driver_lock(struct device *dev, struct device *parent)
+{
+	if (parent && dev->bus->need_parent_lock)
+		device_lock(parent);

[Severity: High]
Could this acquire a mutex within freed parent memory?

If this is called with a dangling parent pointer from __device_attach_async_helper(), this will dereference the freed memory.

[ ... ]

drivers/base/dd.c static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) · line 1073

Quoted 6 lines
drivers/base/dd.c +1 -1
@@ -1038,7 +1070,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 		.want_async	= true,
 	};
 
-	device_lock(dev);
+	__device_driver_lock(dev, dev->parent);

[Severity: High]
Can this cause a use-after-free on dev->parent?

In the device removal path, device_del() sets dev->p->dead to true via kill_device() and later drops the child's reference to dev->parent via put_device(dev->parent).

If the parent device is also being removed and its final reference is dropped, the parent could be freed.

If this async worker runs delayed, taking the lock before checking dev->p->dead could pass a dangling dev->parent pointer. Prior to this patch, device_lock(dev) was called, and dev->parent was never accessed if dev->p->dead was true.

drivers/base/dd.c static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) · line 1095

Quoted 9 lines
drivers/base/dd.c +1 -1
 	/*
 	 * Check if device has already been removed or claimed. This may
@@ -1060,7 +1092,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
 	if (dev->parent)
 		pm_runtime_put(dev->parent);
 out_unlock:
-	device_unlock(dev);
+	__device_driver_unlock(dev, dev->parent);

[Severity: High]
If dev->parent was freed before the dev->p->dead check above, wouldn't this unlock path also dereference the same dangling pointer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Johan Hovold

Hi Johan,

drivers/base/dd.c static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) · line 1073

Quoted 6 lines · Johan Hovold
drivers/base/dd.c +1 -1
@@ -1038,7 +1070,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
                .want_async     = true,
        };
 
-       device_lock(dev);
+       __device_driver_lock(dev, dev->parent);

Thanks for picking this up and for fixing the deadlock.

The use-after-free that the AI review flags here is real, I can trigger it in QEMU. With a debug delay before this line, driver_async_probe=usb,hub, and dummy_hcd unbound while the probe of the emulated hub's usb_device waited, KASAN reports a slab-use-after-free in __mutex_lock() called from __device_attach_async_helper(), on the root hub. An interface pins its usb_device, but a usb_device doesn't pin its parent hub, and dev drops its reference to the parent in device_del(). v2 has the same problem.

Mario

author maintainer
#3 lore raw
Quoted 11 lines · PETER Mario

drivers/base/dd.c static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) · line 1073

Quoted 6 lines · Johan Hovold
drivers/base/dd.c +1 -1
@@ -1038,7 +1070,7 @@ static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
                .want_async     = true,
        };
 
-       device_lock(dev);
+       __device_driver_lock(dev, dev->parent);

Thanks for picking this up and for fixing the deadlock.

Quoted 8 lines

The use-after-free that the AI review flags here is real, I can trigger it in QEMU. With a debug delay before this line, driver_async_probe=usb,hub, and dummy_hcd unbound while the probe of the emulated hub's usb_device waited, KASAN reports a slab-use-after-free in __mutex_lock() called from __device_attach_async_helper(), on the root hub. An interface pins its usb_device, but a usb_device doesn't pin its parent hub, and dev drops its reference to the parent in device_del(). v2 has the same problem.

Thanks for checking. I can also trigger a UAF through some existing paths when disconnecting a hub (with some instrumentation to increase the race window).

I've just sent a v2 series for the deferred probe fix that also addresses the UAF issue and includes your fix:

	https://lore.kernel.org/all/[email protected]/

Johan