From d92029475386e6bbbea10a2f56a87d37e61a2b38 Mon Sep 17 00:00:00 2001 From: Luis Chamberlain Date: Fri, 10 Mar 2023 21:17:12 -0800 Subject: [PATCH] module: use aliases to find module on find_module_all() Modules can have a series of aliases, but we don't currently use them to check if a module is already loaded. Part of this is because load_module() will stick to checking for already loaded modules using the actual module name, not an alias. Its however desriable to also check for aliases on find_module_all() for existing callers and future callers. The curent gain to using aliases on find_module_all() will simply be to be able to support unloading modules using the alias using the delete_module() syscall. You can debug this with dynamic debug: GRUB_CMDLINE_LINUX_DEFAULT="dyndbg=\"func module_process_aliases +p; func module_name_match +p; \" " Suggested-by: Linus Torvalds Signed-off-by: Luis Chamberlain --- kernel/module/aliases.c | 17 +++++++++++++++++ kernel/module/internal.h | 5 +++++ kernel/module/main.c | 24 +++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/kernel/module/aliases.c b/kernel/module/aliases.c index 2f30c9d4c7653d..69518bc5169a05 100644 --- a/kernel/module/aliases.c +++ b/kernel/module/aliases.c @@ -90,3 +90,20 @@ int module_process_aliases(struct module *mod, struct load_info *info) return -ENOMEM; } + +bool module_name_match_aliases(struct module *mod, const char *name, size_t len) +{ + unsigned int i; + const char *alias; + + for (i=0; i < mod->num_aliases; i++) { + alias = mod->aliases[i]; + if (strlen(alias) == len && !memcmp(alias, name, len)) { + pr_debug("module %s alias matched: alias[%u] = %s\n", + mod->name, i, alias); + return true; + } + } + + return false; +} diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 40bb80ed21e240..78aaad74f4ca7d 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -306,6 +306,7 @@ static inline int same_magic(const char *amagic, const char *bmagic, bool has_cr #ifdef CONFIG_MODULE_KERNEL_ALIAS void free_mod_aliases(struct module *mod); int module_process_aliases(struct module *mod, struct load_info *info); +bool module_name_match_aliases(struct module *mod, const char *name, size_t len); #else static void free_mod_aliases(struct module *mod) { @@ -314,4 +315,8 @@ static int module_process_aliases(struct module *mod, struct load_info *info) { return 0; } +static bool module_name_match_aliases(struct module *mod, const char *name, size_t len) +{ + return false; +} #endif /* CONFIG_MODULE_KERNEL_ALIAS */ diff --git a/kernel/module/main.c b/kernel/module/main.c index bc9202b60d5585..cf044329da3c26 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -338,6 +338,28 @@ bool find_symbol(struct find_symbol_arg *fsa) return false; } +static bool module_name_match(struct module *mod, const char *name, size_t len) +{ + unsigned int i; + const char *alias; + + if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) + return true; + + return module_name_match_aliases(mod, name, len); + + for (i=0; i < mod->num_aliases; i++) { + alias = mod->aliases[i]; + if (strlen(alias) == len && !memcmp(alias, name, len)) { + pr_debug("module %s alias matched: alias[%u] = %s\n", + mod->name, i, alias); + return true; + } + } + + return false; +} + /* * Search for module by name: must hold module_mutex (or preempt disabled * for read-only access). @@ -353,7 +375,7 @@ struct module *find_module_all(const char *name, size_t len, lockdep_is_held(&module_mutex)) { if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) continue; - if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) + if (module_name_match(mod, name, len)) return mod; } return NULL;