xref: /openbmc/linux/kernel/pid_namespace.c (revision 3c0411846118a578de3a979faf2da3ab5fb81179)
174bd59bbSPavel Emelyanov /*
274bd59bbSPavel Emelyanov  * Pid namespaces
374bd59bbSPavel Emelyanov  *
474bd59bbSPavel Emelyanov  * Authors:
574bd59bbSPavel Emelyanov  *    (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
674bd59bbSPavel Emelyanov  *    (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
774bd59bbSPavel Emelyanov  *     Many thanks to Oleg Nesterov for comments and help
874bd59bbSPavel Emelyanov  *
974bd59bbSPavel Emelyanov  */
1074bd59bbSPavel Emelyanov 
1174bd59bbSPavel Emelyanov #include <linux/pid.h>
1274bd59bbSPavel Emelyanov #include <linux/pid_namespace.h>
1349f4d8b9SEric W. Biederman #include <linux/user_namespace.h>
1474bd59bbSPavel Emelyanov #include <linux/syscalls.h>
1574bd59bbSPavel Emelyanov #include <linux/err.h>
160b6b030fSPavel Emelyanov #include <linux/acct.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
180bb80f24SDavid Howells #include <linux/proc_ns.h>
19cf3f8921SDaniel Lezcano #include <linux/reboot.h>
20523a6a94SEric W. Biederman #include <linux/export.h>
2174bd59bbSPavel Emelyanov 
2274bd59bbSPavel Emelyanov struct pid_cache {
2374bd59bbSPavel Emelyanov 	int nr_ids;
2474bd59bbSPavel Emelyanov 	char name[16];
2574bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
2674bd59bbSPavel Emelyanov 	struct list_head list;
2774bd59bbSPavel Emelyanov };
2874bd59bbSPavel Emelyanov 
2974bd59bbSPavel Emelyanov static LIST_HEAD(pid_caches_lh);
3074bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
3174bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
3274bd59bbSPavel Emelyanov 
3374bd59bbSPavel Emelyanov /*
3474bd59bbSPavel Emelyanov  * creates the kmem cache to allocate pids from.
3574bd59bbSPavel Emelyanov  * @nr_ids: the number of numerical ids this pid will have to carry
3674bd59bbSPavel Emelyanov  */
3774bd59bbSPavel Emelyanov 
3874bd59bbSPavel Emelyanov static struct kmem_cache *create_pid_cachep(int nr_ids)
3974bd59bbSPavel Emelyanov {
4074bd59bbSPavel Emelyanov 	struct pid_cache *pcache;
4174bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
4274bd59bbSPavel Emelyanov 
4374bd59bbSPavel Emelyanov 	mutex_lock(&pid_caches_mutex);
4474bd59bbSPavel Emelyanov 	list_for_each_entry(pcache, &pid_caches_lh, list)
4574bd59bbSPavel Emelyanov 		if (pcache->nr_ids == nr_ids)
4674bd59bbSPavel Emelyanov 			goto out;
4774bd59bbSPavel Emelyanov 
4874bd59bbSPavel Emelyanov 	pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
4974bd59bbSPavel Emelyanov 	if (pcache == NULL)
5074bd59bbSPavel Emelyanov 		goto err_alloc;
5174bd59bbSPavel Emelyanov 
5274bd59bbSPavel Emelyanov 	snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
5374bd59bbSPavel Emelyanov 	cachep = kmem_cache_create(pcache->name,
5474bd59bbSPavel Emelyanov 			sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
5574bd59bbSPavel Emelyanov 			0, SLAB_HWCACHE_ALIGN, NULL);
5674bd59bbSPavel Emelyanov 	if (cachep == NULL)
5774bd59bbSPavel Emelyanov 		goto err_cachep;
5874bd59bbSPavel Emelyanov 
5974bd59bbSPavel Emelyanov 	pcache->nr_ids = nr_ids;
6074bd59bbSPavel Emelyanov 	pcache->cachep = cachep;
6174bd59bbSPavel Emelyanov 	list_add(&pcache->list, &pid_caches_lh);
6274bd59bbSPavel Emelyanov out:
6374bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
6474bd59bbSPavel Emelyanov 	return pcache->cachep;
6574bd59bbSPavel Emelyanov 
6674bd59bbSPavel Emelyanov err_cachep:
6774bd59bbSPavel Emelyanov 	kfree(pcache);
6874bd59bbSPavel Emelyanov err_alloc:
6974bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
7074bd59bbSPavel Emelyanov 	return NULL;
7174bd59bbSPavel Emelyanov }
7274bd59bbSPavel Emelyanov 
730a01f2ccSEric W. Biederman static void proc_cleanup_work(struct work_struct *work)
740a01f2ccSEric W. Biederman {
750a01f2ccSEric W. Biederman 	struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work);
760a01f2ccSEric W. Biederman 	pid_ns_release_proc(ns);
770a01f2ccSEric W. Biederman }
780a01f2ccSEric W. Biederman 
79f2302505SAndrew Vagin /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
80f2302505SAndrew Vagin #define MAX_PID_NS_LEVEL 32
81f2302505SAndrew Vagin 
8249f4d8b9SEric W. Biederman static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
8349f4d8b9SEric W. Biederman 	struct pid_namespace *parent_pid_ns)
8474bd59bbSPavel Emelyanov {
8574bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
86ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
87f2302505SAndrew Vagin 	int i;
88f2302505SAndrew Vagin 	int err;
8974bd59bbSPavel Emelyanov 
90f2302505SAndrew Vagin 	if (level > MAX_PID_NS_LEVEL) {
91f2302505SAndrew Vagin 		err = -EINVAL;
92f2302505SAndrew Vagin 		goto out;
93f2302505SAndrew Vagin 	}
94f2302505SAndrew Vagin 
95f2302505SAndrew Vagin 	err = -ENOMEM;
9684406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
9774bd59bbSPavel Emelyanov 	if (ns == NULL)
9874bd59bbSPavel Emelyanov 		goto out;
9974bd59bbSPavel Emelyanov 
10074bd59bbSPavel Emelyanov 	ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
10174bd59bbSPavel Emelyanov 	if (!ns->pidmap[0].page)
10274bd59bbSPavel Emelyanov 		goto out_free;
10374bd59bbSPavel Emelyanov 
10474bd59bbSPavel Emelyanov 	ns->pid_cachep = create_pid_cachep(level + 1);
10574bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
10674bd59bbSPavel Emelyanov 		goto out_free_map;
10774bd59bbSPavel Emelyanov 
108435d5f4bSAl Viro 	err = proc_alloc_inum(&ns->ns.inum);
10998f842e6SEric W. Biederman 	if (err)
11098f842e6SEric W. Biederman 		goto out_free_map;
11198f842e6SEric W. Biederman 
11274bd59bbSPavel Emelyanov 	kref_init(&ns->kref);
11374bd59bbSPavel Emelyanov 	ns->level = level;
114ed469a63SAlexey Dobriyan 	ns->parent = get_pid_ns(parent_pid_ns);
11549f4d8b9SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
116c876ad76SEric W. Biederman 	ns->nr_hashed = PIDNS_HASH_ADDING;
1170a01f2ccSEric W. Biederman 	INIT_WORK(&ns->proc_work, proc_cleanup_work);
11874bd59bbSPavel Emelyanov 
11974bd59bbSPavel Emelyanov 	set_bit(0, ns->pidmap[0].page);
12074bd59bbSPavel Emelyanov 	atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
12174bd59bbSPavel Emelyanov 
12284406c15SPavel Emelyanov 	for (i = 1; i < PIDMAP_ENTRIES; i++)
12374bd59bbSPavel Emelyanov 		atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
12474bd59bbSPavel Emelyanov 
12574bd59bbSPavel Emelyanov 	return ns;
12674bd59bbSPavel Emelyanov 
12774bd59bbSPavel Emelyanov out_free_map:
12874bd59bbSPavel Emelyanov 	kfree(ns->pidmap[0].page);
12974bd59bbSPavel Emelyanov out_free:
13074bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
13174bd59bbSPavel Emelyanov out:
1324308eebbSEric W. Biederman 	return ERR_PTR(err);
13374bd59bbSPavel Emelyanov }
13474bd59bbSPavel Emelyanov 
1351adfcb03SAl Viro static void delayed_free_pidns(struct rcu_head *p)
1361adfcb03SAl Viro {
1371adfcb03SAl Viro 	kmem_cache_free(pid_ns_cachep,
1381adfcb03SAl Viro 			container_of(p, struct pid_namespace, rcu));
1391adfcb03SAl Viro }
1401adfcb03SAl Viro 
14174bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
14274bd59bbSPavel Emelyanov {
14374bd59bbSPavel Emelyanov 	int i;
14474bd59bbSPavel Emelyanov 
145435d5f4bSAl Viro 	proc_free_inum(ns->ns.inum);
14674bd59bbSPavel Emelyanov 	for (i = 0; i < PIDMAP_ENTRIES; i++)
14774bd59bbSPavel Emelyanov 		kfree(ns->pidmap[i].page);
14849f4d8b9SEric W. Biederman 	put_user_ns(ns->user_ns);
1491adfcb03SAl Viro 	call_rcu(&ns->rcu, delayed_free_pidns);
15074bd59bbSPavel Emelyanov }
15174bd59bbSPavel Emelyanov 
15249f4d8b9SEric W. Biederman struct pid_namespace *copy_pid_ns(unsigned long flags,
15349f4d8b9SEric W. Biederman 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
15474bd59bbSPavel Emelyanov {
15574bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
156dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
157225778d6SEric W. Biederman 	if (task_active_pid_ns(current) != old_ns)
158225778d6SEric W. Biederman 		return ERR_PTR(-EINVAL);
15949f4d8b9SEric W. Biederman 	return create_pid_namespace(user_ns, old_ns);
16074bd59bbSPavel Emelyanov }
16174bd59bbSPavel Emelyanov 
162bbc2e3efSCyrill Gorcunov static void free_pid_ns(struct kref *kref)
16374bd59bbSPavel Emelyanov {
164bbc2e3efSCyrill Gorcunov 	struct pid_namespace *ns;
16574bd59bbSPavel Emelyanov 
16674bd59bbSPavel Emelyanov 	ns = container_of(kref, struct pid_namespace, kref);
16774bd59bbSPavel Emelyanov 	destroy_pid_namespace(ns);
16874bd59bbSPavel Emelyanov }
169bbc2e3efSCyrill Gorcunov 
170bbc2e3efSCyrill Gorcunov void put_pid_ns(struct pid_namespace *ns)
171bbc2e3efSCyrill Gorcunov {
172bbc2e3efSCyrill Gorcunov 	struct pid_namespace *parent;
173bbc2e3efSCyrill Gorcunov 
174bbc2e3efSCyrill Gorcunov 	while (ns != &init_pid_ns) {
175bbc2e3efSCyrill Gorcunov 		parent = ns->parent;
176bbc2e3efSCyrill Gorcunov 		if (!kref_put(&ns->kref, free_pid_ns))
177bbc2e3efSCyrill Gorcunov 			break;
178bbc2e3efSCyrill Gorcunov 		ns = parent;
179bbc2e3efSCyrill Gorcunov 	}
180bbc2e3efSCyrill Gorcunov }
181bbc2e3efSCyrill Gorcunov EXPORT_SYMBOL_GPL(put_pid_ns);
18274bd59bbSPavel Emelyanov 
18374bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
18474bd59bbSPavel Emelyanov {
18574bd59bbSPavel Emelyanov 	int nr;
18674bd59bbSPavel Emelyanov 	int rc;
18700c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
188751c644bSEric W. Biederman 	int init_pids = thread_group_leader(me) ? 1 : 2;
18900c10bc1SEric W. Biederman 
190c876ad76SEric W. Biederman 	/* Don't allow any more processes into the pid namespace */
191c876ad76SEric W. Biederman 	disable_pid_allocation(pid_ns);
192c876ad76SEric W. Biederman 
19300c10bc1SEric W. Biederman 	/* Ignore SIGCHLD causing any terminated children to autoreap */
19400c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
19500c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
19600c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
19774bd59bbSPavel Emelyanov 
19874bd59bbSPavel Emelyanov 	/*
19974bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
20074bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
20174bd59bbSPavel Emelyanov 	 * to exit.
20274bd59bbSPavel Emelyanov 	 *
20374bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
20474bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
20574bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
20674bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
20774bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
20874bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
20974bd59bbSPavel Emelyanov 	 *
21074bd59bbSPavel Emelyanov 	 */
21174bd59bbSPavel Emelyanov 	read_lock(&tasklist_lock);
21274bd59bbSPavel Emelyanov 	nr = next_pidmap(pid_ns, 1);
21374bd59bbSPavel Emelyanov 	while (nr > 0) {
214e4da026fSSukadev Bhattiprolu 		rcu_read_lock();
215e4da026fSSukadev Bhattiprolu 
216e4da026fSSukadev Bhattiprolu 		task = pid_task(find_vpid(nr), PIDTYPE_PID);
217a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
218a02d6fd6SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
219e4da026fSSukadev Bhattiprolu 
220e4da026fSSukadev Bhattiprolu 		rcu_read_unlock();
221e4da026fSSukadev Bhattiprolu 
22274bd59bbSPavel Emelyanov 		nr = next_pidmap(pid_ns, nr);
22374bd59bbSPavel Emelyanov 	}
22474bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
22574bd59bbSPavel Emelyanov 
2266347e900SEric W. Biederman 	/* Firstly reap the EXIT_ZOMBIE children we may have. */
22774bd59bbSPavel Emelyanov 	do {
22874bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
22974bd59bbSPavel Emelyanov 		rc = sys_wait4(-1, NULL, __WALL, NULL);
23074bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
23174bd59bbSPavel Emelyanov 
2326347e900SEric W. Biederman 	/*
2336347e900SEric W. Biederman 	 * sys_wait4() above can't reap the TASK_DEAD children.
234af4b8a83SEric W. Biederman 	 * Make sure they all go away, see free_pid().
2356347e900SEric W. Biederman 	 */
2366347e900SEric W. Biederman 	for (;;) {
237af4b8a83SEric W. Biederman 		set_current_state(TASK_UNINTERRUPTIBLE);
238751c644bSEric W. Biederman 		if (pid_ns->nr_hashed == init_pids)
2396347e900SEric W. Biederman 			break;
2406347e900SEric W. Biederman 		schedule();
2416347e900SEric W. Biederman 	}
242af4b8a83SEric W. Biederman 	__set_current_state(TASK_RUNNING);
2436347e900SEric W. Biederman 
244cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
245cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
246cf3f8921SDaniel Lezcano 
2470b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
24874bd59bbSPavel Emelyanov 	return;
24974bd59bbSPavel Emelyanov }
25074bd59bbSPavel Emelyanov 
25198ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
252b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write,
253b8f566b0SPavel Emelyanov 		void __user *buffer, size_t *lenp, loff_t *ppos)
254b8f566b0SPavel Emelyanov {
25549f4d8b9SEric W. Biederman 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
256b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
257b8f566b0SPavel Emelyanov 
25849f4d8b9SEric W. Biederman 	if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
259b8f566b0SPavel Emelyanov 		return -EPERM;
260b8f566b0SPavel Emelyanov 
261b8f566b0SPavel Emelyanov 	/*
262b8f566b0SPavel Emelyanov 	 * Writing directly to ns' last_pid field is OK, since this field
263b8f566b0SPavel Emelyanov 	 * is volatile in a living namespace anyway and a code writing to
264b8f566b0SPavel Emelyanov 	 * it should synchronize its usage with external means.
265b8f566b0SPavel Emelyanov 	 */
266b8f566b0SPavel Emelyanov 
26749f4d8b9SEric W. Biederman 	tmp.data = &pid_ns->last_pid;
268579035dcSAndrew Vagin 	return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
269b8f566b0SPavel Emelyanov }
270b8f566b0SPavel Emelyanov 
271579035dcSAndrew Vagin extern int pid_max;
272579035dcSAndrew Vagin static int zero = 0;
273b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = {
274b8f566b0SPavel Emelyanov 	{
275b8f566b0SPavel Emelyanov 		.procname = "ns_last_pid",
276b8f566b0SPavel Emelyanov 		.maxlen = sizeof(int),
277b8f566b0SPavel Emelyanov 		.mode = 0666, /* permissions are checked in the handler */
278b8f566b0SPavel Emelyanov 		.proc_handler = pid_ns_ctl_handler,
279579035dcSAndrew Vagin 		.extra1 = &zero,
280579035dcSAndrew Vagin 		.extra2 = &pid_max,
281b8f566b0SPavel Emelyanov 	},
282b8f566b0SPavel Emelyanov 	{ }
283b8f566b0SPavel Emelyanov };
284b8f566b0SPavel Emelyanov static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
28598ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
286b8f566b0SPavel Emelyanov 
287cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
288cf3f8921SDaniel Lezcano {
289cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
290cf3f8921SDaniel Lezcano 		return 0;
291cf3f8921SDaniel Lezcano 
292cf3f8921SDaniel Lezcano 	switch (cmd) {
293cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
294cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
295cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
296cf3f8921SDaniel Lezcano 		break;
297cf3f8921SDaniel Lezcano 
298cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
299cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
300cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
301cf3f8921SDaniel Lezcano 		break;
302cf3f8921SDaniel Lezcano 	default:
303cf3f8921SDaniel Lezcano 		return -EINVAL;
304cf3f8921SDaniel Lezcano 	}
305cf3f8921SDaniel Lezcano 
306cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
307cf3f8921SDaniel Lezcano 	force_sig(SIGKILL, pid_ns->child_reaper);
308cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
309cf3f8921SDaniel Lezcano 
310cf3f8921SDaniel Lezcano 	do_exit(0);
311cf3f8921SDaniel Lezcano 
312cf3f8921SDaniel Lezcano 	/* Not reached */
313cf3f8921SDaniel Lezcano 	return 0;
314cf3f8921SDaniel Lezcano }
315cf3f8921SDaniel Lezcano 
316*3c041184SAl Viro static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
317*3c041184SAl Viro {
318*3c041184SAl Viro 	return container_of(ns, struct pid_namespace, ns);
319*3c041184SAl Viro }
320*3c041184SAl Viro 
32157e8391dSEric W. Biederman static void *pidns_get(struct task_struct *task)
32257e8391dSEric W. Biederman {
32357e8391dSEric W. Biederman 	struct pid_namespace *ns;
32457e8391dSEric W. Biederman 
32557e8391dSEric W. Biederman 	rcu_read_lock();
326d2308225SOleg Nesterov 	ns = task_active_pid_ns(task);
327d2308225SOleg Nesterov 	if (ns)
328d2308225SOleg Nesterov 		get_pid_ns(ns);
32957e8391dSEric W. Biederman 	rcu_read_unlock();
33057e8391dSEric W. Biederman 
331*3c041184SAl Viro 	return ns ? &ns->ns : NULL;
33257e8391dSEric W. Biederman }
33357e8391dSEric W. Biederman 
33457e8391dSEric W. Biederman static void pidns_put(void *ns)
33557e8391dSEric W. Biederman {
336*3c041184SAl Viro 	put_pid_ns(to_pid_ns(ns));
33757e8391dSEric W. Biederman }
33857e8391dSEric W. Biederman 
33957e8391dSEric W. Biederman static int pidns_install(struct nsproxy *nsproxy, void *ns)
34057e8391dSEric W. Biederman {
34157e8391dSEric W. Biederman 	struct pid_namespace *active = task_active_pid_ns(current);
342*3c041184SAl Viro 	struct pid_namespace *ancestor, *new = to_pid_ns(ns);
34357e8391dSEric W. Biederman 
3445e4a0847SEric W. Biederman 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
345c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
34657e8391dSEric W. Biederman 		return -EPERM;
34757e8391dSEric W. Biederman 
34857e8391dSEric W. Biederman 	/*
34957e8391dSEric W. Biederman 	 * Only allow entering the current active pid namespace
35057e8391dSEric W. Biederman 	 * or a child of the current active pid namespace.
35157e8391dSEric W. Biederman 	 *
35257e8391dSEric W. Biederman 	 * This is required for fork to return a usable pid value and
35357e8391dSEric W. Biederman 	 * this maintains the property that processes and their
35457e8391dSEric W. Biederman 	 * children can not escape their current pid namespace.
35557e8391dSEric W. Biederman 	 */
35657e8391dSEric W. Biederman 	if (new->level < active->level)
35757e8391dSEric W. Biederman 		return -EINVAL;
35857e8391dSEric W. Biederman 
35957e8391dSEric W. Biederman 	ancestor = new;
36057e8391dSEric W. Biederman 	while (ancestor->level > active->level)
36157e8391dSEric W. Biederman 		ancestor = ancestor->parent;
36257e8391dSEric W. Biederman 	if (ancestor != active)
36357e8391dSEric W. Biederman 		return -EINVAL;
36457e8391dSEric W. Biederman 
365c2b1df2eSAndy Lutomirski 	put_pid_ns(nsproxy->pid_ns_for_children);
366c2b1df2eSAndy Lutomirski 	nsproxy->pid_ns_for_children = get_pid_ns(new);
36757e8391dSEric W. Biederman 	return 0;
36857e8391dSEric W. Biederman }
36957e8391dSEric W. Biederman 
37098f842e6SEric W. Biederman static unsigned int pidns_inum(void *ns)
37198f842e6SEric W. Biederman {
372*3c041184SAl Viro 	return ((struct ns_common *)ns)->inum;
37398f842e6SEric W. Biederman }
37498f842e6SEric W. Biederman 
37557e8391dSEric W. Biederman const struct proc_ns_operations pidns_operations = {
37657e8391dSEric W. Biederman 	.name		= "pid",
37757e8391dSEric W. Biederman 	.type		= CLONE_NEWPID,
37857e8391dSEric W. Biederman 	.get		= pidns_get,
37957e8391dSEric W. Biederman 	.put		= pidns_put,
38057e8391dSEric W. Biederman 	.install	= pidns_install,
38198f842e6SEric W. Biederman 	.inum		= pidns_inum,
38257e8391dSEric W. Biederman };
38357e8391dSEric W. Biederman 
38474bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
38574bd59bbSPavel Emelyanov {
38674bd59bbSPavel Emelyanov 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
38798ed57eeSCyrill Gorcunov 
38898ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
389b8f566b0SPavel Emelyanov 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
39098ed57eeSCyrill Gorcunov #endif
39174bd59bbSPavel Emelyanov 	return 0;
39274bd59bbSPavel Emelyanov }
39374bd59bbSPavel Emelyanov 
39474bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
395