xref: /linux/kernel/pid_namespace.c (revision 18b19abc3709b109676ffd1f48dcd332c2e477d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Pid namespaces
4  *
5  * Authors:
6  *    (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
7  *    (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
8  *     Many thanks to Oleg Nesterov for comments and help
9  *
10  */
11 
12 #include <linux/pid.h>
13 #include <linux/pid_namespace.h>
14 #include <linux/user_namespace.h>
15 #include <linux/syscalls.h>
16 #include <linux/cred.h>
17 #include <linux/err.h>
18 #include <linux/acct.h>
19 #include <linux/slab.h>
20 #include <linux/proc_ns.h>
21 #include <linux/reboot.h>
22 #include <linux/export.h>
23 #include <linux/sched/task.h>
24 #include <linux/sched/signal.h>
25 #include <linux/idr.h>
26 #include <linux/nstree.h>
27 #include <uapi/linux/wait.h>
28 #include "pid_sysctl.h"
29 
30 static DEFINE_MUTEX(pid_caches_mutex);
31 static struct kmem_cache *pid_ns_cachep;
32 /* Write once array, filled from the beginning. */
33 static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL];
34 
35 /*
36  * creates the kmem cache to allocate pids from.
37  * @level: pid namespace level
38  */
39 
create_pid_cachep(unsigned int level)40 static struct kmem_cache *create_pid_cachep(unsigned int level)
41 {
42 	/* Level 0 is init_pid_ns.pid_cachep */
43 	struct kmem_cache **pkc = &pid_cache[level - 1];
44 	struct kmem_cache *kc;
45 	char name[4 + 10 + 1];
46 	unsigned int len;
47 
48 	kc = READ_ONCE(*pkc);
49 	if (kc)
50 		return kc;
51 
52 	snprintf(name, sizeof(name), "pid_%u", level + 1);
53 	len = struct_size_t(struct pid, numbers, level + 1);
54 	mutex_lock(&pid_caches_mutex);
55 	/* Name collision forces to do allocation under mutex. */
56 	if (!*pkc)
57 		*pkc = kmem_cache_create(name, len, 0,
58 					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
59 	mutex_unlock(&pid_caches_mutex);
60 	/* current can fail, but someone else can succeed. */
61 	return READ_ONCE(*pkc);
62 }
63 
inc_pid_namespaces(struct user_namespace * ns)64 static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
65 {
66 	return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
67 }
68 
dec_pid_namespaces(struct ucounts * ucounts)69 static void dec_pid_namespaces(struct ucounts *ucounts)
70 {
71 	dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
72 }
73 
74 static void destroy_pid_namespace_work(struct work_struct *work);
75 
create_pid_namespace(struct user_namespace * user_ns,struct pid_namespace * parent_pid_ns)76 static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
77 	struct pid_namespace *parent_pid_ns)
78 {
79 	struct pid_namespace *ns;
80 	unsigned int level = parent_pid_ns->level + 1;
81 	struct ucounts *ucounts;
82 	int err;
83 
84 	err = -EINVAL;
85 	if (!in_userns(parent_pid_ns->user_ns, user_ns))
86 		goto out;
87 
88 	err = -ENOSPC;
89 	if (level > MAX_PID_NS_LEVEL)
90 		goto out;
91 	ucounts = inc_pid_namespaces(user_ns);
92 	if (!ucounts)
93 		goto out;
94 
95 	err = -ENOMEM;
96 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
97 	if (ns == NULL)
98 		goto out_dec;
99 
100 	idr_init(&ns->idr);
101 
102 	ns->pid_cachep = create_pid_cachep(level);
103 	if (ns->pid_cachep == NULL)
104 		goto out_free_idr;
105 
106 	err = ns_common_init(ns);
107 	if (err)
108 		goto out_free_idr;
109 
110 	ns->pid_max = PID_MAX_LIMIT;
111 	err = register_pidns_sysctls(ns);
112 	if (err)
113 		goto out_free_inum;
114 
115 	ns->level = level;
116 	ns->parent = get_pid_ns(parent_pid_ns);
117 	ns->user_ns = get_user_ns(user_ns);
118 	ns->ucounts = ucounts;
119 	ns->pid_allocated = PIDNS_ADDING;
120 	INIT_WORK(&ns->work, destroy_pid_namespace_work);
121 
122 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
123 	ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
124 #endif
125 
126 	ns_tree_add(ns);
127 	return ns;
128 
129 out_free_inum:
130 	ns_common_free(ns);
131 out_free_idr:
132 	idr_destroy(&ns->idr);
133 	kmem_cache_free(pid_ns_cachep, ns);
134 out_dec:
135 	dec_pid_namespaces(ucounts);
136 out:
137 	return ERR_PTR(err);
138 }
139 
delayed_free_pidns(struct rcu_head * p)140 static void delayed_free_pidns(struct rcu_head *p)
141 {
142 	struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
143 
144 	dec_pid_namespaces(ns->ucounts);
145 	put_user_ns(ns->user_ns);
146 
147 	kmem_cache_free(pid_ns_cachep, ns);
148 }
149 
destroy_pid_namespace(struct pid_namespace * ns)150 static void destroy_pid_namespace(struct pid_namespace *ns)
151 {
152 	ns_tree_remove(ns);
153 	unregister_pidns_sysctls(ns);
154 
155 	ns_common_free(ns);
156 
157 	idr_destroy(&ns->idr);
158 	call_rcu(&ns->rcu, delayed_free_pidns);
159 }
160 
destroy_pid_namespace_work(struct work_struct * work)161 static void destroy_pid_namespace_work(struct work_struct *work)
162 {
163 	struct pid_namespace *ns =
164 		container_of(work, struct pid_namespace, work);
165 
166 	do {
167 		struct pid_namespace *parent;
168 
169 		parent = ns->parent;
170 		destroy_pid_namespace(ns);
171 		ns = parent;
172 	} while (ns != &init_pid_ns && ns_ref_put(ns));
173 }
174 
copy_pid_ns(u64 flags,struct user_namespace * user_ns,struct pid_namespace * old_ns)175 struct pid_namespace *copy_pid_ns(u64 flags,
176 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
177 {
178 	if (!(flags & CLONE_NEWPID))
179 		return get_pid_ns(old_ns);
180 	if (task_active_pid_ns(current) != old_ns)
181 		return ERR_PTR(-EINVAL);
182 	return create_pid_namespace(user_ns, old_ns);
183 }
184 
put_pid_ns(struct pid_namespace * ns)185 void put_pid_ns(struct pid_namespace *ns)
186 {
187 	if (ns && ns != &init_pid_ns && ns_ref_put(ns))
188 		schedule_work(&ns->work);
189 }
190 EXPORT_SYMBOL_GPL(put_pid_ns);
191 
zap_pid_ns_processes(struct pid_namespace * pid_ns)192 void zap_pid_ns_processes(struct pid_namespace *pid_ns)
193 {
194 	int nr;
195 	int rc;
196 	struct task_struct *task, *me = current;
197 	int init_pids = thread_group_leader(me) ? 1 : 2;
198 	struct pid *pid;
199 
200 	/* Don't allow any more processes into the pid namespace */
201 	disable_pid_allocation(pid_ns);
202 
203 	/*
204 	 * Ignore SIGCHLD causing any terminated children to autoreap.
205 	 * This speeds up the namespace shutdown, plus see the comment
206 	 * below.
207 	 */
208 	spin_lock_irq(&me->sighand->siglock);
209 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
210 	spin_unlock_irq(&me->sighand->siglock);
211 
212 	/*
213 	 * The last thread in the cgroup-init thread group is terminating.
214 	 * Find remaining pid_ts in the namespace, signal and wait for them
215 	 * to exit.
216 	 *
217 	 * Note:  This signals each threads in the namespace - even those that
218 	 * 	  belong to the same thread group, To avoid this, we would have
219 	 * 	  to walk the entire tasklist looking a processes in this
220 	 * 	  namespace, but that could be unnecessarily expensive if the
221 	 * 	  pid namespace has just a few processes. Or we need to
222 	 * 	  maintain a tasklist for each pid namespace.
223 	 *
224 	 */
225 	rcu_read_lock();
226 	read_lock(&tasklist_lock);
227 	nr = 2;
228 	idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
229 		task = pid_task(pid, PIDTYPE_PID);
230 		if (task && !__fatal_signal_pending(task))
231 			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_MAX);
232 	}
233 	read_unlock(&tasklist_lock);
234 	rcu_read_unlock();
235 
236 	/*
237 	 * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
238 	 * kernel_wait4() will also block until our children traced from the
239 	 * parent namespace are detached and become EXIT_DEAD.
240 	 */
241 	do {
242 		clear_thread_flag(TIF_SIGPENDING);
243 		clear_thread_flag(TIF_NOTIFY_SIGNAL);
244 		rc = kernel_wait4(-1, NULL, __WALL, NULL);
245 	} while (rc != -ECHILD);
246 
247 	/*
248 	 * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
249 	 * process whose parents processes are outside of the pid
250 	 * namespace.  Such processes are created with setns()+fork().
251 	 *
252 	 * If those EXIT_ZOMBIE processes are not reaped by their
253 	 * parents before their parents exit, they will be reparented
254 	 * to pid_ns->child_reaper.  Thus pidns->child_reaper needs to
255 	 * stay valid until they all go away.
256 	 *
257 	 * The code relies on the pid_ns->child_reaper ignoring
258 	 * SIGCHILD to cause those EXIT_ZOMBIE processes to be
259 	 * autoreaped if reparented.
260 	 *
261 	 * Semantically it is also desirable to wait for EXIT_ZOMBIE
262 	 * processes before allowing the child_reaper to be reaped, as
263 	 * that gives the invariant that when the init process of a
264 	 * pid namespace is reaped all of the processes in the pid
265 	 * namespace are gone.
266 	 *
267 	 * Once all of the other tasks are gone from the pid_namespace
268 	 * free_pid() will awaken this task.
269 	 */
270 	for (;;) {
271 		set_current_state(TASK_INTERRUPTIBLE);
272 		if (pid_ns->pid_allocated == init_pids)
273 			break;
274 		schedule();
275 	}
276 	__set_current_state(TASK_RUNNING);
277 
278 	if (pid_ns->reboot)
279 		current->signal->group_exit_code = pid_ns->reboot;
280 
281 	acct_exit_ns(pid_ns);
282 	return;
283 }
284 
285 #ifdef CONFIG_CHECKPOINT_RESTORE
pid_ns_ctl_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)286 static int pid_ns_ctl_handler(const struct ctl_table *table, int write,
287 		void *buffer, size_t *lenp, loff_t *ppos)
288 {
289 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
290 	struct ctl_table tmp = *table;
291 	int ret, next;
292 
293 	if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
294 		return -EPERM;
295 
296 	next = idr_get_cursor(&pid_ns->idr) - 1;
297 
298 	tmp.data = &next;
299 	tmp.extra2 = &pid_ns->pid_max;
300 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
301 	if (!ret && write)
302 		idr_set_cursor(&pid_ns->idr, next + 1);
303 
304 	return ret;
305 }
306 
307 static const struct ctl_table pid_ns_ctl_table[] = {
308 	{
309 		.procname = "ns_last_pid",
310 		.maxlen = sizeof(int),
311 		.mode = 0666, /* permissions are checked in the handler */
312 		.proc_handler = pid_ns_ctl_handler,
313 		.extra1 = SYSCTL_ZERO,
314 		.extra2 = &init_pid_ns.pid_max,
315 	},
316 };
317 #endif	/* CONFIG_CHECKPOINT_RESTORE */
318 
reboot_pid_ns(struct pid_namespace * pid_ns,int cmd)319 int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
320 {
321 	if (pid_ns == &init_pid_ns)
322 		return 0;
323 
324 	switch (cmd) {
325 	case LINUX_REBOOT_CMD_RESTART2:
326 	case LINUX_REBOOT_CMD_RESTART:
327 		pid_ns->reboot = SIGHUP;
328 		break;
329 
330 	case LINUX_REBOOT_CMD_POWER_OFF:
331 	case LINUX_REBOOT_CMD_HALT:
332 		pid_ns->reboot = SIGINT;
333 		break;
334 	default:
335 		return -EINVAL;
336 	}
337 
338 	read_lock(&tasklist_lock);
339 	send_sig(SIGKILL, pid_ns->child_reaper, 1);
340 	read_unlock(&tasklist_lock);
341 
342 	do_exit(0);
343 
344 	/* Not reached */
345 	return 0;
346 }
347 
pidns_get(struct task_struct * task)348 static struct ns_common *pidns_get(struct task_struct *task)
349 {
350 	struct pid_namespace *ns;
351 
352 	rcu_read_lock();
353 	ns = task_active_pid_ns(task);
354 	if (ns)
355 		get_pid_ns(ns);
356 	rcu_read_unlock();
357 
358 	return ns ? &ns->ns : NULL;
359 }
360 
pidns_for_children_get(struct task_struct * task)361 static struct ns_common *pidns_for_children_get(struct task_struct *task)
362 {
363 	struct pid_namespace *ns = NULL;
364 
365 	task_lock(task);
366 	if (task->nsproxy) {
367 		ns = task->nsproxy->pid_ns_for_children;
368 		get_pid_ns(ns);
369 	}
370 	task_unlock(task);
371 
372 	if (ns) {
373 		read_lock(&tasklist_lock);
374 		if (!ns->child_reaper) {
375 			put_pid_ns(ns);
376 			ns = NULL;
377 		}
378 		read_unlock(&tasklist_lock);
379 	}
380 
381 	return ns ? &ns->ns : NULL;
382 }
383 
pidns_put(struct ns_common * ns)384 static void pidns_put(struct ns_common *ns)
385 {
386 	put_pid_ns(to_pid_ns(ns));
387 }
388 
pidns_is_ancestor(struct pid_namespace * child,struct pid_namespace * ancestor)389 bool pidns_is_ancestor(struct pid_namespace *child,
390 		       struct pid_namespace *ancestor)
391 {
392 	struct pid_namespace *ns;
393 
394 	if (child->level < ancestor->level)
395 		return false;
396 	for (ns = child; ns->level > ancestor->level; ns = ns->parent)
397 		;
398 	return ns == ancestor;
399 }
400 
pidns_install(struct nsset * nsset,struct ns_common * ns)401 static int pidns_install(struct nsset *nsset, struct ns_common *ns)
402 {
403 	struct nsproxy *nsproxy = nsset->nsproxy;
404 	struct pid_namespace *active = task_active_pid_ns(current);
405 	struct pid_namespace *new = to_pid_ns(ns);
406 
407 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
408 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
409 		return -EPERM;
410 
411 	/*
412 	 * Only allow entering the current active pid namespace
413 	 * or a child of the current active pid namespace.
414 	 *
415 	 * This is required for fork to return a usable pid value and
416 	 * this maintains the property that processes and their
417 	 * children can not escape their current pid namespace.
418 	 */
419 	if (!pidns_is_ancestor(new, active))
420 		return -EINVAL;
421 
422 	put_pid_ns(nsproxy->pid_ns_for_children);
423 	nsproxy->pid_ns_for_children = get_pid_ns(new);
424 	return 0;
425 }
426 
pidns_get_parent(struct ns_common * ns)427 static struct ns_common *pidns_get_parent(struct ns_common *ns)
428 {
429 	struct pid_namespace *active = task_active_pid_ns(current);
430 	struct pid_namespace *pid_ns, *p;
431 
432 	/* See if the parent is in the current namespace */
433 	pid_ns = p = to_pid_ns(ns)->parent;
434 	for (;;) {
435 		if (!p)
436 			return ERR_PTR(-EPERM);
437 		if (p == active)
438 			break;
439 		p = p->parent;
440 	}
441 
442 	return &get_pid_ns(pid_ns)->ns;
443 }
444 
pidns_owner(struct ns_common * ns)445 static struct user_namespace *pidns_owner(struct ns_common *ns)
446 {
447 	return to_pid_ns(ns)->user_ns;
448 }
449 
450 const struct proc_ns_operations pidns_operations = {
451 	.name		= "pid",
452 	.get		= pidns_get,
453 	.put		= pidns_put,
454 	.install	= pidns_install,
455 	.owner		= pidns_owner,
456 	.get_parent	= pidns_get_parent,
457 };
458 
459 const struct proc_ns_operations pidns_for_children_operations = {
460 	.name		= "pid_for_children",
461 	.real_ns_name	= "pid",
462 	.get		= pidns_for_children_get,
463 	.put		= pidns_put,
464 	.install	= pidns_install,
465 	.owner		= pidns_owner,
466 	.get_parent	= pidns_get_parent,
467 };
468 
pid_namespaces_init(void)469 static __init int pid_namespaces_init(void)
470 {
471 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
472 
473 #ifdef CONFIG_CHECKPOINT_RESTORE
474 	register_sysctl_init("kernel", pid_ns_ctl_table);
475 #endif
476 
477 	register_pid_ns_sysctl_table_vm();
478 	ns_tree_add(&init_pid_ns);
479 	return 0;
480 }
481 
482 __initcall(pid_namespaces_init);
483