linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernel/module: Fix mem leak in module_add_modinfo_attrs
@ 2019-05-15 16:12 YueHaibing
  2019-05-30  9:24 ` Yuehaibing
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: YueHaibing @ 2019-05-15 16:12 UTC (permalink / raw)
  To: jeyu, gregkh; +Cc: linux-kernel, paulmck, YueHaibing

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

Fixes: 03e88ae1b13d ("[PATCH] fix module sysfs files reference counting")
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
---
 kernel/module.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/kernel/module.c b/kernel/module.c
index 0b9aa8a..7da73c4 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1714,15 +1714,29 @@ static int module_add_modinfo_attrs(struct module *mod)
 		return -ENOMEM;
 
 	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)) {
 			memcpy(temp_attr, attr, sizeof(*temp_attr));
 			sysfs_attr_init(&temp_attr->attr);
 			error = sysfs_create_file(&mod->mkobj.kobj,
 					&temp_attr->attr);
+			if (error)
+				goto error_out;
 			++temp_attr;
 		}
 	}
+
+	return 0;
+
+error_out:
+	for (; (attr = &mod->modinfo_attrs[i]) && i >= 0; i--) {
+		if (!attr->attr.name)
+			break;
+		sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
+		if (attr->free)
+			attr->free(mod);
+	}
+	kfree(mod->modinfo_attrs);
 	return error;
 }
 
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2019-06-14  7:54 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-15 16:12 [PATCH] kernel/module: Fix mem leak in module_add_modinfo_attrs YueHaibing
2019-05-30  9:24 ` Yuehaibing
2019-05-30 11:45 ` Jessica Yu
2019-05-30 13:32   ` Yuehaibing
2019-05-30 13:43 ` [PATCH v2] " YueHaibing
2019-06-03 10:47   ` Jessica Yu
2019-06-03 12:41     ` Yuehaibing
2019-06-03 12:11   ` Miroslav Benes
2019-06-03 14:45     ` Yuehaibing
2019-06-03 14:45   ` [PATCH v3] " YueHaibing
2019-06-04 10:46     ` Miroslav Benes
2019-06-04 13:54       ` Yuehaibing
2019-06-04 14:15         ` Miroslav Benes
2019-06-07 14:02       ` Jessica Yu
2019-06-11 13:33     ` Jessica Yu
2019-06-11 14:30       ` Yuehaibing
2019-06-11 15:38         ` Greg KH
2019-06-11 15:00     ` [PATCH v4] " YueHaibing
2019-06-12 11:12       ` Miroslav Benes
2019-06-14  7:54       ` Jessica Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).