From: Tyler Hicks <tyhicks@canonical.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Stephen Hemminger <stephen@networkplumber.org>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
"Eric W. Biederman" <ebiederm@xmission.com>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
bridge@lists.linux-foundation.org,
Linux Containers <containers@lists.linux-foundation.org>
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 [thread overview]
Message-ID: <1531497949-1766-7-git-send-email-tyhicks@canonical.com> (raw)
In-Reply-To: <1531497949-1766-1-git-send-email-tyhicks@canonical.com>
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 <tyhicks@canonical.com>
---
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 <linux/sched.h>
@@ -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
next prev parent reply other threads:[~2018-07-13 16:23 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-13 16:05 [PATCH v2 net-next 0/7] Make /sys/class/net per net namespace objects belong to container Tyler Hicks
2018-07-13 16:05 ` [PATCH net-next v2 1/7] kernfs: allow creating kernfs objects with arbitrary uid/gid Tyler Hicks
2018-07-17 1:04 ` kbuild test robot
2018-07-13 16:05 ` [PATCH net-next v2 2/7] sysfs, kobject: allow creating kobject belonging to arbitrary users Tyler Hicks
2018-07-13 16:05 ` [PATCH net-next v2 3/7] kobject: kset_create_and_add() - fetch ownership info from parent Tyler Hicks
2018-07-13 16:05 ` [PATCH net-next v2 4/7] driver core: set up ownership of class devices in sysfs Tyler Hicks
2018-07-13 16:05 ` [PATCH net-next v2 5/7] net-sysfs: make sure objects belong to contrainer's owner Tyler Hicks
2018-07-13 16:05 ` Tyler Hicks [this message]
2018-07-19 14:36 ` [PATCH net-next v2 6/7] net: Create reusable function for getting ownership info of sysfs inodes Christian Brauner
2018-07-20 21:58 ` Tyler Hicks
2018-07-13 16:05 ` [PATCH net-next v2 7/7] bridge: make sure objects belong to container's owner Tyler Hicks
2018-07-16 20:58 ` [PATCH v2 net-next 0/7] Make /sys/class/net per net namespace objects belong to container David Miller
2018-07-18 4:17 ` David Miller
2018-07-18 4:41 ` David Miller
2018-07-19 1:07 ` Tyler Hicks
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=1531497949-1766-7-git-send-email-tyhicks@canonical.com \
--to=tyhicks@canonical.com \
--cc=bridge@lists.linux-foundation.org \
--cc=containers@lists.linux-foundation.org \
--cc=davem@davemloft.net \
--cc=dmitry.torokhov@gmail.com \
--cc=ebiederm@xmission.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
--cc=tj@kernel.org \
/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).