xref: /linux-6.15/kernel/pid.c (revision cb12fd8e)
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 <[email protected]>, OpenVZ, SWsoft Inc.
25  *    (C) 2007 Sukadev Bhattiprolu <[email protected]>, 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->ino = ++pidfs_ino;
285 #endif
286 	for ( ; upid >= pid->numbers; --upid) {
287 		/* Make the PID visible to find_pid_ns. */
288 		idr_replace(&upid->ns->idr, pid, upid->nr);
289 		upid->ns->pid_allocated++;
290 	}
291 	spin_unlock_irq(&pidmap_lock);
292 
293 	return pid;
294 
295 out_unlock:
296 	spin_unlock_irq(&pidmap_lock);
297 	put_pid_ns(ns);
298 
299 out_free:
300 	spin_lock_irq(&pidmap_lock);
301 	while (++i <= ns->level) {
302 		upid = pid->numbers + i;
303 		idr_remove(&upid->ns->idr, upid->nr);
304 	}
305 
306 	/* On failure to allocate the first pid, reset the state */
307 	if (ns->pid_allocated == PIDNS_ADDING)
308 		idr_set_cursor(&ns->idr, 0);
309 
310 	spin_unlock_irq(&pidmap_lock);
311 
312 	kmem_cache_free(ns->pid_cachep, pid);
313 	return ERR_PTR(retval);
314 }
315 
316 void disable_pid_allocation(struct pid_namespace *ns)
317 {
318 	spin_lock_irq(&pidmap_lock);
319 	ns->pid_allocated &= ~PIDNS_ADDING;
320 	spin_unlock_irq(&pidmap_lock);
321 }
322 
323 struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
324 {
325 	return idr_find(&ns->idr, nr);
326 }
327 EXPORT_SYMBOL_GPL(find_pid_ns);
328 
329 struct pid *find_vpid(int nr)
330 {
331 	return find_pid_ns(nr, task_active_pid_ns(current));
332 }
333 EXPORT_SYMBOL_GPL(find_vpid);
334 
335 static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
336 {
337 	return (type == PIDTYPE_PID) ?
338 		&task->thread_pid :
339 		&task->signal->pids[type];
340 }
341 
342 /*
343  * attach_pid() must be called with the tasklist_lock write-held.
344  */
345 void attach_pid(struct task_struct *task, enum pid_type type)
346 {
347 	struct pid *pid = *task_pid_ptr(task, type);
348 	hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
349 }
350 
351 static void __change_pid(struct task_struct *task, enum pid_type type,
352 			struct pid *new)
353 {
354 	struct pid **pid_ptr = task_pid_ptr(task, type);
355 	struct pid *pid;
356 	int tmp;
357 
358 	pid = *pid_ptr;
359 
360 	hlist_del_rcu(&task->pid_links[type]);
361 	*pid_ptr = new;
362 
363 	if (type == PIDTYPE_PID) {
364 		WARN_ON_ONCE(pid_has_task(pid, PIDTYPE_PID));
365 		wake_up_all(&pid->wait_pidfd);
366 	}
367 
368 	for (tmp = PIDTYPE_MAX; --tmp >= 0; )
369 		if (pid_has_task(pid, tmp))
370 			return;
371 
372 	free_pid(pid);
373 }
374 
375 void detach_pid(struct task_struct *task, enum pid_type type)
376 {
377 	__change_pid(task, type, NULL);
378 }
379 
380 void change_pid(struct task_struct *task, enum pid_type type,
381 		struct pid *pid)
382 {
383 	__change_pid(task, type, pid);
384 	attach_pid(task, type);
385 }
386 
387 void exchange_tids(struct task_struct *left, struct task_struct *right)
388 {
389 	struct pid *pid1 = left->thread_pid;
390 	struct pid *pid2 = right->thread_pid;
391 	struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID];
392 	struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID];
393 
394 	/* Swap the single entry tid lists */
395 	hlists_swap_heads_rcu(head1, head2);
396 
397 	/* Swap the per task_struct pid */
398 	rcu_assign_pointer(left->thread_pid, pid2);
399 	rcu_assign_pointer(right->thread_pid, pid1);
400 
401 	/* Swap the cached value */
402 	WRITE_ONCE(left->pid, pid_nr(pid2));
403 	WRITE_ONCE(right->pid, pid_nr(pid1));
404 }
405 
406 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
407 void transfer_pid(struct task_struct *old, struct task_struct *new,
408 			   enum pid_type type)
409 {
410 	WARN_ON_ONCE(type == PIDTYPE_PID);
411 	hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
412 }
413 
414 struct task_struct *pid_task(struct pid *pid, enum pid_type type)
415 {
416 	struct task_struct *result = NULL;
417 	if (pid) {
418 		struct hlist_node *first;
419 		first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
420 					      lockdep_tasklist_lock_is_held());
421 		if (first)
422 			result = hlist_entry(first, struct task_struct, pid_links[(type)]);
423 	}
424 	return result;
425 }
426 EXPORT_SYMBOL(pid_task);
427 
428 /*
429  * Must be called under rcu_read_lock().
430  */
431 struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
432 {
433 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
434 			 "find_task_by_pid_ns() needs rcu_read_lock() protection");
435 	return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
436 }
437 
438 struct task_struct *find_task_by_vpid(pid_t vnr)
439 {
440 	return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
441 }
442 
443 struct task_struct *find_get_task_by_vpid(pid_t nr)
444 {
445 	struct task_struct *task;
446 
447 	rcu_read_lock();
448 	task = find_task_by_vpid(nr);
449 	if (task)
450 		get_task_struct(task);
451 	rcu_read_unlock();
452 
453 	return task;
454 }
455 
456 struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
457 {
458 	struct pid *pid;
459 	rcu_read_lock();
460 	pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
461 	rcu_read_unlock();
462 	return pid;
463 }
464 EXPORT_SYMBOL_GPL(get_task_pid);
465 
466 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
467 {
468 	struct task_struct *result;
469 	rcu_read_lock();
470 	result = pid_task(pid, type);
471 	if (result)
472 		get_task_struct(result);
473 	rcu_read_unlock();
474 	return result;
475 }
476 EXPORT_SYMBOL_GPL(get_pid_task);
477 
478 struct pid *find_get_pid(pid_t nr)
479 {
480 	struct pid *pid;
481 
482 	rcu_read_lock();
483 	pid = get_pid(find_vpid(nr));
484 	rcu_read_unlock();
485 
486 	return pid;
487 }
488 EXPORT_SYMBOL_GPL(find_get_pid);
489 
490 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
491 {
492 	struct upid *upid;
493 	pid_t nr = 0;
494 
495 	if (pid && ns->level <= pid->level) {
496 		upid = &pid->numbers[ns->level];
497 		if (upid->ns == ns)
498 			nr = upid->nr;
499 	}
500 	return nr;
501 }
502 EXPORT_SYMBOL_GPL(pid_nr_ns);
503 
504 pid_t pid_vnr(struct pid *pid)
505 {
506 	return pid_nr_ns(pid, task_active_pid_ns(current));
507 }
508 EXPORT_SYMBOL_GPL(pid_vnr);
509 
510 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
511 			struct pid_namespace *ns)
512 {
513 	pid_t nr = 0;
514 
515 	rcu_read_lock();
516 	if (!ns)
517 		ns = task_active_pid_ns(current);
518 	nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
519 	rcu_read_unlock();
520 
521 	return nr;
522 }
523 EXPORT_SYMBOL(__task_pid_nr_ns);
524 
525 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
526 {
527 	return ns_of_pid(task_pid(tsk));
528 }
529 EXPORT_SYMBOL_GPL(task_active_pid_ns);
530 
531 /*
532  * Used by proc to find the first pid that is greater than or equal to nr.
533  *
534  * If there is a pid at nr this function is exactly the same as find_pid_ns.
535  */
536 struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
537 {
538 	return idr_get_next(&ns->idr, &nr);
539 }
540 EXPORT_SYMBOL_GPL(find_ge_pid);
541 
542 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
543 {
544 	struct fd f;
545 	struct pid *pid;
546 
547 	f = fdget(fd);
548 	if (!f.file)
549 		return ERR_PTR(-EBADF);
550 
551 	pid = pidfd_pid(f.file);
552 	if (!IS_ERR(pid)) {
553 		get_pid(pid);
554 		*flags = f.file->f_flags;
555 	}
556 
557 	fdput(f);
558 	return pid;
559 }
560 
561 /**
562  * pidfd_get_task() - Get the task associated with a pidfd
563  *
564  * @pidfd: pidfd for which to get the task
565  * @flags: flags associated with this pidfd
566  *
567  * Return the task associated with @pidfd. The function takes a reference on
568  * the returned task. The caller is responsible for releasing that reference.
569  *
570  * Return: On success, the task_struct associated with the pidfd.
571  *	   On error, a negative errno number will be returned.
572  */
573 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
574 {
575 	unsigned int f_flags;
576 	struct pid *pid;
577 	struct task_struct *task;
578 
579 	pid = pidfd_get_pid(pidfd, &f_flags);
580 	if (IS_ERR(pid))
581 		return ERR_CAST(pid);
582 
583 	task = get_pid_task(pid, PIDTYPE_TGID);
584 	put_pid(pid);
585 	if (!task)
586 		return ERR_PTR(-ESRCH);
587 
588 	*flags = f_flags;
589 	return task;
590 }
591 
592 /**
593  * pidfd_create() - Create a new pid file descriptor.
594  *
595  * @pid:   struct pid that the pidfd will reference
596  * @flags: flags to pass
597  *
598  * This creates a new pid file descriptor with the O_CLOEXEC flag set.
599  *
600  * Note, that this function can only be called after the fd table has
601  * been unshared to avoid leaking the pidfd to the new process.
602  *
603  * This symbol should not be explicitly exported to loadable modules.
604  *
605  * Return: On success, a cloexec pidfd is returned.
606  *         On error, a negative errno number will be returned.
607  */
608 static int pidfd_create(struct pid *pid, unsigned int flags)
609 {
610 	int pidfd;
611 	struct file *pidfd_file;
612 
613 	pidfd = pidfd_prepare(pid, flags, &pidfd_file);
614 	if (pidfd < 0)
615 		return pidfd;
616 
617 	fd_install(pidfd, pidfd_file);
618 	return pidfd;
619 }
620 
621 /**
622  * sys_pidfd_open() - Open new pid file descriptor.
623  *
624  * @pid:   pid for which to retrieve a pidfd
625  * @flags: flags to pass
626  *
627  * This creates a new pid file descriptor with the O_CLOEXEC flag set for
628  * the task identified by @pid. Without PIDFD_THREAD flag the target task
629  * must be a thread-group leader.
630  *
631  * Return: On success, a cloexec pidfd is returned.
632  *         On error, a negative errno number will be returned.
633  */
634 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
635 {
636 	int fd;
637 	struct pid *p;
638 
639 	if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD))
640 		return -EINVAL;
641 
642 	if (pid <= 0)
643 		return -EINVAL;
644 
645 	p = find_get_pid(pid);
646 	if (!p)
647 		return -ESRCH;
648 
649 	fd = pidfd_create(p, flags);
650 
651 	put_pid(p);
652 	return fd;
653 }
654 
655 void __init pid_idr_init(void)
656 {
657 	/* Verify no one has done anything silly: */
658 	BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
659 
660 	/* bump default and minimum pid_max based on number of cpus */
661 	pid_max = min(pid_max_max, max_t(int, pid_max,
662 				PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
663 	pid_max_min = max_t(int, pid_max_min,
664 				PIDS_PER_CPU_MIN * num_possible_cpus());
665 	pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
666 
667 	idr_init(&init_pid_ns.idr);
668 
669 	init_pid_ns.pid_cachep = kmem_cache_create("pid",
670 			struct_size_t(struct pid, numbers, 1),
671 			__alignof__(struct pid),
672 			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT,
673 			NULL);
674 }
675 
676 static struct file *__pidfd_fget(struct task_struct *task, int fd)
677 {
678 	struct file *file;
679 	int ret;
680 
681 	ret = down_read_killable(&task->signal->exec_update_lock);
682 	if (ret)
683 		return ERR_PTR(ret);
684 
685 	if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS))
686 		file = fget_task(task, fd);
687 	else
688 		file = ERR_PTR(-EPERM);
689 
690 	up_read(&task->signal->exec_update_lock);
691 
692 	if (!file) {
693 		/*
694 		 * It is possible that the target thread is exiting; it can be
695 		 * either:
696 		 * 1. before exit_signals(), which gives a real fd
697 		 * 2. before exit_files() takes the task_lock() gives a real fd
698 		 * 3. after exit_files() releases task_lock(), ->files is NULL;
699 		 *    this has PF_EXITING, since it was set in exit_signals(),
700 		 *    __pidfd_fget() returns EBADF.
701 		 * In case 3 we get EBADF, but that really means ESRCH, since
702 		 * the task is currently exiting and has freed its files
703 		 * struct, so we fix it up.
704 		 */
705 		if (task->flags & PF_EXITING)
706 			file = ERR_PTR(-ESRCH);
707 		else
708 			file = ERR_PTR(-EBADF);
709 	}
710 
711 	return file;
712 }
713 
714 static int pidfd_getfd(struct pid *pid, int fd)
715 {
716 	struct task_struct *task;
717 	struct file *file;
718 	int ret;
719 
720 	task = get_pid_task(pid, PIDTYPE_PID);
721 	if (!task)
722 		return -ESRCH;
723 
724 	file = __pidfd_fget(task, fd);
725 	put_task_struct(task);
726 	if (IS_ERR(file))
727 		return PTR_ERR(file);
728 
729 	ret = receive_fd(file, NULL, O_CLOEXEC);
730 	fput(file);
731 
732 	return ret;
733 }
734 
735 /**
736  * sys_pidfd_getfd() - Get a file descriptor from another process
737  *
738  * @pidfd:	the pidfd file descriptor of the process
739  * @fd:		the file descriptor number to get
740  * @flags:	flags on how to get the fd (reserved)
741  *
742  * This syscall gets a copy of a file descriptor from another process
743  * based on the pidfd, and file descriptor number. It requires that
744  * the calling process has the ability to ptrace the process represented
745  * by the pidfd. The process which is having its file descriptor copied
746  * is otherwise unaffected.
747  *
748  * Return: On success, a cloexec file descriptor is returned.
749  *         On error, a negative errno number will be returned.
750  */
751 SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
752 		unsigned int, flags)
753 {
754 	struct pid *pid;
755 	struct fd f;
756 	int ret;
757 
758 	/* flags is currently unused - make sure it's unset */
759 	if (flags)
760 		return -EINVAL;
761 
762 	f = fdget(pidfd);
763 	if (!f.file)
764 		return -EBADF;
765 
766 	pid = pidfd_pid(f.file);
767 	if (IS_ERR(pid))
768 		ret = PTR_ERR(pid);
769 	else
770 		ret = pidfd_getfd(pid, fd);
771 
772 	fdput(f);
773 	return ret;
774 }
775