at v2.6.12-rc2 1498 lines 34 kB view raw
1/* 2 * linux/fs/exec.c 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 */ 6 7/* 8 * #!-checking implemented by tytso. 9 */ 10/* 11 * Demand-loading implemented 01.12.91 - no need to read anything but 12 * the header into memory. The inode of the executable is put into 13 * "current->executable", and page faults do the actual loading. Clean. 14 * 15 * Once more I can proudly say that linux stood up to being changed: it 16 * was less than 2 hours work to get demand-loading completely implemented. 17 * 18 * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead, 19 * current->executable is only used by the procfs. This allows a dispatch 20 * table to check for several different types of binary formats. We keep 21 * trying until we recognize the file or we run out of supported binary 22 * formats. 23 */ 24 25#include <linux/config.h> 26#include <linux/slab.h> 27#include <linux/file.h> 28#include <linux/mman.h> 29#include <linux/a.out.h> 30#include <linux/stat.h> 31#include <linux/fcntl.h> 32#include <linux/smp_lock.h> 33#include <linux/init.h> 34#include <linux/pagemap.h> 35#include <linux/highmem.h> 36#include <linux/spinlock.h> 37#include <linux/key.h> 38#include <linux/personality.h> 39#include <linux/binfmts.h> 40#include <linux/swap.h> 41#include <linux/utsname.h> 42#include <linux/module.h> 43#include <linux/namei.h> 44#include <linux/proc_fs.h> 45#include <linux/ptrace.h> 46#include <linux/mount.h> 47#include <linux/security.h> 48#include <linux/syscalls.h> 49#include <linux/rmap.h> 50#include <linux/acct.h> 51 52#include <asm/uaccess.h> 53#include <asm/mmu_context.h> 54 55#ifdef CONFIG_KMOD 56#include <linux/kmod.h> 57#endif 58 59int core_uses_pid; 60char core_pattern[65] = "core"; 61/* The maximal length of core_pattern is also specified in sysctl.c */ 62 63static struct linux_binfmt *formats; 64static DEFINE_RWLOCK(binfmt_lock); 65 66int register_binfmt(struct linux_binfmt * fmt) 67{ 68 struct linux_binfmt ** tmp = &formats; 69 70 if (!fmt) 71 return -EINVAL; 72 if (fmt->next) 73 return -EBUSY; 74 write_lock(&binfmt_lock); 75 while (*tmp) { 76 if (fmt == *tmp) { 77 write_unlock(&binfmt_lock); 78 return -EBUSY; 79 } 80 tmp = &(*tmp)->next; 81 } 82 fmt->next = formats; 83 formats = fmt; 84 write_unlock(&binfmt_lock); 85 return 0; 86} 87 88EXPORT_SYMBOL(register_binfmt); 89 90int unregister_binfmt(struct linux_binfmt * fmt) 91{ 92 struct linux_binfmt ** tmp = &formats; 93 94 write_lock(&binfmt_lock); 95 while (*tmp) { 96 if (fmt == *tmp) { 97 *tmp = fmt->next; 98 write_unlock(&binfmt_lock); 99 return 0; 100 } 101 tmp = &(*tmp)->next; 102 } 103 write_unlock(&binfmt_lock); 104 return -EINVAL; 105} 106 107EXPORT_SYMBOL(unregister_binfmt); 108 109static inline void put_binfmt(struct linux_binfmt * fmt) 110{ 111 module_put(fmt->module); 112} 113 114/* 115 * Note that a shared library must be both readable and executable due to 116 * security reasons. 117 * 118 * Also note that we take the address to load from from the file itself. 119 */ 120asmlinkage long sys_uselib(const char __user * library) 121{ 122 struct file * file; 123 struct nameidata nd; 124 int error; 125 126 nd.intent.open.flags = FMODE_READ; 127 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd); 128 if (error) 129 goto out; 130 131 error = -EINVAL; 132 if (!S_ISREG(nd.dentry->d_inode->i_mode)) 133 goto exit; 134 135 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd); 136 if (error) 137 goto exit; 138 139 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); 140 error = PTR_ERR(file); 141 if (IS_ERR(file)) 142 goto out; 143 144 error = -ENOEXEC; 145 if(file->f_op) { 146 struct linux_binfmt * fmt; 147 148 read_lock(&binfmt_lock); 149 for (fmt = formats ; fmt ; fmt = fmt->next) { 150 if (!fmt->load_shlib) 151 continue; 152 if (!try_module_get(fmt->module)) 153 continue; 154 read_unlock(&binfmt_lock); 155 error = fmt->load_shlib(file); 156 read_lock(&binfmt_lock); 157 put_binfmt(fmt); 158 if (error != -ENOEXEC) 159 break; 160 } 161 read_unlock(&binfmt_lock); 162 } 163 fput(file); 164out: 165 return error; 166exit: 167 path_release(&nd); 168 goto out; 169} 170 171/* 172 * count() counts the number of strings in array ARGV. 173 */ 174static int count(char __user * __user * argv, int max) 175{ 176 int i = 0; 177 178 if (argv != NULL) { 179 for (;;) { 180 char __user * p; 181 182 if (get_user(p, argv)) 183 return -EFAULT; 184 if (!p) 185 break; 186 argv++; 187 if(++i > max) 188 return -E2BIG; 189 cond_resched(); 190 } 191 } 192 return i; 193} 194 195/* 196 * 'copy_strings()' copies argument/environment strings from user 197 * memory to free pages in kernel mem. These are in a format ready 198 * to be put directly into the top of new user memory. 199 */ 200int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm) 201{ 202 struct page *kmapped_page = NULL; 203 char *kaddr = NULL; 204 int ret; 205 206 while (argc-- > 0) { 207 char __user *str; 208 int len; 209 unsigned long pos; 210 211 if (get_user(str, argv+argc) || 212 !(len = strnlen_user(str, bprm->p))) { 213 ret = -EFAULT; 214 goto out; 215 } 216 217 if (bprm->p < len) { 218 ret = -E2BIG; 219 goto out; 220 } 221 222 bprm->p -= len; 223 /* XXX: add architecture specific overflow check here. */ 224 pos = bprm->p; 225 226 while (len > 0) { 227 int i, new, err; 228 int offset, bytes_to_copy; 229 struct page *page; 230 231 offset = pos % PAGE_SIZE; 232 i = pos/PAGE_SIZE; 233 page = bprm->page[i]; 234 new = 0; 235 if (!page) { 236 page = alloc_page(GFP_HIGHUSER); 237 bprm->page[i] = page; 238 if (!page) { 239 ret = -ENOMEM; 240 goto out; 241 } 242 new = 1; 243 } 244 245 if (page != kmapped_page) { 246 if (kmapped_page) 247 kunmap(kmapped_page); 248 kmapped_page = page; 249 kaddr = kmap(kmapped_page); 250 } 251 if (new && offset) 252 memset(kaddr, 0, offset); 253 bytes_to_copy = PAGE_SIZE - offset; 254 if (bytes_to_copy > len) { 255 bytes_to_copy = len; 256 if (new) 257 memset(kaddr+offset+len, 0, 258 PAGE_SIZE-offset-len); 259 } 260 err = copy_from_user(kaddr+offset, str, bytes_to_copy); 261 if (err) { 262 ret = -EFAULT; 263 goto out; 264 } 265 266 pos += bytes_to_copy; 267 str += bytes_to_copy; 268 len -= bytes_to_copy; 269 } 270 } 271 ret = 0; 272out: 273 if (kmapped_page) 274 kunmap(kmapped_page); 275 return ret; 276} 277 278/* 279 * Like copy_strings, but get argv and its values from kernel memory. 280 */ 281int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm) 282{ 283 int r; 284 mm_segment_t oldfs = get_fs(); 285 set_fs(KERNEL_DS); 286 r = copy_strings(argc, (char __user * __user *)argv, bprm); 287 set_fs(oldfs); 288 return r; 289} 290 291EXPORT_SYMBOL(copy_strings_kernel); 292 293#ifdef CONFIG_MMU 294/* 295 * This routine is used to map in a page into an address space: needed by 296 * execve() for the initial stack and environment pages. 297 * 298 * vma->vm_mm->mmap_sem is held for writing. 299 */ 300void install_arg_page(struct vm_area_struct *vma, 301 struct page *page, unsigned long address) 302{ 303 struct mm_struct *mm = vma->vm_mm; 304 pgd_t * pgd; 305 pud_t * pud; 306 pmd_t * pmd; 307 pte_t * pte; 308 309 if (unlikely(anon_vma_prepare(vma))) 310 goto out_sig; 311 312 flush_dcache_page(page); 313 pgd = pgd_offset(mm, address); 314 315 spin_lock(&mm->page_table_lock); 316 pud = pud_alloc(mm, pgd, address); 317 if (!pud) 318 goto out; 319 pmd = pmd_alloc(mm, pud, address); 320 if (!pmd) 321 goto out; 322 pte = pte_alloc_map(mm, pmd, address); 323 if (!pte) 324 goto out; 325 if (!pte_none(*pte)) { 326 pte_unmap(pte); 327 goto out; 328 } 329 inc_mm_counter(mm, rss); 330 lru_cache_add_active(page); 331 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte( 332 page, vma->vm_page_prot)))); 333 page_add_anon_rmap(page, vma, address); 334 pte_unmap(pte); 335 spin_unlock(&mm->page_table_lock); 336 337 /* no need for flush_tlb */ 338 return; 339out: 340 spin_unlock(&mm->page_table_lock); 341out_sig: 342 __free_page(page); 343 force_sig(SIGKILL, current); 344} 345 346#define EXTRA_STACK_VM_PAGES 20 /* random */ 347 348int setup_arg_pages(struct linux_binprm *bprm, 349 unsigned long stack_top, 350 int executable_stack) 351{ 352 unsigned long stack_base; 353 struct vm_area_struct *mpnt; 354 struct mm_struct *mm = current->mm; 355 int i, ret; 356 long arg_size; 357 358#ifdef CONFIG_STACK_GROWSUP 359 /* Move the argument and environment strings to the bottom of the 360 * stack space. 361 */ 362 int offset, j; 363 char *to, *from; 364 365 /* Start by shifting all the pages down */ 366 i = 0; 367 for (j = 0; j < MAX_ARG_PAGES; j++) { 368 struct page *page = bprm->page[j]; 369 if (!page) 370 continue; 371 bprm->page[i++] = page; 372 } 373 374 /* Now move them within their pages */ 375 offset = bprm->p % PAGE_SIZE; 376 to = kmap(bprm->page[0]); 377 for (j = 1; j < i; j++) { 378 memmove(to, to + offset, PAGE_SIZE - offset); 379 from = kmap(bprm->page[j]); 380 memcpy(to + PAGE_SIZE - offset, from, offset); 381 kunmap(bprm->page[j - 1]); 382 to = from; 383 } 384 memmove(to, to + offset, PAGE_SIZE - offset); 385 kunmap(bprm->page[j - 1]); 386 387 /* Limit stack size to 1GB */ 388 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max; 389 if (stack_base > (1 << 30)) 390 stack_base = 1 << 30; 391 stack_base = PAGE_ALIGN(stack_top - stack_base); 392 393 /* Adjust bprm->p to point to the end of the strings. */ 394 bprm->p = stack_base + PAGE_SIZE * i - offset; 395 396 mm->arg_start = stack_base; 397 arg_size = i << PAGE_SHIFT; 398 399 /* zero pages that were copied above */ 400 while (i < MAX_ARG_PAGES) 401 bprm->page[i++] = NULL; 402#else 403 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE); 404 stack_base = PAGE_ALIGN(stack_base); 405 bprm->p += stack_base; 406 mm->arg_start = bprm->p; 407 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start); 408#endif 409 410 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE; 411 412 if (bprm->loader) 413 bprm->loader += stack_base; 414 bprm->exec += stack_base; 415 416 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 417 if (!mpnt) 418 return -ENOMEM; 419 420 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) { 421 kmem_cache_free(vm_area_cachep, mpnt); 422 return -ENOMEM; 423 } 424 425 memset(mpnt, 0, sizeof(*mpnt)); 426 427 down_write(&mm->mmap_sem); 428 { 429 mpnt->vm_mm = mm; 430#ifdef CONFIG_STACK_GROWSUP 431 mpnt->vm_start = stack_base; 432 mpnt->vm_end = stack_base + arg_size; 433#else 434 mpnt->vm_end = stack_top; 435 mpnt->vm_start = mpnt->vm_end - arg_size; 436#endif 437 /* Adjust stack execute permissions; explicitly enable 438 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X 439 * and leave alone (arch default) otherwise. */ 440 if (unlikely(executable_stack == EXSTACK_ENABLE_X)) 441 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC; 442 else if (executable_stack == EXSTACK_DISABLE_X) 443 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC; 444 else 445 mpnt->vm_flags = VM_STACK_FLAGS; 446 mpnt->vm_flags |= mm->def_flags; 447 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7]; 448 if ((ret = insert_vm_struct(mm, mpnt))) { 449 up_write(&mm->mmap_sem); 450 kmem_cache_free(vm_area_cachep, mpnt); 451 return ret; 452 } 453 mm->stack_vm = mm->total_vm = vma_pages(mpnt); 454 } 455 456 for (i = 0 ; i < MAX_ARG_PAGES ; i++) { 457 struct page *page = bprm->page[i]; 458 if (page) { 459 bprm->page[i] = NULL; 460 install_arg_page(mpnt, page, stack_base); 461 } 462 stack_base += PAGE_SIZE; 463 } 464 up_write(&mm->mmap_sem); 465 466 return 0; 467} 468 469EXPORT_SYMBOL(setup_arg_pages); 470 471#define free_arg_pages(bprm) do { } while (0) 472 473#else 474 475static inline void free_arg_pages(struct linux_binprm *bprm) 476{ 477 int i; 478 479 for (i = 0; i < MAX_ARG_PAGES; i++) { 480 if (bprm->page[i]) 481 __free_page(bprm->page[i]); 482 bprm->page[i] = NULL; 483 } 484} 485 486#endif /* CONFIG_MMU */ 487 488struct file *open_exec(const char *name) 489{ 490 struct nameidata nd; 491 int err; 492 struct file *file; 493 494 nd.intent.open.flags = FMODE_READ; 495 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd); 496 file = ERR_PTR(err); 497 498 if (!err) { 499 struct inode *inode = nd.dentry->d_inode; 500 file = ERR_PTR(-EACCES); 501 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) && 502 S_ISREG(inode->i_mode)) { 503 int err = permission(inode, MAY_EXEC, &nd); 504 if (!err && !(inode->i_mode & 0111)) 505 err = -EACCES; 506 file = ERR_PTR(err); 507 if (!err) { 508 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); 509 if (!IS_ERR(file)) { 510 err = deny_write_access(file); 511 if (err) { 512 fput(file); 513 file = ERR_PTR(err); 514 } 515 } 516out: 517 return file; 518 } 519 } 520 path_release(&nd); 521 } 522 goto out; 523} 524 525EXPORT_SYMBOL(open_exec); 526 527int kernel_read(struct file *file, unsigned long offset, 528 char *addr, unsigned long count) 529{ 530 mm_segment_t old_fs; 531 loff_t pos = offset; 532 int result; 533 534 old_fs = get_fs(); 535 set_fs(get_ds()); 536 /* The cast to a user pointer is valid due to the set_fs() */ 537 result = vfs_read(file, (void __user *)addr, count, &pos); 538 set_fs(old_fs); 539 return result; 540} 541 542EXPORT_SYMBOL(kernel_read); 543 544static int exec_mmap(struct mm_struct *mm) 545{ 546 struct task_struct *tsk; 547 struct mm_struct * old_mm, *active_mm; 548 549 /* Notify parent that we're no longer interested in the old VM */ 550 tsk = current; 551 old_mm = current->mm; 552 mm_release(tsk, old_mm); 553 554 if (old_mm) { 555 /* 556 * Make sure that if there is a core dump in progress 557 * for the old mm, we get out and die instead of going 558 * through with the exec. We must hold mmap_sem around 559 * checking core_waiters and changing tsk->mm. The 560 * core-inducing thread will increment core_waiters for 561 * each thread whose ->mm == old_mm. 562 */ 563 down_read(&old_mm->mmap_sem); 564 if (unlikely(old_mm->core_waiters)) { 565 up_read(&old_mm->mmap_sem); 566 return -EINTR; 567 } 568 } 569 task_lock(tsk); 570 active_mm = tsk->active_mm; 571 tsk->mm = mm; 572 tsk->active_mm = mm; 573 activate_mm(active_mm, mm); 574 task_unlock(tsk); 575 arch_pick_mmap_layout(mm); 576 if (old_mm) { 577 up_read(&old_mm->mmap_sem); 578 if (active_mm != old_mm) BUG(); 579 mmput(old_mm); 580 return 0; 581 } 582 mmdrop(active_mm); 583 return 0; 584} 585 586/* 587 * This function makes sure the current process has its own signal table, 588 * so that flush_signal_handlers can later reset the handlers without 589 * disturbing other processes. (Other processes might share the signal 590 * table via the CLONE_SIGHAND option to clone().) 591 */ 592static inline int de_thread(struct task_struct *tsk) 593{ 594 struct signal_struct *sig = tsk->signal; 595 struct sighand_struct *newsighand, *oldsighand = tsk->sighand; 596 spinlock_t *lock = &oldsighand->siglock; 597 int count; 598 599 /* 600 * If we don't share sighandlers, then we aren't sharing anything 601 * and we can just re-use it all. 602 */ 603 if (atomic_read(&oldsighand->count) <= 1) { 604 BUG_ON(atomic_read(&sig->count) != 1); 605 exit_itimers(sig); 606 return 0; 607 } 608 609 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 610 if (!newsighand) 611 return -ENOMEM; 612 613 if (thread_group_empty(current)) 614 goto no_thread_group; 615 616 /* 617 * Kill all other threads in the thread group. 618 * We must hold tasklist_lock to call zap_other_threads. 619 */ 620 read_lock(&tasklist_lock); 621 spin_lock_irq(lock); 622 if (sig->flags & SIGNAL_GROUP_EXIT) { 623 /* 624 * Another group action in progress, just 625 * return so that the signal is processed. 626 */ 627 spin_unlock_irq(lock); 628 read_unlock(&tasklist_lock); 629 kmem_cache_free(sighand_cachep, newsighand); 630 return -EAGAIN; 631 } 632 zap_other_threads(current); 633 read_unlock(&tasklist_lock); 634 635 /* 636 * Account for the thread group leader hanging around: 637 */ 638 count = 2; 639 if (thread_group_leader(current)) 640 count = 1; 641 while (atomic_read(&sig->count) > count) { 642 sig->group_exit_task = current; 643 sig->notify_count = count; 644 __set_current_state(TASK_UNINTERRUPTIBLE); 645 spin_unlock_irq(lock); 646 schedule(); 647 spin_lock_irq(lock); 648 } 649 sig->group_exit_task = NULL; 650 sig->notify_count = 0; 651 spin_unlock_irq(lock); 652 653 /* 654 * At this point all other threads have exited, all we have to 655 * do is to wait for the thread group leader to become inactive, 656 * and to assume its PID: 657 */ 658 if (!thread_group_leader(current)) { 659 struct task_struct *leader = current->group_leader, *parent; 660 struct dentry *proc_dentry1, *proc_dentry2; 661 unsigned long exit_state, ptrace; 662 663 /* 664 * Wait for the thread group leader to be a zombie. 665 * It should already be zombie at this point, most 666 * of the time. 667 */ 668 while (leader->exit_state != EXIT_ZOMBIE) 669 yield(); 670 671 spin_lock(&leader->proc_lock); 672 spin_lock(&current->proc_lock); 673 proc_dentry1 = proc_pid_unhash(current); 674 proc_dentry2 = proc_pid_unhash(leader); 675 write_lock_irq(&tasklist_lock); 676 677 if (leader->tgid != current->tgid) 678 BUG(); 679 if (current->pid == current->tgid) 680 BUG(); 681 /* 682 * An exec() starts a new thread group with the 683 * TGID of the previous thread group. Rehash the 684 * two threads with a switched PID, and release 685 * the former thread group leader: 686 */ 687 ptrace = leader->ptrace; 688 parent = leader->parent; 689 if (unlikely(ptrace) && unlikely(parent == current)) { 690 /* 691 * Joker was ptracing his own group leader, 692 * and now he wants to be his own parent! 693 * We can't have that. 694 */ 695 ptrace = 0; 696 } 697 698 ptrace_unlink(current); 699 ptrace_unlink(leader); 700 remove_parent(current); 701 remove_parent(leader); 702 703 switch_exec_pids(leader, current); 704 705 current->parent = current->real_parent = leader->real_parent; 706 leader->parent = leader->real_parent = child_reaper; 707 current->group_leader = current; 708 leader->group_leader = leader; 709 710 add_parent(current, current->parent); 711 add_parent(leader, leader->parent); 712 if (ptrace) { 713 current->ptrace = ptrace; 714 __ptrace_link(current, parent); 715 } 716 717 list_del(&current->tasks); 718 list_add_tail(&current->tasks, &init_task.tasks); 719 current->exit_signal = SIGCHLD; 720 exit_state = leader->exit_state; 721 722 write_unlock_irq(&tasklist_lock); 723 spin_unlock(&leader->proc_lock); 724 spin_unlock(&current->proc_lock); 725 proc_pid_flush(proc_dentry1); 726 proc_pid_flush(proc_dentry2); 727 728 if (exit_state != EXIT_ZOMBIE) 729 BUG(); 730 release_task(leader); 731 } 732 733 /* 734 * Now there are really no other threads at all, 735 * so it's safe to stop telling them to kill themselves. 736 */ 737 sig->flags = 0; 738 739no_thread_group: 740 BUG_ON(atomic_read(&sig->count) != 1); 741 exit_itimers(sig); 742 743 if (atomic_read(&oldsighand->count) == 1) { 744 /* 745 * Now that we nuked the rest of the thread group, 746 * it turns out we are not sharing sighand any more either. 747 * So we can just keep it. 748 */ 749 kmem_cache_free(sighand_cachep, newsighand); 750 } else { 751 /* 752 * Move our state over to newsighand and switch it in. 753 */ 754 spin_lock_init(&newsighand->siglock); 755 atomic_set(&newsighand->count, 1); 756 memcpy(newsighand->action, oldsighand->action, 757 sizeof(newsighand->action)); 758 759 write_lock_irq(&tasklist_lock); 760 spin_lock(&oldsighand->siglock); 761 spin_lock(&newsighand->siglock); 762 763 current->sighand = newsighand; 764 recalc_sigpending(); 765 766 spin_unlock(&newsighand->siglock); 767 spin_unlock(&oldsighand->siglock); 768 write_unlock_irq(&tasklist_lock); 769 770 if (atomic_dec_and_test(&oldsighand->count)) 771 kmem_cache_free(sighand_cachep, oldsighand); 772 } 773 774 if (!thread_group_empty(current)) 775 BUG(); 776 if (!thread_group_leader(current)) 777 BUG(); 778 return 0; 779} 780 781/* 782 * These functions flushes out all traces of the currently running executable 783 * so that a new one can be started 784 */ 785 786static inline void flush_old_files(struct files_struct * files) 787{ 788 long j = -1; 789 790 spin_lock(&files->file_lock); 791 for (;;) { 792 unsigned long set, i; 793 794 j++; 795 i = j * __NFDBITS; 796 if (i >= files->max_fds || i >= files->max_fdset) 797 break; 798 set = files->close_on_exec->fds_bits[j]; 799 if (!set) 800 continue; 801 files->close_on_exec->fds_bits[j] = 0; 802 spin_unlock(&files->file_lock); 803 for ( ; set ; i++,set >>= 1) { 804 if (set & 1) { 805 sys_close(i); 806 } 807 } 808 spin_lock(&files->file_lock); 809 810 } 811 spin_unlock(&files->file_lock); 812} 813 814void get_task_comm(char *buf, struct task_struct *tsk) 815{ 816 /* buf must be at least sizeof(tsk->comm) in size */ 817 task_lock(tsk); 818 strncpy(buf, tsk->comm, sizeof(tsk->comm)); 819 task_unlock(tsk); 820} 821 822void set_task_comm(struct task_struct *tsk, char *buf) 823{ 824 task_lock(tsk); 825 strlcpy(tsk->comm, buf, sizeof(tsk->comm)); 826 task_unlock(tsk); 827} 828 829int flush_old_exec(struct linux_binprm * bprm) 830{ 831 char * name; 832 int i, ch, retval; 833 struct files_struct *files; 834 char tcomm[sizeof(current->comm)]; 835 836 /* 837 * Make sure we have a private signal table and that 838 * we are unassociated from the previous thread group. 839 */ 840 retval = de_thread(current); 841 if (retval) 842 goto out; 843 844 /* 845 * Make sure we have private file handles. Ask the 846 * fork helper to do the work for us and the exit 847 * helper to do the cleanup of the old one. 848 */ 849 files = current->files; /* refcounted so safe to hold */ 850 retval = unshare_files(); 851 if (retval) 852 goto out; 853 /* 854 * Release all of the old mmap stuff 855 */ 856 retval = exec_mmap(bprm->mm); 857 if (retval) 858 goto mmap_failed; 859 860 bprm->mm = NULL; /* We're using it now */ 861 862 /* This is the point of no return */ 863 steal_locks(files); 864 put_files_struct(files); 865 866 current->sas_ss_sp = current->sas_ss_size = 0; 867 868 if (current->euid == current->uid && current->egid == current->gid) 869 current->mm->dumpable = 1; 870 name = bprm->filename; 871 for (i=0; (ch = *(name++)) != '\0';) { 872 if (ch == '/') 873 i = 0; 874 else 875 if (i < (sizeof(tcomm) - 1)) 876 tcomm[i++] = ch; 877 } 878 tcomm[i] = '\0'; 879 set_task_comm(current, tcomm); 880 881 current->flags &= ~PF_RANDOMIZE; 882 flush_thread(); 883 884 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 885 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) || 886 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) { 887 suid_keys(current); 888 current->mm->dumpable = 0; 889 } 890 891 /* An exec changes our domain. We are no longer part of the thread 892 group */ 893 894 current->self_exec_id++; 895 896 flush_signal_handlers(current, 0); 897 flush_old_files(current->files); 898 899 return 0; 900 901mmap_failed: 902 put_files_struct(current->files); 903 current->files = files; 904out: 905 return retval; 906} 907 908EXPORT_SYMBOL(flush_old_exec); 909 910/* 911 * Fill the binprm structure from the inode. 912 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes 913 */ 914int prepare_binprm(struct linux_binprm *bprm) 915{ 916 int mode; 917 struct inode * inode = bprm->file->f_dentry->d_inode; 918 int retval; 919 920 mode = inode->i_mode; 921 /* 922 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE, 923 * generic_permission lets a non-executable through 924 */ 925 if (!(mode & 0111)) /* with at least _one_ execute bit set */ 926 return -EACCES; 927 if (bprm->file->f_op == NULL) 928 return -EACCES; 929 930 bprm->e_uid = current->euid; 931 bprm->e_gid = current->egid; 932 933 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) { 934 /* Set-uid? */ 935 if (mode & S_ISUID) { 936 current->personality &= ~PER_CLEAR_ON_SETID; 937 bprm->e_uid = inode->i_uid; 938 } 939 940 /* Set-gid? */ 941 /* 942 * If setgid is set but no group execute bit then this 943 * is a candidate for mandatory locking, not a setgid 944 * executable. 945 */ 946 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 947 current->personality &= ~PER_CLEAR_ON_SETID; 948 bprm->e_gid = inode->i_gid; 949 } 950 } 951 952 /* fill in binprm security blob */ 953 retval = security_bprm_set(bprm); 954 if (retval) 955 return retval; 956 957 memset(bprm->buf,0,BINPRM_BUF_SIZE); 958 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE); 959} 960 961EXPORT_SYMBOL(prepare_binprm); 962 963static inline int unsafe_exec(struct task_struct *p) 964{ 965 int unsafe = 0; 966 if (p->ptrace & PT_PTRACED) { 967 if (p->ptrace & PT_PTRACE_CAP) 968 unsafe |= LSM_UNSAFE_PTRACE_CAP; 969 else 970 unsafe |= LSM_UNSAFE_PTRACE; 971 } 972 if (atomic_read(&p->fs->count) > 1 || 973 atomic_read(&p->files->count) > 1 || 974 atomic_read(&p->sighand->count) > 1) 975 unsafe |= LSM_UNSAFE_SHARE; 976 977 return unsafe; 978} 979 980void compute_creds(struct linux_binprm *bprm) 981{ 982 int unsafe; 983 984 if (bprm->e_uid != current->uid) 985 suid_keys(current); 986 exec_keys(current); 987 988 task_lock(current); 989 unsafe = unsafe_exec(current); 990 security_bprm_apply_creds(bprm, unsafe); 991 task_unlock(current); 992 security_bprm_post_apply_creds(bprm); 993} 994 995EXPORT_SYMBOL(compute_creds); 996 997void remove_arg_zero(struct linux_binprm *bprm) 998{ 999 if (bprm->argc) { 1000 unsigned long offset; 1001 char * kaddr; 1002 struct page *page; 1003 1004 offset = bprm->p % PAGE_SIZE; 1005 goto inside; 1006 1007 while (bprm->p++, *(kaddr+offset++)) { 1008 if (offset != PAGE_SIZE) 1009 continue; 1010 offset = 0; 1011 kunmap_atomic(kaddr, KM_USER0); 1012inside: 1013 page = bprm->page[bprm->p/PAGE_SIZE]; 1014 kaddr = kmap_atomic(page, KM_USER0); 1015 } 1016 kunmap_atomic(kaddr, KM_USER0); 1017 bprm->argc--; 1018 } 1019} 1020 1021EXPORT_SYMBOL(remove_arg_zero); 1022 1023/* 1024 * cycle the list of binary formats handler, until one recognizes the image 1025 */ 1026int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs) 1027{ 1028 int try,retval; 1029 struct linux_binfmt *fmt; 1030#ifdef __alpha__ 1031 /* handle /sbin/loader.. */ 1032 { 1033 struct exec * eh = (struct exec *) bprm->buf; 1034 1035 if (!bprm->loader && eh->fh.f_magic == 0x183 && 1036 (eh->fh.f_flags & 0x3000) == 0x3000) 1037 { 1038 struct file * file; 1039 unsigned long loader; 1040 1041 allow_write_access(bprm->file); 1042 fput(bprm->file); 1043 bprm->file = NULL; 1044 1045 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); 1046 1047 file = open_exec("/sbin/loader"); 1048 retval = PTR_ERR(file); 1049 if (IS_ERR(file)) 1050 return retval; 1051 1052 /* Remember if the application is TASO. */ 1053 bprm->sh_bang = eh->ah.entry < 0x100000000UL; 1054 1055 bprm->file = file; 1056 bprm->loader = loader; 1057 retval = prepare_binprm(bprm); 1058 if (retval<0) 1059 return retval; 1060 /* should call search_binary_handler recursively here, 1061 but it does not matter */ 1062 } 1063 } 1064#endif 1065 retval = security_bprm_check(bprm); 1066 if (retval) 1067 return retval; 1068 1069 /* kernel module loader fixup */ 1070 /* so we don't try to load run modprobe in kernel space. */ 1071 set_fs(USER_DS); 1072 retval = -ENOENT; 1073 for (try=0; try<2; try++) { 1074 read_lock(&binfmt_lock); 1075 for (fmt = formats ; fmt ; fmt = fmt->next) { 1076 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary; 1077 if (!fn) 1078 continue; 1079 if (!try_module_get(fmt->module)) 1080 continue; 1081 read_unlock(&binfmt_lock); 1082 retval = fn(bprm, regs); 1083 if (retval >= 0) { 1084 put_binfmt(fmt); 1085 allow_write_access(bprm->file); 1086 if (bprm->file) 1087 fput(bprm->file); 1088 bprm->file = NULL; 1089 current->did_exec = 1; 1090 return retval; 1091 } 1092 read_lock(&binfmt_lock); 1093 put_binfmt(fmt); 1094 if (retval != -ENOEXEC || bprm->mm == NULL) 1095 break; 1096 if (!bprm->file) { 1097 read_unlock(&binfmt_lock); 1098 return retval; 1099 } 1100 } 1101 read_unlock(&binfmt_lock); 1102 if (retval != -ENOEXEC || bprm->mm == NULL) { 1103 break; 1104#ifdef CONFIG_KMOD 1105 }else{ 1106#define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e)) 1107 if (printable(bprm->buf[0]) && 1108 printable(bprm->buf[1]) && 1109 printable(bprm->buf[2]) && 1110 printable(bprm->buf[3])) 1111 break; /* -ENOEXEC */ 1112 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2])); 1113#endif 1114 } 1115 } 1116 return retval; 1117} 1118 1119EXPORT_SYMBOL(search_binary_handler); 1120 1121/* 1122 * sys_execve() executes a new program. 1123 */ 1124int do_execve(char * filename, 1125 char __user *__user *argv, 1126 char __user *__user *envp, 1127 struct pt_regs * regs) 1128{ 1129 struct linux_binprm *bprm; 1130 struct file *file; 1131 int retval; 1132 int i; 1133 1134 retval = -ENOMEM; 1135 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL); 1136 if (!bprm) 1137 goto out_ret; 1138 memset(bprm, 0, sizeof(*bprm)); 1139 1140 file = open_exec(filename); 1141 retval = PTR_ERR(file); 1142 if (IS_ERR(file)) 1143 goto out_kfree; 1144 1145 sched_exec(); 1146 1147 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); 1148 1149 bprm->file = file; 1150 bprm->filename = filename; 1151 bprm->interp = filename; 1152 bprm->mm = mm_alloc(); 1153 retval = -ENOMEM; 1154 if (!bprm->mm) 1155 goto out_file; 1156 1157 retval = init_new_context(current, bprm->mm); 1158 if (retval < 0) 1159 goto out_mm; 1160 1161 bprm->argc = count(argv, bprm->p / sizeof(void *)); 1162 if ((retval = bprm->argc) < 0) 1163 goto out_mm; 1164 1165 bprm->envc = count(envp, bprm->p / sizeof(void *)); 1166 if ((retval = bprm->envc) < 0) 1167 goto out_mm; 1168 1169 retval = security_bprm_alloc(bprm); 1170 if (retval) 1171 goto out; 1172 1173 retval = prepare_binprm(bprm); 1174 if (retval < 0) 1175 goto out; 1176 1177 retval = copy_strings_kernel(1, &bprm->filename, bprm); 1178 if (retval < 0) 1179 goto out; 1180 1181 bprm->exec = bprm->p; 1182 retval = copy_strings(bprm->envc, envp, bprm); 1183 if (retval < 0) 1184 goto out; 1185 1186 retval = copy_strings(bprm->argc, argv, bprm); 1187 if (retval < 0) 1188 goto out; 1189 1190 retval = search_binary_handler(bprm,regs); 1191 if (retval >= 0) { 1192 free_arg_pages(bprm); 1193 1194 /* execve success */ 1195 security_bprm_free(bprm); 1196 acct_update_integrals(current); 1197 update_mem_hiwater(current); 1198 kfree(bprm); 1199 return retval; 1200 } 1201 1202out: 1203 /* Something went wrong, return the inode and free the argument pages*/ 1204 for (i = 0 ; i < MAX_ARG_PAGES ; i++) { 1205 struct page * page = bprm->page[i]; 1206 if (page) 1207 __free_page(page); 1208 } 1209 1210 if (bprm->security) 1211 security_bprm_free(bprm); 1212 1213out_mm: 1214 if (bprm->mm) 1215 mmdrop(bprm->mm); 1216 1217out_file: 1218 if (bprm->file) { 1219 allow_write_access(bprm->file); 1220 fput(bprm->file); 1221 } 1222 1223out_kfree: 1224 kfree(bprm); 1225 1226out_ret: 1227 return retval; 1228} 1229 1230int set_binfmt(struct linux_binfmt *new) 1231{ 1232 struct linux_binfmt *old = current->binfmt; 1233 1234 if (new) { 1235 if (!try_module_get(new->module)) 1236 return -1; 1237 } 1238 current->binfmt = new; 1239 if (old) 1240 module_put(old->module); 1241 return 0; 1242} 1243 1244EXPORT_SYMBOL(set_binfmt); 1245 1246#define CORENAME_MAX_SIZE 64 1247 1248/* format_corename will inspect the pattern parameter, and output a 1249 * name into corename, which must have space for at least 1250 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator. 1251 */ 1252static void format_corename(char *corename, const char *pattern, long signr) 1253{ 1254 const char *pat_ptr = pattern; 1255 char *out_ptr = corename; 1256 char *const out_end = corename + CORENAME_MAX_SIZE; 1257 int rc; 1258 int pid_in_pattern = 0; 1259 1260 /* Repeat as long as we have more pattern to process and more output 1261 space */ 1262 while (*pat_ptr) { 1263 if (*pat_ptr != '%') { 1264 if (out_ptr == out_end) 1265 goto out; 1266 *out_ptr++ = *pat_ptr++; 1267 } else { 1268 switch (*++pat_ptr) { 1269 case 0: 1270 goto out; 1271 /* Double percent, output one percent */ 1272 case '%': 1273 if (out_ptr == out_end) 1274 goto out; 1275 *out_ptr++ = '%'; 1276 break; 1277 /* pid */ 1278 case 'p': 1279 pid_in_pattern = 1; 1280 rc = snprintf(out_ptr, out_end - out_ptr, 1281 "%d", current->tgid); 1282 if (rc > out_end - out_ptr) 1283 goto out; 1284 out_ptr += rc; 1285 break; 1286 /* uid */ 1287 case 'u': 1288 rc = snprintf(out_ptr, out_end - out_ptr, 1289 "%d", current->uid); 1290 if (rc > out_end - out_ptr) 1291 goto out; 1292 out_ptr += rc; 1293 break; 1294 /* gid */ 1295 case 'g': 1296 rc = snprintf(out_ptr, out_end - out_ptr, 1297 "%d", current->gid); 1298 if (rc > out_end - out_ptr) 1299 goto out; 1300 out_ptr += rc; 1301 break; 1302 /* signal that caused the coredump */ 1303 case 's': 1304 rc = snprintf(out_ptr, out_end - out_ptr, 1305 "%ld", signr); 1306 if (rc > out_end - out_ptr) 1307 goto out; 1308 out_ptr += rc; 1309 break; 1310 /* UNIX time of coredump */ 1311 case 't': { 1312 struct timeval tv; 1313 do_gettimeofday(&tv); 1314 rc = snprintf(out_ptr, out_end - out_ptr, 1315 "%lu", tv.tv_sec); 1316 if (rc > out_end - out_ptr) 1317 goto out; 1318 out_ptr += rc; 1319 break; 1320 } 1321 /* hostname */ 1322 case 'h': 1323 down_read(&uts_sem); 1324 rc = snprintf(out_ptr, out_end - out_ptr, 1325 "%s", system_utsname.nodename); 1326 up_read(&uts_sem); 1327 if (rc > out_end - out_ptr) 1328 goto out; 1329 out_ptr += rc; 1330 break; 1331 /* executable */ 1332 case 'e': 1333 rc = snprintf(out_ptr, out_end - out_ptr, 1334 "%s", current->comm); 1335 if (rc > out_end - out_ptr) 1336 goto out; 1337 out_ptr += rc; 1338 break; 1339 default: 1340 break; 1341 } 1342 ++pat_ptr; 1343 } 1344 } 1345 /* Backward compatibility with core_uses_pid: 1346 * 1347 * If core_pattern does not include a %p (as is the default) 1348 * and core_uses_pid is set, then .%pid will be appended to 1349 * the filename */ 1350 if (!pid_in_pattern 1351 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) { 1352 rc = snprintf(out_ptr, out_end - out_ptr, 1353 ".%d", current->tgid); 1354 if (rc > out_end - out_ptr) 1355 goto out; 1356 out_ptr += rc; 1357 } 1358 out: 1359 *out_ptr = 0; 1360} 1361 1362static void zap_threads (struct mm_struct *mm) 1363{ 1364 struct task_struct *g, *p; 1365 struct task_struct *tsk = current; 1366 struct completion *vfork_done = tsk->vfork_done; 1367 int traced = 0; 1368 1369 /* 1370 * Make sure nobody is waiting for us to release the VM, 1371 * otherwise we can deadlock when we wait on each other 1372 */ 1373 if (vfork_done) { 1374 tsk->vfork_done = NULL; 1375 complete(vfork_done); 1376 } 1377 1378 read_lock(&tasklist_lock); 1379 do_each_thread(g,p) 1380 if (mm == p->mm && p != tsk) { 1381 force_sig_specific(SIGKILL, p); 1382 mm->core_waiters++; 1383 if (unlikely(p->ptrace) && 1384 unlikely(p->parent->mm == mm)) 1385 traced = 1; 1386 } 1387 while_each_thread(g,p); 1388 1389 read_unlock(&tasklist_lock); 1390 1391 if (unlikely(traced)) { 1392 /* 1393 * We are zapping a thread and the thread it ptraces. 1394 * If the tracee went into a ptrace stop for exit tracing, 1395 * we could deadlock since the tracer is waiting for this 1396 * coredump to finish. Detach them so they can both die. 1397 */ 1398 write_lock_irq(&tasklist_lock); 1399 do_each_thread(g,p) { 1400 if (mm == p->mm && p != tsk && 1401 p->ptrace && p->parent->mm == mm) { 1402 __ptrace_unlink(p); 1403 } 1404 } while_each_thread(g,p); 1405 write_unlock_irq(&tasklist_lock); 1406 } 1407} 1408 1409static void coredump_wait(struct mm_struct *mm) 1410{ 1411 DECLARE_COMPLETION(startup_done); 1412 1413 mm->core_waiters++; /* let other threads block */ 1414 mm->core_startup_done = &startup_done; 1415 1416 /* give other threads a chance to run: */ 1417 yield(); 1418 1419 zap_threads(mm); 1420 if (--mm->core_waiters) { 1421 up_write(&mm->mmap_sem); 1422 wait_for_completion(&startup_done); 1423 } else 1424 up_write(&mm->mmap_sem); 1425 BUG_ON(mm->core_waiters); 1426} 1427 1428int do_coredump(long signr, int exit_code, struct pt_regs * regs) 1429{ 1430 char corename[CORENAME_MAX_SIZE + 1]; 1431 struct mm_struct *mm = current->mm; 1432 struct linux_binfmt * binfmt; 1433 struct inode * inode; 1434 struct file * file; 1435 int retval = 0; 1436 1437 binfmt = current->binfmt; 1438 if (!binfmt || !binfmt->core_dump) 1439 goto fail; 1440 down_write(&mm->mmap_sem); 1441 if (!mm->dumpable) { 1442 up_write(&mm->mmap_sem); 1443 goto fail; 1444 } 1445 mm->dumpable = 0; 1446 init_completion(&mm->core_done); 1447 spin_lock_irq(&current->sighand->siglock); 1448 current->signal->flags = SIGNAL_GROUP_EXIT; 1449 current->signal->group_exit_code = exit_code; 1450 spin_unlock_irq(&current->sighand->siglock); 1451 coredump_wait(mm); 1452 1453 /* 1454 * Clear any false indication of pending signals that might 1455 * be seen by the filesystem code called to write the core file. 1456 */ 1457 current->signal->group_stop_count = 0; 1458 clear_thread_flag(TIF_SIGPENDING); 1459 1460 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump) 1461 goto fail_unlock; 1462 1463 /* 1464 * lock_kernel() because format_corename() is controlled by sysctl, which 1465 * uses lock_kernel() 1466 */ 1467 lock_kernel(); 1468 format_corename(corename, core_pattern, signr); 1469 unlock_kernel(); 1470 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600); 1471 if (IS_ERR(file)) 1472 goto fail_unlock; 1473 inode = file->f_dentry->d_inode; 1474 if (inode->i_nlink > 1) 1475 goto close_fail; /* multiple links - don't dump */ 1476 if (d_unhashed(file->f_dentry)) 1477 goto close_fail; 1478 1479 if (!S_ISREG(inode->i_mode)) 1480 goto close_fail; 1481 if (!file->f_op) 1482 goto close_fail; 1483 if (!file->f_op->write) 1484 goto close_fail; 1485 if (do_truncate(file->f_dentry, 0) != 0) 1486 goto close_fail; 1487 1488 retval = binfmt->core_dump(signr, regs, file); 1489 1490 if (retval) 1491 current->signal->group_exit_code |= 0x80; 1492close_fail: 1493 filp_close(file, NULL); 1494fail_unlock: 1495 complete_all(&mm->core_done); 1496fail: 1497 return retval; 1498}