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 <net/sock.h> 47 #include <uapi/linux/pidfd.h> 48 49 struct pid init_struct_pid = { 50 .count = REFCOUNT_INIT(1), 51 .tasks = { 52 { .first = NULL }, 53 { .first = NULL }, 54 { .first = NULL }, 55 }, 56 .level = 0, 57 .numbers = { { 58 .nr = 0, 59 .ns = &init_pid_ns, 60 }, } 61 }; 62 63 static int pid_max_min = RESERVED_PIDS + 1; 64 static int pid_max_max = PID_MAX_LIMIT; 65 66 /* 67 * PID-map pages start out as NULL, they get allocated upon 68 * first use and are never deallocated. This way a low pid_max 69 * value does not cause lots of bitmaps to be allocated, but 70 * the scheme scales to up to 4 million PIDs, runtime. 71 */ 72 struct pid_namespace init_pid_ns = { 73 .ns = NS_COMMON_INIT(init_pid_ns), 74 .idr = IDR_INIT(init_pid_ns.idr), 75 .pid_allocated = PIDNS_ADDING, 76 .level = 0, 77 .child_reaper = &init_task, 78 .user_ns = &init_user_ns, 79 .pid_max = PID_MAX_DEFAULT, 80 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) 81 .memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC, 82 #endif 83 }; 84 EXPORT_SYMBOL_GPL(init_pid_ns); 85 86 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock); 87 88 void put_pid(struct pid *pid) 89 { 90 struct pid_namespace *ns; 91 92 if (!pid) 93 return; 94 95 ns = pid->numbers[pid->level].ns; 96 if (refcount_dec_and_test(&pid->count)) { 97 pidfs_free_pid(pid); 98 kmem_cache_free(ns->pid_cachep, pid); 99 put_pid_ns(ns); 100 } 101 } 102 EXPORT_SYMBOL_GPL(put_pid); 103 104 static void delayed_put_pid(struct rcu_head *rhp) 105 { 106 struct pid *pid = container_of(rhp, struct pid, rcu); 107 put_pid(pid); 108 } 109 110 void free_pid(struct pid *pid) 111 { 112 int i; 113 struct pid_namespace *active_ns; 114 115 lockdep_assert_not_held(&tasklist_lock); 116 117 active_ns = pid->numbers[pid->level].ns; 118 ns_ref_active_put(active_ns); 119 120 spin_lock(&pidmap_lock); 121 for (i = 0; i <= pid->level; i++) { 122 struct upid *upid = pid->numbers + i; 123 struct pid_namespace *ns = upid->ns; 124 switch (--ns->pid_allocated) { 125 case 2: 126 case 1: 127 /* When all that is left in the pid namespace 128 * is the reaper wake up the reaper. The reaper 129 * may be sleeping in zap_pid_ns_processes(). 130 */ 131 wake_up_process(READ_ONCE(ns->child_reaper)); 132 break; 133 case PIDNS_ADDING: 134 /* Only possible if the 1st fork fails */ 135 WARN_ON(READ_ONCE(ns->child_reaper)); 136 break; 137 } 138 139 idr_remove(&ns->idr, upid->nr); 140 } 141 spin_unlock(&pidmap_lock); 142 143 pidfs_remove_pid(pid); 144 call_rcu(&pid->rcu, delayed_put_pid); 145 } 146 147 void free_pids(struct pid **pids) 148 { 149 int tmp; 150 151 /* 152 * This can batch pidmap_lock. 153 */ 154 for (tmp = PIDTYPE_MAX; --tmp >= 0; ) 155 if (pids[tmp]) 156 free_pid(pids[tmp]); 157 } 158 159 struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, 160 size_t arg_set_tid_size) 161 { 162 int set_tid[MAX_PID_NS_LEVEL + 1] = {}; 163 int pid_max[MAX_PID_NS_LEVEL + 1] = {}; 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 bool retried_preload; 171 172 /* 173 * arg_set_tid_size contains the size of the arg_set_tid array. Starting at 174 * the most nested currently active PID namespace it tells alloc_pid() 175 * which PID to set for a process in that most nested PID namespace 176 * up to arg_set_tid_size PID namespaces. It does not have to set the PID 177 * for a process in all nested PID namespaces but arg_set_tid_size must 178 * never be greater than the current ns->level + 1. 179 */ 180 if (arg_set_tid_size > ns->level + 1) 181 return ERR_PTR(-EINVAL); 182 183 /* 184 * Prep before we take locks: 185 * 186 * 1. allocate and fill in pid struct 187 */ 188 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); 189 if (!pid) 190 return ERR_PTR(retval); 191 192 get_pid_ns(ns); 193 pid->level = ns->level; 194 refcount_set(&pid->count, 1); 195 spin_lock_init(&pid->lock); 196 for (type = 0; type < PIDTYPE_MAX; ++type) 197 INIT_HLIST_HEAD(&pid->tasks[type]); 198 init_waitqueue_head(&pid->wait_pidfd); 199 INIT_HLIST_HEAD(&pid->inodes); 200 pidfs_prepare_pid(pid); 201 202 /* 203 * 2. perm check checkpoint_restore_ns_capable() 204 * 205 * This stores found pid_max to make sure the used value is the same should 206 * later code need it. 207 */ 208 for (tmp = ns, i = ns->level; i >= 0; i--) { 209 pid_max[ns->level - i] = READ_ONCE(tmp->pid_max); 210 211 if (arg_set_tid_size) { 212 int tid = set_tid[ns->level - i] = arg_set_tid[ns->level - i]; 213 214 retval = -EINVAL; 215 if (tid < 1 || tid >= pid_max[ns->level - i]) 216 goto out_abort; 217 retval = -EPERM; 218 if (!checkpoint_restore_ns_capable(tmp->user_ns)) 219 goto out_abort; 220 arg_set_tid_size--; 221 } 222 223 tmp = tmp->parent; 224 } 225 226 /* 227 * Prep is done, id allocation goes here: 228 */ 229 retried_preload = false; 230 idr_preload(GFP_KERNEL); 231 spin_lock(&pidmap_lock); 232 /* For the case when the previous attempt to create init failed */ 233 if (ns->pid_allocated == PIDNS_ADDING) 234 idr_set_cursor(&ns->idr, 0); 235 236 for (tmp = ns, i = ns->level; i >= 0;) { 237 int tid = set_tid[ns->level - i]; 238 239 if (tid) { 240 nr = idr_alloc(&tmp->idr, NULL, tid, 241 tid + 1, GFP_ATOMIC); 242 /* 243 * If ENOSPC is returned it means that the PID is 244 * alreay in use. Return EEXIST in that case. 245 */ 246 if (nr == -ENOSPC) 247 248 nr = -EEXIST; 249 } else { 250 int pid_min = 1; 251 /* 252 * init really needs pid 1, but after reaching the 253 * maximum wrap back to RESERVED_PIDS 254 */ 255 if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) 256 pid_min = RESERVED_PIDS; 257 258 /* 259 * Store a null pointer so find_pid_ns does not find 260 * a partially initialized PID (see below). 261 */ 262 nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min, 263 pid_max[ns->level - i], GFP_ATOMIC); 264 if (nr == -ENOSPC) 265 nr = -EAGAIN; 266 } 267 268 if (unlikely(nr < 0)) { 269 /* 270 * Preload more memory if idr_alloc{,cyclic} failed with -ENOMEM. 271 * 272 * The IDR API only allows us to preload memory for one call, while we may end 273 * up doing several under pidmap_lock with GFP_ATOMIC. The situation may be 274 * salvageable with GFP_KERNEL. But make sure to not loop indefinitely if preload 275 * did not help (the routine unfortunately returns void, so we have no idea 276 * if it got anywhere). 277 * 278 * The lock can be safely dropped and picked up as historically pid allocation 279 * for different namespaces was *not* atomic -- we try to hold on to it the 280 * entire time only for performance reasons. 281 */ 282 if (nr == -ENOMEM && !retried_preload) { 283 spin_unlock(&pidmap_lock); 284 idr_preload_end(); 285 retried_preload = true; 286 idr_preload(GFP_KERNEL); 287 spin_lock(&pidmap_lock); 288 continue; 289 } 290 retval = nr; 291 goto out_free; 292 } 293 294 pid->numbers[i].nr = nr; 295 pid->numbers[i].ns = tmp; 296 i--; 297 retried_preload = false; 298 299 /* 300 * PID 1 (init) must be created first. 301 */ 302 if (!READ_ONCE(tmp->child_reaper) && nr != 1) { 303 retval = -EINVAL; 304 goto out_free; 305 } 306 307 tmp = tmp->parent; 308 } 309 310 /* 311 * ENOMEM is not the most obvious choice especially for the case 312 * where the child subreaper has already exited and the pid 313 * namespace denies the creation of any new processes. But ENOMEM 314 * is what we have exposed to userspace for a long time and it is 315 * documented behavior for pid namespaces. So we can't easily 316 * change it even if there were an error code better suited. 317 * 318 * This can't be done earlier because we need to preserve other 319 * error conditions. 320 * 321 * We need this even if copy_process() does the same check. If two 322 * or more tasks from parent namespace try to inject a child into a 323 * dead namespace, one of free_pid() calls from the copy_process() 324 * error path may try to wakeup the possibly freed ns->child_reaper. 325 */ 326 retval = -ENOMEM; 327 if (unlikely(!(ns->pid_allocated & PIDNS_ADDING))) 328 goto out_free; 329 for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) { 330 /* Make the PID visible to find_pid_ns. */ 331 idr_replace(&upid->ns->idr, pid, upid->nr); 332 upid->ns->pid_allocated++; 333 } 334 spin_unlock(&pidmap_lock); 335 idr_preload_end(); 336 ns_ref_active_get(ns); 337 338 retval = pidfs_add_pid(pid); 339 if (unlikely(retval)) { 340 free_pid(pid); 341 pid = ERR_PTR(-ENOMEM); 342 } 343 344 return pid; 345 346 out_free: 347 while (++i <= ns->level) { 348 upid = pid->numbers + i; 349 idr_remove(&upid->ns->idr, upid->nr); 350 } 351 352 spin_unlock(&pidmap_lock); 353 idr_preload_end(); 354 355 out_abort: 356 put_pid_ns(ns); 357 kmem_cache_free(ns->pid_cachep, pid); 358 return ERR_PTR(retval); 359 } 360 361 void disable_pid_allocation(struct pid_namespace *ns) 362 { 363 spin_lock(&pidmap_lock); 364 ns->pid_allocated &= ~PIDNS_ADDING; 365 spin_unlock(&pidmap_lock); 366 } 367 368 struct pid *find_pid_ns(int nr, struct pid_namespace *ns) 369 { 370 return idr_find(&ns->idr, nr); 371 } 372 EXPORT_SYMBOL_GPL(find_pid_ns); 373 374 struct pid *find_vpid(int nr) 375 { 376 return find_pid_ns(nr, task_active_pid_ns(current)); 377 } 378 EXPORT_SYMBOL_GPL(find_vpid); 379 380 static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type) 381 { 382 return (type == PIDTYPE_PID) ? 383 &task->thread_pid : 384 &task->signal->pids[type]; 385 } 386 387 /* 388 * attach_pid() must be called with the tasklist_lock write-held. 389 */ 390 void attach_pid(struct task_struct *task, enum pid_type type) 391 { 392 struct pid *pid; 393 394 lockdep_assert_held_write(&tasklist_lock); 395 396 pid = *task_pid_ptr(task, type); 397 hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); 398 } 399 400 static void __change_pid(struct pid **pids, struct task_struct *task, 401 enum pid_type type, struct pid *new) 402 { 403 struct pid **pid_ptr, *pid; 404 int tmp; 405 406 lockdep_assert_held_write(&tasklist_lock); 407 408 pid_ptr = task_pid_ptr(task, type); 409 pid = *pid_ptr; 410 411 hlist_del_rcu(&task->pid_links[type]); 412 *pid_ptr = new; 413 414 for (tmp = PIDTYPE_MAX; --tmp >= 0; ) 415 if (pid_has_task(pid, tmp)) 416 return; 417 418 WARN_ON(pids[type]); 419 pids[type] = pid; 420 } 421 422 void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type) 423 { 424 __change_pid(pids, task, type, NULL); 425 } 426 427 void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type, 428 struct pid *pid) 429 { 430 __change_pid(pids, task, type, pid); 431 attach_pid(task, type); 432 } 433 434 void exchange_tids(struct task_struct *left, struct task_struct *right) 435 { 436 struct pid *pid1 = left->thread_pid; 437 struct pid *pid2 = right->thread_pid; 438 struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID]; 439 struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID]; 440 441 lockdep_assert_held_write(&tasklist_lock); 442 443 /* Swap the single entry tid lists */ 444 hlists_swap_heads_rcu(head1, head2); 445 446 /* Swap the per task_struct pid */ 447 rcu_assign_pointer(left->thread_pid, pid2); 448 rcu_assign_pointer(right->thread_pid, pid1); 449 450 /* Swap the cached value */ 451 WRITE_ONCE(left->pid, pid_nr(pid2)); 452 WRITE_ONCE(right->pid, pid_nr(pid1)); 453 } 454 455 /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */ 456 void transfer_pid(struct task_struct *old, struct task_struct *new, 457 enum pid_type type) 458 { 459 WARN_ON_ONCE(type == PIDTYPE_PID); 460 lockdep_assert_held_write(&tasklist_lock); 461 hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]); 462 } 463 464 struct task_struct *pid_task(struct pid *pid, enum pid_type type) 465 { 466 struct task_struct *result = NULL; 467 if (pid) { 468 struct hlist_node *first; 469 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), 470 lockdep_tasklist_lock_is_held()); 471 if (first) 472 result = hlist_entry(first, struct task_struct, pid_links[(type)]); 473 } 474 return result; 475 } 476 EXPORT_SYMBOL(pid_task); 477 478 /* 479 * Must be called under rcu_read_lock(). 480 */ 481 struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns) 482 { 483 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), 484 "find_task_by_pid_ns() needs rcu_read_lock() protection"); 485 return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); 486 } 487 488 struct task_struct *find_task_by_vpid(pid_t vnr) 489 { 490 return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); 491 } 492 493 struct task_struct *find_get_task_by_vpid(pid_t nr) 494 { 495 struct task_struct *task; 496 497 rcu_read_lock(); 498 task = find_task_by_vpid(nr); 499 if (task) 500 get_task_struct(task); 501 rcu_read_unlock(); 502 503 return task; 504 } 505 506 struct pid *get_task_pid(struct task_struct *task, enum pid_type type) 507 { 508 struct pid *pid; 509 rcu_read_lock(); 510 pid = get_pid(rcu_dereference(*task_pid_ptr(task, type))); 511 rcu_read_unlock(); 512 return pid; 513 } 514 EXPORT_SYMBOL_GPL(get_task_pid); 515 516 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type) 517 { 518 struct task_struct *result; 519 rcu_read_lock(); 520 result = pid_task(pid, type); 521 if (result) 522 get_task_struct(result); 523 rcu_read_unlock(); 524 return result; 525 } 526 EXPORT_SYMBOL_GPL(get_pid_task); 527 528 struct pid *find_get_pid(pid_t nr) 529 { 530 struct pid *pid; 531 532 rcu_read_lock(); 533 pid = get_pid(find_vpid(nr)); 534 rcu_read_unlock(); 535 536 return pid; 537 } 538 EXPORT_SYMBOL_GPL(find_get_pid); 539 540 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) 541 { 542 struct upid *upid; 543 pid_t nr = 0; 544 545 if (pid && ns && ns->level <= pid->level) { 546 upid = &pid->numbers[ns->level]; 547 if (upid->ns == ns) 548 nr = upid->nr; 549 } 550 return nr; 551 } 552 EXPORT_SYMBOL_GPL(pid_nr_ns); 553 554 pid_t pid_vnr(struct pid *pid) 555 { 556 return pid_nr_ns(pid, task_active_pid_ns(current)); 557 } 558 EXPORT_SYMBOL_GPL(pid_vnr); 559 560 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, 561 struct pid_namespace *ns) 562 { 563 pid_t nr = 0; 564 565 rcu_read_lock(); 566 if (!ns) 567 ns = task_active_pid_ns(current); 568 nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns); 569 rcu_read_unlock(); 570 571 return nr; 572 } 573 EXPORT_SYMBOL(__task_pid_nr_ns); 574 575 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk) 576 { 577 return ns_of_pid(task_pid(tsk)); 578 } 579 EXPORT_SYMBOL_GPL(task_active_pid_ns); 580 581 /* 582 * Used by proc to find the first pid that is greater than or equal to nr. 583 * 584 * If there is a pid at nr this function is exactly the same as find_pid_ns. 585 */ 586 struct pid *find_ge_pid(int nr, struct pid_namespace *ns) 587 { 588 return idr_get_next(&ns->idr, &nr); 589 } 590 EXPORT_SYMBOL_GPL(find_ge_pid); 591 592 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) 593 { 594 CLASS(fd, f)(fd); 595 struct pid *pid; 596 597 if (fd_empty(f)) 598 return ERR_PTR(-EBADF); 599 600 pid = pidfd_pid(fd_file(f)); 601 if (!IS_ERR(pid)) { 602 get_pid(pid); 603 *flags = fd_file(f)->f_flags; 604 } 605 return pid; 606 } 607 608 /** 609 * pidfd_get_task() - Get the task associated with a pidfd 610 * 611 * @pidfd: pidfd for which to get the task 612 * @flags: flags associated with this pidfd 613 * 614 * Return the task associated with @pidfd. The function takes a reference on 615 * the returned task. The caller is responsible for releasing that reference. 616 * 617 * Return: On success, the task_struct associated with the pidfd. 618 * On error, a negative errno number will be returned. 619 */ 620 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags) 621 { 622 unsigned int f_flags = 0; 623 struct pid *pid; 624 struct task_struct *task; 625 enum pid_type type; 626 627 switch (pidfd) { 628 case PIDFD_SELF_THREAD: 629 type = PIDTYPE_PID; 630 pid = get_task_pid(current, type); 631 break; 632 case PIDFD_SELF_THREAD_GROUP: 633 type = PIDTYPE_TGID; 634 pid = get_task_pid(current, type); 635 break; 636 default: 637 pid = pidfd_get_pid(pidfd, &f_flags); 638 if (IS_ERR(pid)) 639 return ERR_CAST(pid); 640 type = PIDTYPE_TGID; 641 break; 642 } 643 644 task = get_pid_task(pid, type); 645 put_pid(pid); 646 if (!task) 647 return ERR_PTR(-ESRCH); 648 649 *flags = f_flags; 650 return task; 651 } 652 653 /** 654 * pidfd_create() - Create a new pid file descriptor. 655 * 656 * @pid: struct pid that the pidfd will reference 657 * @flags: flags to pass 658 * 659 * This creates a new pid file descriptor with the O_CLOEXEC flag set. 660 * 661 * Note, that this function can only be called after the fd table has 662 * been unshared to avoid leaking the pidfd to the new process. 663 * 664 * This symbol should not be explicitly exported to loadable modules. 665 * 666 * Return: On success, a cloexec pidfd is returned. 667 * On error, a negative errno number will be returned. 668 */ 669 static int pidfd_create(struct pid *pid, unsigned int flags) 670 { 671 int pidfd; 672 struct file *pidfd_file; 673 674 pidfd = pidfd_prepare(pid, flags, &pidfd_file); 675 if (pidfd < 0) 676 return pidfd; 677 678 fd_install(pidfd, pidfd_file); 679 return pidfd; 680 } 681 682 /** 683 * sys_pidfd_open() - Open new pid file descriptor. 684 * 685 * @pid: pid for which to retrieve a pidfd 686 * @flags: flags to pass 687 * 688 * This creates a new pid file descriptor with the O_CLOEXEC flag set for 689 * the task identified by @pid. Without PIDFD_THREAD flag the target task 690 * must be a thread-group leader. 691 * 692 * Return: On success, a cloexec pidfd is returned. 693 * On error, a negative errno number will be returned. 694 */ 695 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) 696 { 697 int fd; 698 struct pid *p; 699 700 if (flags & ~(PIDFD_NONBLOCK | PIDFD_THREAD)) 701 return -EINVAL; 702 703 if (pid <= 0) 704 return -EINVAL; 705 706 p = find_get_pid(pid); 707 if (!p) 708 return -ESRCH; 709 710 fd = pidfd_create(p, flags); 711 712 put_pid(p); 713 return fd; 714 } 715 716 #ifdef CONFIG_SYSCTL 717 static struct ctl_table_set *pid_table_root_lookup(struct ctl_table_root *root) 718 { 719 return &task_active_pid_ns(current)->set; 720 } 721 722 static int set_is_seen(struct ctl_table_set *set) 723 { 724 return &task_active_pid_ns(current)->set == set; 725 } 726 727 static int pid_table_root_permissions(struct ctl_table_header *head, 728 const struct ctl_table *table) 729 { 730 struct pid_namespace *pidns = 731 container_of(head->set, struct pid_namespace, set); 732 int mode = table->mode; 733 734 if (ns_capable_noaudit(pidns->user_ns, CAP_SYS_ADMIN) || 735 uid_eq(current_euid(), make_kuid(pidns->user_ns, 0))) 736 mode = (mode & S_IRWXU) >> 6; 737 else if (in_egroup_p(make_kgid(pidns->user_ns, 0))) 738 mode = (mode & S_IRWXG) >> 3; 739 else 740 mode = mode & S_IROTH; 741 return (mode << 6) | (mode << 3) | mode; 742 } 743 744 static void pid_table_root_set_ownership(struct ctl_table_header *head, 745 kuid_t *uid, kgid_t *gid) 746 { 747 struct pid_namespace *pidns = 748 container_of(head->set, struct pid_namespace, set); 749 kuid_t ns_root_uid; 750 kgid_t ns_root_gid; 751 752 ns_root_uid = make_kuid(pidns->user_ns, 0); 753 if (uid_valid(ns_root_uid)) 754 *uid = ns_root_uid; 755 756 ns_root_gid = make_kgid(pidns->user_ns, 0); 757 if (gid_valid(ns_root_gid)) 758 *gid = ns_root_gid; 759 } 760 761 static struct ctl_table_root pid_table_root = { 762 .lookup = pid_table_root_lookup, 763 .permissions = pid_table_root_permissions, 764 .set_ownership = pid_table_root_set_ownership, 765 }; 766 767 static int proc_do_cad_pid(const struct ctl_table *table, int write, void *buffer, 768 size_t *lenp, loff_t *ppos) 769 { 770 struct pid *new_pid; 771 pid_t tmp_pid; 772 int r; 773 struct ctl_table tmp_table = *table; 774 775 tmp_pid = pid_vnr(cad_pid); 776 tmp_table.data = &tmp_pid; 777 778 r = proc_dointvec(&tmp_table, write, buffer, lenp, ppos); 779 if (r || !write) 780 return r; 781 782 new_pid = find_get_pid(tmp_pid); 783 if (!new_pid) 784 return -ESRCH; 785 786 put_pid(xchg(&cad_pid, new_pid)); 787 return 0; 788 } 789 790 static const struct ctl_table pid_table[] = { 791 { 792 .procname = "pid_max", 793 .data = &init_pid_ns.pid_max, 794 .maxlen = sizeof(int), 795 .mode = 0644, 796 .proc_handler = proc_dointvec_minmax, 797 .extra1 = &pid_max_min, 798 .extra2 = &pid_max_max, 799 }, 800 #ifdef CONFIG_PROC_SYSCTL 801 { 802 .procname = "cad_pid", 803 .maxlen = sizeof(int), 804 .mode = 0600, 805 .proc_handler = proc_do_cad_pid, 806 }, 807 #endif 808 }; 809 #endif 810 811 int register_pidns_sysctls(struct pid_namespace *pidns) 812 { 813 #ifdef CONFIG_SYSCTL 814 struct ctl_table *tbl; 815 816 setup_sysctl_set(&pidns->set, &pid_table_root, set_is_seen); 817 818 tbl = kmemdup(pid_table, sizeof(pid_table), GFP_KERNEL); 819 if (!tbl) 820 return -ENOMEM; 821 tbl->data = &pidns->pid_max; 822 pidns->pid_max = min(pid_max_max, max_t(int, pidns->pid_max, 823 PIDS_PER_CPU_DEFAULT * num_possible_cpus())); 824 825 pidns->sysctls = __register_sysctl_table(&pidns->set, "kernel", tbl, 826 ARRAY_SIZE(pid_table)); 827 if (!pidns->sysctls) { 828 kfree(tbl); 829 retire_sysctl_set(&pidns->set); 830 return -ENOMEM; 831 } 832 #endif 833 return 0; 834 } 835 836 void unregister_pidns_sysctls(struct pid_namespace *pidns) 837 { 838 #ifdef CONFIG_SYSCTL 839 const struct ctl_table *tbl; 840 841 tbl = pidns->sysctls->ctl_table_arg; 842 unregister_sysctl_table(pidns->sysctls); 843 retire_sysctl_set(&pidns->set); 844 kfree(tbl); 845 #endif 846 } 847 848 void __init pid_idr_init(void) 849 { 850 /* Verify no one has done anything silly: */ 851 BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING); 852 853 /* bump default and minimum pid_max based on number of cpus */ 854 init_pid_ns.pid_max = min(pid_max_max, max_t(int, init_pid_ns.pid_max, 855 PIDS_PER_CPU_DEFAULT * num_possible_cpus())); 856 pid_max_min = max_t(int, pid_max_min, 857 PIDS_PER_CPU_MIN * num_possible_cpus()); 858 pr_info("pid_max: default: %u minimum: %u\n", init_pid_ns.pid_max, pid_max_min); 859 860 idr_init(&init_pid_ns.idr); 861 862 init_pid_ns.pid_cachep = kmem_cache_create("pid", 863 struct_size_t(struct pid, numbers, 1), 864 __alignof__(struct pid), 865 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, 866 NULL); 867 } 868 869 static __init int pid_namespace_sysctl_init(void) 870 { 871 #ifdef CONFIG_SYSCTL 872 /* "kernel" directory will have already been initialized. */ 873 BUG_ON(register_pidns_sysctls(&init_pid_ns)); 874 #endif 875 return 0; 876 } 877 subsys_initcall(pid_namespace_sysctl_init); 878 879 static struct file *__pidfd_fget(struct task_struct *task, int fd) 880 { 881 struct file *file; 882 int ret; 883 884 ret = down_read_killable(&task->signal->exec_update_lock); 885 if (ret) 886 return ERR_PTR(ret); 887 888 if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) 889 file = fget_task(task, fd); 890 else 891 file = ERR_PTR(-EPERM); 892 893 up_read(&task->signal->exec_update_lock); 894 895 if (!file) { 896 /* 897 * It is possible that the target thread is exiting; it can be 898 * either: 899 * 1. before exit_signals(), which gives a real fd 900 * 2. before exit_files() takes the task_lock() gives a real fd 901 * 3. after exit_files() releases task_lock(), ->files is NULL; 902 * this has PF_EXITING, since it was set in exit_signals(), 903 * __pidfd_fget() returns EBADF. 904 * In case 3 we get EBADF, but that really means ESRCH, since 905 * the task is currently exiting and has freed its files 906 * struct, so we fix it up. 907 */ 908 if (task->flags & PF_EXITING) 909 file = ERR_PTR(-ESRCH); 910 else 911 file = ERR_PTR(-EBADF); 912 } 913 914 return file; 915 } 916 917 static int pidfd_getfd(struct pid *pid, int fd) 918 { 919 struct task_struct *task; 920 struct file *file; 921 int ret; 922 923 task = get_pid_task(pid, PIDTYPE_PID); 924 if (!task) 925 return -ESRCH; 926 927 file = __pidfd_fget(task, fd); 928 put_task_struct(task); 929 if (IS_ERR(file)) 930 return PTR_ERR(file); 931 932 ret = receive_fd(file, NULL, O_CLOEXEC); 933 fput(file); 934 935 return ret; 936 } 937 938 /** 939 * sys_pidfd_getfd() - Get a file descriptor from another process 940 * 941 * @pidfd: the pidfd file descriptor of the process 942 * @fd: the file descriptor number to get 943 * @flags: flags on how to get the fd (reserved) 944 * 945 * This syscall gets a copy of a file descriptor from another process 946 * based on the pidfd, and file descriptor number. It requires that 947 * the calling process has the ability to ptrace the process represented 948 * by the pidfd. The process which is having its file descriptor copied 949 * is otherwise unaffected. 950 * 951 * Return: On success, a cloexec file descriptor is returned. 952 * On error, a negative errno number will be returned. 953 */ 954 SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, 955 unsigned int, flags) 956 { 957 struct pid *pid; 958 959 /* flags is currently unused - make sure it's unset */ 960 if (flags) 961 return -EINVAL; 962 963 CLASS(fd, f)(pidfd); 964 if (fd_empty(f)) 965 return -EBADF; 966 967 pid = pidfd_pid(fd_file(f)); 968 if (IS_ERR(pid)) 969 return PTR_ERR(pid); 970 971 return pidfd_getfd(pid, fd); 972 } 973