Skip to contentSkip to conversation

netns, rtnetlink: fix struct net reference leak

PATCH In review Craig Gallek 2 participants 3 messages Dec 22, 2017 – Dec 29, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 netns, rtnetlink: fix struct net reference leak

From: Craig Gallek <[email protected]>

netns ids were added in commit 0c7aecd4bde4 and defined as signed integers in both the kernel datastructures and the netlink interface. However, the semantics of the implementation assume that the ids are always greater than or equal to zero, except for an internal sentinal value NETNSA_NSID_NOT_ASSIGNED.

Several subsequent patches carried this pattern forward. This patch updates all of the netlink input paths of this value to enforce the 'greater than or equal to zero' constraint.

This issue was discovered by syskaller. It would set a negative value for a netns id and then repeatedly call the RTM_GETLINK. The put_net call in that path would not trigger for negative netns ids, caused a reference count leak, and eventually overflowed. There are probably additional error paths that do not handle this situation correctly, but this was the only one I was able to trigger a real issue through.

Fixes 0c7aecd4bde4 ("netns: add rtnl cmd to add and get peer netns ids")
Fixes 317f4810e45e ("rtnl: allow to create device with IFLA_LINK_NETNSID set")
Fixes 79e1ad148c84 ("rtnetlink: use netnsid to query interface")
CC Jiri Benc <[email protected]>
CC Nicolas Dichtel <[email protected]>
CC Jason A. Donenfeld <[email protected]>
---
 net/core/net_namespace.c |  2 ++
 net/core/rtnetlink.c     | 17 +++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
net/core/net_namespace.c +2 -0
index 60a71be75aea..4b7ea33f5705 100644
@@ -627,6 +627,8 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EINVAL;
 	}
 	nsid = nla_get_s32(tb[NETNSA_NSID]);
+	if (nsid < 0)
+		return -EINVAL;
 
 	if (tb[NETNSA_PID]) {
 		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
net/core/rtnetlink.c +13 -4
index dabba2a91fc8..a928b8f081b8 100644
@@ -1733,10 +1733,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 			ifla_policy, NULL) >= 0) {
 		if (tb[IFLA_IF_NETNSID]) {
 			netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
-			tgt_net = get_target_net(skb, netnsid);
-			if (IS_ERR(tgt_net)) {
-				tgt_net = net;
-				netnsid = -1;
+			if (netnsid >= 0) {
+				tgt_net = get_target_net(skb, netnsid);
+				if (IS_ERR(tgt_net)) {
+					tgt_net = net;
+					netnsid = -1;
+				}
 			}
 		}
 
@@ -2792,6 +2794,11 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 		if (tb[IFLA_LINK_NETNSID]) {
 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
 
+			if (id < 0) {
+				err =  -EINVAL;
+				goto out;
+			}
+
 			link_net = get_net_ns_by_id(dest_net, id);
 			if (!link_net) {
 				err =  -EINVAL;
@@ -2883,6 +2890,8 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	if (tb[IFLA_IF_NETNSID]) {
 		netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
+		if (netnsid < 0)
+			return -EINVAL;
 		tgt_net = get_target_net(skb, netnsid);
 		if (IS_ERR(tgt_net))
 			return PTR_ERR(tgt_net);
Signature · 1 line
2.15.1.620.gb9897f4670-goog
#1 lore raw

Le 22/12/2017 à 21:36, Craig Gallek a écrit :

Quoted 37 lines

From: Craig Gallek <[email protected]>

netns ids were added in commit 0c7aecd4bde4 and defined as signed integers in both the kernel datastructures and the netlink interface. However, the semantics of the implementation assume that the ids are always greater than or equal to zero, except for an internal sentinal value NETNSA_NSID_NOT_ASSIGNED.

Several subsequent patches carried this pattern forward. This patch updates all of the netlink input paths of this value to enforce the 'greater than or equal to zero' constraint.

This issue was discovered by syskaller. It would set a negative value for a netns id and then repeatedly call the RTM_GETLINK. The put_net call in that path would not trigger for negative netns ids, caused a reference count leak, and eventually overflowed. There are probably additional error paths that do not handle this situation correctly, but this was the only one I was able to trigger a real issue through.

Fixes 0c7aecd4bde4 ("netns: add rtnl cmd to add and get peer netns ids")
Fixes 317f4810e45e ("rtnl: allow to create device with IFLA_LINK_NETNSID set")
Fixes 79e1ad148c84 ("rtnetlink: use netnsid to query interface")
CC Jiri Benc <[email protected]>
CC Nicolas Dichtel <[email protected]>
CC Jason A. Donenfeld <[email protected]>
---
 net/core/net_namespace.c |  2 ++
 net/core/rtnetlink.c     | 17 +++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
net/core/net_namespace.c +0 -0
index 60a71be75aea..4b7ea33f5705 100644

net/core/net_namespace.c static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh, · line 631

Quoted 5 lines
net/core/net_namespace.c +2 -0
@@ -627,6 +627,8 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EINVAL;
 	}
 	nsid = nla_get_s32(tb[NETNSA_NSID]);
+	if (nsid < 0)
+		return -EINVAL;

No, this breaks the current behavior.
Look at alloc_netid(). If reqid is < 0, the kernel allocates an nsid with no constraint. If reqid is >= 0, it tries to alloc the specified nsid.

Quoted 8 lines
net/core/net_namespace.c +0 -0
 	if (tb[NETNSA_PID]) {
 		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
net/core/rtnetlink.c +0 -0
index dabba2a91fc8..a928b8f081b8 100644

net/core/rtnetlink.c static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) · line 1737

Quoted 9 lines
net/core/rtnetlink.c +2 -4
@@ -1733,10 +1733,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 			ifla_policy, NULL) >= 0) {
 		if (tb[IFLA_IF_NETNSID]) {
 			netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
-			tgt_net = get_target_net(skb, netnsid);
-			if (IS_ERR(tgt_net)) {
-				tgt_net = net;
-				netnsid = -1;
+			if (netnsid >= 0) {
+				tgt_net = get_target_net(skb, netnsid);

I would prefer to put this test in get_target_net.

net/core/rtnetlink.c static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) · line 1740

Quoted 3 lines
net/core/rtnetlink.c +3 -0
+				if (IS_ERR(tgt_net)) {
+					tgt_net = net;
+					netnsid = -1;

Maybe using NETNSA_NSID_NOT_ASSIGNED is better? Same for the initialization of this variable.

net/core/rtnetlink.c static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, · line 2801

Quoted 13 lines
net/core/rtnetlink.c +6 -0
+				}
 			}
 		}
 
@@ -2792,6 +2794,11 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 		if (tb[IFLA_LINK_NETNSID]) {
 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
 
+			if (id < 0) {
+				err =  -EINVAL;
+				goto out;
+			}
+

This is not needed. get_net_ns_by_id() returns NULL if id is < 0.

Quoted 13 lines
net/core/rtnetlink.c +2 -0
 			link_net = get_net_ns_by_id(dest_net, id);
 			if (!link_net) {
 				err =  -EINVAL;
@@ -2883,6 +2890,8 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	if (tb[IFLA_IF_NETNSID]) {
 		netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
+		if (netnsid < 0)
+			return -EINVAL;
 		tgt_net = get_target_net(skb, netnsid);
 		if (IS_ERR(tgt_net))
 			return PTR_ERR(tgt_net);
#2 lore raw
Quoted 15 lines · Nicolas Dichtel

net/core/net_namespace.c static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh, · line 631

Quoted 11 lines · Craig Gallek

From: Craig Gallek <[email protected]>

net/core/net_namespace.c +2 -0
index 60a71be75aea..4b7ea33f5705 100644
@@ -627,6 +627,8 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
              return -EINVAL;
      }
      nsid = nla_get_s32(tb[NETNSA_NSID]);
+     if (nsid < 0)
+             return -EINVAL;

No, this breaks the current behavior.
Look at alloc_netid(). If reqid is < 0, the kernel allocates an nsid with no constraint. If reqid is >= 0, it tries to alloc the specified nsid.

Ah, thanks. alloc_netid does appear to do the right thing. In fact, this seems to be another clue to the problem. The current behavior is to allocate from [0,max) when the input value is negative and the problem seems to trigger when 0 is allocated. Changing this range to [1, max) fixes the problem, so there must be code elsewhere that does not handle the case where the id is zero properly...

Quoted 33 lines
Quoted 8 lines
net/core/net_namespace.c +0 -0
      if (tb[NETNSA_PID]) {
              peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
net/core/rtnetlink.c +0 -0
index dabba2a91fc8..a928b8f081b8 100644

net/core/rtnetlink.c static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) · line 1737

Quoted 9 lines
net/core/rtnetlink.c +2 -4
@@ -1733,10 +1733,12 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
                      ifla_policy, NULL) >= 0) {
              if (tb[IFLA_IF_NETNSID]) {
                      netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
-                     tgt_net = get_target_net(skb, netnsid);
-                     if (IS_ERR(tgt_net)) {
-                             tgt_net = net;
-                             netnsid = -1;
+                     if (netnsid >= 0) {
+                             tgt_net = get_target_net(skb, netnsid);

I would prefer to put this test in get_target_net.

net/core/rtnetlink.c static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) · line 1740

Quoted 3 lines
net/core/rtnetlink.c +3 -0
+                             if (IS_ERR(tgt_net)) {
+                                     tgt_net = net;
+                                     netnsid = -1;

Maybe using NETNSA_NSID_NOT_ASSIGNED is better? Same for the initialization of this variable.

Quoted 8 lines
net/core/rtnetlink.c +1 -0
+                             }
                      }
              }
 
@@ -2792,6 +2794,11 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
              if (tb[IFLA_LINK_NETNSID]) {
                      int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
 
Quoted 6 lines

net/core/rtnetlink.c static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, · line 2801

Quoted 5 lines
net/core/rtnetlink.c +5 -0
+                     if (id < 0) {
+                             err =  -EINVAL;
+                             goto out;
+                     }
+

This is not needed. get_net_ns_by_id() returns NULL if id is < 0.

Indeed, and by extension get_target_net does not need this check either (as it calls get_net_ns_by_id).

I'm having trouble debugging this remotely, so I'll give it a whirl when I get back to the office next week.

Thanks again for the pointers,
Craig