mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cppc_cpufreq: Fix possible null pointer dereference
@ 2024-04-05  9:40 Aleksandr Mishin
  2024-04-05 10:38 ` Mukesh Ojha
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Aleksandr Mishin @ 2024-04-05  9:40 UTC (permalink / raw)
  To: Ionela Voinescu
  Cc: Aleksandr Mishin, Rafael J. Wysocki, Viresh Kumar, linux-pm,
	linux-kernel, lvc-project

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>
---
 drivers/cpufreq/cppc_cpufreq.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index 64420d9cfd1e..5f7e04e8497b 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -741,6 +741,9 @@ 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);
+	if (!policy)
+		return -ENODEV;
+
 	struct cppc_cpudata *cpu_data = policy->driver_data;
 	u64 delivered_perf;
 	int ret;
@@ -822,6 +825,9 @@ 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);
+	if (!policy)
+		return -ENODEV;
+
 	struct cppc_cpudata *cpu_data = policy->driver_data;
 	u64 desired_perf;
 	int ret;
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cppc_cpufreq: Fix possible null pointer dereference
  2024-04-05  9:40 [PATCH] cppc_cpufreq: Fix possible null pointer dereference Aleksandr Mishin
@ 2024-04-05 10:38 ` Mukesh Ojha
  2024-04-05 10:43 ` Viresh Kumar
  2024-04-08  9:35 ` [PATCH v2] " Aleksandr Mishin
  2 siblings, 0 replies; 5+ messages in thread
From: Mukesh Ojha @ 2024-04-05 10:38 UTC (permalink / raw)
  To: Aleksandr Mishin, Ionela Voinescu
  Cc: Rafael J. Wysocki, Viresh Kumar, linux-pm, linux-kernel, lvc-project



On 4/5/2024 3:10 PM, Aleksandr Mishin wrote:
> 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>
> ---
>   drivers/cpufreq/cppc_cpufreq.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
> index 64420d9cfd1e..5f7e04e8497b 100644
> --- a/drivers/cpufreq/cppc_cpufreq.c
> +++ b/drivers/cpufreq/cppc_cpufreq.c
> @@ -741,6 +741,9 @@ 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);
> +	if (!policy)
> +		return -ENODEV;
> +

You should be doing this after all variable declaration, somewhere

...
...
struct cppc_cpudata *cpu_data;
u64 delivered_perf;
int ret;

if (!policy)
	return -ENODEV;

cpu_data = policy->driver_data;
..
..

>   	struct cppc_cpudata *cpu_data = policy->driver_data;
>   	u64 delivered_perf;
>   	int ret;
> @@ -822,6 +825,9 @@ 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);

same here.

> +	if (!policy)
> +		return -ENODEV;
> +
>   	struct cppc_cpudata *cpu_data = policy->driver_data;
>   	u64 desired_perf;
>   	int ret;

-Mukesh

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cppc_cpufreq: Fix possible null pointer dereference
  2024-04-05  9:40 [PATCH] cppc_cpufreq: Fix possible null pointer dereference Aleksandr Mishin
  2024-04-05 10:38 ` Mukesh Ojha
@ 2024-04-05 10:43 ` Viresh Kumar
  2024-04-08  9:35 ` [PATCH v2] " Aleksandr Mishin
  2 siblings, 0 replies; 5+ messages in thread
From: Viresh Kumar @ 2024-04-05 10:43 UTC (permalink / raw)
  To: Aleksandr Mishin
  Cc: Ionela Voinescu, Rafael J. Wysocki, linux-pm, linux-kernel, lvc-project

On 05-04-24, 12:40, Aleksandr Mishin wrote:
> 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>
> ---
>  drivers/cpufreq/cppc_cpufreq.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
> index 64420d9cfd1e..5f7e04e8497b 100644
> --- a/drivers/cpufreq/cppc_cpufreq.c
> +++ b/drivers/cpufreq/cppc_cpufreq.c
> @@ -741,6 +741,9 @@ 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);
> +	if (!policy)
> +		return -ENODEV;
> +
>  	struct cppc_cpudata *cpu_data = policy->driver_data;
>  	u64 delivered_perf;
>  	int ret;
> @@ -822,6 +825,9 @@ 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);
> +	if (!policy)
> +		return -ENODEV;
> +
>  	struct cppc_cpudata *cpu_data = policy->driver_data;
>  	u64 desired_perf;
>  	int ret;

Does this compile fine ?

-- 
viresh

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] cppc_cpufreq: Fix possible null pointer dereference
  2024-04-05  9:40 [PATCH] cppc_cpufreq: Fix possible null pointer dereference Aleksandr Mishin
  2024-04-05 10:38 ` Mukesh Ojha
  2024-04-05 10:43 ` Viresh Kumar
@ 2024-04-08  9:35 ` Aleksandr Mishin
  2024-04-19  6:39   ` Viresh Kumar
  2 siblings, 1 reply; 5+ messages in thread
From: Aleksandr Mishin @ 2024-04-08  9:35 UTC (permalink / raw)
  To: Ionela Voinescu
  Cc: Aleksandr Mishin, Rafael J. Wysocki, Viresh Kumar, linux-pm,
	linux-kernel, lvc-project

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>
---
v2: Fix mixed declarations

 drivers/cpufreq/cppc_cpufreq.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index 64420d9cfd1e..15f1d41920a3 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -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);
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] cppc_cpufreq: Fix possible null pointer dereference
  2024-04-08  9:35 ` [PATCH v2] " Aleksandr Mishin
@ 2024-04-19  6:39   ` Viresh Kumar
  0 siblings, 0 replies; 5+ messages in thread
From: Viresh Kumar @ 2024-04-19  6:39 UTC (permalink / raw)
  To: Aleksandr Mishin
  Cc: Ionela Voinescu, Rafael J. Wysocki, linux-pm, linux-kernel, lvc-project

On 08-04-24, 12:35, Aleksandr Mishin wrote:
> 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>
> ---
> v2: Fix mixed declarations
> 
>  drivers/cpufreq/cppc_cpufreq.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)

Applied. Thanks.

-- 
viresh

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-19  6:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-05  9:40 [PATCH] cppc_cpufreq: Fix possible null pointer dereference Aleksandr Mishin
2024-04-05 10:38 ` Mukesh Ojha
2024-04-05 10:43 ` Viresh Kumar
2024-04-08  9:35 ` [PATCH v2] " Aleksandr Mishin
2024-04-19  6:39   ` Viresh Kumar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®