cpufreq: governor: fix double free in cpufreq_dbs_governor_init() error path
Subsystems (2)
PATCH 1/1 cpufreq: governor: fix double free in cpufreq_dbs_governor_init() error path
When kobject_init_and_add() fails, cpufreq_dbs_governor_init() calls kobject_put(&dbs_data->attr_set.kobj).
The kobject release callback cpufreq_dbs_data_release() calls gov->exit(dbs_data) and kfree(dbs_data), but the current error path then calls gov->exit(dbs_data) and kfree(dbs_data) again, causing a double free.
Keep the direct kfree(dbs_data) for the gov->init() failure path, but after kobject_init_and_add() has been called, let kobject_put() handle the cleanup through cpufreq_dbs_data_release().
--- drivers/cpufreq/cpufreq_governor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/cpufreq/cpufreq_governor.c +3 -3
@@ -468,13 +468,13 @@ int cpufreq_dbs_governor_init(struct cpufreq_policy *policy)/* Failure, so roll back. */ pr_err ("initialization failed (dbs_data kobject init error %d)\n" , ret); -kobject_put (&dbs_data->attr_set.kobj); - policy->governor_data =NULL ;if (!have_governor_per_policy ()) gov->gdbs_data =NULL ; - gov->exit (dbs_data); + +kobject_put (&dbs_data->attr_set.kobj); + goto free_policy_dbs_info; free_dbs_data:kfree (dbs_data);
Signature · 1 line
2.43.0
Quoted 8 lines · Guangshuo Li
When
kobject_init_and_add()fails,cpufreq_dbs_governor_init()callskobject_put(&dbs_data->attr_set.kobj).The kobject release callback
cpufreq_dbs_data_release()callsgov->exit(dbs_data)andkfree(dbs_data), but the current error path then callsgov->exit(dbs_data)andkfree(dbs_data)again, causing a double free.
Quoted 7 lines
Keep the direct
kfree(dbs_data)for thegov->init()failure path, but afterkobject_init_and_add()has been called, letkobject_put()handle the cleanup throughcpufreq_dbs_data_release().Fixes 4ebe36c94aed ("cpufreq: Fix kobject memleak")Signed-off-by Guangshuo Li <[email protected]>
Thanks for the good catch and fix — this aligns with an issue I noticed recently and looks reasonable to me.
Quoted 25 lines
--- drivers/cpufreq/cpufreq_governor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/cpufreq/cpufreq_governor.c +3
-3@@ -468,13 +468,13 @@ int cpufreq_dbs_governor_init(struct cpufreq_policy *policy)/* Failure, so roll back. */ pr_err ("initialization failed (dbs_data kobject init error %d)\n" , ret); -kobject_put (&dbs_data->attr_set.kobj); - policy->governor_data =NULL ;if (!have_governor_per_policy ()) gov->gdbs_data =NULL ; - gov->exit (dbs_data); + +kobject_put (&dbs_data->attr_set.kobj); + goto free_policy_dbs_info; free_dbs_data:kfree (dbs_data);
Signature · 2 lines
Thx and BRs, Zhongqiu Han
Quoted 12 lines · Guangshuo Li
When
kobject_init_and_add()fails,cpufreq_dbs_governor_init()callskobject_put(&dbs_data->attr_set.kobj).The kobject release callback
cpufreq_dbs_data_release()callsgov->exit(dbs_data)andkfree(dbs_data), but the current error path then callsgov->exit(dbs_data)andkfree(dbs_data)again, causing a double free.Keep the direct
kfree(dbs_data)for thegov->init()failure path, but afterkobject_init_and_add()has been called, letkobject_put()handle the cleanup throughcpufreq_dbs_data_release().
Quoted 6 lines
Fixes 4ebe36c94aed ("cpufreq: Fix kobject memleak")Signed-off-by Guangshuo Li <[email protected]>--- drivers/cpufreq/cpufreq_governor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
viresh
Quoted 14 lines · Viresh Kumar
Quoted 12 lines · Guangshuo Li
When
kobject_init_and_add()fails,cpufreq_dbs_governor_init()callskobject_put(&dbs_data->attr_set.kobj).The kobject release callback
cpufreq_dbs_data_release()callsgov->exit(dbs_data)andkfree(dbs_data), but the current error path then callsgov->exit(dbs_data)andkfree(dbs_data)again, causing a double free.Keep the direct
kfree(dbs_data)for thegov->init()failure path, but afterkobject_init_and_add()has been called, letkobject_put()handle the cleanup throughcpufreq_dbs_data_release().
Quoted 8 lines
Quoted 6 lines
Fixes 4ebe36c94aed ("cpufreq: Fix kobject memleak")Signed-off-by Guangshuo Li <[email protected]>--- drivers/cpufreq/cpufreq_governor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)Acked-by Viresh Kumar <[email protected]>
Applied as 7.0-rc material, thanks!