diff options
author | Andrey Vagin <avagin@openvz.org> | 2016-09-06 03:47:15 -0400 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2016-09-22 20:59:41 -0400 |
commit | a7306ed8d94af729ecef8b6e37506a1c6fc14788 (patch) | |
tree | 0d45320911d2a339bc0abd6bacd5703c7308c08c | |
parent | 6786741dbf99e44fb0c0ed85a37582b8a26f1c3b (diff) |
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>
-rw-r--r-- | fs/nsfs.c | 4 | ||||
-rw-r--r-- | include/linux/proc_ns.h | 1 | ||||
-rw-r--r-- | include/uapi/linux/nsfs.h | 2 | ||||
-rw-r--r-- | kernel/pid_namespace.c | 19 | ||||
-rw-r--r-- | kernel/user_namespace.c | 1 |
5 files changed, 27 insertions, 0 deletions
@@ -171,6 +171,10 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, | |||
171 | switch (ioctl) { | 171 | switch (ioctl) { |
172 | case NS_GET_USERNS: | 172 | case NS_GET_USERNS: |
173 | return open_related_ns(ns, ns_get_owner); | 173 | return open_related_ns(ns, ns_get_owner); |
174 | case NS_GET_PARENT: | ||
175 | if (!ns->ops->get_parent) | ||
176 | return -EINVAL; | ||
177 | return open_related_ns(ns, ns->ops->get_parent); | ||
174 | default: | 178 | default: |
175 | return -ENOTTY; | 179 | return -ENOTTY; |
176 | } | 180 | } |
diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h index ca85a4348ffc..12cb8bd81d2d 100644 --- a/include/linux/proc_ns.h +++ b/include/linux/proc_ns.h | |||
@@ -19,6 +19,7 @@ struct proc_ns_operations { | |||
19 | void (*put)(struct ns_common *ns); | 19 | void (*put)(struct ns_common *ns); |
20 | int (*install)(struct nsproxy *nsproxy, struct ns_common *ns); | 20 | int (*install)(struct nsproxy *nsproxy, struct ns_common *ns); |
21 | struct user_namespace *(*owner)(struct ns_common *ns); | 21 | struct user_namespace *(*owner)(struct ns_common *ns); |
22 | struct ns_common *(*get_parent)(struct ns_common *ns); | ||
22 | }; | 23 | }; |
23 | 24 | ||
24 | extern const struct proc_ns_operations netns_operations; | 25 | 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 @@ | |||
7 | 7 | ||
8 | /* Returns a file descriptor that refers to an owning user namespace */ | 8 | /* Returns a file descriptor that refers to an owning user namespace */ |
9 | #define NS_GET_USERNS _IO(NSIO, 0x1) | 9 | #define NS_GET_USERNS _IO(NSIO, 0x1) |
10 | /* Returns a file descriptor that refers to a parent namespace */ | ||
11 | #define NS_GET_PARENT _IO(NSIO, 0x2) | ||
10 | 12 | ||
11 | #endif /* __LINUX_NSFS_H */ | 13 | #endif /* __LINUX_NSFS_H */ |
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index c02d744225e1..4fa2d56a936c 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c | |||
@@ -388,6 +388,24 @@ static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns) | |||
388 | return 0; | 388 | return 0; |
389 | } | 389 | } |
390 | 390 | ||
391 | static struct ns_common *pidns_get_parent(struct ns_common *ns) | ||
392 | { | ||
393 | struct pid_namespace *active = task_active_pid_ns(current); | ||
394 | struct pid_namespace *pid_ns, *p; | ||
395 | |||
396 | /* See if the parent is in the current namespace */ | ||
397 | pid_ns = p = to_pid_ns(ns)->parent; | ||
398 | for (;;) { | ||
399 | if (!p) | ||
400 | return ERR_PTR(-EPERM); | ||
401 | if (p == active) | ||
402 | break; | ||
403 | p = p->parent; | ||
404 | } | ||
405 | |||
406 | return &get_pid_ns(pid_ns)->ns; | ||
407 | } | ||
408 | |||
391 | static struct user_namespace *pidns_owner(struct ns_common *ns) | 409 | static struct user_namespace *pidns_owner(struct ns_common *ns) |
392 | { | 410 | { |
393 | return to_pid_ns(ns)->user_ns; | 411 | return to_pid_ns(ns)->user_ns; |
@@ -400,6 +418,7 @@ const struct proc_ns_operations pidns_operations = { | |||
400 | .put = pidns_put, | 418 | .put = pidns_put, |
401 | .install = pidns_install, | 419 | .install = pidns_install, |
402 | .owner = pidns_owner, | 420 | .owner = pidns_owner, |
421 | .get_parent = pidns_get_parent, | ||
403 | }; | 422 | }; |
404 | 423 | ||
405 | static __init int pid_namespaces_init(void) | 424 | static __init int pid_namespaces_init(void) |
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 0ef683a03c20..a58a219b99c6 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c | |||
@@ -1034,6 +1034,7 @@ const struct proc_ns_operations userns_operations = { | |||
1034 | .put = userns_put, | 1034 | .put = userns_put, |
1035 | .install = userns_install, | 1035 | .install = userns_install, |
1036 | .owner = userns_owner, | 1036 | .owner = userns_owner, |
1037 | .get_parent = ns_get_owner, | ||
1037 | }; | 1038 | }; |
1038 | 1039 | ||
1039 | static __init int user_namespaces_init(void) | 1040 | static __init int user_namespaces_init(void) |