[PATCH] Add tainting for proprietary helper modules
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / module.c
blob5d9078d6f0fa2b0a47a311b9b728e20291e9319c
1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/elf.h>
27 #include <linux/seq_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/fcntl.h>
30 #include <linux/rcupdate.h>
31 #include <linux/cpu.h>
32 #include <linux/moduleparam.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/vermagic.h>
36 #include <linux/notifier.h>
37 #include <linux/stop_machine.h>
38 #include <linux/device.h>
39 #include <linux/string.h>
40 #include <linux/sched.h>
41 #include <asm/uaccess.h>
42 #include <asm/semaphore.h>
43 #include <asm/cacheflush.h>
45 #if 0
46 #define DEBUGP printk
47 #else
48 #define DEBUGP(fmt , a...)
49 #endif
51 #ifndef ARCH_SHF_SMALL
52 #define ARCH_SHF_SMALL 0
53 #endif
55 /* If this is set, the section belongs in the init part of the module */
56 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
58 /* Protects module list */
59 static DEFINE_SPINLOCK(modlist_lock);
61 /* List of modules, protected by module_mutex AND modlist_lock */
62 static DECLARE_MUTEX(module_mutex);
63 static LIST_HEAD(modules);
65 static DECLARE_MUTEX(notify_mutex);
66 static struct notifier_block * module_notify_list;
68 int register_module_notifier(struct notifier_block * nb)
70 int err;
71 down(&notify_mutex);
72 err = notifier_chain_register(&module_notify_list, nb);
73 up(&notify_mutex);
74 return err;
76 EXPORT_SYMBOL(register_module_notifier);
78 int unregister_module_notifier(struct notifier_block * nb)
80 int err;
81 down(&notify_mutex);
82 err = notifier_chain_unregister(&module_notify_list, nb);
83 up(&notify_mutex);
84 return err;
86 EXPORT_SYMBOL(unregister_module_notifier);
88 /* We require a truly strong try_module_get() */
89 static inline int strong_try_module_get(struct module *mod)
91 if (mod && mod->state == MODULE_STATE_COMING)
92 return 0;
93 return try_module_get(mod);
96 /* A thread that wants to hold a reference to a module only while it
97 * is running can call ths to safely exit.
98 * nfsd and lockd use this.
100 void __module_put_and_exit(struct module *mod, long code)
102 module_put(mod);
103 do_exit(code);
105 EXPORT_SYMBOL(__module_put_and_exit);
107 /* Find a module section: 0 means not found. */
108 static unsigned int find_sec(Elf_Ehdr *hdr,
109 Elf_Shdr *sechdrs,
110 const char *secstrings,
111 const char *name)
113 unsigned int i;
115 for (i = 1; i < hdr->e_shnum; i++)
116 /* Alloc bit cleared means "ignore it." */
117 if ((sechdrs[i].sh_flags & SHF_ALLOC)
118 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
119 return i;
120 return 0;
123 /* Provided by the linker */
124 extern const struct kernel_symbol __start___ksymtab[];
125 extern const struct kernel_symbol __stop___ksymtab[];
126 extern const struct kernel_symbol __start___ksymtab_gpl[];
127 extern const struct kernel_symbol __stop___ksymtab_gpl[];
128 extern const unsigned long __start___kcrctab[];
129 extern const unsigned long __start___kcrctab_gpl[];
131 #ifndef CONFIG_MODVERSIONS
132 #define symversion(base, idx) NULL
133 #else
134 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
135 #endif
137 /* Find a symbol, return value, crc and module which owns it */
138 static unsigned long __find_symbol(const char *name,
139 struct module **owner,
140 const unsigned long **crc,
141 int gplok)
143 struct module *mod;
144 unsigned int i;
146 /* Core kernel first. */
147 *owner = NULL;
148 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
149 if (strcmp(__start___ksymtab[i].name, name) == 0) {
150 *crc = symversion(__start___kcrctab, i);
151 return __start___ksymtab[i].value;
154 if (gplok) {
155 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
156 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
157 *crc = symversion(__start___kcrctab_gpl, i);
158 return __start___ksymtab_gpl[i].value;
162 /* Now try modules. */
163 list_for_each_entry(mod, &modules, list) {
164 *owner = mod;
165 for (i = 0; i < mod->num_syms; i++)
166 if (strcmp(mod->syms[i].name, name) == 0) {
167 *crc = symversion(mod->crcs, i);
168 return mod->syms[i].value;
171 if (gplok) {
172 for (i = 0; i < mod->num_gpl_syms; i++) {
173 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
174 *crc = symversion(mod->gpl_crcs, i);
175 return mod->gpl_syms[i].value;
180 DEBUGP("Failed to find symbol %s\n", name);
181 return 0;
184 /* Find a symbol in this elf symbol table */
185 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
186 unsigned int symindex,
187 const char *strtab,
188 const char *name)
190 unsigned int i;
191 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
193 /* Search (defined) internal symbols first. */
194 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
195 if (sym[i].st_shndx != SHN_UNDEF
196 && strcmp(name, strtab + sym[i].st_name) == 0)
197 return sym[i].st_value;
199 return 0;
202 /* Search for module by name: must hold module_mutex. */
203 static struct module *find_module(const char *name)
205 struct module *mod;
207 list_for_each_entry(mod, &modules, list) {
208 if (strcmp(mod->name, name) == 0)
209 return mod;
211 return NULL;
214 #ifdef CONFIG_SMP
215 /* Number of blocks used and allocated. */
216 static unsigned int pcpu_num_used, pcpu_num_allocated;
217 /* Size of each block. -ve means used. */
218 static int *pcpu_size;
220 static int split_block(unsigned int i, unsigned short size)
222 /* Reallocation required? */
223 if (pcpu_num_used + 1 > pcpu_num_allocated) {
224 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
225 GFP_KERNEL);
226 if (!new)
227 return 0;
229 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
230 pcpu_num_allocated *= 2;
231 kfree(pcpu_size);
232 pcpu_size = new;
235 /* Insert a new subblock */
236 memmove(&pcpu_size[i+1], &pcpu_size[i],
237 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
238 pcpu_num_used++;
240 pcpu_size[i+1] -= size;
241 pcpu_size[i] = size;
242 return 1;
245 static inline unsigned int block_size(int val)
247 if (val < 0)
248 return -val;
249 return val;
252 /* Created by linker magic */
253 extern char __per_cpu_start[], __per_cpu_end[];
255 static void *percpu_modalloc(unsigned long size, unsigned long align,
256 const char *name)
258 unsigned long extra;
259 unsigned int i;
260 void *ptr;
262 if (align > SMP_CACHE_BYTES) {
263 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
264 name, align, SMP_CACHE_BYTES);
265 align = SMP_CACHE_BYTES;
268 ptr = __per_cpu_start;
269 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
270 /* Extra for alignment requirement. */
271 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
272 BUG_ON(i == 0 && extra != 0);
274 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
275 continue;
277 /* Transfer extra to previous block. */
278 if (pcpu_size[i-1] < 0)
279 pcpu_size[i-1] -= extra;
280 else
281 pcpu_size[i-1] += extra;
282 pcpu_size[i] -= extra;
283 ptr += extra;
285 /* Split block if warranted */
286 if (pcpu_size[i] - size > sizeof(unsigned long))
287 if (!split_block(i, size))
288 return NULL;
290 /* Mark allocated */
291 pcpu_size[i] = -pcpu_size[i];
292 return ptr;
295 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
296 size);
297 return NULL;
300 static void percpu_modfree(void *freeme)
302 unsigned int i;
303 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
305 /* First entry is core kernel percpu data. */
306 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
307 if (ptr == freeme) {
308 pcpu_size[i] = -pcpu_size[i];
309 goto free;
312 BUG();
314 free:
315 /* Merge with previous? */
316 if (pcpu_size[i-1] >= 0) {
317 pcpu_size[i-1] += pcpu_size[i];
318 pcpu_num_used--;
319 memmove(&pcpu_size[i], &pcpu_size[i+1],
320 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
321 i--;
323 /* Merge with next? */
324 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
325 pcpu_size[i] += pcpu_size[i+1];
326 pcpu_num_used--;
327 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
328 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
332 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
333 Elf_Shdr *sechdrs,
334 const char *secstrings)
336 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
339 static int percpu_modinit(void)
341 pcpu_num_used = 2;
342 pcpu_num_allocated = 2;
343 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
344 GFP_KERNEL);
345 /* Static in-kernel percpu data (used). */
346 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
347 /* Free room. */
348 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
349 if (pcpu_size[1] < 0) {
350 printk(KERN_ERR "No per-cpu room for modules.\n");
351 pcpu_num_used = 1;
354 return 0;
356 __initcall(percpu_modinit);
357 #else /* ... !CONFIG_SMP */
358 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
359 const char *name)
361 return NULL;
363 static inline void percpu_modfree(void *pcpuptr)
365 BUG();
367 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
368 Elf_Shdr *sechdrs,
369 const char *secstrings)
371 return 0;
373 static inline void percpu_modcopy(void *pcpudst, const void *src,
374 unsigned long size)
376 /* pcpusec should be 0, and size of that section should be 0. */
377 BUG_ON(size != 0);
379 #endif /* CONFIG_SMP */
381 #ifdef CONFIG_MODULE_UNLOAD
382 #define MODINFO_ATTR(field) \
383 static void setup_modinfo_##field(struct module *mod, const char *s) \
385 mod->field = kstrdup(s, GFP_KERNEL); \
387 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
388 struct module *mod, char *buffer) \
390 return sprintf(buffer, "%s\n", mod->field); \
392 static int modinfo_##field##_exists(struct module *mod) \
394 return mod->field != NULL; \
396 static void free_modinfo_##field(struct module *mod) \
398 kfree(mod->field); \
399 mod->field = NULL; \
401 static struct module_attribute modinfo_##field = { \
402 .attr = { .name = __stringify(field), .mode = 0444, \
403 .owner = THIS_MODULE }, \
404 .show = show_modinfo_##field, \
405 .setup = setup_modinfo_##field, \
406 .test = modinfo_##field##_exists, \
407 .free = free_modinfo_##field, \
410 MODINFO_ATTR(version);
411 MODINFO_ATTR(srcversion);
413 static struct module_attribute *modinfo_attrs[] = {
414 &modinfo_version,
415 &modinfo_srcversion,
416 NULL,
419 /* Init the unload section of the module. */
420 static void module_unload_init(struct module *mod)
422 unsigned int i;
424 INIT_LIST_HEAD(&mod->modules_which_use_me);
425 for (i = 0; i < NR_CPUS; i++)
426 local_set(&mod->ref[i].count, 0);
427 /* Hold reference count during initialization. */
428 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
429 /* Backwards compatibility macros put refcount during init. */
430 mod->waiter = current;
433 /* modules using other modules */
434 struct module_use
436 struct list_head list;
437 struct module *module_which_uses;
440 /* Does a already use b? */
441 static int already_uses(struct module *a, struct module *b)
443 struct module_use *use;
445 list_for_each_entry(use, &b->modules_which_use_me, list) {
446 if (use->module_which_uses == a) {
447 DEBUGP("%s uses %s!\n", a->name, b->name);
448 return 1;
451 DEBUGP("%s does not use %s!\n", a->name, b->name);
452 return 0;
455 /* Module a uses b */
456 static int use_module(struct module *a, struct module *b)
458 struct module_use *use;
459 if (b == NULL || already_uses(a, b)) return 1;
461 if (!strong_try_module_get(b))
462 return 0;
464 DEBUGP("Allocating new usage for %s.\n", a->name);
465 use = kmalloc(sizeof(*use), GFP_ATOMIC);
466 if (!use) {
467 printk("%s: out of memory loading\n", a->name);
468 module_put(b);
469 return 0;
472 use->module_which_uses = a;
473 list_add(&use->list, &b->modules_which_use_me);
474 return 1;
477 /* Clear the unload stuff of the module. */
478 static void module_unload_free(struct module *mod)
480 struct module *i;
482 list_for_each_entry(i, &modules, list) {
483 struct module_use *use;
485 list_for_each_entry(use, &i->modules_which_use_me, list) {
486 if (use->module_which_uses == mod) {
487 DEBUGP("%s unusing %s\n", mod->name, i->name);
488 module_put(i);
489 list_del(&use->list);
490 kfree(use);
491 /* There can be at most one match. */
492 break;
498 #ifdef CONFIG_MODULE_FORCE_UNLOAD
499 static inline int try_force(unsigned int flags)
501 int ret = (flags & O_TRUNC);
502 if (ret)
503 add_taint(TAINT_FORCED_MODULE);
504 return ret;
506 #else
507 static inline int try_force(unsigned int flags)
509 return 0;
511 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
513 struct stopref
515 struct module *mod;
516 int flags;
517 int *forced;
520 /* Whole machine is stopped with interrupts off when this runs. */
521 static int __try_stop_module(void *_sref)
523 struct stopref *sref = _sref;
525 /* If it's not unused, quit unless we are told to block. */
526 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
527 if (!(*sref->forced = try_force(sref->flags)))
528 return -EWOULDBLOCK;
531 /* Mark it as dying. */
532 sref->mod->state = MODULE_STATE_GOING;
533 return 0;
536 static int try_stop_module(struct module *mod, int flags, int *forced)
538 struct stopref sref = { mod, flags, forced };
540 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
543 unsigned int module_refcount(struct module *mod)
545 unsigned int i, total = 0;
547 for (i = 0; i < NR_CPUS; i++)
548 total += local_read(&mod->ref[i].count);
549 return total;
551 EXPORT_SYMBOL(module_refcount);
553 /* This exists whether we can unload or not */
554 static void free_module(struct module *mod);
556 static void wait_for_zero_refcount(struct module *mod)
558 /* Since we might sleep for some time, drop the semaphore first */
559 up(&module_mutex);
560 for (;;) {
561 DEBUGP("Looking at refcount...\n");
562 set_current_state(TASK_UNINTERRUPTIBLE);
563 if (module_refcount(mod) == 0)
564 break;
565 schedule();
567 current->state = TASK_RUNNING;
568 down(&module_mutex);
571 asmlinkage long
572 sys_delete_module(const char __user *name_user, unsigned int flags)
574 struct module *mod;
575 char name[MODULE_NAME_LEN];
576 int ret, forced = 0;
578 if (!capable(CAP_SYS_MODULE))
579 return -EPERM;
581 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
582 return -EFAULT;
583 name[MODULE_NAME_LEN-1] = '\0';
585 if (down_interruptible(&module_mutex) != 0)
586 return -EINTR;
588 mod = find_module(name);
589 if (!mod) {
590 ret = -ENOENT;
591 goto out;
594 if (!list_empty(&mod->modules_which_use_me)) {
595 /* Other modules depend on us: get rid of them first. */
596 ret = -EWOULDBLOCK;
597 goto out;
600 /* Doing init or already dying? */
601 if (mod->state != MODULE_STATE_LIVE) {
602 /* FIXME: if (force), slam module count and wake up
603 waiter --RR */
604 DEBUGP("%s already dying\n", mod->name);
605 ret = -EBUSY;
606 goto out;
609 /* If it has an init func, it must have an exit func to unload */
610 if ((mod->init != NULL && mod->exit == NULL)
611 || mod->unsafe) {
612 forced = try_force(flags);
613 if (!forced) {
614 /* This module can't be removed */
615 ret = -EBUSY;
616 goto out;
620 /* Set this up before setting mod->state */
621 mod->waiter = current;
623 /* Stop the machine so refcounts can't move and disable module. */
624 ret = try_stop_module(mod, flags, &forced);
625 if (ret != 0)
626 goto out;
628 /* Never wait if forced. */
629 if (!forced && module_refcount(mod) != 0)
630 wait_for_zero_refcount(mod);
632 /* Final destruction now noone is using it. */
633 if (mod->exit != NULL) {
634 up(&module_mutex);
635 mod->exit();
636 down(&module_mutex);
638 free_module(mod);
640 out:
641 up(&module_mutex);
642 return ret;
645 static void print_unload_info(struct seq_file *m, struct module *mod)
647 struct module_use *use;
648 int printed_something = 0;
650 seq_printf(m, " %u ", module_refcount(mod));
652 /* Always include a trailing , so userspace can differentiate
653 between this and the old multi-field proc format. */
654 list_for_each_entry(use, &mod->modules_which_use_me, list) {
655 printed_something = 1;
656 seq_printf(m, "%s,", use->module_which_uses->name);
659 if (mod->unsafe) {
660 printed_something = 1;
661 seq_printf(m, "[unsafe],");
664 if (mod->init != NULL && mod->exit == NULL) {
665 printed_something = 1;
666 seq_printf(m, "[permanent],");
669 if (!printed_something)
670 seq_printf(m, "-");
673 void __symbol_put(const char *symbol)
675 struct module *owner;
676 unsigned long flags;
677 const unsigned long *crc;
679 spin_lock_irqsave(&modlist_lock, flags);
680 if (!__find_symbol(symbol, &owner, &crc, 1))
681 BUG();
682 module_put(owner);
683 spin_unlock_irqrestore(&modlist_lock, flags);
685 EXPORT_SYMBOL(__symbol_put);
687 void symbol_put_addr(void *addr)
689 unsigned long flags;
691 spin_lock_irqsave(&modlist_lock, flags);
692 if (!kernel_text_address((unsigned long)addr))
693 BUG();
695 module_put(module_text_address((unsigned long)addr));
696 spin_unlock_irqrestore(&modlist_lock, flags);
698 EXPORT_SYMBOL_GPL(symbol_put_addr);
700 static ssize_t show_refcnt(struct module_attribute *mattr,
701 struct module *mod, char *buffer)
703 /* sysfs holds a reference */
704 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
707 static struct module_attribute refcnt = {
708 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
709 .show = show_refcnt,
712 #else /* !CONFIG_MODULE_UNLOAD */
713 static void print_unload_info(struct seq_file *m, struct module *mod)
715 /* We don't know the usage count, or what modules are using. */
716 seq_printf(m, " - -");
719 static inline void module_unload_free(struct module *mod)
723 static inline int use_module(struct module *a, struct module *b)
725 return strong_try_module_get(b);
728 static inline void module_unload_init(struct module *mod)
731 #endif /* CONFIG_MODULE_UNLOAD */
733 #ifdef CONFIG_OBSOLETE_MODPARM
734 /* Bounds checking done below */
735 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
737 strcpy(kp->arg, val);
738 return 0;
741 static int set_obsolete(const char *val, struct kernel_param *kp)
743 unsigned int min, max;
744 unsigned int size, maxsize;
745 int dummy;
746 char *endp;
747 const char *p;
748 struct obsolete_modparm *obsparm = kp->arg;
750 if (!val) {
751 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
752 return -EINVAL;
755 /* type is: [min[-max]]{b,h,i,l,s} */
756 p = obsparm->type;
757 min = simple_strtol(p, &endp, 10);
758 if (endp == obsparm->type)
759 min = max = 1;
760 else if (*endp == '-') {
761 p = endp+1;
762 max = simple_strtol(p, &endp, 10);
763 } else
764 max = min;
765 switch (*endp) {
766 case 'b':
767 return param_array(kp->name, val, min, max, obsparm->addr,
768 1, param_set_byte, &dummy);
769 case 'h':
770 return param_array(kp->name, val, min, max, obsparm->addr,
771 sizeof(short), param_set_short, &dummy);
772 case 'i':
773 return param_array(kp->name, val, min, max, obsparm->addr,
774 sizeof(int), param_set_int, &dummy);
775 case 'l':
776 return param_array(kp->name, val, min, max, obsparm->addr,
777 sizeof(long), param_set_long, &dummy);
778 case 's':
779 return param_array(kp->name, val, min, max, obsparm->addr,
780 sizeof(char *), param_set_charp, &dummy);
782 case 'c':
783 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
784 and the decl is "char xxx[5][50];" */
785 p = endp+1;
786 maxsize = simple_strtol(p, &endp, 10);
787 /* We check lengths here (yes, this is a hack). */
788 p = val;
789 while (p[size = strcspn(p, ",")]) {
790 if (size >= maxsize)
791 goto oversize;
792 p += size+1;
794 if (size >= maxsize)
795 goto oversize;
796 return param_array(kp->name, val, min, max, obsparm->addr,
797 maxsize, obsparm_copy_string, &dummy);
799 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
800 return -EINVAL;
801 oversize:
802 printk(KERN_ERR
803 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
804 return -EINVAL;
807 static int obsolete_params(const char *name,
808 char *args,
809 struct obsolete_modparm obsparm[],
810 unsigned int num,
811 Elf_Shdr *sechdrs,
812 unsigned int symindex,
813 const char *strtab)
815 struct kernel_param *kp;
816 unsigned int i;
817 int ret;
819 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
820 if (!kp)
821 return -ENOMEM;
823 for (i = 0; i < num; i++) {
824 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
826 snprintf(sym_name, sizeof(sym_name), "%s%s",
827 MODULE_SYMBOL_PREFIX, obsparm[i].name);
829 kp[i].name = obsparm[i].name;
830 kp[i].perm = 000;
831 kp[i].set = set_obsolete;
832 kp[i].get = NULL;
833 obsparm[i].addr
834 = (void *)find_local_symbol(sechdrs, symindex, strtab,
835 sym_name);
836 if (!obsparm[i].addr) {
837 printk("%s: falsely claims to have parameter %s\n",
838 name, obsparm[i].name);
839 ret = -EINVAL;
840 goto out;
842 kp[i].arg = &obsparm[i];
845 ret = parse_args(name, args, kp, num, NULL);
846 out:
847 kfree(kp);
848 return ret;
850 #else
851 static int obsolete_params(const char *name,
852 char *args,
853 struct obsolete_modparm obsparm[],
854 unsigned int num,
855 Elf_Shdr *sechdrs,
856 unsigned int symindex,
857 const char *strtab)
859 if (num != 0)
860 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
861 name);
862 return 0;
864 #endif /* CONFIG_OBSOLETE_MODPARM */
866 static const char vermagic[] = VERMAGIC_STRING;
868 #ifdef CONFIG_MODVERSIONS
869 static int check_version(Elf_Shdr *sechdrs,
870 unsigned int versindex,
871 const char *symname,
872 struct module *mod,
873 const unsigned long *crc)
875 unsigned int i, num_versions;
876 struct modversion_info *versions;
878 /* Exporting module didn't supply crcs? OK, we're already tainted. */
879 if (!crc)
880 return 1;
882 versions = (void *) sechdrs[versindex].sh_addr;
883 num_versions = sechdrs[versindex].sh_size
884 / sizeof(struct modversion_info);
886 for (i = 0; i < num_versions; i++) {
887 if (strcmp(versions[i].name, symname) != 0)
888 continue;
890 if (versions[i].crc == *crc)
891 return 1;
892 printk("%s: disagrees about version of symbol %s\n",
893 mod->name, symname);
894 DEBUGP("Found checksum %lX vs module %lX\n",
895 *crc, versions[i].crc);
896 return 0;
898 /* Not in module's version table. OK, but that taints the kernel. */
899 if (!(tainted & TAINT_FORCED_MODULE)) {
900 printk("%s: no version for \"%s\" found: kernel tainted.\n",
901 mod->name, symname);
902 add_taint(TAINT_FORCED_MODULE);
904 return 1;
907 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
908 unsigned int versindex,
909 struct module *mod)
911 const unsigned long *crc;
912 struct module *owner;
914 if (!__find_symbol("struct_module", &owner, &crc, 1))
915 BUG();
916 return check_version(sechdrs, versindex, "struct_module", mod,
917 crc);
920 /* First part is kernel version, which we ignore. */
921 static inline int same_magic(const char *amagic, const char *bmagic)
923 amagic += strcspn(amagic, " ");
924 bmagic += strcspn(bmagic, " ");
925 return strcmp(amagic, bmagic) == 0;
927 #else
928 static inline int check_version(Elf_Shdr *sechdrs,
929 unsigned int versindex,
930 const char *symname,
931 struct module *mod,
932 const unsigned long *crc)
934 return 1;
937 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
938 unsigned int versindex,
939 struct module *mod)
941 return 1;
944 static inline int same_magic(const char *amagic, const char *bmagic)
946 return strcmp(amagic, bmagic) == 0;
948 #endif /* CONFIG_MODVERSIONS */
950 /* Resolve a symbol for this module. I.e. if we find one, record usage.
951 Must be holding module_mutex. */
952 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
953 unsigned int versindex,
954 const char *name,
955 struct module *mod)
957 struct module *owner;
958 unsigned long ret;
959 const unsigned long *crc;
961 spin_lock_irq(&modlist_lock);
962 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
963 if (ret) {
964 /* use_module can fail due to OOM, or module unloading */
965 if (!check_version(sechdrs, versindex, name, mod, crc) ||
966 !use_module(mod, owner))
967 ret = 0;
969 spin_unlock_irq(&modlist_lock);
970 return ret;
975 * /sys/module/foo/sections stuff
976 * J. Corbet <corbet@lwn.net>
978 #ifdef CONFIG_KALLSYMS
979 static ssize_t module_sect_show(struct module_attribute *mattr,
980 struct module *mod, char *buf)
982 struct module_sect_attr *sattr =
983 container_of(mattr, struct module_sect_attr, mattr);
984 return sprintf(buf, "0x%lx\n", sattr->address);
987 static void add_sect_attrs(struct module *mod, unsigned int nsect,
988 char *secstrings, Elf_Shdr *sechdrs)
990 unsigned int nloaded = 0, i, size[2];
991 struct module_sect_attrs *sect_attrs;
992 struct module_sect_attr *sattr;
993 struct attribute **gattr;
995 /* Count loaded sections and allocate structures */
996 for (i = 0; i < nsect; i++)
997 if (sechdrs[i].sh_flags & SHF_ALLOC)
998 nloaded++;
999 size[0] = ALIGN(sizeof(*sect_attrs)
1000 + nloaded * sizeof(sect_attrs->attrs[0]),
1001 sizeof(sect_attrs->grp.attrs[0]));
1002 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1003 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1004 return;
1006 /* Setup section attributes. */
1007 sect_attrs->grp.name = "sections";
1008 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1010 sattr = &sect_attrs->attrs[0];
1011 gattr = &sect_attrs->grp.attrs[0];
1012 for (i = 0; i < nsect; i++) {
1013 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1014 continue;
1015 sattr->address = sechdrs[i].sh_addr;
1016 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1017 MODULE_SECT_NAME_LEN);
1018 sattr->mattr.show = module_sect_show;
1019 sattr->mattr.store = NULL;
1020 sattr->mattr.attr.name = sattr->name;
1021 sattr->mattr.attr.owner = mod;
1022 sattr->mattr.attr.mode = S_IRUGO;
1023 *(gattr++) = &(sattr++)->mattr.attr;
1025 *gattr = NULL;
1027 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1028 goto out;
1030 mod->sect_attrs = sect_attrs;
1031 return;
1032 out:
1033 kfree(sect_attrs);
1036 static void remove_sect_attrs(struct module *mod)
1038 if (mod->sect_attrs) {
1039 sysfs_remove_group(&mod->mkobj.kobj,
1040 &mod->sect_attrs->grp);
1041 /* We are positive that no one is using any sect attrs
1042 * at this point. Deallocate immediately. */
1043 kfree(mod->sect_attrs);
1044 mod->sect_attrs = NULL;
1049 #else
1050 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1051 char *sectstrings, Elf_Shdr *sechdrs)
1055 static inline void remove_sect_attrs(struct module *mod)
1058 #endif /* CONFIG_KALLSYMS */
1061 #ifdef CONFIG_MODULE_UNLOAD
1062 static inline int module_add_refcnt_attr(struct module *mod)
1064 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1066 static void module_remove_refcnt_attr(struct module *mod)
1068 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1070 #else
1071 static inline int module_add_refcnt_attr(struct module *mod)
1073 return 0;
1075 static void module_remove_refcnt_attr(struct module *mod)
1078 #endif
1080 #ifdef CONFIG_MODULE_UNLOAD
1081 static int module_add_modinfo_attrs(struct module *mod)
1083 struct module_attribute *attr;
1084 int error = 0;
1085 int i;
1087 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1088 if (!attr->test ||
1089 (attr->test && attr->test(mod)))
1090 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1092 return error;
1095 static void module_remove_modinfo_attrs(struct module *mod)
1097 struct module_attribute *attr;
1098 int i;
1100 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1101 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1102 attr->free(mod);
1105 #endif
1107 static int mod_sysfs_setup(struct module *mod,
1108 struct kernel_param *kparam,
1109 unsigned int num_params)
1111 int err;
1113 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1114 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1115 if (err)
1116 goto out;
1117 kobj_set_kset_s(&mod->mkobj, module_subsys);
1118 mod->mkobj.mod = mod;
1119 err = kobject_register(&mod->mkobj.kobj);
1120 if (err)
1121 goto out;
1123 err = module_add_refcnt_attr(mod);
1124 if (err)
1125 goto out_unreg;
1127 err = module_param_sysfs_setup(mod, kparam, num_params);
1128 if (err)
1129 goto out_unreg;
1131 #ifdef CONFIG_MODULE_UNLOAD
1132 err = module_add_modinfo_attrs(mod);
1133 if (err)
1134 goto out_unreg;
1135 #endif
1137 return 0;
1139 out_unreg:
1140 kobject_unregister(&mod->mkobj.kobj);
1141 out:
1142 return err;
1145 static void mod_kobject_remove(struct module *mod)
1147 #ifdef CONFIG_MODULE_UNLOAD
1148 module_remove_modinfo_attrs(mod);
1149 #endif
1150 module_remove_refcnt_attr(mod);
1151 module_param_sysfs_remove(mod);
1153 kobject_unregister(&mod->mkobj.kobj);
1157 * unlink the module with the whole machine is stopped with interrupts off
1158 * - this defends against kallsyms not taking locks
1160 static int __unlink_module(void *_mod)
1162 struct module *mod = _mod;
1163 list_del(&mod->list);
1164 return 0;
1167 /* Free a module, remove from lists, etc (must hold module mutex). */
1168 static void free_module(struct module *mod)
1170 /* Delete from various lists */
1171 stop_machine_run(__unlink_module, mod, NR_CPUS);
1172 remove_sect_attrs(mod);
1173 mod_kobject_remove(mod);
1175 /* Arch-specific cleanup. */
1176 module_arch_cleanup(mod);
1178 /* Module unload stuff */
1179 module_unload_free(mod);
1181 /* This may be NULL, but that's OK */
1182 module_free(mod, mod->module_init);
1183 kfree(mod->args);
1184 if (mod->percpu)
1185 percpu_modfree(mod->percpu);
1187 /* Finally, free the core (containing the module structure) */
1188 module_free(mod, mod->module_core);
1191 void *__symbol_get(const char *symbol)
1193 struct module *owner;
1194 unsigned long value, flags;
1195 const unsigned long *crc;
1197 spin_lock_irqsave(&modlist_lock, flags);
1198 value = __find_symbol(symbol, &owner, &crc, 1);
1199 if (value && !strong_try_module_get(owner))
1200 value = 0;
1201 spin_unlock_irqrestore(&modlist_lock, flags);
1203 return (void *)value;
1205 EXPORT_SYMBOL_GPL(__symbol_get);
1207 /* Change all symbols so that sh_value encodes the pointer directly. */
1208 static int simplify_symbols(Elf_Shdr *sechdrs,
1209 unsigned int symindex,
1210 const char *strtab,
1211 unsigned int versindex,
1212 unsigned int pcpuindex,
1213 struct module *mod)
1215 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1216 unsigned long secbase;
1217 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1218 int ret = 0;
1220 for (i = 1; i < n; i++) {
1221 switch (sym[i].st_shndx) {
1222 case SHN_COMMON:
1223 /* We compiled with -fno-common. These are not
1224 supposed to happen. */
1225 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1226 printk("%s: please compile with -fno-common\n",
1227 mod->name);
1228 ret = -ENOEXEC;
1229 break;
1231 case SHN_ABS:
1232 /* Don't need to do anything */
1233 DEBUGP("Absolute symbol: 0x%08lx\n",
1234 (long)sym[i].st_value);
1235 break;
1237 case SHN_UNDEF:
1238 sym[i].st_value
1239 = resolve_symbol(sechdrs, versindex,
1240 strtab + sym[i].st_name, mod);
1242 /* Ok if resolved. */
1243 if (sym[i].st_value != 0)
1244 break;
1245 /* Ok if weak. */
1246 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1247 break;
1249 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1250 mod->name, strtab + sym[i].st_name);
1251 ret = -ENOENT;
1252 break;
1254 default:
1255 /* Divert to percpu allocation if a percpu var. */
1256 if (sym[i].st_shndx == pcpuindex)
1257 secbase = (unsigned long)mod->percpu;
1258 else
1259 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1260 sym[i].st_value += secbase;
1261 break;
1265 return ret;
1268 /* Update size with this section: return offset. */
1269 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1271 long ret;
1273 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1274 *size = ret + sechdr->sh_size;
1275 return ret;
1278 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1279 might -- code, read-only data, read-write data, small data. Tally
1280 sizes, and place the offsets into sh_entsize fields: high bit means it
1281 belongs in init. */
1282 static void layout_sections(struct module *mod,
1283 const Elf_Ehdr *hdr,
1284 Elf_Shdr *sechdrs,
1285 const char *secstrings)
1287 static unsigned long const masks[][2] = {
1288 /* NOTE: all executable code must be the first section
1289 * in this array; otherwise modify the text_size
1290 * finder in the two loops below */
1291 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1292 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1293 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1294 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1296 unsigned int m, i;
1298 for (i = 0; i < hdr->e_shnum; i++)
1299 sechdrs[i].sh_entsize = ~0UL;
1301 DEBUGP("Core section allocation order:\n");
1302 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1303 for (i = 0; i < hdr->e_shnum; ++i) {
1304 Elf_Shdr *s = &sechdrs[i];
1306 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1307 || (s->sh_flags & masks[m][1])
1308 || s->sh_entsize != ~0UL
1309 || strncmp(secstrings + s->sh_name,
1310 ".init", 5) == 0)
1311 continue;
1312 s->sh_entsize = get_offset(&mod->core_size, s);
1313 DEBUGP("\t%s\n", secstrings + s->sh_name);
1315 if (m == 0)
1316 mod->core_text_size = mod->core_size;
1319 DEBUGP("Init section allocation order:\n");
1320 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1321 for (i = 0; i < hdr->e_shnum; ++i) {
1322 Elf_Shdr *s = &sechdrs[i];
1324 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1325 || (s->sh_flags & masks[m][1])
1326 || s->sh_entsize != ~0UL
1327 || strncmp(secstrings + s->sh_name,
1328 ".init", 5) != 0)
1329 continue;
1330 s->sh_entsize = (get_offset(&mod->init_size, s)
1331 | INIT_OFFSET_MASK);
1332 DEBUGP("\t%s\n", secstrings + s->sh_name);
1334 if (m == 0)
1335 mod->init_text_size = mod->init_size;
1339 static inline int license_is_gpl_compatible(const char *license)
1341 return (strcmp(license, "GPL") == 0
1342 || strcmp(license, "GPL v2") == 0
1343 || strcmp(license, "GPL and additional rights") == 0
1344 || strcmp(license, "Dual BSD/GPL") == 0
1345 || strcmp(license, "Dual MPL/GPL") == 0);
1348 static void set_license(struct module *mod, const char *license)
1350 if (!license)
1351 license = "unspecified";
1353 mod->license_gplok = license_is_gpl_compatible(license);
1354 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1355 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1356 mod->name, license);
1357 add_taint(TAINT_PROPRIETARY_MODULE);
1361 /* Parse tag=value strings from .modinfo section */
1362 static char *next_string(char *string, unsigned long *secsize)
1364 /* Skip non-zero chars */
1365 while (string[0]) {
1366 string++;
1367 if ((*secsize)-- <= 1)
1368 return NULL;
1371 /* Skip any zero padding. */
1372 while (!string[0]) {
1373 string++;
1374 if ((*secsize)-- <= 1)
1375 return NULL;
1377 return string;
1380 static char *get_modinfo(Elf_Shdr *sechdrs,
1381 unsigned int info,
1382 const char *tag)
1384 char *p;
1385 unsigned int taglen = strlen(tag);
1386 unsigned long size = sechdrs[info].sh_size;
1388 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1389 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1390 return p + taglen + 1;
1392 return NULL;
1395 #ifdef CONFIG_MODULE_UNLOAD
1396 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1397 unsigned int infoindex)
1399 struct module_attribute *attr;
1400 int i;
1402 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1403 if (attr->setup)
1404 attr->setup(mod,
1405 get_modinfo(sechdrs,
1406 infoindex,
1407 attr->attr.name));
1410 #endif
1412 #ifdef CONFIG_KALLSYMS
1413 int is_exported(const char *name, const struct module *mod)
1415 unsigned int i;
1417 if (!mod) {
1418 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1419 if (strcmp(__start___ksymtab[i].name, name) == 0)
1420 return 1;
1421 return 0;
1423 for (i = 0; i < mod->num_syms; i++)
1424 if (strcmp(mod->syms[i].name, name) == 0)
1425 return 1;
1426 return 0;
1429 /* As per nm */
1430 static char elf_type(const Elf_Sym *sym,
1431 Elf_Shdr *sechdrs,
1432 const char *secstrings,
1433 struct module *mod)
1435 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1436 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1437 return 'v';
1438 else
1439 return 'w';
1441 if (sym->st_shndx == SHN_UNDEF)
1442 return 'U';
1443 if (sym->st_shndx == SHN_ABS)
1444 return 'a';
1445 if (sym->st_shndx >= SHN_LORESERVE)
1446 return '?';
1447 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1448 return 't';
1449 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1450 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1451 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1452 return 'r';
1453 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1454 return 'g';
1455 else
1456 return 'd';
1458 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1459 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1460 return 's';
1461 else
1462 return 'b';
1464 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1465 ".debug", strlen(".debug")) == 0)
1466 return 'n';
1467 return '?';
1470 static void add_kallsyms(struct module *mod,
1471 Elf_Shdr *sechdrs,
1472 unsigned int symindex,
1473 unsigned int strindex,
1474 const char *secstrings)
1476 unsigned int i;
1478 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1479 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1480 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1482 /* Set types up while we still have access to sections. */
1483 for (i = 0; i < mod->num_symtab; i++)
1484 mod->symtab[i].st_info
1485 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1487 #else
1488 static inline void add_kallsyms(struct module *mod,
1489 Elf_Shdr *sechdrs,
1490 unsigned int symindex,
1491 unsigned int strindex,
1492 const char *secstrings)
1495 #endif /* CONFIG_KALLSYMS */
1497 /* Allocate and load the module: note that size of section 0 is always
1498 zero, and we rely on this for optional sections. */
1499 static struct module *load_module(void __user *umod,
1500 unsigned long len,
1501 const char __user *uargs)
1503 Elf_Ehdr *hdr;
1504 Elf_Shdr *sechdrs;
1505 char *secstrings, *args, *modmagic, *strtab = NULL;
1506 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1507 exportindex, modindex, obsparmindex, infoindex, gplindex,
1508 crcindex, gplcrcindex, versindex, pcpuindex;
1509 long arglen;
1510 struct module *mod;
1511 long err = 0;
1512 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1513 struct exception_table_entry *extable;
1514 mm_segment_t old_fs;
1516 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1517 umod, len, uargs);
1518 if (len < sizeof(*hdr))
1519 return ERR_PTR(-ENOEXEC);
1521 /* Suck in entire file: we'll want most of it. */
1522 /* vmalloc barfs on "unusual" numbers. Check here */
1523 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1524 return ERR_PTR(-ENOMEM);
1525 if (copy_from_user(hdr, umod, len) != 0) {
1526 err = -EFAULT;
1527 goto free_hdr;
1530 /* Sanity checks against insmoding binaries or wrong arch,
1531 weird elf version */
1532 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1533 || hdr->e_type != ET_REL
1534 || !elf_check_arch(hdr)
1535 || hdr->e_shentsize != sizeof(*sechdrs)) {
1536 err = -ENOEXEC;
1537 goto free_hdr;
1540 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1541 goto truncated;
1543 /* Convenience variables */
1544 sechdrs = (void *)hdr + hdr->e_shoff;
1545 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1546 sechdrs[0].sh_addr = 0;
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;
1553 /* Mark all sections sh_addr with their address in the
1554 temporary image. */
1555 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1557 /* Internal symbols and strings. */
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;
1563 #ifndef CONFIG_MODULE_UNLOAD
1564 /* Don't load .exit sections */
1565 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1566 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1567 #endif
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;
1577 mod = (void *)sechdrs[modindex].sh_addr;
1579 if (symindex == 0) {
1580 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1581 mod->name);
1582 err = -ENOEXEC;
1583 goto free_hdr;
1586 /* Optional sections */
1587 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1588 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1589 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1590 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1591 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1592 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1593 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1594 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1595 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1596 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1598 /* Don't keep modinfo section */
1599 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1600 #ifdef CONFIG_KALLSYMS
1601 /* Keep symbol and string tables for decoding later. */
1602 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1603 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1604 #endif
1606 /* Check module struct version now, before we try to use module. */
1607 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1608 err = -ENOEXEC;
1609 goto free_hdr;
1612 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1613 /* This is allowed: modprobe --force will invalidate it. */
1614 if (!modmagic) {
1615 add_taint(TAINT_FORCED_MODULE);
1616 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1617 mod->name);
1618 } else if (!same_magic(modmagic, vermagic)) {
1619 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1620 mod->name, modmagic, vermagic);
1621 err = -ENOEXEC;
1622 goto free_hdr;
1625 /* Now copy in args */
1626 arglen = strlen_user(uargs);
1627 if (!arglen) {
1628 err = -EFAULT;
1629 goto free_hdr;
1631 args = kmalloc(arglen, GFP_KERNEL);
1632 if (!args) {
1633 err = -ENOMEM;
1634 goto free_hdr;
1636 if (copy_from_user(args, uargs, arglen) != 0) {
1637 err = -EFAULT;
1638 goto free_mod;
1641 if (find_module(mod->name)) {
1642 err = -EEXIST;
1643 goto free_mod;
1646 mod->state = MODULE_STATE_COMING;
1648 /* Allow arches to frob section contents and sizes. */
1649 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1650 if (err < 0)
1651 goto free_mod;
1653 if (pcpuindex) {
1654 /* We have a special allocation for this section. */
1655 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1656 sechdrs[pcpuindex].sh_addralign,
1657 mod->name);
1658 if (!percpu) {
1659 err = -ENOMEM;
1660 goto free_mod;
1662 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1663 mod->percpu = percpu;
1666 /* Determine total sizes, and put offsets in sh_entsize. For now
1667 this is done generically; there doesn't appear to be any
1668 special cases for the architectures. */
1669 layout_sections(mod, hdr, sechdrs, secstrings);
1671 /* Do the allocs. */
1672 ptr = module_alloc(mod->core_size);
1673 if (!ptr) {
1674 err = -ENOMEM;
1675 goto free_percpu;
1677 memset(ptr, 0, mod->core_size);
1678 mod->module_core = ptr;
1680 ptr = module_alloc(mod->init_size);
1681 if (!ptr && mod->init_size) {
1682 err = -ENOMEM;
1683 goto free_core;
1685 memset(ptr, 0, mod->init_size);
1686 mod->module_init = ptr;
1688 /* Transfer each section which specifies SHF_ALLOC */
1689 DEBUGP("final section addresses:\n");
1690 for (i = 0; i < hdr->e_shnum; i++) {
1691 void *dest;
1693 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1694 continue;
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;
1702 if (sechdrs[i].sh_type != SHT_NOBITS)
1703 memcpy(dest, (void *)sechdrs[i].sh_addr,
1704 sechdrs[i].sh_size);
1705 /* Update sh_addr to point to copy in image. */
1706 sechdrs[i].sh_addr = (unsigned long)dest;
1707 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1709 /* Module has been moved. */
1710 mod = (void *)sechdrs[modindex].sh_addr;
1712 /* Now we've moved module, initialize linked lists, etc. */
1713 module_unload_init(mod);
1715 /* Set up license info based on the info section */
1716 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1718 if (strcmp(mod->name, "ndiswrapper") == 0)
1719 add_taint(TAINT_PROPRIETARY_MODULE);
1720 if (strcmp(mod->name, "driverloader") == 0)
1721 add_taint(TAINT_PROPRIETARY_MODULE);
1723 #ifdef CONFIG_MODULE_UNLOAD
1724 /* Set up MODINFO_ATTR fields */
1725 setup_modinfo(mod, sechdrs, infoindex);
1726 #endif
1728 /* Fix up syms, so that st_value is a pointer to location. */
1729 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1730 mod);
1731 if (err < 0)
1732 goto cleanup;
1734 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1735 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1736 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1737 if (crcindex)
1738 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1739 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1740 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1741 if (gplcrcindex)
1742 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1744 #ifdef CONFIG_MODVERSIONS
1745 if ((mod->num_syms && !crcindex) ||
1746 (mod->num_gpl_syms && !gplcrcindex)) {
1747 printk(KERN_WARNING "%s: No versions for exported symbols."
1748 " Tainting kernel.\n", mod->name);
1749 add_taint(TAINT_FORCED_MODULE);
1751 #endif
1753 /* Now do relocations. */
1754 for (i = 1; i < hdr->e_shnum; i++) {
1755 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1756 unsigned int info = sechdrs[i].sh_info;
1758 /* Not a valid relocation section? */
1759 if (info >= hdr->e_shnum)
1760 continue;
1762 /* Don't bother with non-allocated sections */
1763 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1764 continue;
1766 if (sechdrs[i].sh_type == SHT_REL)
1767 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1768 else if (sechdrs[i].sh_type == SHT_RELA)
1769 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1770 mod);
1771 if (err < 0)
1772 goto cleanup;
1775 /* Set up and sort exception table */
1776 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1777 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1778 sort_extable(extable, extable + mod->num_exentries);
1780 /* Finally, copy percpu area over. */
1781 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1782 sechdrs[pcpuindex].sh_size);
1784 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1786 err = module_finalize(hdr, sechdrs, mod);
1787 if (err < 0)
1788 goto cleanup;
1790 /* flush the icache in correct context */
1791 old_fs = get_fs();
1792 set_fs(KERNEL_DS);
1795 * Flush the instruction cache, since we've played with text.
1796 * Do it before processing of module parameters, so the module
1797 * can provide parameter accessor functions of its own.
1799 if (mod->module_init)
1800 flush_icache_range((unsigned long)mod->module_init,
1801 (unsigned long)mod->module_init
1802 + mod->init_size);
1803 flush_icache_range((unsigned long)mod->module_core,
1804 (unsigned long)mod->module_core + mod->core_size);
1806 set_fs(old_fs);
1808 mod->args = args;
1809 if (obsparmindex) {
1810 err = obsolete_params(mod->name, mod->args,
1811 (struct obsolete_modparm *)
1812 sechdrs[obsparmindex].sh_addr,
1813 sechdrs[obsparmindex].sh_size
1814 / sizeof(struct obsolete_modparm),
1815 sechdrs, symindex,
1816 (char *)sechdrs[strindex].sh_addr);
1817 if (setupindex)
1818 printk(KERN_WARNING "%s: Ignoring new-style "
1819 "parameters in presence of obsolete ones\n",
1820 mod->name);
1821 } else {
1822 /* Size of section 0 is 0, so this works well if no params */
1823 err = parse_args(mod->name, mod->args,
1824 (struct kernel_param *)
1825 sechdrs[setupindex].sh_addr,
1826 sechdrs[setupindex].sh_size
1827 / sizeof(struct kernel_param),
1828 NULL);
1830 if (err < 0)
1831 goto arch_cleanup;
1833 err = mod_sysfs_setup(mod,
1834 (struct kernel_param *)
1835 sechdrs[setupindex].sh_addr,
1836 sechdrs[setupindex].sh_size
1837 / sizeof(struct kernel_param));
1838 if (err < 0)
1839 goto arch_cleanup;
1840 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1842 /* Get rid of temporary copy */
1843 vfree(hdr);
1845 /* Done! */
1846 return mod;
1848 arch_cleanup:
1849 module_arch_cleanup(mod);
1850 cleanup:
1851 module_unload_free(mod);
1852 module_free(mod, mod->module_init);
1853 free_core:
1854 module_free(mod, mod->module_core);
1855 free_percpu:
1856 if (percpu)
1857 percpu_modfree(percpu);
1858 free_mod:
1859 kfree(args);
1860 free_hdr:
1861 vfree(hdr);
1862 return ERR_PTR(err);
1864 truncated:
1865 printk(KERN_ERR "Module len %lu truncated\n", len);
1866 err = -ENOEXEC;
1867 goto free_hdr;
1871 * link the module with the whole machine is stopped with interrupts off
1872 * - this defends against kallsyms not taking locks
1874 static int __link_module(void *_mod)
1876 struct module *mod = _mod;
1877 list_add(&mod->list, &modules);
1878 return 0;
1881 /* This is where the real work happens */
1882 asmlinkage long
1883 sys_init_module(void __user *umod,
1884 unsigned long len,
1885 const char __user *uargs)
1887 struct module *mod;
1888 int ret = 0;
1890 /* Must have permission */
1891 if (!capable(CAP_SYS_MODULE))
1892 return -EPERM;
1894 /* Only one module load at a time, please */
1895 if (down_interruptible(&module_mutex) != 0)
1896 return -EINTR;
1898 /* Do all the hard work */
1899 mod = load_module(umod, len, uargs);
1900 if (IS_ERR(mod)) {
1901 up(&module_mutex);
1902 return PTR_ERR(mod);
1905 /* Now sew it into the lists. They won't access us, since
1906 strong_try_module_get() will fail. */
1907 stop_machine_run(__link_module, mod, NR_CPUS);
1909 /* Drop lock so they can recurse */
1910 up(&module_mutex);
1912 down(&notify_mutex);
1913 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1914 up(&notify_mutex);
1916 /* Start the module */
1917 if (mod->init != NULL)
1918 ret = mod->init();
1919 if (ret < 0) {
1920 /* Init routine failed: abort. Try to protect us from
1921 buggy refcounters. */
1922 mod->state = MODULE_STATE_GOING;
1923 synchronize_sched();
1924 if (mod->unsafe)
1925 printk(KERN_ERR "%s: module is now stuck!\n",
1926 mod->name);
1927 else {
1928 module_put(mod);
1929 down(&module_mutex);
1930 free_module(mod);
1931 up(&module_mutex);
1933 return ret;
1936 /* Now it's a first class citizen! */
1937 down(&module_mutex);
1938 mod->state = MODULE_STATE_LIVE;
1939 /* Drop initial reference. */
1940 module_put(mod);
1941 module_free(mod, mod->module_init);
1942 mod->module_init = NULL;
1943 mod->init_size = 0;
1944 mod->init_text_size = 0;
1945 up(&module_mutex);
1947 return 0;
1950 static inline int within(unsigned long addr, void *start, unsigned long size)
1952 return ((void *)addr >= start && (void *)addr < start + size);
1955 #ifdef CONFIG_KALLSYMS
1957 * This ignores the intensely annoying "mapping symbols" found
1958 * in ARM ELF files: $a, $t and $d.
1960 static inline int is_arm_mapping_symbol(const char *str)
1962 return str[0] == '$' && strchr("atd", str[1])
1963 && (str[2] == '\0' || str[2] == '.');
1966 static const char *get_ksymbol(struct module *mod,
1967 unsigned long addr,
1968 unsigned long *size,
1969 unsigned long *offset)
1971 unsigned int i, best = 0;
1972 unsigned long nextval;
1974 /* At worse, next value is at end of module */
1975 if (within(addr, mod->module_init, mod->init_size))
1976 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1977 else
1978 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1980 /* Scan for closest preceeding symbol, and next symbol. (ELF
1981 starts real symbols at 1). */
1982 for (i = 1; i < mod->num_symtab; i++) {
1983 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1984 continue;
1986 /* We ignore unnamed symbols: they're uninformative
1987 * and inserted at a whim. */
1988 if (mod->symtab[i].st_value <= addr
1989 && mod->symtab[i].st_value > mod->symtab[best].st_value
1990 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1991 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1992 best = i;
1993 if (mod->symtab[i].st_value > addr
1994 && mod->symtab[i].st_value < nextval
1995 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1996 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1997 nextval = mod->symtab[i].st_value;
2000 if (!best)
2001 return NULL;
2003 *size = nextval - mod->symtab[best].st_value;
2004 *offset = addr - mod->symtab[best].st_value;
2005 return mod->strtab + mod->symtab[best].st_name;
2008 /* For kallsyms to ask for address resolution. NULL means not found.
2009 We don't lock, as this is used for oops resolution and races are a
2010 lesser concern. */
2011 const char *module_address_lookup(unsigned long addr,
2012 unsigned long *size,
2013 unsigned long *offset,
2014 char **modname)
2016 struct module *mod;
2018 list_for_each_entry(mod, &modules, list) {
2019 if (within(addr, mod->module_init, mod->init_size)
2020 || within(addr, mod->module_core, mod->core_size)) {
2021 *modname = mod->name;
2022 return get_ksymbol(mod, addr, size, offset);
2025 return NULL;
2028 struct module *module_get_kallsym(unsigned int symnum,
2029 unsigned long *value,
2030 char *type,
2031 char namebuf[128])
2033 struct module *mod;
2035 down(&module_mutex);
2036 list_for_each_entry(mod, &modules, list) {
2037 if (symnum < mod->num_symtab) {
2038 *value = mod->symtab[symnum].st_value;
2039 *type = mod->symtab[symnum].st_info;
2040 strncpy(namebuf,
2041 mod->strtab + mod->symtab[symnum].st_name,
2042 127);
2043 up(&module_mutex);
2044 return mod;
2046 symnum -= mod->num_symtab;
2048 up(&module_mutex);
2049 return NULL;
2052 static unsigned long mod_find_symname(struct module *mod, const char *name)
2054 unsigned int i;
2056 for (i = 0; i < mod->num_symtab; i++)
2057 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2058 return mod->symtab[i].st_value;
2059 return 0;
2062 /* Look for this name: can be of form module:name. */
2063 unsigned long module_kallsyms_lookup_name(const char *name)
2065 struct module *mod;
2066 char *colon;
2067 unsigned long ret = 0;
2069 /* Don't lock: we're in enough trouble already. */
2070 if ((colon = strchr(name, ':')) != NULL) {
2071 *colon = '\0';
2072 if ((mod = find_module(name)) != NULL)
2073 ret = mod_find_symname(mod, colon+1);
2074 *colon = ':';
2075 } else {
2076 list_for_each_entry(mod, &modules, list)
2077 if ((ret = mod_find_symname(mod, name)) != 0)
2078 break;
2080 return ret;
2082 #endif /* CONFIG_KALLSYMS */
2084 /* Called by the /proc file system to return a list of modules. */
2085 static void *m_start(struct seq_file *m, loff_t *pos)
2087 struct list_head *i;
2088 loff_t n = 0;
2090 down(&module_mutex);
2091 list_for_each(i, &modules) {
2092 if (n++ == *pos)
2093 break;
2095 if (i == &modules)
2096 return NULL;
2097 return i;
2100 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2102 struct list_head *i = p;
2103 (*pos)++;
2104 if (i->next == &modules)
2105 return NULL;
2106 return i->next;
2109 static void m_stop(struct seq_file *m, void *p)
2111 up(&module_mutex);
2114 static int m_show(struct seq_file *m, void *p)
2116 struct module *mod = list_entry(p, struct module, list);
2117 seq_printf(m, "%s %lu",
2118 mod->name, mod->init_size + mod->core_size);
2119 print_unload_info(m, mod);
2121 /* Informative for users. */
2122 seq_printf(m, " %s",
2123 mod->state == MODULE_STATE_GOING ? "Unloading":
2124 mod->state == MODULE_STATE_COMING ? "Loading":
2125 "Live");
2126 /* Used by oprofile and other similar tools. */
2127 seq_printf(m, " 0x%p", mod->module_core);
2129 seq_printf(m, "\n");
2130 return 0;
2133 /* Format: modulename size refcount deps address
2135 Where refcount is a number or -, and deps is a comma-separated list
2136 of depends or -.
2138 struct seq_operations modules_op = {
2139 .start = m_start,
2140 .next = m_next,
2141 .stop = m_stop,
2142 .show = m_show
2145 /* Given an address, look for it in the module exception tables. */
2146 const struct exception_table_entry *search_module_extables(unsigned long addr)
2148 unsigned long flags;
2149 const struct exception_table_entry *e = NULL;
2150 struct module *mod;
2152 spin_lock_irqsave(&modlist_lock, flags);
2153 list_for_each_entry(mod, &modules, list) {
2154 if (mod->num_exentries == 0)
2155 continue;
2157 e = search_extable(mod->extable,
2158 mod->extable + mod->num_exentries - 1,
2159 addr);
2160 if (e)
2161 break;
2163 spin_unlock_irqrestore(&modlist_lock, flags);
2165 /* Now, if we found one, we are running inside it now, hence
2166 we cannot unload the module, hence no refcnt needed. */
2167 return e;
2170 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2171 struct module *__module_text_address(unsigned long addr)
2173 struct module *mod;
2175 list_for_each_entry(mod, &modules, list)
2176 if (within(addr, mod->module_init, mod->init_text_size)
2177 || within(addr, mod->module_core, mod->core_text_size))
2178 return mod;
2179 return NULL;
2182 struct module *module_text_address(unsigned long addr)
2184 struct module *mod;
2185 unsigned long flags;
2187 spin_lock_irqsave(&modlist_lock, flags);
2188 mod = __module_text_address(addr);
2189 spin_unlock_irqrestore(&modlist_lock, flags);
2191 return mod;
2194 /* Don't grab lock, we're oopsing. */
2195 void print_modules(void)
2197 struct module *mod;
2199 printk("Modules linked in:");
2200 list_for_each_entry(mod, &modules, list)
2201 printk(" %s", mod->name);
2202 printk("\n");
2205 void module_add_driver(struct module *mod, struct device_driver *drv)
2207 if (!mod || !drv)
2208 return;
2210 /* Don't check return code; this call is idempotent */
2211 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2213 EXPORT_SYMBOL(module_add_driver);
2215 void module_remove_driver(struct device_driver *drv)
2217 if (!drv)
2218 return;
2219 sysfs_remove_link(&drv->kobj, "module");
2221 EXPORT_SYMBOL(module_remove_driver);
2223 #ifdef CONFIG_MODVERSIONS
2224 /* Generate the signature for struct module here, too, for modversions. */
2225 void struct_module(struct module *mod) { return; }
2226 EXPORT_SYMBOL(struct_module);
2227 #endif