s390/zcrypt: fix kmalloc 256k failure
[linux/fpc-iii.git] / kernel / module.c
blob819c5d3b4c295343bd0de5b0f21821976d4f9fef
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Copyright (C) 2002 Richard Henderson
4 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
6 */
7 #include <linux/export.h>
8 #include <linux/extable.h>
9 #include <linux/moduleloader.h>
10 #include <linux/module_signature.h>
11 #include <linux/trace_events.h>
12 #include <linux/init.h>
13 #include <linux/kallsyms.h>
14 #include <linux/file.h>
15 #include <linux/fs.h>
16 #include <linux/sysfs.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/elf.h>
21 #include <linux/proc_fs.h>
22 #include <linux/security.h>
23 #include <linux/seq_file.h>
24 #include <linux/syscalls.h>
25 #include <linux/fcntl.h>
26 #include <linux/rcupdate.h>
27 #include <linux/capability.h>
28 #include <linux/cpu.h>
29 #include <linux/moduleparam.h>
30 #include <linux/errno.h>
31 #include <linux/err.h>
32 #include <linux/vermagic.h>
33 #include <linux/notifier.h>
34 #include <linux/sched.h>
35 #include <linux/device.h>
36 #include <linux/string.h>
37 #include <linux/mutex.h>
38 #include <linux/rculist.h>
39 #include <linux/uaccess.h>
40 #include <asm/cacheflush.h>
41 #include <linux/set_memory.h>
42 #include <asm/mmu_context.h>
43 #include <linux/license.h>
44 #include <asm/sections.h>
45 #include <linux/tracepoint.h>
46 #include <linux/ftrace.h>
47 #include <linux/livepatch.h>
48 #include <linux/async.h>
49 #include <linux/percpu.h>
50 #include <linux/kmemleak.h>
51 #include <linux/jump_label.h>
52 #include <linux/pfn.h>
53 #include <linux/bsearch.h>
54 #include <linux/dynamic_debug.h>
55 #include <linux/audit.h>
56 #include <uapi/linux/module.h>
57 #include "module-internal.h"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/module.h>
62 #ifndef ARCH_SHF_SMALL
63 #define ARCH_SHF_SMALL 0
64 #endif
67 * Modules' sections will be aligned on page boundaries
68 * to ensure complete separation of code and data, but
69 * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
71 #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
72 # define debug_align(X) ALIGN(X, PAGE_SIZE)
73 #else
74 # define debug_align(X) (X)
75 #endif
77 /* If this is set, the section belongs in the init part of the module */
78 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
81 * Mutex protects:
82 * 1) List of modules (also safely readable with preempt_disable),
83 * 2) module_use links,
84 * 3) module_addr_min/module_addr_max.
85 * (delete and add uses RCU list operations). */
86 DEFINE_MUTEX(module_mutex);
87 EXPORT_SYMBOL_GPL(module_mutex);
88 static LIST_HEAD(modules);
90 /* Work queue for freeing init sections in success case */
91 static struct work_struct init_free_wq;
92 static struct llist_head init_free_list;
94 #ifdef CONFIG_MODULES_TREE_LOOKUP
97 * Use a latched RB-tree for __module_address(); this allows us to use
98 * RCU-sched lookups of the address from any context.
100 * This is conditional on PERF_EVENTS || TRACING because those can really hit
101 * __module_address() hard by doing a lot of stack unwinding; potentially from
102 * NMI context.
105 static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
107 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
109 return (unsigned long)layout->base;
112 static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
114 struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
116 return (unsigned long)layout->size;
119 static __always_inline bool
120 mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
122 return __mod_tree_val(a) < __mod_tree_val(b);
125 static __always_inline int
126 mod_tree_comp(void *key, struct latch_tree_node *n)
128 unsigned long val = (unsigned long)key;
129 unsigned long start, end;
131 start = __mod_tree_val(n);
132 if (val < start)
133 return -1;
135 end = start + __mod_tree_size(n);
136 if (val >= end)
137 return 1;
139 return 0;
142 static const struct latch_tree_ops mod_tree_ops = {
143 .less = mod_tree_less,
144 .comp = mod_tree_comp,
147 static struct mod_tree_root {
148 struct latch_tree_root root;
149 unsigned long addr_min;
150 unsigned long addr_max;
151 } mod_tree __cacheline_aligned = {
152 .addr_min = -1UL,
155 #define module_addr_min mod_tree.addr_min
156 #define module_addr_max mod_tree.addr_max
158 static noinline void __mod_tree_insert(struct mod_tree_node *node)
160 latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
163 static void __mod_tree_remove(struct mod_tree_node *node)
165 latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
169 * These modifications: insert, remove_init and remove; are serialized by the
170 * module_mutex.
172 static void mod_tree_insert(struct module *mod)
174 mod->core_layout.mtn.mod = mod;
175 mod->init_layout.mtn.mod = mod;
177 __mod_tree_insert(&mod->core_layout.mtn);
178 if (mod->init_layout.size)
179 __mod_tree_insert(&mod->init_layout.mtn);
182 static void mod_tree_remove_init(struct module *mod)
184 if (mod->init_layout.size)
185 __mod_tree_remove(&mod->init_layout.mtn);
188 static void mod_tree_remove(struct module *mod)
190 __mod_tree_remove(&mod->core_layout.mtn);
191 mod_tree_remove_init(mod);
194 static struct module *mod_find(unsigned long addr)
196 struct latch_tree_node *ltn;
198 ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
199 if (!ltn)
200 return NULL;
202 return container_of(ltn, struct mod_tree_node, node)->mod;
205 #else /* MODULES_TREE_LOOKUP */
207 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
209 static void mod_tree_insert(struct module *mod) { }
210 static void mod_tree_remove_init(struct module *mod) { }
211 static void mod_tree_remove(struct module *mod) { }
213 static struct module *mod_find(unsigned long addr)
215 struct module *mod;
217 list_for_each_entry_rcu(mod, &modules, list,
218 lockdep_is_held(&module_mutex)) {
219 if (within_module(addr, mod))
220 return mod;
223 return NULL;
226 #endif /* MODULES_TREE_LOOKUP */
229 * Bounds of module text, for speeding up __module_address.
230 * Protected by module_mutex.
232 static void __mod_update_bounds(void *base, unsigned int size)
234 unsigned long min = (unsigned long)base;
235 unsigned long max = min + size;
237 if (min < module_addr_min)
238 module_addr_min = min;
239 if (max > module_addr_max)
240 module_addr_max = max;
243 static void mod_update_bounds(struct module *mod)
245 __mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
246 if (mod->init_layout.size)
247 __mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
250 #ifdef CONFIG_KGDB_KDB
251 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
252 #endif /* CONFIG_KGDB_KDB */
254 static void module_assert_mutex(void)
256 lockdep_assert_held(&module_mutex);
259 static void module_assert_mutex_or_preempt(void)
261 #ifdef CONFIG_LOCKDEP
262 if (unlikely(!debug_locks))
263 return;
265 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
266 !lockdep_is_held(&module_mutex));
267 #endif
270 static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
271 module_param(sig_enforce, bool_enable_only, 0644);
274 * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
275 * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
277 bool is_module_sig_enforced(void)
279 return sig_enforce;
281 EXPORT_SYMBOL(is_module_sig_enforced);
283 void set_module_sig_enforced(void)
285 sig_enforce = true;
288 /* Block module loading/unloading? */
289 int modules_disabled = 0;
290 core_param(nomodule, modules_disabled, bint, 0);
292 /* Waiting for a module to finish initializing? */
293 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
295 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
297 int register_module_notifier(struct notifier_block *nb)
299 return blocking_notifier_chain_register(&module_notify_list, nb);
301 EXPORT_SYMBOL(register_module_notifier);
303 int unregister_module_notifier(struct notifier_block *nb)
305 return blocking_notifier_chain_unregister(&module_notify_list, nb);
307 EXPORT_SYMBOL(unregister_module_notifier);
310 * We require a truly strong try_module_get(): 0 means success.
311 * Otherwise an error is returned due to ongoing or failed
312 * initialization etc.
314 static inline int strong_try_module_get(struct module *mod)
316 BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
317 if (mod && mod->state == MODULE_STATE_COMING)
318 return -EBUSY;
319 if (try_module_get(mod))
320 return 0;
321 else
322 return -ENOENT;
325 static inline void add_taint_module(struct module *mod, unsigned flag,
326 enum lockdep_ok lockdep_ok)
328 add_taint(flag, lockdep_ok);
329 set_bit(flag, &mod->taints);
333 * A thread that wants to hold a reference to a module only while it
334 * is running can call this to safely exit. nfsd and lockd use this.
336 void __noreturn __module_put_and_exit(struct module *mod, long code)
338 module_put(mod);
339 do_exit(code);
341 EXPORT_SYMBOL(__module_put_and_exit);
343 /* Find a module section: 0 means not found. */
344 static unsigned int find_sec(const struct load_info *info, const char *name)
346 unsigned int i;
348 for (i = 1; i < info->hdr->e_shnum; i++) {
349 Elf_Shdr *shdr = &info->sechdrs[i];
350 /* Alloc bit cleared means "ignore it." */
351 if ((shdr->sh_flags & SHF_ALLOC)
352 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
353 return i;
355 return 0;
358 /* Find a module section, or NULL. */
359 static void *section_addr(const struct load_info *info, const char *name)
361 /* Section 0 has sh_addr 0. */
362 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
365 /* Find a module section, or NULL. Fill in number of "objects" in section. */
366 static void *section_objs(const struct load_info *info,
367 const char *name,
368 size_t object_size,
369 unsigned int *num)
371 unsigned int sec = find_sec(info, name);
373 /* Section 0 has sh_addr 0 and sh_size 0. */
374 *num = info->sechdrs[sec].sh_size / object_size;
375 return (void *)info->sechdrs[sec].sh_addr;
378 /* Provided by the linker */
379 extern const struct kernel_symbol __start___ksymtab[];
380 extern const struct kernel_symbol __stop___ksymtab[];
381 extern const struct kernel_symbol __start___ksymtab_gpl[];
382 extern const struct kernel_symbol __stop___ksymtab_gpl[];
383 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
384 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
385 extern const s32 __start___kcrctab[];
386 extern const s32 __start___kcrctab_gpl[];
387 extern const s32 __start___kcrctab_gpl_future[];
388 #ifdef CONFIG_UNUSED_SYMBOLS
389 extern const struct kernel_symbol __start___ksymtab_unused[];
390 extern const struct kernel_symbol __stop___ksymtab_unused[];
391 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
392 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
393 extern const s32 __start___kcrctab_unused[];
394 extern const s32 __start___kcrctab_unused_gpl[];
395 #endif
397 #ifndef CONFIG_MODVERSIONS
398 #define symversion(base, idx) NULL
399 #else
400 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
401 #endif
403 static bool each_symbol_in_section(const struct symsearch *arr,
404 unsigned int arrsize,
405 struct module *owner,
406 bool (*fn)(const struct symsearch *syms,
407 struct module *owner,
408 void *data),
409 void *data)
411 unsigned int j;
413 for (j = 0; j < arrsize; j++) {
414 if (fn(&arr[j], owner, data))
415 return true;
418 return false;
421 /* Returns true as soon as fn returns true, otherwise false. */
422 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
423 struct module *owner,
424 void *data),
425 void *data)
427 struct module *mod;
428 static const struct symsearch arr[] = {
429 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
430 NOT_GPL_ONLY, false },
431 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
432 __start___kcrctab_gpl,
433 GPL_ONLY, false },
434 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
435 __start___kcrctab_gpl_future,
436 WILL_BE_GPL_ONLY, false },
437 #ifdef CONFIG_UNUSED_SYMBOLS
438 { __start___ksymtab_unused, __stop___ksymtab_unused,
439 __start___kcrctab_unused,
440 NOT_GPL_ONLY, true },
441 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
442 __start___kcrctab_unused_gpl,
443 GPL_ONLY, true },
444 #endif
447 module_assert_mutex_or_preempt();
449 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
450 return true;
452 list_for_each_entry_rcu(mod, &modules, list,
453 lockdep_is_held(&module_mutex)) {
454 struct symsearch arr[] = {
455 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
456 NOT_GPL_ONLY, false },
457 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
458 mod->gpl_crcs,
459 GPL_ONLY, false },
460 { mod->gpl_future_syms,
461 mod->gpl_future_syms + mod->num_gpl_future_syms,
462 mod->gpl_future_crcs,
463 WILL_BE_GPL_ONLY, false },
464 #ifdef CONFIG_UNUSED_SYMBOLS
465 { mod->unused_syms,
466 mod->unused_syms + mod->num_unused_syms,
467 mod->unused_crcs,
468 NOT_GPL_ONLY, true },
469 { mod->unused_gpl_syms,
470 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
471 mod->unused_gpl_crcs,
472 GPL_ONLY, true },
473 #endif
476 if (mod->state == MODULE_STATE_UNFORMED)
477 continue;
479 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
480 return true;
482 return false;
484 EXPORT_SYMBOL_GPL(each_symbol_section);
486 struct find_symbol_arg {
487 /* Input */
488 const char *name;
489 bool gplok;
490 bool warn;
492 /* Output */
493 struct module *owner;
494 const s32 *crc;
495 const struct kernel_symbol *sym;
498 static bool check_exported_symbol(const struct symsearch *syms,
499 struct module *owner,
500 unsigned int symnum, void *data)
502 struct find_symbol_arg *fsa = data;
504 if (!fsa->gplok) {
505 if (syms->licence == GPL_ONLY)
506 return false;
507 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
508 pr_warn("Symbol %s is being used by a non-GPL module, "
509 "which will not be allowed in the future\n",
510 fsa->name);
514 #ifdef CONFIG_UNUSED_SYMBOLS
515 if (syms->unused && fsa->warn) {
516 pr_warn("Symbol %s is marked as UNUSED, however this module is "
517 "using it.\n", fsa->name);
518 pr_warn("This symbol will go away in the future.\n");
519 pr_warn("Please evaluate if this is the right api to use and "
520 "if it really is, submit a report to the linux kernel "
521 "mailing list together with submitting your code for "
522 "inclusion.\n");
524 #endif
526 fsa->owner = owner;
527 fsa->crc = symversion(syms->crcs, symnum);
528 fsa->sym = &syms->start[symnum];
529 return true;
532 static unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
534 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
535 return (unsigned long)offset_to_ptr(&sym->value_offset);
536 #else
537 return sym->value;
538 #endif
541 static const char *kernel_symbol_name(const struct kernel_symbol *sym)
543 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
544 return offset_to_ptr(&sym->name_offset);
545 #else
546 return sym->name;
547 #endif
550 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
552 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
553 if (!sym->namespace_offset)
554 return NULL;
555 return offset_to_ptr(&sym->namespace_offset);
556 #else
557 return sym->namespace;
558 #endif
561 static int cmp_name(const void *name, const void *sym)
563 return strcmp(name, kernel_symbol_name(sym));
566 static bool find_exported_symbol_in_section(const struct symsearch *syms,
567 struct module *owner,
568 void *data)
570 struct find_symbol_arg *fsa = data;
571 struct kernel_symbol *sym;
573 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
574 sizeof(struct kernel_symbol), cmp_name);
576 if (sym != NULL && check_exported_symbol(syms, owner,
577 sym - syms->start, data))
578 return true;
580 return false;
583 /* Find an exported symbol and return it, along with, (optional) crc and
584 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
585 const struct kernel_symbol *find_symbol(const char *name,
586 struct module **owner,
587 const s32 **crc,
588 bool gplok,
589 bool warn)
591 struct find_symbol_arg fsa;
593 fsa.name = name;
594 fsa.gplok = gplok;
595 fsa.warn = warn;
597 if (each_symbol_section(find_exported_symbol_in_section, &fsa)) {
598 if (owner)
599 *owner = fsa.owner;
600 if (crc)
601 *crc = fsa.crc;
602 return fsa.sym;
605 pr_debug("Failed to find symbol %s\n", name);
606 return NULL;
608 EXPORT_SYMBOL_GPL(find_symbol);
611 * Search for module by name: must hold module_mutex (or preempt disabled
612 * for read-only access).
614 static struct module *find_module_all(const char *name, size_t len,
615 bool even_unformed)
617 struct module *mod;
619 module_assert_mutex_or_preempt();
621 list_for_each_entry_rcu(mod, &modules, list,
622 lockdep_is_held(&module_mutex)) {
623 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
624 continue;
625 if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
626 return mod;
628 return NULL;
631 struct module *find_module(const char *name)
633 module_assert_mutex();
634 return find_module_all(name, strlen(name), false);
636 EXPORT_SYMBOL_GPL(find_module);
638 #ifdef CONFIG_SMP
640 static inline void __percpu *mod_percpu(struct module *mod)
642 return mod->percpu;
645 static int percpu_modalloc(struct module *mod, struct load_info *info)
647 Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
648 unsigned long align = pcpusec->sh_addralign;
650 if (!pcpusec->sh_size)
651 return 0;
653 if (align > PAGE_SIZE) {
654 pr_warn("%s: per-cpu alignment %li > %li\n",
655 mod->name, align, PAGE_SIZE);
656 align = PAGE_SIZE;
659 mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
660 if (!mod->percpu) {
661 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
662 mod->name, (unsigned long)pcpusec->sh_size);
663 return -ENOMEM;
665 mod->percpu_size = pcpusec->sh_size;
666 return 0;
669 static void percpu_modfree(struct module *mod)
671 free_percpu(mod->percpu);
674 static unsigned int find_pcpusec(struct load_info *info)
676 return find_sec(info, ".data..percpu");
679 static void percpu_modcopy(struct module *mod,
680 const void *from, unsigned long size)
682 int cpu;
684 for_each_possible_cpu(cpu)
685 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
688 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
690 struct module *mod;
691 unsigned int cpu;
693 preempt_disable();
695 list_for_each_entry_rcu(mod, &modules, list) {
696 if (mod->state == MODULE_STATE_UNFORMED)
697 continue;
698 if (!mod->percpu_size)
699 continue;
700 for_each_possible_cpu(cpu) {
701 void *start = per_cpu_ptr(mod->percpu, cpu);
702 void *va = (void *)addr;
704 if (va >= start && va < start + mod->percpu_size) {
705 if (can_addr) {
706 *can_addr = (unsigned long) (va - start);
707 *can_addr += (unsigned long)
708 per_cpu_ptr(mod->percpu,
709 get_boot_cpu_id());
711 preempt_enable();
712 return true;
717 preempt_enable();
718 return false;
722 * is_module_percpu_address - test whether address is from module static percpu
723 * @addr: address to test
725 * Test whether @addr belongs to module static percpu area.
727 * RETURNS:
728 * %true if @addr is from module static percpu area
730 bool is_module_percpu_address(unsigned long addr)
732 return __is_module_percpu_address(addr, NULL);
735 #else /* ... !CONFIG_SMP */
737 static inline void __percpu *mod_percpu(struct module *mod)
739 return NULL;
741 static int percpu_modalloc(struct module *mod, struct load_info *info)
743 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
744 if (info->sechdrs[info->index.pcpu].sh_size != 0)
745 return -ENOMEM;
746 return 0;
748 static inline void percpu_modfree(struct module *mod)
751 static unsigned int find_pcpusec(struct load_info *info)
753 return 0;
755 static inline void percpu_modcopy(struct module *mod,
756 const void *from, unsigned long size)
758 /* pcpusec should be 0, and size of that section should be 0. */
759 BUG_ON(size != 0);
761 bool is_module_percpu_address(unsigned long addr)
763 return false;
766 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
768 return false;
771 #endif /* CONFIG_SMP */
773 #define MODINFO_ATTR(field) \
774 static void setup_modinfo_##field(struct module *mod, const char *s) \
776 mod->field = kstrdup(s, GFP_KERNEL); \
778 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
779 struct module_kobject *mk, char *buffer) \
781 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
783 static int modinfo_##field##_exists(struct module *mod) \
785 return mod->field != NULL; \
787 static void free_modinfo_##field(struct module *mod) \
789 kfree(mod->field); \
790 mod->field = NULL; \
792 static struct module_attribute modinfo_##field = { \
793 .attr = { .name = __stringify(field), .mode = 0444 }, \
794 .show = show_modinfo_##field, \
795 .setup = setup_modinfo_##field, \
796 .test = modinfo_##field##_exists, \
797 .free = free_modinfo_##field, \
800 MODINFO_ATTR(version);
801 MODINFO_ATTR(srcversion);
803 static char last_unloaded_module[MODULE_NAME_LEN+1];
805 #ifdef CONFIG_MODULE_UNLOAD
807 EXPORT_TRACEPOINT_SYMBOL(module_get);
809 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
810 #define MODULE_REF_BASE 1
812 /* Init the unload section of the module. */
813 static int module_unload_init(struct module *mod)
816 * Initialize reference counter to MODULE_REF_BASE.
817 * refcnt == 0 means module is going.
819 atomic_set(&mod->refcnt, MODULE_REF_BASE);
821 INIT_LIST_HEAD(&mod->source_list);
822 INIT_LIST_HEAD(&mod->target_list);
824 /* Hold reference count during initialization. */
825 atomic_inc(&mod->refcnt);
827 return 0;
830 /* Does a already use b? */
831 static int already_uses(struct module *a, struct module *b)
833 struct module_use *use;
835 list_for_each_entry(use, &b->source_list, source_list) {
836 if (use->source == a) {
837 pr_debug("%s uses %s!\n", a->name, b->name);
838 return 1;
841 pr_debug("%s does not use %s!\n", a->name, b->name);
842 return 0;
846 * Module a uses b
847 * - we add 'a' as a "source", 'b' as a "target" of module use
848 * - the module_use is added to the list of 'b' sources (so
849 * 'b' can walk the list to see who sourced them), and of 'a'
850 * targets (so 'a' can see what modules it targets).
852 static int add_module_usage(struct module *a, struct module *b)
854 struct module_use *use;
856 pr_debug("Allocating new usage for %s.\n", a->name);
857 use = kmalloc(sizeof(*use), GFP_ATOMIC);
858 if (!use)
859 return -ENOMEM;
861 use->source = a;
862 use->target = b;
863 list_add(&use->source_list, &b->source_list);
864 list_add(&use->target_list, &a->target_list);
865 return 0;
868 /* Module a uses b: caller needs module_mutex() */
869 int ref_module(struct module *a, struct module *b)
871 int err;
873 if (b == NULL || already_uses(a, b))
874 return 0;
876 /* If module isn't available, we fail. */
877 err = strong_try_module_get(b);
878 if (err)
879 return err;
881 err = add_module_usage(a, b);
882 if (err) {
883 module_put(b);
884 return err;
886 return 0;
888 EXPORT_SYMBOL_GPL(ref_module);
890 /* Clear the unload stuff of the module. */
891 static void module_unload_free(struct module *mod)
893 struct module_use *use, *tmp;
895 mutex_lock(&module_mutex);
896 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
897 struct module *i = use->target;
898 pr_debug("%s unusing %s\n", mod->name, i->name);
899 module_put(i);
900 list_del(&use->source_list);
901 list_del(&use->target_list);
902 kfree(use);
904 mutex_unlock(&module_mutex);
907 #ifdef CONFIG_MODULE_FORCE_UNLOAD
908 static inline int try_force_unload(unsigned int flags)
910 int ret = (flags & O_TRUNC);
911 if (ret)
912 add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
913 return ret;
915 #else
916 static inline int try_force_unload(unsigned int flags)
918 return 0;
920 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
922 /* Try to release refcount of module, 0 means success. */
923 static int try_release_module_ref(struct module *mod)
925 int ret;
927 /* Try to decrement refcnt which we set at loading */
928 ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
929 BUG_ON(ret < 0);
930 if (ret)
931 /* Someone can put this right now, recover with checking */
932 ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
934 return ret;
937 static int try_stop_module(struct module *mod, int flags, int *forced)
939 /* If it's not unused, quit unless we're forcing. */
940 if (try_release_module_ref(mod) != 0) {
941 *forced = try_force_unload(flags);
942 if (!(*forced))
943 return -EWOULDBLOCK;
946 /* Mark it as dying. */
947 mod->state = MODULE_STATE_GOING;
949 return 0;
953 * module_refcount - return the refcount or -1 if unloading
955 * @mod: the module we're checking
957 * Returns:
958 * -1 if the module is in the process of unloading
959 * otherwise the number of references in the kernel to the module
961 int module_refcount(struct module *mod)
963 return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
965 EXPORT_SYMBOL(module_refcount);
967 /* This exists whether we can unload or not */
968 static void free_module(struct module *mod);
970 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
971 unsigned int, flags)
973 struct module *mod;
974 char name[MODULE_NAME_LEN];
975 int ret, forced = 0;
977 if (!capable(CAP_SYS_MODULE) || modules_disabled)
978 return -EPERM;
980 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
981 return -EFAULT;
982 name[MODULE_NAME_LEN-1] = '\0';
984 audit_log_kern_module(name);
986 if (mutex_lock_interruptible(&module_mutex) != 0)
987 return -EINTR;
989 mod = find_module(name);
990 if (!mod) {
991 ret = -ENOENT;
992 goto out;
995 if (!list_empty(&mod->source_list)) {
996 /* Other modules depend on us: get rid of them first. */
997 ret = -EWOULDBLOCK;
998 goto out;
1001 /* Doing init or already dying? */
1002 if (mod->state != MODULE_STATE_LIVE) {
1003 /* FIXME: if (force), slam module count damn the torpedoes */
1004 pr_debug("%s already dying\n", mod->name);
1005 ret = -EBUSY;
1006 goto out;
1009 /* If it has an init func, it must have an exit func to unload */
1010 if (mod->init && !mod->exit) {
1011 forced = try_force_unload(flags);
1012 if (!forced) {
1013 /* This module can't be removed */
1014 ret = -EBUSY;
1015 goto out;
1019 /* Stop the machine so refcounts can't move and disable module. */
1020 ret = try_stop_module(mod, flags, &forced);
1021 if (ret != 0)
1022 goto out;
1024 mutex_unlock(&module_mutex);
1025 /* Final destruction now no one is using it. */
1026 if (mod->exit != NULL)
1027 mod->exit();
1028 blocking_notifier_call_chain(&module_notify_list,
1029 MODULE_STATE_GOING, mod);
1030 klp_module_going(mod);
1031 ftrace_release_mod(mod);
1033 async_synchronize_full();
1035 /* Store the name of the last unloaded module for diagnostic purposes */
1036 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
1038 free_module(mod);
1039 /* someone could wait for the module in add_unformed_module() */
1040 wake_up_all(&module_wq);
1041 return 0;
1042 out:
1043 mutex_unlock(&module_mutex);
1044 return ret;
1047 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1049 struct module_use *use;
1050 int printed_something = 0;
1052 seq_printf(m, " %i ", module_refcount(mod));
1055 * Always include a trailing , so userspace can differentiate
1056 * between this and the old multi-field proc format.
1058 list_for_each_entry(use, &mod->source_list, source_list) {
1059 printed_something = 1;
1060 seq_printf(m, "%s,", use->source->name);
1063 if (mod->init != NULL && mod->exit == NULL) {
1064 printed_something = 1;
1065 seq_puts(m, "[permanent],");
1068 if (!printed_something)
1069 seq_puts(m, "-");
1072 void __symbol_put(const char *symbol)
1074 struct module *owner;
1076 preempt_disable();
1077 if (!find_symbol(symbol, &owner, NULL, true, false))
1078 BUG();
1079 module_put(owner);
1080 preempt_enable();
1082 EXPORT_SYMBOL(__symbol_put);
1084 /* Note this assumes addr is a function, which it currently always is. */
1085 void symbol_put_addr(void *addr)
1087 struct module *modaddr;
1088 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
1090 if (core_kernel_text(a))
1091 return;
1094 * Even though we hold a reference on the module; we still need to
1095 * disable preemption in order to safely traverse the data structure.
1097 preempt_disable();
1098 modaddr = __module_text_address(a);
1099 BUG_ON(!modaddr);
1100 module_put(modaddr);
1101 preempt_enable();
1103 EXPORT_SYMBOL_GPL(symbol_put_addr);
1105 static ssize_t show_refcnt(struct module_attribute *mattr,
1106 struct module_kobject *mk, char *buffer)
1108 return sprintf(buffer, "%i\n", module_refcount(mk->mod));
1111 static struct module_attribute modinfo_refcnt =
1112 __ATTR(refcnt, 0444, show_refcnt, NULL);
1114 void __module_get(struct module *module)
1116 if (module) {
1117 preempt_disable();
1118 atomic_inc(&module->refcnt);
1119 trace_module_get(module, _RET_IP_);
1120 preempt_enable();
1123 EXPORT_SYMBOL(__module_get);
1125 bool try_module_get(struct module *module)
1127 bool ret = true;
1129 if (module) {
1130 preempt_disable();
1131 /* Note: here, we can fail to get a reference */
1132 if (likely(module_is_live(module) &&
1133 atomic_inc_not_zero(&module->refcnt) != 0))
1134 trace_module_get(module, _RET_IP_);
1135 else
1136 ret = false;
1138 preempt_enable();
1140 return ret;
1142 EXPORT_SYMBOL(try_module_get);
1144 void module_put(struct module *module)
1146 int ret;
1148 if (module) {
1149 preempt_disable();
1150 ret = atomic_dec_if_positive(&module->refcnt);
1151 WARN_ON(ret < 0); /* Failed to put refcount */
1152 trace_module_put(module, _RET_IP_);
1153 preempt_enable();
1156 EXPORT_SYMBOL(module_put);
1158 #else /* !CONFIG_MODULE_UNLOAD */
1159 static inline void print_unload_info(struct seq_file *m, struct module *mod)
1161 /* We don't know the usage count, or what modules are using. */
1162 seq_puts(m, " - -");
1165 static inline void module_unload_free(struct module *mod)
1169 int ref_module(struct module *a, struct module *b)
1171 return strong_try_module_get(b);
1173 EXPORT_SYMBOL_GPL(ref_module);
1175 static inline int module_unload_init(struct module *mod)
1177 return 0;
1179 #endif /* CONFIG_MODULE_UNLOAD */
1181 static size_t module_flags_taint(struct module *mod, char *buf)
1183 size_t l = 0;
1184 int i;
1186 for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
1187 if (taint_flags[i].module && test_bit(i, &mod->taints))
1188 buf[l++] = taint_flags[i].c_true;
1191 return l;
1194 static ssize_t show_initstate(struct module_attribute *mattr,
1195 struct module_kobject *mk, char *buffer)
1197 const char *state = "unknown";
1199 switch (mk->mod->state) {
1200 case MODULE_STATE_LIVE:
1201 state = "live";
1202 break;
1203 case MODULE_STATE_COMING:
1204 state = "coming";
1205 break;
1206 case MODULE_STATE_GOING:
1207 state = "going";
1208 break;
1209 default:
1210 BUG();
1212 return sprintf(buffer, "%s\n", state);
1215 static struct module_attribute modinfo_initstate =
1216 __ATTR(initstate, 0444, show_initstate, NULL);
1218 static ssize_t store_uevent(struct module_attribute *mattr,
1219 struct module_kobject *mk,
1220 const char *buffer, size_t count)
1222 int rc;
1224 rc = kobject_synth_uevent(&mk->kobj, buffer, count);
1225 return rc ? rc : count;
1228 struct module_attribute module_uevent =
1229 __ATTR(uevent, 0200, NULL, store_uevent);
1231 static ssize_t show_coresize(struct module_attribute *mattr,
1232 struct module_kobject *mk, char *buffer)
1234 return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
1237 static struct module_attribute modinfo_coresize =
1238 __ATTR(coresize, 0444, show_coresize, NULL);
1240 static ssize_t show_initsize(struct module_attribute *mattr,
1241 struct module_kobject *mk, char *buffer)
1243 return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
1246 static struct module_attribute modinfo_initsize =
1247 __ATTR(initsize, 0444, show_initsize, NULL);
1249 static ssize_t show_taint(struct module_attribute *mattr,
1250 struct module_kobject *mk, char *buffer)
1252 size_t l;
1254 l = module_flags_taint(mk->mod, buffer);
1255 buffer[l++] = '\n';
1256 return l;
1259 static struct module_attribute modinfo_taint =
1260 __ATTR(taint, 0444, show_taint, NULL);
1262 static struct module_attribute *modinfo_attrs[] = {
1263 &module_uevent,
1264 &modinfo_version,
1265 &modinfo_srcversion,
1266 &modinfo_initstate,
1267 &modinfo_coresize,
1268 &modinfo_initsize,
1269 &modinfo_taint,
1270 #ifdef CONFIG_MODULE_UNLOAD
1271 &modinfo_refcnt,
1272 #endif
1273 NULL,
1276 static const char vermagic[] = VERMAGIC_STRING;
1278 static int try_to_force_load(struct module *mod, const char *reason)
1280 #ifdef CONFIG_MODULE_FORCE_LOAD
1281 if (!test_taint(TAINT_FORCED_MODULE))
1282 pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
1283 add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
1284 return 0;
1285 #else
1286 return -ENOEXEC;
1287 #endif
1290 #ifdef CONFIG_MODVERSIONS
1292 static u32 resolve_rel_crc(const s32 *crc)
1294 return *(u32 *)((void *)crc + *crc);
1297 static int check_version(const struct load_info *info,
1298 const char *symname,
1299 struct module *mod,
1300 const s32 *crc)
1302 Elf_Shdr *sechdrs = info->sechdrs;
1303 unsigned int versindex = info->index.vers;
1304 unsigned int i, num_versions;
1305 struct modversion_info *versions;
1307 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1308 if (!crc)
1309 return 1;
1311 /* No versions at all? modprobe --force does this. */
1312 if (versindex == 0)
1313 return try_to_force_load(mod, symname) == 0;
1315 versions = (void *) sechdrs[versindex].sh_addr;
1316 num_versions = sechdrs[versindex].sh_size
1317 / sizeof(struct modversion_info);
1319 for (i = 0; i < num_versions; i++) {
1320 u32 crcval;
1322 if (strcmp(versions[i].name, symname) != 0)
1323 continue;
1325 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
1326 crcval = resolve_rel_crc(crc);
1327 else
1328 crcval = *crc;
1329 if (versions[i].crc == crcval)
1330 return 1;
1331 pr_debug("Found checksum %X vs module %lX\n",
1332 crcval, versions[i].crc);
1333 goto bad_version;
1336 /* Broken toolchain. Warn once, then let it go.. */
1337 pr_warn_once("%s: no symbol version for %s\n", info->name, symname);
1338 return 1;
1340 bad_version:
1341 pr_warn("%s: disagrees about version of symbol %s\n",
1342 info->name, symname);
1343 return 0;
1346 static inline int check_modstruct_version(const struct load_info *info,
1347 struct module *mod)
1349 const s32 *crc;
1352 * Since this should be found in kernel (which can't be removed), no
1353 * locking is necessary -- use preempt_disable() to placate lockdep.
1355 preempt_disable();
1356 if (!find_symbol("module_layout", NULL, &crc, true, false)) {
1357 preempt_enable();
1358 BUG();
1360 preempt_enable();
1361 return check_version(info, "module_layout", mod, crc);
1364 /* First part is kernel version, which we ignore if module has crcs. */
1365 static inline int same_magic(const char *amagic, const char *bmagic,
1366 bool has_crcs)
1368 if (has_crcs) {
1369 amagic += strcspn(amagic, " ");
1370 bmagic += strcspn(bmagic, " ");
1372 return strcmp(amagic, bmagic) == 0;
1374 #else
1375 static inline int check_version(const struct load_info *info,
1376 const char *symname,
1377 struct module *mod,
1378 const s32 *crc)
1380 return 1;
1383 static inline int check_modstruct_version(const struct load_info *info,
1384 struct module *mod)
1386 return 1;
1389 static inline int same_magic(const char *amagic, const char *bmagic,
1390 bool has_crcs)
1392 return strcmp(amagic, bmagic) == 0;
1394 #endif /* CONFIG_MODVERSIONS */
1396 static char *get_modinfo(const struct load_info *info, const char *tag);
1397 static char *get_next_modinfo(const struct load_info *info, const char *tag,
1398 char *prev);
1400 static int verify_namespace_is_imported(const struct load_info *info,
1401 const struct kernel_symbol *sym,
1402 struct module *mod)
1404 const char *namespace;
1405 char *imported_namespace;
1407 namespace = kernel_symbol_namespace(sym);
1408 if (namespace) {
1409 imported_namespace = get_modinfo(info, "import_ns");
1410 while (imported_namespace) {
1411 if (strcmp(namespace, imported_namespace) == 0)
1412 return 0;
1413 imported_namespace = get_next_modinfo(
1414 info, "import_ns", imported_namespace);
1416 #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1417 pr_warn(
1418 #else
1419 pr_err(
1420 #endif
1421 "%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
1422 mod->name, kernel_symbol_name(sym), namespace);
1423 #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
1424 return -EINVAL;
1425 #endif
1427 return 0;
1431 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1432 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1433 const struct load_info *info,
1434 const char *name,
1435 char ownername[])
1437 struct module *owner;
1438 const struct kernel_symbol *sym;
1439 const s32 *crc;
1440 int err;
1443 * The module_mutex should not be a heavily contended lock;
1444 * if we get the occasional sleep here, we'll go an extra iteration
1445 * in the wait_event_interruptible(), which is harmless.
1447 sched_annotate_sleep();
1448 mutex_lock(&module_mutex);
1449 sym = find_symbol(name, &owner, &crc,
1450 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1451 if (!sym)
1452 goto unlock;
1454 if (!check_version(info, name, mod, crc)) {
1455 sym = ERR_PTR(-EINVAL);
1456 goto getname;
1459 err = verify_namespace_is_imported(info, sym, mod);
1460 if (err) {
1461 sym = ERR_PTR(err);
1462 goto getname;
1465 err = ref_module(mod, owner);
1466 if (err) {
1467 sym = ERR_PTR(err);
1468 goto getname;
1471 getname:
1472 /* We must make copy under the lock if we failed to get ref. */
1473 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1474 unlock:
1475 mutex_unlock(&module_mutex);
1476 return sym;
1479 static const struct kernel_symbol *
1480 resolve_symbol_wait(struct module *mod,
1481 const struct load_info *info,
1482 const char *name)
1484 const struct kernel_symbol *ksym;
1485 char owner[MODULE_NAME_LEN];
1487 if (wait_event_interruptible_timeout(module_wq,
1488 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1489 || PTR_ERR(ksym) != -EBUSY,
1490 30 * HZ) <= 0) {
1491 pr_warn("%s: gave up waiting for init of module %s.\n",
1492 mod->name, owner);
1494 return ksym;
1498 * /sys/module/foo/sections stuff
1499 * J. Corbet <corbet@lwn.net>
1501 #ifdef CONFIG_SYSFS
1503 #ifdef CONFIG_KALLSYMS
1504 static inline bool sect_empty(const Elf_Shdr *sect)
1506 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1509 struct module_sect_attr {
1510 struct bin_attribute battr;
1511 unsigned long address;
1514 struct module_sect_attrs {
1515 struct attribute_group grp;
1516 unsigned int nsections;
1517 struct module_sect_attr attrs[0];
1520 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
1521 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
1522 struct bin_attribute *battr,
1523 char *buf, loff_t pos, size_t count)
1525 struct module_sect_attr *sattr =
1526 container_of(battr, struct module_sect_attr, battr);
1527 char bounce[MODULE_SECT_READ_SIZE + 1];
1528 size_t wrote;
1530 if (pos != 0)
1531 return -EINVAL;
1534 * Since we're a binary read handler, we must account for the
1535 * trailing NUL byte that sprintf will write: if "buf" is
1536 * too small to hold the NUL, or the NUL is exactly the last
1537 * byte, the read will look like it got truncated by one byte.
1538 * Since there is no way to ask sprintf nicely to not write
1539 * the NUL, we have to use a bounce buffer.
1541 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
1542 kallsyms_show_value(file->f_cred)
1543 ? (void *)sattr->address : NULL);
1544 count = min(count, wrote);
1545 memcpy(buf, bounce, count);
1547 return count;
1550 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1552 unsigned int section;
1554 for (section = 0; section < sect_attrs->nsections; section++)
1555 kfree(sect_attrs->attrs[section].battr.attr.name);
1556 kfree(sect_attrs);
1559 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1561 unsigned int nloaded = 0, i, size[2];
1562 struct module_sect_attrs *sect_attrs;
1563 struct module_sect_attr *sattr;
1564 struct bin_attribute **gattr;
1566 /* Count loaded sections and allocate structures */
1567 for (i = 0; i < info->hdr->e_shnum; i++)
1568 if (!sect_empty(&info->sechdrs[i]))
1569 nloaded++;
1570 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
1571 sizeof(sect_attrs->grp.bin_attrs[0]));
1572 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
1573 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1574 if (sect_attrs == NULL)
1575 return;
1577 /* Setup section attributes. */
1578 sect_attrs->grp.name = "sections";
1579 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
1581 sect_attrs->nsections = 0;
1582 sattr = &sect_attrs->attrs[0];
1583 gattr = &sect_attrs->grp.bin_attrs[0];
1584 for (i = 0; i < info->hdr->e_shnum; i++) {
1585 Elf_Shdr *sec = &info->sechdrs[i];
1586 if (sect_empty(sec))
1587 continue;
1588 sysfs_bin_attr_init(&sattr->battr);
1589 sattr->address = sec->sh_addr;
1590 sattr->battr.attr.name =
1591 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
1592 if (sattr->battr.attr.name == NULL)
1593 goto out;
1594 sect_attrs->nsections++;
1595 sattr->battr.read = module_sect_read;
1596 sattr->battr.size = MODULE_SECT_READ_SIZE;
1597 sattr->battr.attr.mode = 0400;
1598 *(gattr++) = &(sattr++)->battr;
1600 *gattr = NULL;
1602 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1603 goto out;
1605 mod->sect_attrs = sect_attrs;
1606 return;
1607 out:
1608 free_sect_attrs(sect_attrs);
1611 static void remove_sect_attrs(struct module *mod)
1613 if (mod->sect_attrs) {
1614 sysfs_remove_group(&mod->mkobj.kobj,
1615 &mod->sect_attrs->grp);
1616 /* We are positive that no one is using any sect attrs
1617 * at this point. Deallocate immediately. */
1618 free_sect_attrs(mod->sect_attrs);
1619 mod->sect_attrs = NULL;
1624 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1627 struct module_notes_attrs {
1628 struct kobject *dir;
1629 unsigned int notes;
1630 struct bin_attribute attrs[0];
1633 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1634 struct bin_attribute *bin_attr,
1635 char *buf, loff_t pos, size_t count)
1638 * The caller checked the pos and count against our size.
1640 memcpy(buf, bin_attr->private + pos, count);
1641 return count;
1644 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1645 unsigned int i)
1647 if (notes_attrs->dir) {
1648 while (i-- > 0)
1649 sysfs_remove_bin_file(notes_attrs->dir,
1650 &notes_attrs->attrs[i]);
1651 kobject_put(notes_attrs->dir);
1653 kfree(notes_attrs);
1656 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1658 unsigned int notes, loaded, i;
1659 struct module_notes_attrs *notes_attrs;
1660 struct bin_attribute *nattr;
1662 /* failed to create section attributes, so can't create notes */
1663 if (!mod->sect_attrs)
1664 return;
1666 /* Count notes sections and allocate structures. */
1667 notes = 0;
1668 for (i = 0; i < info->hdr->e_shnum; i++)
1669 if (!sect_empty(&info->sechdrs[i]) &&
1670 (info->sechdrs[i].sh_type == SHT_NOTE))
1671 ++notes;
1673 if (notes == 0)
1674 return;
1676 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
1677 GFP_KERNEL);
1678 if (notes_attrs == NULL)
1679 return;
1681 notes_attrs->notes = notes;
1682 nattr = &notes_attrs->attrs[0];
1683 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1684 if (sect_empty(&info->sechdrs[i]))
1685 continue;
1686 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1687 sysfs_bin_attr_init(nattr);
1688 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
1689 nattr->attr.mode = S_IRUGO;
1690 nattr->size = info->sechdrs[i].sh_size;
1691 nattr->private = (void *) info->sechdrs[i].sh_addr;
1692 nattr->read = module_notes_read;
1693 ++nattr;
1695 ++loaded;
1698 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1699 if (!notes_attrs->dir)
1700 goto out;
1702 for (i = 0; i < notes; ++i)
1703 if (sysfs_create_bin_file(notes_attrs->dir,
1704 &notes_attrs->attrs[i]))
1705 goto out;
1707 mod->notes_attrs = notes_attrs;
1708 return;
1710 out:
1711 free_notes_attrs(notes_attrs, i);
1714 static void remove_notes_attrs(struct module *mod)
1716 if (mod->notes_attrs)
1717 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1720 #else
1722 static inline void add_sect_attrs(struct module *mod,
1723 const struct load_info *info)
1727 static inline void remove_sect_attrs(struct module *mod)
1731 static inline void add_notes_attrs(struct module *mod,
1732 const struct load_info *info)
1736 static inline void remove_notes_attrs(struct module *mod)
1739 #endif /* CONFIG_KALLSYMS */
1741 static void del_usage_links(struct module *mod)
1743 #ifdef CONFIG_MODULE_UNLOAD
1744 struct module_use *use;
1746 mutex_lock(&module_mutex);
1747 list_for_each_entry(use, &mod->target_list, target_list)
1748 sysfs_remove_link(use->target->holders_dir, mod->name);
1749 mutex_unlock(&module_mutex);
1750 #endif
1753 static int add_usage_links(struct module *mod)
1755 int ret = 0;
1756 #ifdef CONFIG_MODULE_UNLOAD
1757 struct module_use *use;
1759 mutex_lock(&module_mutex);
1760 list_for_each_entry(use, &mod->target_list, target_list) {
1761 ret = sysfs_create_link(use->target->holders_dir,
1762 &mod->mkobj.kobj, mod->name);
1763 if (ret)
1764 break;
1766 mutex_unlock(&module_mutex);
1767 if (ret)
1768 del_usage_links(mod);
1769 #endif
1770 return ret;
1773 static void module_remove_modinfo_attrs(struct module *mod, int end);
1775 static int module_add_modinfo_attrs(struct module *mod)
1777 struct module_attribute *attr;
1778 struct module_attribute *temp_attr;
1779 int error = 0;
1780 int i;
1782 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1783 (ARRAY_SIZE(modinfo_attrs) + 1)),
1784 GFP_KERNEL);
1785 if (!mod->modinfo_attrs)
1786 return -ENOMEM;
1788 temp_attr = mod->modinfo_attrs;
1789 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1790 if (!attr->test || attr->test(mod)) {
1791 memcpy(temp_attr, attr, sizeof(*temp_attr));
1792 sysfs_attr_init(&temp_attr->attr);
1793 error = sysfs_create_file(&mod->mkobj.kobj,
1794 &temp_attr->attr);
1795 if (error)
1796 goto error_out;
1797 ++temp_attr;
1801 return 0;
1803 error_out:
1804 if (i > 0)
1805 module_remove_modinfo_attrs(mod, --i);
1806 else
1807 kfree(mod->modinfo_attrs);
1808 return error;
1811 static void module_remove_modinfo_attrs(struct module *mod, int end)
1813 struct module_attribute *attr;
1814 int i;
1816 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1817 if (end >= 0 && i > end)
1818 break;
1819 /* pick a field to test for end of list */
1820 if (!attr->attr.name)
1821 break;
1822 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
1823 if (attr->free)
1824 attr->free(mod);
1826 kfree(mod->modinfo_attrs);
1829 static void mod_kobject_put(struct module *mod)
1831 DECLARE_COMPLETION_ONSTACK(c);
1832 mod->mkobj.kobj_completion = &c;
1833 kobject_put(&mod->mkobj.kobj);
1834 wait_for_completion(&c);
1837 static int mod_sysfs_init(struct module *mod)
1839 int err;
1840 struct kobject *kobj;
1842 if (!module_sysfs_initialized) {
1843 pr_err("%s: module sysfs not initialized\n", mod->name);
1844 err = -EINVAL;
1845 goto out;
1848 kobj = kset_find_obj(module_kset, mod->name);
1849 if (kobj) {
1850 pr_err("%s: module is already loaded\n", mod->name);
1851 kobject_put(kobj);
1852 err = -EINVAL;
1853 goto out;
1856 mod->mkobj.mod = mod;
1858 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1859 mod->mkobj.kobj.kset = module_kset;
1860 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1861 "%s", mod->name);
1862 if (err)
1863 mod_kobject_put(mod);
1865 /* delay uevent until full sysfs population */
1866 out:
1867 return err;
1870 static int mod_sysfs_setup(struct module *mod,
1871 const struct load_info *info,
1872 struct kernel_param *kparam,
1873 unsigned int num_params)
1875 int err;
1877 err = mod_sysfs_init(mod);
1878 if (err)
1879 goto out;
1881 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1882 if (!mod->holders_dir) {
1883 err = -ENOMEM;
1884 goto out_unreg;
1887 err = module_param_sysfs_setup(mod, kparam, num_params);
1888 if (err)
1889 goto out_unreg_holders;
1891 err = module_add_modinfo_attrs(mod);
1892 if (err)
1893 goto out_unreg_param;
1895 err = add_usage_links(mod);
1896 if (err)
1897 goto out_unreg_modinfo_attrs;
1899 add_sect_attrs(mod, info);
1900 add_notes_attrs(mod, info);
1902 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1903 return 0;
1905 out_unreg_modinfo_attrs:
1906 module_remove_modinfo_attrs(mod, -1);
1907 out_unreg_param:
1908 module_param_sysfs_remove(mod);
1909 out_unreg_holders:
1910 kobject_put(mod->holders_dir);
1911 out_unreg:
1912 mod_kobject_put(mod);
1913 out:
1914 return err;
1917 static void mod_sysfs_fini(struct module *mod)
1919 remove_notes_attrs(mod);
1920 remove_sect_attrs(mod);
1921 mod_kobject_put(mod);
1924 static void init_param_lock(struct module *mod)
1926 mutex_init(&mod->param_lock);
1928 #else /* !CONFIG_SYSFS */
1930 static int mod_sysfs_setup(struct module *mod,
1931 const struct load_info *info,
1932 struct kernel_param *kparam,
1933 unsigned int num_params)
1935 return 0;
1938 static void mod_sysfs_fini(struct module *mod)
1942 static void module_remove_modinfo_attrs(struct module *mod, int end)
1946 static void del_usage_links(struct module *mod)
1950 static void init_param_lock(struct module *mod)
1953 #endif /* CONFIG_SYSFS */
1955 static void mod_sysfs_teardown(struct module *mod)
1957 del_usage_links(mod);
1958 module_remove_modinfo_attrs(mod, -1);
1959 module_param_sysfs_remove(mod);
1960 kobject_put(mod->mkobj.drivers_dir);
1961 kobject_put(mod->holders_dir);
1962 mod_sysfs_fini(mod);
1965 #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
1967 * LKM RO/NX protection: protect module's text/ro-data
1968 * from modification and any data from execution.
1970 * General layout of module is:
1971 * [text] [read-only-data] [ro-after-init] [writable data]
1972 * text_size -----^ ^ ^ ^
1973 * ro_size ------------------------| | |
1974 * ro_after_init_size -----------------------------| |
1975 * size -----------------------------------------------------------|
1977 * These values are always page-aligned (as is base)
1979 static void frob_text(const struct module_layout *layout,
1980 int (*set_memory)(unsigned long start, int num_pages))
1982 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1983 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1984 set_memory((unsigned long)layout->base,
1985 layout->text_size >> PAGE_SHIFT);
1988 #ifdef CONFIG_STRICT_MODULE_RWX
1989 static void frob_rodata(const struct module_layout *layout,
1990 int (*set_memory)(unsigned long start, int num_pages))
1992 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
1993 BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
1994 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
1995 set_memory((unsigned long)layout->base + layout->text_size,
1996 (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
1999 static void frob_ro_after_init(const struct module_layout *layout,
2000 int (*set_memory)(unsigned long start, int num_pages))
2002 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2003 BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
2004 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
2005 set_memory((unsigned long)layout->base + layout->ro_size,
2006 (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
2009 static void frob_writable_data(const struct module_layout *layout,
2010 int (*set_memory)(unsigned long start, int num_pages))
2012 BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
2013 BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
2014 BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
2015 set_memory((unsigned long)layout->base + layout->ro_after_init_size,
2016 (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
2019 /* livepatching wants to disable read-only so it can frob module. */
2020 void module_disable_ro(const struct module *mod)
2022 if (!rodata_enabled)
2023 return;
2025 frob_text(&mod->core_layout, set_memory_rw);
2026 frob_rodata(&mod->core_layout, set_memory_rw);
2027 frob_ro_after_init(&mod->core_layout, set_memory_rw);
2028 frob_text(&mod->init_layout, set_memory_rw);
2029 frob_rodata(&mod->init_layout, set_memory_rw);
2032 void module_enable_ro(const struct module *mod, bool after_init)
2034 if (!rodata_enabled)
2035 return;
2037 set_vm_flush_reset_perms(mod->core_layout.base);
2038 set_vm_flush_reset_perms(mod->init_layout.base);
2039 frob_text(&mod->core_layout, set_memory_ro);
2041 frob_rodata(&mod->core_layout, set_memory_ro);
2042 frob_text(&mod->init_layout, set_memory_ro);
2043 frob_rodata(&mod->init_layout, set_memory_ro);
2045 if (after_init)
2046 frob_ro_after_init(&mod->core_layout, set_memory_ro);
2049 static void module_enable_nx(const struct module *mod)
2051 frob_rodata(&mod->core_layout, set_memory_nx);
2052 frob_ro_after_init(&mod->core_layout, set_memory_nx);
2053 frob_writable_data(&mod->core_layout, set_memory_nx);
2054 frob_rodata(&mod->init_layout, set_memory_nx);
2055 frob_writable_data(&mod->init_layout, set_memory_nx);
2058 /* Iterate through all modules and set each module's text as RW */
2059 void set_all_modules_text_rw(void)
2061 struct module *mod;
2063 if (!rodata_enabled)
2064 return;
2066 mutex_lock(&module_mutex);
2067 list_for_each_entry_rcu(mod, &modules, list) {
2068 if (mod->state == MODULE_STATE_UNFORMED)
2069 continue;
2071 frob_text(&mod->core_layout, set_memory_rw);
2072 frob_text(&mod->init_layout, set_memory_rw);
2074 mutex_unlock(&module_mutex);
2077 /* Iterate through all modules and set each module's text as RO */
2078 void set_all_modules_text_ro(void)
2080 struct module *mod;
2082 if (!rodata_enabled)
2083 return;
2085 mutex_lock(&module_mutex);
2086 list_for_each_entry_rcu(mod, &modules, list) {
2088 * Ignore going modules since it's possible that ro
2089 * protection has already been disabled, otherwise we'll
2090 * run into protection faults at module deallocation.
2092 if (mod->state == MODULE_STATE_UNFORMED ||
2093 mod->state == MODULE_STATE_GOING)
2094 continue;
2096 frob_text(&mod->core_layout, set_memory_ro);
2097 frob_text(&mod->init_layout, set_memory_ro);
2099 mutex_unlock(&module_mutex);
2101 #else /* !CONFIG_STRICT_MODULE_RWX */
2102 static void module_enable_nx(const struct module *mod) { }
2103 #endif /* CONFIG_STRICT_MODULE_RWX */
2104 static void module_enable_x(const struct module *mod)
2106 frob_text(&mod->core_layout, set_memory_x);
2107 frob_text(&mod->init_layout, set_memory_x);
2109 #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2110 static void module_enable_nx(const struct module *mod) { }
2111 static void module_enable_x(const struct module *mod) { }
2112 #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
2115 #ifdef CONFIG_LIVEPATCH
2117 * Persist Elf information about a module. Copy the Elf header,
2118 * section header table, section string table, and symtab section
2119 * index from info to mod->klp_info.
2121 static int copy_module_elf(struct module *mod, struct load_info *info)
2123 unsigned int size, symndx;
2124 int ret;
2126 size = sizeof(*mod->klp_info);
2127 mod->klp_info = kmalloc(size, GFP_KERNEL);
2128 if (mod->klp_info == NULL)
2129 return -ENOMEM;
2131 /* Elf header */
2132 size = sizeof(mod->klp_info->hdr);
2133 memcpy(&mod->klp_info->hdr, info->hdr, size);
2135 /* Elf section header table */
2136 size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
2137 mod->klp_info->sechdrs = kmemdup(info->sechdrs, size, GFP_KERNEL);
2138 if (mod->klp_info->sechdrs == NULL) {
2139 ret = -ENOMEM;
2140 goto free_info;
2143 /* Elf section name string table */
2144 size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
2145 mod->klp_info->secstrings = kmemdup(info->secstrings, size, GFP_KERNEL);
2146 if (mod->klp_info->secstrings == NULL) {
2147 ret = -ENOMEM;
2148 goto free_sechdrs;
2151 /* Elf symbol section index */
2152 symndx = info->index.sym;
2153 mod->klp_info->symndx = symndx;
2156 * For livepatch modules, core_kallsyms.symtab is a complete
2157 * copy of the original symbol table. Adjust sh_addr to point
2158 * to core_kallsyms.symtab since the copy of the symtab in module
2159 * init memory is freed at the end of do_init_module().
2161 mod->klp_info->sechdrs[symndx].sh_addr = \
2162 (unsigned long) mod->core_kallsyms.symtab;
2164 return 0;
2166 free_sechdrs:
2167 kfree(mod->klp_info->sechdrs);
2168 free_info:
2169 kfree(mod->klp_info);
2170 return ret;
2173 static void free_module_elf(struct module *mod)
2175 kfree(mod->klp_info->sechdrs);
2176 kfree(mod->klp_info->secstrings);
2177 kfree(mod->klp_info);
2179 #else /* !CONFIG_LIVEPATCH */
2180 static int copy_module_elf(struct module *mod, struct load_info *info)
2182 return 0;
2185 static void free_module_elf(struct module *mod)
2188 #endif /* CONFIG_LIVEPATCH */
2190 void __weak module_memfree(void *module_region)
2193 * This memory may be RO, and freeing RO memory in an interrupt is not
2194 * supported by vmalloc.
2196 WARN_ON(in_interrupt());
2197 vfree(module_region);
2200 void __weak module_arch_cleanup(struct module *mod)
2204 void __weak module_arch_freeing_init(struct module *mod)
2208 /* Free a module, remove from lists, etc. */
2209 static void free_module(struct module *mod)
2211 trace_module_free(mod);
2213 mod_sysfs_teardown(mod);
2215 /* We leave it in list to prevent duplicate loads, but make sure
2216 * that noone uses it while it's being deconstructed. */
2217 mutex_lock(&module_mutex);
2218 mod->state = MODULE_STATE_UNFORMED;
2219 mutex_unlock(&module_mutex);
2221 /* Remove dynamic debug info */
2222 ddebug_remove_module(mod->name);
2224 /* Arch-specific cleanup. */
2225 module_arch_cleanup(mod);
2227 /* Module unload stuff */
2228 module_unload_free(mod);
2230 /* Free any allocated parameters. */
2231 destroy_params(mod->kp, mod->num_kp);
2233 if (is_livepatch_module(mod))
2234 free_module_elf(mod);
2236 /* Now we can delete it from the lists */
2237 mutex_lock(&module_mutex);
2238 /* Unlink carefully: kallsyms could be walking list. */
2239 list_del_rcu(&mod->list);
2240 mod_tree_remove(mod);
2241 /* Remove this module from bug list, this uses list_del_rcu */
2242 module_bug_cleanup(mod);
2243 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2244 synchronize_rcu();
2245 mutex_unlock(&module_mutex);
2247 /* This may be empty, but that's OK */
2248 module_arch_freeing_init(mod);
2249 module_memfree(mod->init_layout.base);
2250 kfree(mod->args);
2251 percpu_modfree(mod);
2253 /* Free lock-classes; relies on the preceding sync_rcu(). */
2254 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
2256 /* Finally, free the core (containing the module structure) */
2257 module_memfree(mod->core_layout.base);
2260 void *__symbol_get(const char *symbol)
2262 struct module *owner;
2263 const struct kernel_symbol *sym;
2265 preempt_disable();
2266 sym = find_symbol(symbol, &owner, NULL, true, true);
2267 if (sym && strong_try_module_get(owner))
2268 sym = NULL;
2269 preempt_enable();
2271 return sym ? (void *)kernel_symbol_value(sym) : NULL;
2273 EXPORT_SYMBOL_GPL(__symbol_get);
2276 * Ensure that an exported symbol [global namespace] does not already exist
2277 * in the kernel or in some other module's exported symbol table.
2279 * You must hold the module_mutex.
2281 static int verify_exported_symbols(struct module *mod)
2283 unsigned int i;
2284 struct module *owner;
2285 const struct kernel_symbol *s;
2286 struct {
2287 const struct kernel_symbol *sym;
2288 unsigned int num;
2289 } arr[] = {
2290 { mod->syms, mod->num_syms },
2291 { mod->gpl_syms, mod->num_gpl_syms },
2292 { mod->gpl_future_syms, mod->num_gpl_future_syms },
2293 #ifdef CONFIG_UNUSED_SYMBOLS
2294 { mod->unused_syms, mod->num_unused_syms },
2295 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
2296 #endif
2299 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2300 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
2301 if (find_symbol(kernel_symbol_name(s), &owner, NULL,
2302 true, false)) {
2303 pr_err("%s: exports duplicate symbol %s"
2304 " (owned by %s)\n",
2305 mod->name, kernel_symbol_name(s),
2306 module_name(owner));
2307 return -ENOEXEC;
2311 return 0;
2314 /* Change all symbols so that st_value encodes the pointer directly. */
2315 static int simplify_symbols(struct module *mod, const struct load_info *info)
2317 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2318 Elf_Sym *sym = (void *)symsec->sh_addr;
2319 unsigned long secbase;
2320 unsigned int i;
2321 int ret = 0;
2322 const struct kernel_symbol *ksym;
2324 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2325 const char *name = info->strtab + sym[i].st_name;
2327 switch (sym[i].st_shndx) {
2328 case SHN_COMMON:
2329 /* Ignore common symbols */
2330 if (!strncmp(name, "__gnu_lto", 9))
2331 break;
2333 /* We compiled with -fno-common. These are not
2334 supposed to happen. */
2335 pr_debug("Common symbol: %s\n", name);
2336 pr_warn("%s: please compile with -fno-common\n",
2337 mod->name);
2338 ret = -ENOEXEC;
2339 break;
2341 case SHN_ABS:
2342 /* Don't need to do anything */
2343 pr_debug("Absolute symbol: 0x%08lx\n",
2344 (long)sym[i].st_value);
2345 break;
2347 case SHN_LIVEPATCH:
2348 /* Livepatch symbols are resolved by livepatch */
2349 break;
2351 case SHN_UNDEF:
2352 ksym = resolve_symbol_wait(mod, info, name);
2353 /* Ok if resolved. */
2354 if (ksym && !IS_ERR(ksym)) {
2355 sym[i].st_value = kernel_symbol_value(ksym);
2356 break;
2359 /* Ok if weak. */
2360 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
2361 break;
2363 ret = PTR_ERR(ksym) ?: -ENOENT;
2364 pr_warn("%s: Unknown symbol %s (err %d)\n",
2365 mod->name, name, ret);
2366 break;
2368 default:
2369 /* Divert to percpu allocation if a percpu var. */
2370 if (sym[i].st_shndx == info->index.pcpu)
2371 secbase = (unsigned long)mod_percpu(mod);
2372 else
2373 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
2374 sym[i].st_value += secbase;
2375 break;
2379 return ret;
2382 static int apply_relocations(struct module *mod, const struct load_info *info)
2384 unsigned int i;
2385 int err = 0;
2387 /* Now do relocations. */
2388 for (i = 1; i < info->hdr->e_shnum; i++) {
2389 unsigned int infosec = info->sechdrs[i].sh_info;
2391 /* Not a valid relocation section? */
2392 if (infosec >= info->hdr->e_shnum)
2393 continue;
2395 /* Don't bother with non-allocated sections */
2396 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2397 continue;
2399 /* Livepatch relocation sections are applied by livepatch */
2400 if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
2401 continue;
2403 if (info->sechdrs[i].sh_type == SHT_REL)
2404 err = apply_relocate(info->sechdrs, info->strtab,
2405 info->index.sym, i, mod);
2406 else if (info->sechdrs[i].sh_type == SHT_RELA)
2407 err = apply_relocate_add(info->sechdrs, info->strtab,
2408 info->index.sym, i, mod);
2409 if (err < 0)
2410 break;
2412 return err;
2415 /* Additional bytes needed by arch in front of individual sections */
2416 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2417 unsigned int section)
2419 /* default implementation just returns zero */
2420 return 0;
2423 /* Update size with this section: return offset. */
2424 static long get_offset(struct module *mod, unsigned int *size,
2425 Elf_Shdr *sechdr, unsigned int section)
2427 long ret;
2429 *size += arch_mod_section_prepend(mod, section);
2430 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2431 *size = ret + sechdr->sh_size;
2432 return ret;
2435 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2436 might -- code, read-only data, read-write data, small data. Tally
2437 sizes, and place the offsets into sh_entsize fields: high bit means it
2438 belongs in init. */
2439 static void layout_sections(struct module *mod, struct load_info *info)
2441 static unsigned long const masks[][2] = {
2442 /* NOTE: all executable code must be the first section
2443 * in this array; otherwise modify the text_size
2444 * finder in the two loops below */
2445 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2446 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2447 { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
2448 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2449 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2451 unsigned int m, i;
2453 for (i = 0; i < info->hdr->e_shnum; i++)
2454 info->sechdrs[i].sh_entsize = ~0UL;
2456 pr_debug("Core section allocation order:\n");
2457 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2458 for (i = 0; i < info->hdr->e_shnum; ++i) {
2459 Elf_Shdr *s = &info->sechdrs[i];
2460 const char *sname = info->secstrings + s->sh_name;
2462 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2463 || (s->sh_flags & masks[m][1])
2464 || s->sh_entsize != ~0UL
2465 || strstarts(sname, ".init"))
2466 continue;
2467 s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
2468 pr_debug("\t%s\n", sname);
2470 switch (m) {
2471 case 0: /* executable */
2472 mod->core_layout.size = debug_align(mod->core_layout.size);
2473 mod->core_layout.text_size = mod->core_layout.size;
2474 break;
2475 case 1: /* RO: text and ro-data */
2476 mod->core_layout.size = debug_align(mod->core_layout.size);
2477 mod->core_layout.ro_size = mod->core_layout.size;
2478 break;
2479 case 2: /* RO after init */
2480 mod->core_layout.size = debug_align(mod->core_layout.size);
2481 mod->core_layout.ro_after_init_size = mod->core_layout.size;
2482 break;
2483 case 4: /* whole core */
2484 mod->core_layout.size = debug_align(mod->core_layout.size);
2485 break;
2489 pr_debug("Init section allocation order:\n");
2490 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2491 for (i = 0; i < info->hdr->e_shnum; ++i) {
2492 Elf_Shdr *s = &info->sechdrs[i];
2493 const char *sname = info->secstrings + s->sh_name;
2495 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2496 || (s->sh_flags & masks[m][1])
2497 || s->sh_entsize != ~0UL
2498 || !strstarts(sname, ".init"))
2499 continue;
2500 s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
2501 | INIT_OFFSET_MASK);
2502 pr_debug("\t%s\n", sname);
2504 switch (m) {
2505 case 0: /* executable */
2506 mod->init_layout.size = debug_align(mod->init_layout.size);
2507 mod->init_layout.text_size = mod->init_layout.size;
2508 break;
2509 case 1: /* RO: text and ro-data */
2510 mod->init_layout.size = debug_align(mod->init_layout.size);
2511 mod->init_layout.ro_size = mod->init_layout.size;
2512 break;
2513 case 2:
2515 * RO after init doesn't apply to init_layout (only
2516 * core_layout), so it just takes the value of ro_size.
2518 mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
2519 break;
2520 case 4: /* whole init */
2521 mod->init_layout.size = debug_align(mod->init_layout.size);
2522 break;
2527 static void set_license(struct module *mod, const char *license)
2529 if (!license)
2530 license = "unspecified";
2532 if (!license_is_gpl_compatible(license)) {
2533 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2534 pr_warn("%s: module license '%s' taints kernel.\n",
2535 mod->name, license);
2536 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
2537 LOCKDEP_NOW_UNRELIABLE);
2541 /* Parse tag=value strings from .modinfo section */
2542 static char *next_string(char *string, unsigned long *secsize)
2544 /* Skip non-zero chars */
2545 while (string[0]) {
2546 string++;
2547 if ((*secsize)-- <= 1)
2548 return NULL;
2551 /* Skip any zero padding. */
2552 while (!string[0]) {
2553 string++;
2554 if ((*secsize)-- <= 1)
2555 return NULL;
2557 return string;
2560 static char *get_next_modinfo(const struct load_info *info, const char *tag,
2561 char *prev)
2563 char *p;
2564 unsigned int taglen = strlen(tag);
2565 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2566 unsigned long size = infosec->sh_size;
2569 * get_modinfo() calls made before rewrite_section_headers()
2570 * must use sh_offset, as sh_addr isn't set!
2572 char *modinfo = (char *)info->hdr + infosec->sh_offset;
2574 if (prev) {
2575 size -= prev - modinfo;
2576 modinfo = next_string(prev, &size);
2579 for (p = modinfo; p; p = next_string(p, &size)) {
2580 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2581 return p + taglen + 1;
2583 return NULL;
2586 static char *get_modinfo(const struct load_info *info, const char *tag)
2588 return get_next_modinfo(info, tag, NULL);
2591 static void setup_modinfo(struct module *mod, struct load_info *info)
2593 struct module_attribute *attr;
2594 int i;
2596 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2597 if (attr->setup)
2598 attr->setup(mod, get_modinfo(info, attr->attr.name));
2602 static void free_modinfo(struct module *mod)
2604 struct module_attribute *attr;
2605 int i;
2607 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2608 if (attr->free)
2609 attr->free(mod);
2613 #ifdef CONFIG_KALLSYMS
2615 /* Lookup exported symbol in given range of kernel_symbols */
2616 static const struct kernel_symbol *lookup_exported_symbol(const char *name,
2617 const struct kernel_symbol *start,
2618 const struct kernel_symbol *stop)
2620 return bsearch(name, start, stop - start,
2621 sizeof(struct kernel_symbol), cmp_name);
2624 static int is_exported(const char *name, unsigned long value,
2625 const struct module *mod)
2627 const struct kernel_symbol *ks;
2628 if (!mod)
2629 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab);
2630 else
2631 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms);
2633 return ks != NULL && kernel_symbol_value(ks) == value;
2636 /* As per nm */
2637 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2639 const Elf_Shdr *sechdrs = info->sechdrs;
2641 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2642 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2643 return 'v';
2644 else
2645 return 'w';
2647 if (sym->st_shndx == SHN_UNDEF)
2648 return 'U';
2649 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
2650 return 'a';
2651 if (sym->st_shndx >= SHN_LORESERVE)
2652 return '?';
2653 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2654 return 't';
2655 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2656 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2657 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2658 return 'r';
2659 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2660 return 'g';
2661 else
2662 return 'd';
2664 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2665 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2666 return 's';
2667 else
2668 return 'b';
2670 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2671 ".debug")) {
2672 return 'n';
2674 return '?';
2677 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2678 unsigned int shnum, unsigned int pcpundx)
2680 const Elf_Shdr *sec;
2682 if (src->st_shndx == SHN_UNDEF
2683 || src->st_shndx >= shnum
2684 || !src->st_name)
2685 return false;
2687 #ifdef CONFIG_KALLSYMS_ALL
2688 if (src->st_shndx == pcpundx)
2689 return true;
2690 #endif
2692 sec = sechdrs + src->st_shndx;
2693 if (!(sec->sh_flags & SHF_ALLOC)
2694 #ifndef CONFIG_KALLSYMS_ALL
2695 || !(sec->sh_flags & SHF_EXECINSTR)
2696 #endif
2697 || (sec->sh_entsize & INIT_OFFSET_MASK))
2698 return false;
2700 return true;
2704 * We only allocate and copy the strings needed by the parts of symtab
2705 * we keep. This is simple, but has the effect of making multiple
2706 * copies of duplicates. We could be more sophisticated, see
2707 * linux-kernel thread starting with
2708 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2710 static void layout_symtab(struct module *mod, struct load_info *info)
2712 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2713 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2714 const Elf_Sym *src;
2715 unsigned int i, nsrc, ndst, strtab_size = 0;
2717 /* Put symbol section at end of init part of module. */
2718 symsect->sh_flags |= SHF_ALLOC;
2719 symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
2720 info->index.sym) | INIT_OFFSET_MASK;
2721 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2723 src = (void *)info->hdr + symsect->sh_offset;
2724 nsrc = symsect->sh_size / sizeof(*src);
2726 /* Compute total space required for the core symbols' strtab. */
2727 for (ndst = i = 0; i < nsrc; i++) {
2728 if (i == 0 || is_livepatch_module(mod) ||
2729 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2730 info->index.pcpu)) {
2731 strtab_size += strlen(&info->strtab[src[i].st_name])+1;
2732 ndst++;
2736 /* Append room for core symbols at end of core part. */
2737 info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
2738 info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
2739 mod->core_layout.size += strtab_size;
2740 info->core_typeoffs = mod->core_layout.size;
2741 mod->core_layout.size += ndst * sizeof(char);
2742 mod->core_layout.size = debug_align(mod->core_layout.size);
2744 /* Put string table section at end of init part of module. */
2745 strsect->sh_flags |= SHF_ALLOC;
2746 strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
2747 info->index.str) | INIT_OFFSET_MASK;
2748 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2750 /* We'll tack temporary mod_kallsyms on the end. */
2751 mod->init_layout.size = ALIGN(mod->init_layout.size,
2752 __alignof__(struct mod_kallsyms));
2753 info->mod_kallsyms_init_off = mod->init_layout.size;
2754 mod->init_layout.size += sizeof(struct mod_kallsyms);
2755 info->init_typeoffs = mod->init_layout.size;
2756 mod->init_layout.size += nsrc * sizeof(char);
2757 mod->init_layout.size = debug_align(mod->init_layout.size);
2761 * We use the full symtab and strtab which layout_symtab arranged to
2762 * be appended to the init section. Later we switch to the cut-down
2763 * core-only ones.
2765 static void add_kallsyms(struct module *mod, const struct load_info *info)
2767 unsigned int i, ndst;
2768 const Elf_Sym *src;
2769 Elf_Sym *dst;
2770 char *s;
2771 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2773 /* Set up to point into init section. */
2774 mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
2776 mod->kallsyms->symtab = (void *)symsec->sh_addr;
2777 mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2778 /* Make sure we get permanent strtab: don't use info->strtab. */
2779 mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2780 mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
2783 * Now populate the cut down core kallsyms for after init
2784 * and set types up while we still have access to sections.
2786 mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
2787 mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
2788 mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
2789 src = mod->kallsyms->symtab;
2790 for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
2791 mod->kallsyms->typetab[i] = elf_type(src + i, info);
2792 if (i == 0 || is_livepatch_module(mod) ||
2793 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
2794 info->index.pcpu)) {
2795 mod->core_kallsyms.typetab[ndst] =
2796 mod->kallsyms->typetab[i];
2797 dst[ndst] = src[i];
2798 dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
2799 s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
2800 KSYM_NAME_LEN) + 1;
2803 mod->core_kallsyms.num_symtab = ndst;
2805 #else
2806 static inline void layout_symtab(struct module *mod, struct load_info *info)
2810 static void add_kallsyms(struct module *mod, const struct load_info *info)
2813 #endif /* CONFIG_KALLSYMS */
2815 static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
2817 if (!debug)
2818 return;
2819 ddebug_add_module(debug, num, mod->name);
2822 static void dynamic_debug_remove(struct module *mod, struct _ddebug *debug)
2824 if (debug)
2825 ddebug_remove_module(mod->name);
2828 void * __weak module_alloc(unsigned long size)
2830 return vmalloc_exec(size);
2833 bool __weak module_exit_section(const char *name)
2835 return strstarts(name, ".exit");
2838 #ifdef CONFIG_DEBUG_KMEMLEAK
2839 static void kmemleak_load_module(const struct module *mod,
2840 const struct load_info *info)
2842 unsigned int i;
2844 /* only scan the sections containing data */
2845 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2847 for (i = 1; i < info->hdr->e_shnum; i++) {
2848 /* Scan all writable sections that's not executable */
2849 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
2850 !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
2851 (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
2852 continue;
2854 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2855 info->sechdrs[i].sh_size, GFP_KERNEL);
2858 #else
2859 static inline void kmemleak_load_module(const struct module *mod,
2860 const struct load_info *info)
2863 #endif
2865 #ifdef CONFIG_MODULE_SIG
2866 static int module_sig_check(struct load_info *info, int flags)
2868 int err = -ENODATA;
2869 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2870 const char *reason;
2871 const void *mod = info->hdr;
2874 * Require flags == 0, as a module with version information
2875 * removed is no longer the module that was signed
2877 if (flags == 0 &&
2878 info->len > markerlen &&
2879 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2880 /* We truncate the module to discard the signature */
2881 info->len -= markerlen;
2882 err = mod_verify_sig(mod, info);
2885 switch (err) {
2886 case 0:
2887 info->sig_ok = true;
2888 return 0;
2890 /* We don't permit modules to be loaded into trusted kernels
2891 * without a valid signature on them, but if we're not
2892 * enforcing, certain errors are non-fatal.
2894 case -ENODATA:
2895 reason = "Loading of unsigned module";
2896 goto decide;
2897 case -ENOPKG:
2898 reason = "Loading of module with unsupported crypto";
2899 goto decide;
2900 case -ENOKEY:
2901 reason = "Loading of module with unavailable key";
2902 decide:
2903 if (is_module_sig_enforced()) {
2904 pr_notice("%s is rejected\n", reason);
2905 return -EKEYREJECTED;
2908 return security_locked_down(LOCKDOWN_MODULE_SIGNATURE);
2910 /* All other errors are fatal, including nomem, unparseable
2911 * signatures and signature check failures - even if signatures
2912 * aren't required.
2914 default:
2915 return err;
2918 #else /* !CONFIG_MODULE_SIG */
2919 static int module_sig_check(struct load_info *info, int flags)
2921 return 0;
2923 #endif /* !CONFIG_MODULE_SIG */
2925 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2926 static int elf_header_check(struct load_info *info)
2928 if (info->len < sizeof(*(info->hdr)))
2929 return -ENOEXEC;
2931 if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
2932 || info->hdr->e_type != ET_REL
2933 || !elf_check_arch(info->hdr)
2934 || info->hdr->e_shentsize != sizeof(Elf_Shdr))
2935 return -ENOEXEC;
2937 if (info->hdr->e_shoff >= info->len
2938 || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
2939 info->len - info->hdr->e_shoff))
2940 return -ENOEXEC;
2942 return 0;
2945 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2947 static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
2949 do {
2950 unsigned long n = min(len, COPY_CHUNK_SIZE);
2952 if (copy_from_user(dst, usrc, n) != 0)
2953 return -EFAULT;
2954 cond_resched();
2955 dst += n;
2956 usrc += n;
2957 len -= n;
2958 } while (len);
2959 return 0;
2962 #ifdef CONFIG_LIVEPATCH
2963 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2965 if (get_modinfo(info, "livepatch")) {
2966 mod->klp = true;
2967 add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
2968 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2969 mod->name);
2972 return 0;
2974 #else /* !CONFIG_LIVEPATCH */
2975 static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
2977 if (get_modinfo(info, "livepatch")) {
2978 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2979 mod->name);
2980 return -ENOEXEC;
2983 return 0;
2985 #endif /* CONFIG_LIVEPATCH */
2987 static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
2989 if (retpoline_module_ok(get_modinfo(info, "retpoline")))
2990 return;
2992 pr_warn("%s: loading module not compiled with retpoline compiler.\n",
2993 mod->name);
2996 /* Sets info->hdr and info->len. */
2997 static int copy_module_from_user(const void __user *umod, unsigned long len,
2998 struct load_info *info)
3000 int err;
3002 info->len = len;
3003 if (info->len < sizeof(*(info->hdr)))
3004 return -ENOEXEC;
3006 err = security_kernel_load_data(LOADING_MODULE);
3007 if (err)
3008 return err;
3010 /* Suck in entire file: we'll want most of it. */
3011 info->hdr = __vmalloc(info->len,
3012 GFP_KERNEL | __GFP_NOWARN, PAGE_KERNEL);
3013 if (!info->hdr)
3014 return -ENOMEM;
3016 if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
3017 vfree(info->hdr);
3018 return -EFAULT;
3021 return 0;
3024 static void free_copy(struct load_info *info)
3026 vfree(info->hdr);
3029 static int rewrite_section_headers(struct load_info *info, int flags)
3031 unsigned int i;
3033 /* This should always be true, but let's be sure. */
3034 info->sechdrs[0].sh_addr = 0;
3036 for (i = 1; i < info->hdr->e_shnum; i++) {
3037 Elf_Shdr *shdr = &info->sechdrs[i];
3038 if (shdr->sh_type != SHT_NOBITS
3039 && info->len < shdr->sh_offset + shdr->sh_size) {
3040 pr_err("Module len %lu truncated\n", info->len);
3041 return -ENOEXEC;
3044 /* Mark all sections sh_addr with their address in the
3045 temporary image. */
3046 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
3048 #ifndef CONFIG_MODULE_UNLOAD
3049 /* Don't load .exit sections */
3050 if (module_exit_section(info->secstrings+shdr->sh_name))
3051 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
3052 #endif
3055 /* Track but don't keep modinfo and version sections. */
3056 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
3057 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
3059 return 0;
3063 * Set up our basic convenience variables (pointers to section headers,
3064 * search for module section index etc), and do some basic section
3065 * verification.
3067 * Set info->mod to the temporary copy of the module in info->hdr. The final one
3068 * will be allocated in move_module().
3070 static int setup_load_info(struct load_info *info, int flags)
3072 unsigned int i;
3074 /* Set up the convenience variables */
3075 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
3076 info->secstrings = (void *)info->hdr
3077 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
3079 /* Try to find a name early so we can log errors with a module name */
3080 info->index.info = find_sec(info, ".modinfo");
3081 if (info->index.info)
3082 info->name = get_modinfo(info, "name");
3084 /* Find internal symbols and strings. */
3085 for (i = 1; i < info->hdr->e_shnum; i++) {
3086 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
3087 info->index.sym = i;
3088 info->index.str = info->sechdrs[i].sh_link;
3089 info->strtab = (char *)info->hdr
3090 + info->sechdrs[info->index.str].sh_offset;
3091 break;
3095 if (info->index.sym == 0) {
3096 pr_warn("%s: module has no symbols (stripped?)\n",
3097 info->name ?: "(missing .modinfo section or name field)");
3098 return -ENOEXEC;
3101 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
3102 if (!info->index.mod) {
3103 pr_warn("%s: No module found in object\n",
3104 info->name ?: "(missing .modinfo section or name field)");
3105 return -ENOEXEC;
3107 /* This is temporary: point mod into copy of data. */
3108 info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
3111 * If we didn't load the .modinfo 'name' field earlier, fall back to
3112 * on-disk struct mod 'name' field.
3114 if (!info->name)
3115 info->name = info->mod->name;
3117 if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
3118 info->index.vers = 0; /* Pretend no __versions section! */
3119 else
3120 info->index.vers = find_sec(info, "__versions");
3122 info->index.pcpu = find_pcpusec(info);
3124 return 0;
3127 static int check_modinfo(struct module *mod, struct load_info *info, int flags)
3129 const char *modmagic = get_modinfo(info, "vermagic");
3130 int err;
3132 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
3133 modmagic = NULL;
3135 /* This is allowed: modprobe --force will invalidate it. */
3136 if (!modmagic) {
3137 err = try_to_force_load(mod, "bad vermagic");
3138 if (err)
3139 return err;
3140 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
3141 pr_err("%s: version magic '%s' should be '%s'\n",
3142 info->name, modmagic, vermagic);
3143 return -ENOEXEC;
3146 if (!get_modinfo(info, "intree")) {
3147 if (!test_taint(TAINT_OOT_MODULE))
3148 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3149 mod->name);
3150 add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
3153 check_modinfo_retpoline(mod, info);
3155 if (get_modinfo(info, "staging")) {
3156 add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
3157 pr_warn("%s: module is from the staging directory, the quality "
3158 "is unknown, you have been warned.\n", mod->name);
3161 err = check_modinfo_livepatch(mod, info);
3162 if (err)
3163 return err;
3165 /* Set up license info based on the info section */
3166 set_license(mod, get_modinfo(info, "license"));
3168 return 0;
3171 static int find_module_sections(struct module *mod, struct load_info *info)
3173 mod->kp = section_objs(info, "__param",
3174 sizeof(*mod->kp), &mod->num_kp);
3175 mod->syms = section_objs(info, "__ksymtab",
3176 sizeof(*mod->syms), &mod->num_syms);
3177 mod->crcs = section_addr(info, "__kcrctab");
3178 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
3179 sizeof(*mod->gpl_syms),
3180 &mod->num_gpl_syms);
3181 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
3182 mod->gpl_future_syms = section_objs(info,
3183 "__ksymtab_gpl_future",
3184 sizeof(*mod->gpl_future_syms),
3185 &mod->num_gpl_future_syms);
3186 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
3188 #ifdef CONFIG_UNUSED_SYMBOLS
3189 mod->unused_syms = section_objs(info, "__ksymtab_unused",
3190 sizeof(*mod->unused_syms),
3191 &mod->num_unused_syms);
3192 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
3193 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
3194 sizeof(*mod->unused_gpl_syms),
3195 &mod->num_unused_gpl_syms);
3196 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
3197 #endif
3198 #ifdef CONFIG_CONSTRUCTORS
3199 mod->ctors = section_objs(info, ".ctors",
3200 sizeof(*mod->ctors), &mod->num_ctors);
3201 if (!mod->ctors)
3202 mod->ctors = section_objs(info, ".init_array",
3203 sizeof(*mod->ctors), &mod->num_ctors);
3204 else if (find_sec(info, ".init_array")) {
3206 * This shouldn't happen with same compiler and binutils
3207 * building all parts of the module.
3209 pr_warn("%s: has both .ctors and .init_array.\n",
3210 mod->name);
3211 return -EINVAL;
3213 #endif
3215 #ifdef CONFIG_TRACEPOINTS
3216 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
3217 sizeof(*mod->tracepoints_ptrs),
3218 &mod->num_tracepoints);
3219 #endif
3220 #ifdef CONFIG_TREE_SRCU
3221 mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
3222 sizeof(*mod->srcu_struct_ptrs),
3223 &mod->num_srcu_structs);
3224 #endif
3225 #ifdef CONFIG_BPF_EVENTS
3226 mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
3227 sizeof(*mod->bpf_raw_events),
3228 &mod->num_bpf_raw_events);
3229 #endif
3230 #ifdef CONFIG_JUMP_LABEL
3231 mod->jump_entries = section_objs(info, "__jump_table",
3232 sizeof(*mod->jump_entries),
3233 &mod->num_jump_entries);
3234 #endif
3235 #ifdef CONFIG_EVENT_TRACING
3236 mod->trace_events = section_objs(info, "_ftrace_events",
3237 sizeof(*mod->trace_events),
3238 &mod->num_trace_events);
3239 mod->trace_evals = section_objs(info, "_ftrace_eval_map",
3240 sizeof(*mod->trace_evals),
3241 &mod->num_trace_evals);
3242 #endif
3243 #ifdef CONFIG_TRACING
3244 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
3245 sizeof(*mod->trace_bprintk_fmt_start),
3246 &mod->num_trace_bprintk_fmt);
3247 #endif
3248 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3249 /* sechdrs[0].sh_size is always zero */
3250 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
3251 sizeof(*mod->ftrace_callsites),
3252 &mod->num_ftrace_callsites);
3253 #endif
3254 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
3255 mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
3256 sizeof(*mod->ei_funcs),
3257 &mod->num_ei_funcs);
3258 #endif
3259 mod->extable = section_objs(info, "__ex_table",
3260 sizeof(*mod->extable), &mod->num_exentries);
3262 if (section_addr(info, "__obsparm"))
3263 pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
3265 info->debug = section_objs(info, "__verbose",
3266 sizeof(*info->debug), &info->num_debug);
3268 return 0;
3271 static int move_module(struct module *mod, struct load_info *info)
3273 int i;
3274 void *ptr;
3276 /* Do the allocs. */
3277 ptr = module_alloc(mod->core_layout.size);
3279 * The pointer to this block is stored in the module structure
3280 * which is inside the block. Just mark it as not being a
3281 * leak.
3283 kmemleak_not_leak(ptr);
3284 if (!ptr)
3285 return -ENOMEM;
3287 memset(ptr, 0, mod->core_layout.size);
3288 mod->core_layout.base = ptr;
3290 if (mod->init_layout.size) {
3291 ptr = module_alloc(mod->init_layout.size);
3293 * The pointer to this block is stored in the module structure
3294 * which is inside the block. This block doesn't need to be
3295 * scanned as it contains data and code that will be freed
3296 * after the module is initialized.
3298 kmemleak_ignore(ptr);
3299 if (!ptr) {
3300 module_memfree(mod->core_layout.base);
3301 return -ENOMEM;
3303 memset(ptr, 0, mod->init_layout.size);
3304 mod->init_layout.base = ptr;
3305 } else
3306 mod->init_layout.base = NULL;
3308 /* Transfer each section which specifies SHF_ALLOC */
3309 pr_debug("final section addresses:\n");
3310 for (i = 0; i < info->hdr->e_shnum; i++) {
3311 void *dest;
3312 Elf_Shdr *shdr = &info->sechdrs[i];
3314 if (!(shdr->sh_flags & SHF_ALLOC))
3315 continue;
3317 if (shdr->sh_entsize & INIT_OFFSET_MASK)
3318 dest = mod->init_layout.base
3319 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
3320 else
3321 dest = mod->core_layout.base + shdr->sh_entsize;
3323 if (shdr->sh_type != SHT_NOBITS)
3324 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
3325 /* Update sh_addr to point to copy in image. */
3326 shdr->sh_addr = (unsigned long)dest;
3327 pr_debug("\t0x%lx %s\n",
3328 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
3331 return 0;
3334 static int check_module_license_and_versions(struct module *mod)
3336 int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
3339 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3340 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3341 * using GPL-only symbols it needs.
3343 if (strcmp(mod->name, "ndiswrapper") == 0)
3344 add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
3346 /* driverloader was caught wrongly pretending to be under GPL */
3347 if (strcmp(mod->name, "driverloader") == 0)
3348 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3349 LOCKDEP_NOW_UNRELIABLE);
3351 /* lve claims to be GPL but upstream won't provide source */
3352 if (strcmp(mod->name, "lve") == 0)
3353 add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
3354 LOCKDEP_NOW_UNRELIABLE);
3356 if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
3357 pr_warn("%s: module license taints kernel.\n", mod->name);
3359 #ifdef CONFIG_MODVERSIONS
3360 if ((mod->num_syms && !mod->crcs)
3361 || (mod->num_gpl_syms && !mod->gpl_crcs)
3362 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
3363 #ifdef CONFIG_UNUSED_SYMBOLS
3364 || (mod->num_unused_syms && !mod->unused_crcs)
3365 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
3366 #endif
3368 return try_to_force_load(mod,
3369 "no versions for exported symbols");
3371 #endif
3372 return 0;
3375 static void flush_module_icache(const struct module *mod)
3377 mm_segment_t old_fs;
3379 /* flush the icache in correct context */
3380 old_fs = get_fs();
3381 set_fs(KERNEL_DS);
3384 * Flush the instruction cache, since we've played with text.
3385 * Do it before processing of module parameters, so the module
3386 * can provide parameter accessor functions of its own.
3388 if (mod->init_layout.base)
3389 flush_icache_range((unsigned long)mod->init_layout.base,
3390 (unsigned long)mod->init_layout.base
3391 + mod->init_layout.size);
3392 flush_icache_range((unsigned long)mod->core_layout.base,
3393 (unsigned long)mod->core_layout.base + mod->core_layout.size);
3395 set_fs(old_fs);
3398 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
3399 Elf_Shdr *sechdrs,
3400 char *secstrings,
3401 struct module *mod)
3403 return 0;
3406 /* module_blacklist is a comma-separated list of module names */
3407 static char *module_blacklist;
3408 static bool blacklisted(const char *module_name)
3410 const char *p;
3411 size_t len;
3413 if (!module_blacklist)
3414 return false;
3416 for (p = module_blacklist; *p; p += len) {
3417 len = strcspn(p, ",");
3418 if (strlen(module_name) == len && !memcmp(module_name, p, len))
3419 return true;
3420 if (p[len] == ',')
3421 len++;
3423 return false;
3425 core_param(module_blacklist, module_blacklist, charp, 0400);
3427 static struct module *layout_and_allocate(struct load_info *info, int flags)
3429 struct module *mod;
3430 unsigned int ndx;
3431 int err;
3433 err = check_modinfo(info->mod, info, flags);
3434 if (err)
3435 return ERR_PTR(err);
3437 /* Allow arches to frob section contents and sizes. */
3438 err = module_frob_arch_sections(info->hdr, info->sechdrs,
3439 info->secstrings, info->mod);
3440 if (err < 0)
3441 return ERR_PTR(err);
3443 /* We will do a special allocation for per-cpu sections later. */
3444 info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
3447 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3448 * layout_sections() can put it in the right place.
3449 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3451 ndx = find_sec(info, ".data..ro_after_init");
3452 if (ndx)
3453 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3455 * Mark the __jump_table section as ro_after_init as well: these data
3456 * structures are never modified, with the exception of entries that
3457 * refer to code in the __init section, which are annotated as such
3458 * at module load time.
3460 ndx = find_sec(info, "__jump_table");
3461 if (ndx)
3462 info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
3464 /* Determine total sizes, and put offsets in sh_entsize. For now
3465 this is done generically; there doesn't appear to be any
3466 special cases for the architectures. */
3467 layout_sections(info->mod, info);
3468 layout_symtab(info->mod, info);
3470 /* Allocate and move to the final place */
3471 err = move_module(info->mod, info);
3472 if (err)
3473 return ERR_PTR(err);
3475 /* Module has been copied to its final place now: return it. */
3476 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
3477 kmemleak_load_module(mod, info);
3478 return mod;
3481 /* mod is no longer valid after this! */
3482 static void module_deallocate(struct module *mod, struct load_info *info)
3484 percpu_modfree(mod);
3485 module_arch_freeing_init(mod);
3486 module_memfree(mod->init_layout.base);
3487 module_memfree(mod->core_layout.base);
3490 int __weak module_finalize(const Elf_Ehdr *hdr,
3491 const Elf_Shdr *sechdrs,
3492 struct module *me)
3494 return 0;
3497 static int post_relocation(struct module *mod, const struct load_info *info)
3499 /* Sort exception table now relocations are done. */
3500 sort_extable(mod->extable, mod->extable + mod->num_exentries);
3502 /* Copy relocated percpu area over. */
3503 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
3504 info->sechdrs[info->index.pcpu].sh_size);
3506 /* Setup kallsyms-specific fields. */
3507 add_kallsyms(mod, info);
3509 /* Arch-specific module finalizing. */
3510 return module_finalize(info->hdr, info->sechdrs, mod);
3513 /* Is this module of this name done loading? No locks held. */
3514 static bool finished_loading(const char *name)
3516 struct module *mod;
3517 bool ret;
3520 * The module_mutex should not be a heavily contended lock;
3521 * if we get the occasional sleep here, we'll go an extra iteration
3522 * in the wait_event_interruptible(), which is harmless.
3524 sched_annotate_sleep();
3525 mutex_lock(&module_mutex);
3526 mod = find_module_all(name, strlen(name), true);
3527 ret = !mod || mod->state == MODULE_STATE_LIVE;
3528 mutex_unlock(&module_mutex);
3530 return ret;
3533 /* Call module constructors. */
3534 static void do_mod_ctors(struct module *mod)
3536 #ifdef CONFIG_CONSTRUCTORS
3537 unsigned long i;
3539 for (i = 0; i < mod->num_ctors; i++)
3540 mod->ctors[i]();
3541 #endif
3544 /* For freeing module_init on success, in case kallsyms traversing */
3545 struct mod_initfree {
3546 struct llist_node node;
3547 void *module_init;
3550 static void do_free_init(struct work_struct *w)
3552 struct llist_node *pos, *n, *list;
3553 struct mod_initfree *initfree;
3555 list = llist_del_all(&init_free_list);
3557 synchronize_rcu();
3559 llist_for_each_safe(pos, n, list) {
3560 initfree = container_of(pos, struct mod_initfree, node);
3561 module_memfree(initfree->module_init);
3562 kfree(initfree);
3566 static int __init modules_wq_init(void)
3568 INIT_WORK(&init_free_wq, do_free_init);
3569 init_llist_head(&init_free_list);
3570 return 0;
3572 module_init(modules_wq_init);
3575 * This is where the real work happens.
3577 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3578 * helper command 'lx-symbols'.
3580 static noinline int do_init_module(struct module *mod)
3582 int ret = 0;
3583 struct mod_initfree *freeinit;
3585 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
3586 if (!freeinit) {
3587 ret = -ENOMEM;
3588 goto fail;
3590 freeinit->module_init = mod->init_layout.base;
3593 * We want to find out whether @mod uses async during init. Clear
3594 * PF_USED_ASYNC. async_schedule*() will set it.
3596 current->flags &= ~PF_USED_ASYNC;
3598 do_mod_ctors(mod);
3599 /* Start the module */
3600 if (mod->init != NULL)
3601 ret = do_one_initcall(mod->init);
3602 if (ret < 0) {
3603 goto fail_free_freeinit;
3605 if (ret > 0) {
3606 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3607 "follow 0/-E convention\n"
3608 "%s: loading module anyway...\n",
3609 __func__, mod->name, ret, __func__);
3610 dump_stack();
3613 /* Now it's a first class citizen! */
3614 mod->state = MODULE_STATE_LIVE;
3615 blocking_notifier_call_chain(&module_notify_list,
3616 MODULE_STATE_LIVE, mod);
3619 * We need to finish all async code before the module init sequence
3620 * is done. This has potential to deadlock. For example, a newly
3621 * detected block device can trigger request_module() of the
3622 * default iosched from async probing task. Once userland helper
3623 * reaches here, async_synchronize_full() will wait on the async
3624 * task waiting on request_module() and deadlock.
3626 * This deadlock is avoided by perfomring async_synchronize_full()
3627 * iff module init queued any async jobs. This isn't a full
3628 * solution as it will deadlock the same if module loading from
3629 * async jobs nests more than once; however, due to the various
3630 * constraints, this hack seems to be the best option for now.
3631 * Please refer to the following thread for details.
3633 * http://thread.gmane.org/gmane.linux.kernel/1420814
3635 if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
3636 async_synchronize_full();
3638 ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
3639 mod->init_layout.size);
3640 mutex_lock(&module_mutex);
3641 /* Drop initial reference. */
3642 module_put(mod);
3643 trim_init_extable(mod);
3644 #ifdef CONFIG_KALLSYMS
3645 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3646 rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
3647 #endif
3648 module_enable_ro(mod, true);
3649 mod_tree_remove_init(mod);
3650 module_arch_freeing_init(mod);
3651 mod->init_layout.base = NULL;
3652 mod->init_layout.size = 0;
3653 mod->init_layout.ro_size = 0;
3654 mod->init_layout.ro_after_init_size = 0;
3655 mod->init_layout.text_size = 0;
3657 * We want to free module_init, but be aware that kallsyms may be
3658 * walking this with preempt disabled. In all the failure paths, we
3659 * call synchronize_rcu(), but we don't want to slow down the success
3660 * path. module_memfree() cannot be called in an interrupt, so do the
3661 * work and call synchronize_rcu() in a work queue.
3663 * Note that module_alloc() on most architectures creates W+X page
3664 * mappings which won't be cleaned up until do_free_init() runs. Any
3665 * code such as mark_rodata_ro() which depends on those mappings to
3666 * be cleaned up needs to sync with the queued work - ie
3667 * rcu_barrier()
3669 if (llist_add(&freeinit->node, &init_free_list))
3670 schedule_work(&init_free_wq);
3672 mutex_unlock(&module_mutex);
3673 wake_up_all(&module_wq);
3675 return 0;
3677 fail_free_freeinit:
3678 kfree(freeinit);
3679 fail:
3680 /* Try to protect us from buggy refcounters. */
3681 mod->state = MODULE_STATE_GOING;
3682 synchronize_rcu();
3683 module_put(mod);
3684 blocking_notifier_call_chain(&module_notify_list,
3685 MODULE_STATE_GOING, mod);
3686 klp_module_going(mod);
3687 ftrace_release_mod(mod);
3688 free_module(mod);
3689 wake_up_all(&module_wq);
3690 return ret;
3693 static int may_init_module(void)
3695 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3696 return -EPERM;
3698 return 0;
3702 * We try to place it in the list now to make sure it's unique before
3703 * we dedicate too many resources. In particular, temporary percpu
3704 * memory exhaustion.
3706 static int add_unformed_module(struct module *mod)
3708 int err;
3709 struct module *old;
3711 mod->state = MODULE_STATE_UNFORMED;
3713 again:
3714 mutex_lock(&module_mutex);
3715 old = find_module_all(mod->name, strlen(mod->name), true);
3716 if (old != NULL) {
3717 if (old->state != MODULE_STATE_LIVE) {
3718 /* Wait in case it fails to load. */
3719 mutex_unlock(&module_mutex);
3720 err = wait_event_interruptible(module_wq,
3721 finished_loading(mod->name));
3722 if (err)
3723 goto out_unlocked;
3724 goto again;
3726 err = -EEXIST;
3727 goto out;
3729 mod_update_bounds(mod);
3730 list_add_rcu(&mod->list, &modules);
3731 mod_tree_insert(mod);
3732 err = 0;
3734 out:
3735 mutex_unlock(&module_mutex);
3736 out_unlocked:
3737 return err;
3740 static int complete_formation(struct module *mod, struct load_info *info)
3742 int err;
3744 mutex_lock(&module_mutex);
3746 /* Find duplicate symbols (must be called under lock). */
3747 err = verify_exported_symbols(mod);
3748 if (err < 0)
3749 goto out;
3751 /* This relies on module_mutex for list integrity. */
3752 module_bug_finalize(info->hdr, info->sechdrs, mod);
3754 module_enable_ro(mod, false);
3755 module_enable_nx(mod);
3756 module_enable_x(mod);
3758 /* Mark state as coming so strong_try_module_get() ignores us,
3759 * but kallsyms etc. can see us. */
3760 mod->state = MODULE_STATE_COMING;
3761 mutex_unlock(&module_mutex);
3763 return 0;
3765 out:
3766 mutex_unlock(&module_mutex);
3767 return err;
3770 static int prepare_coming_module(struct module *mod)
3772 int err;
3774 ftrace_module_enable(mod);
3775 err = klp_module_coming(mod);
3776 if (err)
3777 return err;
3779 blocking_notifier_call_chain(&module_notify_list,
3780 MODULE_STATE_COMING, mod);
3781 return 0;
3784 static int unknown_module_param_cb(char *param, char *val, const char *modname,
3785 void *arg)
3787 struct module *mod = arg;
3788 int ret;
3790 if (strcmp(param, "async_probe") == 0) {
3791 mod->async_probe_requested = true;
3792 return 0;
3795 /* Check for magic 'dyndbg' arg */
3796 ret = ddebug_dyndbg_module_param_cb(param, val, modname);
3797 if (ret != 0)
3798 pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
3799 return 0;
3802 /* Allocate and load the module: note that size of section 0 is always
3803 zero, and we rely on this for optional sections. */
3804 static int load_module(struct load_info *info, const char __user *uargs,
3805 int flags)
3807 struct module *mod;
3808 long err = 0;
3809 char *after_dashes;
3811 err = elf_header_check(info);
3812 if (err)
3813 goto free_copy;
3815 err = setup_load_info(info, flags);
3816 if (err)
3817 goto free_copy;
3819 if (blacklisted(info->name)) {
3820 err = -EPERM;
3821 goto free_copy;
3824 err = module_sig_check(info, flags);
3825 if (err)
3826 goto free_copy;
3828 err = rewrite_section_headers(info, flags);
3829 if (err)
3830 goto free_copy;
3832 /* Check module struct version now, before we try to use module. */
3833 if (!check_modstruct_version(info, info->mod)) {
3834 err = -ENOEXEC;
3835 goto free_copy;
3838 /* Figure out module layout, and allocate all the memory. */
3839 mod = layout_and_allocate(info, flags);
3840 if (IS_ERR(mod)) {
3841 err = PTR_ERR(mod);
3842 goto free_copy;
3845 audit_log_kern_module(mod->name);
3847 /* Reserve our place in the list. */
3848 err = add_unformed_module(mod);
3849 if (err)
3850 goto free_module;
3852 #ifdef CONFIG_MODULE_SIG
3853 mod->sig_ok = info->sig_ok;
3854 if (!mod->sig_ok) {
3855 pr_notice_once("%s: module verification failed: signature "
3856 "and/or required key missing - tainting "
3857 "kernel\n", mod->name);
3858 add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
3860 #endif
3862 /* To avoid stressing percpu allocator, do this once we're unique. */
3863 err = percpu_modalloc(mod, info);
3864 if (err)
3865 goto unlink_mod;
3867 /* Now module is in final location, initialize linked lists, etc. */
3868 err = module_unload_init(mod);
3869 if (err)
3870 goto unlink_mod;
3872 init_param_lock(mod);
3874 /* Now we've got everything in the final locations, we can
3875 * find optional sections. */
3876 err = find_module_sections(mod, info);
3877 if (err)
3878 goto free_unload;
3880 err = check_module_license_and_versions(mod);
3881 if (err)
3882 goto free_unload;
3884 /* Set up MODINFO_ATTR fields */
3885 setup_modinfo(mod, info);
3887 /* Fix up syms, so that st_value is a pointer to location. */
3888 err = simplify_symbols(mod, info);
3889 if (err < 0)
3890 goto free_modinfo;
3892 err = apply_relocations(mod, info);
3893 if (err < 0)
3894 goto free_modinfo;
3896 err = post_relocation(mod, info);
3897 if (err < 0)
3898 goto free_modinfo;
3900 flush_module_icache(mod);
3902 /* Now copy in args */
3903 mod->args = strndup_user(uargs, ~0UL >> 1);
3904 if (IS_ERR(mod->args)) {
3905 err = PTR_ERR(mod->args);
3906 goto free_arch_cleanup;
3909 dynamic_debug_setup(mod, info->debug, info->num_debug);
3911 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3912 ftrace_module_init(mod);
3914 /* Finally it's fully formed, ready to start executing. */
3915 err = complete_formation(mod, info);
3916 if (err)
3917 goto ddebug_cleanup;
3919 err = prepare_coming_module(mod);
3920 if (err)
3921 goto bug_cleanup;
3923 /* Module is ready to execute: parsing args may do that. */
3924 after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3925 -32768, 32767, mod,
3926 unknown_module_param_cb);
3927 if (IS_ERR(after_dashes)) {
3928 err = PTR_ERR(after_dashes);
3929 goto coming_cleanup;
3930 } else if (after_dashes) {
3931 pr_warn("%s: parameters '%s' after `--' ignored\n",
3932 mod->name, after_dashes);
3935 /* Link in to sysfs. */
3936 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3937 if (err < 0)
3938 goto coming_cleanup;
3940 if (is_livepatch_module(mod)) {
3941 err = copy_module_elf(mod, info);
3942 if (err < 0)
3943 goto sysfs_cleanup;
3946 /* Get rid of temporary copy. */
3947 free_copy(info);
3949 /* Done! */
3950 trace_module_load(mod);
3952 return do_init_module(mod);
3954 sysfs_cleanup:
3955 mod_sysfs_teardown(mod);
3956 coming_cleanup:
3957 mod->state = MODULE_STATE_GOING;
3958 destroy_params(mod->kp, mod->num_kp);
3959 blocking_notifier_call_chain(&module_notify_list,
3960 MODULE_STATE_GOING, mod);
3961 klp_module_going(mod);
3962 bug_cleanup:
3963 /* module_bug_cleanup needs module_mutex protection */
3964 mutex_lock(&module_mutex);
3965 module_bug_cleanup(mod);
3966 mutex_unlock(&module_mutex);
3968 ddebug_cleanup:
3969 ftrace_release_mod(mod);
3970 dynamic_debug_remove(mod, info->debug);
3971 synchronize_rcu();
3972 kfree(mod->args);
3973 free_arch_cleanup:
3974 module_arch_cleanup(mod);
3975 free_modinfo:
3976 free_modinfo(mod);
3977 free_unload:
3978 module_unload_free(mod);
3979 unlink_mod:
3980 mutex_lock(&module_mutex);
3981 /* Unlink carefully: kallsyms could be walking list. */
3982 list_del_rcu(&mod->list);
3983 mod_tree_remove(mod);
3984 wake_up_all(&module_wq);
3985 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3986 synchronize_rcu();
3987 mutex_unlock(&module_mutex);
3988 free_module:
3989 /* Free lock-classes; relies on the preceding sync_rcu() */
3990 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
3992 module_deallocate(mod, info);
3993 free_copy:
3994 free_copy(info);
3995 return err;
3998 SYSCALL_DEFINE3(init_module, void __user *, umod,
3999 unsigned long, len, const char __user *, uargs)
4001 int err;
4002 struct load_info info = { };
4004 err = may_init_module();
4005 if (err)
4006 return err;
4008 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
4009 umod, len, uargs);
4011 err = copy_module_from_user(umod, len, &info);
4012 if (err)
4013 return err;
4015 return load_module(&info, uargs, 0);
4018 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
4020 struct load_info info = { };
4021 loff_t size;
4022 void *hdr;
4023 int err;
4025 err = may_init_module();
4026 if (err)
4027 return err;
4029 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
4031 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
4032 |MODULE_INIT_IGNORE_VERMAGIC))
4033 return -EINVAL;
4035 err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
4036 READING_MODULE);
4037 if (err)
4038 return err;
4039 info.hdr = hdr;
4040 info.len = size;
4042 return load_module(&info, uargs, flags);
4045 static inline int within(unsigned long addr, void *start, unsigned long size)
4047 return ((void *)addr >= start && (void *)addr < start + size);
4050 #ifdef CONFIG_KALLSYMS
4052 * This ignores the intensely annoying "mapping symbols" found
4053 * in ARM ELF files: $a, $t and $d.
4055 static inline int is_arm_mapping_symbol(const char *str)
4057 if (str[0] == '.' && str[1] == 'L')
4058 return true;
4059 return str[0] == '$' && strchr("axtd", str[1])
4060 && (str[2] == '\0' || str[2] == '.');
4063 static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum)
4065 return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
4069 * Given a module and address, find the corresponding symbol and return its name
4070 * while providing its size and offset if needed.
4072 static const char *find_kallsyms_symbol(struct module *mod,
4073 unsigned long addr,
4074 unsigned long *size,
4075 unsigned long *offset)
4077 unsigned int i, best = 0;
4078 unsigned long nextval, bestval;
4079 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4081 /* At worse, next value is at end of module */
4082 if (within_module_init(addr, mod))
4083 nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
4084 else
4085 nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
4087 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]);
4089 /* Scan for closest preceding symbol, and next symbol. (ELF
4090 starts real symbols at 1). */
4091 for (i = 1; i < kallsyms->num_symtab; i++) {
4092 const Elf_Sym *sym = &kallsyms->symtab[i];
4093 unsigned long thisval = kallsyms_symbol_value(sym);
4095 if (sym->st_shndx == SHN_UNDEF)
4096 continue;
4098 /* We ignore unnamed symbols: they're uninformative
4099 * and inserted at a whim. */
4100 if (*kallsyms_symbol_name(kallsyms, i) == '\0'
4101 || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i)))
4102 continue;
4104 if (thisval <= addr && thisval > bestval) {
4105 best = i;
4106 bestval = thisval;
4108 if (thisval > addr && thisval < nextval)
4109 nextval = thisval;
4112 if (!best)
4113 return NULL;
4115 if (size)
4116 *size = nextval - bestval;
4117 if (offset)
4118 *offset = addr - bestval;
4120 return kallsyms_symbol_name(kallsyms, best);
4123 void * __weak dereference_module_function_descriptor(struct module *mod,
4124 void *ptr)
4126 return ptr;
4129 /* For kallsyms to ask for address resolution. NULL means not found. Careful
4130 * not to lock to avoid deadlock on oopses, simply disable preemption. */
4131 const char *module_address_lookup(unsigned long addr,
4132 unsigned long *size,
4133 unsigned long *offset,
4134 char **modname,
4135 char *namebuf)
4137 const char *ret = NULL;
4138 struct module *mod;
4140 preempt_disable();
4141 mod = __module_address(addr);
4142 if (mod) {
4143 if (modname)
4144 *modname = mod->name;
4146 ret = find_kallsyms_symbol(mod, addr, size, offset);
4148 /* Make a copy in here where it's safe */
4149 if (ret) {
4150 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
4151 ret = namebuf;
4153 preempt_enable();
4155 return ret;
4158 int lookup_module_symbol_name(unsigned long addr, char *symname)
4160 struct module *mod;
4162 preempt_disable();
4163 list_for_each_entry_rcu(mod, &modules, list) {
4164 if (mod->state == MODULE_STATE_UNFORMED)
4165 continue;
4166 if (within_module(addr, mod)) {
4167 const char *sym;
4169 sym = find_kallsyms_symbol(mod, addr, NULL, NULL);
4170 if (!sym)
4171 goto out;
4173 strlcpy(symname, sym, KSYM_NAME_LEN);
4174 preempt_enable();
4175 return 0;
4178 out:
4179 preempt_enable();
4180 return -ERANGE;
4183 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
4184 unsigned long *offset, char *modname, char *name)
4186 struct module *mod;
4188 preempt_disable();
4189 list_for_each_entry_rcu(mod, &modules, list) {
4190 if (mod->state == MODULE_STATE_UNFORMED)
4191 continue;
4192 if (within_module(addr, mod)) {
4193 const char *sym;
4195 sym = find_kallsyms_symbol(mod, addr, size, offset);
4196 if (!sym)
4197 goto out;
4198 if (modname)
4199 strlcpy(modname, mod->name, MODULE_NAME_LEN);
4200 if (name)
4201 strlcpy(name, sym, KSYM_NAME_LEN);
4202 preempt_enable();
4203 return 0;
4206 out:
4207 preempt_enable();
4208 return -ERANGE;
4211 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
4212 char *name, char *module_name, int *exported)
4214 struct module *mod;
4216 preempt_disable();
4217 list_for_each_entry_rcu(mod, &modules, list) {
4218 struct mod_kallsyms *kallsyms;
4220 if (mod->state == MODULE_STATE_UNFORMED)
4221 continue;
4222 kallsyms = rcu_dereference_sched(mod->kallsyms);
4223 if (symnum < kallsyms->num_symtab) {
4224 const Elf_Sym *sym = &kallsyms->symtab[symnum];
4226 *value = kallsyms_symbol_value(sym);
4227 *type = kallsyms->typetab[symnum];
4228 strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN);
4229 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
4230 *exported = is_exported(name, *value, mod);
4231 preempt_enable();
4232 return 0;
4234 symnum -= kallsyms->num_symtab;
4236 preempt_enable();
4237 return -ERANGE;
4240 /* Given a module and name of symbol, find and return the symbol's value */
4241 static unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name)
4243 unsigned int i;
4244 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
4246 for (i = 0; i < kallsyms->num_symtab; i++) {
4247 const Elf_Sym *sym = &kallsyms->symtab[i];
4249 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 &&
4250 sym->st_shndx != SHN_UNDEF)
4251 return kallsyms_symbol_value(sym);
4253 return 0;
4256 /* Look for this name: can be of form module:name. */
4257 unsigned long module_kallsyms_lookup_name(const char *name)
4259 struct module *mod;
4260 char *colon;
4261 unsigned long ret = 0;
4263 /* Don't lock: we're in enough trouble already. */
4264 preempt_disable();
4265 if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
4266 if ((mod = find_module_all(name, colon - name, false)) != NULL)
4267 ret = find_kallsyms_symbol_value(mod, colon+1);
4268 } else {
4269 list_for_each_entry_rcu(mod, &modules, list) {
4270 if (mod->state == MODULE_STATE_UNFORMED)
4271 continue;
4272 if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)
4273 break;
4276 preempt_enable();
4277 return ret;
4280 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
4281 struct module *, unsigned long),
4282 void *data)
4284 struct module *mod;
4285 unsigned int i;
4286 int ret;
4288 module_assert_mutex();
4290 list_for_each_entry(mod, &modules, list) {
4291 /* We hold module_mutex: no need for rcu_dereference_sched */
4292 struct mod_kallsyms *kallsyms = mod->kallsyms;
4294 if (mod->state == MODULE_STATE_UNFORMED)
4295 continue;
4296 for (i = 0; i < kallsyms->num_symtab; i++) {
4297 const Elf_Sym *sym = &kallsyms->symtab[i];
4299 if (sym->st_shndx == SHN_UNDEF)
4300 continue;
4302 ret = fn(data, kallsyms_symbol_name(kallsyms, i),
4303 mod, kallsyms_symbol_value(sym));
4304 if (ret != 0)
4305 return ret;
4308 return 0;
4310 #endif /* CONFIG_KALLSYMS */
4312 /* Maximum number of characters written by module_flags() */
4313 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4315 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4316 static char *module_flags(struct module *mod, char *buf)
4318 int bx = 0;
4320 BUG_ON(mod->state == MODULE_STATE_UNFORMED);
4321 if (mod->taints ||
4322 mod->state == MODULE_STATE_GOING ||
4323 mod->state == MODULE_STATE_COMING) {
4324 buf[bx++] = '(';
4325 bx += module_flags_taint(mod, buf + bx);
4326 /* Show a - for module-is-being-unloaded */
4327 if (mod->state == MODULE_STATE_GOING)
4328 buf[bx++] = '-';
4329 /* Show a + for module-is-being-loaded */
4330 if (mod->state == MODULE_STATE_COMING)
4331 buf[bx++] = '+';
4332 buf[bx++] = ')';
4334 buf[bx] = '\0';
4336 return buf;
4339 #ifdef CONFIG_PROC_FS
4340 /* Called by the /proc file system to return a list of modules. */
4341 static void *m_start(struct seq_file *m, loff_t *pos)
4343 mutex_lock(&module_mutex);
4344 return seq_list_start(&modules, *pos);
4347 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
4349 return seq_list_next(p, &modules, pos);
4352 static void m_stop(struct seq_file *m, void *p)
4354 mutex_unlock(&module_mutex);
4357 static int m_show(struct seq_file *m, void *p)
4359 struct module *mod = list_entry(p, struct module, list);
4360 char buf[MODULE_FLAGS_BUF_SIZE];
4361 void *value;
4363 /* We always ignore unformed modules. */
4364 if (mod->state == MODULE_STATE_UNFORMED)
4365 return 0;
4367 seq_printf(m, "%s %u",
4368 mod->name, mod->init_layout.size + mod->core_layout.size);
4369 print_unload_info(m, mod);
4371 /* Informative for users. */
4372 seq_printf(m, " %s",
4373 mod->state == MODULE_STATE_GOING ? "Unloading" :
4374 mod->state == MODULE_STATE_COMING ? "Loading" :
4375 "Live");
4376 /* Used by oprofile and other similar tools. */
4377 value = m->private ? NULL : mod->core_layout.base;
4378 seq_printf(m, " 0x%px", value);
4380 /* Taints info */
4381 if (mod->taints)
4382 seq_printf(m, " %s", module_flags(mod, buf));
4384 seq_puts(m, "\n");
4385 return 0;
4388 /* Format: modulename size refcount deps address
4390 Where refcount is a number or -, and deps is a comma-separated list
4391 of depends or -.
4393 static const struct seq_operations modules_op = {
4394 .start = m_start,
4395 .next = m_next,
4396 .stop = m_stop,
4397 .show = m_show
4401 * This also sets the "private" pointer to non-NULL if the
4402 * kernel pointers should be hidden (so you can just test
4403 * "m->private" to see if you should keep the values private).
4405 * We use the same logic as for /proc/kallsyms.
4407 static int modules_open(struct inode *inode, struct file *file)
4409 int err = seq_open(file, &modules_op);
4411 if (!err) {
4412 struct seq_file *m = file->private_data;
4413 m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
4416 return err;
4419 static const struct file_operations proc_modules_operations = {
4420 .open = modules_open,
4421 .read = seq_read,
4422 .llseek = seq_lseek,
4423 .release = seq_release,
4426 static int __init proc_modules_init(void)
4428 proc_create("modules", 0, NULL, &proc_modules_operations);
4429 return 0;
4431 module_init(proc_modules_init);
4432 #endif
4434 /* Given an address, look for it in the module exception tables. */
4435 const struct exception_table_entry *search_module_extables(unsigned long addr)
4437 const struct exception_table_entry *e = NULL;
4438 struct module *mod;
4440 preempt_disable();
4441 mod = __module_address(addr);
4442 if (!mod)
4443 goto out;
4445 if (!mod->num_exentries)
4446 goto out;
4448 e = search_extable(mod->extable,
4449 mod->num_exentries,
4450 addr);
4451 out:
4452 preempt_enable();
4455 * Now, if we found one, we are running inside it now, hence
4456 * we cannot unload the module, hence no refcnt needed.
4458 return e;
4462 * is_module_address - is this address inside a module?
4463 * @addr: the address to check.
4465 * See is_module_text_address() if you simply want to see if the address
4466 * is code (not data).
4468 bool is_module_address(unsigned long addr)
4470 bool ret;
4472 preempt_disable();
4473 ret = __module_address(addr) != NULL;
4474 preempt_enable();
4476 return ret;
4480 * __module_address - get the module which contains an address.
4481 * @addr: the address.
4483 * Must be called with preempt disabled or module mutex held so that
4484 * module doesn't get freed during this.
4486 struct module *__module_address(unsigned long addr)
4488 struct module *mod;
4490 if (addr < module_addr_min || addr > module_addr_max)
4491 return NULL;
4493 module_assert_mutex_or_preempt();
4495 mod = mod_find(addr);
4496 if (mod) {
4497 BUG_ON(!within_module(addr, mod));
4498 if (mod->state == MODULE_STATE_UNFORMED)
4499 mod = NULL;
4501 return mod;
4503 EXPORT_SYMBOL_GPL(__module_address);
4506 * is_module_text_address - is this address inside module code?
4507 * @addr: the address to check.
4509 * See is_module_address() if you simply want to see if the address is
4510 * anywhere in a module. See kernel_text_address() for testing if an
4511 * address corresponds to kernel or module code.
4513 bool is_module_text_address(unsigned long addr)
4515 bool ret;
4517 preempt_disable();
4518 ret = __module_text_address(addr) != NULL;
4519 preempt_enable();
4521 return ret;
4525 * __module_text_address - get the module whose code contains an address.
4526 * @addr: the address.
4528 * Must be called with preempt disabled or module mutex held so that
4529 * module doesn't get freed during this.
4531 struct module *__module_text_address(unsigned long addr)
4533 struct module *mod = __module_address(addr);
4534 if (mod) {
4535 /* Make sure it's within the text section. */
4536 if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
4537 && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
4538 mod = NULL;
4540 return mod;
4542 EXPORT_SYMBOL_GPL(__module_text_address);
4544 /* Don't grab lock, we're oopsing. */
4545 void print_modules(void)
4547 struct module *mod;
4548 char buf[MODULE_FLAGS_BUF_SIZE];
4550 printk(KERN_DEFAULT "Modules linked in:");
4551 /* Most callers should already have preempt disabled, but make sure */
4552 preempt_disable();
4553 list_for_each_entry_rcu(mod, &modules, list) {
4554 if (mod->state == MODULE_STATE_UNFORMED)
4555 continue;
4556 pr_cont(" %s%s", mod->name, module_flags(mod, buf));
4558 preempt_enable();
4559 if (last_unloaded_module[0])
4560 pr_cont(" [last unloaded: %s]", last_unloaded_module);
4561 pr_cont("\n");
4564 #ifdef CONFIG_MODVERSIONS
4565 /* Generate the signature for all relevant module structures here.
4566 * If these change, we don't want to try to parse the module. */
4567 void module_layout(struct module *mod,
4568 struct modversion_info *ver,
4569 struct kernel_param *kp,
4570 struct kernel_symbol *ks,
4571 struct tracepoint * const *tp)
4574 EXPORT_SYMBOL(module_layout);
4575 #endif