diff options
Diffstat (limited to 'kernel/module.c')
-rw-r--r-- | kernel/module.c | 444 |
1 files changed, 267 insertions, 177 deletions
diff --git a/kernel/module.c b/kernel/module.c index 6e48c3a43599..250092c1d57d 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 |
@@ -372,9 +375,6 @@ static bool check_symbol(const struct symsearch *syms, | |||
372 | printk(KERN_WARNING "Symbol %s is being used " | 375 | printk(KERN_WARNING "Symbol %s is being used " |
373 | "by a non-GPL module, which will not " | 376 | "by a non-GPL module, which will not " |
374 | "be allowed in the future\n", fsa->name); | 377 | "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 | } | 378 | } |
379 | } | 379 | } |
380 | 380 | ||
@@ -2282,7 +2282,7 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2282 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; | 2282 | Elf_Shdr *symsect = info->sechdrs + info->index.sym; |
2283 | Elf_Shdr *strsect = info->sechdrs + info->index.str; | 2283 | Elf_Shdr *strsect = info->sechdrs + info->index.str; |
2284 | const Elf_Sym *src; | 2284 | const Elf_Sym *src; |
2285 | unsigned int i, nsrc, ndst, strtab_size; | 2285 | unsigned int i, nsrc, ndst, strtab_size = 0; |
2286 | 2286 | ||
2287 | /* Put symbol section at end of init part of module. */ | 2287 | /* Put symbol section at end of init part of module. */ |
2288 | symsect->sh_flags |= SHF_ALLOC; | 2288 | symsect->sh_flags |= SHF_ALLOC; |
@@ -2293,9 +2293,6 @@ static void layout_symtab(struct module *mod, struct load_info *info) | |||
2293 | src = (void *)info->hdr + symsect->sh_offset; | 2293 | src = (void *)info->hdr + symsect->sh_offset; |
2294 | nsrc = symsect->sh_size / sizeof(*src); | 2294 | nsrc = symsect->sh_size / sizeof(*src); |
2295 | 2295 | ||
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. */ | 2296 | /* Compute total space required for the core symbols' strtab. */ |
2300 | for (ndst = i = 0; i < nsrc; i++) { | 2297 | for (ndst = i = 0; i < nsrc; i++) { |
2301 | if (i == 0 || | 2298 | if (i == 0 || |
@@ -2337,7 +2334,6 @@ static void add_kallsyms(struct module *mod, const struct load_info *info) | |||
2337 | mod->core_symtab = dst = mod->module_core + info->symoffs; | 2334 | mod->core_symtab = dst = mod->module_core + info->symoffs; |
2338 | mod->core_strtab = s = mod->module_core + info->stroffs; | 2335 | mod->core_strtab = s = mod->module_core + info->stroffs; |
2339 | src = mod->symtab; | 2336 | src = mod->symtab; |
2340 | *s++ = 0; | ||
2341 | for (ndst = i = 0; i < mod->num_symtab; i++) { | 2337 | for (ndst = i = 0; i < mod->num_symtab; i++) { |
2342 | if (i == 0 || | 2338 | if (i == 0 || |
2343 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { | 2339 | is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { |
@@ -2378,7 +2374,7 @@ static void dynamic_debug_remove(struct _ddebug *debug) | |||
2378 | 2374 | ||
2379 | void * __weak module_alloc(unsigned long size) | 2375 | void * __weak module_alloc(unsigned long size) |
2380 | { | 2376 | { |
2381 | return size == 0 ? NULL : vmalloc_exec(size); | 2377 | return vmalloc_exec(size); |
2382 | } | 2378 | } |
2383 | 2379 | ||
2384 | static void *module_alloc_update_bounds(unsigned long size) | 2380 | static void *module_alloc_update_bounds(unsigned long size) |
@@ -2425,18 +2421,17 @@ static inline void kmemleak_load_module(const struct module *mod, | |||
2425 | #endif | 2421 | #endif |
2426 | 2422 | ||
2427 | #ifdef CONFIG_MODULE_SIG | 2423 | #ifdef CONFIG_MODULE_SIG |
2428 | static int module_sig_check(struct load_info *info, | 2424 | static int module_sig_check(struct load_info *info) |
2429 | const void *mod, unsigned long *_len) | ||
2430 | { | 2425 | { |
2431 | int err = -ENOKEY; | 2426 | int err = -ENOKEY; |
2432 | unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; | 2427 | const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; |
2433 | unsigned long len = *_len; | 2428 | const void *mod = info->hdr; |
2434 | 2429 | ||
2435 | if (len > markerlen && | 2430 | if (info->len > markerlen && |
2436 | memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { | 2431 | memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { |
2437 | /* We truncate the module to discard the signature */ | 2432 | /* We truncate the module to discard the signature */ |
2438 | *_len -= markerlen; | 2433 | info->len -= markerlen; |
2439 | err = mod_verify_sig(mod, _len); | 2434 | err = mod_verify_sig(mod, &info->len); |
2440 | } | 2435 | } |
2441 | 2436 | ||
2442 | if (!err) { | 2437 | if (!err) { |
@@ -2454,59 +2449,107 @@ static int module_sig_check(struct load_info *info, | |||
2454 | return err; | 2449 | return err; |
2455 | } | 2450 | } |
2456 | #else /* !CONFIG_MODULE_SIG */ | 2451 | #else /* !CONFIG_MODULE_SIG */ |
2457 | static int module_sig_check(struct load_info *info, | 2452 | static int module_sig_check(struct load_info *info) |
2458 | void *mod, unsigned long *len) | ||
2459 | { | 2453 | { |
2460 | return 0; | 2454 | return 0; |
2461 | } | 2455 | } |
2462 | #endif /* !CONFIG_MODULE_SIG */ | 2456 | #endif /* !CONFIG_MODULE_SIG */ |
2463 | 2457 | ||
2464 | /* Sets info->hdr, info->len and info->sig_ok. */ | 2458 | /* Sanity checks against invalid binaries, wrong arch, weird elf version. */ |
2465 | static int copy_and_check(struct load_info *info, | 2459 | static int elf_header_check(struct load_info *info) |
2466 | const void __user *umod, unsigned long len, | 2460 | { |
2467 | const char __user *uargs) | 2461 | if (info->len < sizeof(*(info->hdr))) |
2462 | return -ENOEXEC; | ||
2463 | |||
2464 | if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0 | ||
2465 | || info->hdr->e_type != ET_REL | ||
2466 | || !elf_check_arch(info->hdr) | ||
2467 | || info->hdr->e_shentsize != sizeof(Elf_Shdr)) | ||
2468 | return -ENOEXEC; | ||
2469 | |||
2470 | if (info->hdr->e_shoff >= info->len | ||
2471 | || (info->hdr->e_shnum * sizeof(Elf_Shdr) > | ||
2472 | info->len - info->hdr->e_shoff)) | ||
2473 | return -ENOEXEC; | ||
2474 | |||
2475 | return 0; | ||
2476 | } | ||
2477 | |||
2478 | /* Sets info->hdr and info->len. */ | ||
2479 | static int copy_module_from_user(const void __user *umod, unsigned long len, | ||
2480 | struct load_info *info) | ||
2468 | { | 2481 | { |
2469 | int err; | 2482 | int err; |
2470 | Elf_Ehdr *hdr; | ||
2471 | 2483 | ||
2472 | if (len < sizeof(*hdr)) | 2484 | info->len = len; |
2485 | if (info->len < sizeof(*(info->hdr))) | ||
2473 | return -ENOEXEC; | 2486 | return -ENOEXEC; |
2474 | 2487 | ||
2488 | err = security_kernel_module_from_file(NULL); | ||
2489 | if (err) | ||
2490 | return err; | ||
2491 | |||
2475 | /* Suck in entire file: we'll want most of it. */ | 2492 | /* Suck in entire file: we'll want most of it. */ |
2476 | if ((hdr = vmalloc(len)) == NULL) | 2493 | info->hdr = vmalloc(info->len); |
2494 | if (!info->hdr) | ||
2477 | return -ENOMEM; | 2495 | return -ENOMEM; |
2478 | 2496 | ||
2479 | if (copy_from_user(hdr, umod, len) != 0) { | 2497 | if (copy_from_user(info->hdr, umod, info->len) != 0) { |
2480 | err = -EFAULT; | 2498 | vfree(info->hdr); |
2481 | goto free_hdr; | 2499 | return -EFAULT; |
2482 | } | 2500 | } |
2483 | 2501 | ||
2484 | err = module_sig_check(info, hdr, &len); | 2502 | return 0; |
2503 | } | ||
2504 | |||
2505 | /* Sets info->hdr and info->len. */ | ||
2506 | static int copy_module_from_fd(int fd, struct load_info *info) | ||
2507 | { | ||
2508 | struct file *file; | ||
2509 | int err; | ||
2510 | struct kstat stat; | ||
2511 | loff_t pos; | ||
2512 | ssize_t bytes = 0; | ||
2513 | |||
2514 | file = fget(fd); | ||
2515 | if (!file) | ||
2516 | return -ENOEXEC; | ||
2517 | |||
2518 | err = security_kernel_module_from_file(file); | ||
2485 | if (err) | 2519 | if (err) |
2486 | goto free_hdr; | 2520 | goto out; |
2487 | 2521 | ||
2488 | /* Sanity checks against insmoding binaries or wrong arch, | 2522 | err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat); |
2489 | weird elf version */ | 2523 | if (err) |
2490 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 | 2524 | 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 | } | ||
2497 | 2525 | ||
2498 | if (hdr->e_shoff >= len || | 2526 | if (stat.size > INT_MAX) { |
2499 | hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) { | 2527 | err = -EFBIG; |
2500 | err = -ENOEXEC; | 2528 | goto out; |
2501 | goto free_hdr; | 2529 | } |
2530 | info->hdr = vmalloc(stat.size); | ||
2531 | if (!info->hdr) { | ||
2532 | err = -ENOMEM; | ||
2533 | goto out; | ||
2502 | } | 2534 | } |
2503 | 2535 | ||
2504 | info->hdr = hdr; | 2536 | pos = 0; |
2505 | info->len = len; | 2537 | while (pos < stat.size) { |
2506 | return 0; | 2538 | bytes = kernel_read(file, pos, (char *)(info->hdr) + pos, |
2539 | stat.size - pos); | ||
2540 | if (bytes < 0) { | ||
2541 | vfree(info->hdr); | ||
2542 | err = bytes; | ||
2543 | goto out; | ||
2544 | } | ||
2545 | if (bytes == 0) | ||
2546 | break; | ||
2547 | pos += bytes; | ||
2548 | } | ||
2549 | info->len = pos; | ||
2507 | 2550 | ||
2508 | free_hdr: | 2551 | out: |
2509 | vfree(hdr); | 2552 | fput(file); |
2510 | return err; | 2553 | return err; |
2511 | } | 2554 | } |
2512 | 2555 | ||
@@ -2515,7 +2558,7 @@ static void free_copy(struct load_info *info) | |||
2515 | vfree(info->hdr); | 2558 | vfree(info->hdr); |
2516 | } | 2559 | } |
2517 | 2560 | ||
2518 | static int rewrite_section_headers(struct load_info *info) | 2561 | static int rewrite_section_headers(struct load_info *info, int flags) |
2519 | { | 2562 | { |
2520 | unsigned int i; | 2563 | unsigned int i; |
2521 | 2564 | ||
@@ -2543,7 +2586,10 @@ static int rewrite_section_headers(struct load_info *info) | |||
2543 | } | 2586 | } |
2544 | 2587 | ||
2545 | /* Track but don't keep modinfo and version sections. */ | 2588 | /* Track but don't keep modinfo and version sections. */ |
2546 | info->index.vers = find_sec(info, "__versions"); | 2589 | if (flags & MODULE_INIT_IGNORE_MODVERSIONS) |
2590 | info->index.vers = 0; /* Pretend no __versions section! */ | ||
2591 | else | ||
2592 | info->index.vers = find_sec(info, "__versions"); | ||
2547 | info->index.info = find_sec(info, ".modinfo"); | 2593 | info->index.info = find_sec(info, ".modinfo"); |
2548 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2594 | info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; |
2549 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; | 2595 | info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; |
@@ -2558,7 +2604,7 @@ static int rewrite_section_headers(struct load_info *info) | |||
2558 | * Return the temporary module pointer (we'll replace it with the final | 2604 | * Return the temporary module pointer (we'll replace it with the final |
2559 | * one when we move the module sections around). | 2605 | * one when we move the module sections around). |
2560 | */ | 2606 | */ |
2561 | static struct module *setup_load_info(struct load_info *info) | 2607 | static struct module *setup_load_info(struct load_info *info, int flags) |
2562 | { | 2608 | { |
2563 | unsigned int i; | 2609 | unsigned int i; |
2564 | int err; | 2610 | int err; |
@@ -2569,7 +2615,7 @@ static struct module *setup_load_info(struct load_info *info) | |||
2569 | info->secstrings = (void *)info->hdr | 2615 | info->secstrings = (void *)info->hdr |
2570 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; | 2616 | + info->sechdrs[info->hdr->e_shstrndx].sh_offset; |
2571 | 2617 | ||
2572 | err = rewrite_section_headers(info); | 2618 | err = rewrite_section_headers(info, flags); |
2573 | if (err) | 2619 | if (err) |
2574 | return ERR_PTR(err); | 2620 | return ERR_PTR(err); |
2575 | 2621 | ||
@@ -2607,11 +2653,14 @@ static struct module *setup_load_info(struct load_info *info) | |||
2607 | return mod; | 2653 | return mod; |
2608 | } | 2654 | } |
2609 | 2655 | ||
2610 | static int check_modinfo(struct module *mod, struct load_info *info) | 2656 | static int check_modinfo(struct module *mod, struct load_info *info, int flags) |
2611 | { | 2657 | { |
2612 | const char *modmagic = get_modinfo(info, "vermagic"); | 2658 | const char *modmagic = get_modinfo(info, "vermagic"); |
2613 | int err; | 2659 | int err; |
2614 | 2660 | ||
2661 | if (flags & MODULE_INIT_IGNORE_VERMAGIC) | ||
2662 | modmagic = NULL; | ||
2663 | |||
2615 | /* This is allowed: modprobe --force will invalidate it. */ | 2664 | /* This is allowed: modprobe --force will invalidate it. */ |
2616 | if (!modmagic) { | 2665 | if (!modmagic) { |
2617 | err = try_to_force_load(mod, "bad vermagic"); | 2666 | err = try_to_force_load(mod, "bad vermagic"); |
@@ -2741,20 +2790,23 @@ static int move_module(struct module *mod, struct load_info *info) | |||
2741 | memset(ptr, 0, mod->core_size); | 2790 | memset(ptr, 0, mod->core_size); |
2742 | mod->module_core = ptr; | 2791 | mod->module_core = ptr; |
2743 | 2792 | ||
2744 | ptr = module_alloc_update_bounds(mod->init_size); | 2793 | if (mod->init_size) { |
2745 | /* | 2794 | ptr = module_alloc_update_bounds(mod->init_size); |
2746 | * The pointer to this block is stored in the module structure | 2795 | /* |
2747 | * which is inside the block. This block doesn't need to be | 2796 | * The pointer to this block is stored in the module structure |
2748 | * scanned as it contains data and code that will be freed | 2797 | * which is inside the block. This block doesn't need to be |
2749 | * after the module is initialized. | 2798 | * scanned as it contains data and code that will be freed |
2750 | */ | 2799 | * after the module is initialized. |
2751 | kmemleak_ignore(ptr); | 2800 | */ |
2752 | if (!ptr && mod->init_size) { | 2801 | kmemleak_ignore(ptr); |
2753 | module_free(mod, mod->module_core); | 2802 | if (!ptr) { |
2754 | return -ENOMEM; | 2803 | module_free(mod, mod->module_core); |
2755 | } | 2804 | return -ENOMEM; |
2756 | memset(ptr, 0, mod->init_size); | 2805 | } |
2757 | mod->module_init = ptr; | 2806 | memset(ptr, 0, mod->init_size); |
2807 | mod->module_init = ptr; | ||
2808 | } else | ||
2809 | mod->module_init = NULL; | ||
2758 | 2810 | ||
2759 | /* Transfer each section which specifies SHF_ALLOC */ | 2811 | /* Transfer each section which specifies SHF_ALLOC */ |
2760 | pr_debug("final section addresses:\n"); | 2812 | pr_debug("final section addresses:\n"); |
@@ -2847,18 +2899,18 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr, | |||
2847 | return 0; | 2899 | return 0; |
2848 | } | 2900 | } |
2849 | 2901 | ||
2850 | static struct module *layout_and_allocate(struct load_info *info) | 2902 | static struct module *layout_and_allocate(struct load_info *info, int flags) |
2851 | { | 2903 | { |
2852 | /* Module within temporary copy. */ | 2904 | /* Module within temporary copy. */ |
2853 | struct module *mod; | 2905 | struct module *mod; |
2854 | Elf_Shdr *pcpusec; | 2906 | Elf_Shdr *pcpusec; |
2855 | int err; | 2907 | int err; |
2856 | 2908 | ||
2857 | mod = setup_load_info(info); | 2909 | mod = setup_load_info(info, flags); |
2858 | if (IS_ERR(mod)) | 2910 | if (IS_ERR(mod)) |
2859 | return mod; | 2911 | return mod; |
2860 | 2912 | ||
2861 | err = check_modinfo(mod, info); | 2913 | err = check_modinfo(mod, info, flags); |
2862 | if (err) | 2914 | if (err) |
2863 | return ERR_PTR(err); | 2915 | return ERR_PTR(err); |
2864 | 2916 | ||
@@ -2945,33 +2997,124 @@ static bool finished_loading(const char *name) | |||
2945 | return ret; | 2997 | return ret; |
2946 | } | 2998 | } |
2947 | 2999 | ||
3000 | /* Call module constructors. */ | ||
3001 | static void do_mod_ctors(struct module *mod) | ||
3002 | { | ||
3003 | #ifdef CONFIG_CONSTRUCTORS | ||
3004 | unsigned long i; | ||
3005 | |||
3006 | for (i = 0; i < mod->num_ctors; i++) | ||
3007 | mod->ctors[i](); | ||
3008 | #endif | ||
3009 | } | ||
3010 | |||
3011 | /* This is where the real work happens */ | ||
3012 | static int do_init_module(struct module *mod) | ||
3013 | { | ||
3014 | int ret = 0; | ||
3015 | |||
3016 | blocking_notifier_call_chain(&module_notify_list, | ||
3017 | MODULE_STATE_COMING, mod); | ||
3018 | |||
3019 | /* Set RO and NX regions for core */ | ||
3020 | set_section_ro_nx(mod->module_core, | ||
3021 | mod->core_text_size, | ||
3022 | mod->core_ro_size, | ||
3023 | mod->core_size); | ||
3024 | |||
3025 | /* Set RO and NX regions for init */ | ||
3026 | set_section_ro_nx(mod->module_init, | ||
3027 | mod->init_text_size, | ||
3028 | mod->init_ro_size, | ||
3029 | mod->init_size); | ||
3030 | |||
3031 | do_mod_ctors(mod); | ||
3032 | /* Start the module */ | ||
3033 | if (mod->init != NULL) | ||
3034 | ret = do_one_initcall(mod->init); | ||
3035 | if (ret < 0) { | ||
3036 | /* Init routine failed: abort. Try to protect us from | ||
3037 | buggy refcounters. */ | ||
3038 | mod->state = MODULE_STATE_GOING; | ||
3039 | synchronize_sched(); | ||
3040 | module_put(mod); | ||
3041 | blocking_notifier_call_chain(&module_notify_list, | ||
3042 | MODULE_STATE_GOING, mod); | ||
3043 | free_module(mod); | ||
3044 | wake_up_all(&module_wq); | ||
3045 | return ret; | ||
3046 | } | ||
3047 | if (ret > 0) { | ||
3048 | printk(KERN_WARNING | ||
3049 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | ||
3050 | "%s: loading module anyway...\n", | ||
3051 | __func__, mod->name, ret, | ||
3052 | __func__); | ||
3053 | dump_stack(); | ||
3054 | } | ||
3055 | |||
3056 | /* Now it's a first class citizen! */ | ||
3057 | mod->state = MODULE_STATE_LIVE; | ||
3058 | blocking_notifier_call_chain(&module_notify_list, | ||
3059 | MODULE_STATE_LIVE, mod); | ||
3060 | |||
3061 | /* We need to finish all async code before the module init sequence is done */ | ||
3062 | async_synchronize_full(); | ||
3063 | |||
3064 | mutex_lock(&module_mutex); | ||
3065 | /* Drop initial reference. */ | ||
3066 | module_put(mod); | ||
3067 | trim_init_extable(mod); | ||
3068 | #ifdef CONFIG_KALLSYMS | ||
3069 | mod->num_symtab = mod->core_num_syms; | ||
3070 | mod->symtab = mod->core_symtab; | ||
3071 | mod->strtab = mod->core_strtab; | ||
3072 | #endif | ||
3073 | unset_module_init_ro_nx(mod); | ||
3074 | module_free(mod, mod->module_init); | ||
3075 | mod->module_init = NULL; | ||
3076 | mod->init_size = 0; | ||
3077 | mod->init_ro_size = 0; | ||
3078 | mod->init_text_size = 0; | ||
3079 | mutex_unlock(&module_mutex); | ||
3080 | wake_up_all(&module_wq); | ||
3081 | |||
3082 | return 0; | ||
3083 | } | ||
3084 | |||
3085 | static int may_init_module(void) | ||
3086 | { | ||
3087 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | ||
3088 | return -EPERM; | ||
3089 | |||
3090 | return 0; | ||
3091 | } | ||
3092 | |||
2948 | /* Allocate and load the module: note that size of section 0 is always | 3093 | /* Allocate and load the module: note that size of section 0 is always |
2949 | zero, and we rely on this for optional sections. */ | 3094 | zero, and we rely on this for optional sections. */ |
2950 | static struct module *load_module(void __user *umod, | 3095 | static int load_module(struct load_info *info, const char __user *uargs, |
2951 | unsigned long len, | 3096 | int flags) |
2952 | const char __user *uargs) | ||
2953 | { | 3097 | { |
2954 | struct load_info info = { NULL, }; | ||
2955 | struct module *mod, *old; | 3098 | struct module *mod, *old; |
2956 | long err; | 3099 | long err; |
2957 | 3100 | ||
2958 | pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", | 3101 | err = module_sig_check(info); |
2959 | umod, len, uargs); | 3102 | if (err) |
3103 | goto free_copy; | ||
2960 | 3104 | ||
2961 | /* Copy in the blobs from userspace, check they are vaguely sane. */ | 3105 | err = elf_header_check(info); |
2962 | err = copy_and_check(&info, umod, len, uargs); | ||
2963 | if (err) | 3106 | if (err) |
2964 | return ERR_PTR(err); | 3107 | goto free_copy; |
2965 | 3108 | ||
2966 | /* Figure out module layout, and allocate all the memory. */ | 3109 | /* Figure out module layout, and allocate all the memory. */ |
2967 | mod = layout_and_allocate(&info); | 3110 | mod = layout_and_allocate(info, flags); |
2968 | if (IS_ERR(mod)) { | 3111 | if (IS_ERR(mod)) { |
2969 | err = PTR_ERR(mod); | 3112 | err = PTR_ERR(mod); |
2970 | goto free_copy; | 3113 | goto free_copy; |
2971 | } | 3114 | } |
2972 | 3115 | ||
2973 | #ifdef CONFIG_MODULE_SIG | 3116 | #ifdef CONFIG_MODULE_SIG |
2974 | mod->sig_ok = info.sig_ok; | 3117 | mod->sig_ok = info->sig_ok; |
2975 | if (!mod->sig_ok) | 3118 | if (!mod->sig_ok) |
2976 | add_taint_module(mod, TAINT_FORCED_MODULE); | 3119 | add_taint_module(mod, TAINT_FORCED_MODULE); |
2977 | #endif | 3120 | #endif |
@@ -2983,25 +3126,25 @@ static struct module *load_module(void __user *umod, | |||
2983 | 3126 | ||
2984 | /* Now we've got everything in the final locations, we can | 3127 | /* Now we've got everything in the final locations, we can |
2985 | * find optional sections. */ | 3128 | * find optional sections. */ |
2986 | find_module_sections(mod, &info); | 3129 | find_module_sections(mod, info); |
2987 | 3130 | ||
2988 | err = check_module_license_and_versions(mod); | 3131 | err = check_module_license_and_versions(mod); |
2989 | if (err) | 3132 | if (err) |
2990 | goto free_unload; | 3133 | goto free_unload; |
2991 | 3134 | ||
2992 | /* Set up MODINFO_ATTR fields */ | 3135 | /* Set up MODINFO_ATTR fields */ |
2993 | setup_modinfo(mod, &info); | 3136 | setup_modinfo(mod, info); |
2994 | 3137 | ||
2995 | /* Fix up syms, so that st_value is a pointer to location. */ | 3138 | /* Fix up syms, so that st_value is a pointer to location. */ |
2996 | err = simplify_symbols(mod, &info); | 3139 | err = simplify_symbols(mod, info); |
2997 | if (err < 0) | 3140 | if (err < 0) |
2998 | goto free_modinfo; | 3141 | goto free_modinfo; |
2999 | 3142 | ||
3000 | err = apply_relocations(mod, &info); | 3143 | err = apply_relocations(mod, info); |
3001 | if (err < 0) | 3144 | if (err < 0) |
3002 | goto free_modinfo; | 3145 | goto free_modinfo; |
3003 | 3146 | ||
3004 | err = post_relocation(mod, &info); | 3147 | err = post_relocation(mod, info); |
3005 | if (err < 0) | 3148 | if (err < 0) |
3006 | goto free_modinfo; | 3149 | goto free_modinfo; |
3007 | 3150 | ||
@@ -3041,14 +3184,14 @@ again: | |||
3041 | } | 3184 | } |
3042 | 3185 | ||
3043 | /* This has to be done once we're sure module name is unique. */ | 3186 | /* This has to be done once we're sure module name is unique. */ |
3044 | dynamic_debug_setup(info.debug, info.num_debug); | 3187 | dynamic_debug_setup(info->debug, info->num_debug); |
3045 | 3188 | ||
3046 | /* Find duplicate symbols */ | 3189 | /* Find duplicate symbols */ |
3047 | err = verify_export_symbols(mod); | 3190 | err = verify_export_symbols(mod); |
3048 | if (err < 0) | 3191 | if (err < 0) |
3049 | goto ddebug; | 3192 | goto ddebug; |
3050 | 3193 | ||
3051 | module_bug_finalize(info.hdr, info.sechdrs, mod); | 3194 | module_bug_finalize(info->hdr, info->sechdrs, mod); |
3052 | list_add_rcu(&mod->list, &modules); | 3195 | list_add_rcu(&mod->list, &modules); |
3053 | mutex_unlock(&module_mutex); | 3196 | mutex_unlock(&module_mutex); |
3054 | 3197 | ||
@@ -3059,16 +3202,17 @@ again: | |||
3059 | goto unlink; | 3202 | goto unlink; |
3060 | 3203 | ||
3061 | /* Link in to syfs. */ | 3204 | /* Link in to syfs. */ |
3062 | err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); | 3205 | err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp); |
3063 | if (err < 0) | 3206 | if (err < 0) |
3064 | goto unlink; | 3207 | goto unlink; |
3065 | 3208 | ||
3066 | /* Get rid of temporary copy. */ | 3209 | /* Get rid of temporary copy. */ |
3067 | free_copy(&info); | 3210 | free_copy(info); |
3068 | 3211 | ||
3069 | /* Done! */ | 3212 | /* Done! */ |
3070 | trace_module_load(mod); | 3213 | trace_module_load(mod); |
3071 | return mod; | 3214 | |
3215 | return do_init_module(mod); | ||
3072 | 3216 | ||
3073 | unlink: | 3217 | unlink: |
3074 | mutex_lock(&module_mutex); | 3218 | mutex_lock(&module_mutex); |
@@ -3077,7 +3221,7 @@ again: | |||
3077 | module_bug_cleanup(mod); | 3221 | module_bug_cleanup(mod); |
3078 | wake_up_all(&module_wq); | 3222 | wake_up_all(&module_wq); |
3079 | ddebug: | 3223 | ddebug: |
3080 | dynamic_debug_remove(info.debug); | 3224 | dynamic_debug_remove(info->debug); |
3081 | unlock: | 3225 | unlock: |
3082 | mutex_unlock(&module_mutex); | 3226 | mutex_unlock(&module_mutex); |
3083 | synchronize_sched(); | 3227 | synchronize_sched(); |
@@ -3089,106 +3233,52 @@ again: | |||
3089 | free_unload: | 3233 | free_unload: |
3090 | module_unload_free(mod); | 3234 | module_unload_free(mod); |
3091 | free_module: | 3235 | free_module: |
3092 | module_deallocate(mod, &info); | 3236 | module_deallocate(mod, info); |
3093 | free_copy: | 3237 | free_copy: |
3094 | free_copy(&info); | 3238 | free_copy(info); |
3095 | return ERR_PTR(err); | 3239 | 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 | } | 3240 | } |
3108 | 3241 | ||
3109 | /* This is where the real work happens */ | ||
3110 | SYSCALL_DEFINE3(init_module, void __user *, umod, | 3242 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
3111 | unsigned long, len, const char __user *, uargs) | 3243 | unsigned long, len, const char __user *, uargs) |
3112 | { | 3244 | { |
3113 | struct module *mod; | 3245 | int err; |
3114 | int ret = 0; | 3246 | struct load_info info = { }; |
3115 | 3247 | ||
3116 | /* Must have permission */ | 3248 | err = may_init_module(); |
3117 | if (!capable(CAP_SYS_MODULE) || modules_disabled) | 3249 | if (err) |
3118 | return -EPERM; | 3250 | return err; |
3119 | 3251 | ||
3120 | /* Do all the hard work */ | 3252 | pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n", |
3121 | mod = load_module(umod, len, uargs); | 3253 | umod, len, uargs); |
3122 | if (IS_ERR(mod)) | ||
3123 | return PTR_ERR(mod); | ||
3124 | 3254 | ||
3125 | blocking_notifier_call_chain(&module_notify_list, | 3255 | err = copy_module_from_user(umod, len, &info); |
3126 | MODULE_STATE_COMING, mod); | 3256 | if (err) |
3257 | return err; | ||
3127 | 3258 | ||
3128 | /* Set RO and NX regions for core */ | 3259 | return load_module(&info, uargs, 0); |
3129 | set_section_ro_nx(mod->module_core, | 3260 | } |
3130 | mod->core_text_size, | ||
3131 | mod->core_ro_size, | ||
3132 | mod->core_size); | ||
3133 | 3261 | ||
3134 | /* Set RO and NX regions for init */ | 3262 | SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |
3135 | set_section_ro_nx(mod->module_init, | 3263 | { |
3136 | mod->init_text_size, | 3264 | int err; |
3137 | mod->init_ro_size, | 3265 | struct load_info info = { }; |
3138 | mod->init_size); | ||
3139 | 3266 | ||
3140 | do_mod_ctors(mod); | 3267 | err = may_init_module(); |
3141 | /* Start the module */ | 3268 | if (err) |
3142 | if (mod->init != NULL) | 3269 | 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 | 3270 | ||
3165 | /* Now it's a first class citizen! */ | 3271 | 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 | 3272 | ||
3170 | /* We need to finish all async code before the module init sequence is done */ | 3273 | if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS |
3171 | async_synchronize_full(); | 3274 | |MODULE_INIT_IGNORE_VERMAGIC)) |
3275 | return -EINVAL; | ||
3172 | 3276 | ||
3173 | mutex_lock(&module_mutex); | 3277 | err = copy_module_from_fd(fd, &info); |
3174 | /* Drop initial reference. */ | 3278 | if (err) |
3175 | module_put(mod); | 3279 | 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 | 3280 | ||
3191 | return 0; | 3281 | return load_module(&info, uargs, flags); |
3192 | } | 3282 | } |
3193 | 3283 | ||
3194 | static inline int within(unsigned long addr, void *start, unsigned long size) | 3284 | static inline int within(unsigned long addr, void *start, unsigned long size) |