]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
net: create reusable function for getting ownership info of sysfs inodes
authorTyler Hicks <tyhicks@canonical.com>
Wed, 31 Oct 2018 00:55:23 +0000 (00:55 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@canonical.com>
Fri, 9 Nov 2018 18:59:53 +0000 (16:59 -0200)
BugLink: https://launchpad.net/bugs/1784501
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>
Signed-off-by: David S. Miller <davem@davemloft.net>
(backported from commit fbdeaed408cf2728c62640c10848ddb1b67e63d3)
[tyhicks: Minor context changes]
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Kleber Souza <kleber.souza@canonical.com>
Acked-by: Stefan Bader <stefan.bader@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
include/net/net_namespace.h
net/core/net-sysfs.c
net/core/net_namespace.c

index 049008493faf67902365570a16f26233a1d9a6ca..a803a854a774a5f8d2a8ab461665f8688d284932 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/list.h>
 #include <linux/sysctl.h>
+#include <linux/uidgid.h>
 
 #include <net/flow.h>
 #include <net/netns/core.h>
@@ -161,6 +162,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>
@@ -173,6 +176,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 */
 
index e753f88dbec5afb262fe8f87eca13cd3aabb8b25..b965dc7959109e4eab660b94b287c84192d839b1 100644 (file)
@@ -679,24 +679,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)
index 60a71be75aea063b418a48ade2a1e1c7804ab35c..9b826d3b20e3c0f3d0fbe7a80eb298e300b2f551 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/user_namespace.h>
 #include <linux/net_namespace.h>
 #include <linux/sched/task.h>
+#include <linux/uidgid.h>
 
 #include <net/sock.h>
 #include <net/netlink.h>
@@ -432,6 +433,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 DEFINE_SPINLOCK(cleanup_list_lock);
 static LIST_HEAD(cleanup_list);  /* Must hold cleanup_list_lock to touch */