mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] cpupower: fix use-after-free and stale limits in frequency-set
@ 2026-11-15 10:03 Lukas Beckmann
  2026-11-16 23:30 ` Shuah Khan
  0 siblings, 1 reply; 3+ messages in thread
From: Lukas Beckmann @ 2026-11-15 10:03 UTC (permalink / raw)
  To: Thomas Renninger, Shuah Khan, John B. Wyatt IV, John Kacur,
	Dominik Brodowski
  Cc: linux-pm, linux-kernel, stable, Lukas Beckmann

do_new_policy() directly fills the unset fields of new_pol from the
current CPU's policy. This is problematic because do_new_policy() is
called with the same new_pol for multiple CPUs by do_one_cpu() in the
slow path. This results in stale values in new_pol being applied
(starting from the second CPU that should be changed), and even a
use-after-free (for the governor string).

Fix this by building the target policy from new_pol and the current
policy in do_new_policy() instead of filling the missing values into
new_pol.

Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
Cc: stable@vger.kernel.org
Signed-off-by: Lukas Beckmann <lbckmnn@mailbox.org>
---
Testing and minimal reproducer:

Running the command

    ./cpupower frequency-set --max 2500MHz --min 2500MHz

with ASAN reports the use-after-free before, but not after the patch.

Run the following commands:

    # set CPU 0 to a different maximum than CPU 1
    echo 2000000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
    echo 3000000 > /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq

    # Change min and governor (but not max) of CPU 0 and 1
    ./cpupower -c 0,1 frequency-set --min 1GHz -g performance

    # See if max got changed
    cat /sys/devices/system/cpu/cpu[0,1]/cpufreq/scaling_max_freq

This shows that the stale maximum of CPU0 is used for CPU1 before the
patch, but not after the patch.
---
Changes in v2:
- use a shorter changelog and testing section
- build temp_pol from new_pol and cur_pol instead of copying new_pol
- Link to v1: https://patch.msgid.link/20261112-cpupower-freq-set-use-after-free-v1-1-4e0034927b3f@mailbox.org
---
 tools/power/cpupower/utils/cpufreq-set.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/tools/power/cpupower/utils/cpufreq-set.c b/tools/power/cpupower/utils/cpufreq-set.c
index 06cd4b280132..674e3aabd31d 100644
--- a/tools/power/cpupower/utils/cpufreq-set.c
+++ b/tools/power/cpupower/utils/cpufreq-set.c
@@ -141,9 +141,10 @@ static unsigned long string_to_frequency(const char *str)
 	}
 }
 
-static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
+static int do_new_policy(unsigned int cpu, const struct cpufreq_policy *new_pol)
 {
 	struct cpufreq_policy *cur_pol = cpufreq_get_policy(cpu);
+	struct cpufreq_policy temp_pol;
 	int ret;
 
 	if (!cur_pol) {
@@ -151,16 +152,22 @@ static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
 		return -EINVAL;
 	}
 
-	if (!new_pol->min)
-		new_pol->min = cur_pol->min;
+	if (new_pol->min)
+		temp_pol.min = new_pol->min;
+	else
+		temp_pol.min = cur_pol->min;
 
-	if (!new_pol->max)
-		new_pol->max = cur_pol->max;
+	if (new_pol->max)
+		temp_pol.max = new_pol->max;
+	else
+		temp_pol.max = cur_pol->max;
 
-	if (!new_pol->governor)
-		new_pol->governor = cur_pol->governor;
+	if (new_pol->governor)
+		temp_pol.governor = new_pol->governor;
+	else
+		temp_pol.governor = cur_pol->governor;
 
-	ret = cpufreq_set_policy(cpu, new_pol);
+	ret = cpufreq_set_policy(cpu, &temp_pol);
 
 	cpufreq_put_policy(cur_pol);
 

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261110-cpupower-freq-set-use-after-free-30e337fadcbf

Best regards,
--  
Lukas Beckmann <lbckmnn@mailbox.org>


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

* Re: [PATCH v2] cpupower: fix use-after-free and stale limits in frequency-set
  2026-11-15 10:03 [PATCH v2] cpupower: fix use-after-free and stale limits in frequency-set Lukas Beckmann
@ 2026-11-16 23:30 ` Shuah Khan
  2026-11-17 16:11   ` Lukas Beckmann
  0 siblings, 1 reply; 3+ messages in thread
From: Shuah Khan @ 2026-11-16 23:30 UTC (permalink / raw)
  To: Lukas Beckmann, Thomas Renninger, Shuah Khan, John B. Wyatt IV,
	John Kacur, Dominik Brodowski
  Cc: linux-pm, linux-kernel, stable, Shuah Khan

On 11/15/26 03:03, Lukas Beckmann wrote:
> do_new_policy() directly fills the unset fields of new_pol from the
> current CPU's policy. This is problematic because do_new_policy() is
> called with the same new_pol for multiple CPUs by do_one_cpu() in the
> slow path. This results in stale values in new_pol being applied
> (starting from the second CPU that should be changed), and even a
> use-after-free (for the governor string).
> 
> Fix this by building the target policy from new_pol and the current
> policy in do_new_policy() instead of filling the missing values into
> new_pol.
> 
> Fixes: 7fe2f6399a84 ("cpupowerutils - cpufrequtils extended with quite some features")
> Cc: stable@vger.kernel.org
> Signed-off-by: Lukas Beckmann <lbckmnn@mailbox.org>
> ---
> Testing and minimal reproducer:
> 
> Running the command
> 
>      ./cpupower frequency-set --max 2500MHz --min 2500MHz
> 
> with ASAN reports the use-after-free before, but not after the patch.
> 
> Run the following commands:
> 
>      # set CPU 0 to a different maximum than CPU 1
>      echo 2000000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
>      echo 3000000 > /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
> 
>      # Change min and governor (but not max) of CPU 0 and 1
>      ./cpupower -c 0,1 frequency-set --min 1GHz -g performance
> 
>      # See if max got changed
>      cat /sys/devices/system/cpu/cpu[0,1]/cpufreq/scaling_max_freq
> 
> This shows that the stale maximum of CPU0 is used for CPU1 before the
> patch, but not after the patch.
> ---
> Changes in v2:
> - use a shorter changelog and testing section
> - build temp_pol from new_pol and cur_pol instead of copying new_pol
> - Link to v1: https://patch.msgid.link/20261112-cpupower-freq-set-use-after-free-v1-1-4e0034927b3f@mailbox.org
> ---
>   tools/power/cpupower/utils/cpufreq-set.c | 23 +++++++++++++++--------
>   1 file changed, 15 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/power/cpupower/utils/cpufreq-set.c b/tools/power/cpupower/utils/cpufreq-set.c
> index 06cd4b280132..674e3aabd31d 100644
> --- a/tools/power/cpupower/utils/cpufreq-set.c
> +++ b/tools/power/cpupower/utils/cpufreq-set.c
> @@ -141,9 +141,10 @@ static unsigned long string_to_frequency(const char *str)
>   	}
>   }
>   
> -static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
> +static int do_new_policy(unsigned int cpu, const struct cpufreq_policy *new_pol)
>   {
>   	struct cpufreq_policy *cur_pol = cpufreq_get_policy(cpu);
> +	struct cpufreq_policy temp_pol;
>   	int ret;
>   
>   	if (!cur_pol) {
> @@ -151,16 +152,22 @@ static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
>   		return -EINVAL;
>   	}
>   
> -	if (!new_pol->min)
> -		new_pol->min = cur_pol->min;

Why can't you leave this logic as is and set temp_pol fieds,
  if (!new_pol->min)
	temp_pol = cur_pol->min;
  else
	temp_pol.min = new_pol->min;
> +	if (new_pol->min)
> +		temp_pol.min = new_pol->min;
> +	else
> +		temp_pol.min = cur_pol->min;
>   
> -	if (!new_pol->max)
> -		new_pol->max = cur_pol->max;
> +	if (new_pol->max)
> +		temp_pol.max = new_pol->max;
> +	else
> +		temp_pol.max = cur_pol->max;
>   
> -	if (!new_pol->governor)
> -		new_pol->governor = cur_pol->governor;
> +	if (new_pol->governor)
> +		temp_pol.governor = new_pol->governor;
> +	else
> +		temp_pol.governor = cur_pol->governor;

Same for all of the above. Make just the minimum changes
to fix the problem instead of changing the logic


>   
> -	ret = cpufreq_set_policy(cpu, new_pol);
> +	ret = cpufreq_set_policy(cpu, &temp_pol);
>   
>   	cpufreq_put_policy(cur_pol);
>   
> 
thanks,
-- Shuah

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

* Re: [PATCH v2] cpupower: fix use-after-free and stale limits in frequency-set
  2026-11-16 23:30 ` Shuah Khan
@ 2026-11-17 16:11   ` Lukas Beckmann
  0 siblings, 0 replies; 3+ messages in thread
From: Lukas Beckmann @ 2026-11-17 16:11 UTC (permalink / raw)
  To: Shuah Khan
  Cc: Thomas Renninger, Shuah Khan, John B. Wyatt IV, John Kacur,
	Dominik Brodowski, linux-pm, linux-kernel, stable

On Mon, Nov 16, 2026 at 04:30:10PM -0700, Shuah Khan wrote:
> On 11/15/26 03:03, Lukas Beckmann wrote:
> > @@ -141,9 +141,10 @@ static unsigned long string_to_frequency(const char *str)
> >   	}
> >   }
> > -static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
> > +static int do_new_policy(unsigned int cpu, const struct cpufreq_policy *new_pol)
> >   {
> >   	struct cpufreq_policy *cur_pol = cpufreq_get_policy(cpu);
> > +	struct cpufreq_policy temp_pol;
> >   	int ret;
> >   	if (!cur_pol) {
> > @@ -151,16 +152,22 @@ static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
> >   		return -EINVAL;
> >   	}
> > -	if (!new_pol->min)
> > -		new_pol->min = cur_pol->min;
> 
> Why can't you leave this logic as is and set temp_pol fieds,
>  if (!new_pol->min)
> 	temp_pol = cur_pol->min;
>  else
> 	temp_pol.min = new_pol->min;
[...]
> 
> Same for all of the above. Make just the minimum changes
> to fix the problem instead of changing the logic
> 

I did it this way because I found it easier to read. I will keep the
original conditions in V3 to keep the diff smaller.

Thanks,
Lukas

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

end of thread, other threads:[~2026-11-17 16:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 10:03 [PATCH v2] cpupower: fix use-after-free and stale limits in frequency-set Lukas Beckmann
2026-11-16 23:30 ` Shuah Khan
2026-11-17 16:11   ` Lukas Beckmann

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®