Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * Generic pidhash and scalable, time-bounded PID allocator | |
4 | * | |
6d49e352 NYC |
5 | * (C) 2002-2003 Nadia Yvette Chambers, IBM |
6 | * (C) 2004 Nadia Yvette Chambers, Oracle | |
1da177e4 LT |
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). | |
30e49c26 PE |
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 | * | |
1da177e4 LT |
28 | */ |
29 | ||
30 | #include <linux/mm.h> | |
9984de1a | 31 | #include <linux/export.h> |
1da177e4 LT |
32 | #include <linux/slab.h> |
33 | #include <linux/init.h> | |
82524746 | 34 | #include <linux/rculist.h> |
57c8a661 | 35 | #include <linux/memblock.h> |
61a58c6c | 36 | #include <linux/pid_namespace.h> |
820e45db | 37 | #include <linux/init_task.h> |
3eb07c8c | 38 | #include <linux/syscalls.h> |
0bb80f24 | 39 | #include <linux/proc_ns.h> |
f57e515a | 40 | #include <linux/refcount.h> |
32fcb426 CB |
41 | #include <linux/anon_inodes.h> |
42 | #include <linux/sched/signal.h> | |
29930025 | 43 | #include <linux/sched/task.h> |
95846ecf | 44 | #include <linux/idr.h> |
cb12fd8e | 45 | #include <linux/pidfs.h> |
4969f8a0 | 46 | #include <net/sock.h> |
6da73d15 | 47 | #include <uapi/linux/pidfd.h> |
1da177e4 | 48 | |
e1e871af | 49 | struct pid init_struct_pid = { |
f57e515a | 50 | .count = REFCOUNT_INIT(1), |
e1e871af DH |
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 | }; | |
1da177e4 LT |
62 | |
63 | int pid_max = PID_MAX_DEFAULT; | |
1da177e4 LT |
64 | |
65 | #define RESERVED_PIDS 300 | |
66 | ||
67 | int pid_max_min = RESERVED_PIDS + 1; | |
68 | int pid_max_max = PID_MAX_LIMIT; | |
cb12fd8e CB |
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 | |
1da177e4 | 76 | |
1da177e4 LT |
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 | */ | |
61a58c6c | 83 | struct pid_namespace init_pid_ns = { |
8eb71d95 | 84 | .ns.count = REFCOUNT_INIT(2), |
f6bb2a2c | 85 | .idr = IDR_INIT(init_pid_ns.idr), |
e8cfbc24 | 86 | .pid_allocated = PIDNS_ADDING, |
faacbfd3 PE |
87 | .level = 0, |
88 | .child_reaper = &init_task, | |
49f4d8b9 | 89 | .user_ns = &init_user_ns, |
435d5f4b | 90 | .ns.inum = PROC_PID_INIT_INO, |
33c42940 AV |
91 | #ifdef CONFIG_PID_NS |
92 | .ns.ops = &pidns_operations, | |
93 | #endif | |
9876cfe8 AS |
94 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) |
95 | .memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC, | |
96 | #endif | |
3fbc9648 | 97 | }; |
198fe21b | 98 | EXPORT_SYMBOL_GPL(init_pid_ns); |
1da177e4 | 99 | |
92476d7f EB |
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 | */ | |
3fbc9648 | 113 | |
1da177e4 LT |
114 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock); |
115 | ||
7ad5b3a5 | 116 | void put_pid(struct pid *pid) |
92476d7f | 117 | { |
baf8f0f8 PE |
118 | struct pid_namespace *ns; |
119 | ||
92476d7f EB |
120 | if (!pid) |
121 | return; | |
baf8f0f8 | 122 | |
8ef047aa | 123 | ns = pid->numbers[pid->level].ns; |
f57e515a | 124 | if (refcount_dec_and_test(&pid->count)) { |
baf8f0f8 | 125 | kmem_cache_free(ns->pid_cachep, pid); |
b461cc03 | 126 | put_pid_ns(ns); |
8ef047aa | 127 | } |
92476d7f | 128 | } |
bbf73147 | 129 | EXPORT_SYMBOL_GPL(put_pid); |
92476d7f EB |
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 | ||
7ad5b3a5 | 137 | void free_pid(struct pid *pid) |
92476d7f EB |
138 | { |
139 | /* We can be called with write_lock_irq(&tasklist_lock) held */ | |
8ef047aa | 140 | int i; |
92476d7f EB |
141 | unsigned long flags; |
142 | ||
143 | spin_lock_irqsave(&pidmap_lock, flags); | |
0a01f2cc EB |
144 | for (i = 0; i <= pid->level; i++) { |
145 | struct upid *upid = pid->numbers + i; | |
af4b8a83 | 146 | struct pid_namespace *ns = upid->ns; |
e8cfbc24 | 147 | switch (--ns->pid_allocated) { |
a6064885 | 148 | case 2: |
af4b8a83 EB |
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; | |
e8cfbc24 | 156 | case PIDNS_ADDING: |
314a8ad0 ON |
157 | /* Handle a fork failure of the first process */ |
158 | WARN_ON(ns->child_reaper); | |
e8cfbc24 | 159 | ns->pid_allocated = 0; |
af4b8a83 | 160 | break; |
5e1182de | 161 | } |
95846ecf GS |
162 | |
163 | idr_remove(&ns->idr, upid->nr); | |
0a01f2cc | 164 | } |
92476d7f EB |
165 | spin_unlock_irqrestore(&pidmap_lock, flags); |
166 | ||
92476d7f EB |
167 | call_rcu(&pid->rcu, delayed_put_pid); |
168 | } | |
169 | ||
49cb2fc4 AR |
170 | struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, |
171 | size_t set_tid_size) | |
92476d7f EB |
172 | { |
173 | struct pid *pid; | |
174 | enum pid_type type; | |
8ef047aa PE |
175 | int i, nr; |
176 | struct pid_namespace *tmp; | |
198fe21b | 177 | struct upid *upid; |
35f71bc0 | 178 | int retval = -ENOMEM; |
92476d7f | 179 | |
49cb2fc4 AR |
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 | ||
baf8f0f8 | 191 | pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); |
92476d7f | 192 | if (!pid) |
35f71bc0 | 193 | return ERR_PTR(retval); |
92476d7f | 194 | |
8ef047aa | 195 | tmp = ns; |
0a01f2cc | 196 | pid->level = ns->level; |
95846ecf | 197 | |
8ef047aa | 198 | for (i = ns->level; i >= 0; i--) { |
49cb2fc4 AR |
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; | |
1caef81d | 214 | if (!checkpoint_restore_ns_capable(tmp->user_ns)) |
49cb2fc4 AR |
215 | goto out_free; |
216 | set_tid_size--; | |
217 | } | |
95846ecf GS |
218 | |
219 | idr_preload(GFP_KERNEL); | |
220 | spin_lock_irq(&pidmap_lock); | |
221 | ||
49cb2fc4 AR |
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 | } | |
95846ecf GS |
247 | spin_unlock_irq(&pidmap_lock); |
248 | idr_preload_end(); | |
249 | ||
287980e4 | 250 | if (nr < 0) { |
f83606f5 | 251 | retval = (nr == -ENOSPC) ? -EAGAIN : nr; |
8ef047aa | 252 | goto out_free; |
35f71bc0 | 253 | } |
92476d7f | 254 | |
8ef047aa PE |
255 | pid->numbers[i].nr = nr; |
256 | pid->numbers[i].ns = tmp; | |
257 | tmp = tmp->parent; | |
258 | } | |
259 | ||
10dab84c CB |
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 | */ | |
b26ebfe1 CM |
268 | retval = -ENOMEM; |
269 | ||
b461cc03 | 270 | get_pid_ns(ns); |
f57e515a | 271 | refcount_set(&pid->count, 1); |
63f818f4 | 272 | spin_lock_init(&pid->lock); |
92476d7f EB |
273 | for (type = 0; type < PIDTYPE_MAX; ++type) |
274 | INIT_HLIST_HEAD(&pid->tasks[type]); | |
275 | ||
b53b0b9d | 276 | init_waitqueue_head(&pid->wait_pidfd); |
7bc3e6e5 | 277 | INIT_HLIST_HEAD(&pid->inodes); |
b53b0b9d | 278 | |
417e3152 | 279 | upid = pid->numbers + ns->level; |
92476d7f | 280 | spin_lock_irq(&pidmap_lock); |
e8cfbc24 | 281 | if (!(ns->pid_allocated & PIDNS_ADDING)) |
5e1182de | 282 | goto out_unlock; |
cb12fd8e | 283 | #ifdef CONFIG_FS_PID |
b28ddcc3 | 284 | pid->stashed = NULL; |
cb12fd8e CB |
285 | pid->ino = ++pidfs_ino; |
286 | #endif | |
0a01f2cc | 287 | for ( ; upid >= pid->numbers; --upid) { |
95846ecf GS |
288 | /* Make the PID visible to find_pid_ns. */ |
289 | idr_replace(&upid->ns->idr, pid, upid->nr); | |
e8cfbc24 | 290 | upid->ns->pid_allocated++; |
0a01f2cc | 291 | } |
92476d7f EB |
292 | spin_unlock_irq(&pidmap_lock); |
293 | ||
92476d7f EB |
294 | return pid; |
295 | ||
5e1182de | 296 | out_unlock: |
6e666884 | 297 | spin_unlock_irq(&pidmap_lock); |
24c037eb ON |
298 | put_pid_ns(ns); |
299 | ||
92476d7f | 300 | out_free: |
95846ecf | 301 | spin_lock_irq(&pidmap_lock); |
1a80dade MW |
302 | while (++i <= ns->level) { |
303 | upid = pid->numbers + i; | |
304 | idr_remove(&upid->ns->idr, upid->nr); | |
305 | } | |
95846ecf | 306 | |
c0ee5549 EB |
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 | ||
95846ecf | 311 | spin_unlock_irq(&pidmap_lock); |
8ef047aa | 312 | |
baf8f0f8 | 313 | kmem_cache_free(ns->pid_cachep, pid); |
35f71bc0 | 314 | return ERR_PTR(retval); |
92476d7f EB |
315 | } |
316 | ||
c876ad76 EB |
317 | void disable_pid_allocation(struct pid_namespace *ns) |
318 | { | |
319 | spin_lock_irq(&pidmap_lock); | |
e8cfbc24 | 320 | ns->pid_allocated &= ~PIDNS_ADDING; |
c876ad76 EB |
321 | spin_unlock_irq(&pidmap_lock); |
322 | } | |
323 | ||
7ad5b3a5 | 324 | struct pid *find_pid_ns(int nr, struct pid_namespace *ns) |
1da177e4 | 325 | { |
e8cfbc24 | 326 | return idr_find(&ns->idr, nr); |
1da177e4 | 327 | } |
198fe21b | 328 | EXPORT_SYMBOL_GPL(find_pid_ns); |
1da177e4 | 329 | |
8990571e PE |
330 | struct pid *find_vpid(int nr) |
331 | { | |
17cf22c3 | 332 | return find_pid_ns(nr, task_active_pid_ns(current)); |
8990571e PE |
333 | } |
334 | EXPORT_SYMBOL_GPL(find_vpid); | |
335 | ||
2c470475 EB |
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 : | |
2c470475 EB |
340 | &task->signal->pids[type]; |
341 | } | |
342 | ||
e713d0da SB |
343 | /* |
344 | * attach_pid() must be called with the tasklist_lock write-held. | |
345 | */ | |
81907739 | 346 | void attach_pid(struct task_struct *task, enum pid_type type) |
1da177e4 | 347 | { |
2c470475 EB |
348 | struct pid *pid = *task_pid_ptr(task, type); |
349 | hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); | |
1da177e4 LT |
350 | } |
351 | ||
24336eae ON |
352 | static void __change_pid(struct task_struct *task, enum pid_type type, |
353 | struct pid *new) | |
1da177e4 | 354 | { |
2c470475 | 355 | struct pid **pid_ptr = task_pid_ptr(task, type); |
92476d7f EB |
356 | struct pid *pid; |
357 | int tmp; | |
1da177e4 | 358 | |
2c470475 | 359 | pid = *pid_ptr; |
1da177e4 | 360 | |
2c470475 EB |
361 | hlist_del_rcu(&task->pid_links[type]); |
362 | *pid_ptr = new; | |
1da177e4 | 363 | |
43f0df54 ON |
364 | if (type == PIDTYPE_PID) { |
365 | WARN_ON_ONCE(pid_has_task(pid, PIDTYPE_PID)); | |
366 | wake_up_all(&pid->wait_pidfd); | |
367 | } | |
368 | ||
92476d7f | 369 | for (tmp = PIDTYPE_MAX; --tmp >= 0; ) |
1d416a11 | 370 | if (pid_has_task(pid, tmp)) |
92476d7f | 371 | return; |
1da177e4 | 372 | |
92476d7f | 373 | free_pid(pid); |
1da177e4 LT |
374 | } |
375 | ||
24336eae ON |
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); | |
81907739 | 385 | attach_pid(task, type); |
24336eae ON |
386 | } |
387 | ||
6b03d130 EB |
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 | ||
c18258c6 | 407 | /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */ |
7ad5b3a5 | 408 | void transfer_pid(struct task_struct *old, struct task_struct *new, |
c18258c6 EB |
409 | enum pid_type type) |
410 | { | |
a1c6d543 | 411 | WARN_ON_ONCE(type == PIDTYPE_PID); |
2c470475 | 412 | hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]); |
c18258c6 EB |
413 | } |
414 | ||
7ad5b3a5 | 415 | struct task_struct *pid_task(struct pid *pid, enum pid_type type) |
1da177e4 | 416 | { |
92476d7f EB |
417 | struct task_struct *result = NULL; |
418 | if (pid) { | |
419 | struct hlist_node *first; | |
67bdbffd | 420 | first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), |
db1466b3 | 421 | lockdep_tasklist_lock_is_held()); |
92476d7f | 422 | if (first) |
2c470475 | 423 | result = hlist_entry(first, struct task_struct, pid_links[(type)]); |
92476d7f EB |
424 | } |
425 | return result; | |
426 | } | |
eccba068 | 427 | EXPORT_SYMBOL(pid_task); |
1da177e4 | 428 | |
92476d7f | 429 | /* |
9728e5d6 | 430 | * Must be called under rcu_read_lock(). |
92476d7f | 431 | */ |
17f98dcf | 432 | struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns) |
92476d7f | 433 | { |
f78f5b90 PM |
434 | RCU_LOCKDEP_WARN(!rcu_read_lock_held(), |
435 | "find_task_by_pid_ns() needs rcu_read_lock() protection"); | |
17f98dcf | 436 | return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); |
92476d7f | 437 | } |
1da177e4 | 438 | |
228ebcbe PE |
439 | struct task_struct *find_task_by_vpid(pid_t vnr) |
440 | { | |
17cf22c3 | 441 | return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); |
228ebcbe | 442 | } |
228ebcbe | 443 | |
2ee08260 MR |
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 | ||
1a657f78 ON |
457 | struct pid *get_task_pid(struct task_struct *task, enum pid_type type) |
458 | { | |
459 | struct pid *pid; | |
460 | rcu_read_lock(); | |
2c470475 | 461 | pid = get_pid(rcu_dereference(*task_pid_ptr(task, type))); |
1a657f78 ON |
462 | rcu_read_unlock(); |
463 | return pid; | |
464 | } | |
77c100c8 | 465 | EXPORT_SYMBOL_GPL(get_task_pid); |
1a657f78 | 466 | |
7ad5b3a5 | 467 | struct task_struct *get_pid_task(struct pid *pid, enum pid_type type) |
92476d7f EB |
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; | |
1da177e4 | 476 | } |
77c100c8 | 477 | EXPORT_SYMBOL_GPL(get_pid_task); |
1da177e4 | 478 | |
92476d7f | 479 | struct pid *find_get_pid(pid_t nr) |
1da177e4 LT |
480 | { |
481 | struct pid *pid; | |
482 | ||
92476d7f | 483 | rcu_read_lock(); |
198fe21b | 484 | pid = get_pid(find_vpid(nr)); |
92476d7f | 485 | rcu_read_unlock(); |
1da177e4 | 486 | |
92476d7f | 487 | return pid; |
1da177e4 | 488 | } |
339caf2a | 489 | EXPORT_SYMBOL_GPL(find_get_pid); |
1da177e4 | 490 | |
7af57294 PE |
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 | } | |
4f82f457 | 503 | EXPORT_SYMBOL_GPL(pid_nr_ns); |
7af57294 | 504 | |
44c4e1b2 EB |
505 | pid_t pid_vnr(struct pid *pid) |
506 | { | |
17cf22c3 | 507 | return pid_nr_ns(pid, task_active_pid_ns(current)); |
44c4e1b2 EB |
508 | } |
509 | EXPORT_SYMBOL_GPL(pid_vnr); | |
510 | ||
52ee2dfd ON |
511 | pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, |
512 | struct pid_namespace *ns) | |
2f2a3a46 | 513 | { |
52ee2dfd ON |
514 | pid_t nr = 0; |
515 | ||
516 | rcu_read_lock(); | |
517 | if (!ns) | |
17cf22c3 | 518 | ns = task_active_pid_ns(current); |
1dd694a1 | 519 | nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); |
52ee2dfd ON |
520 | rcu_read_unlock(); |
521 | ||
522 | return nr; | |
2f2a3a46 | 523 | } |
52ee2dfd | 524 | EXPORT_SYMBOL(__task_pid_nr_ns); |
2f2a3a46 | 525 | |
61bce0f1 EB |
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 | ||
0804ef4b | 532 | /* |
025dfdaf | 533 | * Used by proc to find the first pid that is greater than or equal to nr. |
0804ef4b | 534 | * |
e49859e7 | 535 | * If there is a pid at nr this function is exactly the same as find_pid_ns. |
0804ef4b | 536 | */ |
198fe21b | 537 | struct pid *find_ge_pid(int nr, struct pid_namespace *ns) |
0804ef4b | 538 | { |
95846ecf | 539 | return idr_get_next(&ns->idr, &nr); |
0804ef4b | 540 | } |
4480c27c | 541 | EXPORT_SYMBOL_GPL(find_ge_pid); |
0804ef4b | 542 | |
1aa92cd3 MK |
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 | ||
e9bdcdbf CB |
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 | * | |
e9bdcdbf CB |
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 | ||
32fcb426 CB |
593 | /** |
594 | * pidfd_create() - Create a new pid file descriptor. | |
595 | * | |
6da73d15 CB |
596 | * @pid: struct pid that the pidfd will reference |
597 | * @flags: flags to pass | |
32fcb426 CB |
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 | * | |
c576e0fc MB |
604 | * This symbol should not be explicitly exported to loadable modules. |
605 | * | |
32fcb426 CB |
606 | * Return: On success, a cloexec pidfd is returned. |
607 | * On error, a negative errno number will be returned. | |
608 | */ | |
cdefbf23 | 609 | static int pidfd_create(struct pid *pid, unsigned int flags) |
32fcb426 | 610 | { |
6ae930d9 CB |
611 | int pidfd; |
612 | struct file *pidfd_file; | |
32fcb426 | 613 | |
6ae930d9 CB |
614 | pidfd = pidfd_prepare(pid, flags, &pidfd_file); |
615 | if (pidfd < 0) | |
616 | return pidfd; | |
490b9ba8 | 617 | |
6ae930d9 CB |
618 | fd_install(pidfd, pidfd_file); |
619 | return pidfd; | |
32fcb426 CB |
620 | } |
621 | ||
622 | /** | |
0c7752d5 | 623 | * sys_pidfd_open() - Open new pid file descriptor. |
32fcb426 CB |
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 | |
64bef697 ON |
629 | * the task identified by @pid. Without PIDFD_THREAD flag the target task |
630 | * must be a thread-group leader. | |
32fcb426 CB |
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 | { | |
1e1d0f0b | 637 | int fd; |
32fcb426 CB |
638 | struct pid *p; |
639 | ||
64bef697 | 640 | if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD)) |
32fcb426 CB |
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 | ||
490b9ba8 | 650 | fd = pidfd_create(p, flags); |
32fcb426 | 651 | |
32fcb426 CB |
652 | put_pid(p); |
653 | return fd; | |
654 | } | |
655 | ||
95846ecf | 656 | void __init pid_idr_init(void) |
1da177e4 | 657 | { |
840d6fe7 | 658 | /* Verify no one has done anything silly: */ |
e8cfbc24 | 659 | BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING); |
c876ad76 | 660 | |
72680a19 HB |
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 | ||
95846ecf | 668 | idr_init(&init_pid_ns.idr); |
92476d7f | 669 | |
b69f0aeb | 670 | init_pid_ns.pid_cachep = kmem_cache_create("pid", |
dd546618 | 671 | struct_size_t(struct pid, numbers, 1), |
b69f0aeb KC |
672 | __alignof__(struct pid), |
673 | SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, | |
674 | NULL); | |
1da177e4 | 675 | } |
8649c322 SD |
676 | |
677 | static struct file *__pidfd_fget(struct task_struct *task, int fd) | |
678 | { | |
679 | struct file *file; | |
680 | int ret; | |
681 | ||
f7cfd871 | 682 | ret = down_read_killable(&task->signal->exec_update_lock); |
8649c322 SD |
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 | ||
f7cfd871 | 691 | up_read(&task->signal->exec_update_lock); |
8649c322 | 692 | |
0c9bd6bc TA |
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; | |
8649c322 SD |
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 | ||
4e94ddfe | 730 | ret = receive_fd(file, NULL, O_CLOEXEC); |
910d2f16 | 731 | fput(file); |
8649c322 SD |
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 | } |