Skip to contentSkip to conversation

pmdomain: core: clear states after freeing them

PATCH In review Anfernee Cao 1 participant 2 messages Oct 21 – Nov 9

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

PATCH 1/1 pmdomain: core: clear states after freeing them

genpd_free_data() frees the genpd power states through the ->free_states() callback, but leaves genpd->states and genpd->state_count pointing at the freed memory.

Providers that allocate the states themselves hand them over to genpd by setting ->free_states(). When they fail after genpd has taken over, they still run their own free helper, as psci_pd_init() and the RISC-V SBI idle driver do:

    remove_pd:
            pm_genpd_remove(pd);  /* frees states via ->free_states() */
    ...
            dt_idle_pd_free(pd);  /* frees them again */

The second call dereferences the freed states array and frees it again, causing a use-after-free and a double free.

Clear genpd->states and genpd->state_count once the states have been freed, so that a later free helper becomes a no-op.

This issue was found by manual code inspection.

Fixes 9d976d6721df ("cpuidle: Factor-out power domain related code from PSCI domain driver")
Cc [email protected] # v5.18+
---
 drivers/pmdomain/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pmdomain/core.c +4 -1
index 052ec478dfcc..12db69f53aee 100644
@@ -2353,8 +2353,11 @@ static void genpd_free_data(struct generic_pm_domain *genpd)
 		ida_free(&genpd_ida, genpd->device_id);
 	if (genpd_is_cpu_domain(genpd))
 		free_cpumask_var(genpd->cpus);
-	if (genpd->free_states)
+	if (genpd->free_states) {
 		genpd->free_states(genpd->states, genpd->state_count);
+		genpd->states = NULL;
+		genpd->state_count = 0;
+	}
 	kfree(genpd->gd);
 }
 
Signature · 1 line
2.54.0.windows.1
#1 lore raw

Hi Ulf,

Friendly ping on this one. Patchwork marks it as "handled elsewhere", but I checked the tips of your linux-pm next (6d318db9f) and fixes (4c66c4235), and genpd_free_data() is still unchanged there as well as in mainline. Could you confirm whether it's queued somewhere, or should I resend?

Thanks,
Anfernee