xref: /linux/kernel/pid.c (revision f6e0a4984c2e7244689ea87b62b433bed9d07e94)
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 int pid_max = PID_MAX_DEFAULT;
64 
65 #define RESERVED_PIDS		300
66 
67 int pid_max_min = RESERVED_PIDS + 1;
68 int pid_max_max = PID_MAX_LIMIT;
69 #ifdef CONFIG_FS_PID
70 /*
71  * Pseudo filesystems start inode numbering after one. We use Reserved
72  * PIDs as a natural offset.
73  */
74 static u64 pidfs_ino = RESERVED_PIDS;
75 #endif
76 
77 /*
78  * PID-map pages start out as NULL, they get allocated upon
79  * first use and are never deallocated. This way a low pid_max
80  * value does not cause lots of bitmaps to be allocated, but
81  * the scheme scales to up to 4 million PIDs, runtime.
82  */
83 struct pid_namespace init_pid_ns = {
84 	.ns.count = REFCOUNT_INIT(2),
85 	.idr = IDR_INIT(init_pid_ns.idr),
86 	.pid_allocated = PIDNS_ADDING,
87 	.level = 0,
88 	.child_reaper = &init_task,
89 	.user_ns = &init_user_ns,
90 	.ns.inum = PROC_PID_INIT_INO,
91 #ifdef CONFIG_PID_NS
92 	.ns.ops = &pidns_operations,
93 #endif
94 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
95 	.memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC,
96 #endif
97 };
98 EXPORT_SYMBOL_GPL(init_pid_ns);
99 
100 /*
101  * Note: disable interrupts while the pidmap_lock is held as an
102  * interrupt might come in and do read_lock(&tasklist_lock).
103  *
104  * If we don't disable interrupts there is a nasty deadlock between
105  * detach_pid()->free_pid() and another cpu that does
106  * spin_lock(&pidmap_lock) followed by an interrupt routine that does
107  * read_lock(&tasklist_lock);
108  *
109  * After we clean up the tasklist_lock and know there are no
110  * irq handlers that take it we can leave the interrupts enabled.
111  * For now it is easier to be safe than to prove it can't happen.
112  */
113 
114 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
115 
116 void put_pid(struct pid *pid)
117 {
118 	struct pid_namespace *ns;
119 
120 	if (!pid)
121 		return;
122 
123 	ns = pid->numbers[pid->level].ns;
124 	if (refcount_dec_and_test(&pid->count)) {
125 		kmem_cache_free(ns->pid_cachep, pid);
126 		put_pid_ns(ns);
127 	}
128 }
129 EXPORT_SYMBOL_GPL(put_pid);
130 
131 static void delayed_put_pid(struct rcu_head *rhp)
132 {
133 	struct pid *pid = container_of(rhp, struct pid, rcu);
134 	put_pid(pid);
135 }
136 
137 void free_pid(struct pid *pid)
138 {
139 	/* We can be called with write_lock_irq(&tasklist_lock) held */
140 	int i;
141 	unsigned long flags;
142 
143 	spin_lock_irqsave(&pidmap_lock, flags);
144 	for (i = 0; i <= pid->level; i++) {
145 		struct upid *upid = pid->numbers + i;
146 		struct pid_namespace *ns = upid->ns;
147 		switch (--ns->pid_allocated) {
148 		case 2:
149 		case 1:
150 			/* When all that is left in the pid namespace
151 			 * is the reaper wake up the reaper.  The reaper
152 			 * may be sleeping in zap_pid_ns_processes().
153 			 */
154 			wake_up_process(ns->child_reaper);
155 			break;
156 		case PIDNS_ADDING:
157 			/* Handle a fork failure of the first process */
158 			WARN_ON(ns->child_reaper);
159 			ns->pid_allocated = 0;
160 			break;
161 		}
162 
163 		idr_remove(&ns->idr, upid->nr);
164 	}
165 	spin_unlock_irqrestore(&pidmap_lock, flags);
166 
167 	call_rcu(&pid->rcu, delayed_put_pid);
168 }
169 
170 struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
171 		      size_t set_tid_size)
172 {
173 	struct pid *pid;
174 	enum pid_type type;
175 	int i, nr;
176 	struct pid_namespace *tmp;
177 	struct upid *upid;
178 	int retval = -ENOMEM;
179 
180 	/*
181 	 * set_tid_size contains the size of the set_tid array. Starting at
182 	 * the most nested currently active PID namespace it tells alloc_pid()
183 	 * which PID to set for a process in that most nested PID namespace
184 	 * up to set_tid_size PID namespaces. It does not have to set the PID
185 	 * for a process in all nested PID namespaces but set_tid_size must
186 	 * never be greater than the current ns->level + 1.
187 	 */
188 	if (set_tid_size > ns->level + 1)
189 		return ERR_PTR(-EINVAL);
190 
191 	pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
192 	if (!pid)
193 		return ERR_PTR(retval);
194 
195 	tmp = ns;
196 	pid->level = ns->level;
197 
198 	for (i = ns->level; i >= 0; i--) {
199 		int tid = 0;
200 
201 		if (set_tid_size) {
202 			tid = set_tid[ns->level - i];
203 
204 			retval = -EINVAL;
205 			if (tid < 1 || tid >= pid_max)
206 				goto out_free;
207 			/*
208 			 * Also fail if a PID != 1 is requested and
209 			 * no PID 1 exists.
210 			 */
211 			if (tid != 1 && !tmp->child_reaper)
212 				goto out_free;
213 			retval = -EPERM;
214 			if (!checkpoint_restore_ns_capable(tmp->user_ns))
215 				goto out_free;
216 			set_tid_size--;
217 		}
218 
219 		idr_preload(GFP_KERNEL);
220 		spin_lock_irq(&pidmap_lock);
221 
222 		if (tid) {
223 			nr = idr_alloc(&tmp->idr, NULL, tid,
224 				       tid + 1, GFP_ATOMIC);
225 			/*
226 			 * If ENOSPC is returned it means that the PID is
227 			 * alreay in use. Return EEXIST in that case.
228 			 */
229 			if (nr == -ENOSPC)
230 				nr = -EEXIST;
231 		} else {
232 			int pid_min = 1;
233 			/*
234 			 * init really needs pid 1, but after reaching the
235 			 * maximum wrap back to RESERVED_PIDS
236 			 */
237 			if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
238 				pid_min = RESERVED_PIDS;
239 
240 			/*
241 			 * Store a null pointer so find_pid_ns does not find
242 			 * a partially initialized PID (see below).
243 			 */
244 			nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
245 					      pid_max, GFP_ATOMIC);
246 		}
247 		spin_unlock_irq(&pidmap_lock);
248 		idr_preload_end();
249 
250 		if (nr < 0) {
251 			retval = (nr == -ENOSPC) ? -EAGAIN : nr;
252 			goto out_free;
253 		}
254 
255 		pid->numbers[i].nr = nr;
256 		pid->numbers[i].ns = tmp;
257 		tmp = tmp->parent;
258 	}
259 
260 	/*
261 	 * ENOMEM is not the most obvious choice especially for the case
262 	 * where the child subreaper has already exited and the pid
263 	 * namespace denies the creation of any new processes. But ENOMEM
264 	 * is what we have exposed to userspace for a long time and it is
265 	 * documented behavior for pid namespaces. So we can't easily
266 	 * change it even if there were an error code better suited.
267 	 */
268 	retval = -ENOMEM;
269 
270 	get_pid_ns(ns);
271 	refcount_set(&pid->count, 1);
272 	spin_lock_init(&pid->lock);
273 	for (type = 0; type < PIDTYPE_MAX; ++type)
274 		INIT_HLIST_HEAD(&pid->tasks[type]);
275 
276 	init_waitqueue_head(&pid->wait_pidfd);
277 	INIT_HLIST_HEAD(&pid->inodes);
278 
279 	upid = pid->numbers + ns->level;
280 	spin_lock_irq(&pidmap_lock);
281 	if (!(ns->pid_allocated & PIDNS_ADDING))
282 		goto out_unlock;
283 #ifdef CONFIG_FS_PID
284 	pid->stashed = NULL;
285 	pid->ino = ++pidfs_ino;
286 #endif
287 	for ( ; upid >= pid->numbers; --upid) {
288 		/* Make the PID visible to find_pid_ns. */
289 		idr_replace(&upid->ns->idr, pid, upid->nr);
290 		upid->ns->pid_allocated++;
291 	}
292 	spin_unlock_irq(&pidmap_lock);
293 
294 	return pid;
295 
296 out_unlock:
297 	spin_unlock_irq(&pidmap_lock);
298 	put_pid_ns(ns);
299 
300 out_free:
301 	spin_lock_irq(&pidmap_lock);
302 	while (++i <= ns->level) {
303 		upid = pid->numbers + i;
304 		idr_remove(&upid->ns->idr, upid->nr);
305 	}
306 
307 	/* On failure to allocate the first pid, reset the state */
308 	if (ns->pid_allocated == PIDNS_ADDING)
309 		idr_set_cursor(&ns->idr, 0);
310 
311 	spin_unlock_irq(&pidmap_lock);
312 
313 	kmem_cache_free(ns->pid_cachep, pid);
314 	return ERR_PTR(retval);
315 }
316 
317 void disable_pid_allocation(struct pid_namespace *ns)
318 {
319 	spin_lock_irq(&pidmap_lock);
320 	ns->pid_allocated &= ~PIDNS_ADDING;
321 	spin_unlock_irq(&pidmap_lock);
322 }
323 
324 struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
325 {
326 	return idr_find(&ns->idr, nr);
327 }
328 EXPORT_SYMBOL_GPL(find_pid_ns);
329 
330 struct pid *find_vpid(int nr)
331 {
332 	return find_pid_ns(nr, task_active_pid_ns(current));
333 }
334 EXPORT_SYMBOL_GPL(find_vpid);
335 
336 static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
337 {
338 	return (type == PIDTYPE_PID) ?
339 		&task->thread_pid :
340 		&task->signal->pids[type];
341 }
342 
343 /*
344  * attach_pid() must be called with the tasklist_lock write-held.
345  */
346 void attach_pid(struct task_struct *task, enum pid_type type)
347 {
348 	struct pid *pid = *task_pid_ptr(task, type);
349 	hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
350 }
351 
352 static void __change_pid(struct task_struct *task, enum pid_type type,
353 			struct pid *new)
354 {
355 	struct pid **pid_ptr = task_pid_ptr(task, type);
356 	struct pid *pid;
357 	int tmp;
358 
359 	pid = *pid_ptr;
360 
361 	hlist_del_rcu(&task->pid_links[type]);
362 	*pid_ptr = new;
363 
364 	if (type == PIDTYPE_PID) {
365 		WARN_ON_ONCE(pid_has_task(pid, PIDTYPE_PID));
366 		wake_up_all(&pid->wait_pidfd);
367 	}
368 
369 	for (tmp = PIDTYPE_MAX; --tmp >= 0; )
370 		if (pid_has_task(pid, tmp))
371 			return;
372 
373 	free_pid(pid);
374 }
375 
376 void detach_pid(struct task_struct *task, enum pid_type type)
377 {
378 	__change_pid(task, type, NULL);
379 }
380 
381 void change_pid(struct task_struct *task, enum pid_type type,
382 		struct pid *pid)
383 {
384 	__change_pid(task, type, pid);
385 	attach_pid(task, type);
386 }
387 
388 void exchange_tids(struct task_struct *left, struct task_struct *right)
389 {
390 	struct pid *pid1 = left->thread_pid;
391 	struct pid *pid2 = right->thread_pid;
392 	struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID];
393 	struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID];
394 
395 	/* Swap the single entry tid lists */
396 	hlists_swap_heads_rcu(head1, head2);
397 
398 	/* Swap the per task_struct pid */
399 	rcu_assign_pointer(left->thread_pid, pid2);
400 	rcu_assign_pointer(right->thread_pid, pid1);
401 
402 	/* Swap the cached value */
403 	WRITE_ONCE(left->pid, pid_nr(pid2));
404 	WRITE_ONCE(right->pid, pid_nr(pid1));
405 }
406 
407 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
408 void transfer_pid(struct task_struct *old, struct task_struct *new,
409 			   enum pid_type type)
410 {
411 	WARN_ON_ONCE(type == PIDTYPE_PID);
412 	hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
413 }
414 
415 struct task_struct *pid_task(struct pid *pid, enum pid_type type)
416 {
417 	struct task_struct *result = NULL;
418 	if (pid) {
419 		struct hlist_node *first;
420 		first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
421 					      lockdep_tasklist_lock_is_held());
422 		if (first)
423 			result = hlist_entry(first, struct task_struct, pid_links[(type)]);
424 	}
425 	return result;
426 }
427 EXPORT_SYMBOL(pid_task);
428 
429 /*
430  * Must be called under rcu_read_lock().
431  */
432 struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
433 {
434 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
435 			 "find_task_by_pid_ns() needs rcu_read_lock() protection");
436 	return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
437 }
438 
439 struct task_struct *find_task_by_vpid(pid_t vnr)
440 {
441 	return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
442 }
443 
444 struct task_struct *find_get_task_by_vpid(pid_t nr)
445 {
446 	struct task_struct *task;
447 
448 	rcu_read_lock();
449 	task = find_task_by_vpid(nr);
450 	if (task)
451 		get_task_struct(task);
452 	rcu_read_unlock();
453 
454 	return task;
455 }
456 
457 struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
458 {
459 	struct pid *pid;
460 	rcu_read_lock();
461 	pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
462 	rcu_read_unlock();
463 	return pid;
464 }
465 EXPORT_SYMBOL_GPL(get_task_pid);
466 
467 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
468 {
469 	struct task_struct *result;
470 	rcu_read_lock();
471 	result = pid_task(pid, type);
472 	if (result)
473 		get_task_struct(result);
474 	rcu_read_unlock();
475 	return result;
476 }
477 EXPORT_SYMBOL_GPL(get_pid_task);
478 
479 struct pid *find_get_pid(pid_t nr)
480 {
481 	struct pid *pid;
482 
483 	rcu_read_lock();
484 	pid = get_pid(find_vpid(nr));
485 	rcu_read_unlock();
486 
487 	return pid;
488 }
489 EXPORT_SYMBOL_GPL(find_get_pid);
490 
491 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
492 {
493 	struct upid *upid;
494 	pid_t nr = 0;
495 
496 	if (pid && ns->level <= pid->level) {
497 		upid = &pid->numbers[ns->level];
498 		if (upid->ns == ns)
499 			nr = upid->nr;
500 	}
501 	return nr;
502 }
503 EXPORT_SYMBOL_GPL(pid_nr_ns);
504 
505 pid_t pid_vnr(struct pid *pid)
506 {
507 	return pid_nr_ns(pid, task_active_pid_ns(current));
508 }
509 EXPORT_SYMBOL_GPL(pid_vnr);
510 
511 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
512 			struct pid_namespace *ns)
513 {
514 	pid_t nr = 0;
515 
516 	rcu_read_lock();
517 	if (!ns)
518 		ns = task_active_pid_ns(current);
519 	nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
520 	rcu_read_unlock();
521 
522 	return nr;
523 }
524 EXPORT_SYMBOL(__task_pid_nr_ns);
525 
526 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
527 {
528 	return ns_of_pid(task_pid(tsk));
529 }
530 EXPORT_SYMBOL_GPL(task_active_pid_ns);
531 
532 /*
533  * Used by proc to find the first pid that is greater than or equal to nr.
534  *
535  * If there is a pid at nr this function is exactly the same as find_pid_ns.
536  */
537 struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
538 {
539 	return idr_get_next(&ns->idr, &nr);
540 }
541 EXPORT_SYMBOL_GPL(find_ge_pid);
542 
543 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
544 {
545 	struct fd f;
546 	struct pid *pid;
547 
548 	f = fdget(fd);
549 	if (!f.file)
550 		return ERR_PTR(-EBADF);
551 
552 	pid = pidfd_pid(f.file);
553 	if (!IS_ERR(pid)) {
554 		get_pid(pid);
555 		*flags = f.file->f_flags;
556 	}
557 
558 	fdput(f);
559 	return pid;
560 }
561 
562 /**
563  * pidfd_get_task() - Get the task associated with a pidfd
564  *
565  * @pidfd: pidfd for which to get the task
566  * @flags: flags associated with this pidfd
567  *
568  * Return the task associated with @pidfd. The function takes a reference on
569  * the returned task. The caller is responsible for releasing that reference.
570  *
571  * Return: On success, the task_struct associated with the pidfd.
572  *	   On error, a negative errno number will be returned.
573  */
574 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
575 {
576 	unsigned int f_flags;
577 	struct pid *pid;
578 	struct task_struct *task;
579 
580 	pid = pidfd_get_pid(pidfd, &f_flags);
581 	if (IS_ERR(pid))
582 		return ERR_CAST(pid);
583 
584 	task = get_pid_task(pid, PIDTYPE_TGID);
585 	put_pid(pid);
586 	if (!task)
587 		return ERR_PTR(-ESRCH);
588 
589 	*flags = f_flags;
590 	return task;
591 }
592 
593 /**
594  * pidfd_create() - Create a new pid file descriptor.
595  *
596  * @pid:   struct pid that the pidfd will reference
597  * @flags: flags to pass
598  *
599  * This creates a new pid file descriptor with the O_CLOEXEC flag set.
600  *
601  * Note, that this function can only be called after the fd table has
602  * been unshared to avoid leaking the pidfd to the new process.
603  *
604  * This symbol should not be explicitly exported to loadable modules.
605  *
606  * Return: On success, a cloexec pidfd is returned.
607  *         On error, a negative errno number will be returned.
608  */
609 static int pidfd_create(struct pid *pid, unsigned int flags)
610 {
611 	int pidfd;
612 	struct file *pidfd_file;
613 
614 	pidfd = pidfd_prepare(pid, flags, &pidfd_file);
615 	if (pidfd < 0)
616 		return pidfd;
617 
618 	fd_install(pidfd, pidfd_file);
619 	return pidfd;
620 }
621 
622 /**
623  * sys_pidfd_open() - Open new pid file descriptor.
624  *
625  * @pid:   pid for which to retrieve a pidfd
626  * @flags: flags to pass
627  *
628  * This creates a new pid file descriptor with the O_CLOEXEC flag set for
629  * the task identified by @pid. Without PIDFD_THREAD flag the target task
630  * must be a thread-group leader.
631  *
632  * Return: On success, a cloexec pidfd is returned.
633  *         On error, a negative errno number will be returned.
634  */
635 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
636 {
637 	int fd;
638 	struct pid *p;
639 
640 	if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD))
641 		return -EINVAL;
642 
643 	if (pid <= 0)
644 		return -EINVAL;
645 
646 	p = find_get_pid(pid);
647 	if (!p)
648 		return -ESRCH;
649 
650 	fd = pidfd_create(p, flags);
651 
652 	put_pid(p);
653 	return fd;
654 }
655 
656 void __init pid_idr_init(void)
657 {
658 	/* Verify no one has done anything silly: */
659 	BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
660 
661 	/* bump default and minimum pid_max based on number of cpus */
662 	pid_max = min(pid_max_max, max_t(int, pid_max,
663 				PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
664 	pid_max_min = max_t(int, pid_max_min,
665 				PIDS_PER_CPU_MIN * num_possible_cpus());
666 	pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
667 
668 	idr_init(&init_pid_ns.idr);
669 
670 	init_pid_ns.pid_cachep = kmem_cache_create("pid",
671 			struct_size_t(struct pid, numbers, 1),
672 			__alignof__(struct pid),
673 			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT,
674 			NULL);
675 }
676 
677 static struct file *__pidfd_fget(struct task_struct *task, int fd)
678 {
679 	struct file *file;
680 	int ret;
681 
682 	ret = down_read_killable(&task->signal->exec_update_lock);
683 	if (ret)
684 		return ERR_PTR(ret);
685 
686 	if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS))
687 		file = fget_task(task, fd);
688 	else
689 		file = ERR_PTR(-EPERM);
690 
691 	up_read(&task->signal->exec_update_lock);
692 
693 	if (!file) {
694 		/*
695 		 * It is possible that the target thread is exiting; it can be
696 		 * either:
697 		 * 1. before exit_signals(), which gives a real fd
698 		 * 2. before exit_files() takes the task_lock() gives a real fd
699 		 * 3. after exit_files() releases task_lock(), ->files is NULL;
700 		 *    this has PF_EXITING, since it was set in exit_signals(),
701 		 *    __pidfd_fget() returns EBADF.
702 		 * In case 3 we get EBADF, but that really means ESRCH, since
703 		 * the task is currently exiting and has freed its files
704 		 * struct, so we fix it up.
705 		 */
706 		if (task->flags & PF_EXITING)
707 			file = ERR_PTR(-ESRCH);
708 		else
709 			file = ERR_PTR(-EBADF);
710 	}
711 
712 	return file;
713 }
714 
715 static int pidfd_getfd(struct pid *pid, int fd)
716 {
717 	struct task_struct *task;
718 	struct file *file;
719 	int ret;
720 
721 	task = get_pid_task(pid, PIDTYPE_PID);
722 	if (!task)
723 		return -ESRCH;
724 
725 	file = __pidfd_fget(task, fd);
726 	put_task_struct(task);
727 	if (IS_ERR(file))
728 		return PTR_ERR(file);
729 
730 	ret = receive_fd(file, NULL, O_CLOEXEC);
731 	fput(file);
732 
733 	return ret;
734 }
735 
736 /**
737  * sys_pidfd_getfd() - Get a file descriptor from another process
738  *
739  * @pidfd:	the pidfd file descriptor of the process
740  * @fd:		the file descriptor number to get
741  * @flags:	flags on how to get the fd (reserved)
742  *
743  * This syscall gets a copy of a file descriptor from another process
744  * based on the pidfd, and file descriptor number. It requires that
745  * the calling process has the ability to ptrace the process represented
746  * by the pidfd. The process which is having its file descriptor copied
747  * is otherwise unaffected.
748  *
749  * Return: On success, a cloexec file descriptor is returned.
750  *         On error, a negative errno number will be returned.
751  */
752 SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
753 		unsigned int, flags)
754 {
755 	struct pid *pid;
756 	struct fd f;
757 	int ret;
758 
759 	/* flags is currently unused - make sure it's unset */
760 	if (flags)
761 		return -EINVAL;
762 
763 	f = fdget(pidfd);
764 	if (!f.file)
765 		return -EBADF;
766 
767 	pid = pidfd_pid(f.file);
768 	if (IS_ERR(pid))
769 		ret = PTR_ERR(pid);
770 	else
771 		ret = pidfd_getfd(pid, fd);
772 
773 	fdput(f);
774 	return ret;
775 }
776