modpost: simplify mod->name allocation

JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts: In hunk3 of modpost.c, source diff since
       upstream c25e1c55822f9b3b53 not applied, which would
       have removed the 3 lines added in RHEL9 version of this hunk.

commit 8c9ce89c5b63028dd3be43807f10b009cd2c6e51
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 30 18:01:38 2022 +0900

    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 <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
This commit is contained in:
Donald Dutile 2024-05-09 01:53:49 -04:00
parent 387d6609c9
commit 090feaafca
1 changed files with 12 additions and 16 deletions

View File

@ -173,11 +173,11 @@ static struct module *find_module(const char *modname)
return NULL;
}
static struct module *new_module(const char *modname)
static struct module *new_module(const char *name, size_t namelen)
{
struct module *mod;
mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1));
memset(mod, 0, sizeof(*mod));
INIT_LIST_HEAD(&mod->exported_symbols);
@ -185,8 +185,9 @@ static struct module *new_module(const char *modname)
INIT_LIST_HEAD(&mod->missing_namespaces);
INIT_LIST_HEAD(&mod->imported_namespaces);
strcpy(mod->name, modname);
mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
memcpy(mod->name, name, namelen);
mod->name[namelen] = '\0';
mod->is_vmlinux = (strcmp(mod->name, "vmlinux") == 0);
/*
* Set mod->is_gpl_compatible to true by default. If MODULE_LICENSE()
@ -2007,19 +2008,14 @@ static void read_symbols(const char *modname)
if (!parse_elf(&info, modname))
return;
{
char *tmp;
/* strip trailing .o */
tmp = NOFAIL(strdup(modname));
tmp[strlen(tmp) - 2] = '\0';
/* strip trailing .prelink */
if (strends(tmp, ".prelink"))
tmp[strlen(tmp) - 8] = '\0';
mod = new_module(tmp);
free(tmp);
if (!strends(modname, ".o")) {
error("%s: filename must be suffixed with .o\n", modname);
return;
}
/* strip trailing .o */
mod = new_module(modname, strlen(modname) - strlen(".o"));
if (!mod->is_vmlinux) {
license = get_modinfo(&info, "license");
if (!license)
@ -2488,7 +2484,7 @@ static void read_dump(const char *fname)
mod = find_module(modname);
if (!mod) {
mod = new_module(modname);
mod = new_module(modname, strlen(modname));
mod->from_dump = true;
}
s = sym_add_exported(symname, mod, gpl_only);