From: Andrey Vagin <[email protected]>

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 <[email protected]>
Signed-off-by: Andrei Vagin <[email protected]>
Signed-off-by: Eric W. Biederman <[email protected]>

(cherry picked from VZ8 commit a7306ed8d94af729ecef8b6e37506a1c6fc14788)

https://jira.sw.ru/browse/PSBM-102357

Signed-off-by: Pavel Tikhomirov <[email protected]>
---
 fs/nsfs.c                 |  4 ++++
 include/linux/proc_ns.h   |  1 +
 include/uapi/linux/nsfs.h |  2 ++
 kernel/pid_namespace.c    | 19 +++++++++++++++++++
 kernel/user_namespace.c   |  1 +
 5 files changed, 27 insertions(+)

diff --git a/fs/nsfs.c b/fs/nsfs.c
index c5ddecea66bc..20ae4f7d92e6 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -176,6 +176,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;
        }
diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h
index c860e681d595..a8645c5eb679 100644
--- a/include/linux/proc_ns.h
+++ b/include/linux/proc_ns.h
@@ -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;
diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h
index 5cacd5c1b5d7..3af617230d1b 100644
--- a/include/uapi/linux/nsfs.h
+++ b/include/uapi/linux/nsfs.h
@@ -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 */
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index 302b234f5b06..c5541caf7a09 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -397,6 +397,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;
@@ -409,6 +427,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)
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index fc388fbfad3f..1c83e5eddbad 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -1109,6 +1109,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)
-- 
2.24.1

_______________________________________________
Devel mailing list
[email protected]
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to