[PATCH v2] driver core: Fix potential use-after-free for devs in deferred_sync list
Ulf Hansson
ulf.hansson at oss.qualcomm.com
Thu Dec 3 08:51:32 PST 2026
In case sync_state has been temporarily paused, a supplier device may be put
on the deferred_sync list to be handled when sync_state is resumed. The
device could be removed before that happens, which would lead to a
use-after-free.
Fix this by holding a reference to the device while it's on deferred_sync
list. Let's drop the reference when it's removed from the list in
device_links_supplier_sync_state_resume() or from
device_links_driver_cleanup().
Now as dev->links.defer_sync is shared between deferred_sync and the
transient lists used to call sync_state, the two cases can't easily be
distinguished when deciding who owns the reference for the device. Let's
therefore add a separate list node, links.sync_list, for the transient
lists, so each node and its reference have a single owner.
Note that, the put_device() calls are done outside device_links_lock to
prevent a potential deadlock, in case it triggers the release path to run
for the device.
Fixes: fc5a251d0fd7 ("driver core: Add sync_state driver/bus callback")
Cc: stable at vger.kernel.org
Signed-off-by: Ulf Hansson <ulf.hansson at oss.qualcomm.com>
---
Changes in v2:
- Move the calls to put_device() outside of device_links_lock. Sashiko.
---
drivers/base/core.c | 44 ++++++++++++++++++++++++++++--------------
include/linux/device.h | 2 ++
2 files changed, 31 insertions(+), 15 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 44f79f427b6b..1c394714d6b1 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1219,11 +1219,11 @@ static void __device_links_queue_sync_state(struct device *dev,
*/
dev_set_state_synced(dev);
- if (WARN_ON(!list_empty(&dev->links.defer_sync)))
+ if (WARN_ON(!list_empty(&dev->links.sync_list)))
return;
get_device(dev);
- list_add_tail(&dev->links.defer_sync, list);
+ list_add_tail(&dev->links.sync_list, list);
}
/**
@@ -1241,8 +1241,8 @@ static void device_links_flush_sync_list(struct list_head *list,
{
struct device *dev, *tmp;
- list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
- list_del_init(&dev->links.defer_sync);
+ list_for_each_entry_safe(dev, tmp, list, links.sync_list) {
+ list_del_init(&dev->links.sync_list);
if (dev != dont_lock_dev)
device_lock(dev);
@@ -1265,7 +1265,7 @@ void device_links_supplier_sync_state_pause(void)
void device_links_supplier_sync_state_resume(void)
{
- struct device *dev, *tmp;
+ struct device *dev;
LIST_HEAD(sync_list);
device_links_write_lock();
@@ -1274,16 +1274,20 @@ void device_links_supplier_sync_state_resume(void)
goto out;
}
defer_sync_state_count--;
- if (defer_sync_state_count)
- goto out;
- list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
- /*
- * Delete from deferred_sync list before queuing it to
- * sync_list because defer_sync is used for both lists.
- */
+ /* Stop if sync_state got paused while unlocked. */
+ while (!defer_sync_state_count) {
+ dev = list_first_entry_or_null(&deferred_sync, struct device,
+ links.defer_sync);
+ if (!dev)
+ break;
+
list_del_init(&dev->links.defer_sync);
__device_links_queue_sync_state(dev, &sync_list);
+
+ device_links_write_unlock();
+ put_device(dev);
+ device_links_write_lock();
}
out:
device_links_write_unlock();
@@ -1300,8 +1304,10 @@ late_initcall(sync_state_resume_initcall);
static void __device_links_supplier_defer_sync(struct device *sup)
{
- if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
+ if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup)) {
+ get_device(sup);
list_add_tail(&sup->links.defer_sync, &deferred_sync);
+ }
}
static void device_link_drop_managed(struct device_link *link)
@@ -1565,6 +1571,7 @@ void device_links_no_driver(struct device *dev)
void device_links_driver_cleanup(struct device *dev)
{
struct device_link *link, *ln;
+ bool put = false;
device_links_write_lock();
@@ -1587,10 +1594,16 @@ void device_links_driver_cleanup(struct device *dev)
WRITE_ONCE(link->status, DL_STATE_DORMANT);
}
- list_del_init(&dev->links.defer_sync);
+ if (!list_empty(&dev->links.defer_sync)) {
+ list_del_init(&dev->links.defer_sync);
+ put = true;
+ }
__device_links_no_driver(dev);
device_links_write_unlock();
+
+ if (put)
+ put_device(dev);
}
/**
@@ -1867,7 +1880,7 @@ static int fw_devlink_dev_sync_state(struct device *dev, void *data)
dev_warn(sup, "Timed out. Forcing sync_state()\n");
dev_set_state_synced(sup);
get_device(sup);
- list_add_tail(&sup->links.defer_sync, data);
+ list_add_tail(&sup->links.sync_list, data);
return 0;
}
@@ -3249,6 +3262,7 @@ void device_initialize(struct device *dev)
INIT_LIST_HEAD(&dev->links.consumers);
INIT_LIST_HEAD(&dev->links.suppliers);
INIT_LIST_HEAD(&dev->links.defer_sync);
+ INIT_LIST_HEAD(&dev->links.sync_list);
dev->links.status = DL_DEV_NO_DRIVER;
dev_assign_dma_coherent(dev, dma_default_coherent);
swiotlb_dev_init(dev);
diff --git a/include/linux/device.h b/include/linux/device.h
index 6b082c638d45..ba6879502b60 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -485,12 +485,14 @@ enum device_removable {
* @suppliers: List of links to supplier devices.
* @consumers: List of links to consumer devices.
* @defer_sync: Hook to global list of devices that have deferred sync_state.
+ * @sync_list: Hook to a transient list of devices queued for sync_state.
* @status: Driver status information.
*/
struct dev_links_info {
struct list_head suppliers;
struct list_head consumers;
struct list_head defer_sync;
+ struct list_head sync_list;
enum dl_dev_state status;
};
--
2.43.0
More information about the linux-arm-kernel
mailing list