Skip to contentSkip to conversation

cpufreq: unnecesary double free in pcc_cpufreq_do_osc

PATCH RESEND In review Davidlohr Bueso 4 participants 10 messages Sep 30, 2010 – Oct 1, 2010 x86

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 cpufreq: unnecesary double free in pcc_cpufreq_do_osc

x86, cpufreq: avoid an unnecessary double free when finished in pcc_cpufreq_do_osc()

There is no need to fall through the out_free label thus saving a kfree call.

---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
index 994230d..db7dc35 100644
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	if (!(supported & 0x1))
 		return -ENODEV;
 
+	return ret;
+
 out_free:
 	kfree(output.pointer);
 	return ret;
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 19 lines · Davidlohr Bueso

x86, cpufreq: avoid an unnecessary double free when finished in pcc_cpufreq_do_osc()

There is no need to fall through the out_free label thus saving a kfree call.

---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |    2 ++

 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
index 994230d..db7dc35 100644
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
        if (!(supported & 0x1))
                return -ENODEV;
 
+       return ret;
+
Quoted 3 lines

 out_free:
       kfree(output.pointer);
       return ret;

Where is the double free here? I can't see it. I do see memory leaks happening in error handling paths of pcc_cpufreq_do_osc() which makes me think we need something like the attached patch.

From 8b18a51077c9e5b36d54a5e45f9058eb3aae2477 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <[email protected]>
Date: Thu, 30 Sep 2010 22:57:33 +0300
Subject: [PATCH] x86, cpufreq: Fix pcc_cpufreq_do_osc() memory leaks

If acpi_evaluate_object() function call doesn't fail, we must kfree() output.buffer before returning from pcc_cpufreq_do_osc().

---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +12 -6
index 994230d..4f6f679 100644
@@ -368,16 +368,22 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 		return -ENODEV;
 
 	out_obj = output.pointer;
-	if (out_obj->type != ACPI_TYPE_BUFFER)
-		return -ENODEV;
+	if (out_obj->type != ACPI_TYPE_BUFFER) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 	errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
-	if (errors)
-		return -ENODEV;
+	if (errors) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 	supported = *((u32 *)(out_obj->buffer.pointer + 4));
-	if (!(supported & 0x1))
-		return -ENODEV;
+	if (!(supported & 0x1)) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 out_free:
 	kfree(output.pointer);
Signature · 1 line
1.5.6.4
#6 lore raw
In reply to Davidlohr Bueso
Quoted 14 lines · Davidlohr Bueso

x86, cpufreq: avoid an unnecessary double free when finished in pcc_cpufreq_do_osc()

There is no need to fall through the out_free label thus saving a kfree call.

---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +0 -0
index 994230d..db7dc35 100644

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c static int __init pcc_cpufreq_do_osc(acpi_handle *handle) · line 386

Quoted 8 lines
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	if (!(supported & 0x1))
 		return -ENODEV;
 
+	return ret;
+
 out_free:
 	kfree(output.pointer);
 	return ret;

Why is the kfree() unnecessary? acpi_evaluate_object() will allocate a new output.pointer if it returns 0, so at this point in the code you would now be leaking the buffer.

Instead, it would probably be better to fix the existing memory leaks in that function where we return -ENODEV without going to out_free when output.length is non-zero.

#2 lore raw
In reply to Pekka Enberg
Quoted 5 lines · Pekka Enberg
Quoted 5 lines

+++ b/arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +0 -0
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
        if (!(supported & 0x1))
                return -ENODEV;
 
Quoted 9 lines
Quoted 5 lines
(quoted diff) +2 -0
+       return ret;
+

 out_free:
       kfree(output.pointer);
       return ret;

Where is the double free here? I can't see it. I do see memory leaks happening in error handling paths of pcc_cpufreq_do_osc() which makes me think we need something like the attached patch.

I think Dave's patch is correct. There's a kfree(output.pointer) at line 359. If we fall all the way through without hitting any of the return -ENODEVs, we end up doing a 2nd kfree in the out_free:

Dave

#5 lore raw
In reply to Pekka Enberg
Quoted 9 lines · Pekka Enberg

From 8b18a51077c9e5b36d54a5e45f9058eb3aae2477 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <[email protected]>
Date: Thu, 30 Sep 2010 22:57:33 +0300
Subject: [PATCH] x86, cpufreq: Fix pcc_cpufreq_do_osc() memory leaks

If acpi_evaluate_object() function call doesn't fail, we must kfree() output.buffer before returning from pcc_cpufreq_do_osc().

Quoted 40 lines
---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +12 -6
index 994230d..4f6f679 100644
@@ -368,16 +368,22 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 		return -ENODEV;
 
 	out_obj = output.pointer;
-	if (out_obj->type != ACPI_TYPE_BUFFER)
-		return -ENODEV;
+	if (out_obj->type != ACPI_TYPE_BUFFER) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 	errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
-	if (errors)
-		return -ENODEV;
+	if (errors) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 	supported = *((u32 *)(out_obj->buffer.pointer + 4));
-	if (!(supported & 0x1))
-		return -ENODEV;
+	if (!(supported & 0x1)) {
+		ret = -ENODEV;
+		goto out_free;
+	}
 
 out_free:
 	kfree(output.pointer);
Signature · 1 line
1.5.6.4
#7 lore raw
In reply to David Rientjes
Quoted 17 lines · David Rientjes

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c static int __init pcc_cpufreq_do_osc(acpi_handle *handle) · line 386

Quoted 12 lines

index 994230d..db7dc35 100644

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	if (!(supported & 0x1))
 		return -ENODEV;
 
+	return ret;
+
 out_free:
 	kfree(output.pointer);
 	return ret;

Why is the kfree() unnecessary? acpi_evaluate_object() will allocate a new output.pointer if it returns 0, so at this point in the code you would now be leaking the buffer.

Quoted 3 lines

Instead, it would probably be better to fix the existing memory leaks in that function where we return -ENODEV without going to out_free when output.length is non-zero.

Oh, *duh*. That was subtle.
You, and Pekka are of course correct. I'll merge up Pekka's patch.

thanks,

Dave

#9 lore raw
In reply to David Rientjes
Quoted 29 lines · David Rientjes
Quoted 14 lines · Davidlohr Bueso

x86, cpufreq: avoid an unnecessary double free when finished in pcc_cpufreq_do_osc()

There is no need to fall through the out_free label thus saving a kfree call.

---
 arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +0 -0
index 994230d..db7dc35 100644

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c static int __init pcc_cpufreq_do_osc(acpi_handle *handle) · line 386

Quoted 8 lines
arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	if (!(supported & 0x1))
 		return -ENODEV;
 
+	return ret;
+
 out_free:
 	kfree(output.pointer);
 	return ret;

Why is the kfree() unnecessary? acpi_evaluate_object() will allocate a new output.pointer if it returns 0, so at this point in the code you would now be leaking the buffer.

Quoted 4 lines

Instead, it would probably be better to fix the existing memory leaks in that function where we return -ENODEV without going to out_free when output.length is non-zero.

Silly me, I must be asleep still. Pekka's attached patch is just right. Sorry for the noise.

Davidlohr

#8 lore raw
In reply to Dave Jones
Quoted 18 lines · Dave Jones
Quoted 17 lines · David Rientjes

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c static int __init pcc_cpufreq_do_osc(acpi_handle *handle) · line 386

Quoted 12 lines

index 994230d..db7dc35 100644

arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c +2 -0
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	if (!(supported & 0x1))
 		return -ENODEV;
 
+	return ret;
+
 out_free:
 	kfree(output.pointer);
 	return ret;

Why is the kfree() unnecessary? acpi_evaluate_object() will allocate a new output.pointer if it returns 0, so at this point in the code you would now be leaking the buffer.

Quoted 5 lines
Quoted 3 lines

Instead, it would probably be better to fix the existing memory leaks in that function where we return -ENODEV without going to out_free when output.length is non-zero.

Oh, *duh*. That was subtle.

Yep, anyways, at least we got the correct "fix".

Quoted 7 lines

You, and Pekka are of course correct. I'll merge up Pekka's patch.

thanks,

Dave

#3 lore raw
In reply to Dave Jones

Hi Dave,

Quoted 12 lines · Dave Jones
Quoted 11 lines · Pekka Enberg
Quoted 10 lines

+++ b/arch/x86/kernel/cpu/cpufreq/pcc-cpufreq.c
@@ -379,6 +379,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)

         if (!(supported&  0x1))
                 return -ENODEV;

+ return ret;
+
out_free:

         kfree(output.pointer);
         return ret;
Quoted 7 lines
Quoted 3 lines

Where is the double free here? I can't see it. I do see memory leaks happening in error handling paths of pcc_cpufreq_do_osc() which makes me think we need something like the attached patch.

I think Dave's patch is correct. There's a kfree(output.pointer) at line 359. If we fall all the way through without hitting any of the return -ENODEVs, we end up doing a 2nd kfree in the out_free:

There's a second call to acpi_evaluate_object() which takes "output" as its argument and allocates more memory.

#4 lore raw
Quoted 6 lines · Pekka Enberg
Quoted 3 lines

I think Dave's patch is correct. There's a kfree(output.pointer) at line 359. If we fall all the way through without hitting any of the return -ENODEVs, we end up doing a 2nd kfree in the out_free:

There's a second call to acpi_evaluate_object() which takes "output" as its argument and allocates more memory.

yup. I merged your patch. after it's soaked in -next for a day, I'll push it to Linus. (Looks obviously correct, but just being extra careful given where we are in the release cycle)

Dave