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/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
24 #include <linux/file.h>
26 #include <linux/sysfs.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/elf.h>
31 #include <linux/proc_fs.h>
32 #include <linux/security.h>
33 #include <linux/seq_file.h>
34 #include <linux/syscalls.h>
35 #include <linux/fcntl.h>
36 #include <linux/rcupdate.h>
37 #include <linux/capability.h>
38 #include <linux/cpu.h>
39 #include <linux/moduleparam.h>
40 #include <linux/errno.h>
41 #include <linux/err.h>
42 #include <linux/vermagic.h>
43 #include <linux/notifier.h>
44 #include <linux/sched.h>
45 #include <linux/stop_machine.h>
46 #include <linux/device.h>
47 #include <linux/string.h>
48 #include <linux/mutex.h>
49 #include <linux/rculist.h>
50 #include <asm/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/async.h>
58 #include <linux/percpu.h>
59 #include <linux/kmemleak.h>
60 #include <linux/jump_label.h>
61 #include <linux/pfn.h>
62 #include <linux/bsearch.h>
63 #include <linux/fips.h>
64 #include <uapi/linux/module.h>
65 #include "module-internal.h"
67 #define CREATE_TRACE_POINTS
68 #include <trace/events/module.h>
70 #ifndef ARCH_SHF_SMALL
71 #define ARCH_SHF_SMALL 0
75 * Modules' sections will be aligned on page boundaries
76 * to ensure complete separation of code and data, but
77 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
80 # define debug_align(X) ALIGN(X, PAGE_SIZE)
82 # define debug_align(X) (X)
86 * Given BASE and SIZE this macro calculates the number of pages the
87 * memory regions occupies
89 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
90 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
91 PFN_DOWN((unsigned long)BASE) + 1) \
94 /* If this is set, the section belongs in the init part of the module */
95 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
99 * 1) List of modules (also safely readable with preempt_disable),
100 * 2) module_use links,
101 * 3) module_addr_min/module_addr_max.
102 * (delete uses stop_machine/add uses RCU list operations). */
103 DEFINE_MUTEX(module_mutex
);
104 EXPORT_SYMBOL_GPL(module_mutex
);
105 static LIST_HEAD(modules
);
106 #ifdef CONFIG_KGDB_KDB
107 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
108 #endif /* CONFIG_KGDB_KDB */
110 #ifdef CONFIG_MODULE_SIG
111 #ifdef CONFIG_MODULE_SIG_FORCE
112 static bool sig_enforce
= true;
114 static bool sig_enforce
= false;
116 static int param_set_bool_enable_only(const char *val
,
117 const struct kernel_param
*kp
)
121 struct kernel_param dummy_kp
= *kp
;
123 dummy_kp
.arg
= &test
;
125 err
= param_set_bool(val
, &dummy_kp
);
129 /* Don't let them unset it once it's set! */
130 if (!test
&& sig_enforce
)
138 static const struct kernel_param_ops param_ops_bool_enable_only
= {
139 .set
= param_set_bool_enable_only
,
140 .get
= param_get_bool
,
142 #define param_check_bool_enable_only param_check_bool
144 module_param(sig_enforce
, bool_enable_only
, 0644);
145 #endif /* !CONFIG_MODULE_SIG_FORCE */
146 #endif /* CONFIG_MODULE_SIG */
148 /* Block module loading/unloading? */
149 int modules_disabled
= 0;
150 core_param(nomodule
, modules_disabled
, bint
, 0);
152 /* Waiting for a module to finish initializing? */
153 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
155 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
157 /* Bounds of module allocation, for speeding __module_address.
158 * Protected by module_mutex. */
159 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
161 int register_module_notifier(struct notifier_block
* nb
)
163 return blocking_notifier_chain_register(&module_notify_list
, nb
);
165 EXPORT_SYMBOL(register_module_notifier
);
167 int unregister_module_notifier(struct notifier_block
* nb
)
169 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
171 EXPORT_SYMBOL(unregister_module_notifier
);
177 char *secstrings
, *strtab
;
178 unsigned long symoffs
, stroffs
;
179 struct _ddebug
*debug
;
180 unsigned int num_debug
;
183 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
187 /* We require a truly strong try_module_get(): 0 means failure due to
188 ongoing or failed initialization etc. */
189 static inline int strong_try_module_get(struct module
*mod
)
191 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
193 if (try_module_get(mod
))
199 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
202 mod
->taints
|= (1U << flag
);
206 * A thread that wants to hold a reference to a module only while it
207 * is running can call this to safely exit. nfsd and lockd use this.
209 void __module_put_and_exit(struct module
*mod
, long code
)
214 EXPORT_SYMBOL(__module_put_and_exit
);
216 /* Find a module section: 0 means not found. */
217 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
221 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
222 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
223 /* Alloc bit cleared means "ignore it." */
224 if ((shdr
->sh_flags
& SHF_ALLOC
)
225 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
231 /* Find a module section, or NULL. */
232 static void *section_addr(const struct load_info
*info
, const char *name
)
234 /* Section 0 has sh_addr 0. */
235 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
238 /* Find a module section, or NULL. Fill in number of "objects" in section. */
239 static void *section_objs(const struct load_info
*info
,
244 unsigned int sec
= find_sec(info
, name
);
246 /* Section 0 has sh_addr 0 and sh_size 0. */
247 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
248 return (void *)info
->sechdrs
[sec
].sh_addr
;
251 /* Provided by the linker */
252 extern const struct kernel_symbol __start___ksymtab
[];
253 extern const struct kernel_symbol __stop___ksymtab
[];
254 extern const struct kernel_symbol __start___ksymtab_gpl
[];
255 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
256 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
257 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
258 extern const unsigned long __start___kcrctab
[];
259 extern const unsigned long __start___kcrctab_gpl
[];
260 extern const unsigned long __start___kcrctab_gpl_future
[];
261 #ifdef CONFIG_UNUSED_SYMBOLS
262 extern const struct kernel_symbol __start___ksymtab_unused
[];
263 extern const struct kernel_symbol __stop___ksymtab_unused
[];
264 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
265 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
266 extern const unsigned long __start___kcrctab_unused
[];
267 extern const unsigned long __start___kcrctab_unused_gpl
[];
270 #ifndef CONFIG_MODVERSIONS
271 #define symversion(base, idx) NULL
273 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
276 static bool each_symbol_in_section(const struct symsearch
*arr
,
277 unsigned int arrsize
,
278 struct module
*owner
,
279 bool (*fn
)(const struct symsearch
*syms
,
280 struct module
*owner
,
286 for (j
= 0; j
< arrsize
; j
++) {
287 if (fn(&arr
[j
], owner
, data
))
294 /* Returns true as soon as fn returns true, otherwise false. */
295 bool each_symbol_section(bool (*fn
)(const struct symsearch
*arr
,
296 struct module
*owner
,
301 static const struct symsearch arr
[] = {
302 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
303 NOT_GPL_ONLY
, false },
304 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
305 __start___kcrctab_gpl
,
307 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
308 __start___kcrctab_gpl_future
,
309 WILL_BE_GPL_ONLY
, false },
310 #ifdef CONFIG_UNUSED_SYMBOLS
311 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
312 __start___kcrctab_unused
,
313 NOT_GPL_ONLY
, true },
314 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
315 __start___kcrctab_unused_gpl
,
320 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
323 list_for_each_entry_rcu(mod
, &modules
, list
) {
324 struct symsearch arr
[] = {
325 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
326 NOT_GPL_ONLY
, false },
327 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
330 { mod
->gpl_future_syms
,
331 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
332 mod
->gpl_future_crcs
,
333 WILL_BE_GPL_ONLY
, false },
334 #ifdef CONFIG_UNUSED_SYMBOLS
336 mod
->unused_syms
+ mod
->num_unused_syms
,
338 NOT_GPL_ONLY
, true },
339 { mod
->unused_gpl_syms
,
340 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
341 mod
->unused_gpl_crcs
,
346 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
351 EXPORT_SYMBOL_GPL(each_symbol_section
);
353 struct find_symbol_arg
{
360 struct module
*owner
;
361 const unsigned long *crc
;
362 const struct kernel_symbol
*sym
;
365 static bool check_symbol(const struct symsearch
*syms
,
366 struct module
*owner
,
367 unsigned int symnum
, void *data
)
369 struct find_symbol_arg
*fsa
= data
;
372 if (syms
->licence
== GPL_ONLY
)
374 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
375 printk(KERN_WARNING
"Symbol %s is being used "
376 "by a non-GPL module, which will not "
377 "be allowed in the future\n", fsa
->name
);
378 printk(KERN_WARNING
"Please see the file "
379 "Documentation/feature-removal-schedule.txt "
380 "in the kernel source tree for more details.\n");
384 #ifdef CONFIG_UNUSED_SYMBOLS
385 if (syms
->unused
&& fsa
->warn
) {
386 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
387 "however this module is using it.\n", fsa
->name
);
389 "This symbol will go away in the future.\n");
391 "Please evalute if this is the right api to use and if "
392 "it really is, submit a report the linux kernel "
393 "mailinglist together with submitting your code for "
399 fsa
->crc
= symversion(syms
->crcs
, symnum
);
400 fsa
->sym
= &syms
->start
[symnum
];
404 static int cmp_name(const void *va
, const void *vb
)
407 const struct kernel_symbol
*b
;
409 return strcmp(a
, b
->name
);
412 static bool find_symbol_in_section(const struct symsearch
*syms
,
413 struct module
*owner
,
416 struct find_symbol_arg
*fsa
= data
;
417 struct kernel_symbol
*sym
;
419 sym
= bsearch(fsa
->name
, syms
->start
, syms
->stop
- syms
->start
,
420 sizeof(struct kernel_symbol
), cmp_name
);
422 if (sym
!= NULL
&& check_symbol(syms
, owner
, sym
- syms
->start
, data
))
428 /* Find a symbol and return it, along with, (optional) crc and
429 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
430 const struct kernel_symbol
*find_symbol(const char *name
,
431 struct module
**owner
,
432 const unsigned long **crc
,
436 struct find_symbol_arg fsa
;
442 if (each_symbol_section(find_symbol_in_section
, &fsa
)) {
450 pr_debug("Failed to find symbol %s\n", name
);
453 EXPORT_SYMBOL_GPL(find_symbol
);
455 /* Search for module by name: must hold module_mutex. */
456 struct module
*find_module(const char *name
)
460 list_for_each_entry(mod
, &modules
, list
) {
461 if (strcmp(mod
->name
, name
) == 0)
466 EXPORT_SYMBOL_GPL(find_module
);
470 static inline void __percpu
*mod_percpu(struct module
*mod
)
475 static int percpu_modalloc(struct module
*mod
,
476 unsigned long size
, unsigned long align
)
478 if (align
> PAGE_SIZE
) {
479 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
480 mod
->name
, align
, PAGE_SIZE
);
484 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
487 "%s: Could not allocate %lu bytes percpu data\n",
491 mod
->percpu_size
= size
;
495 static void percpu_modfree(struct module
*mod
)
497 free_percpu(mod
->percpu
);
500 static unsigned int find_pcpusec(struct load_info
*info
)
502 return find_sec(info
, ".data..percpu");
505 static void percpu_modcopy(struct module
*mod
,
506 const void *from
, unsigned long size
)
510 for_each_possible_cpu(cpu
)
511 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
515 * is_module_percpu_address - test whether address is from module static percpu
516 * @addr: address to test
518 * Test whether @addr belongs to module static percpu area.
521 * %true if @addr is from module static percpu area
523 bool is_module_percpu_address(unsigned long addr
)
530 list_for_each_entry_rcu(mod
, &modules
, list
) {
531 if (!mod
->percpu_size
)
533 for_each_possible_cpu(cpu
) {
534 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
536 if ((void *)addr
>= start
&&
537 (void *)addr
< start
+ mod
->percpu_size
) {
548 #else /* ... !CONFIG_SMP */
550 static inline void __percpu
*mod_percpu(struct module
*mod
)
554 static inline int percpu_modalloc(struct module
*mod
,
555 unsigned long size
, unsigned long align
)
559 static inline void percpu_modfree(struct module
*mod
)
562 static unsigned int find_pcpusec(struct load_info
*info
)
566 static inline void percpu_modcopy(struct module
*mod
,
567 const void *from
, unsigned long size
)
569 /* pcpusec should be 0, and size of that section should be 0. */
572 bool is_module_percpu_address(unsigned long addr
)
577 #endif /* CONFIG_SMP */
579 #define MODINFO_ATTR(field) \
580 static void setup_modinfo_##field(struct module *mod, const char *s) \
582 mod->field = kstrdup(s, GFP_KERNEL); \
584 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
585 struct module_kobject *mk, char *buffer) \
587 return sprintf(buffer, "%s\n", mk->mod->field); \
589 static int modinfo_##field##_exists(struct module *mod) \
591 return mod->field != NULL; \
593 static void free_modinfo_##field(struct module *mod) \
598 static struct module_attribute modinfo_##field = { \
599 .attr = { .name = __stringify(field), .mode = 0444 }, \
600 .show = show_modinfo_##field, \
601 .setup = setup_modinfo_##field, \
602 .test = modinfo_##field##_exists, \
603 .free = free_modinfo_##field, \
606 MODINFO_ATTR(version
);
607 MODINFO_ATTR(srcversion
);
609 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
611 #ifdef CONFIG_MODULE_UNLOAD
613 EXPORT_TRACEPOINT_SYMBOL(module_get
);
615 /* Init the unload section of the module. */
616 static int module_unload_init(struct module
*mod
)
618 mod
->refptr
= alloc_percpu(struct module_ref
);
622 INIT_LIST_HEAD(&mod
->source_list
);
623 INIT_LIST_HEAD(&mod
->target_list
);
625 /* Hold reference count during initialization. */
626 __this_cpu_write(mod
->refptr
->incs
, 1);
627 /* Backwards compatibility macros put refcount during init. */
628 mod
->waiter
= current
;
633 /* Does a already use b? */
634 static int already_uses(struct module
*a
, struct module
*b
)
636 struct module_use
*use
;
638 list_for_each_entry(use
, &b
->source_list
, source_list
) {
639 if (use
->source
== a
) {
640 pr_debug("%s uses %s!\n", a
->name
, b
->name
);
644 pr_debug("%s does not use %s!\n", a
->name
, b
->name
);
650 * - we add 'a' as a "source", 'b' as a "target" of module use
651 * - the module_use is added to the list of 'b' sources (so
652 * 'b' can walk the list to see who sourced them), and of 'a'
653 * targets (so 'a' can see what modules it targets).
655 static int add_module_usage(struct module
*a
, struct module
*b
)
657 struct module_use
*use
;
659 pr_debug("Allocating new usage for %s.\n", a
->name
);
660 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
662 printk(KERN_WARNING
"%s: out of memory loading\n", a
->name
);
668 list_add(&use
->source_list
, &b
->source_list
);
669 list_add(&use
->target_list
, &a
->target_list
);
673 /* Module a uses b: caller needs module_mutex() */
674 int ref_module(struct module
*a
, struct module
*b
)
678 if (b
== NULL
|| already_uses(a
, b
))
681 /* If module isn't available, we fail. */
682 err
= strong_try_module_get(b
);
686 err
= add_module_usage(a
, b
);
693 EXPORT_SYMBOL_GPL(ref_module
);
695 /* Clear the unload stuff of the module. */
696 static void module_unload_free(struct module
*mod
)
698 struct module_use
*use
, *tmp
;
700 mutex_lock(&module_mutex
);
701 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
702 struct module
*i
= use
->target
;
703 pr_debug("%s unusing %s\n", mod
->name
, i
->name
);
705 list_del(&use
->source_list
);
706 list_del(&use
->target_list
);
709 mutex_unlock(&module_mutex
);
711 free_percpu(mod
->refptr
);
714 #ifdef CONFIG_MODULE_FORCE_UNLOAD
715 static inline int try_force_unload(unsigned int flags
)
717 int ret
= (flags
& O_TRUNC
);
719 add_taint(TAINT_FORCED_RMMOD
);
723 static inline int try_force_unload(unsigned int flags
)
727 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
736 /* Whole machine is stopped with interrupts off when this runs. */
737 static int __try_stop_module(void *_sref
)
739 struct stopref
*sref
= _sref
;
741 /* If it's not unused, quit unless we're forcing. */
742 if (module_refcount(sref
->mod
) != 0) {
743 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
747 /* Mark it as dying. */
748 sref
->mod
->state
= MODULE_STATE_GOING
;
752 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
754 if (flags
& O_NONBLOCK
) {
755 struct stopref sref
= { mod
, flags
, forced
};
757 return stop_machine(__try_stop_module
, &sref
, NULL
);
759 /* We don't need to stop the machine for this. */
760 mod
->state
= MODULE_STATE_GOING
;
766 unsigned long module_refcount(struct module
*mod
)
768 unsigned long incs
= 0, decs
= 0;
771 for_each_possible_cpu(cpu
)
772 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
774 * ensure the incs are added up after the decs.
775 * module_put ensures incs are visible before decs with smp_wmb.
777 * This 2-count scheme avoids the situation where the refcount
778 * for CPU0 is read, then CPU0 increments the module refcount,
779 * then CPU1 drops that refcount, then the refcount for CPU1 is
780 * read. We would record a decrement but not its corresponding
781 * increment so we would see a low count (disaster).
783 * Rare situation? But module_refcount can be preempted, and we
784 * might be tallying up 4096+ CPUs. So it is not impossible.
787 for_each_possible_cpu(cpu
)
788 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
791 EXPORT_SYMBOL(module_refcount
);
793 /* This exists whether we can unload or not */
794 static void free_module(struct module
*mod
);
796 static void wait_for_zero_refcount(struct module
*mod
)
798 /* Since we might sleep for some time, release the mutex first */
799 mutex_unlock(&module_mutex
);
801 pr_debug("Looking at refcount...\n");
802 set_current_state(TASK_UNINTERRUPTIBLE
);
803 if (module_refcount(mod
) == 0)
807 current
->state
= TASK_RUNNING
;
808 mutex_lock(&module_mutex
);
811 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
815 char name
[MODULE_NAME_LEN
];
818 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
821 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
823 name
[MODULE_NAME_LEN
-1] = '\0';
825 if (mutex_lock_interruptible(&module_mutex
) != 0)
828 mod
= find_module(name
);
834 if (!list_empty(&mod
->source_list
)) {
835 /* Other modules depend on us: get rid of them first. */
840 /* Doing init or already dying? */
841 if (mod
->state
!= MODULE_STATE_LIVE
) {
842 /* FIXME: if (force), slam module count and wake up
844 pr_debug("%s already dying\n", mod
->name
);
849 /* If it has an init func, it must have an exit func to unload */
850 if (mod
->init
&& !mod
->exit
) {
851 forced
= try_force_unload(flags
);
853 /* This module can't be removed */
859 /* Set this up before setting mod->state */
860 mod
->waiter
= current
;
862 /* Stop the machine so refcounts can't move and disable module. */
863 ret
= try_stop_module(mod
, flags
, &forced
);
867 /* Never wait if forced. */
868 if (!forced
&& module_refcount(mod
) != 0)
869 wait_for_zero_refcount(mod
);
871 mutex_unlock(&module_mutex
);
872 /* Final destruction now no one is using it. */
873 if (mod
->exit
!= NULL
)
875 blocking_notifier_call_chain(&module_notify_list
,
876 MODULE_STATE_GOING
, mod
);
877 async_synchronize_full();
879 /* Store the name of the last unloaded module for diagnostic purposes */
880 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
885 mutex_unlock(&module_mutex
);
889 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
891 struct module_use
*use
;
892 int printed_something
= 0;
894 seq_printf(m
, " %lu ", module_refcount(mod
));
896 /* Always include a trailing , so userspace can differentiate
897 between this and the old multi-field proc format. */
898 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
899 printed_something
= 1;
900 seq_printf(m
, "%s,", use
->source
->name
);
903 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
904 printed_something
= 1;
905 seq_printf(m
, "[permanent],");
908 if (!printed_something
)
912 void __symbol_put(const char *symbol
)
914 struct module
*owner
;
917 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
922 EXPORT_SYMBOL(__symbol_put
);
924 /* Note this assumes addr is a function, which it currently always is. */
925 void symbol_put_addr(void *addr
)
927 struct module
*modaddr
;
928 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
930 if (core_kernel_text(a
))
933 /* module_text_address is safe here: we're supposed to have reference
934 * to module from symbol_get, so it can't go away. */
935 modaddr
= __module_text_address(a
);
939 EXPORT_SYMBOL_GPL(symbol_put_addr
);
941 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
942 struct module_kobject
*mk
, char *buffer
)
944 return sprintf(buffer
, "%lu\n", module_refcount(mk
->mod
));
947 static struct module_attribute modinfo_refcnt
=
948 __ATTR(refcnt
, 0444, show_refcnt
, NULL
);
950 void __module_get(struct module
*module
)
954 __this_cpu_inc(module
->refptr
->incs
);
955 trace_module_get(module
, _RET_IP_
);
959 EXPORT_SYMBOL(__module_get
);
961 bool try_module_get(struct module
*module
)
968 if (likely(module_is_live(module
))) {
969 __this_cpu_inc(module
->refptr
->incs
);
970 trace_module_get(module
, _RET_IP_
);
978 EXPORT_SYMBOL(try_module_get
);
980 void module_put(struct module
*module
)
984 smp_wmb(); /* see comment in module_refcount */
985 __this_cpu_inc(module
->refptr
->decs
);
987 trace_module_put(module
, _RET_IP_
);
988 /* Maybe they're waiting for us to drop reference? */
989 if (unlikely(!module_is_live(module
)))
990 wake_up_process(module
->waiter
);
994 EXPORT_SYMBOL(module_put
);
996 #else /* !CONFIG_MODULE_UNLOAD */
997 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
999 /* We don't know the usage count, or what modules are using. */
1000 seq_printf(m
, " - -");
1003 static inline void module_unload_free(struct module
*mod
)
1007 int ref_module(struct module
*a
, struct module
*b
)
1009 return strong_try_module_get(b
);
1011 EXPORT_SYMBOL_GPL(ref_module
);
1013 static inline int module_unload_init(struct module
*mod
)
1017 #endif /* CONFIG_MODULE_UNLOAD */
1019 static size_t module_flags_taint(struct module
*mod
, char *buf
)
1023 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
1025 if (mod
->taints
& (1 << TAINT_OOT_MODULE
))
1027 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
1029 if (mod
->taints
& (1 << TAINT_CRAP
))
1032 * TAINT_FORCED_RMMOD: could be added.
1033 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1039 static ssize_t
show_initstate(struct module_attribute
*mattr
,
1040 struct module_kobject
*mk
, char *buffer
)
1042 const char *state
= "unknown";
1044 switch (mk
->mod
->state
) {
1045 case MODULE_STATE_LIVE
:
1048 case MODULE_STATE_COMING
:
1051 case MODULE_STATE_GOING
:
1055 return sprintf(buffer
, "%s\n", state
);
1058 static struct module_attribute modinfo_initstate
=
1059 __ATTR(initstate
, 0444, show_initstate
, NULL
);
1061 static ssize_t
store_uevent(struct module_attribute
*mattr
,
1062 struct module_kobject
*mk
,
1063 const char *buffer
, size_t count
)
1065 enum kobject_action action
;
1067 if (kobject_action_type(buffer
, count
, &action
) == 0)
1068 kobject_uevent(&mk
->kobj
, action
);
1072 struct module_attribute module_uevent
=
1073 __ATTR(uevent
, 0200, NULL
, store_uevent
);
1075 static ssize_t
show_coresize(struct module_attribute
*mattr
,
1076 struct module_kobject
*mk
, char *buffer
)
1078 return sprintf(buffer
, "%u\n", mk
->mod
->core_size
);
1081 static struct module_attribute modinfo_coresize
=
1082 __ATTR(coresize
, 0444, show_coresize
, NULL
);
1084 static ssize_t
show_initsize(struct module_attribute
*mattr
,
1085 struct module_kobject
*mk
, char *buffer
)
1087 return sprintf(buffer
, "%u\n", mk
->mod
->init_size
);
1090 static struct module_attribute modinfo_initsize
=
1091 __ATTR(initsize
, 0444, show_initsize
, NULL
);
1093 static ssize_t
show_taint(struct module_attribute
*mattr
,
1094 struct module_kobject
*mk
, char *buffer
)
1098 l
= module_flags_taint(mk
->mod
, buffer
);
1103 static struct module_attribute modinfo_taint
=
1104 __ATTR(taint
, 0444, show_taint
, NULL
);
1106 static struct module_attribute
*modinfo_attrs
[] = {
1109 &modinfo_srcversion
,
1114 #ifdef CONFIG_MODULE_UNLOAD
1120 static const char vermagic
[] = VERMAGIC_STRING
;
1122 static int try_to_force_load(struct module
*mod
, const char *reason
)
1124 #ifdef CONFIG_MODULE_FORCE_LOAD
1125 if (!test_taint(TAINT_FORCED_MODULE
))
1126 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
1128 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1135 #ifdef CONFIG_MODVERSIONS
1136 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1137 static unsigned long maybe_relocated(unsigned long crc
,
1138 const struct module
*crc_owner
)
1140 #ifdef ARCH_RELOCATES_KCRCTAB
1141 if (crc_owner
== NULL
)
1142 return crc
- (unsigned long)reloc_start
;
1147 static int check_version(Elf_Shdr
*sechdrs
,
1148 unsigned int versindex
,
1149 const char *symname
,
1151 const unsigned long *crc
,
1152 const struct module
*crc_owner
)
1154 unsigned int i
, num_versions
;
1155 struct modversion_info
*versions
;
1157 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1161 /* No versions at all? modprobe --force does this. */
1163 return try_to_force_load(mod
, symname
) == 0;
1165 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1166 num_versions
= sechdrs
[versindex
].sh_size
1167 / sizeof(struct modversion_info
);
1169 for (i
= 0; i
< num_versions
; i
++) {
1170 if (strcmp(versions
[i
].name
, symname
) != 0)
1173 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
1175 pr_debug("Found checksum %lX vs module %lX\n",
1176 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
1180 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1181 mod
->name
, symname
);
1185 printk("%s: disagrees about version of symbol %s\n",
1186 mod
->name
, symname
);
1190 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1191 unsigned int versindex
,
1194 const unsigned long *crc
;
1196 /* Since this should be found in kernel (which can't be removed),
1197 * no locking is necessary. */
1198 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1201 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1205 /* First part is kernel version, which we ignore if module has crcs. */
1206 static inline int same_magic(const char *amagic
, const char *bmagic
,
1210 amagic
+= strcspn(amagic
, " ");
1211 bmagic
+= strcspn(bmagic
, " ");
1213 return strcmp(amagic
, bmagic
) == 0;
1216 static inline int check_version(Elf_Shdr
*sechdrs
,
1217 unsigned int versindex
,
1218 const char *symname
,
1220 const unsigned long *crc
,
1221 const struct module
*crc_owner
)
1226 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1227 unsigned int versindex
,
1233 static inline int same_magic(const char *amagic
, const char *bmagic
,
1236 return strcmp(amagic
, bmagic
) == 0;
1238 #endif /* CONFIG_MODVERSIONS */
1240 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1241 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1242 const struct load_info
*info
,
1246 struct module
*owner
;
1247 const struct kernel_symbol
*sym
;
1248 const unsigned long *crc
;
1251 mutex_lock(&module_mutex
);
1252 sym
= find_symbol(name
, &owner
, &crc
,
1253 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1257 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
,
1259 sym
= ERR_PTR(-EINVAL
);
1263 err
= ref_module(mod
, owner
);
1270 /* We must make copy under the lock if we failed to get ref. */
1271 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1273 mutex_unlock(&module_mutex
);
1277 static const struct kernel_symbol
*
1278 resolve_symbol_wait(struct module
*mod
,
1279 const struct load_info
*info
,
1282 const struct kernel_symbol
*ksym
;
1283 char owner
[MODULE_NAME_LEN
];
1285 if (wait_event_interruptible_timeout(module_wq
,
1286 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1287 || PTR_ERR(ksym
) != -EBUSY
,
1289 printk(KERN_WARNING
"%s: gave up waiting for init of module %s.\n",
1296 * /sys/module/foo/sections stuff
1297 * J. Corbet <corbet@lwn.net>
1301 #ifdef CONFIG_KALLSYMS
1302 static inline bool sect_empty(const Elf_Shdr
*sect
)
1304 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1307 struct module_sect_attr
1309 struct module_attribute mattr
;
1311 unsigned long address
;
1314 struct module_sect_attrs
1316 struct attribute_group grp
;
1317 unsigned int nsections
;
1318 struct module_sect_attr attrs
[0];
1321 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1322 struct module_kobject
*mk
, char *buf
)
1324 struct module_sect_attr
*sattr
=
1325 container_of(mattr
, struct module_sect_attr
, mattr
);
1326 return sprintf(buf
, "0x%pK\n", (void *)sattr
->address
);
1329 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1331 unsigned int section
;
1333 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1334 kfree(sect_attrs
->attrs
[section
].name
);
1338 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1340 unsigned int nloaded
= 0, i
, size
[2];
1341 struct module_sect_attrs
*sect_attrs
;
1342 struct module_sect_attr
*sattr
;
1343 struct attribute
**gattr
;
1345 /* Count loaded sections and allocate structures */
1346 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1347 if (!sect_empty(&info
->sechdrs
[i
]))
1349 size
[0] = ALIGN(sizeof(*sect_attrs
)
1350 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1351 sizeof(sect_attrs
->grp
.attrs
[0]));
1352 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1353 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1354 if (sect_attrs
== NULL
)
1357 /* Setup section attributes. */
1358 sect_attrs
->grp
.name
= "sections";
1359 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1361 sect_attrs
->nsections
= 0;
1362 sattr
= §_attrs
->attrs
[0];
1363 gattr
= §_attrs
->grp
.attrs
[0];
1364 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1365 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1366 if (sect_empty(sec
))
1368 sattr
->address
= sec
->sh_addr
;
1369 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1371 if (sattr
->name
== NULL
)
1373 sect_attrs
->nsections
++;
1374 sysfs_attr_init(&sattr
->mattr
.attr
);
1375 sattr
->mattr
.show
= module_sect_show
;
1376 sattr
->mattr
.store
= NULL
;
1377 sattr
->mattr
.attr
.name
= sattr
->name
;
1378 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1379 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1383 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1386 mod
->sect_attrs
= sect_attrs
;
1389 free_sect_attrs(sect_attrs
);
1392 static void remove_sect_attrs(struct module
*mod
)
1394 if (mod
->sect_attrs
) {
1395 sysfs_remove_group(&mod
->mkobj
.kobj
,
1396 &mod
->sect_attrs
->grp
);
1397 /* We are positive that no one is using any sect attrs
1398 * at this point. Deallocate immediately. */
1399 free_sect_attrs(mod
->sect_attrs
);
1400 mod
->sect_attrs
= NULL
;
1405 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1408 struct module_notes_attrs
{
1409 struct kobject
*dir
;
1411 struct bin_attribute attrs
[0];
1414 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1415 struct bin_attribute
*bin_attr
,
1416 char *buf
, loff_t pos
, size_t count
)
1419 * The caller checked the pos and count against our size.
1421 memcpy(buf
, bin_attr
->private + pos
, count
);
1425 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1428 if (notes_attrs
->dir
) {
1430 sysfs_remove_bin_file(notes_attrs
->dir
,
1431 ¬es_attrs
->attrs
[i
]);
1432 kobject_put(notes_attrs
->dir
);
1437 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1439 unsigned int notes
, loaded
, i
;
1440 struct module_notes_attrs
*notes_attrs
;
1441 struct bin_attribute
*nattr
;
1443 /* failed to create section attributes, so can't create notes */
1444 if (!mod
->sect_attrs
)
1447 /* Count notes sections and allocate structures. */
1449 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1450 if (!sect_empty(&info
->sechdrs
[i
]) &&
1451 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1457 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1458 + notes
* sizeof(notes_attrs
->attrs
[0]),
1460 if (notes_attrs
== NULL
)
1463 notes_attrs
->notes
= notes
;
1464 nattr
= ¬es_attrs
->attrs
[0];
1465 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1466 if (sect_empty(&info
->sechdrs
[i
]))
1468 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1469 sysfs_bin_attr_init(nattr
);
1470 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1471 nattr
->attr
.mode
= S_IRUGO
;
1472 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1473 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1474 nattr
->read
= module_notes_read
;
1480 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1481 if (!notes_attrs
->dir
)
1484 for (i
= 0; i
< notes
; ++i
)
1485 if (sysfs_create_bin_file(notes_attrs
->dir
,
1486 ¬es_attrs
->attrs
[i
]))
1489 mod
->notes_attrs
= notes_attrs
;
1493 free_notes_attrs(notes_attrs
, i
);
1496 static void remove_notes_attrs(struct module
*mod
)
1498 if (mod
->notes_attrs
)
1499 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1504 static inline void add_sect_attrs(struct module
*mod
,
1505 const struct load_info
*info
)
1509 static inline void remove_sect_attrs(struct module
*mod
)
1513 static inline void add_notes_attrs(struct module
*mod
,
1514 const struct load_info
*info
)
1518 static inline void remove_notes_attrs(struct module
*mod
)
1521 #endif /* CONFIG_KALLSYMS */
1523 static void add_usage_links(struct module
*mod
)
1525 #ifdef CONFIG_MODULE_UNLOAD
1526 struct module_use
*use
;
1529 mutex_lock(&module_mutex
);
1530 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1531 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1532 &mod
->mkobj
.kobj
, mod
->name
);
1534 mutex_unlock(&module_mutex
);
1538 static void del_usage_links(struct module
*mod
)
1540 #ifdef CONFIG_MODULE_UNLOAD
1541 struct module_use
*use
;
1543 mutex_lock(&module_mutex
);
1544 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1545 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1546 mutex_unlock(&module_mutex
);
1550 static int module_add_modinfo_attrs(struct module
*mod
)
1552 struct module_attribute
*attr
;
1553 struct module_attribute
*temp_attr
;
1557 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1558 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1560 if (!mod
->modinfo_attrs
)
1563 temp_attr
= mod
->modinfo_attrs
;
1564 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1566 (attr
->test
&& attr
->test(mod
))) {
1567 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1568 sysfs_attr_init(&temp_attr
->attr
);
1569 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1576 static void module_remove_modinfo_attrs(struct module
*mod
)
1578 struct module_attribute
*attr
;
1581 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1582 /* pick a field to test for end of list */
1583 if (!attr
->attr
.name
)
1585 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1589 kfree(mod
->modinfo_attrs
);
1592 static int mod_sysfs_init(struct module
*mod
)
1595 struct kobject
*kobj
;
1597 if (!module_sysfs_initialized
) {
1598 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1604 kobj
= kset_find_obj(module_kset
, mod
->name
);
1606 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1612 mod
->mkobj
.mod
= mod
;
1614 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1615 mod
->mkobj
.kobj
.kset
= module_kset
;
1616 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1619 kobject_put(&mod
->mkobj
.kobj
);
1621 /* delay uevent until full sysfs population */
1626 static int mod_sysfs_setup(struct module
*mod
,
1627 const struct load_info
*info
,
1628 struct kernel_param
*kparam
,
1629 unsigned int num_params
)
1633 err
= mod_sysfs_init(mod
);
1637 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1638 if (!mod
->holders_dir
) {
1643 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1645 goto out_unreg_holders
;
1647 err
= module_add_modinfo_attrs(mod
);
1649 goto out_unreg_param
;
1651 add_usage_links(mod
);
1652 add_sect_attrs(mod
, info
);
1653 add_notes_attrs(mod
, info
);
1655 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1659 module_param_sysfs_remove(mod
);
1661 kobject_put(mod
->holders_dir
);
1663 kobject_put(&mod
->mkobj
.kobj
);
1668 static void mod_sysfs_fini(struct module
*mod
)
1670 remove_notes_attrs(mod
);
1671 remove_sect_attrs(mod
);
1672 kobject_put(&mod
->mkobj
.kobj
);
1675 #else /* !CONFIG_SYSFS */
1677 static int mod_sysfs_setup(struct module
*mod
,
1678 const struct load_info
*info
,
1679 struct kernel_param
*kparam
,
1680 unsigned int num_params
)
1685 static void mod_sysfs_fini(struct module
*mod
)
1689 static void module_remove_modinfo_attrs(struct module
*mod
)
1693 static void del_usage_links(struct module
*mod
)
1697 #endif /* CONFIG_SYSFS */
1699 static void mod_sysfs_teardown(struct module
*mod
)
1701 del_usage_links(mod
);
1702 module_remove_modinfo_attrs(mod
);
1703 module_param_sysfs_remove(mod
);
1704 kobject_put(mod
->mkobj
.drivers_dir
);
1705 kobject_put(mod
->holders_dir
);
1706 mod_sysfs_fini(mod
);
1710 * unlink the module with the whole machine is stopped with interrupts off
1711 * - this defends against kallsyms not taking locks
1713 static int __unlink_module(void *_mod
)
1715 struct module
*mod
= _mod
;
1716 list_del(&mod
->list
);
1717 module_bug_cleanup(mod
);
1721 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1723 * LKM RO/NX protection: protect module's text/ro-data
1724 * from modification and any data from execution.
1726 void set_page_attributes(void *start
, void *end
, int (*set
)(unsigned long start
, int num_pages
))
1728 unsigned long begin_pfn
= PFN_DOWN((unsigned long)start
);
1729 unsigned long end_pfn
= PFN_DOWN((unsigned long)end
);
1731 if (end_pfn
> begin_pfn
)
1732 set(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1735 static void set_section_ro_nx(void *base
,
1736 unsigned long text_size
,
1737 unsigned long ro_size
,
1738 unsigned long total_size
)
1740 /* begin and end PFNs of the current subsection */
1741 unsigned long begin_pfn
;
1742 unsigned long end_pfn
;
1745 * Set RO for module text and RO-data:
1746 * - Always protect first page.
1747 * - Do not protect last partial page.
1750 set_page_attributes(base
, base
+ ro_size
, set_memory_ro
);
1753 * Set NX permissions for module data:
1754 * - Do not protect first partial page.
1755 * - Always protect last page.
1757 if (total_size
> text_size
) {
1758 begin_pfn
= PFN_UP((unsigned long)base
+ text_size
);
1759 end_pfn
= PFN_UP((unsigned long)base
+ total_size
);
1760 if (end_pfn
> begin_pfn
)
1761 set_memory_nx(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1765 static void unset_module_core_ro_nx(struct module
*mod
)
1767 set_page_attributes(mod
->module_core
+ mod
->core_text_size
,
1768 mod
->module_core
+ mod
->core_size
,
1770 set_page_attributes(mod
->module_core
,
1771 mod
->module_core
+ mod
->core_ro_size
,
1775 static void unset_module_init_ro_nx(struct module
*mod
)
1777 set_page_attributes(mod
->module_init
+ mod
->init_text_size
,
1778 mod
->module_init
+ mod
->init_size
,
1780 set_page_attributes(mod
->module_init
,
1781 mod
->module_init
+ mod
->init_ro_size
,
1785 /* Iterate through all modules and set each module's text as RW */
1786 void set_all_modules_text_rw(void)
1790 mutex_lock(&module_mutex
);
1791 list_for_each_entry_rcu(mod
, &modules
, list
) {
1792 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1793 set_page_attributes(mod
->module_core
,
1794 mod
->module_core
+ mod
->core_text_size
,
1797 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1798 set_page_attributes(mod
->module_init
,
1799 mod
->module_init
+ mod
->init_text_size
,
1803 mutex_unlock(&module_mutex
);
1806 /* Iterate through all modules and set each module's text as RO */
1807 void set_all_modules_text_ro(void)
1811 mutex_lock(&module_mutex
);
1812 list_for_each_entry_rcu(mod
, &modules
, list
) {
1813 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1814 set_page_attributes(mod
->module_core
,
1815 mod
->module_core
+ mod
->core_text_size
,
1818 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1819 set_page_attributes(mod
->module_init
,
1820 mod
->module_init
+ mod
->init_text_size
,
1824 mutex_unlock(&module_mutex
);
1827 static inline void set_section_ro_nx(void *base
, unsigned long text_size
, unsigned long ro_size
, unsigned long total_size
) { }
1828 static void unset_module_core_ro_nx(struct module
*mod
) { }
1829 static void unset_module_init_ro_nx(struct module
*mod
) { }
1832 void __weak
module_free(struct module
*mod
, void *module_region
)
1834 vfree(module_region
);
1837 void __weak
module_arch_cleanup(struct module
*mod
)
1841 /* Free a module, remove from lists, etc. */
1842 static void free_module(struct module
*mod
)
1844 trace_module_free(mod
);
1846 /* Delete from various lists */
1847 mutex_lock(&module_mutex
);
1848 stop_machine(__unlink_module
, mod
, NULL
);
1849 mutex_unlock(&module_mutex
);
1850 mod_sysfs_teardown(mod
);
1852 /* Remove dynamic debug info */
1853 ddebug_remove_module(mod
->name
);
1855 /* Arch-specific cleanup. */
1856 module_arch_cleanup(mod
);
1858 /* Module unload stuff */
1859 module_unload_free(mod
);
1861 /* Free any allocated parameters. */
1862 destroy_params(mod
->kp
, mod
->num_kp
);
1864 /* This may be NULL, but that's OK */
1865 unset_module_init_ro_nx(mod
);
1866 module_free(mod
, mod
->module_init
);
1868 percpu_modfree(mod
);
1870 /* Free lock-classes: */
1871 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1873 /* Finally, free the core (containing the module structure) */
1874 unset_module_core_ro_nx(mod
);
1875 module_free(mod
, mod
->module_core
);
1878 update_protections(current
->mm
);
1882 void *__symbol_get(const char *symbol
)
1884 struct module
*owner
;
1885 const struct kernel_symbol
*sym
;
1888 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1889 if (sym
&& strong_try_module_get(owner
))
1893 return sym
? (void *)sym
->value
: NULL
;
1895 EXPORT_SYMBOL_GPL(__symbol_get
);
1898 * Ensure that an exported symbol [global namespace] does not already exist
1899 * in the kernel or in some other module's exported symbol table.
1901 * You must hold the module_mutex.
1903 static int verify_export_symbols(struct module
*mod
)
1906 struct module
*owner
;
1907 const struct kernel_symbol
*s
;
1909 const struct kernel_symbol
*sym
;
1912 { mod
->syms
, mod
->num_syms
},
1913 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1914 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1915 #ifdef CONFIG_UNUSED_SYMBOLS
1916 { mod
->unused_syms
, mod
->num_unused_syms
},
1917 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1921 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1922 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1923 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1925 "%s: exports duplicate symbol %s"
1927 mod
->name
, s
->name
, module_name(owner
));
1935 /* Change all symbols so that st_value encodes the pointer directly. */
1936 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
1938 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
1939 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
1940 unsigned long secbase
;
1943 const struct kernel_symbol
*ksym
;
1945 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
1946 const char *name
= info
->strtab
+ sym
[i
].st_name
;
1948 switch (sym
[i
].st_shndx
) {
1950 /* We compiled with -fno-common. These are not
1951 supposed to happen. */
1952 pr_debug("Common symbol: %s\n", name
);
1953 printk("%s: please compile with -fno-common\n",
1959 /* Don't need to do anything */
1960 pr_debug("Absolute symbol: 0x%08lx\n",
1961 (long)sym
[i
].st_value
);
1965 ksym
= resolve_symbol_wait(mod
, info
, name
);
1966 /* Ok if resolved. */
1967 if (ksym
&& !IS_ERR(ksym
)) {
1968 sym
[i
].st_value
= ksym
->value
;
1973 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1976 printk(KERN_WARNING
"%s: Unknown symbol %s (err %li)\n",
1977 mod
->name
, name
, PTR_ERR(ksym
));
1978 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
1982 /* Divert to percpu allocation if a percpu var. */
1983 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
1984 secbase
= (unsigned long)mod_percpu(mod
);
1986 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1987 sym
[i
].st_value
+= secbase
;
1995 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
2000 /* Now do relocations. */
2001 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2002 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
2004 /* Not a valid relocation section? */
2005 if (infosec
>= info
->hdr
->e_shnum
)
2008 /* Don't bother with non-allocated sections */
2009 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
2012 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
2013 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
2014 info
->index
.sym
, i
, mod
);
2015 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
2016 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
2017 info
->index
.sym
, i
, mod
);
2024 /* Additional bytes needed by arch in front of individual sections */
2025 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
2026 unsigned int section
)
2028 /* default implementation just returns zero */
2032 /* Update size with this section: return offset. */
2033 static long get_offset(struct module
*mod
, unsigned int *size
,
2034 Elf_Shdr
*sechdr
, unsigned int section
)
2038 *size
+= arch_mod_section_prepend(mod
, section
);
2039 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
2040 *size
= ret
+ sechdr
->sh_size
;
2044 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2045 might -- code, read-only data, read-write data, small data. Tally
2046 sizes, and place the offsets into sh_entsize fields: high bit means it
2048 static void layout_sections(struct module
*mod
, struct load_info
*info
)
2050 static unsigned long const masks
[][2] = {
2051 /* NOTE: all executable code must be the first section
2052 * in this array; otherwise modify the text_size
2053 * finder in the two loops below */
2054 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2055 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
2056 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2057 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
2061 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
2062 info
->sechdrs
[i
].sh_entsize
= ~0UL;
2064 pr_debug("Core section allocation order:\n");
2065 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2066 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2067 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2068 const char *sname
= info
->secstrings
+ s
->sh_name
;
2070 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2071 || (s
->sh_flags
& masks
[m
][1])
2072 || s
->sh_entsize
!= ~0UL
2073 || strstarts(sname
, ".init"))
2075 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
2076 pr_debug("\t%s\n", sname
);
2079 case 0: /* executable */
2080 mod
->core_size
= debug_align(mod
->core_size
);
2081 mod
->core_text_size
= mod
->core_size
;
2083 case 1: /* RO: text and ro-data */
2084 mod
->core_size
= debug_align(mod
->core_size
);
2085 mod
->core_ro_size
= mod
->core_size
;
2087 case 3: /* whole core */
2088 mod
->core_size
= debug_align(mod
->core_size
);
2093 pr_debug("Init section allocation order:\n");
2094 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2095 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2096 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2097 const char *sname
= info
->secstrings
+ s
->sh_name
;
2099 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2100 || (s
->sh_flags
& masks
[m
][1])
2101 || s
->sh_entsize
!= ~0UL
2102 || !strstarts(sname
, ".init"))
2104 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
2105 | INIT_OFFSET_MASK
);
2106 pr_debug("\t%s\n", sname
);
2109 case 0: /* executable */
2110 mod
->init_size
= debug_align(mod
->init_size
);
2111 mod
->init_text_size
= mod
->init_size
;
2113 case 1: /* RO: text and ro-data */
2114 mod
->init_size
= debug_align(mod
->init_size
);
2115 mod
->init_ro_size
= mod
->init_size
;
2117 case 3: /* whole init */
2118 mod
->init_size
= debug_align(mod
->init_size
);
2124 static void set_license(struct module
*mod
, const char *license
)
2127 license
= "unspecified";
2129 if (!license_is_gpl_compatible(license
)) {
2130 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
2131 printk(KERN_WARNING
"%s: module license '%s' taints "
2132 "kernel.\n", mod
->name
, license
);
2133 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2137 /* Parse tag=value strings from .modinfo section */
2138 static char *next_string(char *string
, unsigned long *secsize
)
2140 /* Skip non-zero chars */
2143 if ((*secsize
)-- <= 1)
2147 /* Skip any zero padding. */
2148 while (!string
[0]) {
2150 if ((*secsize
)-- <= 1)
2156 static char *get_modinfo(struct load_info
*info
, const char *tag
)
2159 unsigned int taglen
= strlen(tag
);
2160 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
2161 unsigned long size
= infosec
->sh_size
;
2163 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
2164 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
2165 return p
+ taglen
+ 1;
2170 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2172 struct module_attribute
*attr
;
2175 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2177 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2181 static void free_modinfo(struct module
*mod
)
2183 struct module_attribute
*attr
;
2186 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2192 #ifdef CONFIG_KALLSYMS
2194 /* lookup symbol in given range of kernel_symbols */
2195 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2196 const struct kernel_symbol
*start
,
2197 const struct kernel_symbol
*stop
)
2199 return bsearch(name
, start
, stop
- start
,
2200 sizeof(struct kernel_symbol
), cmp_name
);
2203 static int is_exported(const char *name
, unsigned long value
,
2204 const struct module
*mod
)
2206 const struct kernel_symbol
*ks
;
2208 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2210 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2211 return ks
!= NULL
&& ks
->value
== value
;
2215 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2217 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2219 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2220 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2225 if (sym
->st_shndx
== SHN_UNDEF
)
2227 if (sym
->st_shndx
== SHN_ABS
)
2229 if (sym
->st_shndx
>= SHN_LORESERVE
)
2231 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2233 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2234 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2235 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2237 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2242 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2243 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2248 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2255 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2258 const Elf_Shdr
*sec
;
2260 if (src
->st_shndx
== SHN_UNDEF
2261 || src
->st_shndx
>= shnum
2265 sec
= sechdrs
+ src
->st_shndx
;
2266 if (!(sec
->sh_flags
& SHF_ALLOC
)
2267 #ifndef CONFIG_KALLSYMS_ALL
2268 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2270 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2277 * We only allocate and copy the strings needed by the parts of symtab
2278 * we keep. This is simple, but has the effect of making multiple
2279 * copies of duplicates. We could be more sophisticated, see
2280 * linux-kernel thread starting with
2281 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2283 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2285 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2286 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2288 unsigned int i
, nsrc
, ndst
, strtab_size
= 0;
2290 /* Put symbol section at end of init part of module. */
2291 symsect
->sh_flags
|= SHF_ALLOC
;
2292 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
2293 info
->index
.sym
) | INIT_OFFSET_MASK
;
2294 pr_debug("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2296 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2297 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2299 /* Compute total space required for the core symbols' strtab. */
2300 for (ndst
= i
= 0; i
< nsrc
; i
++) {
2302 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
2303 strtab_size
+= strlen(&info
->strtab
[src
[i
].st_name
])+1;
2308 /* Append room for core symbols at end of core part. */
2309 info
->symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
2310 info
->stroffs
= mod
->core_size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2311 mod
->core_size
+= strtab_size
;
2313 /* Put string table section at end of init part of module. */
2314 strsect
->sh_flags
|= SHF_ALLOC
;
2315 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
2316 info
->index
.str
) | INIT_OFFSET_MASK
;
2317 pr_debug("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2320 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2322 unsigned int i
, ndst
;
2326 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2328 mod
->symtab
= (void *)symsec
->sh_addr
;
2329 mod
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2330 /* Make sure we get permanent strtab: don't use info->strtab. */
2331 mod
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2333 /* Set types up while we still have access to sections. */
2334 for (i
= 0; i
< mod
->num_symtab
; i
++)
2335 mod
->symtab
[i
].st_info
= elf_type(&mod
->symtab
[i
], info
);
2337 mod
->core_symtab
= dst
= mod
->module_core
+ info
->symoffs
;
2338 mod
->core_strtab
= s
= mod
->module_core
+ info
->stroffs
;
2340 for (ndst
= i
= 0; i
< mod
->num_symtab
; i
++) {
2342 is_core_symbol(src
+i
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
2344 dst
[ndst
++].st_name
= s
- mod
->core_strtab
;
2345 s
+= strlcpy(s
, &mod
->strtab
[src
[i
].st_name
],
2349 mod
->core_num_syms
= ndst
;
2352 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2356 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2359 #endif /* CONFIG_KALLSYMS */
2361 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2365 #ifdef CONFIG_DYNAMIC_DEBUG
2366 if (ddebug_add_module(debug
, num
, debug
->modname
))
2367 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
2372 static void dynamic_debug_remove(struct _ddebug
*debug
)
2375 ddebug_remove_module(debug
->modname
);
2378 void * __weak
module_alloc(unsigned long size
)
2380 return vmalloc_exec(size
);
2383 static void *module_alloc_update_bounds(unsigned long size
)
2385 void *ret
= module_alloc(size
);
2388 mutex_lock(&module_mutex
);
2389 /* Update module bounds. */
2390 if ((unsigned long)ret
< module_addr_min
)
2391 module_addr_min
= (unsigned long)ret
;
2392 if ((unsigned long)ret
+ size
> module_addr_max
)
2393 module_addr_max
= (unsigned long)ret
+ size
;
2394 mutex_unlock(&module_mutex
);
2399 #ifdef CONFIG_DEBUG_KMEMLEAK
2400 static void kmemleak_load_module(const struct module
*mod
,
2401 const struct load_info
*info
)
2405 /* only scan the sections containing data */
2406 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2408 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2409 const char *name
= info
->secstrings
+ info
->sechdrs
[i
].sh_name
;
2410 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2412 if (!strstarts(name
, ".data") && !strstarts(name
, ".bss"))
2415 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2416 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2420 static inline void kmemleak_load_module(const struct module
*mod
,
2421 const struct load_info
*info
)
2426 #ifdef CONFIG_MODULE_SIG
2427 static int module_sig_check(struct load_info
*info
)
2430 const unsigned long markerlen
= sizeof(MODULE_SIG_STRING
) - 1;
2431 const void *mod
= info
->hdr
;
2433 if (info
->len
> markerlen
&&
2434 memcmp(mod
+ info
->len
- markerlen
, MODULE_SIG_STRING
, markerlen
) == 0) {
2435 /* We truncate the module to discard the signature */
2436 info
->len
-= markerlen
;
2437 err
= mod_verify_sig(mod
, &info
->len
);
2441 info
->sig_ok
= true;
2445 /* Not having a signature is only an error if we're strict. */
2446 if (err
< 0 && fips_enabled
)
2447 panic("Module verification failed with error %d in FIPS mode\n",
2449 if (err
== -ENOKEY
&& !sig_enforce
)
2454 #else /* !CONFIG_MODULE_SIG */
2455 static int module_sig_check(struct load_info
*info
)
2459 #endif /* !CONFIG_MODULE_SIG */
2461 /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2462 static int elf_header_check(struct load_info
*info
)
2464 if (info
->len
< sizeof(*(info
->hdr
)))
2467 if (memcmp(info
->hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2468 || info
->hdr
->e_type
!= ET_REL
2469 || !elf_check_arch(info
->hdr
)
2470 || info
->hdr
->e_shentsize
!= sizeof(Elf_Shdr
))
2473 if (info
->hdr
->e_shoff
>= info
->len
2474 || (info
->hdr
->e_shnum
* sizeof(Elf_Shdr
) >
2475 info
->len
- info
->hdr
->e_shoff
))
2481 /* Sets info->hdr and info->len. */
2482 static int copy_module_from_user(const void __user
*umod
, unsigned long len
,
2483 struct load_info
*info
)
2488 if (info
->len
< sizeof(*(info
->hdr
)))
2491 err
= security_kernel_module_from_file(NULL
);
2495 /* Suck in entire file: we'll want most of it. */
2496 info
->hdr
= vmalloc(info
->len
);
2500 if (copy_from_user(info
->hdr
, umod
, info
->len
) != 0) {
2508 /* Sets info->hdr and info->len. */
2509 static int copy_module_from_fd(int fd
, struct load_info
*info
)
2521 err
= security_kernel_module_from_file(file
);
2525 err
= vfs_getattr(file
->f_vfsmnt
, file
->f_dentry
, &stat
);
2529 if (stat
.size
> INT_MAX
) {
2533 info
->hdr
= vmalloc(stat
.size
);
2540 while (pos
< stat
.size
) {
2541 bytes
= kernel_read(file
, pos
, (char *)(info
->hdr
) + pos
,
2559 static void free_copy(struct load_info
*info
)
2564 static int rewrite_section_headers(struct load_info
*info
, int flags
)
2568 /* This should always be true, but let's be sure. */
2569 info
->sechdrs
[0].sh_addr
= 0;
2571 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2572 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2573 if (shdr
->sh_type
!= SHT_NOBITS
2574 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2575 printk(KERN_ERR
"Module len %lu truncated\n",
2580 /* Mark all sections sh_addr with their address in the
2582 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2584 #ifndef CONFIG_MODULE_UNLOAD
2585 /* Don't load .exit sections */
2586 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2587 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2591 /* Track but don't keep modinfo and version sections. */
2592 if (flags
& MODULE_INIT_IGNORE_MODVERSIONS
)
2593 info
->index
.vers
= 0; /* Pretend no __versions section! */
2595 info
->index
.vers
= find_sec(info
, "__versions");
2596 info
->index
.info
= find_sec(info
, ".modinfo");
2597 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2598 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2603 * Set up our basic convenience variables (pointers to section headers,
2604 * search for module section index etc), and do some basic section
2607 * Return the temporary module pointer (we'll replace it with the final
2608 * one when we move the module sections around).
2610 static struct module
*setup_load_info(struct load_info
*info
, int flags
)
2616 /* Set up the convenience variables */
2617 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2618 info
->secstrings
= (void *)info
->hdr
2619 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2621 err
= rewrite_section_headers(info
, flags
);
2623 return ERR_PTR(err
);
2625 /* Find internal symbols and strings. */
2626 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2627 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2628 info
->index
.sym
= i
;
2629 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2630 info
->strtab
= (char *)info
->hdr
2631 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2636 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2637 if (!info
->index
.mod
) {
2638 printk(KERN_WARNING
"No module found in object\n");
2639 return ERR_PTR(-ENOEXEC
);
2641 /* This is temporary: point mod into copy of data. */
2642 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2644 if (info
->index
.sym
== 0) {
2645 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2647 return ERR_PTR(-ENOEXEC
);
2650 info
->index
.pcpu
= find_pcpusec(info
);
2652 /* Check module struct version now, before we try to use module. */
2653 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2654 return ERR_PTR(-ENOEXEC
);
2659 static int check_modinfo(struct module
*mod
, struct load_info
*info
, int flags
)
2661 const char *modmagic
= get_modinfo(info
, "vermagic");
2664 if (flags
& MODULE_INIT_IGNORE_VERMAGIC
)
2667 /* This is allowed: modprobe --force will invalidate it. */
2669 err
= try_to_force_load(mod
, "bad vermagic");
2672 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2673 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2674 mod
->name
, modmagic
, vermagic
);
2678 if (!get_modinfo(info
, "intree"))
2679 add_taint_module(mod
, TAINT_OOT_MODULE
);
2681 if (get_modinfo(info
, "staging")) {
2682 add_taint_module(mod
, TAINT_CRAP
);
2683 printk(KERN_WARNING
"%s: module is from the staging directory,"
2684 " the quality is unknown, you have been warned.\n",
2688 /* Set up license info based on the info section */
2689 set_license(mod
, get_modinfo(info
, "license"));
2694 static void find_module_sections(struct module
*mod
, struct load_info
*info
)
2696 mod
->kp
= section_objs(info
, "__param",
2697 sizeof(*mod
->kp
), &mod
->num_kp
);
2698 mod
->syms
= section_objs(info
, "__ksymtab",
2699 sizeof(*mod
->syms
), &mod
->num_syms
);
2700 mod
->crcs
= section_addr(info
, "__kcrctab");
2701 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
2702 sizeof(*mod
->gpl_syms
),
2703 &mod
->num_gpl_syms
);
2704 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
2705 mod
->gpl_future_syms
= section_objs(info
,
2706 "__ksymtab_gpl_future",
2707 sizeof(*mod
->gpl_future_syms
),
2708 &mod
->num_gpl_future_syms
);
2709 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
2711 #ifdef CONFIG_UNUSED_SYMBOLS
2712 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
2713 sizeof(*mod
->unused_syms
),
2714 &mod
->num_unused_syms
);
2715 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
2716 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
2717 sizeof(*mod
->unused_gpl_syms
),
2718 &mod
->num_unused_gpl_syms
);
2719 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
2721 #ifdef CONFIG_CONSTRUCTORS
2722 mod
->ctors
= section_objs(info
, ".ctors",
2723 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2726 #ifdef CONFIG_TRACEPOINTS
2727 mod
->tracepoints_ptrs
= section_objs(info
, "__tracepoints_ptrs",
2728 sizeof(*mod
->tracepoints_ptrs
),
2729 &mod
->num_tracepoints
);
2731 #ifdef HAVE_JUMP_LABEL
2732 mod
->jump_entries
= section_objs(info
, "__jump_table",
2733 sizeof(*mod
->jump_entries
),
2734 &mod
->num_jump_entries
);
2736 #ifdef CONFIG_EVENT_TRACING
2737 mod
->trace_events
= section_objs(info
, "_ftrace_events",
2738 sizeof(*mod
->trace_events
),
2739 &mod
->num_trace_events
);
2741 * This section contains pointers to allocated objects in the trace
2742 * code and not scanning it leads to false positives.
2744 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2745 mod
->num_trace_events
, GFP_KERNEL
);
2747 #ifdef CONFIG_TRACING
2748 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
2749 sizeof(*mod
->trace_bprintk_fmt_start
),
2750 &mod
->num_trace_bprintk_fmt
);
2752 * This section contains pointers to allocated objects in the trace
2753 * code and not scanning it leads to false positives.
2755 kmemleak_scan_area(mod
->trace_bprintk_fmt_start
,
2756 sizeof(*mod
->trace_bprintk_fmt_start
) *
2757 mod
->num_trace_bprintk_fmt
, GFP_KERNEL
);
2759 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2760 /* sechdrs[0].sh_size is always zero */
2761 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
2762 sizeof(*mod
->ftrace_callsites
),
2763 &mod
->num_ftrace_callsites
);
2766 mod
->extable
= section_objs(info
, "__ex_table",
2767 sizeof(*mod
->extable
), &mod
->num_exentries
);
2769 if (section_addr(info
, "__obsparm"))
2770 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2773 info
->debug
= section_objs(info
, "__verbose",
2774 sizeof(*info
->debug
), &info
->num_debug
);
2777 static int move_module(struct module
*mod
, struct load_info
*info
)
2782 /* Do the allocs. */
2783 ptr
= module_alloc_update_bounds(mod
->core_size
);
2785 * The pointer to this block is stored in the module structure
2786 * which is inside the block. Just mark it as not being a
2789 kmemleak_not_leak(ptr
);
2793 memset(ptr
, 0, mod
->core_size
);
2794 mod
->module_core
= ptr
;
2796 if (mod
->init_size
) {
2797 ptr
= module_alloc_update_bounds(mod
->init_size
);
2799 * The pointer to this block is stored in the module structure
2800 * which is inside the block. This block doesn't need to be
2801 * scanned as it contains data and code that will be freed
2802 * after the module is initialized.
2804 kmemleak_ignore(ptr
);
2806 module_free(mod
, mod
->module_core
);
2809 memset(ptr
, 0, mod
->init_size
);
2810 mod
->module_init
= ptr
;
2812 mod
->module_init
= NULL
;
2814 /* Transfer each section which specifies SHF_ALLOC */
2815 pr_debug("final section addresses:\n");
2816 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
2818 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2820 if (!(shdr
->sh_flags
& SHF_ALLOC
))
2823 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
2824 dest
= mod
->module_init
2825 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
2827 dest
= mod
->module_core
+ shdr
->sh_entsize
;
2829 if (shdr
->sh_type
!= SHT_NOBITS
)
2830 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
2831 /* Update sh_addr to point to copy in image. */
2832 shdr
->sh_addr
= (unsigned long)dest
;
2833 pr_debug("\t0x%lx %s\n",
2834 (long)shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
2840 static int check_module_license_and_versions(struct module
*mod
)
2843 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2844 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2845 * using GPL-only symbols it needs.
2847 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2848 add_taint(TAINT_PROPRIETARY_MODULE
);
2850 /* driverloader was caught wrongly pretending to be under GPL */
2851 if (strcmp(mod
->name
, "driverloader") == 0)
2852 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2854 /* lve claims to be GPL but upstream won't provide source */
2855 if (strcmp(mod
->name
, "lve") == 0)
2856 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2858 #ifdef CONFIG_MODVERSIONS
2859 if ((mod
->num_syms
&& !mod
->crcs
)
2860 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2861 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2862 #ifdef CONFIG_UNUSED_SYMBOLS
2863 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2864 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2867 return try_to_force_load(mod
,
2868 "no versions for exported symbols");
2874 static void flush_module_icache(const struct module
*mod
)
2876 mm_segment_t old_fs
;
2878 /* flush the icache in correct context */
2883 * Flush the instruction cache, since we've played with text.
2884 * Do it before processing of module parameters, so the module
2885 * can provide parameter accessor functions of its own.
2887 if (mod
->module_init
)
2888 flush_icache_range((unsigned long)mod
->module_init
,
2889 (unsigned long)mod
->module_init
2891 flush_icache_range((unsigned long)mod
->module_core
,
2892 (unsigned long)mod
->module_core
+ mod
->core_size
);
2897 int __weak
module_frob_arch_sections(Elf_Ehdr
*hdr
,
2905 static struct module
*layout_and_allocate(struct load_info
*info
, int flags
)
2907 /* Module within temporary copy. */
2912 mod
= setup_load_info(info
, flags
);
2916 err
= check_modinfo(mod
, info
, flags
);
2918 return ERR_PTR(err
);
2920 /* Allow arches to frob section contents and sizes. */
2921 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
2922 info
->secstrings
, mod
);
2926 pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
2927 if (pcpusec
->sh_size
) {
2928 /* We have a special allocation for this section. */
2929 err
= percpu_modalloc(mod
,
2930 pcpusec
->sh_size
, pcpusec
->sh_addralign
);
2933 pcpusec
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2936 /* Determine total sizes, and put offsets in sh_entsize. For now
2937 this is done generically; there doesn't appear to be any
2938 special cases for the architectures. */
2939 layout_sections(mod
, info
);
2940 layout_symtab(mod
, info
);
2942 /* Allocate and move to the final place */
2943 err
= move_module(mod
, info
);
2947 /* Module has been copied to its final place now: return it. */
2948 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2949 kmemleak_load_module(mod
, info
);
2953 percpu_modfree(mod
);
2955 return ERR_PTR(err
);
2958 /* mod is no longer valid after this! */
2959 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
2961 percpu_modfree(mod
);
2962 module_free(mod
, mod
->module_init
);
2963 module_free(mod
, mod
->module_core
);
2966 int __weak
module_finalize(const Elf_Ehdr
*hdr
,
2967 const Elf_Shdr
*sechdrs
,
2973 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
2975 /* Sort exception table now relocations are done. */
2976 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2978 /* Copy relocated percpu area over. */
2979 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
2980 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
2982 /* Setup kallsyms-specific fields. */
2983 add_kallsyms(mod
, info
);
2985 /* Arch-specific module finalizing. */
2986 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
2989 /* Is this module of this name done loading? No locks held. */
2990 static bool finished_loading(const char *name
)
2995 mutex_lock(&module_mutex
);
2996 mod
= find_module(name
);
2997 ret
= !mod
|| mod
->state
!= MODULE_STATE_COMING
;
2998 mutex_unlock(&module_mutex
);
3003 /* Call module constructors. */
3004 static void do_mod_ctors(struct module
*mod
)
3006 #ifdef CONFIG_CONSTRUCTORS
3009 for (i
= 0; i
< mod
->num_ctors
; i
++)
3014 /* This is where the real work happens */
3015 static int do_init_module(struct module
*mod
)
3019 blocking_notifier_call_chain(&module_notify_list
,
3020 MODULE_STATE_COMING
, mod
);
3022 /* Set RO and NX regions for core */
3023 set_section_ro_nx(mod
->module_core
,
3024 mod
->core_text_size
,
3028 /* Set RO and NX regions for init */
3029 set_section_ro_nx(mod
->module_init
,
3030 mod
->init_text_size
,
3035 /* Start the module */
3036 if (mod
->init
!= NULL
)
3037 ret
= do_one_initcall(mod
->init
);
3039 /* Init routine failed: abort. Try to protect us from
3040 buggy refcounters. */
3041 mod
->state
= MODULE_STATE_GOING
;
3042 synchronize_sched();
3044 blocking_notifier_call_chain(&module_notify_list
,
3045 MODULE_STATE_GOING
, mod
);
3047 wake_up_all(&module_wq
);
3052 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3053 "%s: loading module anyway...\n",
3054 __func__
, mod
->name
, ret
,
3059 /* Now it's a first class citizen! */
3060 mod
->state
= MODULE_STATE_LIVE
;
3061 blocking_notifier_call_chain(&module_notify_list
,
3062 MODULE_STATE_LIVE
, mod
);
3064 /* We need to finish all async code before the module init sequence is done */
3065 async_synchronize_full();
3067 mutex_lock(&module_mutex
);
3068 /* Drop initial reference. */
3070 trim_init_extable(mod
);
3071 #ifdef CONFIG_KALLSYMS
3072 mod
->num_symtab
= mod
->core_num_syms
;
3073 mod
->symtab
= mod
->core_symtab
;
3074 mod
->strtab
= mod
->core_strtab
;
3076 unset_module_init_ro_nx(mod
);
3077 module_free(mod
, mod
->module_init
);
3078 mod
->module_init
= NULL
;
3080 mod
->init_ro_size
= 0;
3081 mod
->init_text_size
= 0;
3082 mutex_unlock(&module_mutex
);
3083 wake_up_all(&module_wq
);
3088 static int may_init_module(void)
3090 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
3096 /* Allocate and load the module: note that size of section 0 is always
3097 zero, and we rely on this for optional sections. */
3098 static int load_module(struct load_info
*info
, const char __user
*uargs
,
3101 struct module
*mod
, *old
;
3104 err
= module_sig_check(info
);
3108 err
= elf_header_check(info
);
3112 /* Figure out module layout, and allocate all the memory. */
3113 mod
= layout_and_allocate(info
, flags
);
3119 #ifdef CONFIG_MODULE_SIG
3120 mod
->sig_ok
= info
->sig_ok
;
3122 add_taint_module(mod
, TAINT_FORCED_MODULE
);
3125 /* Now module is in final location, initialize linked lists, etc. */
3126 err
= module_unload_init(mod
);
3130 /* Now we've got everything in the final locations, we can
3131 * find optional sections. */
3132 find_module_sections(mod
, info
);
3134 err
= check_module_license_and_versions(mod
);
3138 /* Set up MODINFO_ATTR fields */
3139 setup_modinfo(mod
, info
);
3141 /* Fix up syms, so that st_value is a pointer to location. */
3142 err
= simplify_symbols(mod
, info
);
3146 err
= apply_relocations(mod
, info
);
3150 err
= post_relocation(mod
, info
);
3154 flush_module_icache(mod
);
3156 /* Now copy in args */
3157 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
3158 if (IS_ERR(mod
->args
)) {
3159 err
= PTR_ERR(mod
->args
);
3160 goto free_arch_cleanup
;
3163 /* Mark state as coming so strong_try_module_get() ignores us. */
3164 mod
->state
= MODULE_STATE_COMING
;
3166 /* Now sew it into the lists so we can get lockdep and oops
3167 * info during argument parsing. No one should access us, since
3168 * strong_try_module_get() will fail.
3169 * lockdep/oops can run asynchronous, so use the RCU list insertion
3170 * function to insert in a way safe to concurrent readers.
3171 * The mutex protects against concurrent writers.
3174 mutex_lock(&module_mutex
);
3175 if ((old
= find_module(mod
->name
)) != NULL
) {
3176 if (old
->state
== MODULE_STATE_COMING
) {
3177 /* Wait in case it fails to load. */
3178 mutex_unlock(&module_mutex
);
3179 err
= wait_event_interruptible(module_wq
,
3180 finished_loading(mod
->name
));
3182 goto free_arch_cleanup
;
3189 /* This has to be done once we're sure module name is unique. */
3190 dynamic_debug_setup(info
->debug
, info
->num_debug
);
3192 /* Find duplicate symbols */
3193 err
= verify_export_symbols(mod
);
3197 module_bug_finalize(info
->hdr
, info
->sechdrs
, mod
);
3198 list_add_rcu(&mod
->list
, &modules
);
3199 mutex_unlock(&module_mutex
);
3201 /* Module is ready to execute: parsing args may do that. */
3202 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
,
3203 -32768, 32767, &ddebug_dyndbg_module_param_cb
);
3207 /* Link in to syfs. */
3208 err
= mod_sysfs_setup(mod
, info
, mod
->kp
, mod
->num_kp
);
3212 /* Get rid of temporary copy. */
3216 trace_module_load(mod
);
3218 return do_init_module(mod
);
3221 mutex_lock(&module_mutex
);
3222 /* Unlink carefully: kallsyms could be walking list. */
3223 list_del_rcu(&mod
->list
);
3224 module_bug_cleanup(mod
);
3225 wake_up_all(&module_wq
);
3227 dynamic_debug_remove(info
->debug
);
3229 mutex_unlock(&module_mutex
);
3230 synchronize_sched();
3233 module_arch_cleanup(mod
);
3237 module_unload_free(mod
);
3239 module_deallocate(mod
, info
);
3245 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
3246 unsigned long, len
, const char __user
*, uargs
)
3249 struct load_info info
= { };
3251 err
= may_init_module();
3255 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3258 err
= copy_module_from_user(umod
, len
, &info
);
3262 return load_module(&info
, uargs
, 0);
3265 SYSCALL_DEFINE3(finit_module
, int, fd
, const char __user
*, uargs
, int, flags
)
3268 struct load_info info
= { };
3270 err
= may_init_module();
3274 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd
, uargs
, flags
);
3276 if (flags
& ~(MODULE_INIT_IGNORE_MODVERSIONS
3277 |MODULE_INIT_IGNORE_VERMAGIC
))
3280 err
= copy_module_from_fd(fd
, &info
);
3284 return load_module(&info
, uargs
, flags
);
3287 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
3289 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
3292 #ifdef CONFIG_KALLSYMS
3294 * This ignores the intensely annoying "mapping symbols" found
3295 * in ARM ELF files: $a, $t and $d.
3297 static inline int is_arm_mapping_symbol(const char *str
)
3299 return str
[0] == '$' && strchr("atd", str
[1])
3300 && (str
[2] == '\0' || str
[2] == '.');
3303 static const char *get_ksymbol(struct module
*mod
,
3305 unsigned long *size
,
3306 unsigned long *offset
)
3308 unsigned int i
, best
= 0;
3309 unsigned long nextval
;
3311 /* At worse, next value is at end of module */
3312 if (within_module_init(addr
, mod
))
3313 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
3315 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
3317 /* Scan for closest preceding symbol, and next symbol. (ELF
3318 starts real symbols at 1). */
3319 for (i
= 1; i
< mod
->num_symtab
; i
++) {
3320 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
3323 /* We ignore unnamed symbols: they're uninformative
3324 * and inserted at a whim. */
3325 if (mod
->symtab
[i
].st_value
<= addr
3326 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
3327 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3328 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3330 if (mod
->symtab
[i
].st_value
> addr
3331 && mod
->symtab
[i
].st_value
< nextval
3332 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3333 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3334 nextval
= mod
->symtab
[i
].st_value
;
3341 *size
= nextval
- mod
->symtab
[best
].st_value
;
3343 *offset
= addr
- mod
->symtab
[best
].st_value
;
3344 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
3347 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3348 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3349 const char *module_address_lookup(unsigned long addr
,
3350 unsigned long *size
,
3351 unsigned long *offset
,
3356 const char *ret
= NULL
;
3359 list_for_each_entry_rcu(mod
, &modules
, list
) {
3360 if (within_module_init(addr
, mod
) ||
3361 within_module_core(addr
, mod
)) {
3363 *modname
= mod
->name
;
3364 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3368 /* Make a copy in here where it's safe */
3370 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3377 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3382 list_for_each_entry_rcu(mod
, &modules
, list
) {
3383 if (within_module_init(addr
, mod
) ||
3384 within_module_core(addr
, mod
)) {
3387 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3390 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3400 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3401 unsigned long *offset
, char *modname
, char *name
)
3406 list_for_each_entry_rcu(mod
, &modules
, list
) {
3407 if (within_module_init(addr
, mod
) ||
3408 within_module_core(addr
, mod
)) {
3411 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3415 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3417 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3427 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3428 char *name
, char *module_name
, int *exported
)
3433 list_for_each_entry_rcu(mod
, &modules
, list
) {
3434 if (symnum
< mod
->num_symtab
) {
3435 *value
= mod
->symtab
[symnum
].st_value
;
3436 *type
= mod
->symtab
[symnum
].st_info
;
3437 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
3439 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
3440 *exported
= is_exported(name
, *value
, mod
);
3444 symnum
-= mod
->num_symtab
;
3450 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
3454 for (i
= 0; i
< mod
->num_symtab
; i
++)
3455 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
3456 mod
->symtab
[i
].st_info
!= 'U')
3457 return mod
->symtab
[i
].st_value
;
3461 /* Look for this name: can be of form module:name. */
3462 unsigned long module_kallsyms_lookup_name(const char *name
)
3466 unsigned long ret
= 0;
3468 /* Don't lock: we're in enough trouble already. */
3470 if ((colon
= strchr(name
, ':')) != NULL
) {
3472 if ((mod
= find_module(name
)) != NULL
)
3473 ret
= mod_find_symname(mod
, colon
+1);
3476 list_for_each_entry_rcu(mod
, &modules
, list
)
3477 if ((ret
= mod_find_symname(mod
, name
)) != 0)
3484 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
3485 struct module
*, unsigned long),
3492 list_for_each_entry(mod
, &modules
, list
) {
3493 for (i
= 0; i
< mod
->num_symtab
; i
++) {
3494 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
3495 mod
, mod
->symtab
[i
].st_value
);
3502 #endif /* CONFIG_KALLSYMS */
3504 static char *module_flags(struct module
*mod
, char *buf
)
3509 mod
->state
== MODULE_STATE_GOING
||
3510 mod
->state
== MODULE_STATE_COMING
) {
3512 bx
+= module_flags_taint(mod
, buf
+ bx
);
3513 /* Show a - for module-is-being-unloaded */
3514 if (mod
->state
== MODULE_STATE_GOING
)
3516 /* Show a + for module-is-being-loaded */
3517 if (mod
->state
== MODULE_STATE_COMING
)
3526 #ifdef CONFIG_PROC_FS
3527 /* Called by the /proc file system to return a list of modules. */
3528 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
3530 mutex_lock(&module_mutex
);
3531 return seq_list_start(&modules
, *pos
);
3534 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3536 return seq_list_next(p
, &modules
, pos
);
3539 static void m_stop(struct seq_file
*m
, void *p
)
3541 mutex_unlock(&module_mutex
);
3544 static int m_show(struct seq_file
*m
, void *p
)
3546 struct module
*mod
= list_entry(p
, struct module
, list
);
3549 seq_printf(m
, "%s %u",
3550 mod
->name
, mod
->init_size
+ mod
->core_size
);
3551 print_unload_info(m
, mod
);
3553 /* Informative for users. */
3554 seq_printf(m
, " %s",
3555 mod
->state
== MODULE_STATE_GOING
? "Unloading":
3556 mod
->state
== MODULE_STATE_COMING
? "Loading":
3558 /* Used by oprofile and other similar tools. */
3559 seq_printf(m
, " 0x%pK", mod
->module_core
);
3563 seq_printf(m
, " %s", module_flags(mod
, buf
));
3565 seq_printf(m
, "\n");
3569 /* Format: modulename size refcount deps address
3571 Where refcount is a number or -, and deps is a comma-separated list
3574 static const struct seq_operations modules_op
= {
3581 static int modules_open(struct inode
*inode
, struct file
*file
)
3583 return seq_open(file
, &modules_op
);
3586 static const struct file_operations proc_modules_operations
= {
3587 .open
= modules_open
,
3589 .llseek
= seq_lseek
,
3590 .release
= seq_release
,
3593 static int __init
proc_modules_init(void)
3595 proc_create("modules", 0, NULL
, &proc_modules_operations
);
3598 module_init(proc_modules_init
);
3601 /* Given an address, look for it in the module exception tables. */
3602 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
3604 const struct exception_table_entry
*e
= NULL
;
3608 list_for_each_entry_rcu(mod
, &modules
, list
) {
3609 if (mod
->num_exentries
== 0)
3612 e
= search_extable(mod
->extable
,
3613 mod
->extable
+ mod
->num_exentries
- 1,
3620 /* Now, if we found one, we are running inside it now, hence
3621 we cannot unload the module, hence no refcnt needed. */
3626 * is_module_address - is this address inside a module?
3627 * @addr: the address to check.
3629 * See is_module_text_address() if you simply want to see if the address
3630 * is code (not data).
3632 bool is_module_address(unsigned long addr
)
3637 ret
= __module_address(addr
) != NULL
;
3644 * __module_address - get the module which contains an address.
3645 * @addr: the address.
3647 * Must be called with preempt disabled or module mutex held so that
3648 * module doesn't get freed during this.
3650 struct module
*__module_address(unsigned long addr
)
3654 if (addr
< module_addr_min
|| addr
> module_addr_max
)
3657 list_for_each_entry_rcu(mod
, &modules
, list
)
3658 if (within_module_core(addr
, mod
)
3659 || within_module_init(addr
, mod
))
3663 EXPORT_SYMBOL_GPL(__module_address
);
3666 * is_module_text_address - is this address inside module code?
3667 * @addr: the address to check.
3669 * See is_module_address() if you simply want to see if the address is
3670 * anywhere in a module. See kernel_text_address() for testing if an
3671 * address corresponds to kernel or module code.
3673 bool is_module_text_address(unsigned long addr
)
3678 ret
= __module_text_address(addr
) != NULL
;
3685 * __module_text_address - get the module whose code contains an address.
3686 * @addr: the address.
3688 * Must be called with preempt disabled or module mutex held so that
3689 * module doesn't get freed during this.
3691 struct module
*__module_text_address(unsigned long addr
)
3693 struct module
*mod
= __module_address(addr
);
3695 /* Make sure it's within the text section. */
3696 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
3697 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3702 EXPORT_SYMBOL_GPL(__module_text_address
);
3704 /* Don't grab lock, we're oopsing. */
3705 void print_modules(void)
3710 printk(KERN_DEFAULT
"Modules linked in:");
3711 /* Most callers should already have preempt disabled, but make sure */
3713 list_for_each_entry_rcu(mod
, &modules
, list
)
3714 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3716 if (last_unloaded_module
[0])
3717 printk(" [last unloaded: %s]", last_unloaded_module
);
3721 #ifdef CONFIG_MODVERSIONS
3722 /* Generate the signature for all relevant module structures here.
3723 * If these change, we don't want to try to parse the module. */
3724 void module_layout(struct module
*mod
,
3725 struct modversion_info
*ver
,
3726 struct kernel_param
*kp
,
3727 struct kernel_symbol
*ks
,
3728 struct tracepoint
* const *tp
)
3731 EXPORT_SYMBOL(module_layout
);