Merge tag 'perf-urgent-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.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 <linux/fips.h>
62 #include "module-internal.h"
63
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/module.h>
66
67 #ifndef ARCH_SHF_SMALL
68 #define ARCH_SHF_SMALL 0
69 #endif
70
71 /*
72 * Modules' sections will be aligned on page boundaries
73 * to ensure complete separation of code and data, but
74 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
75 */
76 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
77 # define debug_align(X) ALIGN(X, PAGE_SIZE)
78 #else
79 # define debug_align(X) (X)
80 #endif
81
82 /*
83 * Given BASE and SIZE this macro calculates the number of pages the
84 * memory regions occupies
85 */
86 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
87 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
88 PFN_DOWN((unsigned long)BASE) + 1) \
89 : (0UL))
90
91 /* If this is set, the section belongs in the init part of the module */
92 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
93
94 /*
95 * Mutex protects:
96 * 1) List of modules (also safely readable with preempt_disable),
97 * 2) module_use links,
98 * 3) module_addr_min/module_addr_max.
99 * (delete uses stop_machine/add uses RCU list operations). */
100 DEFINE_MUTEX(module_mutex);
101 EXPORT_SYMBOL_GPL(module_mutex);
102 static LIST_HEAD(modules);
103 #ifdef CONFIG_KGDB_KDB
104 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
105 #endif /* CONFIG_KGDB_KDB */
106
107 #ifdef CONFIG_MODULE_SIG
108 #ifdef CONFIG_MODULE_SIG_FORCE
109 static bool sig_enforce = true;
110 #else
111 static bool sig_enforce = false;
112
113 static int param_set_bool_enable_only(const char *val,
114 const struct kernel_param *kp)
115 {
116 int err;
117 bool test;
118 struct kernel_param dummy_kp = *kp;
119
120 dummy_kp.arg = &test;
121
122 err = param_set_bool(val, &dummy_kp);
123 if (err)
124 return err;
125
126 /* Don't let them unset it once it's set! */
127 if (!test && sig_enforce)
128 return -EROFS;
129
130 if (test)
131 sig_enforce = true;
132 return 0;
133 }
134
135 static const struct kernel_param_ops param_ops_bool_enable_only = {
136 .set = param_set_bool_enable_only,
137 .get = param_get_bool,
138 };
139 #define param_check_bool_enable_only param_check_bool
140
141 module_param(sig_enforce, bool_enable_only, 0644);
142 #endif /* !CONFIG_MODULE_SIG_FORCE */
143 #endif /* CONFIG_MODULE_SIG */
144
145 /* Block module loading/unloading? */
146 int modules_disabled = 0;
147 core_param(nomodule, modules_disabled, bint, 0);
148
149 /* Waiting for a module to finish initializing? */
150 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
151
152 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
153
154 /* Bounds of module allocation, for speeding __module_address.
155 * Protected by module_mutex. */
156 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
157
158 int register_module_notifier(struct notifier_block * nb)
159 {
160 return blocking_notifier_chain_register(&module_notify_list, nb);
161 }
162 EXPORT_SYMBOL(register_module_notifier);
163
164 int unregister_module_notifier(struct notifier_block * nb)
165 {
166 return blocking_notifier_chain_unregister(&module_notify_list, nb);
167 }
168 EXPORT_SYMBOL(unregister_module_notifier);
169
170 struct load_info {
171 Elf_Ehdr *hdr;
172 unsigned long len;
173 Elf_Shdr *sechdrs;
174 char *secstrings, *strtab;
175 unsigned long symoffs, stroffs;
176 struct _ddebug *debug;
177 unsigned int num_debug;
178 bool sig_ok;
179 struct {
180 unsigned int sym, str, mod, vers, info, pcpu;
181 } index;
182 };
183
184 /* We require a truly strong try_module_get(): 0 means failure due to
185 ongoing or failed initialization etc. */
186 static inline int strong_try_module_get(struct module *mod)
187 {
188 if (mod && mod->state == MODULE_STATE_COMING)
189 return -EBUSY;
190 if (try_module_get(mod))
191 return 0;
192 else
193 return -ENOENT;
194 }
195
196 static inline void add_taint_module(struct module *mod, unsigned flag)
197 {
198 add_taint(flag);
199 mod->taints |= (1U << flag);
200 }
201
202 /*
203 * A thread that wants to hold a reference to a module only while it
204 * is running can call this to safely exit. nfsd and lockd use this.
205 */
206 void __module_put_and_exit(struct module *mod, long code)
207 {
208 module_put(mod);
209 do_exit(code);
210 }
211 EXPORT_SYMBOL(__module_put_and_exit);
212
213 /* Find a module section: 0 means not found. */
214 static unsigned int find_sec(const struct load_info *info, const char *name)
215 {
216 unsigned int i;
217
218 for (i = 1; i < info->hdr->e_shnum; i++) {
219 Elf_Shdr *shdr = &info->sechdrs[i];
220 /* Alloc bit cleared means "ignore it." */
221 if ((shdr->sh_flags & SHF_ALLOC)
222 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
223 return i;
224 }
225 return 0;
226 }
227
228 /* Find a module section, or NULL. */
229 static void *section_addr(const struct load_info *info, const char *name)
230 {
231 /* Section 0 has sh_addr 0. */
232 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
233 }
234
235 /* Find a module section, or NULL. Fill in number of "objects" in section. */
236 static void *section_objs(const struct load_info *info,
237 const char *name,
238 size_t object_size,
239 unsigned int *num)
240 {
241 unsigned int sec = find_sec(info, name);
242
243 /* Section 0 has sh_addr 0 and sh_size 0. */
244 *num = info->sechdrs[sec].sh_size / object_size;
245 return (void *)info->sechdrs[sec].sh_addr;
246 }
247
248 /* Provided by the linker */
249 extern const struct kernel_symbol __start___ksymtab[];
250 extern const struct kernel_symbol __stop___ksymtab[];
251 extern const struct kernel_symbol __start___ksymtab_gpl[];
252 extern const struct kernel_symbol __stop___ksymtab_gpl[];
253 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
254 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
255 extern const unsigned long __start___kcrctab[];
256 extern const unsigned long __start___kcrctab_gpl[];
257 extern const unsigned long __start___kcrctab_gpl_future[];
258 #ifdef CONFIG_UNUSED_SYMBOLS
259 extern const struct kernel_symbol __start___ksymtab_unused[];
260 extern const struct kernel_symbol __stop___ksymtab_unused[];
261 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
262 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
263 extern const unsigned long __start___kcrctab_unused[];
264 extern const unsigned long __start___kcrctab_unused_gpl[];
265 #endif
266
267 #ifndef CONFIG_MODVERSIONS
268 #define symversion(base, idx) NULL
269 #else
270 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
271 #endif
272
273 static bool each_symbol_in_section(const struct symsearch *arr,
274 unsigned int arrsize,
275 struct module *owner,
276 bool (*fn)(const struct symsearch *syms,
277 struct module *owner,
278 void *data),
279 void *data)
280 {
281 unsigned int j;
282
283 for (j = 0; j < arrsize; j++) {
284 if (fn(&arr[j], owner, data))
285 return true;
286 }
287
288 return false;
289 }
290
291 /* Returns true as soon as fn returns true, otherwise false. */
292 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
293 struct module *owner,
294 void *data),
295 void *data)
296 {
297 struct module *mod;
298 static const struct symsearch arr[] = {
299 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
300 NOT_GPL_ONLY, false },
301 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
302 __start___kcrctab_gpl,
303 GPL_ONLY, false },
304 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
305 __start___kcrctab_gpl_future,
306 WILL_BE_GPL_ONLY, false },
307 #ifdef CONFIG_UNUSED_SYMBOLS
308 { __start___ksymtab_unused, __stop___ksymtab_unused,
309 __start___kcrctab_unused,
310 NOT_GPL_ONLY, true },
311 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
312 __start___kcrctab_unused_gpl,
313 GPL_ONLY, true },
314 #endif
315 };
316
317 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
318 return true;
319
320 list_for_each_entry_rcu(mod, &modules, list) {
321 struct symsearch arr[] = {
322 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
323 NOT_GPL_ONLY, false },
324 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
325 mod->gpl_crcs,
326 GPL_ONLY, false },
327 { mod->gpl_future_syms,
328 mod->gpl_future_syms + mod->num_gpl_future_syms,
329 mod->gpl_future_crcs,
330 WILL_BE_GPL_ONLY, false },
331 #ifdef CONFIG_UNUSED_SYMBOLS
332 { mod->unused_syms,
333 mod->unused_syms + mod->num_unused_syms,
334 mod->unused_crcs,
335 NOT_GPL_ONLY, true },
336 { mod->unused_gpl_syms,
337 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
338 mod->unused_gpl_crcs,
339 GPL_ONLY, true },
340 #endif
341 };
342
343 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
344 return true;
345 }
346 return false;
347 }
348 EXPORT_SYMBOL_GPL(each_symbol_section);
349
350 struct find_symbol_arg {
351 /* Input */
352 const char *name;
353 bool gplok;
354 bool warn;
355
356 /* Output */
357 struct module *owner;
358 const unsigned long *crc;
359 const struct kernel_symbol *sym;
360 };
361
362 static bool check_symbol(const struct symsearch *syms,
363 struct module *owner,
364 unsigned int symnum, void *data)
365 {
366 struct find_symbol_arg *fsa = data;
367
368 if (!fsa->gplok) {
369 if (syms->licence == GPL_ONLY)
370 return false;
371 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
372 printk(KERN_WARNING "Symbol %s is being used "
373 "by a non-GPL module, which will not "
374 "be allowed in the future\n", fsa->name);
375 printk(KERN_WARNING "Please see the file "
376 "Documentation/feature-removal-schedule.txt "
377 "in the kernel source tree for more details.\n");
378 }
379 }
380
381 #ifdef CONFIG_UNUSED_SYMBOLS
382 if (syms->unused && fsa->warn) {
383 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
384 "however this module is using it.\n", fsa->name);
385 printk(KERN_WARNING
386 "This symbol will go away in the future.\n");
387 printk(KERN_WARNING
388 "Please evalute if this is the right api to use and if "
389 "it really is, submit a report the linux kernel "
390 "mailinglist together with submitting your code for "
391 "inclusion.\n");
392 }
393 #endif
394
395 fsa->owner = owner;
396 fsa->crc = symversion(syms->crcs, symnum);
397 fsa->sym = &syms->start[symnum];
398 return true;
399 }
400
401 static int cmp_name(const void *va, const void *vb)
402 {
403 const char *a;
404 const struct kernel_symbol *b;
405 a = va; b = vb;
406 return strcmp(a, b->name);
407 }
408
409 static bool find_symbol_in_section(const struct symsearch *syms,
410 struct module *owner,
411 void *data)
412 {
413 struct find_symbol_arg *fsa = data;
414 struct kernel_symbol *sym;
415
416 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
417 sizeof(struct kernel_symbol), cmp_name);
418
419 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
420 return true;
421
422 return false;
423 }
424
425 /* Find a symbol and return it, along with, (optional) crc and
426 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
427 const struct kernel_symbol *find_symbol(const char *name,
428 struct module **owner,
429 const unsigned long **crc,
430 bool gplok,
431 bool warn)
432 {
433 struct find_symbol_arg fsa;
434
435 fsa.name = name;
436 fsa.gplok = gplok;
437 fsa.warn = warn;
438
439 if (each_symbol_section(find_symbol_in_section, &fsa)) {
440 if (owner)
441 *owner = fsa.owner;
442 if (crc)
443 *crc = fsa.crc;
444 return fsa.sym;
445 }
446
447 pr_debug("Failed to find symbol %s\n", name);
448 return NULL;
449 }
450 EXPORT_SYMBOL_GPL(find_symbol);
451
452 /* Search for module by name: must hold module_mutex. */
453 struct module *find_module(const char *name)
454 {
455 struct module *mod;
456
457 list_for_each_entry(mod, &modules, list) {
458 if (strcmp(mod->name, name) == 0)
459 return mod;
460 }
461 return NULL;
462 }
463 EXPORT_SYMBOL_GPL(find_module);
464
465 #ifdef CONFIG_SMP
466
467 static inline void __percpu *mod_percpu(struct module *mod)
468 {
469 return mod->percpu;
470 }
471
472 static int percpu_modalloc(struct module *mod,
473 unsigned long size, unsigned long align)
474 {
475 if (align > PAGE_SIZE) {
476 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
477 mod->name, align, PAGE_SIZE);
478 align = PAGE_SIZE;
479 }
480
481 mod->percpu = __alloc_reserved_percpu(size, align);
482 if (!mod->percpu) {
483 printk(KERN_WARNING
484 "%s: Could not allocate %lu bytes percpu data\n",
485 mod->name, size);
486 return -ENOMEM;
487 }
488 mod->percpu_size = size;
489 return 0;
490 }
491
492 static void percpu_modfree(struct module *mod)
493 {
494 free_percpu(mod->percpu);
495 }
496
497 static unsigned int find_pcpusec(struct load_info *info)
498 {
499 return find_sec(info, ".data..percpu");
500 }
501
502 static void percpu_modcopy(struct module *mod,
503 const void *from, unsigned long size)
504 {
505 int cpu;
506
507 for_each_possible_cpu(cpu)
508 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
509 }
510
511 /**
512 * is_module_percpu_address - test whether address is from module static percpu
513 * @addr: address to test
514 *
515 * Test whether @addr belongs to module static percpu area.
516 *
517 * RETURNS:
518 * %true if @addr is from module static percpu area
519 */
520 bool is_module_percpu_address(unsigned long addr)
521 {
522 struct module *mod;
523 unsigned int cpu;
524
525 preempt_disable();
526
527 list_for_each_entry_rcu(mod, &modules, list) {
528 if (!mod->percpu_size)
529 continue;
530 for_each_possible_cpu(cpu) {
531 void *start = per_cpu_ptr(mod->percpu, cpu);
532
533 if ((void *)addr >= start &&
534 (void *)addr < start + mod->percpu_size) {
535 preempt_enable();
536 return true;
537 }
538 }
539 }
540
541 preempt_enable();
542 return false;
543 }
544
545 #else /* ... !CONFIG_SMP */
546
547 static inline void __percpu *mod_percpu(struct module *mod)
548 {
549 return NULL;
550 }
551 static inline int percpu_modalloc(struct module *mod,
552 unsigned long size, unsigned long align)
553 {
554 return -ENOMEM;
555 }
556 static inline void percpu_modfree(struct module *mod)
557 {
558 }
559 static unsigned int find_pcpusec(struct load_info *info)
560 {
561 return 0;
562 }
563 static inline void percpu_modcopy(struct module *mod,
564 const void *from, unsigned long size)
565 {
566 /* pcpusec should be 0, and size of that section should be 0. */
567 BUG_ON(size != 0);
568 }
569 bool is_module_percpu_address(unsigned long addr)
570 {
571 return false;
572 }
573
574 #endif /* CONFIG_SMP */
575
576 #define MODINFO_ATTR(field) \
577 static void setup_modinfo_##field(struct module *mod, const char *s) \
578 { \
579 mod->field = kstrdup(s, GFP_KERNEL); \
580 } \
581 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
582 struct module_kobject *mk, char *buffer) \
583 { \
584 return sprintf(buffer, "%s\n", mk->mod->field); \
585 } \
586 static int modinfo_##field##_exists(struct module *mod) \
587 { \
588 return mod->field != NULL; \
589 } \
590 static void free_modinfo_##field(struct module *mod) \
591 { \
592 kfree(mod->field); \
593 mod->field = NULL; \
594 } \
595 static struct module_attribute modinfo_##field = { \
596 .attr = { .name = __stringify(field), .mode = 0444 }, \
597 .show = show_modinfo_##field, \
598 .setup = setup_modinfo_##field, \
599 .test = modinfo_##field##_exists, \
600 .free = free_modinfo_##field, \
601 };
602
603 MODINFO_ATTR(version);
604 MODINFO_ATTR(srcversion);
605
606 static char last_unloaded_module[MODULE_NAME_LEN+1];
607
608 #ifdef CONFIG_MODULE_UNLOAD
609
610 EXPORT_TRACEPOINT_SYMBOL(module_get);
611
612 /* Init the unload section of the module. */
613 static int module_unload_init(struct module *mod)
614 {
615 mod->refptr = alloc_percpu(struct module_ref);
616 if (!mod->refptr)
617 return -ENOMEM;
618
619 INIT_LIST_HEAD(&mod->source_list);
620 INIT_LIST_HEAD(&mod->target_list);
621
622 /* Hold reference count during initialization. */
623 __this_cpu_write(mod->refptr->incs, 1);
624 /* Backwards compatibility macros put refcount during init. */
625 mod->waiter = current;
626
627 return 0;
628 }
629
630 /* Does a already use b? */
631 static int already_uses(struct module *a, struct module *b)
632 {
633 struct module_use *use;
634
635 list_for_each_entry(use, &b->source_list, source_list) {
636 if (use->source == a) {
637 pr_debug("%s uses %s!\n", a->name, b->name);
638 return 1;
639 }
640 }
641 pr_debug("%s does not use %s!\n", a->name, b->name);
642 return 0;
643 }
644
645 /*
646 * Module a uses b
647 * - we add 'a' as a "source", 'b' as a "target" of module use
648 * - the module_use is added to the list of 'b' sources (so
649 * 'b' can walk the list to see who sourced them), and of 'a'
650 * targets (so 'a' can see what modules it targets).
651 */
652 static int add_module_usage(struct module *a, struct module *b)
653 {
654 struct module_use *use;
655
656 pr_debug("Allocating new usage for %s.\n", a->name);
657 use = kmalloc(sizeof(*use), GFP_ATOMIC);
658 if (!use) {
659 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
660 return -ENOMEM;
661 }
662
663 use->source = a;
664 use->target = b;
665 list_add(&use->source_list, &b->source_list);
666 list_add(&use->target_list, &a->target_list);
667 return 0;
668 }
669
670 /* Module a uses b: caller needs module_mutex() */
671 int ref_module(struct module *a, struct module *b)
672 {
673 int err;
674
675 if (b == NULL || already_uses(a, b))
676 return 0;
677
678 /* If module isn't available, we fail. */
679 err = strong_try_module_get(b);
680 if (err)
681 return err;
682
683 err = add_module_usage(a, b);
684 if (err) {
685 module_put(b);
686 return err;
687 }
688 return 0;
689 }
690 EXPORT_SYMBOL_GPL(ref_module);
691
692 /* Clear the unload stuff of the module. */
693 static void module_unload_free(struct module *mod)
694 {
695 struct module_use *use, *tmp;
696
697 mutex_lock(&module_mutex);
698 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
699 struct module *i = use->target;
700 pr_debug("%s unusing %s\n", mod->name, i->name);
701 module_put(i);
702 list_del(&use->source_list);
703 list_del(&use->target_list);
704 kfree(use);
705 }
706 mutex_unlock(&module_mutex);
707
708 free_percpu(mod->refptr);
709 }
710
711 #ifdef CONFIG_MODULE_FORCE_UNLOAD
712 static inline int try_force_unload(unsigned int flags)
713 {
714 int ret = (flags & O_TRUNC);
715 if (ret)
716 add_taint(TAINT_FORCED_RMMOD);
717 return ret;
718 }
719 #else
720 static inline int try_force_unload(unsigned int flags)
721 {
722 return 0;
723 }
724 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
725
726 struct stopref
727 {
728 struct module *mod;
729 int flags;
730 int *forced;
731 };
732
733 /* Whole machine is stopped with interrupts off when this runs. */
734 static int __try_stop_module(void *_sref)
735 {
736 struct stopref *sref = _sref;
737
738 /* If it's not unused, quit unless we're forcing. */
739 if (module_refcount(sref->mod) != 0) {
740 if (!(*sref->forced = try_force_unload(sref->flags)))
741 return -EWOULDBLOCK;
742 }
743
744 /* Mark it as dying. */
745 sref->mod->state = MODULE_STATE_GOING;
746 return 0;
747 }
748
749 static int try_stop_module(struct module *mod, int flags, int *forced)
750 {
751 if (flags & O_NONBLOCK) {
752 struct stopref sref = { mod, flags, forced };
753
754 return stop_machine(__try_stop_module, &sref, NULL);
755 } else {
756 /* We don't need to stop the machine for this. */
757 mod->state = MODULE_STATE_GOING;
758 synchronize_sched();
759 return 0;
760 }
761 }
762
763 unsigned long module_refcount(struct module *mod)
764 {
765 unsigned long incs = 0, decs = 0;
766 int cpu;
767
768 for_each_possible_cpu(cpu)
769 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
770 /*
771 * ensure the incs are added up after the decs.
772 * module_put ensures incs are visible before decs with smp_wmb.
773 *
774 * This 2-count scheme avoids the situation where the refcount
775 * for CPU0 is read, then CPU0 increments the module refcount,
776 * then CPU1 drops that refcount, then the refcount for CPU1 is
777 * read. We would record a decrement but not its corresponding
778 * increment so we would see a low count (disaster).
779 *
780 * Rare situation? But module_refcount can be preempted, and we
781 * might be tallying up 4096+ CPUs. So it is not impossible.
782 */
783 smp_rmb();
784 for_each_possible_cpu(cpu)
785 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
786 return incs - decs;
787 }
788 EXPORT_SYMBOL(module_refcount);
789
790 /* This exists whether we can unload or not */
791 static void free_module(struct module *mod);
792
793 static void wait_for_zero_refcount(struct module *mod)
794 {
795 /* Since we might sleep for some time, release the mutex first */
796 mutex_unlock(&module_mutex);
797 for (;;) {
798 pr_debug("Looking at refcount...\n");
799 set_current_state(TASK_UNINTERRUPTIBLE);
800 if (module_refcount(mod) == 0)
801 break;
802 schedule();
803 }
804 current->state = TASK_RUNNING;
805 mutex_lock(&module_mutex);
806 }
807
808 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
809 unsigned int, flags)
810 {
811 struct module *mod;
812 char name[MODULE_NAME_LEN];
813 int ret, forced = 0;
814
815 if (!capable(CAP_SYS_MODULE) || modules_disabled)
816 return -EPERM;
817
818 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
819 return -EFAULT;
820 name[MODULE_NAME_LEN-1] = '\0';
821
822 if (mutex_lock_interruptible(&module_mutex) != 0)
823 return -EINTR;
824
825 mod = find_module(name);
826 if (!mod) {
827 ret = -ENOENT;
828 goto out;
829 }
830
831 if (!list_empty(&mod->source_list)) {
832 /* Other modules depend on us: get rid of them first. */
833 ret = -EWOULDBLOCK;
834 goto out;
835 }
836
837 /* Doing init or already dying? */
838 if (mod->state != MODULE_STATE_LIVE) {
839 /* FIXME: if (force), slam module count and wake up
840 waiter --RR */
841 pr_debug("%s already dying\n", mod->name);
842 ret = -EBUSY;
843 goto out;
844 }
845
846 /* If it has an init func, it must have an exit func to unload */
847 if (mod->init && !mod->exit) {
848 forced = try_force_unload(flags);
849 if (!forced) {
850 /* This module can't be removed */
851 ret = -EBUSY;
852 goto out;
853 }
854 }
855
856 /* Set this up before setting mod->state */
857 mod->waiter = current;
858
859 /* Stop the machine so refcounts can't move and disable module. */
860 ret = try_stop_module(mod, flags, &forced);
861 if (ret != 0)
862 goto out;
863
864 /* Never wait if forced. */
865 if (!forced && module_refcount(mod) != 0)
866 wait_for_zero_refcount(mod);
867
868 mutex_unlock(&module_mutex);
869 /* Final destruction now no one is using it. */
870 if (mod->exit != NULL)
871 mod->exit();
872 blocking_notifier_call_chain(&module_notify_list,
873 MODULE_STATE_GOING, mod);
874 async_synchronize_full();
875
876 /* Store the name of the last unloaded module for diagnostic purposes */
877 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
878
879 free_module(mod);
880 return 0;
881 out:
882 mutex_unlock(&module_mutex);
883 return ret;
884 }
885
886 static inline void print_unload_info(struct seq_file *m, struct module *mod)
887 {
888 struct module_use *use;
889 int printed_something = 0;
890
891 seq_printf(m, " %lu ", module_refcount(mod));
892
893 /* Always include a trailing , so userspace can differentiate
894 between this and the old multi-field proc format. */
895 list_for_each_entry(use, &mod->source_list, source_list) {
896 printed_something = 1;
897 seq_printf(m, "%s,", use->source->name);
898 }
899
900 if (mod->init != NULL && mod->exit == NULL) {
901 printed_something = 1;
902 seq_printf(m, "[permanent],");
903 }
904
905 if (!printed_something)
906 seq_printf(m, "-");
907 }
908
909 void __symbol_put(const char *symbol)
910 {
911 struct module *owner;
912
913 preempt_disable();
914 if (!find_symbol(symbol, &owner, NULL, true, false))
915 BUG();
916 module_put(owner);
917 preempt_enable();
918 }
919 EXPORT_SYMBOL(__symbol_put);
920
921 /* Note this assumes addr is a function, which it currently always is. */
922 void symbol_put_addr(void *addr)
923 {
924 struct module *modaddr;
925 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
926
927 if (core_kernel_text(a))
928 return;
929
930 /* module_text_address is safe here: we're supposed to have reference
931 * to module from symbol_get, so it can't go away. */
932 modaddr = __module_text_address(a);
933 BUG_ON(!modaddr);
934 module_put(modaddr);
935 }
936 EXPORT_SYMBOL_GPL(symbol_put_addr);
937
938 static ssize_t show_refcnt(struct module_attribute *mattr,
939 struct module_kobject *mk, char *buffer)
940 {
941 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
942 }
943
944 static struct module_attribute modinfo_refcnt =
945 __ATTR(refcnt, 0444, show_refcnt, NULL);
946
947 void __module_get(struct module *module)
948 {
949 if (module) {
950 preempt_disable();
951 __this_cpu_inc(module->refptr->incs);
952 trace_module_get(module, _RET_IP_);
953 preempt_enable();
954 }
955 }
956 EXPORT_SYMBOL(__module_get);
957
958 bool try_module_get(struct module *module)
959 {
960 bool ret = true;
961
962 if (module) {
963 preempt_disable();
964
965 if (likely(module_is_live(module))) {
966 __this_cpu_inc(module->refptr->incs);
967 trace_module_get(module, _RET_IP_);
968 } else
969 ret = false;
970
971 preempt_enable();
972 }
973 return ret;
974 }
975 EXPORT_SYMBOL(try_module_get);
976
977 void module_put(struct module *module)
978 {
979 if (module) {
980 preempt_disable();
981 smp_wmb(); /* see comment in module_refcount */
982 __this_cpu_inc(module->refptr->decs);
983
984 trace_module_put(module, _RET_IP_);
985 /* Maybe they're waiting for us to drop reference? */
986 if (unlikely(!module_is_live(module)))
987 wake_up_process(module->waiter);
988 preempt_enable();
989 }
990 }
991 EXPORT_SYMBOL(module_put);
992
993 #else /* !CONFIG_MODULE_UNLOAD */
994 static inline void print_unload_info(struct seq_file *m, struct module *mod)
995 {
996 /* We don't know the usage count, or what modules are using. */
997 seq_printf(m, " - -");
998 }
999
1000 static inline void module_unload_free(struct module *mod)
1001 {
1002 }
1003
1004 int ref_module(struct module *a, struct module *b)
1005 {
1006 return strong_try_module_get(b);
1007 }
1008 EXPORT_SYMBOL_GPL(ref_module);
1009
1010 static inline int module_unload_init(struct module *mod)
1011 {
1012 return 0;
1013 }
1014 #endif /* CONFIG_MODULE_UNLOAD */
1015
1016 static size_t module_flags_taint(struct module *mod, char *buf)
1017 {
1018 size_t l = 0;
1019
1020 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1021 buf[l++] = 'P';
1022 if (mod->taints & (1 << TAINT_OOT_MODULE))
1023 buf[l++] = 'O';
1024 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1025 buf[l++] = 'F';
1026 if (mod->taints & (1 << TAINT_CRAP))
1027 buf[l++] = 'C';
1028 /*
1029 * TAINT_FORCED_RMMOD: could be added.
1030 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1031 * apply to modules.
1032 */
1033 return l;
1034 }
1035
1036 static ssize_t show_initstate(struct module_attribute *mattr,
1037 struct module_kobject *mk, char *buffer)
1038 {
1039 const char *state = "unknown";
1040
1041 switch (mk->mod->state) {
1042 case MODULE_STATE_LIVE:
1043 state = "live";
1044 break;
1045 case MODULE_STATE_COMING:
1046 state = "coming";
1047 break;
1048 case MODULE_STATE_GOING:
1049 state = "going";
1050 break;
1051 }
1052 return sprintf(buffer, "%s\n", state);
1053 }
1054
1055 static struct module_attribute modinfo_initstate =
1056 __ATTR(initstate, 0444, show_initstate, NULL);
1057
1058 static ssize_t store_uevent(struct module_attribute *mattr,
1059 struct module_kobject *mk,
1060 const char *buffer, size_t count)
1061 {
1062 enum kobject_action action;
1063
1064 if (kobject_action_type(buffer, count, &action) == 0)
1065 kobject_uevent(&mk->kobj, action);
1066 return count;
1067 }
1068
1069 struct module_attribute module_uevent =
1070 __ATTR(uevent, 0200, NULL, store_uevent);
1071
1072 static ssize_t show_coresize(struct module_attribute *mattr,
1073 struct module_kobject *mk, char *buffer)
1074 {
1075 return sprintf(buffer, "%u\n", mk->mod->core_size);
1076 }
1077
1078 static struct module_attribute modinfo_coresize =
1079 __ATTR(coresize, 0444, show_coresize, NULL);
1080
1081 static ssize_t show_initsize(struct module_attribute *mattr,
1082 struct module_kobject *mk, char *buffer)
1083 {
1084 return sprintf(buffer, "%u\n", mk->mod->init_size);
1085 }
1086
1087 static struct module_attribute modinfo_initsize =
1088 __ATTR(initsize, 0444, show_initsize, NULL);
1089
1090 static ssize_t show_taint(struct module_attribute *mattr,
1091 struct module_kobject *mk, char *buffer)
1092 {
1093 size_t l;
1094
1095 l = module_flags_taint(mk->mod, buffer);
1096 buffer[l++] = '\n';
1097 return l;
1098 }
1099
1100 static struct module_attribute modinfo_taint =
1101 __ATTR(taint, 0444, show_taint, NULL);
1102
1103 static struct module_attribute *modinfo_attrs[] = {
1104 &module_uevent,
1105 &modinfo_version,
1106 &modinfo_srcversion,
1107 &modinfo_initstate,
1108 &modinfo_coresize,
1109 &modinfo_initsize,
1110 &modinfo_taint,
1111 #ifdef CONFIG_MODULE_UNLOAD
1112 &modinfo_refcnt,
1113 #endif
1114 NULL,
1115 };
1116
1117 static const char vermagic[] = VERMAGIC_STRING;
1118
1119 static int try_to_force_load(struct module *mod, const char *reason)
1120 {
1121 #ifdef CONFIG_MODULE_FORCE_LOAD
1122 if (!test_taint(TAINT_FORCED_MODULE))
1123 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1124 mod->name, reason);
1125 add_taint_module(mod, TAINT_FORCED_MODULE);
1126 return 0;
1127 #else
1128 return -ENOEXEC;
1129 #endif
1130 }
1131
1132 #ifdef CONFIG_MODVERSIONS
1133 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1134 static unsigned long maybe_relocated(unsigned long crc,
1135 const struct module *crc_owner)
1136 {
1137 #ifdef ARCH_RELOCATES_KCRCTAB
1138 if (crc_owner == NULL)
1139 return crc - (unsigned long)reloc_start;
1140 #endif
1141 return crc;
1142 }
1143
1144 static int check_version(Elf_Shdr *sechdrs,
1145 unsigned int versindex,
1146 const char *symname,
1147 struct module *mod,
1148 const unsigned long *crc,
1149 const struct module *crc_owner)
1150 {
1151 unsigned int i, num_versions;
1152 struct modversion_info *versions;
1153
1154 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1155 if (!crc)
1156 return 1;
1157
1158 /* No versions at all? modprobe --force does this. */
1159 if (versindex == 0)
1160 return try_to_force_load(mod, symname) == 0;
1161
1162 versions = (void *) sechdrs[versindex].sh_addr;
1163 num_versions = sechdrs[versindex].sh_size
1164 / sizeof(struct modversion_info);
1165
1166 for (i = 0; i < num_versions; i++) {
1167 if (strcmp(versions[i].name, symname) != 0)
1168 continue;
1169
1170 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1171 return 1;
1172 pr_debug("Found checksum %lX vs module %lX\n",
1173 maybe_relocated(*crc, crc_owner), versions[i].crc);
1174 goto bad_version;
1175 }
1176
1177 printk(KERN_WARNING "%s: no symbol version for %s\n",
1178 mod->name, symname);
1179 return 0;
1180
1181 bad_version:
1182 printk("%s: disagrees about version of symbol %s\n",
1183 mod->name, symname);
1184 return 0;
1185 }
1186
1187 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1188 unsigned int versindex,
1189 struct module *mod)
1190 {
1191 const unsigned long *crc;
1192
1193 /* Since this should be found in kernel (which can't be removed),
1194 * no locking is necessary. */
1195 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1196 &crc, true, false))
1197 BUG();
1198 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1199 NULL);
1200 }
1201
1202 /* First part is kernel version, which we ignore if module has crcs. */
1203 static inline int same_magic(const char *amagic, const char *bmagic,
1204 bool has_crcs)
1205 {
1206 if (has_crcs) {
1207 amagic += strcspn(amagic, " ");
1208 bmagic += strcspn(bmagic, " ");
1209 }
1210 return strcmp(amagic, bmagic) == 0;
1211 }
1212 #else
1213 static inline int check_version(Elf_Shdr *sechdrs,
1214 unsigned int versindex,
1215 const char *symname,
1216 struct module *mod,
1217 const unsigned long *crc,
1218 const struct module *crc_owner)
1219 {
1220 return 1;
1221 }
1222
1223 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1224 unsigned int versindex,
1225 struct module *mod)
1226 {
1227 return 1;
1228 }
1229
1230 static inline int same_magic(const char *amagic, const char *bmagic,
1231 bool has_crcs)
1232 {
1233 return strcmp(amagic, bmagic) == 0;
1234 }
1235 #endif /* CONFIG_MODVERSIONS */
1236
1237 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1238 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1239 const struct load_info *info,
1240 const char *name,
1241 char ownername[])
1242 {
1243 struct module *owner;
1244 const struct kernel_symbol *sym;
1245 const unsigned long *crc;
1246 int err;
1247
1248 mutex_lock(&module_mutex);
1249 sym = find_symbol(name, &owner, &crc,
1250 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1251 if (!sym)
1252 goto unlock;
1253
1254 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1255 owner)) {
1256 sym = ERR_PTR(-EINVAL);
1257 goto getname;
1258 }
1259
1260 err = ref_module(mod, owner);
1261 if (err) {
1262 sym = ERR_PTR(err);
1263 goto getname;
1264 }
1265
1266 getname:
1267 /* We must make copy under the lock if we failed to get ref. */
1268 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1269 unlock:
1270 mutex_unlock(&module_mutex);
1271 return sym;
1272 }
1273
1274 static const struct kernel_symbol *
1275 resolve_symbol_wait(struct module *mod,
1276 const struct load_info *info,
1277 const char *name)
1278 {
1279 const struct kernel_symbol *ksym;
1280 char owner[MODULE_NAME_LEN];
1281
1282 if (wait_event_interruptible_timeout(module_wq,
1283 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1284 || PTR_ERR(ksym) != -EBUSY,
1285 30 * HZ) <= 0) {
1286 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1287 mod->name, owner);
1288 }
1289 return ksym;
1290 }
1291
1292 /*
1293 * /sys/module/foo/sections stuff
1294 * J. Corbet <corbet@lwn.net>
1295 */
1296 #ifdef CONFIG_SYSFS
1297
1298 #ifdef CONFIG_KALLSYMS
1299 static inline bool sect_empty(const Elf_Shdr *sect)
1300 {
1301 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1302 }
1303
1304 struct module_sect_attr
1305 {
1306 struct module_attribute mattr;
1307 char *name;
1308 unsigned long address;
1309 };
1310
1311 struct module_sect_attrs
1312 {
1313 struct attribute_group grp;
1314 unsigned int nsections;
1315 struct module_sect_attr attrs[0];
1316 };
1317
1318 static ssize_t module_sect_show(struct module_attribute *mattr,
1319 struct module_kobject *mk, char *buf)
1320 {
1321 struct module_sect_attr *sattr =
1322 container_of(mattr, struct module_sect_attr, mattr);
1323 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
1324 }
1325
1326 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1327 {
1328 unsigned int section;
1329
1330 for (section = 0; section < sect_attrs->nsections; section++)
1331 kfree(sect_attrs->attrs[section].name);
1332 kfree(sect_attrs);
1333 }
1334
1335 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1336 {
1337 unsigned int nloaded = 0, i, size[2];
1338 struct module_sect_attrs *sect_attrs;
1339 struct module_sect_attr *sattr;
1340 struct attribute **gattr;
1341
1342 /* Count loaded sections and allocate structures */
1343 for (i = 0; i < info->hdr->e_shnum; i++)
1344 if (!sect_empty(&info->sechdrs[i]))
1345 nloaded++;
1346 size[0] = ALIGN(sizeof(*sect_attrs)
1347 + nloaded * sizeof(sect_attrs->attrs[0]),
1348 sizeof(sect_attrs->grp.attrs[0]));
1349 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1350 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1351 if (sect_attrs == NULL)
1352 return;
1353
1354 /* Setup section attributes. */
1355 sect_attrs->grp.name = "sections";
1356 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1357
1358 sect_attrs->nsections = 0;
1359 sattr = &sect_attrs->attrs[0];
1360 gattr = &sect_attrs->grp.attrs[0];
1361 for (i = 0; i < info->hdr->e_shnum; i++) {
1362 Elf_Shdr *sec = &info->sechdrs[i];
1363 if (sect_empty(sec))
1364 continue;
1365 sattr->address = sec->sh_addr;
1366 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1367 GFP_KERNEL);
1368 if (sattr->name == NULL)
1369 goto out;
1370 sect_attrs->nsections++;
1371 sysfs_attr_init(&sattr->mattr.attr);
1372 sattr->mattr.show = module_sect_show;
1373 sattr->mattr.store = NULL;
1374 sattr->mattr.attr.name = sattr->name;
1375 sattr->mattr.attr.mode = S_IRUGO;
1376 *(gattr++) = &(sattr++)->mattr.attr;
1377 }
1378 *gattr = NULL;
1379
1380 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1381 goto out;
1382
1383 mod->sect_attrs = sect_attrs;
1384 return;
1385 out:
1386 free_sect_attrs(sect_attrs);
1387 }
1388
1389 static void remove_sect_attrs(struct module *mod)
1390 {
1391 if (mod->sect_attrs) {
1392 sysfs_remove_group(&mod->mkobj.kobj,
1393 &mod->sect_attrs->grp);
1394 /* We are positive that no one is using any sect attrs
1395 * at this point. Deallocate immediately. */
1396 free_sect_attrs(mod->sect_attrs);
1397 mod->sect_attrs = NULL;
1398 }
1399 }
1400
1401 /*
1402 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1403 */
1404
1405 struct module_notes_attrs {
1406 struct kobject *dir;
1407 unsigned int notes;
1408 struct bin_attribute attrs[0];
1409 };
1410
1411 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1412 struct bin_attribute *bin_attr,
1413 char *buf, loff_t pos, size_t count)
1414 {
1415 /*
1416 * The caller checked the pos and count against our size.
1417 */
1418 memcpy(buf, bin_attr->private + pos, count);
1419 return count;
1420 }
1421
1422 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1423 unsigned int i)
1424 {
1425 if (notes_attrs->dir) {
1426 while (i-- > 0)
1427 sysfs_remove_bin_file(notes_attrs->dir,
1428 &notes_attrs->attrs[i]);
1429 kobject_put(notes_attrs->dir);
1430 }
1431 kfree(notes_attrs);
1432 }
1433
1434 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1435 {
1436 unsigned int notes, loaded, i;
1437 struct module_notes_attrs *notes_attrs;
1438 struct bin_attribute *nattr;
1439
1440 /* failed to create section attributes, so can't create notes */
1441 if (!mod->sect_attrs)
1442 return;
1443
1444 /* Count notes sections and allocate structures. */
1445 notes = 0;
1446 for (i = 0; i < info->hdr->e_shnum; i++)
1447 if (!sect_empty(&info->sechdrs[i]) &&
1448 (info->sechdrs[i].sh_type == SHT_NOTE))
1449 ++notes;
1450
1451 if (notes == 0)
1452 return;
1453
1454 notes_attrs = kzalloc(sizeof(*notes_attrs)
1455 + notes * sizeof(notes_attrs->attrs[0]),
1456 GFP_KERNEL);
1457 if (notes_attrs == NULL)
1458 return;
1459
1460 notes_attrs->notes = notes;
1461 nattr = &notes_attrs->attrs[0];
1462 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1463 if (sect_empty(&info->sechdrs[i]))
1464 continue;
1465 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1466 sysfs_bin_attr_init(nattr);
1467 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1468 nattr->attr.mode = S_IRUGO;
1469 nattr->size = info->sechdrs[i].sh_size;
1470 nattr->private = (void *) info->sechdrs[i].sh_addr;
1471 nattr->read = module_notes_read;
1472 ++nattr;
1473 }
1474 ++loaded;
1475 }
1476
1477 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1478 if (!notes_attrs->dir)
1479 goto out;
1480
1481 for (i = 0; i < notes; ++i)
1482 if (sysfs_create_bin_file(notes_attrs->dir,
1483 &notes_attrs->attrs[i]))
1484 goto out;
1485
1486 mod->notes_attrs = notes_attrs;
1487 return;
1488
1489 out:
1490 free_notes_attrs(notes_attrs, i);
1491 }
1492
1493 static void remove_notes_attrs(struct module *mod)
1494 {
1495 if (mod->notes_attrs)
1496 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1497 }
1498
1499 #else
1500
1501 static inline void add_sect_attrs(struct module *mod,
1502 const struct load_info *info)
1503 {
1504 }
1505
1506 static inline void remove_sect_attrs(struct module *mod)
1507 {
1508 }
1509
1510 static inline void add_notes_attrs(struct module *mod,
1511 const struct load_info *info)
1512 {
1513 }
1514
1515 static inline void remove_notes_attrs(struct module *mod)
1516 {
1517 }
1518 #endif /* CONFIG_KALLSYMS */
1519
1520 static void add_usage_links(struct module *mod)
1521 {
1522 #ifdef CONFIG_MODULE_UNLOAD
1523 struct module_use *use;
1524 int nowarn;
1525
1526 mutex_lock(&module_mutex);
1527 list_for_each_entry(use, &mod->target_list, target_list) {
1528 nowarn = sysfs_create_link(use->target->holders_dir,
1529 &mod->mkobj.kobj, mod->name);
1530 }
1531 mutex_unlock(&module_mutex);
1532 #endif
1533 }
1534
1535 static void del_usage_links(struct module *mod)
1536 {
1537 #ifdef CONFIG_MODULE_UNLOAD
1538 struct module_use *use;
1539
1540 mutex_lock(&module_mutex);
1541 list_for_each_entry(use, &mod->target_list, target_list)
1542 sysfs_remove_link(use->target->holders_dir, mod->name);
1543 mutex_unlock(&module_mutex);
1544 #endif
1545 }
1546
1547 static int module_add_modinfo_attrs(struct module *mod)
1548 {
1549 struct module_attribute *attr;
1550 struct module_attribute *temp_attr;
1551 int error = 0;
1552 int i;
1553
1554 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1555 (ARRAY_SIZE(modinfo_attrs) + 1)),
1556 GFP_KERNEL);
1557 if (!mod->modinfo_attrs)
1558 return -ENOMEM;
1559
1560 temp_attr = mod->modinfo_attrs;
1561 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1562 if (!attr->test ||
1563 (attr->test && attr->test(mod))) {
1564 memcpy(temp_attr, attr, sizeof(*temp_attr));
1565 sysfs_attr_init(&temp_attr->attr);
1566 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1567 ++temp_attr;
1568 }
1569 }
1570 return error;
1571 }
1572
1573 static void module_remove_modinfo_attrs(struct module *mod)
1574 {
1575 struct module_attribute *attr;
1576 int i;
1577
1578 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1579 /* pick a field to test for end of list */
1580 if (!attr->attr.name)
1581 break;
1582 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1583 if (attr->free)
1584 attr->free(mod);
1585 }
1586 kfree(mod->modinfo_attrs);
1587 }
1588
1589 static int mod_sysfs_init(struct module *mod)
1590 {
1591 int err;
1592 struct kobject *kobj;
1593
1594 if (!module_sysfs_initialized) {
1595 printk(KERN_ERR "%s: module sysfs not initialized\n",
1596 mod->name);
1597 err = -EINVAL;
1598 goto out;
1599 }
1600
1601 kobj = kset_find_obj(module_kset, mod->name);
1602 if (kobj) {
1603 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1604 kobject_put(kobj);
1605 err = -EINVAL;
1606 goto out;
1607 }
1608
1609 mod->mkobj.mod = mod;
1610
1611 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1612 mod->mkobj.kobj.kset = module_kset;
1613 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1614 "%s", mod->name);
1615 if (err)
1616 kobject_put(&mod->mkobj.kobj);
1617
1618 /* delay uevent until full sysfs population */
1619 out:
1620 return err;
1621 }
1622
1623 static int mod_sysfs_setup(struct module *mod,
1624 const struct load_info *info,
1625 struct kernel_param *kparam,
1626 unsigned int num_params)
1627 {
1628 int err;
1629
1630 err = mod_sysfs_init(mod);
1631 if (err)
1632 goto out;
1633
1634 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1635 if (!mod->holders_dir) {
1636 err = -ENOMEM;
1637 goto out_unreg;
1638 }
1639
1640 err = module_param_sysfs_setup(mod, kparam, num_params);
1641 if (err)
1642 goto out_unreg_holders;
1643
1644 err = module_add_modinfo_attrs(mod);
1645 if (err)
1646 goto out_unreg_param;
1647
1648 add_usage_links(mod);
1649 add_sect_attrs(mod, info);
1650 add_notes_attrs(mod, info);
1651
1652 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1653 return 0;
1654
1655 out_unreg_param:
1656 module_param_sysfs_remove(mod);
1657 out_unreg_holders:
1658 kobject_put(mod->holders_dir);
1659 out_unreg:
1660 kobject_put(&mod->mkobj.kobj);
1661 out:
1662 return err;
1663 }
1664
1665 static void mod_sysfs_fini(struct module *mod)
1666 {
1667 remove_notes_attrs(mod);
1668 remove_sect_attrs(mod);
1669 kobject_put(&mod->mkobj.kobj);
1670 }
1671
1672 #else /* !CONFIG_SYSFS */
1673
1674 static int mod_sysfs_setup(struct module *mod,
1675 const struct load_info *info,
1676 struct kernel_param *kparam,
1677 unsigned int num_params)
1678 {
1679 return 0;
1680 }
1681
1682 static void mod_sysfs_fini(struct module *mod)
1683 {
1684 }
1685
1686 static void module_remove_modinfo_attrs(struct module *mod)
1687 {
1688 }
1689
1690 static void del_usage_links(struct module *mod)
1691 {
1692 }
1693
1694 #endif /* CONFIG_SYSFS */
1695
1696 static void mod_sysfs_teardown(struct module *mod)
1697 {
1698 del_usage_links(mod);
1699 module_remove_modinfo_attrs(mod);
1700 module_param_sysfs_remove(mod);
1701 kobject_put(mod->mkobj.drivers_dir);
1702 kobject_put(mod->holders_dir);
1703 mod_sysfs_fini(mod);
1704 }
1705
1706 /*
1707 * unlink the module with the whole machine is stopped with interrupts off
1708 * - this defends against kallsyms not taking locks
1709 */
1710 static int __unlink_module(void *_mod)
1711 {
1712 struct module *mod = _mod;
1713 list_del(&mod->list);
1714 module_bug_cleanup(mod);
1715 return 0;
1716 }
1717
1718 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1719 /*
1720 * LKM RO/NX protection: protect module's text/ro-data
1721 * from modification and any data from execution.
1722 */
1723 void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1724 {
1725 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1726 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1727
1728 if (end_pfn > begin_pfn)
1729 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1730 }
1731
1732 static void set_section_ro_nx(void *base,
1733 unsigned long text_size,
1734 unsigned long ro_size,
1735 unsigned long total_size)
1736 {
1737 /* begin and end PFNs of the current subsection */
1738 unsigned long begin_pfn;
1739 unsigned long end_pfn;
1740
1741 /*
1742 * Set RO for module text and RO-data:
1743 * - Always protect first page.
1744 * - Do not protect last partial page.
1745 */
1746 if (ro_size > 0)
1747 set_page_attributes(base, base + ro_size, set_memory_ro);
1748
1749 /*
1750 * Set NX permissions for module data:
1751 * - Do not protect first partial page.
1752 * - Always protect last page.
1753 */
1754 if (total_size > text_size) {
1755 begin_pfn = PFN_UP((unsigned long)base + text_size);
1756 end_pfn = PFN_UP((unsigned long)base + total_size);
1757 if (end_pfn > begin_pfn)
1758 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1759 }
1760 }
1761
1762 static void unset_module_core_ro_nx(struct module *mod)
1763 {
1764 set_page_attributes(mod->module_core + mod->core_text_size,
1765 mod->module_core + mod->core_size,
1766 set_memory_x);
1767 set_page_attributes(mod->module_core,
1768 mod->module_core + mod->core_ro_size,
1769 set_memory_rw);
1770 }
1771
1772 static void unset_module_init_ro_nx(struct module *mod)
1773 {
1774 set_page_attributes(mod->module_init + mod->init_text_size,
1775 mod->module_init + mod->init_size,
1776 set_memory_x);
1777 set_page_attributes(mod->module_init,
1778 mod->module_init + mod->init_ro_size,
1779 set_memory_rw);
1780 }
1781
1782 /* Iterate through all modules and set each module's text as RW */
1783 void set_all_modules_text_rw(void)
1784 {
1785 struct module *mod;
1786
1787 mutex_lock(&module_mutex);
1788 list_for_each_entry_rcu(mod, &modules, list) {
1789 if ((mod->module_core) && (mod->core_text_size)) {
1790 set_page_attributes(mod->module_core,
1791 mod->module_core + mod->core_text_size,
1792 set_memory_rw);
1793 }
1794 if ((mod->module_init) && (mod->init_text_size)) {
1795 set_page_attributes(mod->module_init,
1796 mod->module_init + mod->init_text_size,
1797 set_memory_rw);
1798 }
1799 }
1800 mutex_unlock(&module_mutex);
1801 }
1802
1803 /* Iterate through all modules and set each module's text as RO */
1804 void set_all_modules_text_ro(void)
1805 {
1806 struct module *mod;
1807
1808 mutex_lock(&module_mutex);
1809 list_for_each_entry_rcu(mod, &modules, list) {
1810 if ((mod->module_core) && (mod->core_text_size)) {
1811 set_page_attributes(mod->module_core,
1812 mod->module_core + mod->core_text_size,
1813 set_memory_ro);
1814 }
1815 if ((mod->module_init) && (mod->init_text_size)) {
1816 set_page_attributes(mod->module_init,
1817 mod->module_init + mod->init_text_size,
1818 set_memory_ro);
1819 }
1820 }
1821 mutex_unlock(&module_mutex);
1822 }
1823 #else
1824 static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
1825 static void unset_module_core_ro_nx(struct module *mod) { }
1826 static void unset_module_init_ro_nx(struct module *mod) { }
1827 #endif
1828
1829 void __weak module_free(struct module *mod, void *module_region)
1830 {
1831 vfree(module_region);
1832 }
1833
1834 void __weak module_arch_cleanup(struct module *mod)
1835 {
1836 }
1837
1838 /* Free a module, remove from lists, etc. */
1839 static void free_module(struct module *mod)
1840 {
1841 trace_module_free(mod);
1842
1843 /* Delete from various lists */
1844 mutex_lock(&module_mutex);
1845 stop_machine(__unlink_module, mod, NULL);
1846 mutex_unlock(&module_mutex);
1847 mod_sysfs_teardown(mod);
1848
1849 /* Remove dynamic debug info */
1850 ddebug_remove_module(mod->name);
1851
1852 /* Arch-specific cleanup. */
1853 module_arch_cleanup(mod);
1854
1855 /* Module unload stuff */
1856 module_unload_free(mod);
1857
1858 /* Free any allocated parameters. */
1859 destroy_params(mod->kp, mod->num_kp);
1860
1861 /* This may be NULL, but that's OK */
1862 unset_module_init_ro_nx(mod);
1863 module_free(mod, mod->module_init);
1864 kfree(mod->args);
1865 percpu_modfree(mod);
1866
1867 /* Free lock-classes: */
1868 lockdep_free_key_range(mod->module_core, mod->core_size);
1869
1870 /* Finally, free the core (containing the module structure) */
1871 unset_module_core_ro_nx(mod);
1872 module_free(mod, mod->module_core);
1873
1874 #ifdef CONFIG_MPU
1875 update_protections(current->mm);
1876 #endif
1877 }
1878
1879 void *__symbol_get(const char *symbol)
1880 {
1881 struct module *owner;
1882 const struct kernel_symbol *sym;
1883
1884 preempt_disable();
1885 sym = find_symbol(symbol, &owner, NULL, true, true);
1886 if (sym && strong_try_module_get(owner))
1887 sym = NULL;
1888 preempt_enable();
1889
1890 return sym ? (void *)sym->value : NULL;
1891 }
1892 EXPORT_SYMBOL_GPL(__symbol_get);
1893
1894 /*
1895 * Ensure that an exported symbol [global namespace] does not already exist
1896 * in the kernel or in some other module's exported symbol table.
1897 *
1898 * You must hold the module_mutex.
1899 */
1900 static int verify_export_symbols(struct module *mod)
1901 {
1902 unsigned int i;
1903 struct module *owner;
1904 const struct kernel_symbol *s;
1905 struct {
1906 const struct kernel_symbol *sym;
1907 unsigned int num;
1908 } arr[] = {
1909 { mod->syms, mod->num_syms },
1910 { mod->gpl_syms, mod->num_gpl_syms },
1911 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1912 #ifdef CONFIG_UNUSED_SYMBOLS
1913 { mod->unused_syms, mod->num_unused_syms },
1914 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1915 #endif
1916 };
1917
1918 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1919 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1920 if (find_symbol(s->name, &owner, NULL, true, false)) {
1921 printk(KERN_ERR
1922 "%s: exports duplicate symbol %s"
1923 " (owned by %s)\n",
1924 mod->name, s->name, module_name(owner));
1925 return -ENOEXEC;
1926 }
1927 }
1928 }
1929 return 0;
1930 }
1931
1932 /* Change all symbols so that st_value encodes the pointer directly. */
1933 static int simplify_symbols(struct module *mod, const struct load_info *info)
1934 {
1935 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1936 Elf_Sym *sym = (void *)symsec->sh_addr;
1937 unsigned long secbase;
1938 unsigned int i;
1939 int ret = 0;
1940 const struct kernel_symbol *ksym;
1941
1942 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1943 const char *name = info->strtab + sym[i].st_name;
1944
1945 switch (sym[i].st_shndx) {
1946 case SHN_COMMON:
1947 /* We compiled with -fno-common. These are not
1948 supposed to happen. */
1949 pr_debug("Common symbol: %s\n", name);
1950 printk("%s: please compile with -fno-common\n",
1951 mod->name);
1952 ret = -ENOEXEC;
1953 break;
1954
1955 case SHN_ABS:
1956 /* Don't need to do anything */
1957 pr_debug("Absolute symbol: 0x%08lx\n",
1958 (long)sym[i].st_value);
1959 break;
1960
1961 case SHN_UNDEF:
1962 ksym = resolve_symbol_wait(mod, info, name);
1963 /* Ok if resolved. */
1964 if (ksym && !IS_ERR(ksym)) {
1965 sym[i].st_value = ksym->value;
1966 break;
1967 }
1968
1969 /* Ok if weak. */
1970 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1971 break;
1972
1973 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1974 mod->name, name, PTR_ERR(ksym));
1975 ret = PTR_ERR(ksym) ?: -ENOENT;
1976 break;
1977
1978 default:
1979 /* Divert to percpu allocation if a percpu var. */
1980 if (sym[i].st_shndx == info->index.pcpu)
1981 secbase = (unsigned long)mod_percpu(mod);
1982 else
1983 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1984 sym[i].st_value += secbase;
1985 break;
1986 }
1987 }
1988
1989 return ret;
1990 }
1991
1992 static int apply_relocations(struct module *mod, const struct load_info *info)
1993 {
1994 unsigned int i;
1995 int err = 0;
1996
1997 /* Now do relocations. */
1998 for (i = 1; i < info->hdr->e_shnum; i++) {
1999 unsigned int infosec = info->sechdrs[i].sh_info;
2000
2001 /* Not a valid relocation section? */
2002 if (infosec >= info->hdr->e_shnum)
2003 continue;
2004
2005 /* Don't bother with non-allocated sections */
2006 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2007 continue;
2008
2009 if (info->sechdrs[i].sh_type == SHT_REL)
2010 err = apply_relocate(info->sechdrs, info->strtab,
2011 info->index.sym, i, mod);
2012 else if (info->sechdrs[i].sh_type == SHT_RELA)
2013 err = apply_relocate_add(info->sechdrs, info->strtab,
2014 info->index.sym, i, mod);
2015 if (err < 0)
2016 break;
2017 }
2018 return err;
2019 }
2020
2021 /* Additional bytes needed by arch in front of individual sections */
2022 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2023 unsigned int section)
2024 {
2025 /* default implementation just returns zero */
2026 return 0;
2027 }
2028
2029 /* Update size with this section: return offset. */
2030 static long get_offset(struct module *mod, unsigned int *size,
2031 Elf_Shdr *sechdr, unsigned int section)
2032 {
2033 long ret;
2034
2035 *size += arch_mod_section_prepend(mod, section);
2036 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2037 *size = ret + sechdr->sh_size;
2038 return ret;
2039 }
2040
2041 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2042 might -- code, read-only data, read-write data, small data. Tally
2043 sizes, and place the offsets into sh_entsize fields: high bit means it
2044 belongs in init. */
2045 static void layout_sections(struct module *mod, struct load_info *info)
2046 {
2047 static unsigned long const masks[][2] = {
2048 /* NOTE: all executable code must be the first section
2049 * in this array; otherwise modify the text_size
2050 * finder in the two loops below */
2051 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2052 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2053 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2054 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2055 };
2056 unsigned int m, i;
2057
2058 for (i = 0; i < info->hdr->e_shnum; i++)
2059 info->sechdrs[i].sh_entsize = ~0UL;
2060
2061 pr_debug("Core section allocation order:\n");
2062 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2063 for (i = 0; i < info->hdr->e_shnum; ++i) {
2064 Elf_Shdr *s = &info->sechdrs[i];
2065 const char *sname = info->secstrings + s->sh_name;
2066
2067 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2068 || (s->sh_flags & masks[m][1])
2069 || s->sh_entsize != ~0UL
2070 || strstarts(sname, ".init"))
2071 continue;
2072 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
2073 pr_debug("\t%s\n", sname);
2074 }
2075 switch (m) {
2076 case 0: /* executable */
2077 mod->core_size = debug_align(mod->core_size);
2078 mod->core_text_size = mod->core_size;
2079 break;
2080 case 1: /* RO: text and ro-data */
2081 mod->core_size = debug_align(mod->core_size);
2082 mod->core_ro_size = mod->core_size;
2083 break;
2084 case 3: /* whole core */
2085 mod->core_size = debug_align(mod->core_size);
2086 break;
2087 }
2088 }
2089
2090 pr_debug("Init section allocation order:\n");
2091 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2092 for (i = 0; i < info->hdr->e_shnum; ++i) {
2093 Elf_Shdr *s = &info->sechdrs[i];
2094 const char *sname = info->secstrings + s->sh_name;
2095
2096 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2097 || (s->sh_flags & masks[m][1])
2098 || s->sh_entsize != ~0UL
2099 || !strstarts(sname, ".init"))
2100 continue;
2101 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
2102 | INIT_OFFSET_MASK);
2103 pr_debug("\t%s\n", sname);
2104 }
2105 switch (m) {
2106 case 0: /* executable */
2107 mod->init_size = debug_align(mod->init_size);
2108 mod->init_text_size = mod->init_size;
2109 break;
2110 case 1: /* RO: text and ro-data */
2111 mod->init_size = debug_align(mod->init_size);
2112 mod->init_ro_size = mod->init_size;
2113 break;
2114 case 3: /* whole init */
2115 mod->init_size = debug_align(mod->init_size);
2116 break;
2117 }
2118 }
2119 }
2120
2121 static void set_license(struct module *mod, const char *license)
2122 {
2123 if (!license)
2124 license = "unspecified";
2125
2126 if (!license_is_gpl_compatible(license)) {
2127 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2128 printk(KERN_WARNING "%s: module license '%s' taints "
2129 "kernel.\n", mod->name, license);
2130 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2131 }
2132 }
2133
2134 /* Parse tag=value strings from .modinfo section */
2135 static char *next_string(char *string, unsigned long *secsize)
2136 {
2137 /* Skip non-zero chars */
2138 while (string[0]) {
2139 string++;
2140 if ((*secsize)-- <= 1)
2141 return NULL;
2142 }
2143
2144 /* Skip any zero padding. */
2145 while (!string[0]) {
2146 string++;
2147 if ((*secsize)-- <= 1)
2148 return NULL;
2149 }
2150 return string;
2151 }
2152
2153 static char *get_modinfo(struct load_info *info, const char *tag)
2154 {
2155 char *p;
2156 unsigned int taglen = strlen(tag);
2157 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2158 unsigned long size = infosec->sh_size;
2159
2160 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
2161 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2162 return p + taglen + 1;
2163 }
2164 return NULL;
2165 }
2166
2167 static void setup_modinfo(struct module *mod, struct load_info *info)
2168 {
2169 struct module_attribute *attr;
2170 int i;
2171
2172 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2173 if (attr->setup)
2174 attr->setup(mod, get_modinfo(info, attr->attr.name));
2175 }
2176 }
2177
2178 static void free_modinfo(struct module *mod)
2179 {
2180 struct module_attribute *attr;
2181 int i;
2182
2183 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2184 if (attr->free)
2185 attr->free(mod);
2186 }
2187 }
2188
2189 #ifdef CONFIG_KALLSYMS
2190
2191 /* lookup symbol in given range of kernel_symbols */
2192 static const struct kernel_symbol *lookup_symbol(const char *name,
2193 const struct kernel_symbol *start,
2194 const struct kernel_symbol *stop)
2195 {
2196 return bsearch(name, start, stop - start,
2197 sizeof(struct kernel_symbol), cmp_name);
2198 }
2199
2200 static int is_exported(const char *name, unsigned long value,
2201 const struct module *mod)
2202 {
2203 const struct kernel_symbol *ks;
2204 if (!mod)
2205 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2206 else
2207 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2208 return ks != NULL && ks->value == value;
2209 }
2210
2211 /* As per nm */
2212 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2213 {
2214 const Elf_Shdr *sechdrs = info->sechdrs;
2215
2216 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2217 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2218 return 'v';
2219 else
2220 return 'w';
2221 }
2222 if (sym->st_shndx == SHN_UNDEF)
2223 return 'U';
2224 if (sym->st_shndx == SHN_ABS)
2225 return 'a';
2226 if (sym->st_shndx >= SHN_LORESERVE)
2227 return '?';
2228 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2229 return 't';
2230 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2231 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2232 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2233 return 'r';
2234 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2235 return 'g';
2236 else
2237 return 'd';
2238 }
2239 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2240 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2241 return 's';
2242 else
2243 return 'b';
2244 }
2245 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2246 ".debug")) {
2247 return 'n';
2248 }
2249 return '?';
2250 }
2251
2252 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2253 unsigned int shnum)
2254 {
2255 const Elf_Shdr *sec;
2256
2257 if (src->st_shndx == SHN_UNDEF
2258 || src->st_shndx >= shnum
2259 || !src->st_name)
2260 return false;
2261
2262 sec = sechdrs + src->st_shndx;
2263 if (!(sec->sh_flags & SHF_ALLOC)
2264 #ifndef CONFIG_KALLSYMS_ALL
2265 || !(sec->sh_flags & SHF_EXECINSTR)
2266 #endif
2267 || (sec->sh_entsize & INIT_OFFSET_MASK))
2268 return false;
2269
2270 return true;
2271 }
2272
2273 /*
2274 * We only allocate and copy the strings needed by the parts of symtab
2275 * we keep. This is simple, but has the effect of making multiple
2276 * copies of duplicates. We could be more sophisticated, see
2277 * linux-kernel thread starting with
2278 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2279 */
2280 static void layout_symtab(struct module *mod, struct load_info *info)
2281 {
2282 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2283 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2284 const Elf_Sym *src;
2285 unsigned int i, nsrc, ndst, strtab_size;
2286
2287 /* Put symbol section at end of init part of module. */
2288 symsect->sh_flags |= SHF_ALLOC;
2289 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
2290 info->index.sym) | INIT_OFFSET_MASK;
2291 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2292
2293 src = (void *)info->hdr + symsect->sh_offset;
2294 nsrc = symsect->sh_size / sizeof(*src);
2295
2296 /* Compute total space required for the core symbols' strtab. */
2297 for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
2298 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
2299 strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2300 ndst++;
2301 }
2302
2303 /* Append room for core symbols at end of core part. */
2304 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2305 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2306 mod->core_size += strtab_size;
2307
2308 /* Put string table section at end of init part of module. */
2309 strsect->sh_flags |= SHF_ALLOC;
2310 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
2311 info->index.str) | INIT_OFFSET_MASK;
2312 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2313 }
2314
2315 static void add_kallsyms(struct module *mod, const struct load_info *info)
2316 {
2317 unsigned int i, ndst;
2318 const Elf_Sym *src;
2319 Elf_Sym *dst;
2320 char *s;
2321 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2322
2323 mod->symtab = (void *)symsec->sh_addr;
2324 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2325 /* Make sure we get permanent strtab: don't use info->strtab. */
2326 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2327
2328 /* Set types up while we still have access to sections. */
2329 for (i = 0; i < mod->num_symtab; i++)
2330 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
2331
2332 mod->core_symtab = dst = mod->module_core + info->symoffs;
2333 mod->core_strtab = s = mod->module_core + info->stroffs;
2334 src = mod->symtab;
2335 *dst = *src;
2336 *s++ = 0;
2337 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
2338 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
2339 continue;
2340
2341 dst[ndst] = *src;
2342 dst[ndst++].st_name = s - mod->core_strtab;
2343 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
2344 }
2345 mod->core_num_syms = ndst;
2346 }
2347 #else
2348 static inline void layout_symtab(struct module *mod, struct load_info *info)
2349 {
2350 }
2351
2352 static void add_kallsyms(struct module *mod, const struct load_info *info)
2353 {
2354 }
2355 #endif /* CONFIG_KALLSYMS */
2356
2357 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2358 {
2359 if (!debug)
2360 return;
2361 #ifdef CONFIG_DYNAMIC_DEBUG
2362 if (ddebug_add_module(debug, num, debug->modname))
2363 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2364 debug->modname);
2365 #endif
2366 }
2367
2368 static void dynamic_debug_remove(struct _ddebug *debug)
2369 {
2370 if (debug)
2371 ddebug_remove_module(debug->modname);
2372 }
2373
2374 void * __weak module_alloc(unsigned long size)
2375 {
2376 return size == 0 ? NULL : vmalloc_exec(size);
2377 }
2378
2379 static void *module_alloc_update_bounds(unsigned long size)
2380 {
2381 void *ret = module_alloc(size);
2382
2383 if (ret) {
2384 mutex_lock(&module_mutex);
2385 /* Update module bounds. */
2386 if ((unsigned long)ret < module_addr_min)
2387 module_addr_min = (unsigned long)ret;
2388 if ((unsigned long)ret + size > module_addr_max)
2389 module_addr_max = (unsigned long)ret + size;
2390 mutex_unlock(&module_mutex);
2391 }
2392 return ret;
2393 }
2394
2395 #ifdef CONFIG_DEBUG_KMEMLEAK
2396 static void kmemleak_load_module(const struct module *mod,
2397 const struct load_info *info)
2398 {
2399 unsigned int i;
2400
2401 /* only scan the sections containing data */
2402 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2403
2404 for (i = 1; i < info->hdr->e_shnum; i++) {
2405 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2406 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
2407 continue;
2408 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
2409 continue;
2410
2411 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2412 info->sechdrs[i].sh_size, GFP_KERNEL);
2413 }
2414 }
2415 #else
2416 static inline void kmemleak_load_module(const struct module *mod,
2417 const struct load_info *info)
2418 {
2419 }
2420 #endif
2421
2422 #ifdef CONFIG_MODULE_SIG
2423 static int module_sig_check(struct load_info *info,
2424 const void *mod, unsigned long *len)
2425 {
2426 int err = -ENOKEY;
2427 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2428 const void *p = mod, *end = mod + *len;
2429
2430 /* Poor man's memmem. */
2431 while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) {
2432 if (p + markerlen > end)
2433 break;
2434
2435 if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) {
2436 const void *sig = p + markerlen;
2437 /* Truncate module up to signature. */
2438 *len = p - mod;
2439 err = mod_verify_sig(mod, *len, sig, end - sig);
2440 break;
2441 }
2442 p++;
2443 }
2444
2445 if (!err) {
2446 info->sig_ok = true;
2447 return 0;
2448 }
2449
2450 /* Not having a signature is only an error if we're strict. */
2451 if (err < 0 && fips_enabled)
2452 panic("Module verification failed with error %d in FIPS mode\n",
2453 err);
2454 if (err == -ENOKEY && !sig_enforce)
2455 err = 0;
2456
2457 return err;
2458 }
2459 #else /* !CONFIG_MODULE_SIG */
2460 static int module_sig_check(struct load_info *info,
2461 void *mod, unsigned long *len)
2462 {
2463 return 0;
2464 }
2465 #endif /* !CONFIG_MODULE_SIG */
2466
2467 /* Sets info->hdr, info->len and info->sig_ok. */
2468 static int copy_and_check(struct load_info *info,
2469 const void __user *umod, unsigned long len,
2470 const char __user *uargs)
2471 {
2472 int err;
2473 Elf_Ehdr *hdr;
2474
2475 if (len < sizeof(*hdr))
2476 return -ENOEXEC;
2477
2478 /* Suck in entire file: we'll want most of it. */
2479 if ((hdr = vmalloc(len)) == NULL)
2480 return -ENOMEM;
2481
2482 if (copy_from_user(hdr, umod, len) != 0) {
2483 err = -EFAULT;
2484 goto free_hdr;
2485 }
2486
2487 err = module_sig_check(info, hdr, &len);
2488 if (err)
2489 goto free_hdr;
2490
2491 /* Sanity checks against insmoding binaries or wrong arch,
2492 weird elf version */
2493 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2494 || hdr->e_type != ET_REL
2495 || !elf_check_arch(hdr)
2496 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2497 err = -ENOEXEC;
2498 goto free_hdr;
2499 }
2500
2501 if (hdr->e_shoff >= len ||
2502 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) {
2503 err = -ENOEXEC;
2504 goto free_hdr;
2505 }
2506
2507 info->hdr = hdr;
2508 info->len = len;
2509 return 0;
2510
2511 free_hdr:
2512 vfree(hdr);
2513 return err;
2514 }
2515
2516 static void free_copy(struct load_info *info)
2517 {
2518 vfree(info->hdr);
2519 }
2520
2521 static int rewrite_section_headers(struct load_info *info)
2522 {
2523 unsigned int i;
2524
2525 /* This should always be true, but let's be sure. */
2526 info->sechdrs[0].sh_addr = 0;
2527
2528 for (i = 1; i < info->hdr->e_shnum; i++) {
2529 Elf_Shdr *shdr = &info->sechdrs[i];
2530 if (shdr->sh_type != SHT_NOBITS
2531 && info->len < shdr->sh_offset + shdr->sh_size) {
2532 printk(KERN_ERR "Module len %lu truncated\n",
2533 info->len);
2534 return -ENOEXEC;
2535 }
2536
2537 /* Mark all sections sh_addr with their address in the
2538 temporary image. */
2539 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2540
2541 #ifndef CONFIG_MODULE_UNLOAD
2542 /* Don't load .exit sections */
2543 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2544 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2545 #endif
2546 }
2547
2548 /* Track but don't keep modinfo and version sections. */
2549 info->index.vers = find_sec(info, "__versions");
2550 info->index.info = find_sec(info, ".modinfo");
2551 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2552 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2553 return 0;
2554 }
2555
2556 /*
2557 * Set up our basic convenience variables (pointers to section headers,
2558 * search for module section index etc), and do some basic section
2559 * verification.
2560 *
2561 * Return the temporary module pointer (we'll replace it with the final
2562 * one when we move the module sections around).
2563 */
2564 static struct module *setup_load_info(struct load_info *info)
2565 {
2566 unsigned int i;
2567 int err;
2568 struct module *mod;
2569
2570 /* Set up the convenience variables */
2571 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2572 info->secstrings = (void *)info->hdr
2573 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2574
2575 err = rewrite_section_headers(info);
2576 if (err)
2577 return ERR_PTR(err);
2578
2579 /* Find internal symbols and strings. */
2580 for (i = 1; i < info->hdr->e_shnum; i++) {
2581 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2582 info->index.sym = i;
2583 info->index.str = info->sechdrs[i].sh_link;
2584 info->strtab = (char *)info->hdr
2585 + info->sechdrs[info->index.str].sh_offset;
2586 break;
2587 }
2588 }
2589
2590 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2591 if (!info->index.mod) {
2592 printk(KERN_WARNING "No module found in object\n");
2593 return ERR_PTR(-ENOEXEC);
2594 }
2595 /* This is temporary: point mod into copy of data. */
2596 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2597
2598 if (info->index.sym == 0) {
2599 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2600 mod->name);
2601 return ERR_PTR(-ENOEXEC);
2602 }
2603
2604 info->index.pcpu = find_pcpusec(info);
2605
2606 /* Check module struct version now, before we try to use module. */
2607 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2608 return ERR_PTR(-ENOEXEC);
2609
2610 return mod;
2611 }
2612
2613 static int check_modinfo(struct module *mod, struct load_info *info)
2614 {
2615 const char *modmagic = get_modinfo(info, "vermagic");
2616 int err;
2617
2618 /* This is allowed: modprobe --force will invalidate it. */
2619 if (!modmagic) {
2620 err = try_to_force_load(mod, "bad vermagic");
2621 if (err)
2622 return err;
2623 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2624 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2625 mod->name, modmagic, vermagic);
2626 return -ENOEXEC;
2627 }
2628
2629 if (!get_modinfo(info, "intree"))
2630 add_taint_module(mod, TAINT_OOT_MODULE);
2631
2632 if (get_modinfo(info, "staging")) {
2633 add_taint_module(mod, TAINT_CRAP);
2634 printk(KERN_WARNING "%s: module is from the staging directory,"
2635 " the quality is unknown, you have been warned.\n",
2636 mod->name);
2637 }
2638
2639 /* Set up license info based on the info section */
2640 set_license(mod, get_modinfo(info, "license"));
2641
2642 return 0;
2643 }
2644
2645 static void find_module_sections(struct module *mod, struct load_info *info)
2646 {
2647 mod->kp = section_objs(info, "__param",
2648 sizeof(*mod->kp), &mod->num_kp);
2649 mod->syms = section_objs(info, "__ksymtab",
2650 sizeof(*mod->syms), &mod->num_syms);
2651 mod->crcs = section_addr(info, "__kcrctab");
2652 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2653 sizeof(*mod->gpl_syms),
2654 &mod->num_gpl_syms);
2655 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2656 mod->gpl_future_syms = section_objs(info,
2657 "__ksymtab_gpl_future",
2658 sizeof(*mod->gpl_future_syms),
2659 &mod->num_gpl_future_syms);
2660 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2661
2662 #ifdef CONFIG_UNUSED_SYMBOLS
2663 mod->unused_syms = section_objs(info, "__ksymtab_unused",
2664 sizeof(*mod->unused_syms),
2665 &mod->num_unused_syms);
2666 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2667 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2668 sizeof(*mod->unused_gpl_syms),
2669 &mod->num_unused_gpl_syms);
2670 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2671 #endif
2672 #ifdef CONFIG_CONSTRUCTORS
2673 mod->ctors = section_objs(info, ".ctors",
2674 sizeof(*mod->ctors), &mod->num_ctors);
2675 #endif
2676
2677 #ifdef CONFIG_TRACEPOINTS
2678 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2679 sizeof(*mod->tracepoints_ptrs),
2680 &mod->num_tracepoints);
2681 #endif
2682 #ifdef HAVE_JUMP_LABEL
2683 mod->jump_entries = section_objs(info, "__jump_table",
2684 sizeof(*mod->jump_entries),
2685 &mod->num_jump_entries);
2686 #endif
2687 #ifdef CONFIG_EVENT_TRACING
2688 mod->trace_events = section_objs(info, "_ftrace_events",
2689 sizeof(*mod->trace_events),
2690 &mod->num_trace_events);
2691 /*
2692 * This section contains pointers to allocated objects in the trace
2693 * code and not scanning it leads to false positives.
2694 */
2695 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2696 mod->num_trace_events, GFP_KERNEL);
2697 #endif
2698 #ifdef CONFIG_TRACING
2699 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2700 sizeof(*mod->trace_bprintk_fmt_start),
2701 &mod->num_trace_bprintk_fmt);
2702 /*
2703 * This section contains pointers to allocated objects in the trace
2704 * code and not scanning it leads to false positives.
2705 */
2706 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2707 sizeof(*mod->trace_bprintk_fmt_start) *
2708 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2709 #endif
2710 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2711 /* sechdrs[0].sh_size is always zero */
2712 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2713 sizeof(*mod->ftrace_callsites),
2714 &mod->num_ftrace_callsites);
2715 #endif
2716
2717 mod->extable = section_objs(info, "__ex_table",
2718 sizeof(*mod->extable), &mod->num_exentries);
2719
2720 if (section_addr(info, "__obsparm"))
2721 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2722 mod->name);
2723
2724 info->debug = section_objs(info, "__verbose",
2725 sizeof(*info->debug), &info->num_debug);
2726 }
2727
2728 static int move_module(struct module *mod, struct load_info *info)
2729 {
2730 int i;
2731 void *ptr;
2732
2733 /* Do the allocs. */
2734 ptr = module_alloc_update_bounds(mod->core_size);
2735 /*
2736 * The pointer to this block is stored in the module structure
2737 * which is inside the block. Just mark it as not being a
2738 * leak.
2739 */
2740 kmemleak_not_leak(ptr);
2741 if (!ptr)
2742 return -ENOMEM;
2743
2744 memset(ptr, 0, mod->core_size);
2745 mod->module_core = ptr;
2746
2747 ptr = module_alloc_update_bounds(mod->init_size);
2748 /*
2749 * The pointer to this block is stored in the module structure
2750 * which is inside the block. This block doesn't need to be
2751 * scanned as it contains data and code that will be freed
2752 * after the module is initialized.
2753 */
2754 kmemleak_ignore(ptr);
2755 if (!ptr && mod->init_size) {
2756 module_free(mod, mod->module_core);
2757 return -ENOMEM;
2758 }
2759 memset(ptr, 0, mod->init_size);
2760 mod->module_init = ptr;
2761
2762 /* Transfer each section which specifies SHF_ALLOC */
2763 pr_debug("final section addresses:\n");
2764 for (i = 0; i < info->hdr->e_shnum; i++) {
2765 void *dest;
2766 Elf_Shdr *shdr = &info->sechdrs[i];
2767
2768 if (!(shdr->sh_flags & SHF_ALLOC))
2769 continue;
2770
2771 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2772 dest = mod->module_init
2773 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2774 else
2775 dest = mod->module_core + shdr->sh_entsize;
2776
2777 if (shdr->sh_type != SHT_NOBITS)
2778 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2779 /* Update sh_addr to point to copy in image. */
2780 shdr->sh_addr = (unsigned long)dest;
2781 pr_debug("\t0x%lx %s\n",
2782 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
2783 }
2784
2785 return 0;
2786 }
2787
2788 static int check_module_license_and_versions(struct module *mod)
2789 {
2790 /*
2791 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2792 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2793 * using GPL-only symbols it needs.
2794 */
2795 if (strcmp(mod->name, "ndiswrapper") == 0)
2796 add_taint(TAINT_PROPRIETARY_MODULE);
2797
2798 /* driverloader was caught wrongly pretending to be under GPL */
2799 if (strcmp(mod->name, "driverloader") == 0)
2800 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2801
2802 /* lve claims to be GPL but upstream won't provide source */
2803 if (strcmp(mod->name, "lve") == 0)
2804 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2805
2806 #ifdef CONFIG_MODVERSIONS
2807 if ((mod->num_syms && !mod->crcs)
2808 || (mod->num_gpl_syms && !mod->gpl_crcs)
2809 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2810 #ifdef CONFIG_UNUSED_SYMBOLS
2811 || (mod->num_unused_syms && !mod->unused_crcs)
2812 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2813 #endif
2814 ) {
2815 return try_to_force_load(mod,
2816 "no versions for exported symbols");
2817 }
2818 #endif
2819 return 0;
2820 }
2821
2822 static void flush_module_icache(const struct module *mod)
2823 {
2824 mm_segment_t old_fs;
2825
2826 /* flush the icache in correct context */
2827 old_fs = get_fs();
2828 set_fs(KERNEL_DS);
2829
2830 /*
2831 * Flush the instruction cache, since we've played with text.
2832 * Do it before processing of module parameters, so the module
2833 * can provide parameter accessor functions of its own.
2834 */
2835 if (mod->module_init)
2836 flush_icache_range((unsigned long)mod->module_init,
2837 (unsigned long)mod->module_init
2838 + mod->init_size);
2839 flush_icache_range((unsigned long)mod->module_core,
2840 (unsigned long)mod->module_core + mod->core_size);
2841
2842 set_fs(old_fs);
2843 }
2844
2845 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2846 Elf_Shdr *sechdrs,
2847 char *secstrings,
2848 struct module *mod)
2849 {
2850 return 0;
2851 }
2852
2853 static struct module *layout_and_allocate(struct load_info *info)
2854 {
2855 /* Module within temporary copy. */
2856 struct module *mod;
2857 Elf_Shdr *pcpusec;
2858 int err;
2859
2860 mod = setup_load_info(info);
2861 if (IS_ERR(mod))
2862 return mod;
2863
2864 err = check_modinfo(mod, info);
2865 if (err)
2866 return ERR_PTR(err);
2867
2868 /* Allow arches to frob section contents and sizes. */
2869 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2870 info->secstrings, mod);
2871 if (err < 0)
2872 goto out;
2873
2874 pcpusec = &info->sechdrs[info->index.pcpu];
2875 if (pcpusec->sh_size) {
2876 /* We have a special allocation for this section. */
2877 err = percpu_modalloc(mod,
2878 pcpusec->sh_size, pcpusec->sh_addralign);
2879 if (err)
2880 goto out;
2881 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2882 }
2883
2884 /* Determine total sizes, and put offsets in sh_entsize. For now
2885 this is done generically; there doesn't appear to be any
2886 special cases for the architectures. */
2887 layout_sections(mod, info);
2888 layout_symtab(mod, info);
2889
2890 /* Allocate and move to the final place */
2891 err = move_module(mod, info);
2892 if (err)
2893 goto free_percpu;
2894
2895 /* Module has been copied to its final place now: return it. */
2896 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2897 kmemleak_load_module(mod, info);
2898 return mod;
2899
2900 free_percpu:
2901 percpu_modfree(mod);
2902 out:
2903 return ERR_PTR(err);
2904 }
2905
2906 /* mod is no longer valid after this! */
2907 static void module_deallocate(struct module *mod, struct load_info *info)
2908 {
2909 percpu_modfree(mod);
2910 module_free(mod, mod->module_init);
2911 module_free(mod, mod->module_core);
2912 }
2913
2914 int __weak module_finalize(const Elf_Ehdr *hdr,
2915 const Elf_Shdr *sechdrs,
2916 struct module *me)
2917 {
2918 return 0;
2919 }
2920
2921 static int post_relocation(struct module *mod, const struct load_info *info)
2922 {
2923 /* Sort exception table now relocations are done. */
2924 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2925
2926 /* Copy relocated percpu area over. */
2927 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2928 info->sechdrs[info->index.pcpu].sh_size);
2929
2930 /* Setup kallsyms-specific fields. */
2931 add_kallsyms(mod, info);
2932
2933 /* Arch-specific module finalizing. */
2934 return module_finalize(info->hdr, info->sechdrs, mod);
2935 }
2936
2937 /* Is this module of this name done loading? No locks held. */
2938 static bool finished_loading(const char *name)
2939 {
2940 struct module *mod;
2941 bool ret;
2942
2943 mutex_lock(&module_mutex);
2944 mod = find_module(name);
2945 ret = !mod || mod->state != MODULE_STATE_COMING;
2946 mutex_unlock(&module_mutex);
2947
2948 return ret;
2949 }
2950
2951 /* Allocate and load the module: note that size of section 0 is always
2952 zero, and we rely on this for optional sections. */
2953 static struct module *load_module(void __user *umod,
2954 unsigned long len,
2955 const char __user *uargs)
2956 {
2957 struct load_info info = { NULL, };
2958 struct module *mod, *old;
2959 long err;
2960
2961 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
2962 umod, len, uargs);
2963
2964 /* Copy in the blobs from userspace, check they are vaguely sane. */
2965 err = copy_and_check(&info, umod, len, uargs);
2966 if (err)
2967 return ERR_PTR(err);
2968
2969 /* Figure out module layout, and allocate all the memory. */
2970 mod = layout_and_allocate(&info);
2971 if (IS_ERR(mod)) {
2972 err = PTR_ERR(mod);
2973 goto free_copy;
2974 }
2975
2976 #ifdef CONFIG_MODULE_SIG
2977 mod->sig_ok = info.sig_ok;
2978 if (!mod->sig_ok)
2979 add_taint_module(mod, TAINT_FORCED_MODULE);
2980 #endif
2981
2982 /* Now module is in final location, initialize linked lists, etc. */
2983 err = module_unload_init(mod);
2984 if (err)
2985 goto free_module;
2986
2987 /* Now we've got everything in the final locations, we can
2988 * find optional sections. */
2989 find_module_sections(mod, &info);
2990
2991 err = check_module_license_and_versions(mod);
2992 if (err)
2993 goto free_unload;
2994
2995 /* Set up MODINFO_ATTR fields */
2996 setup_modinfo(mod, &info);
2997
2998 /* Fix up syms, so that st_value is a pointer to location. */
2999 err = simplify_symbols(mod, &info);
3000 if (err < 0)
3001 goto free_modinfo;
3002
3003 err = apply_relocations(mod, &info);
3004 if (err < 0)
3005 goto free_modinfo;
3006
3007 err = post_relocation(mod, &info);
3008 if (err < 0)
3009 goto free_modinfo;
3010
3011 flush_module_icache(mod);
3012
3013 /* Now copy in args */
3014 mod->args = strndup_user(uargs, ~0UL >> 1);
3015 if (IS_ERR(mod->args)) {
3016 err = PTR_ERR(mod->args);
3017 goto free_arch_cleanup;
3018 }
3019
3020 /* Mark state as coming so strong_try_module_get() ignores us. */
3021 mod->state = MODULE_STATE_COMING;
3022
3023 /* Now sew it into the lists so we can get lockdep and oops
3024 * info during argument parsing. No one should access us, since
3025 * strong_try_module_get() will fail.
3026 * lockdep/oops can run asynchronous, so use the RCU list insertion
3027 * function to insert in a way safe to concurrent readers.
3028 * The mutex protects against concurrent writers.
3029 */
3030 again:
3031 mutex_lock(&module_mutex);
3032 if ((old = find_module(mod->name)) != NULL) {
3033 if (old->state == MODULE_STATE_COMING) {
3034 /* Wait in case it fails to load. */
3035 mutex_unlock(&module_mutex);
3036 err = wait_event_interruptible(module_wq,
3037 finished_loading(mod->name));
3038 if (err)
3039 goto free_arch_cleanup;
3040 goto again;
3041 }
3042 err = -EEXIST;
3043 goto unlock;
3044 }
3045
3046 /* This has to be done once we're sure module name is unique. */
3047 dynamic_debug_setup(info.debug, info.num_debug);
3048
3049 /* Find duplicate symbols */
3050 err = verify_export_symbols(mod);
3051 if (err < 0)
3052 goto ddebug;
3053
3054 module_bug_finalize(info.hdr, info.sechdrs, mod);
3055 list_add_rcu(&mod->list, &modules);
3056 mutex_unlock(&module_mutex);
3057
3058 /* Module is ready to execute: parsing args may do that. */
3059 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3060 -32768, 32767, &ddebug_dyndbg_module_param_cb);
3061 if (err < 0)
3062 goto unlink;
3063
3064 /* Link in to syfs. */
3065 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
3066 if (err < 0)
3067 goto unlink;
3068
3069 /* Get rid of temporary copy. */
3070 free_copy(&info);
3071
3072 /* Done! */
3073 trace_module_load(mod);
3074 return mod;
3075
3076 unlink:
3077 mutex_lock(&module_mutex);
3078 /* Unlink carefully: kallsyms could be walking list. */
3079 list_del_rcu(&mod->list);
3080 module_bug_cleanup(mod);
3081 wake_up_all(&module_wq);
3082 ddebug:
3083 dynamic_debug_remove(info.debug);
3084 unlock:
3085 mutex_unlock(&module_mutex);
3086 synchronize_sched();
3087 kfree(mod->args);
3088 free_arch_cleanup:
3089 module_arch_cleanup(mod);
3090 free_modinfo:
3091 free_modinfo(mod);
3092 free_unload:
3093 module_unload_free(mod);
3094 free_module:
3095 module_deallocate(mod, &info);
3096 free_copy:
3097 free_copy(&info);
3098 return ERR_PTR(err);
3099 }
3100
3101 /* Call module constructors. */
3102 static void do_mod_ctors(struct module *mod)
3103 {
3104 #ifdef CONFIG_CONSTRUCTORS
3105 unsigned long i;
3106
3107 for (i = 0; i < mod->num_ctors; i++)
3108 mod->ctors[i]();
3109 #endif
3110 }
3111
3112 /* This is where the real work happens */
3113 SYSCALL_DEFINE3(init_module, void __user *, umod,
3114 unsigned long, len, const char __user *, uargs)
3115 {
3116 struct module *mod;
3117 int ret = 0;
3118
3119 /* Must have permission */
3120 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3121 return -EPERM;
3122
3123 /* Do all the hard work */
3124 mod = load_module(umod, len, uargs);
3125 if (IS_ERR(mod))
3126 return PTR_ERR(mod);
3127
3128 blocking_notifier_call_chain(&module_notify_list,
3129 MODULE_STATE_COMING, mod);
3130
3131 /* Set RO and NX regions for core */
3132 set_section_ro_nx(mod->module_core,
3133 mod->core_text_size,
3134 mod->core_ro_size,
3135 mod->core_size);
3136
3137 /* Set RO and NX regions for init */
3138 set_section_ro_nx(mod->module_init,
3139 mod->init_text_size,
3140 mod->init_ro_size,
3141 mod->init_size);
3142
3143 do_mod_ctors(mod);
3144 /* Start the module */
3145 if (mod->init != NULL)
3146 ret = do_one_initcall(mod->init);
3147 if (ret < 0) {
3148 /* Init routine failed: abort. Try to protect us from
3149 buggy refcounters. */
3150 mod->state = MODULE_STATE_GOING;
3151 synchronize_sched();
3152 module_put(mod);
3153 blocking_notifier_call_chain(&module_notify_list,
3154 MODULE_STATE_GOING, mod);
3155 free_module(mod);
3156 wake_up_all(&module_wq);
3157 return ret;
3158 }
3159 if (ret > 0) {
3160 printk(KERN_WARNING
3161 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3162 "%s: loading module anyway...\n",
3163 __func__, mod->name, ret,
3164 __func__);
3165 dump_stack();
3166 }
3167
3168 /* Now it's a first class citizen! */
3169 mod->state = MODULE_STATE_LIVE;
3170 blocking_notifier_call_chain(&module_notify_list,
3171 MODULE_STATE_LIVE, mod);
3172
3173 /* We need to finish all async code before the module init sequence is done */
3174 async_synchronize_full();
3175
3176 mutex_lock(&module_mutex);
3177 /* Drop initial reference. */
3178 module_put(mod);
3179 trim_init_extable(mod);
3180 #ifdef CONFIG_KALLSYMS
3181 mod->num_symtab = mod->core_num_syms;
3182 mod->symtab = mod->core_symtab;
3183 mod->strtab = mod->core_strtab;
3184 #endif
3185 unset_module_init_ro_nx(mod);
3186 module_free(mod, mod->module_init);
3187 mod->module_init = NULL;
3188 mod->init_size = 0;
3189 mod->init_ro_size = 0;
3190 mod->init_text_size = 0;
3191 mutex_unlock(&module_mutex);
3192 wake_up_all(&module_wq);
3193
3194 return 0;
3195 }
3196
3197 static inline int within(unsigned long addr, void *start, unsigned long size)
3198 {
3199 return ((void *)addr >= start && (void *)addr < start + size);
3200 }
3201
3202 #ifdef CONFIG_KALLSYMS
3203 /*
3204 * This ignores the intensely annoying "mapping symbols" found
3205 * in ARM ELF files: $a, $t and $d.
3206 */
3207 static inline int is_arm_mapping_symbol(const char *str)
3208 {
3209 return str[0] == '$' && strchr("atd", str[1])
3210 && (str[2] == '\0' || str[2] == '.');
3211 }
3212
3213 static const char *get_ksymbol(struct module *mod,
3214 unsigned long addr,
3215 unsigned long *size,
3216 unsigned long *offset)
3217 {
3218 unsigned int i, best = 0;
3219 unsigned long nextval;
3220
3221 /* At worse, next value is at end of module */
3222 if (within_module_init(addr, mod))
3223 nextval = (unsigned long)mod->module_init+mod->init_text_size;
3224 else
3225 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3226
3227 /* Scan for closest preceding symbol, and next symbol. (ELF
3228 starts real symbols at 1). */
3229 for (i = 1; i < mod->num_symtab; i++) {
3230 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3231 continue;
3232
3233 /* We ignore unnamed symbols: they're uninformative
3234 * and inserted at a whim. */
3235 if (mod->symtab[i].st_value <= addr
3236 && mod->symtab[i].st_value > mod->symtab[best].st_value
3237 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3238 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3239 best = i;
3240 if (mod->symtab[i].st_value > addr
3241 && mod->symtab[i].st_value < nextval
3242 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3243 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3244 nextval = mod->symtab[i].st_value;
3245 }
3246
3247 if (!best)
3248 return NULL;
3249
3250 if (size)
3251 *size = nextval - mod->symtab[best].st_value;
3252 if (offset)
3253 *offset = addr - mod->symtab[best].st_value;
3254 return mod->strtab + mod->symtab[best].st_name;
3255 }
3256
3257 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3258 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3259 const char *module_address_lookup(unsigned long addr,
3260 unsigned long *size,
3261 unsigned long *offset,
3262 char **modname,
3263 char *namebuf)
3264 {
3265 struct module *mod;
3266 const char *ret = NULL;
3267
3268 preempt_disable();
3269 list_for_each_entry_rcu(mod, &modules, list) {
3270 if (within_module_init(addr, mod) ||
3271 within_module_core(addr, mod)) {
3272 if (modname)
3273 *modname = mod->name;
3274 ret = get_ksymbol(mod, addr, size, offset);
3275 break;
3276 }
3277 }
3278 /* Make a copy in here where it's safe */
3279 if (ret) {
3280 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3281 ret = namebuf;
3282 }
3283 preempt_enable();
3284 return ret;
3285 }
3286
3287 int lookup_module_symbol_name(unsigned long addr, char *symname)
3288 {
3289 struct module *mod;
3290
3291 preempt_disable();
3292 list_for_each_entry_rcu(mod, &modules, list) {
3293 if (within_module_init(addr, mod) ||
3294 within_module_core(addr, mod)) {
3295 const char *sym;
3296
3297 sym = get_ksymbol(mod, addr, NULL, NULL);
3298 if (!sym)
3299 goto out;
3300 strlcpy(symname, sym, KSYM_NAME_LEN);
3301 preempt_enable();
3302 return 0;
3303 }
3304 }
3305 out:
3306 preempt_enable();
3307 return -ERANGE;
3308 }
3309
3310 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3311 unsigned long *offset, char *modname, char *name)
3312 {
3313 struct module *mod;
3314
3315 preempt_disable();
3316 list_for_each_entry_rcu(mod, &modules, list) {
3317 if (within_module_init(addr, mod) ||
3318 within_module_core(addr, mod)) {
3319 const char *sym;
3320
3321 sym = get_ksymbol(mod, addr, size, offset);
3322 if (!sym)
3323 goto out;
3324 if (modname)
3325 strlcpy(modname, mod->name, MODULE_NAME_LEN);
3326 if (name)
3327 strlcpy(name, sym, KSYM_NAME_LEN);
3328 preempt_enable();
3329 return 0;
3330 }
3331 }
3332 out:
3333 preempt_enable();
3334 return -ERANGE;
3335 }
3336
3337 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3338 char *name, char *module_name, int *exported)
3339 {
3340 struct module *mod;
3341
3342 preempt_disable();
3343 list_for_each_entry_rcu(mod, &modules, list) {
3344 if (symnum < mod->num_symtab) {
3345 *value = mod->symtab[symnum].st_value;
3346 *type = mod->symtab[symnum].st_info;
3347 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
3348 KSYM_NAME_LEN);
3349 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
3350 *exported = is_exported(name, *value, mod);
3351 preempt_enable();
3352 return 0;
3353 }
3354 symnum -= mod->num_symtab;
3355 }
3356 preempt_enable();
3357 return -ERANGE;
3358 }
3359
3360 static unsigned long mod_find_symname(struct module *mod, const char *name)
3361 {
3362 unsigned int i;
3363
3364 for (i = 0; i < mod->num_symtab; i++)
3365 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3366 mod->symtab[i].st_info != 'U')
3367 return mod->symtab[i].st_value;
3368 return 0;
3369 }
3370
3371 /* Look for this name: can be of form module:name. */
3372 unsigned long module_kallsyms_lookup_name(const char *name)
3373 {
3374 struct module *mod;
3375 char *colon;
3376 unsigned long ret = 0;
3377
3378 /* Don't lock: we're in enough trouble already. */
3379 preempt_disable();
3380 if ((colon = strchr(name, ':')) != NULL) {
3381 *colon = '\0';
3382 if ((mod = find_module(name)) != NULL)
3383 ret = mod_find_symname(mod, colon+1);
3384 *colon = ':';
3385 } else {
3386 list_for_each_entry_rcu(mod, &modules, list)
3387 if ((ret = mod_find_symname(mod, name)) != 0)
3388 break;
3389 }
3390 preempt_enable();
3391 return ret;
3392 }
3393
3394 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3395 struct module *, unsigned long),
3396 void *data)
3397 {
3398 struct module *mod;
3399 unsigned int i;
3400 int ret;
3401
3402 list_for_each_entry(mod, &modules, list) {
3403 for (i = 0; i < mod->num_symtab; i++) {
3404 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3405 mod, mod->symtab[i].st_value);
3406 if (ret != 0)
3407 return ret;
3408 }
3409 }
3410 return 0;
3411 }
3412 #endif /* CONFIG_KALLSYMS */
3413
3414 static char *module_flags(struct module *mod, char *buf)
3415 {
3416 int bx = 0;
3417
3418 if (mod->taints ||
3419 mod->state == MODULE_STATE_GOING ||
3420 mod->state == MODULE_STATE_COMING) {
3421 buf[bx++] = '(';
3422 bx += module_flags_taint(mod, buf + bx);
3423 /* Show a - for module-is-being-unloaded */
3424 if (mod->state == MODULE_STATE_GOING)
3425 buf[bx++] = '-';
3426 /* Show a + for module-is-being-loaded */
3427 if (mod->state == MODULE_STATE_COMING)
3428 buf[bx++] = '+';
3429 buf[bx++] = ')';
3430 }
3431 buf[bx] = '\0';
3432
3433 return buf;
3434 }
3435
3436 #ifdef CONFIG_PROC_FS
3437 /* Called by the /proc file system to return a list of modules. */
3438 static void *m_start(struct seq_file *m, loff_t *pos)
3439 {
3440 mutex_lock(&module_mutex);
3441 return seq_list_start(&modules, *pos);
3442 }
3443
3444 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3445 {
3446 return seq_list_next(p, &modules, pos);
3447 }
3448
3449 static void m_stop(struct seq_file *m, void *p)
3450 {
3451 mutex_unlock(&module_mutex);
3452 }
3453
3454 static int m_show(struct seq_file *m, void *p)
3455 {
3456 struct module *mod = list_entry(p, struct module, list);
3457 char buf[8];
3458
3459 seq_printf(m, "%s %u",
3460 mod->name, mod->init_size + mod->core_size);
3461 print_unload_info(m, mod);
3462
3463 /* Informative for users. */
3464 seq_printf(m, " %s",
3465 mod->state == MODULE_STATE_GOING ? "Unloading":
3466 mod->state == MODULE_STATE_COMING ? "Loading":
3467 "Live");
3468 /* Used by oprofile and other similar tools. */
3469 seq_printf(m, " 0x%pK", mod->module_core);
3470
3471 /* Taints info */
3472 if (mod->taints)
3473 seq_printf(m, " %s", module_flags(mod, buf));
3474
3475 seq_printf(m, "\n");
3476 return 0;
3477 }
3478
3479 /* Format: modulename size refcount deps address
3480
3481 Where refcount is a number or -, and deps is a comma-separated list
3482 of depends or -.
3483 */
3484 static const struct seq_operations modules_op = {
3485 .start = m_start,
3486 .next = m_next,
3487 .stop = m_stop,
3488 .show = m_show
3489 };
3490
3491 static int modules_open(struct inode *inode, struct file *file)
3492 {
3493 return seq_open(file, &modules_op);
3494 }
3495
3496 static const struct file_operations proc_modules_operations = {
3497 .open = modules_open,
3498 .read = seq_read,
3499 .llseek = seq_lseek,
3500 .release = seq_release,
3501 };
3502
3503 static int __init proc_modules_init(void)
3504 {
3505 proc_create("modules", 0, NULL, &proc_modules_operations);
3506 return 0;
3507 }
3508 module_init(proc_modules_init);
3509 #endif
3510
3511 /* Given an address, look for it in the module exception tables. */
3512 const struct exception_table_entry *search_module_extables(unsigned long addr)
3513 {
3514 const struct exception_table_entry *e = NULL;
3515 struct module *mod;
3516
3517 preempt_disable();
3518 list_for_each_entry_rcu(mod, &modules, list) {
3519 if (mod->num_exentries == 0)
3520 continue;
3521
3522 e = search_extable(mod->extable,
3523 mod->extable + mod->num_exentries - 1,
3524 addr);
3525 if (e)
3526 break;
3527 }
3528 preempt_enable();
3529
3530 /* Now, if we found one, we are running inside it now, hence
3531 we cannot unload the module, hence no refcnt needed. */
3532 return e;
3533 }
3534
3535 /*
3536 * is_module_address - is this address inside a module?
3537 * @addr: the address to check.
3538 *
3539 * See is_module_text_address() if you simply want to see if the address
3540 * is code (not data).
3541 */
3542 bool is_module_address(unsigned long addr)
3543 {
3544 bool ret;
3545
3546 preempt_disable();
3547 ret = __module_address(addr) != NULL;
3548 preempt_enable();
3549
3550 return ret;
3551 }
3552
3553 /*
3554 * __module_address - get the module which contains an address.
3555 * @addr: the address.
3556 *
3557 * Must be called with preempt disabled or module mutex held so that
3558 * module doesn't get freed during this.
3559 */
3560 struct module *__module_address(unsigned long addr)
3561 {
3562 struct module *mod;
3563
3564 if (addr < module_addr_min || addr > module_addr_max)
3565 return NULL;
3566
3567 list_for_each_entry_rcu(mod, &modules, list)
3568 if (within_module_core(addr, mod)
3569 || within_module_init(addr, mod))
3570 return mod;
3571 return NULL;
3572 }
3573 EXPORT_SYMBOL_GPL(__module_address);
3574
3575 /*
3576 * is_module_text_address - is this address inside module code?
3577 * @addr: the address to check.
3578 *
3579 * See is_module_address() if you simply want to see if the address is
3580 * anywhere in a module. See kernel_text_address() for testing if an
3581 * address corresponds to kernel or module code.
3582 */
3583 bool is_module_text_address(unsigned long addr)
3584 {
3585 bool ret;
3586
3587 preempt_disable();
3588 ret = __module_text_address(addr) != NULL;
3589 preempt_enable();
3590
3591 return ret;
3592 }
3593
3594 /*
3595 * __module_text_address - get the module whose code contains an address.
3596 * @addr: the address.
3597 *
3598 * Must be called with preempt disabled or module mutex held so that
3599 * module doesn't get freed during this.
3600 */
3601 struct module *__module_text_address(unsigned long addr)
3602 {
3603 struct module *mod = __module_address(addr);
3604 if (mod) {
3605 /* Make sure it's within the text section. */
3606 if (!within(addr, mod->module_init, mod->init_text_size)
3607 && !within(addr, mod->module_core, mod->core_text_size))
3608 mod = NULL;
3609 }
3610 return mod;
3611 }
3612 EXPORT_SYMBOL_GPL(__module_text_address);
3613
3614 /* Don't grab lock, we're oopsing. */
3615 void print_modules(void)
3616 {
3617 struct module *mod;
3618 char buf[8];
3619
3620 printk(KERN_DEFAULT "Modules linked in:");
3621 /* Most callers should already have preempt disabled, but make sure */
3622 preempt_disable();
3623 list_for_each_entry_rcu(mod, &modules, list)
3624 printk(" %s%s", mod->name, module_flags(mod, buf));
3625 preempt_enable();
3626 if (last_unloaded_module[0])
3627 printk(" [last unloaded: %s]", last_unloaded_module);
3628 printk("\n");
3629 }
3630
3631 #ifdef CONFIG_MODVERSIONS
3632 /* Generate the signature for all relevant module structures here.
3633 * If these change, we don't want to try to parse the module. */
3634 void module_layout(struct module *mod,
3635 struct modversion_info *ver,
3636 struct kernel_param *kp,
3637 struct kernel_symbol *ks,
3638 struct tracepoint * const *tp)
3639 {
3640 }
3641 EXPORT_SYMBOL(module_layout);
3642 #endif
This page took 0.115099 seconds and 6 git commands to generate.