cppc_cpufreq: Fix possible null pointer dereference
authorAleksandr Mishin <amishin@t-argos.ru>
Mon, 8 Apr 2024 09:35:36 +0000 (12:35 +0300)
committerViresh Kumar <viresh.kumar@linaro.org>
Fri, 19 Apr 2024 06:29:16 +0000 (11:59 +0530)
cppc_cpufreq_get_rate() and hisi_cppc_cpufreq_get_rate() can be called from
different places with various parameters. So cpufreq_cpu_get() can return
null as 'policy' in some circumstances.
Fix this bug by adding null return check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: a28b2bfc099c ("cppc_cpufreq: replace per-cpu data array with a list")
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
drivers/cpufreq/cppc_cpufreq.c

index 64420d9cfd1ed3cbec1c6a25a7640c4afa875466..15f1d41920a3397b2900a1d210a4ef644774a270 100644 (file)
@@ -741,10 +741,15 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
 {
        struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
        struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
-       struct cppc_cpudata *cpu_data = policy->driver_data;
+       struct cppc_cpudata *cpu_data;
        u64 delivered_perf;
        int ret;
 
+       if (!policy)
+               return -ENODEV;
+
+       cpu_data = policy->driver_data;
+
        cpufreq_cpu_put(policy);
 
        ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
@@ -822,10 +827,15 @@ static struct cpufreq_driver cppc_cpufreq_driver = {
 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 {
        struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
-       struct cppc_cpudata *cpu_data = policy->driver_data;
+       struct cppc_cpudata *cpu_data;
        u64 desired_perf;
        int ret;
 
+       if (!policy)
+               return -ENODEV;
+
+       cpu_data = policy->driver_data;
+
        cpufreq_cpu_put(policy);
 
        ret = cppc_get_desired_perf(cpu, &desired_perf);