History

Merge branch 'modversions' (modversions fixes for powerpc from Ard)

Merge kcrctab entry fixes from Ard Biesheuvel: "This is a followup to [0] 'modversions: redefine kcrctab entries as relative CRC pointers', but since relative CRC pointers do not work in modules, and are actually only needed by powerpc with CONFIG_RELOCATABLE=y, I have made it a Kconfig selectable feature instead. First it introduces the MODULE_REL_CRCS Kconfig symbol, and adds the kbuild handling of it, i.e., modpost, genksyms and kallsyms. Then it switches all architectures to 32-bit CRC entries in kcrctab, where all architectures except powerpc with CONFIG_RELOCATABLE=y use absolute ELF symbol references as before" [0] http://marc.info/?l=linux-arch&m=148493613415294&w=2 * emailed patches from Ard Biesheuvel: module: unify absolute krctab definitions for 32-bit and 64-bit modversions: treat symbol CRCs as 32 bit quantities kbuild: modversions: add infrastructure for emitting relative CRCs

Linus Torvalds committed 9 years ago

Commit 2cb54ce2 parents 29905b5 4b9eee912 files · +93 −62Browse files

Unified · Split

12 files
  1. arch/powerpc/Kconfig +1 −0
  2. arch/powerpc/include/asm/module.h +0 −4
  3. arch/powerpc/kernel/module_64.c +0 −8
  4. include/asm-generic/export.h +6 −5
  5. include/linux/export.h +12 −5
  6. include/linux/module.h +7 −7
  7. init/Kconfig +4 −0
  8. kernel/module.c +25 −28
  9. scripts/Makefile.build +2 −0
  10. scripts/genksyms/genksyms.c +14 −5
  11. scripts/kallsyms.c +12 −0
  12. scripts/mod/modpost.c +10 −0
arch/powerpc/Kconfig+1−0
@@ -484,6 +484,7 @@ config RELOCATABLE · 483 unmodified lines
484484 bool "Build a relocatable kernel"
485485 depends on (PPC64 && !COMPILE_TEST) || (FLATMEM && (44x || FSL_BOOKE))
486486 select NONSTATIC_KERNEL
487+ select MODULE_REL_CRCS if MODVERSIONS
487488 help
488489 This builds a kernel image that is capable of running at the
489490 location the kernel is loaded at. For ppc32, there is no any
arch/powerpc/include/asm/module.h+0−4
@@ -90,9 +90,5 @@ static inline int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sec · 89 unmodified lines
9090 }
9191 #endif
9292
93−#if defined(CONFIG_MODVERSIONS) && defined(CONFIG_PPC64)
94−#define ARCH_RELOCATES_KCRCTAB
95−#define reloc_start PHYSICAL_START
96−#endif
9793 #endif /* __KERNEL__ */
9894 #endif /* _ASM_POWERPC_MODULE_H */
arch/powerpc/kernel/module_64.c+0−8
@@ -286,14 +286,6 @@ static void dedotify_versions(struct modversion_info *vers, · 285 unmodified lines
286286 for (end = (void *)vers + size; vers < end; vers++)
287287 if (vers->name[0] == '.') {
288288 memmove(vers->name, vers->name+1, strlen(vers->name));
289−#ifdef ARCH_RELOCATES_KCRCTAB
290− /* The TOC symbol has no CRC computed. To avoid CRC
291− * check failing, we must force it to the expected
292− * value (see CRC check in module.c).
293− */
294− if (!strcmp(vers->name, "TOC."))
295− vers->crc = -(unsigned long)reloc_start;
296−#endif
297289 }
298290 }
299291
include/asm-generic/export.h+6−5
@@ -9,18 +9,15 @@ · 8 unmodified lines
99 #ifndef KSYM_ALIGN
1010 #define KSYM_ALIGN 8
1111 #endif
12−#ifndef KCRC_ALIGN
13−#define KCRC_ALIGN 8
14−#endif
1512 #else
1613 #define __put .long
1714 #ifndef KSYM_ALIGN
1815 #define KSYM_ALIGN 4
1916 #endif
17+#endif
2018 #ifndef KCRC_ALIGN
2119 #define KCRC_ALIGN 4
2220 #endif
23−#endif
2421
2522 #ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
2623 #define KSYM(name) _##name
@@ -52,7 +49,11 @@ KSYM(__kstrtab_\name): · 25 unmodified lines
5249 .section ___kcrctab\sec+\name,"a"
5350 .balign KCRC_ALIGN
5451 KSYM(__kcrctab_\name):
55− __put KSYM(__crc_\name)
52+#if defined(CONFIG_MODULE_REL_CRCS)
53+ .long KSYM(__crc_\name) - .
54+#else
55+ .long KSYM(__crc_\name)
56+#endif
5657 .weak KSYM(__crc_\name)
5758 .previous
5859 #endif
include/linux/export.h+12−5
@@ -43,12 +43,19 @@ extern struct module __this_module; · 42 unmodified lines
4343 #ifdef CONFIG_MODVERSIONS
4444 /* Mark the CRC weak since genksyms apparently decides not to
4545 * generate a checksums for some symbols */
46+#if defined(CONFIG_MODULE_REL_CRCS)
4647 #define __CRC_SYMBOL(sym, sec) \
47− extern __visible void *__crc_##sym __attribute__((weak)); \
48− static const unsigned long __kcrctab_##sym \
49− __used \
50− __attribute__((section("___kcrctab" sec "+" #sym), used)) \
51− = (unsigned long) &__crc_##sym;
48+ asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
49+ " .weak " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
50+ " .long " VMLINUX_SYMBOL_STR(__crc_##sym) " - . \n" \
51+ " .previous \n");
52+#else
53+#define __CRC_SYMBOL(sym, sec) \
54+ asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
55+ " .weak " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
56+ " .long " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
57+ " .previous \n");
58+#endif
5259 #else
5360 #define __CRC_SYMBOL(sym, sec)
5461 #endif
include/linux/module.h+7−7
@@ -346,7 +346,7 @@ struct module { · 345 unmodified lines
346346
347347 /* Exported symbols */
348348 const struct kernel_symbol *syms;
349− const unsigned long *crcs;
349+ const s32 *crcs;
350350 unsigned int num_syms;
351351
352352 /* Kernel parameters. */
@@ -359,18 +359,18 @@ struct module { · 6 unmodified lines
359359 /* GPL-only exported symbols. */
360360 unsigned int num_gpl_syms;
361361 const struct kernel_symbol *gpl_syms;
362− const unsigned long *gpl_crcs;
362+ const s32 *gpl_crcs;
363363
364364 #ifdef CONFIG_UNUSED_SYMBOLS
365365 /* unused exported symbols. */
366366 const struct kernel_symbol *unused_syms;
367− const unsigned long *unused_crcs;
367+ const s32 *unused_crcs;
368368 unsigned int num_unused_syms;
369369
370370 /* GPL-only, unused exported symbols. */
371371 unsigned int num_unused_gpl_syms;
372372 const struct kernel_symbol *unused_gpl_syms;
373− const unsigned long *unused_gpl_crcs;
373+ const s32 *unused_gpl_crcs;
374374 #endif
375375
376376 #ifdef CONFIG_MODULE_SIG
@@ -382,7 +382,7 @@ struct module { · 5 unmodified lines
382382
383383 /* symbols that will be GPL-only in the near future. */
384384 const struct kernel_symbol *gpl_future_syms;
385− const unsigned long *gpl_future_crcs;
385+ const s32 *gpl_future_crcs;
386386 unsigned int num_gpl_future_syms;
387387
388388 /* Exception table */
@@ -523,7 +523,7 @@ struct module *find_module(const char *name); · 134 unmodified lines
523523
524524 struct symsearch {
525525 const struct kernel_symbol *start, *stop;
526− const unsigned long *crcs;
526+ const s32 *crcs;
527527 enum {
528528 NOT_GPL_ONLY,
529529 GPL_ONLY,
@@ -539,7 +539,7 @@ struct symsearch { · 9 unmodified lines
539539 */
540540 const struct kernel_symbol *find_symbol(const char *name,
541541 struct module **owner,
542− const unsigned long **crc,
542+ const s32 **crc,
543543 bool gplok,
544544 bool warn);
545545
init/Kconfig+4−0
@@ -1987,6 +1987,10 @@ config MODVERSIONS · 1986 unmodified lines
19871987 make them incompatible with the kernel you are running. If
19881988 unsure, say N.
19891989
1990+config MODULE_REL_CRCS
1991+ bool
1992+ depends on MODVERSIONS
1993+
19901994 config MODULE_SRCVERSION_ALL
19911995 bool "Source checksum for all modules"
19921996 help
kernel/module.c+25−28
@@ -389,16 +389,16 @@ extern const struct kernel_symbol __start___ksymtab_gpl[]; · 388 unmodified lines
389389 extern const struct kernel_symbol __stop___ksymtab_gpl[];
390390 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
391391 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
392−extern const unsigned long __start___kcrctab[];
393−extern const unsigned long __start___kcrctab_gpl[];
394−extern const unsigned long __start___kcrctab_gpl_future[];
392+extern const s32 __start___kcrctab[];
393+extern const s32 __start___kcrctab_gpl[];
394+extern const s32 __start___kcrctab_gpl_future[];
395395 #ifdef CONFIG_UNUSED_SYMBOLS
396396 extern const struct kernel_symbol __start___ksymtab_unused[];
397397 extern const struct kernel_symbol __stop___ksymtab_unused[];
398398 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
399399 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
400−extern const unsigned long __start___kcrctab_unused[];
401−extern const unsigned long __start___kcrctab_unused_gpl[];
400+extern const s32 __start___kcrctab_unused[];
401+extern const s32 __start___kcrctab_unused_gpl[];
402402 #endif
403403
404404 #ifndef CONFIG_MODVERSIONS
@@ -497,7 +497,7 @@ struct find_symbol_arg { · 92 unmodified lines
497497
498498 /* Output */
499499 struct module *owner;
500− const unsigned long *crc;
500+ const s32 *crc;
501501 const struct kernel_symbol *sym;
502502 };
503503
@@ -563,7 +563,7 @@ static bool find_symbol_in_section(const struct symsearch *syms, · 59 unmodified lines
563563 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
564564 const struct kernel_symbol *find_symbol(const char *name,
565565 struct module **owner,
566− const unsigned long **crc,
566+ const s32 **crc,
567567 bool gplok,
568568 bool warn)
569569 {
@@ -1249,23 +1249,17 @@ static int try_to_force_load(struct module *mod, const char *reason) · 679 unmodified lines
12491249 }
12501250
12511251 #ifdef CONFIG_MODVERSIONS
1252−/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1253−static unsigned long maybe_relocated(unsigned long crc,
1254− const struct module *crc_owner)
1252+
1253+static u32 resolve_rel_crc(const s32 *crc)
12551254 {
1256−#ifdef ARCH_RELOCATES_KCRCTAB
1257− if (crc_owner == NULL)
1258− return crc - (unsigned long)reloc_start;
1259−#endif
1260− return crc;
1255+ return *(u32 *)((void *)crc + *crc);
12611256 }
12621257
12631258 static int check_version(Elf_Shdr *sechdrs,
12641259 unsigned int versindex,
12651260 const char *symname,
12661261 struct module *mod,
1267− const unsigned long *crc,
1268− const struct module *crc_owner)
1262+ const s32 *crc)
12691263 {
12701264 unsigned int i, num_versions;
12711265 struct modversion_info *versions;
@@ -1283,13 +1277,19 @@ static int check_version(Elf_Shdr *sechdrs, · 11 unmodified lines
12831277 / sizeof(struct modversion_info);
12841278
12851279 for (i = 0; i < num_versions; i++) {
1280+ u32 crcval;
1281+
12861282 if (strcmp(versions[i].name, symname) != 0)
12871283 continue;
12881284
1289− if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1285+ if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1286+ crcval = resolve_rel_crc(crc);
1287+ else
1288+ crcval = *crc;
1289+ if (versions[i].crc == crcval)
12901290 return 1;
1291− pr_debug("Found checksum %lX vs module %lX\n",
1292− maybe_relocated(*crc, crc_owner), versions[i].crc);
1291+ pr_debug("Found checksum %X vs module %lX\n",
1292+ crcval, versions[i].crc);
12931293 goto bad_version;
12941294 }
12951295
@@ -1307,7 +1307,7 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs, · 11 unmodified lines
13071307 unsigned int versindex,
13081308 struct module *mod)
13091309 {
1310− const unsigned long *crc;
1310+ const s32 *crc;
13111311
13121312 /*
13131313 * Since this should be found in kernel (which can't be removed), no
@@ -1321,8 +1321,7 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs, · 7 unmodified lines
13211321 }
13221322 preempt_enable();
13231323 return check_version(sechdrs, versindex,
1324− VMLINUX_SYMBOL_STR(module_layout), mod, crc,
1325− NULL);
1324+ VMLINUX_SYMBOL_STR(module_layout), mod, crc);
13261325 }
13271326
13281327 /* First part is kernel version, which we ignore if module has crcs. */
@@ -1340,8 +1339,7 @@ static inline int check_version(Elf_Shdr *sechdrs, · 11 unmodified lines
13401339 unsigned int versindex,
13411340 const char *symname,
13421341 struct module *mod,
1343− const unsigned long *crc,
1344− const struct module *crc_owner)
1342+ const s32 *crc)
13451343 {
13461344 return 1;
13471345 }
@@ -1368,7 +1366,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, · 20 unmodified lines
13681366 {
13691367 struct module *owner;
13701368 const struct kernel_symbol *sym;
1371− const unsigned long *crc;
1369+ const s32 *crc;
13721370 int err;
13731371
13741372 /*
@@ -1383,8 +1381,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, · 8 unmodified lines
13831381 if (!sym)
13841382 goto unlock;
13851383
1386− if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1387− owner)) {
1384+ if (!check_version(info->sechdrs, info->index.vers, name, mod, crc)) {
13881385 sym = ERR_PTR(-EINVAL);
13891386 goto getname;
13901387 }
scripts/Makefile.build+2−0
@@ -164,6 +164,7 @@ cmd_gensymtypes_c = \ · 163 unmodified lines
164164 $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
165165 $(GENKSYMS) $(if $(1), -T $(2)) \
166166 $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
167+ $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \
167168 $(if $(KBUILD_PRESERVE),-p) \
168169 -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
169170
@@ -337,6 +338,7 @@ cmd_gensymtypes_S = \ · 167 unmodified lines
337338 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | \
338339 $(GENKSYMS) $(if $(1), -T $(2)) \
339340 $(patsubst y,-s _,$(CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX)) \
341+ $(patsubst y,-R,$(CONFIG_MODULE_REL_CRCS)) \
340342 $(if $(KBUILD_PRESERVE),-p) \
341343 -r $(firstword $(wildcard $(2:.symtypes=.symref) /dev/null))
342344
scripts/genksyms/genksyms.c+14−5
@@ -44,7 +44,7 @@ char *cur_filename, *source_file; · 43 unmodified lines
4444 int in_source_file;
4545
4646 static int flag_debug, flag_dump_defs, flag_reference, flag_dump_types,
47− flag_preserve, flag_warnings;
47+ flag_preserve, flag_warnings, flag_rel_crcs;
4848 static const char *mod_prefix = "";
4949
5050 static int errors;
@@ -693,7 +693,10 @@ void export_symbol(const char *name) · 642 unmodified lines
693693 fputs(">\n", debugfile);
694694
695695 /* Used as a linker script. */
696− printf("%s__crc_%s = 0x%08lx ;\n", mod_prefix, name, crc);
696+ printf(!flag_rel_crcs ? "%s__crc_%s = 0x%08lx;\n" :
697+ "SECTIONS { .rodata : ALIGN(4) { "
698+ "%s__crc_%s = .; LONG(0x%08lx); } }\n",
699+ mod_prefix, name, crc);
697700 }
698701 }
699702
@@ -730,7 +733,7 @@ void error_with_pos(const char *fmt, ...) · 30 unmodified lines
730733
731734 static void genksyms_usage(void)
732735 {
733− fputs("Usage:\n" "genksyms [-adDTwqhV] > /path/to/.tmp_obj.ver\n" "\n"
736+ fputs("Usage:\n" "genksyms [-adDTwqhVR] > /path/to/.tmp_obj.ver\n" "\n"
734737 #ifdef __GNU_LIBRARY__
735738 " -s, --symbol-prefix Select symbol prefix\n"
736739 " -d, --debug Increment the debug level (repeatable)\n"
@@ -742,6 +745,7 @@ static void genksyms_usage(void) · 5 unmodified lines
742745 " -q, --quiet Disable warnings (default)\n"
743746 " -h, --help Print this message\n"
744747 " -V, --version Print the release version\n"
748+ " -R, --relative-crc Emit section relative symbol CRCs\n"
745749 #else /* __GNU_LIBRARY__ */
746750 " -s Select symbol prefix\n"
747751 " -d Increment the debug level (repeatable)\n"
@@ -753,6 +757,7 @@ static void genksyms_usage(void) · 5 unmodified lines
753757 " -q Disable warnings (default)\n"
754758 " -h Print this message\n"
755759 " -V Print the release version\n"
760+ " -R Emit section relative symbol CRCs\n"
756761 #endif /* __GNU_LIBRARY__ */
757762 , stderr);
758763 }
@@ -774,13 +779,14 @@ int main(int argc, char **argv) · 15 unmodified lines
774779 {"preserve", 0, 0, 'p'},
775780 {"version", 0, 0, 'V'},
776781 {"help", 0, 0, 'h'},
782+ {"relative-crc", 0, 0, 'R'},
777783 {0, 0, 0, 0}
778784 };
779785
780− while ((o = getopt_long(argc, argv, "s:dwqVDr:T:ph",
786+ while ((o = getopt_long(argc, argv, "s:dwqVDr:T:phR",
781787 &long_opts[0], NULL)) != EOF)
782788 #else /* __GNU_LIBRARY__ */
783− while ((o = getopt(argc, argv, "s:dwqVDr:T:ph")) != EOF)
789+ while ((o = getopt(argc, argv, "s:dwqVDr:T:phR")) != EOF)
784790 #endif /* __GNU_LIBRARY__ */
785791 switch (o) {
786792 case 's':
@@ -823,6 +829,9 @@ int main(int argc, char **argv) · 36 unmodified lines
823829 case 'h':
824830 genksyms_usage();
825831 return 0;
832+ case 'R':
833+ flag_rel_crcs = 1;
834+ break;
826835 default:
827836 genksyms_usage();
828837 return 1;
scripts/kallsyms.c+12−0
@@ -219,6 +219,10 @@ static int symbol_valid(struct sym_entry *s) · 218 unmodified lines
219219 "_SDA2_BASE_", /* ppc */
220220 NULL };
221221
222+ static char *special_prefixes[] = {
223+ "__crc_", /* modversions */
224+ NULL };
225+
222226 static char *special_suffixes[] = {
223227 "_veneer", /* arm */
224228 "_from_arm", /* arm */
@@ -259,6 +263,14 @@ static int symbol_valid(struct sym_entry *s) · 34 unmodified lines
259263 if (strcmp(sym_name, special_symbols[i]) == 0)
260264 return 0;
261265
266+ for (i = 0; special_prefixes[i]; i++) {
267+ int l = strlen(special_prefixes[i]);
268+
269+ if (l <= strlen(sym_name) &&
270+ strncmp(sym_name, special_prefixes[i], l) == 0)
271+ return 0;
272+ }
273+
262274 for (i = 0; special_suffixes[i]; i++) {
263275 int l = strlen(sym_name) - strlen(special_suffixes[i]);
264276
scripts/mod/modpost.c+10−0
@@ -621,6 +621,16 @@ static void handle_modversions(struct module *mod, struct elf_info *info, · 620 unmodified lines
621621 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
622622 is_crc = true;
623623 crc = (unsigned int) sym->st_value;
624+ if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
625+ unsigned int *crcp;
626+
627+ /* symbol points to the CRC in the ELF object */
628+ crcp = (void *)info->hdr + sym->st_value +
629+ info->sechdrs[sym->st_shndx].sh_offset -
630+ (info->hdr->e_type != ET_REL ?
631+ info->sechdrs[sym->st_shndx].sh_addr : 0);
632+ crc = *crcp;
633+ }
624634 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
625635 export);
626636 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t