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