module: drain external refs before freeing a failed-init module
Subsystems (2)
PATCH 1/1 module: drain external refs before freeing a failed-init module
When a module's __init makes it externally reachable (e.g. by registering a protocol family or a bus driver) and a later __init step fails, do_init_module() frees the module while callers that entered through that interface and called try_module_get() may still be executing module text.
The normal unload path in try_stop_module() refuses to proceed while module_refcount() is non-zero. The init-failure path has no such check and races to free_module() immediately after synchronize_rcu().
Fix it by waiting for all external references to drain before calling free_module(). Add a per-module struct completion refs_gone inside the existing CONFIG_MODULE_UNLOAD block that is initialised in module_unload_init() and completed from module_put() when refcnt drops back to MODULE_REF_BASE and the module state is MODULE_STATE_GOING. The fail: path calls wait_for_completion() (guarded by the same #ifdef) after dropping the loader's own reference, replacing the previous implicit assumption that no external refs exist at that point.
Also move wake_up_all(&module_wq) to before the drain so that concurrent loaders blocked in module_patient_check_exists() are unblocked immediately once MODULE_STATE_GOING is visible, rather than after the drain completes.
---
[RFC] module: init-failure path can free a module with live try_module_get() users
include/linux/module.h | 2 ++ kernel/module/main.c | 33 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 2 deletions(-)
include/linux/module.h +2 -0
@@ -562,6 +562,8 @@ struct module {void (*exit)(void );atomic_t refcnt; +/* Completed when refcnt drops to MODULE_REF_BASE on init failure. */ +struct completion refs_gone;#endif #ifdef CONFIG_CONSTRUCTORS
kernel/module/main.c +31 -2
@@ -655,6 +655,7 @@ static int module_unload_init(struct module *mod)* refcnt == 0 means module is going. */ atomic_set (&mod->refcnt,MODULE_REF_BASE ); +init_completion (&mod->refs_gone);INIT_LIST_HEAD (&mod->source_list);INIT_LIST_HEAD (&mod->target_list);
@@ -956,6 +957,14 @@ void module_put(struct module *module) ret =atomic_dec_if_positive (&module->refcnt);WARN_ON (ret <0 );/* Failed to put refcount */ trace_module_put (module, _RET_IP_); +/* +* If refcnt just dropped to MODULE_REF_BASE and the module is going +* away due to an init failure, all external references are gone. Wake +* any thread waiting in the init-failure drain path. +*/ +if (ret ==MODULE_REF_BASE && + module->state ==MODULE_STATE_GOING ) +complete (&module->refs_gone); } }EXPORT_SYMBOL (module_put);
@@ -3206,16 +3214,36 @@ static noinline int do_init_module(struct module *mod) fail_free_freeinit:kfree (freeinit); fail: -/* Try to protect us from buggy refcounters. */ mod->state =MODULE_STATE_GOING ;synchronize_rcu (); + +/* +* Unblock concurrent loaders waiting in module_patient_check_exists() on +* module_wq. finished_loading() returns true once state is GOING, so they +* can now proceed. Do this before the drain below so a failed init delays +* only this task, not every other loader that raced to load the same +* module. +*/ +wake_up_all (&module_wq); + +/* Drop the loader's own reference taken in module_unload_init(). */ module_put (mod); + +#ifdef CONFIG_MODULE_UNLOAD +/* +* Wait for all external references taken before MODULE_STATE_GOING became +* visible to be released. module_put() completes refs_gone when refcnt +* drops to MODULE_REF_BASE. The hung-task detector will catch any module +* whose error path leaks a reference. +*/ +wait_for_completion (&mod->refs_gone); +#endif +blocking_notifier_call_chain (&module_notify_list,MODULE_STATE_GOING , mod);klp_module_going (mod);ftrace_release_mod (mod);free_module (mod); -wake_up_all (&module_wq);return ret; }
Signature · 1 line
2.43.7
Hi All,
Gentle ping on this patch. Is there anything I should change, or any additional information that would help the review?
Thank You,
Mahanta
Quoted 112 lines · Mahanta Jambigi
When a module's
__initmakes it externally reachable (e.g. by registering a protocol family or a bus driver) and a later__initstep fails,do_init_module()frees the module while callers that entered through that interface and calledtry_module_get()may still be executing module text.The normal unload path in
try_stop_module()refuses to proceed whilemodule_refcount()is non-zero. The init-failure path has no such check and races tofree_module()immediately aftersynchronize_rcu().Fix it by waiting for all external references to drain before calling
free_module(). Add a per-module struct completionrefs_goneinside the existingCONFIG_MODULE_UNLOADblock that is initialised inmodule_unload_init()and completed frommodule_put()when refcnt drops back toMODULE_REF_BASEand the module state isMODULE_STATE_GOING. The fail: path callswait_for_completion()(guarded by the same #ifdef) after dropping the loader's own reference, replacing the previous implicit assumption that no external refs exist at that point.Also move
wake_up_all(&module_wq)to before the drain so that concurrent loaders blocked inmodule_patient_check_exists()are unblocked immediately onceMODULE_STATE_GOINGis visible, rather than after the drain completes.Fixes 1fb9341ac348 ("module: put modules in list much earlier.")Signed-off-by Mahanta Jambigi <[email protected]>---
[RFC] module: init-failure path can free a module with live
try_module_get()usersinclude/linux/module.h | 2 ++ kernel/module/main.c | 33 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 2 deletions(-)include/linux/module.h +2
-0@@ -562,6 +562,8 @@ struct module {void (*exit)(void );atomic_t refcnt; +/* Completed when refcnt drops to MODULE_REF_BASE on init failure. */ +struct completion refs_gone;#endif #ifdef CONFIG_CONSTRUCTORS kernel/module/main.c +31
-2@@ -655,6 +655,7 @@ static int module_unload_init(struct module *mod)* refcnt == 0 means module is going. */ atomic_set (&mod->refcnt,MODULE_REF_BASE ); +init_completion (&mod->refs_gone);INIT_LIST_HEAD (&mod->source_list);INIT_LIST_HEAD (&mod->target_list);@@ -956,6 +957,14 @@ void module_put(struct module *module) ret =atomic_dec_if_positive (&module->refcnt);WARN_ON (ret <0 );/* Failed to put refcount */ trace_module_put (module, _RET_IP_); +/* +* If refcnt just dropped to MODULE_REF_BASE and the module is going +* away due to an init failure, all external references are gone. Wake +* any thread waiting in the init-failure drain path. +*/ +if (ret ==MODULE_REF_BASE && + module->state ==MODULE_STATE_GOING ) +complete (&module->refs_gone); } }EXPORT_SYMBOL (module_put);@@ -3206,16 +3214,36 @@ static noinline int do_init_module(struct module *mod) fail_free_freeinit:kfree (freeinit); fail: -/* Try to protect us from buggy refcounters. */ mod->state =MODULE_STATE_GOING ;synchronize_rcu (); + +/* +* Unblock concurrent loaders waiting in module_patient_check_exists() on +* module_wq. finished_loading() returns true once state is GOING, so they +* can now proceed. Do this before the drain below so a failed init delays +* only this task, not every other loader that raced to load the same +* module. +*/ +wake_up_all (&module_wq); + +/* Drop the loader's own reference taken in module_unload_init(). */ module_put (mod); + +#ifdef CONFIG_MODULE_UNLOAD +/* +* Wait for all external references taken before MODULE_STATE_GOING became +* visible to be released. module_put() completes refs_gone when refcnt +* drops to MODULE_REF_BASE. The hung-task detector will catch any module +* whose error path leaks a reference. +*/ +wait_for_completion (&mod->refs_gone); +#endif +blocking_notifier_call_chain (&module_notify_list,MODULE_STATE_GOING , mod);klp_module_going (mod);ftrace_release_mod (mod);free_module (mod); -wake_up_all (&module_wq);return ret; }
Petr Pavlu
maintainerQuoted 38 lines · Mahanta Jambigi
When a module's
__initmakes it externally reachable (e.g. by registering a protocol family or a bus driver) and a later__initstep fails,do_init_module()frees the module while callers that entered through that interface and calledtry_module_get()may still be executing module text.The normal unload path in
try_stop_module()refuses to proceed whilemodule_refcount()is non-zero. The init-failure path has no such check and races tofree_module()immediately aftersynchronize_rcu().Fix it by waiting for all external references to drain before calling
free_module(). Add a per-module struct completionrefs_goneinside the existingCONFIG_MODULE_UNLOADblock that is initialised inmodule_unload_init()and completed frommodule_put()when refcnt drops back toMODULE_REF_BASEand the module state isMODULE_STATE_GOING. The fail: path callswait_for_completion()(guarded by the same #ifdef) after dropping the loader's own reference, replacing the previous implicit assumption that no external refs exist at that point.Also move
wake_up_all(&module_wq)to before the drain so that concurrent loaders blocked inmodule_patient_check_exists()are unblocked immediately onceMODULE_STATE_GOINGis visible, rather than after the drain completes.Fixes 1fb9341ac348 ("module: put modules in list much earlier.")Signed-off-by Mahanta Jambigi <[email protected]>---
[RFC] module: init-failure path can free a module with live
try_module_get()usersinclude/linux/module.h | 2 ++ kernel/module/main.c | 33 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 2 deletions(-)include/linux/module.h +0
-0
include/linux/module.h struct module { · line 566
Quoted 5 lines
include/linux/module.h +2
-0@@ -562,6 +562,8 @@ struct module {void (*exit)(void );atomic_t refcnt; +/* Completed when refcnt drops to MODULE_REF_BASE on init failure. */ +struct completion refs_gone;
Having a per-module completion that is needed only during module loading is a bit wasteful. I think a global waitqueue, similar to module_wq, should be sufficient. This would also avoid the use-after-free condition described in my next comment.
Quoted 17 lines
include/linux/module.h +0
-0#endif #ifdef CONFIG_CONSTRUCTORS kernel/module/main.c +1
-0@@ -655,6 +655,7 @@ static int module_unload_init(struct module *mod)* refcnt == 0 means module is going. */ atomic_set (&mod->refcnt,MODULE_REF_BASE ); +init_completion (&mod->refs_gone);INIT_LIST_HEAD (&mod->source_list);INIT_LIST_HEAD (&mod->target_list);@@ -956,6 +957,14 @@ void module_put(struct module *module) ret =atomic_dec_if_positive (&module->refcnt);
kernel/module/main.c void module_put(struct module *module) · line 967
Quoted 10 lines
kernel/module/main.c +8
-0WARN_ON (ret <0 );/* Failed to put refcount */ trace_module_put (module, _RET_IP_); +/* +* If refcnt just dropped to MODULE_REF_BASE and the module is going +* away due to an init failure, all external references are gone. Wake +* any thread waiting in the init-failure drain path. +*/ +if (ret ==MODULE_REF_BASE && + module->state ==MODULE_STATE_GOING ) +complete (&module->refs_gone);
module_put() can run concurrently with delete_module(). The module might be gone after the atomic_dec_if_positive() call so this logic could lead to a use-after-free.
Quoted 9 lines
kernel/module/main.c +0
-1} }EXPORT_SYMBOL (module_put);@@ -3206,16 +3214,36 @@ static noinline int do_init_module(struct module *mod) fail_free_freeinit:kfree (freeinit); fail: -/* Try to protect us from buggy refcounters. */ mod->state =MODULE_STATE_GOING ;
kernel/module/main.c static noinline int do_init_module(struct module *mod) · line 3227
Quoted 10 lines
kernel/module/main.c +9
-0synchronize_rcu (); + +/* +* Unblock concurrent loaders waiting in module_patient_check_exists() on +* module_wq. finished_loading() returns true once state is GOING, so they +* can now proceed. Do this before the drain below so a failed init delays +* only this task, not every other loader that raced to load the same +* module. +*/ +wake_up_all (&module_wq);
This wakes up only modules waiting in module_patient_check_exists(). However, most duplicate loads using the newer finit_module() syscall wait in idempotent_wait_for_completion(). They should be woken up as well, so at worst only the current load gets stuck waiting on its references to drain.
Quoted 23 lines
kernel/module/main.c +13
-1+ +/* Drop the loader's own reference taken in module_unload_init(). */ module_put (mod); + +#ifdef CONFIG_MODULE_UNLOAD +/* +* Wait for all external references taken before MODULE_STATE_GOING became +* visible to be released. module_put() completes refs_gone when refcnt +* drops to MODULE_REF_BASE. The hung-task detector will catch any module +* whose error path leaks a reference. +*/ +wait_for_completion (&mod->refs_gone); +#endif +blocking_notifier_call_chain (&module_notify_list,MODULE_STATE_GOING , mod);klp_module_going (mod);ftrace_release_mod (mod);free_module (mod); -wake_up_all (&module_wq);return ret; }
Signature · 2 lines
Thanks, Petr