Commit bc6f2a75 authored by YueHaibing's avatar YueHaibing Committed by Jessica Yu

kernel/module: Fix mem leak in module_add_modinfo_attrs

In module_add_modinfo_attrs if sysfs_create_file
fails, we forget to free allocated modinfo_attrs
and roll back the sysfs files.

Fixes: 03e88ae1 ("[PATCH] fix module sysfs files reference counting")
Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
Signed-off-by: default avatarYueHaibing <yuehaibing@huawei.com>
Signed-off-by: default avatarJessica Yu <jeyu@kernel.org>
parent 8d1b73dd
...@@ -1708,6 +1708,8 @@ static int add_usage_links(struct module *mod) ...@@ -1708,6 +1708,8 @@ static int add_usage_links(struct module *mod)
return ret; return ret;
} }
static void module_remove_modinfo_attrs(struct module *mod, int end);
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;
...@@ -1722,24 +1724,34 @@ static int module_add_modinfo_attrs(struct module *mod) ...@@ -1722,24 +1724,34 @@ static int module_add_modinfo_attrs(struct module *mod)
return -ENOMEM; return -ENOMEM;
temp_attr = mod->modinfo_attrs; temp_attr = mod->modinfo_attrs;
for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { for (i = 0; (attr = modinfo_attrs[i]); i++) {
if (!attr->test || attr->test(mod)) { if (!attr->test || attr->test(mod)) {
memcpy(temp_attr, attr, sizeof(*temp_attr)); memcpy(temp_attr, attr, sizeof(*temp_attr));
sysfs_attr_init(&temp_attr->attr); sysfs_attr_init(&temp_attr->attr);
error = sysfs_create_file(&mod->mkobj.kobj, error = sysfs_create_file(&mod->mkobj.kobj,
&temp_attr->attr); &temp_attr->attr);
if (error)
goto error_out;
++temp_attr; ++temp_attr;
} }
} }
return 0;
error_out:
if (i > 0)
module_remove_modinfo_attrs(mod, --i);
return error; return error;
} }
static void module_remove_modinfo_attrs(struct module *mod) static void module_remove_modinfo_attrs(struct module *mod, int end)
{ {
struct module_attribute *attr; struct module_attribute *attr;
int i; int i;
for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
if (end >= 0 && i > end)
break;
/* pick a field to test for end of list */ /* pick a field to test for end of list */
if (!attr->attr.name) if (!attr->attr.name)
break; break;
...@@ -1827,7 +1839,7 @@ static int mod_sysfs_setup(struct module *mod, ...@@ -1827,7 +1839,7 @@ static int mod_sysfs_setup(struct module *mod,
return 0; return 0;
out_unreg_modinfo_attrs: out_unreg_modinfo_attrs:
module_remove_modinfo_attrs(mod); module_remove_modinfo_attrs(mod, -1);
out_unreg_param: out_unreg_param:
module_param_sysfs_remove(mod); module_param_sysfs_remove(mod);
out_unreg_holders: out_unreg_holders:
...@@ -1863,7 +1875,7 @@ static void mod_sysfs_fini(struct module *mod) ...@@ -1863,7 +1875,7 @@ static void mod_sysfs_fini(struct module *mod)
{ {
} }
static void module_remove_modinfo_attrs(struct module *mod) static void module_remove_modinfo_attrs(struct module *mod, int end)
{ {
} }
...@@ -1879,7 +1891,7 @@ static void init_param_lock(struct module *mod) ...@@ -1879,7 +1891,7 @@ static void init_param_lock(struct module *mod)
static void mod_sysfs_teardown(struct module *mod) static void mod_sysfs_teardown(struct module *mod)
{ {
del_usage_links(mod); del_usage_links(mod);
module_remove_modinfo_attrs(mod); module_remove_modinfo_attrs(mod, -1);
module_param_sysfs_remove(mod); module_param_sysfs_remove(mod);
kobject_put(mod->mkobj.drivers_dir); kobject_put(mod->mkobj.drivers_dir);
kobject_put(mod->holders_dir); kobject_put(mod->holders_dir);
......
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