From patchwork Tue Aug 2 23:19:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 655182 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3s3scG0QmCz9t0M for ; Wed, 3 Aug 2016 09:19:58 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=pkEMrdal; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755832AbcHBXTe (ORCPT ); Tue, 2 Aug 2016 19:19:34 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:35401 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752316AbcHBXTa (ORCPT ); Tue, 2 Aug 2016 19:19:30 -0400 Received: by mail-pf0-f193.google.com with SMTP id h186so13326939pfg.2; Tue, 02 Aug 2016 16:19:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=+YLJGBQfPF21kbcMy/ZRjLt4cwrxuqjWoTSkfzK+sY0=; b=pkEMrdalalANdS8NZJkDkDqji6Ms0ClBIDNy75XNastzKy6gqqOs3GKtLa39cz3tCx BtOYvjs8n77w8EulR7l/W9j7zxFIHFu+AJWLO1ciM6XB8fxMEDOKukKrjfMc+2FtLTVd avcZNogyHz/dZ5U/BXEE08rO0I6v+qu7Gvjdv3kN+zFK5nrAkoGSrvTIjYwBWkb4IF6a Vum/nofVLSZ/iq7a9Kqu054Or4NG3vzzrCtPX3LGlK3gCOiTE6+HhEPQitL3BnCuOKQG 5BzrMZYLT+tAbyaYrh/0Bj/bJTCzlIedda1aQMqHWltqkip5po0zRO8Y0/anOjaYTgLB QJVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=+YLJGBQfPF21kbcMy/ZRjLt4cwrxuqjWoTSkfzK+sY0=; b=lt2L3gUMjulLStW61Ayaw8lZX6UFkJScLijhW+SLYnvhDFx75fhCLf0G6BOdFk+lJo 7fGtznh1NlDCkBqGHwghOHX7PVdIlDHZ4iwv4wQxQ78i7bSMN2BFGIKjGJ/88w4h1+fe K8bSQzdzo89wBORgJ+yQ6+iPIGbkd8g0PBsUOXprq/qQg3KGHtQTg3yR1sIXTfwVqc3i Nbv3s2b6PF0rYsGVxyBo/SnzXy/Rco44IQvWjfHl9sALaODMC7EWNO5PVZdKHI1vvjVd c6cCaf5+07oQ25qfwwfC+RxfVg8rf9dgfIrhzXbS9+kSNGQyJxiHOBA35VCM2olLsoNp x0ww== X-Gm-Message-State: AEkoous7o3ZaX0y3MTqXiPVii6FUNorwEi2JIHLgjAsJ+vNsY3hXZy0MrcxLbFB1LzRYQw== X-Received: by 10.98.204.216 with SMTP id j85mr110737702pfk.147.1470179969700; Tue, 02 Aug 2016 16:19:29 -0700 (PDT) Received: from dtor-ws ([2620:0:1000:1311:8cf8:bfac:67b8:4dfe]) by smtp.gmail.com with ESMTPSA id s89sm7257252pfi.83.2016.08.02.16.19.28 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 02 Aug 2016 16:19:28 -0700 (PDT) Date: Tue, 2 Aug 2016 16:19:26 -0700 From: Dmitry Torokhov To: "David S. Miller" Cc: Al Viro , "Eric W. Biederman" , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH] net: make net namespace sysctls belong to container's owner Message-ID: <20160802231926.GA8108@dtor-ws> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org If net namespace is attached to a user namespace let's make container's root owner of sysctls affecting said network namespace instead of global root. This also allows us to clean up net_ctl_permissions() because we do not need to fudge permissions anymore for the container's owner since it now owns the objects in question. Signed-off-by: Dmitry Torokhov Acked-by: "Eric W. Biederman" --- This helps when running Android CTS in a container, but I think it makes sense regardless. fs/proc/proc_sysctl.c | 5 +++++ include/linux/sysctl.h | 4 ++++ net/sysctl_net.c | 27 ++++++++++++++++++--------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 5e57c3e..28f9085 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -430,6 +430,7 @@ static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, i static struct inode *proc_sys_make_inode(struct super_block *sb, struct ctl_table_header *head, struct ctl_table *table) { + struct ctl_table_root *root = head->root; struct inode *inode; struct proc_inode *ei; @@ -457,6 +458,10 @@ static struct inode *proc_sys_make_inode(struct super_block *sb, if (is_empty_dir(head)) make_empty_dir_inode(inode); } + + if (root->set_ownership) + root->set_ownership(head, table, &inode->i_uid, &inode->i_gid); + out: return inode; } diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index fa7bc29..55bec2f 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -25,6 +25,7 @@ #include #include #include +#include #include /* For the /proc/sys support */ @@ -156,6 +157,9 @@ struct ctl_table_root { struct ctl_table_set default_set; struct ctl_table_set *(*lookup)(struct ctl_table_root *root, struct nsproxy *namespaces); + void (*set_ownership)(struct ctl_table_header *head, + struct ctl_table *table, + kuid_t *uid, kgid_t *gid); int (*permissions)(struct ctl_table_header *head, struct ctl_table *table); }; diff --git a/net/sysctl_net.c b/net/sysctl_net.c index ed98c1f..ff68326 100644 --- a/net/sysctl_net.c +++ b/net/sysctl_net.c @@ -42,26 +42,35 @@ static int net_ctl_permissions(struct ctl_table_header *head, struct ctl_table *table) { struct net *net = container_of(head->set, struct net, sysctls); - kuid_t root_uid = make_kuid(net->user_ns, 0); - kgid_t root_gid = make_kgid(net->user_ns, 0); /* Allow network administrator to have same access as root. */ - if (ns_capable(net->user_ns, CAP_NET_ADMIN) || - uid_eq(root_uid, current_euid())) { + if (ns_capable(net->user_ns, CAP_NET_ADMIN)) { int mode = (table->mode >> 6) & 7; return (mode << 6) | (mode << 3) | mode; } - /* Allow netns root group to have the same access as the root group */ - if (in_egroup_p(root_gid)) { - int mode = (table->mode >> 3) & 7; - return (mode << 3) | mode; - } + return table->mode; } +static void net_ctl_set_ownership(struct ctl_table_header *head, + struct ctl_table *table, + kuid_t *uid, kgid_t *gid) +{ + struct net *net = container_of(head->set, struct net, sysctls); + + *uid = make_kuid(net->user_ns, 0); + if (!uid_valid(*uid)) + *uid = GLOBAL_ROOT_UID; + + *gid = make_kgid(net->user_ns, 0); + if (!gid_valid(*gid)) + *gid = GLOBAL_ROOT_GID; +} + static struct ctl_table_root net_sysctl_root = { .lookup = net_ctl_header_lookup, .permissions = net_ctl_permissions, + .set_ownership = net_ctl_set_ownership, }; static int __net_init sysctl_net_init(struct net *net)