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