xref: /linux/kernel/pid_namespace.c (revision 018cf9bf786d501a27963dca6777f169f978e2a5)
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_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 	/* Task work must not busy-loop the reaper, see signal_pending(). */
242 	guard(no_notify_signal)();
243 	do {
244 		clear_thread_flag(TIF_SIGPENDING);
245 		rc = kernel_wait4(-1, NULL, __WALL, NULL);
246 	} while (rc != -ECHILD);
247 
248 	/*
249 	 * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
250 	 * process whose parents processes are outside of the pid
251 	 * namespace.  Such processes are created with setns()+fork().
252 	 *
253 	 * If those EXIT_ZOMBIE processes are not reaped by their
254 	 * parents before their parents exit, they will be reparented
255 	 * to pid_ns->child_reaper.  Thus pidns->child_reaper needs to
256 	 * stay valid until they all go away.
257 	 *
258 	 * The code relies on the pid_ns->child_reaper ignoring
259 	 * SIGCHILD to cause those EXIT_ZOMBIE processes to be
260 	 * autoreaped if reparented.
261 	 *
262 	 * Semantically it is also desirable to wait for EXIT_ZOMBIE
263 	 * processes before allowing the child_reaper to be reaped, as
264 	 * that gives the invariant that when the init process of a
265 	 * pid namespace is reaped all of the processes in the pid
266 	 * namespace are gone.
267 	 *
268 	 * Once all of the other tasks are gone from the pid_namespace
269 	 * free_pid() will awaken this task.
270 	 */
271 	for (;;) {
272 		set_current_state(TASK_IDLE | TASK_FREEZABLE);
273 		if (pid_ns->pid_allocated == init_pids)
274 			break;
275 		schedule();
276 	}
277 	__set_current_state(TASK_RUNNING);
278 
279 	if (pid_ns->reboot)
280 		current->signal->group_exit_code = pid_ns->reboot;
281 
282 	acct_exit_ns(pid_ns);
283 	return;
284 }
285 
286 #ifdef CONFIG_CHECKPOINT_RESTORE
pid_ns_ctl_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)287 static int pid_ns_ctl_handler(const struct ctl_table *table, int write,
288 		void *buffer, size_t *lenp, loff_t *ppos)
289 {
290 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
291 	struct ctl_table tmp = *table;
292 	int ret, next;
293 
294 	if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
295 		return -EPERM;
296 
297 	next = idr_get_cursor(&pid_ns->idr) - 1;
298 
299 	tmp.data = &next;
300 	tmp.extra2 = &pid_ns->pid_max;
301 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
302 	if (!ret && write)
303 		idr_set_cursor(&pid_ns->idr, next + 1);
304 
305 	return ret;
306 }
307 
308 static const struct ctl_table pid_ns_ctl_table[] = {
309 	{
310 		.procname = "ns_last_pid",
311 		.maxlen = sizeof(int),
312 		.mode = 0666, /* permissions are checked in the handler */
313 		.proc_handler = pid_ns_ctl_handler,
314 		.extra1 = SYSCTL_ZERO,
315 		.extra2 = &init_pid_ns.pid_max,
316 	},
317 };
318 #endif	/* CONFIG_CHECKPOINT_RESTORE */
319 
reboot_pid_ns(struct pid_namespace * pid_ns,int cmd)320 int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
321 {
322 	if (pid_ns == &init_pid_ns)
323 		return 0;
324 
325 	switch (cmd) {
326 	case LINUX_REBOOT_CMD_RESTART2:
327 	case LINUX_REBOOT_CMD_RESTART:
328 		pid_ns->reboot = SIGHUP;
329 		break;
330 
331 	case LINUX_REBOOT_CMD_POWER_OFF:
332 	case LINUX_REBOOT_CMD_HALT:
333 		pid_ns->reboot = SIGINT;
334 		break;
335 	default:
336 		return -EINVAL;
337 	}
338 
339 	read_lock(&tasklist_lock);
340 	send_sig(SIGKILL, pid_ns->child_reaper, 1);
341 	read_unlock(&tasklist_lock);
342 
343 	do_exit(0);
344 
345 	/* Not reached */
346 	return 0;
347 }
348 
pidns_get(struct task_struct * task)349 static struct ns_common *pidns_get(struct task_struct *task)
350 {
351 	struct pid_namespace *ns;
352 
353 	rcu_read_lock();
354 	ns = task_active_pid_ns(task);
355 	if (ns)
356 		get_pid_ns(ns);
357 	rcu_read_unlock();
358 
359 	return ns ? &ns->ns : NULL;
360 }
361 
pidns_for_children_get(struct task_struct * task)362 static struct ns_common *pidns_for_children_get(struct task_struct *task)
363 {
364 	struct pid_namespace *ns = NULL;
365 
366 	task_lock(task);
367 	if (task->nsproxy) {
368 		ns = task->nsproxy->pid_ns_for_children;
369 		get_pid_ns(ns);
370 	}
371 	task_unlock(task);
372 
373 	return ns ? &ns->ns : NULL;
374 }
375 
pidns_put(struct ns_common * ns)376 static void pidns_put(struct ns_common *ns)
377 {
378 	put_pid_ns(to_pid_ns(ns));
379 }
380 
pidns_is_ancestor(struct pid_namespace * child,struct pid_namespace * ancestor)381 bool pidns_is_ancestor(struct pid_namespace *child,
382 		       struct pid_namespace *ancestor)
383 {
384 	struct pid_namespace *ns;
385 
386 	if (child->level < ancestor->level)
387 		return false;
388 	for (ns = child; ns->level > ancestor->level; ns = ns->parent)
389 		;
390 	return ns == ancestor;
391 }
392 
pidns_install(struct nsset * nsset,struct ns_common * ns)393 static int pidns_install(struct nsset *nsset, struct ns_common *ns)
394 {
395 	struct nsproxy *nsproxy = nsset->nsproxy;
396 	struct pid_namespace *active = task_active_pid_ns(current);
397 	struct pid_namespace *new = to_pid_ns(ns);
398 
399 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
400 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
401 		return -EPERM;
402 
403 	/*
404 	 * Only allow entering the current active pid namespace
405 	 * or a child of the current active pid namespace.
406 	 *
407 	 * This is required for fork to return a usable pid value and
408 	 * this maintains the property that processes and their
409 	 * children can not escape their current pid namespace.
410 	 */
411 	if (!pidns_is_ancestor(new, active))
412 		return -EINVAL;
413 
414 	put_pid_ns(nsproxy->pid_ns_for_children);
415 	nsproxy->pid_ns_for_children = get_pid_ns(new);
416 	return 0;
417 }
418 
pidns_get_parent(struct ns_common * ns)419 static struct ns_common *pidns_get_parent(struct ns_common *ns)
420 {
421 	struct pid_namespace *active = task_active_pid_ns(current);
422 	struct pid_namespace *pid_ns, *p;
423 
424 	/* See if the parent is in the current namespace */
425 	pid_ns = p = to_pid_ns(ns)->parent;
426 	for (;;) {
427 		if (!p)
428 			return ERR_PTR(-EPERM);
429 		if (p == active)
430 			break;
431 		p = p->parent;
432 	}
433 
434 	return &get_pid_ns(pid_ns)->ns;
435 }
436 
pidns_owner(struct ns_common * ns)437 static struct user_namespace *pidns_owner(struct ns_common *ns)
438 {
439 	return to_pid_ns(ns)->user_ns;
440 }
441 
442 const struct proc_ns_operations pidns_operations = {
443 	.name		= "pid",
444 	.get		= pidns_get,
445 	.put		= pidns_put,
446 	.install	= pidns_install,
447 	.owner		= pidns_owner,
448 	.get_parent	= pidns_get_parent,
449 };
450 
451 const struct proc_ns_operations pidns_for_children_operations = {
452 	.name		= "pid_for_children",
453 	.real_ns_name	= "pid",
454 	.get		= pidns_for_children_get,
455 	.put		= pidns_put,
456 	.install	= pidns_install,
457 	.owner		= pidns_owner,
458 	.get_parent	= pidns_get_parent,
459 };
460 
pid_namespaces_init(void)461 static __init int pid_namespaces_init(void)
462 {
463 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
464 
465 #ifdef CONFIG_CHECKPOINT_RESTORE
466 	register_sysctl_init("kernel", pid_ns_ctl_table);
467 #endif
468 
469 	register_pid_ns_sysctl_table_vm();
470 	ns_tree_add(&init_pid_ns);
471 	return 0;
472 }
473 
474 __initcall(pid_namespaces_init);
475