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>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
58 #include <linux/jump_label.h>
59 #include <linux/pfn.h>
60 #include <linux/bsearch.h>
61 #include "module-internal.h"
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/module.h>
66 #ifndef ARCH_SHF_SMALL
67 #define ARCH_SHF_SMALL 0
71 * Modules' sections will be aligned on page boundaries
72 * to ensure complete separation of code and data, but
73 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
75 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
76 # define debug_align(X) ALIGN(X, PAGE_SIZE)
78 # define debug_align(X) (X)
82 * Given BASE and SIZE this macro calculates the number of pages the
83 * memory regions occupies
85 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
86 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
87 PFN_DOWN((unsigned long)BASE) + 1) \
90 /* If this is set, the section belongs in the init part of the module */
91 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
95 * 1) List of modules (also safely readable with preempt_disable),
96 * 2) module_use links,
97 * 3) module_addr_min/module_addr_max.
98 * (delete uses stop_machine/add uses RCU list operations). */
99 DEFINE_MUTEX(module_mutex
);
100 EXPORT_SYMBOL_GPL(module_mutex
);
101 static LIST_HEAD(modules
);
102 #ifdef CONFIG_KGDB_KDB
103 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
104 #endif /* CONFIG_KGDB_KDB */
106 #ifdef CONFIG_MODULE_SIG
107 #ifdef CONFIG_MODULE_SIG_FORCE
108 static bool sig_enforce
= true;
110 static bool sig_enforce
= false;
112 static int param_set_bool_enable_only(const char *val
,
113 const struct kernel_param
*kp
)
117 struct kernel_param dummy_kp
= *kp
;
119 dummy_kp
.arg
= &test
;
121 err
= param_set_bool(val
, &dummy_kp
);
125 /* Don't let them unset it once it's set! */
126 if (!test
&& sig_enforce
)
134 static const struct kernel_param_ops param_ops_bool_enable_only
= {
135 .set
= param_set_bool_enable_only
,
136 .get
= param_get_bool
,
138 #define param_check_bool_enable_only param_check_bool
140 module_param(sig_enforce
, bool_enable_only
, 0644);
141 #endif /* !CONFIG_MODULE_SIG_FORCE */
142 #endif /* CONFIG_MODULE_SIG */
144 /* Block module loading/unloading? */
145 int modules_disabled
= 0;
146 core_param(nomodule
, modules_disabled
, bint
, 0);
148 /* Waiting for a module to finish initializing? */
149 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
151 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
153 /* Bounds of module allocation, for speeding __module_address.
154 * Protected by module_mutex. */
155 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
157 int register_module_notifier(struct notifier_block
* nb
)
159 return blocking_notifier_chain_register(&module_notify_list
, nb
);
161 EXPORT_SYMBOL(register_module_notifier
);
163 int unregister_module_notifier(struct notifier_block
* nb
)
165 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
167 EXPORT_SYMBOL(unregister_module_notifier
);
173 char *secstrings
, *strtab
;
174 unsigned long symoffs
, stroffs
;
175 struct _ddebug
*debug
;
176 unsigned int num_debug
;
179 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
183 /* We require a truly strong try_module_get(): 0 means failure due to
184 ongoing or failed initialization etc. */
185 static inline int strong_try_module_get(struct module
*mod
)
187 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
189 if (try_module_get(mod
))
195 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
198 mod
->taints
|= (1U << flag
);
202 * A thread that wants to hold a reference to a module only while it
203 * is running can call this to safely exit. nfsd and lockd use this.
205 void __module_put_and_exit(struct module
*mod
, long code
)
210 EXPORT_SYMBOL(__module_put_and_exit
);
212 /* Find a module section: 0 means not found. */
213 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
217 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
218 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
219 /* Alloc bit cleared means "ignore it." */
220 if ((shdr
->sh_flags
& SHF_ALLOC
)
221 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
227 /* Find a module section, or NULL. */
228 static void *section_addr(const struct load_info
*info
, const char *name
)
230 /* Section 0 has sh_addr 0. */
231 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
234 /* Find a module section, or NULL. Fill in number of "objects" in section. */
235 static void *section_objs(const struct load_info
*info
,
240 unsigned int sec
= find_sec(info
, name
);
242 /* Section 0 has sh_addr 0 and sh_size 0. */
243 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
244 return (void *)info
->sechdrs
[sec
].sh_addr
;
247 /* Provided by the linker */
248 extern const struct kernel_symbol __start___ksymtab
[];
249 extern const struct kernel_symbol __stop___ksymtab
[];
250 extern const struct kernel_symbol __start___ksymtab_gpl
[];
251 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
252 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
253 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
254 extern const unsigned long __start___kcrctab
[];
255 extern const unsigned long __start___kcrctab_gpl
[];
256 extern const unsigned long __start___kcrctab_gpl_future
[];
257 #ifdef CONFIG_UNUSED_SYMBOLS
258 extern const struct kernel_symbol __start___ksymtab_unused
[];
259 extern const struct kernel_symbol __stop___ksymtab_unused
[];
260 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
261 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
262 extern const unsigned long __start___kcrctab_unused
[];
263 extern const unsigned long __start___kcrctab_unused_gpl
[];
266 #ifndef CONFIG_MODVERSIONS
267 #define symversion(base, idx) NULL
269 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
272 static bool each_symbol_in_section(const struct symsearch
*arr
,
273 unsigned int arrsize
,
274 struct module
*owner
,
275 bool (*fn
)(const struct symsearch
*syms
,
276 struct module
*owner
,
282 for (j
= 0; j
< arrsize
; j
++) {
283 if (fn(&arr
[j
], owner
, data
))
290 /* Returns true as soon as fn returns true, otherwise false. */
291 bool each_symbol_section(bool (*fn
)(const struct symsearch
*arr
,
292 struct module
*owner
,
297 static const struct symsearch arr
[] = {
298 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
299 NOT_GPL_ONLY
, false },
300 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
301 __start___kcrctab_gpl
,
303 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
304 __start___kcrctab_gpl_future
,
305 WILL_BE_GPL_ONLY
, false },
306 #ifdef CONFIG_UNUSED_SYMBOLS
307 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
308 __start___kcrctab_unused
,
309 NOT_GPL_ONLY
, true },
310 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
311 __start___kcrctab_unused_gpl
,
316 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
319 list_for_each_entry_rcu(mod
, &modules
, list
) {
320 struct symsearch arr
[] = {
321 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
322 NOT_GPL_ONLY
, false },
323 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
326 { mod
->gpl_future_syms
,
327 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
328 mod
->gpl_future_crcs
,
329 WILL_BE_GPL_ONLY
, false },
330 #ifdef CONFIG_UNUSED_SYMBOLS
332 mod
->unused_syms
+ mod
->num_unused_syms
,
334 NOT_GPL_ONLY
, true },
335 { mod
->unused_gpl_syms
,
336 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
337 mod
->unused_gpl_crcs
,
342 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
347 EXPORT_SYMBOL_GPL(each_symbol_section
);
349 struct find_symbol_arg
{
356 struct module
*owner
;
357 const unsigned long *crc
;
358 const struct kernel_symbol
*sym
;
361 static bool check_symbol(const struct symsearch
*syms
,
362 struct module
*owner
,
363 unsigned int symnum
, void *data
)
365 struct find_symbol_arg
*fsa
= data
;
368 if (syms
->licence
== GPL_ONLY
)
370 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
371 printk(KERN_WARNING
"Symbol %s is being used "
372 "by a non-GPL module, which will not "
373 "be allowed in the future\n", fsa
->name
);
374 printk(KERN_WARNING
"Please see the file "
375 "Documentation/feature-removal-schedule.txt "
376 "in the kernel source tree for more details.\n");
380 #ifdef CONFIG_UNUSED_SYMBOLS
381 if (syms
->unused
&& fsa
->warn
) {
382 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
383 "however this module is using it.\n", fsa
->name
);
385 "This symbol will go away in the future.\n");
387 "Please evalute if this is the right api to use and if "
388 "it really is, submit a report the linux kernel "
389 "mailinglist together with submitting your code for "
395 fsa
->crc
= symversion(syms
->crcs
, symnum
);
396 fsa
->sym
= &syms
->start
[symnum
];
400 static int cmp_name(const void *va
, const void *vb
)
403 const struct kernel_symbol
*b
;
405 return strcmp(a
, b
->name
);
408 static bool find_symbol_in_section(const struct symsearch
*syms
,
409 struct module
*owner
,
412 struct find_symbol_arg
*fsa
= data
;
413 struct kernel_symbol
*sym
;
415 sym
= bsearch(fsa
->name
, syms
->start
, syms
->stop
- syms
->start
,
416 sizeof(struct kernel_symbol
), cmp_name
);
418 if (sym
!= NULL
&& check_symbol(syms
, owner
, sym
- syms
->start
, data
))
424 /* Find a symbol and return it, along with, (optional) crc and
425 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
426 const struct kernel_symbol
*find_symbol(const char *name
,
427 struct module
**owner
,
428 const unsigned long **crc
,
432 struct find_symbol_arg fsa
;
438 if (each_symbol_section(find_symbol_in_section
, &fsa
)) {
446 pr_debug("Failed to find symbol %s\n", name
);
449 EXPORT_SYMBOL_GPL(find_symbol
);
451 /* Search for module by name: must hold module_mutex. */
452 struct module
*find_module(const char *name
)
456 list_for_each_entry(mod
, &modules
, list
) {
457 if (strcmp(mod
->name
, name
) == 0)
462 EXPORT_SYMBOL_GPL(find_module
);
466 static inline void __percpu
*mod_percpu(struct module
*mod
)
471 static int percpu_modalloc(struct module
*mod
,
472 unsigned long size
, unsigned long align
)
474 if (align
> PAGE_SIZE
) {
475 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
476 mod
->name
, align
, PAGE_SIZE
);
480 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
483 "%s: Could not allocate %lu bytes percpu data\n",
487 mod
->percpu_size
= size
;
491 static void percpu_modfree(struct module
*mod
)
493 free_percpu(mod
->percpu
);
496 static unsigned int find_pcpusec(struct load_info
*info
)
498 return find_sec(info
, ".data..percpu");
501 static void percpu_modcopy(struct module
*mod
,
502 const void *from
, unsigned long size
)
506 for_each_possible_cpu(cpu
)
507 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
511 * is_module_percpu_address - test whether address is from module static percpu
512 * @addr: address to test
514 * Test whether @addr belongs to module static percpu area.
517 * %true if @addr is from module static percpu area
519 bool is_module_percpu_address(unsigned long addr
)
526 list_for_each_entry_rcu(mod
, &modules
, list
) {
527 if (!mod
->percpu_size
)
529 for_each_possible_cpu(cpu
) {
530 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
532 if ((void *)addr
>= start
&&
533 (void *)addr
< start
+ mod
->percpu_size
) {
544 #else /* ... !CONFIG_SMP */
546 static inline void __percpu
*mod_percpu(struct module
*mod
)
550 static inline int percpu_modalloc(struct module
*mod
,
551 unsigned long size
, unsigned long align
)
555 static inline void percpu_modfree(struct module
*mod
)
558 static unsigned int find_pcpusec(struct load_info
*info
)
562 static inline void percpu_modcopy(struct module
*mod
,
563 const void *from
, unsigned long size
)
565 /* pcpusec should be 0, and size of that section should be 0. */
568 bool is_module_percpu_address(unsigned long addr
)
573 #endif /* CONFIG_SMP */
575 #define MODINFO_ATTR(field) \
576 static void setup_modinfo_##field(struct module *mod, const char *s) \
578 mod->field = kstrdup(s, GFP_KERNEL); \
580 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
581 struct module_kobject *mk, char *buffer) \
583 return sprintf(buffer, "%s\n", mk->mod->field); \
585 static int modinfo_##field##_exists(struct module *mod) \
587 return mod->field != NULL; \
589 static void free_modinfo_##field(struct module *mod) \
594 static struct module_attribute modinfo_##field = { \
595 .attr = { .name = __stringify(field), .mode = 0444 }, \
596 .show = show_modinfo_##field, \
597 .setup = setup_modinfo_##field, \
598 .test = modinfo_##field##_exists, \
599 .free = free_modinfo_##field, \
602 MODINFO_ATTR(version
);
603 MODINFO_ATTR(srcversion
);
605 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
607 #ifdef CONFIG_MODULE_UNLOAD
609 EXPORT_TRACEPOINT_SYMBOL(module_get
);
611 /* Init the unload section of the module. */
612 static int module_unload_init(struct module
*mod
)
614 mod
->refptr
= alloc_percpu(struct module_ref
);
618 INIT_LIST_HEAD(&mod
->source_list
);
619 INIT_LIST_HEAD(&mod
->target_list
);
621 /* Hold reference count during initialization. */
622 __this_cpu_write(mod
->refptr
->incs
, 1);
623 /* Backwards compatibility macros put refcount during init. */
624 mod
->waiter
= current
;
629 /* Does a already use b? */
630 static int already_uses(struct module
*a
, struct module
*b
)
632 struct module_use
*use
;
634 list_for_each_entry(use
, &b
->source_list
, source_list
) {
635 if (use
->source
== a
) {
636 pr_debug("%s uses %s!\n", a
->name
, b
->name
);
640 pr_debug("%s does not use %s!\n", a
->name
, b
->name
);
646 * - we add 'a' as a "source", 'b' as a "target" of module use
647 * - the module_use is added to the list of 'b' sources (so
648 * 'b' can walk the list to see who sourced them), and of 'a'
649 * targets (so 'a' can see what modules it targets).
651 static int add_module_usage(struct module
*a
, struct module
*b
)
653 struct module_use
*use
;
655 pr_debug("Allocating new usage for %s.\n", a
->name
);
656 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
658 printk(KERN_WARNING
"%s: out of memory loading\n", a
->name
);
664 list_add(&use
->source_list
, &b
->source_list
);
665 list_add(&use
->target_list
, &a
->target_list
);
669 /* Module a uses b: caller needs module_mutex() */
670 int ref_module(struct module
*a
, struct module
*b
)
674 if (b
== NULL
|| already_uses(a
, b
))
677 /* If module isn't available, we fail. */
678 err
= strong_try_module_get(b
);
682 err
= add_module_usage(a
, b
);
689 EXPORT_SYMBOL_GPL(ref_module
);
691 /* Clear the unload stuff of the module. */
692 static void module_unload_free(struct module
*mod
)
694 struct module_use
*use
, *tmp
;
696 mutex_lock(&module_mutex
);
697 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
698 struct module
*i
= use
->target
;
699 pr_debug("%s unusing %s\n", mod
->name
, i
->name
);
701 list_del(&use
->source_list
);
702 list_del(&use
->target_list
);
705 mutex_unlock(&module_mutex
);
707 free_percpu(mod
->refptr
);
710 #ifdef CONFIG_MODULE_FORCE_UNLOAD
711 static inline int try_force_unload(unsigned int flags
)
713 int ret
= (flags
& O_TRUNC
);
715 add_taint(TAINT_FORCED_RMMOD
);
719 static inline int try_force_unload(unsigned int flags
)
723 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
732 /* Whole machine is stopped with interrupts off when this runs. */
733 static int __try_stop_module(void *_sref
)
735 struct stopref
*sref
= _sref
;
737 /* If it's not unused, quit unless we're forcing. */
738 if (module_refcount(sref
->mod
) != 0) {
739 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
743 /* Mark it as dying. */
744 sref
->mod
->state
= MODULE_STATE_GOING
;
748 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
750 if (flags
& O_NONBLOCK
) {
751 struct stopref sref
= { mod
, flags
, forced
};
753 return stop_machine(__try_stop_module
, &sref
, NULL
);
755 /* We don't need to stop the machine for this. */
756 mod
->state
= MODULE_STATE_GOING
;
762 unsigned long module_refcount(struct module
*mod
)
764 unsigned long incs
= 0, decs
= 0;
767 for_each_possible_cpu(cpu
)
768 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
770 * ensure the incs are added up after the decs.
771 * module_put ensures incs are visible before decs with smp_wmb.
773 * This 2-count scheme avoids the situation where the refcount
774 * for CPU0 is read, then CPU0 increments the module refcount,
775 * then CPU1 drops that refcount, then the refcount for CPU1 is
776 * read. We would record a decrement but not its corresponding
777 * increment so we would see a low count (disaster).
779 * Rare situation? But module_refcount can be preempted, and we
780 * might be tallying up 4096+ CPUs. So it is not impossible.
783 for_each_possible_cpu(cpu
)
784 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
787 EXPORT_SYMBOL(module_refcount
);
789 /* This exists whether we can unload or not */
790 static void free_module(struct module
*mod
);
792 static void wait_for_zero_refcount(struct module
*mod
)
794 /* Since we might sleep for some time, release the mutex first */
795 mutex_unlock(&module_mutex
);
797 pr_debug("Looking at refcount...\n");
798 set_current_state(TASK_UNINTERRUPTIBLE
);
799 if (module_refcount(mod
) == 0)
803 current
->state
= TASK_RUNNING
;
804 mutex_lock(&module_mutex
);
807 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
811 char name
[MODULE_NAME_LEN
];
814 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
817 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
819 name
[MODULE_NAME_LEN
-1] = '\0';
821 if (mutex_lock_interruptible(&module_mutex
) != 0)
824 mod
= find_module(name
);
830 if (!list_empty(&mod
->source_list
)) {
831 /* Other modules depend on us: get rid of them first. */
836 /* Doing init or already dying? */
837 if (mod
->state
!= MODULE_STATE_LIVE
) {
838 /* FIXME: if (force), slam module count and wake up
840 pr_debug("%s already dying\n", mod
->name
);
845 /* If it has an init func, it must have an exit func to unload */
846 if (mod
->init
&& !mod
->exit
) {
847 forced
= try_force_unload(flags
);
849 /* This module can't be removed */
855 /* Set this up before setting mod->state */
856 mod
->waiter
= current
;
858 /* Stop the machine so refcounts can't move and disable module. */
859 ret
= try_stop_module(mod
, flags
, &forced
);
863 /* Never wait if forced. */
864 if (!forced
&& module_refcount(mod
) != 0)
865 wait_for_zero_refcount(mod
);
867 mutex_unlock(&module_mutex
);
868 /* Final destruction now no one is using it. */
869 if (mod
->exit
!= NULL
)
871 blocking_notifier_call_chain(&module_notify_list
,
872 MODULE_STATE_GOING
, mod
);
873 async_synchronize_full();
875 /* Store the name of the last unloaded module for diagnostic purposes */
876 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
881 mutex_unlock(&module_mutex
);
885 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
887 struct module_use
*use
;
888 int printed_something
= 0;
890 seq_printf(m
, " %lu ", module_refcount(mod
));
892 /* Always include a trailing , so userspace can differentiate
893 between this and the old multi-field proc format. */
894 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
895 printed_something
= 1;
896 seq_printf(m
, "%s,", use
->source
->name
);
899 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
900 printed_something
= 1;
901 seq_printf(m
, "[permanent],");
904 if (!printed_something
)
908 void __symbol_put(const char *symbol
)
910 struct module
*owner
;
913 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
918 EXPORT_SYMBOL(__symbol_put
);
920 /* Note this assumes addr is a function, which it currently always is. */
921 void symbol_put_addr(void *addr
)
923 struct module
*modaddr
;
924 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
926 if (core_kernel_text(a
))
929 /* module_text_address is safe here: we're supposed to have reference
930 * to module from symbol_get, so it can't go away. */
931 modaddr
= __module_text_address(a
);
935 EXPORT_SYMBOL_GPL(symbol_put_addr
);
937 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
938 struct module_kobject
*mk
, char *buffer
)
940 return sprintf(buffer
, "%lu\n", module_refcount(mk
->mod
));
943 static struct module_attribute modinfo_refcnt
=
944 __ATTR(refcnt
, 0444, show_refcnt
, NULL
);
946 void __module_get(struct module
*module
)
950 __this_cpu_inc(module
->refptr
->incs
);
951 trace_module_get(module
, _RET_IP_
);
955 EXPORT_SYMBOL(__module_get
);
957 bool try_module_get(struct module
*module
)
964 if (likely(module_is_live(module
))) {
965 __this_cpu_inc(module
->refptr
->incs
);
966 trace_module_get(module
, _RET_IP_
);
974 EXPORT_SYMBOL(try_module_get
);
976 void module_put(struct module
*module
)
980 smp_wmb(); /* see comment in module_refcount */
981 __this_cpu_inc(module
->refptr
->decs
);
983 trace_module_put(module
, _RET_IP_
);
984 /* Maybe they're waiting for us to drop reference? */
985 if (unlikely(!module_is_live(module
)))
986 wake_up_process(module
->waiter
);
990 EXPORT_SYMBOL(module_put
);
992 #else /* !CONFIG_MODULE_UNLOAD */
993 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
995 /* We don't know the usage count, or what modules are using. */
996 seq_printf(m
, " - -");
999 static inline void module_unload_free(struct module
*mod
)
1003 int ref_module(struct module
*a
, struct module
*b
)
1005 return strong_try_module_get(b
);
1007 EXPORT_SYMBOL_GPL(ref_module
);
1009 static inline int module_unload_init(struct module
*mod
)
1013 #endif /* CONFIG_MODULE_UNLOAD */
1015 static size_t module_flags_taint(struct module
*mod
, char *buf
)
1019 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
1021 if (mod
->taints
& (1 << TAINT_OOT_MODULE
))
1023 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
1025 if (mod
->taints
& (1 << TAINT_CRAP
))
1028 * TAINT_FORCED_RMMOD: could be added.
1029 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1035 static ssize_t
show_initstate(struct module_attribute
*mattr
,
1036 struct module_kobject
*mk
, char *buffer
)
1038 const char *state
= "unknown";
1040 switch (mk
->mod
->state
) {
1041 case MODULE_STATE_LIVE
:
1044 case MODULE_STATE_COMING
:
1047 case MODULE_STATE_GOING
:
1051 return sprintf(buffer
, "%s\n", state
);
1054 static struct module_attribute modinfo_initstate
=
1055 __ATTR(initstate
, 0444, show_initstate
, NULL
);
1057 static ssize_t
store_uevent(struct module_attribute
*mattr
,
1058 struct module_kobject
*mk
,
1059 const char *buffer
, size_t count
)
1061 enum kobject_action action
;
1063 if (kobject_action_type(buffer
, count
, &action
) == 0)
1064 kobject_uevent(&mk
->kobj
, action
);
1068 struct module_attribute module_uevent
=
1069 __ATTR(uevent
, 0200, NULL
, store_uevent
);
1071 static ssize_t
show_coresize(struct module_attribute
*mattr
,
1072 struct module_kobject
*mk
, char *buffer
)
1074 return sprintf(buffer
, "%u\n", mk
->mod
->core_size
);
1077 static struct module_attribute modinfo_coresize
=
1078 __ATTR(coresize
, 0444, show_coresize
, NULL
);
1080 static ssize_t
show_initsize(struct module_attribute
*mattr
,
1081 struct module_kobject
*mk
, char *buffer
)
1083 return sprintf(buffer
, "%u\n", mk
->mod
->init_size
);
1086 static struct module_attribute modinfo_initsize
=
1087 __ATTR(initsize
, 0444, show_initsize
, NULL
);
1089 static ssize_t
show_taint(struct module_attribute
*mattr
,
1090 struct module_kobject
*mk
, char *buffer
)
1094 l
= module_flags_taint(mk
->mod
, buffer
);
1099 static struct module_attribute modinfo_taint
=
1100 __ATTR(taint
, 0444, show_taint
, NULL
);
1102 static struct module_attribute
*modinfo_attrs
[] = {
1105 &modinfo_srcversion
,
1110 #ifdef CONFIG_MODULE_UNLOAD
1116 static const char vermagic
[] = VERMAGIC_STRING
;
1118 static int try_to_force_load(struct module
*mod
, const char *reason
)
1120 #ifdef CONFIG_MODULE_FORCE_LOAD
1121 if (!test_taint(TAINT_FORCED_MODULE
))
1122 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
1124 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1131 #ifdef CONFIG_MODVERSIONS
1132 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1133 static unsigned long maybe_relocated(unsigned long crc
,
1134 const struct module
*crc_owner
)
1136 #ifdef ARCH_RELOCATES_KCRCTAB
1137 if (crc_owner
== NULL
)
1138 return crc
- (unsigned long)reloc_start
;
1143 static int check_version(Elf_Shdr
*sechdrs
,
1144 unsigned int versindex
,
1145 const char *symname
,
1147 const unsigned long *crc
,
1148 const struct module
*crc_owner
)
1150 unsigned int i
, num_versions
;
1151 struct modversion_info
*versions
;
1153 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1157 /* No versions at all? modprobe --force does this. */
1159 return try_to_force_load(mod
, symname
) == 0;
1161 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1162 num_versions
= sechdrs
[versindex
].sh_size
1163 / sizeof(struct modversion_info
);
1165 for (i
= 0; i
< num_versions
; i
++) {
1166 if (strcmp(versions
[i
].name
, symname
) != 0)
1169 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
1171 pr_debug("Found checksum %lX vs module %lX\n",
1172 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
1176 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1177 mod
->name
, symname
);
1181 printk("%s: disagrees about version of symbol %s\n",
1182 mod
->name
, symname
);
1186 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1187 unsigned int versindex
,
1190 const unsigned long *crc
;
1192 /* Since this should be found in kernel (which can't be removed),
1193 * no locking is necessary. */
1194 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1197 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1201 /* First part is kernel version, which we ignore if module has crcs. */
1202 static inline int same_magic(const char *amagic
, const char *bmagic
,
1206 amagic
+= strcspn(amagic
, " ");
1207 bmagic
+= strcspn(bmagic
, " ");
1209 return strcmp(amagic
, bmagic
) == 0;
1212 static inline int check_version(Elf_Shdr
*sechdrs
,
1213 unsigned int versindex
,
1214 const char *symname
,
1216 const unsigned long *crc
,
1217 const struct module
*crc_owner
)
1222 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1223 unsigned int versindex
,
1229 static inline int same_magic(const char *amagic
, const char *bmagic
,
1232 return strcmp(amagic
, bmagic
) == 0;
1234 #endif /* CONFIG_MODVERSIONS */
1236 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1237 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1238 const struct load_info
*info
,
1242 struct module
*owner
;
1243 const struct kernel_symbol
*sym
;
1244 const unsigned long *crc
;
1247 mutex_lock(&module_mutex
);
1248 sym
= find_symbol(name
, &owner
, &crc
,
1249 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1253 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
,
1255 sym
= ERR_PTR(-EINVAL
);
1259 err
= ref_module(mod
, owner
);
1266 /* We must make copy under the lock if we failed to get ref. */
1267 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1269 mutex_unlock(&module_mutex
);
1273 static const struct kernel_symbol
*
1274 resolve_symbol_wait(struct module
*mod
,
1275 const struct load_info
*info
,
1278 const struct kernel_symbol
*ksym
;
1279 char owner
[MODULE_NAME_LEN
];
1281 if (wait_event_interruptible_timeout(module_wq
,
1282 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1283 || PTR_ERR(ksym
) != -EBUSY
,
1285 printk(KERN_WARNING
"%s: gave up waiting for init of module %s.\n",
1292 * /sys/module/foo/sections stuff
1293 * J. Corbet <corbet@lwn.net>
1297 #ifdef CONFIG_KALLSYMS
1298 static inline bool sect_empty(const Elf_Shdr
*sect
)
1300 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1303 struct module_sect_attr
1305 struct module_attribute mattr
;
1307 unsigned long address
;
1310 struct module_sect_attrs
1312 struct attribute_group grp
;
1313 unsigned int nsections
;
1314 struct module_sect_attr attrs
[0];
1317 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1318 struct module_kobject
*mk
, char *buf
)
1320 struct module_sect_attr
*sattr
=
1321 container_of(mattr
, struct module_sect_attr
, mattr
);
1322 return sprintf(buf
, "0x%pK\n", (void *)sattr
->address
);
1325 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1327 unsigned int section
;
1329 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1330 kfree(sect_attrs
->attrs
[section
].name
);
1334 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1336 unsigned int nloaded
= 0, i
, size
[2];
1337 struct module_sect_attrs
*sect_attrs
;
1338 struct module_sect_attr
*sattr
;
1339 struct attribute
**gattr
;
1341 /* Count loaded sections and allocate structures */
1342 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1343 if (!sect_empty(&info
->sechdrs
[i
]))
1345 size
[0] = ALIGN(sizeof(*sect_attrs
)
1346 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1347 sizeof(sect_attrs
->grp
.attrs
[0]));
1348 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1349 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1350 if (sect_attrs
== NULL
)
1353 /* Setup section attributes. */
1354 sect_attrs
->grp
.name
= "sections";
1355 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1357 sect_attrs
->nsections
= 0;
1358 sattr
= §_attrs
->attrs
[0];
1359 gattr
= §_attrs
->grp
.attrs
[0];
1360 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1361 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1362 if (sect_empty(sec
))
1364 sattr
->address
= sec
->sh_addr
;
1365 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1367 if (sattr
->name
== NULL
)
1369 sect_attrs
->nsections
++;
1370 sysfs_attr_init(&sattr
->mattr
.attr
);
1371 sattr
->mattr
.show
= module_sect_show
;
1372 sattr
->mattr
.store
= NULL
;
1373 sattr
->mattr
.attr
.name
= sattr
->name
;
1374 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1375 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1379 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1382 mod
->sect_attrs
= sect_attrs
;
1385 free_sect_attrs(sect_attrs
);
1388 static void remove_sect_attrs(struct module
*mod
)
1390 if (mod
->sect_attrs
) {
1391 sysfs_remove_group(&mod
->mkobj
.kobj
,
1392 &mod
->sect_attrs
->grp
);
1393 /* We are positive that no one is using any sect attrs
1394 * at this point. Deallocate immediately. */
1395 free_sect_attrs(mod
->sect_attrs
);
1396 mod
->sect_attrs
= NULL
;
1401 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1404 struct module_notes_attrs
{
1405 struct kobject
*dir
;
1407 struct bin_attribute attrs
[0];
1410 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1411 struct bin_attribute
*bin_attr
,
1412 char *buf
, loff_t pos
, size_t count
)
1415 * The caller checked the pos and count against our size.
1417 memcpy(buf
, bin_attr
->private + pos
, count
);
1421 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1424 if (notes_attrs
->dir
) {
1426 sysfs_remove_bin_file(notes_attrs
->dir
,
1427 ¬es_attrs
->attrs
[i
]);
1428 kobject_put(notes_attrs
->dir
);
1433 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1435 unsigned int notes
, loaded
, i
;
1436 struct module_notes_attrs
*notes_attrs
;
1437 struct bin_attribute
*nattr
;
1439 /* failed to create section attributes, so can't create notes */
1440 if (!mod
->sect_attrs
)
1443 /* Count notes sections and allocate structures. */
1445 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1446 if (!sect_empty(&info
->sechdrs
[i
]) &&
1447 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1453 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1454 + notes
* sizeof(notes_attrs
->attrs
[0]),
1456 if (notes_attrs
== NULL
)
1459 notes_attrs
->notes
= notes
;
1460 nattr
= ¬es_attrs
->attrs
[0];
1461 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1462 if (sect_empty(&info
->sechdrs
[i
]))
1464 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1465 sysfs_bin_attr_init(nattr
);
1466 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1467 nattr
->attr
.mode
= S_IRUGO
;
1468 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1469 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1470 nattr
->read
= module_notes_read
;
1476 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1477 if (!notes_attrs
->dir
)
1480 for (i
= 0; i
< notes
; ++i
)
1481 if (sysfs_create_bin_file(notes_attrs
->dir
,
1482 ¬es_attrs
->attrs
[i
]))
1485 mod
->notes_attrs
= notes_attrs
;
1489 free_notes_attrs(notes_attrs
, i
);
1492 static void remove_notes_attrs(struct module
*mod
)
1494 if (mod
->notes_attrs
)
1495 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1500 static inline void add_sect_attrs(struct module
*mod
,
1501 const struct load_info
*info
)
1505 static inline void remove_sect_attrs(struct module
*mod
)
1509 static inline void add_notes_attrs(struct module
*mod
,
1510 const struct load_info
*info
)
1514 static inline void remove_notes_attrs(struct module
*mod
)
1517 #endif /* CONFIG_KALLSYMS */
1519 static void add_usage_links(struct module
*mod
)
1521 #ifdef CONFIG_MODULE_UNLOAD
1522 struct module_use
*use
;
1525 mutex_lock(&module_mutex
);
1526 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1527 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1528 &mod
->mkobj
.kobj
, mod
->name
);
1530 mutex_unlock(&module_mutex
);
1534 static void del_usage_links(struct module
*mod
)
1536 #ifdef CONFIG_MODULE_UNLOAD
1537 struct module_use
*use
;
1539 mutex_lock(&module_mutex
);
1540 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1541 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1542 mutex_unlock(&module_mutex
);
1546 static int module_add_modinfo_attrs(struct module
*mod
)
1548 struct module_attribute
*attr
;
1549 struct module_attribute
*temp_attr
;
1553 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1554 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1556 if (!mod
->modinfo_attrs
)
1559 temp_attr
= mod
->modinfo_attrs
;
1560 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1562 (attr
->test
&& attr
->test(mod
))) {
1563 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1564 sysfs_attr_init(&temp_attr
->attr
);
1565 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1572 static void module_remove_modinfo_attrs(struct module
*mod
)
1574 struct module_attribute
*attr
;
1577 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1578 /* pick a field to test for end of list */
1579 if (!attr
->attr
.name
)
1581 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1585 kfree(mod
->modinfo_attrs
);
1588 static int mod_sysfs_init(struct module
*mod
)
1591 struct kobject
*kobj
;
1593 if (!module_sysfs_initialized
) {
1594 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1600 kobj
= kset_find_obj(module_kset
, mod
->name
);
1602 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1608 mod
->mkobj
.mod
= mod
;
1610 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1611 mod
->mkobj
.kobj
.kset
= module_kset
;
1612 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1615 kobject_put(&mod
->mkobj
.kobj
);
1617 /* delay uevent until full sysfs population */
1622 static int mod_sysfs_setup(struct module
*mod
,
1623 const struct load_info
*info
,
1624 struct kernel_param
*kparam
,
1625 unsigned int num_params
)
1629 err
= mod_sysfs_init(mod
);
1633 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1634 if (!mod
->holders_dir
) {
1639 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1641 goto out_unreg_holders
;
1643 err
= module_add_modinfo_attrs(mod
);
1645 goto out_unreg_param
;
1647 add_usage_links(mod
);
1648 add_sect_attrs(mod
, info
);
1649 add_notes_attrs(mod
, info
);
1651 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1655 module_param_sysfs_remove(mod
);
1657 kobject_put(mod
->holders_dir
);
1659 kobject_put(&mod
->mkobj
.kobj
);
1664 static void mod_sysfs_fini(struct module
*mod
)
1666 remove_notes_attrs(mod
);
1667 remove_sect_attrs(mod
);
1668 kobject_put(&mod
->mkobj
.kobj
);
1671 #else /* !CONFIG_SYSFS */
1673 static int mod_sysfs_setup(struct module
*mod
,
1674 const struct load_info
*info
,
1675 struct kernel_param
*kparam
,
1676 unsigned int num_params
)
1681 static void mod_sysfs_fini(struct module
*mod
)
1685 static void module_remove_modinfo_attrs(struct module
*mod
)
1689 static void del_usage_links(struct module
*mod
)
1693 #endif /* CONFIG_SYSFS */
1695 static void mod_sysfs_teardown(struct module
*mod
)
1697 del_usage_links(mod
);
1698 module_remove_modinfo_attrs(mod
);
1699 module_param_sysfs_remove(mod
);
1700 kobject_put(mod
->mkobj
.drivers_dir
);
1701 kobject_put(mod
->holders_dir
);
1702 mod_sysfs_fini(mod
);
1706 * unlink the module with the whole machine is stopped with interrupts off
1707 * - this defends against kallsyms not taking locks
1709 static int __unlink_module(void *_mod
)
1711 struct module
*mod
= _mod
;
1712 list_del(&mod
->list
);
1713 module_bug_cleanup(mod
);
1717 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1719 * LKM RO/NX protection: protect module's text/ro-data
1720 * from modification and any data from execution.
1722 void set_page_attributes(void *start
, void *end
, int (*set
)(unsigned long start
, int num_pages
))
1724 unsigned long begin_pfn
= PFN_DOWN((unsigned long)start
);
1725 unsigned long end_pfn
= PFN_DOWN((unsigned long)end
);
1727 if (end_pfn
> begin_pfn
)
1728 set(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1731 static void set_section_ro_nx(void *base
,
1732 unsigned long text_size
,
1733 unsigned long ro_size
,
1734 unsigned long total_size
)
1736 /* begin and end PFNs of the current subsection */
1737 unsigned long begin_pfn
;
1738 unsigned long end_pfn
;
1741 * Set RO for module text and RO-data:
1742 * - Always protect first page.
1743 * - Do not protect last partial page.
1746 set_page_attributes(base
, base
+ ro_size
, set_memory_ro
);
1749 * Set NX permissions for module data:
1750 * - Do not protect first partial page.
1751 * - Always protect last page.
1753 if (total_size
> text_size
) {
1754 begin_pfn
= PFN_UP((unsigned long)base
+ text_size
);
1755 end_pfn
= PFN_UP((unsigned long)base
+ total_size
);
1756 if (end_pfn
> begin_pfn
)
1757 set_memory_nx(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1761 static void unset_module_core_ro_nx(struct module
*mod
)
1763 set_page_attributes(mod
->module_core
+ mod
->core_text_size
,
1764 mod
->module_core
+ mod
->core_size
,
1766 set_page_attributes(mod
->module_core
,
1767 mod
->module_core
+ mod
->core_ro_size
,
1771 static void unset_module_init_ro_nx(struct module
*mod
)
1773 set_page_attributes(mod
->module_init
+ mod
->init_text_size
,
1774 mod
->module_init
+ mod
->init_size
,
1776 set_page_attributes(mod
->module_init
,
1777 mod
->module_init
+ mod
->init_ro_size
,
1781 /* Iterate through all modules and set each module's text as RW */
1782 void set_all_modules_text_rw(void)
1786 mutex_lock(&module_mutex
);
1787 list_for_each_entry_rcu(mod
, &modules
, list
) {
1788 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1789 set_page_attributes(mod
->module_core
,
1790 mod
->module_core
+ mod
->core_text_size
,
1793 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1794 set_page_attributes(mod
->module_init
,
1795 mod
->module_init
+ mod
->init_text_size
,
1799 mutex_unlock(&module_mutex
);
1802 /* Iterate through all modules and set each module's text as RO */
1803 void set_all_modules_text_ro(void)
1807 mutex_lock(&module_mutex
);
1808 list_for_each_entry_rcu(mod
, &modules
, list
) {
1809 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1810 set_page_attributes(mod
->module_core
,
1811 mod
->module_core
+ mod
->core_text_size
,
1814 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1815 set_page_attributes(mod
->module_init
,
1816 mod
->module_init
+ mod
->init_text_size
,
1820 mutex_unlock(&module_mutex
);
1823 static inline void set_section_ro_nx(void *base
, unsigned long text_size
, unsigned long ro_size
, unsigned long total_size
) { }
1824 static void unset_module_core_ro_nx(struct module
*mod
) { }
1825 static void unset_module_init_ro_nx(struct module
*mod
) { }
1828 void __weak
module_free(struct module
*mod
, void *module_region
)
1830 vfree(module_region
);
1833 void __weak
module_arch_cleanup(struct module
*mod
)
1837 /* Free a module, remove from lists, etc. */
1838 static void free_module(struct module
*mod
)
1840 trace_module_free(mod
);
1842 /* Delete from various lists */
1843 mutex_lock(&module_mutex
);
1844 stop_machine(__unlink_module
, mod
, NULL
);
1845 mutex_unlock(&module_mutex
);
1846 mod_sysfs_teardown(mod
);
1848 /* Remove dynamic debug info */
1849 ddebug_remove_module(mod
->name
);
1851 /* Arch-specific cleanup. */
1852 module_arch_cleanup(mod
);
1854 /* Module unload stuff */
1855 module_unload_free(mod
);
1857 /* Free any allocated parameters. */
1858 destroy_params(mod
->kp
, mod
->num_kp
);
1860 /* This may be NULL, but that's OK */
1861 unset_module_init_ro_nx(mod
);
1862 module_free(mod
, mod
->module_init
);
1864 percpu_modfree(mod
);
1866 /* Free lock-classes: */
1867 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1869 /* Finally, free the core (containing the module structure) */
1870 unset_module_core_ro_nx(mod
);
1871 module_free(mod
, mod
->module_core
);
1874 update_protections(current
->mm
);
1878 void *__symbol_get(const char *symbol
)
1880 struct module
*owner
;
1881 const struct kernel_symbol
*sym
;
1884 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1885 if (sym
&& strong_try_module_get(owner
))
1889 return sym
? (void *)sym
->value
: NULL
;
1891 EXPORT_SYMBOL_GPL(__symbol_get
);
1894 * Ensure that an exported symbol [global namespace] does not already exist
1895 * in the kernel or in some other module's exported symbol table.
1897 * You must hold the module_mutex.
1899 static int verify_export_symbols(struct module
*mod
)
1902 struct module
*owner
;
1903 const struct kernel_symbol
*s
;
1905 const struct kernel_symbol
*sym
;
1908 { mod
->syms
, mod
->num_syms
},
1909 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1910 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1911 #ifdef CONFIG_UNUSED_SYMBOLS
1912 { mod
->unused_syms
, mod
->num_unused_syms
},
1913 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1917 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1918 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1919 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1921 "%s: exports duplicate symbol %s"
1923 mod
->name
, s
->name
, module_name(owner
));
1931 /* Change all symbols so that st_value encodes the pointer directly. */
1932 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
1934 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
1935 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
1936 unsigned long secbase
;
1939 const struct kernel_symbol
*ksym
;
1941 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
1942 const char *name
= info
->strtab
+ sym
[i
].st_name
;
1944 switch (sym
[i
].st_shndx
) {
1946 /* We compiled with -fno-common. These are not
1947 supposed to happen. */
1948 pr_debug("Common symbol: %s\n", name
);
1949 printk("%s: please compile with -fno-common\n",
1955 /* Don't need to do anything */
1956 pr_debug("Absolute symbol: 0x%08lx\n",
1957 (long)sym
[i
].st_value
);
1961 ksym
= resolve_symbol_wait(mod
, info
, name
);
1962 /* Ok if resolved. */
1963 if (ksym
&& !IS_ERR(ksym
)) {
1964 sym
[i
].st_value
= ksym
->value
;
1969 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1972 printk(KERN_WARNING
"%s: Unknown symbol %s (err %li)\n",
1973 mod
->name
, name
, PTR_ERR(ksym
));
1974 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
1978 /* Divert to percpu allocation if a percpu var. */
1979 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
1980 secbase
= (unsigned long)mod_percpu(mod
);
1982 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1983 sym
[i
].st_value
+= secbase
;
1991 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
1996 /* Now do relocations. */
1997 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
1998 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
2000 /* Not a valid relocation section? */
2001 if (infosec
>= info
->hdr
->e_shnum
)
2004 /* Don't bother with non-allocated sections */
2005 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
2008 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
2009 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
2010 info
->index
.sym
, i
, mod
);
2011 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
2012 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
2013 info
->index
.sym
, i
, mod
);
2020 /* Additional bytes needed by arch in front of individual sections */
2021 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
2022 unsigned int section
)
2024 /* default implementation just returns zero */
2028 /* Update size with this section: return offset. */
2029 static long get_offset(struct module
*mod
, unsigned int *size
,
2030 Elf_Shdr
*sechdr
, unsigned int section
)
2034 *size
+= arch_mod_section_prepend(mod
, section
);
2035 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
2036 *size
= ret
+ sechdr
->sh_size
;
2040 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2041 might -- code, read-only data, read-write data, small data. Tally
2042 sizes, and place the offsets into sh_entsize fields: high bit means it
2044 static void layout_sections(struct module
*mod
, struct load_info
*info
)
2046 static unsigned long const masks
[][2] = {
2047 /* NOTE: all executable code must be the first section
2048 * in this array; otherwise modify the text_size
2049 * finder in the two loops below */
2050 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2051 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
2052 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
2053 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
2057 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
2058 info
->sechdrs
[i
].sh_entsize
= ~0UL;
2060 pr_debug("Core section allocation order:\n");
2061 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2062 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2063 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2064 const char *sname
= info
->secstrings
+ s
->sh_name
;
2066 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2067 || (s
->sh_flags
& masks
[m
][1])
2068 || s
->sh_entsize
!= ~0UL
2069 || strstarts(sname
, ".init"))
2071 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
2072 pr_debug("\t%s\n", sname
);
2075 case 0: /* executable */
2076 mod
->core_size
= debug_align(mod
->core_size
);
2077 mod
->core_text_size
= mod
->core_size
;
2079 case 1: /* RO: text and ro-data */
2080 mod
->core_size
= debug_align(mod
->core_size
);
2081 mod
->core_ro_size
= mod
->core_size
;
2083 case 3: /* whole core */
2084 mod
->core_size
= debug_align(mod
->core_size
);
2089 pr_debug("Init section allocation order:\n");
2090 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
2091 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
2092 Elf_Shdr
*s
= &info
->sechdrs
[i
];
2093 const char *sname
= info
->secstrings
+ s
->sh_name
;
2095 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
2096 || (s
->sh_flags
& masks
[m
][1])
2097 || s
->sh_entsize
!= ~0UL
2098 || !strstarts(sname
, ".init"))
2100 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
2101 | INIT_OFFSET_MASK
);
2102 pr_debug("\t%s\n", sname
);
2105 case 0: /* executable */
2106 mod
->init_size
= debug_align(mod
->init_size
);
2107 mod
->init_text_size
= mod
->init_size
;
2109 case 1: /* RO: text and ro-data */
2110 mod
->init_size
= debug_align(mod
->init_size
);
2111 mod
->init_ro_size
= mod
->init_size
;
2113 case 3: /* whole init */
2114 mod
->init_size
= debug_align(mod
->init_size
);
2120 static void set_license(struct module
*mod
, const char *license
)
2123 license
= "unspecified";
2125 if (!license_is_gpl_compatible(license
)) {
2126 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
2127 printk(KERN_WARNING
"%s: module license '%s' taints "
2128 "kernel.\n", mod
->name
, license
);
2129 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2133 /* Parse tag=value strings from .modinfo section */
2134 static char *next_string(char *string
, unsigned long *secsize
)
2136 /* Skip non-zero chars */
2139 if ((*secsize
)-- <= 1)
2143 /* Skip any zero padding. */
2144 while (!string
[0]) {
2146 if ((*secsize
)-- <= 1)
2152 static char *get_modinfo(struct load_info
*info
, const char *tag
)
2155 unsigned int taglen
= strlen(tag
);
2156 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
2157 unsigned long size
= infosec
->sh_size
;
2159 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
2160 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
2161 return p
+ taglen
+ 1;
2166 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2168 struct module_attribute
*attr
;
2171 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2173 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2177 static void free_modinfo(struct module
*mod
)
2179 struct module_attribute
*attr
;
2182 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2188 #ifdef CONFIG_KALLSYMS
2190 /* lookup symbol in given range of kernel_symbols */
2191 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2192 const struct kernel_symbol
*start
,
2193 const struct kernel_symbol
*stop
)
2195 return bsearch(name
, start
, stop
- start
,
2196 sizeof(struct kernel_symbol
), cmp_name
);
2199 static int is_exported(const char *name
, unsigned long value
,
2200 const struct module
*mod
)
2202 const struct kernel_symbol
*ks
;
2204 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2206 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2207 return ks
!= NULL
&& ks
->value
== value
;
2211 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2213 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2215 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2216 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2221 if (sym
->st_shndx
== SHN_UNDEF
)
2223 if (sym
->st_shndx
== SHN_ABS
)
2225 if (sym
->st_shndx
>= SHN_LORESERVE
)
2227 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2229 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2230 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2231 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2233 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2238 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2239 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2244 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2251 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2254 const Elf_Shdr
*sec
;
2256 if (src
->st_shndx
== SHN_UNDEF
2257 || src
->st_shndx
>= shnum
2261 sec
= sechdrs
+ src
->st_shndx
;
2262 if (!(sec
->sh_flags
& SHF_ALLOC
)
2263 #ifndef CONFIG_KALLSYMS_ALL
2264 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2266 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2273 * We only allocate and copy the strings needed by the parts of symtab
2274 * we keep. This is simple, but has the effect of making multiple
2275 * copies of duplicates. We could be more sophisticated, see
2276 * linux-kernel thread starting with
2277 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2279 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2281 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2282 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2284 unsigned int i
, nsrc
, ndst
, strtab_size
;
2286 /* Put symbol section at end of init part of module. */
2287 symsect
->sh_flags
|= SHF_ALLOC
;
2288 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
2289 info
->index
.sym
) | INIT_OFFSET_MASK
;
2290 pr_debug("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2292 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2293 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2295 /* Compute total space required for the core symbols' strtab. */
2296 for (ndst
= i
= strtab_size
= 1; i
< nsrc
; ++i
, ++src
)
2297 if (is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
2298 strtab_size
+= strlen(&info
->strtab
[src
->st_name
]) + 1;
2302 /* Append room for core symbols at end of core part. */
2303 info
->symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
2304 info
->stroffs
= mod
->core_size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2305 mod
->core_size
+= strtab_size
;
2307 /* Put string table section at end of init part of module. */
2308 strsect
->sh_flags
|= SHF_ALLOC
;
2309 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
2310 info
->index
.str
) | INIT_OFFSET_MASK
;
2311 pr_debug("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2314 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2316 unsigned int i
, ndst
;
2320 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2322 mod
->symtab
= (void *)symsec
->sh_addr
;
2323 mod
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2324 /* Make sure we get permanent strtab: don't use info->strtab. */
2325 mod
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2327 /* Set types up while we still have access to sections. */
2328 for (i
= 0; i
< mod
->num_symtab
; i
++)
2329 mod
->symtab
[i
].st_info
= elf_type(&mod
->symtab
[i
], info
);
2331 mod
->core_symtab
= dst
= mod
->module_core
+ info
->symoffs
;
2332 mod
->core_strtab
= s
= mod
->module_core
+ info
->stroffs
;
2336 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
2337 if (!is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
))
2341 dst
[ndst
++].st_name
= s
- mod
->core_strtab
;
2342 s
+= strlcpy(s
, &mod
->strtab
[src
->st_name
], KSYM_NAME_LEN
) + 1;
2344 mod
->core_num_syms
= ndst
;
2347 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2351 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2354 #endif /* CONFIG_KALLSYMS */
2356 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2360 #ifdef CONFIG_DYNAMIC_DEBUG
2361 if (ddebug_add_module(debug
, num
, debug
->modname
))
2362 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
2367 static void dynamic_debug_remove(struct _ddebug
*debug
)
2370 ddebug_remove_module(debug
->modname
);
2373 void * __weak
module_alloc(unsigned long size
)
2375 return size
== 0 ? NULL
: vmalloc_exec(size
);
2378 static void *module_alloc_update_bounds(unsigned long size
)
2380 void *ret
= module_alloc(size
);
2383 mutex_lock(&module_mutex
);
2384 /* Update module bounds. */
2385 if ((unsigned long)ret
< module_addr_min
)
2386 module_addr_min
= (unsigned long)ret
;
2387 if ((unsigned long)ret
+ size
> module_addr_max
)
2388 module_addr_max
= (unsigned long)ret
+ size
;
2389 mutex_unlock(&module_mutex
);
2394 #ifdef CONFIG_DEBUG_KMEMLEAK
2395 static void kmemleak_load_module(const struct module
*mod
,
2396 const struct load_info
*info
)
2400 /* only scan the sections containing data */
2401 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2403 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2404 const char *name
= info
->secstrings
+ info
->sechdrs
[i
].sh_name
;
2405 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2407 if (!strstarts(name
, ".data") && !strstarts(name
, ".bss"))
2410 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2411 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2415 static inline void kmemleak_load_module(const struct module
*mod
,
2416 const struct load_info
*info
)
2421 #ifdef CONFIG_MODULE_SIG
2422 static int module_sig_check(struct load_info
*info
,
2423 const void *mod
, unsigned long *len
)
2426 const unsigned long markerlen
= sizeof(MODULE_SIG_STRING
) - 1;
2427 const void *p
= mod
, *end
= mod
+ *len
;
2429 /* Poor man's memmem. */
2430 while ((p
= memchr(p
, MODULE_SIG_STRING
[0], end
- p
))) {
2431 if (p
+ markerlen
> end
)
2434 if (memcmp(p
, MODULE_SIG_STRING
, markerlen
) == 0) {
2435 const void *sig
= p
+ markerlen
;
2436 /* Truncate module up to signature. */
2438 err
= mod_verify_sig(mod
, *len
, sig
, end
- sig
);
2445 info
->sig_ok
= true;
2449 /* Not having a signature is only an error if we're strict. */
2450 if (err
== -ENOKEY
&& !sig_enforce
)
2455 #else /* !CONFIG_MODULE_SIG */
2456 static int module_sig_check(struct load_info
*info
,
2457 void *mod
, unsigned long *len
)
2461 #endif /* !CONFIG_MODULE_SIG */
2463 /* Sets info->hdr, info->len and info->sig_ok. */
2464 static int copy_and_check(struct load_info
*info
,
2465 const void __user
*umod
, unsigned long len
,
2466 const char __user
*uargs
)
2471 if (len
< sizeof(*hdr
))
2474 /* Suck in entire file: we'll want most of it. */
2475 if ((hdr
= vmalloc(len
)) == NULL
)
2478 if (copy_from_user(hdr
, umod
, len
) != 0) {
2483 err
= module_sig_check(info
, hdr
, &len
);
2487 /* Sanity checks against insmoding binaries or wrong arch,
2488 weird elf version */
2489 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2490 || hdr
->e_type
!= ET_REL
2491 || !elf_check_arch(hdr
)
2492 || hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
2497 if (hdr
->e_shoff
>= len
||
2498 hdr
->e_shnum
* sizeof(Elf_Shdr
) > len
- hdr
->e_shoff
) {
2512 static void free_copy(struct load_info
*info
)
2517 static int rewrite_section_headers(struct load_info
*info
)
2521 /* This should always be true, but let's be sure. */
2522 info
->sechdrs
[0].sh_addr
= 0;
2524 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2525 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2526 if (shdr
->sh_type
!= SHT_NOBITS
2527 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2528 printk(KERN_ERR
"Module len %lu truncated\n",
2533 /* Mark all sections sh_addr with their address in the
2535 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2537 #ifndef CONFIG_MODULE_UNLOAD
2538 /* Don't load .exit sections */
2539 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2540 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2544 /* Track but don't keep modinfo and version sections. */
2545 info
->index
.vers
= find_sec(info
, "__versions");
2546 info
->index
.info
= find_sec(info
, ".modinfo");
2547 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2548 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2553 * Set up our basic convenience variables (pointers to section headers,
2554 * search for module section index etc), and do some basic section
2557 * Return the temporary module pointer (we'll replace it with the final
2558 * one when we move the module sections around).
2560 static struct module
*setup_load_info(struct load_info
*info
)
2566 /* Set up the convenience variables */
2567 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2568 info
->secstrings
= (void *)info
->hdr
2569 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2571 err
= rewrite_section_headers(info
);
2573 return ERR_PTR(err
);
2575 /* Find internal symbols and strings. */
2576 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2577 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2578 info
->index
.sym
= i
;
2579 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2580 info
->strtab
= (char *)info
->hdr
2581 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2586 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2587 if (!info
->index
.mod
) {
2588 printk(KERN_WARNING
"No module found in object\n");
2589 return ERR_PTR(-ENOEXEC
);
2591 /* This is temporary: point mod into copy of data. */
2592 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2594 if (info
->index
.sym
== 0) {
2595 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2597 return ERR_PTR(-ENOEXEC
);
2600 info
->index
.pcpu
= find_pcpusec(info
);
2602 /* Check module struct version now, before we try to use module. */
2603 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2604 return ERR_PTR(-ENOEXEC
);
2609 static int check_modinfo(struct module
*mod
, struct load_info
*info
)
2611 const char *modmagic
= get_modinfo(info
, "vermagic");
2614 /* This is allowed: modprobe --force will invalidate it. */
2616 err
= try_to_force_load(mod
, "bad vermagic");
2619 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2620 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2621 mod
->name
, modmagic
, vermagic
);
2625 if (!get_modinfo(info
, "intree"))
2626 add_taint_module(mod
, TAINT_OOT_MODULE
);
2628 if (get_modinfo(info
, "staging")) {
2629 add_taint_module(mod
, TAINT_CRAP
);
2630 printk(KERN_WARNING
"%s: module is from the staging directory,"
2631 " the quality is unknown, you have been warned.\n",
2635 /* Set up license info based on the info section */
2636 set_license(mod
, get_modinfo(info
, "license"));
2641 static void find_module_sections(struct module
*mod
, struct load_info
*info
)
2643 mod
->kp
= section_objs(info
, "__param",
2644 sizeof(*mod
->kp
), &mod
->num_kp
);
2645 mod
->syms
= section_objs(info
, "__ksymtab",
2646 sizeof(*mod
->syms
), &mod
->num_syms
);
2647 mod
->crcs
= section_addr(info
, "__kcrctab");
2648 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
2649 sizeof(*mod
->gpl_syms
),
2650 &mod
->num_gpl_syms
);
2651 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
2652 mod
->gpl_future_syms
= section_objs(info
,
2653 "__ksymtab_gpl_future",
2654 sizeof(*mod
->gpl_future_syms
),
2655 &mod
->num_gpl_future_syms
);
2656 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
2658 #ifdef CONFIG_UNUSED_SYMBOLS
2659 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
2660 sizeof(*mod
->unused_syms
),
2661 &mod
->num_unused_syms
);
2662 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
2663 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
2664 sizeof(*mod
->unused_gpl_syms
),
2665 &mod
->num_unused_gpl_syms
);
2666 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
2668 #ifdef CONFIG_CONSTRUCTORS
2669 mod
->ctors
= section_objs(info
, ".ctors",
2670 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2673 #ifdef CONFIG_TRACEPOINTS
2674 mod
->tracepoints_ptrs
= section_objs(info
, "__tracepoints_ptrs",
2675 sizeof(*mod
->tracepoints_ptrs
),
2676 &mod
->num_tracepoints
);
2678 #ifdef HAVE_JUMP_LABEL
2679 mod
->jump_entries
= section_objs(info
, "__jump_table",
2680 sizeof(*mod
->jump_entries
),
2681 &mod
->num_jump_entries
);
2683 #ifdef CONFIG_EVENT_TRACING
2684 mod
->trace_events
= section_objs(info
, "_ftrace_events",
2685 sizeof(*mod
->trace_events
),
2686 &mod
->num_trace_events
);
2688 * This section contains pointers to allocated objects in the trace
2689 * code and not scanning it leads to false positives.
2691 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2692 mod
->num_trace_events
, GFP_KERNEL
);
2694 #ifdef CONFIG_TRACING
2695 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
2696 sizeof(*mod
->trace_bprintk_fmt_start
),
2697 &mod
->num_trace_bprintk_fmt
);
2699 * This section contains pointers to allocated objects in the trace
2700 * code and not scanning it leads to false positives.
2702 kmemleak_scan_area(mod
->trace_bprintk_fmt_start
,
2703 sizeof(*mod
->trace_bprintk_fmt_start
) *
2704 mod
->num_trace_bprintk_fmt
, GFP_KERNEL
);
2706 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2707 /* sechdrs[0].sh_size is always zero */
2708 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
2709 sizeof(*mod
->ftrace_callsites
),
2710 &mod
->num_ftrace_callsites
);
2713 mod
->extable
= section_objs(info
, "__ex_table",
2714 sizeof(*mod
->extable
), &mod
->num_exentries
);
2716 if (section_addr(info
, "__obsparm"))
2717 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2720 info
->debug
= section_objs(info
, "__verbose",
2721 sizeof(*info
->debug
), &info
->num_debug
);
2724 static int move_module(struct module
*mod
, struct load_info
*info
)
2729 /* Do the allocs. */
2730 ptr
= module_alloc_update_bounds(mod
->core_size
);
2732 * The pointer to this block is stored in the module structure
2733 * which is inside the block. Just mark it as not being a
2736 kmemleak_not_leak(ptr
);
2740 memset(ptr
, 0, mod
->core_size
);
2741 mod
->module_core
= ptr
;
2743 ptr
= module_alloc_update_bounds(mod
->init_size
);
2745 * The pointer to this block is stored in the module structure
2746 * which is inside the block. This block doesn't need to be
2747 * scanned as it contains data and code that will be freed
2748 * after the module is initialized.
2750 kmemleak_ignore(ptr
);
2751 if (!ptr
&& mod
->init_size
) {
2752 module_free(mod
, mod
->module_core
);
2755 memset(ptr
, 0, mod
->init_size
);
2756 mod
->module_init
= ptr
;
2758 /* Transfer each section which specifies SHF_ALLOC */
2759 pr_debug("final section addresses:\n");
2760 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
2762 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2764 if (!(shdr
->sh_flags
& SHF_ALLOC
))
2767 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
2768 dest
= mod
->module_init
2769 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
2771 dest
= mod
->module_core
+ shdr
->sh_entsize
;
2773 if (shdr
->sh_type
!= SHT_NOBITS
)
2774 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
2775 /* Update sh_addr to point to copy in image. */
2776 shdr
->sh_addr
= (unsigned long)dest
;
2777 pr_debug("\t0x%lx %s\n",
2778 (long)shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
2784 static int check_module_license_and_versions(struct module
*mod
)
2787 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2788 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2789 * using GPL-only symbols it needs.
2791 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2792 add_taint(TAINT_PROPRIETARY_MODULE
);
2794 /* driverloader was caught wrongly pretending to be under GPL */
2795 if (strcmp(mod
->name
, "driverloader") == 0)
2796 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2798 /* lve claims to be GPL but upstream won't provide source */
2799 if (strcmp(mod
->name
, "lve") == 0)
2800 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2802 #ifdef CONFIG_MODVERSIONS
2803 if ((mod
->num_syms
&& !mod
->crcs
)
2804 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2805 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2806 #ifdef CONFIG_UNUSED_SYMBOLS
2807 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2808 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2811 return try_to_force_load(mod
,
2812 "no versions for exported symbols");
2818 static void flush_module_icache(const struct module
*mod
)
2820 mm_segment_t old_fs
;
2822 /* flush the icache in correct context */
2827 * Flush the instruction cache, since we've played with text.
2828 * Do it before processing of module parameters, so the module
2829 * can provide parameter accessor functions of its own.
2831 if (mod
->module_init
)
2832 flush_icache_range((unsigned long)mod
->module_init
,
2833 (unsigned long)mod
->module_init
2835 flush_icache_range((unsigned long)mod
->module_core
,
2836 (unsigned long)mod
->module_core
+ mod
->core_size
);
2841 int __weak
module_frob_arch_sections(Elf_Ehdr
*hdr
,
2849 static struct module
*layout_and_allocate(struct load_info
*info
)
2851 /* Module within temporary copy. */
2856 mod
= setup_load_info(info
);
2860 err
= check_modinfo(mod
, info
);
2862 return ERR_PTR(err
);
2864 /* Allow arches to frob section contents and sizes. */
2865 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
2866 info
->secstrings
, mod
);
2870 pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
2871 if (pcpusec
->sh_size
) {
2872 /* We have a special allocation for this section. */
2873 err
= percpu_modalloc(mod
,
2874 pcpusec
->sh_size
, pcpusec
->sh_addralign
);
2877 pcpusec
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2880 /* Determine total sizes, and put offsets in sh_entsize. For now
2881 this is done generically; there doesn't appear to be any
2882 special cases for the architectures. */
2883 layout_sections(mod
, info
);
2884 layout_symtab(mod
, info
);
2886 /* Allocate and move to the final place */
2887 err
= move_module(mod
, info
);
2891 /* Module has been copied to its final place now: return it. */
2892 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2893 kmemleak_load_module(mod
, info
);
2897 percpu_modfree(mod
);
2899 return ERR_PTR(err
);
2902 /* mod is no longer valid after this! */
2903 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
2905 percpu_modfree(mod
);
2906 module_free(mod
, mod
->module_init
);
2907 module_free(mod
, mod
->module_core
);
2910 int __weak
module_finalize(const Elf_Ehdr
*hdr
,
2911 const Elf_Shdr
*sechdrs
,
2917 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
2919 /* Sort exception table now relocations are done. */
2920 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2922 /* Copy relocated percpu area over. */
2923 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
2924 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
2926 /* Setup kallsyms-specific fields. */
2927 add_kallsyms(mod
, info
);
2929 /* Arch-specific module finalizing. */
2930 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
2933 /* Is this module of this name done loading? No locks held. */
2934 static bool finished_loading(const char *name
)
2939 mutex_lock(&module_mutex
);
2940 mod
= find_module(name
);
2941 ret
= !mod
|| mod
->state
!= MODULE_STATE_COMING
;
2942 mutex_unlock(&module_mutex
);
2947 /* Allocate and load the module: note that size of section 0 is always
2948 zero, and we rely on this for optional sections. */
2949 static struct module
*load_module(void __user
*umod
,
2951 const char __user
*uargs
)
2953 struct load_info info
= { NULL
, };
2954 struct module
*mod
, *old
;
2957 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
2960 /* Copy in the blobs from userspace, check they are vaguely sane. */
2961 err
= copy_and_check(&info
, umod
, len
, uargs
);
2963 return ERR_PTR(err
);
2965 /* Figure out module layout, and allocate all the memory. */
2966 mod
= layout_and_allocate(&info
);
2972 #ifdef CONFIG_MODULE_SIG
2973 mod
->sig_ok
= info
.sig_ok
;
2975 add_taint_module(mod
, TAINT_FORCED_MODULE
);
2978 /* Now module is in final location, initialize linked lists, etc. */
2979 err
= module_unload_init(mod
);
2983 /* Now we've got everything in the final locations, we can
2984 * find optional sections. */
2985 find_module_sections(mod
, &info
);
2987 err
= check_module_license_and_versions(mod
);
2991 /* Set up MODINFO_ATTR fields */
2992 setup_modinfo(mod
, &info
);
2994 /* Fix up syms, so that st_value is a pointer to location. */
2995 err
= simplify_symbols(mod
, &info
);
2999 err
= apply_relocations(mod
, &info
);
3003 err
= post_relocation(mod
, &info
);
3007 flush_module_icache(mod
);
3009 /* Now copy in args */
3010 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
3011 if (IS_ERR(mod
->args
)) {
3012 err
= PTR_ERR(mod
->args
);
3013 goto free_arch_cleanup
;
3016 /* Mark state as coming so strong_try_module_get() ignores us. */
3017 mod
->state
= MODULE_STATE_COMING
;
3019 /* Now sew it into the lists so we can get lockdep and oops
3020 * info during argument parsing. No one should access us, since
3021 * strong_try_module_get() will fail.
3022 * lockdep/oops can run asynchronous, so use the RCU list insertion
3023 * function to insert in a way safe to concurrent readers.
3024 * The mutex protects against concurrent writers.
3027 mutex_lock(&module_mutex
);
3028 if ((old
= find_module(mod
->name
)) != NULL
) {
3029 if (old
->state
== MODULE_STATE_COMING
) {
3030 /* Wait in case it fails to load. */
3031 mutex_unlock(&module_mutex
);
3032 err
= wait_event_interruptible(module_wq
,
3033 finished_loading(mod
->name
));
3035 goto free_arch_cleanup
;
3042 /* This has to be done once we're sure module name is unique. */
3043 dynamic_debug_setup(info
.debug
, info
.num_debug
);
3045 /* Find duplicate symbols */
3046 err
= verify_export_symbols(mod
);
3050 module_bug_finalize(info
.hdr
, info
.sechdrs
, mod
);
3051 list_add_rcu(&mod
->list
, &modules
);
3052 mutex_unlock(&module_mutex
);
3054 /* Module is ready to execute: parsing args may do that. */
3055 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
,
3056 -32768, 32767, &ddebug_dyndbg_module_param_cb
);
3060 /* Link in to syfs. */
3061 err
= mod_sysfs_setup(mod
, &info
, mod
->kp
, mod
->num_kp
);
3065 /* Get rid of temporary copy. */
3069 trace_module_load(mod
);
3073 mutex_lock(&module_mutex
);
3074 /* Unlink carefully: kallsyms could be walking list. */
3075 list_del_rcu(&mod
->list
);
3076 module_bug_cleanup(mod
);
3077 wake_up_all(&module_wq
);
3079 dynamic_debug_remove(info
.debug
);
3081 mutex_unlock(&module_mutex
);
3082 synchronize_sched();
3085 module_arch_cleanup(mod
);
3089 module_unload_free(mod
);
3091 module_deallocate(mod
, &info
);
3094 return ERR_PTR(err
);
3097 /* Call module constructors. */
3098 static void do_mod_ctors(struct module
*mod
)
3100 #ifdef CONFIG_CONSTRUCTORS
3103 for (i
= 0; i
< mod
->num_ctors
; i
++)
3108 /* This is where the real work happens */
3109 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
3110 unsigned long, len
, const char __user
*, uargs
)
3115 /* Must have permission */
3116 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
3119 /* Do all the hard work */
3120 mod
= load_module(umod
, len
, uargs
);
3122 return PTR_ERR(mod
);
3124 blocking_notifier_call_chain(&module_notify_list
,
3125 MODULE_STATE_COMING
, mod
);
3127 /* Set RO and NX regions for core */
3128 set_section_ro_nx(mod
->module_core
,
3129 mod
->core_text_size
,
3133 /* Set RO and NX regions for init */
3134 set_section_ro_nx(mod
->module_init
,
3135 mod
->init_text_size
,
3140 /* Start the module */
3141 if (mod
->init
!= NULL
)
3142 ret
= do_one_initcall(mod
->init
);
3144 /* Init routine failed: abort. Try to protect us from
3145 buggy refcounters. */
3146 mod
->state
= MODULE_STATE_GOING
;
3147 synchronize_sched();
3149 blocking_notifier_call_chain(&module_notify_list
,
3150 MODULE_STATE_GOING
, mod
);
3152 wake_up_all(&module_wq
);
3157 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3158 "%s: loading module anyway...\n",
3159 __func__
, mod
->name
, ret
,
3164 /* Now it's a first class citizen! */
3165 mod
->state
= MODULE_STATE_LIVE
;
3166 blocking_notifier_call_chain(&module_notify_list
,
3167 MODULE_STATE_LIVE
, mod
);
3169 /* We need to finish all async code before the module init sequence is done */
3170 async_synchronize_full();
3172 mutex_lock(&module_mutex
);
3173 /* Drop initial reference. */
3175 trim_init_extable(mod
);
3176 #ifdef CONFIG_KALLSYMS
3177 mod
->num_symtab
= mod
->core_num_syms
;
3178 mod
->symtab
= mod
->core_symtab
;
3179 mod
->strtab
= mod
->core_strtab
;
3181 unset_module_init_ro_nx(mod
);
3182 module_free(mod
, mod
->module_init
);
3183 mod
->module_init
= NULL
;
3185 mod
->init_ro_size
= 0;
3186 mod
->init_text_size
= 0;
3187 mutex_unlock(&module_mutex
);
3188 wake_up_all(&module_wq
);
3193 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
3195 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
3198 #ifdef CONFIG_KALLSYMS
3200 * This ignores the intensely annoying "mapping symbols" found
3201 * in ARM ELF files: $a, $t and $d.
3203 static inline int is_arm_mapping_symbol(const char *str
)
3205 return str
[0] == '$' && strchr("atd", str
[1])
3206 && (str
[2] == '\0' || str
[2] == '.');
3209 static const char *get_ksymbol(struct module
*mod
,
3211 unsigned long *size
,
3212 unsigned long *offset
)
3214 unsigned int i
, best
= 0;
3215 unsigned long nextval
;
3217 /* At worse, next value is at end of module */
3218 if (within_module_init(addr
, mod
))
3219 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
3221 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
3223 /* Scan for closest preceding symbol, and next symbol. (ELF
3224 starts real symbols at 1). */
3225 for (i
= 1; i
< mod
->num_symtab
; i
++) {
3226 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
3229 /* We ignore unnamed symbols: they're uninformative
3230 * and inserted at a whim. */
3231 if (mod
->symtab
[i
].st_value
<= addr
3232 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
3233 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3234 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3236 if (mod
->symtab
[i
].st_value
> addr
3237 && mod
->symtab
[i
].st_value
< nextval
3238 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3239 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3240 nextval
= mod
->symtab
[i
].st_value
;
3247 *size
= nextval
- mod
->symtab
[best
].st_value
;
3249 *offset
= addr
- mod
->symtab
[best
].st_value
;
3250 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
3253 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3254 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3255 const char *module_address_lookup(unsigned long addr
,
3256 unsigned long *size
,
3257 unsigned long *offset
,
3262 const char *ret
= NULL
;
3265 list_for_each_entry_rcu(mod
, &modules
, list
) {
3266 if (within_module_init(addr
, mod
) ||
3267 within_module_core(addr
, mod
)) {
3269 *modname
= mod
->name
;
3270 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3274 /* Make a copy in here where it's safe */
3276 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3283 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3288 list_for_each_entry_rcu(mod
, &modules
, list
) {
3289 if (within_module_init(addr
, mod
) ||
3290 within_module_core(addr
, mod
)) {
3293 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3296 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3306 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3307 unsigned long *offset
, char *modname
, char *name
)
3312 list_for_each_entry_rcu(mod
, &modules
, list
) {
3313 if (within_module_init(addr
, mod
) ||
3314 within_module_core(addr
, mod
)) {
3317 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3321 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3323 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3333 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3334 char *name
, char *module_name
, int *exported
)
3339 list_for_each_entry_rcu(mod
, &modules
, list
) {
3340 if (symnum
< mod
->num_symtab
) {
3341 *value
= mod
->symtab
[symnum
].st_value
;
3342 *type
= mod
->symtab
[symnum
].st_info
;
3343 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
3345 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
3346 *exported
= is_exported(name
, *value
, mod
);
3350 symnum
-= mod
->num_symtab
;
3356 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
3360 for (i
= 0; i
< mod
->num_symtab
; i
++)
3361 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
3362 mod
->symtab
[i
].st_info
!= 'U')
3363 return mod
->symtab
[i
].st_value
;
3367 /* Look for this name: can be of form module:name. */
3368 unsigned long module_kallsyms_lookup_name(const char *name
)
3372 unsigned long ret
= 0;
3374 /* Don't lock: we're in enough trouble already. */
3376 if ((colon
= strchr(name
, ':')) != NULL
) {
3378 if ((mod
= find_module(name
)) != NULL
)
3379 ret
= mod_find_symname(mod
, colon
+1);
3382 list_for_each_entry_rcu(mod
, &modules
, list
)
3383 if ((ret
= mod_find_symname(mod
, name
)) != 0)
3390 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
3391 struct module
*, unsigned long),
3398 list_for_each_entry(mod
, &modules
, list
) {
3399 for (i
= 0; i
< mod
->num_symtab
; i
++) {
3400 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
3401 mod
, mod
->symtab
[i
].st_value
);
3408 #endif /* CONFIG_KALLSYMS */
3410 static char *module_flags(struct module
*mod
, char *buf
)
3415 mod
->state
== MODULE_STATE_GOING
||
3416 mod
->state
== MODULE_STATE_COMING
) {
3418 bx
+= module_flags_taint(mod
, buf
+ bx
);
3419 /* Show a - for module-is-being-unloaded */
3420 if (mod
->state
== MODULE_STATE_GOING
)
3422 /* Show a + for module-is-being-loaded */
3423 if (mod
->state
== MODULE_STATE_COMING
)
3432 #ifdef CONFIG_PROC_FS
3433 /* Called by the /proc file system to return a list of modules. */
3434 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
3436 mutex_lock(&module_mutex
);
3437 return seq_list_start(&modules
, *pos
);
3440 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3442 return seq_list_next(p
, &modules
, pos
);
3445 static void m_stop(struct seq_file
*m
, void *p
)
3447 mutex_unlock(&module_mutex
);
3450 static int m_show(struct seq_file
*m
, void *p
)
3452 struct module
*mod
= list_entry(p
, struct module
, list
);
3455 seq_printf(m
, "%s %u",
3456 mod
->name
, mod
->init_size
+ mod
->core_size
);
3457 print_unload_info(m
, mod
);
3459 /* Informative for users. */
3460 seq_printf(m
, " %s",
3461 mod
->state
== MODULE_STATE_GOING
? "Unloading":
3462 mod
->state
== MODULE_STATE_COMING
? "Loading":
3464 /* Used by oprofile and other similar tools. */
3465 seq_printf(m
, " 0x%pK", mod
->module_core
);
3469 seq_printf(m
, " %s", module_flags(mod
, buf
));
3471 seq_printf(m
, "\n");
3475 /* Format: modulename size refcount deps address
3477 Where refcount is a number or -, and deps is a comma-separated list
3480 static const struct seq_operations modules_op
= {
3487 static int modules_open(struct inode
*inode
, struct file
*file
)
3489 return seq_open(file
, &modules_op
);
3492 static const struct file_operations proc_modules_operations
= {
3493 .open
= modules_open
,
3495 .llseek
= seq_lseek
,
3496 .release
= seq_release
,
3499 static int __init
proc_modules_init(void)
3501 proc_create("modules", 0, NULL
, &proc_modules_operations
);
3504 module_init(proc_modules_init
);
3507 /* Given an address, look for it in the module exception tables. */
3508 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
3510 const struct exception_table_entry
*e
= NULL
;
3514 list_for_each_entry_rcu(mod
, &modules
, list
) {
3515 if (mod
->num_exentries
== 0)
3518 e
= search_extable(mod
->extable
,
3519 mod
->extable
+ mod
->num_exentries
- 1,
3526 /* Now, if we found one, we are running inside it now, hence
3527 we cannot unload the module, hence no refcnt needed. */
3532 * is_module_address - is this address inside a module?
3533 * @addr: the address to check.
3535 * See is_module_text_address() if you simply want to see if the address
3536 * is code (not data).
3538 bool is_module_address(unsigned long addr
)
3543 ret
= __module_address(addr
) != NULL
;
3550 * __module_address - get the module which contains an address.
3551 * @addr: the address.
3553 * Must be called with preempt disabled or module mutex held so that
3554 * module doesn't get freed during this.
3556 struct module
*__module_address(unsigned long addr
)
3560 if (addr
< module_addr_min
|| addr
> module_addr_max
)
3563 list_for_each_entry_rcu(mod
, &modules
, list
)
3564 if (within_module_core(addr
, mod
)
3565 || within_module_init(addr
, mod
))
3569 EXPORT_SYMBOL_GPL(__module_address
);
3572 * is_module_text_address - is this address inside module code?
3573 * @addr: the address to check.
3575 * See is_module_address() if you simply want to see if the address is
3576 * anywhere in a module. See kernel_text_address() for testing if an
3577 * address corresponds to kernel or module code.
3579 bool is_module_text_address(unsigned long addr
)
3584 ret
= __module_text_address(addr
) != NULL
;
3591 * __module_text_address - get the module whose code contains an address.
3592 * @addr: the address.
3594 * Must be called with preempt disabled or module mutex held so that
3595 * module doesn't get freed during this.
3597 struct module
*__module_text_address(unsigned long addr
)
3599 struct module
*mod
= __module_address(addr
);
3601 /* Make sure it's within the text section. */
3602 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
3603 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3608 EXPORT_SYMBOL_GPL(__module_text_address
);
3610 /* Don't grab lock, we're oopsing. */
3611 void print_modules(void)
3616 printk(KERN_DEFAULT
"Modules linked in:");
3617 /* Most callers should already have preempt disabled, but make sure */
3619 list_for_each_entry_rcu(mod
, &modules
, list
)
3620 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3622 if (last_unloaded_module
[0])
3623 printk(" [last unloaded: %s]", last_unloaded_module
);
3627 #ifdef CONFIG_MODVERSIONS
3628 /* Generate the signature for all relevant module structures here.
3629 * If these change, we don't want to try to parse the module. */
3630 void module_layout(struct module
*mod
,
3631 struct modversion_info
*ver
,
3632 struct kernel_param
*kp
,
3633 struct kernel_symbol
*ks
,
3634 struct tracepoint
* const *tp
)
3637 EXPORT_SYMBOL(module_layout
);