Skip to contentSkip to conversation

cpufreq/powernv: Fix use-after-free

PATCH In review Oliver O'Halloran 5 participants 7 messages Feb 6, 2020 – Mar 17, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 cpufreq/powernv: Fix use-after-free

The cpufreq driver has a use-after-free that we can hit if:

a) There's an OCC message pending when the notifier is registered, and
b) The cpufreq driver fails to register with the core.

When a) occurs the notifier schedules a workqueue item to handle the message. The backing work_struct is located on chips[].throttle and when b) happens we clean up by freeing the array. Once we get to the (now free) queued item and the kernel crashes.

Cc Vaidyanathan Srinivasan <[email protected]>
Fixes c5e29ea ("cpufreq: powernv: Fix bugs in powernv_cpufreq_{init/exit}")
---
 drivers/cpufreq/powernv-cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpufreq/powernv-cpufreq.c +6 -0
index 56f4bc0..1806b1d 100644
@@ -1080,6 +1080,12 @@ static int init_chip_info(void)
 
 static inline void clean_chip_info(void)
 {
+	int i;
+
+	/* flush any pending work items */
+	if (chips)
+		for (i = 0; i < nr_chips; i++)
+			cancel_work_sync(&chips[i].throttle);
 	kfree(chips);
 }
 
Signature · 1 line
2.9.5
#1 lore raw

PATCH 2/2 cpufreq/powernv: Fix unsafe notifiers

The PowerNV cpufreq driver registers two notifiers: one to catch throttle messages from the OCC and one to bump the CPU frequency back to normal before a reboot. Both require the cpufreq driver to be registered in order to function since the notifier callbacks use various cpufreq_*() functions.

Right now we register both notifiers before we've initialised the driver. This seems to work, but we should head off any protential problems by registering the notifiers after the driver is initialised.

Cc Vaidyanathan Srinivasan <[email protected]>
---
 drivers/cpufreq/powernv-cpufreq.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/cpufreq/powernv-cpufreq.c +5 -6
index 1806b1d..03798c4 100644
@@ -1114,9 +1114,6 @@ static int __init powernv_cpufreq_init(void)
 	if (rc)
 		goto out;
 
-	register_reboot_notifier(&powernv_cpufreq_reboot_nb);
-	opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
-
 	if (powernv_pstate_info.wof_enabled)
 		powernv_cpufreq_driver.boost_enabled = true;
 	else
@@ -1125,15 +1122,17 @@ static int __init powernv_cpufreq_init(void)
 	rc = cpufreq_register_driver(&powernv_cpufreq_driver);
 	if (rc) {
 		pr_info("Failed to register the cpufreq driver (%d)\n", rc);
-		goto cleanup_notifiers;
+		goto cleanup;
 	}
 
 	if (powernv_pstate_info.wof_enabled)
 		cpufreq_enable_boost_support();
 
+	register_reboot_notifier(&powernv_cpufreq_reboot_nb);
+	opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
+
 	return 0;
-cleanup_notifiers:
-	unregister_all_notifiers();
+cleanup:
 	clean_chip_info();
 out:
 	pr_info("Platform driver disabled. System does not support PState control\n");
Signature · 1 line
2.9.5
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] cpufreq/powernv: Fix unsafe notifiers

Quoted 11 lines · Oliver O'Halloran

The PowerNV cpufreq driver registers two notifiers: one to catch throttle messages from the OCC and one to bump the CPU frequency back to normal before a reboot. Both require the cpufreq driver to be registered in order to function since the notifier callbacks use various cpufreq_*() functions.

Right now we register both notifiers before we've initialised the driver. This seems to work, but we should head off any protential problems by registering the notifiers after the driver is initialised.

Cc Vaidyanathan Srinivasan <[email protected]>
Quoted 42 lines
---
 drivers/cpufreq/powernv-cpufreq.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/cpufreq/powernv-cpufreq.c +5 -6
index 1806b1d..03798c4 100644
@@ -1114,9 +1114,6 @@ static int __init powernv_cpufreq_init(void)
 	if (rc)
 		goto out;
 
-	register_reboot_notifier(&powernv_cpufreq_reboot_nb);
-	opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
-
 	if (powernv_pstate_info.wof_enabled)
 		powernv_cpufreq_driver.boost_enabled = true;
 	else
@@ -1125,15 +1122,17 @@ static int __init powernv_cpufreq_init(void)
 	rc = cpufreq_register_driver(&powernv_cpufreq_driver);
 	if (rc) {
 		pr_info("Failed to register the cpufreq driver (%d)\n", rc);
-		goto cleanup_notifiers;
+		goto cleanup;
 	}
 
 	if (powernv_pstate_info.wof_enabled)
 		cpufreq_enable_boost_support();
 
+	register_reboot_notifier(&powernv_cpufreq_reboot_nb);
+	opal_message_notifier_register(OPAL_MSG_OCC, &powernv_cpufreq_opal_nb);
+
 	return 0;
-cleanup_notifiers:
-	unregister_all_notifiers();
+cleanup:
 	clean_chip_info();
 out:
 	pr_info("Platform driver disabled. System does not support PState control\n");
Signature · 1 line
2.9.5
Reviewed-by on v1 1/2
#3 lore raw
In reply to Oliver O'Halloran
Quoted 5 lines · Oliver O'Halloran

The cpufreq driver has a use-after-free that we can hit if:

a) There's an OCC message pending when the notifier is registered, and
b) The cpufreq driver fails to register with the core.

Quoted 8 lines

When a) occurs the notifier schedules a workqueue item to handle the message. The backing work_struct is located on chips[].throttle and when b) happens we clean up by freeing the array. Once we get to the (now free) queued item and the kernel crashes.

Cc Vaidyanathan Srinivasan <[email protected]>
Fixes c5e29ea ("cpufreq: powernv: Fix bugs in powernv_cpufreq_{init/exit}")

Thanks for this fix Oliver.

Quoted 24 lines
---
 drivers/cpufreq/powernv-cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpufreq/powernv-cpufreq.c +6 -0
index 56f4bc0..1806b1d 100644
@@ -1080,6 +1080,12 @@ static int init_chip_info(void)
 
 static inline void clean_chip_info(void)
 {
+	int i;
+
+	/* flush any pending work items */
+	if (chips)
+		for (i = 0; i < nr_chips; i++)
+			cancel_work_sync(&chips[i].throttle);
 	kfree(chips);
 }
 
Signature · 1 line
2.9.5
#4 lore raw
In reply to Oliver O'Halloran
Quoted 5 lines · Oliver O'Halloran

The cpufreq driver has a use-after-free that we can hit if:

a) There's an OCC message pending when the notifier is registered, and
b) The cpufreq driver fails to register with the core.

Quoted 8 lines

When a) occurs the notifier schedules a workqueue item to handle the message. The backing work_struct is located on chips[].throttle and when b) happens we clean up by freeing the array. Once we get to the (now free) queued item and the kernel crashes.

Cc Vaidyanathan Srinivasan <[email protected]>
Fixes c5e29ea ("cpufreq: powernv: Fix bugs in powernv_cpufreq_{init/exit}")

This sounds like it needs to go to stable.

Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]             IBM Australia Limited
#5 lore raw
Quoted 11 lines · Andrew Donnellan
Quoted 10 lines · Oliver O'Halloran

The cpufreq driver has a use-after-free that we can hit if:

a) There's an OCC message pending when the notifier is registered, and
b) The cpufreq driver fails to register with the core.

When a) occurs the notifier schedules a workqueue item to handle the message. The backing work_struct is located on chips[].throttle and when b) happens we clean up by freeing the array. Once we get to the (now free) queued item and the kernel crashes.

Quoted 5 lines
Quoted 3 lines
Cc Vaidyanathan Srinivasan <[email protected]>
Fixes c5e29ea ("cpufreq: powernv: Fix bugs in powernv_cpufreq_{init/exit}")

This sounds like it needs to go to stable.

I tagged it for stable when applying.

cheers

#6 lore raw
In reply to Oliver O'Halloran
Quoted 5 lines · Oliver O'Halloran

The cpufreq driver has a use-after-free that we can hit if:

a) There's an OCC message pending when the notifier is registered, and
b) The cpufreq driver fails to register with the core.

Quoted 8 lines

When a) occurs the notifier schedules a workqueue item to handle the message. The backing work_struct is located on chips[].throttle and when b) happens we clean up by freeing the array. Once we get to the (now free) queued item and the kernel crashes.

Cc Vaidyanathan Srinivasan <[email protected]>
Fixes c5e29ea ("cpufreq: powernv: Fix bugs in powernv_cpufreq_{init/exit}")

Series applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/d0a72efac89d1c35ac55197895201b7b94c5e6ef

cheers