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 #include <asm/mmu_context.h>
53 #include <linux/license.h>
54 #include <asm/sections.h>
55 #include <linux/tracepoint.h>
56 #include <linux/ftrace.h>
57 #include <linux/livepatch.h>
58 #include <linux/async.h>
59 #include <linux/percpu.h>
60 #include <linux/kmemleak.h>
61 #include <linux/jump_label.h>
62 #include <linux/pfn.h>
63 #include <linux/bsearch.h>
64 #include <linux/dynamic_debug.h>
65 #include <linux/audit.h>
66 #include <uapi/linux/module.h>
67 #include "module-internal.h"
69 #define CREATE_TRACE_POINTS
70 #include <trace/events/module.h>
72 #ifndef ARCH_SHF_SMALL
73 #define ARCH_SHF_SMALL 0
77 * Modules' sections will be aligned on page boundaries
78 * to ensure complete separation of code and data, but
79 * only when CONFIG_STRICT_MODULE_RWX=y
81 #ifdef CONFIG_STRICT_MODULE_RWX
82 # define debug_align(X) ALIGN(X, PAGE_SIZE)
84 # define debug_align(X) (X)
87 /* If this is set, the section belongs in the init part of the module */
88 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
92 * 1) List of modules (also safely readable with preempt_disable),
93 * 2) module_use links,
94 * 3) module_addr_min/module_addr_max.
95 * (delete and add uses RCU list operations). */
96 DEFINE_MUTEX(module_mutex
);
97 EXPORT_SYMBOL_GPL(module_mutex
);
98 static LIST_HEAD(modules
);
100 #ifdef CONFIG_MODULES_TREE_LOOKUP
103 * Use a latched RB-tree for __module_address(); this allows us to use
104 * RCU-sched lookups of the address from any context.
106 * This is conditional on PERF_EVENTS || TRACING because those can really hit
107 * __module_address() hard by doing a lot of stack unwinding; potentially from
111 static __always_inline
unsigned long __mod_tree_val(struct latch_tree_node
*n
)
113 struct module_layout
*layout
= container_of(n
, struct module_layout
, mtn
.node
);
115 return (unsigned long)layout
->base
;
118 static __always_inline
unsigned long __mod_tree_size(struct latch_tree_node
*n
)
120 struct module_layout
*layout
= container_of(n
, struct module_layout
, mtn
.node
);
122 return (unsigned long)layout
->size
;
125 static __always_inline
bool
126 mod_tree_less(struct latch_tree_node
*a
, struct latch_tree_node
*b
)
128 return __mod_tree_val(a
) < __mod_tree_val(b
);
131 static __always_inline
int
132 mod_tree_comp(void *key
, struct latch_tree_node
*n
)
134 unsigned long val
= (unsigned long)key
;
135 unsigned long start
, end
;
137 start
= __mod_tree_val(n
);
141 end
= start
+ __mod_tree_size(n
);
148 static const struct latch_tree_ops mod_tree_ops
= {
149 .less
= mod_tree_less
,
150 .comp
= mod_tree_comp
,
153 static struct mod_tree_root
{
154 struct latch_tree_root root
;
155 unsigned long addr_min
;
156 unsigned long addr_max
;
157 } mod_tree __cacheline_aligned
= {
161 #define module_addr_min mod_tree.addr_min
162 #define module_addr_max mod_tree.addr_max
164 static noinline
void __mod_tree_insert(struct mod_tree_node
*node
)
166 latch_tree_insert(&node
->node
, &mod_tree
.root
, &mod_tree_ops
);
169 static void __mod_tree_remove(struct mod_tree_node
*node
)
171 latch_tree_erase(&node
->node
, &mod_tree
.root
, &mod_tree_ops
);
175 * These modifications: insert, remove_init and remove; are serialized by the
178 static void mod_tree_insert(struct module
*mod
)
180 mod
->core_layout
.mtn
.mod
= mod
;
181 mod
->init_layout
.mtn
.mod
= mod
;
183 __mod_tree_insert(&mod
->core_layout
.mtn
);
184 if (mod
->init_layout
.size
)
185 __mod_tree_insert(&mod
->init_layout
.mtn
);
188 static void mod_tree_remove_init(struct module
*mod
)
190 if (mod
->init_layout
.size
)
191 __mod_tree_remove(&mod
->init_layout
.mtn
);
194 static void mod_tree_remove(struct module
*mod
)
196 __mod_tree_remove(&mod
->core_layout
.mtn
);
197 mod_tree_remove_init(mod
);
200 static struct module
*mod_find(unsigned long addr
)
202 struct latch_tree_node
*ltn
;
204 ltn
= latch_tree_find((void *)addr
, &mod_tree
.root
, &mod_tree_ops
);
208 return container_of(ltn
, struct mod_tree_node
, node
)->mod
;
211 #else /* MODULES_TREE_LOOKUP */
213 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
215 static void mod_tree_insert(struct module
*mod
) { }
216 static void mod_tree_remove_init(struct module
*mod
) { }
217 static void mod_tree_remove(struct module
*mod
) { }
219 static struct module
*mod_find(unsigned long addr
)
223 list_for_each_entry_rcu(mod
, &modules
, list
) {
224 if (within_module(addr
, mod
))
231 #endif /* MODULES_TREE_LOOKUP */
234 * Bounds of module text, for speeding up __module_address.
235 * Protected by module_mutex.
237 static void __mod_update_bounds(void *base
, unsigned int size
)
239 unsigned long min
= (unsigned long)base
;
240 unsigned long max
= min
+ size
;
242 if (min
< module_addr_min
)
243 module_addr_min
= min
;
244 if (max
> module_addr_max
)
245 module_addr_max
= max
;
248 static void mod_update_bounds(struct module
*mod
)
250 __mod_update_bounds(mod
->core_layout
.base
, mod
->core_layout
.size
);
251 if (mod
->init_layout
.size
)
252 __mod_update_bounds(mod
->init_layout
.base
, mod
->init_layout
.size
);
255 #ifdef CONFIG_KGDB_KDB
256 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
257 #endif /* CONFIG_KGDB_KDB */
259 static void module_assert_mutex(void)
261 lockdep_assert_held(&module_mutex
);
264 static void module_assert_mutex_or_preempt(void)
266 #ifdef CONFIG_LOCKDEP
267 if (unlikely(!debug_locks
))
270 WARN_ON_ONCE(!rcu_read_lock_sched_held() &&
271 !lockdep_is_held(&module_mutex
));
275 static bool sig_enforce
= IS_ENABLED(CONFIG_MODULE_SIG_FORCE
);
276 #ifndef CONFIG_MODULE_SIG_FORCE
277 module_param(sig_enforce
, bool_enable_only
, 0644);
278 #endif /* !CONFIG_MODULE_SIG_FORCE */
280 /* Block module loading/unloading? */
281 int modules_disabled
= 0;
282 core_param(nomodule
, modules_disabled
, bint
, 0);
284 /* Waiting for a module to finish initializing? */
285 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
287 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
289 int register_module_notifier(struct notifier_block
*nb
)
291 return blocking_notifier_chain_register(&module_notify_list
, nb
);
293 EXPORT_SYMBOL(register_module_notifier
);
295 int unregister_module_notifier(struct notifier_block
*nb
)
297 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
299 EXPORT_SYMBOL(unregister_module_notifier
);
305 char *secstrings
, *strtab
;
306 unsigned long symoffs
, stroffs
;
307 struct _ddebug
*debug
;
308 unsigned int num_debug
;
310 #ifdef CONFIG_KALLSYMS
311 unsigned long mod_kallsyms_init_off
;
314 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
319 * We require a truly strong try_module_get(): 0 means success.
320 * Otherwise an error is returned due to ongoing or failed
321 * initialization etc.
323 static inline int strong_try_module_get(struct module
*mod
)
325 BUG_ON(mod
&& mod
->state
== MODULE_STATE_UNFORMED
);
326 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
328 if (try_module_get(mod
))
334 static inline void add_taint_module(struct module
*mod
, unsigned flag
,
335 enum lockdep_ok lockdep_ok
)
337 add_taint(flag
, lockdep_ok
);
338 set_bit(flag
, &mod
->taints
);
342 * A thread that wants to hold a reference to a module only while it
343 * is running can call this to safely exit. nfsd and lockd use this.
345 void __noreturn
__module_put_and_exit(struct module
*mod
, long code
)
350 EXPORT_SYMBOL(__module_put_and_exit
);
352 /* Find a module section: 0 means not found. */
353 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
357 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
358 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
359 /* Alloc bit cleared means "ignore it." */
360 if ((shdr
->sh_flags
& SHF_ALLOC
)
361 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
367 /* Find a module section, or NULL. */
368 static void *section_addr(const struct load_info
*info
, const char *name
)
370 /* Section 0 has sh_addr 0. */
371 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
374 /* Find a module section, or NULL. Fill in number of "objects" in section. */
375 static void *section_objs(const struct load_info
*info
,
380 unsigned int sec
= find_sec(info
, name
);
382 /* Section 0 has sh_addr 0 and sh_size 0. */
383 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
384 return (void *)info
->sechdrs
[sec
].sh_addr
;
387 /* Provided by the linker */
388 extern const struct kernel_symbol __start___ksymtab
[];
389 extern const struct kernel_symbol __stop___ksymtab
[];
390 extern const struct kernel_symbol __start___ksymtab_gpl
[];
391 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
392 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
393 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
394 extern const s32 __start___kcrctab
[];
395 extern const s32 __start___kcrctab_gpl
[];
396 extern const s32 __start___kcrctab_gpl_future
[];
397 #ifdef CONFIG_UNUSED_SYMBOLS
398 extern const struct kernel_symbol __start___ksymtab_unused
[];
399 extern const struct kernel_symbol __stop___ksymtab_unused
[];
400 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
401 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
402 extern const s32 __start___kcrctab_unused
[];
403 extern const s32 __start___kcrctab_unused_gpl
[];
406 #ifndef CONFIG_MODVERSIONS
407 #define symversion(base, idx) NULL
409 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
412 static bool each_symbol_in_section(const struct symsearch
*arr
,
413 unsigned int arrsize
,
414 struct module
*owner
,
415 bool (*fn
)(const struct symsearch
*syms
,
416 struct module
*owner
,
422 for (j
= 0; j
< arrsize
; j
++) {
423 if (fn(&arr
[j
], owner
, data
))
430 /* Returns true as soon as fn returns true, otherwise false. */
431 bool each_symbol_section(bool (*fn
)(const struct symsearch
*arr
,
432 struct module
*owner
,
437 static const struct symsearch arr
[] = {
438 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
439 NOT_GPL_ONLY
, false },
440 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
441 __start___kcrctab_gpl
,
443 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
444 __start___kcrctab_gpl_future
,
445 WILL_BE_GPL_ONLY
, false },
446 #ifdef CONFIG_UNUSED_SYMBOLS
447 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
448 __start___kcrctab_unused
,
449 NOT_GPL_ONLY
, true },
450 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
451 __start___kcrctab_unused_gpl
,
456 module_assert_mutex_or_preempt();
458 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
461 list_for_each_entry_rcu(mod
, &modules
, list
) {
462 struct symsearch arr
[] = {
463 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
464 NOT_GPL_ONLY
, false },
465 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
468 { mod
->gpl_future_syms
,
469 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
470 mod
->gpl_future_crcs
,
471 WILL_BE_GPL_ONLY
, false },
472 #ifdef CONFIG_UNUSED_SYMBOLS
474 mod
->unused_syms
+ mod
->num_unused_syms
,
476 NOT_GPL_ONLY
, true },
477 { mod
->unused_gpl_syms
,
478 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
479 mod
->unused_gpl_crcs
,
484 if (mod
->state
== MODULE_STATE_UNFORMED
)
487 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
492 EXPORT_SYMBOL_GPL(each_symbol_section
);
494 struct find_symbol_arg
{
501 struct module
*owner
;
503 const struct kernel_symbol
*sym
;
506 static bool check_symbol(const struct symsearch
*syms
,
507 struct module
*owner
,
508 unsigned int symnum
, void *data
)
510 struct find_symbol_arg
*fsa
= data
;
513 if (syms
->licence
== GPL_ONLY
)
515 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
516 pr_warn("Symbol %s is being used by a non-GPL module, "
517 "which will not be allowed in the future\n",
522 #ifdef CONFIG_UNUSED_SYMBOLS
523 if (syms
->unused
&& fsa
->warn
) {
524 pr_warn("Symbol %s is marked as UNUSED, however this module is "
525 "using it.\n", fsa
->name
);
526 pr_warn("This symbol will go away in the future.\n");
527 pr_warn("Please evaluate if this is the right api to use and "
528 "if it really is, submit a report to the linux kernel "
529 "mailing list together with submitting your code for "
535 fsa
->crc
= symversion(syms
->crcs
, symnum
);
536 fsa
->sym
= &syms
->start
[symnum
];
540 static int cmp_name(const void *va
, const void *vb
)
543 const struct kernel_symbol
*b
;
545 return strcmp(a
, b
->name
);
548 static bool find_symbol_in_section(const struct symsearch
*syms
,
549 struct module
*owner
,
552 struct find_symbol_arg
*fsa
= data
;
553 struct kernel_symbol
*sym
;
555 sym
= bsearch(fsa
->name
, syms
->start
, syms
->stop
- syms
->start
,
556 sizeof(struct kernel_symbol
), cmp_name
);
558 if (sym
!= NULL
&& check_symbol(syms
, owner
, sym
- syms
->start
, data
))
564 /* Find a symbol and return it, along with, (optional) crc and
565 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
566 const struct kernel_symbol
*find_symbol(const char *name
,
567 struct module
**owner
,
572 struct find_symbol_arg fsa
;
578 if (each_symbol_section(find_symbol_in_section
, &fsa
)) {
586 pr_debug("Failed to find symbol %s\n", name
);
589 EXPORT_SYMBOL_GPL(find_symbol
);
592 * Search for module by name: must hold module_mutex (or preempt disabled
593 * for read-only access).
595 static struct module
*find_module_all(const char *name
, size_t len
,
600 module_assert_mutex_or_preempt();
602 list_for_each_entry(mod
, &modules
, list
) {
603 if (!even_unformed
&& mod
->state
== MODULE_STATE_UNFORMED
)
605 if (strlen(mod
->name
) == len
&& !memcmp(mod
->name
, name
, len
))
611 struct module
*find_module(const char *name
)
613 module_assert_mutex();
614 return find_module_all(name
, strlen(name
), false);
616 EXPORT_SYMBOL_GPL(find_module
);
620 static inline void __percpu
*mod_percpu(struct module
*mod
)
625 static int percpu_modalloc(struct module
*mod
, struct load_info
*info
)
627 Elf_Shdr
*pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
628 unsigned long align
= pcpusec
->sh_addralign
;
630 if (!pcpusec
->sh_size
)
633 if (align
> PAGE_SIZE
) {
634 pr_warn("%s: per-cpu alignment %li > %li\n",
635 mod
->name
, align
, PAGE_SIZE
);
639 mod
->percpu
= __alloc_reserved_percpu(pcpusec
->sh_size
, align
);
641 pr_warn("%s: Could not allocate %lu bytes percpu data\n",
642 mod
->name
, (unsigned long)pcpusec
->sh_size
);
645 mod
->percpu_size
= pcpusec
->sh_size
;
649 static void percpu_modfree(struct module
*mod
)
651 free_percpu(mod
->percpu
);
654 static unsigned int find_pcpusec(struct load_info
*info
)
656 return find_sec(info
, ".data..percpu");
659 static void percpu_modcopy(struct module
*mod
,
660 const void *from
, unsigned long size
)
664 for_each_possible_cpu(cpu
)
665 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
668 bool __is_module_percpu_address(unsigned long addr
, unsigned long *can_addr
)
675 list_for_each_entry_rcu(mod
, &modules
, list
) {
676 if (mod
->state
== MODULE_STATE_UNFORMED
)
678 if (!mod
->percpu_size
)
680 for_each_possible_cpu(cpu
) {
681 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
682 void *va
= (void *)addr
;
684 if (va
>= start
&& va
< start
+ mod
->percpu_size
) {
686 *can_addr
= (unsigned long) (va
- start
);
687 *can_addr
+= (unsigned long)
688 per_cpu_ptr(mod
->percpu
,
702 * is_module_percpu_address - test whether address is from module static percpu
703 * @addr: address to test
705 * Test whether @addr belongs to module static percpu area.
708 * %true if @addr is from module static percpu area
710 bool is_module_percpu_address(unsigned long addr
)
712 return __is_module_percpu_address(addr
, NULL
);
715 #else /* ... !CONFIG_SMP */
717 static inline void __percpu
*mod_percpu(struct module
*mod
)
721 static int percpu_modalloc(struct module
*mod
, struct load_info
*info
)
723 /* UP modules shouldn't have this section: ENOMEM isn't quite right */
724 if (info
->sechdrs
[info
->index
.pcpu
].sh_size
!= 0)
728 static inline void percpu_modfree(struct module
*mod
)
731 static unsigned int find_pcpusec(struct load_info
*info
)
735 static inline void percpu_modcopy(struct module
*mod
,
736 const void *from
, unsigned long size
)
738 /* pcpusec should be 0, and size of that section should be 0. */
741 bool is_module_percpu_address(unsigned long addr
)
746 bool __is_module_percpu_address(unsigned long addr
, unsigned long *can_addr
)
751 #endif /* CONFIG_SMP */
753 #define MODINFO_ATTR(field) \
754 static void setup_modinfo_##field(struct module *mod, const char *s) \
756 mod->field = kstrdup(s, GFP_KERNEL); \
758 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
759 struct module_kobject *mk, char *buffer) \
761 return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
763 static int modinfo_##field##_exists(struct module *mod) \
765 return mod->field != NULL; \
767 static void free_modinfo_##field(struct module *mod) \
772 static struct module_attribute modinfo_##field = { \
773 .attr = { .name = __stringify(field), .mode = 0444 }, \
774 .show = show_modinfo_##field, \
775 .setup = setup_modinfo_##field, \
776 .test = modinfo_##field##_exists, \
777 .free = free_modinfo_##field, \
780 MODINFO_ATTR(version
);
781 MODINFO_ATTR(srcversion
);
783 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
785 #ifdef CONFIG_MODULE_UNLOAD
787 EXPORT_TRACEPOINT_SYMBOL(module_get
);
789 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
790 #define MODULE_REF_BASE 1
792 /* Init the unload section of the module. */
793 static int module_unload_init(struct module
*mod
)
796 * Initialize reference counter to MODULE_REF_BASE.
797 * refcnt == 0 means module is going.
799 atomic_set(&mod
->refcnt
, MODULE_REF_BASE
);
801 INIT_LIST_HEAD(&mod
->source_list
);
802 INIT_LIST_HEAD(&mod
->target_list
);
804 /* Hold reference count during initialization. */
805 atomic_inc(&mod
->refcnt
);
810 /* Does a already use b? */
811 static int already_uses(struct module
*a
, struct module
*b
)
813 struct module_use
*use
;
815 list_for_each_entry(use
, &b
->source_list
, source_list
) {
816 if (use
->source
== a
) {
817 pr_debug("%s uses %s!\n", a
->name
, b
->name
);
821 pr_debug("%s does not use %s!\n", a
->name
, b
->name
);
827 * - we add 'a' as a "source", 'b' as a "target" of module use
828 * - the module_use is added to the list of 'b' sources (so
829 * 'b' can walk the list to see who sourced them), and of 'a'
830 * targets (so 'a' can see what modules it targets).
832 static int add_module_usage(struct module
*a
, struct module
*b
)
834 struct module_use
*use
;
836 pr_debug("Allocating new usage for %s.\n", a
->name
);
837 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
839 pr_warn("%s: out of memory loading\n", a
->name
);
845 list_add(&use
->source_list
, &b
->source_list
);
846 list_add(&use
->target_list
, &a
->target_list
);
850 /* Module a uses b: caller needs module_mutex() */
851 int ref_module(struct module
*a
, struct module
*b
)
855 if (b
== NULL
|| already_uses(a
, b
))
858 /* If module isn't available, we fail. */
859 err
= strong_try_module_get(b
);
863 err
= add_module_usage(a
, b
);
870 EXPORT_SYMBOL_GPL(ref_module
);
872 /* Clear the unload stuff of the module. */
873 static void module_unload_free(struct module
*mod
)
875 struct module_use
*use
, *tmp
;
877 mutex_lock(&module_mutex
);
878 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
879 struct module
*i
= use
->target
;
880 pr_debug("%s unusing %s\n", mod
->name
, i
->name
);
882 list_del(&use
->source_list
);
883 list_del(&use
->target_list
);
886 mutex_unlock(&module_mutex
);
889 #ifdef CONFIG_MODULE_FORCE_UNLOAD
890 static inline int try_force_unload(unsigned int flags
)
892 int ret
= (flags
& O_TRUNC
);
894 add_taint(TAINT_FORCED_RMMOD
, LOCKDEP_NOW_UNRELIABLE
);
898 static inline int try_force_unload(unsigned int flags
)
902 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
904 /* Try to release refcount of module, 0 means success. */
905 static int try_release_module_ref(struct module
*mod
)
909 /* Try to decrement refcnt which we set at loading */
910 ret
= atomic_sub_return(MODULE_REF_BASE
, &mod
->refcnt
);
913 /* Someone can put this right now, recover with checking */
914 ret
= atomic_add_unless(&mod
->refcnt
, MODULE_REF_BASE
, 0);
919 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
921 /* If it's not unused, quit unless we're forcing. */
922 if (try_release_module_ref(mod
) != 0) {
923 *forced
= try_force_unload(flags
);
928 /* Mark it as dying. */
929 mod
->state
= MODULE_STATE_GOING
;
935 * module_refcount - return the refcount or -1 if unloading
937 * @mod: the module we're checking
940 * -1 if the module is in the process of unloading
941 * otherwise the number of references in the kernel to the module
943 int module_refcount(struct module
*mod
)
945 return atomic_read(&mod
->refcnt
) - MODULE_REF_BASE
;
947 EXPORT_SYMBOL(module_refcount
);
949 /* This exists whether we can unload or not */
950 static void free_module(struct module
*mod
);
952 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
956 char name
[MODULE_NAME_LEN
];
959 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
962 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
964 name
[MODULE_NAME_LEN
-1] = '\0';
966 if (mutex_lock_interruptible(&module_mutex
) != 0)
969 mod
= find_module(name
);
975 if (!list_empty(&mod
->source_list
)) {
976 /* Other modules depend on us: get rid of them first. */
981 /* Doing init or already dying? */
982 if (mod
->state
!= MODULE_STATE_LIVE
) {
983 /* FIXME: if (force), slam module count damn the torpedoes */
984 pr_debug("%s already dying\n", mod
->name
);
989 /* If it has an init func, it must have an exit func to unload */
990 if (mod
->init
&& !mod
->exit
) {
991 forced
= try_force_unload(flags
);
993 /* This module can't be removed */
999 /* Stop the machine so refcounts can't move and disable module. */
1000 ret
= try_stop_module(mod
, flags
, &forced
);
1004 mutex_unlock(&module_mutex
);
1005 /* Final destruction now no one is using it. */
1006 if (mod
->exit
!= NULL
)
1008 blocking_notifier_call_chain(&module_notify_list
,
1009 MODULE_STATE_GOING
, mod
);
1010 klp_module_going(mod
);
1011 ftrace_release_mod(mod
);
1013 async_synchronize_full();
1015 /* Store the name of the last unloaded module for diagnostic purposes */
1016 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
1021 mutex_unlock(&module_mutex
);
1025 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
1027 struct module_use
*use
;
1028 int printed_something
= 0;
1030 seq_printf(m
, " %i ", module_refcount(mod
));
1033 * Always include a trailing , so userspace can differentiate
1034 * between this and the old multi-field proc format.
1036 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
1037 printed_something
= 1;
1038 seq_printf(m
, "%s,", use
->source
->name
);
1041 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
1042 printed_something
= 1;
1043 seq_puts(m
, "[permanent],");
1046 if (!printed_something
)
1050 void __symbol_put(const char *symbol
)
1052 struct module
*owner
;
1055 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
1060 EXPORT_SYMBOL(__symbol_put
);
1062 /* Note this assumes addr is a function, which it currently always is. */
1063 void symbol_put_addr(void *addr
)
1065 struct module
*modaddr
;
1066 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
1068 if (core_kernel_text(a
))
1072 * Even though we hold a reference on the module; we still need to
1073 * disable preemption in order to safely traverse the data structure.
1076 modaddr
= __module_text_address(a
);
1078 module_put(modaddr
);
1081 EXPORT_SYMBOL_GPL(symbol_put_addr
);
1083 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
1084 struct module_kobject
*mk
, char *buffer
)
1086 return sprintf(buffer
, "%i\n", module_refcount(mk
->mod
));
1089 static struct module_attribute modinfo_refcnt
=
1090 __ATTR(refcnt
, 0444, show_refcnt
, NULL
);
1092 void __module_get(struct module
*module
)
1096 atomic_inc(&module
->refcnt
);
1097 trace_module_get(module
, _RET_IP_
);
1101 EXPORT_SYMBOL(__module_get
);
1103 bool try_module_get(struct module
*module
)
1109 /* Note: here, we can fail to get a reference */
1110 if (likely(module_is_live(module
) &&
1111 atomic_inc_not_zero(&module
->refcnt
) != 0))
1112 trace_module_get(module
, _RET_IP_
);
1120 EXPORT_SYMBOL(try_module_get
);
1122 void module_put(struct module
*module
)
1128 ret
= atomic_dec_if_positive(&module
->refcnt
);
1129 WARN_ON(ret
< 0); /* Failed to put refcount */
1130 trace_module_put(module
, _RET_IP_
);
1134 EXPORT_SYMBOL(module_put
);
1136 #else /* !CONFIG_MODULE_UNLOAD */
1137 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
1139 /* We don't know the usage count, or what modules are using. */
1140 seq_puts(m
, " - -");
1143 static inline void module_unload_free(struct module
*mod
)
1147 int ref_module(struct module
*a
, struct module
*b
)
1149 return strong_try_module_get(b
);
1151 EXPORT_SYMBOL_GPL(ref_module
);
1153 static inline int module_unload_init(struct module
*mod
)
1157 #endif /* CONFIG_MODULE_UNLOAD */
1159 static size_t module_flags_taint(struct module
*mod
, char *buf
)
1164 for (i
= 0; i
< TAINT_FLAGS_COUNT
; i
++) {
1165 if (taint_flags
[i
].module
&& test_bit(i
, &mod
->taints
))
1166 buf
[l
++] = taint_flags
[i
].c_true
;
1172 static ssize_t
show_initstate(struct module_attribute
*mattr
,
1173 struct module_kobject
*mk
, char *buffer
)
1175 const char *state
= "unknown";
1177 switch (mk
->mod
->state
) {
1178 case MODULE_STATE_LIVE
:
1181 case MODULE_STATE_COMING
:
1184 case MODULE_STATE_GOING
:
1190 return sprintf(buffer
, "%s\n", state
);
1193 static struct module_attribute modinfo_initstate
=
1194 __ATTR(initstate
, 0444, show_initstate
, NULL
);
1196 static ssize_t
store_uevent(struct module_attribute
*mattr
,
1197 struct module_kobject
*mk
,
1198 const char *buffer
, size_t count
)
1200 enum kobject_action action
;
1202 if (kobject_action_type(buffer
, count
, &action
) == 0)
1203 kobject_uevent(&mk
->kobj
, action
);
1207 struct module_attribute module_uevent
=
1208 __ATTR(uevent
, 0200, NULL
, store_uevent
);
1210 static ssize_t
show_coresize(struct module_attribute
*mattr
,
1211 struct module_kobject
*mk
, char *buffer
)
1213 return sprintf(buffer
, "%u\n", mk
->mod
->core_layout
.size
);
1216 static struct module_attribute modinfo_coresize
=
1217 __ATTR(coresize
, 0444, show_coresize
, NULL
);
1219 static ssize_t
show_initsize(struct module_attribute
*mattr
,
1220 struct module_kobject
*mk
, char *buffer
)
1222 return sprintf(buffer
, "%u\n", mk
->mod
->init_layout
.size
);
1225 static struct module_attribute modinfo_initsize
=
1226 __ATTR(initsize
, 0444, show_initsize
, NULL
);
1228 static ssize_t
show_taint(struct module_attribute
*mattr
,
1229 struct module_kobject
*mk
, char *buffer
)
1233 l
= module_flags_taint(mk
->mod
, buffer
);
1238 static struct module_attribute modinfo_taint
=
1239 __ATTR(taint
, 0444, show_taint
, NULL
);
1241 static struct module_attribute
*modinfo_attrs
[] = {
1244 &modinfo_srcversion
,
1249 #ifdef CONFIG_MODULE_UNLOAD
1255 static const char vermagic
[] = VERMAGIC_STRING
;
1257 static int try_to_force_load(struct module
*mod
, const char *reason
)
1259 #ifdef CONFIG_MODULE_FORCE_LOAD
1260 if (!test_taint(TAINT_FORCED_MODULE
))
1261 pr_warn("%s: %s: kernel tainted.\n", mod
->name
, reason
);
1262 add_taint_module(mod
, TAINT_FORCED_MODULE
, LOCKDEP_NOW_UNRELIABLE
);
1269 #ifdef CONFIG_MODVERSIONS
1271 static u32
resolve_rel_crc(const s32
*crc
)
1273 return *(u32
*)((void *)crc
+ *crc
);
1276 static int check_version(Elf_Shdr
*sechdrs
,
1277 unsigned int versindex
,
1278 const char *symname
,
1282 unsigned int i
, num_versions
;
1283 struct modversion_info
*versions
;
1285 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1289 /* No versions at all? modprobe --force does this. */
1291 return try_to_force_load(mod
, symname
) == 0;
1293 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1294 num_versions
= sechdrs
[versindex
].sh_size
1295 / sizeof(struct modversion_info
);
1297 for (i
= 0; i
< num_versions
; i
++) {
1300 if (strcmp(versions
[i
].name
, symname
) != 0)
1303 if (IS_ENABLED(CONFIG_MODULE_REL_CRCS
))
1304 crcval
= resolve_rel_crc(crc
);
1307 if (versions
[i
].crc
== crcval
)
1309 pr_debug("Found checksum %X vs module %lX\n",
1310 crcval
, versions
[i
].crc
);
1314 /* Broken toolchain. Warn once, then let it go.. */
1315 pr_warn_once("%s: no symbol version for %s\n", mod
->name
, symname
);
1319 pr_warn("%s: disagrees about version of symbol %s\n",
1320 mod
->name
, symname
);
1324 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1325 unsigned int versindex
,
1331 * Since this should be found in kernel (which can't be removed), no
1332 * locking is necessary -- use preempt_disable() to placate lockdep.
1335 if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout
), NULL
,
1336 &crc
, true, false)) {
1341 return check_version(sechdrs
, versindex
,
1342 VMLINUX_SYMBOL_STR(module_layout
), mod
, crc
);
1345 /* First part is kernel version, which we ignore if module has crcs. */
1346 static inline int same_magic(const char *amagic
, const char *bmagic
,
1350 amagic
+= strcspn(amagic
, " ");
1351 bmagic
+= strcspn(bmagic
, " ");
1353 return strcmp(amagic
, bmagic
) == 0;
1356 static inline int check_version(Elf_Shdr
*sechdrs
,
1357 unsigned int versindex
,
1358 const char *symname
,
1365 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1366 unsigned int versindex
,
1372 static inline int same_magic(const char *amagic
, const char *bmagic
,
1375 return strcmp(amagic
, bmagic
) == 0;
1377 #endif /* CONFIG_MODVERSIONS */
1379 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1380 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1381 const struct load_info
*info
,
1385 struct module
*owner
;
1386 const struct kernel_symbol
*sym
;
1391 * The module_mutex should not be a heavily contended lock;
1392 * if we get the occasional sleep here, we'll go an extra iteration
1393 * in the wait_event_interruptible(), which is harmless.
1395 sched_annotate_sleep();
1396 mutex_lock(&module_mutex
);
1397 sym
= find_symbol(name
, &owner
, &crc
,
1398 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1402 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
)) {
1403 sym
= ERR_PTR(-EINVAL
);
1407 err
= ref_module(mod
, owner
);
1414 /* We must make copy under the lock if we failed to get ref. */
1415 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1417 mutex_unlock(&module_mutex
);
1421 static const struct kernel_symbol
*
1422 resolve_symbol_wait(struct module
*mod
,
1423 const struct load_info
*info
,
1426 const struct kernel_symbol
*ksym
;
1427 char owner
[MODULE_NAME_LEN
];
1429 if (wait_event_interruptible_timeout(module_wq
,
1430 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1431 || PTR_ERR(ksym
) != -EBUSY
,
1433 pr_warn("%s: gave up waiting for init of module %s.\n",
1440 * /sys/module/foo/sections stuff
1441 * J. Corbet <corbet@lwn.net>
1445 #ifdef CONFIG_KALLSYMS
1446 static inline bool sect_empty(const Elf_Shdr
*sect
)
1448 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1451 struct module_sect_attr
{
1452 struct module_attribute mattr
;
1454 unsigned long address
;
1457 struct module_sect_attrs
{
1458 struct attribute_group grp
;
1459 unsigned int nsections
;
1460 struct module_sect_attr attrs
[0];
1463 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1464 struct module_kobject
*mk
, char *buf
)
1466 struct module_sect_attr
*sattr
=
1467 container_of(mattr
, struct module_sect_attr
, mattr
);
1468 return sprintf(buf
, "0x%pK\n", (void *)sattr
->address
);
1471 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1473 unsigned int section
;
1475 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1476 kfree(sect_attrs
->attrs
[section
].name
);
1480 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1482 unsigned int nloaded
= 0, i
, size
[2];
1483 struct module_sect_attrs
*sect_attrs
;
1484 struct module_sect_attr
*sattr
;
1485 struct attribute
**gattr
;
1487 /* Count loaded sections and allocate structures */
1488 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1489 if (!sect_empty(&info
->sechdrs
[i
]))
1491 size
[0] = ALIGN(sizeof(*sect_attrs
)
1492 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1493 sizeof(sect_attrs
->grp
.attrs
[0]));
1494 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1495 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1496 if (sect_attrs
== NULL
)
1499 /* Setup section attributes. */
1500 sect_attrs
->grp
.name
= "sections";
1501 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1503 sect_attrs
->nsections
= 0;
1504 sattr
= §_attrs
->attrs
[0];
1505 gattr
= §_attrs
->grp
.attrs
[0];
1506 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1507 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1508 if (sect_empty(sec
))
1510 sattr
->address
= sec
->sh_addr
;
1511 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1513 if (sattr
->name
== NULL
)
1515 sect_attrs
->nsections
++;
1516 sysfs_attr_init(&sattr
->mattr
.attr
);
1517 sattr
->mattr
.show
= module_sect_show
;
1518 sattr
->mattr
.store
= NULL
;
1519 sattr
->mattr
.attr
.name
= sattr
->name
;
1520 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1521 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1525 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1528 mod
->sect_attrs
= sect_attrs
;
1531 free_sect_attrs(sect_attrs
);
1534 static void remove_sect_attrs(struct module
*mod
)
1536 if (mod
->sect_attrs
) {
1537 sysfs_remove_group(&mod
->mkobj
.kobj
,
1538 &mod
->sect_attrs
->grp
);
1539 /* We are positive that no one is using any sect attrs
1540 * at this point. Deallocate immediately. */
1541 free_sect_attrs(mod
->sect_attrs
);
1542 mod
->sect_attrs
= NULL
;
1547 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1550 struct module_notes_attrs
{
1551 struct kobject
*dir
;
1553 struct bin_attribute attrs
[0];
1556 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1557 struct bin_attribute
*bin_attr
,
1558 char *buf
, loff_t pos
, size_t count
)
1561 * The caller checked the pos and count against our size.
1563 memcpy(buf
, bin_attr
->private + pos
, count
);
1567 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1570 if (notes_attrs
->dir
) {
1572 sysfs_remove_bin_file(notes_attrs
->dir
,
1573 ¬es_attrs
->attrs
[i
]);
1574 kobject_put(notes_attrs
->dir
);
1579 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1581 unsigned int notes
, loaded
, i
;
1582 struct module_notes_attrs
*notes_attrs
;
1583 struct bin_attribute
*nattr
;
1585 /* failed to create section attributes, so can't create notes */
1586 if (!mod
->sect_attrs
)
1589 /* Count notes sections and allocate structures. */
1591 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1592 if (!sect_empty(&info
->sechdrs
[i
]) &&
1593 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1599 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1600 + notes
* sizeof(notes_attrs
->attrs
[0]),
1602 if (notes_attrs
== NULL
)
1605 notes_attrs
->notes
= notes
;
1606 nattr
= ¬es_attrs
->attrs
[0];
1607 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1608 if (sect_empty(&info
->sechdrs
[i
]))
1610 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1611 sysfs_bin_attr_init(nattr
);
1612 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1613 nattr
->attr
.mode
= S_IRUGO
;
1614 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1615 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1616 nattr
->read
= module_notes_read
;
1622 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1623 if (!notes_attrs
->dir
)
1626 for (i
= 0; i
< notes
; ++i
)
1627 if (sysfs_create_bin_file(notes_attrs
->dir
,
1628 ¬es_attrs
->attrs
[i
]))
1631 mod
->notes_attrs
= notes_attrs
;
1635 free_notes_attrs(notes_attrs
, i
);
1638 static void remove_notes_attrs(struct module
*mod
)
1640 if (mod
->notes_attrs
)
1641 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1646 static inline void add_sect_attrs(struct module
*mod
,
1647 const struct load_info
*info
)
1651 static inline void remove_sect_attrs(struct module
*mod
)
1655 static inline void add_notes_attrs(struct module
*mod
,
1656 const struct load_info
*info
)
1660 static inline void remove_notes_attrs(struct module
*mod
)
1663 #endif /* CONFIG_KALLSYMS */
1665 static void add_usage_links(struct module
*mod
)
1667 #ifdef CONFIG_MODULE_UNLOAD
1668 struct module_use
*use
;
1671 mutex_lock(&module_mutex
);
1672 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1673 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1674 &mod
->mkobj
.kobj
, mod
->name
);
1676 mutex_unlock(&module_mutex
);
1680 static void del_usage_links(struct module
*mod
)
1682 #ifdef CONFIG_MODULE_UNLOAD
1683 struct module_use
*use
;
1685 mutex_lock(&module_mutex
);
1686 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1687 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1688 mutex_unlock(&module_mutex
);
1692 static int module_add_modinfo_attrs(struct module
*mod
)
1694 struct module_attribute
*attr
;
1695 struct module_attribute
*temp_attr
;
1699 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1700 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1702 if (!mod
->modinfo_attrs
)
1705 temp_attr
= mod
->modinfo_attrs
;
1706 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1707 if (!attr
->test
|| attr
->test(mod
)) {
1708 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1709 sysfs_attr_init(&temp_attr
->attr
);
1710 error
= sysfs_create_file(&mod
->mkobj
.kobj
,
1718 static void module_remove_modinfo_attrs(struct module
*mod
)
1720 struct module_attribute
*attr
;
1723 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1724 /* pick a field to test for end of list */
1725 if (!attr
->attr
.name
)
1727 sysfs_remove_file(&mod
->mkobj
.kobj
, &attr
->attr
);
1731 kfree(mod
->modinfo_attrs
);
1734 static void mod_kobject_put(struct module
*mod
)
1736 DECLARE_COMPLETION_ONSTACK(c
);
1737 mod
->mkobj
.kobj_completion
= &c
;
1738 kobject_put(&mod
->mkobj
.kobj
);
1739 wait_for_completion(&c
);
1742 static int mod_sysfs_init(struct module
*mod
)
1745 struct kobject
*kobj
;
1747 if (!module_sysfs_initialized
) {
1748 pr_err("%s: module sysfs not initialized\n", mod
->name
);
1753 kobj
= kset_find_obj(module_kset
, mod
->name
);
1755 pr_err("%s: module is already loaded\n", mod
->name
);
1761 mod
->mkobj
.mod
= mod
;
1763 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1764 mod
->mkobj
.kobj
.kset
= module_kset
;
1765 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1768 mod_kobject_put(mod
);
1770 /* delay uevent until full sysfs population */
1775 static int mod_sysfs_setup(struct module
*mod
,
1776 const struct load_info
*info
,
1777 struct kernel_param
*kparam
,
1778 unsigned int num_params
)
1782 err
= mod_sysfs_init(mod
);
1786 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1787 if (!mod
->holders_dir
) {
1792 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1794 goto out_unreg_holders
;
1796 err
= module_add_modinfo_attrs(mod
);
1798 goto out_unreg_param
;
1800 add_usage_links(mod
);
1801 add_sect_attrs(mod
, info
);
1802 add_notes_attrs(mod
, info
);
1804 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1808 module_param_sysfs_remove(mod
);
1810 kobject_put(mod
->holders_dir
);
1812 mod_kobject_put(mod
);
1817 static void mod_sysfs_fini(struct module
*mod
)
1819 remove_notes_attrs(mod
);
1820 remove_sect_attrs(mod
);
1821 mod_kobject_put(mod
);
1824 static void init_param_lock(struct module
*mod
)
1826 mutex_init(&mod
->param_lock
);
1828 #else /* !CONFIG_SYSFS */
1830 static int mod_sysfs_setup(struct module
*mod
,
1831 const struct load_info
*info
,
1832 struct kernel_param
*kparam
,
1833 unsigned int num_params
)
1838 static void mod_sysfs_fini(struct module
*mod
)
1842 static void module_remove_modinfo_attrs(struct module
*mod
)
1846 static void del_usage_links(struct module
*mod
)
1850 static void init_param_lock(struct module
*mod
)
1853 #endif /* CONFIG_SYSFS */
1855 static void mod_sysfs_teardown(struct module
*mod
)
1857 del_usage_links(mod
);
1858 module_remove_modinfo_attrs(mod
);
1859 module_param_sysfs_remove(mod
);
1860 kobject_put(mod
->mkobj
.drivers_dir
);
1861 kobject_put(mod
->holders_dir
);
1862 mod_sysfs_fini(mod
);
1865 #ifdef CONFIG_STRICT_MODULE_RWX
1867 * LKM RO/NX protection: protect module's text/ro-data
1868 * from modification and any data from execution.
1870 * General layout of module is:
1871 * [text] [read-only-data] [ro-after-init] [writable data]
1872 * text_size -----^ ^ ^ ^
1873 * ro_size ------------------------| | |
1874 * ro_after_init_size -----------------------------| |
1875 * size -----------------------------------------------------------|
1877 * These values are always page-aligned (as is base)
1879 static void frob_text(const struct module_layout
*layout
,
1880 int (*set_memory
)(unsigned long start
, int num_pages
))
1882 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1883 BUG_ON((unsigned long)layout
->text_size
& (PAGE_SIZE
-1));
1884 set_memory((unsigned long)layout
->base
,
1885 layout
->text_size
>> PAGE_SHIFT
);
1888 static void frob_rodata(const struct module_layout
*layout
,
1889 int (*set_memory
)(unsigned long start
, int num_pages
))
1891 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1892 BUG_ON((unsigned long)layout
->text_size
& (PAGE_SIZE
-1));
1893 BUG_ON((unsigned long)layout
->ro_size
& (PAGE_SIZE
-1));
1894 set_memory((unsigned long)layout
->base
+ layout
->text_size
,
1895 (layout
->ro_size
- layout
->text_size
) >> PAGE_SHIFT
);
1898 static void frob_ro_after_init(const struct module_layout
*layout
,
1899 int (*set_memory
)(unsigned long start
, int num_pages
))
1901 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1902 BUG_ON((unsigned long)layout
->ro_size
& (PAGE_SIZE
-1));
1903 BUG_ON((unsigned long)layout
->ro_after_init_size
& (PAGE_SIZE
-1));
1904 set_memory((unsigned long)layout
->base
+ layout
->ro_size
,
1905 (layout
->ro_after_init_size
- layout
->ro_size
) >> PAGE_SHIFT
);
1908 static void frob_writable_data(const struct module_layout
*layout
,
1909 int (*set_memory
)(unsigned long start
, int num_pages
))
1911 BUG_ON((unsigned long)layout
->base
& (PAGE_SIZE
-1));
1912 BUG_ON((unsigned long)layout
->ro_after_init_size
& (PAGE_SIZE
-1));
1913 BUG_ON((unsigned long)layout
->size
& (PAGE_SIZE
-1));
1914 set_memory((unsigned long)layout
->base
+ layout
->ro_after_init_size
,
1915 (layout
->size
- layout
->ro_after_init_size
) >> PAGE_SHIFT
);
1918 /* livepatching wants to disable read-only so it can frob module. */
1919 void module_disable_ro(const struct module
*mod
)
1921 if (!rodata_enabled
)
1924 frob_text(&mod
->core_layout
, set_memory_rw
);
1925 frob_rodata(&mod
->core_layout
, set_memory_rw
);
1926 frob_ro_after_init(&mod
->core_layout
, set_memory_rw
);
1927 frob_text(&mod
->init_layout
, set_memory_rw
);
1928 frob_rodata(&mod
->init_layout
, set_memory_rw
);
1931 void module_enable_ro(const struct module
*mod
, bool after_init
)
1933 if (!rodata_enabled
)
1936 frob_text(&mod
->core_layout
, set_memory_ro
);
1937 frob_rodata(&mod
->core_layout
, set_memory_ro
);
1938 frob_text(&mod
->init_layout
, set_memory_ro
);
1939 frob_rodata(&mod
->init_layout
, set_memory_ro
);
1942 frob_ro_after_init(&mod
->core_layout
, set_memory_ro
);
1945 static void module_enable_nx(const struct module
*mod
)
1947 frob_rodata(&mod
->core_layout
, set_memory_nx
);
1948 frob_ro_after_init(&mod
->core_layout
, set_memory_nx
);
1949 frob_writable_data(&mod
->core_layout
, set_memory_nx
);
1950 frob_rodata(&mod
->init_layout
, set_memory_nx
);
1951 frob_writable_data(&mod
->init_layout
, set_memory_nx
);
1954 static void module_disable_nx(const struct module
*mod
)
1956 frob_rodata(&mod
->core_layout
, set_memory_x
);
1957 frob_ro_after_init(&mod
->core_layout
, set_memory_x
);
1958 frob_writable_data(&mod
->core_layout
, set_memory_x
);
1959 frob_rodata(&mod
->init_layout
, set_memory_x
);
1960 frob_writable_data(&mod
->init_layout
, set_memory_x
);
1963 /* Iterate through all modules and set each module's text as RW */
1964 void set_all_modules_text_rw(void)
1968 if (!rodata_enabled
)
1971 mutex_lock(&module_mutex
);
1972 list_for_each_entry_rcu(mod
, &modules
, list
) {
1973 if (mod
->state
== MODULE_STATE_UNFORMED
)
1976 frob_text(&mod
->core_layout
, set_memory_rw
);
1977 frob_text(&mod
->init_layout
, set_memory_rw
);
1979 mutex_unlock(&module_mutex
);
1982 /* Iterate through all modules and set each module's text as RO */
1983 void set_all_modules_text_ro(void)
1987 if (!rodata_enabled
)
1990 mutex_lock(&module_mutex
);
1991 list_for_each_entry_rcu(mod
, &modules
, list
) {
1993 * Ignore going modules since it's possible that ro
1994 * protection has already been disabled, otherwise we'll
1995 * run into protection faults at module deallocation.
1997 if (mod
->state
== MODULE_STATE_UNFORMED
||
1998 mod
->state
== MODULE_STATE_GOING
)
2001 frob_text(&mod
->core_layout
, set_memory_ro
);
2002 frob_text(&mod
->init_layout
, set_memory_ro
);
2004 mutex_unlock(&module_mutex
);
2007 static void disable_ro_nx(const struct module_layout
*layout
)
2009 if (rodata_enabled
) {
2010 frob_text(layout
, set_memory_rw
);
2011 frob_rodata(layout
, set_memory_rw
);
2012 frob_ro_after_init(layout
, set_memory_rw
);
2014 frob_rodata(layout
, set_memory_x
);
2015 frob_ro_after_init(layout
, set_memory_x
);
2016 frob_writable_data(layout
, set_memory_x
);
2020 static void disable_ro_nx(const struct module_layout
*layout
) { }
2021 static void module_enable_nx(const struct module
*mod
) { }
2022 static void module_disable_nx(const struct module
*mod
) { }
2025 #ifdef CONFIG_LIVEPATCH
2027 * Persist Elf information about a module. Copy the Elf header,
2028 * section header table, section string table, and symtab section
2029 * index from info to mod->klp_info.
2031 static int copy_module_elf(struct module
*mod
, struct load_info
*info
)
2033 unsigned int size
, symndx
;
2036 size
= sizeof(*mod
->klp_info
);
2037 mod
->klp_info
= kmalloc(size
, GFP_KERNEL
);
2038 if (mod
->klp_info
== NULL
)
2042 size
= sizeof(mod
->klp_info
->hdr
);
2043 memcpy(&mod
->klp_info
->hdr
, info
->hdr
, size
);
2045 /* Elf section header table */
2046 size
= sizeof(*info
->sechdrs
) * info
->hdr
->e_shnum
;
2047 mod
->klp_info
->sechdrs
= kmalloc(size
, GFP_KERNEL
);
2048 if (mod
->klp_info
->sechdrs
== NULL
) {
2052 memcpy(mod
->klp_info
->sechdrs
, info
->sechdrs
, size
);
2054 /* Elf section name string table */
2055 size
= info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_size
;
2056 mod
->klp_info
->secstrings
= kmalloc(size
, GFP_KERNEL
);
2057 if (mod
->klp_info
->secstrings
== NULL
) {
2061 memcpy(mod
->klp_info
->secstrings
, info
->secstrings
, size
);
2063 /* Elf symbol section index */
2064 symndx
= info
->index
.sym
;
2065 mod
->klp_info
->symndx
= symndx
;
2068 * For livepatch modules, core_kallsyms.symtab is a complete
2069 * copy of the original symbol table. Adjust sh_addr to point
2070 * to core_kallsyms.symtab since the copy of the symtab in module
2071 * init memory is freed at the end of do_init_module().
2073 mod
->klp_info
->sechdrs
[symndx
].sh_addr
= \
2074 (unsigned long) mod
->core_kallsyms
.symtab
;
2079 kfree(mod
->klp_info
->sechdrs
);
2081 kfree(mod
->klp_info
);
2085 static void free_module_elf(struct module
*mod
)
2087 kfree(mod
->klp_info
->sechdrs
);
2088 kfree(mod
->klp_info
->secstrings
);
2089 kfree(mod
->klp_info
);
2091 #else /* !CONFIG_LIVEPATCH */
2092 static int copy_module_elf(struct module
*mod
, struct load_info
*info
)
2097 static void free_module_elf(struct module
*mod
)
2100 #endif /* CONFIG_LIVEPATCH */
2102 void __weak
module_memfree(void *module_region
)
2104 vfree(module_region
);
2107 void __weak
module_arch_cleanup(struct module
*mod
)
2111 void __weak
module_arch_freeing_init(struct module
*mod
)
2115 /* Free a module, remove from lists, etc. */
2116 static void free_module(struct module
*mod
)
2118 trace_module_free(mod
);
2120 mod_sysfs_teardown(mod
);
2122 /* We leave it in list to prevent duplicate loads, but make sure
2123 * that noone uses it while it's being deconstructed. */
2124 mutex_lock(&module_mutex
);
2125 mod
->state
= MODULE_STATE_UNFORMED
;
2126 mutex_unlock(&module_mutex
);
2128 /* Remove dynamic debug info */
2129 ddebug_remove_module(mod
->name
);
2131 /* Arch-specific cleanup. */
2132 module_arch_cleanup(mod
);
2134 /* Module unload stuff */
2135 module_unload_free(mod
);
2137 /* Free any allocated parameters. */
2138 destroy_params(mod
->kp
, mod
->num_kp
);
2140 if (is_livepatch_module(mod
))
2141 free_module_elf(mod
);
2143 /* Now we can delete it from the lists */
2144 mutex_lock(&module_mutex
);
2145 /* Unlink carefully: kallsyms could be walking list. */
2146 list_del_rcu(&mod
->list
);
2147 mod_tree_remove(mod
);
2148 /* Remove this module from bug list, this uses list_del_rcu */
2149 module_bug_cleanup(mod
);
2150 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
2151 synchronize_sched();
2152 mutex_unlock(&module_mutex
);
2154 /* This may be empty, but that's OK */
2155 disable_ro_nx(&mod
->init_layout
);
2156 module_arch_freeing_init(mod
);
2157 module_memfree(mod
->init_layout
.base
);
2159 percpu_modfree(mod
);
2161 /* Free lock-classes; relies on the preceding sync_rcu(). */
2162 lockdep_free_key_range(mod
->core_layout
.base
, mod
->core_layout
.size
);
2164 /* Finally, free the core (containing the module structure) */
2165 disable_ro_nx(&mod
->core_layout
);
2166 module_memfree(mod
->core_layout
.base
);
2169 update_protections(current
->mm
);
2173 void *__symbol_get(const char *symbol
)
2175 struct module
*owner
;
2176 const struct kernel_symbol
*sym
;
2179 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
2180 if (sym
&& strong_try_module_get(owner
))
2184 return sym
? (void *)sym
->value
: NULL
;
2186 EXPORT_SYMBOL_GPL(__symbol_get
);
2189 * Ensure that an exported symbol [global namespace] does not already exist
2190 * in the kernel or in some other module's exported symbol table.
2192 * You must hold the module_mutex.
2194 static int verify_export_symbols(struct module
*mod
)
2197 struct module
*owner
;
2198 const struct kernel_symbol
*s
;
2200 const struct kernel_symbol
*sym
;
2203 { mod
->syms
, mod
->num_syms
},
2204 { mod
->gpl_syms
, mod
->num_gpl_syms
},
2205 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
2206 #ifdef CONFIG_UNUSED_SYMBOLS
2207 { mod
->unused_syms
, mod
->num_unused_syms
},
2208 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
2212 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
2213 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
2214 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
2215 pr_err("%s: exports duplicate symbol %s"
2217 mod
->name
, s
->name
, module_name(owner
));
2225 /* Change all symbols so that st_value encodes the pointer directly. */
2226 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
2228 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2229 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
2230 unsigned long secbase
;
2233 const struct kernel_symbol
*ksym
;
2235 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
2236 const char *name
= info
->strtab
+ sym
[i
].st_name
;
2238 switch (sym
[i
].st_shndx
) {
2240 /* Ignore common symbols */
2241 if (!strncmp(name
, "__gnu_lto", 9))
2244 /* We compiled with -fno-common. These are not
2245 supposed to happen. */
2246 pr_debug("Common symbol: %s\n", name
);
2247 pr_warn("%s: please compile with -fno-common\n",
2253 /* Don't need to do anything */
2254 pr_debug("Absolute symbol: 0x%08lx\n",
2255 (long)sym
[i
].st_value
);
2259 /* Livepatch symbols are resolved by livepatch */
2263 ksym
= resolve_symbol_wait(mod
, info
, name
);
2264 /* Ok if resolved. */
2265 if (ksym
&& !IS_ERR(ksym
)) {
2266 sym
[i
].st_value
= ksym
->value
;
2271 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
2274 pr_warn("%s: Unknown symbol %s (err %li)\n",
2275 mod
->name
, name
, PTR_ERR(ksym
));
2276 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
2280 /* Divert to percpu allocation if a percpu var. */
2281 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
2282 secbase
= (unsigned long)mod_percpu(mod
);
2284 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
2285 sym
[i
].st_value
+= secbase
;
2293 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
2298 /* Now do relocations. */
2299 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2300 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
2302 /* Not a valid relocation section? */
2303 if (infosec
>= info
->hdr
->e_shnum
)
2306 /* Don't bother with non-allocated sections */
2307 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
2310 /* Livepatch relocation sections are applied by livepatch */
2311 if (info
->sechdrs
[i
].sh_flags
& SHF_RELA_LIVEPATCH
)
2314 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
2315 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
2316 info
->index
.sym
, i
, mod
);
2317 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
2318 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
2319 info
->index
.sym
, i
, mod
);
2326 /* Additional bytes needed by arch in front of individual sections */
2327 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
2328 unsigned int section
)
2330 /* default implementation just returns zero */
2334 /* Update size with this section: return offset. */
2335 static long get_offset(struct module
*mod
, unsigned int *size
,
2336 Elf_Shdr
*sechdr
, unsigned int section
)
2340 *size
+= arch_mod_section_prepend(mod
, section
);
2341 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
2342 *size
= ret
+ sechdr
->sh_size
;
2346 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2347 might -- code, read-only data, read-write data, small data. Tally
2348 sizes, and place the offsets into sh_entsize fields: high bit means it
2350 static void layout_sections(struct module
*mod
, struct load_info
*info
)
2352 static unsigned long const masks
[][2] = {
2353 /* NOTE: all executable code must be the first section
2354 * in this array; otherwise modify the text_size
2355 * finder in the two loops below */
2356 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2357 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
2358 { SHF_RO_AFTER_INIT
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2359 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2360 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
2364 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
2365 info
->sechdrs
[i
].sh_entsize
= ~0UL;
2367 pr_debug("Core section allocation order:\n");
2368 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2369 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2370 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2371 const char *sname
= info
->secstrings
+ s
->sh_name
;
2373 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2374 || (s
->sh_flags
& masks
[m
][1])
2375 || s
->sh_entsize
!= ~0UL
2376 || strstarts(sname
, ".init"))
2378 s
->sh_entsize
= get_offset(mod
, &mod
->core_layout
.size
, s
, i
);
2379 pr_debug("\t%s\n", sname
);
2382 case 0: /* executable */
2383 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2384 mod
->core_layout
.text_size
= mod
->core_layout
.size
;
2386 case 1: /* RO: text and ro-data */
2387 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2388 mod
->core_layout
.ro_size
= mod
->core_layout
.size
;
2390 case 2: /* RO after init */
2391 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2392 mod
->core_layout
.ro_after_init_size
= mod
->core_layout
.size
;
2394 case 4: /* whole core */
2395 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2400 pr_debug("Init section allocation order:\n");
2401 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2402 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2403 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2404 const char *sname
= info
->secstrings
+ s
->sh_name
;
2406 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2407 || (s
->sh_flags
& masks
[m
][1])
2408 || s
->sh_entsize
!= ~0UL
2409 || !strstarts(sname
, ".init"))
2411 s
->sh_entsize
= (get_offset(mod
, &mod
->init_layout
.size
, s
, i
)
2412 | INIT_OFFSET_MASK
);
2413 pr_debug("\t%s\n", sname
);
2416 case 0: /* executable */
2417 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2418 mod
->init_layout
.text_size
= mod
->init_layout
.size
;
2420 case 1: /* RO: text and ro-data */
2421 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2422 mod
->init_layout
.ro_size
= mod
->init_layout
.size
;
2426 * RO after init doesn't apply to init_layout (only
2427 * core_layout), so it just takes the value of ro_size.
2429 mod
->init_layout
.ro_after_init_size
= mod
->init_layout
.ro_size
;
2431 case 4: /* whole init */
2432 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2438 static void set_license(struct module
*mod
, const char *license
)
2441 license
= "unspecified";
2443 if (!license_is_gpl_compatible(license
)) {
2444 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
2445 pr_warn("%s: module license '%s' taints kernel.\n",
2446 mod
->name
, license
);
2447 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
2448 LOCKDEP_NOW_UNRELIABLE
);
2452 /* Parse tag=value strings from .modinfo section */
2453 static char *next_string(char *string
, unsigned long *secsize
)
2455 /* Skip non-zero chars */
2458 if ((*secsize
)-- <= 1)
2462 /* Skip any zero padding. */
2463 while (!string
[0]) {
2465 if ((*secsize
)-- <= 1)
2471 static char *get_modinfo(struct load_info
*info
, const char *tag
)
2474 unsigned int taglen
= strlen(tag
);
2475 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
2476 unsigned long size
= infosec
->sh_size
;
2478 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
2479 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
2480 return p
+ taglen
+ 1;
2485 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2487 struct module_attribute
*attr
;
2490 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2492 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2496 static void free_modinfo(struct module
*mod
)
2498 struct module_attribute
*attr
;
2501 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2507 #ifdef CONFIG_KALLSYMS
2509 /* lookup symbol in given range of kernel_symbols */
2510 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2511 const struct kernel_symbol
*start
,
2512 const struct kernel_symbol
*stop
)
2514 return bsearch(name
, start
, stop
- start
,
2515 sizeof(struct kernel_symbol
), cmp_name
);
2518 static int is_exported(const char *name
, unsigned long value
,
2519 const struct module
*mod
)
2521 const struct kernel_symbol
*ks
;
2523 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2525 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2526 return ks
!= NULL
&& ks
->value
== value
;
2530 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2532 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2534 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2535 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2540 if (sym
->st_shndx
== SHN_UNDEF
)
2542 if (sym
->st_shndx
== SHN_ABS
|| sym
->st_shndx
== info
->index
.pcpu
)
2544 if (sym
->st_shndx
>= SHN_LORESERVE
)
2546 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2548 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2549 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2550 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2552 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2557 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2558 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2563 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2570 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2571 unsigned int shnum
, unsigned int pcpundx
)
2573 const Elf_Shdr
*sec
;
2575 if (src
->st_shndx
== SHN_UNDEF
2576 || src
->st_shndx
>= shnum
2580 #ifdef CONFIG_KALLSYMS_ALL
2581 if (src
->st_shndx
== pcpundx
)
2585 sec
= sechdrs
+ src
->st_shndx
;
2586 if (!(sec
->sh_flags
& SHF_ALLOC
)
2587 #ifndef CONFIG_KALLSYMS_ALL
2588 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2590 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2597 * We only allocate and copy the strings needed by the parts of symtab
2598 * we keep. This is simple, but has the effect of making multiple
2599 * copies of duplicates. We could be more sophisticated, see
2600 * linux-kernel thread starting with
2601 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2603 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2605 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2606 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2608 unsigned int i
, nsrc
, ndst
, strtab_size
= 0;
2610 /* Put symbol section at end of init part of module. */
2611 symsect
->sh_flags
|= SHF_ALLOC
;
2612 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_layout
.size
, symsect
,
2613 info
->index
.sym
) | INIT_OFFSET_MASK
;
2614 pr_debug("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2616 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2617 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2619 /* Compute total space required for the core symbols' strtab. */
2620 for (ndst
= i
= 0; i
< nsrc
; i
++) {
2621 if (i
== 0 || is_livepatch_module(mod
) ||
2622 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
,
2623 info
->index
.pcpu
)) {
2624 strtab_size
+= strlen(&info
->strtab
[src
[i
].st_name
])+1;
2629 /* Append room for core symbols at end of core part. */
2630 info
->symoffs
= ALIGN(mod
->core_layout
.size
, symsect
->sh_addralign
?: 1);
2631 info
->stroffs
= mod
->core_layout
.size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2632 mod
->core_layout
.size
+= strtab_size
;
2633 mod
->core_layout
.size
= debug_align(mod
->core_layout
.size
);
2635 /* Put string table section at end of init part of module. */
2636 strsect
->sh_flags
|= SHF_ALLOC
;
2637 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_layout
.size
, strsect
,
2638 info
->index
.str
) | INIT_OFFSET_MASK
;
2639 pr_debug("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2641 /* We'll tack temporary mod_kallsyms on the end. */
2642 mod
->init_layout
.size
= ALIGN(mod
->init_layout
.size
,
2643 __alignof__(struct mod_kallsyms
));
2644 info
->mod_kallsyms_init_off
= mod
->init_layout
.size
;
2645 mod
->init_layout
.size
+= sizeof(struct mod_kallsyms
);
2646 mod
->init_layout
.size
= debug_align(mod
->init_layout
.size
);
2650 * We use the full symtab and strtab which layout_symtab arranged to
2651 * be appended to the init section. Later we switch to the cut-down
2654 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2656 unsigned int i
, ndst
;
2660 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2662 /* Set up to point into init section. */
2663 mod
->kallsyms
= mod
->init_layout
.base
+ info
->mod_kallsyms_init_off
;
2665 mod
->kallsyms
->symtab
= (void *)symsec
->sh_addr
;
2666 mod
->kallsyms
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2667 /* Make sure we get permanent strtab: don't use info->strtab. */
2668 mod
->kallsyms
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2670 /* Set types up while we still have access to sections. */
2671 for (i
= 0; i
< mod
->kallsyms
->num_symtab
; i
++)
2672 mod
->kallsyms
->symtab
[i
].st_info
2673 = elf_type(&mod
->kallsyms
->symtab
[i
], info
);
2675 /* Now populate the cut down core kallsyms for after init. */
2676 mod
->core_kallsyms
.symtab
= dst
= mod
->core_layout
.base
+ info
->symoffs
;
2677 mod
->core_kallsyms
.strtab
= s
= mod
->core_layout
.base
+ info
->stroffs
;
2678 src
= mod
->kallsyms
->symtab
;
2679 for (ndst
= i
= 0; i
< mod
->kallsyms
->num_symtab
; i
++) {
2680 if (i
== 0 || is_livepatch_module(mod
) ||
2681 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
,
2682 info
->index
.pcpu
)) {
2684 dst
[ndst
++].st_name
= s
- mod
->core_kallsyms
.strtab
;
2685 s
+= strlcpy(s
, &mod
->kallsyms
->strtab
[src
[i
].st_name
],
2689 mod
->core_kallsyms
.num_symtab
= ndst
;
2692 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2696 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2699 #endif /* CONFIG_KALLSYMS */
2701 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2705 #ifdef CONFIG_DYNAMIC_DEBUG
2706 if (ddebug_add_module(debug
, num
, debug
->modname
))
2707 pr_err("dynamic debug error adding module: %s\n",
2712 static void dynamic_debug_remove(struct _ddebug
*debug
)
2715 ddebug_remove_module(debug
->modname
);
2718 void * __weak
module_alloc(unsigned long size
)
2720 return vmalloc_exec(size
);
2723 #ifdef CONFIG_DEBUG_KMEMLEAK
2724 static void kmemleak_load_module(const struct module
*mod
,
2725 const struct load_info
*info
)
2729 /* only scan the sections containing data */
2730 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2732 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2733 /* Scan all writable sections that's not executable */
2734 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
) ||
2735 !(info
->sechdrs
[i
].sh_flags
& SHF_WRITE
) ||
2736 (info
->sechdrs
[i
].sh_flags
& SHF_EXECINSTR
))
2739 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2740 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2744 static inline void kmemleak_load_module(const struct module
*mod
,
2745 const struct load_info
*info
)
2750 #ifdef CONFIG_MODULE_SIG
2751 static int module_sig_check(struct load_info
*info
, int flags
)
2754 const unsigned long markerlen
= sizeof(MODULE_SIG_STRING
) - 1;
2755 const void *mod
= info
->hdr
;
2758 * Require flags == 0, as a module with version information
2759 * removed is no longer the module that was signed
2762 info
->len
> markerlen
&&
2763 memcmp(mod
+ info
->len
- markerlen
, MODULE_SIG_STRING
, markerlen
) == 0) {
2764 /* We truncate the module to discard the signature */
2765 info
->len
-= markerlen
;
2766 err
= mod_verify_sig(mod
, &info
->len
);
2770 info
->sig_ok
= true;
2774 /* Not having a signature is only an error if we're strict. */
2775 if (err
== -ENOKEY
&& !sig_enforce
)
2780 #else /* !CONFIG_MODULE_SIG */
2781 static int module_sig_check(struct load_info
*info
, int flags
)
2785 #endif /* !CONFIG_MODULE_SIG */
2787 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2788 static int elf_header_check(struct load_info
*info
)
2790 if (info
->len
< sizeof(*(info
->hdr
)))
2793 if (memcmp(info
->hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2794 || info
->hdr
->e_type
!= ET_REL
2795 || !elf_check_arch(info
->hdr
)
2796 || info
->hdr
->e_shentsize
!= sizeof(Elf_Shdr
))
2799 if (info
->hdr
->e_shoff
>= info
->len
2800 || (info
->hdr
->e_shnum
* sizeof(Elf_Shdr
) >
2801 info
->len
- info
->hdr
->e_shoff
))
2807 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
2809 static int copy_chunked_from_user(void *dst
, const void __user
*usrc
, unsigned long len
)
2812 unsigned long n
= min(len
, COPY_CHUNK_SIZE
);
2814 if (copy_from_user(dst
, usrc
, n
) != 0)
2824 #ifdef CONFIG_LIVEPATCH
2825 static int check_modinfo_livepatch(struct module
*mod
, struct load_info
*info
)
2827 if (get_modinfo(info
, "livepatch")) {
2829 add_taint_module(mod
, TAINT_LIVEPATCH
, LOCKDEP_STILL_OK
);
2830 pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
2836 #else /* !CONFIG_LIVEPATCH */
2837 static int check_modinfo_livepatch(struct module
*mod
, struct load_info
*info
)
2839 if (get_modinfo(info
, "livepatch")) {
2840 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
2847 #endif /* CONFIG_LIVEPATCH */
2849 /* Sets info->hdr and info->len. */
2850 static int copy_module_from_user(const void __user
*umod
, unsigned long len
,
2851 struct load_info
*info
)
2856 if (info
->len
< sizeof(*(info
->hdr
)))
2859 err
= security_kernel_read_file(NULL
, READING_MODULE
);
2863 /* Suck in entire file: we'll want most of it. */
2864 info
->hdr
= __vmalloc(info
->len
,
2865 GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_NOWARN
, PAGE_KERNEL
);
2869 if (copy_chunked_from_user(info
->hdr
, umod
, info
->len
) != 0) {
2877 static void free_copy(struct load_info
*info
)
2882 static int rewrite_section_headers(struct load_info
*info
, int flags
)
2886 /* This should always be true, but let's be sure. */
2887 info
->sechdrs
[0].sh_addr
= 0;
2889 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2890 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2891 if (shdr
->sh_type
!= SHT_NOBITS
2892 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2893 pr_err("Module len %lu truncated\n", info
->len
);
2897 /* Mark all sections sh_addr with their address in the
2899 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2901 #ifndef CONFIG_MODULE_UNLOAD
2902 /* Don't load .exit sections */
2903 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2904 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2908 /* Track but don't keep modinfo and version sections. */
2909 if (flags
& MODULE_INIT_IGNORE_MODVERSIONS
)
2910 info
->index
.vers
= 0; /* Pretend no __versions section! */
2912 info
->index
.vers
= find_sec(info
, "__versions");
2913 info
->index
.info
= find_sec(info
, ".modinfo");
2914 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2915 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2920 * Set up our basic convenience variables (pointers to section headers,
2921 * search for module section index etc), and do some basic section
2924 * Return the temporary module pointer (we'll replace it with the final
2925 * one when we move the module sections around).
2927 static struct module
*setup_load_info(struct load_info
*info
, int flags
)
2933 /* Set up the convenience variables */
2934 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2935 info
->secstrings
= (void *)info
->hdr
2936 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2938 err
= rewrite_section_headers(info
, flags
);
2940 return ERR_PTR(err
);
2942 /* Find internal symbols and strings. */
2943 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2944 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2945 info
->index
.sym
= i
;
2946 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2947 info
->strtab
= (char *)info
->hdr
2948 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2953 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2954 if (!info
->index
.mod
) {
2955 pr_warn("No module found in object\n");
2956 return ERR_PTR(-ENOEXEC
);
2958 /* This is temporary: point mod into copy of data. */
2959 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2961 if (info
->index
.sym
== 0) {
2962 pr_warn("%s: module has no symbols (stripped?)\n", mod
->name
);
2963 return ERR_PTR(-ENOEXEC
);
2966 info
->index
.pcpu
= find_pcpusec(info
);
2968 /* Check module struct version now, before we try to use module. */
2969 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2970 return ERR_PTR(-ENOEXEC
);
2975 static int check_modinfo(struct module
*mod
, struct load_info
*info
, int flags
)
2977 const char *modmagic
= get_modinfo(info
, "vermagic");
2980 if (flags
& MODULE_INIT_IGNORE_VERMAGIC
)
2983 /* This is allowed: modprobe --force will invalidate it. */
2985 err
= try_to_force_load(mod
, "bad vermagic");
2988 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2989 pr_err("%s: version magic '%s' should be '%s'\n",
2990 mod
->name
, modmagic
, vermagic
);
2994 if (!get_modinfo(info
, "intree")) {
2995 if (!test_taint(TAINT_OOT_MODULE
))
2996 pr_warn("%s: loading out-of-tree module taints kernel.\n",
2998 add_taint_module(mod
, TAINT_OOT_MODULE
, LOCKDEP_STILL_OK
);
3001 if (get_modinfo(info
, "staging")) {
3002 add_taint_module(mod
, TAINT_CRAP
, LOCKDEP_STILL_OK
);
3003 pr_warn("%s: module is from the staging directory, the quality "
3004 "is unknown, you have been warned.\n", mod
->name
);
3007 err
= check_modinfo_livepatch(mod
, info
);
3011 /* Set up license info based on the info section */
3012 set_license(mod
, get_modinfo(info
, "license"));
3017 static int find_module_sections(struct module
*mod
, struct load_info
*info
)
3019 mod
->kp
= section_objs(info
, "__param",
3020 sizeof(*mod
->kp
), &mod
->num_kp
);
3021 mod
->syms
= section_objs(info
, "__ksymtab",
3022 sizeof(*mod
->syms
), &mod
->num_syms
);
3023 mod
->crcs
= section_addr(info
, "__kcrctab");
3024 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
3025 sizeof(*mod
->gpl_syms
),
3026 &mod
->num_gpl_syms
);
3027 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
3028 mod
->gpl_future_syms
= section_objs(info
,
3029 "__ksymtab_gpl_future",
3030 sizeof(*mod
->gpl_future_syms
),
3031 &mod
->num_gpl_future_syms
);
3032 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
3034 #ifdef CONFIG_UNUSED_SYMBOLS
3035 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
3036 sizeof(*mod
->unused_syms
),
3037 &mod
->num_unused_syms
);
3038 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
3039 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
3040 sizeof(*mod
->unused_gpl_syms
),
3041 &mod
->num_unused_gpl_syms
);
3042 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
3044 #ifdef CONFIG_CONSTRUCTORS
3045 mod
->ctors
= section_objs(info
, ".ctors",
3046 sizeof(*mod
->ctors
), &mod
->num_ctors
);
3048 mod
->ctors
= section_objs(info
, ".init_array",
3049 sizeof(*mod
->ctors
), &mod
->num_ctors
);
3050 else if (find_sec(info
, ".init_array")) {
3052 * This shouldn't happen with same compiler and binutils
3053 * building all parts of the module.
3055 pr_warn("%s: has both .ctors and .init_array.\n",
3061 #ifdef CONFIG_TRACEPOINTS
3062 mod
->tracepoints_ptrs
= section_objs(info
, "__tracepoints_ptrs",
3063 sizeof(*mod
->tracepoints_ptrs
),
3064 &mod
->num_tracepoints
);
3066 #ifdef HAVE_JUMP_LABEL
3067 mod
->jump_entries
= section_objs(info
, "__jump_table",
3068 sizeof(*mod
->jump_entries
),
3069 &mod
->num_jump_entries
);
3071 #ifdef CONFIG_EVENT_TRACING
3072 mod
->trace_events
= section_objs(info
, "_ftrace_events",
3073 sizeof(*mod
->trace_events
),
3074 &mod
->num_trace_events
);
3075 mod
->trace_enums
= section_objs(info
, "_ftrace_enum_map",
3076 sizeof(*mod
->trace_enums
),
3077 &mod
->num_trace_enums
);
3079 #ifdef CONFIG_TRACING
3080 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
3081 sizeof(*mod
->trace_bprintk_fmt_start
),
3082 &mod
->num_trace_bprintk_fmt
);
3084 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
3085 /* sechdrs[0].sh_size is always zero */
3086 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
3087 sizeof(*mod
->ftrace_callsites
),
3088 &mod
->num_ftrace_callsites
);
3091 mod
->extable
= section_objs(info
, "__ex_table",
3092 sizeof(*mod
->extable
), &mod
->num_exentries
);
3094 if (section_addr(info
, "__obsparm"))
3095 pr_warn("%s: Ignoring obsolete parameters\n", mod
->name
);
3097 info
->debug
= section_objs(info
, "__verbose",
3098 sizeof(*info
->debug
), &info
->num_debug
);
3103 static int move_module(struct module
*mod
, struct load_info
*info
)
3108 /* Do the allocs. */
3109 ptr
= module_alloc(mod
->core_layout
.size
);
3111 * The pointer to this block is stored in the module structure
3112 * which is inside the block. Just mark it as not being a
3115 kmemleak_not_leak(ptr
);
3119 memset(ptr
, 0, mod
->core_layout
.size
);
3120 mod
->core_layout
.base
= ptr
;
3122 if (mod
->init_layout
.size
) {
3123 ptr
= module_alloc(mod
->init_layout
.size
);
3125 * The pointer to this block is stored in the module structure
3126 * which is inside the block. This block doesn't need to be
3127 * scanned as it contains data and code that will be freed
3128 * after the module is initialized.
3130 kmemleak_ignore(ptr
);
3132 module_memfree(mod
->core_layout
.base
);
3135 memset(ptr
, 0, mod
->init_layout
.size
);
3136 mod
->init_layout
.base
= ptr
;
3138 mod
->init_layout
.base
= NULL
;
3140 /* Transfer each section which specifies SHF_ALLOC */
3141 pr_debug("final section addresses:\n");
3142 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
3144 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
3146 if (!(shdr
->sh_flags
& SHF_ALLOC
))
3149 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
3150 dest
= mod
->init_layout
.base
3151 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
3153 dest
= mod
->core_layout
.base
+ shdr
->sh_entsize
;
3155 if (shdr
->sh_type
!= SHT_NOBITS
)
3156 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
3157 /* Update sh_addr to point to copy in image. */
3158 shdr
->sh_addr
= (unsigned long)dest
;
3159 pr_debug("\t0x%lx %s\n",
3160 (long)shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
3166 static int check_module_license_and_versions(struct module
*mod
)
3168 int prev_taint
= test_taint(TAINT_PROPRIETARY_MODULE
);
3171 * ndiswrapper is under GPL by itself, but loads proprietary modules.
3172 * Don't use add_taint_module(), as it would prevent ndiswrapper from
3173 * using GPL-only symbols it needs.
3175 if (strcmp(mod
->name
, "ndiswrapper") == 0)
3176 add_taint(TAINT_PROPRIETARY_MODULE
, LOCKDEP_NOW_UNRELIABLE
);
3178 /* driverloader was caught wrongly pretending to be under GPL */
3179 if (strcmp(mod
->name
, "driverloader") == 0)
3180 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
3181 LOCKDEP_NOW_UNRELIABLE
);
3183 /* lve claims to be GPL but upstream won't provide source */
3184 if (strcmp(mod
->name
, "lve") == 0)
3185 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
,
3186 LOCKDEP_NOW_UNRELIABLE
);
3188 if (!prev_taint
&& test_taint(TAINT_PROPRIETARY_MODULE
))
3189 pr_warn("%s: module license taints kernel.\n", mod
->name
);
3191 #ifdef CONFIG_MODVERSIONS
3192 if ((mod
->num_syms
&& !mod
->crcs
)
3193 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
3194 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
3195 #ifdef CONFIG_UNUSED_SYMBOLS
3196 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
3197 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
3200 return try_to_force_load(mod
,
3201 "no versions for exported symbols");
3207 static void flush_module_icache(const struct module
*mod
)
3209 mm_segment_t old_fs
;
3211 /* flush the icache in correct context */
3216 * Flush the instruction cache, since we've played with text.
3217 * Do it before processing of module parameters, so the module
3218 * can provide parameter accessor functions of its own.
3220 if (mod
->init_layout
.base
)
3221 flush_icache_range((unsigned long)mod
->init_layout
.base
,
3222 (unsigned long)mod
->init_layout
.base
3223 + mod
->init_layout
.size
);
3224 flush_icache_range((unsigned long)mod
->core_layout
.base
,
3225 (unsigned long)mod
->core_layout
.base
+ mod
->core_layout
.size
);
3230 int __weak
module_frob_arch_sections(Elf_Ehdr
*hdr
,
3238 /* module_blacklist is a comma-separated list of module names */
3239 static char *module_blacklist
;
3240 static bool blacklisted(char *module_name
)
3245 if (!module_blacklist
)
3248 for (p
= module_blacklist
; *p
; p
+= len
) {
3249 len
= strcspn(p
, ",");
3250 if (strlen(module_name
) == len
&& !memcmp(module_name
, p
, len
))
3257 core_param(module_blacklist
, module_blacklist
, charp
, 0400);
3259 static struct module
*layout_and_allocate(struct load_info
*info
, int flags
)
3261 /* Module within temporary copy. */
3266 mod
= setup_load_info(info
, flags
);
3270 if (blacklisted(mod
->name
))
3271 return ERR_PTR(-EPERM
);
3273 err
= check_modinfo(mod
, info
, flags
);
3275 return ERR_PTR(err
);
3277 /* Allow arches to frob section contents and sizes. */
3278 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
3279 info
->secstrings
, mod
);
3281 return ERR_PTR(err
);
3283 /* We will do a special allocation for per-cpu sections later. */
3284 info
->sechdrs
[info
->index
.pcpu
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
3287 * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
3288 * layout_sections() can put it in the right place.
3289 * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
3291 ndx
= find_sec(info
, ".data..ro_after_init");
3293 info
->sechdrs
[ndx
].sh_flags
|= SHF_RO_AFTER_INIT
;
3295 /* Determine total sizes, and put offsets in sh_entsize. For now
3296 this is done generically; there doesn't appear to be any
3297 special cases for the architectures. */
3298 layout_sections(mod
, info
);
3299 layout_symtab(mod
, info
);
3301 /* Allocate and move to the final place */
3302 err
= move_module(mod
, info
);
3304 return ERR_PTR(err
);
3306 /* Module has been copied to its final place now: return it. */
3307 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
3308 kmemleak_load_module(mod
, info
);
3312 /* mod is no longer valid after this! */
3313 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
3315 percpu_modfree(mod
);
3316 module_arch_freeing_init(mod
);
3317 module_memfree(mod
->init_layout
.base
);
3318 module_memfree(mod
->core_layout
.base
);
3321 int __weak
module_finalize(const Elf_Ehdr
*hdr
,
3322 const Elf_Shdr
*sechdrs
,
3328 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
3330 /* Sort exception table now relocations are done. */
3331 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
3333 /* Copy relocated percpu area over. */
3334 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
3335 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
3337 /* Setup kallsyms-specific fields. */
3338 add_kallsyms(mod
, info
);
3340 /* Arch-specific module finalizing. */
3341 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
3344 /* Is this module of this name done loading? No locks held. */
3345 static bool finished_loading(const char *name
)
3351 * The module_mutex should not be a heavily contended lock;
3352 * if we get the occasional sleep here, we'll go an extra iteration
3353 * in the wait_event_interruptible(), which is harmless.
3355 sched_annotate_sleep();
3356 mutex_lock(&module_mutex
);
3357 mod
= find_module_all(name
, strlen(name
), true);
3358 ret
= !mod
|| mod
->state
== MODULE_STATE_LIVE
3359 || mod
->state
== MODULE_STATE_GOING
;
3360 mutex_unlock(&module_mutex
);
3365 /* Call module constructors. */
3366 static void do_mod_ctors(struct module
*mod
)
3368 #ifdef CONFIG_CONSTRUCTORS
3371 for (i
= 0; i
< mod
->num_ctors
; i
++)
3376 /* For freeing module_init on success, in case kallsyms traversing */
3377 struct mod_initfree
{
3378 struct rcu_head rcu
;
3382 static void do_free_init(struct rcu_head
*head
)
3384 struct mod_initfree
*m
= container_of(head
, struct mod_initfree
, rcu
);
3385 module_memfree(m
->module_init
);
3390 * This is where the real work happens.
3392 * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
3393 * helper command 'lx-symbols'.
3395 static noinline
int do_init_module(struct module
*mod
)
3398 struct mod_initfree
*freeinit
;
3400 freeinit
= kmalloc(sizeof(*freeinit
), GFP_KERNEL
);
3405 freeinit
->module_init
= mod
->init_layout
.base
;
3408 * We want to find out whether @mod uses async during init. Clear
3409 * PF_USED_ASYNC. async_schedule*() will set it.
3411 current
->flags
&= ~PF_USED_ASYNC
;
3414 /* Start the module */
3415 if (mod
->init
!= NULL
)
3416 ret
= do_one_initcall(mod
->init
);
3418 goto fail_free_freeinit
;
3421 pr_warn("%s: '%s'->init suspiciously returned %d, it should "
3422 "follow 0/-E convention\n"
3423 "%s: loading module anyway...\n",
3424 __func__
, mod
->name
, ret
, __func__
);
3428 /* Now it's a first class citizen! */
3429 mod
->state
= MODULE_STATE_LIVE
;
3430 blocking_notifier_call_chain(&module_notify_list
,
3431 MODULE_STATE_LIVE
, mod
);
3434 * We need to finish all async code before the module init sequence
3435 * is done. This has potential to deadlock. For example, a newly
3436 * detected block device can trigger request_module() of the
3437 * default iosched from async probing task. Once userland helper
3438 * reaches here, async_synchronize_full() will wait on the async
3439 * task waiting on request_module() and deadlock.
3441 * This deadlock is avoided by perfomring async_synchronize_full()
3442 * iff module init queued any async jobs. This isn't a full
3443 * solution as it will deadlock the same if module loading from
3444 * async jobs nests more than once; however, due to the various
3445 * constraints, this hack seems to be the best option for now.
3446 * Please refer to the following thread for details.
3448 * http://thread.gmane.org/gmane.linux.kernel/1420814
3450 if (!mod
->async_probe_requested
&& (current
->flags
& PF_USED_ASYNC
))
3451 async_synchronize_full();
3453 mutex_lock(&module_mutex
);
3454 /* Drop initial reference. */
3456 trim_init_extable(mod
);
3457 #ifdef CONFIG_KALLSYMS
3458 /* Switch to core kallsyms now init is done: kallsyms may be walking! */
3459 rcu_assign_pointer(mod
->kallsyms
, &mod
->core_kallsyms
);
3461 module_enable_ro(mod
, true);
3462 mod_tree_remove_init(mod
);
3463 disable_ro_nx(&mod
->init_layout
);
3464 module_arch_freeing_init(mod
);
3465 mod
->init_layout
.base
= NULL
;
3466 mod
->init_layout
.size
= 0;
3467 mod
->init_layout
.ro_size
= 0;
3468 mod
->init_layout
.ro_after_init_size
= 0;
3469 mod
->init_layout
.text_size
= 0;
3471 * We want to free module_init, but be aware that kallsyms may be
3472 * walking this with preempt disabled. In all the failure paths, we
3473 * call synchronize_sched(), but we don't want to slow down the success
3474 * path, so use actual RCU here.
3476 call_rcu_sched(&freeinit
->rcu
, do_free_init
);
3477 mutex_unlock(&module_mutex
);
3478 wake_up_all(&module_wq
);
3485 /* Try to protect us from buggy refcounters. */
3486 mod
->state
= MODULE_STATE_GOING
;
3487 synchronize_sched();
3489 blocking_notifier_call_chain(&module_notify_list
,
3490 MODULE_STATE_GOING
, mod
);
3491 klp_module_going(mod
);
3492 ftrace_release_mod(mod
);
3494 wake_up_all(&module_wq
);
3498 static int may_init_module(void)
3500 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
3507 * We try to place it in the list now to make sure it's unique before
3508 * we dedicate too many resources. In particular, temporary percpu
3509 * memory exhaustion.
3511 static int add_unformed_module(struct module
*mod
)
3516 mod
->state
= MODULE_STATE_UNFORMED
;
3519 mutex_lock(&module_mutex
);
3520 old
= find_module_all(mod
->name
, strlen(mod
->name
), true);
3522 if (old
->state
== MODULE_STATE_COMING
3523 || old
->state
== MODULE_STATE_UNFORMED
) {
3524 /* Wait in case it fails to load. */
3525 mutex_unlock(&module_mutex
);
3526 err
= wait_event_interruptible(module_wq
,
3527 finished_loading(mod
->name
));
3535 mod_update_bounds(mod
);
3536 list_add_rcu(&mod
->list
, &modules
);
3537 mod_tree_insert(mod
);
3541 mutex_unlock(&module_mutex
);
3546 static int complete_formation(struct module
*mod
, struct load_info
*info
)
3550 mutex_lock(&module_mutex
);
3552 /* Find duplicate symbols (must be called under lock). */
3553 err
= verify_export_symbols(mod
);
3557 /* This relies on module_mutex for list integrity. */
3558 module_bug_finalize(info
->hdr
, info
->sechdrs
, mod
);
3560 module_enable_ro(mod
, false);
3561 module_enable_nx(mod
);
3563 /* Mark state as coming so strong_try_module_get() ignores us,
3564 * but kallsyms etc. can see us. */
3565 mod
->state
= MODULE_STATE_COMING
;
3566 mutex_unlock(&module_mutex
);
3571 mutex_unlock(&module_mutex
);
3575 static int prepare_coming_module(struct module
*mod
)
3579 ftrace_module_enable(mod
);
3580 err
= klp_module_coming(mod
);
3584 blocking_notifier_call_chain(&module_notify_list
,
3585 MODULE_STATE_COMING
, mod
);
3589 static int unknown_module_param_cb(char *param
, char *val
, const char *modname
,
3592 struct module
*mod
= arg
;
3595 if (strcmp(param
, "async_probe") == 0) {
3596 mod
->async_probe_requested
= true;
3600 /* Check for magic 'dyndbg' arg */
3601 ret
= ddebug_dyndbg_module_param_cb(param
, val
, modname
);
3603 pr_warn("%s: unknown parameter '%s' ignored\n", modname
, param
);
3607 /* Allocate and load the module: note that size of section 0 is always
3608 zero, and we rely on this for optional sections. */
3609 static int load_module(struct load_info
*info
, const char __user
*uargs
,
3616 err
= module_sig_check(info
, flags
);
3620 err
= elf_header_check(info
);
3624 /* Figure out module layout, and allocate all the memory. */
3625 mod
= layout_and_allocate(info
, flags
);
3631 audit_log_kern_module(mod
->name
);
3633 /* Reserve our place in the list. */
3634 err
= add_unformed_module(mod
);
3638 #ifdef CONFIG_MODULE_SIG
3639 mod
->sig_ok
= info
->sig_ok
;
3641 pr_notice_once("%s: module verification failed: signature "
3642 "and/or required key missing - tainting "
3643 "kernel\n", mod
->name
);
3644 add_taint_module(mod
, TAINT_UNSIGNED_MODULE
, LOCKDEP_STILL_OK
);
3648 /* To avoid stressing percpu allocator, do this once we're unique. */
3649 err
= percpu_modalloc(mod
, info
);
3653 /* Now module is in final location, initialize linked lists, etc. */
3654 err
= module_unload_init(mod
);
3658 init_param_lock(mod
);
3660 /* Now we've got everything in the final locations, we can
3661 * find optional sections. */
3662 err
= find_module_sections(mod
, info
);
3666 err
= check_module_license_and_versions(mod
);
3670 /* Set up MODINFO_ATTR fields */
3671 setup_modinfo(mod
, info
);
3673 /* Fix up syms, so that st_value is a pointer to location. */
3674 err
= simplify_symbols(mod
, info
);
3678 err
= apply_relocations(mod
, info
);
3682 err
= post_relocation(mod
, info
);
3686 flush_module_icache(mod
);
3688 /* Now copy in args */
3689 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
3690 if (IS_ERR(mod
->args
)) {
3691 err
= PTR_ERR(mod
->args
);
3692 goto free_arch_cleanup
;
3695 dynamic_debug_setup(info
->debug
, info
->num_debug
);
3697 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
3698 ftrace_module_init(mod
);
3700 /* Finally it's fully formed, ready to start executing. */
3701 err
= complete_formation(mod
, info
);
3703 goto ddebug_cleanup
;
3705 err
= prepare_coming_module(mod
);
3709 /* Module is ready to execute: parsing args may do that. */
3710 after_dashes
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
,
3712 unknown_module_param_cb
);
3713 if (IS_ERR(after_dashes
)) {
3714 err
= PTR_ERR(after_dashes
);
3715 goto coming_cleanup
;
3716 } else if (after_dashes
) {
3717 pr_warn("%s: parameters '%s' after `--' ignored\n",
3718 mod
->name
, after_dashes
);
3721 /* Link in to sysfs. */
3722 err
= mod_sysfs_setup(mod
, info
, mod
->kp
, mod
->num_kp
);
3724 goto coming_cleanup
;
3726 if (is_livepatch_module(mod
)) {
3727 err
= copy_module_elf(mod
, info
);
3732 /* Get rid of temporary copy. */
3736 trace_module_load(mod
);
3738 return do_init_module(mod
);
3741 mod_sysfs_teardown(mod
);
3743 mod
->state
= MODULE_STATE_GOING
;
3744 destroy_params(mod
->kp
, mod
->num_kp
);
3745 blocking_notifier_call_chain(&module_notify_list
,
3746 MODULE_STATE_GOING
, mod
);
3747 klp_module_going(mod
);
3749 /* module_bug_cleanup needs module_mutex protection */
3750 mutex_lock(&module_mutex
);
3751 module_bug_cleanup(mod
);
3752 mutex_unlock(&module_mutex
);
3754 /* we can't deallocate the module until we clear memory protection */
3755 module_disable_ro(mod
);
3756 module_disable_nx(mod
);
3759 dynamic_debug_remove(info
->debug
);
3760 synchronize_sched();
3763 module_arch_cleanup(mod
);
3767 module_unload_free(mod
);
3769 mutex_lock(&module_mutex
);
3770 /* Unlink carefully: kallsyms could be walking list. */
3771 list_del_rcu(&mod
->list
);
3772 mod_tree_remove(mod
);
3773 wake_up_all(&module_wq
);
3774 /* Wait for RCU-sched synchronizing before releasing mod->list. */
3775 synchronize_sched();
3776 mutex_unlock(&module_mutex
);
3779 * Ftrace needs to clean up what it initialized.
3780 * This does nothing if ftrace_module_init() wasn't called,
3781 * but it must be called outside of module_mutex.
3783 ftrace_release_mod(mod
);
3784 /* Free lock-classes; relies on the preceding sync_rcu() */
3785 lockdep_free_key_range(mod
->core_layout
.base
, mod
->core_layout
.size
);
3787 module_deallocate(mod
, info
);
3793 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
3794 unsigned long, len
, const char __user
*, uargs
)
3797 struct load_info info
= { };
3799 err
= may_init_module();
3803 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3806 err
= copy_module_from_user(umod
, len
, &info
);
3810 return load_module(&info
, uargs
, 0);
3813 SYSCALL_DEFINE3(finit_module
, int, fd
, const char __user
*, uargs
, int, flags
)
3815 struct load_info info
= { };
3820 err
= may_init_module();
3824 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd
, uargs
, flags
);
3826 if (flags
& ~(MODULE_INIT_IGNORE_MODVERSIONS
3827 |MODULE_INIT_IGNORE_VERMAGIC
))
3830 err
= kernel_read_file_from_fd(fd
, &hdr
, &size
, INT_MAX
,
3837 return load_module(&info
, uargs
, flags
);
3840 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
3842 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
3845 #ifdef CONFIG_KALLSYMS
3847 * This ignores the intensely annoying "mapping symbols" found
3848 * in ARM ELF files: $a, $t and $d.
3850 static inline int is_arm_mapping_symbol(const char *str
)
3852 if (str
[0] == '.' && str
[1] == 'L')
3854 return str
[0] == '$' && strchr("axtd", str
[1])
3855 && (str
[2] == '\0' || str
[2] == '.');
3858 static const char *symname(struct mod_kallsyms
*kallsyms
, unsigned int symnum
)
3860 return kallsyms
->strtab
+ kallsyms
->symtab
[symnum
].st_name
;
3863 static const char *get_ksymbol(struct module
*mod
,
3865 unsigned long *size
,
3866 unsigned long *offset
)
3868 unsigned int i
, best
= 0;
3869 unsigned long nextval
;
3870 struct mod_kallsyms
*kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
3872 /* At worse, next value is at end of module */
3873 if (within_module_init(addr
, mod
))
3874 nextval
= (unsigned long)mod
->init_layout
.base
+mod
->init_layout
.text_size
;
3876 nextval
= (unsigned long)mod
->core_layout
.base
+mod
->core_layout
.text_size
;
3878 /* Scan for closest preceding symbol, and next symbol. (ELF
3879 starts real symbols at 1). */
3880 for (i
= 1; i
< kallsyms
->num_symtab
; i
++) {
3881 if (kallsyms
->symtab
[i
].st_shndx
== SHN_UNDEF
)
3884 /* We ignore unnamed symbols: they're uninformative
3885 * and inserted at a whim. */
3886 if (*symname(kallsyms
, i
) == '\0'
3887 || is_arm_mapping_symbol(symname(kallsyms
, i
)))
3890 if (kallsyms
->symtab
[i
].st_value
<= addr
3891 && kallsyms
->symtab
[i
].st_value
> kallsyms
->symtab
[best
].st_value
)
3893 if (kallsyms
->symtab
[i
].st_value
> addr
3894 && kallsyms
->symtab
[i
].st_value
< nextval
)
3895 nextval
= kallsyms
->symtab
[i
].st_value
;
3902 *size
= nextval
- kallsyms
->symtab
[best
].st_value
;
3904 *offset
= addr
- kallsyms
->symtab
[best
].st_value
;
3905 return symname(kallsyms
, best
);
3908 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3909 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3910 const char *module_address_lookup(unsigned long addr
,
3911 unsigned long *size
,
3912 unsigned long *offset
,
3916 const char *ret
= NULL
;
3920 mod
= __module_address(addr
);
3923 *modname
= mod
->name
;
3924 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3926 /* Make a copy in here where it's safe */
3928 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3936 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3941 list_for_each_entry_rcu(mod
, &modules
, list
) {
3942 if (mod
->state
== MODULE_STATE_UNFORMED
)
3944 if (within_module(addr
, mod
)) {
3947 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3950 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3960 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3961 unsigned long *offset
, char *modname
, char *name
)
3966 list_for_each_entry_rcu(mod
, &modules
, list
) {
3967 if (mod
->state
== MODULE_STATE_UNFORMED
)
3969 if (within_module(addr
, mod
)) {
3972 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3976 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3978 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3988 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3989 char *name
, char *module_name
, int *exported
)
3994 list_for_each_entry_rcu(mod
, &modules
, list
) {
3995 struct mod_kallsyms
*kallsyms
;
3997 if (mod
->state
== MODULE_STATE_UNFORMED
)
3999 kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
4000 if (symnum
< kallsyms
->num_symtab
) {
4001 *value
= kallsyms
->symtab
[symnum
].st_value
;
4002 *type
= kallsyms
->symtab
[symnum
].st_info
;
4003 strlcpy(name
, symname(kallsyms
, symnum
), KSYM_NAME_LEN
);
4004 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
4005 *exported
= is_exported(name
, *value
, mod
);
4009 symnum
-= kallsyms
->num_symtab
;
4015 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
4018 struct mod_kallsyms
*kallsyms
= rcu_dereference_sched(mod
->kallsyms
);
4020 for (i
= 0; i
< kallsyms
->num_symtab
; i
++)
4021 if (strcmp(name
, symname(kallsyms
, i
)) == 0 &&
4022 kallsyms
->symtab
[i
].st_info
!= 'U')
4023 return kallsyms
->symtab
[i
].st_value
;
4027 /* Look for this name: can be of form module:name. */
4028 unsigned long module_kallsyms_lookup_name(const char *name
)
4032 unsigned long ret
= 0;
4034 /* Don't lock: we're in enough trouble already. */
4036 if ((colon
= strchr(name
, ':')) != NULL
) {
4037 if ((mod
= find_module_all(name
, colon
- name
, false)) != NULL
)
4038 ret
= mod_find_symname(mod
, colon
+1);
4040 list_for_each_entry_rcu(mod
, &modules
, list
) {
4041 if (mod
->state
== MODULE_STATE_UNFORMED
)
4043 if ((ret
= mod_find_symname(mod
, name
)) != 0)
4051 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
4052 struct module
*, unsigned long),
4059 module_assert_mutex();
4061 list_for_each_entry(mod
, &modules
, list
) {
4062 /* We hold module_mutex: no need for rcu_dereference_sched */
4063 struct mod_kallsyms
*kallsyms
= mod
->kallsyms
;
4065 if (mod
->state
== MODULE_STATE_UNFORMED
)
4067 for (i
= 0; i
< kallsyms
->num_symtab
; i
++) {
4068 ret
= fn(data
, symname(kallsyms
, i
),
4069 mod
, kallsyms
->symtab
[i
].st_value
);
4076 #endif /* CONFIG_KALLSYMS */
4078 /* Maximum number of characters written by module_flags() */
4079 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4081 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
4082 static char *module_flags(struct module
*mod
, char *buf
)
4086 BUG_ON(mod
->state
== MODULE_STATE_UNFORMED
);
4088 mod
->state
== MODULE_STATE_GOING
||
4089 mod
->state
== MODULE_STATE_COMING
) {
4091 bx
+= module_flags_taint(mod
, buf
+ bx
);
4092 /* Show a - for module-is-being-unloaded */
4093 if (mod
->state
== MODULE_STATE_GOING
)
4095 /* Show a + for module-is-being-loaded */
4096 if (mod
->state
== MODULE_STATE_COMING
)
4105 #ifdef CONFIG_PROC_FS
4106 /* Called by the /proc file system to return a list of modules. */
4107 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
4109 mutex_lock(&module_mutex
);
4110 return seq_list_start(&modules
, *pos
);
4113 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
4115 return seq_list_next(p
, &modules
, pos
);
4118 static void m_stop(struct seq_file
*m
, void *p
)
4120 mutex_unlock(&module_mutex
);
4123 static int m_show(struct seq_file
*m
, void *p
)
4125 struct module
*mod
= list_entry(p
, struct module
, list
);
4126 char buf
[MODULE_FLAGS_BUF_SIZE
];
4128 /* We always ignore unformed modules. */
4129 if (mod
->state
== MODULE_STATE_UNFORMED
)
4132 seq_printf(m
, "%s %u",
4133 mod
->name
, mod
->init_layout
.size
+ mod
->core_layout
.size
);
4134 print_unload_info(m
, mod
);
4136 /* Informative for users. */
4137 seq_printf(m
, " %s",
4138 mod
->state
== MODULE_STATE_GOING
? "Unloading" :
4139 mod
->state
== MODULE_STATE_COMING
? "Loading" :
4141 /* Used by oprofile and other similar tools. */
4142 seq_printf(m
, " 0x%pK", mod
->core_layout
.base
);
4146 seq_printf(m
, " %s", module_flags(mod
, buf
));
4152 /* Format: modulename size refcount deps address
4154 Where refcount is a number or -, and deps is a comma-separated list
4157 static const struct seq_operations modules_op
= {
4164 static int modules_open(struct inode
*inode
, struct file
*file
)
4166 return seq_open(file
, &modules_op
);
4169 static const struct file_operations proc_modules_operations
= {
4170 .open
= modules_open
,
4172 .llseek
= seq_lseek
,
4173 .release
= seq_release
,
4176 static int __init
proc_modules_init(void)
4178 proc_create("modules", 0, NULL
, &proc_modules_operations
);
4181 module_init(proc_modules_init
);
4184 /* Given an address, look for it in the module exception tables. */
4185 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
4187 const struct exception_table_entry
*e
= NULL
;
4191 mod
= __module_address(addr
);
4195 if (!mod
->num_exentries
)
4198 e
= search_extable(mod
->extable
,
4199 mod
->extable
+ mod
->num_exentries
- 1,
4205 * Now, if we found one, we are running inside it now, hence
4206 * we cannot unload the module, hence no refcnt needed.
4212 * is_module_address - is this address inside a module?
4213 * @addr: the address to check.
4215 * See is_module_text_address() if you simply want to see if the address
4216 * is code (not data).
4218 bool is_module_address(unsigned long addr
)
4223 ret
= __module_address(addr
) != NULL
;
4230 * __module_address - get the module which contains an address.
4231 * @addr: the address.
4233 * Must be called with preempt disabled or module mutex held so that
4234 * module doesn't get freed during this.
4236 struct module
*__module_address(unsigned long addr
)
4240 if (addr
< module_addr_min
|| addr
> module_addr_max
)
4243 module_assert_mutex_or_preempt();
4245 mod
= mod_find(addr
);
4247 BUG_ON(!within_module(addr
, mod
));
4248 if (mod
->state
== MODULE_STATE_UNFORMED
)
4253 EXPORT_SYMBOL_GPL(__module_address
);
4256 * is_module_text_address - is this address inside module code?
4257 * @addr: the address to check.
4259 * See is_module_address() if you simply want to see if the address is
4260 * anywhere in a module. See kernel_text_address() for testing if an
4261 * address corresponds to kernel or module code.
4263 bool is_module_text_address(unsigned long addr
)
4268 ret
= __module_text_address(addr
) != NULL
;
4275 * __module_text_address - get the module whose code contains an address.
4276 * @addr: the address.
4278 * Must be called with preempt disabled or module mutex held so that
4279 * module doesn't get freed during this.
4281 struct module
*__module_text_address(unsigned long addr
)
4283 struct module
*mod
= __module_address(addr
);
4285 /* Make sure it's within the text section. */
4286 if (!within(addr
, mod
->init_layout
.base
, mod
->init_layout
.text_size
)
4287 && !within(addr
, mod
->core_layout
.base
, mod
->core_layout
.text_size
))
4292 EXPORT_SYMBOL_GPL(__module_text_address
);
4294 /* Don't grab lock, we're oopsing. */
4295 void print_modules(void)
4298 char buf
[MODULE_FLAGS_BUF_SIZE
];
4300 printk(KERN_DEFAULT
"Modules linked in:");
4301 /* Most callers should already have preempt disabled, but make sure */
4303 list_for_each_entry_rcu(mod
, &modules
, list
) {
4304 if (mod
->state
== MODULE_STATE_UNFORMED
)
4306 pr_cont(" %s%s", mod
->name
, module_flags(mod
, buf
));
4309 if (last_unloaded_module
[0])
4310 pr_cont(" [last unloaded: %s]", last_unloaded_module
);
4314 #ifdef CONFIG_MODVERSIONS
4315 /* Generate the signature for all relevant module structures here.
4316 * If these change, we don't want to try to parse the module. */
4317 void module_layout(struct module
*mod
,
4318 struct modversion_info
*ver
,
4319 struct kernel_param
*kp
,
4320 struct kernel_symbol
*ks
,
4321 struct tracepoint
* const *tp
)
4324 EXPORT_SYMBOL(module_layout
);