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