1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/moduleloader.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/elf.h>
26#include <linux/seq_file.h>
27#include <linux/syscalls.h>
28#include <linux/fcntl.h>
29#include <linux/rcupdate.h>
30#include <linux/cpu.h>
31#include <linux/moduleparam.h>
32#include <linux/errno.h>
33#include <linux/err.h>
34#include <linux/vermagic.h>
35#include <linux/notifier.h>
36#include <linux/stop_machine.h>
37#include <asm/uaccess.h>
38#include <asm/semaphore.h>
39#include <asm/cacheflush.h>
40#include "module-verify.h"
41
42#if 0
43#define DEBUGP printk
44#else
45#define DEBUGP(fmt , a...)
46#endif
47
48#ifndef ARCH_SHF_SMALL
49#define ARCH_SHF_SMALL 0
50#endif
51
52
53#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
54
55
56static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
57
58
59static DECLARE_MUTEX(module_mutex);
60static LIST_HEAD(modules);
61
62static DECLARE_MUTEX(notify_mutex);
63static struct notifier_block * module_notify_list;
64
65int register_module_notifier(struct notifier_block * nb)
66{
67 int err;
68 down(¬ify_mutex);
69 err = notifier_chain_register(&module_notify_list, nb);
70 up(¬ify_mutex);
71 return err;
72}
73EXPORT_SYMBOL(register_module_notifier);
74
75int unregister_module_notifier(struct notifier_block * nb)
76{
77 int err;
78 down(¬ify_mutex);
79 err = notifier_chain_unregister(&module_notify_list, nb);
80 up(¬ify_mutex);
81 return err;
82}
83EXPORT_SYMBOL(unregister_module_notifier);
84
85
86static inline int strong_try_module_get(struct module *mod)
87{
88 if (mod && mod->state == MODULE_STATE_COMING)
89 return 0;
90 return try_module_get(mod);
91}
92
93
94
95
96
97void __module_put_and_exit(struct module *mod, long code)
98{
99 module_put(mod);
100 do_exit(code);
101}
102EXPORT_SYMBOL(__module_put_and_exit);
103
104
105static unsigned int find_sec(Elf_Ehdr *hdr,
106 Elf_Shdr *sechdrs,
107 const char *secstrings,
108 const char *name)
109{
110 unsigned int i;
111
112 for (i = 1; i < hdr->e_shnum; i++)
113
114 if ((sechdrs[i].sh_flags & SHF_ALLOC)
115 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
116 return i;
117 return 0;
118}
119
120
121extern const struct kernel_symbol __start___ksymtab[];
122extern const struct kernel_symbol __stop___ksymtab[];
123extern const struct kernel_symbol __start___ksymtab_gpl[];
124extern const struct kernel_symbol __stop___ksymtab_gpl[];
125extern const unsigned long __start___kcrctab[];
126extern const unsigned long __start___kcrctab_gpl[];
127
128#ifndef CONFIG_MODVERSIONS
129#define symversion(base, idx) NULL
130#else
131#define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
132#endif
133
134
135static unsigned long __find_symbol(const char *name,
136 struct module **owner,
137 const unsigned long **crc,
138 int gplok)
139{
140 struct module *mod;
141 unsigned int i;
142
143
144 *owner = NULL;
145 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
146 if (strcmp(__start___ksymtab[i].name, name) == 0) {
147 *crc = symversion(__start___kcrctab, i);
148 return __start___ksymtab[i].value;
149 }
150 }
151 if (gplok) {
152 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
153 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
154 *crc = symversion(__start___kcrctab_gpl, i);
155 return __start___ksymtab_gpl[i].value;
156 }
157 }
158
159
160 list_for_each_entry(mod, &modules, list) {
161 *owner = mod;
162 for (i = 0; i < mod->num_syms; i++)
163 if (strcmp(mod->syms[i].name, name) == 0) {
164 *crc = symversion(mod->crcs, i);
165 return mod->syms[i].value;
166 }
167
168 if (gplok) {
169 for (i = 0; i < mod->num_gpl_syms; i++) {
170 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
171 *crc = symversion(mod->gpl_crcs, i);
172 return mod->gpl_syms[i].value;
173 }
174 }
175 }
176 }
177 DEBUGP("Failed to find symbol %s\n", name);
178 return 0;
179}
180
181
182static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
183 unsigned int symindex,
184 const char *strtab,
185 const char *name)
186{
187 unsigned int i;
188 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
189
190
191 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
192 if (sym[i].st_shndx != SHN_UNDEF
193 && strcmp(name, strtab + sym[i].st_name) == 0)
194 return sym[i].st_value;
195 }
196 return 0;
197}
198
199
200static struct module *find_module(const char *name)
201{
202 struct module *mod;
203
204 list_for_each_entry(mod, &modules, list) {
205 if (strcmp(mod->name, name) == 0)
206 return mod;
207 }
208 return NULL;
209}
210
211#ifdef CONFIG_SMP
212
213static unsigned int pcpu_num_used, pcpu_num_allocated;
214
215static int *pcpu_size;
216
217static int split_block(unsigned int i, unsigned short size)
218{
219
220 if (pcpu_num_used + 1 > pcpu_num_allocated) {
221 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
222 GFP_KERNEL);
223 if (!new)
224 return 0;
225
226 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
227 pcpu_num_allocated *= 2;
228 kfree(pcpu_size);
229 pcpu_size = new;
230 }
231
232
233 memmove(&pcpu_size[i+1], &pcpu_size[i],
234 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
235 pcpu_num_used++;
236
237 pcpu_size[i+1] -= size;
238 pcpu_size[i] = size;
239 return 1;
240}
241
242static inline unsigned int block_size(int val)
243{
244 if (val < 0)
245 return -val;
246 return val;
247}
248
249
250extern char __per_cpu_start[], __per_cpu_end[];
251
252static void *percpu_modalloc(unsigned long size, unsigned long align)
253{
254 unsigned long extra;
255 unsigned int i;
256 void *ptr;
257
258 BUG_ON(align > SMP_CACHE_BYTES);
259
260 ptr = __per_cpu_start;
261 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
262
263 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
264 BUG_ON(i == 0 && extra != 0);
265
266 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
267 continue;
268
269
270 if (pcpu_size[i-1] < 0)
271 pcpu_size[i-1] -= extra;
272 else
273 pcpu_size[i-1] += extra;
274 pcpu_size[i] -= extra;
275 ptr += extra;
276
277
278 if (pcpu_size[i] - size > sizeof(unsigned long))
279 if (!split_block(i, size))
280 return NULL;
281
282
283 pcpu_size[i] = -pcpu_size[i];
284 return ptr;
285 }
286
287 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
288 size);
289 return NULL;
290}
291
292static void percpu_modfree(void *freeme)
293{
294 unsigned int i;
295 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
296
297
298 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
299 if (ptr == freeme) {
300 pcpu_size[i] = -pcpu_size[i];
301 goto free;
302 }
303 }
304 BUG();
305
306 free:
307
308 if (pcpu_size[i-1] >= 0) {
309 pcpu_size[i-1] += pcpu_size[i];
310 pcpu_num_used--;
311 memmove(&pcpu_size[i], &pcpu_size[i+1],
312 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
313 i--;
314 }
315
316 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
317 pcpu_size[i] += pcpu_size[i+1];
318 pcpu_num_used--;
319 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
320 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
321 }
322}
323
324static unsigned int find_pcpusec(Elf_Ehdr *hdr,
325 Elf_Shdr *sechdrs,
326 const char *secstrings)
327{
328 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
329}
330
331static int percpu_modinit(void)
332{
333 pcpu_num_used = 2;
334 pcpu_num_allocated = 2;
335 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
336 GFP_KERNEL);
337
338 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
339
340 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
341 if (pcpu_size[1] < 0) {
342 printk(KERN_ERR "No per-cpu room for modules.\n");
343 pcpu_num_used = 1;
344 }
345
346 return 0;
347}
348__initcall(percpu_modinit);
349#else
350static inline void *percpu_modalloc(unsigned long size, unsigned long align)
351{
352 return NULL;
353}
354static inline void percpu_modfree(void *pcpuptr)
355{
356 BUG();
357}
358static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
359 Elf_Shdr *sechdrs,
360 const char *secstrings)
361{
362 return 0;
363}
364static inline void percpu_modcopy(void *pcpudst, const void *src,
365 unsigned long size)
366{
367
368 BUG_ON(size != 0);
369}
370#endif
371
372static int add_attribute(struct module *mod, struct kernel_param *kp)
373{
374 struct module_attribute *a;
375 int retval;
376
377 a = &mod->mkobj->attr[mod->mkobj->num_attributes];
378 a->attr.name = (char *)kp->name;
379 a->attr.owner = mod;
380 a->attr.mode = kp->perm;
381 a->param = kp;
382 retval = sysfs_create_file(&mod->mkobj->kobj, &a->attr);
383 if (!retval)
384 mod->mkobj->num_attributes++;
385 return retval;
386}
387
388#ifdef CONFIG_MODULE_UNLOAD
389
390static void module_unload_init(struct module *mod)
391{
392 unsigned int i;
393
394 INIT_LIST_HEAD(&mod->modules_which_use_me);
395 for (i = 0; i < NR_CPUS; i++)
396 local_set(&mod->ref[i].count, 0);
397
398 local_set(&mod->ref[smp_processor_id()].count, 1);
399
400 mod->waiter = current;
401}
402
403
404struct module_use
405{
406 struct list_head list;
407 struct module *module_which_uses;
408};
409
410
411static int already_uses(struct module *a, struct module *b)
412{
413 struct module_use *use;
414
415 list_for_each_entry(use, &b->modules_which_use_me, list) {
416 if (use->module_which_uses == a) {
417 DEBUGP("%s uses %s!\n", a->name, b->name);
418 return 1;
419 }
420 }
421 DEBUGP("%s does not use %s!\n", a->name, b->name);
422 return 0;
423}
424
425
426static int use_module(struct module *a, struct module *b)
427{
428 struct module_use *use;
429 if (b == NULL || already_uses(a, b)) return 1;
430
431 if (!strong_try_module_get(b))
432 return 0;
433
434 DEBUGP("Allocating new usage for %s.\n", a->name);
435 use = kmalloc(sizeof(*use), GFP_ATOMIC);
436 if (!use) {
437 printk("%s: out of memory loading\n", a->name);
438 module_put(b);
439 return 0;
440 }
441
442 use->module_which_uses = a;
443 list_add(&use->list, &b->modules_which_use_me);
444 return 1;
445}
446
447
448static void module_unload_free(struct module *mod)
449{
450 struct module *i;
451
452 list_for_each_entry(i, &modules, list) {
453 struct module_use *use;
454
455 list_for_each_entry(use, &i->modules_which_use_me, list) {
456 if (use->module_which_uses == mod) {
457 DEBUGP("%s unusing %s\n", mod->name, i->name);
458 module_put(i);
459 list_del(&use->list);
460 kfree(use);
461
462 break;
463 }
464 }
465 }
466}
467
468#ifdef CONFIG_MODULE_FORCE_UNLOAD
469static inline int try_force(unsigned int flags)
470{
471 int ret = (flags & O_TRUNC);
472 if (ret)
473 tainted |= TAINT_FORCED_MODULE;
474 return ret;
475}
476#else
477static inline int try_force(unsigned int flags)
478{
479 return 0;
480}
481#endif
482
483struct stopref
484{
485 struct module *mod;
486 int flags;
487 int *forced;
488};
489
490
491static inline int __try_stop_module(void *_sref)
492{
493 struct stopref *sref = _sref;
494
495
496 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
497 if (!(*sref->forced = try_force(sref->flags)))
498 return -EWOULDBLOCK;
499 }
500
501
502 sref->mod->state = MODULE_STATE_GOING;
503 return 0;
504}
505
506static int try_stop_module(struct module *mod, int flags, int *forced)
507{
508 struct stopref sref = { mod, flags, forced };
509
510 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
511}
512
513unsigned int module_refcount(struct module *mod)
514{
515 unsigned int i, total = 0;
516
517 for (i = 0; i < NR_CPUS; i++)
518 total += local_read(&mod->ref[i].count);
519 return total;
520}
521EXPORT_SYMBOL(module_refcount);
522
523
524static void free_module(struct module *mod);
525
526static void wait_for_zero_refcount(struct module *mod)
527{
528
529 up(&module_mutex);
530 for (;;) {
531 DEBUGP("Looking at refcount...\n");
532 set_current_state(TASK_UNINTERRUPTIBLE);
533 if (module_refcount(mod) == 0)
534 break;
535 schedule();
536 }
537 current->state = TASK_RUNNING;
538 down(&module_mutex);
539}
540
541asmlinkage long
542sys_delete_module(const char __user *name_user, unsigned int flags)
543{
544 struct module *mod;
545 char name[MODULE_NAME_LEN];
546 int ret, forced = 0;
547
548 if (!capable(CAP_SYS_MODULE))
549 return -EPERM;
550
551 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
552 return -EFAULT;
553 name[MODULE_NAME_LEN-1] = '\0';
554
555 if (down_interruptible(&module_mutex) != 0)
556 return -EINTR;
557
558 mod = find_module(name);
559 if (!mod) {
560 ret = -ENOENT;
561 goto out;
562 }
563
564 if (!list_empty(&mod->modules_which_use_me)) {
565
566 ret = -EWOULDBLOCK;
567 goto out;
568 }
569
570
571 if (mod->state != MODULE_STATE_LIVE) {
572
573
574 DEBUGP("%s already dying\n", mod->name);
575 ret = -EBUSY;
576 goto out;
577 }
578
579
580 if ((mod->init != NULL && mod->exit == NULL)
581 || mod->unsafe) {
582 forced = try_force(flags);
583 if (!forced) {
584
585 ret = -EBUSY;
586 goto out;
587 }
588 }
589
590
591 mod->waiter = current;
592
593
594 ret = try_stop_module(mod, flags, &forced);
595 if (ret)
596 goto out;
597
598
599 if (!forced && module_refcount(mod) != 0)
600 wait_for_zero_refcount(mod);
601
602
603 if (mod->exit != NULL) {
604 up(&module_mutex);
605 mod->exit();
606 down(&module_mutex);
607 }
608 free_module(mod);
609
610 out:
611 up(&module_mutex);
612 return ret;
613}
614
615static void print_unload_info(struct seq_file *m, struct module *mod)
616{
617 struct module_use *use;
618 int printed_something = 0;
619
620 seq_printf(m, " %u ", module_refcount(mod));
621
622
623
624 list_for_each_entry(use, &mod->modules_which_use_me, list) {
625 printed_something = 1;
626 seq_printf(m, "%s,", use->module_which_uses->name);
627 }
628
629 if (mod->unsafe) {
630 printed_something = 1;
631 seq_printf(m, "[unsafe],");
632 }
633
634 if (mod->init != NULL && mod->exit == NULL) {
635 printed_something = 1;
636 seq_printf(m, "[permanent],");
637 }
638
639 if (!printed_something)
640 seq_printf(m, "-");
641}
642
643void __symbol_put(const char *symbol)
644{
645 struct module *owner;
646 unsigned long flags;
647 const unsigned long *crc;
648
649 spin_lock_irqsave(&modlist_lock, flags);
650 if (!__find_symbol(symbol, &owner, &crc, 1))
651 BUG();
652 module_put(owner);
653 spin_unlock_irqrestore(&modlist_lock, flags);
654}
655EXPORT_SYMBOL(__symbol_put);
656
657void symbol_put_addr(void *addr)
658{
659 unsigned long flags;
660
661 spin_lock_irqsave(&modlist_lock, flags);
662 if (!kernel_text_address((unsigned long)addr))
663 BUG();
664
665 module_put(module_text_address((unsigned long)addr));
666 spin_unlock_irqrestore(&modlist_lock, flags);
667}
668EXPORT_SYMBOL_GPL(symbol_put_addr);
669
670static int refcnt_get_fn(char *buffer, struct kernel_param *kp)
671{
672 struct module *mod = container_of(kp, struct module, refcnt_param);
673
674
675 return sprintf(buffer, "%u", module_refcount(mod)-1);
676}
677
678static inline int sysfs_unload_setup(struct module *mod)
679{
680 mod->refcnt_param.name = "refcnt";
681 mod->refcnt_param.perm = 0444;
682 mod->refcnt_param.get = refcnt_get_fn;
683
684 return add_attribute(mod, &mod->refcnt_param);
685}
686
687#else
688static void print_unload_info(struct seq_file *m, struct module *mod)
689{
690
691 seq_printf(m, " - -");
692}
693
694static inline void module_unload_free(struct module *mod)
695{
696}
697
698static inline int use_module(struct module *a, struct module *b)
699{
700 return strong_try_module_get(b);
701}
702
703static inline void module_unload_init(struct module *mod)
704{
705}
706
707asmlinkage long
708sys_delete_module(const char __user *name_user, unsigned int flags)
709{
710 return -ENOSYS;
711}
712
713static inline int sysfs_unload_setup(struct module *mod)
714{
715 return 0;
716}
717#endif
718
719#ifdef CONFIG_OBSOLETE_MODPARM
720
721static int obsparm_copy_string(const char *val, struct kernel_param *kp)
722{
723 strcpy(kp->arg, val);
724 return 0;
725}
726
727int set_obsolete(const char *val, struct kernel_param *kp)
728{
729 unsigned int min, max;
730 unsigned int size, maxsize;
731 int dummy;
732 char *endp;
733 const char *p;
734 struct obsolete_modparm *obsparm = kp->arg;
735
736 if (!val) {
737 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
738 return -EINVAL;
739 }
740
741
742 p = obsparm->type;
743 min = simple_strtol(p, &endp, 10);
744 if (endp == obsparm->type)
745 min = max = 1;
746 else if (*endp == '-') {
747 p = endp+1;
748 max = simple_strtol(p, &endp, 10);
749 } else
750 max = min;
751 switch (*endp) {
752 case 'b':
753 return param_array(kp->name, val, min, max, obsparm->addr,
754 1, param_set_byte, &dummy);
755 case 'h':
756 return param_array(kp->name, val, min, max, obsparm->addr,
757 sizeof(short), param_set_short, &dummy);
758 case 'i':
759 return param_array(kp->name, val, min, max, obsparm->addr,
760 sizeof(int), param_set_int, &dummy);
761 case 'l':
762 return param_array(kp->name, val, min, max, obsparm->addr,
763 sizeof(long), param_set_long, &dummy);
764 case 's':
765 return param_array(kp->name, val, min, max, obsparm->addr,
766 sizeof(char *), param_set_charp, &dummy);
767
768 case 'c':
769
770
771 p = endp+1;
772 maxsize = simple_strtol(p, &endp, 10);
773
774 p = val;
775 while (p[size = strcspn(p, ",")]) {
776 if (size >= maxsize)
777 goto oversize;
778 p += size+1;
779 }
780 if (size >= maxsize)
781 goto oversize;
782 return param_array(kp->name, val, min, max, obsparm->addr,
783 maxsize, obsparm_copy_string, &dummy);
784 }
785 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
786 return -EINVAL;
787 oversize:
788 printk(KERN_ERR
789 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
790 return -EINVAL;
791}
792
793static int obsolete_params(const char *name,
794 char *args,
795 struct obsolete_modparm obsparm[],
796 unsigned int num,
797 Elf_Shdr *sechdrs,
798 unsigned int symindex,
799 const char *strtab)
800{
801 struct kernel_param *kp;
802 unsigned int i;
803 int ret;
804
805 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
806 if (!kp)
807 return -ENOMEM;
808
809 for (i = 0; i < num; i++) {
810 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
811
812 snprintf(sym_name, sizeof(sym_name), "%s%s",
813 MODULE_SYMBOL_PREFIX, obsparm[i].name);
814
815 kp[i].name = obsparm[i].name;
816 kp[i].perm = 000;
817 kp[i].set = set_obsolete;
818 kp[i].get = NULL;
819 obsparm[i].addr
820 = (void *)find_local_symbol(sechdrs, symindex, strtab,
821 sym_name);
822 if (!obsparm[i].addr) {
823 printk("%s: falsely claims to have parameter %s\n",
824 name, obsparm[i].name);
825 ret = -EINVAL;
826 goto out;
827 }
828 kp[i].arg = &obsparm[i];
829 }
830
831 ret = parse_args(name, args, kp, num, NULL);
832 out:
833 kfree(kp);
834 return ret;
835}
836#else
837static int obsolete_params(const char *name,
838 char *args,
839 struct obsolete_modparm obsparm[],
840 unsigned int num,
841 Elf_Shdr *sechdrs,
842 unsigned int symindex,
843 const char *strtab)
844{
845 if (num != 0)
846 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
847 name);
848 return 0;
849}
850#endif
851
852static const char vermagic[] = VERMAGIC_STRING;
853
854#ifdef CONFIG_MODVERSIONS
855static int check_version(Elf_Shdr *sechdrs,
856 unsigned int versindex,
857 const char *symname,
858 struct module *mod,
859 const unsigned long *crc)
860{
861 unsigned int i, num_versions;
862 struct modversion_info *versions;
863
864
865 if (!crc)
866 return 1;
867
868 versions = (void *) sechdrs[versindex].sh_addr;
869 num_versions = sechdrs[versindex].sh_size
870 / sizeof(struct modversion_info);
871
872 for (i = 0; i < num_versions; i++) {
873 if (strcmp(versions[i].name, symname) != 0)
874 continue;
875
876 if (versions[i].crc == *crc)
877 return 1;
878 printk("%s: disagrees about version of symbol %s\n",
879 mod->name, symname);
880 DEBUGP("Found checksum %lX vs module %lX\n",
881 *crc, versions[i].crc);
882 return 0;
883 }
884
885 if (!(tainted & TAINT_FORCED_MODULE)) {
886 printk("%s: no version for \"%s\" found: kernel tainted.\n",
887 mod->name, symname);
888 tainted |= TAINT_FORCED_MODULE;
889 }
890 return 1;
891}
892
893static inline int check_modstruct_version(Elf_Shdr *sechdrs,
894 unsigned int versindex,
895 struct module *mod)
896{
897 const unsigned long *crc;
898 struct module *owner;
899
900 if (!__find_symbol("struct_module", &owner, &crc, 1))
901 BUG();
902 return check_version(sechdrs, versindex, "struct_module", mod,
903 crc);
904}
905
906
907static inline int same_magic(const char *amagic, const char *bmagic)
908{
909 amagic += strcspn(amagic, " ");
910 bmagic += strcspn(bmagic, " ");
911 return strcmp(amagic, bmagic) == 0;
912}
913#else
914static inline int check_version(Elf_Shdr *sechdrs,
915 unsigned int versindex,
916 const char *symname,
917 struct module *mod,
918 const unsigned long *crc)
919{
920 return 1;
921}
922
923static inline int check_modstruct_version(Elf_Shdr *sechdrs,
924 unsigned int versindex,
925 struct module *mod)
926{
927 return 1;
928}
929
930static inline int same_magic(const char *amagic, const char *bmagic)
931{
932 return strcmp(amagic, bmagic) == 0;
933}
934#endif
935
936
937
938static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
939 unsigned int versindex,
940 const char *name,
941 struct module *mod)
942{
943 struct module *owner;
944 unsigned long ret;
945 const unsigned long *crc;
946
947 spin_lock_irq(&modlist_lock);
948 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
949 if (ret) {
950
951 if (!check_version(sechdrs, versindex, name, mod, crc) ||
952 !use_module(mod, owner))
953 ret = 0;
954 }
955 spin_unlock_irq(&modlist_lock);
956 return ret;
957}
958
959
960
961
962
963
964#ifdef CONFIG_KALLSYMS
965static void module_sect_attrs_release(struct kobject *kobj)
966{
967 kfree(container_of(kobj, struct module_sections, kobj));
968}
969
970static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
971 char *buf)
972{
973 struct module_sect_attr *sattr =
974 container_of(attr, struct module_sect_attr, attr);
975 return sprintf(buf, "0x%lx\n", sattr->address);
976}
977
978static struct sysfs_ops module_sect_ops = {
979 .show = module_sect_show,
980};
981
982static struct kobj_type module_sect_ktype = {
983 .sysfs_ops = &module_sect_ops,
984 .release = module_sect_attrs_release,
985};
986
987static void add_sect_attrs(struct module *mod, unsigned int nsect,
988 char *secstrings, Elf_Shdr *sechdrs)
989{
990 unsigned int nloaded = 0, i;
991 struct module_sect_attr *sattr;
992
993 if (!mod->mkobj)
994 return;
995
996
997 for (i = 0; i < nsect; i++)
998 if (sechdrs[i].sh_flags & SHF_ALLOC)
999 nloaded++;
1000 mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
1001 nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
1002 if (! mod->sect_attrs)
1003 return;
1004
1005
1006 memset(mod->sect_attrs, 0, sizeof(struct module_sections));
1007 if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
1008 goto out;
1009 mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
1010 mod->sect_attrs->kobj.ktype = &module_sect_ktype;
1011 if (kobject_register(&mod->sect_attrs->kobj))
1012 goto out;
1013
1014
1015 sattr = &mod->sect_attrs->attrs[0];
1016 for (i = 0; i < nsect; i++) {
1017 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1018 continue;
1019 sattr->address = sechdrs[i].sh_addr;
1020 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1021 MODULE_SECT_NAME_LEN);
1022 sattr->attr.name = sattr->name;
1023 sattr->attr.owner = mod;
1024 sattr->attr.mode = S_IRUGO;
1025 (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
1026 sattr++;
1027 }
1028 return;
1029 out:
1030 kfree(mod->sect_attrs);
1031 mod->sect_attrs = NULL;
1032}
1033
1034static void remove_sect_attrs(struct module *mod)
1035{
1036 if (mod->sect_attrs) {
1037 kobject_unregister(&mod->sect_attrs->kobj);
1038 mod->sect_attrs = NULL;
1039 }
1040}
1041
1042
1043#else
1044static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1045 char *sectstrings, Elf_Shdr *sechdrs)
1046{
1047}
1048
1049static inline void remove_sect_attrs(struct module *mod)
1050{
1051}
1052#endif
1053
1054
1055
1056
1057#define to_module_attr(n) container_of(n, struct module_attribute, attr);
1058
1059static ssize_t module_attr_show(struct kobject *kobj,
1060 struct attribute *attr,
1061 char *buf)
1062{
1063 int count;
1064 struct module_attribute *attribute = to_module_attr(attr);
1065
1066 if (!attribute->param->get)
1067 return -EPERM;
1068
1069 count = attribute->param->get(buf, attribute->param);
1070 if (count > 0) {
1071 strcat(buf, "\n");
1072 ++count;
1073 }
1074 return count;
1075}
1076
1077
1078static ssize_t module_attr_store(struct kobject *kobj,
1079 struct attribute *attr,
1080 const char *buf, size_t len)
1081{
1082 int err;
1083 struct module_attribute *attribute = to_module_attr(attr);
1084
1085 if (!attribute->param->set)
1086 return -EPERM;
1087
1088 err = attribute->param->set(buf, attribute->param);
1089 if (!err)
1090 return len;
1091 return err;
1092}
1093
1094static struct sysfs_ops module_sysfs_ops = {
1095 .show = module_attr_show,
1096 .store = module_attr_store,
1097};
1098
1099static void module_kobj_release(struct kobject *kobj)
1100{
1101 kfree(container_of(kobj, struct module_kobject, kobj));
1102}
1103
1104static struct kobj_type module_ktype = {
1105 .sysfs_ops = &module_sysfs_ops,
1106 .release = &module_kobj_release,
1107};
1108static decl_subsys(module, &module_ktype, NULL);
1109
1110static int mod_sysfs_setup(struct module *mod,
1111 struct kernel_param *kparam,
1112 unsigned int num_params)
1113{
1114 unsigned int i;
1115 int err;
1116
1117
1118 mod->mkobj = kmalloc(sizeof(*mod->mkobj)
1119 + sizeof(mod->mkobj->attr[0]) * (num_params+1),
1120 GFP_KERNEL);
1121 if (!mod->mkobj)
1122 return -ENOMEM;
1123
1124 memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1125 err = kobject_set_name(&mod->mkobj->kobj, mod->name);
1126 if (err)
1127 goto out;
1128 kobj_set_kset_s(mod->mkobj, module_subsys);
1129 err = kobject_register(&mod->mkobj->kobj);
1130 if (err)
1131 goto out;
1132
1133 mod->mkobj->num_attributes = 0;
1134
1135 for (i = 0; i < num_params; i++) {
1136 if (kparam[i].perm) {
1137 err = add_attribute(mod, &kparam[i]);
1138 if (err)
1139 goto out_unreg;
1140 }
1141 }
1142 err = sysfs_unload_setup(mod);
1143 if (err)
1144 goto out_unreg;
1145 return 0;
1146
1147out_unreg:
1148 for (i = 0; i < mod->mkobj->num_attributes; i++)
1149 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1150
1151 kobject_unregister(&mod->mkobj->kobj);
1152 return err;
1153out:
1154 kfree(mod->mkobj);
1155 return err;
1156}
1157
1158static void mod_kobject_remove(struct module *mod)
1159{
1160 unsigned int i;
1161 for (i = 0; i < mod->mkobj->num_attributes; i++)
1162 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1163
1164 kobject_unregister(&mod->mkobj->kobj);
1165}
1166
1167
1168
1169
1170
1171static inline int __unlink_module(void *_mod)
1172{
1173 struct module *mod = _mod;
1174 list_del(&mod->list);
1175 return 0;
1176}
1177
1178
1179static void free_module(struct module *mod)
1180{
1181
1182 stop_machine_run(__unlink_module, mod, NR_CPUS);
1183 remove_sect_attrs(mod);
1184 mod_kobject_remove(mod);
1185
1186
1187 module_arch_cleanup(mod);
1188
1189
1190 module_unload_free(mod);
1191
1192
1193 module_free(mod, mod->module_init);
1194 kfree(mod->args);
1195 if (mod->percpu)
1196 percpu_modfree(mod->percpu);
1197
1198
1199 module_free(mod, mod->module_core);
1200}
1201
1202void *__symbol_get(const char *symbol)
1203{
1204 struct module *owner;
1205 unsigned long value, flags;
1206 const unsigned long *crc;
1207
1208 spin_lock_irqsave(&modlist_lock, flags);
1209 value = __find_symbol(symbol, &owner, &crc, 1);
1210 if (value && !strong_try_module_get(owner))
1211 value = 0;
1212 spin_unlock_irqrestore(&modlist_lock, flags);
1213
1214 return (void *)value;
1215}
1216EXPORT_SYMBOL_GPL(__symbol_get);
1217
1218
1219static int simplify_symbols(Elf_Shdr *sechdrs,
1220 unsigned int symindex,
1221 const char *strtab,
1222 unsigned int versindex,
1223 unsigned int pcpuindex,
1224 struct module *mod)
1225{
1226 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1227 unsigned long secbase;
1228 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1229 int ret = 0;
1230
1231 for (i = 1; i < n; i++) {
1232 switch (sym[i].st_shndx) {
1233 case SHN_COMMON:
1234
1235
1236 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1237 printk("%s: please compile with -fno-common\n",
1238 mod->name);
1239 ret = -ENOEXEC;
1240 break;
1241
1242 case SHN_ABS:
1243
1244 DEBUGP("Absolute symbol: 0x%08lx\n",
1245 (long)sym[i].st_value);
1246 break;
1247
1248 case SHN_UNDEF:
1249 sym[i].st_value
1250 = resolve_symbol(sechdrs, versindex,
1251 strtab + sym[i].st_name, mod);
1252
1253
1254 if (sym[i].st_value != 0)
1255 break;
1256
1257 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1258 break;
1259
1260 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1261 mod->name, strtab + sym[i].st_name);
1262 ret = -ENOENT;
1263 break;
1264
1265 default:
1266
1267 if (sym[i].st_shndx == pcpuindex)
1268 secbase = (unsigned long)mod->percpu;
1269 else
1270 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1271 sym[i].st_value += secbase;
1272 break;
1273 }
1274 }
1275
1276 return ret;
1277}
1278
1279
1280static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1281{
1282 long ret;
1283
1284 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1285 *size = ret + sechdr->sh_size;
1286 return ret;
1287}
1288
1289
1290
1291
1292
1293static void layout_sections(struct module *mod,
1294 const Elf_Ehdr *hdr,
1295 Elf_Shdr *sechdrs,
1296 const char *secstrings)
1297{
1298 static unsigned long const masks[][2] = {
1299
1300
1301
1302 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1303 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1304 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1305 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1306 };
1307 unsigned int m, i;
1308
1309 for (i = 0; i < hdr->e_shnum; i++)
1310 sechdrs[i].sh_entsize = ~0UL;
1311
1312 DEBUGP("Core section allocation order:\n");
1313 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1314 for (i = 0; i < hdr->e_shnum; ++i) {
1315 Elf_Shdr *s = &sechdrs[i];
1316
1317 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1318 || (s->sh_flags & masks[m][1])
1319 || s->sh_entsize != ~0UL
1320 || strncmp(secstrings + s->sh_name,
1321 ".init", 5) == 0)
1322 continue;
1323 s->sh_entsize = get_offset(&mod->core_size, s);
1324 DEBUGP("\t%s\n", secstrings + s->sh_name);
1325 }
1326 if (m == 0)
1327 mod->core_text_size = mod->core_size;
1328 }
1329
1330 DEBUGP("Init section allocation order:\n");
1331 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1332 for (i = 0; i < hdr->e_shnum; ++i) {
1333 Elf_Shdr *s = &sechdrs[i];
1334
1335 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1336 || (s->sh_flags & masks[m][1])
1337 || s->sh_entsize != ~0UL
1338 || strncmp(secstrings + s->sh_name,
1339 ".init", 5) != 0)
1340 continue;
1341 s->sh_entsize = (get_offset(&mod->init_size, s)
1342 | INIT_OFFSET_MASK);
1343 DEBUGP("\t%s\n", secstrings + s->sh_name);
1344 }
1345 if (m == 0)
1346 mod->init_text_size = mod->init_size;
1347 }
1348}
1349
1350static inline int license_is_gpl_compatible(const char *license)
1351{
1352 return (strcmp(license, "GPL") == 0
1353 || strcmp(license, "GPL v2") == 0
1354 || strcmp(license, "GPL and additional rights") == 0
1355 || strcmp(license, "Dual BSD/GPL") == 0
1356 || strcmp(license, "Dual MPL/GPL") == 0);
1357}
1358
1359static void set_license(struct module *mod, const char *license)
1360{
1361 if (!license)
1362 license = "unspecified";
1363
1364 mod->license_gplok = license_is_gpl_compatible(license);
1365 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1366 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1367 mod->name, license);
1368 tainted |= TAINT_PROPRIETARY_MODULE;
1369 }
1370}
1371
1372
1373static char *next_string(char *string, unsigned long *secsize)
1374{
1375
1376 while (string[0]) {
1377 string++;
1378 if ((*secsize)-- <= 1)
1379 return NULL;
1380 }
1381
1382
1383 while (!string[0]) {
1384 string++;
1385 if ((*secsize)-- <= 1)
1386 return NULL;
1387 }
1388 return string;
1389}
1390
1391static char *get_modinfo(Elf_Shdr *sechdrs,
1392 unsigned int info,
1393 const char *tag)
1394{
1395 char *p;
1396 unsigned int taglen = strlen(tag);
1397 unsigned long size = sechdrs[info].sh_size;
1398
1399 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1400 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1401 return p + taglen + 1;
1402 }
1403 return NULL;
1404}
1405
1406#ifdef CONFIG_KALLSYMS
1407int is_exported(const char *name, const struct module *mod)
1408{
1409 unsigned int i;
1410
1411 if (!mod) {
1412 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1413 if (strcmp(__start___ksymtab[i].name, name) == 0)
1414 return 1;
1415 return 0;
1416 }
1417 for (i = 0; i < mod->num_syms; i++)
1418 if (strcmp(mod->syms[i].name, name) == 0)
1419 return 1;
1420 return 0;
1421}
1422
1423
1424static char elf_type(const Elf_Sym *sym,
1425 Elf_Shdr *sechdrs,
1426 const char *secstrings,
1427 struct module *mod)
1428{
1429 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1430 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1431 return 'v';
1432 else
1433 return 'w';
1434 }
1435 if (sym->st_shndx == SHN_UNDEF)
1436 return 'U';
1437 if (sym->st_shndx == SHN_ABS)
1438 return 'a';
1439 if (sym->st_shndx >= SHN_LORESERVE)
1440 return '?';
1441 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1442 return 't';
1443 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1444 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1445 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1446 return 'r';
1447 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1448 return 'g';
1449 else
1450 return 'd';
1451 }
1452 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1453 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1454 return 's';
1455 else
1456 return 'b';
1457 }
1458 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1459 ".debug", strlen(".debug")) == 0)
1460 return 'n';
1461 return '?';
1462}
1463
1464static void add_kallsyms(struct module *mod,
1465 Elf_Shdr *sechdrs,
1466 unsigned int symindex,
1467 unsigned int strindex,
1468 const char *secstrings)
1469{
1470 unsigned int i;
1471
1472 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1473 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1474 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1475
1476
1477 for (i = 0; i < mod->num_symtab; i++)
1478 mod->symtab[i].st_info
1479 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1480}
1481#else
1482static inline void add_kallsyms(struct module *mod,
1483 Elf_Shdr *sechdrs,
1484 unsigned int symindex,
1485 unsigned int strindex,
1486 const char *secstrings)
1487{
1488}
1489#endif
1490
1491
1492
1493static struct module *load_module(void __user *umod,
1494 unsigned long len,
1495 const char __user *uargs)
1496{
1497 Elf_Ehdr *hdr;
1498 Elf_Shdr *sechdrs;
1499 char *secstrings, *args, *modmagic, *strtab = NULL;
1500 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1501 exportindex, modindex, obsparmindex, infoindex, gplindex,
1502 crcindex, gplcrcindex, versindex, pcpuindex;
1503 long arglen;
1504 struct module *mod;
1505 long err = 0;
1506 void *percpu = NULL, *ptr = NULL;
1507 struct exception_table_entry *extable;
1508 mm_segment_t old_fs;
1509 int gpgsig_ok;
1510
1511 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1512 umod, len, uargs);
1513 if (len < sizeof(*hdr))
1514 return ERR_PTR(-ENOEXEC);
1515
1516
1517
1518 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1519 return ERR_PTR(-ENOMEM);
1520 if (copy_from_user(hdr, umod, len) != 0) {
1521 err = -EFAULT;
1522 goto free_hdr;
1523 }
1524
1525
1526
1527 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1528 || hdr->e_type != ET_REL
1529 || !elf_check_arch(hdr)
1530 || hdr->e_shentsize != sizeof(*sechdrs)) {
1531 err = -ENOEXEC;
1532 goto free_hdr;
1533 }
1534
1535
1536 gpgsig_ok = 0;
1537 err = module_verify(hdr, len);
1538 if (err < 0)
1539 goto free_hdr;
1540 if (err == 1)
1541 gpgsig_ok = 1;
1542
1543
1544 sechdrs = (void *)hdr + hdr->e_shoff;
1545 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1546 sechdrs[0].sh_addr = 0;
1547
1548 for (i = 1; i < hdr->e_shnum; i++) {
1549 if (sechdrs[i].sh_type != SHT_NOBITS
1550 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1551 goto truncated;
1552
1553
1554
1555 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1556
1557
1558 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1559 symindex = i;
1560 strindex = sechdrs[i].sh_link;
1561 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1562 }
1563#ifndef CONFIG_MODULE_UNLOAD
1564
1565 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1566 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1567#endif
1568 }
1569
1570 modindex = find_sec(hdr, sechdrs, secstrings,
1571 ".gnu.linkonce.this_module");
1572 if (!modindex) {
1573 printk(KERN_WARNING "No module found in object\n");
1574 err = -ENOEXEC;
1575 goto free_hdr;
1576 }
1577 mod = (void *)sechdrs[modindex].sh_addr;
1578 mod->gpgsig_ok = gpgsig_ok;
1579
1580 if (symindex == 0) {
1581 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1582 mod->name);
1583 err = -ENOEXEC;
1584 goto free_hdr;
1585 }
1586
1587
1588 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1589 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1590 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1591 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1592 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1593 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1594 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1595 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1596 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1597 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1598
1599
1600 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1601#ifdef CONFIG_KALLSYMS
1602
1603 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1604 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1605#endif
1606
1607
1608 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1609 err = -ENOEXEC;
1610 goto free_hdr;
1611 }
1612
1613 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1614
1615 if (!modmagic) {
1616 tainted |= TAINT_FORCED_MODULE;
1617 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1618 mod->name);
1619 } else if (!same_magic(modmagic, vermagic)) {
1620 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1621 mod->name, modmagic, vermagic);
1622 err = -ENOEXEC;
1623 goto free_hdr;
1624 }
1625
1626
1627 arglen = strlen_user(uargs);
1628 if (!arglen) {
1629 err = -EFAULT;
1630 goto free_hdr;
1631 }
1632 args = kmalloc(arglen, GFP_KERNEL);
1633 if (!args) {
1634 err = -ENOMEM;
1635 goto free_hdr;
1636 }
1637 if (copy_from_user(args, uargs, arglen) != 0) {
1638 err = -EFAULT;
1639 goto free_mod;
1640 }
1641
1642 if (find_module(mod->name)) {
1643 err = -EEXIST;
1644 goto free_mod;
1645 }
1646
1647 mod->state = MODULE_STATE_COMING;
1648
1649
1650 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1651 if (err < 0)
1652 goto free_mod;
1653
1654 if (pcpuindex) {
1655
1656 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1657 sechdrs[pcpuindex].sh_addralign);
1658 if (!percpu) {
1659 err = -ENOMEM;
1660 goto free_mod;
1661 }
1662 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1663 mod->percpu = percpu;
1664 }
1665
1666
1667
1668
1669 layout_sections(mod, hdr, sechdrs, secstrings);
1670
1671
1672 ptr = module_alloc(mod->core_size);
1673 if (!ptr) {
1674 err = -ENOMEM;
1675 goto free_percpu;
1676 }
1677 memset(ptr, 0, mod->core_size);
1678 mod->module_core = ptr;
1679
1680 ptr = module_alloc(mod->init_size);
1681 if (!ptr && mod->init_size) {
1682 err = -ENOMEM;
1683 goto free_core;
1684 }
1685 memset(ptr, 0, mod->init_size);
1686 mod->module_init = ptr;
1687
1688
1689 DEBUGP("final section addresses:\n");
1690 for (i = 0; i < hdr->e_shnum; i++) {
1691 void *dest;
1692
1693 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1694 continue;
1695
1696 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1697 dest = mod->module_init
1698 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1699 else
1700 dest = mod->module_core + sechdrs[i].sh_entsize;
1701
1702 if (sechdrs[i].sh_type != SHT_NOBITS)
1703 memcpy(dest, (void *)sechdrs[i].sh_addr,
1704 sechdrs[i].sh_size);
1705
1706 sechdrs[i].sh_addr = (unsigned long)dest;
1707 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1708 }
1709
1710 mod = (void *)sechdrs[modindex].sh_addr;
1711
1712
1713 module_unload_init(mod);
1714
1715
1716 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1717
1718
1719 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1720 mod);
1721 if (err < 0)
1722 goto cleanup;
1723
1724
1725 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1726 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1727 if (crcindex)
1728 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1729 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1730 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1731 if (gplcrcindex)
1732 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1733
1734#ifdef CONFIG_MODVERSIONS
1735 if ((mod->num_syms && !crcindex) ||
1736 (mod->num_gpl_syms && !gplcrcindex)) {
1737 printk(KERN_WARNING "%s: No versions for exported symbols."
1738 " Tainting kernel.\n", mod->name);
1739 tainted |= TAINT_FORCED_MODULE;
1740 }
1741#endif
1742
1743
1744 for (i = 1; i < hdr->e_shnum; i++) {
1745 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1746 unsigned int info = sechdrs[i].sh_info;
1747
1748
1749 if (info >= hdr->e_shnum)
1750 continue;
1751
1752
1753 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1754 continue;
1755
1756 if (sechdrs[i].sh_type == SHT_REL)
1757 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1758 else if (sechdrs[i].sh_type == SHT_RELA)
1759 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1760 mod);
1761 if (err < 0)
1762 goto cleanup;
1763 }
1764
1765
1766 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1767 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1768 sort_extable(extable, extable + mod->num_exentries);
1769
1770
1771 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1772 sechdrs[pcpuindex].sh_size);
1773
1774 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1775
1776 err = module_finalize(hdr, sechdrs, mod);
1777 if (err < 0)
1778 goto cleanup;
1779
1780
1781 old_fs = get_fs();
1782 set_fs(KERNEL_DS);
1783
1784
1785
1786
1787
1788
1789 if (mod->module_init)
1790 flush_icache_range((unsigned long)mod->module_init,
1791 (unsigned long)mod->module_init
1792 + mod->init_size);
1793 flush_icache_range((unsigned long)mod->module_core,
1794 (unsigned long)mod->module_core + mod->core_size);
1795
1796 set_fs(old_fs);
1797
1798 mod->args = args;
1799 if (obsparmindex) {
1800 err = obsolete_params(mod->name, mod->args,
1801 (struct obsolete_modparm *)
1802 sechdrs[obsparmindex].sh_addr,
1803 sechdrs[obsparmindex].sh_size
1804 / sizeof(struct obsolete_modparm),
1805 sechdrs, symindex,
1806 (char *)sechdrs[strindex].sh_addr);
1807 if (setupindex)
1808 printk(KERN_WARNING "%s: Ignoring new-style "
1809 "parameters in presence of obsolete ones\n",
1810 mod->name);
1811 } else {
1812
1813 err = parse_args(mod->name, mod->args,
1814 (struct kernel_param *)
1815 sechdrs[setupindex].sh_addr,
1816 sechdrs[setupindex].sh_size
1817 / sizeof(struct kernel_param),
1818 NULL);
1819 }
1820 err = mod_sysfs_setup(mod,
1821 (struct kernel_param *)
1822 sechdrs[setupindex].sh_addr,
1823 sechdrs[setupindex].sh_size
1824 / sizeof(struct kernel_param));
1825 if (err < 0)
1826 goto arch_cleanup;
1827 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1828
1829
1830 vfree(hdr);
1831
1832
1833 return mod;
1834
1835 arch_cleanup:
1836 module_arch_cleanup(mod);
1837 cleanup:
1838 module_unload_free(mod);
1839 module_free(mod, mod->module_init);
1840 free_core:
1841 module_free(mod, mod->module_core);
1842 free_percpu:
1843 if (percpu)
1844 percpu_modfree(percpu);
1845 free_mod:
1846 kfree(args);
1847 free_hdr:
1848 vfree(hdr);
1849 if (err < 0) return ERR_PTR(err);
1850 else return ptr;
1851
1852 truncated:
1853 printk(KERN_ERR "Module len %lu truncated\n", len);
1854 err = -ENOEXEC;
1855 goto free_hdr;
1856}
1857
1858
1859
1860
1861
1862static inline int __link_module(void *_mod)
1863{
1864 struct module *mod = _mod;
1865 list_add(&mod->list, &modules);
1866 return 0;
1867}
1868
1869
1870asmlinkage long
1871sys_init_module(void __user *umod,
1872 unsigned long len,
1873 const char __user *uargs)
1874{
1875 struct module *mod;
1876 int ret = 0;
1877
1878
1879 if (!capable(CAP_SYS_MODULE))
1880 return -EPERM;
1881
1882
1883 if (down_interruptible(&module_mutex) != 0)
1884 return -EINTR;
1885
1886
1887 mod = load_module(umod, len, uargs);
1888 if (IS_ERR(mod)) {
1889 up(&module_mutex);
1890 return PTR_ERR(mod);
1891 }
1892
1893
1894
1895 stop_machine_run(__link_module, mod, NR_CPUS);
1896
1897
1898 up(&module_mutex);
1899
1900 down(¬ify_mutex);
1901 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1902 up(¬ify_mutex);
1903
1904
1905 if (mod->init != NULL)
1906 ret = mod->init();
1907 if (ret < 0) {
1908
1909
1910 mod->state = MODULE_STATE_GOING;
1911 synchronize_kernel();
1912 if (mod->unsafe)
1913 printk(KERN_ERR "%s: module is now stuck!\n",
1914 mod->name);
1915 else {
1916 module_put(mod);
1917 down(&module_mutex);
1918 free_module(mod);
1919 up(&module_mutex);
1920 }
1921 return ret;
1922 }
1923
1924
1925 down(&module_mutex);
1926 mod->state = MODULE_STATE_LIVE;
1927
1928 module_put(mod);
1929 module_free(mod, mod->module_init);
1930 mod->module_init = NULL;
1931 mod->init_size = 0;
1932 mod->init_text_size = 0;
1933 up(&module_mutex);
1934
1935 return 0;
1936}
1937
1938static inline int within(unsigned long addr, void *start, unsigned long size)
1939{
1940 return ((void *)addr >= start && (void *)addr < start + size);
1941}
1942
1943#ifdef CONFIG_KALLSYMS
1944static const char *get_ksymbol(struct module *mod,
1945 unsigned long addr,
1946 unsigned long *size,
1947 unsigned long *offset)
1948{
1949 unsigned int i, best = 0;
1950 unsigned long nextval;
1951
1952
1953 if (within(addr, mod->module_init, mod->init_size))
1954 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1955 else
1956 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1957
1958
1959
1960 for (i = 1; i < mod->num_symtab; i++) {
1961 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1962 continue;
1963
1964
1965
1966 if (mod->symtab[i].st_value <= addr
1967 && mod->symtab[i].st_value > mod->symtab[best].st_value
1968 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1969 best = i;
1970 if (mod->symtab[i].st_value > addr
1971 && mod->symtab[i].st_value < nextval
1972 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1973 nextval = mod->symtab[i].st_value;
1974 }
1975
1976 if (!best)
1977 return NULL;
1978
1979 *size = nextval - mod->symtab[best].st_value;
1980 *offset = addr - mod->symtab[best].st_value;
1981 return mod->strtab + mod->symtab[best].st_name;
1982}
1983
1984
1985
1986
1987const char *module_address_lookup(unsigned long addr,
1988 unsigned long *size,
1989 unsigned long *offset,
1990 char **modname)
1991{
1992 struct module *mod;
1993
1994 list_for_each_entry(mod, &modules, list) {
1995 if (within(addr, mod->module_init, mod->init_size)
1996 || within(addr, mod->module_core, mod->core_size)) {
1997 *modname = mod->name;
1998 return get_ksymbol(mod, addr, size, offset);
1999 }
2000 }
2001 return NULL;
2002}
2003
2004struct module *module_get_kallsym(unsigned int symnum,
2005 unsigned long *value,
2006 char *type,
2007 char namebuf[128])
2008{
2009 struct module *mod;
2010
2011 down(&module_mutex);
2012 list_for_each_entry(mod, &modules, list) {
2013 if (symnum < mod->num_symtab) {
2014 *value = mod->symtab[symnum].st_value;
2015 *type = mod->symtab[symnum].st_info;
2016 strncpy(namebuf,
2017 mod->strtab + mod->symtab[symnum].st_name,
2018 127);
2019 up(&module_mutex);
2020 return mod;
2021 }
2022 symnum -= mod->num_symtab;
2023 }
2024 up(&module_mutex);
2025 return NULL;
2026}
2027
2028static unsigned long mod_find_symname(struct module *mod, const char *name)
2029{
2030 unsigned int i;
2031
2032 for (i = 0; i < mod->num_symtab; i++)
2033 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2034 mod->symtab[i].st_info != 'U')
2035 return mod->symtab[i].st_value;
2036 return 0;
2037}
2038
2039
2040unsigned long module_kallsyms_lookup_name(const char *name)
2041{
2042 struct module *mod;
2043 char *colon;
2044 unsigned long ret = 0;
2045
2046
2047 if ((colon = strchr(name, ':')) != NULL) {
2048 *colon = '\0';
2049 if ((mod = find_module(name)) != NULL)
2050 ret = mod_find_symname(mod, colon+1);
2051 *colon = ':';
2052 } else {
2053 list_for_each_entry(mod, &modules, list)
2054 if ((ret = mod_find_symname(mod, name)) != 0)
2055 break;
2056 }
2057 return ret;
2058}
2059#endif
2060
2061
2062static void *m_start(struct seq_file *m, loff_t *pos)
2063{
2064 struct list_head *i;
2065 loff_t n = 0;
2066
2067 down(&module_mutex);
2068 list_for_each(i, &modules) {
2069 if (n++ == *pos)
2070 break;
2071 }
2072 if (i == &modules)
2073 return NULL;
2074 return i;
2075}
2076
2077static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2078{
2079 struct list_head *i = p;
2080 (*pos)++;
2081 if (i->next == &modules)
2082 return NULL;
2083 return i->next;
2084}
2085
2086static void m_stop(struct seq_file *m, void *p)
2087{
2088 up(&module_mutex);
2089}
2090
2091static int m_show(struct seq_file *m, void *p)
2092{
2093 struct module *mod = list_entry(p, struct module, list);
2094 seq_printf(m, "%s %lu",
2095 mod->name, mod->init_size + mod->core_size);
2096 print_unload_info(m, mod);
2097
2098
2099 seq_printf(m, " %s",
2100 mod->state == MODULE_STATE_GOING ? "Unloading":
2101 mod->state == MODULE_STATE_COMING ? "Loading":
2102 "Live");
2103
2104 seq_printf(m, " 0x%p", mod->module_core);
2105
2106 seq_printf(m, "\n");
2107 return 0;
2108}
2109
2110
2111
2112
2113
2114
2115struct seq_operations modules_op = {
2116 .start = m_start,
2117 .next = m_next,
2118 .stop = m_stop,
2119 .show = m_show
2120};
2121
2122
2123const struct exception_table_entry *search_module_extables(unsigned long addr)
2124{
2125 unsigned long flags;
2126 const struct exception_table_entry *e = NULL;
2127 struct module *mod;
2128
2129 spin_lock_irqsave(&modlist_lock, flags);
2130 list_for_each_entry(mod, &modules, list) {
2131 if (mod->num_exentries == 0)
2132 continue;
2133
2134 e = search_extable(mod->extable,
2135 mod->extable + mod->num_exentries - 1,
2136 addr);
2137 if (e)
2138 break;
2139 }
2140 spin_unlock_irqrestore(&modlist_lock, flags);
2141
2142
2143
2144 return e;
2145}
2146
2147
2148struct module *__module_text_address(unsigned long addr)
2149{
2150 struct module *mod;
2151
2152 list_for_each_entry(mod, &modules, list)
2153 if (within(addr, mod->module_init, mod->init_text_size)
2154 || within(addr, mod->module_core, mod->core_text_size))
2155 return mod;
2156 return NULL;
2157}
2158
2159struct module *module_text_address(unsigned long addr)
2160{
2161 struct module *mod;
2162 unsigned long flags;
2163
2164 spin_lock_irqsave(&modlist_lock, flags);
2165 mod = __module_text_address(addr);
2166 spin_unlock_irqrestore(&modlist_lock, flags);
2167
2168 return mod;
2169}
2170
2171
2172void print_modules(void)
2173{
2174 struct module *mod;
2175
2176 printk("Modules linked in:");
2177 list_for_each_entry(mod, &modules, list) {
2178 printk(" %s", mod->name);
2179#if CONFIG_MODULE_SIG
2180 if (!mod->gpgsig_ok)
2181 printk("(U)");
2182#endif
2183 }
2184 printk("\n");
2185}
2186
2187#ifdef CONFIG_MODVERSIONS
2188
2189void struct_module(struct module *mod) { return; }
2190EXPORT_SYMBOL(struct_module);
2191#endif
2192
2193static int __init modules_init(void)
2194{
2195 return subsystem_register(&module_subsys);
2196}
2197__initcall(modules_init);
2198