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 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 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 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 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 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 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 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 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 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 */ 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 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 for (tmp = PIDTYPE_MAX; --tmp >= 0; ) 363 if (pid_has_task(pid, tmp)) 364 return; 365 366 WARN_ON(pids[type]); 367 pids[type] = pid; 368 } 369 370 void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type) 371 { 372 __change_pid(pids, task, type, NULL); 373 } 374 375 void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type, 376 struct pid *pid) 377 { 378 __change_pid(pids, task, type, pid); 379 attach_pid(task, type); 380 } 381 382 void exchange_tids(struct task_struct *left, struct task_struct *right) 383 { 384 struct pid *pid1 = left->thread_pid; 385 struct pid *pid2 = right->thread_pid; 386 struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID]; 387 struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID]; 388 389 lockdep_assert_held_write(&tasklist_lock); 390 391 /* Swap the single entry tid lists */ 392 hlists_swap_heads_rcu(head1, head2); 393 394 /* Swap the per task_struct pid */ 395 rcu_assign_pointer(left->thread_pid, pid2); 396 rcu_assign_pointer(right->thread_pid, pid1); 397 398 /* Swap the cached value */ 399 WRITE_ONCE(left->pid, pid_nr(pid2)); 400 WRITE_ONCE(right->pid, pid_nr(pid1)); 401 } 402 403 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */ 404 void transfer_pid(struct task_struct *old, struct task_struct *new, 405 enum pid_type type) 406 { 407 WARN_ON_ONCE(type == PIDTYPE_PID); 408 lockdep_assert_held_write(&tasklist_lock); 409 hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]); 410 } 411 412 struct task_struct *pid_task(struct pid *pid, enum pid_type type) 413 { 414 struct task_struct *result = NULL; 415 if (pid) { 416 struct hlist_node *first; 417 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), 418 lockdep_tasklist_lock_is_held()); 419 if (first) 420 result = hlist_entry(first, struct task_struct, pid_links[(type)]); 421 } 422 return result; 423 } 424 EXPORT_SYMBOL(pid_task); 425 426 /* 427 * Must be called under rcu_read_lock(). 428 */ 429 struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns) 430 { 431 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), 432 "find_task_by_pid_ns() needs rcu_read_lock() protection"); 433 return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); 434 } 435 436 struct task_struct *find_task_by_vpid(pid_t vnr) 437 { 438 return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); 439 } 440 441 struct task_struct *find_get_task_by_vpid(pid_t nr) 442 { 443 struct task_struct *task; 444 445 rcu_read_lock(); 446 task = find_task_by_vpid(nr); 447 if (task) 448 get_task_struct(task); 449 rcu_read_unlock(); 450 451 return task; 452 } 453 454 struct pid *get_task_pid(struct task_struct *task, enum pid_type type) 455 { 456 struct pid *pid; 457 rcu_read_lock(); 458 pid = get_pid(rcu_dereference(*task_pid_ptr(task, type))); 459 rcu_read_unlock(); 460 return pid; 461 } 462 EXPORT_SYMBOL_GPL(get_task_pid); 463 464 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type) 465 { 466 struct task_struct *result; 467 rcu_read_lock(); 468 result = pid_task(pid, type); 469 if (result) 470 get_task_struct(result); 471 rcu_read_unlock(); 472 return result; 473 } 474 EXPORT_SYMBOL_GPL(get_pid_task); 475 476 struct pid *find_get_pid(pid_t nr) 477 { 478 struct pid *pid; 479 480 rcu_read_lock(); 481 pid = get_pid(find_vpid(nr)); 482 rcu_read_unlock(); 483 484 return pid; 485 } 486 EXPORT_SYMBOL_GPL(find_get_pid); 487 488 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) 489 { 490 struct upid *upid; 491 pid_t nr = 0; 492 493 if (pid && ns->level <= pid->level) { 494 upid = &pid->numbers[ns->level]; 495 if (upid->ns == ns) 496 nr = upid->nr; 497 } 498 return nr; 499 } 500 EXPORT_SYMBOL_GPL(pid_nr_ns); 501 502 pid_t pid_vnr(struct pid *pid) 503 { 504 return pid_nr_ns(pid, task_active_pid_ns(current)); 505 } 506 EXPORT_SYMBOL_GPL(pid_vnr); 507 508 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, 509 struct pid_namespace *ns) 510 { 511 pid_t nr = 0; 512 513 rcu_read_lock(); 514 if (!ns) 515 ns = task_active_pid_ns(current); 516 nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); 517 rcu_read_unlock(); 518 519 return nr; 520 } 521 EXPORT_SYMBOL(__task_pid_nr_ns); 522 523 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk) 524 { 525 return ns_of_pid(task_pid(tsk)); 526 } 527 EXPORT_SYMBOL_GPL(task_active_pid_ns); 528 529 /* 530 * Used by proc to find the first pid that is greater than or equal to nr. 531 * 532 * If there is a pid at nr this function is exactly the same as find_pid_ns. 533 */ 534 struct pid *find_ge_pid(int nr, struct pid_namespace *ns) 535 { 536 return idr_get_next(&ns->idr, &nr); 537 } 538 EXPORT_SYMBOL_GPL(find_ge_pid); 539 540 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) 541 { 542 CLASS(fd, f)(fd); 543 struct pid *pid; 544 545 if (fd_empty(f)) 546 return ERR_PTR(-EBADF); 547 548 pid = pidfd_pid(fd_file(f)); 549 if (!IS_ERR(pid)) { 550 get_pid(pid); 551 *flags = fd_file(f)->f_flags; 552 } 553 return pid; 554 } 555 556 /** 557 * pidfd_get_task() - Get the task associated with a pidfd 558 * 559 * @pidfd: pidfd for which to get the task 560 * @flags: flags associated with this pidfd 561 * 562 * Return the task associated with @pidfd. The function takes a reference on 563 * the returned task. The caller is responsible for releasing that reference. 564 * 565 * Return: On success, the task_struct associated with the pidfd. 566 * On error, a negative errno number will be returned. 567 */ 568 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags) 569 { 570 unsigned int f_flags = 0; 571 struct pid *pid; 572 struct task_struct *task; 573 enum pid_type type; 574 575 switch (pidfd) { 576 case PIDFD_SELF_THREAD: 577 type = PIDTYPE_PID; 578 pid = get_task_pid(current, type); 579 break; 580 case PIDFD_SELF_THREAD_GROUP: 581 type = PIDTYPE_TGID; 582 pid = get_task_pid(current, type); 583 break; 584 default: 585 pid = pidfd_get_pid(pidfd, &f_flags); 586 if (IS_ERR(pid)) 587 return ERR_CAST(pid); 588 type = PIDTYPE_TGID; 589 break; 590 } 591 592 task = get_pid_task(pid, type); 593 put_pid(pid); 594 if (!task) 595 return ERR_PTR(-ESRCH); 596 597 *flags = f_flags; 598 return task; 599 } 600 601 /** 602 * pidfd_create() - Create a new pid file descriptor. 603 * 604 * @pid: struct pid that the pidfd will reference 605 * @flags: flags to pass 606 * 607 * This creates a new pid file descriptor with the O_CLOEXEC flag set. 608 * 609 * Note, that this function can only be called after the fd table has 610 * been unshared to avoid leaking the pidfd to the new process. 611 * 612 * This symbol should not be explicitly exported to loadable modules. 613 * 614 * Return: On success, a cloexec pidfd is returned. 615 * On error, a negative errno number will be returned. 616 */ 617 static int pidfd_create(struct pid *pid, unsigned int flags) 618 { 619 int pidfd; 620 struct file *pidfd_file; 621 622 pidfd = pidfd_prepare(pid, flags, &pidfd_file); 623 if (pidfd < 0) 624 return pidfd; 625 626 fd_install(pidfd, pidfd_file); 627 return pidfd; 628 } 629 630 /** 631 * sys_pidfd_open() - Open new pid file descriptor. 632 * 633 * @pid: pid for which to retrieve a pidfd 634 * @flags: flags to pass 635 * 636 * This creates a new pid file descriptor with the O_CLOEXEC flag set for 637 * the task identified by @pid. Without PIDFD_THREAD flag the target task 638 * must be a thread-group leader. 639 * 640 * Return: On success, a cloexec pidfd is returned. 641 * On error, a negative errno number will be returned. 642 */ 643 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) 644 { 645 int fd; 646 struct pid *p; 647 648 if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD)) 649 return -EINVAL; 650 651 if (pid <= 0) 652 return -EINVAL; 653 654 p = find_get_pid(pid); 655 if (!p) 656 return -ESRCH; 657 658 fd = pidfd_create(p, flags); 659 660 put_pid(p); 661 return fd; 662 } 663 664 #ifdef CONFIG_SYSCTL 665 static struct ctl_table_set *pid_table_root_lookup(struct ctl_table_root *root) 666 { 667 return &task_active_pid_ns(current)->set; 668 } 669 670 static int set_is_seen(struct ctl_table_set *set) 671 { 672 return &task_active_pid_ns(current)->set == set; 673 } 674 675 static int pid_table_root_permissions(struct ctl_table_header *head, 676 const struct ctl_table *table) 677 { 678 struct pid_namespace *pidns = 679 container_of(head->set, struct pid_namespace, set); 680 int mode = table->mode; 681 682 if (ns_capable(pidns->user_ns, CAP_SYS_ADMIN) || 683 uid_eq(current_euid(), make_kuid(pidns->user_ns, 0))) 684 mode = (mode & S_IRWXU) >> 6; 685 else if (in_egroup_p(make_kgid(pidns->user_ns, 0))) 686 mode = (mode & S_IRWXG) >> 3; 687 else 688 mode = mode & S_IROTH; 689 return (mode << 6) | (mode << 3) | mode; 690 } 691 692 static void pid_table_root_set_ownership(struct ctl_table_header *head, 693 kuid_t *uid, kgid_t *gid) 694 { 695 struct pid_namespace *pidns = 696 container_of(head->set, struct pid_namespace, set); 697 kuid_t ns_root_uid; 698 kgid_t ns_root_gid; 699 700 ns_root_uid = make_kuid(pidns->user_ns, 0); 701 if (uid_valid(ns_root_uid)) 702 *uid = ns_root_uid; 703 704 ns_root_gid = make_kgid(pidns->user_ns, 0); 705 if (gid_valid(ns_root_gid)) 706 *gid = ns_root_gid; 707 } 708 709 static struct ctl_table_root pid_table_root = { 710 .lookup = pid_table_root_lookup, 711 .permissions = pid_table_root_permissions, 712 .set_ownership = pid_table_root_set_ownership, 713 }; 714 715 static const struct ctl_table pid_table[] = { 716 { 717 .procname = "pid_max", 718 .data = &init_pid_ns.pid_max, 719 .maxlen = sizeof(int), 720 .mode = 0644, 721 .proc_handler = proc_dointvec_minmax, 722 .extra1 = &pid_max_min, 723 .extra2 = &pid_max_max, 724 }, 725 }; 726 #endif 727 728 int register_pidns_sysctls(struct pid_namespace *pidns) 729 { 730 #ifdef CONFIG_SYSCTL 731 struct ctl_table *tbl; 732 733 setup_sysctl_set(&pidns->set, &pid_table_root, set_is_seen); 734 735 tbl = kmemdup(pid_table, sizeof(pid_table), GFP_KERNEL); 736 if (!tbl) 737 return -ENOMEM; 738 tbl->data = &pidns->pid_max; 739 pidns->pid_max = min(pid_max_max, max_t(int, pidns->pid_max, 740 PIDS_PER_CPU_DEFAULT * num_possible_cpus())); 741 742 pidns->sysctls = __register_sysctl_table(&pidns->set, "kernel", tbl, 743 ARRAY_SIZE(pid_table)); 744 if (!pidns->sysctls) { 745 kfree(tbl); 746 retire_sysctl_set(&pidns->set); 747 return -ENOMEM; 748 } 749 #endif 750 return 0; 751 } 752 753 void unregister_pidns_sysctls(struct pid_namespace *pidns) 754 { 755 #ifdef CONFIG_SYSCTL 756 const struct ctl_table *tbl; 757 758 tbl = pidns->sysctls->ctl_table_arg; 759 unregister_sysctl_table(pidns->sysctls); 760 retire_sysctl_set(&pidns->set); 761 kfree(tbl); 762 #endif 763 } 764 765 void __init pid_idr_init(void) 766 { 767 /* Verify no one has done anything silly: */ 768 BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING); 769 770 /* bump default and minimum pid_max based on number of cpus */ 771 init_pid_ns.pid_max = min(pid_max_max, max_t(int, init_pid_ns.pid_max, 772 PIDS_PER_CPU_DEFAULT * num_possible_cpus())); 773 pid_max_min = max_t(int, pid_max_min, 774 PIDS_PER_CPU_MIN * num_possible_cpus()); 775 pr_info("pid_max: default: %u minimum: %u\n", init_pid_ns.pid_max, pid_max_min); 776 777 idr_init(&init_pid_ns.idr); 778 779 init_pid_ns.pid_cachep = kmem_cache_create("pid", 780 struct_size_t(struct pid, numbers, 1), 781 __alignof__(struct pid), 782 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, 783 NULL); 784 } 785 786 static __init int pid_namespace_sysctl_init(void) 787 { 788 #ifdef CONFIG_SYSCTL 789 /* "kernel" directory will have already been initialized. */ 790 BUG_ON(register_pidns_sysctls(&init_pid_ns)); 791 #endif 792 return 0; 793 } 794 subsys_initcall(pid_namespace_sysctl_init); 795 796 static struct file *__pidfd_fget(struct task_struct *task, int fd) 797 { 798 struct file *file; 799 int ret; 800 801 ret = down_read_killable(&task->signal->exec_update_lock); 802 if (ret) 803 return ERR_PTR(ret); 804 805 if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) 806 file = fget_task(task, fd); 807 else 808 file = ERR_PTR(-EPERM); 809 810 up_read(&task->signal->exec_update_lock); 811 812 if (!file) { 813 /* 814 * It is possible that the target thread is exiting; it can be 815 * either: 816 * 1. before exit_signals(), which gives a real fd 817 * 2. before exit_files() takes the task_lock() gives a real fd 818 * 3. after exit_files() releases task_lock(), ->files is NULL; 819 * this has PF_EXITING, since it was set in exit_signals(), 820 * __pidfd_fget() returns EBADF. 821 * In case 3 we get EBADF, but that really means ESRCH, since 822 * the task is currently exiting and has freed its files 823 * struct, so we fix it up. 824 */ 825 if (task->flags & PF_EXITING) 826 file = ERR_PTR(-ESRCH); 827 else 828 file = ERR_PTR(-EBADF); 829 } 830 831 return file; 832 } 833 834 static int pidfd_getfd(struct pid *pid, int fd) 835 { 836 struct task_struct *task; 837 struct file *file; 838 int ret; 839 840 task = get_pid_task(pid, PIDTYPE_PID); 841 if (!task) 842 return -ESRCH; 843 844 file = __pidfd_fget(task, fd); 845 put_task_struct(task); 846 if (IS_ERR(file)) 847 return PTR_ERR(file); 848 849 ret = receive_fd(file, NULL, O_CLOEXEC); 850 fput(file); 851 852 return ret; 853 } 854 855 /** 856 * sys_pidfd_getfd() - Get a file descriptor from another process 857 * 858 * @pidfd: the pidfd file descriptor of the process 859 * @fd: the file descriptor number to get 860 * @flags: flags on how to get the fd (reserved) 861 * 862 * This syscall gets a copy of a file descriptor from another process 863 * based on the pidfd, and file descriptor number. It requires that 864 * the calling process has the ability to ptrace the process represented 865 * by the pidfd. The process which is having its file descriptor copied 866 * is otherwise unaffected. 867 * 868 * Return: On success, a cloexec file descriptor is returned. 869 * On error, a negative errno number will be returned. 870 */ 871 SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, 872 unsigned int, flags) 873 { 874 struct pid *pid; 875 876 /* flags is currently unused - make sure it's unset */ 877 if (flags) 878 return -EINVAL; 879 880 CLASS(fd, f)(pidfd); 881 if (fd_empty(f)) 882 return -EBADF; 883 884 pid = pidfd_pid(fd_file(f)); 885 if (IS_ERR(pid)) 886 return PTR_ERR(pid); 887 888 return pidfd_getfd(pid, fd); 889 } 890