Skip to contentSkip to conversation

cpufreq: scmi: Fix of_node use-after-free in scmi_dev_used_by_cpus()

PATCH In review Hans Zhang 2 participants 4 messages Mar 2 – Mar 9

Subsystems (2)
lore
Share & tools
lore

Hans Zhang

author
#0 lore raw

PATCH 1/1 cpufreq: scmi: Fix of_node use-after-free in scmi_dev_used_by_cpus()

In scmi_dev_used_by_cpus(), the code previously released the of_node reference via of_node_put(np) before checking whether np equals scmi_np. This could lead to a use-after-free if the node pointer was accessed after being freed. Reorder the logic to perform the comparison first and only put the node after the check, or immediately return if they match. This ensures safe reference counting and avoids potential kernel crashes.

Fixes 6c9bb8692272 ("cpufreq: scmi: Skip SCMI devices that aren't used by the CPUs")
---
 drivers/cpufreq/scmi-cpufreq.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/cpufreq/scmi-cpufreq.c +8 -6
index 4edb4f7a8aa9..187aeb65e221 100644
@@ -413,17 +413,19 @@ static bool scmi_dev_used_by_cpus(struct device *scmi_dev)
 		cpu_np = dev_of_node(cpu_dev);
 
 		np = of_parse_phandle(cpu_np, "clocks", 0);
-		of_node_put(np);
-
-		if (np == scmi_np)
+		if (np == scmi_np) {
+			of_node_put(np);
 			return true;
+		}
+		of_node_put(np);
 
 		idx = of_property_match_string(cpu_np, "power-domain-names", "perf");
 		np = of_parse_phandle(cpu_np, "power-domains", idx);
-		of_node_put(np);
-
-		if (np == scmi_np)
+		if (np == scmi_np) {
+			of_node_put(np);
 			return true;
+		}
+		of_node_put(np);
 	}
 
 	/*
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 7 lines · Hans Zhang

In scmi_dev_used_by_cpus(), the code previously released the of_node reference via of_node_put(np) before checking whether np equals scmi_np. This could lead to a use-after-free if the node pointer was accessed after being freed. Reorder the logic to perform the comparison first and only put the node after the check, or immediately return if they match. This ensures safe reference counting and avoids potential kernel crashes.

It shouldn't lead to a use-after-free problem as we aren't using the np for anything apart from simple comparison of value.

Signature · 1 line
viresh
author
#2 lore raw
Quoted 9 lines · Viresh Kumar
Quoted 7 lines · Hans Zhang

In scmi_dev_used_by_cpus(), the code previously released the of_node reference via of_node_put(np) before checking whether np equals scmi_np. This could lead to a use-after-free if the node pointer was accessed after being freed. Reorder the logic to perform the comparison first and only put the node after the check, or immediately return if they match. This ensures safe reference counting and avoids potential kernel crashes.

Quoted 3 lines

It shouldn't lead to a use-after-free problem as we aren't using the np for anything apart from simple comparison of value.

Hi Viresh,

Thank you very much for your reply. Then, do you think this patch is necessary? Or should I revise the commit message?

Best regards,
Hans

maintainer
#3 lore raw
Quoted 2 lines · Hans Zhang

Thank you very much for your reply. Then, do you think this patch is necessary?

No, this patch isn't required I think.

Signature · 1 line
viresh