[Devel] [PATCH RHEL7 COMMIT] nsfs: add ioctl to get a parent namespace

Konstantin Khorenko khorenko at virtuozzo.com
Thu Dec 7 13:40:14 MSK 2017


The commit is pushed to "branch-rh7-3.10.0-693.11.1.vz7.39.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-693.11.1.vz7.39.1
------>
commit 78a9e66ec6f01307da155953cef3b9fd8024099a
Author: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
Date:   Thu Dec 7 13:40:14 2017 +0300

    nsfs: add ioctl to get a parent namespace
    
    Patchset description:
    ioctl: port NS_GET_USERNS and NS_GET_PARENT
    
    Need these to be able to enter owner's userns of mountnamespace to have
    privileges to unmount everything, to uncover dumpable overmounted
    mounts. NS_GET_PARENT is not a must here but it is nice to have it too
    for criu.
    
    https://jira.sw.ru/browse/PSBM-57362
    
    Rework mainstream patches as we don't have nsfs and ns_common.
    
    Other option can be porting 10 more patches:
    
    5d826c847b34 ("new helper: readlink_copy()")
    435d5f4bb2cc ("common object embedded into various struct ....ns")
    58be28256d98 ("make mntns ->get()/->put()/->install()/->inum() work with
    &mnt_ns->ns")
    ff24870f46d5 ("netns: switch ->get()/->put()/->install()/->inum() to
    working with &net->ns")
    3c0411846118 ("switch the rest of proc_ns_operations to working with
    &...->ns")
    64964528b24e ("make proc_ns_operations work with struct ns_common *
    instead of void *")
    6344c433a452 ("new helpers: ns_alloc_inum/ns_free_inum")
    33c429405a2c ("copy address of proc_ns_ops into ns_common")
    f77c80142e1a ("bury struct proc_ns in fs/proc")
    e149ed2b805f ("take the targets of /proc/*/ns/* symlinks to separate fs")
    
    And re-applying on top of them:
    25b14e92af1a ("ns: allow ns_entries to have custom symlink content")
    
    And porting fix as new version uses rcu:
    073c516ff735 ("nsfs: mark dentry with DCACHE_RCUACCESS")
    
    But still ioctls won't apply to clean after it, so I think complete
    rework here is a better option.
    
    Pavel Tikhomirov (3):
      kernel: add a helper to get an owning user namespace for a namespace
      nsfs: add ioctl to get an owning user namespace for ns file descriptor
      nsfs: add ioctl to get a parent namespace
    
    ============================
    This patch description:
    
    Changes:
    Replace ns_common in args to ns pointer and ns_ops, as we don't
    have the former.
    
    https://jira.sw.ru/browse/PSBM-57362
    
    ms commit a7306ed8d94a ("nsfs: add ioctl to get a parent namespace")
    
    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 at hallyn.com>
    Signed-off-by: Andrei Vagin <avagin at openvz.org>
    
    Signed-off-by: Eric W. Biederman <ebiederm at xmission.com>
    Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 fs/proc/namespaces.c    |  6 ++++++
 include/linux/proc_ns.h |  1 +
 kernel/pid_namespace.c  | 20 ++++++++++++++++++++
 kernel/user_namespace.c |  1 +
 4 files changed, 28 insertions(+)

diff --git a/fs/proc/namespaces.c b/fs/proc/namespaces.c
index 121b76d3fcd0..626d943b23f8 100644
--- a/fs/proc/namespaces.c
+++ b/fs/proc/namespaces.c
@@ -19,6 +19,8 @@
 
 /* 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)
 
 static const struct proc_ns_operations *ns_entries[] = {
 #ifdef CONFIG_NET_NS
@@ -175,6 +177,10 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 	switch (ioctl) {
 	case NS_GET_USERNS:
 		return open_related_ns(mnt, ns, &userns_operations, ns_get_owner);
+	case NS_GET_PARENT:
+		if (!ns->ns_ops->get_parent)
+			return -EINVAL;
+		return open_related_ns(mnt, ns, ns->ns_ops, ns->ns_ops->get_parent);
 	default:
 		return -ENOTTY;
 	}
diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h
index 3f71f8e1aa18..af898ed6fbad 100644
--- a/include/linux/proc_ns.h
+++ b/include/linux/proc_ns.h
@@ -17,6 +17,7 @@ struct proc_ns_operations {
 	int (*install)(struct nsproxy *nsproxy, void *ns);
 	unsigned int (*inum)(void *ns);
 	struct user_namespace *(*owner)(void *ns);
+	void *(*get_parent)(void *ns, const struct proc_ns_operations *ns_ops);
 };
 
 struct proc_ns {
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index 1bab842a8e1e..38e2c2da1417 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -426,6 +426,24 @@ static struct user_namespace *pidns_owner(void *ns)
 	return pid_ns->user_ns;
 }
 
+static void *pidns_get_parent(void *ns, const struct proc_ns_operations *ns_ops)
+{
+	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 = ((struct pid_namespace *)ns)->parent;
+	for (;;) {
+		if (!p)
+			return ERR_PTR(-EPERM);
+		if (p == active)
+			break;
+		p = p->parent;
+	}
+
+	return get_pid_ns(pid_ns);
+}
+
 const struct proc_ns_operations pidns_operations = {
 	.name		= "pid",
 	.type		= CLONE_NEWPID,
@@ -434,6 +452,7 @@ const struct proc_ns_operations pidns_operations = {
 	.install	= pidns_install,
 	.inum		= pidns_inum,
 	.owner		= pidns_owner,
+	.get_parent     = pidns_get_parent,
 };
 
 const struct proc_ns_operations pidns_for_children_operations = {
@@ -445,6 +464,7 @@ const struct proc_ns_operations pidns_for_children_operations = {
 	.install	= pidns_install,
 	.inum		= pidns_inum,
 	.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 9b145831e07c..87719ed7dc19 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -1096,6 +1096,7 @@ const struct proc_ns_operations userns_operations = {
 	.install	= userns_install,
 	.inum		= userns_inum,
 	.owner		= userns_owner,
+	.get_parent	= ns_get_owner,
 };
 
 static __init int user_namespaces_init(void)


More information about the Devel mailing list