]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/module.c
68c564edb2c18c65829c954469ad5c708d05e559
[mirror_ubuntu-bionic-kernel.git] / kernel / module.c
1 /*
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
4
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.
9
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.
14
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
18 */
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/fs.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"
62
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/module.h>
65
66 #ifndef ARCH_SHF_SMALL
67 #define ARCH_SHF_SMALL 0
68 #endif
69
70 /*
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
74 */
75 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
76 # define debug_align(X) ALIGN(X, PAGE_SIZE)
77 #else
78 # define debug_align(X) (X)
79 #endif
80
81 /*
82 * Given BASE and SIZE this macro calculates the number of pages the
83 * memory regions occupies
84 */
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) \
88 : (0UL))
89
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))
92
93 /*
94 * Mutex protects:
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 */
105
106 #ifdef CONFIG_MODULE_SIG
107 #ifdef CONFIG_MODULE_SIG_FORCE
108 static bool sig_enforce = true;
109 #else
110 static bool sig_enforce = false;
111
112 static int param_set_bool_enable_only(const char *val,
113 const struct kernel_param *kp)
114 {
115 int err;
116 bool test;
117 struct kernel_param dummy_kp = *kp;
118
119 dummy_kp.arg = &test;
120
121 err = param_set_bool(val, &dummy_kp);
122 if (err)
123 return err;
124
125 /* Don't let them unset it once it's set! */
126 if (!test && sig_enforce)
127 return -EROFS;
128
129 if (test)
130 sig_enforce = true;
131 return 0;
132 }
133
134 static const struct kernel_param_ops param_ops_bool_enable_only = {
135 .set = param_set_bool_enable_only,
136 .get = param_get_bool,
137 };
138 #define param_check_bool_enable_only param_check_bool
139
140 module_param(sig_enforce, bool_enable_only, 0644);
141 #endif /* !CONFIG_MODULE_SIG_FORCE */
142 #endif /* CONFIG_MODULE_SIG */
143
144 /* Block module loading/unloading? */
145 int modules_disabled = 0;
146 core_param(nomodule, modules_disabled, bint, 0);
147
148 /* Waiting for a module to finish initializing? */
149 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
150
151 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
152
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;
156
157 int register_module_notifier(struct notifier_block * nb)
158 {
159 return blocking_notifier_chain_register(&module_notify_list, nb);
160 }
161 EXPORT_SYMBOL(register_module_notifier);
162
163 int unregister_module_notifier(struct notifier_block * nb)
164 {
165 return blocking_notifier_chain_unregister(&module_notify_list, nb);
166 }
167 EXPORT_SYMBOL(unregister_module_notifier);
168
169 struct load_info {
170 Elf_Ehdr *hdr;
171 unsigned long len;
172 Elf_Shdr *sechdrs;
173 char *secstrings, *strtab;
174 unsigned long symoffs, stroffs;
175 struct _ddebug *debug;
176 unsigned int num_debug;
177 bool sig_ok;
178 struct {
179 unsigned int sym, str, mod, vers, info, pcpu;
180 } index;
181 };
182
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)
186 {
187 if (mod && mod->state == MODULE_STATE_COMING)
188 return -EBUSY;
189 if (try_module_get(mod))
190 return 0;
191 else
192 return -ENOENT;
193 }
194
195 static inline void add_taint_module(struct module *mod, unsigned flag)
196 {
197 add_taint(flag);
198 mod->taints |= (1U << flag);
199 }
200
201 /*
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.
204 */
205 void __module_put_and_exit(struct module *mod, long code)
206 {
207 module_put(mod);
208 do_exit(code);
209 }
210 EXPORT_SYMBOL(__module_put_and_exit);
211
212 /* Find a module section: 0 means not found. */
213 static unsigned int find_sec(const struct load_info *info, const char *name)
214 {
215 unsigned int i;
216
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)
222 return i;
223 }
224 return 0;
225 }
226
227 /* Find a module section, or NULL. */
228 static void *section_addr(const struct load_info *info, const char *name)
229 {
230 /* Section 0 has sh_addr 0. */
231 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
232 }
233
234 /* Find a module section, or NULL. Fill in number of "objects" in section. */
235 static void *section_objs(const struct load_info *info,
236 const char *name,
237 size_t object_size,
238 unsigned int *num)
239 {
240 unsigned int sec = find_sec(info, name);
241
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;
245 }
246
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[];
264 #endif
265
266 #ifndef CONFIG_MODVERSIONS
267 #define symversion(base, idx) NULL
268 #else
269 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
270 #endif
271
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,
277 void *data),
278 void *data)
279 {
280 unsigned int j;
281
282 for (j = 0; j < arrsize; j++) {
283 if (fn(&arr[j], owner, data))
284 return true;
285 }
286
287 return false;
288 }
289
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,
293 void *data),
294 void *data)
295 {
296 struct module *mod;
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,
302 GPL_ONLY, false },
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,
312 GPL_ONLY, true },
313 #endif
314 };
315
316 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
317 return true;
318
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,
324 mod->gpl_crcs,
325 GPL_ONLY, false },
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
331 { mod->unused_syms,
332 mod->unused_syms + mod->num_unused_syms,
333 mod->unused_crcs,
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,
338 GPL_ONLY, true },
339 #endif
340 };
341
342 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
343 return true;
344 }
345 return false;
346 }
347 EXPORT_SYMBOL_GPL(each_symbol_section);
348
349 struct find_symbol_arg {
350 /* Input */
351 const char *name;
352 bool gplok;
353 bool warn;
354
355 /* Output */
356 struct module *owner;
357 const unsigned long *crc;
358 const struct kernel_symbol *sym;
359 };
360
361 static bool check_symbol(const struct symsearch *syms,
362 struct module *owner,
363 unsigned int symnum, void *data)
364 {
365 struct find_symbol_arg *fsa = data;
366
367 if (!fsa->gplok) {
368 if (syms->licence == GPL_ONLY)
369 return false;
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");
377 }
378 }
379
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);
384 printk(KERN_WARNING
385 "This symbol will go away in the future.\n");
386 printk(KERN_WARNING
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 "
390 "inclusion.\n");
391 }
392 #endif
393
394 fsa->owner = owner;
395 fsa->crc = symversion(syms->crcs, symnum);
396 fsa->sym = &syms->start[symnum];
397 return true;
398 }
399
400 static int cmp_name(const void *va, const void *vb)
401 {
402 const char *a;
403 const struct kernel_symbol *b;
404 a = va; b = vb;
405 return strcmp(a, b->name);
406 }
407
408 static bool find_symbol_in_section(const struct symsearch *syms,
409 struct module *owner,
410 void *data)
411 {
412 struct find_symbol_arg *fsa = data;
413 struct kernel_symbol *sym;
414
415 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
416 sizeof(struct kernel_symbol), cmp_name);
417
418 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
419 return true;
420
421 return false;
422 }
423
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,
429 bool gplok,
430 bool warn)
431 {
432 struct find_symbol_arg fsa;
433
434 fsa.name = name;
435 fsa.gplok = gplok;
436 fsa.warn = warn;
437
438 if (each_symbol_section(find_symbol_in_section, &fsa)) {
439 if (owner)
440 *owner = fsa.owner;
441 if (crc)
442 *crc = fsa.crc;
443 return fsa.sym;
444 }
445
446 pr_debug("Failed to find symbol %s\n", name);
447 return NULL;
448 }
449 EXPORT_SYMBOL_GPL(find_symbol);
450
451 /* Search for module by name: must hold module_mutex. */
452 struct module *find_module(const char *name)
453 {
454 struct module *mod;
455
456 list_for_each_entry(mod, &modules, list) {
457 if (strcmp(mod->name, name) == 0)
458 return mod;
459 }
460 return NULL;
461 }
462 EXPORT_SYMBOL_GPL(find_module);
463
464 #ifdef CONFIG_SMP
465
466 static inline void __percpu *mod_percpu(struct module *mod)
467 {
468 return mod->percpu;
469 }
470
471 static int percpu_modalloc(struct module *mod,
472 unsigned long size, unsigned long align)
473 {
474 if (align > PAGE_SIZE) {
475 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
476 mod->name, align, PAGE_SIZE);
477 align = PAGE_SIZE;
478 }
479
480 mod->percpu = __alloc_reserved_percpu(size, align);
481 if (!mod->percpu) {
482 printk(KERN_WARNING
483 "%s: Could not allocate %lu bytes percpu data\n",
484 mod->name, size);
485 return -ENOMEM;
486 }
487 mod->percpu_size = size;
488 return 0;
489 }
490
491 static void percpu_modfree(struct module *mod)
492 {
493 free_percpu(mod->percpu);
494 }
495
496 static unsigned int find_pcpusec(struct load_info *info)
497 {
498 return find_sec(info, ".data..percpu");
499 }
500
501 static void percpu_modcopy(struct module *mod,
502 const void *from, unsigned long size)
503 {
504 int cpu;
505
506 for_each_possible_cpu(cpu)
507 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
508 }
509
510 /**
511 * is_module_percpu_address - test whether address is from module static percpu
512 * @addr: address to test
513 *
514 * Test whether @addr belongs to module static percpu area.
515 *
516 * RETURNS:
517 * %true if @addr is from module static percpu area
518 */
519 bool is_module_percpu_address(unsigned long addr)
520 {
521 struct module *mod;
522 unsigned int cpu;
523
524 preempt_disable();
525
526 list_for_each_entry_rcu(mod, &modules, list) {
527 if (!mod->percpu_size)
528 continue;
529 for_each_possible_cpu(cpu) {
530 void *start = per_cpu_ptr(mod->percpu, cpu);
531
532 if ((void *)addr >= start &&
533 (void *)addr < start + mod->percpu_size) {
534 preempt_enable();
535 return true;
536 }
537 }
538 }
539
540 preempt_enable();
541 return false;
542 }
543
544 #else /* ... !CONFIG_SMP */
545
546 static inline void __percpu *mod_percpu(struct module *mod)
547 {
548 return NULL;
549 }
550 static inline int percpu_modalloc(struct module *mod,
551 unsigned long size, unsigned long align)
552 {
553 return -ENOMEM;
554 }
555 static inline void percpu_modfree(struct module *mod)
556 {
557 }
558 static unsigned int find_pcpusec(struct load_info *info)
559 {
560 return 0;
561 }
562 static inline void percpu_modcopy(struct module *mod,
563 const void *from, unsigned long size)
564 {
565 /* pcpusec should be 0, and size of that section should be 0. */
566 BUG_ON(size != 0);
567 }
568 bool is_module_percpu_address(unsigned long addr)
569 {
570 return false;
571 }
572
573 #endif /* CONFIG_SMP */
574
575 #define MODINFO_ATTR(field) \
576 static void setup_modinfo_##field(struct module *mod, const char *s) \
577 { \
578 mod->field = kstrdup(s, GFP_KERNEL); \
579 } \
580 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
581 struct module_kobject *mk, char *buffer) \
582 { \
583 return sprintf(buffer, "%s\n", mk->mod->field); \
584 } \
585 static int modinfo_##field##_exists(struct module *mod) \
586 { \
587 return mod->field != NULL; \
588 } \
589 static void free_modinfo_##field(struct module *mod) \
590 { \
591 kfree(mod->field); \
592 mod->field = NULL; \
593 } \
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, \
600 };
601
602 MODINFO_ATTR(version);
603 MODINFO_ATTR(srcversion);
604
605 static char last_unloaded_module[MODULE_NAME_LEN+1];
606
607 #ifdef CONFIG_MODULE_UNLOAD
608
609 EXPORT_TRACEPOINT_SYMBOL(module_get);
610
611 /* Init the unload section of the module. */
612 static int module_unload_init(struct module *mod)
613 {
614 mod->refptr = alloc_percpu(struct module_ref);
615 if (!mod->refptr)
616 return -ENOMEM;
617
618 INIT_LIST_HEAD(&mod->source_list);
619 INIT_LIST_HEAD(&mod->target_list);
620
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;
625
626 return 0;
627 }
628
629 /* Does a already use b? */
630 static int already_uses(struct module *a, struct module *b)
631 {
632 struct module_use *use;
633
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);
637 return 1;
638 }
639 }
640 pr_debug("%s does not use %s!\n", a->name, b->name);
641 return 0;
642 }
643
644 /*
645 * Module a uses b
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).
650 */
651 static int add_module_usage(struct module *a, struct module *b)
652 {
653 struct module_use *use;
654
655 pr_debug("Allocating new usage for %s.\n", a->name);
656 use = kmalloc(sizeof(*use), GFP_ATOMIC);
657 if (!use) {
658 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
659 return -ENOMEM;
660 }
661
662 use->source = a;
663 use->target = b;
664 list_add(&use->source_list, &b->source_list);
665 list_add(&use->target_list, &a->target_list);
666 return 0;
667 }
668
669 /* Module a uses b: caller needs module_mutex() */
670 int ref_module(struct module *a, struct module *b)
671 {
672 int err;
673
674 if (b == NULL || already_uses(a, b))
675 return 0;
676
677 /* If module isn't available, we fail. */
678 err = strong_try_module_get(b);
679 if (err)
680 return err;
681
682 err = add_module_usage(a, b);
683 if (err) {
684 module_put(b);
685 return err;
686 }
687 return 0;
688 }
689 EXPORT_SYMBOL_GPL(ref_module);
690
691 /* Clear the unload stuff of the module. */
692 static void module_unload_free(struct module *mod)
693 {
694 struct module_use *use, *tmp;
695
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);
700 module_put(i);
701 list_del(&use->source_list);
702 list_del(&use->target_list);
703 kfree(use);
704 }
705 mutex_unlock(&module_mutex);
706
707 free_percpu(mod->refptr);
708 }
709
710 #ifdef CONFIG_MODULE_FORCE_UNLOAD
711 static inline int try_force_unload(unsigned int flags)
712 {
713 int ret = (flags & O_TRUNC);
714 if (ret)
715 add_taint(TAINT_FORCED_RMMOD);
716 return ret;
717 }
718 #else
719 static inline int try_force_unload(unsigned int flags)
720 {
721 return 0;
722 }
723 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
724
725 struct stopref
726 {
727 struct module *mod;
728 int flags;
729 int *forced;
730 };
731
732 /* Whole machine is stopped with interrupts off when this runs. */
733 static int __try_stop_module(void *_sref)
734 {
735 struct stopref *sref = _sref;
736
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)))
740 return -EWOULDBLOCK;
741 }
742
743 /* Mark it as dying. */
744 sref->mod->state = MODULE_STATE_GOING;
745 return 0;
746 }
747
748 static int try_stop_module(struct module *mod, int flags, int *forced)
749 {
750 if (flags & O_NONBLOCK) {
751 struct stopref sref = { mod, flags, forced };
752
753 return stop_machine(__try_stop_module, &sref, NULL);
754 } else {
755 /* We don't need to stop the machine for this. */
756 mod->state = MODULE_STATE_GOING;
757 synchronize_sched();
758 return 0;
759 }
760 }
761
762 unsigned long module_refcount(struct module *mod)
763 {
764 unsigned long incs = 0, decs = 0;
765 int cpu;
766
767 for_each_possible_cpu(cpu)
768 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
769 /*
770 * ensure the incs are added up after the decs.
771 * module_put ensures incs are visible before decs with smp_wmb.
772 *
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).
778 *
779 * Rare situation? But module_refcount can be preempted, and we
780 * might be tallying up 4096+ CPUs. So it is not impossible.
781 */
782 smp_rmb();
783 for_each_possible_cpu(cpu)
784 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
785 return incs - decs;
786 }
787 EXPORT_SYMBOL(module_refcount);
788
789 /* This exists whether we can unload or not */
790 static void free_module(struct module *mod);
791
792 static void wait_for_zero_refcount(struct module *mod)
793 {
794 /* Since we might sleep for some time, release the mutex first */
795 mutex_unlock(&module_mutex);
796 for (;;) {
797 pr_debug("Looking at refcount...\n");
798 set_current_state(TASK_UNINTERRUPTIBLE);
799 if (module_refcount(mod) == 0)
800 break;
801 schedule();
802 }
803 current->state = TASK_RUNNING;
804 mutex_lock(&module_mutex);
805 }
806
807 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
808 unsigned int, flags)
809 {
810 struct module *mod;
811 char name[MODULE_NAME_LEN];
812 int ret, forced = 0;
813
814 if (!capable(CAP_SYS_MODULE) || modules_disabled)
815 return -EPERM;
816
817 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
818 return -EFAULT;
819 name[MODULE_NAME_LEN-1] = '\0';
820
821 if (mutex_lock_interruptible(&module_mutex) != 0)
822 return -EINTR;
823
824 mod = find_module(name);
825 if (!mod) {
826 ret = -ENOENT;
827 goto out;
828 }
829
830 if (!list_empty(&mod->source_list)) {
831 /* Other modules depend on us: get rid of them first. */
832 ret = -EWOULDBLOCK;
833 goto out;
834 }
835
836 /* Doing init or already dying? */
837 if (mod->state != MODULE_STATE_LIVE) {
838 /* FIXME: if (force), slam module count and wake up
839 waiter --RR */
840 pr_debug("%s already dying\n", mod->name);
841 ret = -EBUSY;
842 goto out;
843 }
844
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);
848 if (!forced) {
849 /* This module can't be removed */
850 ret = -EBUSY;
851 goto out;
852 }
853 }
854
855 /* Set this up before setting mod->state */
856 mod->waiter = current;
857
858 /* Stop the machine so refcounts can't move and disable module. */
859 ret = try_stop_module(mod, flags, &forced);
860 if (ret != 0)
861 goto out;
862
863 /* Never wait if forced. */
864 if (!forced && module_refcount(mod) != 0)
865 wait_for_zero_refcount(mod);
866
867 mutex_unlock(&module_mutex);
868 /* Final destruction now no one is using it. */
869 if (mod->exit != NULL)
870 mod->exit();
871 blocking_notifier_call_chain(&module_notify_list,
872 MODULE_STATE_GOING, mod);
873 async_synchronize_full();
874
875 /* Store the name of the last unloaded module for diagnostic purposes */
876 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
877
878 free_module(mod);
879 return 0;
880 out:
881 mutex_unlock(&module_mutex);
882 return ret;
883 }
884
885 static inline void print_unload_info(struct seq_file *m, struct module *mod)
886 {
887 struct module_use *use;
888 int printed_something = 0;
889
890 seq_printf(m, " %lu ", module_refcount(mod));
891
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);
897 }
898
899 if (mod->init != NULL && mod->exit == NULL) {
900 printed_something = 1;
901 seq_printf(m, "[permanent],");
902 }
903
904 if (!printed_something)
905 seq_printf(m, "-");
906 }
907
908 void __symbol_put(const char *symbol)
909 {
910 struct module *owner;
911
912 preempt_disable();
913 if (!find_symbol(symbol, &owner, NULL, true, false))
914 BUG();
915 module_put(owner);
916 preempt_enable();
917 }
918 EXPORT_SYMBOL(__symbol_put);
919
920 /* Note this assumes addr is a function, which it currently always is. */
921 void symbol_put_addr(void *addr)
922 {
923 struct module *modaddr;
924 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
925
926 if (core_kernel_text(a))
927 return;
928
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);
932 BUG_ON(!modaddr);
933 module_put(modaddr);
934 }
935 EXPORT_SYMBOL_GPL(symbol_put_addr);
936
937 static ssize_t show_refcnt(struct module_attribute *mattr,
938 struct module_kobject *mk, char *buffer)
939 {
940 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
941 }
942
943 static struct module_attribute modinfo_refcnt =
944 __ATTR(refcnt, 0444, show_refcnt, NULL);
945
946 void __module_get(struct module *module)
947 {
948 if (module) {
949 preempt_disable();
950 __this_cpu_inc(module->refptr->incs);
951 trace_module_get(module, _RET_IP_);
952 preempt_enable();
953 }
954 }
955 EXPORT_SYMBOL(__module_get);
956
957 bool try_module_get(struct module *module)
958 {
959 bool ret = true;
960
961 if (module) {
962 preempt_disable();
963
964 if (likely(module_is_live(module))) {
965 __this_cpu_inc(module->refptr->incs);
966 trace_module_get(module, _RET_IP_);
967 } else
968 ret = false;
969
970 preempt_enable();
971 }
972 return ret;
973 }
974 EXPORT_SYMBOL(try_module_get);
975
976 void module_put(struct module *module)
977 {
978 if (module) {
979 preempt_disable();
980 smp_wmb(); /* see comment in module_refcount */
981 __this_cpu_inc(module->refptr->decs);
982
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);
987 preempt_enable();
988 }
989 }
990 EXPORT_SYMBOL(module_put);
991
992 #else /* !CONFIG_MODULE_UNLOAD */
993 static inline void print_unload_info(struct seq_file *m, struct module *mod)
994 {
995 /* We don't know the usage count, or what modules are using. */
996 seq_printf(m, " - -");
997 }
998
999 static inline void module_unload_free(struct module *mod)
1000 {
1001 }
1002
1003 int ref_module(struct module *a, struct module *b)
1004 {
1005 return strong_try_module_get(b);
1006 }
1007 EXPORT_SYMBOL_GPL(ref_module);
1008
1009 static inline int module_unload_init(struct module *mod)
1010 {
1011 return 0;
1012 }
1013 #endif /* CONFIG_MODULE_UNLOAD */
1014
1015 static size_t module_flags_taint(struct module *mod, char *buf)
1016 {
1017 size_t l = 0;
1018
1019 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1020 buf[l++] = 'P';
1021 if (mod->taints & (1 << TAINT_OOT_MODULE))
1022 buf[l++] = 'O';
1023 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1024 buf[l++] = 'F';
1025 if (mod->taints & (1 << TAINT_CRAP))
1026 buf[l++] = 'C';
1027 /*
1028 * TAINT_FORCED_RMMOD: could be added.
1029 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1030 * apply to modules.
1031 */
1032 return l;
1033 }
1034
1035 static ssize_t show_initstate(struct module_attribute *mattr,
1036 struct module_kobject *mk, char *buffer)
1037 {
1038 const char *state = "unknown";
1039
1040 switch (mk->mod->state) {
1041 case MODULE_STATE_LIVE:
1042 state = "live";
1043 break;
1044 case MODULE_STATE_COMING:
1045 state = "coming";
1046 break;
1047 case MODULE_STATE_GOING:
1048 state = "going";
1049 break;
1050 }
1051 return sprintf(buffer, "%s\n", state);
1052 }
1053
1054 static struct module_attribute modinfo_initstate =
1055 __ATTR(initstate, 0444, show_initstate, NULL);
1056
1057 static ssize_t store_uevent(struct module_attribute *mattr,
1058 struct module_kobject *mk,
1059 const char *buffer, size_t count)
1060 {
1061 enum kobject_action action;
1062
1063 if (kobject_action_type(buffer, count, &action) == 0)
1064 kobject_uevent(&mk->kobj, action);
1065 return count;
1066 }
1067
1068 struct module_attribute module_uevent =
1069 __ATTR(uevent, 0200, NULL, store_uevent);
1070
1071 static ssize_t show_coresize(struct module_attribute *mattr,
1072 struct module_kobject *mk, char *buffer)
1073 {
1074 return sprintf(buffer, "%u\n", mk->mod->core_size);
1075 }
1076
1077 static struct module_attribute modinfo_coresize =
1078 __ATTR(coresize, 0444, show_coresize, NULL);
1079
1080 static ssize_t show_initsize(struct module_attribute *mattr,
1081 struct module_kobject *mk, char *buffer)
1082 {
1083 return sprintf(buffer, "%u\n", mk->mod->init_size);
1084 }
1085
1086 static struct module_attribute modinfo_initsize =
1087 __ATTR(initsize, 0444, show_initsize, NULL);
1088
1089 static ssize_t show_taint(struct module_attribute *mattr,
1090 struct module_kobject *mk, char *buffer)
1091 {
1092 size_t l;
1093
1094 l = module_flags_taint(mk->mod, buffer);
1095 buffer[l++] = '\n';
1096 return l;
1097 }
1098
1099 static struct module_attribute modinfo_taint =
1100 __ATTR(taint, 0444, show_taint, NULL);
1101
1102 static struct module_attribute *modinfo_attrs[] = {
1103 &module_uevent,
1104 &modinfo_version,
1105 &modinfo_srcversion,
1106 &modinfo_initstate,
1107 &modinfo_coresize,
1108 &modinfo_initsize,
1109 &modinfo_taint,
1110 #ifdef CONFIG_MODULE_UNLOAD
1111 &modinfo_refcnt,
1112 #endif
1113 NULL,
1114 };
1115
1116 static const char vermagic[] = VERMAGIC_STRING;
1117
1118 static int try_to_force_load(struct module *mod, const char *reason)
1119 {
1120 #ifdef CONFIG_MODULE_FORCE_LOAD
1121 if (!test_taint(TAINT_FORCED_MODULE))
1122 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1123 mod->name, reason);
1124 add_taint_module(mod, TAINT_FORCED_MODULE);
1125 return 0;
1126 #else
1127 return -ENOEXEC;
1128 #endif
1129 }
1130
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)
1135 {
1136 #ifdef ARCH_RELOCATES_KCRCTAB
1137 if (crc_owner == NULL)
1138 return crc - (unsigned long)reloc_start;
1139 #endif
1140 return crc;
1141 }
1142
1143 static int check_version(Elf_Shdr *sechdrs,
1144 unsigned int versindex,
1145 const char *symname,
1146 struct module *mod,
1147 const unsigned long *crc,
1148 const struct module *crc_owner)
1149 {
1150 unsigned int i, num_versions;
1151 struct modversion_info *versions;
1152
1153 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1154 if (!crc)
1155 return 1;
1156
1157 /* No versions at all? modprobe --force does this. */
1158 if (versindex == 0)
1159 return try_to_force_load(mod, symname) == 0;
1160
1161 versions = (void *) sechdrs[versindex].sh_addr;
1162 num_versions = sechdrs[versindex].sh_size
1163 / sizeof(struct modversion_info);
1164
1165 for (i = 0; i < num_versions; i++) {
1166 if (strcmp(versions[i].name, symname) != 0)
1167 continue;
1168
1169 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1170 return 1;
1171 pr_debug("Found checksum %lX vs module %lX\n",
1172 maybe_relocated(*crc, crc_owner), versions[i].crc);
1173 goto bad_version;
1174 }
1175
1176 printk(KERN_WARNING "%s: no symbol version for %s\n",
1177 mod->name, symname);
1178 return 0;
1179
1180 bad_version:
1181 printk("%s: disagrees about version of symbol %s\n",
1182 mod->name, symname);
1183 return 0;
1184 }
1185
1186 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1187 unsigned int versindex,
1188 struct module *mod)
1189 {
1190 const unsigned long *crc;
1191
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,
1195 &crc, true, false))
1196 BUG();
1197 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1198 NULL);
1199 }
1200
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,
1203 bool has_crcs)
1204 {
1205 if (has_crcs) {
1206 amagic += strcspn(amagic, " ");
1207 bmagic += strcspn(bmagic, " ");
1208 }
1209 return strcmp(amagic, bmagic) == 0;
1210 }
1211 #else
1212 static inline int check_version(Elf_Shdr *sechdrs,
1213 unsigned int versindex,
1214 const char *symname,
1215 struct module *mod,
1216 const unsigned long *crc,
1217 const struct module *crc_owner)
1218 {
1219 return 1;
1220 }
1221
1222 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1223 unsigned int versindex,
1224 struct module *mod)
1225 {
1226 return 1;
1227 }
1228
1229 static inline int same_magic(const char *amagic, const char *bmagic,
1230 bool has_crcs)
1231 {
1232 return strcmp(amagic, bmagic) == 0;
1233 }
1234 #endif /* CONFIG_MODVERSIONS */
1235
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,
1239 const char *name,
1240 char ownername[])
1241 {
1242 struct module *owner;
1243 const struct kernel_symbol *sym;
1244 const unsigned long *crc;
1245 int err;
1246
1247 mutex_lock(&module_mutex);
1248 sym = find_symbol(name, &owner, &crc,
1249 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1250 if (!sym)
1251 goto unlock;
1252
1253 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1254 owner)) {
1255 sym = ERR_PTR(-EINVAL);
1256 goto getname;
1257 }
1258
1259 err = ref_module(mod, owner);
1260 if (err) {
1261 sym = ERR_PTR(err);
1262 goto getname;
1263 }
1264
1265 getname:
1266 /* We must make copy under the lock if we failed to get ref. */
1267 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1268 unlock:
1269 mutex_unlock(&module_mutex);
1270 return sym;
1271 }
1272
1273 static const struct kernel_symbol *
1274 resolve_symbol_wait(struct module *mod,
1275 const struct load_info *info,
1276 const char *name)
1277 {
1278 const struct kernel_symbol *ksym;
1279 char owner[MODULE_NAME_LEN];
1280
1281 if (wait_event_interruptible_timeout(module_wq,
1282 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1283 || PTR_ERR(ksym) != -EBUSY,
1284 30 * HZ) <= 0) {
1285 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1286 mod->name, owner);
1287 }
1288 return ksym;
1289 }
1290
1291 /*
1292 * /sys/module/foo/sections stuff
1293 * J. Corbet <corbet@lwn.net>
1294 */
1295 #ifdef CONFIG_SYSFS
1296
1297 #ifdef CONFIG_KALLSYMS
1298 static inline bool sect_empty(const Elf_Shdr *sect)
1299 {
1300 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1301 }
1302
1303 struct module_sect_attr
1304 {
1305 struct module_attribute mattr;
1306 char *name;
1307 unsigned long address;
1308 };
1309
1310 struct module_sect_attrs
1311 {
1312 struct attribute_group grp;
1313 unsigned int nsections;
1314 struct module_sect_attr attrs[0];
1315 };
1316
1317 static ssize_t module_sect_show(struct module_attribute *mattr,
1318 struct module_kobject *mk, char *buf)
1319 {
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);
1323 }
1324
1325 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1326 {
1327 unsigned int section;
1328
1329 for (section = 0; section < sect_attrs->nsections; section++)
1330 kfree(sect_attrs->attrs[section].name);
1331 kfree(sect_attrs);
1332 }
1333
1334 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1335 {
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;
1340
1341 /* Count loaded sections and allocate structures */
1342 for (i = 0; i < info->hdr->e_shnum; i++)
1343 if (!sect_empty(&info->sechdrs[i]))
1344 nloaded++;
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)
1351 return;
1352
1353 /* Setup section attributes. */
1354 sect_attrs->grp.name = "sections";
1355 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1356
1357 sect_attrs->nsections = 0;
1358 sattr = &sect_attrs->attrs[0];
1359 gattr = &sect_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))
1363 continue;
1364 sattr->address = sec->sh_addr;
1365 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1366 GFP_KERNEL);
1367 if (sattr->name == NULL)
1368 goto out;
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;
1376 }
1377 *gattr = NULL;
1378
1379 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1380 goto out;
1381
1382 mod->sect_attrs = sect_attrs;
1383 return;
1384 out:
1385 free_sect_attrs(sect_attrs);
1386 }
1387
1388 static void remove_sect_attrs(struct module *mod)
1389 {
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;
1397 }
1398 }
1399
1400 /*
1401 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1402 */
1403
1404 struct module_notes_attrs {
1405 struct kobject *dir;
1406 unsigned int notes;
1407 struct bin_attribute attrs[0];
1408 };
1409
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)
1413 {
1414 /*
1415 * The caller checked the pos and count against our size.
1416 */
1417 memcpy(buf, bin_attr->private + pos, count);
1418 return count;
1419 }
1420
1421 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1422 unsigned int i)
1423 {
1424 if (notes_attrs->dir) {
1425 while (i-- > 0)
1426 sysfs_remove_bin_file(notes_attrs->dir,
1427 &notes_attrs->attrs[i]);
1428 kobject_put(notes_attrs->dir);
1429 }
1430 kfree(notes_attrs);
1431 }
1432
1433 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1434 {
1435 unsigned int notes, loaded, i;
1436 struct module_notes_attrs *notes_attrs;
1437 struct bin_attribute *nattr;
1438
1439 /* failed to create section attributes, so can't create notes */
1440 if (!mod->sect_attrs)
1441 return;
1442
1443 /* Count notes sections and allocate structures. */
1444 notes = 0;
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))
1448 ++notes;
1449
1450 if (notes == 0)
1451 return;
1452
1453 notes_attrs = kzalloc(sizeof(*notes_attrs)
1454 + notes * sizeof(notes_attrs->attrs[0]),
1455 GFP_KERNEL);
1456 if (notes_attrs == NULL)
1457 return;
1458
1459 notes_attrs->notes = notes;
1460 nattr = &notes_attrs->attrs[0];
1461 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1462 if (sect_empty(&info->sechdrs[i]))
1463 continue;
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;
1471 ++nattr;
1472 }
1473 ++loaded;
1474 }
1475
1476 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1477 if (!notes_attrs->dir)
1478 goto out;
1479
1480 for (i = 0; i < notes; ++i)
1481 if (sysfs_create_bin_file(notes_attrs->dir,
1482 &notes_attrs->attrs[i]))
1483 goto out;
1484
1485 mod->notes_attrs = notes_attrs;
1486 return;
1487
1488 out:
1489 free_notes_attrs(notes_attrs, i);
1490 }
1491
1492 static void remove_notes_attrs(struct module *mod)
1493 {
1494 if (mod->notes_attrs)
1495 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1496 }
1497
1498 #else
1499
1500 static inline void add_sect_attrs(struct module *mod,
1501 const struct load_info *info)
1502 {
1503 }
1504
1505 static inline void remove_sect_attrs(struct module *mod)
1506 {
1507 }
1508
1509 static inline void add_notes_attrs(struct module *mod,
1510 const struct load_info *info)
1511 {
1512 }
1513
1514 static inline void remove_notes_attrs(struct module *mod)
1515 {
1516 }
1517 #endif /* CONFIG_KALLSYMS */
1518
1519 static void add_usage_links(struct module *mod)
1520 {
1521 #ifdef CONFIG_MODULE_UNLOAD
1522 struct module_use *use;
1523 int nowarn;
1524
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);
1529 }
1530 mutex_unlock(&module_mutex);
1531 #endif
1532 }
1533
1534 static void del_usage_links(struct module *mod)
1535 {
1536 #ifdef CONFIG_MODULE_UNLOAD
1537 struct module_use *use;
1538
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);
1543 #endif
1544 }
1545
1546 static int module_add_modinfo_attrs(struct module *mod)
1547 {
1548 struct module_attribute *attr;
1549 struct module_attribute *temp_attr;
1550 int error = 0;
1551 int i;
1552
1553 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1554 (ARRAY_SIZE(modinfo_attrs) + 1)),
1555 GFP_KERNEL);
1556 if (!mod->modinfo_attrs)
1557 return -ENOMEM;
1558
1559 temp_attr = mod->modinfo_attrs;
1560 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1561 if (!attr->test ||
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);
1566 ++temp_attr;
1567 }
1568 }
1569 return error;
1570 }
1571
1572 static void module_remove_modinfo_attrs(struct module *mod)
1573 {
1574 struct module_attribute *attr;
1575 int i;
1576
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)
1580 break;
1581 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1582 if (attr->free)
1583 attr->free(mod);
1584 }
1585 kfree(mod->modinfo_attrs);
1586 }
1587
1588 static int mod_sysfs_init(struct module *mod)
1589 {
1590 int err;
1591 struct kobject *kobj;
1592
1593 if (!module_sysfs_initialized) {
1594 printk(KERN_ERR "%s: module sysfs not initialized\n",
1595 mod->name);
1596 err = -EINVAL;
1597 goto out;
1598 }
1599
1600 kobj = kset_find_obj(module_kset, mod->name);
1601 if (kobj) {
1602 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1603 kobject_put(kobj);
1604 err = -EINVAL;
1605 goto out;
1606 }
1607
1608 mod->mkobj.mod = mod;
1609
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,
1613 "%s", mod->name);
1614 if (err)
1615 kobject_put(&mod->mkobj.kobj);
1616
1617 /* delay uevent until full sysfs population */
1618 out:
1619 return err;
1620 }
1621
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)
1626 {
1627 int err;
1628
1629 err = mod_sysfs_init(mod);
1630 if (err)
1631 goto out;
1632
1633 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1634 if (!mod->holders_dir) {
1635 err = -ENOMEM;
1636 goto out_unreg;
1637 }
1638
1639 err = module_param_sysfs_setup(mod, kparam, num_params);
1640 if (err)
1641 goto out_unreg_holders;
1642
1643 err = module_add_modinfo_attrs(mod);
1644 if (err)
1645 goto out_unreg_param;
1646
1647 add_usage_links(mod);
1648 add_sect_attrs(mod, info);
1649 add_notes_attrs(mod, info);
1650
1651 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1652 return 0;
1653
1654 out_unreg_param:
1655 module_param_sysfs_remove(mod);
1656 out_unreg_holders:
1657 kobject_put(mod->holders_dir);
1658 out_unreg:
1659 kobject_put(&mod->mkobj.kobj);
1660 out:
1661 return err;
1662 }
1663
1664 static void mod_sysfs_fini(struct module *mod)
1665 {
1666 remove_notes_attrs(mod);
1667 remove_sect_attrs(mod);
1668 kobject_put(&mod->mkobj.kobj);
1669 }
1670
1671 #else /* !CONFIG_SYSFS */
1672
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)
1677 {
1678 return 0;
1679 }
1680
1681 static void mod_sysfs_fini(struct module *mod)
1682 {
1683 }
1684
1685 static void module_remove_modinfo_attrs(struct module *mod)
1686 {
1687 }
1688
1689 static void del_usage_links(struct module *mod)
1690 {
1691 }
1692
1693 #endif /* CONFIG_SYSFS */
1694
1695 static void mod_sysfs_teardown(struct module *mod)
1696 {
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);
1703 }
1704
1705 /*
1706 * unlink the module with the whole machine is stopped with interrupts off
1707 * - this defends against kallsyms not taking locks
1708 */
1709 static int __unlink_module(void *_mod)
1710 {
1711 struct module *mod = _mod;
1712 list_del(&mod->list);
1713 module_bug_cleanup(mod);
1714 return 0;
1715 }
1716
1717 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1718 /*
1719 * LKM RO/NX protection: protect module's text/ro-data
1720 * from modification and any data from execution.
1721 */
1722 void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1723 {
1724 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1725 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1726
1727 if (end_pfn > begin_pfn)
1728 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1729 }
1730
1731 static void set_section_ro_nx(void *base,
1732 unsigned long text_size,
1733 unsigned long ro_size,
1734 unsigned long total_size)
1735 {
1736 /* begin and end PFNs of the current subsection */
1737 unsigned long begin_pfn;
1738 unsigned long end_pfn;
1739
1740 /*
1741 * Set RO for module text and RO-data:
1742 * - Always protect first page.
1743 * - Do not protect last partial page.
1744 */
1745 if (ro_size > 0)
1746 set_page_attributes(base, base + ro_size, set_memory_ro);
1747
1748 /*
1749 * Set NX permissions for module data:
1750 * - Do not protect first partial page.
1751 * - Always protect last page.
1752 */
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);
1758 }
1759 }
1760
1761 static void unset_module_core_ro_nx(struct module *mod)
1762 {
1763 set_page_attributes(mod->module_core + mod->core_text_size,
1764 mod->module_core + mod->core_size,
1765 set_memory_x);
1766 set_page_attributes(mod->module_core,
1767 mod->module_core + mod->core_ro_size,
1768 set_memory_rw);
1769 }
1770
1771 static void unset_module_init_ro_nx(struct module *mod)
1772 {
1773 set_page_attributes(mod->module_init + mod->init_text_size,
1774 mod->module_init + mod->init_size,
1775 set_memory_x);
1776 set_page_attributes(mod->module_init,
1777 mod->module_init + mod->init_ro_size,
1778 set_memory_rw);
1779 }
1780
1781 /* Iterate through all modules and set each module's text as RW */
1782 void set_all_modules_text_rw(void)
1783 {
1784 struct module *mod;
1785
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,
1791 set_memory_rw);
1792 }
1793 if ((mod->module_init) && (mod->init_text_size)) {
1794 set_page_attributes(mod->module_init,
1795 mod->module_init + mod->init_text_size,
1796 set_memory_rw);
1797 }
1798 }
1799 mutex_unlock(&module_mutex);
1800 }
1801
1802 /* Iterate through all modules and set each module's text as RO */
1803 void set_all_modules_text_ro(void)
1804 {
1805 struct module *mod;
1806
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,
1812 set_memory_ro);
1813 }
1814 if ((mod->module_init) && (mod->init_text_size)) {
1815 set_page_attributes(mod->module_init,
1816 mod->module_init + mod->init_text_size,
1817 set_memory_ro);
1818 }
1819 }
1820 mutex_unlock(&module_mutex);
1821 }
1822 #else
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) { }
1826 #endif
1827
1828 void __weak module_free(struct module *mod, void *module_region)
1829 {
1830 vfree(module_region);
1831 }
1832
1833 void __weak module_arch_cleanup(struct module *mod)
1834 {
1835 }
1836
1837 /* Free a module, remove from lists, etc. */
1838 static void free_module(struct module *mod)
1839 {
1840 trace_module_free(mod);
1841
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);
1847
1848 /* Remove dynamic debug info */
1849 ddebug_remove_module(mod->name);
1850
1851 /* Arch-specific cleanup. */
1852 module_arch_cleanup(mod);
1853
1854 /* Module unload stuff */
1855 module_unload_free(mod);
1856
1857 /* Free any allocated parameters. */
1858 destroy_params(mod->kp, mod->num_kp);
1859
1860 /* This may be NULL, but that's OK */
1861 unset_module_init_ro_nx(mod);
1862 module_free(mod, mod->module_init);
1863 kfree(mod->args);
1864 percpu_modfree(mod);
1865
1866 /* Free lock-classes: */
1867 lockdep_free_key_range(mod->module_core, mod->core_size);
1868
1869 /* Finally, free the core (containing the module structure) */
1870 unset_module_core_ro_nx(mod);
1871 module_free(mod, mod->module_core);
1872
1873 #ifdef CONFIG_MPU
1874 update_protections(current->mm);
1875 #endif
1876 }
1877
1878 void *__symbol_get(const char *symbol)
1879 {
1880 struct module *owner;
1881 const struct kernel_symbol *sym;
1882
1883 preempt_disable();
1884 sym = find_symbol(symbol, &owner, NULL, true, true);
1885 if (sym && strong_try_module_get(owner))
1886 sym = NULL;
1887 preempt_enable();
1888
1889 return sym ? (void *)sym->value : NULL;
1890 }
1891 EXPORT_SYMBOL_GPL(__symbol_get);
1892
1893 /*
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.
1896 *
1897 * You must hold the module_mutex.
1898 */
1899 static int verify_export_symbols(struct module *mod)
1900 {
1901 unsigned int i;
1902 struct module *owner;
1903 const struct kernel_symbol *s;
1904 struct {
1905 const struct kernel_symbol *sym;
1906 unsigned int num;
1907 } arr[] = {
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 },
1914 #endif
1915 };
1916
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)) {
1920 printk(KERN_ERR
1921 "%s: exports duplicate symbol %s"
1922 " (owned by %s)\n",
1923 mod->name, s->name, module_name(owner));
1924 return -ENOEXEC;
1925 }
1926 }
1927 }
1928 return 0;
1929 }
1930
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)
1933 {
1934 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1935 Elf_Sym *sym = (void *)symsec->sh_addr;
1936 unsigned long secbase;
1937 unsigned int i;
1938 int ret = 0;
1939 const struct kernel_symbol *ksym;
1940
1941 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1942 const char *name = info->strtab + sym[i].st_name;
1943
1944 switch (sym[i].st_shndx) {
1945 case SHN_COMMON:
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",
1950 mod->name);
1951 ret = -ENOEXEC;
1952 break;
1953
1954 case SHN_ABS:
1955 /* Don't need to do anything */
1956 pr_debug("Absolute symbol: 0x%08lx\n",
1957 (long)sym[i].st_value);
1958 break;
1959
1960 case SHN_UNDEF:
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;
1965 break;
1966 }
1967
1968 /* Ok if weak. */
1969 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1970 break;
1971
1972 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1973 mod->name, name, PTR_ERR(ksym));
1974 ret = PTR_ERR(ksym) ?: -ENOENT;
1975 break;
1976
1977 default:
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);
1981 else
1982 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1983 sym[i].st_value += secbase;
1984 break;
1985 }
1986 }
1987
1988 return ret;
1989 }
1990
1991 static int apply_relocations(struct module *mod, const struct load_info *info)
1992 {
1993 unsigned int i;
1994 int err = 0;
1995
1996 /* Now do relocations. */
1997 for (i = 1; i < info->hdr->e_shnum; i++) {
1998 unsigned int infosec = info->sechdrs[i].sh_info;
1999
2000 /* Not a valid relocation section? */
2001 if (infosec >= info->hdr->e_shnum)
2002 continue;
2003
2004 /* Don't bother with non-allocated sections */
2005 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2006 continue;
2007
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);
2014 if (err < 0)
2015 break;
2016 }
2017 return err;
2018 }
2019
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)
2023 {
2024 /* default implementation just returns zero */
2025 return 0;
2026 }
2027
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)
2031 {
2032 long ret;
2033
2034 *size += arch_mod_section_prepend(mod, section);
2035 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2036 *size = ret + sechdr->sh_size;
2037 return ret;
2038 }
2039
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
2043 belongs in init. */
2044 static void layout_sections(struct module *mod, struct load_info *info)
2045 {
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 }
2054 };
2055 unsigned int m, i;
2056
2057 for (i = 0; i < info->hdr->e_shnum; i++)
2058 info->sechdrs[i].sh_entsize = ~0UL;
2059
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;
2065
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"))
2070 continue;
2071 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
2072 pr_debug("\t%s\n", sname);
2073 }
2074 switch (m) {
2075 case 0: /* executable */
2076 mod->core_size = debug_align(mod->core_size);
2077 mod->core_text_size = mod->core_size;
2078 break;
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;
2082 break;
2083 case 3: /* whole core */
2084 mod->core_size = debug_align(mod->core_size);
2085 break;
2086 }
2087 }
2088
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;
2094
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"))
2099 continue;
2100 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
2101 | INIT_OFFSET_MASK);
2102 pr_debug("\t%s\n", sname);
2103 }
2104 switch (m) {
2105 case 0: /* executable */
2106 mod->init_size = debug_align(mod->init_size);
2107 mod->init_text_size = mod->init_size;
2108 break;
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;
2112 break;
2113 case 3: /* whole init */
2114 mod->init_size = debug_align(mod->init_size);
2115 break;
2116 }
2117 }
2118 }
2119
2120 static void set_license(struct module *mod, const char *license)
2121 {
2122 if (!license)
2123 license = "unspecified";
2124
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);
2130 }
2131 }
2132
2133 /* Parse tag=value strings from .modinfo section */
2134 static char *next_string(char *string, unsigned long *secsize)
2135 {
2136 /* Skip non-zero chars */
2137 while (string[0]) {
2138 string++;
2139 if ((*secsize)-- <= 1)
2140 return NULL;
2141 }
2142
2143 /* Skip any zero padding. */
2144 while (!string[0]) {
2145 string++;
2146 if ((*secsize)-- <= 1)
2147 return NULL;
2148 }
2149 return string;
2150 }
2151
2152 static char *get_modinfo(struct load_info *info, const char *tag)
2153 {
2154 char *p;
2155 unsigned int taglen = strlen(tag);
2156 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2157 unsigned long size = infosec->sh_size;
2158
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;
2162 }
2163 return NULL;
2164 }
2165
2166 static void setup_modinfo(struct module *mod, struct load_info *info)
2167 {
2168 struct module_attribute *attr;
2169 int i;
2170
2171 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2172 if (attr->setup)
2173 attr->setup(mod, get_modinfo(info, attr->attr.name));
2174 }
2175 }
2176
2177 static void free_modinfo(struct module *mod)
2178 {
2179 struct module_attribute *attr;
2180 int i;
2181
2182 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2183 if (attr->free)
2184 attr->free(mod);
2185 }
2186 }
2187
2188 #ifdef CONFIG_KALLSYMS
2189
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)
2194 {
2195 return bsearch(name, start, stop - start,
2196 sizeof(struct kernel_symbol), cmp_name);
2197 }
2198
2199 static int is_exported(const char *name, unsigned long value,
2200 const struct module *mod)
2201 {
2202 const struct kernel_symbol *ks;
2203 if (!mod)
2204 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2205 else
2206 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2207 return ks != NULL && ks->value == value;
2208 }
2209
2210 /* As per nm */
2211 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2212 {
2213 const Elf_Shdr *sechdrs = info->sechdrs;
2214
2215 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2216 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2217 return 'v';
2218 else
2219 return 'w';
2220 }
2221 if (sym->st_shndx == SHN_UNDEF)
2222 return 'U';
2223 if (sym->st_shndx == SHN_ABS)
2224 return 'a';
2225 if (sym->st_shndx >= SHN_LORESERVE)
2226 return '?';
2227 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2228 return 't';
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))
2232 return 'r';
2233 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2234 return 'g';
2235 else
2236 return 'd';
2237 }
2238 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2239 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2240 return 's';
2241 else
2242 return 'b';
2243 }
2244 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2245 ".debug")) {
2246 return 'n';
2247 }
2248 return '?';
2249 }
2250
2251 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2252 unsigned int shnum)
2253 {
2254 const Elf_Shdr *sec;
2255
2256 if (src->st_shndx == SHN_UNDEF
2257 || src->st_shndx >= shnum
2258 || !src->st_name)
2259 return false;
2260
2261 sec = sechdrs + src->st_shndx;
2262 if (!(sec->sh_flags & SHF_ALLOC)
2263 #ifndef CONFIG_KALLSYMS_ALL
2264 || !(sec->sh_flags & SHF_EXECINSTR)
2265 #endif
2266 || (sec->sh_entsize & INIT_OFFSET_MASK))
2267 return false;
2268
2269 return true;
2270 }
2271
2272 /*
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>.
2278 */
2279 static void layout_symtab(struct module *mod, struct load_info *info)
2280 {
2281 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2282 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2283 const Elf_Sym *src;
2284 unsigned int i, nsrc, ndst, strtab_size;
2285
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);
2291
2292 src = (void *)info->hdr + symsect->sh_offset;
2293 nsrc = symsect->sh_size / sizeof(*src);
2294
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;
2299 ndst++;
2300 }
2301
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;
2306
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);
2312 }
2313
2314 static void add_kallsyms(struct module *mod, const struct load_info *info)
2315 {
2316 unsigned int i, ndst;
2317 const Elf_Sym *src;
2318 Elf_Sym *dst;
2319 char *s;
2320 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2321
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;
2326
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);
2330
2331 mod->core_symtab = dst = mod->module_core + info->symoffs;
2332 mod->core_strtab = s = mod->module_core + info->stroffs;
2333 src = mod->symtab;
2334 *dst = *src;
2335 *s++ = 0;
2336 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
2337 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
2338 continue;
2339
2340 dst[ndst] = *src;
2341 dst[ndst++].st_name = s - mod->core_strtab;
2342 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
2343 }
2344 mod->core_num_syms = ndst;
2345 }
2346 #else
2347 static inline void layout_symtab(struct module *mod, struct load_info *info)
2348 {
2349 }
2350
2351 static void add_kallsyms(struct module *mod, const struct load_info *info)
2352 {
2353 }
2354 #endif /* CONFIG_KALLSYMS */
2355
2356 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2357 {
2358 if (!debug)
2359 return;
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",
2363 debug->modname);
2364 #endif
2365 }
2366
2367 static void dynamic_debug_remove(struct _ddebug *debug)
2368 {
2369 if (debug)
2370 ddebug_remove_module(debug->modname);
2371 }
2372
2373 void * __weak module_alloc(unsigned long size)
2374 {
2375 return size == 0 ? NULL : vmalloc_exec(size);
2376 }
2377
2378 static void *module_alloc_update_bounds(unsigned long size)
2379 {
2380 void *ret = module_alloc(size);
2381
2382 if (ret) {
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);
2390 }
2391 return ret;
2392 }
2393
2394 #ifdef CONFIG_DEBUG_KMEMLEAK
2395 static void kmemleak_load_module(const struct module *mod,
2396 const struct load_info *info)
2397 {
2398 unsigned int i;
2399
2400 /* only scan the sections containing data */
2401 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2402
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))
2406 continue;
2407 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
2408 continue;
2409
2410 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2411 info->sechdrs[i].sh_size, GFP_KERNEL);
2412 }
2413 }
2414 #else
2415 static inline void kmemleak_load_module(const struct module *mod,
2416 const struct load_info *info)
2417 {
2418 }
2419 #endif
2420
2421 #ifdef CONFIG_MODULE_SIG
2422 static int module_sig_check(struct load_info *info,
2423 const void *mod, unsigned long *len)
2424 {
2425 int err = -ENOKEY;
2426 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2427 const void *p = mod, *end = mod + *len;
2428
2429 /* Poor man's memmem. */
2430 while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) {
2431 if (p + markerlen > end)
2432 break;
2433
2434 if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) {
2435 const void *sig = p + markerlen;
2436 /* Truncate module up to signature. */
2437 *len = p - mod;
2438 err = mod_verify_sig(mod, *len, sig, end - sig);
2439 break;
2440 }
2441 p++;
2442 }
2443
2444 if (!err) {
2445 info->sig_ok = true;
2446 return 0;
2447 }
2448
2449 /* Not having a signature is only an error if we're strict. */
2450 if (err == -ENOKEY && !sig_enforce)
2451 err = 0;
2452
2453 return err;
2454 }
2455 #else /* !CONFIG_MODULE_SIG */
2456 static int module_sig_check(struct load_info *info,
2457 void *mod, unsigned long *len)
2458 {
2459 return 0;
2460 }
2461 #endif /* !CONFIG_MODULE_SIG */
2462
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)
2467 {
2468 int err;
2469 Elf_Ehdr *hdr;
2470
2471 if (len < sizeof(*hdr))
2472 return -ENOEXEC;
2473
2474 /* Suck in entire file: we'll want most of it. */
2475 if ((hdr = vmalloc(len)) == NULL)
2476 return -ENOMEM;
2477
2478 if (copy_from_user(hdr, umod, len) != 0) {
2479 err = -EFAULT;
2480 goto free_hdr;
2481 }
2482
2483 err = module_sig_check(info, hdr, &len);
2484 if (err)
2485 goto free_hdr;
2486
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)) {
2493 err = -ENOEXEC;
2494 goto free_hdr;
2495 }
2496
2497 if (hdr->e_shoff >= len ||
2498 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) {
2499 err = -ENOEXEC;
2500 goto free_hdr;
2501 }
2502
2503 info->hdr = hdr;
2504 info->len = len;
2505 return 0;
2506
2507 free_hdr:
2508 vfree(hdr);
2509 return err;
2510 }
2511
2512 static void free_copy(struct load_info *info)
2513 {
2514 vfree(info->hdr);
2515 }
2516
2517 static int rewrite_section_headers(struct load_info *info)
2518 {
2519 unsigned int i;
2520
2521 /* This should always be true, but let's be sure. */
2522 info->sechdrs[0].sh_addr = 0;
2523
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",
2529 info->len);
2530 return -ENOEXEC;
2531 }
2532
2533 /* Mark all sections sh_addr with their address in the
2534 temporary image. */
2535 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2536
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;
2541 #endif
2542 }
2543
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;
2549 return 0;
2550 }
2551
2552 /*
2553 * Set up our basic convenience variables (pointers to section headers,
2554 * search for module section index etc), and do some basic section
2555 * verification.
2556 *
2557 * Return the temporary module pointer (we'll replace it with the final
2558 * one when we move the module sections around).
2559 */
2560 static struct module *setup_load_info(struct load_info *info)
2561 {
2562 unsigned int i;
2563 int err;
2564 struct module *mod;
2565
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;
2570
2571 err = rewrite_section_headers(info);
2572 if (err)
2573 return ERR_PTR(err);
2574
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;
2582 break;
2583 }
2584 }
2585
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);
2590 }
2591 /* This is temporary: point mod into copy of data. */
2592 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2593
2594 if (info->index.sym == 0) {
2595 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2596 mod->name);
2597 return ERR_PTR(-ENOEXEC);
2598 }
2599
2600 info->index.pcpu = find_pcpusec(info);
2601
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);
2605
2606 return mod;
2607 }
2608
2609 static int check_modinfo(struct module *mod, struct load_info *info)
2610 {
2611 const char *modmagic = get_modinfo(info, "vermagic");
2612 int err;
2613
2614 /* This is allowed: modprobe --force will invalidate it. */
2615 if (!modmagic) {
2616 err = try_to_force_load(mod, "bad vermagic");
2617 if (err)
2618 return err;
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);
2622 return -ENOEXEC;
2623 }
2624
2625 if (!get_modinfo(info, "intree"))
2626 add_taint_module(mod, TAINT_OOT_MODULE);
2627
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",
2632 mod->name);
2633 }
2634
2635 /* Set up license info based on the info section */
2636 set_license(mod, get_modinfo(info, "license"));
2637
2638 return 0;
2639 }
2640
2641 static void find_module_sections(struct module *mod, struct load_info *info)
2642 {
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");
2657
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");
2667 #endif
2668 #ifdef CONFIG_CONSTRUCTORS
2669 mod->ctors = section_objs(info, ".ctors",
2670 sizeof(*mod->ctors), &mod->num_ctors);
2671 #endif
2672
2673 #ifdef CONFIG_TRACEPOINTS
2674 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2675 sizeof(*mod->tracepoints_ptrs),
2676 &mod->num_tracepoints);
2677 #endif
2678 #ifdef HAVE_JUMP_LABEL
2679 mod->jump_entries = section_objs(info, "__jump_table",
2680 sizeof(*mod->jump_entries),
2681 &mod->num_jump_entries);
2682 #endif
2683 #ifdef CONFIG_EVENT_TRACING
2684 mod->trace_events = section_objs(info, "_ftrace_events",
2685 sizeof(*mod->trace_events),
2686 &mod->num_trace_events);
2687 /*
2688 * This section contains pointers to allocated objects in the trace
2689 * code and not scanning it leads to false positives.
2690 */
2691 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2692 mod->num_trace_events, GFP_KERNEL);
2693 #endif
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);
2698 /*
2699 * This section contains pointers to allocated objects in the trace
2700 * code and not scanning it leads to false positives.
2701 */
2702 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2703 sizeof(*mod->trace_bprintk_fmt_start) *
2704 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2705 #endif
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);
2711 #endif
2712
2713 mod->extable = section_objs(info, "__ex_table",
2714 sizeof(*mod->extable), &mod->num_exentries);
2715
2716 if (section_addr(info, "__obsparm"))
2717 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2718 mod->name);
2719
2720 info->debug = section_objs(info, "__verbose",
2721 sizeof(*info->debug), &info->num_debug);
2722 }
2723
2724 static int move_module(struct module *mod, struct load_info *info)
2725 {
2726 int i;
2727 void *ptr;
2728
2729 /* Do the allocs. */
2730 ptr = module_alloc_update_bounds(mod->core_size);
2731 /*
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
2734 * leak.
2735 */
2736 kmemleak_not_leak(ptr);
2737 if (!ptr)
2738 return -ENOMEM;
2739
2740 memset(ptr, 0, mod->core_size);
2741 mod->module_core = ptr;
2742
2743 ptr = module_alloc_update_bounds(mod->init_size);
2744 /*
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.
2749 */
2750 kmemleak_ignore(ptr);
2751 if (!ptr && mod->init_size) {
2752 module_free(mod, mod->module_core);
2753 return -ENOMEM;
2754 }
2755 memset(ptr, 0, mod->init_size);
2756 mod->module_init = ptr;
2757
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++) {
2761 void *dest;
2762 Elf_Shdr *shdr = &info->sechdrs[i];
2763
2764 if (!(shdr->sh_flags & SHF_ALLOC))
2765 continue;
2766
2767 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2768 dest = mod->module_init
2769 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2770 else
2771 dest = mod->module_core + shdr->sh_entsize;
2772
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);
2779 }
2780
2781 return 0;
2782 }
2783
2784 static int check_module_license_and_versions(struct module *mod)
2785 {
2786 /*
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.
2790 */
2791 if (strcmp(mod->name, "ndiswrapper") == 0)
2792 add_taint(TAINT_PROPRIETARY_MODULE);
2793
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);
2797
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);
2801
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)
2809 #endif
2810 ) {
2811 return try_to_force_load(mod,
2812 "no versions for exported symbols");
2813 }
2814 #endif
2815 return 0;
2816 }
2817
2818 static void flush_module_icache(const struct module *mod)
2819 {
2820 mm_segment_t old_fs;
2821
2822 /* flush the icache in correct context */
2823 old_fs = get_fs();
2824 set_fs(KERNEL_DS);
2825
2826 /*
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.
2830 */
2831 if (mod->module_init)
2832 flush_icache_range((unsigned long)mod->module_init,
2833 (unsigned long)mod->module_init
2834 + mod->init_size);
2835 flush_icache_range((unsigned long)mod->module_core,
2836 (unsigned long)mod->module_core + mod->core_size);
2837
2838 set_fs(old_fs);
2839 }
2840
2841 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2842 Elf_Shdr *sechdrs,
2843 char *secstrings,
2844 struct module *mod)
2845 {
2846 return 0;
2847 }
2848
2849 static struct module *layout_and_allocate(struct load_info *info)
2850 {
2851 /* Module within temporary copy. */
2852 struct module *mod;
2853 Elf_Shdr *pcpusec;
2854 int err;
2855
2856 mod = setup_load_info(info);
2857 if (IS_ERR(mod))
2858 return mod;
2859
2860 err = check_modinfo(mod, info);
2861 if (err)
2862 return ERR_PTR(err);
2863
2864 /* Allow arches to frob section contents and sizes. */
2865 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2866 info->secstrings, mod);
2867 if (err < 0)
2868 goto out;
2869
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);
2875 if (err)
2876 goto out;
2877 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2878 }
2879
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);
2885
2886 /* Allocate and move to the final place */
2887 err = move_module(mod, info);
2888 if (err)
2889 goto free_percpu;
2890
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);
2894 return mod;
2895
2896 free_percpu:
2897 percpu_modfree(mod);
2898 out:
2899 return ERR_PTR(err);
2900 }
2901
2902 /* mod is no longer valid after this! */
2903 static void module_deallocate(struct module *mod, struct load_info *info)
2904 {
2905 percpu_modfree(mod);
2906 module_free(mod, mod->module_init);
2907 module_free(mod, mod->module_core);
2908 }
2909
2910 int __weak module_finalize(const Elf_Ehdr *hdr,
2911 const Elf_Shdr *sechdrs,
2912 struct module *me)
2913 {
2914 return 0;
2915 }
2916
2917 static int post_relocation(struct module *mod, const struct load_info *info)
2918 {
2919 /* Sort exception table now relocations are done. */
2920 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2921
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);
2925
2926 /* Setup kallsyms-specific fields. */
2927 add_kallsyms(mod, info);
2928
2929 /* Arch-specific module finalizing. */
2930 return module_finalize(info->hdr, info->sechdrs, mod);
2931 }
2932
2933 /* Is this module of this name done loading? No locks held. */
2934 static bool finished_loading(const char *name)
2935 {
2936 struct module *mod;
2937 bool ret;
2938
2939 mutex_lock(&module_mutex);
2940 mod = find_module(name);
2941 ret = !mod || mod->state != MODULE_STATE_COMING;
2942 mutex_unlock(&module_mutex);
2943
2944 return ret;
2945 }
2946
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,
2950 unsigned long len,
2951 const char __user *uargs)
2952 {
2953 struct load_info info = { NULL, };
2954 struct module *mod, *old;
2955 long err;
2956
2957 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
2958 umod, len, uargs);
2959
2960 /* Copy in the blobs from userspace, check they are vaguely sane. */
2961 err = copy_and_check(&info, umod, len, uargs);
2962 if (err)
2963 return ERR_PTR(err);
2964
2965 /* Figure out module layout, and allocate all the memory. */
2966 mod = layout_and_allocate(&info);
2967 if (IS_ERR(mod)) {
2968 err = PTR_ERR(mod);
2969 goto free_copy;
2970 }
2971
2972 #ifdef CONFIG_MODULE_SIG
2973 mod->sig_ok = info.sig_ok;
2974 if (!mod->sig_ok)
2975 add_taint_module(mod, TAINT_FORCED_MODULE);
2976 #endif
2977
2978 /* Now module is in final location, initialize linked lists, etc. */
2979 err = module_unload_init(mod);
2980 if (err)
2981 goto free_module;
2982
2983 /* Now we've got everything in the final locations, we can
2984 * find optional sections. */
2985 find_module_sections(mod, &info);
2986
2987 err = check_module_license_and_versions(mod);
2988 if (err)
2989 goto free_unload;
2990
2991 /* Set up MODINFO_ATTR fields */
2992 setup_modinfo(mod, &info);
2993
2994 /* Fix up syms, so that st_value is a pointer to location. */
2995 err = simplify_symbols(mod, &info);
2996 if (err < 0)
2997 goto free_modinfo;
2998
2999 err = apply_relocations(mod, &info);
3000 if (err < 0)
3001 goto free_modinfo;
3002
3003 err = post_relocation(mod, &info);
3004 if (err < 0)
3005 goto free_modinfo;
3006
3007 flush_module_icache(mod);
3008
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;
3014 }
3015
3016 /* Mark state as coming so strong_try_module_get() ignores us. */
3017 mod->state = MODULE_STATE_COMING;
3018
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.
3025 */
3026 again:
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));
3034 if (err)
3035 goto free_arch_cleanup;
3036 goto again;
3037 }
3038 err = -EEXIST;
3039 goto unlock;
3040 }
3041
3042 /* This has to be done once we're sure module name is unique. */
3043 dynamic_debug_setup(info.debug, info.num_debug);
3044
3045 /* Find duplicate symbols */
3046 err = verify_export_symbols(mod);
3047 if (err < 0)
3048 goto ddebug;
3049
3050 module_bug_finalize(info.hdr, info.sechdrs, mod);
3051 list_add_rcu(&mod->list, &modules);
3052 mutex_unlock(&module_mutex);
3053
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);
3057 if (err < 0)
3058 goto unlink;
3059
3060 /* Link in to syfs. */
3061 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
3062 if (err < 0)
3063 goto unlink;
3064
3065 /* Get rid of temporary copy. */
3066 free_copy(&info);
3067
3068 /* Done! */
3069 trace_module_load(mod);
3070 return mod;
3071
3072 unlink:
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);
3078 ddebug:
3079 dynamic_debug_remove(info.debug);
3080 unlock:
3081 mutex_unlock(&module_mutex);
3082 synchronize_sched();
3083 kfree(mod->args);
3084 free_arch_cleanup:
3085 module_arch_cleanup(mod);
3086 free_modinfo:
3087 free_modinfo(mod);
3088 free_unload:
3089 module_unload_free(mod);
3090 free_module:
3091 module_deallocate(mod, &info);
3092 free_copy:
3093 free_copy(&info);
3094 return ERR_PTR(err);
3095 }
3096
3097 /* Call module constructors. */
3098 static void do_mod_ctors(struct module *mod)
3099 {
3100 #ifdef CONFIG_CONSTRUCTORS
3101 unsigned long i;
3102
3103 for (i = 0; i < mod->num_ctors; i++)
3104 mod->ctors[i]();
3105 #endif
3106 }
3107
3108 /* This is where the real work happens */
3109 SYSCALL_DEFINE3(init_module, void __user *, umod,
3110 unsigned long, len, const char __user *, uargs)
3111 {
3112 struct module *mod;
3113 int ret = 0;
3114
3115 /* Must have permission */
3116 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3117 return -EPERM;
3118
3119 /* Do all the hard work */
3120 mod = load_module(umod, len, uargs);
3121 if (IS_ERR(mod))
3122 return PTR_ERR(mod);
3123
3124 blocking_notifier_call_chain(&module_notify_list,
3125 MODULE_STATE_COMING, mod);
3126
3127 /* Set RO and NX regions for core */
3128 set_section_ro_nx(mod->module_core,
3129 mod->core_text_size,
3130 mod->core_ro_size,
3131 mod->core_size);
3132
3133 /* Set RO and NX regions for init */
3134 set_section_ro_nx(mod->module_init,
3135 mod->init_text_size,
3136 mod->init_ro_size,
3137 mod->init_size);
3138
3139 do_mod_ctors(mod);
3140 /* Start the module */
3141 if (mod->init != NULL)
3142 ret = do_one_initcall(mod->init);
3143 if (ret < 0) {
3144 /* Init routine failed: abort. Try to protect us from
3145 buggy refcounters. */
3146 mod->state = MODULE_STATE_GOING;
3147 synchronize_sched();
3148 module_put(mod);
3149 blocking_notifier_call_chain(&module_notify_list,
3150 MODULE_STATE_GOING, mod);
3151 free_module(mod);
3152 wake_up_all(&module_wq);
3153 return ret;
3154 }
3155 if (ret > 0) {
3156 printk(KERN_WARNING
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,
3160 __func__);
3161 dump_stack();
3162 }
3163
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);
3168
3169 /* We need to finish all async code before the module init sequence is done */
3170 async_synchronize_full();
3171
3172 mutex_lock(&module_mutex);
3173 /* Drop initial reference. */
3174 module_put(mod);
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;
3180 #endif
3181 unset_module_init_ro_nx(mod);
3182 module_free(mod, mod->module_init);
3183 mod->module_init = NULL;
3184 mod->init_size = 0;
3185 mod->init_ro_size = 0;
3186 mod->init_text_size = 0;
3187 mutex_unlock(&module_mutex);
3188 wake_up_all(&module_wq);
3189
3190 return 0;
3191 }
3192
3193 static inline int within(unsigned long addr, void *start, unsigned long size)
3194 {
3195 return ((void *)addr >= start && (void *)addr < start + size);
3196 }
3197
3198 #ifdef CONFIG_KALLSYMS
3199 /*
3200 * This ignores the intensely annoying "mapping symbols" found
3201 * in ARM ELF files: $a, $t and $d.
3202 */
3203 static inline int is_arm_mapping_symbol(const char *str)
3204 {
3205 return str[0] == '$' && strchr("atd", str[1])
3206 && (str[2] == '\0' || str[2] == '.');
3207 }
3208
3209 static const char *get_ksymbol(struct module *mod,
3210 unsigned long addr,
3211 unsigned long *size,
3212 unsigned long *offset)
3213 {
3214 unsigned int i, best = 0;
3215 unsigned long nextval;
3216
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;
3220 else
3221 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3222
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)
3227 continue;
3228
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))
3235 best = i;
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;
3241 }
3242
3243 if (!best)
3244 return NULL;
3245
3246 if (size)
3247 *size = nextval - mod->symtab[best].st_value;
3248 if (offset)
3249 *offset = addr - mod->symtab[best].st_value;
3250 return mod->strtab + mod->symtab[best].st_name;
3251 }
3252
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,
3258 char **modname,
3259 char *namebuf)
3260 {
3261 struct module *mod;
3262 const char *ret = NULL;
3263
3264 preempt_disable();
3265 list_for_each_entry_rcu(mod, &modules, list) {
3266 if (within_module_init(addr, mod) ||
3267 within_module_core(addr, mod)) {
3268 if (modname)
3269 *modname = mod->name;
3270 ret = get_ksymbol(mod, addr, size, offset);
3271 break;
3272 }
3273 }
3274 /* Make a copy in here where it's safe */
3275 if (ret) {
3276 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3277 ret = namebuf;
3278 }
3279 preempt_enable();
3280 return ret;
3281 }
3282
3283 int lookup_module_symbol_name(unsigned long addr, char *symname)
3284 {
3285 struct module *mod;
3286
3287 preempt_disable();
3288 list_for_each_entry_rcu(mod, &modules, list) {
3289 if (within_module_init(addr, mod) ||
3290 within_module_core(addr, mod)) {
3291 const char *sym;
3292
3293 sym = get_ksymbol(mod, addr, NULL, NULL);
3294 if (!sym)
3295 goto out;
3296 strlcpy(symname, sym, KSYM_NAME_LEN);
3297 preempt_enable();
3298 return 0;
3299 }
3300 }
3301 out:
3302 preempt_enable();
3303 return -ERANGE;
3304 }
3305
3306 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3307 unsigned long *offset, char *modname, char *name)
3308 {
3309 struct module *mod;
3310
3311 preempt_disable();
3312 list_for_each_entry_rcu(mod, &modules, list) {
3313 if (within_module_init(addr, mod) ||
3314 within_module_core(addr, mod)) {
3315 const char *sym;
3316
3317 sym = get_ksymbol(mod, addr, size, offset);
3318 if (!sym)
3319 goto out;
3320 if (modname)
3321 strlcpy(modname, mod->name, MODULE_NAME_LEN);
3322 if (name)
3323 strlcpy(name, sym, KSYM_NAME_LEN);
3324 preempt_enable();
3325 return 0;
3326 }
3327 }
3328 out:
3329 preempt_enable();
3330 return -ERANGE;
3331 }
3332
3333 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3334 char *name, char *module_name, int *exported)
3335 {
3336 struct module *mod;
3337
3338 preempt_disable();
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,
3344 KSYM_NAME_LEN);
3345 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
3346 *exported = is_exported(name, *value, mod);
3347 preempt_enable();
3348 return 0;
3349 }
3350 symnum -= mod->num_symtab;
3351 }
3352 preempt_enable();
3353 return -ERANGE;
3354 }
3355
3356 static unsigned long mod_find_symname(struct module *mod, const char *name)
3357 {
3358 unsigned int i;
3359
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;
3364 return 0;
3365 }
3366
3367 /* Look for this name: can be of form module:name. */
3368 unsigned long module_kallsyms_lookup_name(const char *name)
3369 {
3370 struct module *mod;
3371 char *colon;
3372 unsigned long ret = 0;
3373
3374 /* Don't lock: we're in enough trouble already. */
3375 preempt_disable();
3376 if ((colon = strchr(name, ':')) != NULL) {
3377 *colon = '\0';
3378 if ((mod = find_module(name)) != NULL)
3379 ret = mod_find_symname(mod, colon+1);
3380 *colon = ':';
3381 } else {
3382 list_for_each_entry_rcu(mod, &modules, list)
3383 if ((ret = mod_find_symname(mod, name)) != 0)
3384 break;
3385 }
3386 preempt_enable();
3387 return ret;
3388 }
3389
3390 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3391 struct module *, unsigned long),
3392 void *data)
3393 {
3394 struct module *mod;
3395 unsigned int i;
3396 int ret;
3397
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);
3402 if (ret != 0)
3403 return ret;
3404 }
3405 }
3406 return 0;
3407 }
3408 #endif /* CONFIG_KALLSYMS */
3409
3410 static char *module_flags(struct module *mod, char *buf)
3411 {
3412 int bx = 0;
3413
3414 if (mod->taints ||
3415 mod->state == MODULE_STATE_GOING ||
3416 mod->state == MODULE_STATE_COMING) {
3417 buf[bx++] = '(';
3418 bx += module_flags_taint(mod, buf + bx);
3419 /* Show a - for module-is-being-unloaded */
3420 if (mod->state == MODULE_STATE_GOING)
3421 buf[bx++] = '-';
3422 /* Show a + for module-is-being-loaded */
3423 if (mod->state == MODULE_STATE_COMING)
3424 buf[bx++] = '+';
3425 buf[bx++] = ')';
3426 }
3427 buf[bx] = '\0';
3428
3429 return buf;
3430 }
3431
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)
3435 {
3436 mutex_lock(&module_mutex);
3437 return seq_list_start(&modules, *pos);
3438 }
3439
3440 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3441 {
3442 return seq_list_next(p, &modules, pos);
3443 }
3444
3445 static void m_stop(struct seq_file *m, void *p)
3446 {
3447 mutex_unlock(&module_mutex);
3448 }
3449
3450 static int m_show(struct seq_file *m, void *p)
3451 {
3452 struct module *mod = list_entry(p, struct module, list);
3453 char buf[8];
3454
3455 seq_printf(m, "%s %u",
3456 mod->name, mod->init_size + mod->core_size);
3457 print_unload_info(m, mod);
3458
3459 /* Informative for users. */
3460 seq_printf(m, " %s",
3461 mod->state == MODULE_STATE_GOING ? "Unloading":
3462 mod->state == MODULE_STATE_COMING ? "Loading":
3463 "Live");
3464 /* Used by oprofile and other similar tools. */
3465 seq_printf(m, " 0x%pK", mod->module_core);
3466
3467 /* Taints info */
3468 if (mod->taints)
3469 seq_printf(m, " %s", module_flags(mod, buf));
3470
3471 seq_printf(m, "\n");
3472 return 0;
3473 }
3474
3475 /* Format: modulename size refcount deps address
3476
3477 Where refcount is a number or -, and deps is a comma-separated list
3478 of depends or -.
3479 */
3480 static const struct seq_operations modules_op = {
3481 .start = m_start,
3482 .next = m_next,
3483 .stop = m_stop,
3484 .show = m_show
3485 };
3486
3487 static int modules_open(struct inode *inode, struct file *file)
3488 {
3489 return seq_open(file, &modules_op);
3490 }
3491
3492 static const struct file_operations proc_modules_operations = {
3493 .open = modules_open,
3494 .read = seq_read,
3495 .llseek = seq_lseek,
3496 .release = seq_release,
3497 };
3498
3499 static int __init proc_modules_init(void)
3500 {
3501 proc_create("modules", 0, NULL, &proc_modules_operations);
3502 return 0;
3503 }
3504 module_init(proc_modules_init);
3505 #endif
3506
3507 /* Given an address, look for it in the module exception tables. */
3508 const struct exception_table_entry *search_module_extables(unsigned long addr)
3509 {
3510 const struct exception_table_entry *e = NULL;
3511 struct module *mod;
3512
3513 preempt_disable();
3514 list_for_each_entry_rcu(mod, &modules, list) {
3515 if (mod->num_exentries == 0)
3516 continue;
3517
3518 e = search_extable(mod->extable,
3519 mod->extable + mod->num_exentries - 1,
3520 addr);
3521 if (e)
3522 break;
3523 }
3524 preempt_enable();
3525
3526 /* Now, if we found one, we are running inside it now, hence
3527 we cannot unload the module, hence no refcnt needed. */
3528 return e;
3529 }
3530
3531 /*
3532 * is_module_address - is this address inside a module?
3533 * @addr: the address to check.
3534 *
3535 * See is_module_text_address() if you simply want to see if the address
3536 * is code (not data).
3537 */
3538 bool is_module_address(unsigned long addr)
3539 {
3540 bool ret;
3541
3542 preempt_disable();
3543 ret = __module_address(addr) != NULL;
3544 preempt_enable();
3545
3546 return ret;
3547 }
3548
3549 /*
3550 * __module_address - get the module which contains an address.
3551 * @addr: the address.
3552 *
3553 * Must be called with preempt disabled or module mutex held so that
3554 * module doesn't get freed during this.
3555 */
3556 struct module *__module_address(unsigned long addr)
3557 {
3558 struct module *mod;
3559
3560 if (addr < module_addr_min || addr > module_addr_max)
3561 return NULL;
3562
3563 list_for_each_entry_rcu(mod, &modules, list)
3564 if (within_module_core(addr, mod)
3565 || within_module_init(addr, mod))
3566 return mod;
3567 return NULL;
3568 }
3569 EXPORT_SYMBOL_GPL(__module_address);
3570
3571 /*
3572 * is_module_text_address - is this address inside module code?
3573 * @addr: the address to check.
3574 *
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.
3578 */
3579 bool is_module_text_address(unsigned long addr)
3580 {
3581 bool ret;
3582
3583 preempt_disable();
3584 ret = __module_text_address(addr) != NULL;
3585 preempt_enable();
3586
3587 return ret;
3588 }
3589
3590 /*
3591 * __module_text_address - get the module whose code contains an address.
3592 * @addr: the address.
3593 *
3594 * Must be called with preempt disabled or module mutex held so that
3595 * module doesn't get freed during this.
3596 */
3597 struct module *__module_text_address(unsigned long addr)
3598 {
3599 struct module *mod = __module_address(addr);
3600 if (mod) {
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))
3604 mod = NULL;
3605 }
3606 return mod;
3607 }
3608 EXPORT_SYMBOL_GPL(__module_text_address);
3609
3610 /* Don't grab lock, we're oopsing. */
3611 void print_modules(void)
3612 {
3613 struct module *mod;
3614 char buf[8];
3615
3616 printk(KERN_DEFAULT "Modules linked in:");
3617 /* Most callers should already have preempt disabled, but make sure */
3618 preempt_disable();
3619 list_for_each_entry_rcu(mod, &modules, list)
3620 printk(" %s%s", mod->name, module_flags(mod, buf));
3621 preempt_enable();
3622 if (last_unloaded_module[0])
3623 printk(" [last unloaded: %s]", last_unloaded_module);
3624 printk("\n");
3625 }
3626
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)
3635 {
3636 }
3637 EXPORT_SYMBOL(module_layout);
3638 #endif