Skip to contentSkip to conversation

cpufreq: s3c: avoid use after free issue in xxx_cpufreq_reboot_notifier_evt()

PATCH Superseded [email protected] 2 participants 6 messages Dec 28, 2019 – Jan 7, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: s3c: avoid use after free issue in xxx_cpufreq_reboot_notifier_evt()

From: chenqiwu <[email protected]>

There is a potential UAF issue in xxx_cpufreq_reboot_notifier_evt() that the cpufreq policy of cpu0 has been released before using it. So we should make a judgement to avoid it.

---
 drivers/cpufreq/s3c2416-cpufreq.c | 11 ++++++++++-
 drivers/cpufreq/s5pv210-cpufreq.c | 10 +++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
drivers/cpufreq/s3c2416-cpufreq.c +10 -1
index 1069103..0f576ba 100644
@@ -304,6 +304,7 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 {
 	struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
 	int ret;
+	struct cpufreq_policy policy;
 
 	mutex_lock(&cpufreq_lock);
 
@@ -318,7 +319,15 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 	 */
 	if (s3c_freq->is_dvs) {
 		pr_debug("cpufreq: leave dvs on reboot\n");
-		ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
+
+		memset(&policy, 0, sizeof(policy));
+		ret = cpufreq_get_policy(&policy, 0);
+		if (ret < 0) {
+			pr_debug("cpufreq: get no policy for cpu0\n");
+			return NOTIFY_BAD;
+		}
+
+		ret = cpufreq_driver_target(&policy, FREQ_SLEEP, 0);
 		if (ret < 0)
 			return NOTIFY_BAD;
 	}
drivers/cpufreq/s5pv210-cpufreq.c +9 -1
index 5d10030..d99b4b1 100644
@@ -555,8 +555,16 @@ static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
 						 unsigned long event, void *ptr)
 {
 	int ret;
+	struct cpufreq_policy *policy;
 
-	ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
+	policy = cpufreq_cpu_get(0);
+	if (!policy) {
+		pr_debug("cpufreq: get no policy for cpu0\n");
+		return NOTIFY_BAD;
+	}
+
+	ret = cpufreq_driver_target(policy, SLEEP_FREQ, 0);
+	cpufreq_cpu_put(policy);
 	if (ret < 0)
 		return NOTIFY_BAD;
 
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 5 lines · On 28-12-19, 14:43, [email protected] wrote:

From: chenqiwu <[email protected]>

There is a potential UAF issue in xxx_cpufreq_reboot_notifier_evt() that the cpufreq policy of cpu0 has been released before using it. So we should make a judgement to avoid it.

There is no UAF problem here, but that we do cpufreq_cpu_get() with a corresponding cpufreq_cpu_put().

Quoted 21 lines
---
 drivers/cpufreq/s3c2416-cpufreq.c | 11 ++++++++++-
 drivers/cpufreq/s5pv210-cpufreq.c | 10 +++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
index 1069103..0f576ba 100644
@@ -304,6 +304,7 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 {
 	struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
 	int ret;
+	struct cpufreq_policy policy;
 
 	mutex_lock(&cpufreq_lock);
 
@@ -318,7 +319,15 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 	 */
 	if (s3c_freq->is_dvs) {

drivers/cpufreq/s3c2416-cpufreq.c static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this, · line 329

Quoted 10 lines
drivers/cpufreq/s3c2416-cpufreq.c +8 -1
 		pr_debug("cpufreq: leave dvs on reboot\n");
-		ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
+
+		memset(&policy, 0, sizeof(policy));
+		ret = cpufreq_get_policy(&policy, 0);
+		if (ret < 0) {
+			pr_debug("cpufreq: get no policy for cpu0\n");
+			return NOTIFY_BAD;
+		}
+

This doesn't make sense to me, why don't you do cpufreq_cpu_get() and put() instead ?

Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
+		ret = cpufreq_driver_target(&policy, FREQ_SLEEP, 0);
 		if (ret < 0)
 			return NOTIFY_BAD;
 	}
drivers/cpufreq/s5pv210-cpufreq.c +1 -0
index 5d10030..d99b4b1 100644
@@ -555,8 +555,16 @@ static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
 						 unsigned long event, void *ptr)
 {
 	int ret;
+	struct cpufreq_policy *policy;

drivers/cpufreq/s5pv210-cpufreq.c static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this, · line 567

Quoted 10 lines
drivers/cpufreq/s5pv210-cpufreq.c +8 -1
 
-	ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
+	policy = cpufreq_cpu_get(0);
+	if (!policy) {
+		pr_debug("cpufreq: get no policy for cpu0\n");
+		return NOTIFY_BAD;
+	}
+
+	ret = cpufreq_driver_target(policy, SLEEP_FREQ, 0);
+	cpufreq_cpu_put(policy);

Like what is done here.

Also add a blank line here.

Quoted 5 lines
drivers/cpufreq/s5pv210-cpufreq.c +0 -0
 	if (ret < 0)
 		return NOTIFY_BAD;
 
Signature · 1 line
1.9.1
Signature · 1 line
viresh
author
#2 lore raw
Quoted 28 lines · Viresh Kumar
Quoted 5 lines · On 28-12-19, 14:43, [email protected] wrote:

From: chenqiwu <[email protected]>

There is a potential UAF issue in xxx_cpufreq_reboot_notifier_evt() that the cpufreq policy of cpu0 has been released before using it. So we should make a judgement to avoid it.

There is no UAF problem here, but that we do cpufreq_cpu_get() with a corresponding cpufreq_cpu_put().

Quoted 18 lines
---
 drivers/cpufreq/s3c2416-cpufreq.c | 11 ++++++++++-
 drivers/cpufreq/s5pv210-cpufreq.c | 10 +++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
index 1069103..0f576ba 100644
@@ -304,6 +304,7 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 {
 	struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
 	int ret;
+	struct cpufreq_policy policy;
 
 	mutex_lock(&cpufreq_lock);
 
Quoted 17 lines

drivers/cpufreq/s3c2416-cpufreq.c static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this, · line 329

Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +8 -1
@@ -318,7 +319,15 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 	 */
 	if (s3c_freq->is_dvs) {
 		pr_debug("cpufreq: leave dvs on reboot\n");
-		ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
+
+		memset(&policy, 0, sizeof(policy));
+		ret = cpufreq_get_policy(&policy, 0);
+		if (ret < 0) {
+			pr_debug("cpufreq: get no policy for cpu0\n");
+			return NOTIFY_BAD;
+		}
+

This doesn't make sense to me, why don't you do cpufreq_cpu_get() and put() instead ?

Hi viresh,
I can't explain which approach is better, but I think both approaches are effective for the situation.
By the way, there is a possibility that the cpu0 hotplug thread will call cpufreq_policy_free() to free cpufreq policy if cpu0 hotplug failed. I think there should be a judgement to avoid this UAF risk if necessary, or we just do panic if cpu0's cpufreq policy is free.

Quoted 26 lines
Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
+		ret = cpufreq_driver_target(&policy, FREQ_SLEEP, 0);
 		if (ret < 0)
 			return NOTIFY_BAD;
 	}
drivers/cpufreq/s5pv210-cpufreq.c +1 -0
index 5d10030..d99b4b1 100644
@@ -555,8 +555,16 @@ static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
 						 unsigned long event, void *ptr)
 {
 	int ret;
+	struct cpufreq_policy *policy;

drivers/cpufreq/s5pv210-cpufreq.c static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this, · line 567

Quoted 10 lines
drivers/cpufreq/s5pv210-cpufreq.c +8 -1
 
-	ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
+	policy = cpufreq_cpu_get(0);
+	if (!policy) {
+		pr_debug("cpufreq: get no policy for cpu0\n");
+		return NOTIFY_BAD;
+	}
+
+	ret = cpufreq_driver_target(policy, SLEEP_FREQ, 0);
+	cpufreq_cpu_put(policy);

Like what is done here.

Quoted 10 lines

Also add a blank line here.

Quoted 5 lines
drivers/cpufreq/s5pv210-cpufreq.c +0 -0
 	if (ret < 0)
 		return NOTIFY_BAD;
 
Signature · 1 line
1.9.1
Signature · 1 line
viresh

Thanks for your review!
Qiwu

maintainer
#3 lore raw
Quoted 46 lines · chenqiwu
Quoted 28 lines · Viresh Kumar
Quoted 5 lines · On 28-12-19, 14:43, [email protected] wrote:

From: chenqiwu <[email protected]>

There is a potential UAF issue in xxx_cpufreq_reboot_notifier_evt() that the cpufreq policy of cpu0 has been released before using it. So we should make a judgement to avoid it.

There is no UAF problem here, but that we do cpufreq_cpu_get() with a corresponding cpufreq_cpu_put().

Quoted 18 lines
---
 drivers/cpufreq/s3c2416-cpufreq.c | 11 ++++++++++-
 drivers/cpufreq/s5pv210-cpufreq.c | 10 +++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
index 1069103..0f576ba 100644
@@ -304,6 +304,7 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 {
 	struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
 	int ret;
+	struct cpufreq_policy policy;
 
 	mutex_lock(&cpufreq_lock);
 
Quoted 17 lines

drivers/cpufreq/s3c2416-cpufreq.c static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this, · line 329

Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +8 -1
@@ -318,7 +319,15 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 	 */
 	if (s3c_freq->is_dvs) {
 		pr_debug("cpufreq: leave dvs on reboot\n");
-		ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
+
+		memset(&policy, 0, sizeof(policy));
+		ret = cpufreq_get_policy(&policy, 0);
+		if (ret < 0) {
+			pr_debug("cpufreq: get no policy for cpu0\n");
+			return NOTIFY_BAD;
+		}
+

This doesn't make sense to me, why don't you do cpufreq_cpu_get() and put() instead ?

Quoted 3 lines

Hi viresh,
I can't explain which approach is better, but I think both approaches are effective for the situation.

The second one is better as it doesn't make copy of the policy, but rather just increments the counter.

Quoted 4 lines

By the way, there is a possibility that the cpu0 hotplug thread will call cpufreq_policy_free() to free cpufreq policy if cpu0 hotplug failed. I think there should be a judgement to avoid this UAF risk if necessary, or we just do panic if cpu0's cpufreq policy is free.

I think there are enough locks in place to avoid such issues and they shouldn't happen.

Quoted 39 lines
Quoted 26 lines
Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
+		ret = cpufreq_driver_target(&policy, FREQ_SLEEP, 0);
 		if (ret < 0)
 			return NOTIFY_BAD;
 	}
drivers/cpufreq/s5pv210-cpufreq.c +1 -0
index 5d10030..d99b4b1 100644
@@ -555,8 +555,16 @@ static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
 						 unsigned long event, void *ptr)
 {
 	int ret;
+	struct cpufreq_policy *policy;

drivers/cpufreq/s5pv210-cpufreq.c static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this, · line 567

Quoted 10 lines
drivers/cpufreq/s5pv210-cpufreq.c +8 -1
 
-	ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
+	policy = cpufreq_cpu_get(0);
+	if (!policy) {
+		pr_debug("cpufreq: get no policy for cpu0\n");
+		return NOTIFY_BAD;
+	}
+
+	ret = cpufreq_driver_target(policy, SLEEP_FREQ, 0);
+	cpufreq_cpu_put(policy);

Like what is done here.

Quoted 10 lines

Also add a blank line here.

Quoted 5 lines
drivers/cpufreq/s5pv210-cpufreq.c +0 -0
 	if (ret < 0)
 		return NOTIFY_BAD;
 
Signature · 1 line
1.9.1
Signature · 1 line
viresh

Thanks for your review!
Qiwu

Signature · 1 line
viresh
author
#4 lore raw
Quoted 54 lines · Viresh Kumar
Quoted 46 lines · chenqiwu
Quoted 28 lines · Viresh Kumar
Quoted 5 lines · On 28-12-19, 14:43, [email protected] wrote:

From: chenqiwu <[email protected]>

There is a potential UAF issue in xxx_cpufreq_reboot_notifier_evt() that the cpufreq policy of cpu0 has been released before using it. So we should make a judgement to avoid it.

There is no UAF problem here, but that we do cpufreq_cpu_get() with a corresponding cpufreq_cpu_put().

Quoted 18 lines
---
 drivers/cpufreq/s3c2416-cpufreq.c | 11 ++++++++++-
 drivers/cpufreq/s5pv210-cpufreq.c | 10 +++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
index 1069103..0f576ba 100644
@@ -304,6 +304,7 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 {
 	struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
 	int ret;
+	struct cpufreq_policy policy;
 
 	mutex_lock(&cpufreq_lock);
 
Quoted 17 lines

drivers/cpufreq/s3c2416-cpufreq.c static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this, · line 329

Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +8 -1
@@ -318,7 +319,15 @@ static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
 	 */
 	if (s3c_freq->is_dvs) {
 		pr_debug("cpufreq: leave dvs on reboot\n");
-		ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
+
+		memset(&policy, 0, sizeof(policy));
+		ret = cpufreq_get_policy(&policy, 0);
+		if (ret < 0) {
+			pr_debug("cpufreq: get no policy for cpu0\n");
+			return NOTIFY_BAD;
+		}
+

This doesn't make sense to me, why don't you do cpufreq_cpu_get() and put() instead ?

Quoted 3 lines

Hi viresh,
I can't explain which approach is better, but I think both approaches are effective for the situation.

The second one is better as it doesn't make copy of the policy, but rather just increments the counter.

Quoted 8 lines
Quoted 4 lines

By the way, there is a possibility that the cpu0 hotplug thread will call cpufreq_policy_free() to free cpufreq policy if cpu0 hotplug failed. I think there should be a judgement to avoid this UAF risk if necessary, or we just do panic if cpu0's cpufreq policy is free.

I think there are enough locks in place to avoid such issues and they shouldn't happen.

Hhh..I don't agree this, since the cpufreq policy of cpu0 may have been released before such UAF issue happenning.
By the way, why not get invaild cpufreq policy of another cpu but only cpu0 here?

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 13 lines
drivers/cpufreq/s3c2416-cpufreq.c +1 -0
+		ret = cpufreq_driver_target(&policy, FREQ_SLEEP, 0);
 		if (ret < 0)
 			return NOTIFY_BAD;
 	}
drivers/cpufreq/s5pv210-cpufreq.c +1 -0
index 5d10030..d99b4b1 100644
@@ -555,8 +555,16 @@ static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this,
 						 unsigned long event, void *ptr)
 {
 	int ret;
+	struct cpufreq_policy *policy;

drivers/cpufreq/s5pv210-cpufreq.c static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block *this, · line 567

Quoted 10 lines
drivers/cpufreq/s5pv210-cpufreq.c +8 -1
 
-	ret = cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ, 0);
+	policy = cpufreq_cpu_get(0);
+	if (!policy) {
+		pr_debug("cpufreq: get no policy for cpu0\n");
+		return NOTIFY_BAD;
+	}
+
+	ret = cpufreq_driver_target(policy, SLEEP_FREQ, 0);
+	cpufreq_cpu_put(policy);

Like what is done here.

Also add a blank line here.

Quoted 3 lines
drivers/cpufreq/s5pv210-cpufreq.c +0 -0
 	if (ret < 0)
 		return NOTIFY_BAD;
 
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Signature · 1 line
1.9.1
Signature · 1 line
viresh

Thanks for your review!
Qiwu

Signature · 1 line
viresh

Thanks!
Qiwu

maintainer
#5 lore raw
Quoted 2 lines · chenqiwu

Hhh..I don't agree this, since the cpufreq policy of cpu0 may have been released before such UAF issue happenning.

That won't happen if you do cpufreq_cpu_get(), isn't it ?

Quoted 2 lines

By the way, why not get invaild cpufreq policy of another cpu but only cpu0 here?

Probably because this platform has a single cpufreq policy which covers all the CPUs and so it doesn't really matter which CPU you use to get the policy.

Signature · 1 line
viresh