diff options
Diffstat (limited to 'kernel/module.c')
-rw-r--r-- | kernel/module.c | 615 |
1 files changed, 395 insertions, 220 deletions
diff --git a/kernel/module.c b/kernel/module.c index 6e48c3a43599..eab08274ec9b 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)) |
@@ -340,6 +344,9 @@ bool each_symbol_section(bool (*fn)(const struct symsearch *arr, | |||
340 | #endif | 344 | #endif |
341 | }; | 345 | }; |
342 | 346 | ||
347 | if (mod->state == MODULE_STATE_UNFORMED) | ||
348 | continue; | ||
349 | |||
343 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) | 350 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) |
344 | return true; | 351 | return true; |
345 | } | 352 | } |
@@ -372,9 +379,6 @@ static bool check_symbol(const struct symsearch *syms, | |||
372 | printk(KERN_WARNING "Symbol %s is being used " | 379 | printk(KERN_WARNING "Symbol %s is being used " |
373 | "by a non-GPL module, which will not " | 380 | "by a non-GPL module, which will not " |
374 | "be allowed in the future\n", fsa->name); | 381 | "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 | } | 382 | } |
379 | } | 383 | } |
380 | 384 | ||
@@ -450,16 +454,24 @@ const struct kernel_symbol *find_symbol(const char *name, | |||
450 | EXPORT_SYMBOL_GPL(find_symbol); | 454 | EXPORT_SYMBOL_GPL(find_symbol); |
451 | 455 | ||
452 | /* Search for module by name: must hold module_mutex. */ | 456 | /* Search for module by name: must hold module_mutex. */ |
453 | struct module *find_module(const char *name) | 457 | static struct module *find_module_all(const char *name, |
458 | bool even_unformed) | ||
454 | { | 459 | { |
455 | struct module *mod; | 460 | struct module *mod; |
456 | 461 | ||
457 | list_for_each_entry(mod, &modules, list) { | 462 | list_for_each_entry(mod, &modules, list) { |
463 | if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) | ||
464 | continue; | ||
458 | if (strcmp(mod->name, name) == 0) | 465 | if (strcmp(mod->name, name) == 0) |
459 | return mod; | 466 | return mod; |
460 | } | 467 | } |
461 | return NULL; | 468 | return NULL; |
462 | } | 469 | } |
470 | |||
471 | struct module *find_module(const char *name) | ||
472 | { | ||
473 | return find_module_all(name, false); | ||
474 | } | ||
463 | EXPORT_SYMBOL_GPL(find_module); | 475 | EXPORT_SYMBOL_GPL(find_module); |
464 | 476 | ||
465 | #ifdef CONFIG_SMP | 477 | #ifdef CONFIG_SMP |
@@ -525,6 +537,8 @@ bool is_module_percpu_address(unsigned long addr) | |||
525 | preempt_disable(); | 537 | preempt_disable(); |
526 | 538 | ||
527 | list_for_each_entry_rcu(mod, &modules, list) { | 539 | list_for_each_entry_rcu(mod, &modules, list) { |
540 | if (mod->state == MODULE_STATE_UNFORMED) | ||
541 | continue; | ||
528 | if (!mod->percpu_size) | 542 | if (!mod->percpu_size) |
529 | continue; | 543 | continue; |
530 | for_each_possible_cpu(cpu) { | 544 | for_each_possible_cpu(cpu) { |
@@ -1048,6 +1062,8 @@ static ssize_t show_initstate(struct module_attribute *mattr, | |||
1048 | case MODULE_STATE_GOING: | 1062 | case MODULE_STATE_GOING: |
1049 | state = "going"; | 1063 | state = "going"; |
1050 | break; | 1064 | break; |
1065 | default: | ||
1066 | BUG(); | ||
1051 | } | 1067 | } |
1052 | return sprintf(buffer, "%s\n", state); | 1068 | return sprintf(buffer, "%s\n", state); |
1053 | } | 1069 | } |
@@ -1786,6 +1802,8 @@ void set_all_modules_text_rw(void) | |||
1786 | 1802 | ||
1787 | mutex_lock(&module_mutex); | 1803 | mutex_lock(&module_mutex); |
1788 | list_for_each_entry_rcu(mod, &modules, list) { | 1804 | list_for_each_entry_rcu(mod, &modules, list) { |
1805 | if (mod->state == MODULE_STATE_UNFORMED) | ||
1806 | continue; | ||
1789 | if ((mod->module_core) && (mod->core_text_size)) { | 1807 | if ((mod->module_core) && (mod->core_text_size)) { |
1790 | set_page_attributes(mod->module_core, | 1808 | set_page_attributes(mod->module_core, |
1791 | mod->module_core + mod->core_text_size, | 1809 | mod->module_core + mod->core_text_size, |
@@ -1807,6 +1825,8 @@ void set_all_modules_text_ro(void) | |||
1807 | 1825 | ||
1808 | mutex_lock(&module_mutex); | 1826 | mutex_lock(&module_mutex); |
1809 | list_for_each_entry_rcu(mod, &modules, list) { | 1827 | list_for_each_entry_rcu(mod, &modules, list) { |
1828 | if (mod->state == MODULE_STATE_UNFORMED) | ||
1829 | continue; | ||
1810 | if ((mod->module_core) && (mod->core_text_size)) { | 1830 | if ((mod->module_core) && (mod->core_text_size)) { |
1811 | set_page_attributes(mod->module_core, | 1831 | set_page_attributes(mod->module_core, |
1812 | mod->module_core + mod->core_text_size, | 1832 | mod->module_core + mod->core_text_size, |
@@ -2282,7 +2302,7 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2282 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; | 2302 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; |
2283 | Elf_Shdr *strsect = info->sechdrs + info->index.str; | 2303 | Elf_Shdr *strsect = info->sechdrs + info->index.str; |
2284 | const Elf_Sym *src; | 2304 | const Elf_Sym *src; |
2285 | unsigned int i, nsrc, ndst, strtab_size; | 2305 | unsigned int i, nsrc, ndst, strtab_size = 0; |
2286 | 2306 | ||
2287 | /* Put symbol section at end of init part of module. */ | 2307 | /* Put symbol section at end of init part of module. */ |
2288 | symsect->sh_flags |= SHF_ALLOC; | 2308 | symsect->sh_flags |= SHF_ALLOC; |
@@ -2293,9 +2313,6 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2293 | src = (void *)info->hdr + symsect->sh_offset; | 2313 | src = (void *)info->hdr + symsect->sh_offset; |
2294 | nsrc = symsect->sh_size / sizeof(*src); | 2314 | nsrc = symsect->sh_size / sizeof(*src); |
2295 | 2315 | ||
2296 | /* strtab always starts with a nul, so offset 0 is the empty string. */ | ||
2297 | strtab_size = 1; | ||
2298 | |||
2299 | /* Compute total space required for the core symbols' strtab. */ | 2316 | /* Compute total space required for the core symbols' strtab. */ |
2300 | for (ndst = i = 0; i < nsrc; i++) { | 2317 | for (ndst = i = 0; i < nsrc; i++) { |
2301 | if (i == 0 || | 2318 | if (i == 0 || |
@@ -2337,7 +2354,6 @@ static void add_kallsyms(struct module *mod, const struct load_info *info) | |||
2337 | mod->core_symtab = dst = mod->module_core + info->symoffs; | 2354 | mod->core_symtab = dst = mod->module_core + info->symoffs; |
2338 | mod->core_strtab = s = mod->module_core + info->stroffs; | 2355 | mod->core_strtab = s = mod->module_core + info->stroffs; |
2339 | src = mod->symtab; | 2356 | src = mod->symtab; |
2340 | *s++ = 0; | ||
2341 | for (ndst = i = 0; i < mod->num_symtab; i++) { | 2357 | for (ndst = i = 0; i < mod->num_symtab; i++) { |
2342 | if (i == 0 || | 2358 | if (i == 0 || |
2343 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { | 2359 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { |
@@ -2378,7 +2394,7 @@ static void dynamic_debug_remove(struct _ddebug *debug) | |||
2378 | 2394 | ||
2379 | void * __weak module_alloc(unsigned long size) | 2395 | void * __weak module_alloc(unsigned long size) |
2380 | { | 2396 | { |
2381 | return size == 0 ? NULL : vmalloc_exec(size); | 2397 | return vmalloc_exec(size); |
2382 | } | 2398 | } |
2383 | 2399 | ||
2384 | static void *module_alloc_update_bounds(unsigned long size) | 2400 | static void *module_alloc_update_bounds(unsigned long size) |
@@ -2425,18 +2441,17 @@ static inline void kmemleak_load_module(const struct module *mod, | |||
2425 | #endif | 2441 | #endif |
2426 | 2442 | ||
2427 | #ifdef CONFIG_MODULE_SIG | 2443 | #ifdef CONFIG_MODULE_SIG |
2428 | static int module_sig_check(struct load_info *info, | 2444 | static int module_sig_check(struct load_info *info) |
2429 | const void *mod, unsigned long *_len) | ||
2430 | { | 2445 | { |
2431 | int err = -ENOKEY; | 2446 | int err = -ENOKEY; |
2432 | unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; | 2447 | const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; |
2433 | unsigned long len = *_len; | 2448 | const void *mod = info->hdr; |
2434 | 2449 | ||
2435 | if (len > markerlen && | 2450 | if (info->len > markerlen && |
2436 | memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { | 2451 | memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { |
2437 | /* We truncate the module to discard the signature */ | 2452 | /* We truncate the module to discard the signature */ |
2438 | *_len -= markerlen; | 2453 | info->len -= markerlen; |
2439 | err = mod_verify_sig(mod, _len); | 2454 | err = mod_verify_sig(mod, &info->len); |
2440 | } | 2455 | } |
2441 | 2456 | ||
2442 | if (!err) { | 2457 | if (!err) { |
@@ -2454,59 +2469,114 @@ static int module_sig_check(struct load_info *info, | |||
2454 | return err; | 2469 | return err; |
2455 | } | 2470 | } |
2456 | #else /* !CONFIG_MODULE_SIG */ | 2471 | #else /* !CONFIG_MODULE_SIG */ |
2457 | static int module_sig_check(struct load_info *info, | 2472 | static int module_sig_check(struct load_info *info) |
2458 | void *mod, unsigned long *len) | ||
2459 | { | 2473 | { |
2460 | return 0; | 2474 | return 0; |
2461 | } | 2475 | } |
2462 | #endif /* !CONFIG_MODULE_SIG */ | 2476 | #endif /* !CONFIG_MODULE_SIG */ |
2463 | 2477 | ||
2464 | /* Sets info->hdr, info->len and info->sig_ok. */ | 2478 | /* Sanity checks against invalid binaries, wrong arch, weird elf version. */ |
2465 | static int copy_and_check(struct load_info *info, | 2479 | static int elf_header_check(struct load_info *info) |
2466 | const void __user *umod, unsigned long len, | 2480 | { |
2467 | const char __user *uargs) | 2481 | if (info->len < sizeof(*(info->hdr))) |
2482 | return -ENOEXEC; | ||
2483 | |||
2484 | if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0 | ||
2485 | || info->hdr->e_type != ET_REL | ||
2486 | || !elf_check_arch(info->hdr) | ||
2487 | || info->hdr->e_shentsize != sizeof(Elf_Shdr)) | ||
2488 | return -ENOEXEC; | ||
2489 | |||
2490 | if (info->hdr->e_shoff >= info->len | ||
2491 | || (info->hdr->e_shnum * sizeof(Elf_Shdr) > | ||
2492 | info->len - info->hdr->e_shoff)) | ||
2493 | return -ENOEXEC; | ||
2494 | |||
2495 | return 0; | ||
2496 | } | ||
2497 | |||
2498 | /* Sets info->hdr and info->len. */ | ||
2499 | static int copy_module_from_user(const void __user *umod, unsigned long len, | ||
2500 | struct load_info *info) | ||
2468 | { | 2501 | { |
2469 | int err; | 2502 | int err; |
2470 | Elf_Ehdr *hdr; | ||
2471 | 2503 | ||
2472 | if (len < sizeof(*hdr)) | 2504 | info->len = len; |
2505 | if (info->len < sizeof(*(info->hdr))) | ||
2473 | return -ENOEXEC; | 2506 | return -ENOEXEC; |
2474 | 2507 | ||
2508 | err = security_kernel_module_from_file(NULL); | ||
2509 | if (err) | ||
2510 | return err; | ||
2511 | |||
2475 | /* Suck in entire file: we'll want most of it. */ | 2512 | /* Suck in entire file: we'll want most of it. */ |
2476 | if ((hdr = vmalloc(len)) == NULL) | 2513 | info->hdr = vmalloc(info->len); |
2514 | if (!info->hdr) | ||
2477 | return -ENOMEM; | 2515 | return -ENOMEM; |
2478 | 2516 | ||
2479 | if (copy_from_user(hdr, umod, len) != 0) { | 2517 | if (copy_from_user(info->hdr, umod, info->len) != 0) { |
2480 | err = -EFAULT; | 2518 | vfree(info->hdr); |
2481 | goto free_hdr; | 2519 | return -EFAULT; |
2482 | } | 2520 | } |
2483 | 2521 | ||
2484 | err = module_sig_check(info, hdr, &len); | 2522 | return 0; |
2523 | } | ||
2524 | |||
2525 | /* Sets info->hdr and info->len. */ | ||
2526 | static int copy_module_from_fd(int fd, struct load_info *info) | ||
2527 | { | ||
2528 | struct file *file; | ||
2529 | int err; | ||
2530 | struct kstat stat; | ||
2531 | loff_t pos; | ||
2532 | ssize_t bytes = 0; | ||
2533 | |||
2534 | file = fget(fd); | ||
2535 | if (!file) | ||
2536 | return -ENOEXEC; | ||
2537 | |||
2538 | err = security_kernel_module_from_file(file); | ||
2539 | if (err) | ||
2540 | goto out; | ||
2541 | |||
2542 | err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat); | ||
2485 | if (err) | 2543 | if (err) |
2486 | goto free_hdr; | 2544 | goto out; |
2487 | 2545 | ||
2488 | /* Sanity checks against insmoding binaries or wrong arch, | 2546 | if (stat.size > INT_MAX) { |
2489 | weird elf version */ | 2547 | err = -EFBIG; |
2490 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 | 2548 | goto out; |
2491 | || hdr->e_type != ET_REL | ||
2492 | || !elf_check_arch(hdr) | ||
2493 | || hdr->e_shentsize != sizeof(Elf_Shdr)) { | ||
2494 | err = -ENOEXEC; | ||
2495 | goto free_hdr; | ||
2496 | } | 2549 | } |
2497 | 2550 | ||
2498 | if (hdr->e_shoff >= len || | 2551 | /* Don't hand 0 to vmalloc, it whines. */ |
2499 | hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) { | 2552 | if (stat.size == 0) { |
2500 | err = -ENOEXEC; | 2553 | err = -EINVAL; |
2501 | goto free_hdr; | 2554 | goto out; |
2502 | } | 2555 | } |
2503 | 2556 | ||
2504 | info->hdr = hdr; | 2557 | info->hdr = vmalloc(stat.size); |
2505 | info->len = len; | 2558 | if (!info->hdr) { |
2506 | return 0; | 2559 | err = -ENOMEM; |
2560 | goto out; | ||
2561 | } | ||
2562 | |||
2563 | pos = 0; | ||
2564 | while (pos < stat.size) { | ||
2565 | bytes = kernel_read(file, pos, (char *)(info->hdr) + pos, | ||
2566 | stat.size - pos); | ||
2567 | if (bytes < 0) { | ||
2568 | vfree(info->hdr); | ||
2569 | err = bytes; | ||
2570 | goto out; | ||
2571 | } | ||
2572 | if (bytes == 0) | ||
2573 | break; | ||
2574 | pos += bytes; | ||
2575 | } | ||
2576 | info->len = pos; | ||
2507 | 2577 | ||
2508 | free_hdr: | 2578 | out: |
2509 | vfree(hdr); | 2579 | fput(file); |
2510 | return err; | 2580 | return err; |
2511 | } | 2581 | } |
2512 | 2582 | ||
@@ -2515,7 +2585,7 @@ static void free_copy(struct load_info *info) | |||
2515 | vfree(info->hdr); | 2585 | vfree(info->hdr); |
2516 | } | 2586 | } |
2517 | 2587 | ||
2518 | static int rewrite_section_headers(struct load_info *info) | 2588 | static int rewrite_section_headers(struct load_info *info, int flags) |
2519 | { | 2589 | { |
2520 | unsigned int i; | 2590 | unsigned int i; |
2521 | 2591 | ||
@@ -2543,7 +2613,10 @@ static int rewrite_section_headers(struct load_info *info) | |||
2543 | } | 2613 | } |
2544 | 2614 | ||
2545 | /* Track but don't keep modinfo and version sections. */ | 2615 | /* Track but don't keep modinfo and version sections. */ |
2546 | info->index.vers = find_sec(info, "__versions"); | 2616 | if (flags & MODULE_INIT_IGNORE_MODVERSIONS) |
2617 | info->index.vers = 0; /* Pretend no __versions section! */ | ||
2618 | else | ||
2619 | info->index.vers = find_sec(info, "__versions"); | ||
2547 | info->index.info = find_sec(info, ".modinfo"); | 2620 | info->index.info = find_sec(info, ".modinfo"); |
2548 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2621 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; |
2549 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2622 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; |
@@ -2558,7 +2631,7 @@ static int rewrite_section_headers(struct load_info *info) | |||
2558 | * Return the temporary module pointer (we'll replace it with the final | 2631 | * Return the temporary module pointer (we'll replace it with the final |
2559 | * one when we move the module sections around). | 2632 | * one when we move the module sections around). |
2560 | */ | 2633 | */ |
2561 | static struct module *setup_load_info(struct load_info *info) | 2634 | static struct module *setup_load_info(struct load_info *info, int flags) |
2562 | { | 2635 | { |
2563 | unsigned int i; | 2636 | unsigned int i; |
2564 | int err; | 2637 | int err; |
@@ -2569,7 +2642,7 @@ static struct module *setup_load_info(struct load_info *info) | |||
2569 | info->secstrings = (void *)info->hdr | 2642 | info->secstrings = (void *)info->hdr |
2570 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; | 2643 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; |
2571 | 2644 | ||
2572 | err = rewrite_section_headers(info); | 2645 | err = rewrite_section_headers(info, flags); |
2573 | if (err) | 2646 | if (err) |
2574 | return ERR_PTR(err); | 2647 | return ERR_PTR(err); |
2575 | 2648 | ||
@@ -2607,11 +2680,14 @@ static struct module *setup_load_info(struct load_info *info) | |||
2607 | return mod; | 2680 | return mod; |
2608 | } | 2681 | } |
2609 | 2682 | ||
2610 | static int check_modinfo(struct module *mod, struct load_info *info) | 2683 | static int check_modinfo(struct module *mod, struct load_info *info, int flags) |
2611 | { | 2684 | { |
2612 | const char *modmagic = get_modinfo(info, "vermagic"); | 2685 | const char *modmagic = get_modinfo(info, "vermagic"); |
2613 | int err; | 2686 | int err; |
2614 | 2687 | ||
2688 | if (flags & MODULE_INIT_IGNORE_VERMAGIC) | ||
2689 | modmagic = NULL; | ||
2690 | |||
2615 | /* This is allowed: modprobe --force will invalidate it. */ | 2691 | /* This is allowed: modprobe --force will invalidate it. */ |
2616 | if (!modmagic) { | 2692 | if (!modmagic) { |
2617 | err = try_to_force_load(mod, "bad vermagic"); | 2693 | err = try_to_force_load(mod, "bad vermagic"); |
@@ -2741,20 +2817,23 @@ static int move_module(struct module *mod, struct load_info *info) | |||
2741 | memset(ptr, 0, mod->core_size); | 2817 | memset(ptr, 0, mod->core_size); |
2742 | mod->module_core = ptr; | 2818 | mod->module_core = ptr; |
2743 | 2819 | ||
2744 | ptr = module_alloc_update_bounds(mod->init_size); | 2820 | if (mod->init_size) { |
2745 | /* | 2821 | ptr = module_alloc_update_bounds(mod->init_size); |
2746 | * The pointer to this block is stored in the module structure | 2822 | /* |
2747 | * which is inside the block. This block doesn't need to be | 2823 | * The pointer to this block is stored in the module structure |
2748 | * scanned as it contains data and code that will be freed | 2824 | * which is inside the block. This block doesn't need to be |
2749 | * after the module is initialized. | 2825 | * scanned as it contains data and code that will be freed |
2750 | */ | 2826 | * after the module is initialized. |
2751 | kmemleak_ignore(ptr); | 2827 | */ |
2752 | if (!ptr && mod->init_size) { | 2828 | kmemleak_ignore(ptr); |
2753 | module_free(mod, mod->module_core); | 2829 | if (!ptr) { |
2754 | return -ENOMEM; | 2830 | module_free(mod, mod->module_core); |
2755 | } | 2831 | return -ENOMEM; |
2756 | memset(ptr, 0, mod->init_size); | 2832 | } |
2757 | mod->module_init = ptr; | 2833 | memset(ptr, 0, mod->init_size); |
2834 | mod->module_init = ptr; | ||
2835 | } else | ||
2836 | mod->module_init = NULL; | ||
2758 | 2837 | ||
2759 | /* Transfer each section which specifies SHF_ALLOC */ | 2838 | /* Transfer each section which specifies SHF_ALLOC */ |
2760 | pr_debug("final section addresses:\n"); | 2839 | pr_debug("final section addresses:\n"); |
@@ -2847,18 +2926,18 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr, | |||
2847 | return 0; | 2926 | return 0; |
2848 | } | 2927 | } |
2849 | 2928 | ||
2850 | static struct module *layout_and_allocate(struct load_info *info) | 2929 | static struct module *layout_and_allocate(struct load_info *info, int flags) |
2851 | { | 2930 | { |
2852 | /* Module within temporary copy. */ | 2931 | /* Module within temporary copy. */ |
2853 | struct module *mod; | 2932 | struct module *mod; |
2854 | Elf_Shdr *pcpusec; | 2933 | Elf_Shdr *pcpusec; |
2855 | int err; | 2934 | int err; |
2856 | 2935 | ||
2857 | mod = setup_load_info(info); | 2936 | mod = setup_load_info(info, flags); |
2858 | if (IS_ERR(mod)) | 2937 | if (IS_ERR(mod)) |
2859 | return mod; | 2938 | return mod; |
2860 | 2939 | ||
2861 | err = check_modinfo(mod, info); | 2940 | err = check_modinfo(mod, info, flags); |
2862 | if (err) | 2941 | if (err) |
2863 | return ERR_PTR(err); | 2942 | return ERR_PTR(err); |
2864 | 2943 | ||
@@ -2938,40 +3017,181 @@ static bool finished_loading(const char *name) | |||
2938 | bool ret; | 3017 | bool ret; |
2939 | 3018 | ||
2940 | mutex_lock(&module_mutex); | 3019 | mutex_lock(&module_mutex); |
2941 | mod = find_module(name); | 3020 | mod = find_module_all(name, true); |
2942 | ret = !mod || mod->state != MODULE_STATE_COMING; | 3021 | ret = !mod || mod->state == MODULE_STATE_LIVE |
3022 | || mod->state == MODULE_STATE_GOING; | ||
2943 | mutex_unlock(&module_mutex); | 3023 | mutex_unlock(&module_mutex); |
2944 | 3024 | ||
2945 | return ret; | 3025 | return ret; |
2946 | } | 3026 | } |
2947 | 3027 | ||
3028 | /* Call module constructors. */ | ||
3029 | static void do_mod_ctors(struct module *mod) | ||
3030 | { | ||
3031 | #ifdef CONFIG_CONSTRUCTORS | ||
3032 | unsigned long i; | ||
3033 | |||
3034 | for (i = 0; i < mod->num_ctors; i++) | ||
3035 | mod->ctors[i](); | ||
3036 | #endif | ||
3037 | } | ||
3038 | |||
3039 | /* This is where the real work happens */ | ||
3040 | static int do_init_module(struct module *mod) | ||
3041 | { | ||
3042 | int ret = 0; | ||
3043 | |||
3044 | /* | ||
3045 | * We want to find out whether @mod uses async during init. Clear | ||
3046 | * PF_USED_ASYNC. async_schedule*() will set it. | ||
3047 | */ | ||
3048 | current->flags &= ~PF_USED_ASYNC; | ||
3049 | |||
3050 | blocking_notifier_call_chain(&module_notify_list, | ||
3051 | MODULE_STATE_COMING, mod); | ||
3052 | |||
3053 | /* Set RO and NX regions for core */ | ||
3054 | set_section_ro_nx(mod->module_core, | ||
3055 | mod->core_text_size, | ||
3056 | mod->core_ro_size, | ||
3057 | mod->core_size); | ||
3058 | |||
3059 | /* Set RO and NX regions for init */ | ||
3060 | set_section_ro_nx(mod->module_init, | ||
3061 | mod->init_text_size, | ||
3062 | mod->init_ro_size, | ||
3063 | mod->init_size); | ||
3064 | |||
3065 | do_mod_ctors(mod); | ||
3066 | /* Start the module */ | ||
3067 | if (mod->init != NULL) | ||
3068 | ret = do_one_initcall(mod->init); | ||
3069 | if (ret < 0) { | ||
3070 | /* Init routine failed: abort. Try to protect us from | ||
3071 | buggy refcounters. */ | ||
3072 | mod->state = MODULE_STATE_GOING; | ||
3073 | synchronize_sched(); | ||
3074 | module_put(mod); | ||
3075 | blocking_notifier_call_chain(&module_notify_list, | ||
3076 | MODULE_STATE_GOING, mod); | ||
3077 | free_module(mod); | ||
3078 | wake_up_all(&module_wq); | ||
3079 | return ret; | ||
3080 | } | ||
3081 | if (ret > 0) { | ||
3082 | printk(KERN_WARNING | ||
3083 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | ||
3084 | "%s: loading module anyway...\n", | ||
3085 | __func__, mod->name, ret, | ||
3086 | __func__); | ||
3087 | dump_stack(); | ||
3088 | } | ||
3089 | |||
3090 | /* Now it's a first class citizen! */ | ||
3091 | mod->state = MODULE_STATE_LIVE; | ||
3092 | blocking_notifier_call_chain(&module_notify_list, | ||
3093 | MODULE_STATE_LIVE, mod); | ||
3094 | |||
3095 | /* | ||
3096 | * We need to finish all async code before the module init sequence | ||
3097 | * is done. This has potential to deadlock. For example, a newly | ||
3098 | * detected block device can trigger request_module() of the | ||
3099 | * default iosched from async probing task. Once userland helper | ||
3100 | * reaches here, async_synchronize_full() will wait on the async | ||
3101 | * task waiting on request_module() and deadlock. | ||
3102 | * | ||
3103 | * This deadlock is avoided by perfomring async_synchronize_full() | ||
3104 | * iff module init queued any async jobs. This isn't a full | ||
3105 | * solution as it will deadlock the same if module loading from | ||
3106 | * async jobs nests more than once; however, due to the various | ||
3107 | * constraints, this hack seems to be the best option for now. | ||
3108 | * Please refer to the following thread for details. | ||
3109 | * | ||
3110 | * http://thread.gmane.org/gmane.linux.kernel/1420814 | ||
3111 | */ | ||
3112 | if (current->flags & PF_USED_ASYNC) | ||
3113 | async_synchronize_full(); | ||
3114 | |||
3115 | mutex_lock(&module_mutex); | ||
3116 | /* Drop initial reference. */ | ||
3117 | module_put(mod); | ||
3118 | trim_init_extable(mod); | ||
3119 | #ifdef CONFIG_KALLSYMS | ||
3120 | mod->num_symtab = mod->core_num_syms; | ||
3121 | mod->symtab = mod->core_symtab; | ||
3122 | mod->strtab = mod->core_strtab; | ||
3123 | #endif | ||
3124 | unset_module_init_ro_nx(mod); | ||
3125 | module_free(mod, mod->module_init); | ||
3126 | mod->module_init = NULL; | ||
3127 | mod->init_size = 0; | ||
3128 | mod->init_ro_size = 0; | ||
3129 | mod->init_text_size = 0; | ||
3130 | mutex_unlock(&module_mutex); | ||
3131 | wake_up_all(&module_wq); | ||
3132 | |||
3133 | return 0; | ||
3134 | } | ||
3135 | |||
3136 | static int may_init_module(void) | ||
3137 | { | ||
3138 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | ||
3139 | return -EPERM; | ||
3140 | |||
3141 | return 0; | ||
3142 | } | ||
3143 | |||
2948 | /* Allocate and load the module: note that size of section 0 is always | 3144 | /* Allocate and load the module: note that size of section 0 is always |
2949 | zero, and we rely on this for optional sections. */ | 3145 | zero, and we rely on this for optional sections. */ |
2950 | static struct module *load_module(void __user *umod, | 3146 | static int load_module(struct load_info *info, const char __user *uargs, |
2951 | unsigned long len, | 3147 | int flags) |
2952 | const char __user *uargs) | ||
2953 | { | 3148 | { |
2954 | struct load_info info = { NULL, }; | ||
2955 | struct module *mod, *old; | 3149 | struct module *mod, *old; |
2956 | long err; | 3150 | long err; |
2957 | 3151 | ||
2958 | pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", | 3152 | err = module_sig_check(info); |
2959 | umod, len, uargs); | 3153 | if (err) |
3154 | goto free_copy; | ||
2960 | 3155 | ||
2961 | /* Copy in the blobs from userspace, check they are vaguely sane. */ | 3156 | err = elf_header_check(info); |
2962 | err = copy_and_check(&info, umod, len, uargs); | ||
2963 | if (err) | 3157 | if (err) |
2964 | return ERR_PTR(err); | 3158 | goto free_copy; |
2965 | 3159 | ||
2966 | /* Figure out module layout, and allocate all the memory. */ | 3160 | /* Figure out module layout, and allocate all the memory. */ |
2967 | mod = layout_and_allocate(&info); | 3161 | mod = layout_and_allocate(info, flags); |
2968 | if (IS_ERR(mod)) { | 3162 | if (IS_ERR(mod)) { |
2969 | err = PTR_ERR(mod); | 3163 | err = PTR_ERR(mod); |
2970 | goto free_copy; | 3164 | goto free_copy; |
2971 | } | 3165 | } |
2972 | 3166 | ||
3167 | /* | ||
3168 | * We try to place it in the list now to make sure it's unique | ||
3169 | * before we dedicate too many resources. In particular, | ||
3170 | * temporary percpu memory exhaustion. | ||
3171 | */ | ||
3172 | mod->state = MODULE_STATE_UNFORMED; | ||
3173 | again: | ||
3174 | mutex_lock(&module_mutex); | ||
3175 | if ((old = find_module_all(mod->name, true)) != NULL) { | ||
3176 | if (old->state == MODULE_STATE_COMING | ||
3177 | || old->state == MODULE_STATE_UNFORMED) { | ||
3178 | /* Wait in case it fails to load. */ | ||
3179 | mutex_unlock(&module_mutex); | ||
3180 | err = wait_event_interruptible(module_wq, | ||
3181 | finished_loading(mod->name)); | ||
3182 | if (err) | ||
3183 | goto free_module; | ||
3184 | goto again; | ||
3185 | } | ||
3186 | err = -EEXIST; | ||
3187 | mutex_unlock(&module_mutex); | ||
3188 | goto free_module; | ||
3189 | } | ||
3190 | list_add_rcu(&mod->list, &modules); | ||
3191 | mutex_unlock(&module_mutex); | ||
3192 | |||
2973 | #ifdef CONFIG_MODULE_SIG | 3193 | #ifdef CONFIG_MODULE_SIG |
2974 | mod->sig_ok = info.sig_ok; | 3194 | mod->sig_ok = info->sig_ok; |
2975 | if (!mod->sig_ok) | 3195 | if (!mod->sig_ok) |
2976 | add_taint_module(mod, TAINT_FORCED_MODULE); | 3196 | add_taint_module(mod, TAINT_FORCED_MODULE); |
2977 | #endif | 3197 | #endif |
@@ -2979,29 +3199,29 @@ static struct module *load_module(void __user *umod, | |||
2979 | /* Now module is in final location, initialize linked lists, etc. */ | 3199 | /* Now module is in final location, initialize linked lists, etc. */ |
2980 | err = module_unload_init(mod); | 3200 | err = module_unload_init(mod); |
2981 | if (err) | 3201 | if (err) |
2982 | goto free_module; | 3202 | goto unlink_mod; |
2983 | 3203 | ||
2984 | /* Now we've got everything in the final locations, we can | 3204 | /* Now we've got everything in the final locations, we can |
2985 | * find optional sections. */ | 3205 | * find optional sections. */ |
2986 | find_module_sections(mod, &info); | 3206 | find_module_sections(mod, info); |
2987 | 3207 | ||
2988 | err = check_module_license_and_versions(mod); | 3208 | err = check_module_license_and_versions(mod); |
2989 | if (err) | 3209 | if (err) |
2990 | goto free_unload; | 3210 | goto free_unload; |
2991 | 3211 | ||
2992 | /* Set up MODINFO_ATTR fields */ | 3212 | /* Set up MODINFO_ATTR fields */ |
2993 | setup_modinfo(mod, &info); | 3213 | setup_modinfo(mod, info); |
2994 | 3214 | ||
2995 | /* Fix up syms, so that st_value is a pointer to location. */ | 3215 | /* Fix up syms, so that st_value is a pointer to location. */ |
2996 | err = simplify_symbols(mod, &info); | 3216 | err = simplify_symbols(mod, info); |
2997 | if (err < 0) | 3217 | if (err < 0) |
2998 | goto free_modinfo; | 3218 | goto free_modinfo; |
2999 | 3219 | ||
3000 | err = apply_relocations(mod, &info); | 3220 | err = apply_relocations(mod, info); |
3001 | if (err < 0) | 3221 | if (err < 0) |
3002 | goto free_modinfo; | 3222 | goto free_modinfo; |
3003 | 3223 | ||
3004 | err = post_relocation(mod, &info); | 3224 | err = post_relocation(mod, info); |
3005 | if (err < 0) | 3225 | if (err < 0) |
3006 | goto free_modinfo; | 3226 | goto free_modinfo; |
3007 | 3227 | ||
@@ -3014,72 +3234,49 @@ static struct module *load_module(void __user *umod, | |||
3014 | goto free_arch_cleanup; | 3234 | goto free_arch_cleanup; |
3015 | } | 3235 | } |
3016 | 3236 | ||
3017 | /* Mark state as coming so strong_try_module_get() ignores us. */ | 3237 | dynamic_debug_setup(info->debug, info->num_debug); |
3018 | mod->state = MODULE_STATE_COMING; | ||
3019 | 3238 | ||
3020 | /* Now sew it into the lists so we can get lockdep and oops | ||
3021 | * info during argument parsing. No one should access us, since | ||
3022 | * strong_try_module_get() will fail. | ||
3023 | * lockdep/oops can run asynchronous, so use the RCU list insertion | ||
3024 | * function to insert in a way safe to concurrent readers. | ||
3025 | * The mutex protects against concurrent writers. | ||
3026 | */ | ||
3027 | again: | ||
3028 | mutex_lock(&module_mutex); | 3239 | mutex_lock(&module_mutex); |
3029 | if ((old = find_module(mod->name)) != NULL) { | 3240 | /* Find duplicate symbols (must be called under lock). */ |
3030 | if (old->state == MODULE_STATE_COMING) { | ||
3031 | /* Wait in case it fails to load. */ | ||
3032 | mutex_unlock(&module_mutex); | ||
3033 | err = wait_event_interruptible(module_wq, | ||
3034 | finished_loading(mod->name)); | ||
3035 | if (err) | ||
3036 | goto free_arch_cleanup; | ||
3037 | goto again; | ||
3038 | } | ||
3039 | err = -EEXIST; | ||
3040 | goto unlock; | ||
3041 | } | ||
3042 | |||
3043 | /* This has to be done once we're sure module name is unique. */ | ||
3044 | dynamic_debug_setup(info.debug, info.num_debug); | ||
3045 | |||
3046 | /* Find duplicate symbols */ | ||
3047 | err = verify_export_symbols(mod); | 3241 | err = verify_export_symbols(mod); |
3048 | if (err < 0) | 3242 | if (err < 0) |
3049 | goto ddebug; | 3243 | goto ddebug_cleanup; |
3244 | |||
3245 | /* This relies on module_mutex for list integrity. */ | ||
3246 | module_bug_finalize(info->hdr, info->sechdrs, mod); | ||
3247 | |||
3248 | /* Mark state as coming so strong_try_module_get() ignores us, | ||
3249 | * but kallsyms etc. can see us. */ | ||
3250 | mod->state = MODULE_STATE_COMING; | ||
3050 | 3251 | ||
3051 | module_bug_finalize(info.hdr, info.sechdrs, mod); | ||
3052 | list_add_rcu(&mod->list, &modules); | ||
3053 | mutex_unlock(&module_mutex); | 3252 | mutex_unlock(&module_mutex); |
3054 | 3253 | ||
3055 | /* Module is ready to execute: parsing args may do that. */ | 3254 | /* Module is ready to execute: parsing args may do that. */ |
3056 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, | 3255 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, |
3057 | -32768, 32767, &ddebug_dyndbg_module_param_cb); | 3256 | -32768, 32767, &ddebug_dyndbg_module_param_cb); |
3058 | if (err < 0) | 3257 | if (err < 0) |
3059 | goto unlink; | 3258 | goto bug_cleanup; |
3060 | 3259 | ||
3061 | /* Link in to syfs. */ | 3260 | /* Link in to syfs. */ |
3062 | err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); | 3261 | err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp); |
3063 | if (err < 0) | 3262 | if (err < 0) |
3064 | goto unlink; | 3263 | goto bug_cleanup; |
3065 | 3264 | ||
3066 | /* Get rid of temporary copy. */ | 3265 | /* Get rid of temporary copy. */ |
3067 | free_copy(&info); | 3266 | free_copy(info); |
3068 | 3267 | ||
3069 | /* Done! */ | 3268 | /* Done! */ |
3070 | trace_module_load(mod); | 3269 | trace_module_load(mod); |
3071 | return mod; | ||
3072 | 3270 | ||
3073 | unlink: | 3271 | return do_init_module(mod); |
3272 | |||
3273 | bug_cleanup: | ||
3274 | /* module_bug_cleanup needs module_mutex protection */ | ||
3074 | mutex_lock(&module_mutex); | 3275 | mutex_lock(&module_mutex); |
3075 | /* Unlink carefully: kallsyms could be walking list. */ | ||
3076 | list_del_rcu(&mod->list); | ||
3077 | module_bug_cleanup(mod); | 3276 | module_bug_cleanup(mod); |
3078 | wake_up_all(&module_wq); | 3277 | ddebug_cleanup: |
3079 | ddebug: | ||
3080 | dynamic_debug_remove(info.debug); | ||
3081 | unlock: | ||
3082 | mutex_unlock(&module_mutex); | 3278 | mutex_unlock(&module_mutex); |
3279 | dynamic_debug_remove(info->debug); | ||
3083 | synchronize_sched(); | 3280 | synchronize_sched(); |
3084 | kfree(mod->args); | 3281 | kfree(mod->args); |
3085 | free_arch_cleanup: | 3282 | free_arch_cleanup: |
@@ -3088,107 +3285,59 @@ again: | |||
3088 | free_modinfo(mod); | 3285 | free_modinfo(mod); |
3089 | free_unload: | 3286 | free_unload: |
3090 | module_unload_free(mod); | 3287 | module_unload_free(mod); |
3288 | unlink_mod: | ||
3289 | mutex_lock(&module_mutex); | ||
3290 | /* Unlink carefully: kallsyms could be walking list. */ | ||
3291 | list_del_rcu(&mod->list); | ||
3292 | wake_up_all(&module_wq); | ||
3293 | mutex_unlock(&module_mutex); | ||
3091 | free_module: | 3294 | free_module: |
3092 | module_deallocate(mod, &info); | 3295 | module_deallocate(mod, info); |
3093 | free_copy: | 3296 | free_copy: |
3094 | free_copy(&info); | 3297 | free_copy(info); |
3095 | return ERR_PTR(err); | 3298 | return err; |
3096 | } | ||
3097 | |||
3098 | /* Call module constructors. */ | ||
3099 | static void do_mod_ctors(struct module *mod) | ||
3100 | { | ||
3101 | #ifdef CONFIG_CONSTRUCTORS | ||
3102 | unsigned long i; | ||
3103 | |||
3104 | for (i = 0; i < mod->num_ctors; i++) | ||
3105 | mod->ctors[i](); | ||
3106 | #endif | ||
3107 | } | 3299 | } |
3108 | 3300 | ||
3109 | /* This is where the real work happens */ | ||
3110 | SYSCALL_DEFINE3(init_module, void __user *, umod, | 3301 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
3111 | unsigned long, len, const char __user *, uargs) | 3302 | unsigned long, len, const char __user *, uargs) |
3112 | { | 3303 | { |
3113 | struct module *mod; | 3304 | int err; |
3114 | int ret = 0; | 3305 | struct load_info info = { }; |
3115 | 3306 | ||
3116 | /* Must have permission */ | 3307 | err = may_init_module(); |
3117 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | 3308 | if (err) |
3118 | return -EPERM; | 3309 | return err; |
3119 | 3310 | ||
3120 | /* Do all the hard work */ | 3311 | pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n", |
3121 | mod = load_module(umod, len, uargs); | 3312 | umod, len, uargs); |
3122 | if (IS_ERR(mod)) | ||
3123 | return PTR_ERR(mod); | ||
3124 | 3313 | ||
3125 | blocking_notifier_call_chain(&module_notify_list, | 3314 | err = copy_module_from_user(umod, len, &info); |
3126 | MODULE_STATE_COMING, mod); | 3315 | if (err) |
3316 | return err; | ||
3127 | 3317 | ||
3128 | /* Set RO and NX regions for core */ | 3318 | return load_module(&info, uargs, 0); |
3129 | set_section_ro_nx(mod->module_core, | 3319 | } |
3130 | mod->core_text_size, | ||
3131 | mod->core_ro_size, | ||
3132 | mod->core_size); | ||
3133 | 3320 | ||
3134 | /* Set RO and NX regions for init */ | 3321 | SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |
3135 | set_section_ro_nx(mod->module_init, | 3322 | { |
3136 | mod->init_text_size, | 3323 | int err; |
3137 | mod->init_ro_size, | 3324 | struct load_info info = { }; |
3138 | mod->init_size); | ||
3139 | 3325 | ||
3140 | do_mod_ctors(mod); | 3326 | err = may_init_module(); |
3141 | /* Start the module */ | 3327 | if (err) |
3142 | if (mod->init != NULL) | 3328 | return err; |
3143 | ret = do_one_initcall(mod->init); | ||
3144 | if (ret < 0) { | ||
3145 | /* Init routine failed: abort. Try to protect us from | ||
3146 | buggy refcounters. */ | ||
3147 | mod->state = MODULE_STATE_GOING; | ||
3148 | synchronize_sched(); | ||
3149 | module_put(mod); | ||
3150 | blocking_notifier_call_chain(&module_notify_list, | ||
3151 | MODULE_STATE_GOING, mod); | ||
3152 | free_module(mod); | ||
3153 | wake_up_all(&module_wq); | ||
3154 | return ret; | ||
3155 | } | ||
3156 | if (ret > 0) { | ||
3157 | printk(KERN_WARNING | ||
3158 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | ||
3159 | "%s: loading module anyway...\n", | ||
3160 | __func__, mod->name, ret, | ||
3161 | __func__); | ||
3162 | dump_stack(); | ||
3163 | } | ||
3164 | 3329 | ||
3165 | /* Now it's a first class citizen! */ | 3330 | pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags); |
3166 | mod->state = MODULE_STATE_LIVE; | ||
3167 | blocking_notifier_call_chain(&module_notify_list, | ||
3168 | MODULE_STATE_LIVE, mod); | ||
3169 | 3331 | ||
3170 | /* We need to finish all async code before the module init sequence is done */ | 3332 | if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS |
3171 | async_synchronize_full(); | 3333 | |MODULE_INIT_IGNORE_VERMAGIC)) |
3334 | return -EINVAL; | ||
3172 | 3335 | ||
3173 | mutex_lock(&module_mutex); | 3336 | err = copy_module_from_fd(fd, &info); |
3174 | /* Drop initial reference. */ | 3337 | if (err) |
3175 | module_put(mod); | 3338 | return err; |
3176 | trim_init_extable(mod); | ||
3177 | #ifdef CONFIG_KALLSYMS | ||
3178 | mod->num_symtab = mod->core_num_syms; | ||
3179 | mod->symtab = mod->core_symtab; | ||
3180 | mod->strtab = mod->core_strtab; | ||
3181 | #endif | ||
3182 | unset_module_init_ro_nx(mod); | ||
3183 | module_free(mod, mod->module_init); | ||
3184 | mod->module_init = NULL; | ||
3185 | mod->init_size = 0; | ||
3186 | mod->init_ro_size = 0; | ||
3187 | mod->init_text_size = 0; | ||
3188 | mutex_unlock(&module_mutex); | ||
3189 | wake_up_all(&module_wq); | ||
3190 | 3339 | ||
3191 | return 0; | 3340 | return load_module(&info, uargs, flags); |
3192 | } | 3341 | } |
3193 | 3342 | ||
3194 | static inline int within(unsigned long addr, void *start, unsigned long size) | 3343 | static inline int within(unsigned long addr, void *start, unsigned long size) |
@@ -3264,6 +3413,8 @@ const char *module_address_lookup(unsigned long addr, | |||
3264 | 3413 | ||
3265 | preempt_disable(); | 3414 | preempt_disable(); |
3266 | list_for_each_entry_rcu(mod, &modules, list) { | 3415 | list_for_each_entry_rcu(mod, &modules, list) { |
3416 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3417 | continue; | ||
3267 | if (within_module_init(addr, mod) || | 3418 | if (within_module_init(addr, mod) || |
3268 | within_module_core(addr, mod)) { | 3419 | within_module_core(addr, mod)) { |
3269 | if (modname) | 3420 | if (modname) |
@@ -3287,6 +3438,8 @@ int lookup_module_symbol_name(unsigned long addr, char *symname) | |||
3287 | 3438 | ||
3288 | preempt_disable(); | 3439 | preempt_disable(); |
3289 | list_for_each_entry_rcu(mod, &modules, list) { | 3440 | list_for_each_entry_rcu(mod, &modules, list) { |
3441 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3442 | continue; | ||
3290 | if (within_module_init(addr, mod) || | 3443 | if (within_module_init(addr, mod) || |
3291 | within_module_core(addr, mod)) { | 3444 | within_module_core(addr, mod)) { |
3292 | const char *sym; | 3445 | const char *sym; |
@@ -3311,6 +3464,8 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, | |||
3311 | 3464 | ||
3312 | preempt_disable(); | 3465 | preempt_disable(); |
3313 | list_for_each_entry_rcu(mod, &modules, list) { | 3466 | list_for_each_entry_rcu(mod, &modules, list) { |
3467 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3468 | continue; | ||
3314 | if (within_module_init(addr, mod) || | 3469 | if (within_module_init(addr, mod) || |
3315 | within_module_core(addr, mod)) { | 3470 | within_module_core(addr, mod)) { |
3316 | const char *sym; | 3471 | const char *sym; |
@@ -3338,6 +3493,8 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, | |||
3338 | 3493 | ||
3339 | preempt_disable(); | 3494 | preempt_disable(); |
3340 | list_for_each_entry_rcu(mod, &modules, list) { | 3495 | list_for_each_entry_rcu(mod, &modules, list) { |
3496 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3497 | continue; | ||
3341 | if (symnum < mod->num_symtab) { | 3498 | if (symnum < mod->num_symtab) { |
3342 | *value = mod->symtab[symnum].st_value; | 3499 | *value = mod->symtab[symnum].st_value; |
3343 | *type = mod->symtab[symnum].st_info; | 3500 | *type = mod->symtab[symnum].st_info; |
@@ -3380,9 +3537,12 @@ unsigned long module_kallsyms_lookup_name(const char *name) | |||
3380 | ret = mod_find_symname(mod, colon+1); | 3537 | ret = mod_find_symname(mod, colon+1); |
3381 | *colon = ':'; | 3538 | *colon = ':'; |
3382 | } else { | 3539 | } else { |
3383 | list_for_each_entry_rcu(mod, &modules, list) | 3540 | list_for_each_entry_rcu(mod, &modules, list) { |
3541 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3542 | continue; | ||
3384 | if ((ret = mod_find_symname(mod, name)) != 0) | 3543 | if ((ret = mod_find_symname(mod, name)) != 0) |
3385 | break; | 3544 | break; |
3545 | } | ||
3386 | } | 3546 | } |
3387 | preempt_enable(); | 3547 | preempt_enable(); |
3388 | return ret; | 3548 | return ret; |
@@ -3397,6 +3557,8 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, | |||
3397 | int ret; | 3557 | int ret; |
3398 | 3558 | ||
3399 | list_for_each_entry(mod, &modules, list) { | 3559 | list_for_each_entry(mod, &modules, list) { |
3560 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3561 | continue; | ||
3400 | for (i = 0; i < mod->num_symtab; i++) { | 3562 | for (i = 0; i < mod->num_symtab; i++) { |
3401 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, | 3563 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, |
3402 | mod, mod->symtab[i].st_value); | 3564 | mod, mod->symtab[i].st_value); |
@@ -3412,6 +3574,7 @@ static char *module_flags(struct module *mod, char *buf) | |||
3412 | { | 3574 | { |
3413 | int bx = 0; | 3575 | int bx = 0; |
3414 | 3576 | ||
3577 | BUG_ON(mod->state == MODULE_STATE_UNFORMED); | ||
3415 | if (mod->taints || | 3578 | if (mod->taints || |
3416 | mod->state == MODULE_STATE_GOING || | 3579 | mod->state == MODULE_STATE_GOING || |
3417 | mod->state == MODULE_STATE_COMING) { | 3580 | mod->state == MODULE_STATE_COMING) { |
@@ -3453,6 +3616,10 @@ static int m_show(struct seq_file *m, void *p) | |||
3453 | struct module *mod = list_entry(p, struct module, list); | 3616 | struct module *mod = list_entry(p, struct module, list); |
3454 | char buf[8]; | 3617 | char buf[8]; |
3455 | 3618 | ||
3619 | /* We always ignore unformed modules. */ | ||
3620 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3621 | return 0; | ||
3622 | |||
3456 | seq_printf(m, "%s %u", | 3623 | seq_printf(m, "%s %u", |
3457 | mod->name, mod->init_size + mod->core_size); | 3624 | mod->name, mod->init_size + mod->core_size); |
3458 | print_unload_info(m, mod); | 3625 | print_unload_info(m, mod); |
@@ -3513,6 +3680,8 @@ const struct exception_table_entry *search_module_extables(unsigned long addr) | |||
3513 | 3680 | ||
3514 | preempt_disable(); | 3681 | preempt_disable(); |
3515 | list_for_each_entry_rcu(mod, &modules, list) { | 3682 | list_for_each_entry_rcu(mod, &modules, list) { |
3683 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3684 | continue; | ||
3516 | if (mod->num_exentries == 0) | 3685 | if (mod->num_exentries == 0) |
3517 | continue; | 3686 | continue; |
3518 | 3687 | ||
@@ -3561,10 +3730,13 @@ struct module *__module_address(unsigned long addr) | |||
3561 | if (addr < module_addr_min || addr > module_addr_max) | 3730 | if (addr < module_addr_min || addr > module_addr_max) |
3562 | return NULL; | 3731 | return NULL; |
3563 | 3732 | ||
3564 | list_for_each_entry_rcu(mod, &modules, list) | 3733 | list_for_each_entry_rcu(mod, &modules, list) { |
3734 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3735 | continue; | ||
3565 | if (within_module_core(addr, mod) | 3736 | if (within_module_core(addr, mod) |
3566 | || within_module_init(addr, mod)) | 3737 | || within_module_init(addr, mod)) |
3567 | return mod; | 3738 | return mod; |
3739 | } | ||
3568 | return NULL; | 3740 | return NULL; |
3569 | } | 3741 | } |
3570 | EXPORT_SYMBOL_GPL(__module_address); | 3742 | EXPORT_SYMBOL_GPL(__module_address); |
@@ -3617,8 +3789,11 @@ void print_modules(void) | |||
3617 | printk(KERN_DEFAULT "Modules linked in:"); | 3789 | printk(KERN_DEFAULT "Modules linked in:"); |
3618 | /* Most callers should already have preempt disabled, but make sure */ | 3790 | /* Most callers should already have preempt disabled, but make sure */ |
3619 | preempt_disable(); | 3791 | preempt_disable(); |
3620 | list_for_each_entry_rcu(mod, &modules, list) | 3792 | list_for_each_entry_rcu(mod, &modules, list) { |
3793 | if (mod->state == MODULE_STATE_UNFORMED) | ||
3794 | continue; | ||
3621 | printk(" %s%s", mod->name, module_flags(mod, buf)); | 3795 | printk(" %s%s", mod->name, module_flags(mod, buf)); |
3796 | } | ||
3622 | preempt_enable(); | 3797 | preempt_enable(); |
3623 | if (last_unloaded_module[0]) | 3798 | if (last_unloaded_module[0]) |
3624 | printk(" [last unloaded: %s]", last_unloaded_module); | 3799 | printk(" [last unloaded: %s]", last_unloaded_module); |