Skip to contentSkip to conversation

PM: EM: allocate the cpumask for every perf domain

PATCH Applied Karl Mehltretter 3 participants 4 messages Sep 10 – Oct 22

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: EM: allocate the cpumask for every perf domain

em_create_pd() allocates space for the trailing cpus[] flexible array only for CPU devices. A performance domain registered for another device therefore has no storage for its cpumask.

All performance domains are added to em_pd_list. The netlink interface does not distinguish CPU and non-CPU domains when it calls cpumask_weight() or iterates over pd->cpus. A request for a non-CPU domain reads cpumask_size() bytes beyond the allocation and can report bits from the following memory as CPU attributes.

KASAN reports slab-out-of-bounds reads for both dump and by-ID requests against a synthetic non-CPU performance domain.

GET_PERF_DOMAINS does not require GENL_ADMIN_PERM, so an unprivileged request can reach this path when a GPU or another devfreq device has registered an energy model.

Allocate the cpumask for non-CPU domains too. The zeroed mask remains empty, as required for the documented unused field.

Fixes d8eef0453132 ("PM: EM: Implement em_nl_get_pds_doit()")
Assisted-by LLM
---
 kernel/power/energy_model.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/power/energy_model.c +3 -1
index e610cf8e9a06..fd07c564c201 100644
@@ -439,7 +439,9 @@ static int em_create_pd(struct device *dev, int nr_states,
 
 		cpumask_copy(em_span_cpus(pd), cpus);
 	} else {
-		pd = kzalloc_obj(*pd);
+		/* Readers expect pd->cpus to exist and remain empty. */
+		pd = kzalloc_flex(*pd, cpus,
+				  BITS_TO_LONGS(large_cpumask_bits));
 		if (!pd)
 			return -ENOMEM;
 	}
Signature · 1 line
2.53.0
#1 lore raw
Quoted 7 lines · Karl Mehltretter

KASAN reports slab-out-of-bounds reads for both dump and by-ID requests against a synthetic non-CPU performance domain.

GET_PERF_DOMAINS does not require GENL_ADMIN_PERM, so an unprivileged request can reach this path when a GPU or another devfreq device has registered an energy model.

Quoted 7 lines

Allocate the cpumask for non-CPU domains too. The zeroed mask remains empty, as required for the documented unused field.

Fixes d8eef0453132 ("PM: EM: Implement em_nl_get_pds_doit()")
Assisted-by LLM

Friendly ping on this patch

Thanks,
Karl

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Karl Mehltretter

Hi Karl,

Quoted 33 lines · Karl Mehltretter

em_create_pd() allocates space for the trailing cpus[] flexible array only for CPU devices. A performance domain registered for another device therefore has no storage for its cpumask.

All performance domains are added to em_pd_list. The netlink interface does not distinguish CPU and non-CPU domains when it calls cpumask_weight() or iterates over pd->cpus. A request for a non-CPU domain reads cpumask_size() bytes beyond the allocation and can report bits from the following memory as CPU attributes.

KASAN reports slab-out-of-bounds reads for both dump and by-ID requests against a synthetic non-CPU performance domain.

GET_PERF_DOMAINS does not require GENL_ADMIN_PERM, so an unprivileged request can reach this path when a GPU or another devfreq device has registered an energy model.

Allocate the cpumask for non-CPU domains too. The zeroed mask remains empty, as required for the documented unused field.

Fixes d8eef0453132 ("PM: EM: Implement em_nl_get_pds_doit()")
Assisted-by LLM
---
  kernel/power/energy_model.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
kernel/power/energy_model.c +0 -0
index e610cf8e9a06..fd07c564c201 100644

kernel/power/energy_model.c static int em_create_pd(struct device *dev, int nr_states, · line 447

Quoted 10 lines
kernel/power/energy_model.c +3 -1
@@ -439,7 +439,9 @@ static int em_create_pd(struct device *dev, int nr_states,
 
  		cpumask_copy(em_span_cpus(pd), cpus);
  	} else {
-		pd = kzalloc_obj(*pd);
+		/* Readers expect pd->cpus to exist and remain empty. */
+		pd = kzalloc_flex(*pd, cpus,
+				  BITS_TO_LONGS(large_cpumask_bits));
  		if (!pd)
  			return -ENOMEM;
  	}

my apologies for the delay.
The patch looks good, thanks for catching this!

maintainer Applied
#3 lore raw
Quoted 51 lines · Lukasz Luba

Hi Karl,

Quoted 33 lines · Karl Mehltretter

em_create_pd() allocates space for the trailing cpus[] flexible array only for CPU devices. A performance domain registered for another device therefore has no storage for its cpumask.

All performance domains are added to em_pd_list. The netlink interface does not distinguish CPU and non-CPU domains when it calls cpumask_weight() or iterates over pd->cpus. A request for a non-CPU domain reads cpumask_size() bytes beyond the allocation and can report bits from the following memory as CPU attributes.

KASAN reports slab-out-of-bounds reads for both dump and by-ID requests against a synthetic non-CPU performance domain.

GET_PERF_DOMAINS does not require GENL_ADMIN_PERM, so an unprivileged request can reach this path when a GPU or another devfreq device has registered an energy model.

Allocate the cpumask for non-CPU domains too. The zeroed mask remains empty, as required for the documented unused field.

Fixes d8eef0453132 ("PM: EM: Implement em_nl_get_pds_doit()")
Assisted-by LLM
---
  kernel/power/energy_model.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
kernel/power/energy_model.c +0 -0
index e610cf8e9a06..fd07c564c201 100644

kernel/power/energy_model.c static int em_create_pd(struct device *dev, int nr_states, · line 447

Quoted 10 lines
kernel/power/energy_model.c +3 -1
@@ -439,7 +439,9 @@ static int em_create_pd(struct device *dev, int nr_states,
 
              cpumask_copy(em_span_cpus(pd), cpus);
      } else {
-             pd = kzalloc_obj(*pd);
+             /* Readers expect pd->cpus to exist and remain empty. */
+             pd = kzalloc_flex(*pd, cpus,
+                               BITS_TO_LONGS(large_cpumask_bits));
              if (!pd)
                      return -ENOMEM;
      }

my apologies for the delay.
The patch looks good, thanks for catching this!

Quoted 1 line

Applied as 7.4-rc material, thanks!