- fix 2.6.10 merge regression
[linux-2.6.git] / kernel / module.c
index 5a9846b..ad5ccf7 100644 (file)
 #include <linux/vermagic.h>
 #include <linux/notifier.h>
 #include <linux/stop_machine.h>
+#include <linux/device.h>
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
-#include <asm/pgalloc.h>
 #include <asm/cacheflush.h>
+#include "module-verify.h"
 
 #if 0
 #define DEBUGP printk
@@ -52,9 +53,6 @@
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
-#define symbol_is(literal, string)                             \
-       (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
-
 /* Protects module list */
 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
 
@@ -93,13 +91,6 @@ static inline int strong_try_module_get(struct module *mod)
        return try_module_get(mod);
 }
 
-/* Stub function for modules which don't have an initfn */
-int init_module(void)
-{
-       return 0;
-}
-EXPORT_SYMBOL(init_module);
-
 /* A thread that wants to hold a reference to a module only while it
  * is running can call ths to safely exit.
  * nfsd and lockd use this.
@@ -517,12 +508,6 @@ EXPORT_SYMBOL(module_refcount);
 /* This exists whether we can unload or not */
 static void free_module(struct module *mod);
 
-/* Stub function for modules which don't have an exitfn */
-void cleanup_module(void)
-{
-}
-EXPORT_SYMBOL(cleanup_module);
-
 static void wait_for_zero_refcount(struct module *mod)
 {
        /* Since we might sleep for some time, drop the semaphore first */
@@ -577,7 +562,7 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
        }
 
        /* If it has an init func, it must have an exit func to unload */
-       if ((mod->init != init_module && mod->exit == cleanup_module)
+       if ((mod->init != NULL && mod->exit == NULL)
            || mod->unsafe) {
                forced = try_force(flags);
                if (!forced) {
@@ -592,15 +577,19 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
 
        /* Stop the machine so refcounts can't move and disable module. */
        ret = try_stop_module(mod, flags, &forced);
+       if (ret != 0)
+               goto out;
 
        /* Never wait if forced. */
        if (!forced && module_refcount(mod) != 0)
                wait_for_zero_refcount(mod);
 
        /* Final destruction now noone is using it. */
-       up(&module_mutex);
-       mod->exit();
-       down(&module_mutex);
+       if (mod->exit != NULL) {
+               up(&module_mutex);
+               mod->exit();
+               down(&module_mutex);
+       }
        free_module(mod);
 
  out:
@@ -627,7 +616,7 @@ static void print_unload_info(struct seq_file *m, struct module *mod)
                seq_printf(m, "[unsafe],");
        }
 
-       if (mod->init != init_module && mod->exit == cleanup_module) {
+       if (mod->init != NULL && mod->exit == NULL) {
                printed_something = 1;
                seq_printf(m, "[permanent],");
        }
@@ -663,6 +652,17 @@ void symbol_put_addr(void *addr)
 }
 EXPORT_SYMBOL_GPL(symbol_put_addr);
 
+static ssize_t show_refcnt(struct module *mod, char *buffer)
+{
+       /* sysfs holds a reference */
+       return sprintf(buffer, "%u\n", module_refcount(mod)-1);
+}
+
+static struct module_attribute refcnt = {
+       .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
+       .show = show_refcnt,
+};
+
 #else /* !CONFIG_MODULE_UNLOAD */
 static void print_unload_info(struct seq_file *m, struct module *mod)
 {
@@ -684,7 +684,7 @@ static inline void module_unload_init(struct module *mod)
 }
 
 asmlinkage long
-sys_delete_module(const char *name_user, unsigned int flags)
+sys_delete_module(const char __user *name_user, unsigned int flags)
 {
        return -ENOSYS;
 }
@@ -692,19 +692,6 @@ sys_delete_module(const char *name_user, unsigned int flags)
 #endif /* CONFIG_MODULE_UNLOAD */
 
 #ifdef CONFIG_OBSOLETE_MODPARM
-static int param_set_byte(const char *val, struct kernel_param *kp)  
-{
-       char *endp;
-       long l;
-
-       if (!val) return -EINVAL;
-       l = simple_strtol(val, &endp, 0);
-       if (endp == val || *endp || ((char)l != l))
-               return -EINVAL;
-       *((char *)kp->arg) = l;
-       return 0;
-}
-
 /* Bounds checking done below */
 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
 {
@@ -944,6 +931,170 @@ static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
        return ret;
 }
 
+
+/*
+ * /sys/module/foo/sections stuff
+ * J. Corbet <corbet@lwn.net>
+ */
+#ifdef CONFIG_KALLSYMS
+static void module_sect_attrs_release(struct kobject *kobj)
+{
+       kfree(container_of(kobj, struct module_sections, kobj));
+}
+
+static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
+               char *buf)
+{
+       struct module_sect_attr *sattr =
+               container_of(attr, struct module_sect_attr, attr);
+       return sprintf(buf, "0x%lx\n", sattr->address);
+}
+
+static struct sysfs_ops module_sect_ops = {
+       .show = module_sect_show,
+};
+
+static struct kobj_type module_sect_ktype = {
+       .sysfs_ops = &module_sect_ops,
+       .release =   module_sect_attrs_release,
+};
+
+static void add_sect_attrs(struct module *mod, unsigned int nsect,
+               char *secstrings, Elf_Shdr *sechdrs)
+{
+       unsigned int nloaded = 0, i;
+       struct module_sect_attr *sattr;
+       
+       if (!mod->mkobj)
+               return;
+       
+       /* Count loaded sections and allocate structures */
+       for (i = 0; i < nsect; i++)
+               if (sechdrs[i].sh_flags & SHF_ALLOC)
+                       nloaded++;
+       mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
+                       nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
+       if (! mod->sect_attrs)
+               return;
+
+       /* sections entry setup */
+       memset(mod->sect_attrs, 0, sizeof(struct module_sections));
+       if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
+               goto out;
+       mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
+       mod->sect_attrs->kobj.ktype = &module_sect_ktype;
+       if (kobject_register(&mod->sect_attrs->kobj))
+               goto out;
+
+       /* And the section attributes. */
+       sattr = &mod->sect_attrs->attrs[0];
+       for (i = 0; i < nsect; i++) {
+               if (! (sechdrs[i].sh_flags & SHF_ALLOC))
+                       continue;
+               sattr->address = sechdrs[i].sh_addr;
+               strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
+                               MODULE_SECT_NAME_LEN);
+               sattr->attr.name = sattr->name;
+               sattr->attr.owner = mod;
+               sattr->attr.mode = S_IRUGO;
+               (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
+               sattr++;
+       }
+       return;
+  out:
+       kfree(mod->sect_attrs);
+       mod->sect_attrs = NULL;
+}
+
+static void remove_sect_attrs(struct module *mod)
+{
+       if (mod->sect_attrs) {
+               kobject_unregister(&mod->sect_attrs->kobj);
+               mod->sect_attrs = NULL;
+       }
+}
+
+
+#else
+static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
+               char *sectstrings, Elf_Shdr *sechdrs)
+{
+}
+
+static inline void remove_sect_attrs(struct module *mod)
+{
+}
+#endif /* CONFIG_KALLSYMS */
+
+
+#ifdef CONFIG_MODULE_UNLOAD
+static inline int module_add_refcnt_attr(struct module *mod)
+{
+       return sysfs_create_file(&mod->mkobj->kobj, &refcnt.attr);
+}
+static void module_remove_refcnt_attr(struct module *mod)
+{
+       return sysfs_remove_file(&mod->mkobj->kobj, &refcnt.attr);
+}
+#else
+static inline int module_add_refcnt_attr(struct module *mod)
+{
+       return 0;
+}
+static void module_remove_refcnt_attr(struct module *mod)
+{
+}
+#endif
+
+
+static int mod_sysfs_setup(struct module *mod,
+                          struct kernel_param *kparam,
+                          unsigned int num_params)
+{
+       int err;
+
+       mod->mkobj = kmalloc(sizeof(struct module_kobject), GFP_KERNEL);
+       if (!mod->mkobj)
+               return -ENOMEM;
+
+       memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
+       err = kobject_set_name(&mod->mkobj->kobj, "%s", mod->name);
+       if (err)
+               goto out;
+       kobj_set_kset_s(mod->mkobj, module_subsys);
+       mod->mkobj->mod = mod;
+       err = kobject_register(&mod->mkobj->kobj);
+       if (err)
+               goto out;
+
+       err = module_add_refcnt_attr(mod);
+       if (err)
+               goto out_unreg;
+
+       err = module_param_sysfs_setup(mod, kparam, num_params);
+       if (err)
+               goto out_unreg;
+
+       return 0;
+
+out_unreg:
+       /* Calls module_kobj_release */
+       kobject_unregister(&mod->mkobj->kobj);
+       return err;
+out:
+       kfree(mod->mkobj);
+       return err;
+}
+
+static void mod_kobject_remove(struct module *mod)
+{
+       module_remove_refcnt_attr(mod);
+       module_param_sysfs_remove(mod);
+
+       /* Calls module_kobj_release */
+       kobject_unregister(&mod->mkobj->kobj);
+}
+
 /* Free a module, remove from lists, etc (must hold module mutex). */
 static void free_module(struct module *mod)
 {
@@ -952,6 +1103,9 @@ static void free_module(struct module *mod)
        list_del(&mod->list);
        spin_unlock_irq(&modlist_lock);
 
+       remove_sect_attrs(mod);
+       mod_kobject_remove(mod);
+
        /* Arch-specific cleanup. */
        module_arch_cleanup(mod);
 
@@ -1131,7 +1285,7 @@ static void set_license(struct module *mod, const char *license)
                license = "unspecified";
 
        mod->license_gplok = license_is_gpl_compatible(license);
-       if (!mod->license_gplok) {
+       if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
                printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
                       mod->name, license);
                tainted |= TAINT_PROPRIETARY_MODULE;
@@ -1274,6 +1428,7 @@ static struct module *load_module(void __user *umod,
        long err = 0;
        void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
        struct exception_table_entry *extable;
+       int gpgsig_ok;
 
        DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
               umod, len, uargs);
@@ -1299,17 +1454,19 @@ static struct module *load_module(void __user *umod,
                goto free_hdr;
        }
 
-       if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
-               goto truncated;
+       /* verify the module (validates ELF and checks signature) */
+       gpgsig_ok = 0;
+       err = module_verify(hdr, len);
+       if (err < 0)
+               goto free_hdr;
+       if (err == 1)
+               gpgsig_ok = 1;
 
        /* Convenience variables */
        sechdrs = (void *)hdr + hdr->e_shoff;
        secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
        sechdrs[0].sh_addr = 0;
 
-       /* And these should exist, but gcc whinges if we don't init them */
-       symindex = strindex = 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)
@@ -1340,6 +1497,14 @@ static struct module *load_module(void __user *umod,
                goto free_hdr;
        }
        mod = (void *)sechdrs[modindex].sh_addr;
+       mod->gpgsig_ok = gpgsig_ok;
+
+       if (symindex == 0) {
+               printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
+                      mod->name);
+               err = -ENOEXEC;
+               goto free_hdr;
+       }
 
        /* Optional sections */
        exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
@@ -1556,8 +1721,15 @@ static struct module *load_module(void __user *umod,
                                 / sizeof(struct kernel_param),
                                 NULL);
        }
+
+       err = mod_sysfs_setup(mod, 
+                             (struct kernel_param *)
+                             sechdrs[setupindex].sh_addr,
+                             sechdrs[setupindex].sh_size
+                             / sizeof(struct kernel_param));
        if (err < 0)
                goto arch_cleanup;
+       add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
 
        /* Get rid of temporary copy */
        vfree(hdr);
@@ -1595,7 +1767,7 @@ sys_init_module(void __user *umod,
                const char __user *uargs)
 {
        struct module *mod;
-       int ret;
+       int ret = 0;
 
        /* Must have permission */
        if (!capable(CAP_SYS_MODULE))
@@ -1634,7 +1806,8 @@ sys_init_module(void __user *umod,
        up(&notify_mutex);
 
        /* Start the module */
-       ret = mod->init();
+       if (mod->init != NULL)
+               ret = mod->init();
        if (ret < 0) {
                /* Init routine failed: abort.  Try to protect us from
                    buggy refcounters. */
@@ -1672,6 +1845,16 @@ static inline int within(unsigned long addr, void *start, unsigned long size)
 }
 
 #ifdef CONFIG_KALLSYMS
+/*
+ * This ignores the intensely annoying "mapping symbols" found
+ * in ARM ELF files: $a, $t and $d.
+ */
+static inline int is_arm_mapping_symbol(const char *str)
+{
+       return str[0] == '$' && strchr("atd", str[1]) 
+              && (str[2] == '\0' || str[2] == '.');
+}
+
 static const char *get_ksymbol(struct module *mod,
                               unsigned long addr,
                               unsigned long *size,
@@ -1696,11 +1879,13 @@ static const char *get_ksymbol(struct module *mod,
                 * and inserted at a whim. */
                if (mod->symtab[i].st_value <= addr
                    && mod->symtab[i].st_value > mod->symtab[best].st_value
-                   && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
+                   && *(mod->strtab + mod->symtab[i].st_name) != '\0'
+                   && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
                        best = i;
                if (mod->symtab[i].st_value > addr
                    && mod->symtab[i].st_value < nextval
-                   && *(mod->strtab + mod->symtab[i].st_name) != '\0')
+                   && *(mod->strtab + mod->symtab[i].st_name) != '\0'
+                   && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
                        nextval = mod->symtab[i].st_value;
        }
 
@@ -1875,7 +2060,7 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
 }
 
 /* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
-struct module *module_text_address(unsigned long addr)
+struct module *__module_text_address(unsigned long addr)
 {
        struct module *mod;
 
@@ -1886,6 +2071,54 @@ struct module *module_text_address(unsigned long addr)
        return NULL;
 }
 
+struct module *module_text_address(unsigned long addr)
+{
+       struct module *mod;
+       unsigned long flags;
+
+       spin_lock_irqsave(&modlist_lock, flags);
+       mod = __module_text_address(addr);
+       spin_unlock_irqrestore(&modlist_lock, flags);
+
+       return mod;
+}
+
+/* Don't grab lock, we're oopsing. */
+void print_modules(void)
+{
+       struct module *mod;
+
+       printk("Modules linked in:");
+       list_for_each_entry(mod, &modules, list) {
+               printk(" %s", mod->name);
+#if CONFIG_MODULE_SIG          
+               if (!mod->gpgsig_ok)
+                       printk("(U)");
+#endif         
+       }
+       printk("\n");
+}
+
+void module_add_driver(struct module *mod, struct device_driver *drv)
+{
+       if (!mod || !drv)
+               return;
+       if (!mod->mkobj)
+               return;
+
+       /* Don't check return code; this call is idempotent */
+       sysfs_create_link(&drv->kobj, &mod->mkobj->kobj, "module");
+}
+EXPORT_SYMBOL(module_add_driver);
+
+void module_remove_driver(struct device_driver *drv)
+{
+       if (!drv)
+               return;
+       sysfs_remove_link(&drv->kobj, "module");
+}
+EXPORT_SYMBOL(module_remove_driver);
+
 #ifdef CONFIG_MODVERSIONS
 /* Generate the signature for struct module here, too, for modversions. */
 void struct_module(struct module *mod) { return; }