Skip to content

Commit 8c9ce89

Browse files
committed
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: Masahiro Yamada <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]>
1 parent b42d230 commit 8c9ce89

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

scripts/mod/modpost.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,21 @@ static struct module *find_module(const char *modname)
172172
return NULL;
173173
}
174174

175-
static struct module *new_module(const char *modname)
175+
static struct module *new_module(const char *name, size_t namelen)
176176
{
177177
struct module *mod;
178178

179-
mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
179+
mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
180180
memset(mod, 0, sizeof(*mod));
181181

182182
INIT_LIST_HEAD(&mod->exported_symbols);
183183
INIT_LIST_HEAD(&mod->unresolved_symbols);
184184
INIT_LIST_HEAD(&mod->missing_namespaces);
185185
INIT_LIST_HEAD(&mod->imported_namespaces);
186186

187-
strcpy(mod->name, modname);
188-
mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
187+
memcpy(mod->name, name, namelen);
188+
mod->name[namelen] = '\0';
189+
mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
189190

190191
/*
191192
* Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
@@ -2017,16 +2018,14 @@ static void read_symbols(const char *modname)
20172018
if (!parse_elf(&info, modname))
20182019
return;
20192020

2020-
{
2021-
char *tmp;
2022-
2023-
/* strip trailing .o */
2024-
tmp = NOFAIL(strdup(modname));
2025-
tmp[strlen(tmp) - 2] = '\0';
2026-
mod = new_module(tmp);
2027-
free(tmp);
2021+
if (!strends(modname, ".o")) {
2022+
error("%s: filename must be suffixed with .o\n", modname);
2023+
return;
20282024
}
20292025

2026+
/* strip trailing .o */
2027+
mod = new_module(modname, strlen(modname) - strlen(".o"));
2028+
20302029
if (!mod->is_vmlinux) {
20312030
license = get_modinfo(&info, "license");
20322031
if (!license)
@@ -2488,7 +2487,7 @@ static void read_dump(const char *fname)
24882487

24892488
mod = find_module(modname);
24902489
if (!mod) {
2491-
mod = new_module(modname);
2490+
mod = new_module(modname, strlen(modname));
24922491
mod->from_dump = true;
24932492
}
24942493
s = sym_add_exported(symname, mod, gpl_only);

0 commit comments

Comments
 (0)