4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * #!-checking implemented by tytso.
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.
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.
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
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 #include <linux/cn_proc.h>
52 #include <linux/vs_memory.h>
53 #include <linux/vs_cvirt.h>
55 #include <asm/uaccess.h>
56 #include <asm/mmu_context.h>
59 #include <linux/kmod.h>
63 char core_pattern[65] = "core";
64 int suid_dumpable = 0;
66 EXPORT_SYMBOL(suid_dumpable);
67 /* The maximal length of core_pattern is also specified in sysctl.c */
69 static struct linux_binfmt *formats;
70 static DEFINE_RWLOCK(binfmt_lock);
72 int register_binfmt(struct linux_binfmt * fmt)
74 struct linux_binfmt ** tmp = &formats;
80 write_lock(&binfmt_lock);
83 write_unlock(&binfmt_lock);
90 write_unlock(&binfmt_lock);
94 EXPORT_SYMBOL(register_binfmt);
96 int unregister_binfmt(struct linux_binfmt * fmt)
98 struct linux_binfmt ** tmp = &formats;
100 write_lock(&binfmt_lock);
104 write_unlock(&binfmt_lock);
109 write_unlock(&binfmt_lock);
113 EXPORT_SYMBOL(unregister_binfmt);
115 static inline void put_binfmt(struct linux_binfmt * fmt)
117 module_put(fmt->module);
121 * Note that a shared library must be both readable and executable due to
124 * Also note that we take the address to load from from the file itself.
126 asmlinkage long sys_uselib(const char __user * library)
132 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
137 if (!S_ISREG(nd.dentry->d_inode->i_mode))
140 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
144 file = nameidata_to_filp(&nd, O_RDONLY);
145 error = PTR_ERR(file);
151 struct linux_binfmt * fmt;
153 read_lock(&binfmt_lock);
154 for (fmt = formats ; fmt ; fmt = fmt->next) {
155 if (!fmt->load_shlib)
157 if (!try_module_get(fmt->module))
159 read_unlock(&binfmt_lock);
160 error = fmt->load_shlib(file);
161 read_lock(&binfmt_lock);
163 if (error != -ENOEXEC)
166 read_unlock(&binfmt_lock);
172 release_open_intent(&nd);
178 * count() counts the number of strings in array ARGV.
180 static int count(char __user * __user * argv, int max)
188 if (get_user(p, argv))
202 * 'copy_strings()' copies argument/environment strings from user
203 * memory to free pages in kernel mem. These are in a format ready
204 * to be put directly into the top of new user memory.
206 static int copy_strings(int argc, char __user * __user * argv,
207 struct linux_binprm *bprm)
209 struct page *kmapped_page = NULL;
218 if (get_user(str, argv+argc) ||
219 !(len = strnlen_user(str, bprm->p))) {
230 /* XXX: add architecture specific overflow check here. */
235 int offset, bytes_to_copy;
238 offset = pos % PAGE_SIZE;
240 page = bprm->page[i];
243 page = alloc_page(GFP_HIGHUSER);
244 bprm->page[i] = page;
252 if (page != kmapped_page) {
254 kunmap(kmapped_page);
256 kaddr = kmap(kmapped_page);
259 memset(kaddr, 0, offset);
260 bytes_to_copy = PAGE_SIZE - offset;
261 if (bytes_to_copy > len) {
264 memset(kaddr+offset+len, 0,
265 PAGE_SIZE-offset-len);
267 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
273 pos += bytes_to_copy;
274 str += bytes_to_copy;
275 len -= bytes_to_copy;
281 kunmap(kmapped_page);
286 * Like copy_strings, but get argv and its values from kernel memory.
288 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
291 mm_segment_t oldfs = get_fs();
293 r = copy_strings(argc, (char __user * __user *)argv, bprm);
298 EXPORT_SYMBOL(copy_strings_kernel);
302 * This routine is used to map in a page into an address space: needed by
303 * execve() for the initial stack and environment pages.
305 * vma->vm_mm->mmap_sem is held for writing.
307 void install_arg_page(struct vm_area_struct *vma,
308 struct page *page, unsigned long address)
310 struct mm_struct *mm = vma->vm_mm;
314 if (unlikely(anon_vma_prepare(vma)))
317 flush_dcache_page(page);
318 pte = get_locked_pte(mm, address, &ptl);
321 if (!pte_none(*pte)) {
322 pte_unmap_unlock(pte, ptl);
325 inc_mm_counter(mm, anon_rss);
326 lru_cache_add_active(page);
327 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
328 page, vma->vm_page_prot))));
329 page_add_new_anon_rmap(page, vma, address);
330 pte_unmap_unlock(pte, ptl);
332 /* no need for flush_tlb */
336 force_sig(SIGKILL, current);
339 #define EXTRA_STACK_VM_PAGES 20 /* random */
341 int setup_arg_pages(struct linux_binprm *bprm,
342 unsigned long stack_top,
343 int executable_stack)
345 unsigned long stack_base;
346 struct vm_area_struct *mpnt;
347 struct mm_struct *mm = current->mm;
351 #ifdef CONFIG_STACK_GROWSUP
352 /* Move the argument and environment strings to the bottom of the
358 /* Start by shifting all the pages down */
360 for (j = 0; j < MAX_ARG_PAGES; j++) {
361 struct page *page = bprm->page[j];
364 bprm->page[i++] = page;
367 /* Now move them within their pages */
368 offset = bprm->p % PAGE_SIZE;
369 to = kmap(bprm->page[0]);
370 for (j = 1; j < i; j++) {
371 memmove(to, to + offset, PAGE_SIZE - offset);
372 from = kmap(bprm->page[j]);
373 memcpy(to + PAGE_SIZE - offset, from, offset);
374 kunmap(bprm->page[j - 1]);
377 memmove(to, to + offset, PAGE_SIZE - offset);
378 kunmap(bprm->page[j - 1]);
380 /* Limit stack size to 1GB */
381 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
382 if (stack_base > (1 << 30))
383 stack_base = 1 << 30;
384 stack_base = PAGE_ALIGN(stack_top - stack_base);
386 /* Adjust bprm->p to point to the end of the strings. */
387 bprm->p = stack_base + PAGE_SIZE * i - offset;
389 mm->arg_start = stack_base;
390 arg_size = i << PAGE_SHIFT;
392 /* zero pages that were copied above */
393 while (i < MAX_ARG_PAGES)
394 bprm->page[i++] = NULL;
396 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
397 stack_base = PAGE_ALIGN(stack_base);
398 bprm->p += stack_base;
399 mm->arg_start = bprm->p;
400 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
403 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
406 bprm->loader += stack_base;
407 bprm->exec += stack_base;
409 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
413 memset(mpnt, 0, sizeof(*mpnt));
415 down_write(&mm->mmap_sem);
418 #ifdef CONFIG_STACK_GROWSUP
419 mpnt->vm_start = stack_base;
420 mpnt->vm_end = stack_base + arg_size;
422 mpnt->vm_end = stack_top;
423 mpnt->vm_start = mpnt->vm_end - arg_size;
425 /* Adjust stack execute permissions; explicitly enable
426 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
427 * and leave alone (arch default) otherwise. */
428 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
429 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
430 else if (executable_stack == EXSTACK_DISABLE_X)
431 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
433 mpnt->vm_flags = VM_STACK_FLAGS;
434 mpnt->vm_flags |= mm->def_flags;
435 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
436 if ((ret = insert_vm_struct(mm, mpnt))) {
437 up_write(&mm->mmap_sem);
438 kmem_cache_free(vm_area_cachep, mpnt);
441 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
442 mm->stack_vm = mm->total_vm;
445 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
446 struct page *page = bprm->page[i];
448 bprm->page[i] = NULL;
449 install_arg_page(mpnt, page, stack_base);
451 stack_base += PAGE_SIZE;
453 up_write(&mm->mmap_sem);
458 EXPORT_SYMBOL(setup_arg_pages);
460 #define free_arg_pages(bprm) do { } while (0)
464 static inline void free_arg_pages(struct linux_binprm *bprm)
468 for (i = 0; i < MAX_ARG_PAGES; i++) {
470 __free_page(bprm->page[i]);
471 bprm->page[i] = NULL;
475 #endif /* CONFIG_MMU */
477 struct file *open_exec(const char *name)
483 err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ);
487 struct inode *inode = nd.dentry->d_inode;
488 file = ERR_PTR(-EACCES);
489 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
490 S_ISREG(inode->i_mode)) {
491 int err = vfs_permission(&nd, MAY_EXEC);
492 if (!err && !(inode->i_mode & 0111))
496 file = nameidata_to_filp(&nd, O_RDONLY);
498 err = deny_write_access(file);
508 release_open_intent(&nd);
514 EXPORT_SYMBOL(open_exec);
516 int kernel_read(struct file *file, unsigned long offset,
517 char *addr, unsigned long count)
525 /* The cast to a user pointer is valid due to the set_fs() */
526 result = vfs_read(file, (void __user *)addr, count, &pos);
531 EXPORT_SYMBOL(kernel_read);
533 static int exec_mmap(struct mm_struct *mm)
535 struct task_struct *tsk;
536 struct mm_struct * old_mm, *active_mm;
538 /* Notify parent that we're no longer interested in the old VM */
540 old_mm = current->mm;
541 mm_release(tsk, old_mm);
545 * Make sure that if there is a core dump in progress
546 * for the old mm, we get out and die instead of going
547 * through with the exec. We must hold mmap_sem around
548 * checking core_waiters and changing tsk->mm. The
549 * core-inducing thread will increment core_waiters for
550 * each thread whose ->mm == old_mm.
552 down_read(&old_mm->mmap_sem);
553 if (unlikely(old_mm->core_waiters)) {
554 up_read(&old_mm->mmap_sem);
559 active_mm = tsk->active_mm;
562 activate_mm(active_mm, mm);
564 arch_pick_mmap_layout(mm);
566 up_read(&old_mm->mmap_sem);
567 if (active_mm != old_mm) BUG();
576 * This function makes sure the current process has its own signal table,
577 * so that flush_signal_handlers can later reset the handlers without
578 * disturbing other processes. (Other processes might share the signal
579 * table via the CLONE_SIGHAND option to clone().)
581 static int de_thread(struct task_struct *tsk)
583 struct signal_struct *sig = tsk->signal;
584 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
585 spinlock_t *lock = &oldsighand->siglock;
586 struct task_struct *leader = NULL;
590 * If we don't share sighandlers, then we aren't sharing anything
591 * and we can just re-use it all.
593 if (atomic_read(&oldsighand->count) <= 1) {
594 BUG_ON(atomic_read(&sig->count) != 1);
599 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
603 if (thread_group_empty(current))
604 goto no_thread_group;
607 * Kill all other threads in the thread group.
608 * We must hold tasklist_lock to call zap_other_threads.
610 read_lock(&tasklist_lock);
612 if (sig->flags & SIGNAL_GROUP_EXIT) {
614 * Another group action in progress, just
615 * return so that the signal is processed.
617 spin_unlock_irq(lock);
618 read_unlock(&tasklist_lock);
619 kmem_cache_free(sighand_cachep, newsighand);
622 zap_other_threads(current);
623 read_unlock(&tasklist_lock);
626 * Account for the thread group leader hanging around:
629 if (!thread_group_leader(current)) {
632 * The SIGALRM timer survives the exec, but needs to point
633 * at us as the new group leader now. We have a race with
634 * a timer firing now getting the old leader, so we need to
635 * synchronize with any firing (by calling del_timer_sync)
636 * before we can safely let the old group leader die.
638 sig->real_timer.data = current;
639 spin_unlock_irq(lock);
640 if (hrtimer_cancel(&sig->real_timer))
641 hrtimer_restart(&sig->real_timer);
644 while (atomic_read(&sig->count) > count) {
645 sig->group_exit_task = current;
646 sig->notify_count = count;
647 __set_current_state(TASK_UNINTERRUPTIBLE);
648 spin_unlock_irq(lock);
652 sig->group_exit_task = NULL;
653 sig->notify_count = 0;
654 spin_unlock_irq(lock);
657 * At this point all other threads have exited, all we have to
658 * do is to wait for the thread group leader to become inactive,
659 * and to assume its PID:
661 if (!thread_group_leader(current)) {
662 struct task_struct *parent;
663 struct dentry *proc_dentry1, *proc_dentry2;
664 unsigned long ptrace;
667 * Wait for the thread group leader to be a zombie.
668 * It should already be zombie at this point, most
671 leader = current->group_leader;
672 while (leader->exit_state != EXIT_ZOMBIE)
675 spin_lock(&leader->proc_lock);
676 spin_lock(¤t->proc_lock);
677 proc_dentry1 = proc_pid_unhash(current);
678 proc_dentry2 = proc_pid_unhash(leader);
679 write_lock_irq(&tasklist_lock);
681 BUG_ON(leader->tgid != current->tgid);
682 BUG_ON(current->pid == current->tgid);
684 * An exec() starts a new thread group with the
685 * TGID of the previous thread group. Rehash the
686 * two threads with a switched PID, and release
687 * the former thread group leader:
689 ptrace = leader->ptrace;
690 parent = leader->parent;
691 if (unlikely(ptrace) && unlikely(parent == current)) {
693 * Joker was ptracing his own group leader,
694 * and now he wants to be his own parent!
695 * We can't have that.
700 ptrace_unlink(current);
701 ptrace_unlink(leader);
702 remove_parent(current);
703 remove_parent(leader);
705 switch_exec_pids(leader, current);
707 current->parent = current->real_parent = leader->real_parent;
708 leader->parent = leader->real_parent = child_reaper;
709 current->group_leader = current;
710 leader->group_leader = leader;
712 add_parent(current, current->parent);
713 add_parent(leader, leader->parent);
715 current->ptrace = ptrace;
716 __ptrace_link(current, parent);
719 list_del(¤t->tasks);
720 list_add_tail(¤t->tasks, &init_task.tasks);
721 current->exit_signal = SIGCHLD;
723 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
724 leader->exit_state = EXIT_DEAD;
726 write_unlock_irq(&tasklist_lock);
727 spin_unlock(&leader->proc_lock);
728 spin_unlock(¤t->proc_lock);
729 proc_pid_flush(proc_dentry1);
730 proc_pid_flush(proc_dentry2);
734 * There may be one thread left which is just exiting,
735 * but it's safe to stop telling the group to kill themselves.
742 release_task(leader);
744 BUG_ON(atomic_read(&sig->count) != 1);
746 if (atomic_read(&oldsighand->count) == 1) {
748 * Now that we nuked the rest of the thread group,
749 * it turns out we are not sharing sighand any more either.
750 * So we can just keep it.
752 kmem_cache_free(sighand_cachep, newsighand);
755 * Move our state over to newsighand and switch it in.
757 spin_lock_init(&newsighand->siglock);
758 atomic_set(&newsighand->count, 1);
759 memcpy(newsighand->action, oldsighand->action,
760 sizeof(newsighand->action));
762 write_lock_irq(&tasklist_lock);
763 spin_lock(&oldsighand->siglock);
764 spin_lock(&newsighand->siglock);
766 rcu_assign_pointer(current->sighand, newsighand);
769 spin_unlock(&newsighand->siglock);
770 spin_unlock(&oldsighand->siglock);
771 write_unlock_irq(&tasklist_lock);
773 if (atomic_dec_and_test(&oldsighand->count))
774 sighand_free(oldsighand);
777 BUG_ON(!thread_group_leader(current));
782 * These functions flushes out all traces of the currently running executable
783 * so that a new one can be started
786 static void flush_old_files(struct files_struct * files)
791 spin_lock(&files->file_lock);
793 unsigned long set, i;
797 fdt = files_fdtable(files);
798 if (i >= fdt->max_fds || i >= fdt->max_fdset)
800 set = fdt->close_on_exec->fds_bits[j];
803 fdt->close_on_exec->fds_bits[j] = 0;
804 spin_unlock(&files->file_lock);
805 for ( ; set ; i++,set >>= 1) {
810 spin_lock(&files->file_lock);
813 spin_unlock(&files->file_lock);
816 void get_task_comm(char *buf, struct task_struct *tsk)
818 /* buf must be at least sizeof(tsk->comm) in size */
820 strncpy(buf, tsk->comm, sizeof(tsk->comm));
824 void set_task_comm(struct task_struct *tsk, char *buf)
827 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
831 int flush_old_exec(struct linux_binprm * bprm)
835 struct files_struct *files;
836 char tcomm[sizeof(current->comm)];
839 * Make sure we have a private signal table and that
840 * we are unassociated from the previous thread group.
842 retval = de_thread(current);
847 * Make sure we have private file handles. Ask the
848 * fork helper to do the work for us and the exit
849 * helper to do the cleanup of the old one.
851 files = current->files; /* refcounted so safe to hold */
852 retval = unshare_files();
856 * Release all of the old mmap stuff
858 retval = exec_mmap(bprm->mm);
862 bprm->mm = NULL; /* We're using it now */
864 /* This is the point of no return */
866 put_files_struct(files);
868 current->sas_ss_sp = current->sas_ss_size = 0;
870 if (current->euid == current->uid && current->egid == current->gid)
871 current->mm->dumpable = 1;
873 current->mm->dumpable = suid_dumpable;
875 name = bprm->filename;
877 /* Copies the binary name from after last slash */
878 for (i=0; (ch = *(name++)) != '\0';) {
880 i = 0; /* overwrite what we wrote */
882 if (i < (sizeof(tcomm) - 1))
886 set_task_comm(current, tcomm);
888 current->flags &= ~PF_RANDOMIZE;
891 /* Set the new mm task size. We have to do that late because it may
892 * depend on TIF_32BIT which is only updated in flush_thread() on
893 * some architectures like powerpc
895 current->mm->task_size = TASK_SIZE;
897 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
898 file_permission(bprm->file, MAY_READ) ||
899 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
901 current->mm->dumpable = suid_dumpable;
904 /* An exec changes our domain. We are no longer part of the thread
907 current->self_exec_id++;
909 flush_signal_handlers(current, 0);
910 flush_old_files(current->files);
915 put_files_struct(current->files);
916 current->files = files;
921 EXPORT_SYMBOL(flush_old_exec);
924 * Fill the binprm structure from the inode.
925 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
927 int prepare_binprm(struct linux_binprm *bprm)
930 struct inode * inode = bprm->file->f_dentry->d_inode;
933 mode = inode->i_mode;
935 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
936 * generic_permission lets a non-executable through
938 if (!(mode & 0111)) /* with at least _one_ execute bit set */
940 if (bprm->file->f_op == NULL)
943 bprm->e_uid = current->euid;
944 bprm->e_gid = current->egid;
946 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
948 if (mode & S_ISUID) {
949 current->personality &= ~PER_CLEAR_ON_SETID;
950 bprm->e_uid = inode->i_uid;
955 * If setgid is set but no group execute bit then this
956 * is a candidate for mandatory locking, not a setgid
959 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
960 current->personality &= ~PER_CLEAR_ON_SETID;
961 bprm->e_gid = inode->i_gid;
965 /* fill in binprm security blob */
966 retval = security_bprm_set(bprm);
970 memset(bprm->buf,0,BINPRM_BUF_SIZE);
971 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
974 EXPORT_SYMBOL(prepare_binprm);
976 static int unsafe_exec(struct task_struct *p)
979 if (p->ptrace & PT_PTRACED) {
980 if (p->ptrace & PT_PTRACE_CAP)
981 unsafe |= LSM_UNSAFE_PTRACE_CAP;
983 unsafe |= LSM_UNSAFE_PTRACE;
985 if (atomic_read(&p->fs->count) > 1 ||
986 atomic_read(&p->files->count) > 1 ||
987 atomic_read(&p->sighand->count) > 1)
988 unsafe |= LSM_UNSAFE_SHARE;
993 void compute_creds(struct linux_binprm *bprm)
997 if (bprm->e_uid != current->uid)
1002 unsafe = unsafe_exec(current);
1003 security_bprm_apply_creds(bprm, unsafe);
1004 task_unlock(current);
1005 security_bprm_post_apply_creds(bprm);
1008 EXPORT_SYMBOL(compute_creds);
1010 void remove_arg_zero(struct linux_binprm *bprm)
1013 unsigned long offset;
1017 offset = bprm->p % PAGE_SIZE;
1020 while (bprm->p++, *(kaddr+offset++)) {
1021 if (offset != PAGE_SIZE)
1024 kunmap_atomic(kaddr, KM_USER0);
1026 page = bprm->page[bprm->p/PAGE_SIZE];
1027 kaddr = kmap_atomic(page, KM_USER0);
1029 kunmap_atomic(kaddr, KM_USER0);
1034 EXPORT_SYMBOL(remove_arg_zero);
1037 * cycle the list of binary formats handler, until one recognizes the image
1039 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1042 struct linux_binfmt *fmt;
1044 /* handle /sbin/loader.. */
1046 struct exec * eh = (struct exec *) bprm->buf;
1048 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1049 (eh->fh.f_flags & 0x3000) == 0x3000)
1052 unsigned long loader;
1054 allow_write_access(bprm->file);
1058 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1060 file = open_exec("/sbin/loader");
1061 retval = PTR_ERR(file);
1065 /* Remember if the application is TASO. */
1066 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1069 bprm->loader = loader;
1070 retval = prepare_binprm(bprm);
1073 /* should call search_binary_handler recursively here,
1074 but it does not matter */
1078 retval = security_bprm_check(bprm);
1082 /* kernel module loader fixup */
1083 /* so we don't try to load run modprobe in kernel space. */
1086 for (try=0; try<2; try++) {
1087 read_lock(&binfmt_lock);
1088 for (fmt = formats ; fmt ; fmt = fmt->next) {
1089 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1092 if (!try_module_get(fmt->module))
1094 read_unlock(&binfmt_lock);
1095 retval = fn(bprm, regs);
1098 allow_write_access(bprm->file);
1102 current->did_exec = 1;
1103 proc_exec_connector(current);
1106 read_lock(&binfmt_lock);
1108 if (retval != -ENOEXEC || bprm->mm == NULL)
1111 read_unlock(&binfmt_lock);
1115 read_unlock(&binfmt_lock);
1116 if (retval != -ENOEXEC || bprm->mm == NULL) {
1120 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1121 if (printable(bprm->buf[0]) &&
1122 printable(bprm->buf[1]) &&
1123 printable(bprm->buf[2]) &&
1124 printable(bprm->buf[3]))
1125 break; /* -ENOEXEC */
1126 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1133 EXPORT_SYMBOL(search_binary_handler);
1136 * sys_execve() executes a new program.
1138 int do_execve(char * filename,
1139 char __user *__user *argv,
1140 char __user *__user *envp,
1141 struct pt_regs * regs)
1143 struct linux_binprm *bprm;
1149 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1152 memset(bprm, 0, sizeof(*bprm));
1154 file = open_exec(filename);
1155 retval = PTR_ERR(file);
1161 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1164 bprm->filename = filename;
1165 bprm->interp = filename;
1166 bprm->mm = mm_alloc();
1171 retval = init_new_context(current, bprm->mm);
1175 bprm->argc = count(argv, bprm->p / sizeof(void *));
1176 if ((retval = bprm->argc) < 0)
1179 bprm->envc = count(envp, bprm->p / sizeof(void *));
1180 if ((retval = bprm->envc) < 0)
1183 retval = security_bprm_alloc(bprm);
1187 retval = prepare_binprm(bprm);
1191 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1195 bprm->exec = bprm->p;
1196 retval = copy_strings(bprm->envc, envp, bprm);
1200 retval = copy_strings(bprm->argc, argv, bprm);
1204 retval = search_binary_handler(bprm,regs);
1206 free_arg_pages(bprm);
1208 /* execve success */
1209 security_bprm_free(bprm);
1210 acct_update_integrals(current);
1216 /* Something went wrong, return the inode and free the argument pages*/
1217 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1218 struct page * page = bprm->page[i];
1224 security_bprm_free(bprm);
1232 allow_write_access(bprm->file);
1243 int set_binfmt(struct linux_binfmt *new)
1245 struct linux_binfmt *old = current->binfmt;
1248 if (!try_module_get(new->module))
1251 current->binfmt = new;
1253 module_put(old->module);
1257 EXPORT_SYMBOL(set_binfmt);
1259 #define CORENAME_MAX_SIZE 64
1261 /* format_corename will inspect the pattern parameter, and output a
1262 * name into corename, which must have space for at least
1263 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1265 static void format_corename(char *corename, const char *pattern, long signr)
1267 const char *pat_ptr = pattern;
1268 char *out_ptr = corename;
1269 char *const out_end = corename + CORENAME_MAX_SIZE;
1271 int pid_in_pattern = 0;
1273 /* Repeat as long as we have more pattern to process and more output
1276 if (*pat_ptr != '%') {
1277 if (out_ptr == out_end)
1279 *out_ptr++ = *pat_ptr++;
1281 switch (*++pat_ptr) {
1284 /* Double percent, output one percent */
1286 if (out_ptr == out_end)
1293 rc = snprintf(out_ptr, out_end - out_ptr,
1294 "%d", current->tgid);
1295 if (rc > out_end - out_ptr)
1301 rc = snprintf(out_ptr, out_end - out_ptr,
1302 "%d", current->uid);
1303 if (rc > out_end - out_ptr)
1309 rc = snprintf(out_ptr, out_end - out_ptr,
1310 "%d", current->gid);
1311 if (rc > out_end - out_ptr)
1315 /* signal that caused the coredump */
1317 rc = snprintf(out_ptr, out_end - out_ptr,
1319 if (rc > out_end - out_ptr)
1323 /* UNIX time of coredump */
1326 do_gettimeofday(&tv);
1327 rc = snprintf(out_ptr, out_end - out_ptr,
1329 if (rc > out_end - out_ptr)
1336 down_read(&uts_sem);
1337 rc = snprintf(out_ptr, out_end - out_ptr,
1338 "%s", vx_new_uts(nodename));
1340 if (rc > out_end - out_ptr)
1346 rc = snprintf(out_ptr, out_end - out_ptr,
1347 "%s", current->comm);
1348 if (rc > out_end - out_ptr)
1358 /* Backward compatibility with core_uses_pid:
1360 * If core_pattern does not include a %p (as is the default)
1361 * and core_uses_pid is set, then .%pid will be appended to
1364 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1365 rc = snprintf(out_ptr, out_end - out_ptr,
1366 ".%d", current->tgid);
1367 if (rc > out_end - out_ptr)
1375 static void zap_threads (struct mm_struct *mm)
1377 struct task_struct *g, *p;
1378 struct task_struct *tsk = current;
1379 struct completion *vfork_done = tsk->vfork_done;
1383 * Make sure nobody is waiting for us to release the VM,
1384 * otherwise we can deadlock when we wait on each other
1387 tsk->vfork_done = NULL;
1388 complete(vfork_done);
1391 read_lock(&tasklist_lock);
1393 if (mm == p->mm && p != tsk) {
1394 force_sig_specific(SIGKILL, p);
1396 if (unlikely(p->ptrace) &&
1397 unlikely(p->parent->mm == mm))
1400 while_each_thread(g,p);
1402 read_unlock(&tasklist_lock);
1404 if (unlikely(traced)) {
1406 * We are zapping a thread and the thread it ptraces.
1407 * If the tracee went into a ptrace stop for exit tracing,
1408 * we could deadlock since the tracer is waiting for this
1409 * coredump to finish. Detach them so they can both die.
1411 write_lock_irq(&tasklist_lock);
1412 do_each_thread(g,p) {
1413 if (mm == p->mm && p != tsk &&
1414 p->ptrace && p->parent->mm == mm) {
1415 __ptrace_detach(p, 0);
1417 } while_each_thread(g,p);
1418 write_unlock_irq(&tasklist_lock);
1422 static void coredump_wait(struct mm_struct *mm)
1424 DECLARE_COMPLETION(startup_done);
1427 mm->core_startup_done = &startup_done;
1430 core_waiters = mm->core_waiters;
1431 up_write(&mm->mmap_sem);
1434 wait_for_completion(&startup_done);
1435 BUG_ON(mm->core_waiters);
1438 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1440 char corename[CORENAME_MAX_SIZE + 1];
1441 struct mm_struct *mm = current->mm;
1442 struct linux_binfmt * binfmt;
1443 struct inode * inode;
1446 int fsuid = current->fsuid;
1449 binfmt = current->binfmt;
1450 if (!binfmt || !binfmt->core_dump)
1452 down_write(&mm->mmap_sem);
1453 if (!mm->dumpable) {
1454 up_write(&mm->mmap_sem);
1459 * We cannot trust fsuid as being the "true" uid of the
1460 * process nor do we know its entire history. We only know it
1461 * was tainted so we dump it as root in mode 2.
1463 if (mm->dumpable == 2) { /* Setuid core dump mode */
1464 flag = O_EXCL; /* Stop rewrite attacks */
1465 current->fsuid = 0; /* Dump root private */
1470 spin_lock_irq(¤t->sighand->siglock);
1471 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
1472 current->signal->flags = SIGNAL_GROUP_EXIT;
1473 current->signal->group_exit_code = exit_code;
1474 current->signal->group_stop_count = 0;
1477 spin_unlock_irq(¤t->sighand->siglock);
1479 up_write(&mm->mmap_sem);
1483 init_completion(&mm->core_done);
1487 * Clear any false indication of pending signals that might
1488 * be seen by the filesystem code called to write the core file.
1490 clear_thread_flag(TIF_SIGPENDING);
1492 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1496 * lock_kernel() because format_corename() is controlled by sysctl, which
1497 * uses lock_kernel()
1500 format_corename(corename, core_pattern, signr);
1502 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1505 inode = file->f_dentry->d_inode;
1506 if (inode->i_nlink > 1)
1507 goto close_fail; /* multiple links - don't dump */
1508 if (d_unhashed(file->f_dentry))
1511 if (!S_ISREG(inode->i_mode))
1515 if (!file->f_op->write)
1517 if (do_truncate(file->f_dentry, 0, 0, file) != 0)
1520 retval = binfmt->core_dump(signr, regs, file);
1523 current->signal->group_exit_code |= 0x80;
1525 filp_close(file, NULL);
1527 current->fsuid = fsuid;
1528 complete_all(&mm->core_done);