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