[RH7,3/3] nsfs: add ioctl to get a parent namespace

Submitted by Pavel Tikhomirov on Nov. 29, 2017, 12:47 p.m.

Details

Message ID 20171129124707.25875-4-ptikhomirov@virtuozzo.com
State New
Series "ioctl: port NS_GET_USERNS and NS_GET_PARENT"
Headers show

Commit Message

Pavel Tikhomirov Nov. 29, 2017, 12:47 p.m.
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@hallyn.com>
Signed-off-by: Andrei Vagin <avagin@openvz.org>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Pavel Tikhomirov <ptikhomirov@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(+)

Patch hide | download patch | download mbox

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)