[PATCH RFC v3 3/4] module: update state and release reference under lock

From: Daniel Gomez via B4 Relay
Date: Fri Nov 08 2024 - 11:12:45 EST


From: Daniel Gomez <da.gomez@xxxxxxxxxxx>

Ensure the module state change and reference drop operations are
performed within module_mutex to prevent potential race conditions in
the failure path.

Signed-off-by: Daniel Gomez <da.gomez@xxxxxxxxxxx>
---
kernel/module/main.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 35a50ded64a6..2b45a6efa0a9 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2994,10 +2994,12 @@ static noinline int do_init_module(struct module *mod)
fail_free_freeinit:
kfree(freeinit);
fail:
+ mutex_lock(&module_mutex);
/* Try to protect us from buggy refcounters. */
mod->state = MODULE_STATE_GOING;
- synchronize_rcu();
module_put(mod);
+ mutex_unlock(&module_mutex);
+ synchronize_rcu();
blocking_notifier_call_chain(&module_notify_list,
MODULE_STATE_GOING, mod);
klp_module_going(mod);

--
2.39.2