diff options
Diffstat (limited to 'kernel/module.c')
-rw-r--r-- | kernel/module.c | 710 |
1 files changed, 464 insertions, 246 deletions
diff --git a/kernel/module.c b/kernel/module.c index 6085f5ef88ea..0925c9a71975 100644 --- a/kernel/module.c +++ b/kernel/module.c | |||
@@ -21,6 +21,7 @@ | |||
21 | #include <linux/ftrace_event.h> | 21 | #include <linux/ftrace_event.h> |
22 | #include <linux/init.h> | 22 | #include <linux/init.h> |
23 | #include <linux/kallsyms.h> | 23 | #include <linux/kallsyms.h> |
24 | #include <linux/file.h> | ||
24 | #include <linux/fs.h> | 25 | #include <linux/fs.h> |
25 | #include <linux/sysfs.h> | 26 | #include <linux/sysfs.h> |
26 | #include <linux/kernel.h> | 27 | #include <linux/kernel.h> |
@@ -28,6 +29,7 @@ | |||
28 | #include <linux/vmalloc.h> | 29 | #include <linux/vmalloc.h> |
29 | #include <linux/elf.h> | 30 | #include <linux/elf.h> |
30 | #include <linux/proc_fs.h> | 31 | #include <linux/proc_fs.h> |
32 | #include <linux/security.h> | ||
31 | #include <linux/seq_file.h> | 33 | #include <linux/seq_file.h> |
32 | #include <linux/syscalls.h> | 34 | #include <linux/syscalls.h> |
33 | #include <linux/fcntl.h> | 35 | #include <linux/fcntl.h> |
@@ -59,6 +61,7 @@ | |||
59 | #include <linux/pfn.h> | 61 | #include <linux/pfn.h> |
60 | #include <linux/bsearch.h> | 62 | #include <linux/bsearch.h> |
61 | #include <linux/fips.h> | 63 | #include <linux/fips.h> |
64 | #include <uapi/linux/module.h> | ||
62 | #include "module-internal.h" | 65 | #include "module-internal.h" |
63 | 66 | ||
64 | #define CREATE_TRACE_POINTS | 67 | #define CREATE_TRACE_POINTS |
@@ -185,6 +188,7 @@ struct load_info { | |||
185 | ongoing or failed initialization etc. */ | 188 | ongoing or failed initialization etc. */ |
186 | static inline int strong_try_module_get(struct module *mod) | 189 | static inline int strong_try_module_get(struct module *mod) |
187 | { | 190 | { |
191 | BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED); | ||
188 | if (mod && mod->state == MODULE_STATE_COMING) | 192 | if (mod && mod->state == MODULE_STATE_COMING) |
189 | return -EBUSY; | 193 | return -EBUSY; |
190 | if (try_module_get(mod)) | 194 | if (try_module_get(mod)) |
@@ -193,9 +197,10 @@ static inline int strong_try_module_get(struct module *mod) | |||
193 | return -ENOENT; | 197 | return -ENOENT; |
194 | } | 198 | } |
195 | 199 | ||
196 | static inline void add_taint_module(struct module *mod, unsigned flag) | 200 | static inline void add_taint_module(struct module *mod, unsigned flag, |
201 | enum lockdep_ok lockdep_ok) | ||
197 | { | 202 | { |
198 | add_taint(flag); | 203 | add_taint(flag, lockdep_ok); |
199 | mod->taints |= (1U << flag); | 204 | mod->taints |= (1U << flag); |
200 | } | 205 | } |
201 | 206 | ||
@@ -340,6 +345,9 @@ bool each_symbol_section(bool (*fn)(const struct symsearch *arr, | |||
340 | #endif | 345 | #endif |
341 | }; | 346 | }; |
342 | 347 | ||
348 | if (mod->state == MODULE_STATE_UNFORMED) | ||
349 | continue; | ||
350 | |||
343 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) | 351 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) |
344 | return true; | 352 | return true; |
345 | } | 353 | } |
@@ -372,9 +380,6 @@ static bool check_symbol(const struct symsearch *syms, | |||
372 | printk(KERN_WARNING "Symbol %s is being used " | 380 | printk(KERN_WARNING "Symbol %s is being used " |
373 | "by a non-GPL module, which will not " | 381 | "by a non-GPL module, which will not " |
374 | "be allowed in the future\n", fsa->name); | 382 | "be allowed in the future\n", fsa->name); |
375 | printk(KERN_WARNING "Please see the file " | ||
376 | "Documentation/feature-removal-schedule.txt " | ||
377 | "in the kernel source tree for more details.\n"); | ||
378 | } | 383 | } |
379 | } | 384 | } |
380 | 385 | ||
@@ -450,16 +455,24 @@ const struct kernel_symbol *find_symbol(const char *name, | |||
450 | EXPORT_SYMBOL_GPL(find_symbol); | 455 | EXPORT_SYMBOL_GPL(find_symbol); |
451 | 456 | ||
452 | /* Search for module by name: must hold module_mutex. */ | 457 | /* Search for module by name: must hold module_mutex. */ |
453 | struct module *find_module(const char *name) | 458 | static struct module *find_module_all(const char *name, |
459 | bool even_unformed) | ||
454 | { | 460 | { |
455 | struct module *mod; | 461 | struct module *mod; |
456 | 462 | ||
457 | list_for_each_entry(mod, &modules, list) { | 463 | list_for_each_entry(mod, &modules, list) { |
464 | if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) | ||
465 | continue; | ||
458 | if (strcmp(mod->name, name) == 0) | 466 | if (strcmp(mod->name, name) == 0) |
459 | return mod; | 467 | return mod; |
460 | } | 468 | } |
461 | return NULL; | 469 | return NULL; |
462 | } | 470 | } |
471 | |||
472 | struct module *find_module(const char *name) | ||
473 | { | ||
474 | return find_module_all(name, false); | ||
475 | } | ||
463 | EXPORT_SYMBOL_GPL(find_module); | 476 | EXPORT_SYMBOL_GPL(find_module); |
464 | 477 | ||
465 | #ifdef CONFIG_SMP | 478 | #ifdef CONFIG_SMP |
@@ -525,6 +538,8 @@ bool is_module_percpu_address(unsigned long addr) | |||
525 | preempt_disable(); | 538 | preempt_disable(); |
526 | 539 | ||
527 | list_for_each_entry_rcu(mod, &modules, list) { | 540 | list_for_each_entry_rcu(mod, &modules, list) { |
541 | if (mod->state == MODULE_STATE_UNFORMED) | ||
542 | continue; | ||
528 | if (!mod->percpu_size) | 543 | if (!mod->percpu_size) |
529 | continue; | 544 | continue; |
530 | for_each_possible_cpu(cpu) { | 545 | for_each_possible_cpu(cpu) { |
@@ -713,7 +728,7 @@ static inline int try_force_unload(unsigned int flags) | |||
713 | { | 728 | { |
714 | int ret = (flags & O_TRUNC); | 729 | int ret = (flags & O_TRUNC); |
715 | if (ret) | 730 | if (ret) |
716 | add_taint(TAINT_FORCED_RMMOD); | 731 | add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE); |
717 | return ret; | 732 | return ret; |
718 | } | 733 | } |
719 | #else | 734 | #else |
@@ -1048,6 +1063,8 @@ static ssize_t show_initstate(struct module_attribute *mattr, | |||
1048 | case MODULE_STATE_GOING: | 1063 | case MODULE_STATE_GOING: |
1049 | state = "going"; | 1064 | state = "going"; |
1050 | break; | 1065 | break; |
1066 | default: | ||
1067 | BUG(); | ||
1051 | } | 1068 | } |
1052 | return sprintf(buffer, "%s\n", state); | 1069 | return sprintf(buffer, "%s\n", state); |
1053 | } | 1070 | } |
@@ -1122,7 +1139,7 @@ static int try_to_force_load(struct module *mod, const char *reason) | |||
1122 | if (!test_taint(TAINT_FORCED_MODULE)) | 1139 | if (!test_taint(TAINT_FORCED_MODULE)) |
1123 | printk(KERN_WARNING "%s: %s: kernel tainted.\n", | 1140 | printk(KERN_WARNING "%s: %s: kernel tainted.\n", |
1124 | mod->name, reason); | 1141 | mod->name, reason); |
1125 | add_taint_module(mod, TAINT_FORCED_MODULE); | 1142 | add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE); |
1126 | return 0; | 1143 | return 0; |
1127 | #else | 1144 | #else |
1128 | return -ENOEXEC; | 1145 | return -ENOEXEC; |
@@ -1786,6 +1803,8 @@ void set_all_modules_text_rw(void) | |||
1786 | 1803 | ||
1787 | mutex_lock(&module_mutex); | 1804 | mutex_lock(&module_mutex); |
1788 | list_for_each_entry_rcu(mod, &modules, list) { | 1805 | list_for_each_entry_rcu(mod, &modules, list) { |
1806 | if (mod->state == MODULE_STATE_UNFORMED) | ||
1807 | continue; | ||
1789 | if ((mod->module_core) && (mod->core_text_size)) { | 1808 | if ((mod->module_core) && (mod->core_text_size)) { |
1790 | set_page_attributes(mod->module_core, | 1809 | set_page_attributes(mod->module_core, |
1791 | mod->module_core + mod->core_text_size, | 1810 | mod->module_core + mod->core_text_size, |
@@ -1807,6 +1826,8 @@ void set_all_modules_text_ro(void) | |||
1807 | 1826 | ||
1808 | mutex_lock(&module_mutex); | 1827 | mutex_lock(&module_mutex); |
1809 | list_for_each_entry_rcu(mod, &modules, list) { | 1828 | list_for_each_entry_rcu(mod, &modules, list) { |
1829 | if (mod->state == MODULE_STATE_UNFORMED) | ||
1830 | continue; | ||
1810 | if ((mod->module_core) && (mod->core_text_size)) { | 1831 | if ((mod->module_core) && (mod->core_text_size)) { |
1811 | set_page_attributes(mod->module_core, | 1832 | set_page_attributes(mod->module_core, |
1812 | mod->module_core + mod->core_text_size, | 1833 | mod->module_core + mod->core_text_size, |
@@ -2127,7 +2148,8 @@ static void set_license(struct module *mod, const char *license) | |||
2127 | if (!test_taint(TAINT_PROPRIETARY_MODULE)) | 2148 | if (!test_taint(TAINT_PROPRIETARY_MODULE)) |
2128 | printk(KERN_WARNING "%s: module license '%s' taints " | 2149 | printk(KERN_WARNING "%s: module license '%s' taints " |
2129 | "kernel.\n", mod->name, license); | 2150 | "kernel.\n", mod->name, license); |
2130 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | 2151 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE, |
2152 | LOCKDEP_NOW_UNRELIABLE); | ||
2131 | } | 2153 | } |
2132 | } | 2154 | } |
2133 | 2155 | ||
@@ -2282,7 +2304,7 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2282 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; | 2304 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; |
2283 | Elf_Shdr *strsect = info->sechdrs + info->index.str; | 2305 | Elf_Shdr *strsect = info->sechdrs + info->index.str; |
2284 | const Elf_Sym *src; | 2306 | const Elf_Sym *src; |
2285 | unsigned int i, nsrc, ndst, strtab_size; | 2307 | unsigned int i, nsrc, ndst, strtab_size = 0; |
2286 | 2308 | ||
2287 | /* Put symbol section at end of init part of module. */ | 2309 | /* Put symbol section at end of init part of module. */ |
2288 | symsect->sh_flags |= SHF_ALLOC; | 2310 | symsect->sh_flags |= SHF_ALLOC; |
@@ -2294,11 +2316,13 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2294 | nsrc = symsect->sh_size / sizeof(*src); | 2316 | nsrc = symsect->sh_size / sizeof(*src); |
2295 | 2317 | ||
2296 | /* Compute total space required for the core symbols' strtab. */ | 2318 | /* Compute total space required for the core symbols' strtab. */ |
2297 | for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src) | 2319 | for (ndst = i = 0; i < nsrc; i++) { |
2298 | if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) { | 2320 | if (i == 0 || |
2299 | strtab_size += strlen(&info->strtab[src->st_name]) + 1; | 2321 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { |
2322 | strtab_size += strlen(&info->strtab[src[i].st_name])+1; | ||
2300 | ndst++; | 2323 | ndst++; |
2301 | } | 2324 | } |
2325 | } | ||
2302 | 2326 | ||
2303 | /* Append room for core symbols at end of core part. */ | 2327 | /* Append room for core symbols at end of core part. */ |
2304 | info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); | 2328 | info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); |
@@ -2332,15 +2356,14 @@ static void add_kallsyms(struct module *mod, const struct load_info *info) | |||
2332 | mod->core_symtab = dst = mod->module_core + info->symoffs; | 2356 | mod->core_symtab = dst = mod->module_core + info->symoffs; |
2333 | mod->core_strtab = s = mod->module_core + info->stroffs; | 2357 | mod->core_strtab = s = mod->module_core + info->stroffs; |
2334 | src = mod->symtab; | 2358 | src = mod->symtab; |
2335 | *dst = *src; | 2359 | for (ndst = i = 0; i < mod->num_symtab; i++) { |
2336 | *s++ = 0; | 2360 | if (i == 0 || |
2337 | for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) { | 2361 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { |
2338 | if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) | 2362 | dst[ndst] = src[i]; |
2339 | continue; | 2363 | dst[ndst++].st_name = s - mod->core_strtab; |
2340 | 2364 | s += strlcpy(s, &mod->strtab[src[i].st_name], | |
2341 | dst[ndst] = *src; | 2365 | KSYM_NAME_LEN) + 1; |
2342 | dst[ndst++].st_name = s - mod->core_strtab; | 2366 | } |
2343 | s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1; | ||
2344 | } | 2367 | } |
2345 | mod->core_num_syms = ndst; | 2368 | mod->core_num_syms = ndst; |
2346 | } | 2369 | } |
@@ -2373,7 +2396,7 @@ static void dynamic_debug_remove(struct _ddebug *debug) | |||
2373 | 2396 | ||
2374 | void * __weak module_alloc(unsigned long size) | 2397 | void * __weak module_alloc(unsigned long size) |
2375 | { | 2398 | { |
2376 | return size == 0 ? NULL : vmalloc_exec(size); | 2399 | return vmalloc_exec(size); |
2377 | } | 2400 | } |
2378 | 2401 | ||
2379 | static void *module_alloc_update_bounds(unsigned long size) | 2402 | static void *module_alloc_update_bounds(unsigned long size) |
@@ -2420,18 +2443,17 @@ static inline void kmemleak_load_module(const struct module *mod, | |||
2420 | #endif | 2443 | #endif |
2421 | 2444 | ||
2422 | #ifdef CONFIG_MODULE_SIG | 2445 | #ifdef CONFIG_MODULE_SIG |
2423 | static int module_sig_check(struct load_info *info, | 2446 | static int module_sig_check(struct load_info *info) |
2424 | const void *mod, unsigned long *_len) | ||
2425 | { | 2447 | { |
2426 | int err = -ENOKEY; | 2448 | int err = -ENOKEY; |
2427 | unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; | 2449 | const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; |
2428 | unsigned long len = *_len; | 2450 | const void *mod = info->hdr; |
2429 | 2451 | ||
2430 | if (len > markerlen && | 2452 | if (info->len > markerlen && |
2431 | memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { | 2453 | memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { |
2432 | /* We truncate the module to discard the signature */ | 2454 | /* We truncate the module to discard the signature */ |
2433 | *_len -= markerlen; | 2455 | info->len -= markerlen; |
2434 | err = mod_verify_sig(mod, _len); | 2456 | err = mod_verify_sig(mod, &info->len); |
2435 | } | 2457 | } |
2436 | 2458 | ||
2437 | if (!err) { | 2459 | if (!err) { |
@@ -2449,59 +2471,114 @@ static int module_sig_check(struct load_info *info, | |||
2449 | return err; | 2471 | return err; |
2450 | } | 2472 | } |
2451 | #else /* !CONFIG_MODULE_SIG */ | 2473 | #else /* !CONFIG_MODULE_SIG */ |
2452 | static int module_sig_check(struct load_info *info, | 2474 | static int module_sig_check(struct load_info *info) |
2453 | void *mod, unsigned long *len) | ||
2454 | { | 2475 | { |
2455 | return 0; | 2476 | return 0; |
2456 | } | 2477 | } |
2457 | #endif /* !CONFIG_MODULE_SIG */ | 2478 | #endif /* !CONFIG_MODULE_SIG */ |
2458 | 2479 | ||
2459 | /* Sets info->hdr, info->len and info->sig_ok. */ | 2480 | /* Sanity checks against invalid binaries, wrong arch, weird elf version. */ |
2460 | static int copy_and_check(struct load_info *info, | 2481 | static int elf_header_check(struct load_info *info) |
2461 | const void __user *umod, unsigned long len, | 2482 | { |
2462 | const char __user *uargs) | 2483 | if (info->len < sizeof(*(info->hdr))) |
2484 | return -ENOEXEC; | ||
2485 | |||
2486 | if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0 | ||
2487 | || info->hdr->e_type != ET_REL | ||
2488 | || !elf_check_arch(info->hdr) | ||
2489 | || info->hdr->e_shentsize != sizeof(Elf_Shdr)) | ||
2490 | return -ENOEXEC; | ||
2491 | |||
2492 | if (info->hdr->e_shoff >= info->len | ||
2493 | || (info->hdr->e_shnum * sizeof(Elf_Shdr) > | ||
2494 | info->len - info->hdr->e_shoff)) | ||
2495 | return -ENOEXEC; | ||
2496 | |||
2497 | return 0; | ||
2498 | } | ||
2499 | |||
2500 | /* Sets info->hdr and info->len. */ | ||
2501 | static int copy_module_from_user(const void __user *umod, unsigned long len, | ||
2502 | struct load_info *info) | ||
2463 | { | 2503 | { |
2464 | int err; | 2504 | int err; |
2465 | Elf_Ehdr *hdr; | ||
2466 | 2505 | ||
2467 | if (len < sizeof(*hdr)) | 2506 | info->len = len; |
2507 | if (info->len < sizeof(*(info->hdr))) | ||
2468 | return -ENOEXEC; | 2508 | return -ENOEXEC; |
2469 | 2509 | ||
2510 | err = security_kernel_module_from_file(NULL); | ||
2511 | if (err) | ||
2512 | return err; | ||
2513 | |||
2470 | /* Suck in entire file: we'll want most of it. */ | 2514 | /* Suck in entire file: we'll want most of it. */ |
2471 | if ((hdr = vmalloc(len)) == NULL) | 2515 | info->hdr = vmalloc(info->len); |
2516 | if (!info->hdr) | ||
2472 | return -ENOMEM; | 2517 | return -ENOMEM; |
2473 | 2518 | ||
2474 | if (copy_from_user(hdr, umod, len) != 0) { | 2519 | if (copy_from_user(info->hdr, umod, info->len) != 0) { |
2475 | err = -EFAULT; | 2520 | vfree(info->hdr); |
2476 | goto free_hdr; | 2521 | return -EFAULT; |
2477 | } | 2522 | } |
2478 | 2523 | ||
2479 | err = module_sig_check(info, hdr, &len); | 2524 | return 0; |
2525 | } | ||
2526 | |||
2527 | /* Sets info->hdr and info->len. */ | ||
2528 | static int copy_module_from_fd(int fd, struct load_info *info) | ||
2529 | { | ||
2530 | struct file *file; | ||
2531 | int err; | ||
2532 | struct kstat stat; | ||
2533 | loff_t pos; | ||
2534 | ssize_t bytes = 0; | ||
2535 | |||
2536 | file = fget(fd); | ||
2537 | if (!file) | ||
2538 | return -ENOEXEC; | ||
2539 | |||
2540 | err = security_kernel_module_from_file(file); | ||
2480 | if (err) | 2541 | if (err) |
2481 | goto free_hdr; | 2542 | goto out; |
2482 | 2543 | ||
2483 | /* Sanity checks against insmoding binaries or wrong arch, | 2544 | err = vfs_getattr(&file->f_path, &stat); |
2484 | weird elf version */ | 2545 | if (err) |
2485 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 | 2546 | goto out; |
2486 | || hdr->e_type != ET_REL | 2547 | |
2487 | || !elf_check_arch(hdr) | 2548 | if (stat.size > INT_MAX) { |
2488 | || hdr->e_shentsize != sizeof(Elf_Shdr)) { | 2549 | err = -EFBIG; |
2489 | err = -ENOEXEC; | 2550 | goto out; |
2490 | goto free_hdr; | ||
2491 | } | 2551 | } |
2492 | 2552 | ||
2493 | if (hdr->e_shoff >= len || | 2553 | /* Don't hand 0 to vmalloc, it whines. */ |
2494 | hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) { | 2554 | if (stat.size == 0) { |
2495 | err = -ENOEXEC; | 2555 | err = -EINVAL; |
2496 | goto free_hdr; | 2556 | goto out; |
2497 | } | 2557 | } |
2498 | 2558 | ||
2499 | info->hdr = hdr; | 2559 | info->hdr = vmalloc(stat.size); |
2500 | info->len = len; | 2560 | if (!info->hdr) { |
2501 | return 0; | 2561 | err = -ENOMEM; |
2562 | goto out; | ||
2563 | } | ||
2564 | |||
2565 | pos = 0; | ||
2566 | while (pos < stat.size) { | ||
2567 | bytes = kernel_read(file, pos, (char *)(info->hdr) + pos, | ||
2568 | stat.size - pos); | ||
2569 | if (bytes < 0) { | ||
2570 | vfree(info->hdr); | ||
2571 | err = bytes; | ||
2572 | goto out; | ||
2573 | } | ||
2574 | if (bytes == 0) | ||
2575 | break; | ||
2576 | pos += bytes; | ||
2577 | } | ||
2578 | info->len = pos; | ||
2502 | 2579 | ||
2503 | free_hdr: | 2580 | out: |
2504 | vfree(hdr); | 2581 | fput(file); |
2505 | return err; | 2582 | return err; |
2506 | } | 2583 | } |
2507 | 2584 | ||
@@ -2510,7 +2587,7 @@ static void free_copy(struct load_info *info) | |||
2510 | vfree(info->hdr); | 2587 | vfree(info->hdr); |
2511 | } | 2588 | } |
2512 | 2589 | ||
2513 | static int rewrite_section_headers(struct load_info *info) | 2590 | static int rewrite_section_headers(struct load_info *info, int flags) |
2514 | { | 2591 | { |
2515 | unsigned int i; | 2592 | unsigned int i; |
2516 | 2593 | ||
@@ -2538,7 +2615,10 @@ static int rewrite_section_headers(struct load_info *info) | |||
2538 | } | 2615 | } |
2539 | 2616 | ||
2540 | /* Track but don't keep modinfo and version sections. */ | 2617 | /* Track but don't keep modinfo and version sections. */ |
2541 | info->index.vers = find_sec(info, "__versions"); | 2618 | if (flags & MODULE_INIT_IGNORE_MODVERSIONS) |
2619 | info->index.vers = 0; /* Pretend no __versions section! */ | ||
2620 | else | ||
2621 | info->index.vers = find_sec(info, "__versions"); | ||
2542 | info->index.info = find_sec(info, ".modinfo"); | 2622 | info->index.info = find_sec(info, ".modinfo"); |
2543 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2623 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; |
2544 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2624 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; |
@@ -2553,7 +2633,7 @@ static int rewrite_section_headers(struct load_info *info) | |||
2553 | * Return the temporary module pointer (we'll replace it with the final | 2633 | * Return the temporary module pointer (we'll replace it with the final |
2554 | * one when we move the module sections around). | 2634 | * one when we move the module sections around). |
2555 | */ | 2635 | */ |
2556 | static struct module *setup_load_info(struct load_info *info) | 2636 | static struct module *setup_load_info(struct load_info *info, int flags) |
2557 | { | 2637 | { |
2558 | unsigned int i; | 2638 | unsigned int i; |
2559 | int err; | 2639 | int err; |
@@ -2564,7 +2644,7 @@ static struct module *setup_load_info(struct load_info *info) | |||
2564 | info->secstrings = (void *)info->hdr | 2644 | info->secstrings = (void *)info->hdr |
2565 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; | 2645 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; |
2566 | 2646 | ||
2567 | err = rewrite_section_headers(info); | 2647 | err = rewrite_section_headers(info, flags); |
2568 | if (err) | 2648 | if (err) |
2569 | return ERR_PTR(err); | 2649 | return ERR_PTR(err); |
2570 | 2650 | ||
@@ -2602,11 +2682,14 @@ static struct module *setup_load_info(struct load_info *info) | |||
2602 | return mod; | 2682 | return mod; |
2603 | } | 2683 | } |
2604 | 2684 | ||
2605 | static int check_modinfo(struct module *mod, struct load_info *info) | 2685 | static int check_modinfo(struct module *mod, struct load_info *info, int flags) |
2606 | { | 2686 | { |
2607 | const char *modmagic = get_modinfo(info, "vermagic"); | 2687 | const char *modmagic = get_modinfo(info, "vermagic"); |
2608 | int err; | 2688 | int err; |
2609 | 2689 | ||
2690 | if (flags & MODULE_INIT_IGNORE_VERMAGIC) | ||
2691 | modmagic = NULL; | ||
2692 | |||
2610 | /* This is allowed: modprobe --force will invalidate it. */ | 2693 | /* This is allowed: modprobe --force will invalidate it. */ |
2611 | if (!modmagic) { | 2694 | if (!modmagic) { |
2612 | err = try_to_force_load(mod, "bad vermagic"); | 2695 | err = try_to_force_load(mod, "bad vermagic"); |
@@ -2619,10 +2702,10 @@ static int check_modinfo(struct module *mod, struct load_info *info) | |||
2619 | } | 2702 | } |
2620 | 2703 | ||
2621 | if (!get_modinfo(info, "intree")) | 2704 | if (!get_modinfo(info, "intree")) |
2622 | add_taint_module(mod, TAINT_OOT_MODULE); | 2705 | add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK); |
2623 | 2706 | ||
2624 | if (get_modinfo(info, "staging")) { | 2707 | if (get_modinfo(info, "staging")) { |
2625 | add_taint_module(mod, TAINT_CRAP); | 2708 | add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK); |
2626 | printk(KERN_WARNING "%s: module is from the staging directory," | 2709 | printk(KERN_WARNING "%s: module is from the staging directory," |
2627 | " the quality is unknown, you have been warned.\n", | 2710 | " the quality is unknown, you have been warned.\n", |
2628 | mod->name); | 2711 | mod->name); |
@@ -2736,20 +2819,23 @@ static int move_module(struct module *mod, struct load_info *info) | |||
2736 | memset(ptr, 0, mod->core_size); | 2819 | memset(ptr, 0, mod->core_size); |
2737 | mod->module_core = ptr; | 2820 | mod->module_core = ptr; |
2738 | 2821 | ||
2739 | ptr = module_alloc_update_bounds(mod->init_size); | 2822 | if (mod->init_size) { |
2740 | /* | 2823 | ptr = module_alloc_update_bounds(mod->init_size); |
2741 | * The pointer to this block is stored in the module structure | 2824 | /* |
2742 | * which is inside the block. This block doesn't need to be | 2825 | * The pointer to this block is stored in the module structure |
2743 | * scanned as it contains data and code that will be freed | 2826 | * which is inside the block. This block doesn't need to be |
2744 | * after the module is initialized. | 2827 | * scanned as it contains data and code that will be freed |
2745 | */ | 2828 | * after the module is initialized. |
2746 | kmemleak_ignore(ptr); | 2829 | */ |
2747 | if (!ptr && mod->init_size) { | 2830 | kmemleak_ignore(ptr); |
2748 | module_free(mod, mod->module_core); | 2831 | if (!ptr) { |
2749 | return -ENOMEM; | 2832 | module_free(mod, mod->module_core); |
2750 | } | 2833 | return -ENOMEM; |
2751 | memset(ptr, 0, mod->init_size); | 2834 | } |
2752 | mod->module_init = ptr; | 2835 | memset(ptr, 0, mod->init_size); |
2836 | mod->module_init = ptr; | ||
2837 | } else | ||
2838 | mod->module_init = NULL; | ||
2753 | 2839 | ||
2754 | /* Transfer each section which specifies SHF_ALLOC */ | 2840 | /* Transfer each section which specifies SHF_ALLOC */ |
2755 | pr_debug("final section addresses:\n"); | 2841 | pr_debug("final section addresses:\n"); |
@@ -2785,15 +2871,17 @@ static int check_module_license_and_versions(struct module *mod) | |||
2785 | * using GPL-only symbols it needs. | 2871 | * using GPL-only symbols it needs. |
2786 | */ | 2872 | */ |
2787 | if (strcmp(mod->name, "ndiswrapper") == 0) | 2873 | if (strcmp(mod->name, "ndiswrapper") == 0) |
2788 | add_taint(TAINT_PROPRIETARY_MODULE); | 2874 | add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE); |
2789 | 2875 | ||
2790 | /* driverloader was caught wrongly pretending to be under GPL */ | 2876 | /* driverloader was caught wrongly pretending to be under GPL */ |
2791 | if (strcmp(mod->name, "driverloader") == 0) | 2877 | if (strcmp(mod->name, "driverloader") == 0) |
2792 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | 2878 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE, |
2879 | LOCKDEP_NOW_UNRELIABLE); | ||
2793 | 2880 | ||
2794 | /* lve claims to be GPL but upstream won't provide source */ | 2881 | /* lve claims to be GPL but upstream won't provide source */ |
2795 | if (strcmp(mod->name, "lve") == 0) | 2882 | if (strcmp(mod->name, "lve") == 0) |
2796 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | 2883 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE, |
2884 | LOCKDEP_NOW_UNRELIABLE); | ||
2797 | 2885 | ||
2798 | #ifdef CONFIG_MODVERSIONS | 2886 | #ifdef CONFIG_MODVERSIONS |
2799 | if ((mod->num_syms && !mod->crcs) | 2887 | if ((mod->num_syms && !mod->crcs) |
@@ -2842,18 +2930,18 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr, | |||
2842 | return 0; | 2930 | return 0; |
2843 | } | 2931 | } |
2844 | 2932 | ||
2845 | static struct module *layout_and_allocate(struct load_info *info) | 2933 | static struct module *layout_and_allocate(struct load_info *info, int flags) |
2846 | { | 2934 | { |
2847 | /* Module within temporary copy. */ | 2935 | /* Module within temporary copy. */ |
2848 | struct module *mod; | 2936 | struct module *mod; |
2849 | Elf_Shdr *pcpusec; | 2937 | Elf_Shdr *pcpusec; |
2850 | int err; | 2938 | int err; |
2851 | 2939 | ||
2852 | mod = setup_load_info(info); | 2940 | mod = setup_load_info(info, flags); |
2853 | if (IS_ERR(mod)) | 2941 | if (IS_ERR(mod)) |
2854 | return mod; | 2942 | return mod; |
2855 | 2943 | ||
2856 | err = check_modinfo(mod, info); | 2944 | err = check_modinfo(mod, info, flags); |
2857 | if (err) | 2945 | if (err) |
2858 | return ERR_PTR(err); | 2946 | return ERR_PTR(err); |
2859 | 2947 | ||
@@ -2933,70 +3021,255 @@ static bool finished_loading(const char *name) | |||
2933 | bool ret; | 3021 | bool ret; |
2934 | 3022 | ||
2935 | mutex_lock(&module_mutex); | 3023 | mutex_lock(&module_mutex); |
2936 | mod = find_module(name); | 3024 | mod = find_module_all(name, true); |
2937 | ret = !mod || mod->state != MODULE_STATE_COMING; | 3025 | ret = !mod || mod->state == MODULE_STATE_LIVE |
3026 | || mod->state == MODULE_STATE_GOING; | ||
2938 | mutex_unlock(&module_mutex); | 3027 | mutex_unlock(&module_mutex); |
2939 | 3028 | ||
2940 | return ret; | 3029 | return ret; |
2941 | } | 3030 | } |
2942 | 3031 | ||
3032 | /* Call module constructors. */ | ||
3033 | static void do_mod_ctors(struct module *mod) | ||
3034 | { | ||
3035 | #ifdef CONFIG_CONSTRUCTORS | ||
3036 | unsigned long i; | ||
3037 | |||
3038 | for (i = 0; i < mod->num_ctors; i++) | ||
3039 | mod->ctors[i](); | ||
3040 | #endif | ||
3041 | } | ||
3042 | |||
3043 | /* This is where the real work happens */ | ||
3044 | static int do_init_module(struct module *mod) | ||
3045 | { | ||
3046 | int ret = 0; | ||
3047 | |||
3048 | /* | ||
3049 | * We want to find out whether @mod uses async during init. Clear | ||
3050 | * PF_USED_ASYNC. async_schedule*() will set it. | ||
3051 | */ | ||
3052 | current->flags &= ~PF_USED_ASYNC; | ||
3053 | |||
3054 | blocking_notifier_call_chain(&module_notify_list, | ||
3055 | MODULE_STATE_COMING, mod); | ||
3056 | |||
3057 | /* Set RO and NX regions for core */ | ||
3058 | set_section_ro_nx(mod->module_core, | ||
3059 | mod->core_text_size, | ||
3060 | mod->core_ro_size, | ||
3061 | mod->core_size); | ||
3062 | |||
3063 | /* Set RO and NX regions for init */ | ||
3064 | set_section_ro_nx(mod->module_init, | ||
3065 | mod->init_text_size, | ||
3066 | mod->init_ro_size, | ||
3067 | mod->init_size); | ||
3068 | |||
3069 | do_mod_ctors(mod); | ||
3070 | /* Start the module */ | ||
3071 | if (mod->init != NULL) | ||
3072 | ret = do_one_initcall(mod->init); | ||
3073 | if (ret < 0) { | ||
3074 | /* Init routine failed: abort. Try to protect us from | ||
3075 | buggy refcounters. */ | ||
3076 | mod->state = MODULE_STATE_GOING; | ||
3077 | synchronize_sched(); | ||
3078 | module_put(mod); | ||
3079 | blocking_notifier_call_chain(&module_notify_list, | ||
3080 | MODULE_STATE_GOING, mod); | ||
3081 | free_module(mod); | ||
3082 | wake_up_all(&module_wq); | ||
3083 | return ret; | ||
3084 | } | ||
3085 | if (ret > 0) { | ||
3086 | printk(KERN_WARNING | ||
3087 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | ||
3088 | "%s: loading module anyway...\n", | ||
3089 | __func__, mod->name, ret, | ||
3090 | __func__); | ||
3091 | dump_stack(); | ||
3092 | } | ||
3093 | |||
3094 | /* Now it's a first class citizen! */ | ||
3095 | mod->state = MODULE_STATE_LIVE; | ||
3096 | blocking_notifier_call_chain(&module_notify_list, | ||
3097 | MODULE_STATE_LIVE, mod); | ||
3098 | |||
3099 | /* | ||
3100 | * We need to finish all async code before the module init sequence | ||
3101 | * is done. This has potential to deadlock. For example, a newly | ||
3102 | * detected block device can trigger request_module() of the | ||
3103 | * default iosched from async probing task. Once userland helper | ||
3104 | * reaches here, async_synchronize_full() will wait on the async | ||
3105 | * task waiting on request_module() and deadlock. | ||
3106 | * | ||
3107 | * This deadlock is avoided by perfomring async_synchronize_full() | ||
3108 | * iff module init queued any async jobs. This isn't a full | ||
3109 | * solution as it will deadlock the same if module loading from | ||
3110 | * async jobs nests more than once; however, due to the various | ||
3111 | * constraints, this hack seems to be the best option for now. | ||
3112 | * Please refer to the following thread for details. | ||
3113 | * | ||
3114 | * http://thread.gmane.org/gmane.linux.kernel/1420814 | ||
3115 | */ | ||
3116 | if (current->flags & PF_USED_ASYNC) | ||
3117 | async_synchronize_full(); | ||
3118 | |||
3119 | mutex_lock(&module_mutex); | ||
3120 | /* Drop initial reference. */ | ||
3121 | module_put(mod); | ||
3122 | trim_init_extable(mod); | ||
3123 | #ifdef CONFIG_KALLSYMS | ||
3124 | mod->num_symtab = mod->core_num_syms; | ||
3125 | mod->symtab = mod->core_symtab; | ||
3126 | mod->strtab = mod->core_strtab; | ||
3127 | #endif | ||
3128 | unset_module_init_ro_nx(mod); | ||
3129 | module_free(mod, mod->module_init); | ||
3130 | mod->module_init = NULL; | ||
3131 | mod->init_size = 0; | ||
3132 | mod->init_ro_size = 0; | ||
3133 | mod->init_text_size = 0; | ||
3134 | mutex_unlock(&module_mutex); | ||
3135 | wake_up_all(&module_wq); | ||
3136 | |||
3137 | return 0; | ||
3138 | } | ||
3139 | |||
3140 | static int may_init_module(void) | ||
3141 | { | ||
3142 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | ||
3143 | return -EPERM; | ||
3144 | |||
3145 | return 0; | ||
3146 | } | ||
3147 | |||
3148 | /* | ||
3149 | * We try to place it in the list now to make sure it's unique before | ||
3150 | * we dedicate too many resources. In particular, temporary percpu | ||
3151 | * memory exhaustion. | ||
3152 | */ | ||
3153 | static int add_unformed_module(struct module *mod) | ||
3154 | { | ||
3155 | int err; | ||
3156 | struct module *old; | ||
3157 | |||
3158 | mod->state = MODULE_STATE_UNFORMED; | ||
3159 | |||
3160 | again: | ||
3161 | mutex_lock(&module_mutex); | ||
3162 | if ((old = find_module_all(mod->name, true)) != NULL) { | ||
3163 | if (old->state == MODULE_STATE_COMING | ||
3164 | || old->state == MODULE_STATE_UNFORMED) { | ||
3165 | /* Wait in case it fails to load. */ | ||
3166 | mutex_unlock(&module_mutex); | ||
3167 | err = wait_event_interruptible(module_wq, | ||
3168 | finished_loading(mod->name)); | ||
3169 | if (err) | ||
3170 | goto out_unlocked; | ||
3171 | goto again; | ||
3172 | } | ||
3173 | err = -EEXIST; | ||
3174 | goto out; | ||
3175 | } | ||
3176 | list_add_rcu(&mod->list, &modules); | ||
3177 | err = 0; | ||
3178 | |||
3179 | out: | ||
3180 | mutex_unlock(&module_mutex); | ||
3181 | out_unlocked: | ||
3182 | return err; | ||
3183 | } | ||
3184 | |||
3185 | static int complete_formation(struct module *mod, struct load_info *info) | ||
3186 | { | ||
3187 | int err; | ||
3188 | |||
3189 | mutex_lock(&module_mutex); | ||
3190 | |||
3191 | /* Find duplicate symbols (must be called under lock). */ | ||
3192 | err = verify_export_symbols(mod); | ||
3193 | if (err < 0) | ||
3194 | goto out; | ||
3195 | |||
3196 | /* This relies on module_mutex for list integrity. */ | ||
3197 | module_bug_finalize(info->hdr, info->sechdrs, mod); | ||
3198 | |||
3199 | /* Mark state as coming so strong_try_module_get() ignores us, | ||
3200 | * but kallsyms etc. can see us. */ | ||
3201 | mod->state = MODULE_STATE_COMING; | ||
3202 | |||
3203 | out: | ||
3204 | mutex_unlock(&module_mutex); | ||
3205 | return err; | ||
3206 | } | ||
3207 | |||
2943 | /* Allocate and load the module: note that size of section 0 is always | 3208 | /* Allocate and load the module: note that size of section 0 is always |
2944 | zero, and we rely on this for optional sections. */ | 3209 | zero, and we rely on this for optional sections. */ |
2945 | static struct module *load_module(void __user *umod, | 3210 | static int load_module(struct load_info *info, const char __user *uargs, |
2946 | unsigned long len, | 3211 | int flags) |
2947 | const char __user *uargs) | ||
2948 | { | 3212 | { |
2949 | struct load_info info = { NULL, }; | 3213 | struct module *mod; |
2950 | struct module *mod, *old; | ||
2951 | long err; | 3214 | long err; |
2952 | 3215 | ||
2953 | pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", | 3216 | err = module_sig_check(info); |
2954 | umod, len, uargs); | 3217 | if (err) |
3218 | goto free_copy; | ||
2955 | 3219 | ||
2956 | /* Copy in the blobs from userspace, check they are vaguely sane. */ | 3220 | err = elf_header_check(info); |
2957 | err = copy_and_check(&info, umod, len, uargs); | ||
2958 | if (err) | 3221 | if (err) |
2959 | return ERR_PTR(err); | 3222 | goto free_copy; |
2960 | 3223 | ||
2961 | /* Figure out module layout, and allocate all the memory. */ | 3224 | /* Figure out module layout, and allocate all the memory. */ |
2962 | mod = layout_and_allocate(&info); | 3225 | mod = layout_and_allocate(info, flags); |
2963 | if (IS_ERR(mod)) { | 3226 | if (IS_ERR(mod)) { |
2964 | err = PTR_ERR(mod); | 3227 | err = PTR_ERR(mod); |
2965 | goto free_copy; | 3228 | goto free_copy; |
2966 | } | 3229 | } |
2967 | 3230 | ||
3231 | /* Reserve our place in the list. */ | ||
3232 | err = add_unformed_module(mod); | ||
3233 | if (err) | ||
3234 | goto free_module; | ||
3235 | |||
2968 | #ifdef CONFIG_MODULE_SIG | 3236 | #ifdef CONFIG_MODULE_SIG |
2969 | mod->sig_ok = info.sig_ok; | 3237 | mod->sig_ok = info->sig_ok; |
2970 | if (!mod->sig_ok) | 3238 | if (!mod->sig_ok) { |
2971 | add_taint_module(mod, TAINT_FORCED_MODULE); | 3239 | printk_once(KERN_NOTICE |
3240 | "%s: module verification failed: signature and/or" | ||
3241 | " required key missing - tainting kernel\n", | ||
3242 | mod->name); | ||
3243 | add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_STILL_OK); | ||
3244 | } | ||
2972 | #endif | 3245 | #endif |
2973 | 3246 | ||
2974 | /* Now module is in final location, initialize linked lists, etc. */ | 3247 | /* Now module is in final location, initialize linked lists, etc. */ |
2975 | err = module_unload_init(mod); | 3248 | err = module_unload_init(mod); |
2976 | if (err) | 3249 | if (err) |
2977 | goto free_module; | 3250 | goto unlink_mod; |
2978 | 3251 | ||
2979 | /* Now we've got everything in the final locations, we can | 3252 | /* Now we've got everything in the final locations, we can |
2980 | * find optional sections. */ | 3253 | * find optional sections. */ |
2981 | find_module_sections(mod, &info); | 3254 | find_module_sections(mod, info); |
2982 | 3255 | ||
2983 | err = check_module_license_and_versions(mod); | 3256 | err = check_module_license_and_versions(mod); |
2984 | if (err) | 3257 | if (err) |
2985 | goto free_unload; | 3258 | goto free_unload; |
2986 | 3259 | ||
2987 | /* Set up MODINFO_ATTR fields */ | 3260 | /* Set up MODINFO_ATTR fields */ |
2988 | setup_modinfo(mod, &info); | 3261 | setup_modinfo(mod, info); |
2989 | 3262 | ||
2990 | /* Fix up syms, so that st_value is a pointer to location. */ | 3263 | /* Fix up syms, so that st_value is a pointer to location. */ |
2991 | err = simplify_symbols(mod, &info); | 3264 | err = simplify_symbols(mod, info); |
2992 | if (err < 0) | 3265 | if (err < 0) |
2993 | goto free_modinfo; | 3266 | goto free_modinfo; |
2994 | 3267 | ||
2995 | err = apply_relocations(mod, &info); | 3268 | err = apply_relocations(mod, info); |
2996 | if (err < 0) | 3269 | if (err < 0) |
2997 | goto free_modinfo; | 3270 | goto free_modinfo; |
2998 | 3271 | ||
2999 | err = post_relocation(mod, &info); | 3272 | err = post_relocation(mod, info); |
3000 | if (err < 0) | 3273 | if (err < 0) |
3001 | goto free_modinfo; | 3274 | goto free_modinfo; |
3002 | 3275 | ||
@@ -3009,72 +3282,39 @@ static struct module *load_module(void __user *umod, | |||
3009 | goto free_arch_cleanup; | 3282 | goto free_arch_cleanup; |
3010 | } | 3283 | } |
3011 | 3284 | ||
3012 | /* Mark state as coming so strong_try_module_get() ignores us. */ | 3285 | dynamic_debug_setup(info->debug, info->num_debug); |
3013 | mod->state = MODULE_STATE_COMING; | ||
3014 | 3286 | ||
3015 | /* Now sew it into the lists so we can get lockdep and oops | 3287 | /* Finally it's fully formed, ready to start executing. */ |
3016 | * info during argument parsing. No one should access us, since | 3288 | err = complete_formation(mod, info); |
3017 | * strong_try_module_get() will fail. | 3289 | if (err) |
3018 | * lockdep/oops can run asynchronous, so use the RCU list insertion | 3290 | goto ddebug_cleanup; |
3019 | * function to insert in a way safe to concurrent readers. | ||
3020 | * The mutex protects against concurrent writers. | ||
3021 | */ | ||
3022 | again: | ||
3023 | mutex_lock(&module_mutex); | ||
3024 | if ((old = find_module(mod->name)) != NULL) { | ||
3025 | if (old->state == MODULE_STATE_COMING) { | ||
3026 | /* Wait in case it fails to load. */ | ||
3027 | mutex_unlock(&module_mutex); | ||
3028 | err = wait_event_interruptible(module_wq, | ||
3029 | finished_loading(mod->name)); | ||
3030 | if (err) | ||
3031 | goto free_arch_cleanup; | ||
3032 | goto again; | ||
3033 | } | ||
3034 | err = -EEXIST; | ||
3035 | goto unlock; | ||
3036 | } | ||
3037 | |||
3038 | /* This has to be done once we're sure module name is unique. */ | ||
3039 | dynamic_debug_setup(info.debug, info.num_debug); | ||
3040 | |||
3041 | /* Find duplicate symbols */ | ||
3042 | err = verify_export_symbols(mod); | ||
3043 | if (err < 0) | ||
3044 | goto ddebug; | ||
3045 | |||
3046 | module_bug_finalize(info.hdr, info.sechdrs, mod); | ||
3047 | list_add_rcu(&mod->list, &modules); | ||
3048 | mutex_unlock(&module_mutex); | ||
3049 | 3291 | ||
3050 | /* Module is ready to execute: parsing args may do that. */ | 3292 | /* Module is ready to execute: parsing args may do that. */ |
3051 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, | 3293 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, |
3052 | -32768, 32767, &ddebug_dyndbg_module_param_cb); | 3294 | -32768, 32767, &ddebug_dyndbg_module_param_cb); |
3053 | if (err < 0) | 3295 | if (err < 0) |
3054 | goto unlink; | 3296 | goto bug_cleanup; |
3055 | 3297 | ||
3056 | /* Link in to syfs. */ | 3298 | /* Link in to syfs. */ |
3057 | err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); | 3299 | err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp); |
3058 | if (err < 0) | 3300 | if (err < 0) |
3059 | goto unlink; | 3301 | goto bug_cleanup; |
3060 | 3302 | ||
3061 | /* Get rid of temporary copy. */ | 3303 | /* Get rid of temporary copy. */ |
3062 | free_copy(&info); | 3304 | free_copy(info); |
3063 | 3305 | ||
3064 | /* Done! */ | 3306 | /* Done! */ |
3065 | trace_module_load(mod); | 3307 | trace_module_load(mod); |
3066 | return mod; | ||
3067 | 3308 | ||
3068 | unlink: | 3309 | return do_init_module(mod); |
3310 | |||
3311 | bug_cleanup: | ||
3312 | /* module_bug_cleanup needs module_mutex protection */ | ||
3069 | mutex_lock(&module_mutex); | 3313 | mutex_lock(&module_mutex); |
3070 | /* Unlink carefully: kallsyms could be walking list. */ | ||
3071 | list_del_rcu(&mod->list); | ||
3072 | module_bug_cleanup(mod); | 3314 | module_bug_cleanup(mod); |
3073 | wake_up_all(&module_wq); | ||
3074 | ddebug: | ||
3075 | dynamic_debug_remove(info.debug); | ||
3076 | unlock: | ||
3077 | mutex_unlock(&module_mutex); | 3315 | mutex_unlock(&module_mutex); |
3316 | ddebug_cleanup: | ||
3317 | dynamic_debug_remove(info->debug); | ||
3078 | synchronize_sched(); | 3318 | synchronize_sched(); |
3079 | kfree(mod->args); | 3319 | kfree(mod->args); |
3080 | free_arch_cleanup: | 3320 | free_arch_cleanup: |
@@ -3083,107 +3323,59 @@ again: | |||
3083 | free_modinfo(mod); | 3323 | free_modinfo(mod); |
3084 | free_unload: | 3324 | free_unload: |
3085 | module_unload_free(mod); | 3325 | module_unload_free(mod); |
3326 | unlink_mod: | ||
3327 | mutex_lock(&module_mutex); | ||
3328 | /* Unlink carefully: kallsyms could be walking list. */ | ||
3329 | list_del_rcu(&mod->list); | ||
3330 | wake_up_all(&module_wq); | ||
3331 | mutex_unlock(&module_mutex); | ||
3086 | free_module: | 3332 | free_module: |
3087 | module_deallocate(mod, &info); | 3333 | module_deallocate(mod, info); |
3088 | free_copy: | 3334 | free_copy: |
3089 | free_copy(&info); | 3335 | free_copy(info); |
3090 | return ERR_PTR(err); | 3336 | return err; |
3091 | } | ||
3092 | |||
3093 | /* Call module constructors. */ | ||
3094 | static void do_mod_ctors(struct module *mod) | ||
3095 | { | ||
3096 | #ifdef CONFIG_CONSTRUCTORS | ||
3097 | unsigned long i; | ||
3098 | |||
3099 | for (i = 0; i < mod->num_ctors; i++) | ||
3100 | mod->ctors[i](); | ||
3101 | #endif | ||
3102 | } | 3337 | } |
3103 | 3338 | ||
3104 | /* This is where the real work happens */ | ||
3105 | SYSCALL_DEFINE3(init_module, void __user *, umod, | 3339 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
3106 | unsigned long, len, const char __user *, uargs) | 3340 | unsigned long, len, const char __user *, uargs) |
3107 | { | 3341 | { |
3108 | struct module *mod; | 3342 | int err; |
3109 | int ret = 0; | 3343 | struct load_info info = { }; |
3110 | 3344 | ||
3111 | /* Must have permission */ | 3345 | err = may_init_module(); |
3112 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | 3346 | if (err) |
3113 | return -EPERM; | 3347 | return err; |
3114 | 3348 | ||
3115 | /* Do all the hard work */ | 3349 | pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n", |
3116 | mod = load_module(umod, len, uargs); | 3350 | umod, len, uargs); |
3117 | if (IS_ERR(mod)) | ||
3118 | return PTR_ERR(mod); | ||
3119 | 3351 | ||
3120 | blocking_notifier_call_chain(&module_notify_list, | 3352 | err = copy_module_from_user(umod, len, &info); |
3121 | MODULE_STATE_COMING, mod); | 3353 | if (err) |
3354 | return err; | ||
3122 | 3355 | ||
3123 | /* Set RO and NX regions for core */ | 3356 | return load_module(&info, uargs, 0); |
3124 | set_section_ro_nx(mod->module_core, | 3357 | } |
3125 | mod->core_text_size, | ||
3126 | mod->core_ro_size, | ||
3127 | mod->core_size); | ||
3128 | 3358 | ||
3129 | /* Set RO and NX regions for init */ | 3359 | SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |
3130 | set_section_ro_nx(mod->module_init, | 3360 | { |
3131 | mod->init_text_size, | 3361 | int err; |
3132 | mod->init_ro_size, | 3362 | struct load_info info = { }; |
3133 | mod->init_size); | ||
3134 | 3363 | ||
3135 | do_mod_ctors(mod); | 3364 | err = may_init_module(); |
3136 | /* Start the module */ | 3365 | if (err) |
3137 | if (mod->init != NULL) | 3366 | return err; |
3138 | ret = do_one_initcall(mod->init); | ||
3139 | if (ret < 0) { | ||
3140 | /* Init routine failed: abort. Try to protect us from | ||
3141 | buggy refcounters. */ | ||
3142 | mod->state = MODULE_STATE_GOING; | ||
3143 | synchronize_sched(); | ||
3144 | module_put(mod); | ||
3145 | blocking_notifier_call_chain(&module_notify_list, | ||
3146 | MODULE_STATE_GOING, mod); | ||
3147 | free_module(mod); | ||
3148 | wake_up_all(&module_wq); | ||
3149 | return ret; | ||
3150 | } | ||
3151 | if (ret > 0) { | ||
3152 | printk(KERN_WARNING | ||
3153 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | ||
3154 | "%s: loading module anyway...\n", | ||
3155 | __func__, mod->name, ret, | ||
3156 | __func__); | ||
3157 | dump_stack(); | ||
3158 | } | ||
3159 | 3367 | ||
3160 | /* Now it's a first class citizen! */ | 3368 | pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags); |
3161 | mod->state = MODULE_STATE_LIVE; | ||
3162 | blocking_notifier_call_chain(&module_notify_list, | ||
3163 | MODULE_STATE_LIVE, mod); | ||
3164 | 3369 | ||
3165 | /* We need to finish all async code before the module init sequence is done */ | 3370 | if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS |
3166 | async_synchronize_full(); | 3371 | |MODULE_INIT_IGNORE_VERMAGIC)) |
3372 | return -EINVAL; | ||
3167 | 3373 | ||
3168 | mutex_lock(&module_mutex); | 3374 | err = copy_module_from_fd(fd, &info); |
3169 | /* Drop initial reference. */ | 3375 | if (err) |
3170 | module_put(mod); | 3376 | return err; |
3171 | trim_init_extable(mod); | ||
3172 | #ifdef CONFIG_KALLSYMS | ||
3173 | mod->num_symtab = mod->core_num_syms; | ||
3174 | mod->symtab = mod->core_symtab; | ||
3175 | mod->strtab = mod->core_strtab; | ||
3176 | #endif | ||
3177 | unset_module_init_ro_nx(mod); | ||
3178 | module_free(mod, mod->module_init); | ||
3179 | mod->module_init = NULL; | ||
3180 | mod->init_size = 0; | ||
3181 | mod->init_ro_size = 0; | ||
3182 | mod->init_text_size = 0; | ||
3183 | mutex_unlock(&module_mutex); | ||
3184 | wake_up_all(&module_wq); | ||
3185 | 3377 | ||
3186 | return 0; | 3378 | return load_module(&info, uargs, flags); |
3187 | } | 3379 | } |
3188 | 3380 | ||
3189 | static inline int within(unsigned long addr, void *start, unsigned long size) | 3381 | static inline int within(unsigned long addr, void *start, unsigned long size) |
@@ -3259,6 +3451,8 @@ const char *module_address_lookup(unsigned long addr, | |||
3259 | 3451 | ||
3260 | preempt_disable(); | 3452 | preempt_disable(); |
3261 | list_for_each_entry_rcu(mod, &modules, list) { | 3453 | list_for_each_entry_rcu(mod, &modules, list) { |
3454 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3455 | continue; | ||
3262 | if (within_module_init(addr, mod) || | 3456 | if (within_module_init(addr, mod) || |
3263 | within_module_core(addr, mod)) { | 3457 | within_module_core(addr, mod)) { |
3264 | if (modname) | 3458 | if (modname) |
@@ -3282,6 +3476,8 @@ int lookup_module_symbol_name(unsigned long addr, char *symname) | |||
3282 | 3476 | ||
3283 | preempt_disable(); | 3477 | preempt_disable(); |
3284 | list_for_each_entry_rcu(mod, &modules, list) { | 3478 | list_for_each_entry_rcu(mod, &modules, list) { |
3479 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3480 | continue; | ||
3285 | if (within_module_init(addr, mod) || | 3481 | if (within_module_init(addr, mod) || |
3286 | within_module_core(addr, mod)) { | 3482 | within_module_core(addr, mod)) { |
3287 | const char *sym; | 3483 | const char *sym; |
@@ -3306,6 +3502,8 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, | |||
3306 | 3502 | ||
3307 | preempt_disable(); | 3503 | preempt_disable(); |
3308 | list_for_each_entry_rcu(mod, &modules, list) { | 3504 | list_for_each_entry_rcu(mod, &modules, list) { |
3505 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3506 | continue; | ||
3309 | if (within_module_init(addr, mod) || | 3507 | if (within_module_init(addr, mod) || |
3310 | within_module_core(addr, mod)) { | 3508 | within_module_core(addr, mod)) { |
3311 | const char *sym; | 3509 | const char *sym; |
@@ -3333,6 +3531,8 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, | |||
3333 | 3531 | ||
3334 | preempt_disable(); | 3532 | preempt_disable(); |
3335 | list_for_each_entry_rcu(mod, &modules, list) { | 3533 | list_for_each_entry_rcu(mod, &modules, list) { |
3534 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3535 | continue; | ||
3336 | if (symnum < mod->num_symtab) { | 3536 | if (symnum < mod->num_symtab) { |
3337 | *value = mod->symtab[symnum].st_value; | 3537 | *value = mod->symtab[symnum].st_value; |
3338 | *type = mod->symtab[symnum].st_info; | 3538 | *type = mod->symtab[symnum].st_info; |
@@ -3375,9 +3575,12 @@ unsigned long module_kallsyms_lookup_name(const char *name) | |||
3375 | ret = mod_find_symname(mod, colon+1); | 3575 | ret = mod_find_symname(mod, colon+1); |
3376 | *colon = ':'; | 3576 | *colon = ':'; |
3377 | } else { | 3577 | } else { |
3378 | list_for_each_entry_rcu(mod, &modules, list) | 3578 | list_for_each_entry_rcu(mod, &modules, list) { |
3579 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3580 | continue; | ||
3379 | if ((ret = mod_find_symname(mod, name)) != 0) | 3581 | if ((ret = mod_find_symname(mod, name)) != 0) |
3380 | break; | 3582 | break; |
3583 | } | ||
3381 | } | 3584 | } |
3382 | preempt_enable(); | 3585 | preempt_enable(); |
3383 | return ret; | 3586 | return ret; |
@@ -3392,6 +3595,8 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, | |||
3392 | int ret; | 3595 | int ret; |
3393 | 3596 | ||
3394 | list_for_each_entry(mod, &modules, list) { | 3597 | list_for_each_entry(mod, &modules, list) { |
3598 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3599 | continue; | ||
3395 | for (i = 0; i < mod->num_symtab; i++) { | 3600 | for (i = 0; i < mod->num_symtab; i++) { |
3396 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, | 3601 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, |
3397 | mod, mod->symtab[i].st_value); | 3602 | mod, mod->symtab[i].st_value); |
@@ -3407,6 +3612,7 @@ static char *module_flags(struct module *mod, char *buf) | |||
3407 | { | 3612 | { |
3408 | int bx = 0; | 3613 | int bx = 0; |
3409 | 3614 | ||
3615 | BUG_ON(mod->state == MODULE_STATE_UNFORMED); | ||
3410 | if (mod->taints || | 3616 | if (mod->taints || |
3411 | mod->state == MODULE_STATE_GOING || | 3617 | mod->state == MODULE_STATE_GOING || |
3412 | mod->state == MODULE_STATE_COMING) { | 3618 | mod->state == MODULE_STATE_COMING) { |
@@ -3448,6 +3654,10 @@ static int m_show(struct seq_file *m, void *p) | |||
3448 | struct module *mod = list_entry(p, struct module, list); | 3654 | struct module *mod = list_entry(p, struct module, list); |
3449 | char buf[8]; | 3655 | char buf[8]; |
3450 | 3656 | ||
3657 | /* We always ignore unformed modules. */ | ||
3658 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3659 | return 0; | ||
3660 | |||
3451 | seq_printf(m, "%s %u", | 3661 | seq_printf(m, "%s %u", |
3452 | mod->name, mod->init_size + mod->core_size); | 3662 | mod->name, mod->init_size + mod->core_size); |
3453 | print_unload_info(m, mod); | 3663 | print_unload_info(m, mod); |
@@ -3508,6 +3718,8 @@ const struct exception_table_entry *search_module_extables(unsigned long addr) | |||
3508 | 3718 | ||
3509 | preempt_disable(); | 3719 | preempt_disable(); |
3510 | list_for_each_entry_rcu(mod, &modules, list) { | 3720 | list_for_each_entry_rcu(mod, &modules, list) { |
3721 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3722 | continue; | ||
3511 | if (mod->num_exentries == 0) | 3723 | if (mod->num_exentries == 0) |
3512 | continue; | 3724 | continue; |
3513 | 3725 | ||
@@ -3556,10 +3768,13 @@ struct module *__module_address(unsigned long addr) | |||
3556 | if (addr < module_addr_min || addr > module_addr_max) | 3768 | if (addr < module_addr_min || addr > module_addr_max) |
3557 | return NULL; | 3769 | return NULL; |
3558 | 3770 | ||
3559 | list_for_each_entry_rcu(mod, &modules, list) | 3771 | list_for_each_entry_rcu(mod, &modules, list) { |
3772 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3773 | continue; | ||
3560 | if (within_module_core(addr, mod) | 3774 | if (within_module_core(addr, mod) |
3561 | || within_module_init(addr, mod)) | 3775 | || within_module_init(addr, mod)) |
3562 | return mod; | 3776 | return mod; |
3777 | } | ||
3563 | return NULL; | 3778 | return NULL; |
3564 | } | 3779 | } |
3565 | EXPORT_SYMBOL_GPL(__module_address); | 3780 | EXPORT_SYMBOL_GPL(__module_address); |
@@ -3612,8 +3827,11 @@ void print_modules(void) | |||
3612 | printk(KERN_DEFAULT "Modules linked in:"); | 3827 | printk(KERN_DEFAULT "Modules linked in:"); |
3613 | /* Most callers should already have preempt disabled, but make sure */ | 3828 | /* Most callers should already have preempt disabled, but make sure */ |
3614 | preempt_disable(); | 3829 | preempt_disable(); |
3615 | list_for_each_entry_rcu(mod, &modules, list) | 3830 | list_for_each_entry_rcu(mod, &modules, list) { |
3831 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3832 | continue; | ||
3616 | printk(" %s%s", mod->name, module_flags(mod, buf)); | 3833 | printk(" %s%s", mod->name, module_flags(mod, buf)); |
3834 | } | ||
3617 | preempt_enable(); | 3835 | preempt_enable(); |
3618 | if (last_unloaded_module[0]) | 3836 | if (last_unloaded_module[0]) |
3619 | printk(" [last unloaded: %s]", last_unloaded_module); | 3837 | printk(" [last unloaded: %s]", last_unloaded_module); |