Skip to contentSkip to conversation

PM: EM: Fix NULL pointer dereference in em_create_pd()

PATCH In review Malaya Kumar Rout 3 participants 3 messages Jan 15 – Jan 21

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

PATCH 1/1 PM: EM: Fix NULL pointer dereference in em_create_pd()

The get_cpu_device() function can return NULL if the CPU device is not registered. However, the code in em_create_pd() dereferences the returned pointer without checking for NULL, which can lead to a kernel panic during energy model initialization.

Add a NULL check before dereferencing cpu_dev. If get_cpu_device() returns NULL, return -ENODEV and properly clean up allocated resources through the existing error path.

This issue was found by code inspection. The same function is correctly handled with NULL checking in em_cpu_get() at line 555-557.

Fixes 1bc138c62295 ("PM / EM: add support for other devices than CPUs in Energy Model")
---
 kernel/power/energy_model.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/power/energy_model.c +4 -0
index 11af9f64aa82..3971743d7e67 100644
@@ -466,6 +466,10 @@ static int em_create_pd(struct device *dev, int nr_states,
 	if (_is_cpu_device(dev))
 		for_each_cpu(cpu, cpus) {
 			cpu_dev = get_cpu_device(cpu);
+			if (!cpu_dev) {
+				ret = -ENODEV;
+				goto free_pd_table;
+			}
 			cpu_dev->em_pd = pd;
 		}
 
Signature · 1 line
2.52.0
#1 lore raw
Quoted 24 lines · Malaya Kumar Rout

The get_cpu_device() function can return NULL if the CPU device is not registered. However, the code in em_create_pd() dereferences the returned pointer without checking for NULL, which can lead to a kernel panic during energy model initialization.

Add a NULL check before dereferencing cpu_dev. If get_cpu_device() returns NULL, return -ENODEV and properly clean up allocated resources through the existing error path.

This issue was found by code inspection. The same function is correctly handled with NULL checking in em_cpu_get() at line 555-557.

Fixes 1bc138c62295 ("PM / EM: add support for other devices than CPUs in Energy Model")
---
 kernel/power/energy_model.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/power/energy_model.c +0 -0
index 11af9f64aa82..3971743d7e67 100644

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

Quoted 6 lines
kernel/power/energy_model.c +3 -0
@@ -466,6 +466,10 @@ static int em_create_pd(struct device *dev, int nr_states,
        if (_is_cpu_device(dev))
                for_each_cpu(cpu, cpus) {
                        cpu_dev = get_cpu_device(cpu);
+                       if (!cpu_dev) {
+                               ret = -ENODEV;
+                               goto free_pd_table;

No, you need to clear em_pd for the CPUs for which it has been set already.

Maybe it would be better to do this check upfront for all CPUs in the mask. Lukasz?

Quoted 5 lines
kernel/power/energy_model.c +1 -1
+                       }
                        cpu_dev->em_pd = pd;
                }
 
--
maintainer
#2 lore raw
Quoted 34 lines · Rafael J. Wysocki
Quoted 24 lines · Malaya Kumar Rout

The get_cpu_device() function can return NULL if the CPU device is not registered. However, the code in em_create_pd() dereferences the returned pointer without checking for NULL, which can lead to a kernel panic during energy model initialization.

Add a NULL check before dereferencing cpu_dev. If get_cpu_device() returns NULL, return -ENODEV and properly clean up allocated resources through the existing error path.

This issue was found by code inspection. The same function is correctly handled with NULL checking in em_cpu_get() at line 555-557.

Fixes 1bc138c62295 ("PM / EM: add support for other devices than CPUs in Energy Model")
---
  kernel/power/energy_model.c | 4 ++++
  1 file changed, 4 insertions(+)
kernel/power/energy_model.c +0 -0
index 11af9f64aa82..3971743d7e67 100644

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

Quoted 6 lines
kernel/power/energy_model.c +3 -0
@@ -466,6 +466,10 @@ static int em_create_pd(struct device *dev, int nr_states,
         if (_is_cpu_device(dev))
                 for_each_cpu(cpu, cpus) {
                         cpu_dev = get_cpu_device(cpu);
+                       if (!cpu_dev) {
+                               ret = -ENODEV;
+                               goto free_pd_table;

No, you need to clear em_pd for the CPUs for which it has been set already.

Quoted 3 lines

Maybe it would be better to do this check upfront for all CPUs in the mask. Lukasz?

Right, let me study this and come back with the recommendation...