1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2006 IBM Corporation
4 *
5 * Author: Serge Hallyn <serue@us.ibm.com>
6 *
7 * Jun 2006 - namespaces support
8 * OpenVZ, SWsoft Inc.
9 * Pavel Emelianov <xemul@openvz.org>
10 */
11
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <linux/nsproxy.h>
15 #include <linux/init_task.h>
16 #include <linux/mnt_namespace.h>
17 #include <linux/utsname.h>
18 #include <linux/pid_namespace.h>
19 #include <net/net_namespace.h>
20 #include <linux/ipc_namespace.h>
21 #include <linux/time_namespace.h>
22 #include <linux/fs_struct.h>
23 #include <linux/proc_fs.h>
24 #include <linux/proc_ns.h>
25 #include <linux/file.h>
26 #include <linux/syscalls.h>
27 #include <linux/cgroup.h>
28 #include <linux/perf_event.h>
29 #include <linux/hck/lite_hck_ced.h>
30
31 static struct kmem_cache *nsproxy_cachep;
32
33 struct nsproxy init_nsproxy = {
34 .count = ATOMIC_INIT(1),
35 .uts_ns = &init_uts_ns,
36 #if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
37 .ipc_ns = &init_ipc_ns,
38 #endif
39 .mnt_ns = NULL,
40 .pid_ns_for_children = &init_pid_ns,
41 #ifdef CONFIG_NET
42 .net_ns = &init_net,
43 #endif
44 #ifdef CONFIG_CGROUPS
45 .cgroup_ns = &init_cgroup_ns,
46 #endif
47 #ifdef CONFIG_TIME_NS
48 .time_ns = &init_time_ns,
49 .time_ns_for_children = &init_time_ns,
50 #endif
51 };
52
create_nsproxy(void)53 static inline struct nsproxy *create_nsproxy(void)
54 {
55 struct nsproxy *nsproxy;
56
57 nsproxy = kmem_cache_alloc(nsproxy_cachep, GFP_KERNEL);
58 if (nsproxy)
59 atomic_set(&nsproxy->count, 1);
60 return nsproxy;
61 }
62
63 /*
64 * Create new nsproxy and all of its the associated namespaces.
65 * Return the newly created nsproxy. Do not attach this to the task,
66 * leave it to the caller to do proper locking and attach it to task.
67 */
create_new_namespaces(unsigned long flags,struct task_struct * tsk,struct user_namespace * user_ns,struct fs_struct * new_fs)68 static struct nsproxy *create_new_namespaces(unsigned long flags,
69 struct task_struct *tsk, struct user_namespace *user_ns,
70 struct fs_struct *new_fs)
71 {
72 struct nsproxy *new_nsp;
73 int err;
74
75 new_nsp = create_nsproxy();
76 if (!new_nsp)
77 return ERR_PTR(-ENOMEM);
78
79 new_nsp->mnt_ns = copy_mnt_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs);
80 if (IS_ERR(new_nsp->mnt_ns)) {
81 err = PTR_ERR(new_nsp->mnt_ns);
82 goto out_ns;
83 }
84
85 new_nsp->uts_ns = copy_utsname(flags, user_ns, tsk->nsproxy->uts_ns);
86 if (IS_ERR(new_nsp->uts_ns)) {
87 err = PTR_ERR(new_nsp->uts_ns);
88 goto out_uts;
89 }
90
91 new_nsp->ipc_ns = copy_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns);
92 if (IS_ERR(new_nsp->ipc_ns)) {
93 err = PTR_ERR(new_nsp->ipc_ns);
94 goto out_ipc;
95 }
96
97 new_nsp->pid_ns_for_children =
98 copy_pid_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children);
99 if (IS_ERR(new_nsp->pid_ns_for_children)) {
100 err = PTR_ERR(new_nsp->pid_ns_for_children);
101 goto out_pid;
102 }
103
104 new_nsp->cgroup_ns = copy_cgroup_ns(flags, user_ns,
105 tsk->nsproxy->cgroup_ns);
106 if (IS_ERR(new_nsp->cgroup_ns)) {
107 err = PTR_ERR(new_nsp->cgroup_ns);
108 goto out_cgroup;
109 }
110
111 new_nsp->net_ns = copy_net_ns(flags, user_ns, tsk->nsproxy->net_ns);
112 if (IS_ERR(new_nsp->net_ns)) {
113 err = PTR_ERR(new_nsp->net_ns);
114 goto out_net;
115 }
116
117 new_nsp->time_ns_for_children = copy_time_ns(flags, user_ns,
118 tsk->nsproxy->time_ns_for_children);
119 if (IS_ERR(new_nsp->time_ns_for_children)) {
120 err = PTR_ERR(new_nsp->time_ns_for_children);
121 goto out_time;
122 }
123 new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
124
125 return new_nsp;
126
127 out_time:
128 put_net(new_nsp->net_ns);
129 out_net:
130 put_cgroup_ns(new_nsp->cgroup_ns);
131 out_cgroup:
132 if (new_nsp->pid_ns_for_children)
133 put_pid_ns(new_nsp->pid_ns_for_children);
134 out_pid:
135 if (new_nsp->ipc_ns)
136 put_ipc_ns(new_nsp->ipc_ns);
137 out_ipc:
138 if (new_nsp->uts_ns)
139 put_uts_ns(new_nsp->uts_ns);
140 out_uts:
141 if (new_nsp->mnt_ns)
142 put_mnt_ns(new_nsp->mnt_ns);
143 out_ns:
144 kmem_cache_free(nsproxy_cachep, new_nsp);
145 return ERR_PTR(err);
146 }
147
148 /*
149 * called from clone. This now handles copy for nsproxy and all
150 * namespaces therein.
151 */
copy_namespaces(unsigned long flags,struct task_struct * tsk)152 int copy_namespaces(unsigned long flags, struct task_struct *tsk)
153 {
154 struct nsproxy *old_ns = tsk->nsproxy;
155 struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
156 struct nsproxy *new_ns;
157 int ret;
158
159 if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
160 CLONE_NEWPID | CLONE_NEWNET |
161 CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
162 if (likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
163 get_nsproxy(old_ns);
164 return 0;
165 }
166 } else if (!ns_capable(user_ns, CAP_SYS_ADMIN))
167 return -EPERM;
168
169 /*
170 * CLONE_NEWIPC must detach from the undolist: after switching
171 * to a new ipc namespace, the semaphore arrays from the old
172 * namespace are unreachable. In clone parlance, CLONE_SYSVSEM
173 * means share undolist with parent, so we must forbid using
174 * it along with CLONE_NEWIPC.
175 */
176 if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
177 (CLONE_NEWIPC | CLONE_SYSVSEM))
178 return -EINVAL;
179
180 new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
181 if (IS_ERR(new_ns))
182 return PTR_ERR(new_ns);
183
184 ret = timens_on_fork(new_ns, tsk);
185 if (ret) {
186 free_nsproxy(new_ns);
187 return ret;
188 }
189
190 tsk->nsproxy = new_ns;
191 return 0;
192 }
193
free_nsproxy(struct nsproxy * ns)194 void free_nsproxy(struct nsproxy *ns)
195 {
196 if (ns->mnt_ns)
197 put_mnt_ns(ns->mnt_ns);
198 if (ns->uts_ns)
199 put_uts_ns(ns->uts_ns);
200 if (ns->ipc_ns)
201 put_ipc_ns(ns->ipc_ns);
202 if (ns->pid_ns_for_children)
203 put_pid_ns(ns->pid_ns_for_children);
204 if (ns->time_ns)
205 put_time_ns(ns->time_ns);
206 if (ns->time_ns_for_children)
207 put_time_ns(ns->time_ns_for_children);
208 put_cgroup_ns(ns->cgroup_ns);
209 put_net(ns->net_ns);
210 kmem_cache_free(nsproxy_cachep, ns);
211 }
212
213 /*
214 * Called from unshare. Unshare all the namespaces part of nsproxy.
215 * On success, returns the new nsproxy.
216 */
unshare_nsproxy_namespaces(unsigned long unshare_flags,struct nsproxy ** new_nsp,struct cred * new_cred,struct fs_struct * new_fs)217 int unshare_nsproxy_namespaces(unsigned long unshare_flags,
218 struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs)
219 {
220 struct user_namespace *user_ns;
221 int err = 0;
222
223 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
224 CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
225 CLONE_NEWTIME)))
226 return 0;
227
228 user_ns = new_cred ? new_cred->user_ns : current_user_ns();
229 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
230 return -EPERM;
231
232 *new_nsp = create_new_namespaces(unshare_flags, current, user_ns,
233 new_fs ? new_fs : current->fs);
234 if (IS_ERR(*new_nsp)) {
235 err = PTR_ERR(*new_nsp);
236 goto out;
237 }
238
239 out:
240 return err;
241 }
242
switch_task_namespaces(struct task_struct * p,struct nsproxy * new)243 void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
244 {
245 struct nsproxy *ns;
246 int ret = 0;
247 CALL_HCK_LITE_HOOK(ced_switch_task_namespaces_lhck, new);
248 CALL_HCK_LITE_HOOK(ced_switch_task_namespaces_permission_lhck, new, &ret);
249 if (ret)
250 return;
251
252 might_sleep();
253
254 task_lock(p);
255 ns = p->nsproxy;
256 p->nsproxy = new;
257 task_unlock(p);
258
259 if (ns && atomic_dec_and_test(&ns->count))
260 free_nsproxy(ns);
261 }
262
exit_task_namespaces(struct task_struct * p)263 void exit_task_namespaces(struct task_struct *p)
264 {
265 switch_task_namespaces(p, NULL);
266 }
267
check_setns_flags(unsigned long flags)268 static int check_setns_flags(unsigned long flags)
269 {
270 if (!flags || (flags & ~(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
271 CLONE_NEWNET | CLONE_NEWTIME | CLONE_NEWUSER |
272 CLONE_NEWPID | CLONE_NEWCGROUP)))
273 return -EINVAL;
274
275 #ifndef CONFIG_USER_NS
276 if (flags & CLONE_NEWUSER)
277 return -EINVAL;
278 #endif
279 #ifndef CONFIG_PID_NS
280 if (flags & CLONE_NEWPID)
281 return -EINVAL;
282 #endif
283 #ifndef CONFIG_UTS_NS
284 if (flags & CLONE_NEWUTS)
285 return -EINVAL;
286 #endif
287 #ifndef CONFIG_IPC_NS
288 if (flags & CLONE_NEWIPC)
289 return -EINVAL;
290 #endif
291 #ifndef CONFIG_CGROUPS
292 if (flags & CLONE_NEWCGROUP)
293 return -EINVAL;
294 #endif
295 #ifndef CONFIG_NET_NS
296 if (flags & CLONE_NEWNET)
297 return -EINVAL;
298 #endif
299 #ifndef CONFIG_TIME_NS
300 if (flags & CLONE_NEWTIME)
301 return -EINVAL;
302 #endif
303
304 return 0;
305 }
306
put_nsset(struct nsset * nsset)307 static void put_nsset(struct nsset *nsset)
308 {
309 unsigned flags = nsset->flags;
310
311 if (flags & CLONE_NEWUSER)
312 put_cred(nsset_cred(nsset));
313 /*
314 * We only created a temporary copy if we attached to more than just
315 * the mount namespace.
316 */
317 if (nsset->fs && (flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS))
318 free_fs_struct(nsset->fs);
319 if (nsset->nsproxy)
320 free_nsproxy(nsset->nsproxy);
321 }
322
prepare_nsset(unsigned flags,struct nsset * nsset)323 static int prepare_nsset(unsigned flags, struct nsset *nsset)
324 {
325 struct task_struct *me = current;
326
327 nsset->nsproxy = create_new_namespaces(0, me, current_user_ns(), me->fs);
328 if (IS_ERR(nsset->nsproxy))
329 return PTR_ERR(nsset->nsproxy);
330
331 if (flags & CLONE_NEWUSER)
332 nsset->cred = prepare_creds();
333 else
334 nsset->cred = current_cred();
335 if (!nsset->cred)
336 goto out;
337
338 /* Only create a temporary copy of fs_struct if we really need to. */
339 if (flags == CLONE_NEWNS) {
340 nsset->fs = me->fs;
341 } else if (flags & CLONE_NEWNS) {
342 nsset->fs = copy_fs_struct(me->fs);
343 if (!nsset->fs)
344 goto out;
345 }
346
347 nsset->flags = flags;
348 return 0;
349
350 out:
351 put_nsset(nsset);
352 return -ENOMEM;
353 }
354
validate_ns(struct nsset * nsset,struct ns_common * ns)355 static inline int validate_ns(struct nsset *nsset, struct ns_common *ns)
356 {
357 return ns->ops->install(nsset, ns);
358 }
359
360 /*
361 * This is the inverse operation to unshare().
362 * Ordering is equivalent to the standard ordering used everywhere else
363 * during unshare and process creation. The switch to the new set of
364 * namespaces occurs at the point of no return after installation of
365 * all requested namespaces was successful in commit_nsset().
366 */
validate_nsset(struct nsset * nsset,struct pid * pid)367 static int validate_nsset(struct nsset *nsset, struct pid *pid)
368 {
369 int ret = 0;
370 unsigned flags = nsset->flags;
371 struct user_namespace *user_ns = NULL;
372 struct pid_namespace *pid_ns = NULL;
373 struct nsproxy *nsp;
374 struct task_struct *tsk;
375
376 /* Take a "snapshot" of the target task's namespaces. */
377 rcu_read_lock();
378 tsk = pid_task(pid, PIDTYPE_PID);
379 if (!tsk) {
380 rcu_read_unlock();
381 return -ESRCH;
382 }
383
384 if (!ptrace_may_access(tsk, PTRACE_MODE_READ_REALCREDS)) {
385 rcu_read_unlock();
386 return -EPERM;
387 }
388
389 task_lock(tsk);
390 nsp = tsk->nsproxy;
391 if (nsp)
392 get_nsproxy(nsp);
393 task_unlock(tsk);
394 if (!nsp) {
395 rcu_read_unlock();
396 return -ESRCH;
397 }
398
399 #ifdef CONFIG_PID_NS
400 if (flags & CLONE_NEWPID) {
401 pid_ns = task_active_pid_ns(tsk);
402 if (unlikely(!pid_ns)) {
403 rcu_read_unlock();
404 ret = -ESRCH;
405 goto out;
406 }
407 get_pid_ns(pid_ns);
408 }
409 #endif
410
411 #ifdef CONFIG_USER_NS
412 if (flags & CLONE_NEWUSER)
413 user_ns = get_user_ns(__task_cred(tsk)->user_ns);
414 #endif
415 rcu_read_unlock();
416
417 /*
418 * Install requested namespaces. The caller will have
419 * verified earlier that the requested namespaces are
420 * supported on this kernel. We don't report errors here
421 * if a namespace is requested that isn't supported.
422 */
423 #ifdef CONFIG_USER_NS
424 if (flags & CLONE_NEWUSER) {
425 ret = validate_ns(nsset, &user_ns->ns);
426 if (ret)
427 goto out;
428 }
429 #endif
430
431 if (flags & CLONE_NEWNS) {
432 ret = validate_ns(nsset, from_mnt_ns(nsp->mnt_ns));
433 if (ret)
434 goto out;
435 }
436
437 #ifdef CONFIG_UTS_NS
438 if (flags & CLONE_NEWUTS) {
439 ret = validate_ns(nsset, &nsp->uts_ns->ns);
440 if (ret)
441 goto out;
442 }
443 #endif
444
445 #ifdef CONFIG_IPC_NS
446 if (flags & CLONE_NEWIPC) {
447 ret = validate_ns(nsset, &nsp->ipc_ns->ns);
448 if (ret)
449 goto out;
450 }
451 #endif
452
453 #ifdef CONFIG_PID_NS
454 if (flags & CLONE_NEWPID) {
455 ret = validate_ns(nsset, &pid_ns->ns);
456 if (ret)
457 goto out;
458 }
459 #endif
460
461 #ifdef CONFIG_CGROUPS
462 if (flags & CLONE_NEWCGROUP) {
463 ret = validate_ns(nsset, &nsp->cgroup_ns->ns);
464 if (ret)
465 goto out;
466 }
467 #endif
468
469 #ifdef CONFIG_NET_NS
470 if (flags & CLONE_NEWNET) {
471 ret = validate_ns(nsset, &nsp->net_ns->ns);
472 if (ret)
473 goto out;
474 }
475 #endif
476
477 #ifdef CONFIG_TIME_NS
478 if (flags & CLONE_NEWTIME) {
479 ret = validate_ns(nsset, &nsp->time_ns->ns);
480 if (ret)
481 goto out;
482 }
483 #endif
484
485 out:
486 if (pid_ns)
487 put_pid_ns(pid_ns);
488 if (nsp)
489 put_nsproxy(nsp);
490 put_user_ns(user_ns);
491
492 return ret;
493 }
494
495 /*
496 * This is the point of no return. There are just a few namespaces
497 * that do some actual work here and it's sufficiently minimal that
498 * a separate ns_common operation seems unnecessary for now.
499 * Unshare is doing the same thing. If we'll end up needing to do
500 * more in a given namespace or a helper here is ultimately not
501 * exported anymore a simple commit handler for each namespace
502 * should be added to ns_common.
503 */
commit_nsset(struct nsset * nsset)504 static void commit_nsset(struct nsset *nsset)
505 {
506 unsigned flags = nsset->flags;
507 struct task_struct *me = current;
508
509 #ifdef CONFIG_USER_NS
510 if (flags & CLONE_NEWUSER) {
511 /* transfer ownership */
512 commit_creds(nsset_cred(nsset));
513 nsset->cred = NULL;
514 }
515 #endif
516
517 /* We only need to commit if we have used a temporary fs_struct. */
518 if ((flags & CLONE_NEWNS) && (flags & ~CLONE_NEWNS)) {
519 set_fs_root(me->fs, &nsset->fs->root);
520 set_fs_pwd(me->fs, &nsset->fs->pwd);
521 }
522
523 #ifdef CONFIG_IPC_NS
524 if (flags & CLONE_NEWIPC)
525 exit_sem(me);
526 #endif
527
528 #ifdef CONFIG_TIME_NS
529 if (flags & CLONE_NEWTIME)
530 timens_commit(me, nsset->nsproxy->time_ns);
531 #endif
532
533 /* transfer ownership */
534 switch_task_namespaces(me, nsset->nsproxy);
535 nsset->nsproxy = NULL;
536 }
537
SYSCALL_DEFINE2(setns,int,fd,int,flags)538 SYSCALL_DEFINE2(setns, int, fd, int, flags)
539 {
540 struct file *file;
541 struct ns_common *ns = NULL;
542 struct nsset nsset = {};
543 int err = 0;
544
545 file = fget(fd);
546 if (!file)
547 return -EBADF;
548
549 if (proc_ns_file(file)) {
550 ns = get_proc_ns(file_inode(file));
551 if (flags && (ns->ops->type != flags))
552 err = -EINVAL;
553 flags = ns->ops->type;
554 } else if (!IS_ERR(pidfd_pid(file))) {
555 err = check_setns_flags(flags);
556 } else {
557 err = -EINVAL;
558 }
559 if (err)
560 goto out;
561
562 err = prepare_nsset(flags, &nsset);
563 if (err)
564 goto out;
565
566 if (proc_ns_file(file))
567 err = validate_ns(&nsset, ns);
568 else
569 err = validate_nsset(&nsset, file->private_data);
570 if (!err) {
571 commit_nsset(&nsset);
572 perf_event_namespaces(current);
573 }
574 put_nsset(&nsset);
575 out:
576 fput(file);
577 return err;
578 }
579
nsproxy_cache_init(void)580 int __init nsproxy_cache_init(void)
581 {
582 nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
583 return 0;
584 }
585