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