Skip to contentSkip to conversation

rtnetlink: require unique netns identifier

RFC In review Christian Brauner 2 participants 3 messages Feb 2, 2018

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

cover rtnetlink: require unique netns identifier

Hey,

Since we've added support for IFLA_IF_NETNSID for RTM_{DEL,GET,SET,NEW}LINK it is possible for userspace to send us requests with three different properties to identify a target network namespace. This affects at least RTM_{NEW,SET}LINK. Each of them could potentially refer to a different network namespace which is confusing. For legacy reasons the kernel will pick the IFLA_NET_NS_PID property first and then look for the IFLA_NET_NS_FD property but there is no reason to extend this type of behavior to network namespace ids. The regression potential is quite minimal since the rtnetlink requests in question either won't allow IFLA_IF_NETNSID requests before 4.16 is out (RTM_{NEW,SET}LINK) or don't support IFLA_NET_NS_{PID,FD} (RTM_{DEL,GET}LINK) in the first place. But maybe I'm missing a reason for allowing multiple properties in the request.

We obviously cannot prevent users from passing both IFLA_NET_NS_PID and IFLA_NET_NS_FD since we have supported this somehow for a long time. So the check I'm proposing is to only fail when both IFLA_IF_NETNSID, and IFLA_NET_NS_PID or IFLA_NET_NS_FD are passed.

Thanks!
Christian

Christian Brauner (1):
rtnetlink: require unique netns identifier

 net/core/rtnetlink.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
Signature · 1 line
2.14.1
#1 lore raw

RFC 1/1 rtnetlink: require unique netns identifier

Since we've added support for IFLA_IF_NETNSID for RTM_{DEL,GET,SET,NEW}LINK it is possible for userspace to send us requests with three different properties to identify a target network namespace. This affects at least RTM_{NEW,SET}LINK. Each of them could potentially refer to a different network namespace which is confusing. For legacy reasons the kernel will pick the IFLA_NET_NS_PID property first and then look for the IFLA_NET_NS_FD property but there is no reason to extend this type of behavior to network namespace ids. The regression potential is quite minimal since the rtnetlink requests in question either won't allow IFLA_IF_NETNSID requests before 4.16 is out (RTM_{NEW,SET}LINK) or don't support IFLA_NET_NS_{PID,FD} (RTM_{DEL,GET}LINK) in the first place.

---
 net/core/rtnetlink.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
net/core/rtnetlink.c +28 -0
index 56af8e41abfc..d0b7ab22eff4 100644
@@ -1951,6 +1951,18 @@ static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
 	return net;
 }
 
+/* Verify that rtnetlink requests that support network namespace ids do not pass
+ * additional properties that allow to identify a network namespace as they
+ * might conflict.
+ */
+static int rtnl_ensure_unique_netns_attr(struct nlattr *tb[])
+{
+	if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
+		return -EINVAL;
+
+	return 0;
+}
+
 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
 {
 	if (dev) {
@@ -2553,6 +2565,10 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err < 0)
 		goto errout;
 
+	err = rtnl_ensure_unique_netns_attr(tb);
+	if (err < 0)
+		goto errout;
+
 	if (tb[IFLA_IFNAME])
 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
 	else
@@ -2649,6 +2665,10 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err < 0)
 		return err;
 
+	err = rtnl_ensure_unique_netns_attr(tb);
+	if (err < 0)
+		return err;
+
 	if (tb[IFLA_IFNAME])
 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
 
@@ -2802,6 +2822,10 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err < 0)
 		return err;
 
+	err = rtnl_ensure_unique_netns_attr(tb);
+	if (err < 0)
+		return err;
+
 	if (tb[IFLA_IFNAME])
 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
 	else
@@ -3045,6 +3069,10 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err < 0)
 		return err;
 
+	err = rtnl_ensure_unique_netns_attr(tb);
+	if (err < 0)
+		return err;
+
 	if (tb[IFLA_IF_NETNSID]) {
 		netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
 		tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
Signature · 1 line
2.14.1
#2 lore raw
Quoted 6 lines · Christian Brauner
net/core/rtnetlink.c +0 -0
index 56af8e41abfc..d0b7ab22eff4 100644
@@ -1951,6 +1951,18 @@ static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
 	return net;

net/core/rtnetlink.c static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, · line 1961

Quoted 10 lines
net/core/rtnetlink.c +8 -0
 }
 
+/* Verify that rtnetlink requests that support network namespace ids do not pass
+ * additional properties that allow to identify a network namespace as they
+ * might conflict.
+ */
+static int rtnl_ensure_unique_netns_attr(struct nlattr *tb[])
+{
+	if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
+		return -EINVAL;

The days of just returning EINVAL are over; please plumb extack arg to this message and add a string describing the problem. There are plenty of examples in rtnetlink.c

Also, what if those NSID's all point to the same namespace? That should not fail right?