netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Patch net] genetlink: fix a memory leak on error path
@ 2016-11-03 16:42 Cong Wang
  2016-11-03 16:42 ` [Patch net] taskstats: fix the length of cgroupstats_cmd_get_policy Cong Wang
  2016-11-03 20:53 ` [Patch net] genetlink: fix a memory leak on error path David Miller
  0 siblings, 2 replies; 3+ messages in thread
From: Cong Wang @ 2016-11-03 16:42 UTC (permalink / raw)
  To: netdev; +Cc: Cong Wang, Jakub Kicinski, Johannes Berg

In __genl_register_family(), when genl_validate_assign_mc_groups()
fails, we forget to free the memory we possibly allocate for
family->attrbuf.

Note, some callers call genl_unregister_family() to clean up
on error path, it doesn't work because the family is inserted
to the global list in the nearly last step.

Cc: Jakub Kicinski <kubakici@wp.pl>
Cc: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
---
 net/netlink/genetlink.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 23cc126..49c28e8 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -404,7 +404,7 @@ int __genl_register_family(struct genl_family *family)
 
 	err = genl_validate_assign_mc_groups(family);
 	if (err)
-		goto errout_locked;
+		goto errout_free;
 
 	list_add_tail(&family->family_list, genl_family_chain(family->id));
 	genl_unlock_all();
@@ -417,6 +417,8 @@ int __genl_register_family(struct genl_family *family)
 
 	return 0;
 
+errout_free:
+	kfree(family->attrbuf);
 errout_locked:
 	genl_unlock_all();
 errout:
-- 
2.1.0

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

* [Patch net] taskstats: fix the length of cgroupstats_cmd_get_policy
  2016-11-03 16:42 [Patch net] genetlink: fix a memory leak on error path Cong Wang
@ 2016-11-03 16:42 ` Cong Wang
  2016-11-03 20:53 ` [Patch net] genetlink: fix a memory leak on error path David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: Cong Wang @ 2016-11-03 16:42 UTC (permalink / raw)
  To: netdev; +Cc: Cong Wang

cgroupstats_cmd_get_policy is [CGROUPSTATS_CMD_ATTR_MAX+1],
taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1],
but their family.maxattr is TASKSTATS_CMD_ATTR_MAX.
CGROUPSTATS_CMD_ATTR_MAX is less than TASKSTATS_CMD_ATTR_MAX,
so we could end up accessing out-of-bound.

Change cgroupstats_cmd_get_policy to TASKSTATS_CMD_ATTR_MAX+1,
this is safe because the rest are initialized to 0's.

Reported-by: Andrey Konovalov <andreyknvl@google.com>
Tested-by: Andrey Konovalov <andreyknvl@google.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
---
 kernel/taskstats.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/taskstats.c b/kernel/taskstats.c
index b3f05ee..cbb387a 100644
--- a/kernel/taskstats.c
+++ b/kernel/taskstats.c
@@ -54,7 +54,11 @@ static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1
 	[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
 	[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
 
-static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = {
+/*
+ * We have to use TASKSTATS_CMD_ATTR_MAX here, it is the maxattr in the family.
+ * Make sure they are always aligned.
+ */
+static const struct nla_policy cgroupstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
 	[CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
 };
 
-- 
2.1.0

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

* Re: [Patch net] genetlink: fix a memory leak on error path
  2016-11-03 16:42 [Patch net] genetlink: fix a memory leak on error path Cong Wang
  2016-11-03 16:42 ` [Patch net] taskstats: fix the length of cgroupstats_cmd_get_policy Cong Wang
@ 2016-11-03 20:53 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2016-11-03 20:53 UTC (permalink / raw)
  To: xiyou.wangcong; +Cc: netdev, kubakici, johannes

From: Cong Wang <xiyou.wangcong@gmail.com>
Date: Thu,  3 Nov 2016 09:42:35 -0700

> In __genl_register_family(), when genl_validate_assign_mc_groups()
> fails, we forget to free the memory we possibly allocate for
> family->attrbuf.
> 
> Note, some callers call genl_unregister_family() to clean up
> on error path, it doesn't work because the family is inserted
> to the global list in the nearly last step.
> 
> Cc: Jakub Kicinski <kubakici@wp.pl>
> Cc: Johannes Berg <johannes@sipsolutions.net>
> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>

Applied, thanks Cong.

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

end of thread, other threads:[~2016-11-03 20:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-03 16:42 [Patch net] genetlink: fix a memory leak on error path Cong Wang
2016-11-03 16:42 ` [Patch net] taskstats: fix the length of cgroupstats_cmd_get_policy Cong Wang
2016-11-03 20:53 ` [Patch net] genetlink: fix a memory leak on error path David Miller

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).