History

Merge tag 'modules-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux

Pull modules updates from Luis Chamberlain: - It was time to tidy up kernel/module.c and one way of starting with that effort was to split it up into files. At my request Aaron Tomlin spearheaded that effort with the goal to not introduce any functional at all during that endeavour. The penalty for the split is +1322 bytes total, +112 bytes in data, +1210 bytes in text while bss is unchanged. One of the benefits of this other than helping make the code easier to read and review is summoning more help on review for changes with livepatching so kernel/module/livepatch.c is now pegged as maintained by the live patching folks. The before and after with just the move on a defconfig on x86-64: $ size kernel/module.o text data bss dec hex filename 38434 4540 104 43078 a846 kernel/module.o $ size -t kernel/module/*.o text data bss dec hex filename 4785 120 0 4905 1329 kernel/module/kallsyms.o 28577 4416 104 33097 8149 kernel/module/main.o 1158 8 0 1166 48e kernel/module/procfs.o 902 108 0 1010 3f2 kernel/module/strict_rwx.o 3390 0 0 3390 d3e kernel/module/sysfs.o 832 0 0 832 340 kernel/module/tree_lookup.o 39644 4652 104 44400 ad70 (TOTALS) - Aaron added module unload taint tracking (MODULE_UNLOAD_TAINT_TRACKING), to enable tracking unloaded modules which did taint the kernel. - Christophe Leroy added CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC which lets architectures to request having modules data in vmalloc area instead of module area. There are three reasons why an architecture might want this: a) On some architectures (like book3s/32) it is not possible to protect against execution on a page basis. The exec stuff can be mapped by different arch segment sizes (on book3s/32 that is 256M segments). By default the module area is in an Exec segment while vmalloc area is in a NoExec segment. Using vmalloc lets you muck with module data as NoExec on those architectures whereas before you could not. b) By pushing more module data to vmalloc you also increase the probability of module text to remain within a closer distance from kernel core text and this reduces trampolines, this has been reported on arm first and powerpc folks are following that lead. c) Free'ing module_alloc() (Exec by default) area leaves this exposed as Exec by default, some architectures have some security enhancements to set this as NoExec on free, and splitting module data with text let's future generic special allocators be added to the kernel without having developers try to grok the tribal knowledge per arch. Work like Rick Edgecombe's permission vmalloc interface [0] becomes easier to address over time. [0] https://lore.kernel.org/lkml/20201120202426.18009-1-rick.p.edgecombe@intel.com/#r - Masahiro Yamada's symbol search enhancements * tag 'modules-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux: (33 commits) module: merge check_exported_symbol() into find_exported_symbol_in_section() module: do not binary-search in __ksymtab_gpl if fsa->gplok is false module: do not pass opaque pointer for symbol search module: show disallowed symbol name for inherit_taint() module: fix [e_shstrndx].sh_size=0 OOB access module: Introduce module unload taint tracking module: Move module_assert_mutex_or_preempt() to internal.h module: Make module_flags_taint() accept a module's taints bitmap and usable outside core code module.h: simplify MODULE_IMPORT_NS powerpc: Select ARCH_WANTS_MODULES_DATA_IN_VMALLOC on book3s/32 and 8xx module: Remove module_addr_min and module_addr_max module: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC module: Introduce data_layout module: Prepare for handling several RB trees module: Always have struct mod_tree_root module: Rename debug_align() as strict_align() module: Rework layout alignment to avoid BUG_ON()s module: Move module_enable_x() and frob_text() in strict_rwx.c module: Make module_enable_x() independent of CONFIG_ARCH_HAS_STRICT_MODULE_RWX module: Move version support into a separate file ...

Linus Torvalds committed 4 years ago

Commit ef98f9c2 parents 44d3572 7390b9429 files · +2384 −2052Browse files

Unified · Split

29 files
  1. MAINTAINERS +3 −1
  2. arch/Kconfig +6 −0
  3. arch/powerpc/Kconfig +1 −0
  4. include/linux/kdb.h +1 −0
  5. include/linux/module.h +19 −13
  6. init/Kconfig +11 −0
  7. kernel/Makefile +1 −4
  8. kernel/debug/kdb/kdb_io.c +0 −1
  9. kernel/debug/kdb/kdb_keyboard.c +0 −1
  10. kernel/debug/kdb/kdb_main.c +0 −49
  11. kernel/debug/kdb/kdb_private.h +0 −4
  12. kernel/debug/kdb/kdb_support.c +0 −1
  13. kernel/module-internal.h +0 −50
  14. kernel/module/Makefile +21 −0
  15. kernel/module/debug_kmemleak.c +30 −0
  16. kernel/module/decompress.c +4 −1
  17. kernel/module/internal.h +302 −0
  18. kernel/module/kallsyms.c +512 −0
  19. kernel/module/kdb.c +62 −0
  20. kernel/module/livepatch.c +74 −0
  21. kernel/module/main.c +211 −1882
  22. kernel/module/procfs.c +146 −0
  23. kernel/module/signing.c +122 −0
  24. kernel/module/strict_rwx.c +143 −0
  25. kernel/module/sysfs.c +436 −0
  26. kernel/module/tracking.c +61 −0
  27. kernel/module/tree_lookup.c +117 −0
  28. kernel/module/version.c +101 −0
  29. kernel/module_signing.c +0 −45
MAINTAINERS+3−1
@@ -10987,6 +10987,7 @@ F: drivers/tty/serial/kgdboc.c · 10986 unmodified lines
10987F: include/linux/kdb.h10987F: include/linux/kdb.h
10988F: include/linux/kgdb.h10988F: include/linux/kgdb.h
10989F: kernel/debug/10989F: kernel/debug/
10990F: kernel/module/kdb.c
10990 10991
10991KHADAS MCU MFD DRIVER10992KHADAS MCU MFD DRIVER
10992M: Neil Armstrong <narmstrong@baylibre.com>10993M: Neil Armstrong <narmstrong@baylibre.com>
@@ -11440,6 +11441,7 @@ F: arch/s390/include/asm/livepatch.h · 447 unmodified lines
11440F: arch/x86/include/asm/livepatch.h11441F: arch/x86/include/asm/livepatch.h
11441F: include/linux/livepatch.h11442F: include/linux/livepatch.h
11442F: kernel/livepatch/11443F: kernel/livepatch/
11444F: kernel/module/livepatch.c
11443F: lib/livepatch/11445F: lib/livepatch/
11444F: samples/livepatch/11446F: samples/livepatch/
11445F: tools/testing/selftests/livepatch/11447F: tools/testing/selftests/livepatch/
@@ -13371,7 +13373,7 @@ L: linux-kernel@vger.kernel.org · 1925 unmodified lines
13371S: Maintained13373S: Maintained
13372T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next13374T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next
13373F: include/linux/module.h13375F: include/linux/module.h
13374F: kernel/module.c13376F: kernel/module/
13375 13377
13376MONOLITHIC POWER SYSTEM PMIC DRIVER13378MONOLITHIC POWER SYSTEM PMIC DRIVER
13377M: Saravanan Sekar <sravanhome@gmail.com>13379M: Saravanan Sekar <sravanhome@gmail.com>
arch/Kconfig+6−0
@@ -892,6 +892,12 @@ config MODULES_USE_ELF_REL · 891 unmodified lines
892 Modules only use ELF REL relocations. Modules with ELF RELA892 Modules only use ELF REL relocations. Modules with ELF RELA
893 relocations will give an error.893 relocations will give an error.
894 894
895config ARCH_WANTS_MODULES_DATA_IN_VMALLOC
896 bool
897 help
898 For architectures like powerpc/32 which have constraints on module
899 allocation and need to allocate module data outside of module area.
900
895config HAVE_IRQ_EXIT_ON_IRQ_STACK901config HAVE_IRQ_EXIT_ON_IRQ_STACK
896 bool902 bool
897 help903 help
arch/powerpc/Kconfig+1−0
@@ -158,6 +158,7 @@ config PPC · 157 unmodified lines
158 select ARCH_WANT_IPC_PARSE_VERSION158 select ARCH_WANT_IPC_PARSE_VERSION
159 select ARCH_WANT_IRQS_OFF_ACTIVATE_MM159 select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
160 select ARCH_WANT_LD_ORPHAN_WARN160 select ARCH_WANT_LD_ORPHAN_WARN
161 select ARCH_WANTS_MODULES_DATA_IN_VMALLOC if PPC_BOOK3S_32 || PPC_8xx
161 select ARCH_WEAK_RELEASE_ACQUIRE162 select ARCH_WEAK_RELEASE_ACQUIRE
162 select BINFMT_ELF163 select BINFMT_ELF
163 select BUILDTIME_TABLE_SORT164 select BUILDTIME_TABLE_SORT
include/linux/kdb.h+1−0
@@ -222,5 +222,6 @@ enum { · 221 unmodified lines
222 222
223extern int kdbgetintenv(const char *, int *);223extern int kdbgetintenv(const char *, int *);
224extern int kdb_set(int, const char **);224extern int kdb_set(int, const char **);
225int kdb_lsmod(int argc, const char **argv);
225 226
226#endif /* !_KDB_H */227#endif /* !_KDB_H */
include/linux/module.h+19−13
@@ -290,8 +290,7 @@ extern typeof(name) __mod_##type##__##name##_device_table \ · 289 unmodified lines
290 * files require multiple MODULE_FIRMWARE() specifiers */290 * files require multiple MODULE_FIRMWARE() specifiers */
291#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)291#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
292 292
293#define _MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)293#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, __stringify(ns))
294#define MODULE_IMPORT_NS(ns) _MODULE_IMPORT_NS(ns)
295 294
296struct notifier_block;295struct notifier_block;
297 296
@@ -422,6 +421,9 @@ struct module { · 124 unmodified lines
422 /* Core layout: rbtree is accessed frequently, so keep together. */421 /* Core layout: rbtree is accessed frequently, so keep together. */
423 struct module_layout core_layout __module_layout_align;422 struct module_layout core_layout __module_layout_align;
424 struct module_layout init_layout;423 struct module_layout init_layout;
424#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
425 struct module_layout data_layout;
426#endif
425 427
426 /* Arch-specific module values */428 /* Arch-specific module values */
427 struct mod_arch_specific arch;429 struct mod_arch_specific arch;
@@ -569,6 +571,11 @@ bool is_module_text_address(unsigned long addr); · 141 unmodified lines
569static inline bool within_module_core(unsigned long addr,571static inline bool within_module_core(unsigned long addr,
570 const struct module *mod)572 const struct module *mod)
571{573{
574#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
575 if ((unsigned long)mod->data_layout.base <= addr &&
576 addr < (unsigned long)mod->data_layout.base + mod->data_layout.size)
577 return true;
578#endif
572 return (unsigned long)mod->core_layout.base <= addr &&579 return (unsigned long)mod->core_layout.base <= addr &&
573 addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;580 addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;
574}581}
@@ -663,19 +670,15 @@ static inline bool module_requested_async_probing(struct module *module) · 88 unmodified lines
663 return module && module->async_probe_requested;670 return module && module->async_probe_requested;
664}671}
665 672
666#ifdef CONFIG_LIVEPATCH
667static inline bool is_livepatch_module(struct module *mod)673static inline bool is_livepatch_module(struct module *mod)
668{674{
675#ifdef CONFIG_LIVEPATCH
669 return mod->klp;676 return mod->klp;
670}677#else
671#else /* !CONFIG_LIVEPATCH */
672static inline bool is_livepatch_module(struct module *mod)
673{
674 return false;678 return false;
679#endif
675}680}
676#endif /* CONFIG_LIVEPATCH */
677 681
678bool is_module_sig_enforced(void);
679void set_module_sig_enforced(void);682void set_module_sig_enforced(void);
680 683
681#else /* !CONFIG_MODULES... */684#else /* !CONFIG_MODULES... */
@@ -802,10 +805,6 @@ static inline bool module_requested_async_probing(struct module *module) · 120 unmodified lines
802 return false;805 return false;
803}806}
804 807
805static inline bool is_module_sig_enforced(void)
806{
807 return false;
808}
809 808
810static inline void set_module_sig_enforced(void)809static inline void set_module_sig_enforced(void)
811{810{
@@ -857,11 +856,18 @@ static inline bool retpoline_module_ok(bool has_retpoline) · 45 unmodified lines
857#endif856#endif
858 857
859#ifdef CONFIG_MODULE_SIG858#ifdef CONFIG_MODULE_SIG
859bool is_module_sig_enforced(void);
860
860static inline bool module_sig_ok(struct module *module)861static inline bool module_sig_ok(struct module *module)
861{862{
862 return module->sig_ok;863 return module->sig_ok;
863}864}
864#else /* !CONFIG_MODULE_SIG */865#else /* !CONFIG_MODULE_SIG */
866static inline bool is_module_sig_enforced(void)
867{
868 return false;
869}
870
865static inline bool module_sig_ok(struct module *module)871static inline bool module_sig_ok(struct module *module)
866{872{
867 return true;873 return true;
init/Kconfig+11−0
@@ -1983,6 +1983,17 @@ config MODULE_FORCE_UNLOAD · 1982 unmodified lines
1983 rmmod). This is mainly for kernel developers and desperate users.1983 rmmod). This is mainly for kernel developers and desperate users.
1984 If unsure, say N.1984 If unsure, say N.
1985 1985
1986config MODULE_UNLOAD_TAINT_TRACKING
1987 bool "Tainted module unload tracking"
1988 depends on MODULE_UNLOAD
1989 default n
1990 help
1991 This option allows you to maintain a record of each unloaded
1992 module that tainted the kernel. In addition to displaying a
1993 list of linked (or loaded) modules e.g. on detection of a bad
1994 page (see bad_page()), the aforementioned details are also
1995 shown. If unsure, say N.
1996
1986config MODVERSIONS1997config MODVERSIONS
1987 bool "Module versioning support"1998 bool "Module versioning support"
1988 help1999 help
kernel/Makefile+1−4
@@ -29,7 +29,6 @@ KCOV_INSTRUMENT_softirq.o := n · 28 unmodified lines
29KCSAN_SANITIZE_softirq.o = n29KCSAN_SANITIZE_softirq.o = n
30# These are called from save_stack_trace() on slub debug path,30# These are called from save_stack_trace() on slub debug path,
31# and produce insane amounts of uninteresting coverage.31# and produce insane amounts of uninteresting coverage.
32KCOV_INSTRUMENT_module.o := n
33KCOV_INSTRUMENT_extable.o := n32KCOV_INSTRUMENT_extable.o := n
34KCOV_INSTRUMENT_stacktrace.o := n33KCOV_INSTRUMENT_stacktrace.o := n
35# Don't self-instrument.34# Don't self-instrument.
@@ -53,6 +52,7 @@ obj-y += rcu/ · 17 unmodified lines
53obj-y += livepatch/52obj-y += livepatch/
54obj-y += dma/53obj-y += dma/
55obj-y += entry/54obj-y += entry/
55obj-$(CONFIG_MODULES) += module/
56 56
57obj-$(CONFIG_KCMP) += kcmp.o57obj-$(CONFIG_KCMP) += kcmp.o
58obj-$(CONFIG_FREEZER) += freezer.o58obj-$(CONFIG_FREEZER) += freezer.o
@@ -66,9 +66,6 @@ ifneq ($(CONFIG_SMP),y) · 7 unmodified lines
66obj-y += up.o66obj-y += up.o
67endif67endif
68obj-$(CONFIG_UID16) += uid16.o68obj-$(CONFIG_UID16) += uid16.o
69obj-$(CONFIG_MODULES) += module.o
70obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
71obj-$(CONFIG_MODULE_SIG) += module_signing.o
72obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o69obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
73obj-$(CONFIG_KALLSYMS) += kallsyms.o70obj-$(CONFIG_KALLSYMS) += kallsyms.o
74obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o71obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
kernel/debug/kdb/kdb_io.c+0−1
@@ -9,7 +9,6 @@ · 8 unmodified lines
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
10 */10 */
11 11
12#include <linux/module.h>
13#include <linux/types.h>12#include <linux/types.h>
14#include <linux/ctype.h>13#include <linux/ctype.h>
15#include <linux/kernel.h>14#include <linux/kernel.h>
kernel/debug/kdb/kdb_keyboard.c+0−1
@@ -11,7 +11,6 @@ · 10 unmodified lines
11#include <linux/kdb.h>11#include <linux/kdb.h>
12#include <linux/keyboard.h>12#include <linux/keyboard.h>
13#include <linux/ctype.h>13#include <linux/ctype.h>
14#include <linux/module.h>
15#include <linux/io.h>14#include <linux/io.h>
16 15
17/* Keyboard Controller Registers on normal PCs. */16/* Keyboard Controller Registers on normal PCs. */
kernel/debug/kdb/kdb_main.c+0−49
@@ -26,7 +26,6 @@ · 25 unmodified lines
26#include <linux/utsname.h>26#include <linux/utsname.h>
27#include <linux/vmalloc.h>27#include <linux/vmalloc.h>
28#include <linux/atomic.h>28#include <linux/atomic.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>29#include <linux/moduleparam.h>
31#include <linux/mm.h>30#include <linux/mm.h>
32#include <linux/init.h>31#include <linux/init.h>
@@ -2060,54 +2059,6 @@ static int kdb_ef(int argc, const char **argv) · 2027 unmodified lines
2060 return 0;2059 return 0;
2061}2060}
2062 2061
2063#if defined(CONFIG_MODULES)
2064/*
2065 * kdb_lsmod - This function implements the 'lsmod' command. Lists
2066 * currently loaded kernel modules.
2067 * Mostly taken from userland lsmod.
2068 */
2069static int kdb_lsmod(int argc, const char **argv)
2070{
2071 struct module *mod;
2072
2073 if (argc != 0)
2074 return KDB_ARGCOUNT;
2075
2076 kdb_printf("Module Size modstruct Used by\n");
2077 list_for_each_entry(mod, kdb_modules, list) {
2078 if (mod->state == MODULE_STATE_UNFORMED)
2079 continue;
2080
2081 kdb_printf("%-20s%8u 0x%px ", mod->name,
2082 mod->core_layout.size, (void *)mod);
2083#ifdef CONFIG_MODULE_UNLOAD
2084 kdb_printf("%4d ", module_refcount(mod));
2085#endif
2086 if (mod->state == MODULE_STATE_GOING)
2087 kdb_printf(" (Unloading)");
2088 else if (mod->state == MODULE_STATE_COMING)
2089 kdb_printf(" (Loading)");
2090 else
2091 kdb_printf(" (Live)");
2092 kdb_printf(" 0x%px", mod->core_layout.base);
2093
2094#ifdef CONFIG_MODULE_UNLOAD
2095 {
2096 struct module_use *use;
2097 kdb_printf(" [ ");
2098 list_for_each_entry(use, &mod->source_list,
2099 source_list)
2100 kdb_printf("%s ", use->target->name);
2101 kdb_printf("]\n");
2102 }
2103#endif
2104 }
2105
2106 return 0;
2107}
2108
2109#endif /* CONFIG_MODULES */
2110
2111/*2062/*
2112 * kdb_env - This function implements the 'env' command. Display the2063 * kdb_env - This function implements the 'env' command. Display the
2113 * current environment variables.2064 * current environment variables.
kernel/debug/kdb/kdb_private.h+0−4
@@ -226,10 +226,6 @@ extern void kdb_kbd_cleanup_state(void); · 225 unmodified lines
226#define kdb_kbd_cleanup_state()226#define kdb_kbd_cleanup_state()
227#endif /* ! CONFIG_KDB_KEYBOARD */227#endif /* ! CONFIG_KDB_KEYBOARD */
228 228
229#ifdef CONFIG_MODULES
230extern struct list_head *kdb_modules;
231#endif /* CONFIG_MODULES */
232
233extern char kdb_prompt_str[];229extern char kdb_prompt_str[];
234 230
235#define KDB_WORD_SIZE ((int)sizeof(unsigned long))231#define KDB_WORD_SIZE ((int)sizeof(unsigned long))
kernel/debug/kdb/kdb_support.c+0−1
@@ -17,7 +17,6 @@ · 16 unmodified lines
17#include <linux/stddef.h>17#include <linux/stddef.h>
18#include <linux/vmalloc.h>18#include <linux/vmalloc.h>
19#include <linux/ptrace.h>19#include <linux/ptrace.h>
20#include <linux/module.h>
21#include <linux/highmem.h>20#include <linux/highmem.h>
22#include <linux/hardirq.h>21#include <linux/hardirq.h>
23#include <linux/delay.h>22#include <linux/delay.h>
kernel/module-internal.h+0−50
@@ -1,50 +0,0 @@
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* Module internals
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/elf.h>
9#include <asm/module.h>
10
11struct load_info {
12 const char *name;
13 /* pointer to module in temporary copy, freed at end of load_module() */
14 struct module *mod;
15 Elf_Ehdr *hdr;
16 unsigned long len;
17 Elf_Shdr *sechdrs;
18 char *secstrings, *strtab;
19 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
20 struct _ddebug *debug;
21 unsigned int num_debug;
22 bool sig_ok;
23#ifdef CONFIG_KALLSYMS
24 unsigned long mod_kallsyms_init_off;
25#endif
26#ifdef CONFIG_MODULE_DECOMPRESS
27 struct page **pages;
28 unsigned int max_pages;
29 unsigned int used_pages;
30#endif
31 struct {
32 unsigned int sym, str, mod, vers, info, pcpu;
33 } index;
34};
35
36extern int mod_verify_sig(const void *mod, struct load_info *info);
37
38#ifdef CONFIG_MODULE_DECOMPRESS
39int module_decompress(struct load_info *info, const void *buf, size_t size);
40void module_decompress_cleanup(struct load_info *info);
41#else
42static inline int module_decompress(struct load_info *info,
43 const void *buf, size_t size)
44{
45 return -EOPNOTSUPP;
46}
47static inline void module_decompress_cleanup(struct load_info *info)
48{
49}
50#endif
kernel/module/Makefile+21−0
@@ -0,0 +1,21 @@
1# SPDX-License-Identifier: GPL-2.0-only
2#
3# Makefile for linux kernel module support
4#
5
6# These are called from save_stack_trace() on slub debug path,
7# and produce insane amounts of uninteresting coverage.
8KCOV_INSTRUMENT_module.o := n
9
10obj-y += main.o strict_rwx.o
11obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
12obj-$(CONFIG_MODULE_SIG) += signing.o
13obj-$(CONFIG_LIVEPATCH) += livepatch.o
14obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
15obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
16obj-$(CONFIG_KALLSYMS) += kallsyms.o
17obj-$(CONFIG_PROC_FS) += procfs.o
18obj-$(CONFIG_SYSFS) += sysfs.o
19obj-$(CONFIG_KGDB_KDB) += kdb.o
20obj-$(CONFIG_MODVERSIONS) += version.o
21obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) += tracking.o
kernel/module/debug_kmemleak.c+30−0
@@ -0,0 +1,30 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module kmemleak support
4 *
5 * Copyright (C) 2009 Catalin Marinas
6 */
7
8#include <linux/module.h>
9#include <linux/kmemleak.h>
10#include "internal.h"
11
12void kmemleak_load_module(const struct module *mod,
13 const struct load_info *info)
14{
15 unsigned int i;
16
17 /* only scan the sections containing data */
18 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
19
20 for (i = 1; i < info->hdr->e_shnum; i++) {
21 /* Scan all writable sections that's not executable */
22 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
23 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
24 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
25 continue;
26
27 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
28 info->sechdrs[i].sh_size, GFP_KERNEL);
29 }
30}
kernel/module/decompress.crenamed from kernel/module_decompress.c+4−1
@@ -12,7 +12,7 @@ · 11 unmodified lines
12#include <linux/sysfs.h>12#include <linux/sysfs.h>
13#include <linux/vmalloc.h>13#include <linux/vmalloc.h>
14 14
15#include "module-internal.h"15#include "internal.h"
16 16
17static int module_extend_max_pages(struct load_info *info, unsigned int extent)17static int module_extend_max_pages(struct load_info *info, unsigned int extent)
18{18{
@@ -113,6 +113,7 @@ static ssize_t module_gzip_decompress(struct load_info *info, · 94 unmodified lines
113 113
114 do {114 do {
115 struct page *page = module_get_next_page(info);115 struct page *page = module_get_next_page(info);
116
116 if (!page) {117 if (!page) {
117 retval = -ENOMEM;118 retval = -ENOMEM;
118 goto out_inflate_end;119 goto out_inflate_end;
@@ -171,6 +172,7 @@ static ssize_t module_xz_decompress(struct load_info *info, · 52 unmodified lines
171 172
172 do {173 do {
173 struct page *page = module_get_next_page(info);174 struct page *page = module_get_next_page(info);
175
174 if (!page) {176 if (!page) {
175 retval = -ENOMEM;177 retval = -ENOMEM;
176 goto out;178 goto out;
@@ -256,6 +258,7 @@ static ssize_t compression_show(struct kobject *kobj, · 79 unmodified lines
256{258{
257 return sysfs_emit(buf, "%s\n", __stringify(MODULE_COMPRESSION));259 return sysfs_emit(buf, "%s\n", __stringify(MODULE_COMPRESSION));
258}260}
261
259static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);262static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
260 263
261static int __init module_decompress_sysfs_init(void)264static int __init module_decompress_sysfs_init(void)
kernel/module/internal.h+302−0
@@ -0,0 +1,302 @@
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* Module internals
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/elf.h>
9#include <linux/compiler.h>
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/rculist.h>
13#include <linux/rcupdate.h>
14
15#ifndef ARCH_SHF_SMALL
16#define ARCH_SHF_SMALL 0
17#endif
18
19/* If this is set, the section belongs in the init part of the module */
20#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
21/* Maximum number of characters written by module_flags() */
22#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
23
24#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
25#define data_layout core_layout
26#endif
27
28/*
29 * Modules' sections will be aligned on page boundaries
30 * to ensure complete separation of code and data, but
31 * only when CONFIG_STRICT_MODULE_RWX=y
32 */
33#ifdef CONFIG_STRICT_MODULE_RWX
34# define strict_align(X) PAGE_ALIGN(X)
35#else
36# define strict_align(X) (X)
37#endif
38
39extern struct mutex module_mutex;
40extern struct list_head modules;
41
42extern struct module_attribute *modinfo_attrs[];
43extern size_t modinfo_attrs_count;
44
45/* Provided by the linker */
46extern const struct kernel_symbol __start___ksymtab[];
47extern const struct kernel_symbol __stop___ksymtab[];
48extern const struct kernel_symbol __start___ksymtab_gpl[];
49extern const struct kernel_symbol __stop___ksymtab_gpl[];
50extern const s32 __start___kcrctab[];
51extern const s32 __start___kcrctab_gpl[];
52
53struct load_info {
54 const char *name;
55 /* pointer to module in temporary copy, freed at end of load_module() */
56 struct module *mod;
57 Elf_Ehdr *hdr;
58 unsigned long len;
59 Elf_Shdr *sechdrs;
60 char *secstrings, *strtab;
61 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs;
62 struct _ddebug *debug;
63 unsigned int num_debug;
64 bool sig_ok;
65#ifdef CONFIG_KALLSYMS
66 unsigned long mod_kallsyms_init_off;
67#endif
68#ifdef CONFIG_MODULE_DECOMPRESS
69 struct page **pages;
70 unsigned int max_pages;
71 unsigned int used_pages;
72#endif
73 struct {
74 unsigned int sym, str, mod, vers, info, pcpu;
75 } index;
76};
77
78enum mod_license {
79 NOT_GPL_ONLY,
80 GPL_ONLY,
81};
82
83struct find_symbol_arg {
84 /* Input */
85 const char *name;
86 bool gplok;
87 bool warn;
88
89 /* Output */
90 struct module *owner;
91 const s32 *crc;
92 const struct kernel_symbol *sym;
93 enum mod_license license;
94};
95
96int mod_verify_sig(const void *mod, struct load_info *info);
97int try_to_force_load(struct module *mod, const char *reason);
98bool find_symbol(struct find_symbol_arg *fsa);
99struct module *find_module_all(const char *name, size_t len, bool even_unformed);
100int cmp_name(const void *name, const void *sym);
101long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
102 unsigned int section);
103char *module_flags(struct module *mod, char *buf);
104size_t module_flags_taint(unsigned long taints, char *buf);
105
106static inline void module_assert_mutex_or_preempt(void)
107{
108#ifdef CONFIG_LOCKDEP
109 if (unlikely(!debug_locks))
110 return;
111
112 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
113 !lockdep_is_held(&module_mutex));
114#endif
115}
116
117static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
118{
119#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
120 return (unsigned long)offset_to_ptr(&sym->value_offset);
121#else
122 return sym->value;
123#endif
124}
125
126#ifdef CONFIG_LIVEPATCH
127int copy_module_elf(struct module *mod, struct load_info *info);
128void free_module_elf(struct module *mod);
129#else /* !CONFIG_LIVEPATCH */
130static inline int copy_module_elf(struct module *mod, struct load_info *info)
131{
132 return 0;
133}
134
135static inline void free_module_elf(struct module *mod) { }
136#endif /* CONFIG_LIVEPATCH */
137
138static inline bool set_livepatch_module(struct module *mod)
139{
140#ifdef CONFIG_LIVEPATCH
141 mod->klp = true;
142 return true;
143#else
144 return false;
145#endif
146}
147
148#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING
149struct mod_unload_taint {
150 struct list_head list;
151 char name[MODULE_NAME_LEN];
152 unsigned long taints;
153 u64 count;
154};
155
156int try_add_tainted_module(struct module *mod);
157void print_unloaded_tainted_modules(void);
158#else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
159static inline int try_add_tainted_module(struct module *mod)
160{
161 return 0;
162}
163
164static inline void print_unloaded_tainted_modules(void)
165{
166}
167#endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
168
169#ifdef CONFIG_MODULE_DECOMPRESS
170int module_decompress(struct load_info *info, const void *buf, size_t size);
171void module_decompress_cleanup(struct load_info *info);
172#else
173static inline int module_decompress(struct load_info *info,
174 const void *buf, size_t size)
175{
176 return -EOPNOTSUPP;
177}
178
179static inline void module_decompress_cleanup(struct load_info *info)
180{
181}
182#endif
183
184struct mod_tree_root {
185#ifdef CONFIG_MODULES_TREE_LOOKUP
186 struct latch_tree_root root;
187#endif
188 unsigned long addr_min;
189 unsigned long addr_max;
190};
191
192extern struct mod_tree_root mod_tree;
193extern struct mod_tree_root mod_data_tree;
194
195#ifdef CONFIG_MODULES_TREE_LOOKUP
196void mod_tree_insert(struct module *mod);
197void mod_tree_remove_init(struct module *mod);
198void mod_tree_remove(struct module *mod);
199struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
200#else /* !CONFIG_MODULES_TREE_LOOKUP */
201
202static inline void mod_tree_insert(struct module *mod) { }
203static inline void mod_tree_remove_init(struct module *mod) { }
204static inline void mod_tree_remove(struct module *mod) { }
205static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
206{
207 struct module *mod;
208
209 list_for_each_entry_rcu(mod, &modules, list,
210 lockdep_is_held(&module_mutex)) {
211 if (within_module(addr, mod))
212 return mod;
213 }
214
215 return NULL;
216}
217#endif /* CONFIG_MODULES_TREE_LOOKUP */
218
219void module_enable_ro(const struct module *mod, bool after_init);
220void module_enable_nx(const struct module *mod);
221void module_enable_x(const struct module *mod);
222int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
223 char *secstrings, struct module *mod);
224bool module_check_misalignment(const struct module *mod);
225
226#ifdef CONFIG_MODULE_SIG
227int module_sig_check(struct load_info *info, int flags);
228#else /* !CONFIG_MODULE_SIG */
229static inline int module_sig_check(struct load_info *info, int flags)
230{
231 return 0;
232}
233#endif /* !CONFIG_MODULE_SIG */
234
235#ifdef CONFIG_DEBUG_KMEMLEAK
236void kmemleak_load_module(const struct module *mod, const struct load_info *info);
237#else /* !CONFIG_DEBUG_KMEMLEAK */
238static inline void kmemleak_load_module(const struct module *mod,
239 const struct load_info *info) { }
240#endif /* CONFIG_DEBUG_KMEMLEAK */
241
242#ifdef CONFIG_KALLSYMS
243void init_build_id(struct module *mod, const struct load_info *info);
244void layout_symtab(struct module *mod, struct load_info *info);
245void add_kallsyms(struct module *mod, const struct load_info *info);
246unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
247
248static inline bool sect_empty(const Elf_Shdr *sect)
249{
250 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
251}
252#else /* !CONFIG_KALLSYMS */
253static inline void init_build_id(struct module *mod, const struct load_info *info) { }
254static inline void layout_symtab(struct module *mod, struct load_info *info) { }
255static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
256#endif /* CONFIG_KALLSYMS */
257
258#ifdef CONFIG_SYSFS
259int mod_sysfs_setup(struct module *mod, const struct load_info *info,
260 struct kernel_param *kparam, unsigned int num_params);
261void mod_sysfs_teardown(struct module *mod);
262void init_param_lock(struct module *mod);
263#else /* !CONFIG_SYSFS */
264static inline int mod_sysfs_setup(struct module *mod,
265 const struct load_info *info,
266 struct kernel_param *kparam,
267 unsigned int num_params)
268{
269 return 0;
270}
271
272static inline void mod_sysfs_teardown(struct module *mod) { }
273static inline void init_param_lock(struct module *mod) { }
274#endif /* CONFIG_SYSFS */
275
276#ifdef CONFIG_MODVERSIONS
277int check_version(const struct load_info *info,
278 const char *symname, struct module *mod, const s32 *crc);
279void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
280 struct kernel_symbol *ks, struct tracepoint * const *tp);
281int check_modstruct_version(const struct load_info *info, struct module *mod);
282int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
283#else /* !CONFIG_MODVERSIONS */
284static inline int check_version(const struct load_info *info,
285 const char *symname,
286 struct module *mod,
287 const s32 *crc)
288{
289 return 1;
290}
291
292static inline int check_modstruct_version(const struct load_info *info,
293 struct module *mod)
294{
295 return 1;
296}
297
298static inline int same_magic(const char *amagic, const char *bmagic, bool has_crcs)
299{
300 return strcmp(amagic, bmagic) == 0;
301}
302#endif /* CONFIG_MODVERSIONS */
kernel/module/kallsyms.c+512−0
@@ -0,0 +1,512 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module kallsyms support
4 *
5 * Copyright (C) 2010 Rusty Russell
6 */
7
8#include <linux/module.h>
9#include <linux/kallsyms.h>
10#include <linux/buildid.h>
11#include <linux/bsearch.h>
12#include "internal.h"
13
14/* Lookup exported symbol in given range of kernel_symbols */
15static const struct kernel_symbol *lookup_exported_symbol(const char *name,
16 const struct kernel_symbol *start,
17 const struct kernel_symbol *stop)
18{
19 return bsearch(name, start, stop - start,
20 sizeof(struct kernel_symbol), cmp_name);
21}
22
23static int is_exported(const char *name, unsigned long value,
24 const struct module *mod)
25{
26 const struct kernel_symbol *ks;
27
28 if (!mod)
29 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
30 else
31 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
32
33 return ks && kernel_symbol_value(ks) == value;
34}
35
36/* As per nm */
37static char elf_type(const Elf_Sym *sym, const struct load_info *info)
38{
39 const Elf_Shdr *sechdrs = info->sechdrs;
40
41 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
42 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
43 return 'v';
44 else
45 return 'w';
46 }
47 if (sym->st_shndx == SHN_UNDEF)
48 return 'U';
49 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
50 return 'a';
51 if (sym->st_shndx >= SHN_LORESERVE)
52 return '?';
53 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
54 return 't';
55 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC &&
56 sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
57 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
58 return 'r';
59 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
60 return 'g';
61 else
62 return 'd';
63 }
64 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
65 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
66 return 's';
67 else
68 return 'b';
69 }
70 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
71 ".debug")) {
72 return 'n';
73 }
74 return '?';
75}
76
77static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
78 unsigned int shnum, unsigned int pcpundx)
79{
80 const Elf_Shdr *sec;
81
82 if (src->st_shndx == SHN_UNDEF ||
83 src->st_shndx >= shnum ||
84 !src->st_name)
85 return false;
86
87#ifdef CONFIG_KALLSYMS_ALL
88 if (src->st_shndx == pcpundx)
89 return true;
90#endif
91
92 sec = sechdrs + src->st_shndx;
93 if (!(sec->sh_flags & SHF_ALLOC)
94#ifndef CONFIG_KALLSYMS_ALL
95 || !(sec->sh_flags & SHF_EXECINSTR)
96#endif
97 || (sec->sh_entsize & INIT_OFFSET_MASK))
98 return false;
99
100 return true;
101}
102
103/*
104 * We only allocate and copy the strings needed by the parts of symtab
105 * we keep. This is simple, but has the effect of making multiple
106 * copies of duplicates. We could be more sophisticated, see
107 * linux-kernel thread starting with
108 * <73defb5e4bca04a6431392cc341112b1@localhost>.
109 */
110void layout_symtab(struct module *mod, struct load_info *info)
111{
112 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
113 Elf_Shdr *strsect = info->sechdrs + info->index.str;
114 const Elf_Sym *src;
115 unsigned int i, nsrc, ndst, strtab_size = 0;
116
117 /* Put symbol section at end of init part of module. */
118 symsect->sh_flags |= SHF_ALLOC;
119 symsect->sh_entsize = module_get_offset(mod, &mod->init_layout.size, symsect,
120 info->index.sym) | INIT_OFFSET_MASK;
121 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
122
123 src = (void *)info->hdr + symsect->sh_offset;
124 nsrc = symsect->sh_size / sizeof(*src);
125
126 /* Compute total space required for the core symbols' strtab. */
127 for (ndst = i = 0; i < nsrc; i++) {
128 if (i == 0 || is_livepatch_module(mod) ||
129 is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
130 info->index.pcpu)) {
131 strtab_size += strlen(&info->strtab[src[i].st_name]) + 1;
132 ndst++;
133 }
134 }
135
136 /* Append room for core symbols at end of core part. */
137 info->symoffs = ALIGN(mod->data_layout.size, symsect->sh_addralign ?: 1);
138 info->stroffs = mod->data_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
139 mod->data_layout.size += strtab_size;
140 info->core_typeoffs = mod->data_layout.size;
141 mod->data_layout.size += ndst * sizeof(char);
142 mod->data_layout.size = strict_align(mod->data_layout.size);
143
144 /* Put string table section at end of init part of module. */
145 strsect->sh_flags |= SHF_ALLOC;
146 strsect->sh_entsize = module_get_offset(mod, &mod->init_layout.size, strsect,
147 info->index.str) | INIT_OFFSET_MASK;
148 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
149
150 /* We'll tack temporary mod_kallsyms on the end. */
151 mod->init_layout.size = ALIGN(mod->init_layout.size,
152 __alignof__(struct mod_kallsyms));
153 info->mod_kallsyms_init_off = mod->init_layout.size;
154 mod->init_layout.size += sizeof(struct mod_kallsyms);
155 info->init_typeoffs = mod->init_layout.size;
156 mod->init_layout.size += nsrc * sizeof(char);
157 mod->init_layout.size = strict_align(mod->init_layout.size);
158}
159
160/*
161 * We use the full symtab and strtab which layout_symtab arranged to
162 * be appended to the init section. Later we switch to the cut-down
163 * core-only ones.
164 */
165void add_kallsyms(struct module *mod, const struct load_info *info)
166{
167 unsigned int i, ndst;
168 const Elf_Sym *src;
169 Elf_Sym *dst;
170 char *s;
171 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
172
173 /* Set up to point into init section. */
174 mod->kallsyms = (void __rcu *)mod->init_layout.base +
175 info->mod_kallsyms_init_off;
176
177 preempt_disable();
178 /* The following is safe since this pointer cannot change */
179 rcu_dereference_sched(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
180 rcu_dereference_sched(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
181 /* Make sure we get permanent strtab: don't use info->strtab. */
182 rcu_dereference_sched(mod->kallsyms)->strtab =
183 (void *)info->sechdrs[info->index.str].sh_addr;
184 rcu_dereference_sched(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
185
186 /*
187 * Now populate the cut down core kallsyms for after init
188 * and set types up while we still have access to sections.
189 */
190 mod->core_kallsyms.symtab = dst = mod->data_layout.base + info->symoffs;
191 mod->core_kallsyms.strtab = s = mod->data_layout.base + info->stroffs;
192 mod->core_kallsyms.typetab = mod->data_layout.base + info->core_typeoffs;
193 src = rcu_dereference_sched(mod->kallsyms)->symtab;
194 for (ndst = i = 0; i < rcu_dereference_sched(mod->kallsyms)->num_symtab; i++) {
195 rcu_dereference_sched(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
196 if (i == 0 || is_livepatch_module(mod) ||
197 is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
198 info->index.pcpu)) {
199 mod->core_kallsyms.typetab[ndst] =
200 rcu_dereference_sched(mod->kallsyms)->typetab[i];
201 dst[ndst] = src[i];
202 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
203 s += strscpy(s,
204 &rcu_dereference_sched(mod->kallsyms)->strtab[src[i].st_name],
205 KSYM_NAME_LEN) + 1;
206 }
207 }
208 preempt_enable();
209 mod->core_kallsyms.num_symtab = ndst;
210}
211
212#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
213void init_build_id(struct module *mod, const struct load_info *info)
214{
215 const Elf_Shdr *sechdr;
216 unsigned int i;
217
218 for (i = 0; i < info->hdr->e_shnum; i++) {
219 sechdr = &info->sechdrs[i];
220 if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE &&
221 !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id,
222 sechdr->sh_size))
223 break;
224 }
225}
226#else
227void init_build_id(struct module *mod, const struct load_info *info)
228{
229}
230#endif
231
232/*
233 * This ignores the intensely annoying "mapping symbols" found
234 * in ARM ELF files: $a, $t and $d.
235 */
236static inline int is_arm_mapping_symbol(const char *str)
237{
238 if (str[0] == '.' && str[1] == 'L')
239 return true;
240 return str[0] == '$' && strchr("axtd", str[1]) &&
241 (str[2] == '\0' || str[2] == '.');
242}
243
244static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
245{
246 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
247}
248
249/*
250 * Given a module and address, find the corresponding symbol and return its name
251 * while providing its size and offset if needed.
252 */
253static const char *find_kallsyms_symbol(struct module *mod,
254 unsigned long addr,
255 unsigned long *size,
256 unsigned long *offset)
257{
258 unsigned int i, best = 0;
259 unsigned long nextval, bestval;
260 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
261
262 /* At worse, next value is at end of module */
263 if (within_module_init(addr, mod))
264 nextval = (unsigned long)mod->init_layout.base + mod->init_layout.text_size;
265 else
266 nextval = (unsigned long)mod->core_layout.base + mod->core_layout.text_size;
267
268 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
269
270 /*
271 * Scan for closest preceding symbol, and next symbol. (ELF
272 * starts real symbols at 1).
273 */
274 for (i = 1; i < kallsyms->num_symtab; i++) {
275 const Elf_Sym *sym = &kallsyms->symtab[i];
276 unsigned long thisval = kallsyms_symbol_value(sym);
277
278 if (sym->st_shndx == SHN_UNDEF)
279 continue;
280
281 /*
282 * We ignore unnamed symbols: they're uninformative
283 * and inserted at a whim.
284 */
285 if (*kallsyms_symbol_name(kallsyms, i) == '\0' ||
286 is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
287 continue;
288
289 if (thisval <= addr && thisval > bestval) {
290 best = i;
291 bestval = thisval;
292 }
293 if (thisval > addr && thisval < nextval)
294 nextval = thisval;
295 }
296
297 if (!best)
298 return NULL;
299
300 if (size)
301 *size = nextval - bestval;
302 if (offset)
303 *offset = addr - bestval;
304
305 return kallsyms_symbol_name(kallsyms, best);
306}
307
308void * __weak dereference_module_function_descriptor(struct module *mod,
309 void *ptr)
310{
311 return ptr;
312}
313
314/*
315 * For kallsyms to ask for address resolution. NULL means not found. Careful
316 * not to lock to avoid deadlock on oopses, simply disable preemption.
317 */
318const char *module_address_lookup(unsigned long addr,
319 unsigned long *size,
320 unsigned long *offset,
321 char **modname,
322 const unsigned char **modbuildid,
323 char *namebuf)
324{
325 const char *ret = NULL;
326 struct module *mod;
327
328 preempt_disable();
329 mod = __module_address(addr);
330 if (mod) {
331 if (modname)
332 *modname = mod->name;
333 if (modbuildid) {
334#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
335 *modbuildid = mod->build_id;
336#else
337 *modbuildid = NULL;
338#endif
339 }
340
341 ret = find_kallsyms_symbol(mod, addr, size, offset);
342 }
343 /* Make a copy in here where it's safe */
344 if (ret) {
345 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
346 ret = namebuf;
347 }
348 preempt_enable();
349
350 return ret;
351}
352
353int lookup_module_symbol_name(unsigned long addr, char *symname)
354{
355 struct module *mod;
356
357 preempt_disable();
358 list_for_each_entry_rcu(mod, &modules, list) {
359 if (mod->state == MODULE_STATE_UNFORMED)
360 continue;
361 if (within_module(addr, mod)) {
362 const char *sym;
363
364 sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
365 if (!sym)
366 goto out;
367
368 strscpy(symname, sym, KSYM_NAME_LEN);
369 preempt_enable();
370 return 0;
371 }
372 }
373out:
374 preempt_enable();
375 return -ERANGE;
376}
377
378int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
379 unsigned long *offset, char *modname, char *name)
380{
381 struct module *mod;
382
383 preempt_disable();
384 list_for_each_entry_rcu(mod, &modules, list) {
385 if (mod->state == MODULE_STATE_UNFORMED)
386 continue;
387 if (within_module(addr, mod)) {
388 const char *sym;
389
390 sym = find_kallsyms_symbol(mod, addr, size, offset);
391 if (!sym)
392 goto out;
393 if (modname)
394 strscpy(modname, mod->name, MODULE_NAME_LEN);
395 if (name)
396 strscpy(name, sym, KSYM_NAME_LEN);
397 preempt_enable();
398 return 0;
399 }
400 }
401out:
402 preempt_enable();
403 return -ERANGE;
404}
405
406int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
407 char *name, char *module_name, int *exported)
408{
409 struct module *mod;
410
411 preempt_disable();
412 list_for_each_entry_rcu(mod, &modules, list) {
413 struct mod_kallsyms *kallsyms;
414
415 if (mod->state == MODULE_STATE_UNFORMED)
416 continue;
417 kallsyms = rcu_dereference_sched(mod->kallsyms);
418 if (symnum < kallsyms->num_symtab) {
419 const Elf_Sym *sym = &kallsyms->symtab[symnum];
420
421 *value = kallsyms_symbol_value(sym);
422 *type = kallsyms->typetab[symnum];
423 strscpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
424 strscpy(module_name, mod->name, MODULE_NAME_LEN);
425 *exported = is_exported(name, *value, mod);
426 preempt_enable();
427 return 0;
428 }
429 symnum -= kallsyms->num_symtab;
430 }
431 preempt_enable();
432 return -ERANGE;
433}
434
435/* Given a module and name of symbol, find and return the symbol's value */
436unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
437{
438 unsigned int i;
439 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
440
441 for (i = 0; i < kallsyms->num_symtab; i++) {
442 const Elf_Sym *sym = &kallsyms->symtab[i];
443
444 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
445 sym->st_shndx != SHN_UNDEF)
446 return kallsyms_symbol_value(sym);
447 }
448 return 0;
449}
450
451/* Look for this name: can be of form module:name. */
452unsigned long module_kallsyms_lookup_name(const char *name)
453{
454 struct module *mod;
455 char *colon;
456 unsigned long ret = 0;
457
458 /* Don't lock: we're in enough trouble already. */
459 preempt_disable();
460 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
461 if ((mod = find_module_all(name, colon - name, false)) != NULL)
462 ret = find_kallsyms_symbol_value(mod, colon + 1);
463 } else {
464 list_for_each_entry_rcu(mod, &modules, list) {
465 if (mod->state == MODULE_STATE_UNFORMED)
466 continue;
467 if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
468 break;
469 }
470 }
471 preempt_enable();
472 return ret;
473}
474
475#ifdef CONFIG_LIVEPATCH
476int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
477 struct module *, unsigned long),
478 void *data)
479{
480 struct module *mod;
481 unsigned int i;
482 int ret = 0;
483
484 mutex_lock(&module_mutex);
485 list_for_each_entry(mod, &modules, list) {
486 struct mod_kallsyms *kallsyms;
487
488 if (mod->state == MODULE_STATE_UNFORMED)
489 continue;
490
491 /* Use rcu_dereference_sched() to remain compliant with the sparse tool */
492 preempt_disable();
493 kallsyms = rcu_dereference_sched(mod->kallsyms);
494 preempt_enable();
495
496 for (i = 0; i < kallsyms->num_symtab; i++) {
497 const Elf_Sym *sym = &kallsyms->symtab[i];
498
499 if (sym->st_shndx == SHN_UNDEF)
500 continue;
501
502 ret = fn(data, kallsyms_symbol_name(kallsyms, i),
503 mod, kallsyms_symbol_value(sym));
504 if (ret != 0)
505 goto out;
506 }
507 }
508out:
509 mutex_unlock(&module_mutex);
510 return ret;
511}
512#endif /* CONFIG_LIVEPATCH */
kernel/module/kdb.c+62−0
@@ -0,0 +1,62 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module kdb support
4 *
5 * Copyright (C) 2010 Jason Wessel
6 */
7
8#include <linux/module.h>
9#include <linux/kdb.h>
10#include "internal.h"
11
12/*
13 * kdb_lsmod - This function implements the 'lsmod' command. Lists
14 * currently loaded kernel modules.
15 * Mostly taken from userland lsmod.
16 */
17int kdb_lsmod(int argc, const char **argv)
18{
19 struct module *mod;
20
21 if (argc != 0)
22 return KDB_ARGCOUNT;
23
24 kdb_printf("Module Size modstruct Used by\n");
25 list_for_each_entry(mod, &modules, list) {
26 if (mod->state == MODULE_STATE_UNFORMED)
27 continue;
28
29 kdb_printf("%-20s%8u", mod->name, mod->core_layout.size);
30#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
31 kdb_printf("/%8u", mod->data_layout.size);
32#endif
33 kdb_printf(" 0x%px ", (void *)mod);
34#ifdef CONFIG_MODULE_UNLOAD
35 kdb_printf("%4d ", module_refcount(mod));
36#endif
37 if (mod->state == MODULE_STATE_GOING)
38 kdb_printf(" (Unloading)");
39 else if (mod->state == MODULE_STATE_COMING)
40 kdb_printf(" (Loading)");
41 else
42 kdb_printf(" (Live)");
43 kdb_printf(" 0x%px", mod->core_layout.base);
44#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
45 kdb_printf("/0x%px", mod->data_layout.base);
46#endif
47
48#ifdef CONFIG_MODULE_UNLOAD
49 {
50 struct module_use *use;
51
52 kdb_printf(" [ ");
53 list_for_each_entry(use, &mod->source_list,
54 source_list)
55 kdb_printf("%s ", use->target->name);
56 kdb_printf("]\n");
57 }
58#endif
59 }
60
61 return 0;
62}
kernel/module/livepatch.c+74−0
@@ -0,0 +1,74 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module livepatch support
4 *
5 * Copyright (C) 2016 Jessica Yu <jeyu@redhat.com>
6 */
7
8#include <linux/module.h>
9#include <linux/string.h>
10#include <linux/slab.h>
11#include "internal.h"
12
13/*
14 * Persist Elf information about a module. Copy the Elf header,
15 * section header table, section string table, and symtab section
16 * index from info to mod->klp_info.
17 */
18int copy_module_elf(struct module *mod, struct load_info *info)
19{
20 unsigned int size, symndx;
21 int ret;
22
23 size = sizeof(*mod->klp_info);
24 mod->klp_info = kmalloc(size, GFP_KERNEL);
25 if (!mod->klp_info)
26 return -ENOMEM;
27
28 /* Elf header */
29 size = sizeof(mod->klp_info->hdr);
30 memcpy(&mod->klp_info->hdr, info->hdr, size);
31
32 /* Elf section header table */
33 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
34 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
35 if (!mod->klp_info->sechdrs) {
36 ret = -ENOMEM;
37 goto free_info;
38 }
39
40 /* Elf section name string table */
41 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
42 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
43 if (!mod->klp_info->secstrings) {
44 ret = -ENOMEM;
45 goto free_sechdrs;
46 }
47
48 /* Elf symbol section index */
49 symndx = info->index.sym;
50 mod->klp_info->symndx = symndx;
51
52 /*
53 * For livepatch modules, core_kallsyms.symtab is a complete
54 * copy of the original symbol table. Adjust sh_addr to point
55 * to core_kallsyms.symtab since the copy of the symtab in module
56 * init memory is freed at the end of do_init_module().
57 */
58 mod->klp_info->sechdrs[symndx].sh_addr = (unsigned long)mod->core_kallsyms.symtab;
59
60 return 0;
61
62free_sechdrs:
63 kfree(mod->klp_info->sechdrs);
64free_info:
65 kfree(mod->klp_info);
66 return ret;
67}
68
69void free_module_elf(struct module *mod)
70{
71 kfree(mod->klp_info->sechdrs);
72 kfree(mod->klp_info->secstrings);
73 kfree(mod->klp_info);
74}
kernel/module/main.crenamed from kernel/module.c+211−1882
@@ -14,16 +14,12 @@ · 13 unmodified lines
14#include <linux/init.h>14#include <linux/init.h>
15#include <linux/kallsyms.h>15#include <linux/kallsyms.h>
16#include <linux/buildid.h>16#include <linux/buildid.h>
17#include <linux/file.h>
18#include <linux/fs.h>17#include <linux/fs.h>
19#include <linux/sysfs.h>
20#include <linux/kernel.h>18#include <linux/kernel.h>
21#include <linux/kernel_read_file.h>19#include <linux/kernel_read_file.h>
22#include <linux/slab.h>20#include <linux/slab.h>
23#include <linux/vmalloc.h>21#include <linux/vmalloc.h>
24#include <linux/elf.h>22#include <linux/elf.h>
25#include <linux/proc_fs.h>
26#include <linux/security.h>
27#include <linux/seq_file.h>23#include <linux/seq_file.h>
28#include <linux/syscalls.h>24#include <linux/syscalls.h>
29#include <linux/fcntl.h>25#include <linux/fcntl.h>
@@ -58,236 +54,69 @@ · 28 unmodified lines
58#include <linux/dynamic_debug.h>54#include <linux/dynamic_debug.h>
59#include <linux/audit.h>55#include <linux/audit.h>
60#include <uapi/linux/module.h>56#include <uapi/linux/module.h>
61#include "module-internal.h"57#include "internal.h"
62 58
63#define CREATE_TRACE_POINTS59#define CREATE_TRACE_POINTS
64#include <trace/events/module.h>60#include <trace/events/module.h>
65 61
66#ifndef ARCH_SHF_SMALL
67#define ARCH_SHF_SMALL 0
68#endif
69
70/*
71 * Modules' sections will be aligned on page boundaries
72 * to ensure complete separation of code and data, but
73 * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
74 */
75#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
76# define debug_align(X) ALIGN(X, PAGE_SIZE)
77#else
78# define debug_align(X) (X)
79#endif
80
81/* If this is set, the section belongs in the init part of the module */
82#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
83
84/*62/*
85 * Mutex protects:63 * Mutex protects:
86 * 1) List of modules (also safely readable with preempt_disable),64 * 1) List of modules (also safely readable with preempt_disable),
87 * 2) module_use links,65 * 2) module_use links,
88 * 3) module_addr_min/module_addr_max.66 * 3) mod_tree.addr_min/mod_tree.addr_max.
89 * (delete and add uses RCU list operations).67 * (delete and add uses RCU list operations).
90 */68 */
91static DEFINE_MUTEX(module_mutex);69DEFINE_MUTEX(module_mutex);
92static LIST_HEAD(modules);70LIST_HEAD(modules);
93 71
94/* Work queue for freeing init sections in success case */72/* Work queue for freeing init sections in success case */
95static void do_free_init(struct work_struct *w);73static void do_free_init(struct work_struct *w);
96static DECLARE_WORK(init_free_wq, do_free_init);74static DECLARE_WORK(init_free_wq, do_free_init);
97static LLIST_HEAD(init_free_list);75static LLIST_HEAD(init_free_list);
98 76
99#ifdef CONFIG_MODULES_TREE_LOOKUP77struct mod_tree_root mod_tree __cacheline_aligned = {
100 78 .addr_min = -1UL,
101/*
102 * Use a latched RB-tree for __module_address(); this allows us to use
103 * RCU-sched lookups of the address from any context.
104 *
105 * This is conditional on PERF_EVENTS || TRACING because those can really hit
106 * __module_address() hard by doing a lot of stack unwinding; potentially from
107 * NMI context.
108 */
109
110static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
111{
112 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
113
114 return (unsigned long)layout->base;
115}
116
117static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
118{
119 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
120
121 return (unsigned long)layout->size;
122}
123
124static __always_inline bool
125mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
126{
127 return __mod_tree_val(a) < __mod_tree_val(b);
128}
129
130static __always_inline int
131mod_tree_comp(void *key, struct latch_tree_node *n)
132{
133 unsigned long val = (unsigned long)key;
134 unsigned long start, end;
135
136 start = __mod_tree_val(n);
137 if (val < start)
138 return -1;
139
140 end = start + __mod_tree_size(n);
141 if (val >= end)
142 return 1;
143
144 return 0;
145}
146
147static const struct latch_tree_ops mod_tree_ops = {
148 .less = mod_tree_less,
149 .comp = mod_tree_comp,
150};79};
151 80
152static struct mod_tree_root {81#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
153 struct latch_tree_root root;82struct mod_tree_root mod_data_tree __cacheline_aligned = {
154 unsigned long addr_min;
155 unsigned long addr_max;
156} mod_tree __cacheline_aligned = {
157 .addr_min = -1UL,83 .addr_min = -1UL,
158};84};
85#endif
159 86
160#define module_addr_min mod_tree.addr_min87#define module_addr_min mod_tree.addr_min
161#define module_addr_max mod_tree.addr_max88#define module_addr_max mod_tree.addr_max
162 89
163static noinline void __mod_tree_insert(struct mod_tree_node *node)90struct symsearch {
164{91 const struct kernel_symbol *start, *stop;
165 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);92 const s32 *crcs;
166}93 enum mod_license license;
167 94};
168static void __mod_tree_remove(struct mod_tree_node *node)
169{
170 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
171}
172
173/*
174 * These modifications: insert, remove_init and remove; are serialized by the
175 * module_mutex.
176 */
177static void mod_tree_insert(struct module *mod)
178{
179 mod->core_layout.mtn.mod = mod;
180 mod->init_layout.mtn.mod = mod;
181
182 __mod_tree_insert(&mod->core_layout.mtn);
183 if (mod->init_layout.size)
184 __mod_tree_insert(&mod->init_layout.mtn);
185}
186
187static void mod_tree_remove_init(struct module *mod)
188{
189 if (mod->init_layout.size)
190 __mod_tree_remove(&mod->init_layout.mtn);
191}
192
193static void mod_tree_remove(struct module *mod)
194{
195 __mod_tree_remove(&mod->core_layout.mtn);
196 mod_tree_remove_init(mod);
197}
198
199static struct module *mod_find(unsigned long addr)
200{
201 struct latch_tree_node *ltn;
202
203 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
204 if (!ltn)
205 return NULL;
206
207 return container_of(ltn, struct mod_tree_node, node)->mod;
208}
209
210#else /* MODULES_TREE_LOOKUP */
211
212static unsigned long module_addr_min = -1UL, module_addr_max = 0;
213
214static void mod_tree_insert(struct module *mod) { }
215static void mod_tree_remove_init(struct module *mod) { }
216static void mod_tree_remove(struct module *mod) { }
217
218static struct module *mod_find(unsigned long addr)
219{
220 struct module *mod;
221
222 list_for_each_entry_rcu(mod, &modules, list,
223 lockdep_is_held(&module_mutex)) {
224 if (within_module(addr, mod))
225 return mod;
226 }
227
228 return NULL;
229}
230
231#endif /* MODULES_TREE_LOOKUP */
232 95
233/*96/*
234 * Bounds of module text, for speeding up __module_address.97 * Bounds of module text, for speeding up __module_address.
235 * Protected by module_mutex.98 * Protected by module_mutex.
236 */99 */
237static void __mod_update_bounds(void *base, unsigned int size)100static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree)
238{101{
239 unsigned long min = (unsigned long)base;102 unsigned long min = (unsigned long)base;
240 unsigned long max = min + size;103 unsigned long max = min + size;
241 104
242 if (min < module_addr_min)105 if (min < tree->addr_min)
243 module_addr_min = min;106 tree->addr_min = min;
244 if (max > module_addr_max)107 if (max > tree->addr_max)
245 module_addr_max = max;108 tree->addr_max = max;
246}109}
247 110
248static void mod_update_bounds(struct module *mod)111static void mod_update_bounds(struct module *mod)
249{112{
250 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);113 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size, &mod_tree);
251 if (mod->init_layout.size)114 if (mod->init_layout.size)
252 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);115 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree);
253}116#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
254 117 __mod_update_bounds(mod->data_layout.base, mod->data_layout.size, &mod_data_tree);
255#ifdef CONFIG_KGDB_KDB
256struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
257#endif /* CONFIG_KGDB_KDB */
258
259static void module_assert_mutex_or_preempt(void)
260{
261#ifdef CONFIG_LOCKDEP
262 if (unlikely(!debug_locks))
263 return;
264
265 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
266 !lockdep_is_held(&module_mutex));
267#endif
268}
269
270#ifdef CONFIG_MODULE_SIG
271static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
272module_param(sig_enforce, bool_enable_only, 0644);
273
274void set_module_sig_enforced(void)
275{
276 sig_enforce = true;
277}
278#else
279#define sig_enforce false
280#endif118#endif
281
282/*
283 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
284 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
285 */
286bool is_module_sig_enforced(void)
287{
288 return sig_enforce;
289}119}
290EXPORT_SYMBOL(is_module_sig_enforced);
291 120
292/* Block module loading/unloading? */121/* Block module loading/unloading? */
293int modules_disabled = 0;122int modules_disabled = 0;
@@ -408,66 +237,12 @@ static __maybe_unused void *any_section_objs(const struct load_info *info, · 114 unmodified lines
408 return (void *)info->sechdrs[sec].sh_addr;237 return (void *)info->sechdrs[sec].sh_addr;
409}238}
410 239
411/* Provided by the linker */
412extern const struct kernel_symbol __start___ksymtab[];
413extern const struct kernel_symbol __stop___ksymtab[];
414extern const struct kernel_symbol __start___ksymtab_gpl[];
415extern const struct kernel_symbol __stop___ksymtab_gpl[];
416extern const s32 __start___kcrctab[];
417extern const s32 __start___kcrctab_gpl[];
418
419#ifndef CONFIG_MODVERSIONS240#ifndef CONFIG_MODVERSIONS
420#define symversion(base, idx) NULL241#define symversion(base, idx) NULL
421#else242#else
422#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)243#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
423#endif244#endif
424 245
425struct symsearch {
426 const struct kernel_symbol *start, *stop;
427 const s32 *crcs;
428 enum mod_license {
429 NOT_GPL_ONLY,
430 GPL_ONLY,
431 } license;
432};
433
434struct find_symbol_arg {
435 /* Input */
436 const char *name;
437 bool gplok;
438 bool warn;
439
440 /* Output */
441 struct module *owner;
442 const s32 *crc;
443 const struct kernel_symbol *sym;
444 enum mod_license license;
445};
446
447static bool check_exported_symbol(const struct symsearch *syms,
448 struct module *owner,
449 unsigned int symnum, void *data)
450{
451 struct find_symbol_arg *fsa = data;
452
453 if (!fsa->gplok && syms->license == GPL_ONLY)
454 return false;
455 fsa->owner = owner;
456 fsa->crc = symversion(syms->crcs, symnum);
457 fsa->sym = &syms->start[symnum];
458 fsa->license = syms->license;
459 return true;
460}
461
462static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
463{
464#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
465 return (unsigned long)offset_to_ptr(&sym->value_offset);
466#else
467 return sym->value;
468#endif
469}
470
471static const char *kernel_symbol_name(const struct kernel_symbol *sym)246static const char *kernel_symbol_name(const struct kernel_symbol *sym)
472{247{
473#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS248#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
@@ -488,33 +263,38 @@ static const char *kernel_symbol_namespace(const struct kernel_symbol *sym) · 14 unmodified lines
488#endif263#endif
489}264}
490 265
491static int cmp_name(const void *name, const void *sym)266int cmp_name(const void *name, const void *sym)
492{267{
493 return strcmp(name, kernel_symbol_name(sym));268 return strcmp(name, kernel_symbol_name(sym));
494}269}
495 270
496static bool find_exported_symbol_in_section(const struct symsearch *syms,271static bool find_exported_symbol_in_section(const struct symsearch *syms,
497 struct module *owner,272 struct module *owner,
498 void *data)273 struct find_symbol_arg *fsa)
499{274{
500 struct find_symbol_arg *fsa = data;
501 struct kernel_symbol *sym;275 struct kernel_symbol *sym;
502 276
277 if (!fsa->gplok && syms->license == GPL_ONLY)
278 return false;
279
503 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,280 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
504 sizeof(struct kernel_symbol), cmp_name);281 sizeof(struct kernel_symbol), cmp_name);
282 if (!sym)
283 return false;
505 284
506 if (sym != NULL && check_exported_symbol(syms, owner,285 fsa->owner = owner;
507 sym - syms->start, data))286 fsa->crc = symversion(syms->crcs, sym - syms->start);
508 return true;287 fsa->sym = sym;
288 fsa->license = syms->license;
509 289
510 return false;290 return true;
511}291}
512 292
513/*293/*
514 * Find an exported symbol and return it, along with, (optional) crc and294 * Find an exported symbol and return it, along with, (optional) crc and
515 * (optional) module which owns it. Needs preempt disabled or module_mutex.295 * (optional) module which owns it. Needs preempt disabled or module_mutex.
516 */296 */
517static bool find_symbol(struct find_symbol_arg *fsa)297bool find_symbol(struct find_symbol_arg *fsa)
518{298{
519 static const struct symsearch arr[] = {299 static const struct symsearch arr[] = {
520 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,300 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
@@ -558,8 +338,8 @@ static bool find_symbol(struct find_symbol_arg *fsa) · 37 unmodified lines
558 * Search for module by name: must hold module_mutex (or preempt disabled338 * Search for module by name: must hold module_mutex (or preempt disabled
559 * for read-only access).339 * for read-only access).
560 */340 */
561static struct module *find_module_all(const char *name, size_t len,341struct module *find_module_all(const char *name, size_t len,
562 bool even_unformed)342 bool even_unformed)
563{343{
564 struct module *mod;344 struct module *mod;
565 345
@@ -985,31 +765,6 @@ out: · 419 unmodified lines
985 return ret;765 return ret;
986}766}
987 767
988static inline void print_unload_info(struct seq_file *m, struct module *mod)
989{
990 struct module_use *use;
991 int printed_something = 0;
992
993 seq_printf(m, " %i ", module_refcount(mod));
994
995 /*
996 * Always include a trailing , so userspace can differentiate
997 * between this and the old multi-field proc format.
998 */
999 list_for_each_entry(use, &mod->source_list, source_list) {
1000 printed_something = 1;
1001 seq_printf(m, "%s,", use->source->name);
1002 }
1003
1004 if (mod->init != NULL && mod->exit == NULL) {
1005 printed_something = 1;
1006 seq_puts(m, "[permanent],");
1007 }
1008
1009 if (!printed_something)
1010 seq_puts(m, "-");
1011}
1012
1013void __symbol_put(const char *symbol)768void __symbol_put(const char *symbol)
1014{769{
1015 struct find_symbol_arg fsa = {770 struct find_symbol_arg fsa = {
@@ -1099,12 +854,6 @@ void module_put(struct module *module) · 83 unmodified lines
1099EXPORT_SYMBOL(module_put);854EXPORT_SYMBOL(module_put);
1100 855
1101#else /* !CONFIG_MODULE_UNLOAD */856#else /* !CONFIG_MODULE_UNLOAD */
1102static inline void print_unload_info(struct seq_file *m, struct module *mod)
1103{
1104 /* We don't know the usage count, or what modules are using. */
1105 seq_puts(m, " - -");
1106}
1107
1108static inline void module_unload_free(struct module *mod)857static inline void module_unload_free(struct module *mod)
1109{858{
1110}859}
@@ -1120,13 +869,13 @@ static inline int module_unload_init(struct module *mod) · 9 unmodified lines
1120}869}
1121#endif /* CONFIG_MODULE_UNLOAD */870#endif /* CONFIG_MODULE_UNLOAD */
1122 871
1123static size_t module_flags_taint(struct module *mod, char *buf)872size_t module_flags_taint(unsigned long taints, char *buf)
1124{873{
1125 size_t l = 0;874 size_t l = 0;
1126 int i;875 int i;
1127 876
1128 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {877 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1129 if (taint_flags[i].module && test_bit(i, &mod->taints))878 if (taint_flags[i].module && test_bit(i, &taints))
1130 buf[l++] = taint_flags[i].c_true;879 buf[l++] = taint_flags[i].c_true;
1131 }880 }
1132 881
@@ -1179,6 +928,17 @@ static ssize_t show_coresize(struct module_attribute *mattr, · 46 unmodified lines
1179static struct module_attribute modinfo_coresize =928static struct module_attribute modinfo_coresize =
1180 __ATTR(coresize, 0444, show_coresize, NULL);929 __ATTR(coresize, 0444, show_coresize, NULL);
1181 930
931#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
932static ssize_t show_datasize(struct module_attribute *mattr,
933 struct module_kobject *mk, char *buffer)
934{
935 return sprintf(buffer, "%u\n", mk->mod->data_layout.size);
936}
937
938static struct module_attribute modinfo_datasize =
939 __ATTR(datasize, 0444, show_datasize, NULL);
940#endif
941
1182static ssize_t show_initsize(struct module_attribute *mattr,942static ssize_t show_initsize(struct module_attribute *mattr,
1183 struct module_kobject *mk, char *buffer)943 struct module_kobject *mk, char *buffer)
1184{944{
@@ -1193,7 +953,7 @@ static ssize_t show_taint(struct module_attribute *mattr, · 8 unmodified lines
1193{953{
1194 size_t l;954 size_t l;
1195 955
1196 l = module_flags_taint(mk->mod, buffer);956 l = module_flags_taint(mk->mod->taints, buffer);
1197 buffer[l++] = '\n';957 buffer[l++] = '\n';
1198 return l;958 return l;
1199}959}
@@ -1201,12 +961,15 @@ static ssize_t show_taint(struct module_attribute *mattr, · 1 unmodified line
1201static struct module_attribute modinfo_taint =961static struct module_attribute modinfo_taint =
1202 __ATTR(taint, 0444, show_taint, NULL);962 __ATTR(taint, 0444, show_taint, NULL);
1203 963
1204static struct module_attribute *modinfo_attrs[] = {964struct module_attribute *modinfo_attrs[] = {
1205 &module_uevent,965 &module_uevent,
1206 &modinfo_version,966 &modinfo_version,
1207 &modinfo_srcversion,967 &modinfo_srcversion,
1208 &modinfo_initstate,968 &modinfo_initstate,
1209 &modinfo_coresize,969 &modinfo_coresize,
970#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
971 &modinfo_datasize,
972#endif
1210 &modinfo_initsize,973 &modinfo_initsize,
1211 &modinfo_taint,974 &modinfo_taint,
1212#ifdef CONFIG_MODULE_UNLOAD975#ifdef CONFIG_MODULE_UNLOAD
@@ -1215,9 +978,11 @@ static struct module_attribute *modinfo_attrs[] = { · 2 unmodified lines
1215 NULL,978 NULL,
1216};979};
1217 980
981size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
982
1218static const char vermagic[] = VERMAGIC_STRING;983static const char vermagic[] = VERMAGIC_STRING;
1219 984
1220static int try_to_force_load(struct module *mod, const char *reason)985int try_to_force_load(struct module *mod, const char *reason)
1221{986{
1222#ifdef CONFIG_MODULE_FORCE_LOAD987#ifdef CONFIG_MODULE_FORCE_LOAD
1223 if (!test_taint(TAINT_FORCED_MODULE))988 if (!test_taint(TAINT_FORCED_MODULE))
@@ -1229,107 +994,6 @@ static int try_to_force_load(struct module *mod, const char *reason) · 5 unmodified lines
1229#endif994#endif
1230}995}
1231 996
1232#ifdef CONFIG_MODVERSIONS
1233
1234static int check_version(const struct load_info *info,
1235 const char *symname,
1236 struct module *mod,
1237 const s32 *crc)
1238{
1239 Elf_Shdr *sechdrs = info->sechdrs;
1240 unsigned int versindex = info->index.vers;
1241 unsigned int i, num_versions;
1242 struct modversion_info *versions;
1243
1244 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1245 if (!crc)
1246 return 1;
1247
1248 /* No versions at all? modprobe --force does this. */
1249 if (versindex == 0)
1250 return try_to_force_load(mod, symname) == 0;
1251
1252 versions = (void *) sechdrs[versindex].sh_addr;
1253 num_versions = sechdrs[versindex].sh_size
1254 / sizeof(struct modversion_info);
1255
1256 for (i = 0; i < num_versions; i++) {
1257 u32 crcval;
1258
1259 if (strcmp(versions[i].name, symname) != 0)
1260 continue;
1261
1262 crcval = *crc;
1263 if (versions[i].crc == crcval)
1264 return 1;
1265 pr_debug("Found checksum %X vs module %lX\n",
1266 crcval, versions[i].crc);
1267 goto bad_version;
1268 }
1269
1270 /* Broken toolchain. Warn once, then let it go.. */
1271 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
1272 return 1;
1273
1274bad_version:
1275 pr_warn("%s: disagrees about version of symbol %s\n",
1276 info->name, symname);
1277 return 0;
1278}
1279
1280static inline int check_modstruct_version(const struct load_info *info,
1281 struct module *mod)
1282{
1283 struct find_symbol_arg fsa = {
1284 .name = "module_layout",
1285 .gplok = true,
1286 };
1287
1288 /*
1289 * Since this should be found in kernel (which can't be removed), no
1290 * locking is necessary -- use preempt_disable() to placate lockdep.
1291 */
1292 preempt_disable();
1293 if (!find_symbol(&fsa)) {
1294 preempt_enable();
1295 BUG();
1296 }
1297 preempt_enable();
1298 return check_version(info, "module_layout", mod, fsa.crc);
1299}
1300
1301/* First part is kernel version, which we ignore if module has crcs. */
1302static inline int same_magic(const char *amagic, const char *bmagic,
1303 bool has_crcs)
1304{
1305 if (has_crcs) {
1306 amagic += strcspn(amagic, " ");
1307 bmagic += strcspn(bmagic, " ");
1308 }
1309 return strcmp(amagic, bmagic) == 0;
1310}
1311#else
1312static inline int check_version(const struct load_info *info,
1313 const char *symname,
1314 struct module *mod,
1315 const s32 *crc)
1316{
1317 return 1;
1318}
1319
1320static inline int check_modstruct_version(const struct load_info *info,
1321 struct module *mod)
1322{
1323 return 1;
1324}
1325
1326static inline int same_magic(const char *amagic, const char *bmagic,
1327 bool has_crcs)
1328{
1329 return strcmp(amagic, bmagic) == 0;
1330}
1331#endif /* CONFIG_MODVERSIONS */
1332
1333static char *get_modinfo(const struct load_info *info, const char *tag);997static char *get_modinfo(const struct load_info *info, const char *tag);
1334static char *get_next_modinfo(const struct load_info *info, const char *tag,998static char *get_next_modinfo(const struct load_info *info, const char *tag,
1335 char *prev);999 char *prev);
@@ -1364,20 +1028,20 @@ static int verify_namespace_is_imported(const struct load_info *info, · 28 unmodified lines
1364 return 0;1028 return 0;
1365}1029}
1366 1030
1367static bool inherit_taint(struct module *mod, struct module *owner)1031static bool inherit_taint(struct module *mod, struct module *owner, const char *name)
1368{1032{
1369 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))1033 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
1370 return true;1034 return true;
1371 1035
1372 if (mod->using_gplonly_symbols) {1036 if (mod->using_gplonly_symbols) {
1373 pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",1037 pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n",
1374 mod->name, owner->name);1038 mod->name, name, owner->name);
1375 return false;1039 return false;
1376 }1040 }
1377 1041
1378 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {1042 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1379 pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",1043 pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n",
1380 mod->name, owner->name);1044 mod->name, name, owner->name);
1381 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);1045 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
1382 }1046 }
1383 return true;1047 return true;
@@ -1409,7 +1073,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, · 25 unmodified lines
1409 if (fsa.license == GPL_ONLY)1073 if (fsa.license == GPL_ONLY)
1410 mod->using_gplonly_symbols = true;1074 mod->using_gplonly_symbols = true;
1411 1075
1412 if (!inherit_taint(mod, fsa.owner)) {1076 if (!inherit_taint(mod, fsa.owner, name)) {
1413 fsa.sym = NULL;1077 fsa.sym = NULL;
1414 goto getname;1078 goto getname;
1415 }1079 }
@@ -1457,734 +1121,69 @@ resolve_symbol_wait(struct module *mod, · 41 unmodified lines
1457 return ksym;1121 return ksym;
1458}1122}
1459 1123
1460#ifdef CONFIG_KALLSYMS1124void __weak module_memfree(void *module_region)
1461static inline bool sect_empty(const Elf_Shdr *sect)
1462{1125{
1463 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;1126 /*
1127 * This memory may be RO, and freeing RO memory in an interrupt is not
1128 * supported by vmalloc.
1129 */
1130 WARN_ON(in_interrupt());
1131 vfree(module_region);
1464}1132}
1465#endif
1466 1133
1467/*1134void __weak module_arch_cleanup(struct module *mod)
1468 * /sys/module/foo/sections stuff1135{
1469 * J. Corbet <corbet@lwn.net>1136}
1470 */
1471#ifdef CONFIG_SYSFS
1472 1137
1473#ifdef CONFIG_KALLSYMS1138void __weak module_arch_freeing_init(struct module *mod)
1474struct module_sect_attr {1139{
1475 struct bin_attribute battr;1140}
1476 unsigned long address;
1477};
1478 1141
1479struct module_sect_attrs {1142static void cfi_cleanup(struct module *mod);
1480 struct attribute_group grp;
1481 unsigned int nsections;
1482 struct module_sect_attr attrs[];
1483};
1484 1143
1485#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))1144/* Free a module, remove from lists, etc. */
1486static ssize_t module_sect_read(struct file *file, struct kobject *kobj,1145static void free_module(struct module *mod)
1487 struct bin_attribute *battr,
1488 char *buf, loff_t pos, size_t count)
1489{1146{
1490 struct module_sect_attr *sattr =1147 trace_module_free(mod);
1491 container_of(battr, struct module_sect_attr, battr);
1492 char bounce[MODULE_SECT_READ_SIZE + 1];
1493 size_t wrote;
1494 1148
1495 if (pos != 0)1149 mod_sysfs_teardown(mod);
1496 return -EINVAL;
1497 1150
1498 /*1151 /*
1499 * Since we're a binary read handler, we must account for the1152 * We leave it in list to prevent duplicate loads, but make sure
1500 * trailing NUL byte that sprintf will write: if "buf" is1153 * that noone uses it while it's being deconstructed.
1501 * too small to hold the NUL, or the NUL is exactly the last
1502 * byte, the read will look like it got truncated by one byte.
1503 * Since there is no way to ask sprintf nicely to not write
1504 * the NUL, we have to use a bounce buffer.
1505 */1154 */
1506 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",1155 mutex_lock(&module_mutex);
1507 kallsyms_show_value(file->f_cred)1156 mod->state = MODULE_STATE_UNFORMED;
1508 ? (void *)sattr->address : NULL);1157 mutex_unlock(&module_mutex);
1509 count = min(count, wrote);
1510 memcpy(buf, bounce, count);
1511 1158
1512 return count;1159 /* Remove dynamic debug info */
1513}1160 ddebug_remove_module(mod->name);
1514 1161
1515static void free_sect_attrs(struct module_sect_attrs *sect_attrs)1162 /* Arch-specific cleanup. */
1516{1163 module_arch_cleanup(mod);
1517 unsigned int section;
1518 1164
1519 for (section = 0; section < sect_attrs->nsections; section++)1165 /* Module unload stuff */
1520 kfree(sect_attrs->attrs[section].battr.attr.name);1166 module_unload_free(mod);
1521 kfree(sect_attrs);
1522}
1523 1167
1524static void add_sect_attrs(struct module *mod, const struct load_info *info)1168 /* Free any allocated parameters. */
1525{1169 destroy_params(mod->kp, mod->num_kp);
1526 unsigned int nloaded = 0, i, size[2];
1527 struct module_sect_attrs *sect_attrs;
1528 struct module_sect_attr *sattr;
1529 struct bin_attribute **gattr;
1530 1170
1531 /* Count loaded sections and allocate structures */1171 if (is_livepatch_module(mod))
1532 for (i = 0; i < info->hdr->e_shnum; i++)1172 free_module_elf(mod);
1533 if (!sect_empty(&info->sechdrs[i]))
1534 nloaded++;
1535 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1536 sizeof(sect_attrs->grp.bin_attrs[0]));
1537 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
1538 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1539 if (sect_attrs == NULL)
1540 return;
1541 1173
1542 /* Setup section attributes. */1174 /* Now we can delete it from the lists */
1543 sect_attrs->grp.name = "sections";1175 mutex_lock(&module_mutex);
1544 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];1176 /* Unlink carefully: kallsyms could be walking list. */
1545 1177 list_del_rcu(&mod->list);
1546 sect_attrs->nsections = 0;1178 mod_tree_remove(mod);
1547 sattr = &sect_attrs->attrs[0];1179 /* Remove this module from bug list, this uses list_del_rcu */
1548 gattr = &sect_attrs->grp.bin_attrs[0];1180 module_bug_cleanup(mod);
1549 for (i = 0; i < info->hdr->e_shnum; i++) {1181 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
1550 Elf_Shdr *sec = &info->sechdrs[i];1182 synchronize_rcu();
1551 if (sect_empty(sec))1183 if (try_add_tainted_module(mod))
1552 continue;1184 pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n",
1553 sysfs_bin_attr_init(&sattr->battr);1185 mod->name);
1554 sattr->address = sec->sh_addr;1186 mutex_unlock(&module_mutex);
1555 sattr->battr.attr.name =
1556 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1557 if (sattr->battr.attr.name == NULL)
1558 goto out;
1559 sect_attrs->nsections++;
1560 sattr->battr.read = module_sect_read;
1561 sattr->battr.size = MODULE_SECT_READ_SIZE;
1562 sattr->battr.attr.mode = 0400;
1563 *(gattr++) = &(sattr++)->battr;
1564 }
1565 *gattr = NULL;
1566
1567 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1568 goto out;
1569
1570 mod->sect_attrs = sect_attrs;
1571 return;
1572 out:
1573 free_sect_attrs(sect_attrs);
1574}
1575
1576static void remove_sect_attrs(struct module *mod)
1577{
1578 if (mod->sect_attrs) {
1579 sysfs_remove_group(&mod->mkobj.kobj,
1580 &mod->sect_attrs->grp);
1581 /*
1582 * We are positive that no one is using any sect attrs
1583 * at this point. Deallocate immediately.
1584 */
1585 free_sect_attrs(mod->sect_attrs);
1586 mod->sect_attrs = NULL;
1587 }
1588}
1589
1590/*
1591 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1592 */
1593
1594struct module_notes_attrs {
1595 struct kobject *dir;
1596 unsigned int notes;
1597 struct bin_attribute attrs[];
1598};
1599
1600static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1601 struct bin_attribute *bin_attr,
1602 char *buf, loff_t pos, size_t count)
1603{
1604 /*
1605 * The caller checked the pos and count against our size.
1606 */
1607 memcpy(buf, bin_attr->private + pos, count);
1608 return count;
1609}
1610
1611static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1612 unsigned int i)
1613{
1614 if (notes_attrs->dir) {
1615 while (i-- > 0)
1616 sysfs_remove_bin_file(notes_attrs->dir,
1617 &notes_attrs->attrs[i]);
1618 kobject_put(notes_attrs->dir);
1619 }
1620 kfree(notes_attrs);
1621}
1622
1623static void add_notes_attrs(struct module *mod, const struct load_info *info)
1624{
1625 unsigned int notes, loaded, i;
1626 struct module_notes_attrs *notes_attrs;
1627 struct bin_attribute *nattr;
1628
1629 /* failed to create section attributes, so can't create notes */
1630 if (!mod->sect_attrs)
1631 return;
1632
1633 /* Count notes sections and allocate structures. */
1634 notes = 0;
1635 for (i = 0; i < info->hdr->e_shnum; i++)
1636 if (!sect_empty(&info->sechdrs[i]) &&
1637 (info->sechdrs[i].sh_type == SHT_NOTE))
1638 ++notes;
1639
1640 if (notes == 0)
1641 return;
1642
1643 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
1644 GFP_KERNEL);
1645 if (notes_attrs == NULL)
1646 return;
1647
1648 notes_attrs->notes = notes;
1649 nattr = &notes_attrs->attrs[0];
1650 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1651 if (sect_empty(&info->sechdrs[i]))
1652 continue;
1653 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1654 sysfs_bin_attr_init(nattr);
1655 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
1656 nattr->attr.mode = S_IRUGO;
1657 nattr->size = info->sechdrs[i].sh_size;
1658 nattr->private = (void *) info->sechdrs[i].sh_addr;
1659 nattr->read = module_notes_read;
1660 ++nattr;
1661 }
1662 ++loaded;
1663 }
1664
1665 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1666 if (!notes_attrs->dir)
1667 goto out;
1668
1669 for (i = 0; i < notes; ++i)
1670 if (sysfs_create_bin_file(notes_attrs->dir,
1671 &notes_attrs->attrs[i]))
1672 goto out;
1673
1674 mod->notes_attrs = notes_attrs;
1675 return;
1676
1677 out:
1678 free_notes_attrs(notes_attrs, i);
1679}
1680
1681static void remove_notes_attrs(struct module *mod)
1682{
1683 if (mod->notes_attrs)
1684 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1685}
1686
1687#else
1688
1689static inline void add_sect_attrs(struct module *mod,
1690 const struct load_info *info)
1691{
1692}
1693
1694static inline void remove_sect_attrs(struct module *mod)
1695{
1696}
1697
1698static inline void add_notes_attrs(struct module *mod,
1699 const struct load_info *info)
1700{
1701}
1702
1703static inline void remove_notes_attrs(struct module *mod)
1704{
1705}
1706#endif /* CONFIG_KALLSYMS */
1707
1708static void del_usage_links(struct module *mod)
1709{
1710#ifdef CONFIG_MODULE_UNLOAD
1711 struct module_use *use;
1712
1713 mutex_lock(&module_mutex);
1714 list_for_each_entry(use, &mod->target_list, target_list)
1715 sysfs_remove_link(use->target->holders_dir, mod->name);
1716 mutex_unlock(&module_mutex);
1717#endif
1718}
1719
1720static int add_usage_links(struct module *mod)
1721{
1722 int ret = 0;
1723#ifdef CONFIG_MODULE_UNLOAD
1724 struct module_use *use;
1725
1726 mutex_lock(&module_mutex);
1727 list_for_each_entry(use, &mod->target_list, target_list) {
1728 ret = sysfs_create_link(use->target->holders_dir,
1729 &mod->mkobj.kobj, mod->name);
1730 if (ret)
1731 break;
1732 }
1733 mutex_unlock(&module_mutex);
1734 if (ret)
1735 del_usage_links(mod);
1736#endif
1737 return ret;
1738}
1739
1740static void module_remove_modinfo_attrs(struct module *mod, int end);
1741
1742static int module_add_modinfo_attrs(struct module *mod)
1743{
1744 struct module_attribute *attr;
1745 struct module_attribute *temp_attr;
1746 int error = 0;
1747 int i;
1748
1749 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1750 (ARRAY_SIZE(modinfo_attrs) + 1)),
1751 GFP_KERNEL);
1752 if (!mod->modinfo_attrs)
1753 return -ENOMEM;
1754
1755 temp_attr = mod->modinfo_attrs;
1756 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1757 if (!attr->test || attr->test(mod)) {
1758 memcpy(temp_attr, attr, sizeof(*temp_attr));
1759 sysfs_attr_init(&temp_attr->attr);
1760 error = sysfs_create_file(&mod->mkobj.kobj,
1761 &temp_attr->attr);
1762 if (error)
1763 goto error_out;
1764 ++temp_attr;
1765 }
1766 }
1767
1768 return 0;
1769
1770error_out:
1771 if (i > 0)
1772 module_remove_modinfo_attrs(mod, --i);
1773 else
1774 kfree(mod->modinfo_attrs);
1775 return error;
1776}
1777
1778static void module_remove_modinfo_attrs(struct module *mod, int end)
1779{
1780 struct module_attribute *attr;
1781 int i;
1782
1783 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1784 if (end >= 0 && i > end)
1785 break;
1786 /* pick a field to test for end of list */
1787 if (!attr->attr.name)
1788 break;
1789 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
1790 if (attr->free)
1791 attr->free(mod);
1792 }
1793 kfree(mod->modinfo_attrs);
1794}
1795
1796static void mod_kobject_put(struct module *mod)
1797{
1798 DECLARE_COMPLETION_ONSTACK(c);
1799 mod->mkobj.kobj_completion = &c;
1800 kobject_put(&mod->mkobj.kobj);
1801 wait_for_completion(&c);
1802}
1803
1804static int mod_sysfs_init(struct module *mod)
1805{
1806 int err;
1807 struct kobject *kobj;
1808
1809 if (!module_sysfs_initialized) {
1810 pr_err("%s: module sysfs not initialized\n", mod->name);
1811 err = -EINVAL;
1812 goto out;
1813 }
1814
1815 kobj = kset_find_obj(module_kset, mod->name);
1816 if (kobj) {
1817 pr_err("%s: module is already loaded\n", mod->name);
1818 kobject_put(kobj);
1819 err = -EINVAL;
1820 goto out;
1821 }
1822
1823 mod->mkobj.mod = mod;
1824
1825 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1826 mod->mkobj.kobj.kset = module_kset;
1827 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1828 "%s", mod->name);
1829 if (err)
1830 mod_kobject_put(mod);
1831
1832out:
1833 return err;
1834}
1835
1836static int mod_sysfs_setup(struct module *mod,
1837 const struct load_info *info,
1838 struct kernel_param *kparam,
1839 unsigned int num_params)
1840{
1841 int err;
1842
1843 err = mod_sysfs_init(mod);
1844 if (err)
1845 goto out;
1846
1847 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1848 if (!mod->holders_dir) {
1849 err = -ENOMEM;
1850 goto out_unreg;
1851 }
1852
1853 err = module_param_sysfs_setup(mod, kparam, num_params);
1854 if (err)
1855 goto out_unreg_holders;
1856
1857 err = module_add_modinfo_attrs(mod);
1858 if (err)
1859 goto out_unreg_param;
1860
1861 err = add_usage_links(mod);
1862 if (err)
1863 goto out_unreg_modinfo_attrs;
1864
1865 add_sect_attrs(mod, info);
1866 add_notes_attrs(mod, info);
1867
1868 return 0;
1869
1870out_unreg_modinfo_attrs:
1871 module_remove_modinfo_attrs(mod, -1);
1872out_unreg_param:
1873 module_param_sysfs_remove(mod);
1874out_unreg_holders:
1875 kobject_put(mod->holders_dir);
1876out_unreg:
1877 mod_kobject_put(mod);
1878out:
1879 return err;
1880}
1881
1882static void mod_sysfs_fini(struct module *mod)
1883{
1884 remove_notes_attrs(mod);
1885 remove_sect_attrs(mod);
1886 mod_kobject_put(mod);
1887}
1888
1889static void init_param_lock(struct module *mod)
1890{
1891 mutex_init(&mod->param_lock);
1892}
1893#else /* !CONFIG_SYSFS */
1894
1895static int mod_sysfs_setup(struct module *mod,
1896 const struct load_info *info,
1897 struct kernel_param *kparam,
1898 unsigned int num_params)
1899{
1900 return 0;
1901}
1902
1903static void mod_sysfs_fini(struct module *mod)
1904{
1905}
1906
1907static void module_remove_modinfo_attrs(struct module *mod, int end)
1908{
1909}
1910
1911static void del_usage_links(struct module *mod)
1912{
1913}
1914
1915static void init_param_lock(struct module *mod)
1916{
1917}
1918#endif /* CONFIG_SYSFS */
1919
1920static void mod_sysfs_teardown(struct module *mod)
1921{
1922 del_usage_links(mod);
1923 module_remove_modinfo_attrs(mod, -1);
1924 module_param_sysfs_remove(mod);
1925 kobject_put(mod->mkobj.drivers_dir);
1926 kobject_put(mod->holders_dir);
1927 mod_sysfs_fini(mod);
1928}
1929
1930/*
1931 * LKM RO/NX protection: protect module's text/ro-data
1932 * from modification and any data from execution.
1933 *
1934 * General layout of module is:
1935 * [text] [read-only-data] [ro-after-init] [writable data]
1936 * text_size -----^ ^ ^ ^
1937 * ro_size ------------------------| | |
1938 * ro_after_init_size -----------------------------| |
1939 * size -----------------------------------------------------------|
1940 *
1941 * These values are always page-aligned (as is base)
1942 */
1943
1944/*
1945 * Since some arches are moving towards PAGE_KERNEL module allocations instead
1946 * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the
1947 * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of
1948 * whether we are strict.
1949 */
1950#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
1951static void frob_text(const struct module_layout *layout,
1952 int (*set_memory)(unsigned long start, int num_pages))
1953{
1954 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1955 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1956 set_memory((unsigned long)layout->base,
1957 layout->text_size >> PAGE_SHIFT);
1958}
1959
1960static void module_enable_x(const struct module *mod)
1961{
1962 frob_text(&mod->core_layout, set_memory_x);
1963 frob_text(&mod->init_layout, set_memory_x);
1964}
1965#else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
1966static void module_enable_x(const struct module *mod) { }
1967#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
1968
1969#ifdef CONFIG_STRICT_MODULE_RWX
1970static void frob_rodata(const struct module_layout *layout,
1971 int (*set_memory)(unsigned long start, int num_pages))
1972{
1973 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1974 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1975 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1976 set_memory((unsigned long)layout->base + layout->text_size,
1977 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
1978}
1979
1980static void frob_ro_after_init(const struct module_layout *layout,
1981 int (*set_memory)(unsigned long start, int num_pages))
1982{
1983 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1984 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1985 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1986 set_memory((unsigned long)layout->base + layout->ro_size,
1987 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
1988}
1989
1990static void frob_writable_data(const struct module_layout *layout,
1991 int (*set_memory)(unsigned long start, int num_pages))
1992{
1993 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1994 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
1995 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
1996 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
1997 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
1998}
1999
2000static void module_enable_ro(const struct module *mod, bool after_init)
2001{
2002 if (!rodata_enabled)
2003 return;
2004
2005 set_vm_flush_reset_perms(mod->core_layout.base);
2006 set_vm_flush_reset_perms(mod->init_layout.base);
2007 frob_text(&mod->core_layout, set_memory_ro);
2008
2009 frob_rodata(&mod->core_layout, set_memory_ro);
2010 frob_text(&mod->init_layout, set_memory_ro);
2011 frob_rodata(&mod->init_layout, set_memory_ro);
2012
2013 if (after_init)
2014 frob_ro_after_init(&mod->core_layout, set_memory_ro);
2015}
2016
2017static void module_enable_nx(const struct module *mod)
2018{
2019 frob_rodata(&mod->core_layout, set_memory_nx);
2020 frob_ro_after_init(&mod->core_layout, set_memory_nx);
2021 frob_writable_data(&mod->core_layout, set_memory_nx);
2022 frob_rodata(&mod->init_layout, set_memory_nx);
2023 frob_writable_data(&mod->init_layout, set_memory_nx);
2024}
2025
2026static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2027 char *secstrings, struct module *mod)
2028{
2029 const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR;
2030 int i;
2031
2032 for (i = 0; i < hdr->e_shnum; i++) {
2033 if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
2034 pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
2035 mod->name, secstrings + sechdrs[i].sh_name, i);
2036 return -ENOEXEC;
2037 }
2038 }
2039
2040 return 0;
2041}
2042
2043#else /* !CONFIG_STRICT_MODULE_RWX */
2044static void module_enable_nx(const struct module *mod) { }
2045static void module_enable_ro(const struct module *mod, bool after_init) {}
2046static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2047 char *secstrings, struct module *mod)
2048{
2049 return 0;
2050}
2051#endif /* CONFIG_STRICT_MODULE_RWX */
2052
2053#ifdef CONFIG_LIVEPATCH
2054/*
2055 * Persist Elf information about a module. Copy the Elf header,
2056 * section header table, section string table, and symtab section
2057 * index from info to mod->klp_info.
2058 */
2059static int copy_module_elf(struct module *mod, struct load_info *info)
2060{
2061 unsigned int size, symndx;
2062 int ret;
2063
2064 size = sizeof(*mod->klp_info);
2065 mod->klp_info = kmalloc(size, GFP_KERNEL);
2066 if (mod->klp_info == NULL)
2067 return -ENOMEM;
2068
2069 /* Elf header */
2070 size = sizeof(mod->klp_info->hdr);
2071 memcpy(&mod->klp_info->hdr, info->hdr, size);
2072
2073 /* Elf section header table */
2074 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2075 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
2076 if (mod->klp_info->sechdrs == NULL) {
2077 ret = -ENOMEM;
2078 goto free_info;
2079 }
2080
2081 /* Elf section name string table */
2082 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2083 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
2084 if (mod->klp_info->secstrings == NULL) {
2085 ret = -ENOMEM;
2086 goto free_sechdrs;
2087 }
2088
2089 /* Elf symbol section index */
2090 symndx = info->index.sym;
2091 mod->klp_info->symndx = symndx;
2092
2093 /*
2094 * For livepatch modules, core_kallsyms.symtab is a complete
2095 * copy of the original symbol table. Adjust sh_addr to point
2096 * to core_kallsyms.symtab since the copy of the symtab in module
2097 * init memory is freed at the end of do_init_module().
2098 */
2099 mod->klp_info->sechdrs[symndx].sh_addr = \
2100 (unsigned long) mod->core_kallsyms.symtab;
2101
2102 return 0;
2103
2104free_sechdrs:
2105 kfree(mod->klp_info->sechdrs);
2106free_info:
2107 kfree(mod->klp_info);
2108 return ret;
2109}
2110
2111static void free_module_elf(struct module *mod)
2112{
2113 kfree(mod->klp_info->sechdrs);
2114 kfree(mod->klp_info->secstrings);
2115 kfree(mod->klp_info);
2116}
2117#else /* !CONFIG_LIVEPATCH */
2118static int copy_module_elf(struct module *mod, struct load_info *info)
2119{
2120 return 0;
2121}
2122
2123static void free_module_elf(struct module *mod)
2124{
2125}
2126#endif /* CONFIG_LIVEPATCH */
2127
2128void __weak module_memfree(void *module_region)
2129{
2130 /*
2131 * This memory may be RO, and freeing RO memory in an interrupt is not
2132 * supported by vmalloc.
2133 */
2134 WARN_ON(in_interrupt());
2135 vfree(module_region);
2136}
2137
2138void __weak module_arch_cleanup(struct module *mod)
2139{
2140}
2141
2142void __weak module_arch_freeing_init(struct module *mod)
2143{
2144}
2145
2146static void cfi_cleanup(struct module *mod);
2147
2148/* Free a module, remove from lists, etc. */
2149static void free_module(struct module *mod)
2150{
2151 trace_module_free(mod);
2152
2153 mod_sysfs_teardown(mod);
2154
2155 /*
2156 * We leave it in list to prevent duplicate loads, but make sure
2157 * that noone uses it while it's being deconstructed.
2158 */
2159 mutex_lock(&module_mutex);
2160 mod->state = MODULE_STATE_UNFORMED;
2161 mutex_unlock(&module_mutex);
2162
2163 /* Remove dynamic debug info */
2164 ddebug_remove_module(mod->name);
2165
2166 /* Arch-specific cleanup. */
2167 module_arch_cleanup(mod);
2168
2169 /* Module unload stuff */
2170 module_unload_free(mod);
2171
2172 /* Free any allocated parameters. */
2173 destroy_params(mod->kp, mod->num_kp);
2174
2175 if (is_livepatch_module(mod))
2176 free_module_elf(mod);
2177
2178 /* Now we can delete it from the lists */
2179 mutex_lock(&module_mutex);
2180 /* Unlink carefully: kallsyms could be walking list. */
2181 list_del_rcu(&mod->list);
2182 mod_tree_remove(mod);
2183 /* Remove this module from bug list, this uses list_del_rcu */
2184 module_bug_cleanup(mod);
2185 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2186 synchronize_rcu();
2187 mutex_unlock(&module_mutex);
2188 1187
2189 /* Clean up CFI for the module. */1188 /* Clean up CFI for the module. */
2190 cfi_cleanup(mod);1189 cfi_cleanup(mod);
@@ -2196,10 +1195,13 @@ static void free_module(struct module *mod) · 5 unmodified lines
2196 percpu_modfree(mod);1195 percpu_modfree(mod);
2197 1196
2198 /* Free lock-classes; relies on the preceding sync_rcu(). */1197 /* Free lock-classes; relies on the preceding sync_rcu(). */
2199 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);1198 lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
2200 1199
2201 /* Finally, free the core (containing the module structure) */1200 /* Finally, free the core (containing the module structure) */
2202 module_memfree(mod->core_layout.base);1201 module_memfree(mod->core_layout.base);
1202#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
1203 vfree(mod->data_layout.base);
1204#endif
2203}1205}
2204 1206
2205void *__symbol_get(const char *symbol)1207void *__symbol_get(const char *symbol)
@@ -2387,7 +1389,7 @@ unsigned int __weak arch_mod_section_prepend(struct module *mod, · 181 unmodified lines
2387}1389}
2388 1390
2389/* Update size with this section: return offset. */1391/* Update size with this section: return offset. */
2390static long get_offset(struct module *mod, unsigned int *size,1392long module_get_offset(struct module *mod, unsigned int *size,
2391 Elf_Shdr *sechdr, unsigned int section)1393 Elf_Shdr *sechdr, unsigned int section)
2392{1394{
2393 long ret;1395 long ret;
@@ -2437,30 +1439,32 @@ static void layout_sections(struct module *mod, struct load_info *info) · 43 unmodified lines
2437 for (i = 0; i < info->hdr->e_shnum; ++i) {1439 for (i = 0; i < info->hdr->e_shnum; ++i) {
2438 Elf_Shdr *s = &info->sechdrs[i];1440 Elf_Shdr *s = &info->sechdrs[i];
2439 const char *sname = info->secstrings + s->sh_name;1441 const char *sname = info->secstrings + s->sh_name;
1442 unsigned int *sizep;
2440 1443
2441 if ((s->sh_flags & masks[m][0]) != masks[m][0]1444 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2442 || (s->sh_flags & masks[m][1])1445 || (s->sh_flags & masks[m][1])
2443 || s->sh_entsize != ~0UL1446 || s->sh_entsize != ~0UL
2444 || module_init_layout_section(sname))1447 || module_init_layout_section(sname))
2445 continue;1448 continue;
2446 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);1449 sizep = m ? &mod->data_layout.size : &mod->core_layout.size;
1450 s->sh_entsize = module_get_offset(mod, sizep, s, i);
2447 pr_debug("\t%s\n", sname);1451 pr_debug("\t%s\n", sname);
2448 }1452 }
2449 switch (m) {1453 switch (m) {
2450 case 0: /* executable */1454 case 0: /* executable */
2451 mod->core_layout.size = debug_align(mod->core_layout.size);1455 mod->core_layout.size = strict_align(mod->core_layout.size);
2452 mod->core_layout.text_size = mod->core_layout.size;1456 mod->core_layout.text_size = mod->core_layout.size;
2453 break;1457 break;
2454 case 1: /* RO: text and ro-data */1458 case 1: /* RO: text and ro-data */
2455 mod->core_layout.size = debug_align(mod->core_layout.size);1459 mod->data_layout.size = strict_align(mod->data_layout.size);
2456 mod->core_layout.ro_size = mod->core_layout.size;1460 mod->data_layout.ro_size = mod->data_layout.size;
2457 break;1461 break;
2458 case 2: /* RO after init */1462 case 2: /* RO after init */
2459 mod->core_layout.size = debug_align(mod->core_layout.size);1463 mod->data_layout.size = strict_align(mod->data_layout.size);
2460 mod->core_layout.ro_after_init_size = mod->core_layout.size;1464 mod->data_layout.ro_after_init_size = mod->data_layout.size;
2461 break;1465 break;
2462 case 4: /* whole core */1466 case 4: /* whole core */
2463 mod->core_layout.size = debug_align(mod->core_layout.size);1467 mod->data_layout.size = strict_align(mod->data_layout.size);
2464 break;1468 break;
2465 }1469 }
2466 }1470 }
@@ -2476,17 +1480,17 @@ static void layout_sections(struct module *mod, struct load_info *info) · 9 unmodified lines
2476 || s->sh_entsize != ~0UL1480 || s->sh_entsize != ~0UL
2477 || !module_init_layout_section(sname))1481 || !module_init_layout_section(sname))
2478 continue;1482 continue;
2479 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)1483 s->sh_entsize = (module_get_offset(mod, &mod->init_layout.size, s, i)
2480 | INIT_OFFSET_MASK);1484 | INIT_OFFSET_MASK);
2481 pr_debug("\t%s\n", sname);1485 pr_debug("\t%s\n", sname);
2482 }1486 }
2483 switch (m) {1487 switch (m) {
2484 case 0: /* executable */1488 case 0: /* executable */
2485 mod->init_layout.size = debug_align(mod->init_layout.size);1489 mod->init_layout.size = strict_align(mod->init_layout.size);
2486 mod->init_layout.text_size = mod->init_layout.size;1490 mod->init_layout.text_size = mod->init_layout.size;
2487 break;1491 break;
2488 case 1: /* RO: text and ro-data */1492 case 1: /* RO: text and ro-data */
2489 mod->init_layout.size = debug_align(mod->init_layout.size);1493 mod->init_layout.size = strict_align(mod->init_layout.size);
2490 mod->init_layout.ro_size = mod->init_layout.size;1494 mod->init_layout.ro_size = mod->init_layout.size;
2491 break;1495 break;
2492 case 2:1496 case 2:
@@ -2497,7 +1501,7 @@ static void layout_sections(struct module *mod, struct load_info *info) · 4 unmodified lines
2497 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;1501 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2498 break;1502 break;
2499 case 4: /* whole init */1503 case 4: /* whole init */
2500 mod->init_layout.size = debug_align(mod->init_layout.size);1504 mod->init_layout.size = strict_align(mod->init_layout.size);
2501 break;1505 break;
2502 }1506 }
2503 }1507 }
@@ -2587,350 +1591,37 @@ static void free_modinfo(struct module *mod) · 83 unmodified lines
2587 if (attr->free)1591 if (attr->free)
2588 attr->free(mod);1592 attr->free(mod);
2589 }1593 }
2590}1594}
2591
2592#ifdef CONFIG_KALLSYMS
2593
2594/* Lookup exported symbol in given range of kernel_symbols */
2595static const struct kernel_symbol *lookup_exported_symbol(const char *name,
2596 const struct kernel_symbol *start,
2597 const struct kernel_symbol *stop)
2598{
2599 return bsearch(name, start, stop - start,
2600 sizeof(struct kernel_symbol), cmp_name);
2601}
2602
2603static int is_exported(const char *name, unsigned long value,
2604 const struct module *mod)
2605{
2606 const struct kernel_symbol *ks;
2607 if (!mod)
2608 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
2609 else
2610 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
2611
2612 return ks != NULL && kernel_symbol_value(ks) == value;
2613}
2614
2615/* As per nm */
2616static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2617{
2618 const Elf_Shdr *sechdrs = info->sechdrs;
2619
2620 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2621 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2622 return 'v';
2623 else
2624 return 'w';
2625 }
2626 if (sym->st_shndx == SHN_UNDEF)
2627 return 'U';
2628 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
2629 return 'a';
2630 if (sym->st_shndx >= SHN_LORESERVE)
2631 return '?';
2632 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2633 return 't';
2634 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2635 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2636 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2637 return 'r';
2638 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2639 return 'g';
2640 else
2641 return 'd';
2642 }
2643 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2644 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2645 return 's';
2646 else
2647 return 'b';
2648 }
2649 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2650 ".debug")) {
2651 return 'n';
2652 }
2653 return '?';
2654}
2655
2656static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2657 unsigned int shnum, unsigned int pcpundx)
2658{
2659 const Elf_Shdr *sec;
2660
2661 if (src->st_shndx == SHN_UNDEF
2662 || src->st_shndx >= shnum
2663 || !src->st_name)
2664 return false;
2665
2666#ifdef CONFIG_KALLSYMS_ALL
2667 if (src->st_shndx == pcpundx)
2668 return true;
2669#endif
2670
2671 sec = sechdrs + src->st_shndx;
2672 if (!(sec->sh_flags & SHF_ALLOC)
2673#ifndef CONFIG_KALLSYMS_ALL
2674 || !(sec->sh_flags & SHF_EXECINSTR)
2675#endif
2676 || (sec->sh_entsize & INIT_OFFSET_MASK))
2677 return false;
2678
2679 return true;
2680}
2681
2682/*
2683 * We only allocate and copy the strings needed by the parts of symtab
2684 * we keep. This is simple, but has the effect of making multiple
2685 * copies of duplicates. We could be more sophisticated, see
2686 * linux-kernel thread starting with
2687 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2688 */
2689static void layout_symtab(struct module *mod, struct load_info *info)
2690{
2691 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2692 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2693 const Elf_Sym *src;
2694 unsigned int i, nsrc, ndst, strtab_size = 0;
2695
2696 /* Put symbol section at end of init part of module. */
2697 symsect->sh_flags |= SHF_ALLOC;
2698 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
2699 info->index.sym) | INIT_OFFSET_MASK;
2700 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2701
2702 src = (void *)info->hdr + symsect->sh_offset;
2703 nsrc = symsect->sh_size / sizeof(*src);
2704
2705 /* Compute total space required for the core symbols' strtab. */
2706 for (ndst = i = 0; i < nsrc; i++) {
2707 if (i == 0 || is_livepatch_module(mod) ||
2708 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2709 info->index.pcpu)) {
2710 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2711 ndst++;
2712 }
2713 }
2714
2715 /* Append room for core symbols at end of core part. */
2716 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2717 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2718 mod->core_layout.size += strtab_size;
2719 info->core_typeoffs = mod->core_layout.size;
2720 mod->core_layout.size += ndst * sizeof(char);
2721 mod->core_layout.size = debug_align(mod->core_layout.size);
2722
2723 /* Put string table section at end of init part of module. */
2724 strsect->sh_flags |= SHF_ALLOC;
2725 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
2726 info->index.str) | INIT_OFFSET_MASK;
2727 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2728
2729 /* We'll tack temporary mod_kallsyms on the end. */
2730 mod->init_layout.size = ALIGN(mod->init_layout.size,
2731 __alignof__(struct mod_kallsyms));
2732 info->mod_kallsyms_init_off = mod->init_layout.size;
2733 mod->init_layout.size += sizeof(struct mod_kallsyms);
2734 info->init_typeoffs = mod->init_layout.size;
2735 mod->init_layout.size += nsrc * sizeof(char);
2736 mod->init_layout.size = debug_align(mod->init_layout.size);
2737}
2738
2739/*
2740 * We use the full symtab and strtab which layout_symtab arranged to
2741 * be appended to the init section. Later we switch to the cut-down
2742 * core-only ones.
2743 */
2744static void add_kallsyms(struct module *mod, const struct load_info *info)
2745{
2746 unsigned int i, ndst;
2747 const Elf_Sym *src;
2748 Elf_Sym *dst;
2749 char *s;
2750 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2751
2752 /* Set up to point into init section. */
2753 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2754
2755 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2756 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2757 /* Make sure we get permanent strtab: don't use info->strtab. */
2758 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2759 mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
2760
2761 /*
2762 * Now populate the cut down core kallsyms for after init
2763 * and set types up while we still have access to sections.
2764 */
2765 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2766 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2767 mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
2768 src = mod->kallsyms->symtab;
2769 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2770 mod->kallsyms->typetab[i] = elf_type(src + i, info);
2771 if (i == 0 || is_livepatch_module(mod) ||
2772 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2773 info->index.pcpu)) {
2774 mod->core_kallsyms.typetab[ndst] =
2775 mod->kallsyms->typetab[i];
2776 dst[ndst] = src[i];
2777 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2778 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2779 KSYM_NAME_LEN) + 1;
2780 }
2781 }
2782 mod->core_kallsyms.num_symtab = ndst;
2783}
2784#else
2785static inline void layout_symtab(struct module *mod, struct load_info *info)
2786{
2787}
2788
2789static void add_kallsyms(struct module *mod, const struct load_info *info)
2790{
2791}
2792#endif /* CONFIG_KALLSYMS */
2793
2794#if IS_ENABLED(CONFIG_KALLSYMS) && IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
2795static void init_build_id(struct module *mod, const struct load_info *info)
2796{
2797 const Elf_Shdr *sechdr;
2798 unsigned int i;
2799
2800 for (i = 0; i < info->hdr->e_shnum; i++) {
2801 sechdr = &info->sechdrs[i];
2802 if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE &&
2803 !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id,
2804 sechdr->sh_size))
2805 break;
2806 }
2807}
2808#else
2809static void init_build_id(struct module *mod, const struct load_info *info)
2810{
2811}
2812#endif
2813
2814static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2815{
2816 if (!debug)
2817 return;
2818 ddebug_add_module(debug, num, mod->name);
2819}
2820
2821static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2822{
2823 if (debug)
2824 ddebug_remove_module(mod->name);
2825}
2826
2827void * __weak module_alloc(unsigned long size)
2828{
2829 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2830 GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
2831 NUMA_NO_NODE, __builtin_return_address(0));
2832}
2833
2834bool __weak module_init_section(const char *name)
2835{
2836 return strstarts(name, ".init");
2837}
2838
2839bool __weak module_exit_section(const char *name)
2840{
2841 return strstarts(name, ".exit");
2842}
2843
2844#ifdef CONFIG_DEBUG_KMEMLEAK
2845static void kmemleak_load_module(const struct module *mod,
2846 const struct load_info *info)
2847{
2848 unsigned int i;
2849
2850 /* only scan the sections containing data */
2851 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2852
2853 for (i = 1; i < info->hdr->e_shnum; i++) {
2854 /* Scan all writable sections that's not executable */
2855 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2856 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2857 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2858 continue;
2859
2860 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2861 info->sechdrs[i].sh_size, GFP_KERNEL);
2862 }
2863}
2864#else
2865static inline void kmemleak_load_module(const struct module *mod,
2866 const struct load_info *info)
2867{
2868}
2869#endif
2870
2871#ifdef CONFIG_MODULE_SIG
2872static int module_sig_check(struct load_info *info, int flags)
2873{
2874 int err = -ENODATA;
2875 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2876 const char *reason;
2877 const void *mod = info->hdr;
2878 bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS |
2879 MODULE_INIT_IGNORE_VERMAGIC);
2880 /*
2881 * Do not allow mangled modules as a module with version information
2882 * removed is no longer the module that was signed.
2883 */
2884 if (!mangled_module &&
2885 info->len > markerlen &&
2886 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2887 /* We truncate the module to discard the signature */
2888 info->len -= markerlen;
2889 err = mod_verify_sig(mod, info);
2890 if (!err) {
2891 info->sig_ok = true;
2892 return 0;
2893 }
2894 }
2895 1595
2896 /*1596static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2897 * We don't permit modules to be loaded into the trusted kernels1597{
2898 * without a valid signature on them, but if we're not enforcing,1598 if (!debug)
2899 * certain errors are non-fatal.1599 return;
2900 */1600 ddebug_add_module(debug, num, mod->name);
2901 switch (err) {1601}
2902 case -ENODATA:
2903 reason = "unsigned module";
2904 break;
2905 case -ENOPKG:
2906 reason = "module with unsupported crypto";
2907 break;
2908 case -ENOKEY:
2909 reason = "module with unavailable key";
2910 break;
2911 1602
2912 default:1603static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2913 /*1604{
2914 * All other errors are fatal, including lack of memory,1605 if (debug)
2915 * unparseable signatures, and signature check failures --1606 ddebug_remove_module(mod->name);
2916 * even if signatures aren't required.1607}
2917 */
2918 return err;
2919 }
2920 1608
2921 if (is_module_sig_enforced()) {1609void * __weak module_alloc(unsigned long size)
2922 pr_notice("Loading of %s is rejected\n", reason);1610{
2923 return -EKEYREJECTED;1611 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2924 }1612 GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
1613 NUMA_NO_NODE, __builtin_return_address(0));
1614}
2925 1615
2926 return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);1616bool __weak module_init_section(const char *name)
1617{
1618 return strstarts(name, ".init");
2927}1619}
2928#else /* !CONFIG_MODULE_SIG */1620
2929static int module_sig_check(struct load_info *info, int flags)1621bool __weak module_exit_section(const char *name)
2930{1622{
2931 return 0;1623 return strstarts(name, ".exit");
2932}1624}
2933#endif /* !CONFIG_MODULE_SIG */
2934 1625
2935static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)1626static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
2936{1627{
@@ -3025,6 +1716,10 @@ static int elf_validity_check(struct load_info *info) · 88 unmodified lines
3025 * strings in the section safe.1716 * strings in the section safe.
3026 */1717 */
3027 info->secstrings = (void *)info->hdr + strhdr->sh_offset;1718 info->secstrings = (void *)info->hdr + strhdr->sh_offset;
1719 if (strhdr->sh_size == 0) {
1720 pr_err("empty section name table\n");
1721 goto no_exec;
1722 }
3028 if (info->secstrings[strhdr->sh_size - 1] != '\0') {1723 if (info->secstrings[strhdr->sh_size - 1] != '\0') {
3029 pr_err("ELF Spec violation: section name table isn't null terminated\n");1724 pr_err("ELF Spec violation: section name table isn't null terminated\n");
3030 goto no_exec;1725 goto no_exec;
@@ -3099,30 +1794,23 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l · 68 unmodified lines
3099 return 0;1794 return 0;
3100}1795}
3101 1796
3102#ifdef CONFIG_LIVEPATCH
3103static int check_modinfo_livepatch(struct module *mod, struct load_info *info)1797static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
3104{1798{
3105 if (get_modinfo(info, "livepatch")) {1799 if (!get_modinfo(info, "livepatch"))
3106 mod->klp = true;1800 /* Nothing more to do */
1801 return 0;
1802
1803 if (set_livepatch_module(mod)) {
3107 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);1804 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
3108 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",1805 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
3109 mod->name);1806 mod->name);
3110 }1807 return 0;
3111
3112 return 0;
3113}
3114#else /* !CONFIG_LIVEPATCH */
3115static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
3116{
3117 if (get_modinfo(info, "livepatch")) {
3118 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
3119 mod->name);
3120 return -ENOEXEC;
3121 }1808 }
3122 1809
3123 return 0;1810 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
1811 mod->name);
1812 return -ENOEXEC;
3124}1813}
3125#endif /* CONFIG_LIVEPATCH */
3126 1814
3127static void check_modinfo_retpoline(struct module *mod, struct load_info *info)1815static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
3128{1816{
@@ -3448,6 +2136,24 @@ static int move_module(struct module *mod, struct load_info *info) · 319 unmodified lines
3448 } else2136 } else
3449 mod->init_layout.base = NULL;2137 mod->init_layout.base = NULL;
3450 2138
2139#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
2140 /* Do the allocs. */
2141 ptr = vmalloc(mod->data_layout.size);
2142 /*
2143 * The pointer to this block is stored in the module structure
2144 * which is inside the block. Just mark it as not being a
2145 * leak.
2146 */
2147 kmemleak_not_leak(ptr);
2148 if (!ptr) {
2149 module_memfree(mod->core_layout.base);
2150 module_memfree(mod->init_layout.base);
2151 return -ENOMEM;
2152 }
2153
2154 memset(ptr, 0, mod->data_layout.size);
2155 mod->data_layout.base = ptr;
2156#endif
3451 /* Transfer each section which specifies SHF_ALLOC */2157 /* Transfer each section which specifies SHF_ALLOC */
3452 pr_debug("final section addresses:\n");2158 pr_debug("final section addresses:\n");
3453 for (i = 0; i < info->hdr->e_shnum; i++) {2159 for (i = 0; i < info->hdr->e_shnum; i++) {
@@ -3460,6 +2166,8 @@ static int move_module(struct module *mod, struct load_info *info) · 6 unmodified lines
3460 if (shdr->sh_entsize & INIT_OFFSET_MASK)2166 if (shdr->sh_entsize & INIT_OFFSET_MASK)
3461 dest = mod->init_layout.base2167 dest = mod->init_layout.base
3462 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);2168 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2169 else if (!(shdr->sh_flags & SHF_EXECINSTR))
2170 dest = mod->data_layout.base + shdr->sh_entsize;
3463 else2171 else
3464 dest = mod->core_layout.base + shdr->sh_entsize;2172 dest = mod->core_layout.base + shdr->sh_entsize;
3465 2173
@@ -3621,6 +2329,9 @@ static void module_deallocate(struct module *mod, struct load_info *info) · 155 unmodified lines
3621 module_arch_freeing_init(mod);2329 module_arch_freeing_init(mod);
3622 module_memfree(mod->init_layout.base);2330 module_memfree(mod->init_layout.base);
3623 module_memfree(mod->core_layout.base);2331 module_memfree(mod->core_layout.base);
2332#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
2333 vfree(mod->data_layout.base);
2334#endif
3624}2335}
3625 2336
3626int __weak module_finalize(const Elf_Ehdr *hdr,2337int __weak module_finalize(const Elf_Ehdr *hdr,
@@ -3871,6 +2582,9 @@ static int complete_formation(struct module *mod, struct load_info *info) · 244 unmodified lines
3871 /* This relies on module_mutex for list integrity. */2582 /* This relies on module_mutex for list integrity. */
3872 module_bug_finalize(info->hdr, info->sechdrs, mod);2583 module_bug_finalize(info->hdr, info->sechdrs, mod);
3873 2584
2585 if (module_check_misalignment(mod))
2586 goto out_misaligned;
2587
3874 module_enable_ro(mod, false);2588 module_enable_ro(mod, false);
3875 module_enable_nx(mod);2589 module_enable_nx(mod);
3876 module_enable_x(mod);2590 module_enable_x(mod);
@@ -3884,6 +2598,8 @@ static int complete_formation(struct module *mod, struct load_info *info) · 7 unmodified lines
3884 2598
3885 return 0;2599 return 0;
3886 2600
2601out_misaligned:
2602 err = -EINVAL;
3887out:2603out:
3888 mutex_unlock(&module_mutex);2604 mutex_unlock(&module_mutex);
3889 return err;2605 return err;
@@ -4150,7 +2866,7 @@ static int load_module(struct load_info *info, const char __user *uargs, · 260 unmodified lines
4150 mutex_unlock(&module_mutex);2866 mutex_unlock(&module_mutex);
4151 free_module:2867 free_module:
4152 /* Free lock-classes; relies on the preceding sync_rcu() */2868 /* Free lock-classes; relies on the preceding sync_rcu() */
4153 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);2869 lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
4154 2870
4155 module_deallocate(mod, info);2871 module_deallocate(mod, info);
4156 free_copy:2872 free_copy:
@@ -4219,287 +2935,6 @@ static inline int within(unsigned long addr, void *start, unsigned long size) · 62 unmodified lines
4219 return ((void *)addr >= start && (void *)addr < start + size);2935 return ((void *)addr >= start && (void *)addr < start + size);
4220}2936}
4221 2937
4222#ifdef CONFIG_KALLSYMS
4223/*
4224 * This ignores the intensely annoying "mapping symbols" found
4225 * in ARM ELF files: $a, $t and $d.
4226 */
4227static inline int is_arm_mapping_symbol(const char *str)
4228{
4229 if (str[0] == '.' && str[1] == 'L')
4230 return true;
4231 return str[0] == '$' && strchr("axtd", str[1])
4232 && (str[2] == '\0' || str[2] == '.');
4233}
4234
4235static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
4236{
4237 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
4238}
4239
4240/*
4241 * Given a module and address, find the corresponding symbol and return its name
4242 * while providing its size and offset if needed.
4243 */
4244static const char *find_kallsyms_symbol(struct module *mod,
4245 unsigned long addr,
4246 unsigned long *size,
4247 unsigned long *offset)
4248{
4249 unsigned int i, best = 0;
4250 unsigned long nextval, bestval;
4251 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4252
4253 /* At worse, next value is at end of module */
4254 if (within_module_init(addr, mod))
4255 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
4256 else
4257 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
4258
4259 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
4260
4261 /*
4262 * Scan for closest preceding symbol, and next symbol. (ELF
4263 * starts real symbols at 1).
4264 */
4265 for (i = 1; i < kallsyms->num_symtab; i++) {
4266 const Elf_Sym *sym = &kallsyms->symtab[i];
4267 unsigned long thisval = kallsyms_symbol_value(sym);
4268
4269 if (sym->st_shndx == SHN_UNDEF)
4270 continue;
4271
4272 /*
4273 * We ignore unnamed symbols: they're uninformative
4274 * and inserted at a whim.
4275 */
4276 if (*kallsyms_symbol_name(kallsyms, i) == '\0'
4277 || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
4278 continue;
4279
4280 if (thisval <= addr && thisval > bestval) {
4281 best = i;
4282 bestval = thisval;
4283 }
4284 if (thisval > addr && thisval < nextval)
4285 nextval = thisval;
4286 }
4287
4288 if (!best)
4289 return NULL;
4290
4291 if (size)
4292 *size = nextval - bestval;
4293 if (offset)
4294 *offset = addr - bestval;
4295
4296 return kallsyms_symbol_name(kallsyms, best);
4297}
4298
4299void * __weak dereference_module_function_descriptor(struct module *mod,
4300 void *ptr)
4301{
4302 return ptr;
4303}
4304
4305/*
4306 * For kallsyms to ask for address resolution. NULL means not found. Careful
4307 * not to lock to avoid deadlock on oopses, simply disable preemption.
4308 */
4309const char *module_address_lookup(unsigned long addr,
4310 unsigned long *size,
4311 unsigned long *offset,
4312 char **modname,
4313 const unsigned char **modbuildid,
4314 char *namebuf)
4315{
4316 const char *ret = NULL;
4317 struct module *mod;
4318
4319 preempt_disable();
4320 mod = __module_address(addr);
4321 if (mod) {
4322 if (modname)
4323 *modname = mod->name;
4324 if (modbuildid) {
4325#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
4326 *modbuildid = mod->build_id;
4327#else
4328 *modbuildid = NULL;
4329#endif
4330 }
4331
4332 ret = find_kallsyms_symbol(mod, addr, size, offset);
4333 }
4334 /* Make a copy in here where it's safe */
4335 if (ret) {
4336 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4337 ret = namebuf;
4338 }
4339 preempt_enable();
4340
4341 return ret;
4342}
4343
4344int lookup_module_symbol_name(unsigned long addr, char *symname)
4345{
4346 struct module *mod;
4347
4348 preempt_disable();
4349 list_for_each_entry_rcu(mod, &modules, list) {
4350 if (mod->state == MODULE_STATE_UNFORMED)
4351 continue;
4352 if (within_module(addr, mod)) {
4353 const char *sym;
4354
4355 sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
4356 if (!sym)
4357 goto out;
4358
4359 strlcpy(symname, sym, KSYM_NAME_LEN);
4360 preempt_enable();
4361 return 0;
4362 }
4363 }
4364out:
4365 preempt_enable();
4366 return -ERANGE;
4367}
4368
4369int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4370 unsigned long *offset, char *modname, char *name)
4371{
4372 struct module *mod;
4373
4374 preempt_disable();
4375 list_for_each_entry_rcu(mod, &modules, list) {
4376 if (mod->state == MODULE_STATE_UNFORMED)
4377 continue;
4378 if (within_module(addr, mod)) {
4379 const char *sym;
4380
4381 sym = find_kallsyms_symbol(mod, addr, size, offset);
4382 if (!sym)
4383 goto out;
4384 if (modname)
4385 strlcpy(modname, mod->name, MODULE_NAME_LEN);
4386 if (name)
4387 strlcpy(name, sym, KSYM_NAME_LEN);
4388 preempt_enable();
4389 return 0;
4390 }
4391 }
4392out:
4393 preempt_enable();
4394 return -ERANGE;
4395}
4396
4397int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4398 char *name, char *module_name, int *exported)
4399{
4400 struct module *mod;
4401
4402 preempt_disable();
4403 list_for_each_entry_rcu(mod, &modules, list) {
4404 struct mod_kallsyms *kallsyms;
4405
4406 if (mod->state == MODULE_STATE_UNFORMED)
4407 continue;
4408 kallsyms = rcu_dereference_sched(mod->kallsyms);
4409 if (symnum < kallsyms->num_symtab) {
4410 const Elf_Sym *sym = &kallsyms->symtab[symnum];
4411
4412 *value = kallsyms_symbol_value(sym);
4413 *type = kallsyms->typetab[symnum];
4414 strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
4415 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
4416 *exported = is_exported(name, *value, mod);
4417 preempt_enable();
4418 return 0;
4419 }
4420 symnum -= kallsyms->num_symtab;
4421 }
4422 preempt_enable();
4423 return -ERANGE;
4424}
4425
4426/* Given a module and name of symbol, find and return the symbol's value */
4427static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
4428{
4429 unsigned int i;
4430 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4431
4432 for (i = 0; i < kallsyms->num_symtab; i++) {
4433 const Elf_Sym *sym = &kallsyms->symtab[i];
4434
4435 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
4436 sym->st_shndx != SHN_UNDEF)
4437 return kallsyms_symbol_value(sym);
4438 }
4439 return 0;
4440}
4441
4442/* Look for this name: can be of form module:name. */
4443unsigned long module_kallsyms_lookup_name(const char *name)
4444{
4445 struct module *mod;
4446 char *colon;
4447 unsigned long ret = 0;
4448
4449 /* Don't lock: we're in enough trouble already. */
4450 preempt_disable();
4451 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
4452 if ((mod = find_module_all(name, colon - name, false)) != NULL)
4453 ret = find_kallsyms_symbol_value(mod, colon+1);
4454 } else {
4455 list_for_each_entry_rcu(mod, &modules, list) {
4456 if (mod->state == MODULE_STATE_UNFORMED)
4457 continue;
4458 if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
4459 break;
4460 }
4461 }
4462 preempt_enable();
4463 return ret;
4464}
4465
4466#ifdef CONFIG_LIVEPATCH
4467int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4468 struct module *, unsigned long),
4469 void *data)
4470{
4471 struct module *mod;
4472 unsigned int i;
4473 int ret = 0;
4474
4475 mutex_lock(&module_mutex);
4476 list_for_each_entry(mod, &modules, list) {
4477 /* We hold module_mutex: no need for rcu_dereference_sched */
4478 struct mod_kallsyms *kallsyms = mod->kallsyms;
4479
4480 if (mod->state == MODULE_STATE_UNFORMED)
4481 continue;
4482 for (i = 0; i < kallsyms->num_symtab; i++) {
4483 const Elf_Sym *sym = &kallsyms->symtab[i];
4484
4485 if (sym->st_shndx == SHN_UNDEF)
4486 continue;
4487
4488 ret = fn(data, kallsyms_symbol_name(kallsyms, i),
4489 mod, kallsyms_symbol_value(sym));
4490 if (ret != 0)
4491 goto out;
4492
4493 cond_resched();
4494 }
4495 }
4496out:
4497 mutex_unlock(&module_mutex);
4498 return ret;
4499}
4500#endif /* CONFIG_LIVEPATCH */
4501#endif /* CONFIG_KALLSYMS */
4502
4503static void cfi_init(struct module *mod)2938static void cfi_init(struct module *mod)
4504{2939{
4505#ifdef CONFIG_CFI_CLANG2940#ifdef CONFIG_CFI_CLANG
@@ -4523,22 +2958,19 @@ static void cfi_init(struct module *mod) · 17 unmodified lines
4523 mod->exit = *exit;2958 mod->exit = *exit;
4524#endif2959#endif
4525 2960
4526 cfi_module_add(mod, module_addr_min);2961 cfi_module_add(mod, mod_tree.addr_min);
4527#endif2962#endif
4528}2963}
4529 2964
4530static void cfi_cleanup(struct module *mod)2965static void cfi_cleanup(struct module *mod)
4531{2966{
4532#ifdef CONFIG_CFI_CLANG2967#ifdef CONFIG_CFI_CLANG
4533 cfi_module_remove(mod, module_addr_min);2968 cfi_module_remove(mod, mod_tree.addr_min);
4534#endif2969#endif
4535}2970}
4536 2971
4537/* Maximum number of characters written by module_flags() */
4538#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4539
4540/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */2972/* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4541static char *module_flags(struct module *mod, char *buf)2973char *module_flags(struct module *mod, char *buf)
4542{2974{
4543 int bx = 0;2975 int bx = 0;
4544 2976
@@ -4547,7 +2979,7 @@ static char *module_flags(struct module *mod, char *buf) · 2 unmodified lines
4547 mod->state == MODULE_STATE_GOING ||2979 mod->state == MODULE_STATE_GOING ||
4548 mod->state == MODULE_STATE_COMING) {2980 mod->state == MODULE_STATE_COMING) {
4549 buf[bx++] = '(';2981 buf[bx++] = '(';
4550 bx += module_flags_taint(mod, buf + bx);2982 bx += module_flags_taint(mod->taints, buf + bx);
4551 /* Show a - for module-is-being-unloaded */2983 /* Show a - for module-is-being-unloaded */
4552 if (mod->state == MODULE_STATE_GOING)2984 if (mod->state == MODULE_STATE_GOING)
4553 buf[bx++] = '-';2985 buf[bx++] = '-';
@@ -4561,103 +2993,6 @@ static char *module_flags(struct module *mod, char *buf) · 7 unmodified lines
4561 return buf;2993 return buf;
4562}2994}
4563 2995
4564#ifdef CONFIG_PROC_FS
4565/* Called by the /proc file system to return a list of modules. */
4566static void *m_start(struct seq_file *m, loff_t *pos)
4567{
4568 mutex_lock(&module_mutex);
4569 return seq_list_start(&modules, *pos);
4570}
4571
4572static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4573{
4574 return seq_list_next(p, &modules, pos);
4575}
4576
4577static void m_stop(struct seq_file *m, void *p)
4578{
4579 mutex_unlock(&module_mutex);
4580}
4581
4582static int m_show(struct seq_file *m, void *p)
4583{
4584 struct module *mod = list_entry(p, struct module, list);
4585 char buf[MODULE_FLAGS_BUF_SIZE];
4586 void *value;
4587
4588 /* We always ignore unformed modules. */
4589 if (mod->state == MODULE_STATE_UNFORMED)
4590 return 0;
4591
4592 seq_printf(m, "%s %u",
4593 mod->name, mod->init_layout.size + mod->core_layout.size);
4594 print_unload_info(m, mod);
4595
4596 /* Informative for users. */
4597 seq_printf(m, " %s",
4598 mod->state == MODULE_STATE_GOING ? "Unloading" :
4599 mod->state == MODULE_STATE_COMING ? "Loading" :
4600 "Live");
4601 /* Used by oprofile and other similar tools. */
4602 value = m->private ? NULL : mod->core_layout.base;
4603 seq_printf(m, " 0x%px", value);
4604
4605 /* Taints info */
4606 if (mod->taints)
4607 seq_printf(m, " %s", module_flags(mod, buf));
4608
4609 seq_puts(m, "\n");
4610 return 0;
4611}
4612
4613/*
4614 * Format: modulename size refcount deps address
4615 *
4616 * Where refcount is a number or -, and deps is a comma-separated list
4617 * of depends or -.
4618 */
4619static const struct seq_operations modules_op = {
4620 .start = m_start,
4621 .next = m_next,
4622 .stop = m_stop,
4623 .show = m_show
4624};
4625
4626/*
4627 * This also sets the "private" pointer to non-NULL if the
4628 * kernel pointers should be hidden (so you can just test
4629 * "m->private" to see if you should keep the values private).
4630 *
4631 * We use the same logic as for /proc/kallsyms.
4632 */
4633static int modules_open(struct inode *inode, struct file *file)
4634{
4635 int err = seq_open(file, &modules_op);
4636
4637 if (!err) {
4638 struct seq_file *m = file->private_data;
4639 m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
4640 }
4641
4642 return err;
4643}
4644
4645static const struct proc_ops modules_proc_ops = {
4646 .proc_flags = PROC_ENTRY_PERMANENT,
4647 .proc_open = modules_open,
4648 .proc_read = seq_read,
4649 .proc_lseek = seq_lseek,
4650 .proc_release = seq_release,
4651};
4652
4653static int __init proc_modules_init(void)
4654{
4655 proc_create("modules", 0, NULL, &modules_proc_ops);
4656 return 0;
4657}
4658module_init(proc_modules_init);
4659#endif
4660
4661/* Given an address, look for it in the module exception tables. */2996/* Given an address, look for it in the module exception tables. */
4662const struct exception_table_entry *search_module_extables(unsigned long addr)2997const struct exception_table_entry *search_module_extables(unsigned long addr)
4663{2998{
@@ -4713,13 +3048,20 @@ bool is_module_address(unsigned long addr) · 49 unmodified lines
4713struct module *__module_address(unsigned long addr)3048struct module *__module_address(unsigned long addr)
4714{3049{
4715 struct module *mod;3050 struct module *mod;
3051 struct mod_tree_root *tree;
4716 3052
4717 if (addr < module_addr_min || addr > module_addr_max)3053 if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max)
3054 tree = &mod_tree;
3055#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
3056 else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max)
3057 tree = &mod_data_tree;
3058#endif
3059 else
4718 return NULL;3060 return NULL;
4719 3061
4720 module_assert_mutex_or_preempt();3062 module_assert_mutex_or_preempt();
4721 3063
4722 mod = mod_find(addr);3064 mod = mod_find(addr, tree);
4723 if (mod) {3065 if (mod) {
4724 BUG_ON(!within_module(addr, mod));3066 BUG_ON(!within_module(addr, mod));
4725 if (mod->state == MODULE_STATE_UNFORMED)3067 if (mod->state == MODULE_STATE_UNFORMED)
@@ -4780,23 +3122,10 @@ void print_modules(void) · 54 unmodified lines
4780 continue;3122 continue;
4781 pr_cont(" %s%s", mod->name, module_flags(mod, buf));3123 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
4782 }3124 }
3125
3126 print_unloaded_tainted_modules();
4783 preempt_enable();3127 preempt_enable();
4784 if (last_unloaded_module[0])3128 if (last_unloaded_module[0])
4785 pr_cont(" [last unloaded: %s]", last_unloaded_module);3129 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4786 pr_cont("\n");3130 pr_cont("\n");
4787}3131}
4788
4789#ifdef CONFIG_MODVERSIONS
4790/*
4791 * Generate the signature for all relevant module structures here.
4792 * If these change, we don't want to try to parse the module.
4793 */
4794void module_layout(struct module *mod,
4795 struct modversion_info *ver,
4796 struct kernel_param *kp,
4797 struct kernel_symbol *ks,
4798 struct tracepoint * const *tp)
4799{
4800}
4801EXPORT_SYMBOL(module_layout);
4802#endif
kernel/module/procfs.c+146−0
@@ -0,0 +1,146 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module proc support
4 *
5 * Copyright (C) 2008 Alexey Dobriyan
6 */
7
8#include <linux/module.h>
9#include <linux/kallsyms.h>
10#include <linux/mutex.h>
11#include <linux/seq_file.h>
12#include <linux/proc_fs.h>
13#include "internal.h"
14
15#ifdef CONFIG_MODULE_UNLOAD
16static inline void print_unload_info(struct seq_file *m, struct module *mod)
17{
18 struct module_use *use;
19 int printed_something = 0;
20
21 seq_printf(m, " %i ", module_refcount(mod));
22
23 /*
24 * Always include a trailing , so userspace can differentiate
25 * between this and the old multi-field proc format.
26 */
27 list_for_each_entry(use, &mod->source_list, source_list) {
28 printed_something = 1;
29 seq_printf(m, "%s,", use->source->name);
30 }
31
32 if (mod->init && !mod->exit) {
33 printed_something = 1;
34 seq_puts(m, "[permanent],");
35 }
36
37 if (!printed_something)
38 seq_puts(m, "-");
39}
40#else /* !CONFIG_MODULE_UNLOAD */
41static inline void print_unload_info(struct seq_file *m, struct module *mod)
42{
43 /* We don't know the usage count, or what modules are using. */
44 seq_puts(m, " - -");
45}
46#endif /* CONFIG_MODULE_UNLOAD */
47
48/* Called by the /proc file system to return a list of modules. */
49static void *m_start(struct seq_file *m, loff_t *pos)
50{
51 mutex_lock(&module_mutex);
52 return seq_list_start(&modules, *pos);
53}
54
55static void *m_next(struct seq_file *m, void *p, loff_t *pos)
56{
57 return seq_list_next(p, &modules, pos);
58}
59
60static void m_stop(struct seq_file *m, void *p)
61{
62 mutex_unlock(&module_mutex);
63}
64
65static int m_show(struct seq_file *m, void *p)
66{
67 struct module *mod = list_entry(p, struct module, list);
68 char buf[MODULE_FLAGS_BUF_SIZE];
69 void *value;
70 unsigned int size;
71
72 /* We always ignore unformed modules. */
73 if (mod->state == MODULE_STATE_UNFORMED)
74 return 0;
75
76 size = mod->init_layout.size + mod->core_layout.size;
77#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
78 size += mod->data_layout.size;
79#endif
80 seq_printf(m, "%s %u", mod->name, size);
81 print_unload_info(m, mod);
82
83 /* Informative for users. */
84 seq_printf(m, " %s",
85 mod->state == MODULE_STATE_GOING ? "Unloading" :
86 mod->state == MODULE_STATE_COMING ? "Loading" :
87 "Live");
88 /* Used by oprofile and other similar tools. */
89 value = m->private ? NULL : mod->core_layout.base;
90 seq_printf(m, " 0x%px", value);
91
92 /* Taints info */
93 if (mod->taints)
94 seq_printf(m, " %s", module_flags(mod, buf));
95
96 seq_puts(m, "\n");
97 return 0;
98}
99
100/*
101 * Format: modulename size refcount deps address
102 *
103 * Where refcount is a number or -, and deps is a comma-separated list
104 * of depends or -.
105 */
106static const struct seq_operations modules_op = {
107 .start = m_start,
108 .next = m_next,
109 .stop = m_stop,
110 .show = m_show
111};
112
113/*
114 * This also sets the "private" pointer to non-NULL if the
115 * kernel pointers should be hidden (so you can just test
116 * "m->private" to see if you should keep the values private).
117 *
118 * We use the same logic as for /proc/kallsyms.
119 */
120static int modules_open(struct inode *inode, struct file *file)
121{
122 int err = seq_open(file, &modules_op);
123
124 if (!err) {
125 struct seq_file *m = file->private_data;
126
127 m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
128 }
129
130 return err;
131}
132
133static const struct proc_ops modules_proc_ops = {
134 .proc_flags = PROC_ENTRY_PERMANENT,
135 .proc_open = modules_open,
136 .proc_read = seq_read,
137 .proc_lseek = seq_lseek,
138 .proc_release = seq_release,
139};
140
141static int __init proc_modules_init(void)
142{
143 proc_create("modules", 0, NULL, &modules_proc_ops);
144 return 0;
145}
146module_init(proc_modules_init);
kernel/module/signing.c+122−0
@@ -0,0 +1,122 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* Module signature checker
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/module_signature.h>
12#include <linux/string.h>
13#include <linux/verification.h>
14#include <linux/security.h>
15#include <crypto/public_key.h>
16#include <uapi/linux/module.h>
17#include "internal.h"
18
19static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
20module_param(sig_enforce, bool_enable_only, 0644);
21
22/*
23 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
24 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
25 */
26bool is_module_sig_enforced(void)
27{
28 return sig_enforce;
29}
30EXPORT_SYMBOL(is_module_sig_enforced);
31
32void set_module_sig_enforced(void)
33{
34 sig_enforce = true;
35}
36
37/*
38 * Verify the signature on a module.
39 */
40int mod_verify_sig(const void *mod, struct load_info *info)
41{
42 struct module_signature ms;
43 size_t sig_len, modlen = info->len;
44 int ret;
45
46 pr_devel("==>%s(,%zu)\n", __func__, modlen);
47
48 if (modlen <= sizeof(ms))
49 return -EBADMSG;
50
51 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
52
53 ret = mod_check_sig(&ms, modlen, "module");
54 if (ret)
55 return ret;
56
57 sig_len = be32_to_cpu(ms.sig_len);
58 modlen -= sig_len + sizeof(ms);
59 info->len = modlen;
60
61 return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
62 VERIFY_USE_SECONDARY_KEYRING,
63 VERIFYING_MODULE_SIGNATURE,
64 NULL, NULL);
65}
66
67int module_sig_check(struct load_info *info, int flags)
68{
69 int err = -ENODATA;
70 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
71 const char *reason;
72 const void *mod = info->hdr;
73 bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS |
74 MODULE_INIT_IGNORE_VERMAGIC);
75 /*
76 * Do not allow mangled modules as a module with version information
77 * removed is no longer the module that was signed.
78 */
79 if (!mangled_module &&
80 info->len > markerlen &&
81 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
82 /* We truncate the module to discard the signature */
83 info->len -= markerlen;
84 err = mod_verify_sig(mod, info);
85 if (!err) {
86 info->sig_ok = true;
87 return 0;
88 }
89 }
90
91 /*
92 * We don't permit modules to be loaded into the trusted kernels
93 * without a valid signature on them, but if we're not enforcing,
94 * certain errors are non-fatal.
95 */
96 switch (err) {
97 case -ENODATA:
98 reason = "unsigned module";
99 break;
100 case -ENOPKG:
101 reason = "module with unsupported crypto";
102 break;
103 case -ENOKEY:
104 reason = "module with unavailable key";
105 break;
106
107 default:
108 /*
109 * All other errors are fatal, including lack of memory,
110 * unparseable signatures, and signature check failures --
111 * even if signatures aren't required.
112 */
113 return err;
114 }
115
116 if (is_module_sig_enforced()) {
117 pr_notice("Loading of %s is rejected\n", reason);
118 return -EKEYREJECTED;
119 }
120
121 return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);
122}
kernel/module/strict_rwx.c+143−0
@@ -0,0 +1,143 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module strict rwx
4 *
5 * Copyright (C) 2015 Rusty Russell
6 */
7
8#include <linux/module.h>
9#include <linux/mm.h>
10#include <linux/vmalloc.h>
11#include <linux/set_memory.h>
12#include "internal.h"
13
14/*
15 * LKM RO/NX protection: protect module's text/ro-data
16 * from modification and any data from execution.
17 *
18 * General layout of module is:
19 * [text] [read-only-data] [ro-after-init] [writable data]
20 * text_size -----^ ^ ^ ^
21 * ro_size ------------------------| | |
22 * ro_after_init_size -----------------------------| |
23 * size -----------------------------------------------------------|
24 *
25 * These values are always page-aligned (as is base) when
26 * CONFIG_STRICT_MODULE_RWX is set.
27 */
28
29/*
30 * Since some arches are moving towards PAGE_KERNEL module allocations instead
31 * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() independent of
32 * CONFIG_STRICT_MODULE_RWX because they are needed regardless of whether we
33 * are strict.
34 */
35static void frob_text(const struct module_layout *layout,
36 int (*set_memory)(unsigned long start, int num_pages))
37{
38 set_memory((unsigned long)layout->base,
39 PAGE_ALIGN(layout->text_size) >> PAGE_SHIFT);
40}
41
42static void frob_rodata(const struct module_layout *layout,
43 int (*set_memory)(unsigned long start, int num_pages))
44{
45 set_memory((unsigned long)layout->base + layout->text_size,
46 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
47}
48
49static void frob_ro_after_init(const struct module_layout *layout,
50 int (*set_memory)(unsigned long start, int num_pages))
51{
52 set_memory((unsigned long)layout->base + layout->ro_size,
53 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
54}
55
56static void frob_writable_data(const struct module_layout *layout,
57 int (*set_memory)(unsigned long start, int num_pages))
58{
59 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
60 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
61}
62
63static bool layout_check_misalignment(const struct module_layout *layout)
64{
65 return WARN_ON(!PAGE_ALIGNED(layout->base)) ||
66 WARN_ON(!PAGE_ALIGNED(layout->text_size)) ||
67 WARN_ON(!PAGE_ALIGNED(layout->ro_size)) ||
68 WARN_ON(!PAGE_ALIGNED(layout->ro_after_init_size)) ||
69 WARN_ON(!PAGE_ALIGNED(layout->size));
70}
71
72bool module_check_misalignment(const struct module *mod)
73{
74 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
75 return false;
76
77 return layout_check_misalignment(&mod->core_layout) ||
78 layout_check_misalignment(&mod->data_layout) ||
79 layout_check_misalignment(&mod->init_layout);
80}
81
82void module_enable_x(const struct module *mod)
83{
84 if (!PAGE_ALIGNED(mod->core_layout.base) ||
85 !PAGE_ALIGNED(mod->init_layout.base))
86 return;
87
88 frob_text(&mod->core_layout, set_memory_x);
89 frob_text(&mod->init_layout, set_memory_x);
90}
91
92void module_enable_ro(const struct module *mod, bool after_init)
93{
94 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
95 return;
96#ifdef CONFIG_STRICT_MODULE_RWX
97 if (!rodata_enabled)
98 return;
99#endif
100
101 set_vm_flush_reset_perms(mod->core_layout.base);
102 set_vm_flush_reset_perms(mod->init_layout.base);
103 frob_text(&mod->core_layout, set_memory_ro);
104
105 frob_rodata(&mod->data_layout, set_memory_ro);
106 frob_text(&mod->init_layout, set_memory_ro);
107 frob_rodata(&mod->init_layout, set_memory_ro);
108
109 if (after_init)
110 frob_ro_after_init(&mod->data_layout, set_memory_ro);
111}
112
113void module_enable_nx(const struct module *mod)
114{
115 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
116 return;
117
118 frob_rodata(&mod->data_layout, set_memory_nx);
119 frob_ro_after_init(&mod->data_layout, set_memory_nx);
120 frob_writable_data(&mod->data_layout, set_memory_nx);
121 frob_rodata(&mod->init_layout, set_memory_nx);
122 frob_writable_data(&mod->init_layout, set_memory_nx);
123}
124
125int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
126 char *secstrings, struct module *mod)
127{
128 const unsigned long shf_wx = SHF_WRITE | SHF_EXECINSTR;
129 int i;
130
131 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
132 return 0;
133
134 for (i = 0; i < hdr->e_shnum; i++) {
135 if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
136 pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
137 mod->name, secstrings + sechdrs[i].sh_name, i);
138 return -ENOEXEC;
139 }
140 }
141
142 return 0;
143}
kernel/module/sysfs.c+436−0
@@ -0,0 +1,436 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module sysfs support
4 *
5 * Copyright (C) 2008 Rusty Russell
6 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/sysfs.h>
12#include <linux/slab.h>
13#include <linux/kallsyms.h>
14#include <linux/mutex.h>
15#include "internal.h"
16
17/*
18 * /sys/module/foo/sections stuff
19 * J. Corbet <corbet@lwn.net>
20 */
21#ifdef CONFIG_KALLSYMS
22struct module_sect_attr {
23 struct bin_attribute battr;
24 unsigned long address;
25};
26
27struct module_sect_attrs {
28 struct attribute_group grp;
29 unsigned int nsections;
30 struct module_sect_attr attrs[];
31};
32
33#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
34static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
35 struct bin_attribute *battr,
36 char *buf, loff_t pos, size_t count)
37{
38 struct module_sect_attr *sattr =
39 container_of(battr, struct module_sect_attr, battr);
40 char bounce[MODULE_SECT_READ_SIZE + 1];
41 size_t wrote;
42
43 if (pos != 0)
44 return -EINVAL;
45
46 /*
47 * Since we're a binary read handler, we must account for the
48 * trailing NUL byte that sprintf will write: if "buf" is
49 * too small to hold the NUL, or the NUL is exactly the last
50 * byte, the read will look like it got truncated by one byte.
51 * Since there is no way to ask sprintf nicely to not write
52 * the NUL, we have to use a bounce buffer.
53 */
54 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
55 kallsyms_show_value(file->f_cred)
56 ? (void *)sattr->address : NULL);
57 count = min(count, wrote);
58 memcpy(buf, bounce, count);
59
60 return count;
61}
62
63static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
64{
65 unsigned int section;
66
67 for (section = 0; section < sect_attrs->nsections; section++)
68 kfree(sect_attrs->attrs[section].battr.attr.name);
69 kfree(sect_attrs);
70}
71
72static void add_sect_attrs(struct module *mod, const struct load_info *info)
73{
74 unsigned int nloaded = 0, i, size[2];
75 struct module_sect_attrs *sect_attrs;
76 struct module_sect_attr *sattr;
77 struct bin_attribute **gattr;
78
79 /* Count loaded sections and allocate structures */
80 for (i = 0; i < info->hdr->e_shnum; i++)
81 if (!sect_empty(&info->sechdrs[i]))
82 nloaded++;
83 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
84 sizeof(sect_attrs->grp.bin_attrs[0]));
85 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
86 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
87 if (!sect_attrs)
88 return;
89
90 /* Setup section attributes. */
91 sect_attrs->grp.name = "sections";
92 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
93
94 sect_attrs->nsections = 0;
95 sattr = &sect_attrs->attrs[0];
96 gattr = &sect_attrs->grp.bin_attrs[0];
97 for (i = 0; i < info->hdr->e_shnum; i++) {
98 Elf_Shdr *sec = &info->sechdrs[i];
99
100 if (sect_empty(sec))
101 continue;
102 sysfs_bin_attr_init(&sattr->battr);
103 sattr->address = sec->sh_addr;
104 sattr->battr.attr.name =
105 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
106 if (!sattr->battr.attr.name)
107 goto out;
108 sect_attrs->nsections++;
109 sattr->battr.read = module_sect_read;
110 sattr->battr.size = MODULE_SECT_READ_SIZE;
111 sattr->battr.attr.mode = 0400;
112 *(gattr++) = &(sattr++)->battr;
113 }
114 *gattr = NULL;
115
116 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
117 goto out;
118
119 mod->sect_attrs = sect_attrs;
120 return;
121out:
122 free_sect_attrs(sect_attrs);
123}
124
125static void remove_sect_attrs(struct module *mod)
126{
127 if (mod->sect_attrs) {
128 sysfs_remove_group(&mod->mkobj.kobj,
129 &mod->sect_attrs->grp);
130 /*
131 * We are positive that no one is using any sect attrs
132 * at this point. Deallocate immediately.
133 */
134 free_sect_attrs(mod->sect_attrs);
135 mod->sect_attrs = NULL;
136 }
137}
138
139/*
140 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
141 */
142
143struct module_notes_attrs {
144 struct kobject *dir;
145 unsigned int notes;
146 struct bin_attribute attrs[];
147};
148
149static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
150 struct bin_attribute *bin_attr,
151 char *buf, loff_t pos, size_t count)
152{
153 /*
154 * The caller checked the pos and count against our size.
155 */
156 memcpy(buf, bin_attr->private + pos, count);
157 return count;
158}
159
160static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
161 unsigned int i)
162{
163 if (notes_attrs->dir) {
164 while (i-- > 0)
165 sysfs_remove_bin_file(notes_attrs->dir,
166 &notes_attrs->attrs[i]);
167 kobject_put(notes_attrs->dir);
168 }
169 kfree(notes_attrs);
170}
171
172static void add_notes_attrs(struct module *mod, const struct load_info *info)
173{
174 unsigned int notes, loaded, i;
175 struct module_notes_attrs *notes_attrs;
176 struct bin_attribute *nattr;
177
178 /* failed to create section attributes, so can't create notes */
179 if (!mod->sect_attrs)
180 return;
181
182 /* Count notes sections and allocate structures. */
183 notes = 0;
184 for (i = 0; i < info->hdr->e_shnum; i++)
185 if (!sect_empty(&info->sechdrs[i]) &&
186 info->sechdrs[i].sh_type == SHT_NOTE)
187 ++notes;
188
189 if (notes == 0)
190 return;
191
192 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
193 GFP_KERNEL);
194 if (!notes_attrs)
195 return;
196
197 notes_attrs->notes = notes;
198 nattr = &notes_attrs->attrs[0];
199 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
200 if (sect_empty(&info->sechdrs[i]))
201 continue;
202 if (info->sechdrs[i].sh_type == SHT_NOTE) {
203 sysfs_bin_attr_init(nattr);
204 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
205 nattr->attr.mode = 0444;
206 nattr->size = info->sechdrs[i].sh_size;
207 nattr->private = (void *)info->sechdrs[i].sh_addr;
208 nattr->read = module_notes_read;
209 ++nattr;
210 }
211 ++loaded;
212 }
213
214 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
215 if (!notes_attrs->dir)
216 goto out;
217
218 for (i = 0; i < notes; ++i)
219 if (sysfs_create_bin_file(notes_attrs->dir,
220 &notes_attrs->attrs[i]))
221 goto out;
222
223 mod->notes_attrs = notes_attrs;
224 return;
225
226out:
227 free_notes_attrs(notes_attrs, i);
228}
229
230static void remove_notes_attrs(struct module *mod)
231{
232 if (mod->notes_attrs)
233 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
234}
235
236#else /* !CONFIG_KALLSYMS */
237static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
238static inline void remove_sect_attrs(struct module *mod) { }
239static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
240static inline void remove_notes_attrs(struct module *mod) { }
241#endif /* CONFIG_KALLSYMS */
242
243static void del_usage_links(struct module *mod)
244{
245#ifdef CONFIG_MODULE_UNLOAD
246 struct module_use *use;
247
248 mutex_lock(&module_mutex);
249 list_for_each_entry(use, &mod->target_list, target_list)
250 sysfs_remove_link(use->target->holders_dir, mod->name);
251 mutex_unlock(&module_mutex);
252#endif
253}
254
255static int add_usage_links(struct module *mod)
256{
257 int ret = 0;
258#ifdef CONFIG_MODULE_UNLOAD
259 struct module_use *use;
260
261 mutex_lock(&module_mutex);
262 list_for_each_entry(use, &mod->target_list, target_list) {
263 ret = sysfs_create_link(use->target->holders_dir,
264 &mod->mkobj.kobj, mod->name);
265 if (ret)
266 break;
267 }
268 mutex_unlock(&module_mutex);
269 if (ret)
270 del_usage_links(mod);
271#endif
272 return ret;
273}
274
275static void module_remove_modinfo_attrs(struct module *mod, int end)
276{
277 struct module_attribute *attr;
278 int i;
279
280 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
281 if (end >= 0 && i > end)
282 break;
283 /* pick a field to test for end of list */
284 if (!attr->attr.name)
285 break;
286 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
287 if (attr->free)
288 attr->free(mod);
289 }
290 kfree(mod->modinfo_attrs);
291}
292
293static int module_add_modinfo_attrs(struct module *mod)
294{
295 struct module_attribute *attr;
296 struct module_attribute *temp_attr;
297 int error = 0;
298 int i;
299
300 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
301 (modinfo_attrs_count + 1)),
302 GFP_KERNEL);
303 if (!mod->modinfo_attrs)
304 return -ENOMEM;
305
306 temp_attr = mod->modinfo_attrs;
307 for (i = 0; (attr = modinfo_attrs[i]); i++) {
308 if (!attr->test || attr->test(mod)) {
309 memcpy(temp_attr, attr, sizeof(*temp_attr));
310 sysfs_attr_init(&temp_attr->attr);
311 error = sysfs_create_file(&mod->mkobj.kobj,
312 &temp_attr->attr);
313 if (error)
314 goto error_out;
315 ++temp_attr;
316 }
317 }
318
319 return 0;
320
321error_out:
322 if (i > 0)
323 module_remove_modinfo_attrs(mod, --i);
324 else
325 kfree(mod->modinfo_attrs);
326 return error;
327}
328
329static void mod_kobject_put(struct module *mod)
330{
331 DECLARE_COMPLETION_ONSTACK(c);
332
333 mod->mkobj.kobj_completion = &c;
334 kobject_put(&mod->mkobj.kobj);
335 wait_for_completion(&c);
336}
337
338static int mod_sysfs_init(struct module *mod)
339{
340 int err;
341 struct kobject *kobj;
342
343 if (!module_sysfs_initialized) {
344 pr_err("%s: module sysfs not initialized\n", mod->name);
345 err = -EINVAL;
346 goto out;
347 }
348
349 kobj = kset_find_obj(module_kset, mod->name);
350 if (kobj) {
351 pr_err("%s: module is already loaded\n", mod->name);
352 kobject_put(kobj);
353 err = -EINVAL;
354 goto out;
355 }
356
357 mod->mkobj.mod = mod;
358
359 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
360 mod->mkobj.kobj.kset = module_kset;
361 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
362 "%s", mod->name);
363 if (err)
364 mod_kobject_put(mod);
365
366out:
367 return err;
368}
369
370int mod_sysfs_setup(struct module *mod,
371 const struct load_info *info,
372 struct kernel_param *kparam,
373 unsigned int num_params)
374{
375 int err;
376
377 err = mod_sysfs_init(mod);
378 if (err)
379 goto out;
380
381 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
382 if (!mod->holders_dir) {
383 err = -ENOMEM;
384 goto out_unreg;
385 }
386
387 err = module_param_sysfs_setup(mod, kparam, num_params);
388 if (err)
389 goto out_unreg_holders;
390
391 err = module_add_modinfo_attrs(mod);
392 if (err)
393 goto out_unreg_param;
394
395 err = add_usage_links(mod);
396 if (err)
397 goto out_unreg_modinfo_attrs;
398
399 add_sect_attrs(mod, info);
400 add_notes_attrs(mod, info);
401
402 return 0;
403
404out_unreg_modinfo_attrs:
405 module_remove_modinfo_attrs(mod, -1);
406out_unreg_param:
407 module_param_sysfs_remove(mod);
408out_unreg_holders:
409 kobject_put(mod->holders_dir);
410out_unreg:
411 mod_kobject_put(mod);
412out:
413 return err;
414}
415
416static void mod_sysfs_fini(struct module *mod)
417{
418 remove_notes_attrs(mod);
419 remove_sect_attrs(mod);
420 mod_kobject_put(mod);
421}
422
423void mod_sysfs_teardown(struct module *mod)
424{
425 del_usage_links(mod);
426 module_remove_modinfo_attrs(mod, -1);
427 module_param_sysfs_remove(mod);
428 kobject_put(mod->mkobj.drivers_dir);
429 kobject_put(mod->holders_dir);
430 mod_sysfs_fini(mod);
431}
432
433void init_param_lock(struct module *mod)
434{
435 mutex_init(&mod->param_lock);
436}
kernel/module/tracking.c+61−0
@@ -0,0 +1,61 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module taint unload tracking support
4 *
5 * Copyright (C) 2022 Aaron Tomlin
6 */
7
8#include <linux/module.h>
9#include <linux/string.h>
10#include <linux/printk.h>
11#include <linux/slab.h>
12#include <linux/list.h>
13#include <linux/rculist.h>
14#include "internal.h"
15
16static LIST_HEAD(unloaded_tainted_modules);
17
18int try_add_tainted_module(struct module *mod)
19{
20 struct mod_unload_taint *mod_taint;
21
22 module_assert_mutex_or_preempt();
23
24 list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, list,
25 lockdep_is_held(&module_mutex)) {
26 if (!strcmp(mod_taint->name, mod->name) &&
27 mod_taint->taints & mod->taints) {
28 mod_taint->count++;
29 goto out;
30 }
31 }
32
33 mod_taint = kmalloc(sizeof(*mod_taint), GFP_KERNEL);
34 if (unlikely(!mod_taint))
35 return -ENOMEM;
36 strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN);
37 mod_taint->taints = mod->taints;
38 list_add_rcu(&mod_taint->list, &unloaded_tainted_modules);
39 mod_taint->count = 1;
40out:
41 return 0;
42}
43
44void print_unloaded_tainted_modules(void)
45{
46 struct mod_unload_taint *mod_taint;
47 char buf[MODULE_FLAGS_BUF_SIZE];
48
49 if (!list_empty(&unloaded_tainted_modules)) {
50 printk(KERN_DEFAULT "Unloaded tainted modules:");
51 list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules,
52 list) {
53 size_t l;
54
55 l = module_flags_taint(mod_taint->taints, buf);
56 buf[l++] = '\0';
57 pr_cont(" %s(%s):%llu", mod_taint->name, buf,
58 mod_taint->count);
59 }
60 }
61}
kernel/module/tree_lookup.c+117−0
@@ -0,0 +1,117 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Modules tree lookup
4 *
5 * Copyright (C) 2015 Peter Zijlstra
6 * Copyright (C) 2015 Rusty Russell
7 */
8
9#include <linux/module.h>
10#include <linux/rbtree_latch.h>
11#include "internal.h"
12
13/*
14 * Use a latched RB-tree for __module_address(); this allows us to use
15 * RCU-sched lookups of the address from any context.
16 *
17 * This is conditional on PERF_EVENTS || TRACING because those can really hit
18 * __module_address() hard by doing a lot of stack unwinding; potentially from
19 * NMI context.
20 */
21
22static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
23{
24 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
25
26 return (unsigned long)layout->base;
27}
28
29static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
30{
31 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
32
33 return (unsigned long)layout->size;
34}
35
36static __always_inline bool
37mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
38{
39 return __mod_tree_val(a) < __mod_tree_val(b);
40}
41
42static __always_inline int
43mod_tree_comp(void *key, struct latch_tree_node *n)
44{
45 unsigned long val = (unsigned long)key;
46 unsigned long start, end;
47
48 start = __mod_tree_val(n);
49 if (val < start)
50 return -1;
51
52 end = start + __mod_tree_size(n);
53 if (val >= end)
54 return 1;
55
56 return 0;
57}
58
59static const struct latch_tree_ops mod_tree_ops = {
60 .less = mod_tree_less,
61 .comp = mod_tree_comp,
62};
63
64static noinline void __mod_tree_insert(struct mod_tree_node *node, struct mod_tree_root *tree)
65{
66 latch_tree_insert(&node->node, &tree->root, &mod_tree_ops);
67}
68
69static void __mod_tree_remove(struct mod_tree_node *node, struct mod_tree_root *tree)
70{
71 latch_tree_erase(&node->node, &tree->root, &mod_tree_ops);
72}
73
74/*
75 * These modifications: insert, remove_init and remove; are serialized by the
76 * module_mutex.
77 */
78void mod_tree_insert(struct module *mod)
79{
80 mod->core_layout.mtn.mod = mod;
81 mod->init_layout.mtn.mod = mod;
82
83 __mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
84 if (mod->init_layout.size)
85 __mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
86
87#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
88 mod->data_layout.mtn.mod = mod;
89 __mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
90#endif
91}
92
93void mod_tree_remove_init(struct module *mod)
94{
95 if (mod->init_layout.size)
96 __mod_tree_remove(&mod->init_layout.mtn, &mod_tree);
97}
98
99void mod_tree_remove(struct module *mod)
100{
101 __mod_tree_remove(&mod->core_layout.mtn, &mod_tree);
102 mod_tree_remove_init(mod);
103#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
104 __mod_tree_remove(&mod->data_layout.mtn, &mod_data_tree);
105#endif
106}
107
108struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
109{
110 struct latch_tree_node *ltn;
111
112 ltn = latch_tree_find((void *)addr, &tree->root, &mod_tree_ops);
113 if (!ltn)
114 return NULL;
115
116 return container_of(ltn, struct mod_tree_node, node)->mod;
117}
kernel/module/version.c+101−0
@@ -0,0 +1,101 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module version support
4 *
5 * Copyright (C) 2008 Rusty Russell
6 */
7
8#include <linux/module.h>
9#include <linux/string.h>
10#include <linux/printk.h>
11#include "internal.h"
12
13int check_version(const struct load_info *info,
14 const char *symname,
15 struct module *mod,
16 const s32 *crc)
17{
18 Elf_Shdr *sechdrs = info->sechdrs;
19 unsigned int versindex = info->index.vers;
20 unsigned int i, num_versions;
21 struct modversion_info *versions;
22
23 /* Exporting module didn't supply crcs? OK, we're already tainted. */
24 if (!crc)
25 return 1;
26
27 /* No versions at all? modprobe --force does this. */
28 if (versindex == 0)
29 return try_to_force_load(mod, symname) == 0;
30
31 versions = (void *)sechdrs[versindex].sh_addr;
32 num_versions = sechdrs[versindex].sh_size
33 / sizeof(struct modversion_info);
34
35 for (i = 0; i < num_versions; i++) {
36 u32 crcval;
37
38 if (strcmp(versions[i].name, symname) != 0)
39 continue;
40
41 crcval = *crc;
42 if (versions[i].crc == crcval)
43 return 1;
44 pr_debug("Found checksum %X vs module %lX\n",
45 crcval, versions[i].crc);
46 goto bad_version;
47 }
48
49 /* Broken toolchain. Warn once, then let it go.. */
50 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
51 return 1;
52
53bad_version:
54 pr_warn("%s: disagrees about version of symbol %s\n", info->name, symname);
55 return 0;
56}
57
58int check_modstruct_version(const struct load_info *info,
59 struct module *mod)
60{
61 struct find_symbol_arg fsa = {
62 .name = "module_layout",
63 .gplok = true,
64 };
65
66 /*
67 * Since this should be found in kernel (which can't be removed), no
68 * locking is necessary -- use preempt_disable() to placate lockdep.
69 */
70 preempt_disable();
71 if (!find_symbol(&fsa)) {
72 preempt_enable();
73 BUG();
74 }
75 preempt_enable();
76 return check_version(info, "module_layout", mod, fsa.crc);
77}
78
79/* First part is kernel version, which we ignore if module has crcs. */
80int same_magic(const char *amagic, const char *bmagic,
81 bool has_crcs)
82{
83 if (has_crcs) {
84 amagic += strcspn(amagic, " ");
85 bmagic += strcspn(bmagic, " ");
86 }
87 return strcmp(amagic, bmagic) == 0;
88}
89
90/*
91 * Generate the signature for all relevant module structures here.
92 * If these change, we don't want to try to parse the module.
93 */
94void module_layout(struct module *mod,
95 struct modversion_info *ver,
96 struct kernel_param *kp,
97 struct kernel_symbol *ks,
98 struct tracepoint * const *tp)
99{
100}
101EXPORT_SYMBOL(module_layout);
kernel/module_signing.c+0−45
@@ -1,45 +0,0 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* Module signature checker
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/module_signature.h>
12#include <linux/string.h>
13#include <linux/verification.h>
14#include <crypto/public_key.h>
15#include "module-internal.h"
16
17/*
18 * Verify the signature on a module.
19 */
20int mod_verify_sig(const void *mod, struct load_info *info)
21{
22 struct module_signature ms;
23 size_t sig_len, modlen = info->len;
24 int ret;
25
26 pr_devel("==>%s(,%zu)\n", __func__, modlen);
27
28 if (modlen <= sizeof(ms))
29 return -EBADMSG;
30
31 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
32
33 ret = mod_check_sig(&ms, modlen, "module");
34 if (ret)
35 return ret;
36
37 sig_len = be32_to_cpu(ms.sig_len);
38 modlen -= sig_len + sizeof(ms);
39 info->len = modlen;
40
41 return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
42 VERIFY_USE_SECONDARY_KEYRING,
43 VERIFYING_MODULE_SIGNATURE,
44 NULL, NULL);
45}