Commit 03e88ae1 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

[PATCH] fix module sysfs files reference counting

The module files, refcnt, version, and srcversion did not properly
increment the owner's module reference count, allowing the modules to
be removed while the files were open, causing oopses.

This patch fixes this, and also fixes the problem that the version and
srcversion files were not showing up, unless CONFIG_MODULE_UNLOAD was
enabled, which is not correct.

Cc: Nathan Lynch <ntl@pobox.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent b87ba0a3
...@@ -245,6 +245,7 @@ struct module ...@@ -245,6 +245,7 @@ struct module
/* Sysfs stuff. */ /* Sysfs stuff. */
struct module_kobject mkobj; struct module_kobject mkobj;
struct module_param_attrs *param_attrs; struct module_param_attrs *param_attrs;
struct module_attribute *modinfo_attrs;
const char *version; const char *version;
const char *srcversion; const char *srcversion;
......
...@@ -429,7 +429,6 @@ static inline void percpu_modcopy(void *pcpudst, const void *src, ...@@ -429,7 +429,6 @@ static inline void percpu_modcopy(void *pcpudst, const void *src,
} }
#endif /* CONFIG_SMP */ #endif /* CONFIG_SMP */
#ifdef CONFIG_MODULE_UNLOAD
#define MODINFO_ATTR(field) \ #define MODINFO_ATTR(field) \
static void setup_modinfo_##field(struct module *mod, const char *s) \ static void setup_modinfo_##field(struct module *mod, const char *s) \
{ \ { \
...@@ -461,12 +460,7 @@ static struct module_attribute modinfo_##field = { \ ...@@ -461,12 +460,7 @@ static struct module_attribute modinfo_##field = { \
MODINFO_ATTR(version); MODINFO_ATTR(version);
MODINFO_ATTR(srcversion); MODINFO_ATTR(srcversion);
static struct module_attribute *modinfo_attrs[] = { #ifdef CONFIG_MODULE_UNLOAD
&modinfo_version,
&modinfo_srcversion,
NULL,
};
/* Init the unload section of the module. */ /* Init the unload section of the module. */
static void module_unload_init(struct module *mod) static void module_unload_init(struct module *mod)
{ {
...@@ -781,6 +775,15 @@ static inline void module_unload_init(struct module *mod) ...@@ -781,6 +775,15 @@ static inline void module_unload_init(struct module *mod)
} }
#endif /* CONFIG_MODULE_UNLOAD */ #endif /* CONFIG_MODULE_UNLOAD */
static struct module_attribute *modinfo_attrs[] = {
&modinfo_version,
&modinfo_srcversion,
#ifdef CONFIG_MODULE_UNLOAD
&refcnt,
#endif
NULL,
};
#ifdef CONFIG_OBSOLETE_MODPARM #ifdef CONFIG_OBSOLETE_MODPARM
/* Bounds checking done below */ /* Bounds checking done below */
static int obsparm_copy_string(const char *val, struct kernel_param *kp) static int obsparm_copy_string(const char *val, struct kernel_param *kp)
...@@ -1106,37 +1109,28 @@ static inline void remove_sect_attrs(struct module *mod) ...@@ -1106,37 +1109,28 @@ static inline void remove_sect_attrs(struct module *mod)
} }
#endif /* CONFIG_KALLSYMS */ #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
#ifdef CONFIG_MODULE_UNLOAD
static int module_add_modinfo_attrs(struct module *mod) static int module_add_modinfo_attrs(struct module *mod)
{ {
struct module_attribute *attr; struct module_attribute *attr;
struct module_attribute *temp_attr;
int error = 0; int error = 0;
int i; int i;
mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
(ARRAY_SIZE(modinfo_attrs) + 1)),
GFP_KERNEL);
if (!mod->modinfo_attrs)
return -ENOMEM;
temp_attr = mod->modinfo_attrs;
for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
if (!attr->test || if (!attr->test ||
(attr->test && attr->test(mod))) (attr->test && attr->test(mod))) {
error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr); memcpy(temp_attr, attr, sizeof(*temp_attr));
temp_attr->attr.owner = mod;
error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
++temp_attr;
}
} }
return error; return error;
} }
...@@ -1146,12 +1140,16 @@ static void module_remove_modinfo_attrs(struct module *mod) ...@@ -1146,12 +1140,16 @@ static void module_remove_modinfo_attrs(struct module *mod)
struct module_attribute *attr; struct module_attribute *attr;
int i; int i;
for (i = 0; (attr = modinfo_attrs[i]); i++) { for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
/* pick a field to test for end of list */
if (!attr->attr.name)
break;
sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
attr->free(mod); if (attr->free)
attr->free(mod);
} }
kfree(mod->modinfo_attrs);
} }
#endif
static int mod_sysfs_setup(struct module *mod, static int mod_sysfs_setup(struct module *mod,
struct kernel_param *kparam, struct kernel_param *kparam,
...@@ -1169,19 +1167,13 @@ static int mod_sysfs_setup(struct module *mod, ...@@ -1169,19 +1167,13 @@ static int mod_sysfs_setup(struct module *mod,
if (err) if (err)
goto out; goto out;
err = module_add_refcnt_attr(mod);
if (err)
goto out_unreg;
err = module_param_sysfs_setup(mod, kparam, num_params); err = module_param_sysfs_setup(mod, kparam, num_params);
if (err) if (err)
goto out_unreg; goto out_unreg;
#ifdef CONFIG_MODULE_UNLOAD
err = module_add_modinfo_attrs(mod); err = module_add_modinfo_attrs(mod);
if (err) if (err)
goto out_unreg; goto out_unreg;
#endif
return 0; return 0;
...@@ -1193,10 +1185,7 @@ static int mod_sysfs_setup(struct module *mod, ...@@ -1193,10 +1185,7 @@ static int mod_sysfs_setup(struct module *mod,
static void mod_kobject_remove(struct module *mod) static void mod_kobject_remove(struct module *mod)
{ {
#ifdef CONFIG_MODULE_UNLOAD
module_remove_modinfo_attrs(mod); module_remove_modinfo_attrs(mod);
#endif
module_remove_refcnt_attr(mod);
module_param_sysfs_remove(mod); module_param_sysfs_remove(mod);
kobject_unregister(&mod->mkobj.kobj); kobject_unregister(&mod->mkobj.kobj);
...@@ -1474,7 +1463,6 @@ static char *get_modinfo(Elf_Shdr *sechdrs, ...@@ -1474,7 +1463,6 @@ static char *get_modinfo(Elf_Shdr *sechdrs,
return NULL; return NULL;
} }
#ifdef CONFIG_MODULE_UNLOAD
static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
unsigned int infoindex) unsigned int infoindex)
{ {
...@@ -1489,7 +1477,6 @@ static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, ...@@ -1489,7 +1477,6 @@ static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
attr->attr.name)); attr->attr.name));
} }
} }
#endif
#ifdef CONFIG_KALLSYMS #ifdef CONFIG_KALLSYMS
int is_exported(const char *name, const struct module *mod) int is_exported(const char *name, const struct module *mod)
...@@ -1803,10 +1790,8 @@ static struct module *load_module(void __user *umod, ...@@ -1803,10 +1790,8 @@ static struct module *load_module(void __user *umod,
if (strcmp(mod->name, "driverloader") == 0) if (strcmp(mod->name, "driverloader") == 0)
add_taint(TAINT_PROPRIETARY_MODULE); add_taint(TAINT_PROPRIETARY_MODULE);
#ifdef CONFIG_MODULE_UNLOAD
/* Set up MODINFO_ATTR fields */ /* Set up MODINFO_ATTR fields */
setup_modinfo(mod, sechdrs, infoindex); setup_modinfo(mod, sechdrs, infoindex);
#endif
/* Fix up syms, so that st_value is a pointer to location. */ /* Fix up syms, so that st_value is a pointer to location. */
err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex, err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
......
...@@ -638,13 +638,8 @@ static ssize_t module_attr_show(struct kobject *kobj, ...@@ -638,13 +638,8 @@ static ssize_t module_attr_show(struct kobject *kobj,
if (!attribute->show) if (!attribute->show)
return -EIO; return -EIO;
if (!try_module_get(mk->mod))
return -ENODEV;
ret = attribute->show(attribute, mk->mod, buf); ret = attribute->show(attribute, mk->mod, buf);
module_put(mk->mod);
return ret; return ret;
} }
...@@ -662,13 +657,8 @@ static ssize_t module_attr_store(struct kobject *kobj, ...@@ -662,13 +657,8 @@ static ssize_t module_attr_store(struct kobject *kobj,
if (!attribute->store) if (!attribute->store)
return -EIO; return -EIO;
if (!try_module_get(mk->mod))
return -ENODEV;
ret = attribute->store(attribute, mk->mod, buf, len); ret = attribute->store(attribute, mk->mod, buf, len);
module_put(mk->mod);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment