Skip to contentSkip to conversation

ipnetns: use-after-free problem in get_netnsid_from_name func

PATCH Applied Zhiqiang Liu 3 participants 7 messages May 4, 2019 – May 6, 2019 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 22 lines

Hi,

Quoted 13 lines · Zhiqiang Liu

From: Zhiqiang Liu <[email protected]>

Follow the following steps:

# ip netns add net1
# export MALLOC_MMAP_THRESHOLD_=0
# ip netns list
then Segmentation fault (core dumped) will occur.

In get_netnsid_from_name func, answer is freed before rta_getattr_u32(tb[NETNSA_NSID]), where tb[] refers to answer`s content. If we set MALLOC_MMAP_THRESHOLD_=0, mmap will be adoped to malloc memory, which will be freed immediately after calling free func. So reading tb[NETNSA_NSID] will access the released memory after free(answer).

Quoted 5 lines

Here, we will call get_netnsid_from_name(tb[NETNSA_NSID]) before free(answer).

Fixes 86bf43c7c2f ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")
Quoted 5 lines

Please always Cc: [email protected] for iproute2 patches.

Thanks, Phil

Thank you for reminding me. I will Cc: [email protected] in the v3 patch.

Quoted 3 lines

.

#4 lore raw

PATCH v3 1/1 ipnetns: use-after-free problem in get_netnsid_from_name func

In reply to a message not in the archive

From: Zhiqiang Liu <[email protected]>

Follow the following steps:

# ip netns add net1
# export MALLOC_MMAP_THRESHOLD_=0
# ip netns list
then Segmentation fault (core dumped) will occur.

In get_netnsid_from_name func, answer is freed before rta_getattr_u32(tb[NETNSA_NSID]), where tb[] refers to answer`s content. If we set MALLOC_MMAP_THRESHOLD_=0, mmap will be adoped to malloc memory, which will be freed immediately after calling free func. So reading tb[NETNSA_NSID] will access the released memory after free(answer).

Here, we will call get_netnsid_from_name(tb[NETNSA_NSID]) before free(answer).

Fixes 86bf43c7c2f ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")

---
v2->v3: add Cc:[email protected] suggested by Phil Sutter v1->v2: correct commit log

 ip/ipnetns.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
ip/ipnetns.c +3 -2
index 430d884..d72be95 100644
@@ -107,7 +107,7 @@ int get_netnsid_from_name(const char *name)
 	struct nlmsghdr *answer;
 	struct rtattr *tb[NETNSA_MAX + 1];
 	struct rtgenmsg *rthdr;
-	int len, fd;
+	int len, fd, ret = -1;
 
 	netns_nsid_socket_init();
 
@@ -134,8 +134,9 @@ int get_netnsid_from_name(const char *name)
 	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
 
 	if (tb[NETNSA_NSID]) {
+		ret = rta_getattr_u32(tb[NETNSA_NSID]);
 		free(answer);
-		return rta_getattr_u32(tb[NETNSA_NSID]);
+		return ret;
 	}
 
 err_out:
Signature · 1 line
1.8.3.1
#5 lore raw
Quoted 15 lines · Zhiqiang Liu
ip/ipnetns.c +1 -1
index 430d884..d72be95 100644
@@ -107,7 +107,7 @@ int get_netnsid_from_name(const char *name)
 	struct nlmsghdr *answer;
 	struct rtattr *tb[NETNSA_MAX + 1];
 	struct rtgenmsg *rthdr;
-	int len, fd;
+	int len, fd, ret = -1;
 
 	netns_nsid_socket_init();
 

ip/ipnetns.c int get_netnsid_from_name(const char *name) · line 139

Quoted 7 lines
ip/ipnetns.c +2 -1
@@ -134,8 +134,9 @@ int get_netnsid_from_name(const char *name)
 	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
 
 	if (tb[NETNSA_NSID]) {
+		ret = rta_getattr_u32(tb[NETNSA_NSID]);
 		free(answer);
-		return rta_getattr_u32(tb[NETNSA_NSID]);
+		return ret;

set ret here, drop the free, let it proceed down to the existing free and return but now using ret. That way there is 1 exit path and handles the cleanup.

Quoted 4 lines
ip/ipnetns.c +0 -0
 	}
 
 err_out:
#6 lore raw
Quoted 18 lines · Zhiqiang Liu
Quoted 17 lines
ip/ipnetns.c +1 -1
index 430d884..d72be95 100644
@@ -107,7 +107,7 @@ int get_netnsid_from_name(const char *name)
 	struct nlmsghdr *answer;
 	struct rtattr *tb[NETNSA_MAX + 1];
 	struct rtgenmsg *rthdr;
-	int len, fd;
+	int len, fd, ret = -1;
 
 	netns_nsid_socket_init();
 
@@ -134,8 +134,9 @@ int get_netnsid_from_name(const char *name)
 	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
 
Quoted 9 lines

ip/ipnetns.c int get_netnsid_from_name(const char *name) · line 139

Quoted 5 lines
ip/ipnetns.c +2 -1
 	if (tb[NETNSA_NSID]) {
+		ret = rta_getattr_u32(tb[NETNSA_NSID]);
 		free(answer);
-		return rta_getattr_u32(tb[NETNSA_NSID]);
+		return ret;

set ret here, drop the free, let it proceed down to the existing free and return but now using ret. That way there is 1 exit path and handles the cleanup

Yes, you are right. I will do that in the v4 patch. That is very nice of you. Thanks a lot.

#7 lore raw

PATCH v4 1/1 ipnetns: use-after-free problem in get_netnsid_from_name func

In reply to Zhiqiang Liu

From: Zhiqiang Liu <[email protected]>

Follow the following steps:

# ip netns add net1
# export MALLOC_MMAP_THRESHOLD_=0
# ip netns list
then Segmentation fault (core dumped) will occur.

In get_netnsid_from_name func, answer is freed before rta_getattr_u32(tb[NETNSA_NSID]), where tb[] refers to answer`s content. If we set MALLOC_MMAP_THRESHOLD_=0, mmap will be adoped to malloc memory, which will be freed immediately after calling free func. So reading tb[NETNSA_NSID] will access the released memory after free(answer).

Here, we will call get_netnsid_from_name(tb[NETNSA_NSID]) before free(answer).

Fixes 86bf43c7c2f ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")

---
v3->v4: optimize code suggested by David Ahern v2->v3: add Cc:[email protected] suggested by Phil Sutter v1->v2: correct commit log

 ip/ipnetns.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
ip/ipnetns.c +6 -7
index 430d884..52aefac 100644
@@ -107,7 +107,7 @@ int get_netnsid_from_name(const char *name)
 	struct nlmsghdr *answer;
 	struct rtattr *tb[NETNSA_MAX + 1];
 	struct rtgenmsg *rthdr;
-	int len, fd;
+	int len, fd, ret = -1;
 
 	netns_nsid_socket_init();
 
@@ -124,23 +124,22 @@ int get_netnsid_from_name(const char *name)
 
 	/* Validate message and parse attributes */
 	if (answer->nlmsg_type == NLMSG_ERROR)
-		goto err_out;
+		goto out;
 
 	rthdr = NLMSG_DATA(answer);
 	len = answer->nlmsg_len - NLMSG_SPACE(sizeof(*rthdr));
 	if (len < 0)
-		goto err_out;
+		goto out;
 
 	parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
 
 	if (tb[NETNSA_NSID]) {
-		free(answer);
-		return rta_getattr_u32(tb[NETNSA_NSID]);
+		ret = rta_getattr_u32(tb[NETNSA_NSID]);
 	}
 
-err_out:
+out:
 	free(answer);
-	return -1;
+	return ret;
 }
 
 struct nsid_cache {
Signature · 1 line
1.8.3.1
#8 lore raw
In reply to Zhiqiang Liu
Quoted 13 lines · On Sat, 4 May 2019 15:26:25 +0800 Zhiqiang Liu <[email protected]> wrote:

From: Zhiqiang Liu <[email protected]>

Follow the following steps:

# ip netns add net1
# export MALLOC_MMAP_THRESHOLD_=0
# ip netns list
then Segmentation fault (core dumped) will occur.

In get_netnsid_from_name func, answer is freed before rta_getattr_u32(tb[NETNSA_NSID]), where tb[] refers to answer`s content. If we set MALLOC_MMAP_THRESHOLD_=0, mmap will be adoped to malloc memory, which will be freed immediately after calling free func. So reading tb[NETNSA_NSID] will access the released memory after free(answer).

Quoted 6 lines

Here, we will call get_netnsid_from_name(tb[NETNSA_NSID]) before free(answer).

Fixes 86bf43c7c2f ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")

Applied. You can get better and more detailed checks by running with valgrind. Which is what I did after applying your patch.

#9 lore raw
Quoted 18 lines
Quoted 15 lines · On Sat, 4 May 2019 15:26:25 +0800 Zhiqiang Liu <[email protected]> wrote:

From: Zhiqiang Liu <[email protected]>

Follow the following steps:

# ip netns add net1
# export MALLOC_MMAP_THRESHOLD_=0
# ip netns list
then Segmentation fault (core dumped) will occur.

In get_netnsid_from_name func, answer is freed before rta_getattr_u32(tb[NETNSA_NSID]), where tb[] refers to answer`s content. If we set MALLOC_MMAP_THRESHOLD_=0, mmap will be adoped to malloc memory, which will be freed immediately after calling free func. So reading tb[NETNSA_NSID] will access the released memory after free(answer).

Here, we will call get_netnsid_from_name(tb[NETNSA_NSID]) before free(answer).

Quoted 7 lines
Quoted 4 lines
Fixes 86bf43c7c2f ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")

Applied. You can get better and more detailed checks by running with valgrind. Which is what I did after applying your patch.

Thank you for your advice. I will learn how to use valgrind, and use it to obtain more detailed checks.

Quoted 3 lines

.