kernel/module: add a safer implementation of try_module_get()
RFC PATCH 1/1 kernel/module: add a safer implementation of try_module_get()
The current implementation of try_module_get() requires the module to exist and be live as a precondition. While this may seem intuitive at first glance, enforcing the precondition can be tricky, considering that modules can be unloaded at any time if not previously taken. For instance, the caller could be preempted just before calling try_module_get(), and while preempted, the module could be unloaded and freed. More subtly, the module could also be unloaded at any point while executing try_module_get() before incrementing the refount with atomic_inc_not_zero().
Neglecting the precondition that the module must exist and be live can cause unexpected race conditions that can lead to crashes. However, ensuring that the precondition is met may require additional locking that increases the complexity of the code and can make it more error-prone.
This patch adds a slower yet safer implementation of try_module_get() that checks if the module is valid by looking into the mod_tree before taking the module's refcount. This new function can be safely called on stale and invalid module pointers, relieving developers from the burden of ensuring that the module exists and is live before attempting to take it.
The tree lookup and refcount increment are executed after taking the module_mutex to prevent the module from being unloaded after looking up the tree.
--- include/linux/module.h | 15 +++++++++++++++ kernel/module/main.c | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
include/linux/module.h +15 -0
@@ -695,6 +695,19 @@ extern void __module_get(struct module *module);*/ extern bool try_module_get (struct module *module); +/** +* try_module_get_safe() - safely take the refcount of a module. +* @module: address of the module to be taken. +* +* Safer version of try_module_get(). Check first if the module exists and is alive, +* and then take its reference count. +* +* Return: +* * %true - module exists and its refcount has been incremented or module is NULL. +* * %false - module does not exist. +*/ +extern bool try_module_get_safe (struct module *module); +/** * module_put() - release a reference count to a module * @module: the module we should release a reference count for
@@ -815,6 +828,8 @@ static inline bool try_module_get(struct module *module)return true; } +#define try_module_get_safe (module) try_module_get(module) +static inline void module_put (struct module *module) { }
kernel/module/main.c +27 -0
@@ -842,6 +842,33 @@ bool try_module_get(struct module *module) }EXPORT_SYMBOL (try_module_get); +bool try_module_get_safe (struct module *module) +{ +struct module *mod; +bool ret = true; + +if (!module) + goto out; + +mutex_lock (&module_mutex); + +/* +* Check if the address points to a valid live module and take +* the refcount only if it points to the module struct. +*/ + mod =__module_address ((unsigned long )module); +if (mod && mod == module &&module_is_live (mod)) +__module_get (mod); +else + ret = false; + +mutex_unlock (&module_mutex); + +out: +return ret; +} +EXPORT_SYMBOL (try_module_get_safe); +void module_put (struct module *module) {int ret;
base-commit: 4515d08a742c76612b65d2f47a87d12860519842
Signature · 1 line
2.43.0
Luis Chamberlain
maintainerQuoted 23 lines · Marco Pagani
The current implementation of
try_module_get()requires the module to exist and be live as a precondition. While this may seem intuitive at first glance, enforcing the precondition can be tricky, considering that modules can be unloaded at any time if not previously taken. For instance, the caller could be preempted just before callingtry_module_get(), and while preempted, the module could be unloaded and freed. More subtly, the module could also be unloaded at any point while executingtry_module_get()before incrementing the refount withatomic_inc_not_zero().Neglecting the precondition that the module must exist and be live can cause unexpected race conditions that can lead to crashes. However, ensuring that the precondition is met may require additional locking that increases the complexity of the code and can make it more error-prone.
This patch adds a slower yet safer implementation of
try_module_get()that checks if the module is valid by looking into themod_treebefore taking the module's refcount. This new function can be safely called on stale and invalid module pointers, relieving developers from the burden of ensuring that the module exists and is live before attempting to take it.
Quoted 5 lines
The tree lookup and refcount increment are executed after taking the
module_mutexto prevent the module from being unloaded after looking up the tree.Signed-off-by Marco Pagani <[email protected]>
It very much sounds like there is a desire to have this but without a user, there is no justification.
kernel/module/main.c bool try_module_get(struct module *module) · line 853
Quoted 9 lines
kernel/module/main.c +9
-0+bool try_module_get_safe (struct module *module) +{ +struct module *mod; +bool ret = true; + +if (!module) + goto out; + +mutex_lock (&module_mutex);
If a user comes around then this should be mutex_lock_interruptible(), and add might_sleep()
Quoted 7 lines
kernel/module/main.c +7
-0+ +/* +* Check if the address points to a valid live module and take +* the refcount only if it points to the module struct. +*/ + mod =__module_address ((unsigned long )module); +if (mod && mod == module &&module_is_live (mod))
kernel/module/main.c bool try_module_get(struct module *module) · line 870
Quoted 10 lines
kernel/module/main.c +10
-0+__module_get (mod); + else + ret = false; + +mutex_unlock (&module_mutex); + +out: +return ret; +} +EXPORT_SYMBOL (try_module_get_safe);
And EXPORT_SYMBOL_GPL() would need to be used.
I'd also expect selftests to be expanded for this case, but again, without a user, this is just trying to resolve a problem which does not exist.
Luis
Quoted 24 lines · Luis Chamberlain
Quoted 23 lines · Marco Pagani
The current implementation of
try_module_get()requires the module to exist and be live as a precondition. While this may seem intuitive at first glance, enforcing the precondition can be tricky, considering that modules can be unloaded at any time if not previously taken. For instance, the caller could be preempted just before callingtry_module_get(), and while preempted, the module could be unloaded and freed. More subtly, the module could also be unloaded at any point while executingtry_module_get()before incrementing the refount withatomic_inc_not_zero().Neglecting the precondition that the module must exist and be live can cause unexpected race conditions that can lead to crashes. However, ensuring that the precondition is met may require additional locking that increases the complexity of the code and can make it more error-prone.
This patch adds a slower yet safer implementation of
try_module_get()that checks if the module is valid by looking into themod_treebefore taking the module's refcount. This new function can be safely called on stale and invalid module pointers, relieving developers from the burden of ensuring that the module exists and is live before attempting to take it.
Quoted 8 lines
Quoted 5 lines
The tree lookup and refcount increment are executed after taking the
module_mutexto prevent the module from being unloaded after looking up the tree.Signed-off-by Marco Pagani <[email protected]>It very much sounds like there is a desire to have this but without a user, there is no justification.
I was working on a set of patches to fix an issue in the fpga subsystem when I came across your commit 557aafac1153 ("kernel/module: add documentation for try_module_get()") that made me realize we also had a safety problem.
To solve this problem for the fpga manager, we had to add a mutex to ensure the low-level module still exists before calling try_module_get(). However, having a safer version of try_module_get() would have simplified the code and made it more robust against changes.
https://lore.kernel.org/linux-fpga/[email protected]/
I suspect there may be other cases where try_module_get() is inadvertently called without ensuring that the module still exists that may benefit from a safer implementation.
Quoted 4 lines
Quoted 4 lines
kernel/module/main.c +4
-0+bool try_module_get_safe (struct module *module) +{ +struct module *mod; +bool ret = true;
Quoted 8 lines
kernel/module/main.c bool try_module_get(struct module *module) · line 853
Quoted 5 lines
kernel/module/main.c +5
-0+ +if (!module) + goto out; + +mutex_lock (&module_mutex);If a user comes around then this should be
mutex_lock_interruptible(), and addmight_sleep()
Would it be okay to return false if it gets interrupted, or should I change the return type to int to propagate -EINTR? My concern with changing the signature is that it would be less straightforward to use the function in place of try_module_get().
Quoted 11 lines
Quoted 11 lines
kernel/module/main.c +11
-0+ +/* +* Check if the address points to a valid live module and take +* the refcount only if it points to the module struct. +*/ + mod =__module_address ((unsigned long )module); +if (mod && mod == module &&module_is_live (mod)) +__module_get (mod); +else + ret = false; +
Quoted 8 lines
kernel/module/main.c bool try_module_get(struct module *module) · line 870
Quoted 6 lines
kernel/module/main.c +6
-0+mutex_unlock (&module_mutex); + +out: +return ret; +} +EXPORT_SYMBOL (try_module_get_safe);And
EXPORT_SYMBOL_GPL()would need to be used.
Okay, I initially used EXPORT_SYMBOL() to be compatible with try_module_get().
Quoted 4 lines
I'd also expect selftests to be expanded for this case, but again, without a user, this is just trying to resolve a problem which does not exist.
I can add selftests in the next versions.
Thanks,
Marco
Luis Chamberlain
maintainerQuoted 16 lines · Marco Pagani
Quoted 3 lines · Luis Chamberlain
It very much sounds like there is a desire to have this but without a user, there is no justification.
I was working on a set of patches to fix an issue in the fpga subsystem when I came across your commit
557aafac1153("kernel/module: add documentation fortry_module_get()") that made me realize we also had a safety problem.To solve this problem for the fpga manager, we had to add a mutex to ensure the low-level module still exists before calling
try_module_get(). However, having a safer version oftry_module_get()would have simplified the code and made it more robust against changes.
Quoted 5 lines
https://lore.kernel.org/linux-fpga/[email protected]/
I suspect there may be other cases where
try_module_get()is inadvertently called without ensuring that the module still exists that may benefit from a safer implementation.
Maybe so, however I'm not yet sure if this is safe from deadlocks. Please work on a series of selftest simple modules which demonstrate its use / and a simple bash script selftest loader which verifies this won't bust. Consider you may have third party modules which also race with this too, and other users without this new API.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/module/main.c +4
-0+bool try_module_get_safe (struct module *module) +{ +struct module *mod; +bool ret = true;Quoted 8 lines
kernel/module/main.c bool try_module_get(struct module *module) · line 853
Quoted 5 lines
kernel/module/main.c +5
-0+ +if (!module) + goto out; + +mutex_lock (&module_mutex);If a user comes around then this should be
mutex_lock_interruptible(), and addmight_sleep()
Quoted 4 lines
Would it be okay to return false if it gets interrupted, or should I change the return type to int to propagate
-EINTR? My concern with changing the signature is that it would be less straightforward to use the function in place oftry_module_get().
Since we want a safe mechanism we might as well not allow a simple drop in replacement but a more robust one so that users take care of the return value properly.
Luis