aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-12-19 10:55:08 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-12-19 10:55:08 -0500
commit7a684c452e2589f3ddd7e2d466b4f747d3715ad9 (patch)
treefed803e7450770993575b37807ba2195eafd5b0e /kernel
parent7f2de8171ddf28fdb2ca7f9a683ee1207849f718 (diff)
parente10e1774efbdaec54698454200619a03a01e1d64 (diff)
Merge tag 'modules-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux
Pull module update from Rusty Russell: "Nothing all that exciting; a new module-from-fd syscall for those who want to verify the source of the module (ChromeOS) and/or use standard IMA on it or other security hooks." * tag 'modules-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux: MODSIGN: Fix kbuild output when using default extra_certificates MODSIGN: Avoid using .incbin in C source modules: don't hand 0 to vmalloc. module: Remove a extra null character at the top of module->strtab. ASN.1: Use the ASN1_LONG_TAG and ASN1_INDEFINITE_LENGTH constants ASN.1: Define indefinite length marker constant moduleparam: use __UNIQUE_ID() __UNIQUE_ID() MODSIGN: Add modules_sign make target powerpc: add finit_module syscall. ima: support new kernel module syscall add finit_module syscall to asm-generic ARM: add finit_module syscall to ARM security: introduce kernel_module_from_file hook module: add flags arg to sys_finit_module() module: add syscall to load module from fd
Diffstat (limited to 'kernel')
-rw-r--r--kernel/Makefile10
-rw-r--r--kernel/modsign_certificate.S19
-rw-r--r--kernel/modsign_pubkey.c6
-rw-r--r--kernel/module.c441
-rw-r--r--kernel/sys_ni.c1
5 files changed, 294 insertions, 183 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index ac0d533eb7de..6c072b6da239 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -54,7 +54,7 @@ obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
54obj-$(CONFIG_PROVE_LOCKING) += spinlock.o 54obj-$(CONFIG_PROVE_LOCKING) += spinlock.o
55obj-$(CONFIG_UID16) += uid16.o 55obj-$(CONFIG_UID16) += uid16.o
56obj-$(CONFIG_MODULES) += module.o 56obj-$(CONFIG_MODULES) += module.o
57obj-$(CONFIG_MODULE_SIG) += module_signing.o modsign_pubkey.o 57obj-$(CONFIG_MODULE_SIG) += module_signing.o modsign_pubkey.o modsign_certificate.o
58obj-$(CONFIG_KALLSYMS) += kallsyms.o 58obj-$(CONFIG_KALLSYMS) += kallsyms.o
59obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o 59obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
60obj-$(CONFIG_KEXEC) += kexec.o 60obj-$(CONFIG_KEXEC) += kexec.o
@@ -137,10 +137,14 @@ ifeq ($(CONFIG_MODULE_SIG),y)
137# 137#
138# Pull the signing certificate and any extra certificates into the kernel 138# Pull the signing certificate and any extra certificates into the kernel
139# 139#
140
141quiet_cmd_touch = TOUCH $@
142 cmd_touch = touch $@
143
140extra_certificates: 144extra_certificates:
141 touch $@ 145 $(call cmd,touch)
142 146
143kernel/modsign_pubkey.o: signing_key.x509 extra_certificates 147kernel/modsign_certificate.o: signing_key.x509 extra_certificates
144 148
145############################################################################### 149###############################################################################
146# 150#
diff --git a/kernel/modsign_certificate.S b/kernel/modsign_certificate.S
new file mode 100644
index 000000000000..246b4c6e6135
--- /dev/null
+++ b/kernel/modsign_certificate.S
@@ -0,0 +1,19 @@
1/* SYMBOL_PREFIX defined on commandline from CONFIG_SYMBOL_PREFIX */
2#ifndef SYMBOL_PREFIX
3#define ASM_SYMBOL(sym) sym
4#else
5#define PASTE2(x,y) x##y
6#define PASTE(x,y) PASTE2(x,y)
7#define ASM_SYMBOL(sym) PASTE(SYMBOL_PREFIX, sym)
8#endif
9
10#define GLOBAL(name) \
11 .globl ASM_SYMBOL(name); \
12 ASM_SYMBOL(name):
13
14 .section ".init.data","aw"
15
16GLOBAL(modsign_certificate_list)
17 .incbin "signing_key.x509"
18 .incbin "extra_certificates"
19GLOBAL(modsign_certificate_list_end)
diff --git a/kernel/modsign_pubkey.c b/kernel/modsign_pubkey.c
index 767e559dfb10..045504fffbb2 100644
--- a/kernel/modsign_pubkey.c
+++ b/kernel/modsign_pubkey.c
@@ -20,12 +20,6 @@ struct key *modsign_keyring;
20 20
21extern __initdata const u8 modsign_certificate_list[]; 21extern __initdata const u8 modsign_certificate_list[];
22extern __initdata const u8 modsign_certificate_list_end[]; 22extern __initdata const u8 modsign_certificate_list_end[];
23asm(".section .init.data,\"aw\"\n"
24 SYMBOL_PREFIX "modsign_certificate_list:\n"
25 ".incbin \"signing_key.x509\"\n"
26 ".incbin \"extra_certificates\"\n"
27 SYMBOL_PREFIX "modsign_certificate_list_end:"
28 );
29 23
30/* 24/*
31 * We need to make sure ccache doesn't cache the .o file as it doesn't notice 25 * We need to make sure ccache doesn't cache the .o file as it doesn't notice
diff --git a/kernel/module.c b/kernel/module.c
index 808bd62e1723..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
@@ -2279,7 +2282,7 @@ static void layout_symtab(struct module *mod, struct load_info *info)
2279 Elf_Shdr *symsect = info->sechdrs + info->index.sym; 2282 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2280 Elf_Shdr *strsect = info->sechdrs + info->index.str; 2283 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2281 const Elf_Sym *src; 2284 const Elf_Sym *src;
2282 unsigned int i, nsrc, ndst, strtab_size; 2285 unsigned int i, nsrc, ndst, strtab_size = 0;
2283 2286
2284 /* Put symbol section at end of init part of module. */ 2287 /* Put symbol section at end of init part of module. */
2285 symsect->sh_flags |= SHF_ALLOC; 2288 symsect->sh_flags |= SHF_ALLOC;
@@ -2290,9 +2293,6 @@ static void layout_symtab(struct module *mod, struct load_info *info)
2290 src = (void *)info->hdr + symsect->sh_offset; 2293 src = (void *)info->hdr + symsect->sh_offset;
2291 nsrc = symsect->sh_size / sizeof(*src); 2294 nsrc = symsect->sh_size / sizeof(*src);
2292 2295
2293 /* strtab always starts with a nul, so offset 0 is the empty string. */
2294 strtab_size = 1;
2295
2296 /* Compute total space required for the core symbols' strtab. */ 2296 /* Compute total space required for the core symbols' strtab. */
2297 for (ndst = i = 0; i < nsrc; i++) { 2297 for (ndst = i = 0; i < nsrc; i++) {
2298 if (i == 0 || 2298 if (i == 0 ||
@@ -2334,7 +2334,6 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
2334 mod->core_symtab = dst = mod->module_core + info->symoffs; 2334 mod->core_symtab = dst = mod->module_core + info->symoffs;
2335 mod->core_strtab = s = mod->module_core + info->stroffs; 2335 mod->core_strtab = s = mod->module_core + info->stroffs;
2336 src = mod->symtab; 2336 src = mod->symtab;
2337 *s++ = 0;
2338 for (ndst = i = 0; i < mod->num_symtab; i++) { 2337 for (ndst = i = 0; i < mod->num_symtab; i++) {
2339 if (i == 0 || 2338 if (i == 0 ||
2340 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) { 2339 is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
@@ -2375,7 +2374,7 @@ static void dynamic_debug_remove(struct _ddebug *debug)
2375 2374
2376void * __weak module_alloc(unsigned long size) 2375void * __weak module_alloc(unsigned long size)
2377{ 2376{
2378 return size == 0 ? NULL : vmalloc_exec(size); 2377 return vmalloc_exec(size);
2379} 2378}
2380 2379
2381static void *module_alloc_update_bounds(unsigned long size) 2380static void *module_alloc_update_bounds(unsigned long size)
@@ -2422,18 +2421,17 @@ static inline void kmemleak_load_module(const struct module *mod,
2422#endif 2421#endif
2423 2422
2424#ifdef CONFIG_MODULE_SIG 2423#ifdef CONFIG_MODULE_SIG
2425static int module_sig_check(struct load_info *info, 2424static int module_sig_check(struct load_info *info)
2426 const void *mod, unsigned long *_len)
2427{ 2425{
2428 int err = -ENOKEY; 2426 int err = -ENOKEY;
2429 unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; 2427 const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2430 unsigned long len = *_len; 2428 const void *mod = info->hdr;
2431 2429
2432 if (len > markerlen && 2430 if (info->len > markerlen &&
2433 memcmp(mod + len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { 2431 memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
2434 /* We truncate the module to discard the signature */ 2432 /* We truncate the module to discard the signature */
2435 *_len -= markerlen; 2433 info->len -= markerlen;
2436 err = mod_verify_sig(mod, _len); 2434 err = mod_verify_sig(mod, &info->len);
2437 } 2435 }
2438 2436
2439 if (!err) { 2437 if (!err) {
@@ -2451,59 +2449,107 @@ static int module_sig_check(struct load_info *info,
2451 return err; 2449 return err;
2452} 2450}
2453#else /* !CONFIG_MODULE_SIG */ 2451#else /* !CONFIG_MODULE_SIG */
2454static int module_sig_check(struct load_info *info, 2452static int module_sig_check(struct load_info *info)
2455 void *mod, unsigned long *len)
2456{ 2453{
2457 return 0; 2454 return 0;
2458} 2455}
2459#endif /* !CONFIG_MODULE_SIG */ 2456#endif /* !CONFIG_MODULE_SIG */
2460 2457
2461/* Sets info->hdr, info->len and info->sig_ok. */ 2458/* Sanity checks against invalid binaries, wrong arch, weird elf version. */
2462static int copy_and_check(struct load_info *info, 2459static int elf_header_check(struct load_info *info)
2463 const void __user *umod, unsigned long len, 2460{
2464 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. */
2479static int copy_module_from_user(const void __user *umod, unsigned long len,
2480 struct load_info *info)
2465{ 2481{
2466 int err; 2482 int err;
2467 Elf_Ehdr *hdr;
2468 2483
2469 if (len < sizeof(*hdr)) 2484 info->len = len;
2485 if (info->len < sizeof(*(info->hdr)))
2470 return -ENOEXEC; 2486 return -ENOEXEC;
2471 2487
2488 err = security_kernel_module_from_file(NULL);
2489 if (err)
2490 return err;
2491
2472 /* Suck in entire file: we'll want most of it. */ 2492 /* Suck in entire file: we'll want most of it. */
2473 if ((hdr = vmalloc(len)) == NULL) 2493 info->hdr = vmalloc(info->len);
2494 if (!info->hdr)
2474 return -ENOMEM; 2495 return -ENOMEM;
2475 2496
2476 if (copy_from_user(hdr, umod, len) != 0) { 2497 if (copy_from_user(info->hdr, umod, info->len) != 0) {
2477 err = -EFAULT; 2498 vfree(info->hdr);
2478 goto free_hdr; 2499 return -EFAULT;
2479 } 2500 }
2480 2501
2481 err = module_sig_check(info, hdr, &len); 2502 return 0;
2503}
2504
2505/* Sets info->hdr and info->len. */
2506static 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);
2482 if (err) 2519 if (err)
2483 goto free_hdr; 2520 goto out;
2484 2521
2485 /* Sanity checks against insmoding binaries or wrong arch, 2522 err = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
2486 weird elf version */ 2523 if (err)
2487 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 2524 goto out;
2488 || hdr->e_type != ET_REL
2489 || !elf_check_arch(hdr)
2490 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2491 err = -ENOEXEC;
2492 goto free_hdr;
2493 }
2494 2525
2495 if (hdr->e_shoff >= len || 2526 if (stat.size > INT_MAX) {
2496 hdr->e_shnum * sizeof(Elf_Shdr) > len - hdr->e_shoff) { 2527 err = -EFBIG;
2497 err = -ENOEXEC; 2528 goto out;
2498 goto free_hdr; 2529 }
2530 info->hdr = vmalloc(stat.size);
2531 if (!info->hdr) {
2532 err = -ENOMEM;
2533 goto out;
2499 } 2534 }
2500 2535
2501 info->hdr = hdr; 2536 pos = 0;
2502 info->len = len; 2537 while (pos < stat.size) {
2503 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;
2504 2550
2505free_hdr: 2551out:
2506 vfree(hdr); 2552 fput(file);
2507 return err; 2553 return err;
2508} 2554}
2509 2555
@@ -2512,7 +2558,7 @@ static void free_copy(struct load_info *info)
2512 vfree(info->hdr); 2558 vfree(info->hdr);
2513} 2559}
2514 2560
2515static int rewrite_section_headers(struct load_info *info) 2561static int rewrite_section_headers(struct load_info *info, int flags)
2516{ 2562{
2517 unsigned int i; 2563 unsigned int i;
2518 2564
@@ -2540,7 +2586,10 @@ static int rewrite_section_headers(struct load_info *info)
2540 } 2586 }
2541 2587
2542 /* Track but don't keep modinfo and version sections. */ 2588 /* Track but don't keep modinfo and version sections. */
2543 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");
2544 info->index.info = find_sec(info, ".modinfo"); 2593 info->index.info = find_sec(info, ".modinfo");
2545 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; 2594 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2546 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; 2595 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
@@ -2555,7 +2604,7 @@ static int rewrite_section_headers(struct load_info *info)
2555 * 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
2556 * one when we move the module sections around). 2605 * one when we move the module sections around).
2557 */ 2606 */
2558static struct module *setup_load_info(struct load_info *info) 2607static struct module *setup_load_info(struct load_info *info, int flags)
2559{ 2608{
2560 unsigned int i; 2609 unsigned int i;
2561 int err; 2610 int err;
@@ -2566,7 +2615,7 @@ static struct module *setup_load_info(struct load_info *info)
2566 info->secstrings = (void *)info->hdr 2615 info->secstrings = (void *)info->hdr
2567 + info->sechdrs[info->hdr->e_shstrndx].sh_offset; 2616 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2568 2617
2569 err = rewrite_section_headers(info); 2618 err = rewrite_section_headers(info, flags);
2570 if (err) 2619 if (err)
2571 return ERR_PTR(err); 2620 return ERR_PTR(err);
2572 2621
@@ -2604,11 +2653,14 @@ static struct module *setup_load_info(struct load_info *info)
2604 return mod; 2653 return mod;
2605} 2654}
2606 2655
2607static int check_modinfo(struct module *mod, struct load_info *info) 2656static int check_modinfo(struct module *mod, struct load_info *info, int flags)
2608{ 2657{
2609 const char *modmagic = get_modinfo(info, "vermagic"); 2658 const char *modmagic = get_modinfo(info, "vermagic");
2610 int err; 2659 int err;
2611 2660
2661 if (flags & MODULE_INIT_IGNORE_VERMAGIC)
2662 modmagic = NULL;
2663
2612 /* This is allowed: modprobe --force will invalidate it. */ 2664 /* This is allowed: modprobe --force will invalidate it. */
2613 if (!modmagic) { 2665 if (!modmagic) {
2614 err = try_to_force_load(mod, "bad vermagic"); 2666 err = try_to_force_load(mod, "bad vermagic");
@@ -2738,20 +2790,23 @@ static int move_module(struct module *mod, struct load_info *info)
2738 memset(ptr, 0, mod->core_size); 2790 memset(ptr, 0, mod->core_size);
2739 mod->module_core = ptr; 2791 mod->module_core = ptr;
2740 2792
2741 ptr = module_alloc_update_bounds(mod->init_size); 2793 if (mod->init_size) {
2742 /* 2794 ptr = module_alloc_update_bounds(mod->init_size);
2743 * The pointer to this block is stored in the module structure 2795 /*
2744 * which is inside the block. This block doesn't need to be 2796 * The pointer to this block is stored in the module structure
2745 * scanned as it contains data and code that will be freed 2797 * which is inside the block. This block doesn't need to be
2746 * after the module is initialized. 2798 * scanned as it contains data and code that will be freed
2747 */ 2799 * after the module is initialized.
2748 kmemleak_ignore(ptr); 2800 */
2749 if (!ptr && mod->init_size) { 2801 kmemleak_ignore(ptr);
2750 module_free(mod, mod->module_core); 2802 if (!ptr) {
2751 return -ENOMEM; 2803 module_free(mod, mod->module_core);
2752 } 2804 return -ENOMEM;
2753 memset(ptr, 0, mod->init_size); 2805 }
2754 mod->module_init = ptr; 2806 memset(ptr, 0, mod->init_size);
2807 mod->module_init = ptr;
2808 } else
2809 mod->module_init = NULL;
2755 2810
2756 /* Transfer each section which specifies SHF_ALLOC */ 2811 /* Transfer each section which specifies SHF_ALLOC */
2757 pr_debug("final section addresses:\n"); 2812 pr_debug("final section addresses:\n");
@@ -2844,18 +2899,18 @@ int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2844 return 0; 2899 return 0;
2845} 2900}
2846 2901
2847static struct module *layout_and_allocate(struct load_info *info) 2902static struct module *layout_and_allocate(struct load_info *info, int flags)
2848{ 2903{
2849 /* Module within temporary copy. */ 2904 /* Module within temporary copy. */
2850 struct module *mod; 2905 struct module *mod;
2851 Elf_Shdr *pcpusec; 2906 Elf_Shdr *pcpusec;
2852 int err; 2907 int err;
2853 2908
2854 mod = setup_load_info(info); 2909 mod = setup_load_info(info, flags);
2855 if (IS_ERR(mod)) 2910 if (IS_ERR(mod))
2856 return mod; 2911 return mod;
2857 2912
2858 err = check_modinfo(mod, info); 2913 err = check_modinfo(mod, info, flags);
2859 if (err) 2914 if (err)
2860 return ERR_PTR(err); 2915 return ERR_PTR(err);
2861 2916
@@ -2942,33 +2997,124 @@ static bool finished_loading(const char *name)
2942 return ret; 2997 return ret;
2943} 2998}
2944 2999
3000/* Call module constructors. */
3001static 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 */
3012static 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
3085static int may_init_module(void)
3086{
3087 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3088 return -EPERM;
3089
3090 return 0;
3091}
3092
2945/* 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
2946 zero, and we rely on this for optional sections. */ 3094 zero, and we rely on this for optional sections. */
2947static struct module *load_module(void __user *umod, 3095static int load_module(struct load_info *info, const char __user *uargs,
2948 unsigned long len, 3096 int flags)
2949 const char __user *uargs)
2950{ 3097{
2951 struct load_info info = { NULL, };
2952 struct module *mod, *old; 3098 struct module *mod, *old;
2953 long err; 3099 long err;
2954 3100
2955 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", 3101 err = module_sig_check(info);
2956 umod, len, uargs); 3102 if (err)
3103 goto free_copy;
2957 3104
2958 /* Copy in the blobs from userspace, check they are vaguely sane. */ 3105 err = elf_header_check(info);
2959 err = copy_and_check(&info, umod, len, uargs);
2960 if (err) 3106 if (err)
2961 return ERR_PTR(err); 3107 goto free_copy;
2962 3108
2963 /* Figure out module layout, and allocate all the memory. */ 3109 /* Figure out module layout, and allocate all the memory. */
2964 mod = layout_and_allocate(&info); 3110 mod = layout_and_allocate(info, flags);
2965 if (IS_ERR(mod)) { 3111 if (IS_ERR(mod)) {
2966 err = PTR_ERR(mod); 3112 err = PTR_ERR(mod);
2967 goto free_copy; 3113 goto free_copy;
2968 } 3114 }
2969 3115
2970#ifdef CONFIG_MODULE_SIG 3116#ifdef CONFIG_MODULE_SIG
2971 mod->sig_ok = info.sig_ok; 3117 mod->sig_ok = info->sig_ok;
2972 if (!mod->sig_ok) 3118 if (!mod->sig_ok)
2973 add_taint_module(mod, TAINT_FORCED_MODULE); 3119 add_taint_module(mod, TAINT_FORCED_MODULE);
2974#endif 3120#endif
@@ -2980,25 +3126,25 @@ static struct module *load_module(void __user *umod,
2980 3126
2981 /* Now we've got everything in the final locations, we can 3127 /* Now we've got everything in the final locations, we can
2982 * find optional sections. */ 3128 * find optional sections. */
2983 find_module_sections(mod, &info); 3129 find_module_sections(mod, info);
2984 3130
2985 err = check_module_license_and_versions(mod); 3131 err = check_module_license_and_versions(mod);
2986 if (err) 3132 if (err)
2987 goto free_unload; 3133 goto free_unload;
2988 3134
2989 /* Set up MODINFO_ATTR fields */ 3135 /* Set up MODINFO_ATTR fields */
2990 setup_modinfo(mod, &info); 3136 setup_modinfo(mod, info);
2991 3137
2992 /* 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. */
2993 err = simplify_symbols(mod, &info); 3139 err = simplify_symbols(mod, info);
2994 if (err < 0) 3140 if (err < 0)
2995 goto free_modinfo; 3141 goto free_modinfo;
2996 3142
2997 err = apply_relocations(mod, &info); 3143 err = apply_relocations(mod, info);
2998 if (err < 0) 3144 if (err < 0)
2999 goto free_modinfo; 3145 goto free_modinfo;
3000 3146
3001 err = post_relocation(mod, &info); 3147 err = post_relocation(mod, info);
3002 if (err < 0) 3148 if (err < 0)
3003 goto free_modinfo; 3149 goto free_modinfo;
3004 3150
@@ -3038,14 +3184,14 @@ again:
3038 } 3184 }
3039 3185
3040 /* 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. */
3041 dynamic_debug_setup(info.debug, info.num_debug); 3187 dynamic_debug_setup(info->debug, info->num_debug);
3042 3188
3043 /* Find duplicate symbols */ 3189 /* Find duplicate symbols */
3044 err = verify_export_symbols(mod); 3190 err = verify_export_symbols(mod);
3045 if (err < 0) 3191 if (err < 0)
3046 goto ddebug; 3192 goto ddebug;
3047 3193
3048 module_bug_finalize(info.hdr, info.sechdrs, mod); 3194 module_bug_finalize(info->hdr, info->sechdrs, mod);
3049 list_add_rcu(&mod->list, &modules); 3195 list_add_rcu(&mod->list, &modules);
3050 mutex_unlock(&module_mutex); 3196 mutex_unlock(&module_mutex);
3051 3197
@@ -3056,16 +3202,17 @@ again:
3056 goto unlink; 3202 goto unlink;
3057 3203
3058 /* Link in to syfs. */ 3204 /* Link in to syfs. */
3059 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp); 3205 err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
3060 if (err < 0) 3206 if (err < 0)
3061 goto unlink; 3207 goto unlink;
3062 3208
3063 /* Get rid of temporary copy. */ 3209 /* Get rid of temporary copy. */
3064 free_copy(&info); 3210 free_copy(info);
3065 3211
3066 /* Done! */ 3212 /* Done! */
3067 trace_module_load(mod); 3213 trace_module_load(mod);
3068 return mod; 3214
3215 return do_init_module(mod);
3069 3216
3070 unlink: 3217 unlink:
3071 mutex_lock(&module_mutex); 3218 mutex_lock(&module_mutex);
@@ -3074,7 +3221,7 @@ again:
3074 module_bug_cleanup(mod); 3221 module_bug_cleanup(mod);
3075 wake_up_all(&module_wq); 3222 wake_up_all(&module_wq);
3076 ddebug: 3223 ddebug:
3077 dynamic_debug_remove(info.debug); 3224 dynamic_debug_remove(info->debug);
3078 unlock: 3225 unlock:
3079 mutex_unlock(&module_mutex); 3226 mutex_unlock(&module_mutex);
3080 synchronize_sched(); 3227 synchronize_sched();
@@ -3086,106 +3233,52 @@ again:
3086 free_unload: 3233 free_unload:
3087 module_unload_free(mod); 3234 module_unload_free(mod);
3088 free_module: 3235 free_module:
3089 module_deallocate(mod, &info); 3236 module_deallocate(mod, info);
3090 free_copy: 3237 free_copy:
3091 free_copy(&info); 3238 free_copy(info);
3092 return ERR_PTR(err); 3239 return err;
3093}
3094
3095/* Call module constructors. */
3096static void do_mod_ctors(struct module *mod)
3097{
3098#ifdef CONFIG_CONSTRUCTORS
3099 unsigned long i;
3100
3101 for (i = 0; i < mod->num_ctors; i++)
3102 mod->ctors[i]();
3103#endif
3104} 3240}
3105 3241
3106/* This is where the real work happens */
3107SYSCALL_DEFINE3(init_module, void __user *, umod, 3242SYSCALL_DEFINE3(init_module, void __user *, umod,
3108 unsigned long, len, const char __user *, uargs) 3243 unsigned long, len, const char __user *, uargs)
3109{ 3244{
3110 struct module *mod; 3245 int err;
3111 int ret = 0; 3246 struct load_info info = { };
3112 3247
3113 /* Must have permission */ 3248 err = may_init_module();
3114 if (!capable(CAP_SYS_MODULE) || modules_disabled) 3249 if (err)
3115 return -EPERM; 3250 return err;
3116 3251
3117 /* Do all the hard work */ 3252 pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
3118 mod = load_module(umod, len, uargs); 3253 umod, len, uargs);
3119 if (IS_ERR(mod))
3120 return PTR_ERR(mod);
3121 3254
3122 blocking_notifier_call_chain(&module_notify_list, 3255 err = copy_module_from_user(umod, len, &info);
3123 MODULE_STATE_COMING, mod); 3256 if (err)
3257 return err;
3124 3258
3125 /* Set RO and NX regions for core */ 3259 return load_module(&info, uargs, 0);
3126 set_section_ro_nx(mod->module_core, 3260}
3127 mod->core_text_size,
3128 mod->core_ro_size,
3129 mod->core_size);
3130 3261
3131 /* Set RO and NX regions for init */ 3262SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
3132 set_section_ro_nx(mod->module_init, 3263{
3133 mod->init_text_size, 3264 int err;
3134 mod->init_ro_size, 3265 struct load_info info = { };
3135 mod->init_size);
3136 3266
3137 do_mod_ctors(mod); 3267 err = may_init_module();
3138 /* Start the module */ 3268 if (err)
3139 if (mod->init != NULL) 3269 return err;
3140 ret = do_one_initcall(mod->init);
3141 if (ret < 0) {
3142 /* Init routine failed: abort. Try to protect us from
3143 buggy refcounters. */
3144 mod->state = MODULE_STATE_GOING;
3145 synchronize_sched();
3146 module_put(mod);
3147 blocking_notifier_call_chain(&module_notify_list,
3148 MODULE_STATE_GOING, mod);
3149 free_module(mod);
3150 wake_up_all(&module_wq);
3151 return ret;
3152 }
3153 if (ret > 0) {
3154 printk(KERN_WARNING
3155"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3156"%s: loading module anyway...\n",
3157 __func__, mod->name, ret,
3158 __func__);
3159 dump_stack();
3160 }
3161 3270
3162 /* Now it's a first class citizen! */ 3271 pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
3163 mod->state = MODULE_STATE_LIVE;
3164 blocking_notifier_call_chain(&module_notify_list,
3165 MODULE_STATE_LIVE, mod);
3166 3272
3167 /* We need to finish all async code before the module init sequence is done */ 3273 if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
3168 async_synchronize_full(); 3274 |MODULE_INIT_IGNORE_VERMAGIC))
3275 return -EINVAL;
3169 3276
3170 mutex_lock(&module_mutex); 3277 err = copy_module_from_fd(fd, &info);
3171 /* Drop initial reference. */ 3278 if (err)
3172 module_put(mod); 3279 return err;
3173 trim_init_extable(mod);
3174#ifdef CONFIG_KALLSYMS
3175 mod->num_symtab = mod->core_num_syms;
3176 mod->symtab = mod->core_symtab;
3177 mod->strtab = mod->core_strtab;
3178#endif
3179 unset_module_init_ro_nx(mod);
3180 module_free(mod, mod->module_init);
3181 mod->module_init = NULL;
3182 mod->init_size = 0;
3183 mod->init_ro_size = 0;
3184 mod->init_text_size = 0;
3185 mutex_unlock(&module_mutex);
3186 wake_up_all(&module_wq);
3187 3280
3188 return 0; 3281 return load_module(&info, uargs, flags);
3189} 3282}
3190 3283
3191static inline int within(unsigned long addr, void *start, unsigned long size) 3284static inline int within(unsigned long addr, void *start, unsigned long size)
diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
index dbff751e4086..395084d4ce16 100644
--- a/kernel/sys_ni.c
+++ b/kernel/sys_ni.c
@@ -25,6 +25,7 @@ cond_syscall(sys_swapoff);
25cond_syscall(sys_kexec_load); 25cond_syscall(sys_kexec_load);
26cond_syscall(compat_sys_kexec_load); 26cond_syscall(compat_sys_kexec_load);
27cond_syscall(sys_init_module); 27cond_syscall(sys_init_module);
28cond_syscall(sys_finit_module);
28cond_syscall(sys_delete_module); 29cond_syscall(sys_delete_module);
29cond_syscall(sys_socketpair); 30cond_syscall(sys_socketpair);
30cond_syscall(sys_bind); 31cond_syscall(sys_bind);