diff options
| author | Eric W. Biederman <ebiederm@xmission.com> | 2011-06-15 13:21:48 -0400 |
|---|---|---|
| committer | Eric W. Biederman <ebiederm@xmission.com> | 2012-11-20 07:19:49 -0500 |
| commit | 98f842e675f96ffac96e6c50315790912b2812be (patch) | |
| tree | ed4dee9a6e54e3443e9f3f1614c8a2fcf9b31e0a /kernel | |
| parent | bf056bfa80596a5d14b26b17276a56a0dcb080e5 (diff) | |
proc: Usable inode numbers for the namespace file descriptors.
Assign a unique proc inode to each namespace, and use that
inode number to ensure we only allocate at most one proc
inode for every namespace in proc.
A single proc inode per namespace allows userspace to test
to see if two processes are in the same namespace.
This has been a long requested feature and only blocked because
a naive implementation would put the id in a global space and
would ultimately require having a namespace for the names of
namespaces, making migration and certain virtualization tricks
impossible.
We still don't have per superblock inode numbers for proc, which
appears necessary for application unaware checkpoint/restart and
migrations (if the application is using namespace file descriptors)
but that is now allowd by the design if it becomes important.
I have preallocated the ipc and uts initial proc inode numbers so
their structures can be statically initialized.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/pid.c | 1 | ||||
| -rw-r--r-- | kernel/pid_namespace.c | 12 | ||||
| -rw-r--r-- | kernel/user.c | 2 | ||||
| -rw-r--r-- | kernel/user_namespace.c | 15 | ||||
| -rw-r--r-- | kernel/utsname.c | 17 |
5 files changed, 46 insertions, 1 deletions
diff --git a/kernel/pid.c b/kernel/pid.c index 6e8da291de4..3026ddae0a3 100644 --- a/kernel/pid.c +++ b/kernel/pid.c | |||
| @@ -80,6 +80,7 @@ struct pid_namespace init_pid_ns = { | |||
| 80 | .level = 0, | 80 | .level = 0, |
| 81 | .child_reaper = &init_task, | 81 | .child_reaper = &init_task, |
| 82 | .user_ns = &init_user_ns, | 82 | .user_ns = &init_user_ns, |
| 83 | .proc_inum = PROC_PID_INIT_INO, | ||
| 83 | }; | 84 | }; |
| 84 | EXPORT_SYMBOL_GPL(init_pid_ns); | 85 | EXPORT_SYMBOL_GPL(init_pid_ns); |
| 85 | 86 | ||
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 68508d33063..560da0dab23 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c | |||
| @@ -107,6 +107,10 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns | |||
| 107 | if (ns->pid_cachep == NULL) | 107 | if (ns->pid_cachep == NULL) |
| 108 | goto out_free_map; | 108 | goto out_free_map; |
| 109 | 109 | ||
| 110 | err = proc_alloc_inum(&ns->proc_inum); | ||
| 111 | if (err) | ||
| 112 | goto out_free_map; | ||
| 113 | |||
| 110 | kref_init(&ns->kref); | 114 | kref_init(&ns->kref); |
| 111 | ns->level = level; | 115 | ns->level = level; |
| 112 | ns->parent = get_pid_ns(parent_pid_ns); | 116 | ns->parent = get_pid_ns(parent_pid_ns); |
| @@ -133,6 +137,7 @@ static void destroy_pid_namespace(struct pid_namespace *ns) | |||
| 133 | { | 137 | { |
| 134 | int i; | 138 | int i; |
| 135 | 139 | ||
| 140 | proc_free_inum(ns->proc_inum); | ||
| 136 | for (i = 0; i < PIDMAP_ENTRIES; i++) | 141 | for (i = 0; i < PIDMAP_ENTRIES; i++) |
| 137 | kfree(ns->pidmap[i].page); | 142 | kfree(ns->pidmap[i].page); |
| 138 | put_user_ns(ns->user_ns); | 143 | put_user_ns(ns->user_ns); |
| @@ -345,12 +350,19 @@ static int pidns_install(struct nsproxy *nsproxy, void *ns) | |||
| 345 | return 0; | 350 | return 0; |
| 346 | } | 351 | } |
| 347 | 352 | ||
| 353 | static unsigned int pidns_inum(void *ns) | ||
| 354 | { | ||
| 355 | struct pid_namespace *pid_ns = ns; | ||
| 356 | return pid_ns->proc_inum; | ||
| 357 | } | ||
| 358 | |||
| 348 | const struct proc_ns_operations pidns_operations = { | 359 | const struct proc_ns_operations pidns_operations = { |
| 349 | .name = "pid", | 360 | .name = "pid", |
| 350 | .type = CLONE_NEWPID, | 361 | .type = CLONE_NEWPID, |
| 351 | .get = pidns_get, | 362 | .get = pidns_get, |
| 352 | .put = pidns_put, | 363 | .put = pidns_put, |
| 353 | .install = pidns_install, | 364 | .install = pidns_install, |
| 365 | .inum = pidns_inum, | ||
| 354 | }; | 366 | }; |
| 355 | 367 | ||
| 356 | static __init int pid_namespaces_init(void) | 368 | static __init int pid_namespaces_init(void) |
diff --git a/kernel/user.c b/kernel/user.c index 750acffbe9e..33acb5e53a5 100644 --- a/kernel/user.c +++ b/kernel/user.c | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | #include <linux/interrupt.h> | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/export.h> | 17 | #include <linux/export.h> |
| 18 | #include <linux/user_namespace.h> | 18 | #include <linux/user_namespace.h> |
| 19 | #include <linux/proc_fs.h> | ||
| 19 | 20 | ||
| 20 | /* | 21 | /* |
| 21 | * userns count is 1 for root user, 1 for init_uts_ns, | 22 | * userns count is 1 for root user, 1 for init_uts_ns, |
| @@ -51,6 +52,7 @@ struct user_namespace init_user_ns = { | |||
| 51 | }, | 52 | }, |
| 52 | .owner = GLOBAL_ROOT_UID, | 53 | .owner = GLOBAL_ROOT_UID, |
| 53 | .group = GLOBAL_ROOT_GID, | 54 | .group = GLOBAL_ROOT_GID, |
| 55 | .proc_inum = PROC_USER_INIT_INO, | ||
| 54 | }; | 56 | }; |
| 55 | EXPORT_SYMBOL_GPL(init_user_ns); | 57 | EXPORT_SYMBOL_GPL(init_user_ns); |
| 56 | 58 | ||
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 89f6eaed067..f5975ccf934 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c | |||
| @@ -58,6 +58,7 @@ int create_user_ns(struct cred *new) | |||
| 58 | struct user_namespace *ns, *parent_ns = new->user_ns; | 58 | struct user_namespace *ns, *parent_ns = new->user_ns; |
| 59 | kuid_t owner = new->euid; | 59 | kuid_t owner = new->euid; |
| 60 | kgid_t group = new->egid; | 60 | kgid_t group = new->egid; |
| 61 | int ret; | ||
| 61 | 62 | ||
| 62 | /* The creator needs a mapping in the parent user namespace | 63 | /* The creator needs a mapping in the parent user namespace |
| 63 | * or else we won't be able to reasonably tell userspace who | 64 | * or else we won't be able to reasonably tell userspace who |
| @@ -71,6 +72,12 @@ int create_user_ns(struct cred *new) | |||
| 71 | if (!ns) | 72 | if (!ns) |
| 72 | return -ENOMEM; | 73 | return -ENOMEM; |
| 73 | 74 | ||
| 75 | ret = proc_alloc_inum(&ns->proc_inum); | ||
| 76 | if (ret) { | ||
| 77 | kmem_cache_free(user_ns_cachep, ns); | ||
| 78 | return ret; | ||
| 79 | } | ||
| 80 | |||
| 74 | kref_init(&ns->kref); | 81 | kref_init(&ns->kref); |
| 75 | /* Leave the new->user_ns reference with the new user namespace. */ | 82 | /* Leave the new->user_ns reference with the new user namespace. */ |
| 76 | ns->parent = parent_ns; | 83 | ns->parent = parent_ns; |
| @@ -103,6 +110,7 @@ void free_user_ns(struct kref *kref) | |||
| 103 | container_of(kref, struct user_namespace, kref); | 110 | container_of(kref, struct user_namespace, kref); |
| 104 | 111 | ||
| 105 | parent = ns->parent; | 112 | parent = ns->parent; |
| 113 | proc_free_inum(ns->proc_inum); | ||
| 106 | kmem_cache_free(user_ns_cachep, ns); | 114 | kmem_cache_free(user_ns_cachep, ns); |
| 107 | put_user_ns(parent); | 115 | put_user_ns(parent); |
| 108 | } | 116 | } |
| @@ -808,12 +816,19 @@ static int userns_install(struct nsproxy *nsproxy, void *ns) | |||
| 808 | return commit_creds(cred); | 816 | return commit_creds(cred); |
| 809 | } | 817 | } |
| 810 | 818 | ||
| 819 | static unsigned int userns_inum(void *ns) | ||
| 820 | { | ||
| 821 | struct user_namespace *user_ns = ns; | ||
| 822 | return user_ns->proc_inum; | ||
| 823 | } | ||
| 824 | |||
| 811 | const struct proc_ns_operations userns_operations = { | 825 | const struct proc_ns_operations userns_operations = { |
| 812 | .name = "user", | 826 | .name = "user", |
| 813 | .type = CLONE_NEWUSER, | 827 | .type = CLONE_NEWUSER, |
| 814 | .get = userns_get, | 828 | .get = userns_get, |
| 815 | .put = userns_put, | 829 | .put = userns_put, |
| 816 | .install = userns_install, | 830 | .install = userns_install, |
| 831 | .inum = userns_inum, | ||
| 817 | }; | 832 | }; |
| 818 | 833 | ||
| 819 | static __init int user_namespaces_init(void) | 834 | static __init int user_namespaces_init(void) |
diff --git a/kernel/utsname.c b/kernel/utsname.c index fdc619eb61e..f6336d51d64 100644 --- a/kernel/utsname.c +++ b/kernel/utsname.c | |||
| @@ -36,11 +36,18 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, | |||
| 36 | struct uts_namespace *old_ns) | 36 | struct uts_namespace *old_ns) |
| 37 | { | 37 | { |
| 38 | struct uts_namespace *ns; | 38 | struct uts_namespace *ns; |
| 39 | int err; | ||
| 39 | 40 | ||
| 40 | ns = create_uts_ns(); | 41 | ns = create_uts_ns(); |
| 41 | if (!ns) | 42 | if (!ns) |
| 42 | return ERR_PTR(-ENOMEM); | 43 | return ERR_PTR(-ENOMEM); |
| 43 | 44 | ||
| 45 | err = proc_alloc_inum(&ns->proc_inum); | ||
| 46 | if (err) { | ||
| 47 | kfree(ns); | ||
| 48 | return ERR_PTR(err); | ||
| 49 | } | ||
| 50 | |||
| 44 | down_read(&uts_sem); | 51 | down_read(&uts_sem); |
| 45 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); | 52 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
| 46 | ns->user_ns = get_user_ns(user_ns); | 53 | ns->user_ns = get_user_ns(user_ns); |
| @@ -77,6 +84,7 @@ void free_uts_ns(struct kref *kref) | |||
| 77 | 84 | ||
| 78 | ns = container_of(kref, struct uts_namespace, kref); | 85 | ns = container_of(kref, struct uts_namespace, kref); |
| 79 | put_user_ns(ns->user_ns); | 86 | put_user_ns(ns->user_ns); |
| 87 | proc_free_inum(ns->proc_inum); | ||
| 80 | kfree(ns); | 88 | kfree(ns); |
| 81 | } | 89 | } |
| 82 | 90 | ||
| @@ -114,11 +122,18 @@ static int utsns_install(struct nsproxy *nsproxy, void *new) | |||
| 114 | return 0; | 122 | return 0; |
| 115 | } | 123 | } |
| 116 | 124 | ||
| 125 | static unsigned int utsns_inum(void *vp) | ||
| 126 | { | ||
| 127 | struct uts_namespace *ns = vp; | ||
| 128 | |||
| 129 | return ns->proc_inum; | ||
| 130 | } | ||
| 131 | |||
| 117 | const struct proc_ns_operations utsns_operations = { | 132 | const struct proc_ns_operations utsns_operations = { |
| 118 | .name = "uts", | 133 | .name = "uts", |
| 119 | .type = CLONE_NEWUTS, | 134 | .type = CLONE_NEWUTS, |
| 120 | .get = utsns_get, | 135 | .get = utsns_get, |
| 121 | .put = utsns_put, | 136 | .put = utsns_put, |
| 122 | .install = utsns_install, | 137 | .install = utsns_install, |
| 138 | .inum = utsns_inum, | ||
| 123 | }; | 139 | }; |
| 124 | |||
