]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
net: ip_gre: require CAP_NET_ADMIN in the device netns for changelink
authorMaoyi Xie <maoyixie.tju@gmail.com>
Fri, 12 Jun 2026 08:59:35 +0000 (16:59 +0800)
committerJakub Kicinski <kuba@kernel.org>
Wed, 17 Jun 2026 23:01:52 +0000 (16:01 -0700)
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.

Reported-by: Xiao Liang <shaw.leon@gmail.com>
Closes: https://lore.kernel.org/netdev/CABAhCOSzP1vaThGV35_VnsRCb=87_CPjPVsTHbq905k8A+BuUg@mail.gmail.com/
Fixes: b57708add314 ("gre: add x-netns support")
Cc: stable@vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20260612085941.3158249-2-maoyixie.tju@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/rtnetlink.h
net/core/rtnetlink.c
net/ipv4/ip_gre.c

index ec65a8cebb9922bc05e4f55b00b39997bc44fbda..2bff41aacc987182468cc409b9350d765bd96ee3 100644 (file)
@@ -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)
 
index 61d095ce1b3b98a69649d03833fc0dcb86437554..12aa3aa1688b13f7f1523130ac4684ce48d2aa5c 100644 (file)
@@ -2438,6 +2438,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)
index 208dd48012d963b9df0eddbdda73dd319930e48f..3efdfb4ffa2112db3b056169d02d28e8853873ae 100644 (file)
@@ -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;