xref: /openbmc/linux/kernel/pid_namespace.c (revision a2b426267c56773201f968fdb5eda6ab9ae94e34)
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>
155b825c3aSIngo Molnar #include <linux/cred.h>
1674bd59bbSPavel Emelyanov #include <linux/err.h>
170b6b030fSPavel Emelyanov #include <linux/acct.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
190bb80f24SDavid Howells #include <linux/proc_ns.h>
20cf3f8921SDaniel Lezcano #include <linux/reboot.h>
21523a6a94SEric W. Biederman #include <linux/export.h>
2229930025SIngo Molnar #include <linux/sched/task.h>
23f361bf4aSIngo Molnar #include <linux/sched/signal.h>
2474bd59bbSPavel Emelyanov 
2574bd59bbSPavel Emelyanov struct pid_cache {
2674bd59bbSPavel Emelyanov 	int nr_ids;
2774bd59bbSPavel Emelyanov 	char name[16];
2874bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
2974bd59bbSPavel Emelyanov 	struct list_head list;
3074bd59bbSPavel Emelyanov };
3174bd59bbSPavel Emelyanov 
3274bd59bbSPavel Emelyanov static LIST_HEAD(pid_caches_lh);
3374bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
3474bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
3574bd59bbSPavel Emelyanov 
3674bd59bbSPavel Emelyanov /*
3774bd59bbSPavel Emelyanov  * creates the kmem cache to allocate pids from.
3874bd59bbSPavel Emelyanov  * @nr_ids: the number of numerical ids this pid will have to carry
3974bd59bbSPavel Emelyanov  */
4074bd59bbSPavel Emelyanov 
4174bd59bbSPavel Emelyanov static struct kmem_cache *create_pid_cachep(int nr_ids)
4274bd59bbSPavel Emelyanov {
4374bd59bbSPavel Emelyanov 	struct pid_cache *pcache;
4474bd59bbSPavel Emelyanov 	struct kmem_cache *cachep;
4574bd59bbSPavel Emelyanov 
4674bd59bbSPavel Emelyanov 	mutex_lock(&pid_caches_mutex);
4774bd59bbSPavel Emelyanov 	list_for_each_entry(pcache, &pid_caches_lh, list)
4874bd59bbSPavel Emelyanov 		if (pcache->nr_ids == nr_ids)
4974bd59bbSPavel Emelyanov 			goto out;
5074bd59bbSPavel Emelyanov 
5174bd59bbSPavel Emelyanov 	pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
5274bd59bbSPavel Emelyanov 	if (pcache == NULL)
5374bd59bbSPavel Emelyanov 		goto err_alloc;
5474bd59bbSPavel Emelyanov 
5574bd59bbSPavel Emelyanov 	snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
5674bd59bbSPavel Emelyanov 	cachep = kmem_cache_create(pcache->name,
5774bd59bbSPavel Emelyanov 			sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
5874bd59bbSPavel Emelyanov 			0, SLAB_HWCACHE_ALIGN, NULL);
5974bd59bbSPavel Emelyanov 	if (cachep == NULL)
6074bd59bbSPavel Emelyanov 		goto err_cachep;
6174bd59bbSPavel Emelyanov 
6274bd59bbSPavel Emelyanov 	pcache->nr_ids = nr_ids;
6374bd59bbSPavel Emelyanov 	pcache->cachep = cachep;
6474bd59bbSPavel Emelyanov 	list_add(&pcache->list, &pid_caches_lh);
6574bd59bbSPavel Emelyanov out:
6674bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
6774bd59bbSPavel Emelyanov 	return pcache->cachep;
6874bd59bbSPavel Emelyanov 
6974bd59bbSPavel Emelyanov err_cachep:
7074bd59bbSPavel Emelyanov 	kfree(pcache);
7174bd59bbSPavel Emelyanov err_alloc:
7274bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
7374bd59bbSPavel Emelyanov 	return NULL;
7474bd59bbSPavel Emelyanov }
7574bd59bbSPavel Emelyanov 
760a01f2ccSEric W. Biederman static void proc_cleanup_work(struct work_struct *work)
770a01f2ccSEric W. Biederman {
780a01f2ccSEric W. Biederman 	struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work);
790a01f2ccSEric W. Biederman 	pid_ns_release_proc(ns);
800a01f2ccSEric W. Biederman }
810a01f2ccSEric W. Biederman 
82f2302505SAndrew Vagin /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
83f2302505SAndrew Vagin #define MAX_PID_NS_LEVEL 32
84f2302505SAndrew Vagin 
85f333c700SEric W. Biederman static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
86f333c700SEric W. Biederman {
87f333c700SEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
88f333c700SEric W. Biederman }
89f333c700SEric W. Biederman 
90f333c700SEric W. Biederman static void dec_pid_namespaces(struct ucounts *ucounts)
91f333c700SEric W. Biederman {
92f333c700SEric W. Biederman 	dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
93f333c700SEric W. Biederman }
94f333c700SEric W. Biederman 
9549f4d8b9SEric W. Biederman static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
9649f4d8b9SEric W. Biederman 	struct pid_namespace *parent_pid_ns)
9774bd59bbSPavel Emelyanov {
9874bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
99ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
100f333c700SEric W. Biederman 	struct ucounts *ucounts;
101f2302505SAndrew Vagin 	int i;
102f2302505SAndrew Vagin 	int err;
10374bd59bbSPavel Emelyanov 
104*a2b42626SEric W. Biederman 	err = -EINVAL;
105*a2b42626SEric W. Biederman 	if (!in_userns(parent_pid_ns->user_ns, user_ns))
106*a2b42626SEric W. Biederman 		goto out;
107*a2b42626SEric W. Biederman 
108df75e774SEric W. Biederman 	err = -ENOSPC;
109f333c700SEric W. Biederman 	if (level > MAX_PID_NS_LEVEL)
110f2302505SAndrew Vagin 		goto out;
111f333c700SEric W. Biederman 	ucounts = inc_pid_namespaces(user_ns);
112f333c700SEric W. Biederman 	if (!ucounts)
113f333c700SEric W. Biederman 		goto out;
114f2302505SAndrew Vagin 
115f2302505SAndrew Vagin 	err = -ENOMEM;
11684406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
11774bd59bbSPavel Emelyanov 	if (ns == NULL)
118f333c700SEric W. Biederman 		goto out_dec;
11974bd59bbSPavel Emelyanov 
12074bd59bbSPavel Emelyanov 	ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
12174bd59bbSPavel Emelyanov 	if (!ns->pidmap[0].page)
12274bd59bbSPavel Emelyanov 		goto out_free;
12374bd59bbSPavel Emelyanov 
12474bd59bbSPavel Emelyanov 	ns->pid_cachep = create_pid_cachep(level + 1);
12574bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
12674bd59bbSPavel Emelyanov 		goto out_free_map;
12774bd59bbSPavel Emelyanov 
1286344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
12998f842e6SEric W. Biederman 	if (err)
13098f842e6SEric W. Biederman 		goto out_free_map;
13133c42940SAl Viro 	ns->ns.ops = &pidns_operations;
13298f842e6SEric W. Biederman 
13374bd59bbSPavel Emelyanov 	kref_init(&ns->kref);
13474bd59bbSPavel Emelyanov 	ns->level = level;
135ed469a63SAlexey Dobriyan 	ns->parent = get_pid_ns(parent_pid_ns);
13649f4d8b9SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
137f333c700SEric W. Biederman 	ns->ucounts = ucounts;
138c876ad76SEric W. Biederman 	ns->nr_hashed = PIDNS_HASH_ADDING;
1390a01f2ccSEric W. Biederman 	INIT_WORK(&ns->proc_work, proc_cleanup_work);
14074bd59bbSPavel Emelyanov 
14174bd59bbSPavel Emelyanov 	set_bit(0, ns->pidmap[0].page);
14274bd59bbSPavel Emelyanov 	atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
14374bd59bbSPavel Emelyanov 
14484406c15SPavel Emelyanov 	for (i = 1; i < PIDMAP_ENTRIES; i++)
14574bd59bbSPavel Emelyanov 		atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
14674bd59bbSPavel Emelyanov 
14774bd59bbSPavel Emelyanov 	return ns;
14874bd59bbSPavel Emelyanov 
14974bd59bbSPavel Emelyanov out_free_map:
15074bd59bbSPavel Emelyanov 	kfree(ns->pidmap[0].page);
15174bd59bbSPavel Emelyanov out_free:
15274bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
153f333c700SEric W. Biederman out_dec:
154f333c700SEric W. Biederman 	dec_pid_namespaces(ucounts);
15574bd59bbSPavel Emelyanov out:
1564308eebbSEric W. Biederman 	return ERR_PTR(err);
15774bd59bbSPavel Emelyanov }
15874bd59bbSPavel Emelyanov 
1591adfcb03SAl Viro static void delayed_free_pidns(struct rcu_head *p)
1601adfcb03SAl Viro {
161add7c65cSAndrei Vagin 	struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
162add7c65cSAndrei Vagin 
163add7c65cSAndrei Vagin 	dec_pid_namespaces(ns->ucounts);
164add7c65cSAndrei Vagin 	put_user_ns(ns->user_ns);
165add7c65cSAndrei Vagin 
166add7c65cSAndrei Vagin 	kmem_cache_free(pid_ns_cachep, ns);
1671adfcb03SAl Viro }
1681adfcb03SAl Viro 
16974bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
17074bd59bbSPavel Emelyanov {
17174bd59bbSPavel Emelyanov 	int i;
17274bd59bbSPavel Emelyanov 
1736344c433SAl Viro 	ns_free_inum(&ns->ns);
17474bd59bbSPavel Emelyanov 	for (i = 0; i < PIDMAP_ENTRIES; i++)
17574bd59bbSPavel Emelyanov 		kfree(ns->pidmap[i].page);
1761adfcb03SAl Viro 	call_rcu(&ns->rcu, delayed_free_pidns);
17774bd59bbSPavel Emelyanov }
17874bd59bbSPavel Emelyanov 
17949f4d8b9SEric W. Biederman struct pid_namespace *copy_pid_ns(unsigned long flags,
18049f4d8b9SEric W. Biederman 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
18174bd59bbSPavel Emelyanov {
18274bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
183dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
184225778d6SEric W. Biederman 	if (task_active_pid_ns(current) != old_ns)
185225778d6SEric W. Biederman 		return ERR_PTR(-EINVAL);
18649f4d8b9SEric W. Biederman 	return create_pid_namespace(user_ns, old_ns);
18774bd59bbSPavel Emelyanov }
18874bd59bbSPavel Emelyanov 
189bbc2e3efSCyrill Gorcunov static void free_pid_ns(struct kref *kref)
19074bd59bbSPavel Emelyanov {
191bbc2e3efSCyrill Gorcunov 	struct pid_namespace *ns;
19274bd59bbSPavel Emelyanov 
19374bd59bbSPavel Emelyanov 	ns = container_of(kref, struct pid_namespace, kref);
19474bd59bbSPavel Emelyanov 	destroy_pid_namespace(ns);
19574bd59bbSPavel Emelyanov }
196bbc2e3efSCyrill Gorcunov 
197bbc2e3efSCyrill Gorcunov void put_pid_ns(struct pid_namespace *ns)
198bbc2e3efSCyrill Gorcunov {
199bbc2e3efSCyrill Gorcunov 	struct pid_namespace *parent;
200bbc2e3efSCyrill Gorcunov 
201bbc2e3efSCyrill Gorcunov 	while (ns != &init_pid_ns) {
202bbc2e3efSCyrill Gorcunov 		parent = ns->parent;
203bbc2e3efSCyrill Gorcunov 		if (!kref_put(&ns->kref, free_pid_ns))
204bbc2e3efSCyrill Gorcunov 			break;
205bbc2e3efSCyrill Gorcunov 		ns = parent;
206bbc2e3efSCyrill Gorcunov 	}
207bbc2e3efSCyrill Gorcunov }
208bbc2e3efSCyrill Gorcunov EXPORT_SYMBOL_GPL(put_pid_ns);
20974bd59bbSPavel Emelyanov 
21074bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
21174bd59bbSPavel Emelyanov {
21274bd59bbSPavel Emelyanov 	int nr;
21374bd59bbSPavel Emelyanov 	int rc;
21400c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
215751c644bSEric W. Biederman 	int init_pids = thread_group_leader(me) ? 1 : 2;
21600c10bc1SEric W. Biederman 
217c876ad76SEric W. Biederman 	/* Don't allow any more processes into the pid namespace */
218c876ad76SEric W. Biederman 	disable_pid_allocation(pid_ns);
219c876ad76SEric W. Biederman 
220a53b8315SOleg Nesterov 	/*
221a53b8315SOleg Nesterov 	 * Ignore SIGCHLD causing any terminated children to autoreap.
222a53b8315SOleg Nesterov 	 * This speeds up the namespace shutdown, plus see the comment
223a53b8315SOleg Nesterov 	 * below.
224a53b8315SOleg Nesterov 	 */
22500c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
22600c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
22700c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
22874bd59bbSPavel Emelyanov 
22974bd59bbSPavel Emelyanov 	/*
23074bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
23174bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
23274bd59bbSPavel Emelyanov 	 * to exit.
23374bd59bbSPavel Emelyanov 	 *
23474bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
23574bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
23674bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
23774bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
23874bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
23974bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
24074bd59bbSPavel Emelyanov 	 *
24174bd59bbSPavel Emelyanov 	 */
24274bd59bbSPavel Emelyanov 	read_lock(&tasklist_lock);
24374bd59bbSPavel Emelyanov 	nr = next_pidmap(pid_ns, 1);
24474bd59bbSPavel Emelyanov 	while (nr > 0) {
245e4da026fSSukadev Bhattiprolu 		rcu_read_lock();
246e4da026fSSukadev Bhattiprolu 
247e4da026fSSukadev Bhattiprolu 		task = pid_task(find_vpid(nr), PIDTYPE_PID);
248a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
249a02d6fd6SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
250e4da026fSSukadev Bhattiprolu 
251e4da026fSSukadev Bhattiprolu 		rcu_read_unlock();
252e4da026fSSukadev Bhattiprolu 
25374bd59bbSPavel Emelyanov 		nr = next_pidmap(pid_ns, nr);
25474bd59bbSPavel Emelyanov 	}
25574bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
25674bd59bbSPavel Emelyanov 
257a53b8315SOleg Nesterov 	/*
258a53b8315SOleg Nesterov 	 * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
259a53b8315SOleg Nesterov 	 * sys_wait4() will also block until our children traced from the
260a53b8315SOleg Nesterov 	 * parent namespace are detached and become EXIT_DEAD.
261a53b8315SOleg Nesterov 	 */
26274bd59bbSPavel Emelyanov 	do {
26374bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
26474bd59bbSPavel Emelyanov 		rc = sys_wait4(-1, NULL, __WALL, NULL);
26574bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
26674bd59bbSPavel Emelyanov 
2676347e900SEric W. Biederman 	/*
268a53b8315SOleg Nesterov 	 * sys_wait4() above can't reap the EXIT_DEAD children but we do not
269a53b8315SOleg Nesterov 	 * really care, we could reparent them to the global init. We could
270a53b8315SOleg Nesterov 	 * exit and reap ->child_reaper even if it is not the last thread in
271a53b8315SOleg Nesterov 	 * this pid_ns, free_pid(nr_hashed == 0) calls proc_cleanup_work(),
272a53b8315SOleg Nesterov 	 * pid_ns can not go away until proc_kill_sb() drops the reference.
273a53b8315SOleg Nesterov 	 *
274a53b8315SOleg Nesterov 	 * But this ns can also have other tasks injected by setns()+fork().
275a53b8315SOleg Nesterov 	 * Again, ignoring the user visible semantics we do not really need
276a53b8315SOleg Nesterov 	 * to wait until they are all reaped, but they can be reparented to
277a53b8315SOleg Nesterov 	 * us and thus we need to ensure that pid->child_reaper stays valid
278a53b8315SOleg Nesterov 	 * until they all go away. See free_pid()->wake_up_process().
279a53b8315SOleg Nesterov 	 *
280a53b8315SOleg Nesterov 	 * We rely on ignored SIGCHLD, an injected zombie must be autoreaped
281a53b8315SOleg Nesterov 	 * if reparented.
2826347e900SEric W. Biederman 	 */
2836347e900SEric W. Biederman 	for (;;) {
284b9a985dbSEric W. Biederman 		set_current_state(TASK_INTERRUPTIBLE);
285751c644bSEric W. Biederman 		if (pid_ns->nr_hashed == init_pids)
2866347e900SEric W. Biederman 			break;
2876347e900SEric W. Biederman 		schedule();
2886347e900SEric W. Biederman 	}
289af4b8a83SEric W. Biederman 	__set_current_state(TASK_RUNNING);
2906347e900SEric W. Biederman 
291cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
292cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
293cf3f8921SDaniel Lezcano 
2940b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
29574bd59bbSPavel Emelyanov 	return;
29674bd59bbSPavel Emelyanov }
29774bd59bbSPavel Emelyanov 
29898ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
299b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write,
300b8f566b0SPavel Emelyanov 		void __user *buffer, size_t *lenp, loff_t *ppos)
301b8f566b0SPavel Emelyanov {
30249f4d8b9SEric W. Biederman 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
303b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
304b8f566b0SPavel Emelyanov 
30549f4d8b9SEric W. Biederman 	if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
306b8f566b0SPavel Emelyanov 		return -EPERM;
307b8f566b0SPavel Emelyanov 
308b8f566b0SPavel Emelyanov 	/*
309b8f566b0SPavel Emelyanov 	 * Writing directly to ns' last_pid field is OK, since this field
310b8f566b0SPavel Emelyanov 	 * is volatile in a living namespace anyway and a code writing to
311b8f566b0SPavel Emelyanov 	 * it should synchronize its usage with external means.
312b8f566b0SPavel Emelyanov 	 */
313b8f566b0SPavel Emelyanov 
31449f4d8b9SEric W. Biederman 	tmp.data = &pid_ns->last_pid;
315579035dcSAndrew Vagin 	return proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
316b8f566b0SPavel Emelyanov }
317b8f566b0SPavel Emelyanov 
318579035dcSAndrew Vagin extern int pid_max;
319579035dcSAndrew Vagin static int zero = 0;
320b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = {
321b8f566b0SPavel Emelyanov 	{
322b8f566b0SPavel Emelyanov 		.procname = "ns_last_pid",
323b8f566b0SPavel Emelyanov 		.maxlen = sizeof(int),
324b8f566b0SPavel Emelyanov 		.mode = 0666, /* permissions are checked in the handler */
325b8f566b0SPavel Emelyanov 		.proc_handler = pid_ns_ctl_handler,
326579035dcSAndrew Vagin 		.extra1 = &zero,
327579035dcSAndrew Vagin 		.extra2 = &pid_max,
328b8f566b0SPavel Emelyanov 	},
329b8f566b0SPavel Emelyanov 	{ }
330b8f566b0SPavel Emelyanov };
331b8f566b0SPavel Emelyanov static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
33298ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
333b8f566b0SPavel Emelyanov 
334cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
335cf3f8921SDaniel Lezcano {
336cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
337cf3f8921SDaniel Lezcano 		return 0;
338cf3f8921SDaniel Lezcano 
339cf3f8921SDaniel Lezcano 	switch (cmd) {
340cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
341cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
342cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
343cf3f8921SDaniel Lezcano 		break;
344cf3f8921SDaniel Lezcano 
345cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
346cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
347cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
348cf3f8921SDaniel Lezcano 		break;
349cf3f8921SDaniel Lezcano 	default:
350cf3f8921SDaniel Lezcano 		return -EINVAL;
351cf3f8921SDaniel Lezcano 	}
352cf3f8921SDaniel Lezcano 
353cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
354cf3f8921SDaniel Lezcano 	force_sig(SIGKILL, pid_ns->child_reaper);
355cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
356cf3f8921SDaniel Lezcano 
357cf3f8921SDaniel Lezcano 	do_exit(0);
358cf3f8921SDaniel Lezcano 
359cf3f8921SDaniel Lezcano 	/* Not reached */
360cf3f8921SDaniel Lezcano 	return 0;
361cf3f8921SDaniel Lezcano }
362cf3f8921SDaniel Lezcano 
3633c041184SAl Viro static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
3643c041184SAl Viro {
3653c041184SAl Viro 	return container_of(ns, struct pid_namespace, ns);
3663c041184SAl Viro }
3673c041184SAl Viro 
36864964528SAl Viro static struct ns_common *pidns_get(struct task_struct *task)
36957e8391dSEric W. Biederman {
37057e8391dSEric W. Biederman 	struct pid_namespace *ns;
37157e8391dSEric W. Biederman 
37257e8391dSEric W. Biederman 	rcu_read_lock();
373d2308225SOleg Nesterov 	ns = task_active_pid_ns(task);
374d2308225SOleg Nesterov 	if (ns)
375d2308225SOleg Nesterov 		get_pid_ns(ns);
37657e8391dSEric W. Biederman 	rcu_read_unlock();
37757e8391dSEric W. Biederman 
3783c041184SAl Viro 	return ns ? &ns->ns : NULL;
37957e8391dSEric W. Biederman }
38057e8391dSEric W. Biederman 
381eaa0d190SKirill Tkhai static struct ns_common *pidns_for_children_get(struct task_struct *task)
382eaa0d190SKirill Tkhai {
383eaa0d190SKirill Tkhai 	struct pid_namespace *ns = NULL;
384eaa0d190SKirill Tkhai 
385eaa0d190SKirill Tkhai 	task_lock(task);
386eaa0d190SKirill Tkhai 	if (task->nsproxy) {
387eaa0d190SKirill Tkhai 		ns = task->nsproxy->pid_ns_for_children;
388eaa0d190SKirill Tkhai 		get_pid_ns(ns);
389eaa0d190SKirill Tkhai 	}
390eaa0d190SKirill Tkhai 	task_unlock(task);
391eaa0d190SKirill Tkhai 
392eaa0d190SKirill Tkhai 	if (ns) {
393eaa0d190SKirill Tkhai 		read_lock(&tasklist_lock);
394eaa0d190SKirill Tkhai 		if (!ns->child_reaper) {
395eaa0d190SKirill Tkhai 			put_pid_ns(ns);
396eaa0d190SKirill Tkhai 			ns = NULL;
397eaa0d190SKirill Tkhai 		}
398eaa0d190SKirill Tkhai 		read_unlock(&tasklist_lock);
399eaa0d190SKirill Tkhai 	}
400eaa0d190SKirill Tkhai 
401eaa0d190SKirill Tkhai 	return ns ? &ns->ns : NULL;
402eaa0d190SKirill Tkhai }
403eaa0d190SKirill Tkhai 
40464964528SAl Viro static void pidns_put(struct ns_common *ns)
40557e8391dSEric W. Biederman {
4063c041184SAl Viro 	put_pid_ns(to_pid_ns(ns));
40757e8391dSEric W. Biederman }
40857e8391dSEric W. Biederman 
40964964528SAl Viro static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
41057e8391dSEric W. Biederman {
41157e8391dSEric W. Biederman 	struct pid_namespace *active = task_active_pid_ns(current);
4123c041184SAl Viro 	struct pid_namespace *ancestor, *new = to_pid_ns(ns);
41357e8391dSEric W. Biederman 
4145e4a0847SEric W. Biederman 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
415c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
41657e8391dSEric W. Biederman 		return -EPERM;
41757e8391dSEric W. Biederman 
41857e8391dSEric W. Biederman 	/*
41957e8391dSEric W. Biederman 	 * Only allow entering the current active pid namespace
42057e8391dSEric W. Biederman 	 * or a child of the current active pid namespace.
42157e8391dSEric W. Biederman 	 *
42257e8391dSEric W. Biederman 	 * This is required for fork to return a usable pid value and
42357e8391dSEric W. Biederman 	 * this maintains the property that processes and their
42457e8391dSEric W. Biederman 	 * children can not escape their current pid namespace.
42557e8391dSEric W. Biederman 	 */
42657e8391dSEric W. Biederman 	if (new->level < active->level)
42757e8391dSEric W. Biederman 		return -EINVAL;
42857e8391dSEric W. Biederman 
42957e8391dSEric W. Biederman 	ancestor = new;
43057e8391dSEric W. Biederman 	while (ancestor->level > active->level)
43157e8391dSEric W. Biederman 		ancestor = ancestor->parent;
43257e8391dSEric W. Biederman 	if (ancestor != active)
43357e8391dSEric W. Biederman 		return -EINVAL;
43457e8391dSEric W. Biederman 
435c2b1df2eSAndy Lutomirski 	put_pid_ns(nsproxy->pid_ns_for_children);
436c2b1df2eSAndy Lutomirski 	nsproxy->pid_ns_for_children = get_pid_ns(new);
43757e8391dSEric W. Biederman 	return 0;
43857e8391dSEric W. Biederman }
43957e8391dSEric W. Biederman 
440a7306ed8SAndrey Vagin static struct ns_common *pidns_get_parent(struct ns_common *ns)
441a7306ed8SAndrey Vagin {
442a7306ed8SAndrey Vagin 	struct pid_namespace *active = task_active_pid_ns(current);
443a7306ed8SAndrey Vagin 	struct pid_namespace *pid_ns, *p;
444a7306ed8SAndrey Vagin 
445a7306ed8SAndrey Vagin 	/* See if the parent is in the current namespace */
446a7306ed8SAndrey Vagin 	pid_ns = p = to_pid_ns(ns)->parent;
447a7306ed8SAndrey Vagin 	for (;;) {
448a7306ed8SAndrey Vagin 		if (!p)
449a7306ed8SAndrey Vagin 			return ERR_PTR(-EPERM);
450a7306ed8SAndrey Vagin 		if (p == active)
451a7306ed8SAndrey Vagin 			break;
452a7306ed8SAndrey Vagin 		p = p->parent;
453a7306ed8SAndrey Vagin 	}
454a7306ed8SAndrey Vagin 
455a7306ed8SAndrey Vagin 	return &get_pid_ns(pid_ns)->ns;
456a7306ed8SAndrey Vagin }
457a7306ed8SAndrey Vagin 
458bcac25a5SAndrey Vagin static struct user_namespace *pidns_owner(struct ns_common *ns)
459bcac25a5SAndrey Vagin {
460bcac25a5SAndrey Vagin 	return to_pid_ns(ns)->user_ns;
461bcac25a5SAndrey Vagin }
462bcac25a5SAndrey Vagin 
46357e8391dSEric W. Biederman const struct proc_ns_operations pidns_operations = {
46457e8391dSEric W. Biederman 	.name		= "pid",
46557e8391dSEric W. Biederman 	.type		= CLONE_NEWPID,
46657e8391dSEric W. Biederman 	.get		= pidns_get,
46757e8391dSEric W. Biederman 	.put		= pidns_put,
46857e8391dSEric W. Biederman 	.install	= pidns_install,
469bcac25a5SAndrey Vagin 	.owner		= pidns_owner,
470a7306ed8SAndrey Vagin 	.get_parent	= pidns_get_parent,
47157e8391dSEric W. Biederman };
47257e8391dSEric W. Biederman 
473eaa0d190SKirill Tkhai const struct proc_ns_operations pidns_for_children_operations = {
474eaa0d190SKirill Tkhai 	.name		= "pid_for_children",
475eaa0d190SKirill Tkhai 	.real_ns_name	= "pid",
476eaa0d190SKirill Tkhai 	.type		= CLONE_NEWPID,
477eaa0d190SKirill Tkhai 	.get		= pidns_for_children_get,
478eaa0d190SKirill Tkhai 	.put		= pidns_put,
479eaa0d190SKirill Tkhai 	.install	= pidns_install,
480eaa0d190SKirill Tkhai 	.owner		= pidns_owner,
481eaa0d190SKirill Tkhai 	.get_parent	= pidns_get_parent,
482eaa0d190SKirill Tkhai };
483eaa0d190SKirill Tkhai 
48474bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
48574bd59bbSPavel Emelyanov {
48674bd59bbSPavel Emelyanov 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
48798ed57eeSCyrill Gorcunov 
48898ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
489b8f566b0SPavel Emelyanov 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
49098ed57eeSCyrill Gorcunov #endif
49174bd59bbSPavel Emelyanov 	return 0;
49274bd59bbSPavel Emelyanov }
49374bd59bbSPavel Emelyanov 
49474bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
495