1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/slab.h>
17#include <linux/export.h>
18#include <linux/nsproxy.h>
19#include <linux/init_task.h>
20#include <linux/mnt_namespace.h>
21#include <linux/utsname.h>
22#include <linux/pid_namespace.h>
23#include <net/net_namespace.h>
24#include <linux/ipc_namespace.h>
25#include <linux/time_namespace.h>
26#include <linux/proc_ns.h>
27#include <linux/file.h>
28#include <linux/syscalls.h>
29#include <linux/cgroup.h>
30#include <linux/perf_event.h>
31
32static struct kmem_cache *nsproxy_cachep;
33
34struct nsproxy init_nsproxy = {
35 .count = ATOMIC_INIT(1),
36 .uts_ns = &init_uts_ns,
37#if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
38 .ipc_ns = &init_ipc_ns,
39#endif
40 .mnt_ns = NULL,
41 .pid_ns_for_children = &init_pid_ns,
42#ifdef CONFIG_NET
43 .net_ns = &init_net,
44#endif
45#ifdef CONFIG_CGROUPS
46 .cgroup_ns = &init_cgroup_ns,
47#endif
48#ifdef CONFIG_TIME_NS
49 .time_ns = &init_time_ns,
50 .time_ns_for_children = &init_time_ns,
51#endif
52};
53
54static inline struct nsproxy *create_nsproxy(void)
55{
56 struct nsproxy *nsproxy;
57
58 nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
59 if (nsproxy)
60 atomic_set(&nsproxy->count, 1);
61 return nsproxy;
62}
63
64
65
66
67
68
69static struct nsproxy *create_new_namespaces(unsigned long flags,
70 struct task_struct *tsk, struct user_namespace *user_ns,
71 struct fs_struct *new_fs)
72{
73 struct nsproxy *new_nsp;
74 int err;
75
76 new_nsp = create_nsproxy();
77 if (!new_nsp)
78 return ERR_PTR(-ENOMEM);
79
80 new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs);
81 if (IS_ERR(new_nsp->mnt_ns)) {
82 err = PTR_ERR(new_nsp->mnt_ns);
83 goto out_ns;
84 }
85
86 new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns);
87 if (IS_ERR(new_nsp->uts_ns)) {
88 err = PTR_ERR(new_nsp->uts_ns);
89 goto out_uts;
90 }
91
92 new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns);
93 if (IS_ERR(new_nsp->ipc_ns)) {
94 err = PTR_ERR(new_nsp->ipc_ns);
95 goto out_ipc;
96 }
97
98 new_nsp->pid_ns_for_children =
99 copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children);
100 if (IS_ERR(new_nsp->pid_ns_for_children)) {
101 err = PTR_ERR(new_nsp->pid_ns_for_children);
102 goto out_pid;
103 }
104
105 new_nsp->cgroup_ns = copy_cgroup_ns(flags, user_ns,
106 tsk->nsproxy->cgroup_ns);
107 if (IS_ERR(new_nsp->cgroup_ns)) {
108 err = PTR_ERR(new_nsp->cgroup_ns);
109 goto out_cgroup;
110 }
111
112 new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns);
113 if (IS_ERR(new_nsp->net_ns)) {
114 err = PTR_ERR(new_nsp->net_ns);
115 goto out_net;
116 }
117
118 new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns,
119 tsk->nsproxy->time_ns_for_children);
120 if (IS_ERR(new_nsp->time_ns_for_children)) {
121 err = PTR_ERR(new_nsp->time_ns_for_children);
122 goto out_time;
123 }
124 new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
125
126 return new_nsp;
127
128out_time:
129 put_net(new_nsp->net_ns);
130out_net:
131 put_cgroup_ns(new_nsp->cgroup_ns);
132out_cgroup:
133 if (new_nsp->pid_ns_for_children)
134 put_pid_ns(new_nsp->pid_ns_for_children);
135out_pid:
136 if (new_nsp->ipc_ns)
137 put_ipc_ns(new_nsp->ipc_ns);
138out_ipc:
139 if (new_nsp->uts_ns)
140 put_uts_ns(new_nsp->uts_ns);
141out_uts:
142 if (new_nsp->mnt_ns)
143 put_mnt_ns(new_nsp->mnt_ns);
144out_ns:
145 kmem_cache_free(nsproxy_cachep, new_nsp);
146 return ERR_PTR(err);
147}
148
149
150
151
152
153int copy_namespaces(unsigned long flags, struct task_struct *tsk)
154{
155 struct nsproxy *old_ns = tsk->nsproxy;
156 struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
157 struct nsproxy *new_ns;
158 int ret;
159
160 if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
161 CLONE_NEWPID | CLONE_NEWNET |
162 CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
163 if (likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
164 get_nsproxy(old_ns);
165 return 0;
166 }
167 } else if (!ns_capable(user_ns, CAP_SYS_ADMIN))
168 return -EPERM;
169
170
171
172
173
174
175
176
177 if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
178 (CLONE_NEWIPC | CLONE_SYSVSEM))
179 return -EINVAL;
180
181 new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
182 if (IS_ERR(new_ns))
183 return PTR_ERR(new_ns);
184
185 ret = timens_on_fork(new_ns, tsk);
186 if (ret) {
187 free_nsproxy(new_ns);
188 return ret;
189 }
190
191 tsk->nsproxy = new_ns;
192 return 0;
193}
194
195void free_nsproxy(struct nsproxy *ns)
196{
197 if (ns->mnt_ns)
198 put_mnt_ns(ns->mnt_ns);
199 if (ns->uts_ns)
200 put_uts_ns(ns->uts_ns);
201 if (ns->ipc_ns)
202 put_ipc_ns(ns->ipc_ns);
203 if (ns->pid_ns_for_children)
204 put_pid_ns(ns->pid_ns_for_children);
205 if (ns->time_ns)
206 put_time_ns(ns->time_ns);
207 if (ns->time_ns_for_children)
208 put_time_ns(ns->time_ns_for_children);
209 put_cgroup_ns(ns->cgroup_ns);
210 put_net(ns->net_ns);
211 kmem_cache_free(nsproxy_cachep, ns);
212}
213
214
215
216
217
218int unshare_nsproxy_namespaces(unsigned long unshare_flags,
219 struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
220{
221 struct user_namespace *user_ns;
222 int err = 0;
223
224 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
225 CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
226 CLONE_NEWTIME)))
227 return 0;
228
229 user_ns = new_cred ? new_cred->user_ns : current_user_ns();
230 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
231 return -EPERM;
232
233 *new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
234 new_fs ? new_fs : current->fs);
235 if (IS_ERR(*new_nsp)) {
236 err = PTR_ERR(*new_nsp);
237 goto out;
238 }
239
240out:
241 return err;
242}
243
244void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
245{
246 struct nsproxy *ns;
247
248 might_sleep();
249
250 task_lock(p);
251 ns = p->nsproxy;
252 p->nsproxy = new;
253 task_unlock(p);
254
255 if (ns && atomic_dec_and_test(&ns->count))
256 free_nsproxy(ns);
257}
258
259void exit_task_namespaces(struct task_struct *p)
260{
261 switch_task_namespaces(p, NULL);
262}
263
264SYSCALL_DEFINE2(setns, int, fd, int, nstype)
265{
266 struct task_struct *tsk = current;
267 struct nsproxy *new_nsproxy;
268 struct file *file;
269 struct ns_common *ns;
270 int err;
271
272 file = proc_ns_fget(fd);
273 if (IS_ERR(file))
274 return PTR_ERR(file);
275
276 err = -EINVAL;
277 ns = get_proc_ns(file_inode(file));
278 if (nstype && (ns->ops->type != nstype))
279 goto out;
280
281 new_nsproxy = create_new_namespaces(0, tsk, current_user_ns(), tsk->fs);
282 if (IS_ERR(new_nsproxy)) {
283 err = PTR_ERR(new_nsproxy);
284 goto out;
285 }
286
287 err = ns->ops->install(new_nsproxy, ns);
288 if (err) {
289 free_nsproxy(new_nsproxy);
290 goto out;
291 }
292 switch_task_namespaces(tsk, new_nsproxy);
293
294 perf_event_namespaces(tsk);
295out:
296 fput(file);
297 return err;
298}
299
300int __init nsproxy_cache_init(void)
301{
302 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
303 return 0;
304}
305