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