xref: /linux-6.15/kernel/pid_namespace.c (revision d385c8bc)
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 <[email protected]>, OpenVZ, SWsoft Inc.
774bd59bbSPavel Emelyanov  *    (C) 2007 Sukadev Bhattiprolu <[email protected]>, 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>
266dfeff09SMatthew Wilcox (Oracle) #include <uapi/linux/wait.h>
27105ff533SJeff Xu #include "pid_sysctl.h"
2874bd59bbSPavel Emelyanov 
2974bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
3074bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
31dd206becSAlexey Dobriyan /* Write once array, filled from the beginning. */
32dd206becSAlexey Dobriyan static struct kmem_cache *pid_cache[MAX_PID_NS_LEVEL];
3374bd59bbSPavel Emelyanov 
3474bd59bbSPavel Emelyanov /*
3574bd59bbSPavel Emelyanov  * creates the kmem cache to allocate pids from.
36dd206becSAlexey Dobriyan  * @level: pid namespace level
3774bd59bbSPavel Emelyanov  */
3874bd59bbSPavel Emelyanov 
create_pid_cachep(unsigned int level)39dd206becSAlexey Dobriyan static struct kmem_cache *create_pid_cachep(unsigned int level)
4074bd59bbSPavel Emelyanov {
41dd206becSAlexey Dobriyan 	/* Level 0 is init_pid_ns.pid_cachep */
42dd206becSAlexey Dobriyan 	struct kmem_cache **pkc = &pid_cache[level - 1];
43dd206becSAlexey Dobriyan 	struct kmem_cache *kc;
44dd206becSAlexey Dobriyan 	char name[4 + 10 + 1];
45dd206becSAlexey Dobriyan 	unsigned int len;
4674bd59bbSPavel Emelyanov 
47dd206becSAlexey Dobriyan 	kc = READ_ONCE(*pkc);
48dd206becSAlexey Dobriyan 	if (kc)
49dd206becSAlexey Dobriyan 		return kc;
50dd206becSAlexey Dobriyan 
51dd206becSAlexey Dobriyan 	snprintf(name, sizeof(name), "pid_%u", level + 1);
52dd546618SChristian Brauner 	len = struct_size_t(struct pid, numbers, level + 1);
5374bd59bbSPavel Emelyanov 	mutex_lock(&pid_caches_mutex);
54dd206becSAlexey Dobriyan 	/* Name collision forces to do allocation under mutex. */
55dd206becSAlexey Dobriyan 	if (!*pkc)
56fab827dbSVasily Averin 		*pkc = kmem_cache_create(name, len, 0,
57c06d7aafSHaowen Bai 					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
5874bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
59dd206becSAlexey Dobriyan 	/* current can fail, but someone else can succeed. */
60dd206becSAlexey Dobriyan 	return READ_ONCE(*pkc);
6174bd59bbSPavel Emelyanov }
6274bd59bbSPavel Emelyanov 
inc_pid_namespaces(struct user_namespace * ns)63f333c700SEric W. Biederman static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
64f333c700SEric W. Biederman {
65f333c700SEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
66f333c700SEric W. Biederman }
67f333c700SEric W. Biederman 
dec_pid_namespaces(struct ucounts * ucounts)68f333c700SEric W. Biederman static void dec_pid_namespaces(struct ucounts *ucounts)
69f333c700SEric W. Biederman {
70f333c700SEric W. Biederman 	dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
71f333c700SEric W. Biederman }
72f333c700SEric W. Biederman 
737863dcc7SChristian Brauner static void destroy_pid_namespace_work(struct work_struct *work);
747863dcc7SChristian Brauner 
create_pid_namespace(struct user_namespace * user_ns,struct pid_namespace * parent_pid_ns)7549f4d8b9SEric W. Biederman static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
7649f4d8b9SEric W. Biederman 	struct pid_namespace *parent_pid_ns)
7774bd59bbSPavel Emelyanov {
7874bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
79ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
80f333c700SEric W. Biederman 	struct ucounts *ucounts;
81f2302505SAndrew Vagin 	int err;
8274bd59bbSPavel Emelyanov 
83a2b42626SEric W. Biederman 	err = -EINVAL;
84a2b42626SEric W. Biederman 	if (!in_userns(parent_pid_ns->user_ns, user_ns))
85a2b42626SEric W. Biederman 		goto out;
86a2b42626SEric W. Biederman 
87df75e774SEric W. Biederman 	err = -ENOSPC;
88f333c700SEric W. Biederman 	if (level > MAX_PID_NS_LEVEL)
89f2302505SAndrew Vagin 		goto out;
90f333c700SEric W. Biederman 	ucounts = inc_pid_namespaces(user_ns);
91f333c700SEric W. Biederman 	if (!ucounts)
92f333c700SEric W. Biederman 		goto out;
93f2302505SAndrew Vagin 
94f2302505SAndrew Vagin 	err = -ENOMEM;
9584406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
9674bd59bbSPavel Emelyanov 	if (ns == NULL)
97f333c700SEric W. Biederman 		goto out_dec;
9874bd59bbSPavel Emelyanov 
9995846ecfSGargi Sharma 	idr_init(&ns->idr);
10074bd59bbSPavel Emelyanov 
101dd206becSAlexey Dobriyan 	ns->pid_cachep = create_pid_cachep(level);
10274bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
10395846ecfSGargi Sharma 		goto out_free_idr;
10474bd59bbSPavel Emelyanov 
1056344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
10698f842e6SEric W. Biederman 	if (err)
10795846ecfSGargi Sharma 		goto out_free_idr;
10833c42940SAl Viro 	ns->ns.ops = &pidns_operations;
10998f842e6SEric W. Biederman 
110*d385c8bcSMichal Koutný 	ns->pid_max = PID_MAX_LIMIT;
1117863dcc7SChristian Brauner 	err = register_pidns_sysctls(ns);
1127863dcc7SChristian Brauner 	if (err)
1137863dcc7SChristian Brauner 		goto out_free_inum;
1147863dcc7SChristian Brauner 
1158eb71d95SKirill Tkhai 	refcount_set(&ns->ns.count, 1);
11674bd59bbSPavel Emelyanov 	ns->level = level;
117ed469a63SAlexey Dobriyan 	ns->parent = get_pid_ns(parent_pid_ns);
11849f4d8b9SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
119f333c700SEric W. Biederman 	ns->ucounts = ucounts;
120e8cfbc24SGargi Sharma 	ns->pid_allocated = PIDNS_ADDING;
1217863dcc7SChristian Brauner 	INIT_WORK(&ns->work, destroy_pid_namespace_work);
1227863dcc7SChristian Brauner 
1239876cfe8SAleksa Sarai #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
1249876cfe8SAleksa Sarai 	ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
1259876cfe8SAleksa Sarai #endif
1267863dcc7SChristian Brauner 
12774bd59bbSPavel Emelyanov 	return ns;
12874bd59bbSPavel Emelyanov 
1297863dcc7SChristian Brauner out_free_inum:
1307863dcc7SChristian Brauner 	ns_free_inum(&ns->ns);
13195846ecfSGargi Sharma out_free_idr:
13295846ecfSGargi Sharma 	idr_destroy(&ns->idr);
13374bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
134f333c700SEric W. Biederman out_dec:
135f333c700SEric W. Biederman 	dec_pid_namespaces(ucounts);
13674bd59bbSPavel Emelyanov out:
1374308eebbSEric W. Biederman 	return ERR_PTR(err);
13874bd59bbSPavel Emelyanov }
13974bd59bbSPavel Emelyanov 
delayed_free_pidns(struct rcu_head * p)1401adfcb03SAl Viro static void delayed_free_pidns(struct rcu_head *p)
1411adfcb03SAl Viro {
142add7c65cSAndrei Vagin 	struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
143add7c65cSAndrei Vagin 
144add7c65cSAndrei Vagin 	dec_pid_namespaces(ns->ucounts);
145add7c65cSAndrei Vagin 	put_user_ns(ns->user_ns);
146add7c65cSAndrei Vagin 
147add7c65cSAndrei Vagin 	kmem_cache_free(pid_ns_cachep, ns);
1481adfcb03SAl Viro }
1491adfcb03SAl Viro 
destroy_pid_namespace(struct pid_namespace * ns)15074bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
15174bd59bbSPavel Emelyanov {
1527863dcc7SChristian Brauner 	unregister_pidns_sysctls(ns);
1537863dcc7SChristian Brauner 
1546344c433SAl Viro 	ns_free_inum(&ns->ns);
15595846ecfSGargi Sharma 
15695846ecfSGargi Sharma 	idr_destroy(&ns->idr);
1571adfcb03SAl Viro 	call_rcu(&ns->rcu, delayed_free_pidns);
15874bd59bbSPavel Emelyanov }
15974bd59bbSPavel Emelyanov 
destroy_pid_namespace_work(struct work_struct * work)1607863dcc7SChristian Brauner static void destroy_pid_namespace_work(struct work_struct *work)
1617863dcc7SChristian Brauner {
1627863dcc7SChristian Brauner 	struct pid_namespace *ns =
1637863dcc7SChristian Brauner 		container_of(work, struct pid_namespace, work);
1647863dcc7SChristian Brauner 
1657863dcc7SChristian Brauner 	do {
1667863dcc7SChristian Brauner 		struct pid_namespace *parent;
1677863dcc7SChristian Brauner 
1687863dcc7SChristian Brauner 		parent = ns->parent;
1697863dcc7SChristian Brauner 		destroy_pid_namespace(ns);
1707863dcc7SChristian Brauner 		ns = parent;
1717863dcc7SChristian Brauner 	} while (ns != &init_pid_ns && refcount_dec_and_test(&ns->ns.count));
1727863dcc7SChristian Brauner }
1737863dcc7SChristian Brauner 
copy_pid_ns(unsigned long flags,struct user_namespace * user_ns,struct pid_namespace * old_ns)17449f4d8b9SEric W. Biederman struct pid_namespace *copy_pid_ns(unsigned long flags,
17549f4d8b9SEric W. Biederman 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
17674bd59bbSPavel Emelyanov {
17774bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
178dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
179225778d6SEric W. Biederman 	if (task_active_pid_ns(current) != old_ns)
180225778d6SEric W. Biederman 		return ERR_PTR(-EINVAL);
18149f4d8b9SEric W. Biederman 	return create_pid_namespace(user_ns, old_ns);
18274bd59bbSPavel Emelyanov }
18374bd59bbSPavel Emelyanov 
put_pid_ns(struct pid_namespace * ns)184bbc2e3efSCyrill Gorcunov void put_pid_ns(struct pid_namespace *ns)
185bbc2e3efSCyrill Gorcunov {
1867863dcc7SChristian Brauner 	if (ns && ns != &init_pid_ns && refcount_dec_and_test(&ns->ns.count))
1877863dcc7SChristian Brauner 		schedule_work(&ns->work);
188bbc2e3efSCyrill Gorcunov }
189bbc2e3efSCyrill Gorcunov EXPORT_SYMBOL_GPL(put_pid_ns);
19074bd59bbSPavel Emelyanov 
zap_pid_ns_processes(struct pid_namespace * pid_ns)19174bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
19274bd59bbSPavel Emelyanov {
19374bd59bbSPavel Emelyanov 	int nr;
19474bd59bbSPavel Emelyanov 	int rc;
19500c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
196751c644bSEric W. Biederman 	int init_pids = thread_group_leader(me) ? 1 : 2;
19795846ecfSGargi Sharma 	struct pid *pid;
19800c10bc1SEric W. Biederman 
199c876ad76SEric W. Biederman 	/* Don't allow any more processes into the pid namespace */
200c876ad76SEric W. Biederman 	disable_pid_allocation(pid_ns);
201c876ad76SEric W. Biederman 
202a53b8315SOleg Nesterov 	/*
203a53b8315SOleg Nesterov 	 * Ignore SIGCHLD causing any terminated children to autoreap.
204a53b8315SOleg Nesterov 	 * This speeds up the namespace shutdown, plus see the comment
205a53b8315SOleg Nesterov 	 * below.
206a53b8315SOleg Nesterov 	 */
20700c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
20800c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
20900c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
21074bd59bbSPavel Emelyanov 
21174bd59bbSPavel Emelyanov 	/*
21274bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
21374bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
21474bd59bbSPavel Emelyanov 	 * to exit.
21574bd59bbSPavel Emelyanov 	 *
21674bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
21774bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
21874bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
21974bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
22074bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
22174bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
22274bd59bbSPavel Emelyanov 	 *
22374bd59bbSPavel Emelyanov 	 */
224e4da026fSSukadev Bhattiprolu 	rcu_read_lock();
22595846ecfSGargi Sharma 	read_lock(&tasklist_lock);
22695846ecfSGargi Sharma 	nr = 2;
22795846ecfSGargi Sharma 	idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
22895846ecfSGargi Sharma 		task = pid_task(pid, PIDTYPE_PID);
229a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
23082058d66SEric W. Biederman 			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_MAX);
23174bd59bbSPavel Emelyanov 	}
23274bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
23395846ecfSGargi Sharma 	rcu_read_unlock();
23474bd59bbSPavel Emelyanov 
235a53b8315SOleg Nesterov 	/*
236a53b8315SOleg Nesterov 	 * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
237d300b610SDominik Brodowski 	 * kernel_wait4() will also block until our children traced from the
238a53b8315SOleg Nesterov 	 * parent namespace are detached and become EXIT_DEAD.
239a53b8315SOleg Nesterov 	 */
24074bd59bbSPavel Emelyanov 	do {
24174bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
2427fea700eSOleg Nesterov 		clear_thread_flag(TIF_NOTIFY_SIGNAL);
243d300b610SDominik Brodowski 		rc = kernel_wait4(-1, NULL, __WALL, NULL);
24474bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
24574bd59bbSPavel Emelyanov 
2466347e900SEric W. Biederman 	/*
247af9fe6d6SEric W. Biederman 	 * kernel_wait4() misses EXIT_DEAD children, and EXIT_ZOMBIE
248af9fe6d6SEric W. Biederman 	 * process whose parents processes are outside of the pid
249af9fe6d6SEric W. Biederman 	 * namespace.  Such processes are created with setns()+fork().
250a53b8315SOleg Nesterov 	 *
251af9fe6d6SEric W. Biederman 	 * If those EXIT_ZOMBIE processes are not reaped by their
252af9fe6d6SEric W. Biederman 	 * parents before their parents exit, they will be reparented
253af9fe6d6SEric W. Biederman 	 * to pid_ns->child_reaper.  Thus pidns->child_reaper needs to
254af9fe6d6SEric W. Biederman 	 * stay valid until they all go away.
255a53b8315SOleg Nesterov 	 *
2567b7b8a2cSRandy Dunlap 	 * The code relies on the pid_ns->child_reaper ignoring
257af9fe6d6SEric W. Biederman 	 * SIGCHILD to cause those EXIT_ZOMBIE processes to be
258af9fe6d6SEric W. Biederman 	 * autoreaped if reparented.
259af9fe6d6SEric W. Biederman 	 *
260af9fe6d6SEric W. Biederman 	 * Semantically it is also desirable to wait for EXIT_ZOMBIE
261af9fe6d6SEric W. Biederman 	 * processes before allowing the child_reaper to be reaped, as
262af9fe6d6SEric W. Biederman 	 * that gives the invariant that when the init process of a
263af9fe6d6SEric W. Biederman 	 * pid namespace is reaped all of the processes in the pid
264af9fe6d6SEric W. Biederman 	 * namespace are gone.
265af9fe6d6SEric W. Biederman 	 *
266af9fe6d6SEric W. Biederman 	 * Once all of the other tasks are gone from the pid_namespace
267af9fe6d6SEric W. Biederman 	 * free_pid() will awaken this task.
2686347e900SEric W. Biederman 	 */
2696347e900SEric W. Biederman 	for (;;) {
270b9a985dbSEric W. Biederman 		set_current_state(TASK_INTERRUPTIBLE);
271e8cfbc24SGargi Sharma 		if (pid_ns->pid_allocated == init_pids)
2726347e900SEric W. Biederman 			break;
2736347e900SEric W. Biederman 		schedule();
2746347e900SEric W. Biederman 	}
275af4b8a83SEric W. Biederman 	__set_current_state(TASK_RUNNING);
2766347e900SEric W. Biederman 
277cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
278cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
279cf3f8921SDaniel Lezcano 
2800b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
28174bd59bbSPavel Emelyanov 	return;
28274bd59bbSPavel Emelyanov }
28374bd59bbSPavel Emelyanov 
28498ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
pid_ns_ctl_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)28578eb4ea2SJoel Granados static int pid_ns_ctl_handler(const struct ctl_table *table, int write,
28632927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
287b8f566b0SPavel Emelyanov {
28849f4d8b9SEric W. Biederman 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
289b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
29095846ecfSGargi Sharma 	int ret, next;
291b8f566b0SPavel Emelyanov 
292b9a3db92SAdrian Reber 	if (write && !checkpoint_restore_ns_capable(pid_ns->user_ns))
293b8f566b0SPavel Emelyanov 		return -EPERM;
294b8f566b0SPavel Emelyanov 
29595846ecfSGargi Sharma 	next = idr_get_cursor(&pid_ns->idr) - 1;
29695846ecfSGargi Sharma 
29795846ecfSGargi Sharma 	tmp.data = &next;
2987863dcc7SChristian Brauner 	tmp.extra2 = &pid_ns->pid_max;
29995846ecfSGargi Sharma 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
30095846ecfSGargi Sharma 	if (!ret && write)
30195846ecfSGargi Sharma 		idr_set_cursor(&pid_ns->idr, next + 1);
30295846ecfSGargi Sharma 
30395846ecfSGargi Sharma 	return ret;
304b8f566b0SPavel Emelyanov }
305b8f566b0SPavel Emelyanov 
3061751f872SJoel Granados static const 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,
3137863dcc7SChristian Brauner 		.extra2 = &init_pid_ns.pid_max,
314b8f566b0SPavel Emelyanov 	},
315b8f566b0SPavel Emelyanov };
31698ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
317b8f566b0SPavel Emelyanov 
reboot_pid_ns(struct pid_namespace * pid_ns,int cmd)318cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
319cf3f8921SDaniel Lezcano {
320cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
321cf3f8921SDaniel Lezcano 		return 0;
322cf3f8921SDaniel Lezcano 
323cf3f8921SDaniel Lezcano 	switch (cmd) {
324cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
325cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
326cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
327cf3f8921SDaniel Lezcano 		break;
328cf3f8921SDaniel Lezcano 
329cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
330cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
331cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
332cf3f8921SDaniel Lezcano 		break;
333cf3f8921SDaniel Lezcano 	default:
334cf3f8921SDaniel Lezcano 		return -EINVAL;
335cf3f8921SDaniel Lezcano 	}
336cf3f8921SDaniel Lezcano 
337cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
338f9070dc9SEric W. Biederman 	send_sig(SIGKILL, pid_ns->child_reaper, 1);
339cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
340cf3f8921SDaniel Lezcano 
341cf3f8921SDaniel Lezcano 	do_exit(0);
342cf3f8921SDaniel Lezcano 
343cf3f8921SDaniel Lezcano 	/* Not reached */
344cf3f8921SDaniel Lezcano 	return 0;
345cf3f8921SDaniel Lezcano }
346cf3f8921SDaniel Lezcano 
to_pid_ns(struct ns_common * ns)3473c041184SAl Viro static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
3483c041184SAl Viro {
3493c041184SAl Viro 	return container_of(ns, struct pid_namespace, ns);
3503c041184SAl Viro }
3513c041184SAl Viro 
pidns_get(struct task_struct * task)35264964528SAl Viro static struct ns_common *pidns_get(struct task_struct *task)
35357e8391dSEric W. Biederman {
35457e8391dSEric W. Biederman 	struct pid_namespace *ns;
35557e8391dSEric W. Biederman 
35657e8391dSEric W. Biederman 	rcu_read_lock();
357d2308225SOleg Nesterov 	ns = task_active_pid_ns(task);
358d2308225SOleg Nesterov 	if (ns)
359d2308225SOleg Nesterov 		get_pid_ns(ns);
36057e8391dSEric W. Biederman 	rcu_read_unlock();
36157e8391dSEric W. Biederman 
3623c041184SAl Viro 	return ns ? &ns->ns : NULL;
36357e8391dSEric W. Biederman }
36457e8391dSEric W. Biederman 
pidns_for_children_get(struct task_struct * task)365eaa0d190SKirill Tkhai static struct ns_common *pidns_for_children_get(struct task_struct *task)
366eaa0d190SKirill Tkhai {
367eaa0d190SKirill Tkhai 	struct pid_namespace *ns = NULL;
368eaa0d190SKirill Tkhai 
369eaa0d190SKirill Tkhai 	task_lock(task);
370eaa0d190SKirill Tkhai 	if (task->nsproxy) {
371eaa0d190SKirill Tkhai 		ns = task->nsproxy->pid_ns_for_children;
372eaa0d190SKirill Tkhai 		get_pid_ns(ns);
373eaa0d190SKirill Tkhai 	}
374eaa0d190SKirill Tkhai 	task_unlock(task);
375eaa0d190SKirill Tkhai 
376eaa0d190SKirill Tkhai 	if (ns) {
377eaa0d190SKirill Tkhai 		read_lock(&tasklist_lock);
378eaa0d190SKirill Tkhai 		if (!ns->child_reaper) {
379eaa0d190SKirill Tkhai 			put_pid_ns(ns);
380eaa0d190SKirill Tkhai 			ns = NULL;
381eaa0d190SKirill Tkhai 		}
382eaa0d190SKirill Tkhai 		read_unlock(&tasklist_lock);
383eaa0d190SKirill Tkhai 	}
384eaa0d190SKirill Tkhai 
385eaa0d190SKirill Tkhai 	return ns ? &ns->ns : NULL;
386eaa0d190SKirill Tkhai }
387eaa0d190SKirill Tkhai 
pidns_put(struct ns_common * ns)38864964528SAl Viro static void pidns_put(struct ns_common *ns)
38957e8391dSEric W. Biederman {
3903c041184SAl Viro 	put_pid_ns(to_pid_ns(ns));
39157e8391dSEric W. Biederman }
39257e8391dSEric W. Biederman 
pidns_install(struct nsset * nsset,struct ns_common * ns)393f2a8d52eSChristian Brauner static int pidns_install(struct nsset *nsset, struct ns_common *ns)
39457e8391dSEric W. Biederman {
395f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
39657e8391dSEric W. Biederman 	struct pid_namespace *active = task_active_pid_ns(current);
3973c041184SAl Viro 	struct pid_namespace *ancestor, *new = to_pid_ns(ns);
39857e8391dSEric W. Biederman 
3995e4a0847SEric W. Biederman 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
400f2a8d52eSChristian Brauner 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
40157e8391dSEric W. Biederman 		return -EPERM;
40257e8391dSEric W. Biederman 
40357e8391dSEric W. Biederman 	/*
40457e8391dSEric W. Biederman 	 * Only allow entering the current active pid namespace
40557e8391dSEric W. Biederman 	 * or a child of the current active pid namespace.
40657e8391dSEric W. Biederman 	 *
40757e8391dSEric W. Biederman 	 * This is required for fork to return a usable pid value and
40857e8391dSEric W. Biederman 	 * this maintains the property that processes and their
40957e8391dSEric W. Biederman 	 * children can not escape their current pid namespace.
41057e8391dSEric W. Biederman 	 */
41157e8391dSEric W. Biederman 	if (new->level < active->level)
41257e8391dSEric W. Biederman 		return -EINVAL;
41357e8391dSEric W. Biederman 
41457e8391dSEric W. Biederman 	ancestor = new;
41557e8391dSEric W. Biederman 	while (ancestor->level > active->level)
41657e8391dSEric W. Biederman 		ancestor = ancestor->parent;
41757e8391dSEric W. Biederman 	if (ancestor != active)
41857e8391dSEric W. Biederman 		return -EINVAL;
41957e8391dSEric W. Biederman 
420c2b1df2eSAndy Lutomirski 	put_pid_ns(nsproxy->pid_ns_for_children);
421c2b1df2eSAndy Lutomirski 	nsproxy->pid_ns_for_children = get_pid_ns(new);
42257e8391dSEric W. Biederman 	return 0;
42357e8391dSEric W. Biederman }
42457e8391dSEric W. Biederman 
pidns_get_parent(struct ns_common * ns)425a7306ed8SAndrey Vagin static struct ns_common *pidns_get_parent(struct ns_common *ns)
426a7306ed8SAndrey Vagin {
427a7306ed8SAndrey Vagin 	struct pid_namespace *active = task_active_pid_ns(current);
428a7306ed8SAndrey Vagin 	struct pid_namespace *pid_ns, *p;
429a7306ed8SAndrey Vagin 
430a7306ed8SAndrey Vagin 	/* See if the parent is in the current namespace */
431a7306ed8SAndrey Vagin 	pid_ns = p = to_pid_ns(ns)->parent;
432a7306ed8SAndrey Vagin 	for (;;) {
433a7306ed8SAndrey Vagin 		if (!p)
434a7306ed8SAndrey Vagin 			return ERR_PTR(-EPERM);
435a7306ed8SAndrey Vagin 		if (p == active)
436a7306ed8SAndrey Vagin 			break;
437a7306ed8SAndrey Vagin 		p = p->parent;
438a7306ed8SAndrey Vagin 	}
439a7306ed8SAndrey Vagin 
440a7306ed8SAndrey Vagin 	return &get_pid_ns(pid_ns)->ns;
441a7306ed8SAndrey Vagin }
442a7306ed8SAndrey Vagin 
pidns_owner(struct ns_common * ns)443bcac25a5SAndrey Vagin static struct user_namespace *pidns_owner(struct ns_common *ns)
444bcac25a5SAndrey Vagin {
445bcac25a5SAndrey Vagin 	return to_pid_ns(ns)->user_ns;
446bcac25a5SAndrey Vagin }
447bcac25a5SAndrey Vagin 
44857e8391dSEric W. Biederman const struct proc_ns_operations pidns_operations = {
44957e8391dSEric W. Biederman 	.name		= "pid",
45057e8391dSEric W. Biederman 	.type		= CLONE_NEWPID,
45157e8391dSEric W. Biederman 	.get		= pidns_get,
45257e8391dSEric W. Biederman 	.put		= pidns_put,
45357e8391dSEric W. Biederman 	.install	= pidns_install,
454bcac25a5SAndrey Vagin 	.owner		= pidns_owner,
455a7306ed8SAndrey Vagin 	.get_parent	= pidns_get_parent,
45657e8391dSEric W. Biederman };
45757e8391dSEric W. Biederman 
458eaa0d190SKirill Tkhai const struct proc_ns_operations pidns_for_children_operations = {
459eaa0d190SKirill Tkhai 	.name		= "pid_for_children",
460eaa0d190SKirill Tkhai 	.real_ns_name	= "pid",
461eaa0d190SKirill Tkhai 	.type		= CLONE_NEWPID,
462eaa0d190SKirill Tkhai 	.get		= pidns_for_children_get,
463eaa0d190SKirill Tkhai 	.put		= pidns_put,
464eaa0d190SKirill Tkhai 	.install	= pidns_install,
465eaa0d190SKirill Tkhai 	.owner		= pidns_owner,
466eaa0d190SKirill Tkhai 	.get_parent	= pidns_get_parent,
467eaa0d190SKirill Tkhai };
468eaa0d190SKirill Tkhai 
pid_namespaces_init(void)46974bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
47074bd59bbSPavel Emelyanov {
47130acd0bdSVasily Averin 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
47298ed57eeSCyrill Gorcunov 
47398ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
4749e7c73c0SLuis Chamberlain 	register_sysctl_init("kernel", pid_ns_ctl_table);
47598ed57eeSCyrill Gorcunov #endif
476105ff533SJeff Xu 
477105ff533SJeff Xu 	register_pid_ns_sysctl_table_vm();
47874bd59bbSPavel Emelyanov 	return 0;
47974bd59bbSPavel Emelyanov }
48074bd59bbSPavel Emelyanov 
48174bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
482