2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/export.h>
20 #include <linux/extable.h>
21 #include <linux/moduleloader.h>
22 #include <linux/trace_events.h>
23 #include <linux/init.h>
24 #include <linux/kallsyms.h>
25 #include <linux/file.h>
27 #include <linux/sysfs.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/elf.h>
32 #include <linux/proc_fs.h>
33 #include <linux/security.h>
34 #include <linux/seq_file.h>
35 #include <linux/syscalls.h>
36 #include <linux/fcntl.h>
37 #include <linux/rcupdate.h>
38 #include <linux/capability.h>
39 #include <linux/cpu.h>
40 #include <linux/moduleparam.h>
41 #include <linux/errno.h>
42 #include <linux/err.h>
43 #include <linux/vermagic.h>
44 #include <linux/notifier.h>
45 #include <linux/sched.h>
46 #include <linux/device.h>
47 #include <linux/string.h>
48 #include <linux/mutex.h>
49 #include <linux/rculist.h>
50 #include <linux/uaccess.h>
51 #include <asm/cacheflush.h>
52 #ifdef CONFIG_STRICT_MODULE_RWX
53 #include <asm/set_memory.h>
55 #include <asm/mmu_context.h>
56 #include <linux/license.h>
57 #include <asm/sections.h>
58 #include <linux/tracepoint.h>
59 #include <linux/ftrace.h>
60 #include <linux/livepatch.h>
61 #include <linux/async.h>
62 #include <linux/percpu.h>
63 #include <linux/kmemleak.h>
64 #include <linux/jump_label.h>
65 #include <linux/pfn.h>
66 #include <linux/bsearch.h>
67 #include <linux/dynamic_debug.h>
68 #include <linux/audit.h>
69 #include <uapi/linux/module.h>
70 #include "module-internal.h"
72 #define CREATE_TRACE_POINTS
73 #include <trace/events/module.h>
75 #ifndef ARCH_SHF_SMALL
76 #define ARCH_SHF_SMALL 0
80 * Modules' sections will be aligned on page boundaries
81 * to ensure complete separation of code and data, but
82 * only when CONFIG_STRICT_MODULE_RWX=y
84 #ifdef CONFIG_STRICT_MODULE_RWX
85 # define debug_align(X) ALIGN(X, PAGE_SIZE)
87 # define debug_align(X) (X)
90 /* If this is set, the section belongs in the init part of the module */
91 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
95 * 1) List of modules (also safely readable with preempt_disable),
96 * 2) module_use links,
97 * 3) module_addr_min/module_addr_max.
98 * (delete and add uses RCU list operations). */
99 DEFINE_MUTEX(module_mutex
);
100 EXPORT_SYMBOL_GPL(module_mutex
);
101 static LIST_HEAD(modules
);
103 #ifdef CONFIG_MODULES_TREE_LOOKUP
106 * Use a latched RB-tree for __module_address(); this allows us to use
107 * RCU-sched lookups of the address from any context.
109 * This is conditional on PERF_EVENTS || TRACING because those can really hit
110 * __module_address() hard by doing a lot of stack unwinding; potentially from
114 static __always_inline
unsigned long __mod_tree_val(struct latch_tree_node
*n
)
116 struct module_layout
*layout
= container_of(n
, struct module_layout
, mtn
.node
);
118 return (unsigned long)layout
->base
;
121 static __always_inline
unsigned long __mod_tree_size(struct latch_tree_node
*n
)
123 struct module_layout
*layout
= container_of(n
, struct module_layout
, mtn
.node
);
125 return (unsigned long)layout
->size
;
128 static __always_inline
bool
129 mod_tree_less(struct latch_tree_node
*a
, struct latch_tree_node
*b
)
131 return __mod_tree_val(a
) < __mod_tree_val(b
);
134 static __always_inline
int
135 mod_tree_comp(void *key
, struct latch_tree_node
*n
)
137 unsigned long val
= (unsigned long)key
;
138 unsigned long start
, end
;
140 start
= __mod_tree_val(n
);
144 end
= start
+ __mod_tree_size(n
);
151 static const struct latch_tree_ops mod_tree_ops
= {
152 .less
= mod_tree_less
,
153 .comp
= mod_tree_comp
,
156 static struct mod_tree_root
{
157 struct latch_tree_root root
;
158 unsigned long addr_min
;
159 unsigned long addr_max
;
160 } mod_tree __cacheline_aligned
= {
164 #define module_addr_min mod_tree.addr_min
165 #define module_addr_max mod_tree.addr_max
167 static noinline
void __mod_tree_insert(struct mod_tree_node
*node
)
169 latch_tree_insert(&node
->node
, &mod_tree
.root
, &mod_tree_ops
);
172 static void __mod_tree_remove(struct mod_tree_node
*node
)
174 latch_tree_erase(&node
->node
, &mod_tree
.root
, &mod_tree_ops
);
178 * These modifications: insert, remove_init and remove; are serialized by the
181 static void mod_tree_insert(struct module
*mod
)
183 mod
->core_layout
.mtn
.mod
= mod
;
184 mod
->init_layout
.mtn
.mod
= mod
;
186 __mod_tree_insert(&mod
->core_layout
.mtn
);
187 if (mod
->init_layout
.size
)
188 __mod_tree_insert(&mod
->init_layout
.mtn
);
191 static void mod_tree_remove_init(struct module
*mod
)
193 if (mod
->init_layout
.size
)
194 __mod_tree_remove(&mod
->init_layout
.mtn
);
197 static void mod_tree_remove(struct module
*mod
)
199 __mod_tree_remove(&mod
->core_layout
.mtn
);
200 mod_tree_remove_init(mod
);
203 static struct module
*mod_find(unsigned long addr
)
205 struct latch_tree_node
*ltn
;
207 ltn
= latch_tree_find((void *)addr
, &mod_tree
.root
, &mod_tree_ops
);
211 return container_of(ltn
, struct mod_tree_node
, node
)->mod
;
214 #else /* MODULES_TREE_LOOKUP */
216 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
218 static void mod_tree_insert(struct module
*mod
) { }
219 static void mod_tree_remove_init(struct module
*mod
) { }
220 static void mod_tree_remove(struct module
*mod
) { }
222 static struct module
*mod_find(unsigned long addr
)
226 list_for_each_entry_rcu(mod
, &modules
, list
) {
227 if (within_module(addr
, mod
))
234 #endif /* MODULES_TREE_LOOKUP */
237 * Bounds of module text, for speeding up __module_address.
238 * Protected by module_mutex.
240 static void __mod_update_bounds(void *base
, unsigned int size
)
242 unsigned long min
= (unsigned long)base
;
243 unsigned long max
= min
+ size
;
245 if (min
< module_addr_min
)
246 module_addr_min
= min
;
247 if (max
> module_addr_max
)
248 module_addr_max
= max
;
251 static void mod_update_bounds(struct module
*mod
)
253 __mod_update_bounds(mod
->core_layout
.base
, mod
->core_layout
.size
);
254 if (mod
->init_layout
.size
)
255 __mod_update_bounds(mod
->init_layout
.base
, mod
->init_layout
.size
);
258 #ifdef CONFIG_KGDB_KDB
259 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
260 #endif /* CONFIG_KGDB_KDB */
262 static void module_assert_mutex(void)
264 lockdep_assert_held(&module_mutex
);
267 static void module_assert_mutex_or_preempt(void)
269 #ifdef CONFIG_LOCKDEP
270 if (unlikely(!debug_locks
))
273 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
274 !lockdep_is_held(&module_mutex
));
278 static bool sig_enforce
= IS_ENABLED(CONFIG_MODULE_SIG_FORCE
);
279 #ifndef CONFIG_MODULE_SIG_FORCE
280 module_param(sig_enforce
, bool_enable_only
, 0644);
281 #endif /* !CONFIG_MODULE_SIG_FORCE */
283 /* Block module loading/unloading? */
284 int modules_disabled
= 0;
285 core_param(nomodule
, modules_disabled
, bint
, 0);
287 /* Waiting for a module to finish initializing? */
288 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
290 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
292 int register_module_notifier(struct notifier_block
*nb
)
294 return blocking_notifier_chain_register(&module_notify_list
, nb
);
296 EXPORT_SYMBOL(register_module_notifier
);
298 int unregister_module_notifier(struct notifier_block
*nb
)
300 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
302 EXPORT_SYMBOL(unregister_module_notifier
);
308 char *secstrings
, *strtab
;
309 unsigned long symoffs
, stroffs
;
310 struct _ddebug
*debug
;
311 unsigned int num_debug
;
313 #ifdef CONFIG_KALLSYMS
314 unsigned long mod_kallsyms_init_off
;
317 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
322 * We require a truly strong try_module_get(): 0 means success.
323 * Otherwise an error is returned due to ongoing or failed
324 * initialization etc.
326 static inline int strong_try_module_get(struct module
*mod
)
328 BUG_ON(mod
&& mod
->state
== MODULE_STATE_UNFORMED
);
329 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
331 if (try_module_get(mod
))
337 static inline void add_taint_module(struct module
*mod
, unsigned flag
,
338 enum lockdep_ok lockdep_ok
)
340 add_taint(flag
, lockdep_ok
);
341 set_bit(flag
, &mod
->taints
);
345 * A thread that wants to hold a reference to a module only while it
346 * is running can call this to safely exit. nfsd and lockd use this.
348 void __noreturn
__module_put_and_exit(struct module
*mod
, long code
)
353 EXPORT_SYMBOL(__module_put_and_exit
);
355 /* Find a module section: 0 means not found. */
356 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
360 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
361 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
362 /* Alloc bit cleared means "ignore it." */
363 if ((shdr
->sh_flags
& SHF_ALLOC
)
364 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
370 /* Find a module section, or NULL. */
371 static void *section_addr(const struct load_info
*info
, const char *name
)
373 /* Section 0 has sh_addr 0. */
374 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
377 /* Find a module section, or NULL. Fill in number of "objects" in section. */
378 static void *section_objs(const struct load_info
*info
,
383 unsigned int sec
= find_sec(info
, name
);
385 /* Section 0 has sh_addr 0 and sh_size 0. */
386 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
387 return (void *)info
->sechdrs
[sec
].sh_addr
;
390 /* Provided by the linker */
391 extern const struct kernel_symbol __start___ksymtab
[];
392 extern const struct kernel_symbol __stop___ksymtab
[];
393 extern const struct kernel_symbol __start___ksymtab_gpl
[];
394 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
395 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
396 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
397 extern const s32 __start___kcrctab
[];
398 extern const s32 __start___kcrctab_gpl
[];
399 extern const s32 __start___kcrctab_gpl_future
[];
400 #ifdef CONFIG_UNUSED_SYMBOLS
401 extern const struct kernel_symbol __start___ksymtab_unused
[];
402 extern const struct kernel_symbol __stop___ksymtab_unused
[];
403 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
404 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
405 extern const s32 __start___kcrctab_unused
[];
406 extern const s32 __start___kcrctab_unused_gpl
[];
409 #ifndef CONFIG_MODVERSIONS
410 #define symversion(base, idx) NULL
412 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
415 static bool each_symbol_in_section(const struct symsearch
*arr
,
416 unsigned int arrsize
,
417 struct module
*owner
,
418 bool (*fn
)(const struct symsearch
*syms
,
419 struct module
*owner
,
425 for (j
= 0; j
< arrsize
; j
++) {
426 if (fn(&arr
[j
], owner
, data
))
433 /* Returns true as soon as fn returns true, otherwise false. */
434 bool each_symbol_section(bool (*fn
)(const struct symsearch
*arr
,
435 struct module
*owner
,
440 static const struct symsearch arr
[] = {
441 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
442 NOT_GPL_ONLY
, false },
443 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
444 __start___kcrctab_gpl
,
446 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
447 __start___kcrctab_gpl_future
,
448 WILL_BE_GPL_ONLY
, false },
449 #ifdef CONFIG_UNUSED_SYMBOLS
450 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
451 __start___kcrctab_unused
,
452 NOT_GPL_ONLY
, true },
453 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
454 __start___kcrctab_unused_gpl
,
459 module_assert_mutex_or_preempt();
461 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
464 list_for_each_entry_rcu(mod
, &modules
, list
) {
465 struct symsearch arr
[] = {
466 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
467 NOT_GPL_ONLY
, false },
468 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
471 { mod
->gpl_future_syms
,
472 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
473 mod
->gpl_future_crcs
,
474 WILL_BE_GPL_ONLY
, false },
475 #ifdef CONFIG_UNUSED_SYMBOLS
477 mod
->unused_syms
+ mod
->num_unused_syms
,
479 NOT_GPL_ONLY
, true },
480 { mod
->unused_gpl_syms
,
481 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
482 mod
->unused_gpl_crcs
,
487 if (mod
->state
== MODULE_STATE_UNFORMED
)
490 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
495 EXPORT_SYMBOL_GPL(each_symbol_section
);
497 struct find_symbol_arg
{
504 struct module
*owner
;
506 const struct kernel_symbol
*sym
;
509 static bool check_symbol(const struct symsearch
*syms
,
510 struct module
*owner
,
511 unsigned int symnum
, void *data
)
513 struct find_symbol_arg
*fsa
= data
;
516 if (syms
->licence
== GPL_ONLY
)
518 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
519 pr_warn("Symbol %s is being used by a non-GPL module, "
520 "which will not be allowed in the future\n",
525 #ifdef CONFIG_UNUSED_SYMBOLS
526 if (syms
->unused
&& fsa
->warn
) {
527 pr_warn("Symbol %s is marked as UNUSED, however this module is "
528 "using it.\n", fsa
->name
);
529 pr_warn("This symbol will go away in the future.\n");
530 pr_warn("Please evaluate if this is the right api to use and "
531 "if it really is, submit a report to the linux kernel "
532 "mailing list together with submitting your code for "
538 fsa
->crc
= symversion(syms
->crcs
, symnum
);
539 fsa
->sym
= &syms
->start
[symnum
];
543 static int cmp_name(const void *va
, const void *vb
)
546 const struct kernel_symbol
*b
;
548 return strcmp(a
, b
->name
);
551 static bool find_symbol_in_section(const struct symsearch
*syms
,
552 struct module
*owner
,
555 struct find_symbol_arg
*fsa
= data
;
556 struct kernel_symbol
*sym
;
558 sym
= bsearch(fsa
->name
, syms
->start
, syms
->stop
- syms
->start
,
559 sizeof(struct kernel_symbol
), cmp_name
);
561 if (sym
!= NULL
&& check_symbol(syms
, owner
, sym
- syms
->start
, data
))
567 /* Find a symbol and return it, along with, (optional) crc and
568 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
569 const struct kernel_symbol
*find_symbol(const char *name
,
570 struct module
**owner
,
575 struct find_symbol_arg fsa
;
581 if (each_symbol_section(find_symbol_in_section
, &fsa
)) {
589 pr_debug("Failed to find symbol %s\n", name
);
592 EXPORT_SYMBOL_GPL(find_symbol
);
595 * Search for module by name: must hold module_mutex (or preempt disabled
596 * for read-only access).
598 static struct module
*find_module_all(const char *name
, size_t len
,
603 module_assert_mutex_or_preempt();
605 list_for_each_entry(mod
, &modules
, list
) {
606 if (!even_unformed
&& mod
->state
== MODULE_STATE_UNFORMED
)
608 if (strlen(mod
->name
) == len
&& !memcmp(mod
->name
, name
, len
))
614 struct module
*find_module(const char *name
)
616 module_assert_mutex();
617 return find_module_all(name
, strlen(name
), false);
619 EXPORT_SYMBOL_GPL(find_module
);
623 static inline void __percpu
*mod_percpu(struct module
*mod
)
628 static int percpu_modalloc(struct module
*mod
, struct load_info
*info
)
630 Elf_Shdr
*pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
631 unsigned long align
= pcpusec
->sh_addralign
;
633 if (!pcpusec
->sh_size
)
636 if (align
> PAGE_SIZE
) {
637 pr_warn("%s: per-cpu alignment %li > %li\n",
638 mod
->name
, align
, PAGE_SIZE
);
642 mod
->percpu
= __alloc_reserved_percpu(pcpusec
->sh_size
, align
);
644 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
645 mod
->name
, (unsigned long)pcpusec
->sh_size
);
648 mod
->percpu_size
= pcpusec
->sh_size
;
652 static void percpu_modfree(struct module
*mod
)
654 free_percpu(mod
->percpu
);
657 static unsigned int find_pcpusec(struct load_info
*info
)
659 return find_sec(info
, ".data..percpu");
662 static void percpu_modcopy(struct module
*mod
,
663 const void *from
, unsigned long size
)
667 for_each_possible_cpu(cpu
)
668 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
671 bool __is_module_percpu_address(unsigned long addr
, unsigned long *can_addr
)
678 list_for_each_entry_rcu(mod
, &modules
, list
) {
679 if (mod
->state
== MODULE_STATE_UNFORMED
)
681 if (!mod
->percpu_size
)
683 for_each_possible_cpu(cpu
) {
684 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
685 void *va
= (void *)addr
;
687 if (va
>= start
&& va
< start
+ mod
->percpu_size
) {
689 *can_addr
= (unsigned long) (va
- start
);
690 *can_addr
+= (unsigned long)
691 per_cpu_ptr(mod
->percpu
,
705 * is_module_percpu_address - test whether address is from module static percpu
706 * @addr: address to test
708 * Test whether @addr belongs to module static percpu area.
711 * %true if @addr is from module static percpu area
713 bool is_module_percpu_address(unsigned long addr
)
715 return __is_module_percpu_address(addr
, NULL
);
718 #else /* ... !CONFIG_SMP */
720 static inline void __percpu
*mod_percpu(struct module
*mod
)
724 static int percpu_modalloc(struct module
*mod
, struct load_info
*info
)
726 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
727 if (info
->sechdrs
[info
->index
.pcpu
].sh_size
!= 0)
731 static inline void percpu_modfree(struct module
*mod
)
734 static unsigned int find_pcpusec(struct load_info
*info
)
738 static inline void percpu_modcopy(struct module
*mod
,
739 const void *from
, unsigned long size
)
741 /* pcpusec should be 0, and size of that section should be 0. */
744 bool is_module_percpu_address(unsigned long addr
)
749 bool __is_module_percpu_address(unsigned long addr
, unsigned long *can_addr
)
754 #endif /* CONFIG_SMP */
756 #define MODINFO_ATTR(field) \
757 static void setup_modinfo_##field(struct module *mod, const char *s) \
759 mod->field = kstrdup(s, GFP_KERNEL); \
761 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
762 struct module_kobject *mk, char *buffer) \
764 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
766 static int modinfo_##field##_exists(struct module *mod) \
768 return mod->field != NULL; \
770 static void free_modinfo_##field(struct module *mod) \
775 static struct module_attribute modinfo_##field = { \
776 .attr = { .name = __stringify(field), .mode = 0444 }, \
777 .show = show_modinfo_##field, \
778 .setup = setup_modinfo_##field, \
779 .test = modinfo_##field##_exists, \
780 .free = free_modinfo_##field, \
783 MODINFO_ATTR(version
);
784 MODINFO_ATTR(srcversion
);
786 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
788 #ifdef CONFIG_MODULE_UNLOAD
790 EXPORT_TRACEPOINT_SYMBOL(module_get
);
792 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
793 #define MODULE_REF_BASE 1
795 /* Init the unload section of the module. */
796 static int module_unload_init(struct module
*mod
)
799 * Initialize reference counter to MODULE_REF_BASE.
800 * refcnt == 0 means module is going.
802 atomic_set(&mod
->refcnt
, MODULE_REF_BASE
);
804 INIT_LIST_HEAD(&mod
->source_list
);
805 INIT_LIST_HEAD(&mod
->target_list
);
807 /* Hold reference count during initialization. */
808 atomic_inc(&mod
->refcnt
);
813 /* Does a already use b? */
814 static int already_uses(struct module
*a
, struct module
*b
)
816 struct module_use
*use
;
818 list_for_each_entry(use
, &b
->source_list
, source_list
) {
819 if (use
->source
== a
) {
820 pr_debug("%s uses %s!\n", a
->name
, b
->name
);
824 pr_debug("%s does not use %s!\n", a
->name
, b
->name
);
830 * - we add 'a' as a "source", 'b' as a "target" of module use
831 * - the module_use is added to the list of 'b' sources (so
832 * 'b' can walk the list to see who sourced them), and of 'a'
833 * targets (so 'a' can see what modules it targets).
835 static int add_module_usage(struct module
*a
, struct module
*b
)
837 struct module_use
*use
;
839 pr_debug("Allocating new usage for %s.\n", a
->name
);
840 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
842 pr_warn("%s: out of memory loading\n", a
->name
);
848 list_add(&use
->source_list
, &b
->source_list
);
849 list_add(&use
->target_list
, &a
->target_list
);
853 /* Module a uses b: caller needs module_mutex() */
854 int ref_module(struct module
*a
, struct module
*b
)
858 if (b
== NULL
|| already_uses(a
, b
))
861 /* If module isn't available, we fail. */
862 err
= strong_try_module_get(b
);
866 err
= add_module_usage(a
, b
);
873 EXPORT_SYMBOL_GPL(ref_module
);
875 /* Clear the unload stuff of the module. */
876 static void module_unload_free(struct module
*mod
)
878 struct module_use
*use
, *tmp
;
880 mutex_lock(&module_mutex
);
881 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
882 struct module
*i
= use
->target
;
883 pr_debug("%s unusing %s\n", mod
->name
, i
->name
);
885 list_del(&use
->source_list
);
886 list_del(&use
->target_list
);
889 mutex_unlock(&module_mutex
);
892 #ifdef CONFIG_MODULE_FORCE_UNLOAD
893 static inline int try_force_unload(unsigned int flags
)
895 int ret
= (flags
& O_TRUNC
);
897 add_taint(TAINT_FORCED_RMMOD
, LOCKDEP_NOW_UNRELIABLE
);
901 static inline int try_force_unload(unsigned int flags
)
905 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
907 /* Try to release refcount of module, 0 means success. */
908 static int try_release_module_ref(struct module
*mod
)
912 /* Try to decrement refcnt which we set at loading */
913 ret
= atomic_sub_return(MODULE_REF_BASE
, &mod
->refcnt
);
916 /* Someone can put this right now, recover with checking */
917 ret
= atomic_add_unless(&mod
->refcnt
, MODULE_REF_BASE
, 0);
922 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
924 /* If it's not unused, quit unless we're forcing. */
925 if (try_release_module_ref(mod
) != 0) {
926 *forced
= try_force_unload(flags
);
931 /* Mark it as dying. */
932 mod
->state
= MODULE_STATE_GOING
;
938 * module_refcount - return the refcount or -1 if unloading
940 * @mod: the module we're checking
943 * -1 if the module is in the process of unloading
944 * otherwise the number of references in the kernel to the module
946 int module_refcount(struct module
*mod
)
948 return atomic_read(&mod
->refcnt
) - MODULE_REF_BASE
;
950 EXPORT_SYMBOL(module_refcount
);
952 /* This exists whether we can unload or not */
953 static void free_module(struct module
*mod
);
955 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
959 char name
[MODULE_NAME_LEN
];
962 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
965 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
967 name
[MODULE_NAME_LEN
-1] = '\0';
969 audit_log_kern_module(name
);
971 if (mutex_lock_interruptible(&module_mutex
) != 0)
974 mod
= find_module(name
);
980 if (!list_empty(&mod
->source_list
)) {
981 /* Other modules depend on us: get rid of them first. */
986 /* Doing init or already dying? */
987 if (mod
->state
!= MODULE_STATE_LIVE
) {
988 /* FIXME: if (force), slam module count damn the torpedoes */
989 pr_debug("%s already dying\n", mod
->name
);
994 /* If it has an init func, it must have an exit func to unload */
995 if (mod
->init
&& !mod
->exit
) {
996 forced
= try_force_unload(flags
);
998 /* This module can't be removed */
1004 /* Stop the machine so refcounts can't move and disable module. */
1005 ret
= try_stop_module(mod
, flags
, &forced
);
1009 mutex_unlock(&module_mutex
);
1010 /* Final destruction now no one is using it. */
1011 if (mod
->exit
!= NULL
)
1013 blocking_notifier_call_chain(&module_notify_list
,
1014 MODULE_STATE_GOING
, mod
);
1015 klp_module_going(mod
);
1016 ftrace_release_mod(mod
);
1018 async_synchronize_full();
1020 /* Store the name of the last unloaded module for diagnostic purposes */
1021 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
1026 mutex_unlock(&module_mutex
);
1030 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
1032 struct module_use
*use
;
1033 int printed_something
= 0;
1035 seq_printf(m
, " %i ", module_refcount(mod
));
1038 * Always include a trailing , so userspace can differentiate
1039 * between this and the old multi-field proc format.
1041 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
1042 printed_something
= 1;
1043 seq_printf(m
, "%s,", use
->source
->name
);
1046 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
1047 printed_something
= 1;
1048 seq_puts(m
, "[permanent],");
1051 if (!printed_something
)
1055 void __symbol_put(const char *symbol
)
1057 struct module
*owner
;
1060 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
1065 EXPORT_SYMBOL(__symbol_put
);
1067 /* Note this assumes addr is a function, which it currently always is. */
1068 void symbol_put_addr(void *addr
)
1070 struct module
*modaddr
;
1071 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
1073 if (core_kernel_text(a
))
1077 * Even though we hold a reference on the module; we still need to
1078 * disable preemption in order to safely traverse the data structure.
1081 modaddr
= __module_text_address(a
);
1083 module_put(modaddr
);
1086 EXPORT_SYMBOL_GPL(symbol_put_addr
);
1088 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
1089 struct module_kobject
*mk
, char *buffer
)
1091 return sprintf(buffer
, "%i\n", module_refcount(mk
->mod
));
1094 static struct module_attribute modinfo_refcnt
=
1095 __ATTR(refcnt
, 0444, show_refcnt
, NULL
);
1097 void __module_get(struct module
*module
)
1101 atomic_inc(&module
->refcnt
);
1102 trace_module_get(module
, _RET_IP_
);
1106 EXPORT_SYMBOL(__module_get
);
1108 bool try_module_get(struct module
*module
)
1114 /* Note: here, we can fail to get a reference */
1115 if (likely(module_is_live(module
) &&
1116 atomic_inc_not_zero(&module
->refcnt
) != 0))
1117 trace_module_get(module
, _RET_IP_
);
1125 EXPORT_SYMBOL(try_module_get
);
1127 void module_put(struct module
*module
)
1133 ret
= atomic_dec_if_positive(&module
->refcnt
);
1134 WARN_ON(ret
< 0); /* Failed to put refcount */
1135 trace_module_put(module
, _RET_IP_
);
1139 EXPORT_SYMBOL(module_put
);
1141 #else /* !CONFIG_MODULE_UNLOAD */
1142 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
1144 /* We don't know the usage count, or what modules are using. */
1145 seq_puts(m
, " - -");
1148 static inline void module_unload_free(struct module
*mod
)
1152 int ref_module(struct module
*a
, struct module
*b
)
1154 return strong_try_module_get(b
);
1156 EXPORT_SYMBOL_GPL(ref_module
);
1158 static inline int module_unload_init(struct module
*mod
)
1162 #endif /* CONFIG_MODULE_UNLOAD */
1164 static size_t module_flags_taint(struct module
*mod
, char *buf
)
1169 for (i
= 0; i
< TAINT_FLAGS_COUNT
; i
++) {
1170 if (taint_flags
[i
].module
&& test_bit(i
, &mod
->taints
))
1171 buf
[l
++] = taint_flags
[i
].c_true
;
1177 static ssize_t
show_initstate(struct module_attribute
*mattr
,
1178 struct module_kobject
*mk
, char *buffer
)
1180 const char *state
= "unknown";
1182 switch (mk
->mod
->state
) {
1183 case MODULE_STATE_LIVE
:
1186 case MODULE_STATE_COMING
:
1189 case MODULE_STATE_GOING
:
1195 return sprintf(buffer
, "%s\n", state
);
1198 static struct module_attribute modinfo_initstate
=
1199 __ATTR(initstate
, 0444, show_initstate
, NULL
);
1201 static ssize_t
store_uevent(struct module_attribute
*mattr
,
1202 struct module_kobject
*mk
,
1203 const char *buffer
, size_t count
)
1205 enum kobject_action action
;
1207 if (kobject_action_type(buffer
, count
, &action
) == 0)
1208 kobject_uevent(&mk
->kobj
, action
);
1212 struct module_attribute module_uevent
=
1213 __ATTR(uevent
, 0200, NULL
, store_uevent
);
1215 static ssize_t
show_coresize(struct module_attribute
*mattr
,
1216 struct module_kobject
*mk
, char *buffer
)
1218 return sprintf(buffer
, "%u\n", mk
->mod
->core_layout
.size
);
1221 static struct module_attribute modinfo_coresize
=
1222 __ATTR(coresize
, 0444, show_coresize
, NULL
);
1224 static ssize_t
show_initsize(struct module_attribute
*mattr
,
1225 struct module_kobject
*mk
, char *buffer
)
1227 return sprintf(buffer
, "%u\n", mk
->mod
->init_layout
.size
);
1230 static struct module_attribute modinfo_initsize
=
1231 __ATTR(initsize
, 0444, show_initsize
, NULL
);
1233 static ssize_t
show_taint(struct module_attribute
*mattr
,
1234 struct module_kobject
*mk
, char *buffer
)
1238 l
= module_flags_taint(mk
->mod
, buffer
);
1243 static struct module_attribute modinfo_taint
=
1244 __ATTR(taint
, 0444, show_taint
, NULL
);
1246 static struct module_attribute
*modinfo_attrs
[] = {
1249 &modinfo_srcversion
,
1254 #ifdef CONFIG_MODULE_UNLOAD
1260 static const char vermagic
[] = VERMAGIC_STRING
;
1262 static int try_to_force_load(struct module
*mod
, const char *reason
)
1264 #ifdef CONFIG_MODULE_FORCE_LOAD
1265 if (!test_taint(TAINT_FORCED_MODULE
))
1266 pr_warn("%s: %s: kernel tainted.\n", mod
->name
, reason
);
1267 add_taint_module(mod
, TAINT_FORCED_MODULE
, LOCKDEP_NOW_UNRELIABLE
);
1274 #ifdef CONFIG_MODVERSIONS
1276 static u32
resolve_rel_crc(const s32
*crc
)
1278 return *(u32
*)((void *)crc
+ *crc
);
1281 static int check_version(Elf_Shdr
*sechdrs
,
1282 unsigned int versindex
,
1283 const char *symname
,
1287 unsigned int i
, num_versions
;
1288 struct modversion_info
*versions
;
1290 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1294 /* No versions at all? modprobe --force does this. */
1296 return try_to_force_load(mod
, symname
) == 0;
1298 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1299 num_versions
= sechdrs
[versindex
].sh_size
1300 / sizeof(struct modversion_info
);
1302 for (i
= 0; i
< num_versions
; i
++) {
1305 if (strcmp(versions
[i
].name
, symname
) != 0)
1308 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS
))
1309 crcval
= resolve_rel_crc(crc
);
1312 if (versions
[i
].crc
== crcval
)
1314 pr_debug("Found checksum %X vs module %lX\n",
1315 crcval
, versions
[i
].crc
);
1319 /* Broken toolchain. Warn once, then let it go.. */
1320 pr_warn_once("%s: no symbol version for %s\n", mod
->name
, symname
);
1324 pr_warn("%s: disagrees about version of symbol %s\n",
1325 mod
->name
, symname
);
1329 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1330 unsigned int versindex
,
1336 * Since this should be found in kernel (which can't be removed), no
1337 * locking is necessary -- use preempt_disable() to placate lockdep.
1340 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout
), NULL
,
1341 &crc
, true, false)) {
1346 return check_version(sechdrs
, versindex
,
1347 VMLINUX_SYMBOL_STR(module_layout
), mod
, crc
);
1350 /* First part is kernel version, which we ignore if module has crcs. */
1351 static inline int same_magic(const char *amagic
, const char *bmagic
,
1355 amagic
+= strcspn(amagic
, " ");
1356 bmagic
+= strcspn(bmagic
, " ");
1358 return strcmp(amagic
, bmagic
) == 0;
1361 static inline int check_version(Elf_Shdr
*sechdrs
,
1362 unsigned int versindex
,
1363 const char *symname
,
1370 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1371 unsigned int versindex
,
1377 static inline int same_magic(const char *amagic
, const char *bmagic
,
1380 return strcmp(amagic
, bmagic
) == 0;
1382 #endif /* CONFIG_MODVERSIONS */
1384 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1385 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1386 const struct load_info
*info
,
1390 struct module
*owner
;
1391 const struct kernel_symbol
*sym
;
1396 * The module_mutex should not be a heavily contended lock;
1397 * if we get the occasional sleep here, we'll go an extra iteration
1398 * in the wait_event_interruptible(), which is harmless.
1400 sched_annotate_sleep();
1401 mutex_lock(&module_mutex
);
1402 sym
= find_symbol(name
, &owner
, &crc
,
1403 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1407 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
)) {
1408 sym
= ERR_PTR(-EINVAL
);
1412 err
= ref_module(mod
, owner
);
1419 /* We must make copy under the lock if we failed to get ref. */
1420 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1422 mutex_unlock(&module_mutex
);
1426 static const struct kernel_symbol
*
1427 resolve_symbol_wait(struct module
*mod
,
1428 const struct load_info
*info
,
1431 const struct kernel_symbol
*ksym
;
1432 char owner
[MODULE_NAME_LEN
];
1434 if (wait_event_interruptible_timeout(module_wq
,
1435 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1436 || PTR_ERR(ksym
) != -EBUSY
,
1438 pr_warn("%s: gave up waiting for init of module %s.\n",
1445 * /sys/module/foo/sections stuff
1446 * J. Corbet <corbet@lwn.net>
1450 #ifdef CONFIG_KALLSYMS
1451 static inline bool sect_empty(const Elf_Shdr
*sect
)
1453 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1456 struct module_sect_attr
{
1457 struct module_attribute mattr
;
1459 unsigned long address
;
1462 struct module_sect_attrs
{
1463 struct attribute_group grp
;
1464 unsigned int nsections
;
1465 struct module_sect_attr attrs
[0];
1468 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1469 struct module_kobject
*mk
, char *buf
)
1471 struct module_sect_attr
*sattr
=
1472 container_of(mattr
, struct module_sect_attr
, mattr
);
1473 return sprintf(buf
, "0x%pK\n", (void *)sattr
->address
);
1476 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1478 unsigned int section
;
1480 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1481 kfree(sect_attrs
->attrs
[section
].name
);
1485 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1487 unsigned int nloaded
= 0, i
, size
[2];
1488 struct module_sect_attrs
*sect_attrs
;
1489 struct module_sect_attr
*sattr
;
1490 struct attribute
**gattr
;
1492 /* Count loaded sections and allocate structures */
1493 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1494 if (!sect_empty(&info
->sechdrs
[i
]))
1496 size
[0] = ALIGN(sizeof(*sect_attrs
)
1497 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1498 sizeof(sect_attrs
->grp
.attrs
[0]));
1499 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1500 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1501 if (sect_attrs
== NULL
)
1504 /* Setup section attributes. */
1505 sect_attrs
->grp
.name
= "sections";
1506 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1508 sect_attrs
->nsections
= 0;
1509 sattr
= §_attrs
->attrs
[0];
1510 gattr
= §_attrs
->grp
.attrs
[0];
1511 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1512 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1513 if (sect_empty(sec
))
1515 sattr
->address
= sec
->sh_addr
;
1516 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1518 if (sattr
->name
== NULL
)
1520 sect_attrs
->nsections
++;
1521 sysfs_attr_init(&sattr
->mattr
.attr
);
1522 sattr
->mattr
.show
= module_sect_show
;
1523 sattr
->mattr
.store
= NULL
;
1524 sattr
->mattr
.attr
.name
= sattr
->name
;
1525 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1526 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1530 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1533 mod
->sect_attrs
= sect_attrs
;
1536 free_sect_attrs(sect_attrs
);
1539 static void remove_sect_attrs(struct module
*mod
)
1541 if (mod
->sect_attrs
) {
1542 sysfs_remove_group(&mod
->mkobj
.kobj
,
1543 &mod
->sect_attrs
->grp
);
1544 /* We are positive that no one is using any sect attrs
1545 * at this point. Deallocate immediately. */
1546 free_sect_attrs(mod
->sect_attrs
);
1547 mod
->sect_attrs
= NULL
;
1552 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1555 struct module_notes_attrs
{
1556 struct kobject
*dir
;
1558 struct bin_attribute attrs
[0];
1561 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1562 struct bin_attribute
*bin_attr
,
1563 char *buf
, loff_t pos
, size_t count
)
1566 * The caller checked the pos and count against our size.
1568 memcpy(buf
, bin_attr
->private + pos
, count
);
1572 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1575 if (notes_attrs
->dir
) {
1577 sysfs_remove_bin_file(notes_attrs
->dir
,
1578 ¬es_attrs
->attrs
[i
]);
1579 kobject_put(notes_attrs
->dir
);
1584 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1586 unsigned int notes
, loaded
, i
;
1587 struct module_notes_attrs
*notes_attrs
;
1588 struct bin_attribute
*nattr
;
1590 /* failed to create section attributes, so can't create notes */
1591 if (!mod
->sect_attrs
)
1594 /* Count notes sections and allocate structures. */
1596 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1597 if (!sect_empty(&info
->sechdrs
[i
]) &&
1598 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1604 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1605 + notes
* sizeof(notes_attrs
->attrs
[0]),
1607 if (notes_attrs
== NULL
)
1610 notes_attrs
->notes
= notes
;
1611 nattr
= ¬es_attrs
->attrs
[0];
1612 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1613 if (sect_empty(&info
->sechdrs
[i
]))
1615 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1616 sysfs_bin_attr_init(nattr
);
1617 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1618 nattr
->attr
.mode
= S_IRUGO
;
1619 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1620 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1621 nattr
->read
= module_notes_read
;
1627 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1628 if (!notes_attrs
->dir
)
1631 for (i
= 0; i
< notes
; ++i
)
1632 if (sysfs_create_bin_file(notes_attrs
->dir
,
1633 ¬es_attrs
->attrs
[i
]))
1636 mod
->notes_attrs
= notes_attrs
;
1640 free_notes_attrs(notes_attrs
, i
);
1643 static void remove_notes_attrs(struct module
*mod
)
1645 if (mod
->notes_attrs
)
1646 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1651 static inline void add_sect_attrs(struct module
*mod
,
1652 const struct load_info
*info
)
1656 static inline void remove_sect_attrs(struct module
*mod
)
1660 static inline void add_notes_attrs(struct module
*mod
,
1661 const struct load_info
*info
)
1665 static inline void remove_notes_attrs(struct module
*mod
)
1668 #endif /* CONFIG_KALLSYMS */
1670 static void add_usage_links(struct module
*mod
)
1672 #ifdef CONFIG_MODULE_UNLOAD
1673 struct module_use
*use
;
1676 mutex_lock(&module_mutex
);
1677 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1678 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1679 &mod
->mkobj
.kobj
, mod
->name
);
1681 mutex_unlock(&module_mutex
);
1685 static void del_usage_links(struct module
*mod
)
1687 #ifdef CONFIG_MODULE_UNLOAD
1688 struct module_use
*use
;
1690 mutex_lock(&module_mutex
);
1691 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1692 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1693 mutex_unlock(&module_mutex
);
1697 static int module_add_modinfo_attrs(struct module
*mod
)
1699 struct module_attribute
*attr
;
1700 struct module_attribute
*temp_attr
;
1704 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1705 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1707 if (!mod
->modinfo_attrs
)
1710 temp_attr
= mod
->modinfo_attrs
;
1711 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1712 if (!attr
->test
|| attr
->test(mod
)) {
1713 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1714 sysfs_attr_init(&temp_attr
->attr
);
1715 error
= sysfs_create_file(&mod
->mkobj
.kobj
,
1723 static void module_remove_modinfo_attrs(struct module
*mod
)
1725 struct module_attribute
*attr
;
1728 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1729 /* pick a field to test for end of list */
1730 if (!attr
->attr
.name
)
1732 sysfs_remove_file(&mod
->mkobj
.kobj
, &attr
->attr
);
1736 kfree(mod
->modinfo_attrs
);
1739 static void mod_kobject_put(struct module
*mod
)
1741 DECLARE_COMPLETION_ONSTACK(c
);
1742 mod
->mkobj
.kobj_completion
= &c
;
1743 kobject_put(&mod
->mkobj
.kobj
);
1744 wait_for_completion(&c
);
1747 static int mod_sysfs_init(struct module
*mod
)
1750 struct kobject
*kobj
;
1752 if (!module_sysfs_initialized
) {
1753 pr_err("%s: module sysfs not initialized\n", mod
->name
);
1758 kobj
= kset_find_obj(module_kset
, mod
->name
);
1760 pr_err("%s: module is already loaded\n", mod
->name
);
1766 mod
->mkobj
.mod
= mod
;
1768 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1769 mod
->mkobj
.kobj
.kset
= module_kset
;
1770 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1773 mod_kobject_put(mod
);
1775 /* delay uevent until full sysfs population */
1780 static int mod_sysfs_setup(struct module
*mod
,
1781 const struct load_info
*info
,
1782 struct kernel_param
*kparam
,
1783 unsigned int num_params
)
1787 err
= mod_sysfs_init(mod
);
1791 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1792 if (!mod
->holders_dir
) {
1797 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1799 goto out_unreg_holders
;
1801 err
= module_add_modinfo_attrs(mod
);
1803 goto out_unreg_param
;
1805 add_usage_links(mod
);
1806 add_sect_attrs(mod
, info
);
1807 add_notes_attrs(mod
, info
);
1809 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1813 module_param_sysfs_remove(mod
);
1815 kobject_put(mod
->holders_dir
);
1817 mod_kobject_put(mod
);
1822 static void mod_sysfs_fini(struct module
*mod
)
1824 remove_notes_attrs(mod
);
1825 remove_sect_attrs(mod
);
1826 mod_kobject_put(mod
);
1829 static void init_param_lock(struct module
*mod
)
1831 mutex_init(&mod
->param_lock
);
1833 #else /* !CONFIG_SYSFS */
1835 static int mod_sysfs_setup(struct module
*mod
,
1836 const struct load_info
*info
,
1837 struct kernel_param
*kparam
,
1838 unsigned int num_params
)
1843 static void mod_sysfs_fini(struct module
*mod
)
1847 static void module_remove_modinfo_attrs(struct module
*mod
)
1851 static void del_usage_links(struct module
*mod
)
1855 static void init_param_lock(struct module
*mod
)
1858 #endif /* CONFIG_SYSFS */
1860 static void mod_sysfs_teardown(struct module
*mod
)
1862 del_usage_links(mod
);
1863 module_remove_modinfo_attrs(mod
);
1864 module_param_sysfs_remove(mod
);
1865 kobject_put(mod
->mkobj
.drivers_dir
);
1866 kobject_put(mod
->holders_dir
);
1867 mod_sysfs_fini(mod
);
1870 #ifdef CONFIG_STRICT_MODULE_RWX
1872 * LKM RO/NX protection: protect module's text/ro-data
1873 * from modification and any data from execution.
1875 * General layout of module is:
1876 * [text] [read-only-data] [ro-after-init] [writable data]
1877 * text_size -----^ ^ ^ ^
1878 * ro_size ------------------------| | |
1879 * ro_after_init_size -----------------------------| |
1880 * size -----------------------------------------------------------|
1882 * These values are always page-aligned (as is base)
1884 static void frob_text(const struct module_layout
*layout
,
1885 int (*set_memory
)(unsigned long start
, int num_pages
))
1887 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1888 BUG_ON((unsigned long)layout
->text_size
& (PAGE_SIZE
-1));
1889 set_memory((unsigned long)layout
->base
,
1890 layout
->text_size
>> PAGE_SHIFT
);
1893 static void frob_rodata(const struct module_layout
*layout
,
1894 int (*set_memory
)(unsigned long start
, int num_pages
))
1896 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1897 BUG_ON((unsigned long)layout
->text_size
& (PAGE_SIZE
-1));
1898 BUG_ON((unsigned long)layout
->ro_size
& (PAGE_SIZE
-1));
1899 set_memory((unsigned long)layout
->base
+ layout
->text_size
,
1900 (layout
->ro_size
- layout
->text_size
) >> PAGE_SHIFT
);
1903 static void frob_ro_after_init(const struct module_layout
*layout
,
1904 int (*set_memory
)(unsigned long start
, int num_pages
))
1906 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1907 BUG_ON((unsigned long)layout
->ro_size
& (PAGE_SIZE
-1));
1908 BUG_ON((unsigned long)layout
->ro_after_init_size
& (PAGE_SIZE
-1));
1909 set_memory((unsigned long)layout
->base
+ layout
->ro_size
,
1910 (layout
->ro_after_init_size
- layout
->ro_size
) >> PAGE_SHIFT
);
1913 static void frob_writable_data(const struct module_layout
*layout
,
1914 int (*set_memory
)(unsigned long start
, int num_pages
))
1916 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1917 BUG_ON((unsigned long)layout
->ro_after_init_size
& (PAGE_SIZE
-1));
1918 BUG_ON((unsigned long)layout
->size
& (PAGE_SIZE
-1));
1919 set_memory((unsigned long)layout
->base
+ layout
->ro_after_init_size
,
1920 (layout
->size
- layout
->ro_after_init_size
) >> PAGE_SHIFT
);
1923 /* livepatching wants to disable read-only so it can frob module. */
1924 void module_disable_ro(const struct module
*mod
)
1926 if (!rodata_enabled
)
1929 frob_text(&mod
->core_layout
, set_memory_rw
);
1930 frob_rodata(&mod
->core_layout
, set_memory_rw
);
1931 frob_ro_after_init(&mod
->core_layout
, set_memory_rw
);
1932 frob_text(&mod
->init_layout
, set_memory_rw
);
1933 frob_rodata(&mod
->init_layout
, set_memory_rw
);
1936 void module_enable_ro(const struct module
*mod
, bool after_init
)
1938 if (!rodata_enabled
)
1941 frob_text(&mod
->core_layout
, set_memory_ro
);
1942 frob_rodata(&mod
->core_layout
, set_memory_ro
);
1943 frob_text(&mod
->init_layout
, set_memory_ro
);
1944 frob_rodata(&mod
->init_layout
, set_memory_ro
);
1947 frob_ro_after_init(&mod
->core_layout
, set_memory_ro
);
1950 static void module_enable_nx(const struct module
*mod
)
1952 frob_rodata(&mod
->core_layout
, set_memory_nx
);
1953 frob_ro_after_init(&mod
->core_layout
, set_memory_nx
);
1954 frob_writable_data(&mod
->core_layout
, set_memory_nx
);
1955 frob_rodata(&mod
->init_layout
, set_memory_nx
);
1956 frob_writable_data(&mod
->init_layout
, set_memory_nx
);
1959 static void module_disable_nx(const struct module
*mod
)
1961 frob_rodata(&mod
->core_layout
, set_memory_x
);
1962 frob_ro_after_init(&mod
->core_layout
, set_memory_x
);
1963 frob_writable_data(&mod
->core_layout
, set_memory_x
);
1964 frob_rodata(&mod
->init_layout
, set_memory_x
);
1965 frob_writable_data(&mod
->init_layout
, set_memory_x
);
1968 /* Iterate through all modules and set each module's text as RW */
1969 void set_all_modules_text_rw(void)
1973 if (!rodata_enabled
)
1976 mutex_lock(&module_mutex
);
1977 list_for_each_entry_rcu(mod
, &modules
, list
) {
1978 if (mod
->state
== MODULE_STATE_UNFORMED
)
1981 frob_text(&mod
->core_layout
, set_memory_rw
);
1982 frob_text(&mod
->init_layout
, set_memory_rw
);
1984 mutex_unlock(&module_mutex
);
1987 /* Iterate through all modules and set each module's text as RO */
1988 void set_all_modules_text_ro(void)
1992 if (!rodata_enabled
)
1995 mutex_lock(&module_mutex
);
1996 list_for_each_entry_rcu(mod
, &modules
, list
) {
1998 * Ignore going modules since it's possible that ro
1999 * protection has already been disabled, otherwise we'll
2000 * run into protection faults at module deallocation.
2002 if (mod
->state
== MODULE_STATE_UNFORMED
||
2003 mod
->state
== MODULE_STATE_GOING
)
2006 frob_text(&mod
->core_layout
, set_memory_ro
);
2007 frob_text(&mod
->init_layout
, set_memory_ro
);
2009 mutex_unlock(&module_mutex
);
2012 static void disable_ro_nx(const struct module_layout
*layout
)
2014 if (rodata_enabled
) {
2015 frob_text(layout
, set_memory_rw
);
2016 frob_rodata(layout
, set_memory_rw
);
2017 frob_ro_after_init(layout
, set_memory_rw
);
2019 frob_rodata(layout
, set_memory_x
);
2020 frob_ro_after_init(layout
, set_memory_x
);
2021 frob_writable_data(layout
, set_memory_x
);
2025 static void disable_ro_nx(const struct module_layout
*layout
) { }
2026 static void module_enable_nx(const struct module
*mod
) { }
2027 static void module_disable_nx(const struct module
*mod
) { }
2030 #ifdef CONFIG_LIVEPATCH
2032 * Persist Elf information about a module. Copy the Elf header,
2033 * section header table, section string table, and symtab section
2034 * index from info to mod->klp_info.
2036 static int copy_module_elf(struct module
*mod
, struct load_info
*info
)
2038 unsigned int size
, symndx
;
2041 size
= sizeof(*mod
->klp_info
);
2042 mod
->klp_info
= kmalloc(size
, GFP_KERNEL
);
2043 if (mod
->klp_info
== NULL
)
2047 size
= sizeof(mod
->klp_info
->hdr
);
2048 memcpy(&mod
->klp_info
->hdr
, info
->hdr
, size
);
2050 /* Elf section header table */
2051 size
= sizeof(*info
->sechdrs
) * info
->hdr
->e_shnum
;
2052 mod
->klp_info
->sechdrs
= kmalloc(size
, GFP_KERNEL
);
2053 if (mod
->klp_info
->sechdrs
== NULL
) {
2057 memcpy(mod
->klp_info
->sechdrs
, info
->sechdrs
, size
);
2059 /* Elf section name string table */
2060 size
= info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_size
;
2061 mod
->klp_info
->secstrings
= kmalloc(size
, GFP_KERNEL
);
2062 if (mod
->klp_info
->secstrings
== NULL
) {
2066 memcpy(mod
->klp_info
->secstrings
, info
->secstrings
, size
);
2068 /* Elf symbol section index */
2069 symndx
= info
->index
.sym
;
2070 mod
->klp_info
->symndx
= symndx
;
2073 * For livepatch modules, core_kallsyms.symtab is a complete
2074 * copy of the original symbol table. Adjust sh_addr to point
2075 * to core_kallsyms.symtab since the copy of the symtab in module
2076 * init memory is freed at the end of do_init_module().
2078 mod
->klp_info
->sechdrs
[symndx
].sh_addr
= \
2079 (unsigned long) mod
->core_kallsyms
.symtab
;
2084 kfree(mod
->klp_info
->sechdrs
);
2086 kfree(mod
->klp_info
);
2090 static void free_module_elf(struct module
*mod
)
2092 kfree(mod
->klp_info
->sechdrs
);
2093 kfree(mod
->klp_info
->secstrings
);
2094 kfree(mod
->klp_info
);
2096 #else /* !CONFIG_LIVEPATCH */
2097 static int copy_module_elf(struct module
*mod
, struct load_info
*info
)
2102 static void free_module_elf(struct module
*mod
)
2105 #endif /* CONFIG_LIVEPATCH */
2107 void __weak
module_memfree(void *module_region
)
2109 vfree(module_region
);
2112 void __weak
module_arch_cleanup(struct module
*mod
)
2116 void __weak
module_arch_freeing_init(struct module
*mod
)
2120 /* Free a module, remove from lists, etc. */
2121 static void free_module(struct module
*mod
)
2123 trace_module_free(mod
);
2125 mod_sysfs_teardown(mod
);
2127 /* We leave it in list to prevent duplicate loads, but make sure
2128 * that noone uses it while it's being deconstructed. */
2129 mutex_lock(&module_mutex
);
2130 mod
->state
= MODULE_STATE_UNFORMED
;
2131 mutex_unlock(&module_mutex
);
2133 /* Remove dynamic debug info */
2134 ddebug_remove_module(mod
->name
);
2136 /* Arch-specific cleanup. */
2137 module_arch_cleanup(mod
);
2139 /* Module unload stuff */
2140 module_unload_free(mod
);
2142 /* Free any allocated parameters. */
2143 destroy_params(mod
->kp
, mod
->num_kp
);
2145 if (is_livepatch_module(mod
))
2146 free_module_elf(mod
);
2148 /* Now we can delete it from the lists */
2149 mutex_lock(&module_mutex
);
2150 /* Unlink carefully: kallsyms could be walking list. */
2151 list_del_rcu(&mod
->list
);
2152 mod_tree_remove(mod
);
2153 /* Remove this module from bug list, this uses list_del_rcu */
2154 module_bug_cleanup(mod
);
2155 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2156 synchronize_sched();
2157 mutex_unlock(&module_mutex
);
2159 /* This may be empty, but that's OK */
2160 disable_ro_nx(&mod
->init_layout
);
2161 module_arch_freeing_init(mod
);
2162 module_memfree(mod
->init_layout
.base
);
2164 percpu_modfree(mod
);
2166 /* Free lock-classes; relies on the preceding sync_rcu(). */
2167 lockdep_free_key_range(mod
->core_layout
.base
, mod
->core_layout
.size
);
2169 /* Finally, free the core (containing the module structure) */
2170 disable_ro_nx(&mod
->core_layout
);
2171 module_memfree(mod
->core_layout
.base
);
2174 update_protections(current
->mm
);
2178 void *__symbol_get(const char *symbol
)
2180 struct module
*owner
;
2181 const struct kernel_symbol
*sym
;
2184 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
2185 if (sym
&& strong_try_module_get(owner
))
2189 return sym
? (void *)sym
->value
: NULL
;
2191 EXPORT_SYMBOL_GPL(__symbol_get
);
2194 * Ensure that an exported symbol [global namespace] does not already exist
2195 * in the kernel or in some other module's exported symbol table.
2197 * You must hold the module_mutex.
2199 static int verify_export_symbols(struct module
*mod
)
2202 struct module
*owner
;
2203 const struct kernel_symbol
*s
;
2205 const struct kernel_symbol
*sym
;
2208 { mod
->syms
, mod
->num_syms
},
2209 { mod
->gpl_syms
, mod
->num_gpl_syms
},
2210 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
2211 #ifdef CONFIG_UNUSED_SYMBOLS
2212 { mod
->unused_syms
, mod
->num_unused_syms
},
2213 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
2217 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
2218 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
2219 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
2220 pr_err("%s: exports duplicate symbol %s"
2222 mod
->name
, s
->name
, module_name(owner
));
2230 /* Change all symbols so that st_value encodes the pointer directly. */
2231 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
2233 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2234 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
2235 unsigned long secbase
;
2238 const struct kernel_symbol
*ksym
;
2240 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
2241 const char *name
= info
->strtab
+ sym
[i
].st_name
;
2243 switch (sym
[i
].st_shndx
) {
2245 /* Ignore common symbols */
2246 if (!strncmp(name
, "__gnu_lto", 9))
2249 /* We compiled with -fno-common. These are not
2250 supposed to happen. */
2251 pr_debug("Common symbol: %s\n", name
);
2252 pr_warn("%s: please compile with -fno-common\n",
2258 /* Don't need to do anything */
2259 pr_debug("Absolute symbol: 0x%08lx\n",
2260 (long)sym
[i
].st_value
);
2264 /* Livepatch symbols are resolved by livepatch */
2268 ksym
= resolve_symbol_wait(mod
, info
, name
);
2269 /* Ok if resolved. */
2270 if (ksym
&& !IS_ERR(ksym
)) {
2271 sym
[i
].st_value
= ksym
->value
;
2276 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
2279 pr_warn("%s: Unknown symbol %s (err %li)\n",
2280 mod
->name
, name
, PTR_ERR(ksym
));
2281 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
2285 /* Divert to percpu allocation if a percpu var. */
2286 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
2287 secbase
= (unsigned long)mod_percpu(mod
);
2289 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
2290 sym
[i
].st_value
+= secbase
;
2298 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
2303 /* Now do relocations. */
2304 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2305 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
2307 /* Not a valid relocation section? */
2308 if (infosec
>= info
->hdr
->e_shnum
)
2311 /* Don't bother with non-allocated sections */
2312 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
2315 /* Livepatch relocation sections are applied by livepatch */
2316 if (info
->sechdrs
[i
].sh_flags
& SHF_RELA_LIVEPATCH
)
2319 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
2320 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
2321 info
->index
.sym
, i
, mod
);
2322 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
2323 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
2324 info
->index
.sym
, i
, mod
);
2331 /* Additional bytes needed by arch in front of individual sections */
2332 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
2333 unsigned int section
)
2335 /* default implementation just returns zero */
2339 /* Update size with this section: return offset. */
2340 static long get_offset(struct module
*mod
, unsigned int *size
,
2341 Elf_Shdr
*sechdr
, unsigned int section
)
2345 *size
+= arch_mod_section_prepend(mod
, section
);
2346 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
2347 *size
= ret
+ sechdr
->sh_size
;
2351 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2352 might -- code, read-only data, read-write data, small data. Tally
2353 sizes, and place the offsets into sh_entsize fields: high bit means it
2355 static void layout_sections(struct module
*mod
, struct load_info
*info
)
2357 static unsigned long const masks
[][2] = {
2358 /* NOTE: all executable code must be the first section
2359 * in this array; otherwise modify the text_size
2360 * finder in the two loops below */
2361 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2362 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
2363 { SHF_RO_AFTER_INIT
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2364 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2365 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
2369 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
2370 info
->sechdrs
[i
].sh_entsize
= ~0UL;
2372 pr_debug("Core section allocation order:\n");
2373 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2374 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2375 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2376 const char *sname
= info
->secstrings
+ s
->sh_name
;
2378 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2379 || (s
->sh_flags
& masks
[m
][1])
2380 || s
->sh_entsize
!= ~0UL
2381 || strstarts(sname
, ".init"))
2383 s
->sh_entsize
= get_offset(mod
, &mod
->core_layout
.size
, s
, i
);
2384 pr_debug("\t%s\n", sname
);
2387 case 0: /* executable */
2388 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2389 mod
->core_layout
.text_size
= mod
->core_layout
.size
;
2391 case 1: /* RO: text and ro-data */
2392 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2393 mod
->core_layout
.ro_size
= mod
->core_layout
.size
;
2395 case 2: /* RO after init */
2396 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2397 mod
->core_layout
.ro_after_init_size
= mod
->core_layout
.size
;
2399 case 4: /* whole core */
2400 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2405 pr_debug("Init section allocation order:\n");
2406 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2407 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2408 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2409 const char *sname
= info
->secstrings
+ s
->sh_name
;
2411 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2412 || (s
->sh_flags
& masks
[m
][1])
2413 || s
->sh_entsize
!= ~0UL
2414 || !strstarts(sname
, ".init"))
2416 s
->sh_entsize
= (get_offset(mod
, &mod
->init_layout
.size
, s
, i
)
2417 | INIT_OFFSET_MASK
);
2418 pr_debug("\t%s\n", sname
);
2421 case 0: /* executable */
2422 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2423 mod
->init_layout
.text_size
= mod
->init_layout
.size
;
2425 case 1: /* RO: text and ro-data */
2426 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2427 mod
->init_layout
.ro_size
= mod
->init_layout
.size
;
2431 * RO after init doesn't apply to init_layout (only
2432 * core_layout), so it just takes the value of ro_size.
2434 mod
->init_layout
.ro_after_init_size
= mod
->init_layout
.ro_size
;
2436 case 4: /* whole init */
2437 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2443 static void set_license(struct module
*mod
, const char *license
)
2446 license
= "unspecified";
2448 if (!license_is_gpl_compatible(license
)) {
2449 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
2450 pr_warn("%s: module license '%s' taints kernel.\n",
2451 mod
->name
, license
);
2452 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
2453 LOCKDEP_NOW_UNRELIABLE
);
2457 /* Parse tag=value strings from .modinfo section */
2458 static char *next_string(char *string
, unsigned long *secsize
)
2460 /* Skip non-zero chars */
2463 if ((*secsize
)-- <= 1)
2467 /* Skip any zero padding. */
2468 while (!string
[0]) {
2470 if ((*secsize
)-- <= 1)
2476 static char *get_modinfo(struct load_info
*info
, const char *tag
)
2479 unsigned int taglen
= strlen(tag
);
2480 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
2481 unsigned long size
= infosec
->sh_size
;
2483 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
2484 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
2485 return p
+ taglen
+ 1;
2490 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2492 struct module_attribute
*attr
;
2495 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2497 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2501 static void free_modinfo(struct module
*mod
)
2503 struct module_attribute
*attr
;
2506 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2512 #ifdef CONFIG_KALLSYMS
2514 /* lookup symbol in given range of kernel_symbols */
2515 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2516 const struct kernel_symbol
*start
,
2517 const struct kernel_symbol
*stop
)
2519 return bsearch(name
, start
, stop
- start
,
2520 sizeof(struct kernel_symbol
), cmp_name
);
2523 static int is_exported(const char *name
, unsigned long value
,
2524 const struct module
*mod
)
2526 const struct kernel_symbol
*ks
;
2528 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2530 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2531 return ks
!= NULL
&& ks
->value
== value
;
2535 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2537 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2539 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2540 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2545 if (sym
->st_shndx
== SHN_UNDEF
)
2547 if (sym
->st_shndx
== SHN_ABS
|| sym
->st_shndx
== info
->index
.pcpu
)
2549 if (sym
->st_shndx
>= SHN_LORESERVE
)
2551 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2553 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2554 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2555 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2557 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2562 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2563 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2568 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2575 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2576 unsigned int shnum
, unsigned int pcpundx
)
2578 const Elf_Shdr
*sec
;
2580 if (src
->st_shndx
== SHN_UNDEF
2581 || src
->st_shndx
>= shnum
2585 #ifdef CONFIG_KALLSYMS_ALL
2586 if (src
->st_shndx
== pcpundx
)
2590 sec
= sechdrs
+ src
->st_shndx
;
2591 if (!(sec
->sh_flags
& SHF_ALLOC
)
2592 #ifndef CONFIG_KALLSYMS_ALL
2593 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2595 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2602 * We only allocate and copy the strings needed by the parts of symtab
2603 * we keep. This is simple, but has the effect of making multiple
2604 * copies of duplicates. We could be more sophisticated, see
2605 * linux-kernel thread starting with
2606 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2608 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2610 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2611 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2613 unsigned int i
, nsrc
, ndst
, strtab_size
= 0;
2615 /* Put symbol section at end of init part of module. */
2616 symsect
->sh_flags
|= SHF_ALLOC
;
2617 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_layout
.size
, symsect
,
2618 info
->index
.sym
) | INIT_OFFSET_MASK
;
2619 pr_debug("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2621 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2622 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2624 /* Compute total space required for the core symbols' strtab. */
2625 for (ndst
= i
= 0; i
< nsrc
; i
++) {
2626 if (i
== 0 || is_livepatch_module(mod
) ||
2627 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
,
2628 info
->index
.pcpu
)) {
2629 strtab_size
+= strlen(&info
->strtab
[src
[i
].st_name
])+1;
2634 /* Append room for core symbols at end of core part. */
2635 info
->symoffs
= ALIGN(mod
->core_layout
.size
, symsect
->sh_addralign
?: 1);
2636 info
->stroffs
= mod
->core_layout
.size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2637 mod
->core_layout
.size
+= strtab_size
;
2638 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2640 /* Put string table section at end of init part of module. */
2641 strsect
->sh_flags
|= SHF_ALLOC
;
2642 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_layout
.size
, strsect
,
2643 info
->index
.str
) | INIT_OFFSET_MASK
;
2644 pr_debug("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2646 /* We'll tack temporary mod_kallsyms on the end. */
2647 mod
->init_layout
.size
= ALIGN(mod
->init_layout
.size
,
2648 __alignof__(struct mod_kallsyms
));
2649 info
->mod_kallsyms_init_off
= mod
->init_layout
.size
;
2650 mod
->init_layout
.size
+= sizeof(struct mod_kallsyms
);
2651 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2655 * We use the full symtab and strtab which layout_symtab arranged to
2656 * be appended to the init section. Later we switch to the cut-down
2659 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2661 unsigned int i
, ndst
;
2665 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2667 /* Set up to point into init section. */
2668 mod
->kallsyms
= mod
->init_layout
.base
+ info
->mod_kallsyms_init_off
;
2670 mod
->kallsyms
->symtab
= (void *)symsec
->sh_addr
;
2671 mod
->kallsyms
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2672 /* Make sure we get permanent strtab: don't use info->strtab. */
2673 mod
->kallsyms
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2675 /* Set types up while we still have access to sections. */
2676 for (i
= 0; i
< mod
->kallsyms
->num_symtab
; i
++)
2677 mod
->kallsyms
->symtab
[i
].st_info
2678 = elf_type(&mod
->kallsyms
->symtab
[i
], info
);
2680 /* Now populate the cut down core kallsyms for after init. */
2681 mod
->core_kallsyms
.symtab
= dst
= mod
->core_layout
.base
+ info
->symoffs
;
2682 mod
->core_kallsyms
.strtab
= s
= mod
->core_layout
.base
+ info
->stroffs
;
2683 src
= mod
->kallsyms
->symtab
;
2684 for (ndst
= i
= 0; i
< mod
->kallsyms
->num_symtab
; i
++) {
2685 if (i
== 0 || is_livepatch_module(mod
) ||
2686 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
,
2687 info
->index
.pcpu
)) {
2689 dst
[ndst
++].st_name
= s
- mod
->core_kallsyms
.strtab
;
2690 s
+= strlcpy(s
, &mod
->kallsyms
->strtab
[src
[i
].st_name
],
2694 mod
->core_kallsyms
.num_symtab
= ndst
;
2697 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2701 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2704 #endif /* CONFIG_KALLSYMS */
2706 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2710 #ifdef CONFIG_DYNAMIC_DEBUG
2711 if (ddebug_add_module(debug
, num
, debug
->modname
))
2712 pr_err("dynamic debug error adding module: %s\n",
2717 static void dynamic_debug_remove(struct _ddebug
*debug
)
2720 ddebug_remove_module(debug
->modname
);
2723 void * __weak
module_alloc(unsigned long size
)
2725 return vmalloc_exec(size
);
2728 #ifdef CONFIG_DEBUG_KMEMLEAK
2729 static void kmemleak_load_module(const struct module
*mod
,
2730 const struct load_info
*info
)
2734 /* only scan the sections containing data */
2735 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2737 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2738 /* Scan all writable sections that's not executable */
2739 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
) ||
2740 !(info
->sechdrs
[i
].sh_flags
& SHF_WRITE
) ||
2741 (info
->sechdrs
[i
].sh_flags
& SHF_EXECINSTR
))
2744 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2745 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2749 static inline void kmemleak_load_module(const struct module
*mod
,
2750 const struct load_info
*info
)
2755 #ifdef CONFIG_MODULE_SIG
2756 static int module_sig_check(struct load_info
*info
, int flags
)
2759 const unsigned long markerlen
= sizeof(MODULE_SIG_STRING
) - 1;
2760 const void *mod
= info
->hdr
;
2763 * Require flags == 0, as a module with version information
2764 * removed is no longer the module that was signed
2767 info
->len
> markerlen
&&
2768 memcmp(mod
+ info
->len
- markerlen
, MODULE_SIG_STRING
, markerlen
) == 0) {
2769 /* We truncate the module to discard the signature */
2770 info
->len
-= markerlen
;
2771 err
= mod_verify_sig(mod
, &info
->len
);
2775 info
->sig_ok
= true;
2779 /* Not having a signature is only an error if we're strict. */
2780 if (err
== -ENOKEY
&& !sig_enforce
)
2785 #else /* !CONFIG_MODULE_SIG */
2786 static int module_sig_check(struct load_info
*info
, int flags
)
2790 #endif /* !CONFIG_MODULE_SIG */
2792 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2793 static int elf_header_check(struct load_info
*info
)
2795 if (info
->len
< sizeof(*(info
->hdr
)))
2798 if (memcmp(info
->hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2799 || info
->hdr
->e_type
!= ET_REL
2800 || !elf_check_arch(info
->hdr
)
2801 || info
->hdr
->e_shentsize
!= sizeof(Elf_Shdr
))
2804 if (info
->hdr
->e_shoff
>= info
->len
2805 || (info
->hdr
->e_shnum
* sizeof(Elf_Shdr
) >
2806 info
->len
- info
->hdr
->e_shoff
))
2812 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2814 static int copy_chunked_from_user(void *dst
, const void __user
*usrc
, unsigned long len
)
2817 unsigned long n
= min(len
, COPY_CHUNK_SIZE
);
2819 if (copy_from_user(dst
, usrc
, n
) != 0)
2829 #ifdef CONFIG_LIVEPATCH
2830 static int check_modinfo_livepatch(struct module
*mod
, struct load_info
*info
)
2832 if (get_modinfo(info
, "livepatch")) {
2834 add_taint_module(mod
, TAINT_LIVEPATCH
, LOCKDEP_STILL_OK
);
2835 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2841 #else /* !CONFIG_LIVEPATCH */
2842 static int check_modinfo_livepatch(struct module
*mod
, struct load_info
*info
)
2844 if (get_modinfo(info
, "livepatch")) {
2845 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2852 #endif /* CONFIG_LIVEPATCH */
2854 /* Sets info->hdr and info->len. */
2855 static int copy_module_from_user(const void __user
*umod
, unsigned long len
,
2856 struct load_info
*info
)
2861 if (info
->len
< sizeof(*(info
->hdr
)))
2864 err
= security_kernel_read_file(NULL
, READING_MODULE
);
2868 /* Suck in entire file: we'll want most of it. */
2869 info
->hdr
= __vmalloc(info
->len
,
2870 GFP_KERNEL
| __GFP_NOWARN
, PAGE_KERNEL
);
2874 if (copy_chunked_from_user(info
->hdr
, umod
, info
->len
) != 0) {
2882 static void free_copy(struct load_info
*info
)
2887 static int rewrite_section_headers(struct load_info
*info
, int flags
)
2891 /* This should always be true, but let's be sure. */
2892 info
->sechdrs
[0].sh_addr
= 0;
2894 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2895 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2896 if (shdr
->sh_type
!= SHT_NOBITS
2897 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2898 pr_err("Module len %lu truncated\n", info
->len
);
2902 /* Mark all sections sh_addr with their address in the
2904 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2906 #ifndef CONFIG_MODULE_UNLOAD
2907 /* Don't load .exit sections */
2908 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2909 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2913 /* Track but don't keep modinfo and version sections. */
2914 if (flags
& MODULE_INIT_IGNORE_MODVERSIONS
)
2915 info
->index
.vers
= 0; /* Pretend no __versions section! */
2917 info
->index
.vers
= find_sec(info
, "__versions");
2918 info
->index
.info
= find_sec(info
, ".modinfo");
2919 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2920 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2925 * Set up our basic convenience variables (pointers to section headers,
2926 * search for module section index etc), and do some basic section
2929 * Return the temporary module pointer (we'll replace it with the final
2930 * one when we move the module sections around).
2932 static struct module
*setup_load_info(struct load_info
*info
, int flags
)
2938 /* Set up the convenience variables */
2939 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2940 info
->secstrings
= (void *)info
->hdr
2941 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2943 err
= rewrite_section_headers(info
, flags
);
2945 return ERR_PTR(err
);
2947 /* Find internal symbols and strings. */
2948 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2949 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2950 info
->index
.sym
= i
;
2951 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2952 info
->strtab
= (char *)info
->hdr
2953 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2958 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2959 if (!info
->index
.mod
) {
2960 pr_warn("No module found in object\n");
2961 return ERR_PTR(-ENOEXEC
);
2963 /* This is temporary: point mod into copy of data. */
2964 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2966 if (info
->index
.sym
== 0) {
2967 pr_warn("%s: module has no symbols (stripped?)\n", mod
->name
);
2968 return ERR_PTR(-ENOEXEC
);
2971 info
->index
.pcpu
= find_pcpusec(info
);
2973 /* Check module struct version now, before we try to use module. */
2974 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2975 return ERR_PTR(-ENOEXEC
);
2980 static int check_modinfo(struct module
*mod
, struct load_info
*info
, int flags
)
2982 const char *modmagic
= get_modinfo(info
, "vermagic");
2985 if (flags
& MODULE_INIT_IGNORE_VERMAGIC
)
2988 /* This is allowed: modprobe --force will invalidate it. */
2990 err
= try_to_force_load(mod
, "bad vermagic");
2993 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2994 pr_err("%s: version magic '%s' should be '%s'\n",
2995 mod
->name
, modmagic
, vermagic
);
2999 if (!get_modinfo(info
, "intree")) {
3000 if (!test_taint(TAINT_OOT_MODULE
))
3001 pr_warn("%s: loading out-of-tree module taints kernel.\n",
3003 add_taint_module(mod
, TAINT_OOT_MODULE
, LOCKDEP_STILL_OK
);
3006 if (get_modinfo(info
, "staging")) {
3007 add_taint_module(mod
, TAINT_CRAP
, LOCKDEP_STILL_OK
);
3008 pr_warn("%s: module is from the staging directory, the quality "
3009 "is unknown, you have been warned.\n", mod
->name
);
3012 err
= check_modinfo_livepatch(mod
, info
);
3016 /* Set up license info based on the info section */
3017 set_license(mod
, get_modinfo(info
, "license"));
3022 static int find_module_sections(struct module
*mod
, struct load_info
*info
)
3024 mod
->kp
= section_objs(info
, "__param",
3025 sizeof(*mod
->kp
), &mod
->num_kp
);
3026 mod
->syms
= section_objs(info
, "__ksymtab",
3027 sizeof(*mod
->syms
), &mod
->num_syms
);
3028 mod
->crcs
= section_addr(info
, "__kcrctab");
3029 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
3030 sizeof(*mod
->gpl_syms
),
3031 &mod
->num_gpl_syms
);
3032 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
3033 mod
->gpl_future_syms
= section_objs(info
,
3034 "__ksymtab_gpl_future",
3035 sizeof(*mod
->gpl_future_syms
),
3036 &mod
->num_gpl_future_syms
);
3037 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
3039 #ifdef CONFIG_UNUSED_SYMBOLS
3040 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
3041 sizeof(*mod
->unused_syms
),
3042 &mod
->num_unused_syms
);
3043 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
3044 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
3045 sizeof(*mod
->unused_gpl_syms
),
3046 &mod
->num_unused_gpl_syms
);
3047 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
3049 #ifdef CONFIG_CONSTRUCTORS
3050 mod
->ctors
= section_objs(info
, ".ctors",
3051 sizeof(*mod
->ctors
), &mod
->num_ctors
);
3053 mod
->ctors
= section_objs(info
, ".init_array",
3054 sizeof(*mod
->ctors
), &mod
->num_ctors
);
3055 else if (find_sec(info
, ".init_array")) {
3057 * This shouldn't happen with same compiler and binutils
3058 * building all parts of the module.
3060 pr_warn("%s: has both .ctors and .init_array.\n",
3066 #ifdef CONFIG_TRACEPOINTS
3067 mod
->tracepoints_ptrs
= section_objs(info
, "__tracepoints_ptrs",
3068 sizeof(*mod
->tracepoints_ptrs
),
3069 &mod
->num_tracepoints
);
3071 #ifdef HAVE_JUMP_LABEL
3072 mod
->jump_entries
= section_objs(info
, "__jump_table",
3073 sizeof(*mod
->jump_entries
),
3074 &mod
->num_jump_entries
);
3076 #ifdef CONFIG_EVENT_TRACING
3077 mod
->trace_events
= section_objs(info
, "_ftrace_events",
3078 sizeof(*mod
->trace_events
),
3079 &mod
->num_trace_events
);
3080 mod
->trace_enums
= section_objs(info
, "_ftrace_enum_map",
3081 sizeof(*mod
->trace_enums
),
3082 &mod
->num_trace_enums
);
3084 #ifdef CONFIG_TRACING
3085 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
3086 sizeof(*mod
->trace_bprintk_fmt_start
),
3087 &mod
->num_trace_bprintk_fmt
);
3089 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3090 /* sechdrs[0].sh_size is always zero */
3091 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
3092 sizeof(*mod
->ftrace_callsites
),
3093 &mod
->num_ftrace_callsites
);
3096 mod
->extable
= section_objs(info
, "__ex_table",
3097 sizeof(*mod
->extable
), &mod
->num_exentries
);
3099 if (section_addr(info
, "__obsparm"))
3100 pr_warn("%s: Ignoring obsolete parameters\n", mod
->name
);
3102 info
->debug
= section_objs(info
, "__verbose",
3103 sizeof(*info
->debug
), &info
->num_debug
);
3108 static int move_module(struct module
*mod
, struct load_info
*info
)
3113 /* Do the allocs. */
3114 ptr
= module_alloc(mod
->core_layout
.size
);
3116 * The pointer to this block is stored in the module structure
3117 * which is inside the block. Just mark it as not being a
3120 kmemleak_not_leak(ptr
);
3124 memset(ptr
, 0, mod
->core_layout
.size
);
3125 mod
->core_layout
.base
= ptr
;
3127 if (mod
->init_layout
.size
) {
3128 ptr
= module_alloc(mod
->init_layout
.size
);
3130 * The pointer to this block is stored in the module structure
3131 * which is inside the block. This block doesn't need to be
3132 * scanned as it contains data and code that will be freed
3133 * after the module is initialized.
3135 kmemleak_ignore(ptr
);
3137 module_memfree(mod
->core_layout
.base
);
3140 memset(ptr
, 0, mod
->init_layout
.size
);
3141 mod
->init_layout
.base
= ptr
;
3143 mod
->init_layout
.base
= NULL
;
3145 /* Transfer each section which specifies SHF_ALLOC */
3146 pr_debug("final section addresses:\n");
3147 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
3149 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
3151 if (!(shdr
->sh_flags
& SHF_ALLOC
))
3154 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
3155 dest
= mod
->init_layout
.base
3156 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
3158 dest
= mod
->core_layout
.base
+ shdr
->sh_entsize
;
3160 if (shdr
->sh_type
!= SHT_NOBITS
)
3161 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
3162 /* Update sh_addr to point to copy in image. */
3163 shdr
->sh_addr
= (unsigned long)dest
;
3164 pr_debug("\t0x%lx %s\n",
3165 (long)shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
3171 static int check_module_license_and_versions(struct module
*mod
)
3173 int prev_taint
= test_taint(TAINT_PROPRIETARY_MODULE
);
3176 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3177 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3178 * using GPL-only symbols it needs.
3180 if (strcmp(mod
->name
, "ndiswrapper") == 0)
3181 add_taint(TAINT_PROPRIETARY_MODULE
, LOCKDEP_NOW_UNRELIABLE
);
3183 /* driverloader was caught wrongly pretending to be under GPL */
3184 if (strcmp(mod
->name
, "driverloader") == 0)
3185 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
3186 LOCKDEP_NOW_UNRELIABLE
);
3188 /* lve claims to be GPL but upstream won't provide source */
3189 if (strcmp(mod
->name
, "lve") == 0)
3190 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
3191 LOCKDEP_NOW_UNRELIABLE
);
3193 if (!prev_taint
&& test_taint(TAINT_PROPRIETARY_MODULE
))
3194 pr_warn("%s: module license taints kernel.\n", mod
->name
);
3196 #ifdef CONFIG_MODVERSIONS
3197 if ((mod
->num_syms
&& !mod
->crcs
)
3198 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
3199 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
3200 #ifdef CONFIG_UNUSED_SYMBOLS
3201 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
3202 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
3205 return try_to_force_load(mod
,
3206 "no versions for exported symbols");
3212 static void flush_module_icache(const struct module
*mod
)
3214 mm_segment_t old_fs
;
3216 /* flush the icache in correct context */
3221 * Flush the instruction cache, since we've played with text.
3222 * Do it before processing of module parameters, so the module
3223 * can provide parameter accessor functions of its own.
3225 if (mod
->init_layout
.base
)
3226 flush_icache_range((unsigned long)mod
->init_layout
.base
,
3227 (unsigned long)mod
->init_layout
.base
3228 + mod
->init_layout
.size
);
3229 flush_icache_range((unsigned long)mod
->core_layout
.base
,
3230 (unsigned long)mod
->core_layout
.base
+ mod
->core_layout
.size
);
3235 int __weak
module_frob_arch_sections(Elf_Ehdr
*hdr
,
3243 /* module_blacklist is a comma-separated list of module names */
3244 static char *module_blacklist
;
3245 static bool blacklisted(char *module_name
)
3250 if (!module_blacklist
)
3253 for (p
= module_blacklist
; *p
; p
+= len
) {
3254 len
= strcspn(p
, ",");
3255 if (strlen(module_name
) == len
&& !memcmp(module_name
, p
, len
))
3262 core_param(module_blacklist
, module_blacklist
, charp
, 0400);
3264 static struct module
*layout_and_allocate(struct load_info
*info
, int flags
)
3266 /* Module within temporary copy. */
3271 mod
= setup_load_info(info
, flags
);
3275 if (blacklisted(mod
->name
))
3276 return ERR_PTR(-EPERM
);
3278 err
= check_modinfo(mod
, info
, flags
);
3280 return ERR_PTR(err
);
3282 /* Allow arches to frob section contents and sizes. */
3283 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
3284 info
->secstrings
, mod
);
3286 return ERR_PTR(err
);
3288 /* We will do a special allocation for per-cpu sections later. */
3289 info
->sechdrs
[info
->index
.pcpu
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
3292 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3293 * layout_sections() can put it in the right place.
3294 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3296 ndx
= find_sec(info
, ".data..ro_after_init");
3298 info
->sechdrs
[ndx
].sh_flags
|= SHF_RO_AFTER_INIT
;
3300 /* Determine total sizes, and put offsets in sh_entsize. For now
3301 this is done generically; there doesn't appear to be any
3302 special cases for the architectures. */
3303 layout_sections(mod
, info
);
3304 layout_symtab(mod
, info
);
3306 /* Allocate and move to the final place */
3307 err
= move_module(mod
, info
);
3309 return ERR_PTR(err
);
3311 /* Module has been copied to its final place now: return it. */
3312 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
3313 kmemleak_load_module(mod
, info
);
3317 /* mod is no longer valid after this! */
3318 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
3320 percpu_modfree(mod
);
3321 module_arch_freeing_init(mod
);
3322 module_memfree(mod
->init_layout
.base
);
3323 module_memfree(mod
->core_layout
.base
);
3326 int __weak
module_finalize(const Elf_Ehdr
*hdr
,
3327 const Elf_Shdr
*sechdrs
,
3333 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
3335 /* Sort exception table now relocations are done. */
3336 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
3338 /* Copy relocated percpu area over. */
3339 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
3340 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
3342 /* Setup kallsyms-specific fields. */
3343 add_kallsyms(mod
, info
);
3345 /* Arch-specific module finalizing. */
3346 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
3349 /* Is this module of this name done loading? No locks held. */
3350 static bool finished_loading(const char *name
)
3356 * The module_mutex should not be a heavily contended lock;
3357 * if we get the occasional sleep here, we'll go an extra iteration
3358 * in the wait_event_interruptible(), which is harmless.
3360 sched_annotate_sleep();
3361 mutex_lock(&module_mutex
);
3362 mod
= find_module_all(name
, strlen(name
), true);
3363 ret
= !mod
|| mod
->state
== MODULE_STATE_LIVE
3364 || mod
->state
== MODULE_STATE_GOING
;
3365 mutex_unlock(&module_mutex
);
3370 /* Call module constructors. */
3371 static void do_mod_ctors(struct module
*mod
)
3373 #ifdef CONFIG_CONSTRUCTORS
3376 for (i
= 0; i
< mod
->num_ctors
; i
++)
3381 /* For freeing module_init on success, in case kallsyms traversing */
3382 struct mod_initfree
{
3383 struct rcu_head rcu
;
3387 static void do_free_init(struct rcu_head
*head
)
3389 struct mod_initfree
*m
= container_of(head
, struct mod_initfree
, rcu
);
3390 module_memfree(m
->module_init
);
3395 * This is where the real work happens.
3397 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3398 * helper command 'lx-symbols'.
3400 static noinline
int do_init_module(struct module
*mod
)
3403 struct mod_initfree
*freeinit
;
3405 freeinit
= kmalloc(sizeof(*freeinit
), GFP_KERNEL
);
3410 freeinit
->module_init
= mod
->init_layout
.base
;
3413 * We want to find out whether @mod uses async during init. Clear
3414 * PF_USED_ASYNC. async_schedule*() will set it.
3416 current
->flags
&= ~PF_USED_ASYNC
;
3419 /* Start the module */
3420 if (mod
->init
!= NULL
)
3421 ret
= do_one_initcall(mod
->init
);
3423 goto fail_free_freeinit
;
3426 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3427 "follow 0/-E convention\n"
3428 "%s: loading module anyway...\n",
3429 __func__
, mod
->name
, ret
, __func__
);
3433 /* Now it's a first class citizen! */
3434 mod
->state
= MODULE_STATE_LIVE
;
3435 blocking_notifier_call_chain(&module_notify_list
,
3436 MODULE_STATE_LIVE
, mod
);
3439 * We need to finish all async code before the module init sequence
3440 * is done. This has potential to deadlock. For example, a newly
3441 * detected block device can trigger request_module() of the
3442 * default iosched from async probing task. Once userland helper
3443 * reaches here, async_synchronize_full() will wait on the async
3444 * task waiting on request_module() and deadlock.
3446 * This deadlock is avoided by perfomring async_synchronize_full()
3447 * iff module init queued any async jobs. This isn't a full
3448 * solution as it will deadlock the same if module loading from
3449 * async jobs nests more than once; however, due to the various
3450 * constraints, this hack seems to be the best option for now.
3451 * Please refer to the following thread for details.
3453 * http://thread.gmane.org/gmane.linux.kernel/1420814
3455 if (!mod
->async_probe_requested
&& (current
->flags
& PF_USED_ASYNC
))
3456 async_synchronize_full();
3458 mutex_lock(&module_mutex
);
3459 /* Drop initial reference. */
3461 trim_init_extable(mod
);
3462 #ifdef CONFIG_KALLSYMS
3463 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3464 rcu_assign_pointer(mod
->kallsyms
, &mod
->core_kallsyms
);
3466 module_enable_ro(mod
, true);
3467 mod_tree_remove_init(mod
);
3468 disable_ro_nx(&mod
->init_layout
);
3469 module_arch_freeing_init(mod
);
3470 mod
->init_layout
.base
= NULL
;
3471 mod
->init_layout
.size
= 0;
3472 mod
->init_layout
.ro_size
= 0;
3473 mod
->init_layout
.ro_after_init_size
= 0;
3474 mod
->init_layout
.text_size
= 0;
3476 * We want to free module_init, but be aware that kallsyms may be
3477 * walking this with preempt disabled. In all the failure paths, we
3478 * call synchronize_sched(), but we don't want to slow down the success
3479 * path, so use actual RCU here.
3481 call_rcu_sched(&freeinit
->rcu
, do_free_init
);
3482 mutex_unlock(&module_mutex
);
3483 wake_up_all(&module_wq
);
3490 /* Try to protect us from buggy refcounters. */
3491 mod
->state
= MODULE_STATE_GOING
;
3492 synchronize_sched();
3494 blocking_notifier_call_chain(&module_notify_list
,
3495 MODULE_STATE_GOING
, mod
);
3496 klp_module_going(mod
);
3497 ftrace_release_mod(mod
);
3499 wake_up_all(&module_wq
);
3503 static int may_init_module(void)
3505 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
3512 * We try to place it in the list now to make sure it's unique before
3513 * we dedicate too many resources. In particular, temporary percpu
3514 * memory exhaustion.
3516 static int add_unformed_module(struct module
*mod
)
3521 mod
->state
= MODULE_STATE_UNFORMED
;
3524 mutex_lock(&module_mutex
);
3525 old
= find_module_all(mod
->name
, strlen(mod
->name
), true);
3527 if (old
->state
== MODULE_STATE_COMING
3528 || old
->state
== MODULE_STATE_UNFORMED
) {
3529 /* Wait in case it fails to load. */
3530 mutex_unlock(&module_mutex
);
3531 err
= wait_event_interruptible(module_wq
,
3532 finished_loading(mod
->name
));
3540 mod_update_bounds(mod
);
3541 list_add_rcu(&mod
->list
, &modules
);
3542 mod_tree_insert(mod
);
3546 mutex_unlock(&module_mutex
);
3551 static int complete_formation(struct module
*mod
, struct load_info
*info
)
3555 mutex_lock(&module_mutex
);
3557 /* Find duplicate symbols (must be called under lock). */
3558 err
= verify_export_symbols(mod
);
3562 /* This relies on module_mutex for list integrity. */
3563 module_bug_finalize(info
->hdr
, info
->sechdrs
, mod
);
3565 module_enable_ro(mod
, false);
3566 module_enable_nx(mod
);
3568 /* Mark state as coming so strong_try_module_get() ignores us,
3569 * but kallsyms etc. can see us. */
3570 mod
->state
= MODULE_STATE_COMING
;
3571 mutex_unlock(&module_mutex
);
3576 mutex_unlock(&module_mutex
);
3580 static int prepare_coming_module(struct module
*mod
)
3584 ftrace_module_enable(mod
);
3585 err
= klp_module_coming(mod
);
3589 blocking_notifier_call_chain(&module_notify_list
,
3590 MODULE_STATE_COMING
, mod
);
3594 static int unknown_module_param_cb(char *param
, char *val
, const char *modname
,
3597 struct module
*mod
= arg
;
3600 if (strcmp(param
, "async_probe") == 0) {
3601 mod
->async_probe_requested
= true;
3605 /* Check for magic 'dyndbg' arg */
3606 ret
= ddebug_dyndbg_module_param_cb(param
, val
, modname
);
3608 pr_warn("%s: unknown parameter '%s' ignored\n", modname
, param
);
3612 /* Allocate and load the module: note that size of section 0 is always
3613 zero, and we rely on this for optional sections. */
3614 static int load_module(struct load_info
*info
, const char __user
*uargs
,
3621 err
= module_sig_check(info
, flags
);
3625 err
= elf_header_check(info
);
3629 /* Figure out module layout, and allocate all the memory. */
3630 mod
= layout_and_allocate(info
, flags
);
3636 audit_log_kern_module(mod
->name
);
3638 /* Reserve our place in the list. */
3639 err
= add_unformed_module(mod
);
3643 #ifdef CONFIG_MODULE_SIG
3644 mod
->sig_ok
= info
->sig_ok
;
3646 pr_notice_once("%s: module verification failed: signature "
3647 "and/or required key missing - tainting "
3648 "kernel\n", mod
->name
);
3649 add_taint_module(mod
, TAINT_UNSIGNED_MODULE
, LOCKDEP_STILL_OK
);
3653 /* To avoid stressing percpu allocator, do this once we're unique. */
3654 err
= percpu_modalloc(mod
, info
);
3658 /* Now module is in final location, initialize linked lists, etc. */
3659 err
= module_unload_init(mod
);
3663 init_param_lock(mod
);
3665 /* Now we've got everything in the final locations, we can
3666 * find optional sections. */
3667 err
= find_module_sections(mod
, info
);
3671 err
= check_module_license_and_versions(mod
);
3675 /* Set up MODINFO_ATTR fields */
3676 setup_modinfo(mod
, info
);
3678 /* Fix up syms, so that st_value is a pointer to location. */
3679 err
= simplify_symbols(mod
, info
);
3683 err
= apply_relocations(mod
, info
);
3687 err
= post_relocation(mod
, info
);
3691 flush_module_icache(mod
);
3693 /* Now copy in args */
3694 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
3695 if (IS_ERR(mod
->args
)) {
3696 err
= PTR_ERR(mod
->args
);
3697 goto free_arch_cleanup
;
3700 dynamic_debug_setup(info
->debug
, info
->num_debug
);
3702 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3703 ftrace_module_init(mod
);
3705 /* Finally it's fully formed, ready to start executing. */
3706 err
= complete_formation(mod
, info
);
3708 goto ddebug_cleanup
;
3710 err
= prepare_coming_module(mod
);
3714 /* Module is ready to execute: parsing args may do that. */
3715 after_dashes
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
,
3717 unknown_module_param_cb
);
3718 if (IS_ERR(after_dashes
)) {
3719 err
= PTR_ERR(after_dashes
);
3720 goto coming_cleanup
;
3721 } else if (after_dashes
) {
3722 pr_warn("%s: parameters '%s' after `--' ignored\n",
3723 mod
->name
, after_dashes
);
3726 /* Link in to sysfs. */
3727 err
= mod_sysfs_setup(mod
, info
, mod
->kp
, mod
->num_kp
);
3729 goto coming_cleanup
;
3731 if (is_livepatch_module(mod
)) {
3732 err
= copy_module_elf(mod
, info
);
3737 /* Get rid of temporary copy. */
3741 trace_module_load(mod
);
3743 return do_init_module(mod
);
3746 mod_sysfs_teardown(mod
);
3748 mod
->state
= MODULE_STATE_GOING
;
3749 destroy_params(mod
->kp
, mod
->num_kp
);
3750 blocking_notifier_call_chain(&module_notify_list
,
3751 MODULE_STATE_GOING
, mod
);
3752 klp_module_going(mod
);
3754 /* module_bug_cleanup needs module_mutex protection */
3755 mutex_lock(&module_mutex
);
3756 module_bug_cleanup(mod
);
3757 mutex_unlock(&module_mutex
);
3759 /* we can't deallocate the module until we clear memory protection */
3760 module_disable_ro(mod
);
3761 module_disable_nx(mod
);
3764 dynamic_debug_remove(info
->debug
);
3765 synchronize_sched();
3768 module_arch_cleanup(mod
);
3772 module_unload_free(mod
);
3774 mutex_lock(&module_mutex
);
3775 /* Unlink carefully: kallsyms could be walking list. */
3776 list_del_rcu(&mod
->list
);
3777 mod_tree_remove(mod
);
3778 wake_up_all(&module_wq
);
3779 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3780 synchronize_sched();
3781 mutex_unlock(&module_mutex
);
3784 * Ftrace needs to clean up what it initialized.
3785 * This does nothing if ftrace_module_init() wasn't called,
3786 * but it must be called outside of module_mutex.
3788 ftrace_release_mod(mod
);
3789 /* Free lock-classes; relies on the preceding sync_rcu() */
3790 lockdep_free_key_range(mod
->core_layout
.base
, mod
->core_layout
.size
);
3792 module_deallocate(mod
, info
);
3798 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
3799 unsigned long, len
, const char __user
*, uargs
)
3802 struct load_info info
= { };
3804 err
= may_init_module();
3808 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3811 err
= copy_module_from_user(umod
, len
, &info
);
3815 return load_module(&info
, uargs
, 0);
3818 SYSCALL_DEFINE3(finit_module
, int, fd
, const char __user
*, uargs
, int, flags
)
3820 struct load_info info
= { };
3825 err
= may_init_module();
3829 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd
, uargs
, flags
);
3831 if (flags
& ~(MODULE_INIT_IGNORE_MODVERSIONS
3832 |MODULE_INIT_IGNORE_VERMAGIC
))
3835 err
= kernel_read_file_from_fd(fd
, &hdr
, &size
, INT_MAX
,
3842 return load_module(&info
, uargs
, flags
);
3845 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
3847 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
3850 #ifdef CONFIG_KALLSYMS
3852 * This ignores the intensely annoying "mapping symbols" found
3853 * in ARM ELF files: $a, $t and $d.
3855 static inline int is_arm_mapping_symbol(const char *str
)
3857 if (str
[0] == '.' && str
[1] == 'L')
3859 return str
[0] == '$' && strchr("axtd", str
[1])
3860 && (str
[2] == '\0' || str
[2] == '.');
3863 static const char *symname(struct mod_kallsyms
*kallsyms
, unsigned int symnum
)
3865 return kallsyms
->strtab
+ kallsyms
->symtab
[symnum
].st_name
;
3868 static const char *get_ksymbol(struct module
*mod
,
3870 unsigned long *size
,
3871 unsigned long *offset
)
3873 unsigned int i
, best
= 0;
3874 unsigned long nextval
;
3875 struct mod_kallsyms
*kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
3877 /* At worse, next value is at end of module */
3878 if (within_module_init(addr
, mod
))
3879 nextval
= (unsigned long)mod
->init_layout
.base
+mod
->init_layout
.text_size
;
3881 nextval
= (unsigned long)mod
->core_layout
.base
+mod
->core_layout
.text_size
;
3883 /* Scan for closest preceding symbol, and next symbol. (ELF
3884 starts real symbols at 1). */
3885 for (i
= 1; i
< kallsyms
->num_symtab
; i
++) {
3886 if (kallsyms
->symtab
[i
].st_shndx
== SHN_UNDEF
)
3889 /* We ignore unnamed symbols: they're uninformative
3890 * and inserted at a whim. */
3891 if (*symname(kallsyms
, i
) == '\0'
3892 || is_arm_mapping_symbol(symname(kallsyms
, i
)))
3895 if (kallsyms
->symtab
[i
].st_value
<= addr
3896 && kallsyms
->symtab
[i
].st_value
> kallsyms
->symtab
[best
].st_value
)
3898 if (kallsyms
->symtab
[i
].st_value
> addr
3899 && kallsyms
->symtab
[i
].st_value
< nextval
)
3900 nextval
= kallsyms
->symtab
[i
].st_value
;
3907 *size
= nextval
- kallsyms
->symtab
[best
].st_value
;
3909 *offset
= addr
- kallsyms
->symtab
[best
].st_value
;
3910 return symname(kallsyms
, best
);
3913 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3914 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3915 const char *module_address_lookup(unsigned long addr
,
3916 unsigned long *size
,
3917 unsigned long *offset
,
3921 const char *ret
= NULL
;
3925 mod
= __module_address(addr
);
3928 *modname
= mod
->name
;
3929 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3931 /* Make a copy in here where it's safe */
3933 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3941 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3946 list_for_each_entry_rcu(mod
, &modules
, list
) {
3947 if (mod
->state
== MODULE_STATE_UNFORMED
)
3949 if (within_module(addr
, mod
)) {
3952 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3955 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3965 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3966 unsigned long *offset
, char *modname
, char *name
)
3971 list_for_each_entry_rcu(mod
, &modules
, list
) {
3972 if (mod
->state
== MODULE_STATE_UNFORMED
)
3974 if (within_module(addr
, mod
)) {
3977 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3981 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3983 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3993 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3994 char *name
, char *module_name
, int *exported
)
3999 list_for_each_entry_rcu(mod
, &modules
, list
) {
4000 struct mod_kallsyms
*kallsyms
;
4002 if (mod
->state
== MODULE_STATE_UNFORMED
)
4004 kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
4005 if (symnum
< kallsyms
->num_symtab
) {
4006 *value
= kallsyms
->symtab
[symnum
].st_value
;
4007 *type
= kallsyms
->symtab
[symnum
].st_info
;
4008 strlcpy(name
, symname(kallsyms
, symnum
), KSYM_NAME_LEN
);
4009 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
4010 *exported
= is_exported(name
, *value
, mod
);
4014 symnum
-= kallsyms
->num_symtab
;
4020 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
4023 struct mod_kallsyms
*kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
4025 for (i
= 0; i
< kallsyms
->num_symtab
; i
++)
4026 if (strcmp(name
, symname(kallsyms
, i
)) == 0 &&
4027 kallsyms
->symtab
[i
].st_info
!= 'U')
4028 return kallsyms
->symtab
[i
].st_value
;
4032 /* Look for this name: can be of form module:name. */
4033 unsigned long module_kallsyms_lookup_name(const char *name
)
4037 unsigned long ret
= 0;
4039 /* Don't lock: we're in enough trouble already. */
4041 if ((colon
= strnchr(name
, MODULE_NAME_LEN
, ':')) != NULL
) {
4042 if ((mod
= find_module_all(name
, colon
- name
, false)) != NULL
)
4043 ret
= mod_find_symname(mod
, colon
+1);
4045 list_for_each_entry_rcu(mod
, &modules
, list
) {
4046 if (mod
->state
== MODULE_STATE_UNFORMED
)
4048 if ((ret
= mod_find_symname(mod
, name
)) != 0)
4056 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
4057 struct module
*, unsigned long),
4064 module_assert_mutex();
4066 list_for_each_entry(mod
, &modules
, list
) {
4067 /* We hold module_mutex: no need for rcu_dereference_sched */
4068 struct mod_kallsyms
*kallsyms
= mod
->kallsyms
;
4070 if (mod
->state
== MODULE_STATE_UNFORMED
)
4072 for (i
= 0; i
< kallsyms
->num_symtab
; i
++) {
4073 ret
= fn(data
, symname(kallsyms
, i
),
4074 mod
, kallsyms
->symtab
[i
].st_value
);
4081 #endif /* CONFIG_KALLSYMS */
4083 /* Maximum number of characters written by module_flags() */
4084 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4086 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4087 static char *module_flags(struct module
*mod
, char *buf
)
4091 BUG_ON(mod
->state
== MODULE_STATE_UNFORMED
);
4093 mod
->state
== MODULE_STATE_GOING
||
4094 mod
->state
== MODULE_STATE_COMING
) {
4096 bx
+= module_flags_taint(mod
, buf
+ bx
);
4097 /* Show a - for module-is-being-unloaded */
4098 if (mod
->state
== MODULE_STATE_GOING
)
4100 /* Show a + for module-is-being-loaded */
4101 if (mod
->state
== MODULE_STATE_COMING
)
4110 #ifdef CONFIG_PROC_FS
4111 /* Called by the /proc file system to return a list of modules. */
4112 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
4114 mutex_lock(&module_mutex
);
4115 return seq_list_start(&modules
, *pos
);
4118 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
4120 return seq_list_next(p
, &modules
, pos
);
4123 static void m_stop(struct seq_file
*m
, void *p
)
4125 mutex_unlock(&module_mutex
);
4128 static int m_show(struct seq_file
*m
, void *p
)
4130 struct module
*mod
= list_entry(p
, struct module
, list
);
4131 char buf
[MODULE_FLAGS_BUF_SIZE
];
4133 /* We always ignore unformed modules. */
4134 if (mod
->state
== MODULE_STATE_UNFORMED
)
4137 seq_printf(m
, "%s %u",
4138 mod
->name
, mod
->init_layout
.size
+ mod
->core_layout
.size
);
4139 print_unload_info(m
, mod
);
4141 /* Informative for users. */
4142 seq_printf(m
, " %s",
4143 mod
->state
== MODULE_STATE_GOING
? "Unloading" :
4144 mod
->state
== MODULE_STATE_COMING
? "Loading" :
4146 /* Used by oprofile and other similar tools. */
4147 seq_printf(m
, " 0x%pK", mod
->core_layout
.base
);
4151 seq_printf(m
, " %s", module_flags(mod
, buf
));
4157 /* Format: modulename size refcount deps address
4159 Where refcount is a number or -, and deps is a comma-separated list
4162 static const struct seq_operations modules_op
= {
4169 static int modules_open(struct inode
*inode
, struct file
*file
)
4171 return seq_open(file
, &modules_op
);
4174 static const struct file_operations proc_modules_operations
= {
4175 .open
= modules_open
,
4177 .llseek
= seq_lseek
,
4178 .release
= seq_release
,
4181 static int __init
proc_modules_init(void)
4183 proc_create("modules", 0, NULL
, &proc_modules_operations
);
4186 module_init(proc_modules_init
);
4189 /* Given an address, look for it in the module exception tables. */
4190 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
4192 const struct exception_table_entry
*e
= NULL
;
4196 mod
= __module_address(addr
);
4200 if (!mod
->num_exentries
)
4203 e
= search_extable(mod
->extable
,
4204 mod
->extable
+ mod
->num_exentries
- 1,
4210 * Now, if we found one, we are running inside it now, hence
4211 * we cannot unload the module, hence no refcnt needed.
4217 * is_module_address - is this address inside a module?
4218 * @addr: the address to check.
4220 * See is_module_text_address() if you simply want to see if the address
4221 * is code (not data).
4223 bool is_module_address(unsigned long addr
)
4228 ret
= __module_address(addr
) != NULL
;
4235 * __module_address - get the module which contains an address.
4236 * @addr: the address.
4238 * Must be called with preempt disabled or module mutex held so that
4239 * module doesn't get freed during this.
4241 struct module
*__module_address(unsigned long addr
)
4245 if (addr
< module_addr_min
|| addr
> module_addr_max
)
4248 module_assert_mutex_or_preempt();
4250 mod
= mod_find(addr
);
4252 BUG_ON(!within_module(addr
, mod
));
4253 if (mod
->state
== MODULE_STATE_UNFORMED
)
4258 EXPORT_SYMBOL_GPL(__module_address
);
4261 * is_module_text_address - is this address inside module code?
4262 * @addr: the address to check.
4264 * See is_module_address() if you simply want to see if the address is
4265 * anywhere in a module. See kernel_text_address() for testing if an
4266 * address corresponds to kernel or module code.
4268 bool is_module_text_address(unsigned long addr
)
4273 ret
= __module_text_address(addr
) != NULL
;
4280 * __module_text_address - get the module whose code contains an address.
4281 * @addr: the address.
4283 * Must be called with preempt disabled or module mutex held so that
4284 * module doesn't get freed during this.
4286 struct module
*__module_text_address(unsigned long addr
)
4288 struct module
*mod
= __module_address(addr
);
4290 /* Make sure it's within the text section. */
4291 if (!within(addr
, mod
->init_layout
.base
, mod
->init_layout
.text_size
)
4292 && !within(addr
, mod
->core_layout
.base
, mod
->core_layout
.text_size
))
4297 EXPORT_SYMBOL_GPL(__module_text_address
);
4299 /* Don't grab lock, we're oopsing. */
4300 void print_modules(void)
4303 char buf
[MODULE_FLAGS_BUF_SIZE
];
4305 printk(KERN_DEFAULT
"Modules linked in:");
4306 /* Most callers should already have preempt disabled, but make sure */
4308 list_for_each_entry_rcu(mod
, &modules
, list
) {
4309 if (mod
->state
== MODULE_STATE_UNFORMED
)
4311 pr_cont(" %s%s", mod
->name
, module_flags(mod
, buf
));
4314 if (last_unloaded_module
[0])
4315 pr_cont(" [last unloaded: %s]", last_unloaded_module
);
4319 #ifdef CONFIG_MODVERSIONS
4320 /* Generate the signature for all relevant module structures here.
4321 * If these change, we don't want to try to parse the module. */
4322 void module_layout(struct module
*mod
,
4323 struct modversion_info
*ver
,
4324 struct kernel_param
*kp
,
4325 struct kernel_symbol
*ks
,
4326 struct tracepoint
* const *tp
)
4329 EXPORT_SYMBOL(module_layout
);