xref: /openbmc/linux/kernel/pid_namespace.c (revision 82058d6684658430cd9b4123d4c3e863fd48f813)
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>
2495846ecfSGargi Sharma #include <linux/idr.h>
2574bd59bbSPavel Emelyanov 
2674bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex);
2774bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep;
28dd206becSAlexey Dobriyan /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
29dd206becSAlexey Dobriyan #define MAX_PID_NS_LEVEL 32
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)
55dd206becSAlexey Dobriyan 		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
5674bd59bbSPavel Emelyanov 	mutex_unlock(&pid_caches_mutex);
57dd206becSAlexey Dobriyan 	/* current can fail, but someone else can succeed. */
58dd206becSAlexey Dobriyan 	return READ_ONCE(*pkc);
5974bd59bbSPavel Emelyanov }
6074bd59bbSPavel Emelyanov 
610a01f2ccSEric W. Biederman static void proc_cleanup_work(struct work_struct *work)
620a01f2ccSEric W. Biederman {
630a01f2ccSEric W. Biederman 	struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work);
640a01f2ccSEric W. Biederman 	pid_ns_release_proc(ns);
650a01f2ccSEric W. Biederman }
660a01f2ccSEric W. Biederman 
67f333c700SEric W. Biederman static struct ucounts *inc_pid_namespaces(struct user_namespace *ns)
68f333c700SEric W. Biederman {
69f333c700SEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_PID_NAMESPACES);
70f333c700SEric W. Biederman }
71f333c700SEric W. Biederman 
72f333c700SEric W. Biederman static void dec_pid_namespaces(struct ucounts *ucounts)
73f333c700SEric W. Biederman {
74f333c700SEric W. Biederman 	dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
75f333c700SEric W. Biederman }
76f333c700SEric W. Biederman 
7749f4d8b9SEric W. Biederman static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
7849f4d8b9SEric W. Biederman 	struct pid_namespace *parent_pid_ns)
7974bd59bbSPavel Emelyanov {
8074bd59bbSPavel Emelyanov 	struct pid_namespace *ns;
81ed469a63SAlexey Dobriyan 	unsigned int level = parent_pid_ns->level + 1;
82f333c700SEric W. Biederman 	struct ucounts *ucounts;
83f2302505SAndrew Vagin 	int err;
8474bd59bbSPavel Emelyanov 
85a2b42626SEric W. Biederman 	err = -EINVAL;
86a2b42626SEric W. Biederman 	if (!in_userns(parent_pid_ns->user_ns, user_ns))
87a2b42626SEric W. Biederman 		goto out;
88a2b42626SEric W. Biederman 
89df75e774SEric W. Biederman 	err = -ENOSPC;
90f333c700SEric W. Biederman 	if (level > MAX_PID_NS_LEVEL)
91f2302505SAndrew Vagin 		goto out;
92f333c700SEric W. Biederman 	ucounts = inc_pid_namespaces(user_ns);
93f333c700SEric W. Biederman 	if (!ucounts)
94f333c700SEric W. Biederman 		goto out;
95f2302505SAndrew Vagin 
96f2302505SAndrew Vagin 	err = -ENOMEM;
9784406c15SPavel Emelyanov 	ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
9874bd59bbSPavel Emelyanov 	if (ns == NULL)
99f333c700SEric W. Biederman 		goto out_dec;
10074bd59bbSPavel Emelyanov 
10195846ecfSGargi Sharma 	idr_init(&ns->idr);
10274bd59bbSPavel Emelyanov 
103dd206becSAlexey Dobriyan 	ns->pid_cachep = create_pid_cachep(level);
10474bd59bbSPavel Emelyanov 	if (ns->pid_cachep == NULL)
10595846ecfSGargi Sharma 		goto out_free_idr;
10674bd59bbSPavel Emelyanov 
1076344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
10898f842e6SEric W. Biederman 	if (err)
10995846ecfSGargi Sharma 		goto out_free_idr;
11033c42940SAl Viro 	ns->ns.ops = &pidns_operations;
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);
116f333c700SEric W. Biederman 	ns->ucounts = ucounts;
117e8cfbc24SGargi Sharma 	ns->pid_allocated = PIDNS_ADDING;
1180a01f2ccSEric W. Biederman 	INIT_WORK(&ns->proc_work, proc_cleanup_work);
11974bd59bbSPavel Emelyanov 
12074bd59bbSPavel Emelyanov 	return ns;
12174bd59bbSPavel Emelyanov 
12295846ecfSGargi Sharma out_free_idr:
12395846ecfSGargi Sharma 	idr_destroy(&ns->idr);
12474bd59bbSPavel Emelyanov 	kmem_cache_free(pid_ns_cachep, ns);
125f333c700SEric W. Biederman out_dec:
126f333c700SEric W. Biederman 	dec_pid_namespaces(ucounts);
12774bd59bbSPavel Emelyanov out:
1284308eebbSEric W. Biederman 	return ERR_PTR(err);
12974bd59bbSPavel Emelyanov }
13074bd59bbSPavel Emelyanov 
1311adfcb03SAl Viro static void delayed_free_pidns(struct rcu_head *p)
1321adfcb03SAl Viro {
133add7c65cSAndrei Vagin 	struct pid_namespace *ns = container_of(p, struct pid_namespace, rcu);
134add7c65cSAndrei Vagin 
135add7c65cSAndrei Vagin 	dec_pid_namespaces(ns->ucounts);
136add7c65cSAndrei Vagin 	put_user_ns(ns->user_ns);
137add7c65cSAndrei Vagin 
138add7c65cSAndrei Vagin 	kmem_cache_free(pid_ns_cachep, ns);
1391adfcb03SAl Viro }
1401adfcb03SAl Viro 
14174bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns)
14274bd59bbSPavel Emelyanov {
1436344c433SAl Viro 	ns_free_inum(&ns->ns);
14495846ecfSGargi Sharma 
14595846ecfSGargi Sharma 	idr_destroy(&ns->idr);
1461adfcb03SAl Viro 	call_rcu(&ns->rcu, delayed_free_pidns);
14774bd59bbSPavel Emelyanov }
14874bd59bbSPavel Emelyanov 
14949f4d8b9SEric W. Biederman struct pid_namespace *copy_pid_ns(unsigned long flags,
15049f4d8b9SEric W. Biederman 	struct user_namespace *user_ns, struct pid_namespace *old_ns)
15174bd59bbSPavel Emelyanov {
15274bd59bbSPavel Emelyanov 	if (!(flags & CLONE_NEWPID))
153dca4a979SAlexey Dobriyan 		return get_pid_ns(old_ns);
154225778d6SEric W. Biederman 	if (task_active_pid_ns(current) != old_ns)
155225778d6SEric W. Biederman 		return ERR_PTR(-EINVAL);
15649f4d8b9SEric W. Biederman 	return create_pid_namespace(user_ns, old_ns);
15774bd59bbSPavel Emelyanov }
15874bd59bbSPavel Emelyanov 
159bbc2e3efSCyrill Gorcunov static void free_pid_ns(struct kref *kref)
16074bd59bbSPavel Emelyanov {
161bbc2e3efSCyrill Gorcunov 	struct pid_namespace *ns;
16274bd59bbSPavel Emelyanov 
16374bd59bbSPavel Emelyanov 	ns = container_of(kref, struct pid_namespace, kref);
16474bd59bbSPavel Emelyanov 	destroy_pid_namespace(ns);
16574bd59bbSPavel Emelyanov }
166bbc2e3efSCyrill Gorcunov 
167bbc2e3efSCyrill Gorcunov void put_pid_ns(struct pid_namespace *ns)
168bbc2e3efSCyrill Gorcunov {
169bbc2e3efSCyrill Gorcunov 	struct pid_namespace *parent;
170bbc2e3efSCyrill Gorcunov 
171bbc2e3efSCyrill Gorcunov 	while (ns != &init_pid_ns) {
172bbc2e3efSCyrill Gorcunov 		parent = ns->parent;
173bbc2e3efSCyrill Gorcunov 		if (!kref_put(&ns->kref, free_pid_ns))
174bbc2e3efSCyrill Gorcunov 			break;
175bbc2e3efSCyrill Gorcunov 		ns = parent;
176bbc2e3efSCyrill Gorcunov 	}
177bbc2e3efSCyrill Gorcunov }
178bbc2e3efSCyrill Gorcunov EXPORT_SYMBOL_GPL(put_pid_ns);
17974bd59bbSPavel Emelyanov 
18074bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns)
18174bd59bbSPavel Emelyanov {
18274bd59bbSPavel Emelyanov 	int nr;
18374bd59bbSPavel Emelyanov 	int rc;
18400c10bc1SEric W. Biederman 	struct task_struct *task, *me = current;
185751c644bSEric W. Biederman 	int init_pids = thread_group_leader(me) ? 1 : 2;
18695846ecfSGargi Sharma 	struct pid *pid;
18700c10bc1SEric W. Biederman 
188c876ad76SEric W. Biederman 	/* Don't allow any more processes into the pid namespace */
189c876ad76SEric W. Biederman 	disable_pid_allocation(pid_ns);
190c876ad76SEric W. Biederman 
191a53b8315SOleg Nesterov 	/*
192a53b8315SOleg Nesterov 	 * Ignore SIGCHLD causing any terminated children to autoreap.
193a53b8315SOleg Nesterov 	 * This speeds up the namespace shutdown, plus see the comment
194a53b8315SOleg Nesterov 	 * below.
195a53b8315SOleg Nesterov 	 */
19600c10bc1SEric W. Biederman 	spin_lock_irq(&me->sighand->siglock);
19700c10bc1SEric W. Biederman 	me->sighand->action[SIGCHLD - 1].sa.sa_handler = SIG_IGN;
19800c10bc1SEric W. Biederman 	spin_unlock_irq(&me->sighand->siglock);
19974bd59bbSPavel Emelyanov 
20074bd59bbSPavel Emelyanov 	/*
20174bd59bbSPavel Emelyanov 	 * The last thread in the cgroup-init thread group is terminating.
20274bd59bbSPavel Emelyanov 	 * Find remaining pid_ts in the namespace, signal and wait for them
20374bd59bbSPavel Emelyanov 	 * to exit.
20474bd59bbSPavel Emelyanov 	 *
20574bd59bbSPavel Emelyanov 	 * Note:  This signals each threads in the namespace - even those that
20674bd59bbSPavel Emelyanov 	 * 	  belong to the same thread group, To avoid this, we would have
20774bd59bbSPavel Emelyanov 	 * 	  to walk the entire tasklist looking a processes in this
20874bd59bbSPavel Emelyanov 	 * 	  namespace, but that could be unnecessarily expensive if the
20974bd59bbSPavel Emelyanov 	 * 	  pid namespace has just a few processes. Or we need to
21074bd59bbSPavel Emelyanov 	 * 	  maintain a tasklist for each pid namespace.
21174bd59bbSPavel Emelyanov 	 *
21274bd59bbSPavel Emelyanov 	 */
213e4da026fSSukadev Bhattiprolu 	rcu_read_lock();
21495846ecfSGargi Sharma 	read_lock(&tasklist_lock);
21595846ecfSGargi Sharma 	nr = 2;
21695846ecfSGargi Sharma 	idr_for_each_entry_continue(&pid_ns->idr, pid, nr) {
21795846ecfSGargi Sharma 		task = pid_task(pid, PIDTYPE_PID);
218a02d6fd6SOleg Nesterov 		if (task && !__fatal_signal_pending(task))
219*82058d66SEric W. Biederman 			group_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_MAX);
22074bd59bbSPavel Emelyanov 	}
22174bd59bbSPavel Emelyanov 	read_unlock(&tasklist_lock);
22295846ecfSGargi Sharma 	rcu_read_unlock();
22374bd59bbSPavel Emelyanov 
224a53b8315SOleg Nesterov 	/*
225a53b8315SOleg Nesterov 	 * Reap the EXIT_ZOMBIE children we had before we ignored SIGCHLD.
226d300b610SDominik Brodowski 	 * kernel_wait4() will also block until our children traced from the
227a53b8315SOleg Nesterov 	 * parent namespace are detached and become EXIT_DEAD.
228a53b8315SOleg Nesterov 	 */
22974bd59bbSPavel Emelyanov 	do {
23074bd59bbSPavel Emelyanov 		clear_thread_flag(TIF_SIGPENDING);
231d300b610SDominik Brodowski 		rc = kernel_wait4(-1, NULL, __WALL, NULL);
23274bd59bbSPavel Emelyanov 	} while (rc != -ECHILD);
23374bd59bbSPavel Emelyanov 
2346347e900SEric W. Biederman 	/*
235d300b610SDominik Brodowski 	 * kernel_wait4() above can't reap the EXIT_DEAD children but we do not
236a53b8315SOleg Nesterov 	 * really care, we could reparent them to the global init. We could
237a53b8315SOleg Nesterov 	 * exit and reap ->child_reaper even if it is not the last thread in
238e8cfbc24SGargi Sharma 	 * this pid_ns, free_pid(pid_allocated == 0) calls proc_cleanup_work(),
239a53b8315SOleg Nesterov 	 * pid_ns can not go away until proc_kill_sb() drops the reference.
240a53b8315SOleg Nesterov 	 *
241a53b8315SOleg Nesterov 	 * But this ns can also have other tasks injected by setns()+fork().
242a53b8315SOleg Nesterov 	 * Again, ignoring the user visible semantics we do not really need
243a53b8315SOleg Nesterov 	 * to wait until they are all reaped, but they can be reparented to
244a53b8315SOleg Nesterov 	 * us and thus we need to ensure that pid->child_reaper stays valid
245a53b8315SOleg Nesterov 	 * until they all go away. See free_pid()->wake_up_process().
246a53b8315SOleg Nesterov 	 *
247a53b8315SOleg Nesterov 	 * We rely on ignored SIGCHLD, an injected zombie must be autoreaped
248a53b8315SOleg Nesterov 	 * if reparented.
2496347e900SEric W. Biederman 	 */
2506347e900SEric W. Biederman 	for (;;) {
251b9a985dbSEric W. Biederman 		set_current_state(TASK_INTERRUPTIBLE);
252e8cfbc24SGargi Sharma 		if (pid_ns->pid_allocated == init_pids)
2536347e900SEric W. Biederman 			break;
2546347e900SEric W. Biederman 		schedule();
2556347e900SEric W. Biederman 	}
256af4b8a83SEric W. Biederman 	__set_current_state(TASK_RUNNING);
2576347e900SEric W. Biederman 
258cf3f8921SDaniel Lezcano 	if (pid_ns->reboot)
259cf3f8921SDaniel Lezcano 		current->signal->group_exit_code = pid_ns->reboot;
260cf3f8921SDaniel Lezcano 
2610b6b030fSPavel Emelyanov 	acct_exit_ns(pid_ns);
26274bd59bbSPavel Emelyanov 	return;
26374bd59bbSPavel Emelyanov }
26474bd59bbSPavel Emelyanov 
26598ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
266b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write,
267b8f566b0SPavel Emelyanov 		void __user *buffer, size_t *lenp, loff_t *ppos)
268b8f566b0SPavel Emelyanov {
26949f4d8b9SEric W. Biederman 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
270b8f566b0SPavel Emelyanov 	struct ctl_table tmp = *table;
27195846ecfSGargi Sharma 	int ret, next;
272b8f566b0SPavel Emelyanov 
27349f4d8b9SEric W. Biederman 	if (write && !ns_capable(pid_ns->user_ns, CAP_SYS_ADMIN))
274b8f566b0SPavel Emelyanov 		return -EPERM;
275b8f566b0SPavel Emelyanov 
276b8f566b0SPavel Emelyanov 	/*
277b8f566b0SPavel Emelyanov 	 * Writing directly to ns' last_pid field is OK, since this field
278b8f566b0SPavel Emelyanov 	 * is volatile in a living namespace anyway and a code writing to
279b8f566b0SPavel Emelyanov 	 * it should synchronize its usage with external means.
280b8f566b0SPavel Emelyanov 	 */
281b8f566b0SPavel Emelyanov 
28295846ecfSGargi Sharma 	next = idr_get_cursor(&pid_ns->idr) - 1;
28395846ecfSGargi Sharma 
28495846ecfSGargi Sharma 	tmp.data = &next;
28595846ecfSGargi Sharma 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
28695846ecfSGargi Sharma 	if (!ret && write)
28795846ecfSGargi Sharma 		idr_set_cursor(&pid_ns->idr, next + 1);
28895846ecfSGargi Sharma 
28995846ecfSGargi Sharma 	return ret;
290b8f566b0SPavel Emelyanov }
291b8f566b0SPavel Emelyanov 
292579035dcSAndrew Vagin extern int pid_max;
293579035dcSAndrew Vagin static int zero = 0;
294b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = {
295b8f566b0SPavel Emelyanov 	{
296b8f566b0SPavel Emelyanov 		.procname = "ns_last_pid",
297b8f566b0SPavel Emelyanov 		.maxlen = sizeof(int),
298b8f566b0SPavel Emelyanov 		.mode = 0666, /* permissions are checked in the handler */
299b8f566b0SPavel Emelyanov 		.proc_handler = pid_ns_ctl_handler,
300579035dcSAndrew Vagin 		.extra1 = &zero,
301579035dcSAndrew Vagin 		.extra2 = &pid_max,
302b8f566b0SPavel Emelyanov 	},
303b8f566b0SPavel Emelyanov 	{ }
304b8f566b0SPavel Emelyanov };
305b8f566b0SPavel Emelyanov static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
30698ed57eeSCyrill Gorcunov #endif	/* CONFIG_CHECKPOINT_RESTORE */
307b8f566b0SPavel Emelyanov 
308cf3f8921SDaniel Lezcano int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
309cf3f8921SDaniel Lezcano {
310cf3f8921SDaniel Lezcano 	if (pid_ns == &init_pid_ns)
311cf3f8921SDaniel Lezcano 		return 0;
312cf3f8921SDaniel Lezcano 
313cf3f8921SDaniel Lezcano 	switch (cmd) {
314cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART2:
315cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_RESTART:
316cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGHUP;
317cf3f8921SDaniel Lezcano 		break;
318cf3f8921SDaniel Lezcano 
319cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_POWER_OFF:
320cf3f8921SDaniel Lezcano 	case LINUX_REBOOT_CMD_HALT:
321cf3f8921SDaniel Lezcano 		pid_ns->reboot = SIGINT;
322cf3f8921SDaniel Lezcano 		break;
323cf3f8921SDaniel Lezcano 	default:
324cf3f8921SDaniel Lezcano 		return -EINVAL;
325cf3f8921SDaniel Lezcano 	}
326cf3f8921SDaniel Lezcano 
327cf3f8921SDaniel Lezcano 	read_lock(&tasklist_lock);
328cf3f8921SDaniel Lezcano 	force_sig(SIGKILL, pid_ns->child_reaper);
329cf3f8921SDaniel Lezcano 	read_unlock(&tasklist_lock);
330cf3f8921SDaniel Lezcano 
331cf3f8921SDaniel Lezcano 	do_exit(0);
332cf3f8921SDaniel Lezcano 
333cf3f8921SDaniel Lezcano 	/* Not reached */
334cf3f8921SDaniel Lezcano 	return 0;
335cf3f8921SDaniel Lezcano }
336cf3f8921SDaniel Lezcano 
3373c041184SAl Viro static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
3383c041184SAl Viro {
3393c041184SAl Viro 	return container_of(ns, struct pid_namespace, ns);
3403c041184SAl Viro }
3413c041184SAl Viro 
34264964528SAl Viro static struct ns_common *pidns_get(struct task_struct *task)
34357e8391dSEric W. Biederman {
34457e8391dSEric W. Biederman 	struct pid_namespace *ns;
34557e8391dSEric W. Biederman 
34657e8391dSEric W. Biederman 	rcu_read_lock();
347d2308225SOleg Nesterov 	ns = task_active_pid_ns(task);
348d2308225SOleg Nesterov 	if (ns)
349d2308225SOleg Nesterov 		get_pid_ns(ns);
35057e8391dSEric W. Biederman 	rcu_read_unlock();
35157e8391dSEric W. Biederman 
3523c041184SAl Viro 	return ns ? &ns->ns : NULL;
35357e8391dSEric W. Biederman }
35457e8391dSEric W. Biederman 
355eaa0d190SKirill Tkhai static struct ns_common *pidns_for_children_get(struct task_struct *task)
356eaa0d190SKirill Tkhai {
357eaa0d190SKirill Tkhai 	struct pid_namespace *ns = NULL;
358eaa0d190SKirill Tkhai 
359eaa0d190SKirill Tkhai 	task_lock(task);
360eaa0d190SKirill Tkhai 	if (task->nsproxy) {
361eaa0d190SKirill Tkhai 		ns = task->nsproxy->pid_ns_for_children;
362eaa0d190SKirill Tkhai 		get_pid_ns(ns);
363eaa0d190SKirill Tkhai 	}
364eaa0d190SKirill Tkhai 	task_unlock(task);
365eaa0d190SKirill Tkhai 
366eaa0d190SKirill Tkhai 	if (ns) {
367eaa0d190SKirill Tkhai 		read_lock(&tasklist_lock);
368eaa0d190SKirill Tkhai 		if (!ns->child_reaper) {
369eaa0d190SKirill Tkhai 			put_pid_ns(ns);
370eaa0d190SKirill Tkhai 			ns = NULL;
371eaa0d190SKirill Tkhai 		}
372eaa0d190SKirill Tkhai 		read_unlock(&tasklist_lock);
373eaa0d190SKirill Tkhai 	}
374eaa0d190SKirill Tkhai 
375eaa0d190SKirill Tkhai 	return ns ? &ns->ns : NULL;
376eaa0d190SKirill Tkhai }
377eaa0d190SKirill Tkhai 
37864964528SAl Viro static void pidns_put(struct ns_common *ns)
37957e8391dSEric W. Biederman {
3803c041184SAl Viro 	put_pid_ns(to_pid_ns(ns));
38157e8391dSEric W. Biederman }
38257e8391dSEric W. Biederman 
38364964528SAl Viro static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns)
38457e8391dSEric W. Biederman {
38557e8391dSEric W. Biederman 	struct pid_namespace *active = task_active_pid_ns(current);
3863c041184SAl Viro 	struct pid_namespace *ancestor, *new = to_pid_ns(ns);
38757e8391dSEric W. Biederman 
3885e4a0847SEric W. Biederman 	if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) ||
389c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
39057e8391dSEric W. Biederman 		return -EPERM;
39157e8391dSEric W. Biederman 
39257e8391dSEric W. Biederman 	/*
39357e8391dSEric W. Biederman 	 * Only allow entering the current active pid namespace
39457e8391dSEric W. Biederman 	 * or a child of the current active pid namespace.
39557e8391dSEric W. Biederman 	 *
39657e8391dSEric W. Biederman 	 * This is required for fork to return a usable pid value and
39757e8391dSEric W. Biederman 	 * this maintains the property that processes and their
39857e8391dSEric W. Biederman 	 * children can not escape their current pid namespace.
39957e8391dSEric W. Biederman 	 */
40057e8391dSEric W. Biederman 	if (new->level < active->level)
40157e8391dSEric W. Biederman 		return -EINVAL;
40257e8391dSEric W. Biederman 
40357e8391dSEric W. Biederman 	ancestor = new;
40457e8391dSEric W. Biederman 	while (ancestor->level > active->level)
40557e8391dSEric W. Biederman 		ancestor = ancestor->parent;
40657e8391dSEric W. Biederman 	if (ancestor != active)
40757e8391dSEric W. Biederman 		return -EINVAL;
40857e8391dSEric W. Biederman 
409c2b1df2eSAndy Lutomirski 	put_pid_ns(nsproxy->pid_ns_for_children);
410c2b1df2eSAndy Lutomirski 	nsproxy->pid_ns_for_children = get_pid_ns(new);
41157e8391dSEric W. Biederman 	return 0;
41257e8391dSEric W. Biederman }
41357e8391dSEric W. Biederman 
414a7306ed8SAndrey Vagin static struct ns_common *pidns_get_parent(struct ns_common *ns)
415a7306ed8SAndrey Vagin {
416a7306ed8SAndrey Vagin 	struct pid_namespace *active = task_active_pid_ns(current);
417a7306ed8SAndrey Vagin 	struct pid_namespace *pid_ns, *p;
418a7306ed8SAndrey Vagin 
419a7306ed8SAndrey Vagin 	/* See if the parent is in the current namespace */
420a7306ed8SAndrey Vagin 	pid_ns = p = to_pid_ns(ns)->parent;
421a7306ed8SAndrey Vagin 	for (;;) {
422a7306ed8SAndrey Vagin 		if (!p)
423a7306ed8SAndrey Vagin 			return ERR_PTR(-EPERM);
424a7306ed8SAndrey Vagin 		if (p == active)
425a7306ed8SAndrey Vagin 			break;
426a7306ed8SAndrey Vagin 		p = p->parent;
427a7306ed8SAndrey Vagin 	}
428a7306ed8SAndrey Vagin 
429a7306ed8SAndrey Vagin 	return &get_pid_ns(pid_ns)->ns;
430a7306ed8SAndrey Vagin }
431a7306ed8SAndrey Vagin 
432bcac25a5SAndrey Vagin static struct user_namespace *pidns_owner(struct ns_common *ns)
433bcac25a5SAndrey Vagin {
434bcac25a5SAndrey Vagin 	return to_pid_ns(ns)->user_ns;
435bcac25a5SAndrey Vagin }
436bcac25a5SAndrey Vagin 
43757e8391dSEric W. Biederman const struct proc_ns_operations pidns_operations = {
43857e8391dSEric W. Biederman 	.name		= "pid",
43957e8391dSEric W. Biederman 	.type		= CLONE_NEWPID,
44057e8391dSEric W. Biederman 	.get		= pidns_get,
44157e8391dSEric W. Biederman 	.put		= pidns_put,
44257e8391dSEric W. Biederman 	.install	= pidns_install,
443bcac25a5SAndrey Vagin 	.owner		= pidns_owner,
444a7306ed8SAndrey Vagin 	.get_parent	= pidns_get_parent,
44557e8391dSEric W. Biederman };
44657e8391dSEric W. Biederman 
447eaa0d190SKirill Tkhai const struct proc_ns_operations pidns_for_children_operations = {
448eaa0d190SKirill Tkhai 	.name		= "pid_for_children",
449eaa0d190SKirill Tkhai 	.real_ns_name	= "pid",
450eaa0d190SKirill Tkhai 	.type		= CLONE_NEWPID,
451eaa0d190SKirill Tkhai 	.get		= pidns_for_children_get,
452eaa0d190SKirill Tkhai 	.put		= pidns_put,
453eaa0d190SKirill Tkhai 	.install	= pidns_install,
454eaa0d190SKirill Tkhai 	.owner		= pidns_owner,
455eaa0d190SKirill Tkhai 	.get_parent	= pidns_get_parent,
456eaa0d190SKirill Tkhai };
457eaa0d190SKirill Tkhai 
45874bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void)
45974bd59bbSPavel Emelyanov {
46074bd59bbSPavel Emelyanov 	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
46198ed57eeSCyrill Gorcunov 
46298ed57eeSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
463b8f566b0SPavel Emelyanov 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
46498ed57eeSCyrill Gorcunov #endif
46574bd59bbSPavel Emelyanov 	return 0;
46674bd59bbSPavel Emelyanov }
46774bd59bbSPavel Emelyanov 
46874bd59bbSPavel Emelyanov __initcall(pid_namespaces_init);
469