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