net: require CAP_NET_ADMIN in the device netns for tunnel changelink
Subsystems (5)
v5 cover net: require CAP_NET_ADMIN in the device netns for tunnel changelink
A tunnel changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in the link netns can rewrite a tunnel that lives in the link netns. 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). The check is skipped when the link netns is dev_net(dev), where the rtnl path already checked the cap.
The check goes through a new helper, rtnl_dev_link_net_capable(), added in patch 1 next to rtnl_get_net_ns_capable() in net/core/rtnetlink.c.
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 link netns hash is left unchanged. Legit non-migrated changelinks still succeed.
v4 -> v5, addressing Kuniyuki Iwashima's review:
- Move the helper to net/core/rtnetlink.c next to rtnl_get_net_ns_capable(), rename it to rtnl_dev_link_net_capable(dev, link_net), and drop the kdoc.
- Reword each commit message to state that changelink() operates on at most two netns, dev_net(dev) and the link netns.
v4: https://lore.kernel.org/netdev/[email protected]/
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/rtnetlink.h | 2 ++ net/core/rtnetlink.c | 8 ++++++++ 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 +++ 9 files changed, 37 insertions(+)
base-commit: 512db8267b73a220a64180d95ab5eebe7c4964a8
Signature · 1 line
2.34.1
PATCH v5 1/7 net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
A tunnel changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Add rtnl_dev_link_net_capable() next to rtnl_get_net_ns_capable() in net/core/rtnetlink.c. It requires CAP_NET_ADMIN in the link netns and is skipped when the link netns is dev_net(dev), where the rtnl path already checked it. The other patches in this series use the same helper.
Gate ipgre_changelink() and erspan_changelink() with it, 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/rtnetlink.h | 2 ++ net/core/rtnetlink.c | 8 ++++++++ net/ipv4/ip_gre.c | 6 ++++++ 3 files changed, 16 insertions(+)
include/net/rtnetlink.h +2 -0
@@ -256,6 +256,8 @@ int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,int rtnl_nla_parse_ifinfomsg (struct nlattr **tb,const struct nlattr *nla_peer,struct netlink_ext_ack *exterr);struct net *rtnl_get_net_ns_capable (struct sock *sk,int netnsid); +bool rtnl_dev_link_net_capable (const struct net_device *dev, +const struct net *link_net);#define MODULE_ALIAS_RTNL_LINK (kind) MODULE_ALIAS("rtnl-link-" kind)
net/core/rtnetlink.c +8 -0
@@ -2421,6 +2421,14 @@ struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) }EXPORT_SYMBOL_GPL (rtnl_get_net_ns_capable); +bool rtnl_dev_link_net_capable (const struct net_device *dev, +const struct net *link_net) +{ +return net_eq (link_net,dev_net (dev)) || +ns_capable (link_net->user_ns,CAP_NET_ADMIN ); +} +EXPORT_SYMBOL_GPL (rtnl_dev_link_net_capable); +static int rtnl_valid_dump_ifinfo_req (const struct nlmsghdr *nlh,bool strict_check,struct nlattr **tb,struct netlink_ext_ack *extack)
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 (!rtnl_dev_link_net_capable (dev, t->net)) +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 (!rtnl_dev_link_net_capable (dev, t->net)) +return -EPERM ; + err =ipgre_newlink_encap_setup (dev, data);if (err)return err;
Signature · 1 line
2.34.1
Re: [PATCH net v5 1/7] net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
Quoted 18 lines · Maoyi Xie
A tunnel
changelink()operates on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.Add
rtnl_dev_link_net_capable()next tortnl_get_net_ns_capable()innet/core/rtnetlink.c. It requiresCAP_NET_ADMINin the link netns and is skipped when the link netns isdev_net(dev), where the rtnl path already checked it. The other patches in this series use the same helper.Gate
ipgre_changelink()anderspan_changelink()with it, 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.
Quoted 7 lines
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")
This tag looks wrong, the correct one should be
This also applies to erspan since it shared the code until e1f8f78ffe985.
Quoted 2 lines
Signed-off-by Maoyi Xie <[email protected]>
The change itself looks good.
PATCH v5 2/7 net: ipip: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieipip_changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate ipip_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- 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 (!rtnl_dev_link_net_capable (dev, t->net)) +return -EPERM ; +if (ip_tunnel_netlink_encap_parms (data, &ipencap)) {int err =ip_tunnel_encap_setup (t, &ipencap);
Signature · 1 line
2.34.1
Re: [PATCH net v5 2/7] net: ipip: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
ipip_changelink()operates on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.
Quoted 6 lines
Gate
ipip_changelink()onrtnl_dev_link_net_capable()at its top, before any attribute is parsed.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")
This is wrong too.
Quoted 2 lines
Signed-off-by Maoyi Xie <[email protected]>
PATCH v5 3/7 net: ip_vti: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xievti_changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate vti_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- 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 (!rtnl_dev_link_net_capable (dev, t->net)) +return -EPERM ; +vti_netlink_parms (data, &p, &fwmark);return ip_tunnel_changelink (dev, tb, &p, fwmark); }
Signature · 1 line
2.34.1
Re: [PATCH net v5 3/7] net: ip_vti: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
vti_changelink()operates on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.
Quoted 6 lines
Gate
vti_changelink()onrtnl_dev_link_net_capable()at its top, before any attribute is parsed.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")
Wrong tag again..
Quoted 2 lines
Signed-off-by Maoyi Xie <[email protected]>
PATCH v5 4/7 net: ip6_tunnel: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieip6_tnl_changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate ip6_tnl_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- 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 (!rtnl_dev_link_net_capable (dev, net)) +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
Re: [PATCH net v5 4/7] net: ip6_tunnel: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
ip6_tnl_changelink()operates on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.
Quoted 6 lines
Gate
ip6_tnl_changelink()onrtnl_dev_link_net_capable()at its top, before any attribute is parsed.Reported-by Xiao Liang <[email protected]>Closes https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/ open hereFixes 5311a69aaca3 ("net, ip6_tunnel: fix namespaces move")
Quoted 2 lines
Signed-off-by Maoyi Xie <[email protected]>
PATCH v5 5/7 net: ip6_gre: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xieip6gre_changelink() and ip6erspan_changelink() operate on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate both ops on rtnl_dev_link_net_capable() at their top, before any attribute is parsed.
--- 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 (!rtnl_dev_link_net_capable (dev, t->net)) +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 (!rtnl_dev_link_net_capable (dev, t->net)) +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
Re: [PATCH net v5 5/7] net: ip6_gre: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
ip6gre_changelink()andip6erspan_changelink()operate on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.
Quoted 8 lines
Gate both ops on
rtnl_dev_link_net_capable()at their top, before any attribute is parsed.Reported-by Xiao Liang <[email protected]>Closes https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/ open hereFixes 690afc165bb3 ("net: ip6_gre: fix moving ip6gre between namespaces")Signed-off-by Maoyi Xie <[email protected]>
PATCH v5 6/7 net: ip6_vti: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xievti6_changelink() operates on at most two netns, dev_net(dev) and the tunnel link netns t->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in t->net can rewrite a tunnel that lives in t->net.
Gate vti6_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- net/ipv6/ip6_vti.c | 3 +++ 1 file changed, 3 insertions(+)
net/ipv6/ip6_vti.c +3 -0
@@ -1046,6 +1046,9 @@ static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],struct __ip6_tnl_parm p;struct vti6_net *ip6n; +if (!rtnl_dev_link_net_capable (dev, net)) +return -EPERM ; + ip6n =net_generic (net, vti6_net_id);if (dev == ip6n->fb_tnl_dev)return -EINVAL ;
Signature · 1 line
2.34.1
Re: [PATCH net v5 6/7] net: ip6_vti: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
vti6_changelink()operates on at most two netns,dev_net(dev)and the tunnel link netnst->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not int->netcan rewrite a tunnel that lives int->net.
Quoted 6 lines
Gate
vti6_changelink()onrtnl_dev_link_net_capable()at its top, before any attribute is parsed.Reported-by Xiao Liang <[email protected]>Closes https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/ open hereFixes 11b326fb0a37 ("ip6: vti: Use ip6_tnl.net in vti6_changelink().")
Wrong tag again.. :/
Quoted 2 lines
Signed-off-by Maoyi Xie <[email protected]>
PATCH v5 7/7 xfrm: xfrm_interface: require CAP_NET_ADMIN in the device netns for changelink
In reply to Maoyi Xiexfrmi_changelink() operates on at most two netns, dev_net(dev) and the interface link netns xi->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checks CAP_NET_ADMIN only against dev_net(dev), so a caller privileged there but not in xi->net can rewrite an interface that lives in xi->net.
Gate xfrmi_changelink() on rtnl_dev_link_net_capable() at its top, before any attribute is parsed.
--- 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 (!rtnl_dev_link_net_capable (dev, net)) +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
Re: [PATCH net v5 7/7] xfrm: xfrm_interface: require CAP_NET_ADMIN in the device netns for changelink
Quoted 8 lines · Maoyi Xie
xfrmi_changelink()operates on at most two netns,dev_net(dev)and the interface link netnsxi->net. They differ once the device is created in or moved to a netns other than the one the request runs in. The rtnl changelink path checksCAP_NET_ADMINonly againstdev_net(dev), so a caller privileged there but not inxi->netcan rewrite an interface that lives inxi->net.
Quoted 8 lines
Gate
xfrmi_changelink()onrtnl_dev_link_net_capable()at its top, before any attribute is parsed.Reported-by Xiao Liang <[email protected]>Closes https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/ open hereFixes f203b76d7809 ("xfrm: Add virtual xfrm interfaces")Signed-off-by Maoyi Xie <[email protected]>