modpost: abort if module name is too long

Module name has a limited length, but currently the build system
allows the build finishing even if the module name is too long.

CC /root/kprobe_example/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.mod.o
/root/kprobe_example/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.mod.c:9:2:
warning: initializer-string for array of chars is too long [enabled by default]
.name = KBUILD_MODNAME,
^

but it's merely a warning.

This patch adds the check of the module name length in modpost and stops
the build properly.

Signed-off-by: Wanlong Gao <wanlong.gao@gmail.com>
Signed-off-by: Jessica Yu <jeyu@kernel.org>

authored by

Wanlong Gao and committed by
Jessica Yu
4fd3e4ef 520eccdf

+24 -5
+24 -5
scripts/mod/modpost.c
··· 47 47 export_unused_gpl, export_gpl_future, export_unknown 48 48 }; 49 49 50 + /* In kernel, this size is defined in linux/module.h; 51 + * here we use Elf_Addr instead of long for covering cross-compile 52 + */ 53 + 54 + #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) 55 + 50 56 #define PRINTF __attribute__ ((format (printf, 1, 2))) 51 57 52 58 PRINTF void fatal(const char *fmt, ...) ··· 2122 2116 } 2123 2117 } 2124 2118 2119 + static int check_modname_len(struct module *mod) 2120 + { 2121 + const char *mod_name; 2122 + 2123 + mod_name = strrchr(mod->name, '/'); 2124 + if (mod_name == NULL) 2125 + mod_name = mod->name; 2126 + else 2127 + mod_name++; 2128 + if (strlen(mod_name) >= MODULE_NAME_LEN) { 2129 + merror("module name is too long [%s.ko]\n", mod->name); 2130 + return 1; 2131 + } 2132 + 2133 + return 0; 2134 + } 2135 + 2125 2136 /** 2126 2137 * Header for the generated file 2127 2138 **/ ··· 2177 2154 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0) 2178 2155 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); 2179 2156 } 2180 - 2181 - /* In kernel, this size is defined in linux/module.h; 2182 - * here we use Elf_Addr instead of long for covering cross-compile 2183 - */ 2184 - #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) 2185 2157 2186 2158 /** 2187 2159 * Record CRCs for unresolved symbols ··· 2508 2490 2509 2491 buf.pos = 0; 2510 2492 2493 + err |= check_modname_len(mod); 2511 2494 add_header(&buf, mod); 2512 2495 add_intree_flag(&buf, !external_module); 2513 2496 add_staging_flag(&buf, mod->name);