diff mbox series

[Linux-kernel-mentees,net,v2] tipc: Fix memory leak in tipc_group_create_member()

Message ID 20200913080605.1542170-1-yepeilin.cs@gmail.com
State New
Headers show
Series [Linux-kernel-mentees,net,v2] tipc: Fix memory leak in tipc_group_create_member() | expand

Commit Message

Peilin Ye Sept. 13, 2020, 8:06 a.m. UTC
tipc_group_add_to_tree() returns silently if `key` matches `nkey` of an
existing node, causing tipc_group_create_member() to leak memory. Let
tipc_group_add_to_tree() return an error in such a case, so that
tipc_group_create_member() can handle it properly.

Fixes: 75da2163dbb6 ("tipc: introduce communication groups")
Reported-and-tested-by: syzbot+f95d90c454864b3b5bc9@syzkaller.appspotmail.com
Cc: Hillf Danton <hdanton@sina.com>
Link: https://syzkaller.appspot.com/bug?id=048390604fe1b60df34150265479202f10e13aff
Signed-off-by: Peilin Ye <yepeilin.cs@gmail.com>
---
Change in v2:
    - let tipc_group_add_to_tree() return a real error code instead of -1.
      (Suggested by David Miller <davem@davemloft.net>)

 net/tipc/group.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

Comments

David Miller Sept. 14, 2020, 11:37 p.m. UTC | #1
From: Peilin Ye <yepeilin.cs@gmail.com>

Date: Sun, 13 Sep 2020 04:06:05 -0400

> tipc_group_add_to_tree() returns silently if `key` matches `nkey` of an

> existing node, causing tipc_group_create_member() to leak memory. Let

> tipc_group_add_to_tree() return an error in such a case, so that

> tipc_group_create_member() can handle it properly.

> 

> Fixes: 75da2163dbb6 ("tipc: introduce communication groups")

> Reported-and-tested-by: syzbot+f95d90c454864b3b5bc9@syzkaller.appspotmail.com

> Cc: Hillf Danton <hdanton@sina.com>

> Link: https://syzkaller.appspot.com/bug?id=048390604fe1b60df34150265479202f10e13aff

> Signed-off-by: Peilin Ye <yepeilin.cs@gmail.com>

> ---

> Change in v2:

>     - let tipc_group_add_to_tree() return a real error code instead of -1.

>       (Suggested by David Miller <davem@davemloft.net>)


Applied and queued up for -stable, thank you.
diff mbox series

Patch

diff --git a/net/tipc/group.c b/net/tipc/group.c
index 588c2d2b0c69..b1fcd2ad5ecf 100644
--- a/net/tipc/group.c
+++ b/net/tipc/group.c
@@ -273,8 +273,8 @@  static struct tipc_member *tipc_group_find_node(struct tipc_group *grp,
 	return NULL;
 }
 
-static void tipc_group_add_to_tree(struct tipc_group *grp,
-				   struct tipc_member *m)
+static int tipc_group_add_to_tree(struct tipc_group *grp,
+				  struct tipc_member *m)
 {
 	u64 nkey, key = (u64)m->node << 32 | m->port;
 	struct rb_node **n, *parent = NULL;
@@ -291,10 +291,11 @@  static void tipc_group_add_to_tree(struct tipc_group *grp,
 		else if (key > nkey)
 			n = &(*n)->rb_right;
 		else
-			return;
+			return -EEXIST;
 	}
 	rb_link_node(&m->tree_node, parent, n);
 	rb_insert_color(&m->tree_node, &grp->members);
+	return 0;
 }
 
 static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
@@ -302,6 +303,7 @@  static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
 						    u32 instance, int state)
 {
 	struct tipc_member *m;
+	int ret;
 
 	m = kzalloc(sizeof(*m), GFP_ATOMIC);
 	if (!m)
@@ -314,8 +316,12 @@  static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
 	m->port = port;
 	m->instance = instance;
 	m->bc_acked = grp->bc_snd_nxt - 1;
+	ret = tipc_group_add_to_tree(grp, m);
+	if (ret < 0) {
+		kfree(m);
+		return NULL;
+	}
 	grp->member_cnt++;
-	tipc_group_add_to_tree(grp, m);
 	tipc_nlist_add(&grp->dests, m->node);
 	m->state = state;
 	return m;