thermal: core: Address thermal zone removal races with resume
Rafael J. Wysocki
maintainerPATCH v2 1/1 thermal: core: Address thermal zone removal races with resume
From: Rafael J. Wysocki <[email protected]>
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.
---
v1 -> v2: Return -ENOMEM from thermal_init() when the workqueue allocation
fails (Sashiko)
Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/thermal/thermal_core.c +26 -5
@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors *
@@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)
@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);
@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )
@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;
@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )
@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :
@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error; + thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 ); +if (!thermal_wq) { + result = -ENOMEM ; + goto unregister_netlink; + } + result =thermal_register_governors ();if (result) - goto unregister_netlink; + goto destroy_workqueue; thermal_class =kzalloc_obj (*thermal_class);if (!thermal_class) {
@@ -1938,6 +1957,8 @@ static int __init thermal_init(void) unregister_governors:thermal_unregister_governors (); +destroy_workqueue: +destroy_workqueue (thermal_wq); unregister_netlink:thermal_netlink_exit (); error:
Quoted 49 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Since
thermal_zone_pm_complete()andthermal_zone_device_resume()re-initialize thepoll_queuedelayed work for the given thermal zone, thecancel_delayed_work_sync()inthermal_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 invokingthermal_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()callsthermal_zone_pm_complete()for that thermal zone and it continues to run whenthermal_zone_device_unregister()starts. Since thepoll_queuedelayed work has been re-initialized bythermal_pm_notify_complete(), the running work item will be missed by thecancel_delayed_work_sync()inthermal_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 bythermal_pm_notify_complete()runs right after thethermal_zone_exit()called bythermal_zone_device_unregister()has returned. Thepoll_queuedelayed work is re-initialized by it beforecancel_delayed_work_sync()is called bythermal_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 thecancel_delayed_work()call fromthermal_zone_pm_complete()tothermal_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 inthermal_pm_notify()to flush that workqueue afterthermal_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->statecheck tothermal_zone_device_resume()to prevent it from re-initializing thepoll_queuedelayed 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.
Quoted 5 lines
Fixes 5a5efdaffda5 ("thermal: core: Resume thermal zones asynchronously")Reported-by Mauricio Faria de Oliveira <[email protected]>Closes https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] open hereSigned-off-by Rafael J. Wysocki <[email protected]>---
I spent some hours reviewing and thinking of corner cases for this patch,
and found the code to correctly handle all cases I thought of.
I also tested it with the synthetic reproducers for the 2 cases reported in [1], and it prevented the errors.
Please feel free to add:
And please carry these from [1], if that's OK with you:
Thanks,
Mauricio
Quoted 109 lines
v1 -> v2: Return
-ENOMEMfromthermal_init()when the workqueue allocation
fails (Sashiko)Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)drivers/thermal/thermal_core.c +26
-5@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors * @@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error; + thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 ); +if (!thermal_wq) { + result = -ENOMEM ; + goto unregister_netlink; + } + result =thermal_register_governors ();if (result) - goto unregister_netlink; + goto destroy_workqueue; thermal_class =kzalloc_obj (*thermal_class);if (!thermal_class) {@@ -1938,6 +1957,8 @@ static int __init thermal_init(void) unregister_governors:thermal_unregister_governors (); +destroy_workqueue: +destroy_workqueue (thermal_wq); unregister_netlink:thermal_netlink_exit (); error:
Signature · 1 line
Mauricio
Rafael J. Wysocki
maintainerQuoted 66 lines · Mauricio Faria de Oliveira
Quoted 49 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Since
thermal_zone_pm_complete()andthermal_zone_device_resume()re-initialize thepoll_queuedelayed work for the given thermal zone, thecancel_delayed_work_sync()inthermal_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 invokingthermal_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()callsthermal_zone_pm_complete()for that thermal zone and it continues to run whenthermal_zone_device_unregister()starts. Since thepoll_queuedelayed work has been re-initialized bythermal_pm_notify_complete(), the running work item will be missed by thecancel_delayed_work_sync()inthermal_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 bythermal_pm_notify_complete()runs right after thethermal_zone_exit()called bythermal_zone_device_unregister()has returned. Thepoll_queuedelayed work is re-initialized by it beforecancel_delayed_work_sync()is called bythermal_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 thecancel_delayed_work()call fromthermal_zone_pm_complete()tothermal_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 inthermal_pm_notify()to flush that workqueue afterthermal_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->statecheck tothermal_zone_device_resume()to prevent it from re-initializing thepoll_queuedelayed 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.
Quoted 5 lines
Fixes 5a5efdaffda5 ("thermal: core: Resume thermal zones asynchronously")Reported-by Mauricio Faria de Oliveira <[email protected]>Closes https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] open hereSigned-off-by Rafael J. Wysocki <[email protected]>---
I spent some hours reviewing and thinking of corner cases for this patch,
and found the code to correctly handle all cases I thought of.I also tested it with the synthetic reproducers for the 2 cases reported in [1], and it prevented the errors.
Please feel free to add:
Quoted 7 lines
Reviewed-by Mauricio Faria de Oliveira <[email protected]>And please carry these from [1], if that's OK with you:
Reported-by [email protected]
I will, thank you!
Quoted 112 lines
Quoted 101 lines
v1 -> v2: Return
-ENOMEMfromthermal_init()when the workqueue allocation
fails (Sashiko)Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)drivers/thermal/thermal_core.c +24
-5@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors * @@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error; + thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 ); +if (!thermal_wq) { + result = -ENOMEM ; + goto unregister_netlink; + } + result =thermal_register_governors ();if (result) - goto unregister_netlink; + goto destroy_workqueue; thermal_class =kzalloc_obj (*thermal_class);if (!thermal_class) {drivers/thermal/thermal_core.c static int __init thermal_init(void) · line 1964
Quoted 8 lines
drivers/thermal/thermal_core.c +2
-0@@ -1938,6 +1957,8 @@ static int __init thermal_init(void) unregister_governors:thermal_unregister_governors (); +destroy_workqueue: +destroy_workqueue (thermal_wq); unregister_netlink:thermal_netlink_exit (); error:--
Mauricio
Lukasz Luba
reviewerQuoted 53 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Since
thermal_zone_pm_complete()andthermal_zone_device_resume()re-initialize thepoll_queuedelayed work for the given thermal zone, thecancel_delayed_work_sync()inthermal_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 invokingthermal_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()callsthermal_zone_pm_complete()for that thermal zone and it continues to run whenthermal_zone_device_unregister()starts. Since thepoll_queuedelayed work has been re-initialized bythermal_pm_notify_complete(), the running work item will be missed by thecancel_delayed_work_sync()inthermal_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 bythermal_pm_notify_complete()runs right after thethermal_zone_exit()called bythermal_zone_device_unregister()has returned. Thepoll_queuedelayed work is re-initialized by it beforecancel_delayed_work_sync()is called bythermal_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 thecancel_delayed_work()call fromthermal_zone_pm_complete()tothermal_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 inthermal_pm_notify()to flush that workqueue afterthermal_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->statecheck tothermal_zone_device_resume()to prevent it from re-initializing thepoll_queuedelayed 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 5a5efdaffda5 ("thermal: core: Resume thermal zones asynchronously")Reported-by Mauricio Faria de Oliveira <[email protected]>Closes https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] open hereSigned-off-by Rafael J. Wysocki <[email protected]>
Quoted 7 lines
---
v1 -> v2: Return
-ENOMEMfromthermal_init()when the workqueue allocation
fails (Sashiko)Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
My apologies for delay...
Quoted 77 lines
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)drivers/thermal/thermal_core.c +16
-4@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors * @@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :
drivers/thermal/thermal_core.c static int __init thermal_init(void) · line 1925
Quoted 5 lines
drivers/thermal/thermal_core.c +2
-0@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error; + thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 );
Why we need those workqueue instances per-cpu?
It isn't a concurrent work, only one CPU can manage the cpufreq domain.
I would drop that flag and allow to have single instance and migrate between cpus (to not wake up the one which queued the work).
Quoted 24 lines
drivers/thermal/thermal_core.c +8
-1+if (!thermal_wq) { + result = -ENOMEM ; + goto unregister_netlink; + } + result =thermal_register_governors ();if (result) - goto unregister_netlink; + goto destroy_workqueue; thermal_class =kzalloc_obj (*thermal_class);if (!thermal_class) {@@ -1938,6 +1957,8 @@ static int __init thermal_init(void) unregister_governors:thermal_unregister_governors (); +destroy_workqueue: +destroy_workqueue (thermal_wq); unregister_netlink:thermal_netlink_exit (); error:
Rafael J. Wysocki
maintainerQuoted 147 lines · Lukasz Luba
Quoted 53 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Since
thermal_zone_pm_complete()andthermal_zone_device_resume()re-initialize thepoll_queuedelayed work for the given thermal zone, thecancel_delayed_work_sync()inthermal_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 invokingthermal_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()callsthermal_zone_pm_complete()for that thermal zone and it continues to run whenthermal_zone_device_unregister()starts. Since thepoll_queuedelayed work has been re-initialized bythermal_pm_notify_complete(), the running work item will be missed by thecancel_delayed_work_sync()inthermal_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 bythermal_pm_notify_complete()runs right after thethermal_zone_exit()called bythermal_zone_device_unregister()has returned. Thepoll_queuedelayed work is re-initialized by it beforecancel_delayed_work_sync()is called bythermal_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 thecancel_delayed_work()call fromthermal_zone_pm_complete()tothermal_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 inthermal_pm_notify()to flush that workqueue afterthermal_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->statecheck tothermal_zone_device_resume()to prevent it from re-initializing thepoll_queuedelayed 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 5a5efdaffda5 ("thermal: core: Resume thermal zones asynchronously")Reported-by Mauricio Faria de Oliveira <[email protected]>Closes https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] open hereSigned-off-by Rafael J. Wysocki <[email protected]>Quoted 7 lines
---
v1 -> v2: Return
-ENOMEMfromthermal_init()when the workqueue allocation
fails (Sashiko)Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
My apologies for delay...
Quoted 80 lines
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)drivers/thermal/thermal_core.c +16
-4@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors * @@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error;
Quoted 5 lines
drivers/thermal/thermal_core.c static int __init thermal_init(void) · line 1925
Quoted 2 lines
drivers/thermal/thermal_core.c +2
-0+ thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 );Why we need those workqueue instances per-cpu?
That's to limit the changes in this fix to what's needed to address the problem at hand.
The previously used workqueue was also per-CPU and freezable.
Quoted 4 lines
It isn't a concurrent work, only one CPU can manage the cpufreq domain.
I would drop that flag and allow to have single instance and migrate between cpus (to not wake up the one which queued the work).
I have a separate patch for that on top of this one, will post it later.
Let's avoid mixing fixes with changes that aren't directly related to them.
Quoted 159 lines · Rafael J. Wysocki
Quoted 147 lines · Lukasz Luba
Quoted 53 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Since
thermal_zone_pm_complete()andthermal_zone_device_resume()re-initialize thepoll_queuedelayed work for the given thermal zone, thecancel_delayed_work_sync()inthermal_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 invokingthermal_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()callsthermal_zone_pm_complete()for that thermal zone and it continues to run whenthermal_zone_device_unregister()starts. Since thepoll_queuedelayed work has been re-initialized bythermal_pm_notify_complete(), the running work item will be missed by thecancel_delayed_work_sync()inthermal_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 bythermal_pm_notify_complete()runs right after thethermal_zone_exit()called bythermal_zone_device_unregister()has returned. Thepoll_queuedelayed work is re-initialized by it beforecancel_delayed_work_sync()is called bythermal_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 thecancel_delayed_work()call fromthermal_zone_pm_complete()tothermal_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 inthermal_pm_notify()to flush that workqueue afterthermal_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->statecheck tothermal_zone_device_resume()to prevent it from re-initializing thepoll_queuedelayed 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 5a5efdaffda5 ("thermal: core: Resume thermal zones asynchronously")Reported-by Mauricio Faria de Oliveira <[email protected]>Closes https://lore.kernel.org/linux-pm/20260324-thermal-core-uaf-init_delayed_work-v1-1-6611ae76a8a1@igalia.com/ [1] open hereSigned-off-by Rafael J. Wysocki <[email protected]>Quoted 7 lines
---
v1 -> v2: Return
-ENOMEMfromthermal_init()when the workqueue allocation
fails (Sashiko)Lukasz, Daniel, I'm quite confident about this patch, but I would appreciate your feedback.
My apologies for delay...
Quoted 80 lines
--- drivers/thermal/thermal_core.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)drivers/thermal/thermal_core.c +16
-4@@ -42,6 +42,8 @@ static struct thermal_governor *def_govestatic bool thermal_pm_suspended; +static struct workqueue_struct *thermal_wq__ro_after_init ; +/* * Governor section: set of functions to handle thermal governors * @@ -314,7 +316,7 @@ static void thermal_zone_device_set_pollif (delay >HZ ) delay =round_jiffies_relative (delay); -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue, delay); +mod_delayed_work (thermal_wq, &tz->poll_queue, delay); }static void thermal_zone_recheck (struct thermal_zone_device *tz,int error)@@ -1795,6 +1797,10 @@ static void thermal_zone_device_resume(sguard (thermal_zone)(tz); +/* If the thermal zone is going away, there's nothing to do. */ +if (tz->state &TZ_STATE_FLAG_EXIT ) +return ; + tz->state &= ~(TZ_STATE_FLAG_SUSPENDED |TZ_STATE_FLAG_RESUMING );thermal_debug_tz_resume (tz);@@ -1825,6 +1831,9 @@ static void thermal_zone_pm_prepare(stru } tz->state |=TZ_STATE_FLAG_SUSPENDED ; + +/* Prevent new work from getting to the workqueue subsequently. */ +cancel_delayed_work (&tz->poll_queue); }static void thermal_pm_notify_prepare (void )@@ -1843,8 +1852,6 @@ static void thermal_zone_pm_complete(str {guard (thermal_zone)(tz); -cancel_delayed_work (&tz->poll_queue); -reinit_completion (&tz->resume); tz->state |=TZ_STATE_FLAG_RESUMING ;@@ -1854,7 +1861,7 @@ static void thermal_zone_pm_complete(str*/ INIT_DELAYED_WORK (&tz->poll_queue, thermal_zone_device_resume);/* Queue up the work without a delay. */ -mod_delayed_work (system_freezable_power_efficient_wq, &tz->poll_queue,0 ); +mod_delayed_work (thermal_wq, &tz->poll_queue,0 ); }static void thermal_pm_notify_complete (void )@@ -1877,6 +1884,11 @@ static int thermal_pm_notify(struct noticase PM_RESTORE_PREPARE :case PM_SUSPEND_PREPARE :thermal_pm_notify_prepare (); +/* +* Allow any leftover thermal work items already on the +* worqueue to complete so they don't get in the way later. +*/ +flush_workqueue (thermal_wq);break ;case PM_POST_HIBERNATION :case PM_POST_RESTORE :@@ -1909,9 +1921,16 @@ static int __init thermal_init(void)if (result) goto error;Quoted 5 lines
drivers/thermal/thermal_core.c static int __init thermal_init(void) · line 1925
Quoted 2 lines
drivers/thermal/thermal_core.c +2
-0+ thermal_wq =alloc_workqueue ("thermal_events" , +WQ_FREEZABLE |WQ_POWER_EFFICIENT |WQ_PERCPU ,0 );Why we need those workqueue instances per-cpu?
That's to limit the changes in this fix to what's needed to address the problem at hand.
The previously used workqueue was also per-CPU and freezable.
Quoted 8 lines
Quoted 4 lines
It isn't a concurrent work, only one CPU can manage the cpufreq domain.
I would drop that flag and allow to have single instance and migrate between cpus (to not wake up the one which queued the work).
I have a separate patch for that on top of this one, will post it later.
Let's avoid mixing fixes with changes that aren't directly related to them.
Thanks Rafael for clarification. With that in mind feel free to add: