From: Maarten de Vries <maarten@de-vri.es>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] Check CAP_NET_ADMIN in old and new ns before changing network ns.
Date: Mon, 4 Feb 2019 01:05:49 +0100 [thread overview]
Message-ID: <20190204000549.24287-1-maarten@de-vri.es> (raw)
In-Reply-To: <20190203220806.14327-1-maarten@de-vri.es>
---
Forgot to check for CAP_NET_ADMIN. Quite important actually :)
src/netlink.c | 60 +++++++++++++++++++++++++++++----------------------
1 file changed, 34 insertions(+), 26 deletions(-)
diff --git a/src/netlink.c b/src/netlink.c
index 82e9030..2999593 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -473,30 +473,10 @@ out:
return ret;
}
-static int set_tunnel_netns(struct wg_device *wg, u32 fd)
-{
- struct net *new_net;
-
- if (wg->sock4 != NULL || wg->sock6 != NULL)
- return -EINVAL;
-
- new_net = get_net_ns_by_fd(fd);
-
- if (IS_ERR(new_net))
- return PTR_ERR(new_net);
-
- if (wg->have_creating_net_ref)
- put_net(wg->creating_net);
-
- wg->have_creating_net_ref = true;
- wg->creating_net = new_net;
-
- return 0;
-}
-
static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
{
struct wg_device *wg = lookup_interface(info->attrs, skb);
+ struct net *new_net = NULL;
int ret;
if (IS_ERR(wg)) {
@@ -509,10 +489,34 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
ret = -EPERM;
if ((info->attrs[WGDEVICE_A_LISTEN_PORT] ||
- info->attrs[WGDEVICE_A_FWMARK]) &&
+ info->attrs[WGDEVICE_A_FWMARK] ||
+ info->attrs[WGDEVICE_A_TUNNEL_NETNS_FD]) &&
!ns_capable(wg->creating_net->user_ns, CAP_NET_ADMIN))
goto out;
+ if (info->attrs[WGDEVICE_A_TUNNEL_NETNS_FD]) {
+ int fd = nla_get_u32(info->attrs[WGDEVICE_A_TUNNEL_NETNS_FD]);
+ new_net = get_net_ns_by_fd(fd);
+
+ if (IS_ERR(new_net)) {
+ ret = PTR_ERR(new_net);
+ new_net = NULL;
+ goto out;
+ }
+
+ /* Also check that we've got CAP_NET_ADMIN in the new namespace. */
+ if (!ns_capable(new_net->user_ns, CAP_NET_ADMIN)) {
+ ret = -EPERM;
+ goto out;
+ }
+
+ /* And check that there are no initialized sockets. */
+ if (wg->sock4 != NULL || wg->sock6 != NULL) {
+ ret = -EINVAL;
+ goto out;
+ }
+ }
+
++wg->device_update_gen;
if (info->attrs[WGDEVICE_A_FWMARK]) {
@@ -582,15 +586,19 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
}
if (info->attrs[WGDEVICE_A_TUNNEL_NETNS_FD]) {
- int fd = nla_get_u32(info->attrs[WGDEVICE_A_TUNNEL_NETNS_FD]);
- ret = set_tunnel_netns(wg, fd);
- if (ret < 0)
- goto out;
+ if (wg->have_creating_net_ref)
+ put_net(wg->creating_net);
+
+ wg->have_creating_net_ref = true;
+ wg->creating_net = new_net;
+ new_net = NULL;
}
ret = 0;
out:
+ if (new_net)
+ put_net(new_net);
mutex_unlock(&wg->device_update_lock);
rtnl_unlock();
dev_put(wg->dev);
--
2.20.1
_______________________________________________
WireGuard mailing list
WireGuard@lists.zx2c4.com
https://lists.zx2c4.com/mailman/listinfo/wireguard
next prev parent reply other threads:[~2019-02-04 0:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-03 21:31 [PATCH] Allow changing `creator_net` after interface creation Maarten de Vries
2019-02-03 22:04 ` Julian Orth
2019-02-03 22:08 ` Maarten de Vries
2019-02-04 0:05 ` Maarten de Vries [this message]
2019-02-04 0:13 ` [PATCH] Check CAP_NET_ADMIN in old and new ns before changing network ns Maarten de Vries
2019-02-03 23:57 ` [PATCH] Allow changing `creator_net` after interface creation Jason A. Donenfeld
2019-02-04 0:09 ` Maarten de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190204000549.24287-1-maarten@de-vri.es \
--to=maarten@de-vri.es \
--cc=wireguard@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).