[PATCH] driver core: Fix potential use-after-free for devs in deferred_sync list
From: Ulf Hansson
Date: Thu Dec 03 2026 - 06:51:12 EST
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.
Fixes: fc5a251d0fd7 ("driver core: Add sync_state driver/bus callback")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ulf Hansson <ulf.hansson@xxxxxxxxxxxxxxxx>
---
drivers/base/core.c | 25 ++++++++++++++-----------
include/linux/device.h | 2 ++
2 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 44f79f427b6b..693c5d645f83 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);
@@ -1278,12 +1278,9 @@ void device_links_supplier_sync_state_resume(void)
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.
- */
list_del_init(&dev->links.defer_sync);
__device_links_queue_sync_state(dev, &sync_list);
+ put_device(dev);
}
out:
device_links_write_unlock();
@@ -1300,8 +1297,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)
@@ -1587,7 +1586,10 @@ 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_device(dev);
+ }
__device_links_no_driver(dev);
device_links_write_unlock();
@@ -1867,7 +1869,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 +3251,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