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