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
1098710987 F: include/linux/kdb.h
1098810988 F: include/linux/kgdb.h
1098910989 F: kernel/debug/
10990+F: kernel/module/kdb.c
1099010991
1099110992 KHADAS MCU MFD DRIVER
1099210993 M: Neil Armstrong <narmstrong@baylibre.com>
@@ -11440,6 +11441,7 @@ F: arch/s390/include/asm/livepatch.h · 447 unmodified lines
1144011441 F: arch/x86/include/asm/livepatch.h
1144111442 F: include/linux/livepatch.h
1144211443 F: kernel/livepatch/
11444+F: kernel/module/livepatch.c
1144311445 F: lib/livepatch/
1144411446 F: samples/livepatch/
1144511447 F: tools/testing/selftests/livepatch/
@@ -13371,7 +13373,7 @@ L: linux-kernel@vger.kernel.org · 1925 unmodified lines
1337113373 S: Maintained
1337213374 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next
1337313375 F: include/linux/module.h
13374−F: kernel/module.c
13376+F: kernel/module/
1337513377
1337613378 MONOLITHIC POWER SYSTEM PMIC DRIVER
1337713379 M: Saravanan Sekar <sravanhome@gmail.com>
arch/Kconfig+6−0
@@ -892,6 +892,12 @@ config MODULES_USE_ELF_REL · 891 unmodified lines
892892 Modules only use ELF REL relocations. Modules with ELF RELA
893893 relocations will give an error.
894894
895+config 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+
895901 config HAVE_IRQ_EXIT_ON_IRQ_STACK
896902 bool
897903 help
arch/powerpc/Kconfig+1−0
@@ -158,6 +158,7 @@ config PPC · 157 unmodified lines
158158 select ARCH_WANT_IPC_PARSE_VERSION
159159 select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
160160 select ARCH_WANT_LD_ORPHAN_WARN
161+ select ARCH_WANTS_MODULES_DATA_IN_VMALLOC if PPC_BOOK3S_32 || PPC_8xx
161162 select ARCH_WEAK_RELEASE_ACQUIRE
162163 select BINFMT_ELF
163164 select BUILDTIME_TABLE_SORT
include/linux/kdb.h+1−0
@@ -222,5 +222,6 @@ enum { · 221 unmodified lines
222222
223223 extern int kdbgetintenv(const char *, int *);
224224 extern int kdb_set(int, const char **);
225+int kdb_lsmod(int argc, const char **argv);
225226
226227 #endif /* !_KDB_H */
include/linux/module.h+19−13
@@ -290,8 +290,7 @@ extern typeof(name) __mod_##type##__##name##_device_table \ · 289 unmodified lines
290290 * files require multiple MODULE_FIRMWARE() specifiers */
291291 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
292292
293−#define _MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
294−#define MODULE_IMPORT_NS(ns) _MODULE_IMPORT_NS(ns)
293+#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, __stringify(ns))
295294
296295 struct notifier_block;
297296
@@ -422,6 +421,9 @@ struct module { · 124 unmodified lines
422421 /* Core layout: rbtree is accessed frequently, so keep together. */
423422 struct module_layout core_layout __module_layout_align;
424423 struct module_layout init_layout;
424+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
425+ struct module_layout data_layout;
426+#endif
425427
426428 /* Arch-specific module values */
427429 struct mod_arch_specific arch;
@@ -569,6 +571,11 @@ bool is_module_text_address(unsigned long addr); · 141 unmodified lines
569571 static inline bool within_module_core(unsigned long addr,
570572 const struct module *mod)
571573 {
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
572579 return (unsigned long)mod->core_layout.base <= addr &&
573580 addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;
574581 }
@@ -663,19 +670,15 @@ static inline bool module_requested_async_probing(struct module *module) · 88 unmodified lines
663670 return module && module->async_probe_requested;
664671 }
665672
666−#ifdef CONFIG_LIVEPATCH
667673 static inline bool is_livepatch_module(struct module *mod)
668674 {
675+#ifdef CONFIG_LIVEPATCH
669676 return mod->klp;
670−}
671−#else /* !CONFIG_LIVEPATCH */
672−static inline bool is_livepatch_module(struct module *mod)
673−{
677+#else
674678 return false;
679+#endif
675680 }
676−#endif /* CONFIG_LIVEPATCH */
677681
678−bool is_module_sig_enforced(void);
679682 void set_module_sig_enforced(void);
680683
681684 #else /* !CONFIG_MODULES... */
@@ -802,10 +805,6 @@ static inline bool module_requested_async_probing(struct module *module) · 120 unmodified lines
802805 return false;
803806 }
804807
805−static inline bool is_module_sig_enforced(void)
806−{
807− return false;
808−}
809808
810809 static inline void set_module_sig_enforced(void)
811810 {
@@ -857,11 +856,18 @@ static inline bool retpoline_module_ok(bool has_retpoline) · 45 unmodified lines
857856 #endif
858857
859858 #ifdef CONFIG_MODULE_SIG
859+bool is_module_sig_enforced(void);
860+
860861 static inline bool module_sig_ok(struct module *module)
861862 {
862863 return module->sig_ok;
863864 }
864865 #else /* !CONFIG_MODULE_SIG */
866+static inline bool is_module_sig_enforced(void)
867+{
868+ return false;
869+}
870+
865871 static inline bool module_sig_ok(struct module *module)
866872 {
867873 return true;
init/Kconfig+11−0
@@ -1983,6 +1983,17 @@ config MODULE_FORCE_UNLOAD · 1982 unmodified lines
19831983 rmmod). This is mainly for kernel developers and desperate users.
19841984 If unsure, say N.
19851985
1986+config 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+
19861997 config MODVERSIONS
19871998 bool "Module versioning support"
19881999 help
kernel/Makefile+1−4
@@ -29,7 +29,6 @@ KCOV_INSTRUMENT_softirq.o := n · 28 unmodified lines
2929 KCSAN_SANITIZE_softirq.o = n
3030 # These are called from save_stack_trace() on slub debug path,
3131 # and produce insane amounts of uninteresting coverage.
32−KCOV_INSTRUMENT_module.o := n
3332 KCOV_INSTRUMENT_extable.o := n
3433 KCOV_INSTRUMENT_stacktrace.o := n
3534 # Don't self-instrument.
@@ -53,6 +52,7 @@ obj-y += rcu/ · 17 unmodified lines
5352 obj-y += livepatch/
5453 obj-y += dma/
5554 obj-y += entry/
55+obj-$(CONFIG_MODULES) += module/
5656
5757 obj-$(CONFIG_KCMP) += kcmp.o
5858 obj-$(CONFIG_FREEZER) += freezer.o
@@ -66,9 +66,6 @@ ifneq ($(CONFIG_SMP),y) · 7 unmodified lines
6666 obj-y += up.o
6767 endif
6868 obj-$(CONFIG_UID16) += uid16.o
69−obj-$(CONFIG_MODULES) += module.o
70−obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
71−obj-$(CONFIG_MODULE_SIG) += module_signing.o
7269 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
7370 obj-$(CONFIG_KALLSYMS) += kallsyms.o
7471 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
kernel/debug/kdb/kdb_io.c+0−1
@@ -9,7 +9,6 @@ · 8 unmodified lines
99 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
1010 */
1111
12−#include <linux/module.h>
1312 #include <linux/types.h>
1413 #include <linux/ctype.h>
1514 #include <linux/kernel.h>
kernel/debug/kdb/kdb_keyboard.c+0−1
@@ -11,7 +11,6 @@ · 10 unmodified lines
1111 #include <linux/kdb.h>
1212 #include <linux/keyboard.h>
1313 #include <linux/ctype.h>
14−#include <linux/module.h>
1514 #include <linux/io.h>
1615
1716 /* Keyboard Controller Registers on normal PCs. */
kernel/debug/kdb/kdb_main.c+0−49
@@ -26,7 +26,6 @@ · 25 unmodified lines
2626 #include <linux/utsname.h>
2727 #include <linux/vmalloc.h>
2828 #include <linux/atomic.h>
29−#include <linux/module.h>
3029 #include <linux/moduleparam.h>
3130 #include <linux/mm.h>
3231 #include <linux/init.h>
@@ -2060,54 +2059,6 @@ static int kdb_ef(int argc, const char **argv) · 2027 unmodified lines
20602059 return 0;
20612060 }
20622061
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− */
2069−static 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−
21112062 /*
21122063 * kdb_env - This function implements the 'env' command. Display the
21132064 * 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
226226 #define kdb_kbd_cleanup_state()
227227 #endif /* ! CONFIG_KDB_KEYBOARD */
228228
229−#ifdef CONFIG_MODULES
230−extern struct list_head *kdb_modules;
231−#endif /* CONFIG_MODULES */
232−
233229 extern char kdb_prompt_str[];
234230
235231 #define KDB_WORD_SIZE ((int)sizeof(unsigned long))
kernel/debug/kdb/kdb_support.c+0−1
@@ -17,7 +17,6 @@ · 16 unmodified lines
1717 #include <linux/stddef.h>
1818 #include <linux/vmalloc.h>
1919 #include <linux/ptrace.h>
20−#include <linux/module.h>
2120 #include <linux/highmem.h>
2221 #include <linux/hardirq.h>
2322 #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−
11−struct 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−
36−extern int mod_verify_sig(const void *mod, struct load_info *info);
37−
38−#ifdef CONFIG_MODULE_DECOMPRESS
39−int module_decompress(struct load_info *info, const void *buf, size_t size);
40−void module_decompress_cleanup(struct load_info *info);
41−#else
42−static inline int module_decompress(struct load_info *info,
43− const void *buf, size_t size)
44−{
45− return -EOPNOTSUPP;
46−}
47−static 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.
8+KCOV_INSTRUMENT_module.o := n
9+
10+obj-y += main.o strict_rwx.o
11+obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
12+obj-$(CONFIG_MODULE_SIG) += signing.o
13+obj-$(CONFIG_LIVEPATCH) += livepatch.o
14+obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
15+obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
16+obj-$(CONFIG_KALLSYMS) += kallsyms.o
17+obj-$(CONFIG_PROC_FS) += procfs.o
18+obj-$(CONFIG_SYSFS) += sysfs.o
19+obj-$(CONFIG_KGDB_KDB) += kdb.o
20+obj-$(CONFIG_MODVERSIONS) += version.o
21+obj-$(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+
12+void 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
1212 #include <linux/sysfs.h>
1313 #include <linux/vmalloc.h>
1414
15−#include "module-internal.h"
15+#include "internal.h"
1616
1717 static int module_extend_max_pages(struct load_info *info, unsigned int extent)
1818 {
@@ -113,6 +113,7 @@ static ssize_t module_gzip_decompress(struct load_info *info, · 94 unmodified lines
113113
114114 do {
115115 struct page *page = module_get_next_page(info);
116+
116117 if (!page) {
117118 retval = -ENOMEM;
118119 goto out_inflate_end;
@@ -171,6 +172,7 @@ static ssize_t module_xz_decompress(struct load_info *info, · 52 unmodified lines
171172
172173 do {
173174 struct page *page = module_get_next_page(info);
175+
174176 if (!page) {
175177 retval = -ENOMEM;
176178 goto out;
@@ -256,6 +258,7 @@ static ssize_t compression_show(struct kobject *kobj, · 79 unmodified lines
256258 {
257259 return sysfs_emit(buf, "%s\n", __stringify(MODULE_COMPRESSION));
258260 }
261+
259262 static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
260263
261264 static 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+
39+extern struct mutex module_mutex;
40+extern struct list_head modules;
41+
42+extern struct module_attribute *modinfo_attrs[];
43+extern size_t modinfo_attrs_count;
44+
45+/* Provided by the linker */
46+extern const struct kernel_symbol __start___ksymtab[];
47+extern const struct kernel_symbol __stop___ksymtab[];
48+extern const struct kernel_symbol __start___ksymtab_gpl[];
49+extern const struct kernel_symbol __stop___ksymtab_gpl[];
50+extern const s32 __start___kcrctab[];
51+extern const s32 __start___kcrctab_gpl[];
52+
53+struct 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+
78+enum mod_license {
79+ NOT_GPL_ONLY,
80+ GPL_ONLY,
81+};
82+
83+struct 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+
96+int mod_verify_sig(const void *mod, struct load_info *info);
97+int try_to_force_load(struct module *mod, const char *reason);
98+bool find_symbol(struct find_symbol_arg *fsa);
99+struct module *find_module_all(const char *name, size_t len, bool even_unformed);
100+int cmp_name(const void *name, const void *sym);
101+long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
102+ unsigned int section);
103+char *module_flags(struct module *mod, char *buf);
104+size_t module_flags_taint(unsigned long taints, char *buf);
105+
106+static 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+
117+static 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
127+int copy_module_elf(struct module *mod, struct load_info *info);
128+void free_module_elf(struct module *mod);
129+#else /* !CONFIG_LIVEPATCH */
130+static inline int copy_module_elf(struct module *mod, struct load_info *info)
131+{
132+ return 0;
133+}
134+
135+static inline void free_module_elf(struct module *mod) { }
136+#endif /* CONFIG_LIVEPATCH */
137+
138+static 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
149+struct mod_unload_taint {
150+ struct list_head list;
151+ char name[MODULE_NAME_LEN];
152+ unsigned long taints;
153+ u64 count;
154+};
155+
156+int try_add_tainted_module(struct module *mod);
157+void print_unloaded_tainted_modules(void);
158+#else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
159+static inline int try_add_tainted_module(struct module *mod)
160+{
161+ return 0;
162+}
163+
164+static inline void print_unloaded_tainted_modules(void)
165+{
166+}
167+#endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */
168+
169+#ifdef CONFIG_MODULE_DECOMPRESS
170+int module_decompress(struct load_info *info, const void *buf, size_t size);
171+void module_decompress_cleanup(struct load_info *info);
172+#else
173+static inline int module_decompress(struct load_info *info,
174+ const void *buf, size_t size)
175+{
176+ return -EOPNOTSUPP;
177+}
178+
179+static inline void module_decompress_cleanup(struct load_info *info)
180+{
181+}
182+#endif
183+
184+struct 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+
192+extern struct mod_tree_root mod_tree;
193+extern struct mod_tree_root mod_data_tree;
194+
195+#ifdef CONFIG_MODULES_TREE_LOOKUP
196+void mod_tree_insert(struct module *mod);
197+void mod_tree_remove_init(struct module *mod);
198+void mod_tree_remove(struct module *mod);
199+struct module *mod_find(unsigned long addr, struct mod_tree_root *tree);
200+#else /* !CONFIG_MODULES_TREE_LOOKUP */
201+
202+static inline void mod_tree_insert(struct module *mod) { }
203+static inline void mod_tree_remove_init(struct module *mod) { }
204+static inline void mod_tree_remove(struct module *mod) { }
205+static 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+
219+void module_enable_ro(const struct module *mod, bool after_init);
220+void module_enable_nx(const struct module *mod);
221+void module_enable_x(const struct module *mod);
222+int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
223+ char *secstrings, struct module *mod);
224+bool module_check_misalignment(const struct module *mod);
225+
226+#ifdef CONFIG_MODULE_SIG
227+int module_sig_check(struct load_info *info, int flags);
228+#else /* !CONFIG_MODULE_SIG */
229+static 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
236+void kmemleak_load_module(const struct module *mod, const struct load_info *info);
237+#else /* !CONFIG_DEBUG_KMEMLEAK */
238+static 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
243+void init_build_id(struct module *mod, const struct load_info *info);
244+void layout_symtab(struct module *mod, struct load_info *info);
245+void add_kallsyms(struct module *mod, const struct load_info *info);
246+unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
247+
248+static 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 */
253+static inline void init_build_id(struct module *mod, const struct load_info *info) { }
254+static inline void layout_symtab(struct module *mod, struct load_info *info) { }
255+static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
256+#endif /* CONFIG_KALLSYMS */
257+
258+#ifdef CONFIG_SYSFS
259+int mod_sysfs_setup(struct module *mod, const struct load_info *info,
260+ struct kernel_param *kparam, unsigned int num_params);
261+void mod_sysfs_teardown(struct module *mod);
262+void init_param_lock(struct module *mod);
263+#else /* !CONFIG_SYSFS */
264+static 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+
272+static inline void mod_sysfs_teardown(struct module *mod) { }
273+static inline void init_param_lock(struct module *mod) { }
274+#endif /* CONFIG_SYSFS */
275+
276+#ifdef CONFIG_MODVERSIONS
277+int check_version(const struct load_info *info,
278+ const char *symname, struct module *mod, const s32 *crc);
279+void module_layout(struct module *mod, struct modversion_info *ver, struct kernel_param *kp,
280+ struct kernel_symbol *ks, struct tracepoint * const *tp);
281+int check_modstruct_version(const struct load_info *info, struct module *mod);
282+int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
283+#else /* !CONFIG_MODVERSIONS */
284+static 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+
292+static inline int check_modstruct_version(const struct load_info *info,
293+ struct module *mod)
294+{
295+ return 1;
296+}
297+
298+static 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 */
15+static 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+
23+static 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 */
37+static 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+
77+static 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+ */
110+void 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+ */
165+void 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)
213+void 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
227+void 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+ */
236+static 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+
244+static 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+ */
253+static 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+
308+void * __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+ */
318+const 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+
353+int 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+ }
373+out:
374+ preempt_enable();
375+ return -ERANGE;
376+}
377+
378+int 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+ }
401+out:
402+ preempt_enable();
403+ return -ERANGE;
404+}
405+
406+int 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 */
436+unsigned 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. */
452+unsigned 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
476+int 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+ }
508+out:
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+ */
17+int 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+ */
18+int 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+
62+free_sechdrs:
63+ kfree(mod->klp_info->sechdrs);
64+free_info:
65+ kfree(mod->klp_info);
66+ return ret;
67+}
68+
69+void 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
1414 #include <linux/init.h>
1515 #include <linux/kallsyms.h>
1616 #include <linux/buildid.h>
17−#include <linux/file.h>
1817 #include <linux/fs.h>
19−#include <linux/sysfs.h>
2018 #include <linux/kernel.h>
2119 #include <linux/kernel_read_file.h>
2220 #include <linux/slab.h>
2321 #include <linux/vmalloc.h>
2422 #include <linux/elf.h>
25−#include <linux/proc_fs.h>
26−#include <linux/security.h>
2723 #include <linux/seq_file.h>
2824 #include <linux/syscalls.h>
2925 #include <linux/fcntl.h>
@@ -58,236 +54,69 @@ · 28 unmodified lines
5854 #include <linux/dynamic_debug.h>
5955 #include <linux/audit.h>
6056 #include <uapi/linux/module.h>
61−#include "module-internal.h"
57+#include "internal.h"
6258
6359 #define CREATE_TRACE_POINTS
6460 #include <trace/events/module.h>
6561
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−
8462 /*
8563 * Mutex protects:
8664 * 1) List of modules (also safely readable with preempt_disable),
8765 * 2) module_use links,
88− * 3) module_addr_min/module_addr_max.
66+ * 3) mod_tree.addr_min/mod_tree.addr_max.
8967 * (delete and add uses RCU list operations).
9068 */
91−static DEFINE_MUTEX(module_mutex);
92−static LIST_HEAD(modules);
69+DEFINE_MUTEX(module_mutex);
70+LIST_HEAD(modules);
9371
9472 /* Work queue for freeing init sections in success case */
9573 static void do_free_init(struct work_struct *w);
9674 static DECLARE_WORK(init_free_wq, do_free_init);
9775 static LLIST_HEAD(init_free_list);
9876
99−#ifdef CONFIG_MODULES_TREE_LOOKUP
100−
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−
110−static __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−
117−static __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−
124−static __always_inline bool
125−mod_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−
130−static __always_inline int
131−mod_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−
147−static const struct latch_tree_ops mod_tree_ops = {
148− .less = mod_tree_less,
149− .comp = mod_tree_comp,
77+struct mod_tree_root mod_tree __cacheline_aligned = {
78+ .addr_min = -1UL,
15079 };
15180
152−static struct mod_tree_root {
153− struct latch_tree_root root;
154− unsigned long addr_min;
155− unsigned long addr_max;
156−} mod_tree __cacheline_aligned = {
81+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
82+struct mod_tree_root mod_data_tree __cacheline_aligned = {
15783 .addr_min = -1UL,
15884 };
85+#endif
15986
16087 #define module_addr_min mod_tree.addr_min
16188 #define module_addr_max mod_tree.addr_max
16289
163−static noinline void __mod_tree_insert(struct mod_tree_node *node)
164−{
165− latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
166−}
167−
168−static 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− */
177−static 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−
187−static 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−
193−static void mod_tree_remove(struct module *mod)
194−{
195− __mod_tree_remove(&mod->core_layout.mtn);
196− mod_tree_remove_init(mod);
197−}
198−
199−static 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−
212−static unsigned long module_addr_min = -1UL, module_addr_max = 0;
213−
214−static void mod_tree_insert(struct module *mod) { }
215−static void mod_tree_remove_init(struct module *mod) { }
216−static void mod_tree_remove(struct module *mod) { }
217−
218−static 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 */
90+struct symsearch {
91+ const struct kernel_symbol *start, *stop;
92+ const s32 *crcs;
93+ enum mod_license license;
94+};
23295
23396 /*
23497 * Bounds of module text, for speeding up __module_address.
23598 * Protected by module_mutex.
23699 */
237−static void __mod_update_bounds(void *base, unsigned int size)
100+static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree)
238101 {
239102 unsigned long min = (unsigned long)base;
240103 unsigned long max = min + size;
241104
242− if (min < module_addr_min)
243− module_addr_min = min;
244− if (max > module_addr_max)
245− module_addr_max = max;
105+ if (min < tree->addr_min)
106+ tree->addr_min = min;
107+ if (max > tree->addr_max)
108+ tree->addr_max = max;
246109 }
247110
248111 static void mod_update_bounds(struct module *mod)
249112 {
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);
251114 if (mod->init_layout.size)
252− __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
253−}
254−
255−#ifdef CONFIG_KGDB_KDB
256−struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
257−#endif /* CONFIG_KGDB_KDB */
258−
259−static 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
271−static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
272−module_param(sig_enforce, bool_enable_only, 0644);
273−
274−void set_module_sig_enforced(void)
275−{
276− sig_enforce = true;
277−}
278−#else
279−#define sig_enforce false
115+ __mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree);
116+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
117+ __mod_update_bounds(mod->data_layout.base, mod->data_layout.size, &mod_data_tree);
280118 #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− */
286−bool is_module_sig_enforced(void)
287−{
288− return sig_enforce;
289119 }
290−EXPORT_SYMBOL(is_module_sig_enforced);
291120
292121 /* Block module loading/unloading? */
293122 int modules_disabled = 0;
@@ -408,66 +237,12 @@ static __maybe_unused void *any_section_objs(const struct load_info *info, · 114 unmodified lines
408237 return (void *)info->sechdrs[sec].sh_addr;
409238 }
410239
411−/* Provided by the linker */
412−extern const struct kernel_symbol __start___ksymtab[];
413−extern const struct kernel_symbol __stop___ksymtab[];
414−extern const struct kernel_symbol __start___ksymtab_gpl[];
415−extern const struct kernel_symbol __stop___ksymtab_gpl[];
416−extern const s32 __start___kcrctab[];
417−extern const s32 __start___kcrctab_gpl[];
418−
419240 #ifndef CONFIG_MODVERSIONS
420241 #define symversion(base, idx) NULL
421242 #else
422243 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
423244 #endif
424245
425−struct 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−
434−struct 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−
447−static 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−
462−static 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−
471246 static const char *kernel_symbol_name(const struct kernel_symbol *sym)
472247 {
473248 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
@@ -488,33 +263,38 @@ static const char *kernel_symbol_namespace(const struct kernel_symbol *sym) · 14 unmodified lines
488263 #endif
489264 }
490265
491−static int cmp_name(const void *name, const void *sym)
266+int cmp_name(const void *name, const void *sym)
492267 {
493268 return strcmp(name, kernel_symbol_name(sym));
494269 }
495270
496271 static bool find_exported_symbol_in_section(const struct symsearch *syms,
497272 struct module *owner,
498− void *data)
273+ struct find_symbol_arg *fsa)
499274 {
500− struct find_symbol_arg *fsa = data;
501275 struct kernel_symbol *sym;
502276
277+ if (!fsa->gplok && syms->license == GPL_ONLY)
278+ return false;
279+
503280 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
504281 sizeof(struct kernel_symbol), cmp_name);
282+ if (!sym)
283+ return false;
505284
506− if (sym != NULL && check_exported_symbol(syms, owner,
507− sym - syms->start, data))
508− return true;
285+ fsa->owner = owner;
286+ fsa->crc = symversion(syms->crcs, sym - syms->start);
287+ fsa->sym = sym;
288+ fsa->license = syms->license;
509289
510− return false;
290+ return true;
511291 }
512292
513293 /*
514294 * Find an exported symbol and return it, along with, (optional) crc and
515295 * (optional) module which owns it. Needs preempt disabled or module_mutex.
516296 */
517−static bool find_symbol(struct find_symbol_arg *fsa)
297+bool find_symbol(struct find_symbol_arg *fsa)
518298 {
519299 static const struct symsearch arr[] = {
520300 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
@@ -558,8 +338,8 @@ static bool find_symbol(struct find_symbol_arg *fsa) · 37 unmodified lines
558338 * Search for module by name: must hold module_mutex (or preempt disabled
559339 * for read-only access).
560340 */
561−static struct module *find_module_all(const char *name, size_t len,
562− bool even_unformed)
341+struct module *find_module_all(const char *name, size_t len,
342+ bool even_unformed)
563343 {
564344 struct module *mod;
565345
@@ -985,31 +765,6 @@ out: · 419 unmodified lines
985765 return ret;
986766 }
987767
988−static 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−
1013768 void __symbol_put(const char *symbol)
1014769 {
1015770 struct find_symbol_arg fsa = {
@@ -1099,12 +854,6 @@ void module_put(struct module *module) · 83 unmodified lines
1099854 EXPORT_SYMBOL(module_put);
1100855
1101856 #else /* !CONFIG_MODULE_UNLOAD */
1102−static 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−
1108857 static inline void module_unload_free(struct module *mod)
1109858 {
1110859 }
@@ -1120,13 +869,13 @@ static inline int module_unload_init(struct module *mod) · 9 unmodified lines
1120869 }
1121870 #endif /* CONFIG_MODULE_UNLOAD */
1122871
1123−static size_t module_flags_taint(struct module *mod, char *buf)
872+size_t module_flags_taint(unsigned long taints, char *buf)
1124873 {
1125874 size_t l = 0;
1126875 int i;
1127876
1128877 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))
1130879 buf[l++] = taint_flags[i].c_true;
1131880 }
1132881
@@ -1179,6 +928,17 @@ static ssize_t show_coresize(struct module_attribute *mattr, · 46 unmodified lines
1179928 static struct module_attribute modinfo_coresize =
1180929 __ATTR(coresize, 0444, show_coresize, NULL);
1181930
931+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
932+static 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+
938+static struct module_attribute modinfo_datasize =
939+ __ATTR(datasize, 0444, show_datasize, NULL);
940+#endif
941+
1182942 static ssize_t show_initsize(struct module_attribute *mattr,
1183943 struct module_kobject *mk, char *buffer)
1184944 {
@@ -1193,7 +953,7 @@ static ssize_t show_taint(struct module_attribute *mattr, · 8 unmodified lines
1193953 {
1194954 size_t l;
1195955
1196− l = module_flags_taint(mk->mod, buffer);
956+ l = module_flags_taint(mk->mod->taints, buffer);
1197957 buffer[l++] = '\n';
1198958 return l;
1199959 }
@@ -1201,12 +961,15 @@ static ssize_t show_taint(struct module_attribute *mattr, · 1 unmodified line
1201961 static struct module_attribute modinfo_taint =
1202962 __ATTR(taint, 0444, show_taint, NULL);
1203963
1204−static struct module_attribute *modinfo_attrs[] = {
964+struct module_attribute *modinfo_attrs[] = {
1205965 &module_uevent,
1206966 &modinfo_version,
1207967 &modinfo_srcversion,
1208968 &modinfo_initstate,
1209969 &modinfo_coresize,
970+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
971+ &modinfo_datasize,
972+#endif
1210973 &modinfo_initsize,
1211974 &modinfo_taint,
1212975 #ifdef CONFIG_MODULE_UNLOAD
@@ -1215,9 +978,11 @@ static struct module_attribute *modinfo_attrs[] = { · 2 unmodified lines
1215978 NULL,
1216979 };
1217980
981+size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
982+
1218983 static const char vermagic[] = VERMAGIC_STRING;
1219984
1220−static int try_to_force_load(struct module *mod, const char *reason)
985+int try_to_force_load(struct module *mod, const char *reason)
1221986 {
1222987 #ifdef CONFIG_MODULE_FORCE_LOAD
1223988 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
1229994 #endif
1230995 }
1231996
1232−#ifdef CONFIG_MODVERSIONS
1233−
1234−static 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−
1274−bad_version:
1275− pr_warn("%s: disagrees about version of symbol %s\n",
1276− info->name, symname);
1277− return 0;
1278−}
1279−
1280−static 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. */
1302−static 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
1312−static 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−
1320−static inline int check_modstruct_version(const struct load_info *info,
1321− struct module *mod)
1322−{
1323− return 1;
1324−}
1325−
1326−static 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−
1333997 static char *get_modinfo(const struct load_info *info, const char *tag);
1334998 static char *get_next_modinfo(const struct load_info *info, const char *tag,
1335999 char *prev);
@@ -1364,20 +1028,20 @@ static int verify_namespace_is_imported(const struct load_info *info, · 28 unmodified lines
13641028 return 0;
13651029 }
13661030
1367−static bool inherit_taint(struct module *mod, struct module *owner)
1031+static bool inherit_taint(struct module *mod, struct module *owner, const char *name)
13681032 {
13691033 if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
13701034 return true;
13711035
13721036 if (mod->using_gplonly_symbols) {
1373− pr_err("%s: module using GPL-only symbols uses symbols from proprietary module %s.\n",
1374− mod->name, owner->name);
1037+ pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n",
1038+ mod->name, name, owner->name);
13751039 return false;
13761040 }
13771041
13781042 if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
1379− pr_warn("%s: module uses symbols from proprietary module %s, inheriting taint.\n",
1380− mod->name, owner->name);
1043+ pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n",
1044+ mod->name, name, owner->name);
13811045 set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
13821046 }
13831047 return true;
@@ -1409,7 +1073,7 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, · 25 unmodified lines
14091073 if (fsa.license == GPL_ONLY)
14101074 mod->using_gplonly_symbols = true;
14111075
1412− if (!inherit_taint(mod, fsa.owner)) {
1076+ if (!inherit_taint(mod, fsa.owner, name)) {
14131077 fsa.sym = NULL;
14141078 goto getname;
14151079 }
@@ -1457,734 +1121,69 @@ resolve_symbol_wait(struct module *mod, · 41 unmodified lines
14571121 return ksym;
14581122 }
14591123
1460−#ifdef CONFIG_KALLSYMS
1461−static inline bool sect_empty(const Elf_Shdr *sect)
1124+void __weak module_memfree(void *module_region)
14621125 {
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);
14641132 }
1465−#endif
14661133
1467−/*
1468− * /sys/module/foo/sections stuff
1469− * J. Corbet <corbet@lwn.net>
1470− */
1471−#ifdef CONFIG_SYSFS
1134+void __weak module_arch_cleanup(struct module *mod)
1135+{
1136+}
14721137
1473−#ifdef CONFIG_KALLSYMS
1474−struct module_sect_attr {
1475− struct bin_attribute battr;
1476− unsigned long address;
1477−};
1138+void __weak module_arch_freeing_init(struct module *mod)
1139+{
1140+}
14781141
1479−struct module_sect_attrs {
1480− struct attribute_group grp;
1481− unsigned int nsections;
1482− struct module_sect_attr attrs[];
1483−};
1142+static void cfi_cleanup(struct module *mod);
14841143
1485−#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
1486−static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1487− struct bin_attribute *battr,
1488− char *buf, loff_t pos, size_t count)
1144+/* Free a module, remove from lists, etc. */
1145+static void free_module(struct module *mod)
14891146 {
1490− struct module_sect_attr *sattr =
1491− container_of(battr, struct module_sect_attr, battr);
1492− char bounce[MODULE_SECT_READ_SIZE + 1];
1493− size_t wrote;
1147+ trace_module_free(mod);
14941148
1495− if (pos != 0)
1496− return -EINVAL;
1149+ mod_sysfs_teardown(mod);
14971150
14981151 /*
1499− * Since we're a binary read handler, we must account for the
1500− * trailing NUL byte that sprintf will write: if "buf" is
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.
1152+ * We leave it in list to prevent duplicate loads, but make sure
1153+ * that noone uses it while it's being deconstructed.
15051154 */
1506− wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
1507− kallsyms_show_value(file->f_cred)
1508− ? (void *)sattr->address : NULL);
1509− count = min(count, wrote);
1510− memcpy(buf, bounce, count);
1155+ mutex_lock(&module_mutex);
1156+ mod->state = MODULE_STATE_UNFORMED;
1157+ mutex_unlock(&module_mutex);
15111158
1512− return count;
1513−}
1159+ /* Remove dynamic debug info */
1160+ ddebug_remove_module(mod->name);
15141161
1515−static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1516−{
1517− unsigned int section;
1162+ /* Arch-specific cleanup. */
1163+ module_arch_cleanup(mod);
15181164
1519− for (section = 0; section < sect_attrs->nsections; section++)
1520− kfree(sect_attrs->attrs[section].battr.attr.name);
1521− kfree(sect_attrs);
1522−}
1165+ /* Module unload stuff */
1166+ module_unload_free(mod);
15231167
1524−static void add_sect_attrs(struct module *mod, const struct load_info *info)
1525−{
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;
1168+ /* Free any allocated parameters. */
1169+ destroy_params(mod->kp, mod->num_kp);
15301170
1531− /* Count loaded sections and allocate structures */
1532− for (i = 0; i < info->hdr->e_shnum; i++)
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;
1171+ if (is_livepatch_module(mod))
1172+ free_module_elf(mod);
15411173
1542− /* Setup section attributes. */
1543− sect_attrs->grp.name = "sections";
1544− sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
1545−
1546− sect_attrs->nsections = 0;
1547− sattr = &sect_attrs->attrs[0];
1548− gattr = &sect_attrs->grp.bin_attrs[0];
1549− for (i = 0; i < info->hdr->e_shnum; i++) {
1550− Elf_Shdr *sec = &info->sechdrs[i];
1551− if (sect_empty(sec))
1552− continue;
1553− sysfs_bin_attr_init(&sattr->battr);
1554− sattr->address = sec->sh_addr;
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−
1576−static 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−
1594−struct module_notes_attrs {
1595− struct kobject *dir;
1596− unsigned int notes;
1597− struct bin_attribute attrs[];
1598−};
1599−
1600−static 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−
1611−static 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−
1623−static 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−
1681−static 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−
1689−static inline void add_sect_attrs(struct module *mod,
1690− const struct load_info *info)
1691−{
1692−}
1693−
1694−static inline void remove_sect_attrs(struct module *mod)
1695−{
1696−}
1697−
1698−static inline void add_notes_attrs(struct module *mod,
1699− const struct load_info *info)
1700−{
1701−}
1702−
1703−static inline void remove_notes_attrs(struct module *mod)
1704−{
1705−}
1706−#endif /* CONFIG_KALLSYMS */
1707−
1708−static 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−
1720−static 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−
1740−static void module_remove_modinfo_attrs(struct module *mod, int end);
1741−
1742−static 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−
1770−error_out:
1771− if (i > 0)
1772− module_remove_modinfo_attrs(mod, --i);
1773− else
1774− kfree(mod->modinfo_attrs);
1775− return error;
1776−}
1777−
1778−static 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−
1796−static 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−
1804−static 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−
1832−out:
1833− return err;
1834−}
1835−
1836−static 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−
1870−out_unreg_modinfo_attrs:
1871− module_remove_modinfo_attrs(mod, -1);
1872−out_unreg_param:
1873− module_param_sysfs_remove(mod);
1874−out_unreg_holders:
1875− kobject_put(mod->holders_dir);
1876−out_unreg:
1877− mod_kobject_put(mod);
1878−out:
1879− return err;
1880−}
1881−
1882−static 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−
1889−static void init_param_lock(struct module *mod)
1890−{
1891− mutex_init(&mod->param_lock);
1892−}
1893−#else /* !CONFIG_SYSFS */
1894−
1895−static 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−
1903−static void mod_sysfs_fini(struct module *mod)
1904−{
1905−}
1906−
1907−static void module_remove_modinfo_attrs(struct module *mod, int end)
1908−{
1909−}
1910−
1911−static void del_usage_links(struct module *mod)
1912−{
1913−}
1914−
1915−static void init_param_lock(struct module *mod)
1916−{
1917−}
1918−#endif /* CONFIG_SYSFS */
1919−
1920−static 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
1951−static 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−
1960−static 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 */
1966−static void module_enable_x(const struct module *mod) { }
1967−#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
1968−
1969−#ifdef CONFIG_STRICT_MODULE_RWX
1970−static 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−
1980−static 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−
1990−static 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−
2000−static 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−
2017−static 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−
2026−static 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 */
2044−static void module_enable_nx(const struct module *mod) { }
2045−static void module_enable_ro(const struct module *mod, bool after_init) {}
2046−static 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− */
2059−static 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−
2104−free_sechdrs:
2105− kfree(mod->klp_info->sechdrs);
2106−free_info:
2107− kfree(mod->klp_info);
2108− return ret;
2109−}
2110−
2111−static 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 */
2118−static int copy_module_elf(struct module *mod, struct load_info *info)
2119−{
2120− return 0;
2121−}
2122−
2123−static void free_module_elf(struct module *mod)
2124−{
2125−}
2126−#endif /* CONFIG_LIVEPATCH */
2127−
2128−void __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−
2138−void __weak module_arch_cleanup(struct module *mod)
2139−{
2140−}
2141−
2142−void __weak module_arch_freeing_init(struct module *mod)
2143−{
2144−}
2145−
2146−static void cfi_cleanup(struct module *mod);
2147−
2148−/* Free a module, remove from lists, etc. */
2149−static 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);
1174+ /* Now we can delete it from the lists */
1175+ mutex_lock(&module_mutex);
1176+ /* Unlink carefully: kallsyms could be walking list. */
1177+ list_del_rcu(&mod->list);
1178+ mod_tree_remove(mod);
1179+ /* Remove this module from bug list, this uses list_del_rcu */
1180+ module_bug_cleanup(mod);
1181+ /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
1182+ synchronize_rcu();
1183+ if (try_add_tainted_module(mod))
1184+ pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n",
1185+ mod->name);
1186+ mutex_unlock(&module_mutex);
21881187
21891188 /* Clean up CFI for the module. */
21901189 cfi_cleanup(mod);
@@ -2196,10 +1195,13 @@ static void free_module(struct module *mod) · 5 unmodified lines
21961195 percpu_modfree(mod);
21971196
21981197 /* 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);
22001199
22011200 /* Finally, free the core (containing the module structure) */
22021201 module_memfree(mod->core_layout.base);
1202+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
1203+ vfree(mod->data_layout.base);
1204+#endif
22031205 }
22041206
22051207 void *__symbol_get(const char *symbol)
@@ -2387,7 +1389,7 @@ unsigned int __weak arch_mod_section_prepend(struct module *mod, · 181 unmodified lines
23871389 }
23881390
23891391 /* Update size with this section: return offset. */
2390−static long get_offset(struct module *mod, unsigned int *size,
1392+long module_get_offset(struct module *mod, unsigned int *size,
23911393 Elf_Shdr *sechdr, unsigned int section)
23921394 {
23931395 long ret;
@@ -2437,30 +1439,32 @@ static void layout_sections(struct module *mod, struct load_info *info) · 43 unmodified lines
24371439 for (i = 0; i < info->hdr->e_shnum; ++i) {
24381440 Elf_Shdr *s = &info->sechdrs[i];
24391441 const char *sname = info->secstrings + s->sh_name;
1442+ unsigned int *sizep;
24401443
24411444 if ((s->sh_flags & masks[m][0]) != masks[m][0]
24421445 || (s->sh_flags & masks[m][1])
24431446 || s->sh_entsize != ~0UL
24441447 || module_init_layout_section(sname))
24451448 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);
24471451 pr_debug("\t%s\n", sname);
24481452 }
24491453 switch (m) {
24501454 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);
24521456 mod->core_layout.text_size = mod->core_layout.size;
24531457 break;
24541458 case 1: /* RO: text and ro-data */
2455− mod->core_layout.size = debug_align(mod->core_layout.size);
2456− mod->core_layout.ro_size = mod->core_layout.size;
1459+ mod->data_layout.size = strict_align(mod->data_layout.size);
1460+ mod->data_layout.ro_size = mod->data_layout.size;
24571461 break;
24581462 case 2: /* RO after init */
2459− mod->core_layout.size = debug_align(mod->core_layout.size);
2460− mod->core_layout.ro_after_init_size = mod->core_layout.size;
1463+ mod->data_layout.size = strict_align(mod->data_layout.size);
1464+ mod->data_layout.ro_after_init_size = mod->data_layout.size;
24611465 break;
24621466 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);
24641468 break;
24651469 }
24661470 }
@@ -2476,17 +1480,17 @@ static void layout_sections(struct module *mod, struct load_info *info) · 9 unmodified lines
24761480 || s->sh_entsize != ~0UL
24771481 || !module_init_layout_section(sname))
24781482 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)
24801484 | INIT_OFFSET_MASK);
24811485 pr_debug("\t%s\n", sname);
24821486 }
24831487 switch (m) {
24841488 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);
24861490 mod->init_layout.text_size = mod->init_layout.size;
24871491 break;
24881492 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);
24901494 mod->init_layout.ro_size = mod->init_layout.size;
24911495 break;
24921496 case 2:
@@ -2497,7 +1501,7 @@ static void layout_sections(struct module *mod, struct load_info *info) · 4 unmodified lines
24971501 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
24981502 break;
24991503 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);
25011505 break;
25021506 }
25031507 }
@@ -2587,350 +1591,37 @@ static void free_modinfo(struct module *mod) · 83 unmodified lines
25871591 if (attr->free)
25881592 attr->free(mod);
25891593 }
2590−}
2591−
2592−#ifdef CONFIG_KALLSYMS
2593−
2594−/* Lookup exported symbol in given range of kernel_symbols */
2595−static 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−
2603−static 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 */
2616−static 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−
2656−static 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− */
2689−static 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− */
2744−static 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
2785−static inline void layout_symtab(struct module *mod, struct load_info *info)
2786−{
2787−}
2788−
2789−static 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)
2795−static 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
2809−static void init_build_id(struct module *mod, const struct load_info *info)
2810−{
2811−}
2812−#endif
2813−
2814−static 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−
2821−static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2822−{
2823− if (debug)
2824− ddebug_remove_module(mod->name);
2825−}
2826−
2827−void * __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−
2834−bool __weak module_init_section(const char *name)
2835−{
2836− return strstarts(name, ".init");
2837−}
2838−
2839−bool __weak module_exit_section(const char *name)
2840−{
2841− return strstarts(name, ".exit");
2842−}
2843−
2844−#ifdef CONFIG_DEBUG_KMEMLEAK
2845−static 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
2865−static 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
2872−static 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− }
1594+}
28951595
2896− /*
2897− * We don't permit modules to be loaded into the trusted kernels
2898− * without a valid signature on them, but if we're not enforcing,
2899− * certain errors are non-fatal.
2900− */
2901− switch (err) {
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;
1596+static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
1597+{
1598+ if (!debug)
1599+ return;
1600+ ddebug_add_module(debug, num, mod->name);
1601+}
29111602
2912− default:
2913− /*
2914− * All other errors are fatal, including lack of memory,
2915− * unparseable signatures, and signature check failures --
2916− * even if signatures aren't required.
2917− */
2918− return err;
2919− }
1603+static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
1604+{
1605+ if (debug)
1606+ ddebug_remove_module(mod->name);
1607+}
29201608
2921− if (is_module_sig_enforced()) {
2922− pr_notice("Loading of %s is rejected\n", reason);
2923− return -EKEYREJECTED;
2924− }
1609+void * __weak module_alloc(unsigned long size)
1610+{
1611+ return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
1612+ GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
1613+ NUMA_NO_NODE, __builtin_return_address(0));
1614+}
29251615
2926− return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);
1616+bool __weak module_init_section(const char *name)
1617+{
1618+ return strstarts(name, ".init");
29271619 }
2928−#else /* !CONFIG_MODULE_SIG */
2929−static int module_sig_check(struct load_info *info, int flags)
1620+
1621+bool __weak module_exit_section(const char *name)
29301622 {
2931− return 0;
1623+ return strstarts(name, ".exit");
29321624 }
2933−#endif /* !CONFIG_MODULE_SIG */
29341625
29351626 static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
29361627 {
@@ -3025,6 +1716,10 @@ static int elf_validity_check(struct load_info *info) · 88 unmodified lines
30251716 * strings in the section safe.
30261717 */
30271718 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+ }
30281723 if (info->secstrings[strhdr->sh_size - 1] != '\0') {
30291724 pr_err("ELF Spec violation: section name table isn't null terminated\n");
30301725 goto no_exec;
@@ -3099,30 +1794,23 @@ static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned l · 68 unmodified lines
30991794 return 0;
31001795 }
31011796
3102−#ifdef CONFIG_LIVEPATCH
31031797 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
31041798 {
3105− if (get_modinfo(info, "livepatch")) {
3106− mod->klp = true;
1799+ if (!get_modinfo(info, "livepatch"))
1800+ /* Nothing more to do */
1801+ return 0;
1802+
1803+ if (set_livepatch_module(mod)) {
31071804 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
31081805 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
3109− mod->name);
3110− }
3111−
3112− return 0;
3113−}
3114−#else /* !CONFIG_LIVEPATCH */
3115−static 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;
1806+ mod->name);
1807+ return 0;
31211808 }
31221809
3123− return 0;
1810+ pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
1811+ mod->name);
1812+ return -ENOEXEC;
31241813 }
3125−#endif /* CONFIG_LIVEPATCH */
31261814
31271815 static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
31281816 {
@@ -3448,6 +2136,24 @@ static int move_module(struct module *mod, struct load_info *info) · 319 unmodified lines
34482136 } else
34492137 mod->init_layout.base = NULL;
34502138
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
34512157 /* Transfer each section which specifies SHF_ALLOC */
34522158 pr_debug("final section addresses:\n");
34532159 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
34602166 if (shdr->sh_entsize & INIT_OFFSET_MASK)
34612167 dest = mod->init_layout.base
34622168 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2169+ else if (!(shdr->sh_flags & SHF_EXECINSTR))
2170+ dest = mod->data_layout.base + shdr->sh_entsize;
34632171 else
34642172 dest = mod->core_layout.base + shdr->sh_entsize;
34652173
@@ -3621,6 +2329,9 @@ static void module_deallocate(struct module *mod, struct load_info *info) · 155 unmodified lines
36212329 module_arch_freeing_init(mod);
36222330 module_memfree(mod->init_layout.base);
36232331 module_memfree(mod->core_layout.base);
2332+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
2333+ vfree(mod->data_layout.base);
2334+#endif
36242335 }
36252336
36262337 int __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
38712582 /* This relies on module_mutex for list integrity. */
38722583 module_bug_finalize(info->hdr, info->sechdrs, mod);
38732584
2585+ if (module_check_misalignment(mod))
2586+ goto out_misaligned;
2587+
38742588 module_enable_ro(mod, false);
38752589 module_enable_nx(mod);
38762590 module_enable_x(mod);
@@ -3884,6 +2598,8 @@ static int complete_formation(struct module *mod, struct load_info *info) · 7 unmodified lines
38842598
38852599 return 0;
38862600
2601+out_misaligned:
2602+ err = -EINVAL;
38872603 out:
38882604 mutex_unlock(&module_mutex);
38892605 return err;
@@ -4150,7 +2866,7 @@ static int load_module(struct load_info *info, const char __user *uargs, · 260 unmodified lines
41502866 mutex_unlock(&module_mutex);
41512867 free_module:
41522868 /* 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);
41542870
41552871 module_deallocate(mod, info);
41562872 free_copy:
@@ -4219,287 +2935,6 @@ static inline int within(unsigned long addr, void *start, unsigned long size) · 62 unmodified lines
42192935 return ((void *)addr >= start && (void *)addr < start + size);
42202936 }
42212937
4222−#ifdef CONFIG_KALLSYMS
4223−/*
4224− * This ignores the intensely annoying "mapping symbols" found
4225− * in ARM ELF files: $a, $t and $d.
4226− */
4227−static 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−
4235−static 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− */
4244−static 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−
4299−void * __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− */
4309−const 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−
4344−int 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− }
4364−out:
4365− preempt_enable();
4366− return -ERANGE;
4367−}
4368−
4369−int 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− }
4392−out:
4393− preempt_enable();
4394− return -ERANGE;
4395−}
4396−
4397−int 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 */
4427−static 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. */
4443−unsigned 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
4467−int 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− }
4496−out:
4497− mutex_unlock(&module_mutex);
4498− return ret;
4499−}
4500−#endif /* CONFIG_LIVEPATCH */
4501−#endif /* CONFIG_KALLSYMS */
4502−
45032938 static void cfi_init(struct module *mod)
45042939 {
45052940 #ifdef CONFIG_CFI_CLANG
@@ -4523,22 +2958,19 @@ static void cfi_init(struct module *mod) · 17 unmodified lines
45232958 mod->exit = *exit;
45242959 #endif
45252960
4526− cfi_module_add(mod, module_addr_min);
2961+ cfi_module_add(mod, mod_tree.addr_min);
45272962 #endif
45282963 }
45292964
45302965 static void cfi_cleanup(struct module *mod)
45312966 {
45322967 #ifdef CONFIG_CFI_CLANG
4533− cfi_module_remove(mod, module_addr_min);
2968+ cfi_module_remove(mod, mod_tree.addr_min);
45342969 #endif
45352970 }
45362971
4537−/* Maximum number of characters written by module_flags() */
4538−#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4539−
45402972 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4541−static char *module_flags(struct module *mod, char *buf)
2973+char *module_flags(struct module *mod, char *buf)
45422974 {
45432975 int bx = 0;
45442976
@@ -4547,7 +2979,7 @@ static char *module_flags(struct module *mod, char *buf) · 2 unmodified lines
45472979 mod->state == MODULE_STATE_GOING ||
45482980 mod->state == MODULE_STATE_COMING) {
45492981 buf[bx++] = '(';
4550− bx += module_flags_taint(mod, buf + bx);
2982+ bx += module_flags_taint(mod->taints, buf + bx);
45512983 /* Show a - for module-is-being-unloaded */
45522984 if (mod->state == MODULE_STATE_GOING)
45532985 buf[bx++] = '-';
@@ -4561,103 +2993,6 @@ static char *module_flags(struct module *mod, char *buf) · 7 unmodified lines
45612993 return buf;
45622994 }
45632995
4564−#ifdef CONFIG_PROC_FS
4565−/* Called by the /proc file system to return a list of modules. */
4566−static 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−
4572−static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4573−{
4574− return seq_list_next(p, &modules, pos);
4575−}
4576−
4577−static void m_stop(struct seq_file *m, void *p)
4578−{
4579− mutex_unlock(&module_mutex);
4580−}
4581−
4582−static 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− */
4619−static 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− */
4633−static 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−
4645−static 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−
4653−static int __init proc_modules_init(void)
4654−{
4655− proc_create("modules", 0, NULL, &modules_proc_ops);
4656− return 0;
4657−}
4658−module_init(proc_modules_init);
4659−#endif
4660−
46612996 /* Given an address, look for it in the module exception tables. */
46622997 const struct exception_table_entry *search_module_extables(unsigned long addr)
46632998 {
@@ -4713,13 +3048,20 @@ bool is_module_address(unsigned long addr) · 49 unmodified lines
47133048 struct module *__module_address(unsigned long addr)
47143049 {
47153050 struct module *mod;
3051+ struct mod_tree_root *tree;
47163052
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
47183060 return NULL;
47193061
47203062 module_assert_mutex_or_preempt();
47213063
4722− mod = mod_find(addr);
3064+ mod = mod_find(addr, tree);
47233065 if (mod) {
47243066 BUG_ON(!within_module(addr, mod));
47253067 if (mod->state == MODULE_STATE_UNFORMED)
@@ -4780,23 +3122,10 @@ void print_modules(void) · 54 unmodified lines
47803122 continue;
47813123 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
47823124 }
3125+
3126+ print_unloaded_tainted_modules();
47833127 preempt_enable();
47843128 if (last_unloaded_module[0])
47853129 pr_cont(" [last unloaded: %s]", last_unloaded_module);
47863130 pr_cont("\n");
47873131 }
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− */
4794−void 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−}
4801−EXPORT_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
16+static 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 */
41+static 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. */
49+static 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+
55+static void *m_next(struct seq_file *m, void *p, loff_t *pos)
56+{
57+ return seq_list_next(p, &modules, pos);
58+}
59+
60+static void m_stop(struct seq_file *m, void *p)
61+{
62+ mutex_unlock(&module_mutex);
63+}
64+
65+static 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+ */
106+static 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+ */
120+static 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+
133+static 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+
141+static int __init proc_modules_init(void)
142+{
143+ proc_create("modules", 0, NULL, &modules_proc_ops);
144+ return 0;
145+}
146+module_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+
19+static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
20+module_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+ */
26+bool is_module_sig_enforced(void)
27+{
28+ return sig_enforce;
29+}
30+EXPORT_SYMBOL(is_module_sig_enforced);
31+
32+void set_module_sig_enforced(void)
33+{
34+ sig_enforce = true;
35+}
36+
37+/*
38+ * Verify the signature on a module.
39+ */
40+int 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+
67+int 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+ */
35+static 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+
42+static 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+
49+static 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+
56+static 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+
63+static 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+
72+bool 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+
82+void 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+
92+void 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+
113+void 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+
125+int 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
22+struct module_sect_attr {
23+ struct bin_attribute battr;
24+ unsigned long address;
25+};
26+
27+struct 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))
34+static 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+
63+static 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+
72+static 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;
121+out:
122+ free_sect_attrs(sect_attrs);
123+}
124+
125+static 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+
143+struct module_notes_attrs {
144+ struct kobject *dir;
145+ unsigned int notes;
146+ struct bin_attribute attrs[];
147+};
148+
149+static 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+
160+static 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+
172+static 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+
226+out:
227+ free_notes_attrs(notes_attrs, i);
228+}
229+
230+static 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 */
237+static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
238+static inline void remove_sect_attrs(struct module *mod) { }
239+static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
240+static inline void remove_notes_attrs(struct module *mod) { }
241+#endif /* CONFIG_KALLSYMS */
242+
243+static 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+
255+static 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+
275+static 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+
293+static 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+
321+error_out:
322+ if (i > 0)
323+ module_remove_modinfo_attrs(mod, --i);
324+ else
325+ kfree(mod->modinfo_attrs);
326+ return error;
327+}
328+
329+static 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+
338+static 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+
366+out:
367+ return err;
368+}
369+
370+int 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+
404+out_unreg_modinfo_attrs:
405+ module_remove_modinfo_attrs(mod, -1);
406+out_unreg_param:
407+ module_param_sysfs_remove(mod);
408+out_unreg_holders:
409+ kobject_put(mod->holders_dir);
410+out_unreg:
411+ mod_kobject_put(mod);
412+out:
413+ return err;
414+}
415+
416+static 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+
423+void 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+
433+void 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+
16+static LIST_HEAD(unloaded_tainted_modules);
17+
18+int 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;
40+out:
41+ return 0;
42+}
43+
44+void 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+
22+static __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+
29+static __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+
36+static __always_inline bool
37+mod_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+
42+static __always_inline int
43+mod_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+
59+static const struct latch_tree_ops mod_tree_ops = {
60+ .less = mod_tree_less,
61+ .comp = mod_tree_comp,
62+};
63+
64+static 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+
69+static 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+ */
78+void 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+
93+void 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+
99+void 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+
108+struct 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+
13+int 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+
53+bad_version:
54+ pr_warn("%s: disagrees about version of symbol %s\n", info->name, symname);
55+ return 0;
56+}
57+
58+int 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. */
80+int 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+ */
94+void 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+}
101+EXPORT_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− */
20−int 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−}