1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/fork.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 /* 9 * 'fork.c' contains the help-routines for the 'fork' system call 10 * (see also entry.S and others). 11 * Fork is rather simple, once you get the hang of it, but the memory 12 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 13 */ 14 15 #include <linux/anon_inodes.h> 16 #include <linux/slab.h> 17 #include <linux/sched/autogroup.h> 18 #include <linux/sched/mm.h> 19 #include <linux/sched/coredump.h> 20 #include <linux/sched/user.h> 21 #include <linux/sched/numa_balancing.h> 22 #include <linux/sched/stat.h> 23 #include <linux/sched/task.h> 24 #include <linux/sched/task_stack.h> 25 #include <linux/sched/cputime.h> 26 #include <linux/seq_file.h> 27 #include <linux/rtmutex.h> 28 #include <linux/init.h> 29 #include <linux/unistd.h> 30 #include <linux/module.h> 31 #include <linux/vmalloc.h> 32 #include <linux/completion.h> 33 #include <linux/personality.h> 34 #include <linux/mempolicy.h> 35 #include <linux/sem.h> 36 #include <linux/file.h> 37 #include <linux/fdtable.h> 38 #include <linux/iocontext.h> 39 #include <linux/key.h> 40 #include <linux/binfmts.h> 41 #include <linux/mman.h> 42 #include <linux/mmu_notifier.h> 43 #include <linux/hmm.h> 44 #include <linux/fs.h> 45 #include <linux/mm.h> 46 #include <linux/vmacache.h> 47 #include <linux/nsproxy.h> 48 #include <linux/capability.h> 49 #include <linux/cpu.h> 50 #include <linux/cgroup.h> 51 #include <linux/security.h> 52 #include <linux/hugetlb.h> 53 #include <linux/seccomp.h> 54 #include <linux/swap.h> 55 #include <linux/syscalls.h> 56 #include <linux/jiffies.h> 57 #include <linux/futex.h> 58 #include <linux/compat.h> 59 #include <linux/kthread.h> 60 #include <linux/task_io_accounting_ops.h> 61 #include <linux/rcupdate.h> 62 #include <linux/ptrace.h> 63 #include <linux/mount.h> 64 #include <linux/audit.h> 65 #include <linux/memcontrol.h> 66 #include <linux/ftrace.h> 67 #include <linux/proc_fs.h> 68 #include <linux/profile.h> 69 #include <linux/rmap.h> 70 #include <linux/ksm.h> 71 #include <linux/acct.h> 72 #include <linux/userfaultfd_k.h> 73 #include <linux/tsacct_kern.h> 74 #include <linux/cn_proc.h> 75 #include <linux/freezer.h> 76 #include <linux/delayacct.h> 77 #include <linux/taskstats_kern.h> 78 #include <linux/random.h> 79 #include <linux/tty.h> 80 #include <linux/blkdev.h> 81 #include <linux/fs_struct.h> 82 #include <linux/magic.h> 83 #include <linux/perf_event.h> 84 #include <linux/posix-timers.h> 85 #include <linux/user-return-notifier.h> 86 #include <linux/oom.h> 87 #include <linux/khugepaged.h> 88 #include <linux/signalfd.h> 89 #include <linux/uprobes.h> 90 #include <linux/aio.h> 91 #include <linux/compiler.h> 92 #include <linux/sysctl.h> 93 #include <linux/kcov.h> 94 #include <linux/livepatch.h> 95 #include <linux/thread_info.h> 96 #include <linux/stackleak.h> 97 98 #include <asm/pgtable.h> 99 #include <asm/pgalloc.h> 100 #include <linux/uaccess.h> 101 #include <asm/mmu_context.h> 102 #include <asm/cacheflush.h> 103 #include <asm/tlbflush.h> 104 105 #include <trace/events/sched.h> 106 107 #define CREATE_TRACE_POINTS 108 #include <trace/events/task.h> 109 110 /* 111 * Minimum number of threads to boot the kernel 112 */ 113 #define MIN_THREADS 20 114 115 /* 116 * Maximum number of threads 117 */ 118 #define MAX_THREADS FUTEX_TID_MASK 119 120 /* 121 * Protected counters by write_lock_irq(&tasklist_lock) 122 */ 123 unsigned long total_forks; /* Handle normal Linux uptimes. */ 124 int nr_threads; /* The idle threads do not count.. */ 125 126 static int max_threads; /* tunable limit on nr_threads */ 127 128 DEFINE_PER_CPU(unsigned long, process_counts) = 0; 129 130 __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 131 132 #ifdef CONFIG_PROVE_RCU 133 int lockdep_tasklist_lock_is_held(void) 134 { 135 return lockdep_is_held(&tasklist_lock); 136 } 137 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 138 #endif /* #ifdef CONFIG_PROVE_RCU */ 139 140 int nr_processes(void) 141 { 142 int cpu; 143 int total = 0; 144 145 for_each_possible_cpu(cpu) 146 total += per_cpu(process_counts, cpu); 147 148 return total; 149 } 150 151 void __weak arch_release_task_struct(struct task_struct *tsk) 152 { 153 } 154 155 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 156 static struct kmem_cache *task_struct_cachep; 157 158 static inline struct task_struct *alloc_task_struct_node(int node) 159 { 160 return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 161 } 162 163 static inline void free_task_struct(struct task_struct *tsk) 164 { 165 kmem_cache_free(task_struct_cachep, tsk); 166 } 167 #endif 168 169 #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 170 171 /* 172 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 173 * kmemcache based allocator. 174 */ 175 # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 176 177 #ifdef CONFIG_VMAP_STACK 178 /* 179 * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 180 * flush. Try to minimize the number of calls by caching stacks. 181 */ 182 #define NR_CACHED_STACKS 2 183 static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 184 185 static int free_vm_stack_cache(unsigned int cpu) 186 { 187 struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 188 int i; 189 190 for (i = 0; i < NR_CACHED_STACKS; i++) { 191 struct vm_struct *vm_stack = cached_vm_stacks[i]; 192 193 if (!vm_stack) 194 continue; 195 196 vfree(vm_stack->addr); 197 cached_vm_stacks[i] = NULL; 198 } 199 200 return 0; 201 } 202 #endif 203 204 static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 205 { 206 #ifdef CONFIG_VMAP_STACK 207 void *stack; 208 int i; 209 210 for (i = 0; i < NR_CACHED_STACKS; i++) { 211 struct vm_struct *s; 212 213 s = this_cpu_xchg(cached_stacks[i], NULL); 214 215 if (!s) 216 continue; 217 218 /* Clear stale pointers from reused stack. */ 219 memset(s->addr, 0, THREAD_SIZE); 220 221 tsk->stack_vm_area = s; 222 tsk->stack = s->addr; 223 return s->addr; 224 } 225 226 /* 227 * Allocated stacks are cached and later reused by new threads, 228 * so memcg accounting is performed manually on assigning/releasing 229 * stacks to tasks. Drop __GFP_ACCOUNT. 230 */ 231 stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 232 VMALLOC_START, VMALLOC_END, 233 THREADINFO_GFP & ~__GFP_ACCOUNT, 234 PAGE_KERNEL, 235 0, node, __builtin_return_address(0)); 236 237 /* 238 * We can't call find_vm_area() in interrupt context, and 239 * free_thread_stack() can be called in interrupt context, 240 * so cache the vm_struct. 241 */ 242 if (stack) { 243 tsk->stack_vm_area = find_vm_area(stack); 244 tsk->stack = stack; 245 } 246 return stack; 247 #else 248 struct page *page = alloc_pages_node(node, THREADINFO_GFP, 249 THREAD_SIZE_ORDER); 250 251 if (likely(page)) { 252 tsk->stack = page_address(page); 253 return tsk->stack; 254 } 255 return NULL; 256 #endif 257 } 258 259 static inline void free_thread_stack(struct task_struct *tsk) 260 { 261 #ifdef CONFIG_VMAP_STACK 262 struct vm_struct *vm = task_stack_vm_area(tsk); 263 264 if (vm) { 265 int i; 266 267 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 268 mod_memcg_page_state(vm->pages[i], 269 MEMCG_KERNEL_STACK_KB, 270 -(int)(PAGE_SIZE / 1024)); 271 272 memcg_kmem_uncharge(vm->pages[i], 0); 273 } 274 275 for (i = 0; i < NR_CACHED_STACKS; i++) { 276 if (this_cpu_cmpxchg(cached_stacks[i], 277 NULL, tsk->stack_vm_area) != NULL) 278 continue; 279 280 return; 281 } 282 283 vfree_atomic(tsk->stack); 284 return; 285 } 286 #endif 287 288 __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 289 } 290 # else 291 static struct kmem_cache *thread_stack_cache; 292 293 static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 294 int node) 295 { 296 unsigned long *stack; 297 stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 298 tsk->stack = stack; 299 return stack; 300 } 301 302 static void free_thread_stack(struct task_struct *tsk) 303 { 304 kmem_cache_free(thread_stack_cache, tsk->stack); 305 } 306 307 void thread_stack_cache_init(void) 308 { 309 thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 310 THREAD_SIZE, THREAD_SIZE, 0, 0, 311 THREAD_SIZE, NULL); 312 BUG_ON(thread_stack_cache == NULL); 313 } 314 # endif 315 #endif 316 317 /* SLAB cache for signal_struct structures (tsk->signal) */ 318 static struct kmem_cache *signal_cachep; 319 320 /* SLAB cache for sighand_struct structures (tsk->sighand) */ 321 struct kmem_cache *sighand_cachep; 322 323 /* SLAB cache for files_struct structures (tsk->files) */ 324 struct kmem_cache *files_cachep; 325 326 /* SLAB cache for fs_struct structures (tsk->fs) */ 327 struct kmem_cache *fs_cachep; 328 329 /* SLAB cache for vm_area_struct structures */ 330 static struct kmem_cache *vm_area_cachep; 331 332 /* SLAB cache for mm_struct structures (tsk->mm) */ 333 static struct kmem_cache *mm_cachep; 334 335 struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) 336 { 337 struct vm_area_struct *vma; 338 339 vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 340 if (vma) 341 vma_init(vma, mm); 342 return vma; 343 } 344 345 struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) 346 { 347 struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 348 349 if (new) { 350 *new = *orig; 351 INIT_LIST_HEAD(&new->anon_vma_chain); 352 } 353 return new; 354 } 355 356 void vm_area_free(struct vm_area_struct *vma) 357 { 358 kmem_cache_free(vm_area_cachep, vma); 359 } 360 361 static void account_kernel_stack(struct task_struct *tsk, int account) 362 { 363 void *stack = task_stack_page(tsk); 364 struct vm_struct *vm = task_stack_vm_area(tsk); 365 366 BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 367 368 if (vm) { 369 int i; 370 371 BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 372 373 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 374 mod_zone_page_state(page_zone(vm->pages[i]), 375 NR_KERNEL_STACK_KB, 376 PAGE_SIZE / 1024 * account); 377 } 378 } else { 379 /* 380 * All stack pages are in the same zone and belong to the 381 * same memcg. 382 */ 383 struct page *first_page = virt_to_page(stack); 384 385 mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, 386 THREAD_SIZE / 1024 * account); 387 388 mod_memcg_page_state(first_page, MEMCG_KERNEL_STACK_KB, 389 account * (THREAD_SIZE / 1024)); 390 } 391 } 392 393 static int memcg_charge_kernel_stack(struct task_struct *tsk) 394 { 395 #ifdef CONFIG_VMAP_STACK 396 struct vm_struct *vm = task_stack_vm_area(tsk); 397 int ret; 398 399 if (vm) { 400 int i; 401 402 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 403 /* 404 * If memcg_kmem_charge() fails, page->mem_cgroup 405 * pointer is NULL, and both memcg_kmem_uncharge() 406 * and mod_memcg_page_state() in free_thread_stack() 407 * will ignore this page. So it's safe. 408 */ 409 ret = memcg_kmem_charge(vm->pages[i], GFP_KERNEL, 0); 410 if (ret) 411 return ret; 412 413 mod_memcg_page_state(vm->pages[i], 414 MEMCG_KERNEL_STACK_KB, 415 PAGE_SIZE / 1024); 416 } 417 } 418 #endif 419 return 0; 420 } 421 422 static void release_task_stack(struct task_struct *tsk) 423 { 424 if (WARN_ON(tsk->state != TASK_DEAD)) 425 return; /* Better to leak the stack than to free prematurely */ 426 427 account_kernel_stack(tsk, -1); 428 free_thread_stack(tsk); 429 tsk->stack = NULL; 430 #ifdef CONFIG_VMAP_STACK 431 tsk->stack_vm_area = NULL; 432 #endif 433 } 434 435 #ifdef CONFIG_THREAD_INFO_IN_TASK 436 void put_task_stack(struct task_struct *tsk) 437 { 438 if (refcount_dec_and_test(&tsk->stack_refcount)) 439 release_task_stack(tsk); 440 } 441 #endif 442 443 void free_task(struct task_struct *tsk) 444 { 445 #ifndef CONFIG_THREAD_INFO_IN_TASK 446 /* 447 * The task is finally done with both the stack and thread_info, 448 * so free both. 449 */ 450 release_task_stack(tsk); 451 #else 452 /* 453 * If the task had a separate stack allocation, it should be gone 454 * by now. 455 */ 456 WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 457 #endif 458 rt_mutex_debug_task_free(tsk); 459 ftrace_graph_exit_task(tsk); 460 put_seccomp_filter(tsk); 461 arch_release_task_struct(tsk); 462 if (tsk->flags & PF_KTHREAD) 463 free_kthread_struct(tsk); 464 free_task_struct(tsk); 465 } 466 EXPORT_SYMBOL(free_task); 467 468 #ifdef CONFIG_MMU 469 static __latent_entropy int dup_mmap(struct mm_struct *mm, 470 struct mm_struct *oldmm) 471 { 472 struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 473 struct rb_node **rb_link, *rb_parent; 474 int retval; 475 unsigned long charge; 476 LIST_HEAD(uf); 477 478 uprobe_start_dup_mmap(); 479 if (down_write_killable(&oldmm->mmap_sem)) { 480 retval = -EINTR; 481 goto fail_uprobe_end; 482 } 483 flush_cache_dup_mm(oldmm); 484 uprobe_dup_mmap(oldmm, mm); 485 /* 486 * Not linked in yet - no deadlock potential: 487 */ 488 down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 489 490 /* No ordering required: file already has been exposed. */ 491 RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 492 493 mm->total_vm = oldmm->total_vm; 494 mm->data_vm = oldmm->data_vm; 495 mm->exec_vm = oldmm->exec_vm; 496 mm->stack_vm = oldmm->stack_vm; 497 498 rb_link = &mm->mm_rb.rb_node; 499 rb_parent = NULL; 500 pprev = &mm->mmap; 501 retval = ksm_fork(mm, oldmm); 502 if (retval) 503 goto out; 504 retval = khugepaged_fork(mm, oldmm); 505 if (retval) 506 goto out; 507 508 prev = NULL; 509 for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 510 struct file *file; 511 512 if (mpnt->vm_flags & VM_DONTCOPY) { 513 vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 514 continue; 515 } 516 charge = 0; 517 /* 518 * Don't duplicate many vmas if we've been oom-killed (for 519 * example) 520 */ 521 if (fatal_signal_pending(current)) { 522 retval = -EINTR; 523 goto out; 524 } 525 if (mpnt->vm_flags & VM_ACCOUNT) { 526 unsigned long len = vma_pages(mpnt); 527 528 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 529 goto fail_nomem; 530 charge = len; 531 } 532 tmp = vm_area_dup(mpnt); 533 if (!tmp) 534 goto fail_nomem; 535 retval = vma_dup_policy(mpnt, tmp); 536 if (retval) 537 goto fail_nomem_policy; 538 tmp->vm_mm = mm; 539 retval = dup_userfaultfd(tmp, &uf); 540 if (retval) 541 goto fail_nomem_anon_vma_fork; 542 if (tmp->vm_flags & VM_WIPEONFORK) { 543 /* VM_WIPEONFORK gets a clean slate in the child. */ 544 tmp->anon_vma = NULL; 545 if (anon_vma_prepare(tmp)) 546 goto fail_nomem_anon_vma_fork; 547 } else if (anon_vma_fork(tmp, mpnt)) 548 goto fail_nomem_anon_vma_fork; 549 tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 550 tmp->vm_next = tmp->vm_prev = NULL; 551 file = tmp->vm_file; 552 if (file) { 553 struct inode *inode = file_inode(file); 554 struct address_space *mapping = file->f_mapping; 555 556 get_file(file); 557 if (tmp->vm_flags & VM_DENYWRITE) 558 atomic_dec(&inode->i_writecount); 559 i_mmap_lock_write(mapping); 560 if (tmp->vm_flags & VM_SHARED) 561 atomic_inc(&mapping->i_mmap_writable); 562 flush_dcache_mmap_lock(mapping); 563 /* insert tmp into the share list, just after mpnt */ 564 vma_interval_tree_insert_after(tmp, mpnt, 565 &mapping->i_mmap); 566 flush_dcache_mmap_unlock(mapping); 567 i_mmap_unlock_write(mapping); 568 } 569 570 /* 571 * Clear hugetlb-related page reserves for children. This only 572 * affects MAP_PRIVATE mappings. Faults generated by the child 573 * are not guaranteed to succeed, even if read-only 574 */ 575 if (is_vm_hugetlb_page(tmp)) 576 reset_vma_resv_huge_pages(tmp); 577 578 /* 579 * Link in the new vma and copy the page table entries. 580 */ 581 *pprev = tmp; 582 pprev = &tmp->vm_next; 583 tmp->vm_prev = prev; 584 prev = tmp; 585 586 __vma_link_rb(mm, tmp, rb_link, rb_parent); 587 rb_link = &tmp->vm_rb.rb_right; 588 rb_parent = &tmp->vm_rb; 589 590 mm->map_count++; 591 if (!(tmp->vm_flags & VM_WIPEONFORK)) 592 retval = copy_page_range(mm, oldmm, mpnt); 593 594 if (tmp->vm_ops && tmp->vm_ops->open) 595 tmp->vm_ops->open(tmp); 596 597 if (retval) 598 goto out; 599 } 600 /* a new mm has just been created */ 601 retval = arch_dup_mmap(oldmm, mm); 602 out: 603 up_write(&mm->mmap_sem); 604 flush_tlb_mm(oldmm); 605 up_write(&oldmm->mmap_sem); 606 dup_userfaultfd_complete(&uf); 607 fail_uprobe_end: 608 uprobe_end_dup_mmap(); 609 return retval; 610 fail_nomem_anon_vma_fork: 611 mpol_put(vma_policy(tmp)); 612 fail_nomem_policy: 613 vm_area_free(tmp); 614 fail_nomem: 615 retval = -ENOMEM; 616 vm_unacct_memory(charge); 617 goto out; 618 } 619 620 static inline int mm_alloc_pgd(struct mm_struct *mm) 621 { 622 mm->pgd = pgd_alloc(mm); 623 if (unlikely(!mm->pgd)) 624 return -ENOMEM; 625 return 0; 626 } 627 628 static inline void mm_free_pgd(struct mm_struct *mm) 629 { 630 pgd_free(mm, mm->pgd); 631 } 632 #else 633 static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 634 { 635 down_write(&oldmm->mmap_sem); 636 RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 637 up_write(&oldmm->mmap_sem); 638 return 0; 639 } 640 #define mm_alloc_pgd(mm) (0) 641 #define mm_free_pgd(mm) 642 #endif /* CONFIG_MMU */ 643 644 static void check_mm(struct mm_struct *mm) 645 { 646 int i; 647 648 for (i = 0; i < NR_MM_COUNTERS; i++) { 649 long x = atomic_long_read(&mm->rss_stat.count[i]); 650 651 if (unlikely(x)) 652 printk(KERN_ALERT "BUG: Bad rss-counter state " 653 "mm:%p idx:%d val:%ld\n", mm, i, x); 654 } 655 656 if (mm_pgtables_bytes(mm)) 657 pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 658 mm_pgtables_bytes(mm)); 659 660 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 661 VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 662 #endif 663 } 664 665 #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 666 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 667 668 /* 669 * Called when the last reference to the mm 670 * is dropped: either by a lazy thread or by 671 * mmput. Free the page directory and the mm. 672 */ 673 void __mmdrop(struct mm_struct *mm) 674 { 675 BUG_ON(mm == &init_mm); 676 WARN_ON_ONCE(mm == current->mm); 677 WARN_ON_ONCE(mm == current->active_mm); 678 mm_free_pgd(mm); 679 destroy_context(mm); 680 mmu_notifier_mm_destroy(mm); 681 check_mm(mm); 682 put_user_ns(mm->user_ns); 683 free_mm(mm); 684 } 685 EXPORT_SYMBOL_GPL(__mmdrop); 686 687 static void mmdrop_async_fn(struct work_struct *work) 688 { 689 struct mm_struct *mm; 690 691 mm = container_of(work, struct mm_struct, async_put_work); 692 __mmdrop(mm); 693 } 694 695 static void mmdrop_async(struct mm_struct *mm) 696 { 697 if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 698 INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 699 schedule_work(&mm->async_put_work); 700 } 701 } 702 703 static inline void free_signal_struct(struct signal_struct *sig) 704 { 705 taskstats_tgid_free(sig); 706 sched_autogroup_exit(sig); 707 /* 708 * __mmdrop is not safe to call from softirq context on x86 due to 709 * pgd_dtor so postpone it to the async context 710 */ 711 if (sig->oom_mm) 712 mmdrop_async(sig->oom_mm); 713 kmem_cache_free(signal_cachep, sig); 714 } 715 716 static inline void put_signal_struct(struct signal_struct *sig) 717 { 718 if (refcount_dec_and_test(&sig->sigcnt)) 719 free_signal_struct(sig); 720 } 721 722 void __put_task_struct(struct task_struct *tsk) 723 { 724 WARN_ON(!tsk->exit_state); 725 WARN_ON(refcount_read(&tsk->usage)); 726 WARN_ON(tsk == current); 727 728 cgroup_free(tsk); 729 task_numa_free(tsk, true); 730 security_task_free(tsk); 731 exit_creds(tsk); 732 delayacct_tsk_free(tsk); 733 put_signal_struct(tsk->signal); 734 735 if (!profile_handoff_task(tsk)) 736 free_task(tsk); 737 } 738 EXPORT_SYMBOL_GPL(__put_task_struct); 739 740 void __init __weak arch_task_cache_init(void) { } 741 742 /* 743 * set_max_threads 744 */ 745 static void set_max_threads(unsigned int max_threads_suggested) 746 { 747 u64 threads; 748 unsigned long nr_pages = totalram_pages(); 749 750 /* 751 * The number of threads shall be limited such that the thread 752 * structures may only consume a small part of the available memory. 753 */ 754 if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 755 threads = MAX_THREADS; 756 else 757 threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 758 (u64) THREAD_SIZE * 8UL); 759 760 if (threads > max_threads_suggested) 761 threads = max_threads_suggested; 762 763 max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 764 } 765 766 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 767 /* Initialized by the architecture: */ 768 int arch_task_struct_size __read_mostly; 769 #endif 770 771 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 772 static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 773 { 774 /* Fetch thread_struct whitelist for the architecture. */ 775 arch_thread_struct_whitelist(offset, size); 776 777 /* 778 * Handle zero-sized whitelist or empty thread_struct, otherwise 779 * adjust offset to position of thread_struct in task_struct. 780 */ 781 if (unlikely(*size == 0)) 782 *offset = 0; 783 else 784 *offset += offsetof(struct task_struct, thread); 785 } 786 #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ 787 788 void __init fork_init(void) 789 { 790 int i; 791 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 792 #ifndef ARCH_MIN_TASKALIGN 793 #define ARCH_MIN_TASKALIGN 0 794 #endif 795 int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 796 unsigned long useroffset, usersize; 797 798 /* create a slab on which task_structs can be allocated */ 799 task_struct_whitelist(&useroffset, &usersize); 800 task_struct_cachep = kmem_cache_create_usercopy("task_struct", 801 arch_task_struct_size, align, 802 SLAB_PANIC|SLAB_ACCOUNT, 803 useroffset, usersize, NULL); 804 #endif 805 806 /* do the arch specific task caches init */ 807 arch_task_cache_init(); 808 809 set_max_threads(MAX_THREADS); 810 811 init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 812 init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 813 init_task.signal->rlim[RLIMIT_SIGPENDING] = 814 init_task.signal->rlim[RLIMIT_NPROC]; 815 816 for (i = 0; i < UCOUNT_COUNTS; i++) { 817 init_user_ns.ucount_max[i] = max_threads/2; 818 } 819 820 #ifdef CONFIG_VMAP_STACK 821 cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 822 NULL, free_vm_stack_cache); 823 #endif 824 825 lockdep_init_task(&init_task); 826 uprobes_init(); 827 } 828 829 int __weak arch_dup_task_struct(struct task_struct *dst, 830 struct task_struct *src) 831 { 832 *dst = *src; 833 return 0; 834 } 835 836 void set_task_stack_end_magic(struct task_struct *tsk) 837 { 838 unsigned long *stackend; 839 840 stackend = end_of_stack(tsk); 841 *stackend = STACK_END_MAGIC; /* for overflow detection */ 842 } 843 844 static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 845 { 846 struct task_struct *tsk; 847 unsigned long *stack; 848 struct vm_struct *stack_vm_area __maybe_unused; 849 int err; 850 851 if (node == NUMA_NO_NODE) 852 node = tsk_fork_get_node(orig); 853 tsk = alloc_task_struct_node(node); 854 if (!tsk) 855 return NULL; 856 857 stack = alloc_thread_stack_node(tsk, node); 858 if (!stack) 859 goto free_tsk; 860 861 if (memcg_charge_kernel_stack(tsk)) 862 goto free_stack; 863 864 stack_vm_area = task_stack_vm_area(tsk); 865 866 err = arch_dup_task_struct(tsk, orig); 867 868 /* 869 * arch_dup_task_struct() clobbers the stack-related fields. Make 870 * sure they're properly initialized before using any stack-related 871 * functions again. 872 */ 873 tsk->stack = stack; 874 #ifdef CONFIG_VMAP_STACK 875 tsk->stack_vm_area = stack_vm_area; 876 #endif 877 #ifdef CONFIG_THREAD_INFO_IN_TASK 878 refcount_set(&tsk->stack_refcount, 1); 879 #endif 880 881 if (err) 882 goto free_stack; 883 884 #ifdef CONFIG_SECCOMP 885 /* 886 * We must handle setting up seccomp filters once we're under 887 * the sighand lock in case orig has changed between now and 888 * then. Until then, filter must be NULL to avoid messing up 889 * the usage counts on the error path calling free_task. 890 */ 891 tsk->seccomp.filter = NULL; 892 #endif 893 894 setup_thread_stack(tsk, orig); 895 clear_user_return_notifier(tsk); 896 clear_tsk_need_resched(tsk); 897 set_task_stack_end_magic(tsk); 898 899 #ifdef CONFIG_STACKPROTECTOR 900 tsk->stack_canary = get_random_canary(); 901 #endif 902 if (orig->cpus_ptr == &orig->cpus_mask) 903 tsk->cpus_ptr = &tsk->cpus_mask; 904 905 /* 906 * One for the user space visible state that goes away when reaped. 907 * One for the scheduler. 908 */ 909 refcount_set(&tsk->rcu_users, 2); 910 /* One for the rcu users */ 911 refcount_set(&tsk->usage, 1); 912 #ifdef CONFIG_BLK_DEV_IO_TRACE 913 tsk->btrace_seq = 0; 914 #endif 915 tsk->splice_pipe = NULL; 916 tsk->task_frag.page = NULL; 917 tsk->wake_q.next = NULL; 918 919 account_kernel_stack(tsk, 1); 920 921 kcov_task_init(tsk); 922 923 #ifdef CONFIG_FAULT_INJECTION 924 tsk->fail_nth = 0; 925 #endif 926 927 #ifdef CONFIG_BLK_CGROUP 928 tsk->throttle_queue = NULL; 929 tsk->use_memdelay = 0; 930 #endif 931 932 #ifdef CONFIG_MEMCG 933 tsk->active_memcg = NULL; 934 #endif 935 return tsk; 936 937 free_stack: 938 free_thread_stack(tsk); 939 free_tsk: 940 free_task_struct(tsk); 941 return NULL; 942 } 943 944 __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 945 946 static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 947 948 static int __init coredump_filter_setup(char *s) 949 { 950 default_dump_filter = 951 (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 952 MMF_DUMP_FILTER_MASK; 953 return 1; 954 } 955 956 __setup("coredump_filter=", coredump_filter_setup); 957 958 #include <linux/init_task.h> 959 960 static void mm_init_aio(struct mm_struct *mm) 961 { 962 #ifdef CONFIG_AIO 963 spin_lock_init(&mm->ioctx_lock); 964 mm->ioctx_table = NULL; 965 #endif 966 } 967 968 static __always_inline void mm_clear_owner(struct mm_struct *mm, 969 struct task_struct *p) 970 { 971 #ifdef CONFIG_MEMCG 972 if (mm->owner == p) 973 WRITE_ONCE(mm->owner, NULL); 974 #endif 975 } 976 977 static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 978 { 979 #ifdef CONFIG_MEMCG 980 mm->owner = p; 981 #endif 982 } 983 984 static void mm_init_uprobes_state(struct mm_struct *mm) 985 { 986 #ifdef CONFIG_UPROBES 987 mm->uprobes_state.xol_area = NULL; 988 #endif 989 } 990 991 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 992 struct user_namespace *user_ns) 993 { 994 mm->mmap = NULL; 995 mm->mm_rb = RB_ROOT; 996 mm->vmacache_seqnum = 0; 997 atomic_set(&mm->mm_users, 1); 998 atomic_set(&mm->mm_count, 1); 999 init_rwsem(&mm->mmap_sem); 1000 INIT_LIST_HEAD(&mm->mmlist); 1001 mm->core_state = NULL; 1002 mm_pgtables_bytes_init(mm); 1003 mm->map_count = 0; 1004 mm->locked_vm = 0; 1005 atomic64_set(&mm->pinned_vm, 0); 1006 memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 1007 spin_lock_init(&mm->page_table_lock); 1008 spin_lock_init(&mm->arg_lock); 1009 mm_init_cpumask(mm); 1010 mm_init_aio(mm); 1011 mm_init_owner(mm, p); 1012 RCU_INIT_POINTER(mm->exe_file, NULL); 1013 mmu_notifier_mm_init(mm); 1014 hmm_mm_init(mm); 1015 init_tlb_flush_pending(mm); 1016 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 1017 mm->pmd_huge_pte = NULL; 1018 #endif 1019 mm_init_uprobes_state(mm); 1020 1021 if (current->mm) { 1022 mm->flags = current->mm->flags & MMF_INIT_MASK; 1023 mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1024 } else { 1025 mm->flags = default_dump_filter; 1026 mm->def_flags = 0; 1027 } 1028 1029 if (mm_alloc_pgd(mm)) 1030 goto fail_nopgd; 1031 1032 if (init_new_context(p, mm)) 1033 goto fail_nocontext; 1034 1035 mm->user_ns = get_user_ns(user_ns); 1036 return mm; 1037 1038 fail_nocontext: 1039 mm_free_pgd(mm); 1040 fail_nopgd: 1041 free_mm(mm); 1042 return NULL; 1043 } 1044 1045 /* 1046 * Allocate and initialize an mm_struct. 1047 */ 1048 struct mm_struct *mm_alloc(void) 1049 { 1050 struct mm_struct *mm; 1051 1052 mm = allocate_mm(); 1053 if (!mm) 1054 return NULL; 1055 1056 memset(mm, 0, sizeof(*mm)); 1057 return mm_init(mm, current, current_user_ns()); 1058 } 1059 1060 static inline void __mmput(struct mm_struct *mm) 1061 { 1062 VM_BUG_ON(atomic_read(&mm->mm_users)); 1063 1064 uprobe_clear_state(mm); 1065 exit_aio(mm); 1066 ksm_exit(mm); 1067 khugepaged_exit(mm); /* must run before exit_mmap */ 1068 exit_mmap(mm); 1069 mm_put_huge_zero_page(mm); 1070 set_mm_exe_file(mm, NULL); 1071 if (!list_empty(&mm->mmlist)) { 1072 spin_lock(&mmlist_lock); 1073 list_del(&mm->mmlist); 1074 spin_unlock(&mmlist_lock); 1075 } 1076 if (mm->binfmt) 1077 module_put(mm->binfmt->module); 1078 mmdrop(mm); 1079 } 1080 1081 /* 1082 * Decrement the use count and release all resources for an mm. 1083 */ 1084 void mmput(struct mm_struct *mm) 1085 { 1086 might_sleep(); 1087 1088 if (atomic_dec_and_test(&mm->mm_users)) 1089 __mmput(mm); 1090 } 1091 EXPORT_SYMBOL_GPL(mmput); 1092 1093 #ifdef CONFIG_MMU 1094 static void mmput_async_fn(struct work_struct *work) 1095 { 1096 struct mm_struct *mm = container_of(work, struct mm_struct, 1097 async_put_work); 1098 1099 __mmput(mm); 1100 } 1101 1102 void mmput_async(struct mm_struct *mm) 1103 { 1104 if (atomic_dec_and_test(&mm->mm_users)) { 1105 INIT_WORK(&mm->async_put_work, mmput_async_fn); 1106 schedule_work(&mm->async_put_work); 1107 } 1108 } 1109 #endif 1110 1111 /** 1112 * set_mm_exe_file - change a reference to the mm's executable file 1113 * 1114 * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 1115 * 1116 * Main users are mmput() and sys_execve(). Callers prevent concurrent 1117 * invocations: in mmput() nobody alive left, in execve task is single 1118 * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 1119 * mm->exe_file, but does so without using set_mm_exe_file() in order 1120 * to do avoid the need for any locks. 1121 */ 1122 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1123 { 1124 struct file *old_exe_file; 1125 1126 /* 1127 * It is safe to dereference the exe_file without RCU as 1128 * this function is only called if nobody else can access 1129 * this mm -- see comment above for justification. 1130 */ 1131 old_exe_file = rcu_dereference_raw(mm->exe_file); 1132 1133 if (new_exe_file) 1134 get_file(new_exe_file); 1135 rcu_assign_pointer(mm->exe_file, new_exe_file); 1136 if (old_exe_file) 1137 fput(old_exe_file); 1138 } 1139 1140 /** 1141 * get_mm_exe_file - acquire a reference to the mm's executable file 1142 * 1143 * Returns %NULL if mm has no associated executable file. 1144 * User must release file via fput(). 1145 */ 1146 struct file *get_mm_exe_file(struct mm_struct *mm) 1147 { 1148 struct file *exe_file; 1149 1150 rcu_read_lock(); 1151 exe_file = rcu_dereference(mm->exe_file); 1152 if (exe_file && !get_file_rcu(exe_file)) 1153 exe_file = NULL; 1154 rcu_read_unlock(); 1155 return exe_file; 1156 } 1157 EXPORT_SYMBOL(get_mm_exe_file); 1158 1159 /** 1160 * get_task_exe_file - acquire a reference to the task's executable file 1161 * 1162 * Returns %NULL if task's mm (if any) has no associated executable file or 1163 * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1164 * User must release file via fput(). 1165 */ 1166 struct file *get_task_exe_file(struct task_struct *task) 1167 { 1168 struct file *exe_file = NULL; 1169 struct mm_struct *mm; 1170 1171 task_lock(task); 1172 mm = task->mm; 1173 if (mm) { 1174 if (!(task->flags & PF_KTHREAD)) 1175 exe_file = get_mm_exe_file(mm); 1176 } 1177 task_unlock(task); 1178 return exe_file; 1179 } 1180 EXPORT_SYMBOL(get_task_exe_file); 1181 1182 /** 1183 * get_task_mm - acquire a reference to the task's mm 1184 * 1185 * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 1186 * this kernel workthread has transiently adopted a user mm with use_mm, 1187 * to do its AIO) is not set and if so returns a reference to it, after 1188 * bumping up the use count. User must release the mm via mmput() 1189 * after use. Typically used by /proc and ptrace. 1190 */ 1191 struct mm_struct *get_task_mm(struct task_struct *task) 1192 { 1193 struct mm_struct *mm; 1194 1195 task_lock(task); 1196 mm = task->mm; 1197 if (mm) { 1198 if (task->flags & PF_KTHREAD) 1199 mm = NULL; 1200 else 1201 mmget(mm); 1202 } 1203 task_unlock(task); 1204 return mm; 1205 } 1206 EXPORT_SYMBOL_GPL(get_task_mm); 1207 1208 struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 1209 { 1210 struct mm_struct *mm; 1211 int err; 1212 1213 err = mutex_lock_killable(&task->signal->cred_guard_mutex); 1214 if (err) 1215 return ERR_PTR(err); 1216 1217 mm = get_task_mm(task); 1218 if (mm && mm != current->mm && 1219 !ptrace_may_access(task, mode)) { 1220 mmput(mm); 1221 mm = ERR_PTR(-EACCES); 1222 } 1223 mutex_unlock(&task->signal->cred_guard_mutex); 1224 1225 return mm; 1226 } 1227 1228 static void complete_vfork_done(struct task_struct *tsk) 1229 { 1230 struct completion *vfork; 1231 1232 task_lock(tsk); 1233 vfork = tsk->vfork_done; 1234 if (likely(vfork)) { 1235 tsk->vfork_done = NULL; 1236 complete(vfork); 1237 } 1238 task_unlock(tsk); 1239 } 1240 1241 static int wait_for_vfork_done(struct task_struct *child, 1242 struct completion *vfork) 1243 { 1244 int killed; 1245 1246 freezer_do_not_count(); 1247 cgroup_enter_frozen(); 1248 killed = wait_for_completion_killable(vfork); 1249 cgroup_leave_frozen(false); 1250 freezer_count(); 1251 1252 if (killed) { 1253 task_lock(child); 1254 child->vfork_done = NULL; 1255 task_unlock(child); 1256 } 1257 1258 put_task_struct(child); 1259 return killed; 1260 } 1261 1262 /* Please note the differences between mmput and mm_release. 1263 * mmput is called whenever we stop holding onto a mm_struct, 1264 * error success whatever. 1265 * 1266 * mm_release is called after a mm_struct has been removed 1267 * from the current process. 1268 * 1269 * This difference is important for error handling, when we 1270 * only half set up a mm_struct for a new process and need to restore 1271 * the old one. Because we mmput the new mm_struct before 1272 * restoring the old one. . . 1273 * Eric Biederman 10 January 1998 1274 */ 1275 void mm_release(struct task_struct *tsk, struct mm_struct *mm) 1276 { 1277 /* Get rid of any futexes when releasing the mm */ 1278 #ifdef CONFIG_FUTEX 1279 if (unlikely(tsk->robust_list)) { 1280 exit_robust_list(tsk); 1281 tsk->robust_list = NULL; 1282 } 1283 #ifdef CONFIG_COMPAT 1284 if (unlikely(tsk->compat_robust_list)) { 1285 compat_exit_robust_list(tsk); 1286 tsk->compat_robust_list = NULL; 1287 } 1288 #endif 1289 if (unlikely(!list_empty(&tsk->pi_state_list))) 1290 exit_pi_state_list(tsk); 1291 #endif 1292 1293 uprobe_free_utask(tsk); 1294 1295 /* Get rid of any cached register state */ 1296 deactivate_mm(tsk, mm); 1297 1298 /* 1299 * Signal userspace if we're not exiting with a core dump 1300 * because we want to leave the value intact for debugging 1301 * purposes. 1302 */ 1303 if (tsk->clear_child_tid) { 1304 if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 1305 atomic_read(&mm->mm_users) > 1) { 1306 /* 1307 * We don't check the error code - if userspace has 1308 * not set up a proper pointer then tough luck. 1309 */ 1310 put_user(0, tsk->clear_child_tid); 1311 do_futex(tsk->clear_child_tid, FUTEX_WAKE, 1312 1, NULL, NULL, 0, 0); 1313 } 1314 tsk->clear_child_tid = NULL; 1315 } 1316 1317 /* 1318 * All done, finally we can wake up parent and return this mm to him. 1319 * Also kthread_stop() uses this completion for synchronization. 1320 */ 1321 if (tsk->vfork_done) 1322 complete_vfork_done(tsk); 1323 } 1324 1325 /** 1326 * dup_mm() - duplicates an existing mm structure 1327 * @tsk: the task_struct with which the new mm will be associated. 1328 * @oldmm: the mm to duplicate. 1329 * 1330 * Allocates a new mm structure and duplicates the provided @oldmm structure 1331 * content into it. 1332 * 1333 * Return: the duplicated mm or NULL on failure. 1334 */ 1335 static struct mm_struct *dup_mm(struct task_struct *tsk, 1336 struct mm_struct *oldmm) 1337 { 1338 struct mm_struct *mm; 1339 int err; 1340 1341 mm = allocate_mm(); 1342 if (!mm) 1343 goto fail_nomem; 1344 1345 memcpy(mm, oldmm, sizeof(*mm)); 1346 1347 if (!mm_init(mm, tsk, mm->user_ns)) 1348 goto fail_nomem; 1349 1350 err = dup_mmap(mm, oldmm); 1351 if (err) 1352 goto free_pt; 1353 1354 mm->hiwater_rss = get_mm_rss(mm); 1355 mm->hiwater_vm = mm->total_vm; 1356 1357 if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1358 goto free_pt; 1359 1360 return mm; 1361 1362 free_pt: 1363 /* don't put binfmt in mmput, we haven't got module yet */ 1364 mm->binfmt = NULL; 1365 mm_init_owner(mm, NULL); 1366 mmput(mm); 1367 1368 fail_nomem: 1369 return NULL; 1370 } 1371 1372 static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 1373 { 1374 struct mm_struct *mm, *oldmm; 1375 int retval; 1376 1377 tsk->min_flt = tsk->maj_flt = 0; 1378 tsk->nvcsw = tsk->nivcsw = 0; 1379 #ifdef CONFIG_DETECT_HUNG_TASK 1380 tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1381 tsk->last_switch_time = 0; 1382 #endif 1383 1384 tsk->mm = NULL; 1385 tsk->active_mm = NULL; 1386 1387 /* 1388 * Are we cloning a kernel thread? 1389 * 1390 * We need to steal a active VM for that.. 1391 */ 1392 oldmm = current->mm; 1393 if (!oldmm) 1394 return 0; 1395 1396 /* initialize the new vmacache entries */ 1397 vmacache_flush(tsk); 1398 1399 if (clone_flags & CLONE_VM) { 1400 mmget(oldmm); 1401 mm = oldmm; 1402 goto good_mm; 1403 } 1404 1405 retval = -ENOMEM; 1406 mm = dup_mm(tsk, current->mm); 1407 if (!mm) 1408 goto fail_nomem; 1409 1410 good_mm: 1411 tsk->mm = mm; 1412 tsk->active_mm = mm; 1413 return 0; 1414 1415 fail_nomem: 1416 return retval; 1417 } 1418 1419 static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 1420 { 1421 struct fs_struct *fs = current->fs; 1422 if (clone_flags & CLONE_FS) { 1423 /* tsk->fs is already what we want */ 1424 spin_lock(&fs->lock); 1425 if (fs->in_exec) { 1426 spin_unlock(&fs->lock); 1427 return -EAGAIN; 1428 } 1429 fs->users++; 1430 spin_unlock(&fs->lock); 1431 return 0; 1432 } 1433 tsk->fs = copy_fs_struct(fs); 1434 if (!tsk->fs) 1435 return -ENOMEM; 1436 return 0; 1437 } 1438 1439 static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1440 { 1441 struct files_struct *oldf, *newf; 1442 int error = 0; 1443 1444 /* 1445 * A background process may not have any files ... 1446 */ 1447 oldf = current->files; 1448 if (!oldf) 1449 goto out; 1450 1451 if (clone_flags & CLONE_FILES) { 1452 atomic_inc(&oldf->count); 1453 goto out; 1454 } 1455 1456 newf = dup_fd(oldf, &error); 1457 if (!newf) 1458 goto out; 1459 1460 tsk->files = newf; 1461 error = 0; 1462 out: 1463 return error; 1464 } 1465 1466 static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1467 { 1468 #ifdef CONFIG_BLOCK 1469 struct io_context *ioc = current->io_context; 1470 struct io_context *new_ioc; 1471 1472 if (!ioc) 1473 return 0; 1474 /* 1475 * Share io context with parent, if CLONE_IO is set 1476 */ 1477 if (clone_flags & CLONE_IO) { 1478 ioc_task_link(ioc); 1479 tsk->io_context = ioc; 1480 } else if (ioprio_valid(ioc->ioprio)) { 1481 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 1482 if (unlikely(!new_ioc)) 1483 return -ENOMEM; 1484 1485 new_ioc->ioprio = ioc->ioprio; 1486 put_io_context(new_ioc); 1487 } 1488 #endif 1489 return 0; 1490 } 1491 1492 static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 1493 { 1494 struct sighand_struct *sig; 1495 1496 if (clone_flags & CLONE_SIGHAND) { 1497 refcount_inc(¤t->sighand->count); 1498 return 0; 1499 } 1500 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1501 rcu_assign_pointer(tsk->sighand, sig); 1502 if (!sig) 1503 return -ENOMEM; 1504 1505 refcount_set(&sig->count, 1); 1506 spin_lock_irq(¤t->sighand->siglock); 1507 memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 1508 spin_unlock_irq(¤t->sighand->siglock); 1509 return 0; 1510 } 1511 1512 void __cleanup_sighand(struct sighand_struct *sighand) 1513 { 1514 if (refcount_dec_and_test(&sighand->count)) { 1515 signalfd_cleanup(sighand); 1516 /* 1517 * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1518 * without an RCU grace period, see __lock_task_sighand(). 1519 */ 1520 kmem_cache_free(sighand_cachep, sighand); 1521 } 1522 } 1523 1524 #ifdef CONFIG_POSIX_TIMERS 1525 /* 1526 * Initialize POSIX timer handling for a thread group. 1527 */ 1528 static void posix_cpu_timers_init_group(struct signal_struct *sig) 1529 { 1530 unsigned long cpu_limit; 1531 1532 cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 1533 if (cpu_limit != RLIM_INFINITY) { 1534 sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC; 1535 sig->cputimer.running = true; 1536 } 1537 1538 /* The timer lists. */ 1539 INIT_LIST_HEAD(&sig->cpu_timers[0]); 1540 INIT_LIST_HEAD(&sig->cpu_timers[1]); 1541 INIT_LIST_HEAD(&sig->cpu_timers[2]); 1542 } 1543 #else 1544 static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { } 1545 #endif 1546 1547 static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 1548 { 1549 struct signal_struct *sig; 1550 1551 if (clone_flags & CLONE_THREAD) 1552 return 0; 1553 1554 sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 1555 tsk->signal = sig; 1556 if (!sig) 1557 return -ENOMEM; 1558 1559 sig->nr_threads = 1; 1560 atomic_set(&sig->live, 1); 1561 refcount_set(&sig->sigcnt, 1); 1562 1563 /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 1564 sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 1565 tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 1566 1567 init_waitqueue_head(&sig->wait_chldexit); 1568 sig->curr_target = tsk; 1569 init_sigpending(&sig->shared_pending); 1570 INIT_HLIST_HEAD(&sig->multiprocess); 1571 seqlock_init(&sig->stats_lock); 1572 prev_cputime_init(&sig->prev_cputime); 1573 1574 #ifdef CONFIG_POSIX_TIMERS 1575 INIT_LIST_HEAD(&sig->posix_timers); 1576 hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1577 sig->real_timer.function = it_real_fn; 1578 #endif 1579 1580 task_lock(current->group_leader); 1581 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 1582 task_unlock(current->group_leader); 1583 1584 posix_cpu_timers_init_group(sig); 1585 1586 tty_audit_fork(sig); 1587 sched_autogroup_fork(sig); 1588 1589 sig->oom_score_adj = current->signal->oom_score_adj; 1590 sig->oom_score_adj_min = current->signal->oom_score_adj_min; 1591 1592 mutex_init(&sig->cred_guard_mutex); 1593 1594 return 0; 1595 } 1596 1597 static void copy_seccomp(struct task_struct *p) 1598 { 1599 #ifdef CONFIG_SECCOMP 1600 /* 1601 * Must be called with sighand->lock held, which is common to 1602 * all threads in the group. Holding cred_guard_mutex is not 1603 * needed because this new task is not yet running and cannot 1604 * be racing exec. 1605 */ 1606 assert_spin_locked(¤t->sighand->siglock); 1607 1608 /* Ref-count the new filter user, and assign it. */ 1609 get_seccomp_filter(current); 1610 p->seccomp = current->seccomp; 1611 1612 /* 1613 * Explicitly enable no_new_privs here in case it got set 1614 * between the task_struct being duplicated and holding the 1615 * sighand lock. The seccomp state and nnp must be in sync. 1616 */ 1617 if (task_no_new_privs(current)) 1618 task_set_no_new_privs(p); 1619 1620 /* 1621 * If the parent gained a seccomp mode after copying thread 1622 * flags and between before we held the sighand lock, we have 1623 * to manually enable the seccomp thread flag here. 1624 */ 1625 if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1626 set_tsk_thread_flag(p, TIF_SECCOMP); 1627 #endif 1628 } 1629 1630 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 1631 { 1632 current->clear_child_tid = tidptr; 1633 1634 return task_pid_vnr(current); 1635 } 1636 1637 static void rt_mutex_init_task(struct task_struct *p) 1638 { 1639 raw_spin_lock_init(&p->pi_lock); 1640 #ifdef CONFIG_RT_MUTEXES 1641 p->pi_waiters = RB_ROOT_CACHED; 1642 p->pi_top_task = NULL; 1643 p->pi_blocked_on = NULL; 1644 #endif 1645 } 1646 1647 #ifdef CONFIG_POSIX_TIMERS 1648 /* 1649 * Initialize POSIX timer handling for a single task. 1650 */ 1651 static void posix_cpu_timers_init(struct task_struct *tsk) 1652 { 1653 tsk->cputime_expires.prof_exp = 0; 1654 tsk->cputime_expires.virt_exp = 0; 1655 tsk->cputime_expires.sched_exp = 0; 1656 INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1657 INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1658 INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1659 } 1660 #else 1661 static inline void posix_cpu_timers_init(struct task_struct *tsk) { } 1662 #endif 1663 1664 static inline void init_task_pid_links(struct task_struct *task) 1665 { 1666 enum pid_type type; 1667 1668 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1669 INIT_HLIST_NODE(&task->pid_links[type]); 1670 } 1671 } 1672 1673 static inline void 1674 init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 1675 { 1676 if (type == PIDTYPE_PID) 1677 task->thread_pid = pid; 1678 else 1679 task->signal->pids[type] = pid; 1680 } 1681 1682 static inline void rcu_copy_process(struct task_struct *p) 1683 { 1684 #ifdef CONFIG_PREEMPT_RCU 1685 p->rcu_read_lock_nesting = 0; 1686 p->rcu_read_unlock_special.s = 0; 1687 p->rcu_blocked_node = NULL; 1688 INIT_LIST_HEAD(&p->rcu_node_entry); 1689 #endif /* #ifdef CONFIG_PREEMPT_RCU */ 1690 #ifdef CONFIG_TASKS_RCU 1691 p->rcu_tasks_holdout = false; 1692 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 1693 p->rcu_tasks_idle_cpu = -1; 1694 #endif /* #ifdef CONFIG_TASKS_RCU */ 1695 } 1696 1697 struct pid *pidfd_pid(const struct file *file) 1698 { 1699 if (file->f_op == &pidfd_fops) 1700 return file->private_data; 1701 1702 return ERR_PTR(-EBADF); 1703 } 1704 1705 static int pidfd_release(struct inode *inode, struct file *file) 1706 { 1707 struct pid *pid = file->private_data; 1708 1709 file->private_data = NULL; 1710 put_pid(pid); 1711 return 0; 1712 } 1713 1714 #ifdef CONFIG_PROC_FS 1715 static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) 1716 { 1717 struct pid_namespace *ns = proc_pid_ns(file_inode(m->file)); 1718 struct pid *pid = f->private_data; 1719 1720 seq_put_decimal_ull(m, "Pid:\t", pid_nr_ns(pid, ns)); 1721 seq_putc(m, '\n'); 1722 } 1723 #endif 1724 1725 /* 1726 * Poll support for process exit notification. 1727 */ 1728 static unsigned int pidfd_poll(struct file *file, struct poll_table_struct *pts) 1729 { 1730 struct task_struct *task; 1731 struct pid *pid = file->private_data; 1732 int poll_flags = 0; 1733 1734 poll_wait(file, &pid->wait_pidfd, pts); 1735 1736 rcu_read_lock(); 1737 task = pid_task(pid, PIDTYPE_PID); 1738 /* 1739 * Inform pollers only when the whole thread group exits. 1740 * If the thread group leader exits before all other threads in the 1741 * group, then poll(2) should block, similar to the wait(2) family. 1742 */ 1743 if (!task || (task->exit_state && thread_group_empty(task))) 1744 poll_flags = POLLIN | POLLRDNORM; 1745 rcu_read_unlock(); 1746 1747 return poll_flags; 1748 } 1749 1750 const struct file_operations pidfd_fops = { 1751 .release = pidfd_release, 1752 .poll = pidfd_poll, 1753 #ifdef CONFIG_PROC_FS 1754 .show_fdinfo = pidfd_show_fdinfo, 1755 #endif 1756 }; 1757 1758 static void __delayed_free_task(struct rcu_head *rhp) 1759 { 1760 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 1761 1762 free_task(tsk); 1763 } 1764 1765 static __always_inline void delayed_free_task(struct task_struct *tsk) 1766 { 1767 if (IS_ENABLED(CONFIG_MEMCG)) 1768 call_rcu(&tsk->rcu, __delayed_free_task); 1769 else 1770 free_task(tsk); 1771 } 1772 1773 /* 1774 * This creates a new process as a copy of the old one, 1775 * but does not actually start it yet. 1776 * 1777 * It copies the registers, and all the appropriate 1778 * parts of the process environment (as per the clone 1779 * flags). The actual kick-off is left to the caller. 1780 */ 1781 static __latent_entropy struct task_struct *copy_process( 1782 struct pid *pid, 1783 int trace, 1784 int node, 1785 struct kernel_clone_args *args) 1786 { 1787 int pidfd = -1, retval; 1788 struct task_struct *p; 1789 struct multiprocess_signals delayed; 1790 struct file *pidfile = NULL; 1791 u64 clone_flags = args->flags; 1792 1793 /* 1794 * Don't allow sharing the root directory with processes in a different 1795 * namespace 1796 */ 1797 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 1798 return ERR_PTR(-EINVAL); 1799 1800 if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1801 return ERR_PTR(-EINVAL); 1802 1803 /* 1804 * Thread groups must share signals as well, and detached threads 1805 * can only be started up within the thread group. 1806 */ 1807 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 1808 return ERR_PTR(-EINVAL); 1809 1810 /* 1811 * Shared signal handlers imply shared VM. By way of the above, 1812 * thread groups also imply shared VM. Blocking this case allows 1813 * for various simplifications in other code. 1814 */ 1815 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 1816 return ERR_PTR(-EINVAL); 1817 1818 /* 1819 * Siblings of global init remain as zombies on exit since they are 1820 * not reaped by their parent (swapper). To solve this and to avoid 1821 * multi-rooted process trees, prevent global and container-inits 1822 * from creating siblings. 1823 */ 1824 if ((clone_flags & CLONE_PARENT) && 1825 current->signal->flags & SIGNAL_UNKILLABLE) 1826 return ERR_PTR(-EINVAL); 1827 1828 /* 1829 * If the new process will be in a different pid or user namespace 1830 * do not allow it to share a thread group with the forking task. 1831 */ 1832 if (clone_flags & CLONE_THREAD) { 1833 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 1834 (task_active_pid_ns(current) != 1835 current->nsproxy->pid_ns_for_children)) 1836 return ERR_PTR(-EINVAL); 1837 } 1838 1839 if (clone_flags & CLONE_PIDFD) { 1840 /* 1841 * - CLONE_DETACHED is blocked so that we can potentially 1842 * reuse it later for CLONE_PIDFD. 1843 * - CLONE_THREAD is blocked until someone really needs it. 1844 */ 1845 if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) 1846 return ERR_PTR(-EINVAL); 1847 } 1848 1849 /* 1850 * Force any signals received before this point to be delivered 1851 * before the fork happens. Collect up signals sent to multiple 1852 * processes that happen during the fork and delay them so that 1853 * they appear to happen after the fork. 1854 */ 1855 sigemptyset(&delayed.signal); 1856 INIT_HLIST_NODE(&delayed.node); 1857 1858 spin_lock_irq(¤t->sighand->siglock); 1859 if (!(clone_flags & CLONE_THREAD)) 1860 hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 1861 recalc_sigpending(); 1862 spin_unlock_irq(¤t->sighand->siglock); 1863 retval = -ERESTARTNOINTR; 1864 if (signal_pending(current)) 1865 goto fork_out; 1866 1867 retval = -ENOMEM; 1868 p = dup_task_struct(current, node); 1869 if (!p) 1870 goto fork_out; 1871 1872 /* 1873 * This _must_ happen before we call free_task(), i.e. before we jump 1874 * to any of the bad_fork_* labels. This is to avoid freeing 1875 * p->set_child_tid which is (ab)used as a kthread's data pointer for 1876 * kernel threads (PF_KTHREAD). 1877 */ 1878 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 1879 /* 1880 * Clear TID on mm_release()? 1881 */ 1882 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 1883 1884 ftrace_graph_init_task(p); 1885 1886 rt_mutex_init_task(p); 1887 1888 #ifdef CONFIG_PROVE_LOCKING 1889 DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1890 DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1891 #endif 1892 retval = -EAGAIN; 1893 if (atomic_read(&p->real_cred->user->processes) >= 1894 task_rlimit(p, RLIMIT_NPROC)) { 1895 if (p->real_cred->user != INIT_USER && 1896 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 1897 goto bad_fork_free; 1898 } 1899 current->flags &= ~PF_NPROC_EXCEEDED; 1900 1901 retval = copy_creds(p, clone_flags); 1902 if (retval < 0) 1903 goto bad_fork_free; 1904 1905 /* 1906 * If multiple threads are within copy_process(), then this check 1907 * triggers too late. This doesn't hurt, the check is only there 1908 * to stop root fork bombs. 1909 */ 1910 retval = -EAGAIN; 1911 if (nr_threads >= max_threads) 1912 goto bad_fork_cleanup_count; 1913 1914 delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1915 p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); 1916 p->flags |= PF_FORKNOEXEC; 1917 INIT_LIST_HEAD(&p->children); 1918 INIT_LIST_HEAD(&p->sibling); 1919 rcu_copy_process(p); 1920 p->vfork_done = NULL; 1921 spin_lock_init(&p->alloc_lock); 1922 1923 init_sigpending(&p->pending); 1924 1925 p->utime = p->stime = p->gtime = 0; 1926 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 1927 p->utimescaled = p->stimescaled = 0; 1928 #endif 1929 prev_cputime_init(&p->prev_cputime); 1930 1931 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 1932 seqcount_init(&p->vtime.seqcount); 1933 p->vtime.starttime = 0; 1934 p->vtime.state = VTIME_INACTIVE; 1935 #endif 1936 1937 #if defined(SPLIT_RSS_COUNTING) 1938 memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1939 #endif 1940 1941 p->default_timer_slack_ns = current->timer_slack_ns; 1942 1943 #ifdef CONFIG_PSI 1944 p->psi_flags = 0; 1945 #endif 1946 1947 task_io_accounting_init(&p->ioac); 1948 acct_clear_integrals(p); 1949 1950 posix_cpu_timers_init(p); 1951 1952 p->io_context = NULL; 1953 audit_set_context(p, NULL); 1954 cgroup_fork(p); 1955 #ifdef CONFIG_NUMA 1956 p->mempolicy = mpol_dup(p->mempolicy); 1957 if (IS_ERR(p->mempolicy)) { 1958 retval = PTR_ERR(p->mempolicy); 1959 p->mempolicy = NULL; 1960 goto bad_fork_cleanup_threadgroup_lock; 1961 } 1962 #endif 1963 #ifdef CONFIG_CPUSETS 1964 p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1965 p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1966 seqcount_init(&p->mems_allowed_seq); 1967 #endif 1968 #ifdef CONFIG_TRACE_IRQFLAGS 1969 p->irq_events = 0; 1970 p->hardirqs_enabled = 0; 1971 p->hardirq_enable_ip = 0; 1972 p->hardirq_enable_event = 0; 1973 p->hardirq_disable_ip = _THIS_IP_; 1974 p->hardirq_disable_event = 0; 1975 p->softirqs_enabled = 1; 1976 p->softirq_enable_ip = _THIS_IP_; 1977 p->softirq_enable_event = 0; 1978 p->softirq_disable_ip = 0; 1979 p->softirq_disable_event = 0; 1980 p->hardirq_context = 0; 1981 p->softirq_context = 0; 1982 #endif 1983 1984 p->pagefault_disabled = 0; 1985 1986 #ifdef CONFIG_LOCKDEP 1987 lockdep_init_task(p); 1988 #endif 1989 1990 #ifdef CONFIG_DEBUG_MUTEXES 1991 p->blocked_on = NULL; /* not blocked yet */ 1992 #endif 1993 #ifdef CONFIG_BCACHE 1994 p->sequential_io = 0; 1995 p->sequential_io_avg = 0; 1996 #endif 1997 1998 /* Perform scheduler related setup. Assign this task to a CPU. */ 1999 retval = sched_fork(clone_flags, p); 2000 if (retval) 2001 goto bad_fork_cleanup_policy; 2002 2003 retval = perf_event_init_task(p); 2004 if (retval) 2005 goto bad_fork_cleanup_policy; 2006 retval = audit_alloc(p); 2007 if (retval) 2008 goto bad_fork_cleanup_perf; 2009 /* copy all the process information */ 2010 shm_init_task(p); 2011 retval = security_task_alloc(p, clone_flags); 2012 if (retval) 2013 goto bad_fork_cleanup_audit; 2014 retval = copy_semundo(clone_flags, p); 2015 if (retval) 2016 goto bad_fork_cleanup_security; 2017 retval = copy_files(clone_flags, p); 2018 if (retval) 2019 goto bad_fork_cleanup_semundo; 2020 retval = copy_fs(clone_flags, p); 2021 if (retval) 2022 goto bad_fork_cleanup_files; 2023 retval = copy_sighand(clone_flags, p); 2024 if (retval) 2025 goto bad_fork_cleanup_fs; 2026 retval = copy_signal(clone_flags, p); 2027 if (retval) 2028 goto bad_fork_cleanup_sighand; 2029 retval = copy_mm(clone_flags, p); 2030 if (retval) 2031 goto bad_fork_cleanup_signal; 2032 retval = copy_namespaces(clone_flags, p); 2033 if (retval) 2034 goto bad_fork_cleanup_mm; 2035 retval = copy_io(clone_flags, p); 2036 if (retval) 2037 goto bad_fork_cleanup_namespaces; 2038 retval = copy_thread_tls(clone_flags, args->stack, args->stack_size, p, 2039 args->tls); 2040 if (retval) 2041 goto bad_fork_cleanup_io; 2042 2043 stackleak_task_init(p); 2044 2045 if (pid != &init_struct_pid) { 2046 pid = alloc_pid(p->nsproxy->pid_ns_for_children); 2047 if (IS_ERR(pid)) { 2048 retval = PTR_ERR(pid); 2049 goto bad_fork_cleanup_thread; 2050 } 2051 } 2052 2053 /* 2054 * This has to happen after we've potentially unshared the file 2055 * descriptor table (so that the pidfd doesn't leak into the child 2056 * if the fd table isn't shared). 2057 */ 2058 if (clone_flags & CLONE_PIDFD) { 2059 retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 2060 if (retval < 0) 2061 goto bad_fork_free_pid; 2062 2063 pidfd = retval; 2064 2065 pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, 2066 O_RDWR | O_CLOEXEC); 2067 if (IS_ERR(pidfile)) { 2068 put_unused_fd(pidfd); 2069 retval = PTR_ERR(pidfile); 2070 goto bad_fork_free_pid; 2071 } 2072 get_pid(pid); /* held by pidfile now */ 2073 2074 retval = put_user(pidfd, args->pidfd); 2075 if (retval) 2076 goto bad_fork_put_pidfd; 2077 } 2078 2079 #ifdef CONFIG_BLOCK 2080 p->plug = NULL; 2081 #endif 2082 #ifdef CONFIG_FUTEX 2083 p->robust_list = NULL; 2084 #ifdef CONFIG_COMPAT 2085 p->compat_robust_list = NULL; 2086 #endif 2087 INIT_LIST_HEAD(&p->pi_state_list); 2088 p->pi_state_cache = NULL; 2089 #endif 2090 /* 2091 * sigaltstack should be cleared when sharing the same VM 2092 */ 2093 if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 2094 sas_ss_reset(p); 2095 2096 /* 2097 * Syscall tracing and stepping should be turned off in the 2098 * child regardless of CLONE_PTRACE. 2099 */ 2100 user_disable_single_step(p); 2101 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 2102 #ifdef TIF_SYSCALL_EMU 2103 clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 2104 #endif 2105 clear_tsk_latency_tracing(p); 2106 2107 /* ok, now we should be set up.. */ 2108 p->pid = pid_nr(pid); 2109 if (clone_flags & CLONE_THREAD) { 2110 p->exit_signal = -1; 2111 p->group_leader = current->group_leader; 2112 p->tgid = current->tgid; 2113 } else { 2114 if (clone_flags & CLONE_PARENT) 2115 p->exit_signal = current->group_leader->exit_signal; 2116 else 2117 p->exit_signal = args->exit_signal; 2118 p->group_leader = p; 2119 p->tgid = p->pid; 2120 } 2121 2122 p->nr_dirtied = 0; 2123 p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 2124 p->dirty_paused_when = 0; 2125 2126 p->pdeath_signal = 0; 2127 INIT_LIST_HEAD(&p->thread_group); 2128 p->task_works = NULL; 2129 2130 cgroup_threadgroup_change_begin(current); 2131 /* 2132 * Ensure that the cgroup subsystem policies allow the new process to be 2133 * forked. It should be noted the the new process's css_set can be changed 2134 * between here and cgroup_post_fork() if an organisation operation is in 2135 * progress. 2136 */ 2137 retval = cgroup_can_fork(p); 2138 if (retval) 2139 goto bad_fork_cgroup_threadgroup_change_end; 2140 2141 /* 2142 * From this point on we must avoid any synchronous user-space 2143 * communication until we take the tasklist-lock. In particular, we do 2144 * not want user-space to be able to predict the process start-time by 2145 * stalling fork(2) after we recorded the start_time but before it is 2146 * visible to the system. 2147 */ 2148 2149 p->start_time = ktime_get_ns(); 2150 p->real_start_time = ktime_get_boottime_ns(); 2151 2152 /* 2153 * Make it visible to the rest of the system, but dont wake it up yet. 2154 * Need tasklist lock for parent etc handling! 2155 */ 2156 write_lock_irq(&tasklist_lock); 2157 2158 /* CLONE_PARENT re-uses the old parent */ 2159 if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 2160 p->real_parent = current->real_parent; 2161 p->parent_exec_id = current->parent_exec_id; 2162 } else { 2163 p->real_parent = current; 2164 p->parent_exec_id = current->self_exec_id; 2165 } 2166 2167 klp_copy_process(p); 2168 2169 spin_lock(¤t->sighand->siglock); 2170 2171 /* 2172 * Copy seccomp details explicitly here, in case they were changed 2173 * before holding sighand lock. 2174 */ 2175 copy_seccomp(p); 2176 2177 rseq_fork(p, clone_flags); 2178 2179 /* Don't start children in a dying pid namespace */ 2180 if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 2181 retval = -ENOMEM; 2182 goto bad_fork_cancel_cgroup; 2183 } 2184 2185 /* Let kill terminate clone/fork in the middle */ 2186 if (fatal_signal_pending(current)) { 2187 retval = -EINTR; 2188 goto bad_fork_cancel_cgroup; 2189 } 2190 2191 /* past the last point of failure */ 2192 if (pidfile) 2193 fd_install(pidfd, pidfile); 2194 2195 init_task_pid_links(p); 2196 if (likely(p->pid)) { 2197 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 2198 2199 init_task_pid(p, PIDTYPE_PID, pid); 2200 if (thread_group_leader(p)) { 2201 init_task_pid(p, PIDTYPE_TGID, pid); 2202 init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 2203 init_task_pid(p, PIDTYPE_SID, task_session(current)); 2204 2205 if (is_child_reaper(pid)) { 2206 ns_of_pid(pid)->child_reaper = p; 2207 p->signal->flags |= SIGNAL_UNKILLABLE; 2208 } 2209 p->signal->shared_pending.signal = delayed.signal; 2210 p->signal->tty = tty_kref_get(current->signal->tty); 2211 /* 2212 * Inherit has_child_subreaper flag under the same 2213 * tasklist_lock with adding child to the process tree 2214 * for propagate_has_child_subreaper optimization. 2215 */ 2216 p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2217 p->real_parent->signal->is_child_subreaper; 2218 list_add_tail(&p->sibling, &p->real_parent->children); 2219 list_add_tail_rcu(&p->tasks, &init_task.tasks); 2220 attach_pid(p, PIDTYPE_TGID); 2221 attach_pid(p, PIDTYPE_PGID); 2222 attach_pid(p, PIDTYPE_SID); 2223 __this_cpu_inc(process_counts); 2224 } else { 2225 current->signal->nr_threads++; 2226 atomic_inc(¤t->signal->live); 2227 refcount_inc(¤t->signal->sigcnt); 2228 task_join_group_stop(p); 2229 list_add_tail_rcu(&p->thread_group, 2230 &p->group_leader->thread_group); 2231 list_add_tail_rcu(&p->thread_node, 2232 &p->signal->thread_head); 2233 } 2234 attach_pid(p, PIDTYPE_PID); 2235 nr_threads++; 2236 } 2237 total_forks++; 2238 hlist_del_init(&delayed.node); 2239 spin_unlock(¤t->sighand->siglock); 2240 syscall_tracepoint_update(p); 2241 write_unlock_irq(&tasklist_lock); 2242 2243 proc_fork_connector(p); 2244 cgroup_post_fork(p); 2245 cgroup_threadgroup_change_end(current); 2246 perf_event_fork(p); 2247 2248 trace_task_newtask(p, clone_flags); 2249 uprobe_copy_process(p, clone_flags); 2250 2251 return p; 2252 2253 bad_fork_cancel_cgroup: 2254 spin_unlock(¤t->sighand->siglock); 2255 write_unlock_irq(&tasklist_lock); 2256 cgroup_cancel_fork(p); 2257 bad_fork_cgroup_threadgroup_change_end: 2258 cgroup_threadgroup_change_end(current); 2259 bad_fork_put_pidfd: 2260 if (clone_flags & CLONE_PIDFD) { 2261 fput(pidfile); 2262 put_unused_fd(pidfd); 2263 } 2264 bad_fork_free_pid: 2265 if (pid != &init_struct_pid) 2266 free_pid(pid); 2267 bad_fork_cleanup_thread: 2268 exit_thread(p); 2269 bad_fork_cleanup_io: 2270 if (p->io_context) 2271 exit_io_context(p); 2272 bad_fork_cleanup_namespaces: 2273 exit_task_namespaces(p); 2274 bad_fork_cleanup_mm: 2275 if (p->mm) { 2276 mm_clear_owner(p->mm, p); 2277 mmput(p->mm); 2278 } 2279 bad_fork_cleanup_signal: 2280 if (!(clone_flags & CLONE_THREAD)) 2281 free_signal_struct(p->signal); 2282 bad_fork_cleanup_sighand: 2283 __cleanup_sighand(p->sighand); 2284 bad_fork_cleanup_fs: 2285 exit_fs(p); /* blocking */ 2286 bad_fork_cleanup_files: 2287 exit_files(p); /* blocking */ 2288 bad_fork_cleanup_semundo: 2289 exit_sem(p); 2290 bad_fork_cleanup_security: 2291 security_task_free(p); 2292 bad_fork_cleanup_audit: 2293 audit_free(p); 2294 bad_fork_cleanup_perf: 2295 perf_event_free_task(p); 2296 bad_fork_cleanup_policy: 2297 lockdep_free_task(p); 2298 #ifdef CONFIG_NUMA 2299 mpol_put(p->mempolicy); 2300 bad_fork_cleanup_threadgroup_lock: 2301 #endif 2302 delayacct_tsk_free(p); 2303 bad_fork_cleanup_count: 2304 atomic_dec(&p->cred->user->processes); 2305 exit_creds(p); 2306 bad_fork_free: 2307 p->state = TASK_DEAD; 2308 put_task_stack(p); 2309 delayed_free_task(p); 2310 fork_out: 2311 spin_lock_irq(¤t->sighand->siglock); 2312 hlist_del_init(&delayed.node); 2313 spin_unlock_irq(¤t->sighand->siglock); 2314 return ERR_PTR(retval); 2315 } 2316 2317 static inline void init_idle_pids(struct task_struct *idle) 2318 { 2319 enum pid_type type; 2320 2321 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 2322 INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 2323 init_task_pid(idle, type, &init_struct_pid); 2324 } 2325 } 2326 2327 struct task_struct *fork_idle(int cpu) 2328 { 2329 struct task_struct *task; 2330 struct kernel_clone_args args = { 2331 .flags = CLONE_VM, 2332 }; 2333 2334 task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2335 if (!IS_ERR(task)) { 2336 init_idle_pids(task); 2337 init_idle(task, cpu); 2338 } 2339 2340 return task; 2341 } 2342 2343 struct mm_struct *copy_init_mm(void) 2344 { 2345 return dup_mm(NULL, &init_mm); 2346 } 2347 2348 /* 2349 * Ok, this is the main fork-routine. 2350 * 2351 * It copies the process, and if successful kick-starts 2352 * it and waits for it to finish using the VM if required. 2353 * 2354 * args->exit_signal is expected to be checked for sanity by the caller. 2355 */ 2356 long _do_fork(struct kernel_clone_args *args) 2357 { 2358 u64 clone_flags = args->flags; 2359 struct completion vfork; 2360 struct pid *pid; 2361 struct task_struct *p; 2362 int trace = 0; 2363 long nr; 2364 2365 /* 2366 * Determine whether and which event to report to ptracer. When 2367 * called from kernel_thread or CLONE_UNTRACED is explicitly 2368 * requested, no event is reported; otherwise, report if the event 2369 * for the type of forking is enabled. 2370 */ 2371 if (!(clone_flags & CLONE_UNTRACED)) { 2372 if (clone_flags & CLONE_VFORK) 2373 trace = PTRACE_EVENT_VFORK; 2374 else if (args->exit_signal != SIGCHLD) 2375 trace = PTRACE_EVENT_CLONE; 2376 else 2377 trace = PTRACE_EVENT_FORK; 2378 2379 if (likely(!ptrace_event_enabled(current, trace))) 2380 trace = 0; 2381 } 2382 2383 p = copy_process(NULL, trace, NUMA_NO_NODE, args); 2384 add_latent_entropy(); 2385 2386 if (IS_ERR(p)) 2387 return PTR_ERR(p); 2388 2389 /* 2390 * Do this prior waking up the new thread - the thread pointer 2391 * might get invalid after that point, if the thread exits quickly. 2392 */ 2393 trace_sched_process_fork(current, p); 2394 2395 pid = get_task_pid(p, PIDTYPE_PID); 2396 nr = pid_vnr(pid); 2397 2398 if (clone_flags & CLONE_PARENT_SETTID) 2399 put_user(nr, args->parent_tid); 2400 2401 if (clone_flags & CLONE_VFORK) { 2402 p->vfork_done = &vfork; 2403 init_completion(&vfork); 2404 get_task_struct(p); 2405 } 2406 2407 wake_up_new_task(p); 2408 2409 /* forking complete and child started to run, tell ptracer */ 2410 if (unlikely(trace)) 2411 ptrace_event_pid(trace, pid); 2412 2413 if (clone_flags & CLONE_VFORK) { 2414 if (!wait_for_vfork_done(p, &vfork)) 2415 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 2416 } 2417 2418 put_pid(pid); 2419 return nr; 2420 } 2421 2422 bool legacy_clone_args_valid(const struct kernel_clone_args *kargs) 2423 { 2424 /* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */ 2425 if ((kargs->flags & CLONE_PIDFD) && 2426 (kargs->flags & CLONE_PARENT_SETTID)) 2427 return false; 2428 2429 return true; 2430 } 2431 2432 #ifndef CONFIG_HAVE_COPY_THREAD_TLS 2433 /* For compatibility with architectures that call do_fork directly rather than 2434 * using the syscall entry points below. */ 2435 long do_fork(unsigned long clone_flags, 2436 unsigned long stack_start, 2437 unsigned long stack_size, 2438 int __user *parent_tidptr, 2439 int __user *child_tidptr) 2440 { 2441 struct kernel_clone_args args = { 2442 .flags = (clone_flags & ~CSIGNAL), 2443 .pidfd = parent_tidptr, 2444 .child_tid = child_tidptr, 2445 .parent_tid = parent_tidptr, 2446 .exit_signal = (clone_flags & CSIGNAL), 2447 .stack = stack_start, 2448 .stack_size = stack_size, 2449 }; 2450 2451 if (!legacy_clone_args_valid(&args)) 2452 return -EINVAL; 2453 2454 return _do_fork(&args); 2455 } 2456 #endif 2457 2458 /* 2459 * Create a kernel thread. 2460 */ 2461 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 2462 { 2463 struct kernel_clone_args args = { 2464 .flags = ((flags | CLONE_VM | CLONE_UNTRACED) & ~CSIGNAL), 2465 .exit_signal = (flags & CSIGNAL), 2466 .stack = (unsigned long)fn, 2467 .stack_size = (unsigned long)arg, 2468 }; 2469 2470 return _do_fork(&args); 2471 } 2472 2473 #ifdef __ARCH_WANT_SYS_FORK 2474 SYSCALL_DEFINE0(fork) 2475 { 2476 #ifdef CONFIG_MMU 2477 struct kernel_clone_args args = { 2478 .exit_signal = SIGCHLD, 2479 }; 2480 2481 return _do_fork(&args); 2482 #else 2483 /* can not support in nommu mode */ 2484 return -EINVAL; 2485 #endif 2486 } 2487 #endif 2488 2489 #ifdef __ARCH_WANT_SYS_VFORK 2490 SYSCALL_DEFINE0(vfork) 2491 { 2492 struct kernel_clone_args args = { 2493 .flags = CLONE_VFORK | CLONE_VM, 2494 .exit_signal = SIGCHLD, 2495 }; 2496 2497 return _do_fork(&args); 2498 } 2499 #endif 2500 2501 #ifdef __ARCH_WANT_SYS_CLONE 2502 #ifdef CONFIG_CLONE_BACKWARDS 2503 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2504 int __user *, parent_tidptr, 2505 unsigned long, tls, 2506 int __user *, child_tidptr) 2507 #elif defined(CONFIG_CLONE_BACKWARDS2) 2508 SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2509 int __user *, parent_tidptr, 2510 int __user *, child_tidptr, 2511 unsigned long, tls) 2512 #elif defined(CONFIG_CLONE_BACKWARDS3) 2513 SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2514 int, stack_size, 2515 int __user *, parent_tidptr, 2516 int __user *, child_tidptr, 2517 unsigned long, tls) 2518 #else 2519 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2520 int __user *, parent_tidptr, 2521 int __user *, child_tidptr, 2522 unsigned long, tls) 2523 #endif 2524 { 2525 struct kernel_clone_args args = { 2526 .flags = (clone_flags & ~CSIGNAL), 2527 .pidfd = parent_tidptr, 2528 .child_tid = child_tidptr, 2529 .parent_tid = parent_tidptr, 2530 .exit_signal = (clone_flags & CSIGNAL), 2531 .stack = newsp, 2532 .tls = tls, 2533 }; 2534 2535 if (!legacy_clone_args_valid(&args)) 2536 return -EINVAL; 2537 2538 return _do_fork(&args); 2539 } 2540 #endif 2541 2542 #ifdef __ARCH_WANT_SYS_CLONE3 2543 noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, 2544 struct clone_args __user *uargs, 2545 size_t size) 2546 { 2547 struct clone_args args; 2548 2549 if (unlikely(size > PAGE_SIZE)) 2550 return -E2BIG; 2551 2552 if (unlikely(size < sizeof(struct clone_args))) 2553 return -EINVAL; 2554 2555 if (unlikely(!access_ok(uargs, size))) 2556 return -EFAULT; 2557 2558 if (size > sizeof(struct clone_args)) { 2559 unsigned char __user *addr; 2560 unsigned char __user *end; 2561 unsigned char val; 2562 2563 addr = (void __user *)uargs + sizeof(struct clone_args); 2564 end = (void __user *)uargs + size; 2565 2566 for (; addr < end; addr++) { 2567 if (get_user(val, addr)) 2568 return -EFAULT; 2569 if (val) 2570 return -E2BIG; 2571 } 2572 2573 size = sizeof(struct clone_args); 2574 } 2575 2576 if (copy_from_user(&args, uargs, size)) 2577 return -EFAULT; 2578 2579 /* 2580 * Verify that higher 32bits of exit_signal are unset and that 2581 * it is a valid signal 2582 */ 2583 if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || 2584 !valid_signal(args.exit_signal))) 2585 return -EINVAL; 2586 2587 *kargs = (struct kernel_clone_args){ 2588 .flags = args.flags, 2589 .pidfd = u64_to_user_ptr(args.pidfd), 2590 .child_tid = u64_to_user_ptr(args.child_tid), 2591 .parent_tid = u64_to_user_ptr(args.parent_tid), 2592 .exit_signal = args.exit_signal, 2593 .stack = args.stack, 2594 .stack_size = args.stack_size, 2595 .tls = args.tls, 2596 }; 2597 2598 return 0; 2599 } 2600 2601 static bool clone3_args_valid(const struct kernel_clone_args *kargs) 2602 { 2603 /* 2604 * All lower bits of the flag word are taken. 2605 * Verify that no other unknown flags are passed along. 2606 */ 2607 if (kargs->flags & ~CLONE_LEGACY_FLAGS) 2608 return false; 2609 2610 /* 2611 * - make the CLONE_DETACHED bit reuseable for clone3 2612 * - make the CSIGNAL bits reuseable for clone3 2613 */ 2614 if (kargs->flags & (CLONE_DETACHED | CSIGNAL)) 2615 return false; 2616 2617 if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 2618 kargs->exit_signal) 2619 return false; 2620 2621 return true; 2622 } 2623 2624 SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 2625 { 2626 int err; 2627 2628 struct kernel_clone_args kargs; 2629 2630 err = copy_clone_args_from_user(&kargs, uargs, size); 2631 if (err) 2632 return err; 2633 2634 if (!clone3_args_valid(&kargs)) 2635 return -EINVAL; 2636 2637 return _do_fork(&kargs); 2638 } 2639 #endif 2640 2641 void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 2642 { 2643 struct task_struct *leader, *parent, *child; 2644 int res; 2645 2646 read_lock(&tasklist_lock); 2647 leader = top = top->group_leader; 2648 down: 2649 for_each_thread(leader, parent) { 2650 list_for_each_entry(child, &parent->children, sibling) { 2651 res = visitor(child, data); 2652 if (res) { 2653 if (res < 0) 2654 goto out; 2655 leader = child; 2656 goto down; 2657 } 2658 up: 2659 ; 2660 } 2661 } 2662 2663 if (leader != top) { 2664 child = leader; 2665 parent = child->real_parent; 2666 leader = parent->group_leader; 2667 goto up; 2668 } 2669 out: 2670 read_unlock(&tasklist_lock); 2671 } 2672 2673 #ifndef ARCH_MIN_MMSTRUCT_ALIGN 2674 #define ARCH_MIN_MMSTRUCT_ALIGN 0 2675 #endif 2676 2677 static void sighand_ctor(void *data) 2678 { 2679 struct sighand_struct *sighand = data; 2680 2681 spin_lock_init(&sighand->siglock); 2682 init_waitqueue_head(&sighand->signalfd_wqh); 2683 } 2684 2685 void __init proc_caches_init(void) 2686 { 2687 unsigned int mm_size; 2688 2689 sighand_cachep = kmem_cache_create("sighand_cache", 2690 sizeof(struct sighand_struct), 0, 2691 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 2692 SLAB_ACCOUNT, sighand_ctor); 2693 signal_cachep = kmem_cache_create("signal_cache", 2694 sizeof(struct signal_struct), 0, 2695 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 2696 NULL); 2697 files_cachep = kmem_cache_create("files_cache", 2698 sizeof(struct files_struct), 0, 2699 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 2700 NULL); 2701 fs_cachep = kmem_cache_create("fs_cache", 2702 sizeof(struct fs_struct), 0, 2703 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 2704 NULL); 2705 2706 /* 2707 * The mm_cpumask is located at the end of mm_struct, and is 2708 * dynamically sized based on the maximum CPU number this system 2709 * can have, taking hotplug into account (nr_cpu_ids). 2710 */ 2711 mm_size = sizeof(struct mm_struct) + cpumask_size(); 2712 2713 mm_cachep = kmem_cache_create_usercopy("mm_struct", 2714 mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 2715 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 2716 offsetof(struct mm_struct, saved_auxv), 2717 sizeof_field(struct mm_struct, saved_auxv), 2718 NULL); 2719 vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 2720 mmap_init(); 2721 nsproxy_cache_init(); 2722 } 2723 2724 /* 2725 * Check constraints on flags passed to the unshare system call. 2726 */ 2727 static int check_unshare_flags(unsigned long unshare_flags) 2728 { 2729 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 2730 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 2731 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2732 CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) 2733 return -EINVAL; 2734 /* 2735 * Not implemented, but pretend it works if there is nothing 2736 * to unshare. Note that unsharing the address space or the 2737 * signal handlers also need to unshare the signal queues (aka 2738 * CLONE_THREAD). 2739 */ 2740 if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 2741 if (!thread_group_empty(current)) 2742 return -EINVAL; 2743 } 2744 if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 2745 if (refcount_read(¤t->sighand->count) > 1) 2746 return -EINVAL; 2747 } 2748 if (unshare_flags & CLONE_VM) { 2749 if (!current_is_single_threaded()) 2750 return -EINVAL; 2751 } 2752 2753 return 0; 2754 } 2755 2756 /* 2757 * Unshare the filesystem structure if it is being shared 2758 */ 2759 static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2760 { 2761 struct fs_struct *fs = current->fs; 2762 2763 if (!(unshare_flags & CLONE_FS) || !fs) 2764 return 0; 2765 2766 /* don't need lock here; in the worst case we'll do useless copy */ 2767 if (fs->users == 1) 2768 return 0; 2769 2770 *new_fsp = copy_fs_struct(fs); 2771 if (!*new_fsp) 2772 return -ENOMEM; 2773 2774 return 0; 2775 } 2776 2777 /* 2778 * Unshare file descriptor table if it is being shared 2779 */ 2780 static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 2781 { 2782 struct files_struct *fd = current->files; 2783 int error = 0; 2784 2785 if ((unshare_flags & CLONE_FILES) && 2786 (fd && atomic_read(&fd->count) > 1)) { 2787 *new_fdp = dup_fd(fd, &error); 2788 if (!*new_fdp) 2789 return error; 2790 } 2791 2792 return 0; 2793 } 2794 2795 /* 2796 * unshare allows a process to 'unshare' part of the process 2797 * context which was originally shared using clone. copy_* 2798 * functions used by do_fork() cannot be used here directly 2799 * because they modify an inactive task_struct that is being 2800 * constructed. Here we are modifying the current, active, 2801 * task_struct. 2802 */ 2803 int ksys_unshare(unsigned long unshare_flags) 2804 { 2805 struct fs_struct *fs, *new_fs = NULL; 2806 struct files_struct *fd, *new_fd = NULL; 2807 struct cred *new_cred = NULL; 2808 struct nsproxy *new_nsproxy = NULL; 2809 int do_sysvsem = 0; 2810 int err; 2811 2812 /* 2813 * If unsharing a user namespace must also unshare the thread group 2814 * and unshare the filesystem root and working directories. 2815 */ 2816 if (unshare_flags & CLONE_NEWUSER) 2817 unshare_flags |= CLONE_THREAD | CLONE_FS; 2818 /* 2819 * If unsharing vm, must also unshare signal handlers. 2820 */ 2821 if (unshare_flags & CLONE_VM) 2822 unshare_flags |= CLONE_SIGHAND; 2823 /* 2824 * If unsharing a signal handlers, must also unshare the signal queues. 2825 */ 2826 if (unshare_flags & CLONE_SIGHAND) 2827 unshare_flags |= CLONE_THREAD; 2828 /* 2829 * If unsharing namespace, must also unshare filesystem information. 2830 */ 2831 if (unshare_flags & CLONE_NEWNS) 2832 unshare_flags |= CLONE_FS; 2833 2834 err = check_unshare_flags(unshare_flags); 2835 if (err) 2836 goto bad_unshare_out; 2837 /* 2838 * CLONE_NEWIPC must also detach from the undolist: after switching 2839 * to a new ipc namespace, the semaphore arrays from the old 2840 * namespace are unreachable. 2841 */ 2842 if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 2843 do_sysvsem = 1; 2844 err = unshare_fs(unshare_flags, &new_fs); 2845 if (err) 2846 goto bad_unshare_out; 2847 err = unshare_fd(unshare_flags, &new_fd); 2848 if (err) 2849 goto bad_unshare_cleanup_fs; 2850 err = unshare_userns(unshare_flags, &new_cred); 2851 if (err) 2852 goto bad_unshare_cleanup_fd; 2853 err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2854 new_cred, new_fs); 2855 if (err) 2856 goto bad_unshare_cleanup_cred; 2857 2858 if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 2859 if (do_sysvsem) { 2860 /* 2861 * CLONE_SYSVSEM is equivalent to sys_exit(). 2862 */ 2863 exit_sem(current); 2864 } 2865 if (unshare_flags & CLONE_NEWIPC) { 2866 /* Orphan segments in old ns (see sem above). */ 2867 exit_shm(current); 2868 shm_init_task(current); 2869 } 2870 2871 if (new_nsproxy) 2872 switch_task_namespaces(current, new_nsproxy); 2873 2874 task_lock(current); 2875 2876 if (new_fs) { 2877 fs = current->fs; 2878 spin_lock(&fs->lock); 2879 current->fs = new_fs; 2880 if (--fs->users) 2881 new_fs = NULL; 2882 else 2883 new_fs = fs; 2884 spin_unlock(&fs->lock); 2885 } 2886 2887 if (new_fd) { 2888 fd = current->files; 2889 current->files = new_fd; 2890 new_fd = fd; 2891 } 2892 2893 task_unlock(current); 2894 2895 if (new_cred) { 2896 /* Install the new user namespace */ 2897 commit_creds(new_cred); 2898 new_cred = NULL; 2899 } 2900 } 2901 2902 perf_event_namespaces(current); 2903 2904 bad_unshare_cleanup_cred: 2905 if (new_cred) 2906 put_cred(new_cred); 2907 bad_unshare_cleanup_fd: 2908 if (new_fd) 2909 put_files_struct(new_fd); 2910 2911 bad_unshare_cleanup_fs: 2912 if (new_fs) 2913 free_fs_struct(new_fs); 2914 2915 bad_unshare_out: 2916 return err; 2917 } 2918 2919 SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 2920 { 2921 return ksys_unshare(unshare_flags); 2922 } 2923 2924 /* 2925 * Helper to unshare the files of the current task. 2926 * We don't want to expose copy_files internals to 2927 * the exec layer of the kernel. 2928 */ 2929 2930 int unshare_files(struct files_struct **displaced) 2931 { 2932 struct task_struct *task = current; 2933 struct files_struct *copy = NULL; 2934 int error; 2935 2936 error = unshare_fd(CLONE_FILES, ©); 2937 if (error || !copy) { 2938 *displaced = NULL; 2939 return error; 2940 } 2941 *displaced = task->files; 2942 task_lock(task); 2943 task->files = copy; 2944 task_unlock(task); 2945 return 0; 2946 } 2947 2948 int sysctl_max_threads(struct ctl_table *table, int write, 2949 void __user *buffer, size_t *lenp, loff_t *ppos) 2950 { 2951 struct ctl_table t; 2952 int ret; 2953 int threads = max_threads; 2954 int min = MIN_THREADS; 2955 int max = MAX_THREADS; 2956 2957 t = *table; 2958 t.data = &threads; 2959 t.extra1 = &min; 2960 t.extra2 = &max; 2961 2962 ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 2963 if (ret || !write) 2964 return ret; 2965 2966 set_max_threads(threads); 2967 2968 return 0; 2969 } 2970