net: require CAP_NET_ADMIN in the device netns for tunnel changelink
Subsystems (5)
v4 cover net: require CAP_NET_ADMIN in the device netns for tunnel changelink
A tunnel changelink rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that creation netns is not the caller's. The rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns, so a caller with caps only in its current netns can rewrite a tunnel that lives in the creation netns, and it picks the endpoint addresses. Commit 8b484efd5cb4 ("ip6: vti: Use ip6_tnl.net in vti6_siocdevprivate().") added the same check on the ioctl path. This series adds it on the RTM_NEWLINK path.
Each changelink is gated at the top of the op, before any attribute is parsed, because the per-type parsers can update live tunnel fields first. For example ipgre_netlink_parms() sets t->collect_md before ip_tunnel_changelink() runs. The check is skipped when the creation netns equals the device's current netns, where the rtnl path already checked the cap.
This is the same fix as v3, restructured after Paolo's review:
- Split into one patch per tunnel, each with its own Fixes tag.
- Move the repeated check into a helper, net_admin_capable(), added in patch 1 and used by the rest of the series.
Tested on net/main. For every tunnel type in the series a migrated fake-root changelink is rejected with EPERM. For vti6 SIOCGETTUNNEL confirms the creation netns hash is left unchanged. Legit non-migrated changelinks still succeed.
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
Maoyi Xie (7):
net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
net: ipip: require CAP_NET_ADMIN in the device netns for changelink
net: ip_vti: require CAP_NET_ADMIN in the device netns for changelink
net: ip6_tunnel: require CAP_NET_ADMIN in the device netns for
changelink
net: ip6_gre: require CAP_NET_ADMIN in the device netns for changelink
net: ip6_vti: require CAP_NET_ADMIN in the device netns for changelink
xfrm: xfrm_interface: require CAP_NET_ADMIN in the device netns for
changelink
include/net/net_namespace.h | 18 ++++++++++++++++++ net/ipv4/ip_gre.c | 6 ++++++ net/ipv4/ip_vti.c | 3 +++ net/ipv4/ipip.c | 3 +++ net/ipv6/ip6_gre.c | 6 ++++++ net/ipv6/ip6_tunnel.c | 3 +++ net/ipv6/ip6_vti.c | 3 +++ net/xfrm/xfrm_interface_core.c | 3 +++ 8 files changed, 45 insertions(+)
base-commit: 0aa05daef7848a5ac11158949dc73cd741995dc1
Signature · 1 line
2.34.1
PATCH v4 1/7 net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
A tunnel changelink rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's. The rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel that lives in another netns, and it picks the endpoint addresses.
Add net_admin_capable(). It requires CAP_NET_ADMIN in the tunnel's netns and is skipped when that netns is the device's current netns, where the rtnl path already checked the cap. The other patches in this series use the same helper.
Gate ipgre_changelink() and erspan_changelink() with it. The check is at the top of the op, before any attribute is parsed, because the parsers update live tunnel fields first. ipgre_netlink_parms() sets t->collect_md before ip_tunnel_changelink() runs.
Commit 8b484efd5cb4 ("ip6: vti: Use ip6_tnl.net in vti6_siocdevprivate().") added the same check on the ioctl path. This adds it on RTM_NEWLINK.
--- include/net/net_namespace.h | 18 ++++++++++++++++++ net/ipv4/ip_gre.c | 6 ++++++ 2 files changed, 24 insertions(+)
include/net/net_namespace.h +18 -0
@@ -358,6 +358,24 @@ static inline bool net_initialized(const struct net *net)return READ_ONCE (net->list.next); } +/** +* net_admin_capable - test for CAP_NET_ADMIN over a network namespace +* @net: namespace whose state the operation would change +* @cur: namespace the operation runs in, e.g. dev_net(dev) +* +* Returns true when @net is @cur, where CAP_NET_ADMIN was already +* checked for the running namespace, or when the caller holds +* CAP_NET_ADMIN over @net. rtnl changelink paths use this: a device can +* be moved so its state lives in a namespace other than the one the +* request runs in, and the cap must then be held over that namespace. +*/ +static inline bool net_admin_capable (const struct net *net, +const struct net *cur) +{ +return net_eq (net, cur) || +ns_capable (net->user_ns,CAP_NET_ADMIN ); +} + static inline void __netns_tracker_alloc(struct net *net, netns_tracker *tracker, bool refcounted,
net/ipv4/ip_gre.c +6 -0
@@ -1457,6 +1457,9 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],__u32 fwmark = t->fwmark;int err; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + err =ipgre_newlink_encap_setup (dev, data);if (err)return err;
@@ -1486,6 +1489,9 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],__u32 fwmark = t->fwmark;int err; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + err =ipgre_newlink_encap_setup (dev, data);if (err)return err;
Signature · 1 line
2.34.1
Re: [PATCH net v4 1/7] net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
Quoted 6 lines · Maoyi Xie
A tunnel changelink rewrites the tunnel in its creation netns. After an
IFLA_NET_NS_FDmigration that netns is not the caller's. The rtnl changelink path only checksCAP_NET_ADMINagainst the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel that lives in another netns, and it picks the endpoint addresses.
nit: This paragraph is not precise (e.g. even without netns migration a device can use two netns w/ IFLA_LINK_NETNSID, "current netns" sounds like current->nsproxy->net_ns but not w/ IFLA_NET_NS_PID etc, also I don't get the last sentence after "it picks...").
Simply state that changelink() operates on at most two netns, dev_net() and link_net.
include/net/net_namespace.h
Quoted 29 lines
Add
net_admin_capable(). It requiresCAP_NET_ADMINin the tunnel's netns and is skipped when that netns is the device's current netns, where the rtnl path already checked the cap. The other patches in this series use the same helper.Gate
ipgre_changelink()anderspan_changelink()with it. The check is at the top of the op, before any attribute is parsed, because the parsers update live tunnel fields first.ipgre_netlink_parms()setst->collect_mdbeforeip_tunnel_changelink()runs.Commit
8b484efd5cb4("ip6: vti: Use ip6_tnl.net invti6_siocdevprivate().") added the same check on the ioctl path. This adds it onRTM_NEWLINK.Reported-by Xiao Liang <[email protected]>Closes https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/ open hereFixes d0f418516022 ("net, ip_tunnel: fix namespaces move")Signed-off-by Maoyi Xie <[email protected]>--- include/net/net_namespace.h | 18 ++++++++++++++++++ net/ipv4/ip_gre.c | 6 ++++++ 2 files changed, 24 insertions(+)include/net/net_namespace.h +0
-0
net/core/rtnetlink.c is a better fit.
It has rtnl_get_net_ns_capable().
Quoted 16 lines
include/net/net_namespace.h +7
-0@@ -358,6 +358,24 @@ static inline bool net_initialized(const struct net *net)return READ_ONCE (net->list.next); } +/** +* net_admin_capable - test for CAP_NET_ADMIN over a network namespace +* @net: namespace whose state the operation would change +* @cur: namespace the operation runs in, e.g. dev_net(dev) +* +* Returns true when @net is @cur, where CAP_NET_ADMIN was already +* checked for the running namespace, or when the caller holds
+ *CAP_NET_ADMINover @net. rtnl changelink paths use this: a device can
+ * be moved so its state lives in a namespace other than the one the
+ * request runs in, and the cap must then be held over that namespace.
+ */
include/net/net_namespace.h static inline bool net_initialized(const struct net *net) · line 373
Quoted 2 lines
include/net/net_namespace.h +2
-0+static inline bool net_admin_capable (const struct net *net, +const struct net *cur)
Rename the helper and change args to
rtnl_dev_link_net_capable(const struct net_device *dev, const struct net *link_net)
since the netns we care about here is rtnl_newlink_params.link_net (if specified) and dev_net() is redundant in all callers.
Also remove kdoc, it just reiterates the two conditions below.
Quoted 35 lines
include/net/net_namespace.h +5
-0+{ +return net_eq (net, cur) || +ns_capable (net->user_ns,CAP_NET_ADMIN ); +} + static inline void __netns_tracker_alloc(struct net *net, netns_tracker *tracker, bool refcounted,net/ipv4/ip_gre.c +6
-1@@ -1457,6 +1457,9 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],__u32 fwmark = t->fwmark;int err; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + err =ipgre_newlink_encap_setup (dev, data);if (err)return err;@@ -1486,6 +1489,9 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],__u32 fwmark = t->fwmark;int err; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + err =ipgre_newlink_encap_setup (dev, data);if (err)return err; --2.34.1
PATCH v4 2/7 net: ipip: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieipip_changelink() rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel in another netns and pick its endpoint addresses.
Gate the op on net_admin_capable() at its top, before any attribute is parsed. The check is skipped when the tunnel netns is the device's current netns, where the rtnl path already checked the cap.
--- net/ipv4/ipip.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv4/ipip.c +3 -0
@@ -494,6 +494,9 @@ static int ipip_changelink(struct net_device *dev, struct nlattr *tb[],bool collect_md;__u32 fwmark = t->fwmark; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; +if (ip_tunnel_netlink_encap_parms (data, &ipencap)) {int err =ip_tunnel_encap_setup (t, &ipencap);
Signature · 1 line
2.34.1
PATCH v4 3/7 net: ip_vti: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xievti_changelink() rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel in another netns and pick its endpoint addresses.
Gate the op on net_admin_capable() at its top, before any attribute is parsed. The check is skipped when the tunnel netns is the device's current netns, where the rtnl path already checked the cap.
--- net/ipv4/ip_vti.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv4/ip_vti.c +3 -0
@@ -596,6 +596,9 @@ static int vti_changelink(struct net_device *dev, struct nlattr *tb[],struct ip_tunnel_parm_kern p;__u32 fwmark = t->fwmark; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; +vti_netlink_parms (data, &p, &fwmark);return ip_tunnel_changelink (dev, tb, &p, fwmark); }
Signature · 1 line
2.34.1
PATCH v4 4/7 net: ip6_tunnel: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieip6_tnl_changelink() rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel in another netns and pick its endpoint addresses.
Gate the op on net_admin_capable() at its top, before any attribute is parsed. The check is skipped when the tunnel netns is the device's current netns, where the rtnl path already checked the cap.
--- net/ipv6/ip6_tunnel.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv6/ip6_tunnel.c +3 -0
@@ -2102,6 +2102,9 @@ static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],struct ip6_tnl_net *ip6n =net_generic (net, ip6_tnl_net_id);struct ip_tunnel_encap ipencap; +if (!net_admin_capable (net,dev_net (dev))) +return -EPERM ; +if (dev == ip6n->fb_tnl_dev) {if (ip_tunnel_netlink_encap_parms (data, &ipencap)) {/* iproute2 always sets TUNNEL_ENCAP_FLAG_CSUM6, so
Signature · 1 line
2.34.1
PATCH v4 5/7 net: ip6_gre: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieip6gre_changelink() and ip6erspan_changelink() rewrite the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel in another netns and pick its endpoint addresses.
Gate both ops on net_admin_capable() at their top, before any attribute is parsed. The check is skipped when the tunnel netns is the device's current netns, where the rtnl path already checked the cap.
--- net/ipv6/ip6_gre.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/ipv6/ip6_gre.c +6 -0
@@ -2047,6 +2047,9 @@ static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],struct ip6gre_net *ign =net_generic (t->net, ip6gre_net_id);struct __ip6_tnl_parm p; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + t =ip6gre_changelink_common (dev, tb, data, &p, extack);if (IS_ERR (t))return PTR_ERR (t);
@@ -2266,6 +2269,9 @@ static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],struct __ip6_tnl_parm p;struct ip6gre_net *ign; +if (!net_admin_capable (t->net,dev_net (dev))) +return -EPERM ; + ign =net_generic (t->net, ip6gre_net_id); t =ip6gre_changelink_common (dev, tb, data, &p, extack);if (IS_ERR (t))
Signature · 1 line
2.34.1
PATCH v4 6/7 net: ip6_vti: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xievti6_changelink() rewrites the tunnel in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite a tunnel in another netns and pick its endpoint addresses.
Gate the op on net_admin_capable() at its top, before any attribute is parsed. The check is skipped when the tunnel netns is the device's current netns, where the rtnl path already checked the cap.
--- net/ipv6/ip6_vti.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv6/ip6_vti.c +3 -0
@@ -1044,6 +1044,9 @@ static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],struct __ip6_tnl_parm p;struct vti6_net *ip6n; +if (!net_admin_capable (net,dev_net (dev))) +return -EPERM ; + ip6n =net_generic (net, vti6_net_id);if (dev == ip6n->fb_tnl_dev)return -EINVAL ;
Signature · 1 line
2.34.1
PATCH v4 7/7 xfrm: xfrm_interface: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xiexfrmi_changelink() rewrites the interface in its creation netns. After an IFLA_NET_NS_FD migration that netns is not the caller's, but the rtnl changelink path only checks CAP_NET_ADMIN against the caller's netns. A caller with caps only in its current netns can then rewrite an interface in another netns.
Gate the op on net_admin_capable() at its top, before any attribute is parsed. The check is skipped when the interface netns is the device's current netns, where the rtnl path already checked the cap.
--- net/xfrm/xfrm_interface_core.c | 3 +++ 1 file changed, 3 insertions(+)
net/xfrm/xfrm_interface_core.c +3 -0
@@ -869,6 +869,9 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],struct net *net = xi->net;struct xfrm_if_parms p = {}; +if (!net_admin_capable (net,dev_net (dev))) +return -EPERM ; +xfrmi_netlink_parms (data, &p);if (!p.if_id) {NL_SET_ERR_MSG (extack,"if_id must be non zero" );
Signature · 1 line
2.34.1