cpufreq: acpi-cpufreq: add NULL check for acpi_perf_data before freeing
PATCH 1/1 cpufreq: acpi-cpufreq: add NULL check for acpi_perf_data before freeing
From: Li RongQing <[email protected]>
If alloc_percpu() fails in acpi_cpufreq_early_init(), acpi_perf_data is NULL. Calling free_acpi_perf_data() will execute per_cpu_ptr(NULL, i), which does not return NULL but an offset pointer. Dereferencing ->shared_cpu_map on this invalid pointer leads to a kernel crash.
Fix these issues by adding a NULL check at the start of free_acpi_perf_data() and setting acpi_perf_data to NULL after freeing.
--- drivers/cpufreq/acpi-cpufreq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpufreq/acpi-cpufreq.c +4 -1
@@ -517,11 +517,14 @@ static void free_acpi_perf_data(void) {unsigned int i; -/* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ +if (!acpi_perf_data) +return ; +for_each_possible_cpu (i)free_cpumask_var (per_cpu_ptr (acpi_perf_data, i) ->shared_cpu_map);free_percpu (acpi_perf_data); + acpi_perf_data =NULL ; }static int cpufreq_boost_down_prep (unsigned int cpu)
Signature · 1 line
2.9.4
PATCH 1/1 cpufreq: acpi-cpufreq: Initialize cmd.val in drv_read()
From: Li RongQing <[email protected]>
In drv_read(), struct drv_cmd cmd is allocated on the stack with .val left uninitialized. If smp_call_function_any() fails, do_drv_read() will not run to populate cmd.val. As a result, drv_read() would return uninitialized stack data.
Fix this by explicitly initializing .val to 0 when declaring cmd.
--- drivers/cpufreq/acpi-cpufreq.c | 1 + 1 file changed, 1 insertion(+)
drivers/cpufreq/acpi-cpufreq.c +1 -0
@@ -308,6 +308,7 @@ static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)struct acpi_processor_performance *perf =to_perf_data (data);struct drv_cmd cmd = { .reg = &perf->control_register, + .val =0 , .func.read = data->cpu_freq_read, };int err;
Signature · 1 line
2.9.4
Re: [PATCH] cpufreq: acpi-cpufreq: Initialize cmd.val in drv_read()
Quoted 19 lines · lirongqing
From: Li RongQing <[email protected]>
In
drv_read(), structdrv_cmdcmd is allocated on the stack with .val left uninitialized. Ifsmp_call_function_any()fails,do_drv_read()will not run to populate cmd.val. As a result,drv_read()would return uninitialized stack data.Fix this by explicitly initializing .val to 0 when declaring cmd.
Signed-off-by Li RongQing <[email protected]>--- drivers/cpufreq/acpi-cpufreq.c | 1 + 1 file changed, 1 insertion(+)drivers/cpufreq/acpi-cpufreq.c +0
-0
drivers/cpufreq/acpi-cpufreq.c static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask) · line 311
Quoted 4 lines
drivers/cpufreq/acpi-cpufreq.c +1
-0@@ -308,6 +308,7 @@ static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)struct acpi_processor_performance *perf =to_perf_data (data);struct drv_cmd cmd = { .reg = &perf->control_register, + .val =0 ,
Will the compiler handle this automatically?
Quoted 3 lines
drivers/cpufreq/acpi-cpufreq.c +0
-0.func.read = data->cpu_freq_read, };int err;
Signature · 2 lines
Thx and BRs, Zhongqiu Han
PATCH 1/1 cpufreq: acpi-cpufreq: Using cpufreq_for_each_entry() to iterate in extract_io()
In reply to lirongqingFrom: Li RongQing <[email protected]>
In extract_io(), the loop iterates up to perf->state_count. However, when building policy->freq_table in acpi_cpufreq_cpu_init(), duplicate frequency entries are skipped, making freq_table smaller than perf->state_count.
Iterating perf->state_count times directly over policy->freq_table[i] can result in out-of-bounds array reads. Furthermore, policy->freq_table[i] does not necessarily correspond to perf->states[i], as the original P-state index is stored in freq_table[entry].driver_data.
Fix this by using cpufreq_for_each_entry() to iterate over policy->freq_table, similar to extract_msr().
--- drivers/cpufreq/acpi-cpufreq.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +4 -5
@@ -196,15 +196,14 @@ static int check_amd_hwpstate_cpu(unsigned int cpuid)static unsigned extract_io (struct cpufreq_policy *policy,u32 value) {struct acpi_cpufreq_data *data = policy->driver_data; +struct cpufreq_frequency_table *pos;struct acpi_processor_performance *perf; -int i; perf =to_perf_data (data); -for (i =0 ; i < perf->state_count; i++) { -if (value == perf->states[i].status) -return policy->freq_table[i].frequency; - } +cpufreq_for_each_entry (pos, policy->freq_table) +if (value == perf->states[pos->driver_data].status) +return pos->frequency;return 0 ; }
Signature · 1 line
2.9.4
Re: [PATCH] cpufreq: acpi-cpufreq: Using cpufreq_for_each_entry() to iterate in extract_io()
Quoted 9 lines · lirongqing
From: Li RongQing <[email protected]>
In
extract_io(), the loop iterates up toperf->state_count. However, when buildingpolicy->freq_tableinacpi_cpufreq_cpu_init(), duplicate frequency entries are skipped, makingfreq_tablesmaller thanperf->state_count.Iterating
perf->state_counttimes directly over policy->freq_table[i] can result in out-of-bounds array reads. Furthermore, policy->freq_table[i]
There is no out-of-bounds access. The array has "state_count + 1" elements, so every index in "[0, state_count)" is inside the allocation. right? The changelog looks like a memory-safety fix, which it is not, and that wording alone would get the patch (mis)routed to stable and to CVE bots.
Quoted 2 lines
does not necessarily correspond to perf->states[i], as the original P-state index is stored in freq_table[entry].driver_data.
Yes, the real defect is the index space mismatch.
Might be good to note the side effects of it, AFAICT
freq_table[i] is not perf->states[i] once any _PSS entry has been skipped. The function can therefore return a frequency belonging to a different P-state, or 0 (zeroed tail entries), or CPUFREQ_TABLE_END (~1u) when "i == valid_states", i.e. 0xfffffffe kHz reported as a frequency. That last one is worth spelling out.
Quoted 4 lines
Fix this by using
cpufreq_for_each_entry()to iterate overpolicy->freq_table, similar toextract_msr().
Please add one Fixes tag here as well.
Quoted 18 lines
Signed-off-by Li RongQing <[email protected]>--- drivers/cpufreq/acpi-cpufreq.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/acpi-cpufreq.c +1
-1@@ -196,15 +196,14 @@ static int check_amd_hwpstate_cpu(unsigned int cpuid)static unsigned extract_io (struct cpufreq_policy *policy,u32 value) {struct acpi_cpufreq_data *data = policy->driver_data; +struct cpufreq_frequency_table *pos;struct acpi_processor_performance *perf; -int i; perf =to_perf_data (data);
drivers/cpufreq/acpi-cpufreq.c static int check_amd_hwpstate_cpu(unsigned int cpuid) · line 208
Quoted 10 lines
drivers/cpufreq/acpi-cpufreq.c +3
-4-for (i =0 ; i < perf->state_count; i++) { -if (value == perf->states[i].status) -return policy->freq_table[i].frequency; - } +cpufreq_for_each_entry (pos, policy->freq_table) +if (value == perf->states[pos->driver_data].status) +return pos->frequency;return 0 ; }
One more potential same issue is in func get_cur_freq_on_cpu()
cached_freq = policy->freq_table[to_perf_data(data)->state].frequency;
It is better to fix it as well.
For v2, please use ./scripts/get_maintainer.pl to generate the CC list so [email protected] doesn't get missed. Thanks
Quoted 1 line
Signature · 2 lines
Thx and BRs, Zhongqiu Han
答复: [外部邮件] Re: [PATCH] cpufreq: acpi-cpufreq: Using cpufreq_for_each_entry() to iterate in extract_io()
Quoted 70 lines
Quoted 4 lines
From: Li RongQing <[email protected]>
Quoted 8 lines
In
extract_io(), the loop iterates up toperf->state_count. However, when buildingpolicy->freq_tableinacpi_cpufreq_cpu_init(), duplicate frequency entries are skipped, makingfreq_tablesmaller thanperf->state_count.Iterating
perf->state_counttimes directly over policy->freq_table[i] can result in out-of-bounds array reads. Furthermore, policy->freq_table[i]There is no out-of-bounds access. The array has "
state_count+ 1" elements, so every index in "[0,state_count)" is inside the allocation. right? The changelog looks like a memory-safety fix, which it is not, and that wording alone would get the patch (mis)routed to stable and to CVE bots.Quoted 2 lines
does not necessarily correspond to perf->states[i], as the original P-state index is stored in freq_table[entry].driver_data.
Yes, the real defect is the index space mismatch.
Might be good to note the side effects of it, AFAICT
freq_table[i] is not perf->states[i] once any
_PSSentry has been skipped. The function can therefore return a frequency belonging to a different P-state, or 0 (zeroed tail entries), orCPUFREQ_TABLE_END
(~1u) when "i ==valid_states", i.e.0xfffffffekHz reported as a frequency. That last one is worth spelling out.Quoted 4 lines
Fix this by using
cpufreq_for_each_entry()to iterate overpolicy->freq_table, similar toextract_msr().Please add one Fixes tag here as well.
drivers/cpufreq/acpi-cpufreq.c static int check_amd_hwpstate_cpu(unsigned int
Quoted 10 lines
Signed-off-by Li RongQing <[email protected]>--- drivers/cpufreq/acpi-cpufreq.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)(quoted diff) +0
-0
b/drivers/cpufreq/acpi-cpufreq.cindex 21639d9..87e4923 100644drivers/cpufreq/acpi-cpufreq.c +0
-0@@ -196,15 +196,14 @@ static int check_amd_hwpstate_cpu(unsigned intcpuid)
Quoted 8 lines
drivers/cpufreq/acpi-cpufreq.c +1
-1static unsigned extract_io (struct cpufreq_policy *policy,u32 value) {struct acpi_cpufreq_data *data = policy->driver_data; +struct cpufreq_frequency_table *pos;struct acpi_processor_performance *perf; -int i; perf =to_perf_data (data);drivers/cpufreq/acpi-cpufreq.c static int check_amd_hwpstate_cpu(unsigned int cpuid) · line 208
Quoted 10 lines
drivers/cpufreq/acpi-cpufreq.c +3
-4-for (i =0 ; i < perf->state_count; i++) { -if (value == perf->states[i].status) -return policy->freq_table[i].frequency; - } +cpufreq_for_each_entry (pos, policy->freq_table) +if (value == perf->states[pos->driver_data].status) +return pos->frequency;return 0 ; }One more potential same issue is in func
get_cur_freq_on_cpu()
Quoted 7 lines
cached_freq= policy->freq_table[to_perf_data(data)->state].frequency;It is better to fix it as well.
For v2, please use
./scripts/get_maintainer.plto generate the CC list so [email protected] doesn't get missed. Thanks
I will send v2,thanks
[Li,Rongqing]
Quoted 7 lines
Quoted 2 lines
--
Thx and BRs,
Zhongqiu Han
Quoted 5 lines · lirongqing
From: Li RongQing <[email protected]>
If
alloc_percpu()fails inacpi_cpufreq_early_init(),acpi_perf_datais NULL. Callingfree_acpi_perf_data()will executeper_cpu_ptr(NULL, i), which does not return NULL but an offset pointer.
Hi RongQing,
Could you please confirm if acpi_perf_data() can ever be executed when alloc_percpu() fails?
Quoted 31 lines
Dereferencing ->shared_cpu_map on this invalid pointer leads to a kernel crash.
Fix these issues by adding a NULL check at the start of
free_acpi_perf_data()and settingacpi_perf_datato NULL after freeing.Signed-off-by Li RongQing <[email protected]>--- drivers/cpufreq/acpi-cpufreq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/cpufreq/acpi-cpufreq.c +4
-1@@ -517,11 +517,14 @@ static void free_acpi_perf_data(void) {unsigned int i; -/* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ +if (!acpi_perf_data) +return ; +for_each_possible_cpu (i)free_cpumask_var (per_cpu_ptr (acpi_perf_data, i) ->shared_cpu_map);free_percpu (acpi_perf_data); + acpi_perf_data =NULL ; }static int cpufreq_boost_down_prep (unsigned int cpu)
Signature · 2 lines
Thx and BRs, Zhongqiu Han
Quoted 10 lines · Zhongqiu Han
Quoted 5 lines · lirongqing
From: Li RongQing <[email protected]>
If
alloc_percpu()fails inacpi_cpufreq_early_init(),acpi_perf_datais NULL. Callingfree_acpi_perf_data()will executeper_cpu_ptr(NULL, i), which does not return NULL but an offset pointer.Hi RongQing,
Could you please confirm if
acpi_perf_data()can ever be executed whenalloc_percpu()fails?
Sorry, typo: acpi_perf_data() --> free_acpi_perf_data()
Quoted 33 lines
Quoted 31 lines
Dereferencing ->shared_cpu_map on this invalid pointer leads to a kernel crash.
Fix these issues by adding a NULL check at the start of
free_acpi_perf_data()and settingacpi_perf_datato NULL after freeing.Signed-off-by Li RongQing <[email protected]>---
drivers/cpufreq/acpi-cpufreq.c| 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)drivers/cpufreq/acpi-cpufreq.c +4
-1@@ -517,11 +517,14 @@ static void free_acpi_perf_data(void) { unsigned int i; - /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ + if (!acpi_perf_data) + return; + for_each_possible_cpu(i) free_cpumask_var(per_cpu_ptr(acpi_perf_data, i) ->shared_cpu_map); free_percpu(acpi_perf_data); + acpi_perf_data = NULL; } static int cpufreq_boost_down_prep(unsigned int cpu)
Signature · 2 lines
Thx and BRs, Zhongqiu Han
Quoted 11 lines
Quoted 6 lines
If
alloc_percpu()fails inacpi_cpufreq_early_init(),acpi_perf_datais NULL. Callingfree_acpi_perf_data()will executeper_cpu_ptr(NULL, i), which does not return NULL but an offset pointer.Hi RongQing,
Could you please confirm if
acpi_perf_data()can ever be executed whenalloc_percpu()fails?
You are right, acpi_perf_data() can not be called when alloc_percpu() fails
Please drop this patch
thanks
[Li,Rongqing]
Quoted 38 lines
Quoted 23 lines
Dereferencing ->shared_cpu_map on this invalid pointer leads to a kernel crash.
Fix these issues by adding a NULL check at the start of
free_acpi_perf_data()and settingacpi_perf_datato NULL after freeing.Signed-off-by Li RongQing <[email protected]>--- drivers/cpufreq/acpi-cpufreq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/cpufreq/acpi-cpufreq.c +1
-1@@ -517,11 +517,14 @@ static void free_acpi_perf_data(void) {unsigned int i; -/* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ +if (!acpi_perf_data)drivers/cpufreq/acpi-cpufreq.c static void free_acpi_perf_data(void) · line 530
Quoted 10 lines
drivers/cpufreq/acpi-cpufreq.c +3
-0+return ; +for_each_possible_cpu (i)free_cpumask_var (per_cpu_ptr (acpi_perf_data, i) ->shared_cpu_map);free_percpu (acpi_perf_data); + acpi_perf_data =NULL ; }static int cpufreq_boost_down_prep (unsigned int cpu)--
Thx and BRs,
Zhongqiu Han