^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generic pidhash and scalable, time-bounded PID allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) 2002-2003 Nadia Yvette Chambers, IBM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * (C) 2004 Nadia Yvette Chambers, Oracle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * (C) 2002-2004 Ingo Molnar, Red Hat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * pid-structures are backing objects for tasks sharing a given ID to chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * against. There is very little to them aside from hashing them and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * parking tasks using given ID's on a list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * The hash is always changed with the tasklist_lock write-acquired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * and the hash is only accessed with the tasklist_lock at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * read-acquired, so there's no additional SMP locking needed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * We have a list of bitmap pages, which bitmaps represent the PID space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Allocating and freeing PIDs is completely lockless. The worst-case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * allocation scenario when all but one out of 1 million PIDs possible are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Pid namespaces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Many thanks to Oleg Nesterov for comments and help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/init_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/proc_ns.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <uapi/linux/pidfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct pid init_struct_pid = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .count = REFCOUNT_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .tasks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) { .first = NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { .first = NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { .first = NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .level = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .numbers = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .nr = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .ns = &init_pid_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int pid_max = PID_MAX_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define RESERVED_PIDS 300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int pid_max_min = RESERVED_PIDS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int pid_max_max = PID_MAX_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * PID-map pages start out as NULL, they get allocated upon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * first use and are never deallocated. This way a low pid_max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * value does not cause lots of bitmaps to be allocated, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * the scheme scales to up to 4 million PIDs, runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct pid_namespace init_pid_ns = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .kref = KREF_INIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .idr = IDR_INIT(init_pid_ns.idr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .pid_allocated = PIDNS_ADDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .level = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .child_reaper = &init_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .user_ns = &init_user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .ns.inum = PROC_PID_INIT_INO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #ifdef CONFIG_PID_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .ns.ops = &pidns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) EXPORT_SYMBOL_GPL(init_pid_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Note: disable interrupts while the pidmap_lock is held as an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * interrupt might come in and do read_lock(&tasklist_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * If we don't disable interrupts there is a nasty deadlock between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * detach_pid()->free_pid() and another cpu that does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * spin_lock(&pidmap_lock) followed by an interrupt routine that does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * read_lock(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * After we clean up the tasklist_lock and know there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * irq handlers that take it we can leave the interrupts enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * For now it is easier to be safe than to prove it can't happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) void put_pid(struct pid *pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct pid_namespace *ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ns = pid->numbers[pid->level].ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (refcount_dec_and_test(&pid->count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kmem_cache_free(ns->pid_cachep, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) put_pid_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) EXPORT_SYMBOL_GPL(put_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void delayed_put_pid(struct rcu_head *rhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct pid *pid = container_of(rhp, struct pid, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) put_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) void free_pid(struct pid *pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* We can be called with write_lock_irq(&tasklist_lock) held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) spin_lock_irqsave(&pidmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) for (i = 0; i <= pid->level; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct upid *upid = pid->numbers + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct pid_namespace *ns = upid->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) switch (--ns->pid_allocated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* When all that is left in the pid namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * is the reaper wake up the reaper. The reaper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * may be sleeping in zap_pid_ns_processes().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) wake_up_process(ns->child_reaper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case PIDNS_ADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Handle a fork failure of the first process */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) WARN_ON(ns->child_reaper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ns->pid_allocated = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) idr_remove(&ns->idr, upid->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) spin_unlock_irqrestore(&pidmap_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) call_rcu(&pid->rcu, delayed_put_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) size_t set_tid_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) enum pid_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int i, nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct pid_namespace *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct upid *upid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * set_tid_size contains the size of the set_tid array. Starting at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * the most nested currently active PID namespace it tells alloc_pid()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * which PID to set for a process in that most nested PID namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * up to set_tid_size PID namespaces. It does not have to set the PID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * for a process in all nested PID namespaces but set_tid_size must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * never be greater than the current ns->level + 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (set_tid_size > ns->level + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) tmp = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pid->level = ns->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) for (i = ns->level; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int tid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (set_tid_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) tid = set_tid[ns->level - i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (tid < 1 || tid >= pid_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * Also fail if a PID != 1 is requested and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * no PID 1 exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (tid != 1 && !tmp->child_reaper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) retval = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!checkpoint_restore_ns_capable(tmp->user_ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) set_tid_size--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) spin_lock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (tid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) nr = idr_alloc(&tmp->idr, NULL, tid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) tid + 1, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * If ENOSPC is returned it means that the PID is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * alreay in use. Return EEXIST in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (nr == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) nr = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int pid_min = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * init really needs pid 1, but after reaching the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * maximum wrap back to RESERVED_PIDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) pid_min = RESERVED_PIDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Store a null pointer so find_pid_ns does not find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * a partially initialized PID (see below).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pid_max, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) spin_unlock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (nr < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) retval = (nr == -ENOSPC) ? -EAGAIN : nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pid->numbers[i].nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) pid->numbers[i].ns = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) tmp = tmp->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * ENOMEM is not the most obvious choice especially for the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * where the child subreaper has already exited and the pid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * namespace denies the creation of any new processes. But ENOMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * is what we have exposed to userspace for a long time and it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * documented behavior for pid namespaces. So we can't easily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * change it even if there were an error code better suited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) get_pid_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) refcount_set(&pid->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) spin_lock_init(&pid->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) for (type = 0; type < PIDTYPE_MAX; ++type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) INIT_HLIST_HEAD(&pid->tasks[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) init_waitqueue_head(&pid->wait_pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) INIT_HLIST_HEAD(&pid->inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) upid = pid->numbers + ns->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) spin_lock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!(ns->pid_allocated & PIDNS_ADDING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) for ( ; upid >= pid->numbers; --upid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Make the PID visible to find_pid_ns. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) idr_replace(&upid->ns->idr, pid, upid->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) upid->ns->pid_allocated++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) spin_unlock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) spin_unlock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) put_pid_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) spin_lock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) while (++i <= ns->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) upid = pid->numbers + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) idr_remove(&upid->ns->idr, upid->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* On failure to allocate the first pid, reset the state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (ns->pid_allocated == PIDNS_ADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) idr_set_cursor(&ns->idr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) spin_unlock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) kmem_cache_free(ns->pid_cachep, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) void disable_pid_allocation(struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) spin_lock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ns->pid_allocated &= ~PIDNS_ADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) spin_unlock_irq(&pidmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return idr_find(&ns->idr, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) EXPORT_SYMBOL_GPL(find_pid_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct pid *find_vpid(int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return find_pid_ns(nr, task_active_pid_ns(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL_GPL(find_vpid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return (type == PIDTYPE_PID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) &task->thread_pid :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) &task->signal->pids[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * attach_pid() must be called with the tasklist_lock write-held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) void attach_pid(struct task_struct *task, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct pid *pid = *task_pid_ptr(task, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static void __change_pid(struct task_struct *task, enum pid_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct pid *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct pid **pid_ptr = task_pid_ptr(task, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) pid = *pid_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) hlist_del_rcu(&task->pid_links[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *pid_ptr = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) for (tmp = PIDTYPE_MAX; --tmp >= 0; )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (pid_has_task(pid, tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) free_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void detach_pid(struct task_struct *task, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) __change_pid(task, type, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) void change_pid(struct task_struct *task, enum pid_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct pid *pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) __change_pid(task, type, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) attach_pid(task, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) void exchange_tids(struct task_struct *left, struct task_struct *right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct pid *pid1 = left->thread_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct pid *pid2 = right->thread_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Swap the single entry tid lists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) hlists_swap_heads_rcu(head1, head2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* Swap the per task_struct pid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) rcu_assign_pointer(left->thread_pid, pid2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) rcu_assign_pointer(right->thread_pid, pid1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /* Swap the cached value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) WRITE_ONCE(left->pid, pid_nr(pid2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) WRITE_ONCE(right->pid, pid_nr(pid1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) void transfer_pid(struct task_struct *old, struct task_struct *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (type == PIDTYPE_PID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) new->thread_pid = old->thread_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct task_struct *pid_task(struct pid *pid, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct task_struct *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct hlist_node *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) lockdep_tasklist_lock_is_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) result = hlist_entry(first, struct task_struct, pid_links[(type)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) EXPORT_SYMBOL(pid_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * Must be called under rcu_read_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) "find_task_by_pid_ns() needs rcu_read_lock() protection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct task_struct *find_task_by_vpid(pid_t vnr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) EXPORT_SYMBOL_GPL(find_task_by_vpid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct task_struct *find_get_task_by_vpid(pid_t nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) task = find_task_by_vpid(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) get_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) EXPORT_SYMBOL_GPL(get_task_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct task_struct *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) result = pid_task(pid, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) get_task_struct(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) EXPORT_SYMBOL_GPL(get_pid_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct pid *find_get_pid(pid_t nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) pid = get_pid(find_vpid(nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) EXPORT_SYMBOL_GPL(find_get_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct upid *upid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) pid_t nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (pid && ns->level <= pid->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) upid = &pid->numbers[ns->level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (upid->ns == ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) nr = upid->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) EXPORT_SYMBOL_GPL(pid_nr_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pid_t pid_vnr(struct pid *pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return pid_nr_ns(pid, task_active_pid_ns(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) EXPORT_SYMBOL_GPL(pid_vnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) pid_t nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ns = task_active_pid_ns(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) EXPORT_SYMBOL(__task_pid_nr_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return ns_of_pid(task_pid(tsk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) EXPORT_SYMBOL_GPL(task_active_pid_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Used by proc to find the first pid that is greater than or equal to nr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * If there is a pid at nr this function is exactly the same as find_pid_ns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return idr_get_next(&ns->idr, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) f = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return ERR_PTR(-EBADF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) pid = pidfd_pid(f.file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!IS_ERR(pid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) get_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *flags = f.file->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * pidfd_create() - Create a new pid file descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * @pid: struct pid that the pidfd will reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * @flags: flags to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * This creates a new pid file descriptor with the O_CLOEXEC flag set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * Note, that this function can only be called after the fd table has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * been unshared to avoid leaking the pidfd to the new process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * Return: On success, a cloexec pidfd is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * On error, a negative errno number will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static int pidfd_create(struct pid *pid, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) fd = anon_inode_getfd("[pidfd]", &pidfd_fops, get_pid(pid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) flags | O_RDWR | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) put_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * pidfd_open() - Open new pid file descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * @pid: pid for which to retrieve a pidfd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * @flags: flags to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * This creates a new pid file descriptor with the O_CLOEXEC flag set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * the process identified by @pid. Currently, the process identified by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * @pid must be a thread-group leader. This restriction currently exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * for all aspects of pidfds including pidfd creation (CLONE_PIDFD cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * be used with CLONE_THREAD) and pidfd polling (only supports thread group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * leaders).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * Return: On success, a cloexec pidfd is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * On error, a negative errno number will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) struct pid *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (flags & ~PIDFD_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (pid <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) p = find_get_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (pid_has_task(p, PIDTYPE_TGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) fd = pidfd_create(p, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) fd = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) put_pid(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) void __init pid_idr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* Verify no one has done anything silly: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /* bump default and minimum pid_max based on number of cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) pid_max = min(pid_max_max, max_t(int, pid_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) pid_max_min = max_t(int, pid_max_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) PIDS_PER_CPU_MIN * num_possible_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) idr_init(&init_pid_ns.idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) init_pid_ns.pid_cachep = KMEM_CACHE(pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static struct file *__pidfd_fget(struct task_struct *task, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) ret = down_read_killable(&task->signal->exec_update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) file = fget_task(task, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) file = ERR_PTR(-EPERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) up_read(&task->signal->exec_update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return file ?: ERR_PTR(-EBADF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static int pidfd_getfd(struct pid *pid, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) task = get_pid_task(pid, PIDTYPE_PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) file = __pidfd_fget(task, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (IS_ERR(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ret = receive_fd(file, O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * sys_pidfd_getfd() - Get a file descriptor from another process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * @pidfd: the pidfd file descriptor of the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * @fd: the file descriptor number to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * @flags: flags on how to get the fd (reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * This syscall gets a copy of a file descriptor from another process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * based on the pidfd, and file descriptor number. It requires that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * the calling process has the ability to ptrace the process represented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * by the pidfd. The process which is having its file descriptor copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * is otherwise unaffected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * Return: On success, a cloexec file descriptor is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * On error, a negative errno number will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct pid *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /* flags is currently unused - make sure it's unset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) f = fdget(pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (!f.file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) pid = pidfd_pid(f.file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (IS_ERR(pid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) ret = PTR_ERR(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) ret = pidfd_getfd(pid, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }