Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp963504imm; Fri, 13 Jul 2018 09:09:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeMTYmDnFDXUTwDv9ri9wb4CmXWCmnW+BBUns21dEqbb9BC4zjQtwoKXrDDMi7l0xMaRLH8 X-Received: by 2002:a17:902:3e3:: with SMTP id d90-v6mr7029612pld.12.1531498180739; Fri, 13 Jul 2018 09:09:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531498180; cv=none; d=google.com; s=arc-20160816; b=unNj3X0n3y9zHBr05YXf6bKhw3jBrQPZN+KpF+Fqkuw17Wb0vyFNWaqTXMYNQGED3Y 7WBUnR7EsP8VcowTfyXNwyX2ZHkRu2m9qIKHV0dlXyx9ad/ArF81IzIbAD9XAM0TKmLi Rnf/bepsAr4x0+BDncH6fv0p78FIxgZBdOXwJMtlh0UAkEc6xyLnJnM+ZPuQR8fyB/Im jU1YGf6CGNsKeTgAar6p/PkJ0Y4bGAXniyRVh4VVawvWXPRCIh1xASJjHFJYwIZ07c3r 7ev8Afhf6SpZhFPmzZxMTKuLd/hfRGOoMaK8nxm/rkjwhYmA4A/DLOQBsh2Mvtc9RtOt 1Y/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=MuLf4mHl8Ou4ciIWDRV52GZTUc58sx883vJrHjej/WM=; b=lFUMhMspEmELbrqHNRa85QwECwVQouJjV/Tiw5dm36liQ5Aayyirj/keJ+29KPFG+Y ogW7BooAeNE7KMQFrerQmhhjMjbv4nVAPPOQiO64YN+T8QQf4Ta8Xn73K99uPutiXtrf E3hrSYXk94pq1R3Dwh9JW296TqRkmAIKbjaSnIHG+NsHmGeNzHqLoBNVOLhOFww0K6It qYW8cNzQUKoowgxluxEKpDLKyH0CAipMSYevgjK2qDeTS3d7LqeZmS/K+mBmgscgbgw7 omjxAyzQSJMeOXNOFp2GJ+4qDeKDIUB41RJNqbNiHV1BQXqnarc8Jff2yrf5oUnA/Wd7 3LZg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g85-v6si28049301pfa.271.2018.07.13.09.09.19; Fri, 13 Jul 2018 09:09:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387783AbeGMQXh (ORCPT + 99 others); Fri, 13 Jul 2018 12:23:37 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:38416 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729784AbeGMQXh (ORCPT ); Fri, 13 Jul 2018 12:23:37 -0400 Received: from 2.general.tyhicks.us.vpn ([10.172.64.53] helo=sec.ubuntu-ci) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1fe0cD-0006pX-Rv; Fri, 13 Jul 2018 16:08:18 +0000 From: Tyler Hicks To: Greg Kroah-Hartman , Tejun Heo , "David S. Miller" , Stephen Hemminger Cc: Dmitry Torokhov , "Eric W. Biederman" , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bridge@lists.linux-foundation.org, Linux Containers Subject: [PATCH net-next v2 6/7] net: Create reusable function for getting ownership info of sysfs inodes Date: Fri, 13 Jul 2018 16:05:48 +0000 Message-Id: <1531497949-1766-7-git-send-email-tyhicks@canonical.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531497949-1766-1-git-send-email-tyhicks@canonical.com> References: <1531497949-1766-1-git-send-email-tyhicks@canonical.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make net_ns_get_ownership() reusable by networking code outside of core. This is useful, for example, to allow bridge related sysfs files to be owned by container root. Add a function comment since this is a potentially dangerous function to use given the way that kobject_get_ownership() works by initializing uid and gid before calling .get_ownership(). Signed-off-by: Tyler Hicks --- include/net/net_namespace.h | 7 +++++++ net/core/net-sysfs.c | 15 --------------- net/core/net_namespace.c | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index a71264d75d7f..a257710527ce 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -170,6 +170,8 @@ extern struct net init_net; struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, struct net *old_net); +void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid); + void net_ns_barrier(void); #else /* CONFIG_NET_NS */ #include @@ -182,6 +184,11 @@ static inline struct net *copy_net_ns(unsigned long flags, return old_net; } +static inline void net_ns_get_ownership(const struct net *net, + kuid_t *uid, kgid_t *gid) +{ +} + static inline void net_ns_barrier(void) {} #endif /* CONFIG_NET_NS */ diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index 41d84c40fe51..a3ad8108d296 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -656,21 +656,6 @@ static const struct attribute_group wireless_group = { #define net_class_groups NULL #endif /* CONFIG_SYSFS */ -static void net_ns_get_ownership(const struct net *net, - kuid_t *uid, kgid_t *gid) -{ - if (net) { - kuid_t ns_root_uid = make_kuid(net->user_ns, 0); - kgid_t ns_root_gid = make_kgid(net->user_ns, 0); - - if (uid_valid(ns_root_uid)) - *uid = ns_root_uid; - - if (gid_valid(ns_root_gid)) - *gid = ns_root_gid; - } -} - #ifdef CONFIG_SYSFS #define to_rx_queue_attr(_attr) \ container_of(_attr, struct rx_queue_attribute, attr) diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index a11e03f920d3..5257875fa84d 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -448,6 +448,31 @@ struct net *copy_net_ns(unsigned long flags, return net; } +/** + * net_ns_get_ownership - get sysfs ownership data for @net + * @net: network namespace in question (can be NULL) + * @uid: kernel user ID for sysfs objects (must be GLOBAL_ROOT_UID) + * @gid: kernel group ID for sysfs objects (must be GLOBAL_ROOT_GID) + * + * Returns the uid/gid pair of root in the user namespace associated with the + * given network namespace. The caller must initialize @uid and @gid to + * GLOBAL_ROOT_UID/GLOBAL_ROOT_GID before calling this function. + */ +void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid) +{ + if (net) { + kuid_t ns_root_uid = make_kuid(net->user_ns, 0); + kgid_t ns_root_gid = make_kgid(net->user_ns, 0); + + if (uid_valid(ns_root_uid)) + *uid = ns_root_uid; + + if (gid_valid(ns_root_gid)) + *gid = ns_root_gid; + } +} +EXPORT_SYMBOL_GPL(net_ns_get_ownership); + static void unhash_nsid(struct net *net, struct net *last) { struct net *tmp; -- 2.7.4