mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate()
@ 2025-04-05  5:54 Henry Martin
  2025-04-07  9:24 ` [PATCH] " Markus Elfring
  2025-04-07 11:30 ` [PATCH v1] " Sudeep Holla
  0 siblings, 2 replies; 5+ messages in thread
From: Henry Martin @ 2025-04-05  5:54 UTC (permalink / raw)
  To: sudeep.holla, cristian.marussi, rafael, viresh.kumar
  Cc: arm-scmi, linux-arm-kernel, linux-pm, linux-kernel, Henry Martin

cpufreq_cpu_get_raw() can return NULL when the target CPU is not present
in the policy->cpus mask. scmi_cpufreq_get_rate() does not check for
this case, which results in a NULL pointer dereference.

Add NULL check after cpufreq_cpu_get_raw() to prevent this issue.

Fixes: 99d6bdf33877 ("cpufreq: add support for CPU DVFS based on SCMI message protocol")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 drivers/cpufreq/scmi-cpufreq.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c
index c310aeebc8f3..c735f39245bf 100644
--- a/drivers/cpufreq/scmi-cpufreq.c
+++ b/drivers/cpufreq/scmi-cpufreq.c
@@ -37,11 +37,17 @@ static struct cpufreq_driver scmi_cpufreq_driver;
 
 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
 {
-	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
-	struct scmi_data *priv = policy->driver_data;
+	struct cpufreq_policy *policy;
+	struct scmi_data *priv;
 	unsigned long rate;
 	int ret;
 
+	policy = cpufreq_cpu_get_raw(cpu);
+	if (!policy)
+		return 0;
+
+	priv = policy->driver_data;
+
 	ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
 	if (ret)
 		return 0;
-- 
2.34.1


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

* Re: [PATCH] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate()
  2025-04-05  5:54 [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate() Henry Martin
@ 2025-04-07  9:24 ` Markus Elfring
  2025-04-07 11:30 ` [PATCH v1] " Sudeep Holla
  1 sibling, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2025-04-07  9:24 UTC (permalink / raw)
  To: Henry Martin, linux-pm, arm-scmi, linux-arm-kernel
  Cc: LKML, Cristian Marussi, Rafael J. Wysocki, Sudeep Holla, Viresh Kumar

…
> Add NULL check after cpufreq_cpu_get_raw() to prevent this issue.

Can any other summary phrase variant become more desirable accordingly?

Regards,
Markus

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

* Re: [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate()
  2025-04-05  5:54 [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate() Henry Martin
  2025-04-07  9:24 ` [PATCH] " Markus Elfring
@ 2025-04-07 11:30 ` Sudeep Holla
  2025-04-08  5:55   ` Viresh Kumar
  1 sibling, 1 reply; 5+ messages in thread
From: Sudeep Holla @ 2025-04-07 11:30 UTC (permalink / raw)
  To: Henry Martin
  Cc: cristian.marussi, rafael, viresh.kumar, Sudeep Holla, arm-scmi,
	linux-arm-kernel, linux-pm, linux-kernel

On Sat, Apr 05, 2025 at 01:54:47PM +0800, Henry Martin wrote:
> cpufreq_cpu_get_raw() can return NULL when the target CPU is not present
> in the policy->cpus mask. scmi_cpufreq_get_rate() does not check for
> this case, which results in a NULL pointer dereference.
> 
> Add NULL check after cpufreq_cpu_get_raw() to prevent this issue.
> 
> Fixes: 99d6bdf33877 ("cpufreq: add support for CPU DVFS based on SCMI message protocol")
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---
>  drivers/cpufreq/scmi-cpufreq.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c
> index c310aeebc8f3..c735f39245bf 100644
> --- a/drivers/cpufreq/scmi-cpufreq.c
> +++ b/drivers/cpufreq/scmi-cpufreq.c
> @@ -37,11 +37,17 @@ static struct cpufreq_driver scmi_cpufreq_driver;
>  
>  static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
>  {
> -	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
> -	struct scmi_data *priv = policy->driver_data;
> +	struct cpufreq_policy *policy;
> +	struct scmi_data *priv;
>  	unsigned long rate;
>  	int ret;
>  
> +	policy = cpufreq_cpu_get_raw(cpu);
> +	if (!policy)

How about `if (unlikely(!policy))` instead ?

With that you can add :

Reviewed-by: Sudeep Holla <sudeep.holla@arm.com>

Both comment and review applies for scpi-cpufreq.c

-- 
Regards,
Sudeep

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

* Re: [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate()
  2025-04-07 11:30 ` [PATCH v1] " Sudeep Holla
@ 2025-04-08  5:55   ` Viresh Kumar
  2025-04-08 14:38     ` henry martin
  0 siblings, 1 reply; 5+ messages in thread
From: Viresh Kumar @ 2025-04-08  5:55 UTC (permalink / raw)
  To: Sudeep Holla
  Cc: Henry Martin, cristian.marussi, rafael, arm-scmi,
	linux-arm-kernel, linux-pm, linux-kernel

On 07-04-25, 12:30, Sudeep Holla wrote:
> On Sat, Apr 05, 2025 at 01:54:47PM +0800, Henry Martin wrote:
> > cpufreq_cpu_get_raw() can return NULL when the target CPU is not present
> > in the policy->cpus mask. scmi_cpufreq_get_rate() does not check for
> > this case, which results in a NULL pointer dereference.
> > 
> > Add NULL check after cpufreq_cpu_get_raw() to prevent this issue.
> > 
> > Fixes: 99d6bdf33877 ("cpufreq: add support for CPU DVFS based on SCMI message protocol")
> > Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> > ---
> >  drivers/cpufreq/scmi-cpufreq.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c
> > index c310aeebc8f3..c735f39245bf 100644
> > --- a/drivers/cpufreq/scmi-cpufreq.c
> > +++ b/drivers/cpufreq/scmi-cpufreq.c
> > @@ -37,11 +37,17 @@ static struct cpufreq_driver scmi_cpufreq_driver;
> >  
> >  static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
> >  {
> > -	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
> > -	struct scmi_data *priv = policy->driver_data;
> > +	struct cpufreq_policy *policy;
> > +	struct scmi_data *priv;
> >  	unsigned long rate;
> >  	int ret;
> >  
> > +	policy = cpufreq_cpu_get_raw(cpu);
> > +	if (!policy)
> 
> How about `if (unlikely(!policy))` instead ?

Henry, this change applies to all the patches you have sent. Also please send
them as a single series, as they are related changes.

-- 
viresh

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

* Re: [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate()
  2025-04-08  5:55   ` Viresh Kumar
@ 2025-04-08 14:38     ` henry martin
  0 siblings, 0 replies; 5+ messages in thread
From: henry martin @ 2025-04-08 14:38 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Sudeep Holla, cristian.marussi, rafael, arm-scmi,
	linux-arm-kernel, linux-pm, linux-kernel

> How about `if (unlikely(!policy))` instead ?
Agreed, unlikely() makes sense here since the NULL check is for an
exceptional case.
I'll update all relevant patches accordingly.

> Henry, this change applies to all the patches you have sent. Also please send
> them as a single series, as they are related changes.
Noted. I’ll consolidate the patches into a single series with a proper
cover letter and
resend them shortly.

Viresh Kumar <viresh.kumar@linaro.org> 于2025年4月8日周二 13:55写道:
>
> On 07-04-25, 12:30, Sudeep Holla wrote:
> > On Sat, Apr 05, 2025 at 01:54:47PM +0800, Henry Martin wrote:
> > > cpufreq_cpu_get_raw() can return NULL when the target CPU is not present
> > > in the policy->cpus mask. scmi_cpufreq_get_rate() does not check for
> > > this case, which results in a NULL pointer dereference.
> > >
> > > Add NULL check after cpufreq_cpu_get_raw() to prevent this issue.
> > >
> > > Fixes: 99d6bdf33877 ("cpufreq: add support for CPU DVFS based on SCMI message protocol")
> > > Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> > > ---
> > >  drivers/cpufreq/scmi-cpufreq.c | 10 ++++++++--
> > >  1 file changed, 8 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c
> > > index c310aeebc8f3..c735f39245bf 100644
> > > --- a/drivers/cpufreq/scmi-cpufreq.c
> > > +++ b/drivers/cpufreq/scmi-cpufreq.c
> > > @@ -37,11 +37,17 @@ static struct cpufreq_driver scmi_cpufreq_driver;
> > >
> > >  static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
> > >  {
> > > -   struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
> > > -   struct scmi_data *priv = policy->driver_data;
> > > +   struct cpufreq_policy *policy;
> > > +   struct scmi_data *priv;
> > >     unsigned long rate;
> > >     int ret;
> > >
> > > +   policy = cpufreq_cpu_get_raw(cpu);
> > > +   if (!policy)
> >
> > How about `if (unlikely(!policy))` instead ?
>
> Henry, this change applies to all the patches you have sent. Also please send
> them as a single series, as they are related changes.
>
> --
> viresh

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

end of thread, other threads:[~2025-04-08 14:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-05  5:54 [PATCH v1] cpufreq: scmi: Fix null-ptr-deref in scmi_cpufreq_get_rate() Henry Martin
2025-04-07  9:24 ` [PATCH] " Markus Elfring
2025-04-07 11:30 ` [PATCH v1] " Sudeep Holla
2025-04-08  5:55   ` Viresh Kumar
2025-04-08 14:38     ` henry martin

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®