Skip to content

Commit 45b859b

Browse files
committed
thermal: core: Address thermal zone removal races with resume
Since thermal_zone_pm_complete() and thermal_zone_device_resume() re-initialize the poll_queue delayed work for the given thermal zone, the cancel_delayed_work_sync() in thermal_zone_device_unregister() may miss some already running work items and the thermal zone may be freed prematurely [1]. There are two failing scenarios that both start with running thermal_pm_notify_complete() right before invoking thermal_zone_device_unregister() for one of the thermal zones. In the first scenario, there is a work item already running for the given thermal zone when thermal_pm_notify_complete() calls thermal_zone_pm_complete() for that thermal zone and it continues to run when thermal_zone_device_unregister() starts. Since the poll_queue delayed work has been re-initialized by thermal_pm_notify_complete(), the running work item will be missed by the cancel_delayed_work_sync() in thermal_zone_device_unregister() and if it continues to run past the freeing of the thermal zone object, a use-after-free will occur. In the second scenario, thermal_zone_device_resume() queued up by thermal_pm_notify_complete() runs right after the thermal_zone_exit() called by thermal_zone_device_unregister() has returned. The poll_queue delayed work is re-initialized by it before cancel_delayed_work_sync() is called by thermal_zone_device_unregister(), so it may continue to run after the freeing of the thermal zone object, which also leads to a use-after-free. Address the first failing scenario by ensuring that no thermal work items will be running when thermal_pm_notify_complete() is called. For this purpose, first move the cancel_delayed_work() call from thermal_zone_pm_complete() to thermal_zone_pm_prepare() to prevent new work from entering the workqueue going forward. Next, switch over to using a dedicated workqueue for thermal events and update the code in thermal_pm_notify() to flush that workqueue after thermal_pm_notify_prepare() has returned which will take care of all leftover thermal work already on the workqueue (that leftover work would do nothing useful anyway because all of the thermal zones have been flagged as suspended). The second failing scenario is addressed by adding a tz->state check to thermal_zone_device_resume() to prevent it from re-initializing the poll_queue delayed work if the thermal zone is going away. Note that the above changes will also facilitate relocating the suspend and resume of thermal zones closer to the suspend and resume of devices, respectively. Fixes: 5a5efda ("thermal: core: Resume thermal zones asynchronously") Reported-by: syzbot+3b3852c6031d0f30dfaf@syzkaller.appspotmail.com Closes: https://syzbot.org/bug?extid=3b3852c6031d0f30dfaf Reported-by: Mauricio Faria de Oliveira <mfo@igalia.com> Closes: https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Reviewed-by: Mauricio Faria de Oliveira <mfo@igalia.com> Tested-by: Mauricio Faria de Oliveira <mfo@igalia.com> Reviewed-by: Lukasz Luba <lukasz.luba@arm.com> Cc: All applicable <stable@vger.kernel.org> Link: https://patch.msgid.link/6267615.lOV4Wx5bFT@rafael.j.wysocki
1 parent 7aaa804 commit 45b859b

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

drivers/thermal/thermal_core.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static struct thermal_governor *def_governor;
4141

4242
static bool thermal_pm_suspended;
4343

44+
static struct workqueue_struct *thermal_wq __ro_after_init;
45+
4446
/*
4547
* Governor section: set of functions to handle thermal governors
4648
*
@@ -313,7 +315,7 @@ static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
313315
if (delay > HZ)
314316
delay = round_jiffies_relative(delay);
315317

316-
mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, delay);
318+
mod_delayed_work(thermal_wq, &tz->poll_queue, delay);
317319
}
318320

319321
static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
@@ -1785,6 +1787,10 @@ static void thermal_zone_device_resume(struct work_struct *work)
17851787

17861788
guard(thermal_zone)(tz);
17871789

1790+
/* If the thermal zone is going away, there's nothing to do. */
1791+
if (tz->state & TZ_STATE_FLAG_EXIT)
1792+
return;
1793+
17881794
tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING);
17891795

17901796
thermal_debug_tz_resume(tz);
@@ -1811,6 +1817,9 @@ static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
18111817
}
18121818

18131819
tz->state |= TZ_STATE_FLAG_SUSPENDED;
1820+
1821+
/* Prevent new work from getting to the workqueue subsequently. */
1822+
cancel_delayed_work(&tz->poll_queue);
18141823
}
18151824

18161825
static void thermal_pm_notify_prepare(void)
@@ -1829,8 +1838,6 @@ static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
18291838
{
18301839
guard(thermal_zone)(tz);
18311840

1832-
cancel_delayed_work(&tz->poll_queue);
1833-
18341841
reinit_completion(&tz->resume);
18351842
tz->state |= TZ_STATE_FLAG_RESUMING;
18361843

@@ -1840,7 +1847,7 @@ static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
18401847
*/
18411848
INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume);
18421849
/* Queue up the work without a delay. */
1843-
mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);
1850+
mod_delayed_work(thermal_wq, &tz->poll_queue, 0);
18441851
}
18451852

18461853
static void thermal_pm_notify_complete(void)
@@ -1863,6 +1870,11 @@ static int thermal_pm_notify(struct notifier_block *nb,
18631870
case PM_RESTORE_PREPARE:
18641871
case PM_SUSPEND_PREPARE:
18651872
thermal_pm_notify_prepare();
1873+
/*
1874+
* Allow any leftover thermal work items already on the
1875+
* worqueue to complete so they don't get in the way later.
1876+
*/
1877+
flush_workqueue(thermal_wq);
18661878
break;
18671879
case PM_POST_HIBERNATION:
18681880
case PM_POST_RESTORE:
@@ -1895,9 +1907,16 @@ static int __init thermal_init(void)
18951907
if (result)
18961908
goto error;
18971909

1910+
thermal_wq = alloc_workqueue("thermal_events",
1911+
WQ_FREEZABLE | WQ_POWER_EFFICIENT | WQ_PERCPU, 0);
1912+
if (!thermal_wq) {
1913+
result = -ENOMEM;
1914+
goto unregister_netlink;
1915+
}
1916+
18981917
result = thermal_register_governors();
18991918
if (result)
1900-
goto unregister_netlink;
1919+
goto destroy_workqueue;
19011920

19021921
thermal_class = kzalloc_obj(*thermal_class);
19031922
if (!thermal_class) {
@@ -1924,6 +1943,8 @@ static int __init thermal_init(void)
19241943

19251944
unregister_governors:
19261945
thermal_unregister_governors();
1946+
destroy_workqueue:
1947+
destroy_workqueue(thermal_wq);
19271948
unregister_netlink:
19281949
thermal_netlink_exit();
19291950
error:

0 commit comments

Comments
 (0)