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