xref: /openbmc/linux/kernel/pid_namespace.c (revision 523a6a945f3cf5f1d337e50634687a577a732a5f)
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>
1374bd59bbSPavel Emelyanov #include <linux/syscalls.h>
1474bd59bbSPavel Emelyanov #include <linux/err.h>
150b6b030fSPavel Emelyanov #include <linux/acct.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
174308eebbSEric W. Biederman #include <linux/proc_fs.h>
18cf3f8921SDaniel Lezcano #include <linux/reboot.h>
19*523a6a94SEric W. Biederman #include <linux/export.h>
2074bd59bbSPavel Emelyanov 
2174bd59bbSPavel Emelyanov #define BITS_PER_PAGE		(PAGE_SIZE*8)
2274bd59bbSPavel Emelyanov 
2374bd59bbSPavel Emelyanov struct pid_cache {
2474bd59bbSPavel Emelyanov 	int nr_ids;
2574bd59bbSPavel Emelyanov 	char name[16];
2674bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
2774bd59bbSPavel Emelyanov 	struct list_head list;
2874bd59bbSPavel Emelyanov };
2974bd59bbSPavel Emelyanov 
3074bd59bbSPavel Emelyanov static LIST_HEAD(pid_caches_lh);
3174bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
3274bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
3374bd59bbSPavel Emelyanov 
3474bd59bbSPavel Emelyanov /*
3574bd59bbSPavel Emelyanov  * creates the kmem cache to allocate pids from.
3674bd59bbSPavel Emelyanov  * @nr_ids: the number of numerical ids this pid will have to carry
3774bd59bbSPavel Emelyanov  */
3874bd59bbSPavel Emelyanov 
3974bd59bbSPavel Emelyanov static struct kmem_cache *create_pid_cachep(int nr_ids)
4074bd59bbSPavel Emelyanov {
4174bd59bbSPavel Emelyanov 	struct pid_cache *pcache;
4274bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
4374bd59bbSPavel Emelyanov 
4474bd59bbSPavel Emelyanov 	mutex_lock(&pid_caches_mutex);
4574bd59bbSPavel Emelyanov 	list_for_each_entry(pcache, &pid_caches_lh, list)
4674bd59bbSPavel Emelyanov 		if (pcache->nr_ids == nr_ids)
4774bd59bbSPavel Emelyanov 			goto out;
4874bd59bbSPavel Emelyanov 
4974bd59bbSPavel Emelyanov 	pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
5074bd59bbSPavel Emelyanov 	if (pcache == NULL)
5174bd59bbSPavel Emelyanov 		goto err_alloc;
5274bd59bbSPavel Emelyanov 
5374bd59bbSPavel Emelyanov 	snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
5474bd59bbSPavel Emelyanov 	cachep = kmem_cache_create(pcache->name,
5574bd59bbSPavel Emelyanov 			sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
5674bd59bbSPavel Emelyanov 			0, SLAB_HWCACHE_ALIGN, NULL);
5774bd59bbSPavel Emelyanov 	if (cachep == NULL)
5874bd59bbSPavel Emelyanov 		goto err_cachep;
5974bd59bbSPavel Emelyanov 
6074bd59bbSPavel Emelyanov 	pcache->nr_ids = nr_ids;
6174bd59bbSPavel Emelyanov 	pcache->cachep = cachep;
6274bd59bbSPavel Emelyanov 	list_add(&pcache->list, &pid_caches_lh);
6374bd59bbSPavel Emelyanov out:
6474bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
6574bd59bbSPavel Emelyanov 	return pcache->cachep;
6674bd59bbSPavel Emelyanov 
6774bd59bbSPavel Emelyanov err_cachep:
6874bd59bbSPavel Emelyanov 	kfree(pcache);
6974bd59bbSPavel Emelyanov err_alloc:
7074bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
7174bd59bbSPavel Emelyanov 	return NULL;
7274bd59bbSPavel Emelyanov }
7374bd59bbSPavel Emelyanov 
74ed469a63SAlexey Dobriyan static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns)
7574bd59bbSPavel Emelyanov {
7674bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
77ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
784308eebbSEric W. Biederman 	int i, err = -ENOMEM;
7974bd59bbSPavel Emelyanov 
8084406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
8174bd59bbSPavel Emelyanov 	if (ns == NULL)
8274bd59bbSPavel Emelyanov 		goto out;
8374bd59bbSPavel Emelyanov 
8474bd59bbSPavel Emelyanov 	ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
8574bd59bbSPavel Emelyanov 	if (!ns->pidmap[0].page)
8674bd59bbSPavel Emelyanov 		goto out_free;
8774bd59bbSPavel Emelyanov 
8874bd59bbSPavel Emelyanov 	ns->pid_cachep = create_pid_cachep(level + 1);
8974bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
9074bd59bbSPavel Emelyanov 		goto out_free_map;
9174bd59bbSPavel Emelyanov 
9274bd59bbSPavel Emelyanov 	kref_init(&ns->kref);
9374bd59bbSPavel Emelyanov 	ns->level = level;
94ed469a63SAlexey Dobriyan 	ns->parent = get_pid_ns(parent_pid_ns);
9574bd59bbSPavel Emelyanov 
9674bd59bbSPavel Emelyanov 	set_bit(0, ns->pidmap[0].page);
9774bd59bbSPavel Emelyanov 	atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
9874bd59bbSPavel Emelyanov 
9984406c15SPavel Emelyanov 	for (i = 1; i < PIDMAP_ENTRIES; i++)
10074bd59bbSPavel Emelyanov 		atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
10174bd59bbSPavel Emelyanov 
1024308eebbSEric W. Biederman 	err = pid_ns_prepare_proc(ns);
1034308eebbSEric W. Biederman 	if (err)
1044308eebbSEric W. Biederman 		goto out_put_parent_pid_ns;
1054308eebbSEric W. Biederman 
10674bd59bbSPavel Emelyanov 	return ns;
10774bd59bbSPavel Emelyanov 
1084308eebbSEric W. Biederman out_put_parent_pid_ns:
1094308eebbSEric W. Biederman 	put_pid_ns(parent_pid_ns);
11074bd59bbSPavel Emelyanov out_free_map:
11174bd59bbSPavel Emelyanov 	kfree(ns->pidmap[0].page);
11274bd59bbSPavel Emelyanov out_free:
11374bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
11474bd59bbSPavel Emelyanov out:
1154308eebbSEric W. Biederman 	return ERR_PTR(err);
11674bd59bbSPavel Emelyanov }
11774bd59bbSPavel Emelyanov 
11874bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
11974bd59bbSPavel Emelyanov {
12074bd59bbSPavel Emelyanov 	int i;
12174bd59bbSPavel Emelyanov 
12274bd59bbSPavel Emelyanov 	for (i = 0; i < PIDMAP_ENTRIES; i++)
12374bd59bbSPavel Emelyanov 		kfree(ns->pidmap[i].page);
12474bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
12574bd59bbSPavel Emelyanov }
12674bd59bbSPavel Emelyanov 
12774bd59bbSPavel Emelyanov struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
12874bd59bbSPavel Emelyanov {
12974bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
130dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
131e5a47386SSukadev Bhattiprolu 	if (flags & (CLONE_THREAD|CLONE_PARENT))
132dca4a979SAlexey Dobriyan 		return ERR_PTR(-EINVAL);
133dca4a979SAlexey Dobriyan 	return create_pid_namespace(old_ns);
13474bd59bbSPavel Emelyanov }
13574bd59bbSPavel Emelyanov 
13674bd59bbSPavel Emelyanov void free_pid_ns(struct kref *kref)
13774bd59bbSPavel Emelyanov {
13874bd59bbSPavel Emelyanov 	struct pid_namespace *ns, *parent;
13974bd59bbSPavel Emelyanov 
14074bd59bbSPavel Emelyanov 	ns = container_of(kref, struct pid_namespace, kref);
14174bd59bbSPavel Emelyanov 
14274bd59bbSPavel Emelyanov 	parent = ns->parent;
14374bd59bbSPavel Emelyanov 	destroy_pid_namespace(ns);
14474bd59bbSPavel Emelyanov 
14574bd59bbSPavel Emelyanov 	if (parent != NULL)
14674bd59bbSPavel Emelyanov 		put_pid_ns(parent);
14774bd59bbSPavel Emelyanov }
148*523a6a94SEric W. Biederman EXPORT_SYMBOL_GPL(free_pid_ns);
14974bd59bbSPavel Emelyanov 
15074bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
15174bd59bbSPavel Emelyanov {
15274bd59bbSPavel Emelyanov 	int nr;
15374bd59bbSPavel Emelyanov 	int rc;
15400c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
15500c10bc1SEric W. Biederman 
15600c10bc1SEric W. Biederman 	/* Ignore SIGCHLD causing any terminated children to autoreap */
15700c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
15800c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
15900c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
16074bd59bbSPavel Emelyanov 
16174bd59bbSPavel Emelyanov 	/*
16274bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
16374bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
16474bd59bbSPavel Emelyanov 	 * to exit.
16574bd59bbSPavel Emelyanov 	 *
16674bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
16774bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
16874bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
16974bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
17074bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
17174bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
17274bd59bbSPavel Emelyanov 	 *
17374bd59bbSPavel Emelyanov 	 */
17474bd59bbSPavel Emelyanov 	read_lock(&tasklist_lock);
17574bd59bbSPavel Emelyanov 	nr = next_pidmap(pid_ns, 1);
17674bd59bbSPavel Emelyanov 	while (nr > 0) {
177e4da026fSSukadev Bhattiprolu 		rcu_read_lock();
178e4da026fSSukadev Bhattiprolu 
179e4da026fSSukadev Bhattiprolu 		task = pid_task(find_vpid(nr), PIDTYPE_PID);
180a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
181a02d6fd6SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
182e4da026fSSukadev Bhattiprolu 
183e4da026fSSukadev Bhattiprolu 		rcu_read_unlock();
184e4da026fSSukadev Bhattiprolu 
18574bd59bbSPavel Emelyanov 		nr = next_pidmap(pid_ns, nr);
18674bd59bbSPavel Emelyanov 	}
18774bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
18874bd59bbSPavel Emelyanov 
1896347e900SEric W. Biederman 	/* Firstly reap the EXIT_ZOMBIE children we may have. */
19074bd59bbSPavel Emelyanov 	do {
19174bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
19274bd59bbSPavel Emelyanov 		rc = sys_wait4(-1, NULL, __WALL, NULL);
19374bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
19474bd59bbSPavel Emelyanov 
1956347e900SEric W. Biederman 	/*
1966347e900SEric W. Biederman 	 * sys_wait4() above can't reap the TASK_DEAD children.
1976347e900SEric W. Biederman 	 * Make sure they all go away, see __unhash_process().
1986347e900SEric W. Biederman 	 */
1996347e900SEric W. Biederman 	for (;;) {
2006347e900SEric W. Biederman 		bool need_wait = false;
2016347e900SEric W. Biederman 
2026347e900SEric W. Biederman 		read_lock(&tasklist_lock);
2036347e900SEric W. Biederman 		if (!list_empty(&current->children)) {
2046347e900SEric W. Biederman 			__set_current_state(TASK_UNINTERRUPTIBLE);
2056347e900SEric W. Biederman 			need_wait = true;
2066347e900SEric W. Biederman 		}
2076347e900SEric W. Biederman 		read_unlock(&tasklist_lock);
2086347e900SEric W. Biederman 
2096347e900SEric W. Biederman 		if (!need_wait)
2106347e900SEric W. Biederman 			break;
2116347e900SEric W. Biederman 		schedule();
2126347e900SEric W. Biederman 	}
2136347e900SEric W. Biederman 
214cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
215cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
216cf3f8921SDaniel Lezcano 
2170b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
21874bd59bbSPavel Emelyanov 	return;
21974bd59bbSPavel Emelyanov }
22074bd59bbSPavel Emelyanov 
22198ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
222b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write,
223b8f566b0SPavel Emelyanov 		void __user *buffer, size_t *lenp, loff_t *ppos)
224b8f566b0SPavel Emelyanov {
225b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
226b8f566b0SPavel Emelyanov 
227b8f566b0SPavel Emelyanov 	if (write && !capable(CAP_SYS_ADMIN))
228b8f566b0SPavel Emelyanov 		return -EPERM;
229b8f566b0SPavel Emelyanov 
230b8f566b0SPavel Emelyanov 	/*
231b8f566b0SPavel Emelyanov 	 * Writing directly to ns' last_pid field is OK, since this field
232b8f566b0SPavel Emelyanov 	 * is volatile in a living namespace anyway and a code writing to
233b8f566b0SPavel Emelyanov 	 * it should synchronize its usage with external means.
234b8f566b0SPavel Emelyanov 	 */
235b8f566b0SPavel Emelyanov 
236b8f566b0SPavel Emelyanov 	tmp.data = &current->nsproxy->pid_ns->last_pid;
237b8f566b0SPavel Emelyanov 	return proc_dointvec(&tmp, write, buffer, lenp, ppos);
238b8f566b0SPavel Emelyanov }
239b8f566b0SPavel Emelyanov 
240b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = {
241b8f566b0SPavel Emelyanov 	{
242b8f566b0SPavel Emelyanov 		.procname = "ns_last_pid",
243b8f566b0SPavel Emelyanov 		.maxlen = sizeof(int),
244b8f566b0SPavel Emelyanov 		.mode = 0666, /* permissions are checked in the handler */
245b8f566b0SPavel Emelyanov 		.proc_handler = pid_ns_ctl_handler,
246b8f566b0SPavel Emelyanov 	},
247b8f566b0SPavel Emelyanov 	{ }
248b8f566b0SPavel Emelyanov };
249b8f566b0SPavel Emelyanov static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
25098ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
251b8f566b0SPavel Emelyanov 
252cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
253cf3f8921SDaniel Lezcano {
254cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
255cf3f8921SDaniel Lezcano 		return 0;
256cf3f8921SDaniel Lezcano 
257cf3f8921SDaniel Lezcano 	switch (cmd) {
258cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
259cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
260cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
261cf3f8921SDaniel Lezcano 		break;
262cf3f8921SDaniel Lezcano 
263cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
264cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
265cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
266cf3f8921SDaniel Lezcano 		break;
267cf3f8921SDaniel Lezcano 	default:
268cf3f8921SDaniel Lezcano 		return -EINVAL;
269cf3f8921SDaniel Lezcano 	}
270cf3f8921SDaniel Lezcano 
271cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
272cf3f8921SDaniel Lezcano 	force_sig(SIGKILL, pid_ns->child_reaper);
273cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
274cf3f8921SDaniel Lezcano 
275cf3f8921SDaniel Lezcano 	do_exit(0);
276cf3f8921SDaniel Lezcano 
277cf3f8921SDaniel Lezcano 	/* Not reached */
278cf3f8921SDaniel Lezcano 	return 0;
279cf3f8921SDaniel Lezcano }
280cf3f8921SDaniel Lezcano 
28174bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
28274bd59bbSPavel Emelyanov {
28374bd59bbSPavel Emelyanov 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
28498ed57eeSCyrill Gorcunov 
28598ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
286b8f566b0SPavel Emelyanov 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
28798ed57eeSCyrill Gorcunov #endif
28874bd59bbSPavel Emelyanov 	return 0;
28974bd59bbSPavel Emelyanov }
29074bd59bbSPavel Emelyanov 
29174bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
292