xref: /openbmc/linux/kernel/pid_namespace.c (revision 9e7c73c0)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
274bd59bbSPavel Emelyanov /*
374bd59bbSPavel Emelyanov  * Pid namespaces
474bd59bbSPavel Emelyanov  *
574bd59bbSPavel Emelyanov  * Authors:
674bd59bbSPavel Emelyanov  *    (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
774bd59bbSPavel Emelyanov  *    (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
874bd59bbSPavel Emelyanov  *     Many thanks to Oleg Nesterov for comments and help
974bd59bbSPavel Emelyanov  *
1074bd59bbSPavel Emelyanov  */
1174bd59bbSPavel Emelyanov 
1274bd59bbSPavel Emelyanov #include <linux/pid.h>
1374bd59bbSPavel Emelyanov #include <linux/pid_namespace.h>
1449f4d8b9SEric W. Biederman #include <linux/user_namespace.h>
1574bd59bbSPavel Emelyanov #include <linux/syscalls.h>
165b825c3aSIngo Molnar #include <linux/cred.h>
1774bd59bbSPavel Emelyanov #include <linux/err.h>
180b6b030fSPavel Emelyanov #include <linux/acct.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
200bb80f24SDavid Howells #include <linux/proc_ns.h>
21cf3f8921SDaniel Lezcano #include <linux/reboot.h>
22523a6a94SEric W. Biederman #include <linux/export.h>
2329930025SIngo Molnar #include <linux/sched/task.h>
24f361bf4aSIngo Molnar #include <linux/sched/signal.h>
2595846ecfSGargi Sharma #include <linux/idr.h>
26105ff533SJeff Xu #include "pid_sysctl.h"
2774bd59bbSPavel Emelyanov 
2874bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
2974bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
30dd206becSAlexey Dobriyan /* Write once array, filled from the beginning. */
31dd206becSAlexey Dobriyan static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL];
3274bd59bbSPavel Emelyanov 
3374bd59bbSPavel Emelyanov /*
3474bd59bbSPavel Emelyanov  * creates the kmem cache to allocate pids from.
35dd206becSAlexey Dobriyan  * @level: pid namespace level
3674bd59bbSPavel Emelyanov  */
3774bd59bbSPavel Emelyanov 
38dd206becSAlexey Dobriyan static struct kmem_cache *create_pid_cachep(unsigned int level)
3974bd59bbSPavel Emelyanov {
40dd206becSAlexey Dobriyan 	/* Level 0 is init_pid_ns.pid_cachep */
41dd206becSAlexey Dobriyan 	struct kmem_cache **pkc = &pid_cache[level - 1];
42dd206becSAlexey Dobriyan 	struct kmem_cache *kc;
43dd206becSAlexey Dobriyan 	char name[4 + 10 + 1];
44dd206becSAlexey Dobriyan 	unsigned int len;
4574bd59bbSPavel Emelyanov 
46dd206becSAlexey Dobriyan 	kc = READ_ONCE(*pkc);
47dd206becSAlexey Dobriyan 	if (kc)
48dd206becSAlexey Dobriyan 		return kc;
49dd206becSAlexey Dobriyan 
50dd206becSAlexey Dobriyan 	snprintf(name, sizeof(name), "pid_%u", level + 1);
51dd206becSAlexey Dobriyan 	len = sizeof(struct pid) + level * sizeof(struct upid);
5274bd59bbSPavel Emelyanov 	mutex_lock(&pid_caches_mutex);
53dd206becSAlexey Dobriyan 	/* Name collision forces to do allocation under mutex. */
54dd206becSAlexey Dobriyan 	if (!*pkc)
55fab827dbSVasily Averin 		*pkc = kmem_cache_create(name, len, 0,
56c06d7aafSHaowen Bai 					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
5774bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
58dd206becSAlexey Dobriyan 	/* current can fail, but someone else can succeed. */
59dd206becSAlexey Dobriyan 	return READ_ONCE(*pkc);
6074bd59bbSPavel Emelyanov }
6174bd59bbSPavel Emelyanov 
62f333c700SEric W. Biederman static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
63f333c700SEric W. Biederman {
64f333c700SEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
65f333c700SEric W. Biederman }
66f333c700SEric W. Biederman 
67f333c700SEric W. Biederman static void dec_pid_namespaces(struct ucounts *ucounts)
68f333c700SEric W. Biederman {
69f333c700SEric W. Biederman 	dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
70f333c700SEric W. Biederman }
71f333c700SEric W. Biederman 
7249f4d8b9SEric W. Biederman static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
7349f4d8b9SEric W. Biederman 	struct pid_namespace *parent_pid_ns)
7474bd59bbSPavel Emelyanov {
7574bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
76ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
77f333c700SEric W. Biederman 	struct ucounts *ucounts;
78f2302505SAndrew Vagin 	int err;
7974bd59bbSPavel Emelyanov 
80a2b42626SEric W. Biederman 	err = -EINVAL;
81a2b42626SEric W. Biederman 	if (!in_userns(parent_pid_ns->user_ns, user_ns))
82a2b42626SEric W. Biederman 		goto out;
83a2b42626SEric W. Biederman 
84df75e774SEric W. Biederman 	err = -ENOSPC;
85f333c700SEric W. Biederman 	if (level > MAX_PID_NS_LEVEL)
86f2302505SAndrew Vagin 		goto out;
87f333c700SEric W. Biederman 	ucounts = inc_pid_namespaces(user_ns);
88f333c700SEric W. Biederman 	if (!ucounts)
89f333c700SEric W. Biederman 		goto out;
90f2302505SAndrew Vagin 
91f2302505SAndrew Vagin 	err = -ENOMEM;
9284406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
9374bd59bbSPavel Emelyanov 	if (ns == NULL)
94f333c700SEric W. Biederman 		goto out_dec;
9574bd59bbSPavel Emelyanov 
9695846ecfSGargi Sharma 	idr_init(&ns->idr);
9774bd59bbSPavel Emelyanov 
98dd206becSAlexey Dobriyan 	ns->pid_cachep = create_pid_cachep(level);
9974bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
10095846ecfSGargi Sharma 		goto out_free_idr;
10174bd59bbSPavel Emelyanov 
1026344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
10398f842e6SEric W. Biederman 	if (err)
10495846ecfSGargi Sharma 		goto out_free_idr;
10533c42940SAl Viro 	ns->ns.ops = &pidns_operations;
10698f842e6SEric W. Biederman 
1078eb71d95SKirill Tkhai 	refcount_set(&ns->ns.count, 1);
10874bd59bbSPavel Emelyanov 	ns->level = level;
109ed469a63SAlexey Dobriyan 	ns->parent = get_pid_ns(parent_pid_ns);
11049f4d8b9SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
111f333c700SEric W. Biederman 	ns->ucounts = ucounts;
112e8cfbc24SGargi Sharma 	ns->pid_allocated = PIDNS_ADDING;
11374bd59bbSPavel Emelyanov 
114105ff533SJeff Xu 	initialize_memfd_noexec_scope(ns);
115105ff533SJeff Xu 
11674bd59bbSPavel Emelyanov 	return ns;
11774bd59bbSPavel Emelyanov 
11895846ecfSGargi Sharma out_free_idr:
11995846ecfSGargi Sharma 	idr_destroy(&ns->idr);
12074bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
121f333c700SEric W. Biederman out_dec:
122f333c700SEric W. Biederman 	dec_pid_namespaces(ucounts);
12374bd59bbSPavel Emelyanov out:
1244308eebbSEric W. Biederman 	return ERR_PTR(err);
12574bd59bbSPavel Emelyanov }
12674bd59bbSPavel Emelyanov 
1271adfcb03SAl Viro static void delayed_free_pidns(struct rcu_head *p)
1281adfcb03SAl Viro {
129add7c65cSAndrei Vagin 	struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
130add7c65cSAndrei Vagin 
131add7c65cSAndrei Vagin 	dec_pid_namespaces(ns->ucounts);
132add7c65cSAndrei Vagin 	put_user_ns(ns->user_ns);
133add7c65cSAndrei Vagin 
134add7c65cSAndrei Vagin 	kmem_cache_free(pid_ns_cachep, ns);
1351adfcb03SAl Viro }
1361adfcb03SAl Viro 
13774bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
13874bd59bbSPavel Emelyanov {
1396344c433SAl Viro 	ns_free_inum(&ns->ns);
14095846ecfSGargi Sharma 
14195846ecfSGargi Sharma 	idr_destroy(&ns->idr);
1421adfcb03SAl Viro 	call_rcu(&ns->rcu, delayed_free_pidns);
14374bd59bbSPavel Emelyanov }
14474bd59bbSPavel Emelyanov 
14549f4d8b9SEric W. Biederman struct pid_namespace *copy_pid_ns(unsigned long flags,
14649f4d8b9SEric W. Biederman 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
14774bd59bbSPavel Emelyanov {
14874bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
149dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
150225778d6SEric W. Biederman 	if (task_active_pid_ns(current) != old_ns)
151225778d6SEric W. Biederman 		return ERR_PTR(-EINVAL);
15249f4d8b9SEric W. Biederman 	return create_pid_namespace(user_ns, old_ns);
15374bd59bbSPavel Emelyanov }
15474bd59bbSPavel Emelyanov 
155bbc2e3efSCyrill Gorcunov void put_pid_ns(struct pid_namespace *ns)
156bbc2e3efSCyrill Gorcunov {
157bbc2e3efSCyrill Gorcunov 	struct pid_namespace *parent;
158bbc2e3efSCyrill Gorcunov 
159bbc2e3efSCyrill Gorcunov 	while (ns != &init_pid_ns) {
160bbc2e3efSCyrill Gorcunov 		parent = ns->parent;
1618eb71d95SKirill Tkhai 		if (!refcount_dec_and_test(&ns->ns.count))
162bbc2e3efSCyrill Gorcunov 			break;
1638eb71d95SKirill Tkhai 		destroy_pid_namespace(ns);
164bbc2e3efSCyrill Gorcunov 		ns = parent;
165bbc2e3efSCyrill Gorcunov 	}
166bbc2e3efSCyrill Gorcunov }
167bbc2e3efSCyrill Gorcunov EXPORT_SYMBOL_GPL(put_pid_ns);
16874bd59bbSPavel Emelyanov 
16974bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
17074bd59bbSPavel Emelyanov {
17174bd59bbSPavel Emelyanov 	int nr;
17274bd59bbSPavel Emelyanov 	int rc;
17300c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
174751c644bSEric W. Biederman 	int init_pids = thread_group_leader(me) ? 1 : 2;
17595846ecfSGargi Sharma 	struct pid *pid;
17600c10bc1SEric W. Biederman 
177c876ad76SEric W. Biederman 	/* Don't allow any more processes into the pid namespace */
178c876ad76SEric W. Biederman 	disable_pid_allocation(pid_ns);
179c876ad76SEric W. Biederman 
180a53b8315SOleg Nesterov 	/*
181a53b8315SOleg Nesterov 	 * Ignore SIGCHLD causing any terminated children to autoreap.
182a53b8315SOleg Nesterov 	 * This speeds up the namespace shutdown, plus see the comment
183a53b8315SOleg Nesterov 	 * below.
184a53b8315SOleg Nesterov 	 */
18500c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
18600c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
18700c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
18874bd59bbSPavel Emelyanov 
18974bd59bbSPavel Emelyanov 	/*
19074bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
19174bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
19274bd59bbSPavel Emelyanov 	 * to exit.
19374bd59bbSPavel Emelyanov 	 *
19474bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
19574bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
19674bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
19774bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
19874bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
19974bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
20074bd59bbSPavel Emelyanov 	 *
20174bd59bbSPavel Emelyanov 	 */
202e4da026fSSukadev Bhattiprolu 	rcu_read_lock();
20395846ecfSGargi Sharma 	read_lock(&tasklist_lock);
20495846ecfSGargi Sharma 	nr = 2;
20595846ecfSGargi Sharma 	idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
20695846ecfSGargi Sharma 		task = pid_task(pid, PIDTYPE_PID);
207a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
20882058d66SEric W. Biederman 			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_MAX);
20974bd59bbSPavel Emelyanov 	}
21074bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
21195846ecfSGargi Sharma 	rcu_read_unlock();
21274bd59bbSPavel Emelyanov 
213a53b8315SOleg Nesterov 	/*
214a53b8315SOleg Nesterov 	 * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
215d300b610SDominik Brodowski 	 * kernel_wait4() will also block until our children traced from the
216a53b8315SOleg Nesterov 	 * parent namespace are detached and become EXIT_DEAD.
217a53b8315SOleg Nesterov 	 */
21874bd59bbSPavel Emelyanov 	do {
21974bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
220d300b610SDominik Brodowski 		rc = kernel_wait4(-1, NULL, __WALL, NULL);
22174bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
22274bd59bbSPavel Emelyanov 
2236347e900SEric W. Biederman 	/*
224af9fe6d6SEric W. Biederman 	 * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
225af9fe6d6SEric W. Biederman 	 * process whose parents processes are outside of the pid
226af9fe6d6SEric W. Biederman 	 * namespace.  Such processes are created with setns()+fork().
227a53b8315SOleg Nesterov 	 *
228af9fe6d6SEric W. Biederman 	 * If those EXIT_ZOMBIE processes are not reaped by their
229af9fe6d6SEric W. Biederman 	 * parents before their parents exit, they will be reparented
230af9fe6d6SEric W. Biederman 	 * to pid_ns->child_reaper.  Thus pidns->child_reaper needs to
231af9fe6d6SEric W. Biederman 	 * stay valid until they all go away.
232a53b8315SOleg Nesterov 	 *
2337b7b8a2cSRandy Dunlap 	 * The code relies on the pid_ns->child_reaper ignoring
234af9fe6d6SEric W. Biederman 	 * SIGCHILD to cause those EXIT_ZOMBIE processes to be
235af9fe6d6SEric W. Biederman 	 * autoreaped if reparented.
236af9fe6d6SEric W. Biederman 	 *
237af9fe6d6SEric W. Biederman 	 * Semantically it is also desirable to wait for EXIT_ZOMBIE
238af9fe6d6SEric W. Biederman 	 * processes before allowing the child_reaper to be reaped, as
239af9fe6d6SEric W. Biederman 	 * that gives the invariant that when the init process of a
240af9fe6d6SEric W. Biederman 	 * pid namespace is reaped all of the processes in the pid
241af9fe6d6SEric W. Biederman 	 * namespace are gone.
242af9fe6d6SEric W. Biederman 	 *
243af9fe6d6SEric W. Biederman 	 * Once all of the other tasks are gone from the pid_namespace
244af9fe6d6SEric W. Biederman 	 * free_pid() will awaken this task.
2456347e900SEric W. Biederman 	 */
2466347e900SEric W. Biederman 	for (;;) {
247b9a985dbSEric W. Biederman 		set_current_state(TASK_INTERRUPTIBLE);
248e8cfbc24SGargi Sharma 		if (pid_ns->pid_allocated == init_pids)
2496347e900SEric W. Biederman 			break;
25028319d6dSFrederic Weisbecker 		/*
25128319d6dSFrederic Weisbecker 		 * Release tasks_rcu_exit_srcu to avoid following deadlock:
25228319d6dSFrederic Weisbecker 		 *
25328319d6dSFrederic Weisbecker 		 * 1) TASK A unshare(CLONE_NEWPID)
25428319d6dSFrederic Weisbecker 		 * 2) TASK A fork() twice -> TASK B (child reaper for new ns)
25528319d6dSFrederic Weisbecker 		 *    and TASK C
25628319d6dSFrederic Weisbecker 		 * 3) TASK B exits, kills TASK C, waits for TASK A to reap it
25728319d6dSFrederic Weisbecker 		 * 4) TASK A calls synchronize_rcu_tasks()
25828319d6dSFrederic Weisbecker 		 *                   -> synchronize_srcu(tasks_rcu_exit_srcu)
25928319d6dSFrederic Weisbecker 		 * 5) *DEADLOCK*
26028319d6dSFrederic Weisbecker 		 *
26128319d6dSFrederic Weisbecker 		 * It is considered safe to release tasks_rcu_exit_srcu here
26228319d6dSFrederic Weisbecker 		 * because we assume the current task can not be concurrently
26328319d6dSFrederic Weisbecker 		 * reaped at this point.
26428319d6dSFrederic Weisbecker 		 */
26528319d6dSFrederic Weisbecker 		exit_tasks_rcu_stop();
2666347e900SEric W. Biederman 		schedule();
26728319d6dSFrederic Weisbecker 		exit_tasks_rcu_start();
2686347e900SEric W. Biederman 	}
269af4b8a83SEric W. Biederman 	__set_current_state(TASK_RUNNING);
2706347e900SEric W. Biederman 
271cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
272cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
273cf3f8921SDaniel Lezcano 
2740b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
27574bd59bbSPavel Emelyanov 	return;
27674bd59bbSPavel Emelyanov }
27774bd59bbSPavel Emelyanov 
27898ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
279b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write,
28032927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
281b8f566b0SPavel Emelyanov {
28249f4d8b9SEric W. Biederman 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
283b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
28495846ecfSGargi Sharma 	int ret, next;
285b8f566b0SPavel Emelyanov 
286b9a3db92SAdrian Reber 	if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
287b8f566b0SPavel Emelyanov 		return -EPERM;
288b8f566b0SPavel Emelyanov 
289b8f566b0SPavel Emelyanov 	/*
290b8f566b0SPavel Emelyanov 	 * Writing directly to ns' last_pid field is OK, since this field
291b8f566b0SPavel Emelyanov 	 * is volatile in a living namespace anyway and a code writing to
292b8f566b0SPavel Emelyanov 	 * it should synchronize its usage with external means.
293b8f566b0SPavel Emelyanov 	 */
294b8f566b0SPavel Emelyanov 
29595846ecfSGargi Sharma 	next = idr_get_cursor(&pid_ns->idr) - 1;
29695846ecfSGargi Sharma 
29795846ecfSGargi Sharma 	tmp.data = &next;
29895846ecfSGargi Sharma 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
29995846ecfSGargi Sharma 	if (!ret && write)
30095846ecfSGargi Sharma 		idr_set_cursor(&pid_ns->idr, next + 1);
30195846ecfSGargi Sharma 
30295846ecfSGargi Sharma 	return ret;
303b8f566b0SPavel Emelyanov }
304b8f566b0SPavel Emelyanov 
305579035dcSAndrew Vagin extern int pid_max;
306b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = {
307b8f566b0SPavel Emelyanov 	{
308b8f566b0SPavel Emelyanov 		.procname = "ns_last_pid",
309b8f566b0SPavel Emelyanov 		.maxlen = sizeof(int),
310b8f566b0SPavel Emelyanov 		.mode = 0666, /* permissions are checked in the handler */
311b8f566b0SPavel Emelyanov 		.proc_handler = pid_ns_ctl_handler,
312eec4844fSMatteo Croce 		.extra1 = SYSCTL_ZERO,
313579035dcSAndrew Vagin 		.extra2 = &pid_max,
314b8f566b0SPavel Emelyanov 	},
315b8f566b0SPavel Emelyanov 	{ }
316b8f566b0SPavel Emelyanov };
31798ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
318b8f566b0SPavel Emelyanov 
319cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
320cf3f8921SDaniel Lezcano {
321cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
322cf3f8921SDaniel Lezcano 		return 0;
323cf3f8921SDaniel Lezcano 
324cf3f8921SDaniel Lezcano 	switch (cmd) {
325cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
326cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
327cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
328cf3f8921SDaniel Lezcano 		break;
329cf3f8921SDaniel Lezcano 
330cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
331cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
332cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
333cf3f8921SDaniel Lezcano 		break;
334cf3f8921SDaniel Lezcano 	default:
335cf3f8921SDaniel Lezcano 		return -EINVAL;
336cf3f8921SDaniel Lezcano 	}
337cf3f8921SDaniel Lezcano 
338cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
339f9070dc9SEric W. Biederman 	send_sig(SIGKILL, pid_ns->child_reaper, 1);
340cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
341cf3f8921SDaniel Lezcano 
342cf3f8921SDaniel Lezcano 	do_exit(0);
343cf3f8921SDaniel Lezcano 
344cf3f8921SDaniel Lezcano 	/* Not reached */
345cf3f8921SDaniel Lezcano 	return 0;
346cf3f8921SDaniel Lezcano }
347cf3f8921SDaniel Lezcano 
3483c041184SAl Viro static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
3493c041184SAl Viro {
3503c041184SAl Viro 	return container_of(ns, struct pid_namespace, ns);
3513c041184SAl Viro }
3523c041184SAl Viro 
35364964528SAl Viro static struct ns_common *pidns_get(struct task_struct *task)
35457e8391dSEric W. Biederman {
35557e8391dSEric W. Biederman 	struct pid_namespace *ns;
35657e8391dSEric W. Biederman 
35757e8391dSEric W. Biederman 	rcu_read_lock();
358d2308225SOleg Nesterov 	ns = task_active_pid_ns(task);
359d2308225SOleg Nesterov 	if (ns)
360d2308225SOleg Nesterov 		get_pid_ns(ns);
36157e8391dSEric W. Biederman 	rcu_read_unlock();
36257e8391dSEric W. Biederman 
3633c041184SAl Viro 	return ns ? &ns->ns : NULL;
36457e8391dSEric W. Biederman }
36557e8391dSEric W. Biederman 
366eaa0d190SKirill Tkhai static struct ns_common *pidns_for_children_get(struct task_struct *task)
367eaa0d190SKirill Tkhai {
368eaa0d190SKirill Tkhai 	struct pid_namespace *ns = NULL;
369eaa0d190SKirill Tkhai 
370eaa0d190SKirill Tkhai 	task_lock(task);
371eaa0d190SKirill Tkhai 	if (task->nsproxy) {
372eaa0d190SKirill Tkhai 		ns = task->nsproxy->pid_ns_for_children;
373eaa0d190SKirill Tkhai 		get_pid_ns(ns);
374eaa0d190SKirill Tkhai 	}
375eaa0d190SKirill Tkhai 	task_unlock(task);
376eaa0d190SKirill Tkhai 
377eaa0d190SKirill Tkhai 	if (ns) {
378eaa0d190SKirill Tkhai 		read_lock(&tasklist_lock);
379eaa0d190SKirill Tkhai 		if (!ns->child_reaper) {
380eaa0d190SKirill Tkhai 			put_pid_ns(ns);
381eaa0d190SKirill Tkhai 			ns = NULL;
382eaa0d190SKirill Tkhai 		}
383eaa0d190SKirill Tkhai 		read_unlock(&tasklist_lock);
384eaa0d190SKirill Tkhai 	}
385eaa0d190SKirill Tkhai 
386eaa0d190SKirill Tkhai 	return ns ? &ns->ns : NULL;
387eaa0d190SKirill Tkhai }
388eaa0d190SKirill Tkhai 
38964964528SAl Viro static void pidns_put(struct ns_common *ns)
39057e8391dSEric W. Biederman {
3913c041184SAl Viro 	put_pid_ns(to_pid_ns(ns));
39257e8391dSEric W. Biederman }
39357e8391dSEric W. Biederman 
394f2a8d52eSChristian Brauner static int pidns_install(struct nsset *nsset, struct ns_common *ns)
39557e8391dSEric W. Biederman {
396f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
39757e8391dSEric W. Biederman 	struct pid_namespace *active = task_active_pid_ns(current);
3983c041184SAl Viro 	struct pid_namespace *ancestor, *new = to_pid_ns(ns);
39957e8391dSEric W. Biederman 
4005e4a0847SEric W. Biederman 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
401f2a8d52eSChristian Brauner 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
40257e8391dSEric W. Biederman 		return -EPERM;
40357e8391dSEric W. Biederman 
40457e8391dSEric W. Biederman 	/*
40557e8391dSEric W. Biederman 	 * Only allow entering the current active pid namespace
40657e8391dSEric W. Biederman 	 * or a child of the current active pid namespace.
40757e8391dSEric W. Biederman 	 *
40857e8391dSEric W. Biederman 	 * This is required for fork to return a usable pid value and
40957e8391dSEric W. Biederman 	 * this maintains the property that processes and their
41057e8391dSEric W. Biederman 	 * children can not escape their current pid namespace.
41157e8391dSEric W. Biederman 	 */
41257e8391dSEric W. Biederman 	if (new->level < active->level)
41357e8391dSEric W. Biederman 		return -EINVAL;
41457e8391dSEric W. Biederman 
41557e8391dSEric W. Biederman 	ancestor = new;
41657e8391dSEric W. Biederman 	while (ancestor->level > active->level)
41757e8391dSEric W. Biederman 		ancestor = ancestor->parent;
41857e8391dSEric W. Biederman 	if (ancestor != active)
41957e8391dSEric W. Biederman 		return -EINVAL;
42057e8391dSEric W. Biederman 
421c2b1df2eSAndy Lutomirski 	put_pid_ns(nsproxy->pid_ns_for_children);
422c2b1df2eSAndy Lutomirski 	nsproxy->pid_ns_for_children = get_pid_ns(new);
42357e8391dSEric W. Biederman 	return 0;
42457e8391dSEric W. Biederman }
42557e8391dSEric W. Biederman 
426a7306ed8SAndrey Vagin static struct ns_common *pidns_get_parent(struct ns_common *ns)
427a7306ed8SAndrey Vagin {
428a7306ed8SAndrey Vagin 	struct pid_namespace *active = task_active_pid_ns(current);
429a7306ed8SAndrey Vagin 	struct pid_namespace *pid_ns, *p;
430a7306ed8SAndrey Vagin 
431a7306ed8SAndrey Vagin 	/* See if the parent is in the current namespace */
432a7306ed8SAndrey Vagin 	pid_ns = p = to_pid_ns(ns)->parent;
433a7306ed8SAndrey Vagin 	for (;;) {
434a7306ed8SAndrey Vagin 		if (!p)
435a7306ed8SAndrey Vagin 			return ERR_PTR(-EPERM);
436a7306ed8SAndrey Vagin 		if (p == active)
437a7306ed8SAndrey Vagin 			break;
438a7306ed8SAndrey Vagin 		p = p->parent;
439a7306ed8SAndrey Vagin 	}
440a7306ed8SAndrey Vagin 
441a7306ed8SAndrey Vagin 	return &get_pid_ns(pid_ns)->ns;
442a7306ed8SAndrey Vagin }
443a7306ed8SAndrey Vagin 
444bcac25a5SAndrey Vagin static struct user_namespace *pidns_owner(struct ns_common *ns)
445bcac25a5SAndrey Vagin {
446bcac25a5SAndrey Vagin 	return to_pid_ns(ns)->user_ns;
447bcac25a5SAndrey Vagin }
448bcac25a5SAndrey Vagin 
44957e8391dSEric W. Biederman const struct proc_ns_operations pidns_operations = {
45057e8391dSEric W. Biederman 	.name		= "pid",
45157e8391dSEric W. Biederman 	.type		= CLONE_NEWPID,
45257e8391dSEric W. Biederman 	.get		= pidns_get,
45357e8391dSEric W. Biederman 	.put		= pidns_put,
45457e8391dSEric W. Biederman 	.install	= pidns_install,
455bcac25a5SAndrey Vagin 	.owner		= pidns_owner,
456a7306ed8SAndrey Vagin 	.get_parent	= pidns_get_parent,
45757e8391dSEric W. Biederman };
45857e8391dSEric W. Biederman 
459eaa0d190SKirill Tkhai const struct proc_ns_operations pidns_for_children_operations = {
460eaa0d190SKirill Tkhai 	.name		= "pid_for_children",
461eaa0d190SKirill Tkhai 	.real_ns_name	= "pid",
462eaa0d190SKirill Tkhai 	.type		= CLONE_NEWPID,
463eaa0d190SKirill Tkhai 	.get		= pidns_for_children_get,
464eaa0d190SKirill Tkhai 	.put		= pidns_put,
465eaa0d190SKirill Tkhai 	.install	= pidns_install,
466eaa0d190SKirill Tkhai 	.owner		= pidns_owner,
467eaa0d190SKirill Tkhai 	.get_parent	= pidns_get_parent,
468eaa0d190SKirill Tkhai };
469eaa0d190SKirill Tkhai 
47074bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
47174bd59bbSPavel Emelyanov {
47230acd0bdSVasily Averin 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
47398ed57eeSCyrill Gorcunov 
47498ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
475*9e7c73c0SLuis Chamberlain 	register_sysctl_init("kernel", pid_ns_ctl_table);
47698ed57eeSCyrill Gorcunov #endif
477105ff533SJeff Xu 
478105ff533SJeff Xu 	register_pid_ns_sysctl_table_vm();
47974bd59bbSPavel Emelyanov 	return 0;
48074bd59bbSPavel Emelyanov }
48174bd59bbSPavel Emelyanov 
48274bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
483