nsfs: add ioctl to get a parent namespace
authorAndrey Vagin <avagin@openvz.org>
Tue, 6 Sep 2016 07:47:15 +0000 (00:47 -0700)
committerEric W. Biederman <ebiederm@xmission.com>
Fri, 23 Sep 2016 00:59:41 +0000 (19:59 -0500)
Pid and user namepaces are hierarchical. There is no way to discover
parent-child relationships.

In a future we will use this interface to dump and restore nested
namespaces.

Acked-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Andrei Vagin <avagin@openvz.org>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
fs/nsfs.c
include/linux/proc_ns.h
include/uapi/linux/nsfs.h
kernel/pid_namespace.c
kernel/user_namespace.c

index 3887da470f7eb6af9112fc4897ad71d977c93026..fb7b397a1297557bbbc91fa954fe17b0ba61e9a9 100644 (file)
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -171,6 +171,10 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
        switch (ioctl) {
        case NS_GET_USERNS:
                return open_related_ns(ns, ns_get_owner);
+       case NS_GET_PARENT:
+               if (!ns->ops->get_parent)
+                       return -EINVAL;
+               return open_related_ns(ns, ns->ops->get_parent);
        default:
                return -ENOTTY;
        }
index ca85a4348ffcb690d94855fe01515ca6ca6f6f8a..12cb8bd81d2d12b734c83059fb0b0759ee0c92c3 100644 (file)
@@ -19,6 +19,7 @@ struct proc_ns_operations {
        void (*put)(struct ns_common *ns);
        int (*install)(struct nsproxy *nsproxy, struct ns_common *ns);
        struct user_namespace *(*owner)(struct ns_common *ns);
+       struct ns_common *(*get_parent)(struct ns_common *ns);
 };
 
 extern const struct proc_ns_operations netns_operations;
index 5cacd5c1b5d7820f8983d92cc06c9f5f8a4f119c..3af617230d1b760b00b6ac5cfd3f3ce027ec4db1 100644 (file)
@@ -7,5 +7,7 @@
 
 /* Returns a file descriptor that refers to an owning user namespace */
 #define NS_GET_USERNS  _IO(NSIO, 0x1)
+/* Returns a file descriptor that refers to a parent namespace */
+#define NS_GET_PARENT  _IO(NSIO, 0x2)
 
 #endif /* __LINUX_NSFS_H */
index c02d744225e1989d1bdbcac1b88e1537d203e34b..4fa2d56a936cfd76d9322e2c98dd253f28702839 100644 (file)
@@ -388,6 +388,24 @@ static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
        return 0;
 }
 
+static struct ns_common *pidns_get_parent(struct ns_common *ns)
+{
+       struct pid_namespace *active = task_active_pid_ns(current);
+       struct pid_namespace *pid_ns, *p;
+
+       /* See if the parent is in the current namespace */
+       pid_ns = p = to_pid_ns(ns)->parent;
+       for (;;) {
+               if (!p)
+                       return ERR_PTR(-EPERM);
+               if (p == active)
+                       break;
+               p = p->parent;
+       }
+
+       return &get_pid_ns(pid_ns)->ns;
+}
+
 static struct user_namespace *pidns_owner(struct ns_common *ns)
 {
        return to_pid_ns(ns)->user_ns;
@@ -400,6 +418,7 @@ const struct proc_ns_operations pidns_operations = {
        .put            = pidns_put,
        .install        = pidns_install,
        .owner          = pidns_owner,
+       .get_parent     = pidns_get_parent,
 };
 
 static __init int pid_namespaces_init(void)
index 0ef683a03c20840a558b19d3f4a3c2e2fc54997f..a58a219b99c68e250c929e3498631cbc5c9ead27 100644 (file)
@@ -1034,6 +1034,7 @@ const struct proc_ns_operations userns_operations = {
        .put            = userns_put,
        .install        = userns_install,
        .owner          = userns_owner,
+       .get_parent     = ns_get_owner,
 };
 
 static __init int user_namespaces_init(void)