Skip to contentSkip to conversation

cpufreq: pcc: fix use-after-free and double free in _OSC evaluation

PATCH In review Hongyan Xu 2 participants 2 messages May 13 CPU frequency scaling framework

lore
Share & tools
lore

Hongyan Xu

author
#0 lore raw

PATCH 1/1 cpufreq: pcc: fix use-after-free and double free in _OSC evaluation

pcc_cpufreq_do_osc() uses a two-phase _OSC evaluation and frees the output buffer returned by the first acpi_evaluate_object() call before reusing the same acpi_buffer in the second call.

However, output.pointer and output.length are not reset after the first kfree(). That can make the second acpi_evaluate_object() treat the stale metadata as a caller-provided buffer and write into freed memory. The shared out_free path can then free the same pointer again.

Reset the output buffer state after the first kfree() so ACPICA allocates a fresh buffer for the second _OSC evaluation.

Fixes 0f1d683fb35d ("[CPUFREQ] Processor Clocking Control interface driver")

Issue found using a prototype static analysis tool and confirmed by code review.

---
 drivers/cpufreq/pcc-cpufreq.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/cpufreq/pcc-cpufreq.c +2 -0
index ac2e90a65f0c..a355ec4f3dd4 100644
@@ -352,6 +352,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
 	}
 
 	kfree(output.pointer);
+	output.pointer = NULL;
+	output.length = ACPI_ALLOCATE_BUFFER;
 	capabilities[0] = 0x0;
 	capabilities[1] = 0x1;
 
Signature · 1 line
2.50.1.windows.1
#1 lore raw
Quoted 27 lines · Hongyan Xu

pcc_cpufreq_do_osc() uses a two-phase _OSC evaluation and frees the output buffer returned by the first acpi_evaluate_object() call before reusing the same acpi_buffer in the second call.

However, output.pointer and output.length are not reset after the first kfree(). That can make the second acpi_evaluate_object() treat the stale metadata as a caller-provided buffer and write into freed memory. The shared out_free path can then free the same pointer again.

Reset the output buffer state after the first kfree() so ACPICA allocates a fresh buffer for the second _OSC evaluation.

Fixes 0f1d683fb35d ("[CPUFREQ] Processor Clocking Control interface driver")

Issue found using a prototype static analysis tool and confirmed by code review.

---
  drivers/cpufreq/pcc-cpufreq.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/cpufreq/pcc-cpufreq.c +0 -0
index ac2e90a65f0c..a355ec4f3dd4 100644

drivers/cpufreq/pcc-cpufreq.c static int __init pcc_cpufreq_do_osc(acpi_handle *handle) · line 356

Quoted 5 lines
drivers/cpufreq/pcc-cpufreq.c +2 -0
@@ -352,6 +352,8 @@ static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
  	}
 
  	kfree(output.pointer);
+	output.pointer = NULL;
+	output.length = ACPI_ALLOCATE_BUFFER;

Hi Hongyan, Slavin,

Thanks for the patch and for looking into this issue.

There is already a similar patch under discussion:
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

You may want to review that thread and see if your findings or testing can be contributed there.

Quoted 3 lines
drivers/cpufreq/pcc-cpufreq.c +0 -0
  	capabilities[0] = 0x0;
  	capabilities[1] = 0x1;
 
Signature · 2 lines
Thx and BRs,
Zhongqiu Han