module: add load_info
[deliverable/linux.git] / kernel / module.c
index 241bc41dd6bef796ce7402c443d40809d26f5632..cb40a4e64a0eaeda7c4f8b09b7e00851bef74d81 100644 (file)
@@ -227,7 +227,7 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
                            unsigned int symnum, void *data), void *data)
 {
        struct module *mod;
-       const struct symsearch arr[] = {
+       static const struct symsearch arr[] = {
                { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
                  NOT_GPL_ONLY, false },
                { __start___ksymtab_gpl, __stop___ksymtab_gpl,
@@ -1698,6 +1698,39 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
        return ret;
 }
 
+static int apply_relocations(struct module *mod,
+                            Elf_Ehdr *hdr,
+                            Elf_Shdr *sechdrs,
+                            unsigned int symindex,
+                            unsigned int strindex)
+{
+       unsigned int i;
+       int err = 0;
+
+       /* Now do relocations. */
+       for (i = 1; i < hdr->e_shnum; i++) {
+               const char *strtab = (char *)sechdrs[strindex].sh_addr;
+               unsigned int info = sechdrs[i].sh_info;
+
+               /* Not a valid relocation section? */
+               if (info >= hdr->e_shnum)
+                       continue;
+
+               /* Don't bother with non-allocated sections */
+               if (!(sechdrs[info].sh_flags & SHF_ALLOC))
+                       continue;
+
+               if (sechdrs[i].sh_type == SHT_REL)
+                       err = apply_relocate(sechdrs, strtab, symindex, i, mod);
+               else if (sechdrs[i].sh_type == SHT_RELA)
+                       err = apply_relocate_add(sechdrs, strtab, symindex, i,
+                                                mod);
+               if (err < 0)
+                       break;
+       }
+       return err;
+}
+
 /* Additional bytes needed by arch in front of individual sections */
 unsigned int __weak arch_mod_section_prepend(struct module *mod,
                                             unsigned int section)
@@ -2115,8 +2148,17 @@ static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
 }
 #endif
 
-static int copy_and_check(Elf_Ehdr **hdrp,
-                         const void __user *umod, unsigned long len)
+struct load_info {
+       Elf_Ehdr *hdr;
+       unsigned long len;
+       Elf_Shdr *sechdrs;
+       char *secstrings, *args, *strtab;
+       struct {
+               unsigned int sym, str, mod, vers, info, pcpu;
+       } index;
+};
+
+static int copy_and_check(struct load_info *info, const void __user *umod, unsigned long len)
 {
        int err;
        Elf_Ehdr *hdr;
@@ -2126,7 +2168,7 @@ static int copy_and_check(Elf_Ehdr **hdrp,
 
        /* Suck in entire file: we'll want most of it. */
        /* vmalloc barfs on "unusual" numbers.  Check here */
-       if (len > 64 * 1024 * 1024 || (hdr = *hdrp = vmalloc(len)) == NULL)
+       if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
                return -ENOMEM;
 
        if (copy_from_user(hdr, umod, len) != 0) {
@@ -2148,6 +2190,8 @@ static int copy_and_check(Elf_Ehdr **hdrp,
                err = -ENOEXEC;
                goto free_hdr;
        }
+       info->hdr = hdr;
+       info->len = len;
        return 0;
 
 free_hdr:
@@ -2155,6 +2199,80 @@ free_hdr:
        return err;
 }
 
+/*
+ * Set up our basic convenience variables (pointers to section headers,
+ * search for module section index etc), and do some basic section
+ * verification.
+ *
+ * Return the temporary module pointer (we'll replace it with the final
+ * one when we move the module sections around).
+ */
+static struct module *setup_load_info(struct load_info *info)
+{
+       unsigned int i;
+       struct module *mod;
+
+       /* Set up the convenience variables */
+       info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
+       info->secstrings = (void *)info->hdr + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
+       info->sechdrs[0].sh_addr = 0;
+
+       for (i = 1; i < info->hdr->e_shnum; i++) {
+               if (info->sechdrs[i].sh_type != SHT_NOBITS
+                   && info->len < info->sechdrs[i].sh_offset + info->sechdrs[i].sh_size)
+                       goto truncated;
+
+               /* Mark all sections sh_addr with their address in the
+                  temporary image. */
+               info->sechdrs[i].sh_addr = (size_t)info->hdr + info->sechdrs[i].sh_offset;
+
+               /* Internal symbols and strings. */
+               if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
+                       info->index.sym = i;
+                       info->index.str = info->sechdrs[i].sh_link;
+                       info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;
+               }
+#ifndef CONFIG_MODULE_UNLOAD
+               /* Don't load .exit sections */
+               if (strstarts(info->secstrings+info->sechdrs[i].sh_name, ".exit"))
+                       info->sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
+#endif
+       }
+
+       info->index.mod = find_sec(info->hdr, info->sechdrs, info->secstrings,
+                           ".gnu.linkonce.this_module");
+       if (!info->index.mod) {
+               printk(KERN_WARNING "No module found in object\n");
+               return ERR_PTR(-ENOEXEC);
+       }
+       /* This is temporary: point mod into copy of data. */
+       mod = (void *)info->sechdrs[info->index.mod].sh_addr;
+
+       if (info->index.sym == 0) {
+               printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
+                      mod->name);
+               return ERR_PTR(-ENOEXEC);
+       }
+
+       info->index.vers = find_sec(info->hdr, info->sechdrs, info->secstrings, "__versions");
+       info->index.info = find_sec(info->hdr, info->sechdrs, info->secstrings, ".modinfo");
+       info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
+
+       /* Don't keep modinfo and version sections. */
+       info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
+       info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+
+       /* Check module struct version now, before we try to use module. */
+       if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
+               return ERR_PTR(-ENOEXEC);
+
+       return mod;
+
+ truncated:
+       printk(KERN_ERR "Module len %lu truncated\n", info->len);
+       return ERR_PTR(-ENOEXEC);
+}
+
 static int check_modinfo(struct module *mod,
                         const Elf_Shdr *sechdrs,
                         unsigned int infoindex, unsigned int versindex)
@@ -2179,6 +2297,10 @@ static int check_modinfo(struct module *mod,
                       " the quality is unknown, you have been warned.\n",
                       mod->name);
        }
+
+       /* Set up license info based on the info section */
+       set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
+
        return 0;
 }
 
@@ -2245,6 +2367,10 @@ static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
                                             sizeof(*mod->ftrace_callsites),
                                             &mod->num_ftrace_callsites);
 #endif
+
+       if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
+               printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
+                      mod->name);
 }
 
 static struct module *move_module(struct module *mod,
@@ -2311,19 +2437,67 @@ static struct module *move_module(struct module *mod,
        return mod;
 }
 
+static int check_module_license_and_versions(struct module *mod,
+                                            Elf_Shdr *sechdrs)
+{
+       /*
+        * ndiswrapper is under GPL by itself, but loads proprietary modules.
+        * Don't use add_taint_module(), as it would prevent ndiswrapper from
+        * using GPL-only symbols it needs.
+        */
+       if (strcmp(mod->name, "ndiswrapper") == 0)
+               add_taint(TAINT_PROPRIETARY_MODULE);
+
+       /* driverloader was caught wrongly pretending to be under GPL */
+       if (strcmp(mod->name, "driverloader") == 0)
+               add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
+
+#ifdef CONFIG_MODVERSIONS
+       if ((mod->num_syms && !mod->crcs)
+           || (mod->num_gpl_syms && !mod->gpl_crcs)
+           || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
+#ifdef CONFIG_UNUSED_SYMBOLS
+           || (mod->num_unused_syms && !mod->unused_crcs)
+           || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
+#endif
+               ) {
+               return try_to_force_load(mod,
+                                        "no versions for exported symbols");
+       }
+#endif
+       return 0;
+}
+
+static void flush_module_icache(const struct module *mod)
+{
+       mm_segment_t old_fs;
+
+       /* flush the icache in correct context */
+       old_fs = get_fs();
+       set_fs(KERNEL_DS);
+
+       /*
+        * Flush the instruction cache, since we've played with text.
+        * Do it before processing of module parameters, so the module
+        * can provide parameter accessor functions of its own.
+        */
+       if (mod->module_init)
+               flush_icache_range((unsigned long)mod->module_init,
+                                  (unsigned long)mod->module_init
+                                  + mod->init_size);
+       flush_icache_range((unsigned long)mod->module_core,
+                          (unsigned long)mod->module_core + mod->core_size);
+
+       set_fs(old_fs);
+}
+
 /* Allocate and load the module: note that size of section 0 is always
    zero, and we rely on this for optional sections. */
 static noinline struct module *load_module(void __user *umod,
                                  unsigned long len,
                                  const char __user *uargs)
 {
-       Elf_Ehdr *hdr;
-       Elf_Shdr *sechdrs;
-       char *secstrings, *args, *strtab = NULL;
-       unsigned int i;
-       unsigned int symindex = 0;
-       unsigned int strindex = 0;
-       unsigned int modindex, versindex, infoindex, pcpuindex;
+       struct load_info info = { NULL, };
        struct module *mod;
        long err;
        unsigned long symoffs, stroffs, *strmap;
@@ -2331,85 +2505,31 @@ static noinline struct module *load_module(void __user *umod,
        struct _ddebug *debug = NULL;
        unsigned int num_debug = 0;
 
-       mm_segment_t old_fs;
-
        DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
               umod, len, uargs);
 
-       err = copy_and_check(&hdr, umod, len);
+       err = copy_and_check(&info, umod, len);
        if (err)
                return ERR_PTR(err);
 
-       /* Convenience variables */
-       sechdrs = (void *)hdr + hdr->e_shoff;
-       secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
-       sechdrs[0].sh_addr = 0;
-
-       for (i = 1; i < hdr->e_shnum; i++) {
-               if (sechdrs[i].sh_type != SHT_NOBITS
-                   && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
-                       goto truncated;
-
-               /* Mark all sections sh_addr with their address in the
-                  temporary image. */
-               sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
-
-               /* Internal symbols and strings. */
-               if (sechdrs[i].sh_type == SHT_SYMTAB) {
-                       symindex = i;
-                       strindex = sechdrs[i].sh_link;
-                       strtab = (char *)hdr + sechdrs[strindex].sh_offset;
-               }
-#ifndef CONFIG_MODULE_UNLOAD
-               /* Don't load .exit sections */
-               if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
-                       sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
-#endif
-       }
-
-       modindex = find_sec(hdr, sechdrs, secstrings,
-                           ".gnu.linkonce.this_module");
-       if (!modindex) {
-               printk(KERN_WARNING "No module found in object\n");
-               err = -ENOEXEC;
-               goto free_hdr;
-       }
-       /* This is temporary: point mod into copy of data. */
-       mod = (void *)sechdrs[modindex].sh_addr;
-
-       if (symindex == 0) {
-               printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
-                      mod->name);
-               err = -ENOEXEC;
-               goto free_hdr;
-       }
-
-       versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
-       infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
-       pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
-
-       /* Don't keep modinfo and version sections. */
-       sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
-       sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
-
-       /* Check module struct version now, before we try to use module. */
-       if (!check_modstruct_version(sechdrs, versindex, mod)) {
-               err = -ENOEXEC;
+       mod = setup_load_info(&info);
+       if (IS_ERR(mod)) {
+               err = PTR_ERR(mod);
                goto free_hdr;
        }
 
-       err = check_modinfo(mod, sechdrs, infoindex, versindex);
+       err = check_modinfo(mod, info.sechdrs, info.index.info, info.index.vers);
        if (err)
                goto free_hdr;
 
        /* Now copy in args */
-       args = strndup_user(uargs, ~0UL >> 1);
-       if (IS_ERR(args)) {
-               err = PTR_ERR(args);
+       info.args = strndup_user(uargs, ~0UL >> 1);
+       if (IS_ERR(info.args)) {
+               err = PTR_ERR(info.args);
                goto free_hdr;
        }
 
-       strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
+       strmap = kzalloc(BITS_TO_LONGS(info.sechdrs[info.index.str].sh_size)
                         * sizeof(long), GFP_KERNEL);
        if (!strmap) {
                err = -ENOMEM;
@@ -2419,17 +2539,17 @@ static noinline struct module *load_module(void __user *umod,
        mod->state = MODULE_STATE_COMING;
 
        /* Allow arches to frob section contents and sizes.  */
-       err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
+       err = module_frob_arch_sections(info.hdr, info.sechdrs, info.secstrings, mod);
        if (err < 0)
                goto free_mod;
 
-       if (pcpuindex) {
+       if (info.index.pcpu) {
                /* We have a special allocation for this section. */
-               err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
-                                     sechdrs[pcpuindex].sh_addralign);
+               err = percpu_modalloc(mod, info.sechdrs[info.index.pcpu].sh_size,
+                                     info.sechdrs[info.index.pcpu].sh_addralign);
                if (err)
                        goto free_mod;
-               sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
+               info.sechdrs[info.index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
        }
        /* Keep this around for failure path. */
        percpu = mod_percpu(mod);
@@ -2437,12 +2557,12 @@ static noinline struct module *load_module(void __user *umod,
        /* Determine total sizes, and put offsets in sh_entsize.  For now
           this is done generically; there doesn't appear to be any
           special cases for the architectures. */
-       layout_sections(mod, hdr, sechdrs, secstrings);
-       symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
-                               secstrings, &stroffs, strmap);
+       layout_sections(mod, info.hdr, info.sechdrs, info.secstrings);
+       symoffs = layout_symtab(mod, info.sechdrs, info.index.sym, info.index.str, info.hdr,
+                               info.secstrings, &stroffs, strmap);
 
        /* Allocate and move to the final place */
-       mod = move_module(mod, hdr, sechdrs, secstrings, modindex);
+       mod = move_module(mod, info.hdr, info.sechdrs, info.secstrings, info.index.mod);
        if (IS_ERR(mod)) {
                err = PTR_ERR(mod);
                goto free_percpu;
@@ -2453,116 +2573,52 @@ static noinline struct module *load_module(void __user *umod,
        if (err)
                goto free_init;
 
-       /* Set up license info based on the info section */
-       set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
-
-       /*
-        * ndiswrapper is under GPL by itself, but loads proprietary modules.
-        * Don't use add_taint_module(), as it would prevent ndiswrapper from
-        * using GPL-only symbols it needs.
-        */
-       if (strcmp(mod->name, "ndiswrapper") == 0)
-               add_taint(TAINT_PROPRIETARY_MODULE);
+       /* Now we've got everything in the final locations, we can
+        * find optional sections. */
+       find_module_sections(mod, info.hdr, info.sechdrs, info.secstrings);
 
-       /* driverloader was caught wrongly pretending to be under GPL */
-       if (strcmp(mod->name, "driverloader") == 0)
-               add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
+       err = check_module_license_and_versions(mod, info.sechdrs);
+       if (err)
+               goto free_unload;
 
        /* Set up MODINFO_ATTR fields */
-       setup_modinfo(mod, sechdrs, infoindex);
+       setup_modinfo(mod, info.sechdrs, info.index.info);
 
        /* Fix up syms, so that st_value is a pointer to location. */
-       err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
+       err = simplify_symbols(info.sechdrs, info.index.sym, info.strtab, info.index.vers, info.index.pcpu,
                               mod);
        if (err < 0)
                goto cleanup;
 
-       /* Now we've got everything in the final locations, we can
-        * find optional sections. */
-       find_module_sections(mod, hdr, sechdrs, secstrings);
-
-#ifdef CONFIG_MODVERSIONS
-       if ((mod->num_syms && !mod->crcs)
-           || (mod->num_gpl_syms && !mod->gpl_crcs)
-           || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
-#ifdef CONFIG_UNUSED_SYMBOLS
-           || (mod->num_unused_syms && !mod->unused_crcs)
-           || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
-#endif
-               ) {
-               err = try_to_force_load(mod,
-                                       "no versions for exported symbols");
-               if (err)
-                       goto cleanup;
-       }
-#endif
-
-       /* Now do relocations. */
-       for (i = 1; i < hdr->e_shnum; i++) {
-               const char *strtab = (char *)sechdrs[strindex].sh_addr;
-               unsigned int info = sechdrs[i].sh_info;
-
-               /* Not a valid relocation section? */
-               if (info >= hdr->e_shnum)
-                       continue;
-
-               /* Don't bother with non-allocated sections */
-               if (!(sechdrs[info].sh_flags & SHF_ALLOC))
-                       continue;
-
-               if (sechdrs[i].sh_type == SHT_REL)
-                       err = apply_relocate(sechdrs, strtab, symindex, i,mod);
-               else if (sechdrs[i].sh_type == SHT_RELA)
-                       err = apply_relocate_add(sechdrs, strtab, symindex, i,
-                                                mod);
-               if (err < 0)
-                       goto cleanup;
-       }
+       err = apply_relocations(mod, info.hdr, info.sechdrs, info.index.sym, info.index.str);
+       if (err < 0)
+               goto cleanup;
 
        /* Set up and sort exception table */
-       mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
+       mod->extable = section_objs(info.hdr, info.sechdrs, info.secstrings, "__ex_table",
                                    sizeof(*mod->extable), &mod->num_exentries);
        sort_extable(mod->extable, mod->extable + mod->num_exentries);
 
        /* Finally, copy percpu area over. */
-       percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
-                      sechdrs[pcpuindex].sh_size);
+       percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
+                      info.sechdrs[info.index.pcpu].sh_size);
 
-       add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
-                    symoffs, stroffs, secstrings, strmap);
+       add_kallsyms(mod, info.sechdrs, info.hdr->e_shnum, info.index.sym, info.index.str,
+                    symoffs, stroffs, info.secstrings, strmap);
        kfree(strmap);
        strmap = NULL;
 
        if (!mod->taints)
-               debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
+               debug = section_objs(info.hdr, info.sechdrs, info.secstrings, "__verbose",
                                     sizeof(*debug), &num_debug);
 
-       err = module_finalize(hdr, sechdrs, mod);
+       err = module_finalize(info.hdr, info.sechdrs, mod);
        if (err < 0)
                goto cleanup;
 
-       /* flush the icache in correct context */
-       old_fs = get_fs();
-       set_fs(KERNEL_DS);
-
-       /*
-        * Flush the instruction cache, since we've played with text.
-        * Do it before processing of module parameters, so the module
-        * can provide parameter accessor functions of its own.
-        */
-       if (mod->module_init)
-               flush_icache_range((unsigned long)mod->module_init,
-                                  (unsigned long)mod->module_init
-                                  + mod->init_size);
-       flush_icache_range((unsigned long)mod->module_core,
-                          (unsigned long)mod->module_core + mod->core_size);
-
-       set_fs(old_fs);
+       flush_module_icache(mod);
 
-       mod->args = args;
-       if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
-               printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
-                      mod->name);
+       mod->args = info.args;
 
        /* Now sew it into the lists so we can get lockdep and oops
         * info during argument parsing.  Noone should access us, since
@@ -2596,11 +2652,11 @@ static noinline struct module *load_module(void __user *umod,
        if (err < 0)
                goto unlink;
 
-       add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
-       add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
+       add_sect_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
+       add_notes_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
 
        /* Get rid of temporary copy */
-       vfree(hdr);
+       vfree(info.hdr);
 
        trace_module_load(mod);
 
@@ -2619,6 +2675,7 @@ static noinline struct module *load_module(void __user *umod,
        module_arch_cleanup(mod);
  cleanup:
        free_modinfo(mod);
+ free_unload:
        module_unload_free(mod);
  free_init:
        module_free(mod, mod->module_init);
@@ -2627,16 +2684,11 @@ static noinline struct module *load_module(void __user *umod,
  free_percpu:
        free_percpu(percpu);
  free_mod:
-       kfree(args);
+       kfree(info.args);
        kfree(strmap);
  free_hdr:
-       vfree(hdr);
+       vfree(info.hdr);
        return ERR_PTR(err);
-
- truncated:
-       printk(KERN_ERR "Module len %lu truncated\n", len);
-       err = -ENOEXEC;
-       goto free_hdr;
 }
 
 /* Call module constructors. */
This page took 0.051466 seconds and 5 git commands to generate.