]>
Commit | Line | Data |
---|---|---|
ab516013 SH |
1 | /* |
2 | * Copyright (C) 2006 IBM Corporation | |
3 | * | |
4 | * Author: Serge Hallyn <serue@us.ibm.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2 of the | |
9 | * License. | |
25b21cb2 KK |
10 | * |
11 | * Jun 2006 - namespaces support | |
12 | * OpenVZ, SWsoft Inc. | |
13 | * Pavel Emelianov <xemul@openvz.org> | |
ab516013 SH |
14 | */ |
15 | ||
5a0e3ad6 | 16 | #include <linux/slab.h> |
9984de1a | 17 | #include <linux/export.h> |
ab516013 | 18 | #include <linux/nsproxy.h> |
0437eb59 | 19 | #include <linux/init_task.h> |
6b3286ed | 20 | #include <linux/mnt_namespace.h> |
4865ecf1 | 21 | #include <linux/utsname.h> |
9a575a92 | 22 | #include <linux/pid_namespace.h> |
9dd776b6 | 23 | #include <net/net_namespace.h> |
ae5e1b22 | 24 | #include <linux/ipc_namespace.h> |
0bb80f24 | 25 | #include <linux/proc_ns.h> |
0663c6f8 EB |
26 | #include <linux/file.h> |
27 | #include <linux/syscalls.h> | |
0437eb59 | 28 | |
98c0d07c CLG |
29 | static struct kmem_cache *nsproxy_cachep; |
30 | ||
8467005d | 31 | struct nsproxy init_nsproxy = { |
c2b1df2e AL |
32 | .count = ATOMIC_INIT(1), |
33 | .uts_ns = &init_uts_ns, | |
8467005d | 34 | #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC) |
c2b1df2e | 35 | .ipc_ns = &init_ipc_ns, |
8467005d | 36 | #endif |
c2b1df2e AL |
37 | .mnt_ns = NULL, |
38 | .pid_ns_for_children = &init_pid_ns, | |
8467005d | 39 | #ifdef CONFIG_NET |
c2b1df2e | 40 | .net_ns = &init_net, |
8467005d AD |
41 | #endif |
42 | }; | |
ab516013 | 43 | |
90af90d7 | 44 | static inline struct nsproxy *create_nsproxy(void) |
ab516013 | 45 | { |
90af90d7 | 46 | struct nsproxy *nsproxy; |
ab516013 | 47 | |
90af90d7 AD |
48 | nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL); |
49 | if (nsproxy) | |
50 | atomic_set(&nsproxy->count, 1); | |
51 | return nsproxy; | |
ab516013 SH |
52 | } |
53 | ||
54 | /* | |
e3222c4e BP |
55 | * Create new nsproxy and all of its the associated namespaces. |
56 | * Return the newly created nsproxy. Do not attach this to the task, | |
57 | * leave it to the caller to do proper locking and attach it to task. | |
ab516013 | 58 | */ |
213dd266 | 59 | static struct nsproxy *create_new_namespaces(unsigned long flags, |
bcf58e72 EB |
60 | struct task_struct *tsk, struct user_namespace *user_ns, |
61 | struct fs_struct *new_fs) | |
ab516013 | 62 | { |
e3222c4e | 63 | struct nsproxy *new_nsp; |
467e9f4b | 64 | int err; |
ab516013 | 65 | |
90af90d7 | 66 | new_nsp = create_nsproxy(); |
e3222c4e BP |
67 | if (!new_nsp) |
68 | return ERR_PTR(-ENOMEM); | |
1651e14e | 69 | |
bcf58e72 | 70 | new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs); |
467e9f4b CLG |
71 | if (IS_ERR(new_nsp->mnt_ns)) { |
72 | err = PTR_ERR(new_nsp->mnt_ns); | |
e3222c4e | 73 | goto out_ns; |
467e9f4b | 74 | } |
e3222c4e | 75 | |
bcf58e72 | 76 | new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns); |
467e9f4b CLG |
77 | if (IS_ERR(new_nsp->uts_ns)) { |
78 | err = PTR_ERR(new_nsp->uts_ns); | |
e3222c4e | 79 | goto out_uts; |
467e9f4b | 80 | } |
e3222c4e | 81 | |
bcf58e72 | 82 | new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns); |
467e9f4b CLG |
83 | if (IS_ERR(new_nsp->ipc_ns)) { |
84 | err = PTR_ERR(new_nsp->ipc_ns); | |
e3222c4e | 85 | goto out_ipc; |
467e9f4b | 86 | } |
e3222c4e | 87 | |
c2b1df2e AL |
88 | new_nsp->pid_ns_for_children = |
89 | copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children); | |
90 | if (IS_ERR(new_nsp->pid_ns_for_children)) { | |
91 | err = PTR_ERR(new_nsp->pid_ns_for_children); | |
e3222c4e | 92 | goto out_pid; |
467e9f4b | 93 | } |
e3222c4e | 94 | |
bcf58e72 | 95 | new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns); |
9dd776b6 EB |
96 | if (IS_ERR(new_nsp->net_ns)) { |
97 | err = PTR_ERR(new_nsp->net_ns); | |
98 | goto out_net; | |
99 | } | |
100 | ||
e3222c4e BP |
101 | return new_nsp; |
102 | ||
9dd776b6 | 103 | out_net: |
c2b1df2e AL |
104 | if (new_nsp->pid_ns_for_children) |
105 | put_pid_ns(new_nsp->pid_ns_for_children); | |
e3222c4e BP |
106 | out_pid: |
107 | if (new_nsp->ipc_ns) | |
108 | put_ipc_ns(new_nsp->ipc_ns); | |
109 | out_ipc: | |
110 | if (new_nsp->uts_ns) | |
111 | put_uts_ns(new_nsp->uts_ns); | |
112 | out_uts: | |
113 | if (new_nsp->mnt_ns) | |
114 | put_mnt_ns(new_nsp->mnt_ns); | |
115 | out_ns: | |
98c0d07c | 116 | kmem_cache_free(nsproxy_cachep, new_nsp); |
467e9f4b | 117 | return ERR_PTR(err); |
ab516013 SH |
118 | } |
119 | ||
120 | /* | |
121 | * called from clone. This now handles copy for nsproxy and all | |
122 | * namespaces therein. | |
123 | */ | |
213dd266 | 124 | int copy_namespaces(unsigned long flags, struct task_struct *tsk) |
ab516013 SH |
125 | { |
126 | struct nsproxy *old_ns = tsk->nsproxy; | |
b33c77ef | 127 | struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns); |
1651e14e | 128 | struct nsproxy *new_ns; |
ab516013 | 129 | |
dbef0c1c EB |
130 | if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | |
131 | CLONE_NEWPID | CLONE_NEWNET)))) { | |
132 | get_nsproxy(old_ns); | |
ab516013 | 133 | return 0; |
1651e14e SH |
134 | } |
135 | ||
dbef0c1c EB |
136 | if (!ns_capable(user_ns, CAP_SYS_ADMIN)) |
137 | return -EPERM; | |
138 | ||
02fdb36a SH |
139 | /* |
140 | * CLONE_NEWIPC must detach from the undolist: after switching | |
141 | * to a new ipc namespace, the semaphore arrays from the old | |
142 | * namespace are unreachable. In clone parlance, CLONE_SYSVSEM | |
143 | * means share undolist with parent, so we must forbid using | |
144 | * it along with CLONE_NEWIPC. | |
145 | */ | |
21e85194 | 146 | if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) == |
dbef0c1c EB |
147 | (CLONE_NEWIPC | CLONE_SYSVSEM)) |
148 | return -EINVAL; | |
02fdb36a | 149 | |
d7d48f62 | 150 | new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs); |
dbef0c1c EB |
151 | if (IS_ERR(new_ns)) |
152 | return PTR_ERR(new_ns); | |
9a575a92 | 153 | |
e3222c4e | 154 | tsk->nsproxy = new_ns; |
dbef0c1c | 155 | return 0; |
ab516013 SH |
156 | } |
157 | ||
158 | void free_nsproxy(struct nsproxy *ns) | |
159 | { | |
9a575a92 CLG |
160 | if (ns->mnt_ns) |
161 | put_mnt_ns(ns->mnt_ns); | |
162 | if (ns->uts_ns) | |
163 | put_uts_ns(ns->uts_ns); | |
164 | if (ns->ipc_ns) | |
165 | put_ipc_ns(ns->ipc_ns); | |
c2b1df2e AL |
166 | if (ns->pid_ns_for_children) |
167 | put_pid_ns(ns->pid_ns_for_children); | |
9dd776b6 | 168 | put_net(ns->net_ns); |
98c0d07c | 169 | kmem_cache_free(nsproxy_cachep, ns); |
ab516013 | 170 | } |
e3222c4e BP |
171 | |
172 | /* | |
173 | * Called from unshare. Unshare all the namespaces part of nsproxy. | |
4e71e474 | 174 | * On success, returns the new nsproxy. |
e3222c4e BP |
175 | */ |
176 | int unshare_nsproxy_namespaces(unsigned long unshare_flags, | |
b2e0d987 | 177 | struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs) |
e3222c4e | 178 | { |
bcf58e72 | 179 | struct user_namespace *user_ns; |
e3222c4e BP |
180 | int err = 0; |
181 | ||
77ec739d | 182 | if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | |
50804fe3 | 183 | CLONE_NEWNET | CLONE_NEWPID))) |
e3222c4e BP |
184 | return 0; |
185 | ||
b2e0d987 EB |
186 | user_ns = new_cred ? new_cred->user_ns : current_user_ns(); |
187 | if (!ns_capable(user_ns, CAP_SYS_ADMIN)) | |
e3222c4e BP |
188 | return -EPERM; |
189 | ||
bcf58e72 | 190 | *new_nsp = create_new_namespaces(unshare_flags, current, user_ns, |
b2e0d987 | 191 | new_fs ? new_fs : current->fs); |
858d72ea | 192 | if (IS_ERR(*new_nsp)) { |
e3222c4e | 193 | err = PTR_ERR(*new_nsp); |
858d72ea SH |
194 | goto out; |
195 | } | |
196 | ||
858d72ea | 197 | out: |
e3222c4e BP |
198 | return err; |
199 | } | |
98c0d07c | 200 | |
cf7b708c PE |
201 | void switch_task_namespaces(struct task_struct *p, struct nsproxy *new) |
202 | { | |
203 | struct nsproxy *ns; | |
204 | ||
205 | might_sleep(); | |
206 | ||
728dba3a | 207 | task_lock(p); |
cf7b708c | 208 | ns = p->nsproxy; |
728dba3a EB |
209 | p->nsproxy = new; |
210 | task_unlock(p); | |
cf7b708c | 211 | |
728dba3a | 212 | if (ns && atomic_dec_and_test(&ns->count)) |
cf7b708c | 213 | free_nsproxy(ns); |
cf7b708c PE |
214 | } |
215 | ||
216 | void exit_task_namespaces(struct task_struct *p) | |
217 | { | |
218 | switch_task_namespaces(p, NULL); | |
219 | } | |
220 | ||
0663c6f8 EB |
221 | SYSCALL_DEFINE2(setns, int, fd, int, nstype) |
222 | { | |
0663c6f8 EB |
223 | struct task_struct *tsk = current; |
224 | struct nsproxy *new_nsproxy; | |
0663c6f8 | 225 | struct file *file; |
33c42940 | 226 | struct ns_common *ns; |
0663c6f8 EB |
227 | int err; |
228 | ||
0663c6f8 EB |
229 | file = proc_ns_fget(fd); |
230 | if (IS_ERR(file)) | |
231 | return PTR_ERR(file); | |
232 | ||
233 | err = -EINVAL; | |
f77c8014 | 234 | ns = get_proc_ns(file_inode(file)); |
33c42940 | 235 | if (nstype && (ns->ops->type != nstype)) |
0663c6f8 EB |
236 | goto out; |
237 | ||
bcf58e72 | 238 | new_nsproxy = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs); |
0663c6f8 EB |
239 | if (IS_ERR(new_nsproxy)) { |
240 | err = PTR_ERR(new_nsproxy); | |
241 | goto out; | |
242 | } | |
243 | ||
33c42940 | 244 | err = ns->ops->install(new_nsproxy, ns); |
0663c6f8 EB |
245 | if (err) { |
246 | free_nsproxy(new_nsproxy); | |
247 | goto out; | |
248 | } | |
249 | switch_task_namespaces(tsk, new_nsproxy); | |
250 | out: | |
251 | fput(file); | |
252 | return err; | |
253 | } | |
254 | ||
66577193 | 255 | int __init nsproxy_cache_init(void) |
98c0d07c | 256 | { |
db8906da | 257 | nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC); |
98c0d07c CLG |
258 | return 0; |
259 | } |