Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3343338imm; Fri, 20 Jul 2018 14:59:41 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd+Z1wCSQst2UqQ8RhkQkrvTt2awiIQpyGxxG3JhNjjeID4UtEKOf6l4S1YZECSiqgQBjLg X-Received: by 2002:a63:ad07:: with SMTP id g7-v6mr3530472pgf.19.1532123981299; Fri, 20 Jul 2018 14:59:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532123981; cv=none; d=google.com; s=arc-20160816; b=ROStxkfV/DYODHT4cst/FsjWYBHNrXyIyF3PGohOnDAiUKb5J9+V/fhGphCjF8nkpJ pu+uvfYvp5WJ3bAazx09hk3FA0JPQMailHh9vpg+tqVlg+6GKl/x/Sv/Mi5oq8Qz6YHm F0WNFmjVvsH2N8vHj6IhWaxg0WiiF3aHMsEJVK6S7gJhPBmDF1MgdARe9XmuCDsUOKlg j7Lg4i1ZkmYvWfkJGaA7rwwtz9TPmwPphayI4VV9mY+GwIP8VtXr9rh/8ZNRJ0BKkeSQ 57agJBvV8EcEAmLlG0Wl3QMbMp3xKQbFqiqPm7YPTBVyf58/RYOwG0Vt4LVoMAA1lJeO m0Fw== 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=5o0LRwtdINTaZP7V+Nj87UhvPChsoFrezHmThVcNLLM=; b=Tt6mKIu8ZBbmnMSnra4DUi1XIfbCVZo1cuF2d9reXkG2C5WTUJzwYe3R7V5eMrbHCF chJb2zp5OSnjYBFUvmSbMOsUKfTC/2twT+OjOUeLaSeBIU9g2TnsWghiRKXqfH44iEyL Kw0zlGUrPh9tP6cwBiRYQqUgUmJiNpSBhkQvCq++A2Y7fgX+vYPzFZnhWWTjT+btG5is 3SsJp6TL+F2FXu4TTEZPiQo7soMBtjt05YRofyahyUE2Twsj/hAGRzN4yzgDagkLOwsP qjpmnEx6T1kjSX82PoDcDsX9VkY2ywAJeSrwdKliD5qsVHnOIsp8q6t8CCWNIboR8XmT OwDg== 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 e2-v6si2729155pgl.4.2018.07.20.14.59.27; Fri, 20 Jul 2018 14:59:41 -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 S1731916AbeGTWr6 (ORCPT + 99 others); Fri, 20 Jul 2018 18:47:58 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:37664 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728214AbeGTWr6 (ORCPT ); Fri, 20 Jul 2018 18:47:58 -0400 Received: from 2.general.tyhicks.us.vpn ([10.172.64.53] helo=sec.l.tihix.com) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1fgdPF-0004ZT-2D; Fri, 20 Jul 2018 21:57:45 +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 v3 7/8] net: create reusable function for getting ownership info of sysfs inodes Date: Fri, 20 Jul 2018 21:56:53 +0000 Message-Id: <1532123814-1109-8-git-send-email-tyhicks@canonical.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1532123814-1109-1-git-send-email-tyhicks@canonical.com> References: <1532123814-1109-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 | 10 ++++++++++ net/core/net-sysfs.c | 18 ------------------ net/core/net_namespace.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index a71264d75d7f..9b5fdc50519a 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -170,6 +171,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 +185,13 @@ 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) +{ + *uid = GLOBAL_ROOT_UID; + *gid = GLOBAL_ROOT_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 ada065fc685e..0a95bcf64cdc 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -656,24 +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; - } else { - *uid = GLOBAL_ROOT_UID; - *gid = GLOBAL_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..738871af5efa 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -448,6 +449,33 @@ 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 + * @gid: kernel group ID for sysfs objects + * + * Returns the uid/gid pair of root in the user namespace associated with the + * given network namespace. + */ +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; + } else { + *uid = GLOBAL_ROOT_UID; + *gid = GLOBAL_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