Merge branch 'ida-4.19' of git://git.infradead.org/users/willy/linux-dax
[sfrench/cifs-2.6.git] / net / core / net_namespace.c
index f447cebdcea37c6b2e284b210d77dd6a3fed2060..670c84b1bfc23bbb9dde13b4df21a503350d390e 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>
@@ -448,6 +449,33 @@ dec_ucounts:
        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;