From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756273AbcA2OjM (ORCPT ); Fri, 29 Jan 2016 09:39:12 -0500 Received: from out01.mta.xmission.com ([166.70.13.231]:57666 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752330AbcA2OjK (ORCPT ); Fri, 29 Jan 2016 09:39:10 -0500 From: ebiederm@xmission.com (Eric W. Biederman) To: Tycho Andersen Cc: Pravin Shelar , "David S. Miller" , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, Justin Pettit In-Reply-To: <1454072433-20137-1-git-send-email-tycho.andersen@canonical.com> (Tycho Andersen's message of "Fri, 29 Jan 2016 14:00:33 +0100") References: <1454072433-20137-1-git-send-email-tycho.andersen@canonical.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Date: Fri, 29 Jan 2016 08:29:55 -0600 Message-ID: <8760yccvbw.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain X-XM-AID: U2FsdGVkX18OV3DQ7fSTJNdko6WsXf41pRgvg31wDqo= X-SA-Exim-Connect-IP: 97.121.81.63 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.0 TVD_RCVD_IP Message was received from an IP address * 0.7 XMSubLong Long Subject * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa03 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa03 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Tycho Andersen X-Spam-Relay-Country: X-Spam-Timing: total 1554 ms - load_scoreonly_sql: 0.06 (0.0%), signal_user_changed: 3.8 (0.2%), b_tie_ro: 2.7 (0.2%), parse: 1.80 (0.1%), extract_message_metadata: 49 (3.2%), get_uri_detail_list: 10 (0.6%), tests_pri_-1000: 20 (1.3%), tests_pri_-950: 2.1 (0.1%), tests_pri_-900: 1.71 (0.1%), tests_pri_-400: 58 (3.7%), check_bayes: 56 (3.6%), b_tokenize: 29 (1.8%), b_tok_get_all: 12 (0.8%), b_comp_prob: 6 (0.4%), b_tok_touch_all: 4.7 (0.3%), b_finish: 0.88 (0.1%), tests_pri_0: 1404 (90.3%), check_dkim_signature: 1.05 (0.1%), check_dkim_adsp: 4.6 (0.3%), tests_pri_500: 7 (0.5%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH] openvswitch: allow management from inside user namespaces X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Wed, 24 Sep 2014 11:00:52 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Tycho Andersen writes: > Operations with the GENL_ADMIN_PERM flag fail permissions checks because > this flag means we call netlink_capable, which uses the init user ns. > > Instead, let's do permissions checks in each function, but use the netlink > socket's user ns instead of the initial one, to allow management of > openvswitch resources from inside a user ns. > > The motivation for this is to be able to run openvswitch in unprivileged > containers. I've tested this and it seems to work, but I really have no > idea about the security consequences of this patch, so thoughts would be > much appreciated. So at a quick look using ns_capable this way is probably buggy. netlink is goofy (because historically we got this wrong), and I forget what the specific rules are. The general rule is that you need to do your permission checks on open/create/connect and not inside send/write while processing data. Otherwise there is a class of privileged applications where you can set their stdout to some precreated file descriptor and their output can be made to act as a command, bypassing your permission checks. Eric > Reported-by: James Page > Signed-off-by: Tycho Andersen > CC: Eric Biederman > CC: Pravin Shelar > CC: Justin Pettit > CC: "David S. Miller" > --- > net/openvswitch/datapath.c | 63 ++++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 53 insertions(+), 10 deletions(-) > > diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c > index deadfda..aacfb11 100644 > --- a/net/openvswitch/datapath.c > +++ b/net/openvswitch/datapath.c > @@ -557,6 +557,10 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) > int err; > bool log = !a[OVS_PACKET_ATTR_PROBE]; > > + err = -EPERM; > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + goto err; > + > err = -EINVAL; > if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || > !a[OVS_PACKET_ATTR_ACTIONS]) > @@ -654,7 +658,7 @@ static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { > > static const struct genl_ops dp_packet_genl_ops[] = { > { .cmd = OVS_PACKET_CMD_EXECUTE, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = packet_policy, > .doit = ovs_packet_cmd_execute > } > @@ -920,6 +924,10 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) > int error; > bool log = !a[OVS_FLOW_ATTR_PROBE]; > > + error = -EPERM; > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + goto error; > + > /* Must have key and actions. */ > error = -EINVAL; > if (!a[OVS_FLOW_ATTR_KEY]) { > @@ -1104,6 +1112,10 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) > bool log = !a[OVS_FLOW_ATTR_PROBE]; > bool ufid_present; > > + error = -EPERM; > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + goto error; > + > /* Extract key. */ > error = -EINVAL; > if (!a[OVS_FLOW_ATTR_KEY]) { > @@ -1274,6 +1286,9 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) > bool log = !a[OVS_FLOW_ATTR_PROBE]; > bool ufid_present; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); > if (a[OVS_FLOW_ATTR_KEY]) { > ovs_match_init(&match, &key, NULL); > @@ -1391,12 +1406,12 @@ static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { > > static const struct genl_ops dp_flow_genl_ops[] = { > { .cmd = OVS_FLOW_CMD_NEW, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = flow_policy, > .doit = ovs_flow_cmd_new > }, > { .cmd = OVS_FLOW_CMD_DEL, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = flow_policy, > .doit = ovs_flow_cmd_del > }, > @@ -1407,7 +1422,7 @@ static const struct genl_ops dp_flow_genl_ops[] = { > .dumpit = ovs_flow_cmd_dump > }, > { .cmd = OVS_FLOW_CMD_SET, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = flow_policy, > .doit = ovs_flow_cmd_set, > }, > @@ -1530,8 +1545,12 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) > struct datapath *dp; > struct vport *vport; > struct ovs_net *ovs_net; > + struct net *net = sock_net(skb->sk); > int err, i; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > err = -EINVAL; > if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) > goto err; > @@ -1655,8 +1674,12 @@ static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) > { > struct sk_buff *reply; > struct datapath *dp; > + struct net *net = sock_net(skb->sk); > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > reply = ovs_dp_cmd_alloc_info(info); > if (!reply) > return -ENOMEM; > @@ -1688,8 +1711,12 @@ static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) > { > struct sk_buff *reply; > struct datapath *dp; > + struct net *net = sock_net(skb->sk); > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > reply = ovs_dp_cmd_alloc_info(info); > if (!reply) > return -ENOMEM; > @@ -1721,8 +1748,12 @@ static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) > { > struct sk_buff *reply; > struct datapath *dp; > + struct net *net = sock_net(skb->sk); > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > reply = ovs_dp_cmd_alloc_info(info); > if (!reply) > return -ENOMEM; > @@ -1777,12 +1808,12 @@ static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { > > static const struct genl_ops dp_datapath_genl_ops[] = { > { .cmd = OVS_DP_CMD_NEW, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = datapath_policy, > .doit = ovs_dp_cmd_new > }, > { .cmd = OVS_DP_CMD_DEL, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = datapath_policy, > .doit = ovs_dp_cmd_del > }, > @@ -1793,7 +1824,7 @@ static const struct genl_ops dp_datapath_genl_ops[] = { > .dumpit = ovs_dp_cmd_dump > }, > { .cmd = OVS_DP_CMD_SET, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = datapath_policy, > .doit = ovs_dp_cmd_set, > }, > @@ -1920,9 +1951,13 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) > struct sk_buff *reply; > struct vport *vport; > struct datapath *dp; > + struct net *net = sock_net(skb->sk); > u32 port_no; > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || > !a[OVS_VPORT_ATTR_UPCALL_PID]) > return -EINVAL; > @@ -1994,8 +2029,12 @@ static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) > struct nlattr **a = info->attrs; > struct sk_buff *reply; > struct vport *vport; > + struct net *net = sock_net(skb->sk); > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > reply = ovs_vport_cmd_alloc_info(); > if (!reply) > return -ENOMEM; > @@ -2046,8 +2085,12 @@ static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) > struct nlattr **a = info->attrs; > struct sk_buff *reply; > struct vport *vport; > + struct net *net = sock_net(skb->sk); > int err; > > + if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) > + return -EPERM; > + > reply = ovs_vport_cmd_alloc_info(); > if (!reply) > return -ENOMEM; > @@ -2158,12 +2201,12 @@ static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { > > static const struct genl_ops dp_vport_genl_ops[] = { > { .cmd = OVS_VPORT_CMD_NEW, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = vport_policy, > .doit = ovs_vport_cmd_new > }, > { .cmd = OVS_VPORT_CMD_DEL, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = vport_policy, > .doit = ovs_vport_cmd_del > }, > @@ -2174,7 +2217,7 @@ static const struct genl_ops dp_vport_genl_ops[] = { > .dumpit = ovs_vport_cmd_dump > }, > { .cmd = OVS_VPORT_CMD_SET, > - .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ > + .flags = 0, > .policy = vport_policy, > .doit = ovs_vport_cmd_set, > },