cpufreq: scmi: Fix of_node use-after-free in scmi_dev_used_by_cpus()
Subsystems (2)
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.
--- drivers/cpufreq/scmi-cpufreq.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/cpufreq/scmi-cpufreq.c +8 -6
@@ -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
Viresh Kumar
maintainerQuoted 7 lines · Hans Zhang
In
scmi_dev_used_by_cpus(), the code previously released theof_nodereference viaof_node_put(np)before checking whether np equalsscmi_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
Quoted 9 lines · Viresh Kumar
Quoted 7 lines · Hans Zhang
In
scmi_dev_used_by_cpus(), the code previously released theof_nodereference viaof_node_put(np)before checking whether np equalsscmi_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
npfor 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
Viresh Kumar
maintainerQuoted 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