Skip to contentSkip to conversation

driver core: fix race between bus_remove_driver() and dev_driver_uevent()

PATCH In review Dmitry Torokhov 3 participants 6 messages Oct 14 – Nov 5 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: fix race between bus_remove_driver() and dev_driver_uevent()

When a device driver is being removed, bus_remove_driver() removes the driver from the bus klist before detaching it from bound devices. If dev_driver_uevent() runs concurrently on another CPU, it may acquire the bus klist lock after the driver has been removed from the list, read the still-valid driver pointer from the device, and access the driver's name while bus_remove_driver() proceeds to free the driver structure.

To close this race window, acquire and immediately release the bus drivers klist lock after all devices have been detached. This ensures that any in-flight uevent generation that observed the driver pointer before device detachment completed finishes reading the driver name before the driver structure is destroyed.

Fixes 18daa52418e7 ("driver core: fix potential NULL pointer dereference in dev_uevent()")

---

Extremely unlikely to hit so I would not worry about backporting to stable.

 drivers/base/bus.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/base/bus.c +9 -0
index d17bd91490ee..8b3aaf451e4f 100644
@@ -830,6 +830,15 @@ void bus_remove_driver(struct device_driver *drv)
 	driver_remove_file(drv, &driver_attr_uevent);
 	klist_remove(&drv->p->knode_bus);
 	driver_detach(drv);
+
+	/*
+	 * Ensure that any concurrent dev_driver_uevent() that observed
+	 * dev->driver before driver_detach() cleared it, finishes
+	 * accessing drv->name.
+	 */
+	scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+	}
+
 	module_remove_driver(drv);
 	kobject_put(&drv->p->kobj);
 
Signature · 5 lines
2.56.0.385.gd3acb90ef8-goog


-- 
Dmitry
#1 lore raw
Quoted 29 lines · Dmitry Torokhov

When a device driver is being removed, bus_remove_driver() removes the driver from the bus klist before detaching it from bound devices. If dev_driver_uevent() runs concurrently on another CPU, it may acquire the bus klist lock after the driver has been removed from the list, read the still-valid driver pointer from the device, and access the driver's name while bus_remove_driver() proceeds to free the driver structure.

To close this race window, acquire and immediately release the bus drivers klist lock after all devices have been detached. This ensures that any in-flight uevent generation that observed the driver pointer before device detachment completed finishes reading the driver name before the driver structure is destroyed.

Fixes 18daa52418e7 ("driver core: fix potential NULL pointer dereference in dev_uevent()")

---

Extremely unlikely to hit so I would not worry about backporting to stable.

 drivers/base/bus.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/base/bus.c +0 -0
index d17bd91490ee..8b3aaf451e4f 100644
@@ -830,6 +830,15 @@ void bus_remove_driver(struct device_driver *drv)
 	driver_remove_file(drv, &driver_attr_uevent);

drivers/base/bus.c void bus_remove_driver(struct device_driver *drv) · line 840

Quoted 10 lines
drivers/base/bus.c +8 -0
 	klist_remove(&drv->p->knode_bus);
 	driver_detach(drv);
+
+	/*
+	 * Ensure that any concurrent dev_driver_uevent() that observed
+	 * dev->driver before driver_detach() cleared it, finishes
+	 * accessing drv->name.
+	 */
+	scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+	}

Shouldn't the compiler just no-op this away into nothing?

thanks,

greg k-h

#2 lore raw
Quoted 34 lines · Greg Kroah-Hartman
Quoted 33 lines · Dmitry Torokhov

When a device driver is being removed, bus_remove_driver() removes the driver from the bus klist before detaching it from bound devices. If dev_driver_uevent() runs concurrently on another CPU, it may acquire the bus klist lock after the driver has been removed from the list, read the still-valid driver pointer from the device, and access the driver's name while bus_remove_driver() proceeds to free the driver structure.

To close this race window, acquire and immediately release the bus drivers klist lock after all devices have been detached. This ensures that any in-flight uevent generation that observed the driver pointer before device detachment completed finishes reading the driver name before the driver structure is destroyed.

Fixes 18daa52418e7 ("driver core: fix potential NULL pointer dereference in dev_uevent()")

---

Extremely unlikely to hit so I would not worry about backporting to stable.

 drivers/base/bus.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/base/bus.c +2 -0
index d17bd91490ee..8b3aaf451e4f 100644
@@ -830,6 +830,15 @@ void bus_remove_driver(struct device_driver *drv)
 	driver_remove_file(drv, &driver_attr_uevent);
 	klist_remove(&drv->p->knode_bus);
 	driver_detach(drv);
+
+	/*
Quoted 8 lines

drivers/base/bus.c void bus_remove_driver(struct device_driver *drv) · line 840

Quoted 6 lines
drivers/base/bus.c +6 -0
+	 * Ensure that any concurrent dev_driver_uevent() that observed
+	 * dev->driver before driver_detach() cleared it, finishes
+	 * accessing drv->name.
+	 */
+	scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+	}

Shouldn't the compiler just no-op this away into nothing?

It better not. Why would it?

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 44 lines · Dmitry Torokhov
Quoted 34 lines · Greg Kroah-Hartman
Quoted 33 lines · Dmitry Torokhov

When a device driver is being removed, bus_remove_driver() removes the driver from the bus klist before detaching it from bound devices. If dev_driver_uevent() runs concurrently on another CPU, it may acquire the bus klist lock after the driver has been removed from the list, read the still-valid driver pointer from the device, and access the driver's name while bus_remove_driver() proceeds to free the driver structure.

To close this race window, acquire and immediately release the bus drivers klist lock after all devices have been detached. This ensures that any in-flight uevent generation that observed the driver pointer before device detachment completed finishes reading the driver name before the driver structure is destroyed.

Fixes 18daa52418e7 ("driver core: fix potential NULL pointer dereference in dev_uevent()")

---

Extremely unlikely to hit so I would not worry about backporting to stable.

 drivers/base/bus.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/base/bus.c +2 -0
index d17bd91490ee..8b3aaf451e4f 100644
@@ -830,6 +830,15 @@ void bus_remove_driver(struct device_driver *drv)
 	driver_remove_file(drv, &driver_attr_uevent);
 	klist_remove(&drv->p->knode_bus);
 	driver_detach(drv);
+
+	/*
Quoted 8 lines

drivers/base/bus.c void bus_remove_driver(struct device_driver *drv) · line 840

Quoted 6 lines
drivers/base/bus.c +6 -0
+	 * Ensure that any concurrent dev_driver_uevent() that observed
+	 * dev->driver before driver_detach() cleared it, finishes
+	 * accessing drv->name.
+	 */
+	scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+	}

Shouldn't the compiler just no-op this away into nothing?

Quoted 1 line

It better not. Why would it?

Sorry, early morning before my coffee had kicked in, I was thinking a simple lock/unlock pair without any data in the middle might be compiled away...

Still feels odd though I don't have a better idea.

greg k-h

#4 lore raw

Hi Greg,

Quoted 18 lines · Greg Kroah-Hartman
Quoted 17 lines · Dmitry Torokhov
Quoted 15 lines · Greg Kroah-Hartman

drivers/base/bus.c void bus_remove_driver(struct device_driver *drv) · line 840

Quoted 12 lines · Dmitry Torokhov
drivers/base/bus.c +8 -0
@@ -830,6 +830,15 @@ void bus_remove_driver(struct device_driver *drv)
 	driver_remove_file(drv, &driver_attr_uevent);
 	klist_remove(&drv->p->knode_bus);
 	driver_detach(drv);
+
+	/*
+	 * Ensure that any concurrent dev_driver_uevent() that observed
+	 * dev->driver before driver_detach() cleared it, finishes
+	 * accessing drv->name.
+	 */
+	scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+	}

Shouldn't the compiler just no-op this away into nothing?

Quoted 7 lines
Quoted 1 line

It better not. Why would it?

Sorry, early morning before my coffee had kicked in, I was thinking a simple lock/unlock pair without any data in the middle might be compiled away...

Still feels odd though I don't have a better idea.

Just a gentle ping on this patch. Could it be picked up, or would you prefer a plain spin_lock()/spin_unlock() pair instead of the empty scoped_guard() block?

Thanks.

Signature · 1 line
Dmitry
maintainer
#5 lore raw
Quoted 3 lines · Dmitry Torokhov

Just a gentle ping on this patch. Could it be picked up, or would you prefer a plain spin_lock()/spin_unlock() pair instead of the empty scoped_guard() block?

Personally I like the scoped_guard(), but to me either is fine.

But I'd consider updating the comment above dev_driver_uevent() accordingly, e.g.:

drivers/base/core.c +6 -3
index cbe05197d4a0..cc6a1016c251 100644
@@ -2705,9 +2705,12 @@ static const char *dev_uevent_name(const struct kobject *kobj)
  * risky as driver pointer is transitioning to NULL, so READ_ONCE() should
  * be used to make sure we are dealing with the same pointer, and to
  * ensure that driver structure is not going to disappear from under us
- * we take bus' drivers klist lock. The assumption that only registered
- * driver can be bound to a device, and to unregister a driver bus code
- * will take the same lock.
+ * we take the bus' drivers klist lock. During driver unregistration,
+ * bus_remove_driver() acquires and releases this lock after detaching
+ * all devices and clearing their driver pointers. This waits for readers
+ * that observed the old pointer to finish accessing the driver name
+ * before the driver can be freed. Readers acquiring the lock afterward
+ * cannot observe the removed driver.
  */
 static void dev_driver_uevent(const struct device *dev, struct kobj_uevent_env *env)
 {