Commit 8c9ce89c authored by Masahiro Yamada's avatar Masahiro Yamada

modpost: simplify mod->name allocation

mod->name is set to the ELF filename with the suffix ".o" stripped.

The current code calls strdup() and free() to manipulate the string,
but a simpler approach is to pass new_module() with the name length
subtracted by 2.

Also, check if the passed filename ends with ".o" before stripping it.

The current code blindly chops the suffix:

    tmp[strlen(tmp) - 2] = '\0'

It will cause buffer under-run if strlen(tmp) < 2;
Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
parent b42d2306
...@@ -172,11 +172,11 @@ static struct module *find_module(const char *modname) ...@@ -172,11 +172,11 @@ static struct module *find_module(const char *modname)
return NULL; return NULL;
} }
static struct module *new_module(const char *modname) static struct module *new_module(const char *name, size_t namelen)
{ {
struct module *mod; struct module *mod;
mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1)); mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
memset(mod, 0, sizeof(*mod)); memset(mod, 0, sizeof(*mod));
INIT_LIST_HEAD(&mod->exported_symbols); INIT_LIST_HEAD(&mod->exported_symbols);
...@@ -184,8 +184,9 @@ static struct module *new_module(const char *modname) ...@@ -184,8 +184,9 @@ static struct module *new_module(const char *modname)
INIT_LIST_HEAD(&mod->missing_namespaces); INIT_LIST_HEAD(&mod->missing_namespaces);
INIT_LIST_HEAD(&mod->imported_namespaces); INIT_LIST_HEAD(&mod->imported_namespaces);
strcpy(mod->name, modname); memcpy(mod->name, name, namelen);
mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0); mod->name[namelen] = '\0';
mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
/* /*
* Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE() * Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
...@@ -2017,15 +2018,13 @@ static void read_symbols(const char *modname) ...@@ -2017,15 +2018,13 @@ static void read_symbols(const char *modname)
if (!parse_elf(&info, modname)) if (!parse_elf(&info, modname))
return; return;
{ if (!strends(modname, ".o")) {
char *tmp; error("%s: filename must be suffixed with .o\n", modname);
return;
}
/* strip trailing .o */ /* strip trailing .o */
tmp = NOFAIL(strdup(modname)); mod = new_module(modname, strlen(modname) - strlen(".o"));
tmp[strlen(tmp) - 2] = '\0';
mod = new_module(tmp);
free(tmp);
}
if (!mod->is_vmlinux) { if (!mod->is_vmlinux) {
license = get_modinfo(&info, "license"); license = get_modinfo(&info, "license");
...@@ -2488,7 +2487,7 @@ static void read_dump(const char *fname) ...@@ -2488,7 +2487,7 @@ static void read_dump(const char *fname)
mod = find_module(modname); mod = find_module(modname);
if (!mod) { if (!mod) {
mod = new_module(modname); mod = new_module(modname, strlen(modname));
mod->from_dump = true; mod->from_dump = true;
} }
s = sym_add_exported(symname, mod, gpl_only); s = sym_add_exported(symname, mod, gpl_only);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment