xref: /linux/kernel/pid.c (revision 85cdaca6970028bf6f544c355c90035586836ddf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic pidhash and scalable, time-bounded PID allocator
4  *
5  * (C) 2002-2003 Nadia Yvette Chambers, IBM
6  * (C) 2004 Nadia Yvette Chambers, Oracle
7  * (C) 2002-2004 Ingo Molnar, Red Hat
8  *
9  * pid-structures are backing objects for tasks sharing a given ID to chain
10  * against. There is very little to them aside from hashing them and
11  * parking tasks using given ID's on a list.
12  *
13  * The hash is always changed with the tasklist_lock write-acquired,
14  * and the hash is only accessed with the tasklist_lock at least
15  * read-acquired, so there's no additional SMP locking needed here.
16  *
17  * We have a list of bitmap pages, which bitmaps represent the PID space.
18  * Allocating and freeing PIDs is completely lockless. The worst-case
19  * allocation scenario when all but one out of 1 million PIDs possible are
20  * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
21  * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
22  *
23  * Pid namespaces:
24  *    (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
25  *    (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
26  *     Many thanks to Oleg Nesterov for comments and help
27  *
28  */
29 
30 #include <linux/mm.h>
31 #include <linux/export.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/rculist.h>
35 #include <linux/memblock.h>
36 #include <linux/pid_namespace.h>
37 #include <linux/init_task.h>
38 #include <linux/syscalls.h>
39 #include <linux/proc_ns.h>
40 #include <linux/refcount.h>
41 #include <linux/anon_inodes.h>
42 #include <linux/sched/signal.h>
43 #include <linux/sched/task.h>
44 #include <linux/idr.h>
45 #include <linux/pidfs.h>
46 #include <net/sock.h>
47 #include <uapi/linux/pidfd.h>
48 
49 struct pid init_struct_pid = {
50 	.count		= REFCOUNT_INIT(1),
51 	.tasks		= {
52 		{ .first = NULL },
53 		{ .first = NULL },
54 		{ .first = NULL },
55 	},
56 	.level		= 0,
57 	.numbers	= { {
58 		.nr		= 0,
59 		.ns		= &init_pid_ns,
60 	}, }
61 };
62 
63 static int pid_max_min = RESERVED_PIDS + 1;
64 static int pid_max_max = PID_MAX_LIMIT;
65 
66 /*
67  * PID-map pages start out as NULL, they get allocated upon
68  * first use and are never deallocated. This way a low pid_max
69  * value does not cause lots of bitmaps to be allocated, but
70  * the scheme scales to up to 4 million PIDs, runtime.
71  */
72 struct pid_namespace init_pid_ns = {
73 	.ns = NS_COMMON_INIT(init_pid_ns),
74 	.idr = IDR_INIT(init_pid_ns.idr),
75 	.pid_allocated = PIDNS_ADDING,
76 	.level = 0,
77 	.child_reaper = &init_task,
78 	.user_ns = &init_user_ns,
79 	.pid_max = PID_MAX_DEFAULT,
80 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
81 	.memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC,
82 #endif
83 };
84 EXPORT_SYMBOL_GPL(init_pid_ns);
85 
86 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
87 
88 void put_pid(struct pid *pid)
89 {
90 	struct pid_namespace *ns;
91 
92 	if (!pid)
93 		return;
94 
95 	ns = pid->numbers[pid->level].ns;
96 	if (refcount_dec_and_test(&pid->count)) {
97 		pidfs_free_pid(pid);
98 		kmem_cache_free(ns->pid_cachep, pid);
99 		put_pid_ns(ns);
100 	}
101 }
102 EXPORT_SYMBOL_GPL(put_pid);
103 
104 static void delayed_put_pid(struct rcu_head *rhp)
105 {
106 	struct pid *pid = container_of(rhp, struct pid, rcu);
107 	put_pid(pid);
108 }
109 
110 void free_pid(struct pid *pid)
111 {
112 	int i;
113 	struct pid_namespace *active_ns;
114 
115 	lockdep_assert_not_held(&tasklist_lock);
116 
117 	active_ns = pid->numbers[pid->level].ns;
118 	ns_ref_active_put(active_ns);
119 
120 	spin_lock(&pidmap_lock);
121 	for (i = 0; i <= pid->level; i++) {
122 		struct upid *upid = pid->numbers + i;
123 		struct pid_namespace *ns = upid->ns;
124 		switch (--ns->pid_allocated) {
125 		case 2:
126 		case 1:
127 			/* When all that is left in the pid namespace
128 			 * is the reaper wake up the reaper.  The reaper
129 			 * may be sleeping in zap_pid_ns_processes().
130 			 */
131 			wake_up_process(READ_ONCE(ns->child_reaper));
132 			break;
133 		case PIDNS_ADDING:
134 			/* Only possible if the 1st fork fails */
135 			WARN_ON(READ_ONCE(ns->child_reaper));
136 			break;
137 		}
138 
139 		idr_remove(&ns->idr, upid->nr);
140 	}
141 	spin_unlock(&pidmap_lock);
142 
143 	pidfs_remove_pid(pid);
144 	call_rcu(&pid->rcu, delayed_put_pid);
145 }
146 
147 void free_pids(struct pid **pids)
148 {
149 	int tmp;
150 
151 	/*
152 	 * This can batch pidmap_lock.
153 	 */
154 	for (tmp = PIDTYPE_MAX; --tmp >= 0; )
155 		if (pids[tmp])
156 			free_pid(pids[tmp]);
157 }
158 
159 struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,
160 		      size_t arg_set_tid_size)
161 {
162 	int set_tid[MAX_PID_NS_LEVEL + 1] = {};
163 	int pid_max[MAX_PID_NS_LEVEL + 1] = {};
164 	struct pid *pid;
165 	enum pid_type type;
166 	int i, nr;
167 	struct pid_namespace *tmp;
168 	struct upid *upid;
169 	int retval = -ENOMEM;
170 	bool retried_preload;
171 
172 	/*
173 	 * arg_set_tid_size contains the size of the arg_set_tid array. Starting at
174 	 * the most nested currently active PID namespace it tells alloc_pid()
175 	 * which PID to set for a process in that most nested PID namespace
176 	 * up to arg_set_tid_size PID namespaces. It does not have to set the PID
177 	 * for a process in all nested PID namespaces but arg_set_tid_size must
178 	 * never be greater than the current ns->level + 1.
179 	 */
180 	if (arg_set_tid_size > ns->level + 1)
181 		return ERR_PTR(-EINVAL);
182 
183 	/*
184 	 * Prep before we take locks:
185 	 *
186 	 * 1. allocate and fill in pid struct
187 	 */
188 	pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
189 	if (!pid)
190 		return ERR_PTR(retval);
191 
192 	get_pid_ns(ns);
193 	pid->level = ns->level;
194 	refcount_set(&pid->count, 1);
195 	spin_lock_init(&pid->lock);
196 	for (type = 0; type < PIDTYPE_MAX; ++type)
197 		INIT_HLIST_HEAD(&pid->tasks[type]);
198 	init_waitqueue_head(&pid->wait_pidfd);
199 	INIT_HLIST_HEAD(&pid->inodes);
200 	pidfs_prepare_pid(pid);
201 
202 	/*
203 	 * 2. perm check checkpoint_restore_ns_capable()
204 	 *
205 	 * This stores found pid_max to make sure the used value is the same should
206 	 * later code need it.
207 	 */
208 	for (tmp = ns, i = ns->level; i >= 0; i--) {
209 		pid_max[ns->level - i] = READ_ONCE(tmp->pid_max);
210 
211 		if (arg_set_tid_size) {
212 			int tid = set_tid[ns->level - i] = arg_set_tid[ns->level - i];
213 
214 			retval = -EINVAL;
215 			if (tid < 1 || tid >= pid_max[ns->level - i])
216 				goto out_abort;
217 			retval = -EPERM;
218 			if (!checkpoint_restore_ns_capable(tmp->user_ns))
219 				goto out_abort;
220 			arg_set_tid_size--;
221 		}
222 
223 		tmp = tmp->parent;
224 	}
225 
226 	/*
227 	 * Prep is done, id allocation goes here:
228 	 */
229 	retried_preload = false;
230 	idr_preload(GFP_KERNEL);
231 	spin_lock(&pidmap_lock);
232 	/* For the case when the previous attempt to create init failed */
233 	if (ns->pid_allocated == PIDNS_ADDING)
234 		idr_set_cursor(&ns->idr, 0);
235 
236 	for (tmp = ns, i = ns->level; i >= 0;) {
237 		int tid = set_tid[ns->level - i];
238 
239 		if (tid) {
240 			nr = idr_alloc(&tmp->idr, NULL, tid,
241 				       tid + 1, GFP_ATOMIC);
242 			/*
243 			 * If ENOSPC is returned it means that the PID is
244 			 * alreay in use. Return EEXIST in that case.
245 			 */
246 			if (nr == -ENOSPC)
247 
248 				nr = -EEXIST;
249 		} else {
250 			int pid_min = 1;
251 			/*
252 			 * init really needs pid 1, but after reaching the
253 			 * maximum wrap back to RESERVED_PIDS
254 			 */
255 			if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
256 				pid_min = RESERVED_PIDS;
257 
258 			/*
259 			 * Store a null pointer so find_pid_ns does not find
260 			 * a partially initialized PID (see below).
261 			 */
262 			nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
263 					      pid_max[ns->level - i], GFP_ATOMIC);
264 			if (nr == -ENOSPC)
265 				nr = -EAGAIN;
266 		}
267 
268 		if (unlikely(nr < 0)) {
269 			/*
270 			 * Preload more memory if idr_alloc{,cyclic} failed with -ENOMEM.
271 			 *
272 			 * The IDR API only allows us to preload memory for one call, while we may end
273 			 * up doing several under pidmap_lock with GFP_ATOMIC. The situation may be
274 			 * salvageable with GFP_KERNEL. But make sure to not loop indefinitely if preload
275 			 * did not help (the routine unfortunately returns void, so we have no idea
276 			 * if it got anywhere).
277 			 *
278 			 * The lock can be safely dropped and picked up as historically pid allocation
279 			 * for different namespaces was *not* atomic -- we try to hold on to it the
280 			 * entire time only for performance reasons.
281 			 */
282 			if (nr == -ENOMEM && !retried_preload) {
283 				spin_unlock(&pidmap_lock);
284 				idr_preload_end();
285 				retried_preload = true;
286 				idr_preload(GFP_KERNEL);
287 				spin_lock(&pidmap_lock);
288 				continue;
289 			}
290 			retval = nr;
291 			goto out_free;
292 		}
293 
294 		pid->numbers[i].nr = nr;
295 		pid->numbers[i].ns = tmp;
296 		i--;
297 		retried_preload = false;
298 
299 		/*
300 		 * PID 1 (init) must be created first.
301 		 */
302 		if (!READ_ONCE(tmp->child_reaper) && nr != 1) {
303 			retval = -EINVAL;
304 			goto out_free;
305 		}
306 
307 		tmp = tmp->parent;
308 	}
309 
310 	/*
311 	 * ENOMEM is not the most obvious choice especially for the case
312 	 * where the child subreaper has already exited and the pid
313 	 * namespace denies the creation of any new processes. But ENOMEM
314 	 * is what we have exposed to userspace for a long time and it is
315 	 * documented behavior for pid namespaces. So we can't easily
316 	 * change it even if there were an error code better suited.
317 	 *
318 	 * This can't be done earlier because we need to preserve other
319 	 * error conditions.
320 	 *
321 	 * We need this even if copy_process() does the same check. If two
322 	 * or more tasks from parent namespace try to inject a child into a
323 	 * dead namespace, one of free_pid() calls from the copy_process()
324 	 * error path may try to wakeup the possibly freed ns->child_reaper.
325 	 */
326 	retval = -ENOMEM;
327 	for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid)
328 		if (unlikely(!(upid->ns->pid_allocated & PIDNS_ADDING)))
329 			goto out_free;
330 
331 	for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {
332 		/* Make the PID visible to find_pid_ns. */
333 		idr_replace(&upid->ns->idr, pid, upid->nr);
334 		upid->ns->pid_allocated++;
335 	}
336 	spin_unlock(&pidmap_lock);
337 	idr_preload_end();
338 	ns_ref_active_get(ns);
339 
340 	retval = pidfs_add_pid(pid);
341 	if (unlikely(retval)) {
342 		free_pid(pid);
343 		pid = ERR_PTR(-ENOMEM);
344 	}
345 
346 	return pid;
347 
348 out_free:
349 	while (++i <= ns->level) {
350 		upid = pid->numbers + i;
351 		idr_remove(&upid->ns->idr, upid->nr);
352 	}
353 
354 	spin_unlock(&pidmap_lock);
355 	idr_preload_end();
356 
357 out_abort:
358 	put_pid_ns(ns);
359 	kmem_cache_free(ns->pid_cachep, pid);
360 	return ERR_PTR(retval);
361 }
362 
363 void disable_pid_allocation(struct pid_namespace *ns)
364 {
365 	spin_lock(&pidmap_lock);
366 	ns->pid_allocated &= ~PIDNS_ADDING;
367 	spin_unlock(&pidmap_lock);
368 }
369 
370 struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
371 {
372 	return idr_find(&ns->idr, nr);
373 }
374 EXPORT_SYMBOL_GPL(find_pid_ns);
375 
376 struct pid *find_vpid(int nr)
377 {
378 	return find_pid_ns(nr, task_active_pid_ns(current));
379 }
380 EXPORT_SYMBOL_GPL(find_vpid);
381 
382 static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
383 {
384 	return (type == PIDTYPE_PID) ?
385 		&task->thread_pid :
386 		&task->signal->pids[type];
387 }
388 
389 /*
390  * attach_pid() must be called with the tasklist_lock write-held.
391  */
392 void attach_pid(struct task_struct *task, enum pid_type type)
393 {
394 	struct pid *pid;
395 
396 	lockdep_assert_held_write(&tasklist_lock);
397 
398 	pid = *task_pid_ptr(task, type);
399 	hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
400 }
401 
402 static void __change_pid(struct pid **pids, struct task_struct *task,
403 			 enum pid_type type, struct pid *new)
404 {
405 	struct pid **pid_ptr, *pid;
406 	int tmp;
407 
408 	lockdep_assert_held_write(&tasklist_lock);
409 
410 	pid_ptr = task_pid_ptr(task, type);
411 	pid = *pid_ptr;
412 
413 	hlist_del_rcu(&task->pid_links[type]);
414 	*pid_ptr = new;
415 
416 	for (tmp = PIDTYPE_MAX; --tmp >= 0; )
417 		if (pid_has_task(pid, tmp))
418 			return;
419 
420 	WARN_ON(pids[type]);
421 	pids[type] = pid;
422 }
423 
424 void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type)
425 {
426 	__change_pid(pids, task, type, NULL);
427 }
428 
429 void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type,
430 		struct pid *pid)
431 {
432 	__change_pid(pids, task, type, pid);
433 	attach_pid(task, type);
434 }
435 
436 void exchange_tids(struct task_struct *left, struct task_struct *right)
437 {
438 	struct pid *pid1 = left->thread_pid;
439 	struct pid *pid2 = right->thread_pid;
440 	struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID];
441 	struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID];
442 
443 	lockdep_assert_held_write(&tasklist_lock);
444 
445 	/* Swap the single entry tid lists */
446 	hlists_swap_heads_rcu(head1, head2);
447 
448 	/* Swap the per task_struct pid */
449 	rcu_assign_pointer(left->thread_pid, pid2);
450 	rcu_assign_pointer(right->thread_pid, pid1);
451 
452 	/* Swap the cached value */
453 	WRITE_ONCE(left->pid, pid_nr(pid2));
454 	WRITE_ONCE(right->pid, pid_nr(pid1));
455 }
456 
457 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
458 void transfer_pid(struct task_struct *old, struct task_struct *new,
459 			   enum pid_type type)
460 {
461 	WARN_ON_ONCE(type == PIDTYPE_PID);
462 	lockdep_assert_held_write(&tasklist_lock);
463 	hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
464 }
465 
466 struct task_struct *pid_task(struct pid *pid, enum pid_type type)
467 {
468 	struct task_struct *result = NULL;
469 	if (pid) {
470 		struct hlist_node *first;
471 		first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
472 					      lockdep_tasklist_lock_is_held());
473 		if (first)
474 			result = hlist_entry(first, struct task_struct, pid_links[(type)]);
475 	}
476 	return result;
477 }
478 EXPORT_SYMBOL(pid_task);
479 
480 /*
481  * Must be called under rcu_read_lock().
482  */
483 struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
484 {
485 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
486 			 "find_task_by_pid_ns() needs rcu_read_lock() protection");
487 	return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
488 }
489 
490 struct task_struct *find_task_by_vpid(pid_t vnr)
491 {
492 	return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
493 }
494 
495 struct task_struct *find_get_task_by_vpid(pid_t nr)
496 {
497 	struct task_struct *task;
498 
499 	rcu_read_lock();
500 	task = find_task_by_vpid(nr);
501 	if (task)
502 		get_task_struct(task);
503 	rcu_read_unlock();
504 
505 	return task;
506 }
507 
508 struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
509 {
510 	struct pid *pid;
511 	rcu_read_lock();
512 	pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
513 	rcu_read_unlock();
514 	return pid;
515 }
516 EXPORT_SYMBOL_GPL(get_task_pid);
517 
518 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
519 {
520 	struct task_struct *result;
521 	rcu_read_lock();
522 	result = pid_task(pid, type);
523 	if (result)
524 		get_task_struct(result);
525 	rcu_read_unlock();
526 	return result;
527 }
528 EXPORT_SYMBOL_GPL(get_pid_task);
529 
530 struct pid *find_get_pid(pid_t nr)
531 {
532 	struct pid *pid;
533 
534 	rcu_read_lock();
535 	pid = get_pid(find_vpid(nr));
536 	rcu_read_unlock();
537 
538 	return pid;
539 }
540 EXPORT_SYMBOL_GPL(find_get_pid);
541 
542 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
543 {
544 	struct upid *upid;
545 	pid_t nr = 0;
546 
547 	if (pid && ns && ns->level <= pid->level) {
548 		upid = &pid->numbers[ns->level];
549 		if (upid->ns == ns)
550 			nr = upid->nr;
551 	}
552 	return nr;
553 }
554 EXPORT_SYMBOL_GPL(pid_nr_ns);
555 
556 pid_t pid_vnr(struct pid *pid)
557 {
558 	return pid_nr_ns(pid, task_active_pid_ns(current));
559 }
560 EXPORT_SYMBOL_GPL(pid_vnr);
561 
562 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
563 			struct pid_namespace *ns)
564 {
565 	pid_t nr = 0;
566 
567 	rcu_read_lock();
568 	if (!ns)
569 		ns = task_active_pid_ns(current);
570 	nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
571 	rcu_read_unlock();
572 
573 	return nr;
574 }
575 EXPORT_SYMBOL(__task_pid_nr_ns);
576 
577 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
578 {
579 	return ns_of_pid(task_pid(tsk));
580 }
581 EXPORT_SYMBOL_GPL(task_active_pid_ns);
582 
583 /*
584  * Used by proc to find the first pid that is greater than or equal to nr.
585  *
586  * If there is a pid at nr this function is exactly the same as find_pid_ns.
587  */
588 struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
589 {
590 	return idr_get_next(&ns->idr, &nr);
591 }
592 EXPORT_SYMBOL_GPL(find_ge_pid);
593 
594 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
595 {
596 	CLASS(fd, f)(fd);
597 	struct pid *pid;
598 
599 	if (fd_empty(f))
600 		return ERR_PTR(-EBADF);
601 
602 	pid = pidfd_pid(fd_file(f));
603 	if (!IS_ERR(pid)) {
604 		get_pid(pid);
605 		*flags = fd_file(f)->f_flags;
606 	}
607 	return pid;
608 }
609 
610 /**
611  * pidfd_get_task() - Get the task associated with a pidfd
612  *
613  * @pidfd: pidfd for which to get the task
614  * @flags: flags associated with this pidfd
615  *
616  * Return the task associated with @pidfd. The function takes a reference on
617  * the returned task. The caller is responsible for releasing that reference.
618  *
619  * Return: On success, the task_struct associated with the pidfd.
620  *	   On error, a negative errno number will be returned.
621  */
622 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
623 {
624 	unsigned int f_flags = 0;
625 	struct pid *pid;
626 	struct task_struct *task;
627 	enum pid_type type;
628 
629 	switch (pidfd) {
630 	case  PIDFD_SELF_THREAD:
631 		type = PIDTYPE_PID;
632 		pid = get_task_pid(current, type);
633 		break;
634 	case  PIDFD_SELF_THREAD_GROUP:
635 		type = PIDTYPE_TGID;
636 		pid = get_task_pid(current, type);
637 		break;
638 	default:
639 		pid = pidfd_get_pid(pidfd, &f_flags);
640 		if (IS_ERR(pid))
641 			return ERR_CAST(pid);
642 		type = PIDTYPE_TGID;
643 		break;
644 	}
645 
646 	task = get_pid_task(pid, type);
647 	put_pid(pid);
648 	if (!task)
649 		return ERR_PTR(-ESRCH);
650 
651 	*flags = f_flags;
652 	return task;
653 }
654 
655 /**
656  * pidfd_create() - Create a new pid file descriptor.
657  *
658  * @pid:   struct pid that the pidfd will reference
659  * @flags: flags to pass
660  *
661  * This creates a new pid file descriptor with the O_CLOEXEC flag set.
662  *
663  * Note, that this function can only be called after the fd table has
664  * been unshared to avoid leaking the pidfd to the new process.
665  *
666  * This symbol should not be explicitly exported to loadable modules.
667  *
668  * Return: On success, a cloexec pidfd is returned.
669  *         On error, a negative errno number will be returned.
670  */
671 static int pidfd_create(struct pid *pid, unsigned int flags)
672 {
673 	int pidfd;
674 	struct file *pidfd_file;
675 
676 	pidfd = pidfd_prepare(pid, flags, &pidfd_file);
677 	if (pidfd < 0)
678 		return pidfd;
679 
680 	fd_install(pidfd, pidfd_file);
681 	return pidfd;
682 }
683 
684 /**
685  * sys_pidfd_open() - Open new pid file descriptor.
686  *
687  * @pid:   pid for which to retrieve a pidfd
688  * @flags: flags to pass
689  *
690  * This creates a new pid file descriptor with the O_CLOEXEC flag set for
691  * the task identified by @pid. Without PIDFD_THREAD flag the target task
692  * must be a thread-group leader.
693  *
694  * Return: On success, a cloexec pidfd is returned.
695  *         On error, a negative errno number will be returned.
696  */
697 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
698 {
699 	int fd;
700 	struct pid *p;
701 
702 	if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD))
703 		return -EINVAL;
704 
705 	if (pid <= 0)
706 		return -EINVAL;
707 
708 	p = find_get_pid(pid);
709 	if (!p)
710 		return -ESRCH;
711 
712 	fd = pidfd_create(p, flags);
713 
714 	put_pid(p);
715 	return fd;
716 }
717 
718 #ifdef CONFIG_SYSCTL
719 static struct ctl_table_set *pid_table_root_lookup(struct ctl_table_root *root)
720 {
721 	return &task_active_pid_ns(current)->set;
722 }
723 
724 static int set_is_seen(struct ctl_table_set *set)
725 {
726 	return &task_active_pid_ns(current)->set == set;
727 }
728 
729 static int pid_table_root_permissions(struct ctl_table_header *head,
730 				      const struct ctl_table *table)
731 {
732 	struct pid_namespace *pidns =
733 		container_of(head->set, struct pid_namespace, set);
734 	int mode = table->mode;
735 
736 	if (ns_capable_noaudit(pidns->user_ns, CAP_SYS_ADMIN) ||
737 	    uid_eq(current_euid(), make_kuid(pidns->user_ns, 0)))
738 		mode = (mode & S_IRWXU) >> 6;
739 	else if (in_egroup_p(make_kgid(pidns->user_ns, 0)))
740 		mode = (mode & S_IRWXG) >> 3;
741 	else
742 		mode = mode & S_IROTH;
743 	return (mode << 6) | (mode << 3) | mode;
744 }
745 
746 static void pid_table_root_set_ownership(struct ctl_table_header *head,
747 					 kuid_t *uid, kgid_t *gid)
748 {
749 	struct pid_namespace *pidns =
750 		container_of(head->set, struct pid_namespace, set);
751 	kuid_t ns_root_uid;
752 	kgid_t ns_root_gid;
753 
754 	ns_root_uid = make_kuid(pidns->user_ns, 0);
755 	if (uid_valid(ns_root_uid))
756 		*uid = ns_root_uid;
757 
758 	ns_root_gid = make_kgid(pidns->user_ns, 0);
759 	if (gid_valid(ns_root_gid))
760 		*gid = ns_root_gid;
761 }
762 
763 static struct ctl_table_root pid_table_root = {
764 	.lookup		= pid_table_root_lookup,
765 	.permissions	= pid_table_root_permissions,
766 	.set_ownership	= pid_table_root_set_ownership,
767 };
768 
769 static int proc_do_cad_pid(const struct ctl_table *table, int write, void *buffer,
770 		size_t *lenp, loff_t *ppos)
771 {
772 	struct pid *new_pid;
773 	pid_t tmp_pid;
774 	int r;
775 	struct ctl_table tmp_table = *table;
776 
777 	tmp_pid = pid_vnr(cad_pid);
778 	tmp_table.data = &tmp_pid;
779 
780 	r = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
781 	if (r || !write)
782 		return r;
783 
784 	new_pid = find_get_pid(tmp_pid);
785 	if (!new_pid)
786 		return -ESRCH;
787 
788 	put_pid(xchg(&cad_pid, new_pid));
789 	return 0;
790 }
791 
792 static const struct ctl_table pid_table[] = {
793 	{
794 		.procname	= "pid_max",
795 		.data		= &init_pid_ns.pid_max,
796 		.maxlen		= sizeof(int),
797 		.mode		= 0644,
798 		.proc_handler	= proc_dointvec_minmax,
799 		.extra1		= &pid_max_min,
800 		.extra2		= &pid_max_max,
801 	},
802 #ifdef CONFIG_PROC_SYSCTL
803 	{
804 		.procname	= "cad_pid",
805 		.maxlen		= sizeof(int),
806 		.mode		= 0600,
807 		.proc_handler	= proc_do_cad_pid,
808 	},
809 #endif
810 };
811 #endif
812 
813 int register_pidns_sysctls(struct pid_namespace *pidns)
814 {
815 #ifdef CONFIG_SYSCTL
816 	struct ctl_table *tbl;
817 
818 	setup_sysctl_set(&pidns->set, &pid_table_root, set_is_seen);
819 
820 	tbl = kmemdup(pid_table, sizeof(pid_table), GFP_KERNEL);
821 	if (!tbl)
822 		return -ENOMEM;
823 	tbl->data = &pidns->pid_max;
824 	pidns->pid_max = min(pid_max_max, max_t(int, pidns->pid_max,
825 			     PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
826 
827 	pidns->sysctls = __register_sysctl_table(&pidns->set, "kernel", tbl,
828 						 ARRAY_SIZE(pid_table));
829 	if (!pidns->sysctls) {
830 		kfree(tbl);
831 		retire_sysctl_set(&pidns->set);
832 		return -ENOMEM;
833 	}
834 #endif
835 	return 0;
836 }
837 
838 void unregister_pidns_sysctls(struct pid_namespace *pidns)
839 {
840 #ifdef CONFIG_SYSCTL
841 	const struct ctl_table *tbl;
842 
843 	tbl = pidns->sysctls->ctl_table_arg;
844 	unregister_sysctl_table(pidns->sysctls);
845 	retire_sysctl_set(&pidns->set);
846 	kfree(tbl);
847 #endif
848 }
849 
850 void __init pid_idr_init(void)
851 {
852 	/* Verify no one has done anything silly: */
853 	BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
854 
855 	/* bump default and minimum pid_max based on number of cpus */
856 	init_pid_ns.pid_max = min(pid_max_max, max_t(int, init_pid_ns.pid_max,
857 				  PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
858 	pid_max_min = max_t(int, pid_max_min,
859 				PIDS_PER_CPU_MIN * num_possible_cpus());
860 	pr_info("pid_max: default: %u minimum: %u\n", init_pid_ns.pid_max, pid_max_min);
861 
862 	idr_init(&init_pid_ns.idr);
863 
864 	init_pid_ns.pid_cachep = kmem_cache_create("pid",
865 			struct_size_t(struct pid, numbers, 1),
866 			__alignof__(struct pid),
867 			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT,
868 			NULL);
869 }
870 
871 static __init int pid_namespace_sysctl_init(void)
872 {
873 #ifdef CONFIG_SYSCTL
874 	/* "kernel" directory will have already been initialized. */
875 	BUG_ON(register_pidns_sysctls(&init_pid_ns));
876 #endif
877 	return 0;
878 }
879 subsys_initcall(pid_namespace_sysctl_init);
880 
881 static struct file *__pidfd_fget(struct task_struct *task, int fd)
882 {
883 	struct file *file;
884 	int ret;
885 
886 	ret = down_read_killable(&task->signal->exec_update_lock);
887 	if (ret)
888 		return ERR_PTR(ret);
889 
890 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS))
891 		file = ERR_PTR(-EPERM);
892 	else if (task->flags & PF_EXITING)
893 		file = ERR_PTR(-ESRCH);
894 	else
895 		file = fget_task(task, fd);
896 
897 	up_read(&task->signal->exec_update_lock);
898 
899 	if (!file) {
900 		/*
901 		 * It is possible that the target thread is exiting; it can be
902 		 * either:
903 		 * 1. before exit_signals(), which gives a real fd
904 		 * 2. before exit_files() takes the task_lock() gives a real fd
905 		 * 3. after exit_files() releases task_lock(), ->files is NULL;
906 		 *    this has PF_EXITING, since it was set in exit_signals(),
907 		 *    __pidfd_fget() returns EBADF.
908 		 * In case 3 we get EBADF, but that really means ESRCH, since
909 		 * the task is currently exiting and has freed its files
910 		 * struct, so we fix it up.
911 		 */
912 		if (task->flags & PF_EXITING)
913 			file = ERR_PTR(-ESRCH);
914 		else
915 			file = ERR_PTR(-EBADF);
916 	}
917 
918 	return file;
919 }
920 
921 static int pidfd_getfd(struct pid *pid, int fd)
922 {
923 	struct task_struct *task;
924 	struct file *file;
925 	int ret;
926 
927 	task = get_pid_task(pid, PIDTYPE_PID);
928 	if (!task)
929 		return -ESRCH;
930 
931 	file = __pidfd_fget(task, fd);
932 	put_task_struct(task);
933 	if (IS_ERR(file))
934 		return PTR_ERR(file);
935 
936 	ret = receive_fd(file, NULL, O_CLOEXEC);
937 	fput(file);
938 
939 	return ret;
940 }
941 
942 /**
943  * sys_pidfd_getfd() - Get a file descriptor from another process
944  *
945  * @pidfd:	the pidfd file descriptor of the process
946  * @fd:		the file descriptor number to get
947  * @flags:	flags on how to get the fd (reserved)
948  *
949  * This syscall gets a copy of a file descriptor from another process
950  * based on the pidfd, and file descriptor number. It requires that
951  * the calling process has the ability to ptrace the process represented
952  * by the pidfd. The process which is having its file descriptor copied
953  * is otherwise unaffected.
954  *
955  * Return: On success, a cloexec file descriptor is returned.
956  *         On error, a negative errno number will be returned.
957  */
958 SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
959 		unsigned int, flags)
960 {
961 	struct pid *pid;
962 
963 	/* flags is currently unused - make sure it's unset */
964 	if (flags)
965 		return -EINVAL;
966 
967 	CLASS(fd, f)(pidfd);
968 	if (fd_empty(f))
969 		return -EBADF;
970 
971 	pid = pidfd_pid(fd_file(f));
972 	if (IS_ERR(pid))
973 		return PTR_ERR(pid);
974 
975 	return pidfd_getfd(pid, fd);
976 }
977