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>
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
56 #include <linux/kmod.h>
60 char core_pattern[65] = "core";
61 int suid_dumpable = 0;
63 EXPORT_SYMBOL(suid_dumpable);
64 /* The maximal length of core_pattern is also specified in sysctl.c */
66 static struct linux_binfmt *formats;
67 static DEFINE_RWLOCK(binfmt_lock);
69 int register_binfmt(struct linux_binfmt * fmt)
71 struct linux_binfmt ** tmp = &formats;
77 write_lock(&binfmt_lock);
80 write_unlock(&binfmt_lock);
87 write_unlock(&binfmt_lock);
91 EXPORT_SYMBOL(register_binfmt);
93 int unregister_binfmt(struct linux_binfmt * fmt)
95 struct linux_binfmt ** tmp = &formats;
97 write_lock(&binfmt_lock);
101 write_unlock(&binfmt_lock);
106 write_unlock(&binfmt_lock);
110 EXPORT_SYMBOL(unregister_binfmt);
112 static inline void put_binfmt(struct linux_binfmt * fmt)
114 module_put(fmt->module);
118 * Note that a shared library must be both readable and executable due to
121 * Also note that we take the address to load from from the file itself.
123 asmlinkage long sys_uselib(const char __user * library)
129 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
134 if (!S_ISREG(nd.dentry->d_inode->i_mode))
137 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
141 file = nameidata_to_filp(&nd, O_RDONLY);
142 error = PTR_ERR(file);
148 struct linux_binfmt * fmt;
150 read_lock(&binfmt_lock);
151 for (fmt = formats ; fmt ; fmt = fmt->next) {
152 if (!fmt->load_shlib)
154 if (!try_module_get(fmt->module))
156 read_unlock(&binfmt_lock);
157 error = fmt->load_shlib(file);
158 read_lock(&binfmt_lock);
160 if (error != -ENOEXEC)
163 read_unlock(&binfmt_lock);
169 release_open_intent(&nd);
175 * count() counts the number of strings in array ARGV.
177 static int count(char __user * __user * argv, int max)
185 if (get_user(p, argv))
199 * 'copy_strings()' copies argument/environment strings from user
200 * memory to free pages in kernel mem. These are in a format ready
201 * to be put directly into the top of new user memory.
203 static int copy_strings(int argc, char __user * __user * argv,
204 struct linux_binprm *bprm)
206 struct page *kmapped_page = NULL;
215 if (get_user(str, argv+argc) ||
216 !(len = strnlen_user(str, bprm->p))) {
227 /* XXX: add architecture specific overflow check here. */
232 int offset, bytes_to_copy;
235 offset = pos % PAGE_SIZE;
237 page = bprm->page[i];
240 page = alloc_page(GFP_HIGHUSER);
241 bprm->page[i] = page;
249 if (page != kmapped_page) {
251 kunmap(kmapped_page);
253 kaddr = kmap(kmapped_page);
256 memset(kaddr, 0, offset);
257 bytes_to_copy = PAGE_SIZE - offset;
258 if (bytes_to_copy > len) {
261 memset(kaddr+offset+len, 0,
262 PAGE_SIZE-offset-len);
264 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
270 pos += bytes_to_copy;
271 str += bytes_to_copy;
272 len -= bytes_to_copy;
278 kunmap(kmapped_page);
283 * Like copy_strings, but get argv and its values from kernel memory.
285 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
288 mm_segment_t oldfs = get_fs();
290 r = copy_strings(argc, (char __user * __user *)argv, bprm);
295 EXPORT_SYMBOL(copy_strings_kernel);
299 * This routine is used to map in a page into an address space: needed by
300 * execve() for the initial stack and environment pages.
302 * vma->vm_mm->mmap_sem is held for writing.
304 void install_arg_page(struct vm_area_struct *vma,
305 struct page *page, unsigned long address)
307 struct mm_struct *mm = vma->vm_mm;
313 if (unlikely(anon_vma_prepare(vma)))
316 flush_dcache_page(page);
317 pgd = pgd_offset(mm, address);
319 spin_lock(&mm->page_table_lock);
320 pud = pud_alloc(mm, pgd, address);
323 pmd = pmd_alloc(mm, pud, address);
326 pte = pte_alloc_map(mm, pmd, address);
329 if (!pte_none(*pte)) {
333 inc_mm_counter(mm, rss);
334 lru_cache_add_active(page);
335 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
336 page, vma->vm_page_prot))));
337 page_add_anon_rmap(page, vma, address);
339 spin_unlock(&mm->page_table_lock);
341 /* no need for flush_tlb */
344 spin_unlock(&mm->page_table_lock);
347 force_sig(SIGKILL, current);
350 #define EXTRA_STACK_VM_PAGES 20 /* random */
352 int setup_arg_pages(struct linux_binprm *bprm,
353 unsigned long stack_top,
354 int executable_stack)
356 unsigned long stack_base;
357 struct vm_area_struct *mpnt;
358 struct mm_struct *mm = current->mm;
362 #ifdef CONFIG_STACK_GROWSUP
363 /* Move the argument and environment strings to the bottom of the
369 /* Start by shifting all the pages down */
371 for (j = 0; j < MAX_ARG_PAGES; j++) {
372 struct page *page = bprm->page[j];
375 bprm->page[i++] = page;
378 /* Now move them within their pages */
379 offset = bprm->p % PAGE_SIZE;
380 to = kmap(bprm->page[0]);
381 for (j = 1; j < i; j++) {
382 memmove(to, to + offset, PAGE_SIZE - offset);
383 from = kmap(bprm->page[j]);
384 memcpy(to + PAGE_SIZE - offset, from, offset);
385 kunmap(bprm->page[j - 1]);
388 memmove(to, to + offset, PAGE_SIZE - offset);
389 kunmap(bprm->page[j - 1]);
391 /* Limit stack size to 1GB */
392 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
393 if (stack_base > (1 << 30))
394 stack_base = 1 << 30;
395 stack_base = PAGE_ALIGN(stack_top - stack_base);
397 /* Adjust bprm->p to point to the end of the strings. */
398 bprm->p = stack_base + PAGE_SIZE * i - offset;
400 mm->arg_start = stack_base;
401 arg_size = i << PAGE_SHIFT;
403 /* zero pages that were copied above */
404 while (i < MAX_ARG_PAGES)
405 bprm->page[i++] = NULL;
407 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
408 stack_base = PAGE_ALIGN(stack_base);
409 bprm->p += stack_base;
410 mm->arg_start = bprm->p;
411 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
414 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
417 bprm->loader += stack_base;
418 bprm->exec += stack_base;
420 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
424 memset(mpnt, 0, sizeof(*mpnt));
426 down_write(&mm->mmap_sem);
429 #ifdef CONFIG_STACK_GROWSUP
430 mpnt->vm_start = stack_base;
431 mpnt->vm_end = stack_base + arg_size;
433 mpnt->vm_end = stack_top;
434 mpnt->vm_start = mpnt->vm_end - arg_size;
436 /* Adjust stack execute permissions; explicitly enable
437 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
438 * and leave alone (arch default) otherwise. */
439 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
440 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
441 else if (executable_stack == EXSTACK_DISABLE_X)
442 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
444 mpnt->vm_flags = VM_STACK_FLAGS;
445 mpnt->vm_flags |= mm->def_flags;
446 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
447 if ((ret = insert_vm_struct(mm, mpnt))) {
448 up_write(&mm->mmap_sem);
449 kmem_cache_free(vm_area_cachep, mpnt);
452 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
455 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
456 struct page *page = bprm->page[i];
458 bprm->page[i] = NULL;
459 install_arg_page(mpnt, page, stack_base);
461 stack_base += PAGE_SIZE;
463 up_write(&mm->mmap_sem);
468 EXPORT_SYMBOL(setup_arg_pages);
470 #define free_arg_pages(bprm) do { } while (0)
474 static inline void free_arg_pages(struct linux_binprm *bprm)
478 for (i = 0; i < MAX_ARG_PAGES; i++) {
480 __free_page(bprm->page[i]);
481 bprm->page[i] = NULL;
485 #endif /* CONFIG_MMU */
487 struct file *open_exec(const char *name)
493 err = path_lookup_open(name, LOOKUP_FOLLOW, &nd, FMODE_READ);
497 struct inode *inode = nd.dentry->d_inode;
498 file = ERR_PTR(-EACCES);
499 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
500 S_ISREG(inode->i_mode)) {
501 int err = permission(inode, MAY_EXEC, &nd);
502 if (!err && !(inode->i_mode & 0111))
506 file = nameidata_to_filp(&nd, O_RDONLY);
508 err = deny_write_access(file);
518 release_open_intent(&nd);
524 EXPORT_SYMBOL(open_exec);
526 int kernel_read(struct file *file, unsigned long offset,
527 char *addr, unsigned long count)
535 /* The cast to a user pointer is valid due to the set_fs() */
536 result = vfs_read(file, (void __user *)addr, count, &pos);
541 EXPORT_SYMBOL(kernel_read);
543 static int exec_mmap(struct mm_struct *mm)
545 struct task_struct *tsk;
546 struct mm_struct * old_mm, *active_mm;
548 /* Notify parent that we're no longer interested in the old VM */
550 old_mm = current->mm;
551 mm_release(tsk, old_mm);
555 * Make sure that if there is a core dump in progress
556 * for the old mm, we get out and die instead of going
557 * through with the exec. We must hold mmap_sem around
558 * checking core_waiters and changing tsk->mm. The
559 * core-inducing thread will increment core_waiters for
560 * each thread whose ->mm == old_mm.
562 down_read(&old_mm->mmap_sem);
563 if (unlikely(old_mm->core_waiters)) {
564 up_read(&old_mm->mmap_sem);
569 active_mm = tsk->active_mm;
572 activate_mm(active_mm, mm);
574 arch_pick_mmap_layout(mm);
576 up_read(&old_mm->mmap_sem);
577 if (active_mm != old_mm) BUG();
586 * This function makes sure the current process has its own signal table,
587 * so that flush_signal_handlers can later reset the handlers without
588 * disturbing other processes. (Other processes might share the signal
589 * table via the CLONE_SIGHAND option to clone().)
591 static inline int de_thread(struct task_struct *tsk)
593 struct signal_struct *sig = tsk->signal;
594 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
595 spinlock_t *lock = &oldsighand->siglock;
599 * If we don't share sighandlers, then we aren't sharing anything
600 * and we can just re-use it all.
602 if (atomic_read(&oldsighand->count) <= 1) {
603 BUG_ON(atomic_read(&sig->count) != 1);
608 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
612 if (thread_group_empty(current))
613 goto no_thread_group;
616 * Kill all other threads in the thread group.
617 * We must hold tasklist_lock to call zap_other_threads.
619 read_lock(&tasklist_lock);
621 if (sig->flags & SIGNAL_GROUP_EXIT) {
623 * Another group action in progress, just
624 * return so that the signal is processed.
626 spin_unlock_irq(lock);
627 read_unlock(&tasklist_lock);
628 kmem_cache_free(sighand_cachep, newsighand);
631 zap_other_threads(current);
632 read_unlock(&tasklist_lock);
635 * Account for the thread group leader hanging around:
638 if (thread_group_leader(current))
642 * The SIGALRM timer survives the exec, but needs to point
643 * at us as the new group leader now. We have a race with
644 * a timer firing now getting the old leader, so we need to
645 * synchronize with any firing (by calling del_timer_sync)
646 * before we can safely let the old group leader die.
648 sig->real_timer.data = (unsigned long)current;
649 if (del_timer_sync(&sig->real_timer))
650 add_timer(&sig->real_timer);
652 while (atomic_read(&sig->count) > count) {
653 sig->group_exit_task = current;
654 sig->notify_count = count;
655 __set_current_state(TASK_UNINTERRUPTIBLE);
656 spin_unlock_irq(lock);
660 sig->group_exit_task = NULL;
661 sig->notify_count = 0;
662 sig->real_timer.data = (unsigned long)current;
663 spin_unlock_irq(lock);
666 * At this point all other threads have exited, all we have to
667 * do is to wait for the thread group leader to become inactive,
668 * and to assume its PID:
670 if (!thread_group_leader(current)) {
671 struct task_struct *leader = current->group_leader, *parent;
672 struct dentry *proc_dentry1, *proc_dentry2;
673 unsigned long exit_state, ptrace;
676 * Wait for the thread group leader to be a zombie.
677 * It should already be zombie at this point, most
680 while (leader->exit_state != EXIT_ZOMBIE)
683 spin_lock(&leader->proc_lock);
684 spin_lock(¤t->proc_lock);
685 proc_dentry1 = proc_pid_unhash(current);
686 proc_dentry2 = proc_pid_unhash(leader);
687 write_lock_irq(&tasklist_lock);
689 BUG_ON(leader->tgid != current->tgid);
690 BUG_ON(current->pid == current->tgid);
692 * An exec() starts a new thread group with the
693 * TGID of the previous thread group. Rehash the
694 * two threads with a switched PID, and release
695 * the former thread group leader:
697 ptrace = leader->ptrace;
698 parent = leader->parent;
699 if (unlikely(ptrace) && unlikely(parent == current)) {
701 * Joker was ptracing his own group leader,
702 * and now he wants to be his own parent!
703 * We can't have that.
708 ptrace_unlink(current);
709 ptrace_unlink(leader);
710 remove_parent(current);
711 remove_parent(leader);
713 switch_exec_pids(leader, current);
715 current->parent = current->real_parent = leader->real_parent;
716 leader->parent = leader->real_parent = child_reaper;
717 current->group_leader = current;
718 leader->group_leader = leader;
720 add_parent(current, current->parent);
721 add_parent(leader, leader->parent);
723 current->ptrace = ptrace;
724 __ptrace_link(current, parent);
727 list_del(¤t->tasks);
728 list_add_tail(¤t->tasks, &init_task.tasks);
729 current->exit_signal = SIGCHLD;
730 exit_state = leader->exit_state;
732 write_unlock_irq(&tasklist_lock);
733 spin_unlock(&leader->proc_lock);
734 spin_unlock(¤t->proc_lock);
735 proc_pid_flush(proc_dentry1);
736 proc_pid_flush(proc_dentry2);
738 BUG_ON(exit_state != EXIT_ZOMBIE);
739 release_task(leader);
743 * There may be one thread left which is just exiting,
744 * but it's safe to stop telling the group to kill themselves.
749 BUG_ON(atomic_read(&sig->count) != 1);
752 if (atomic_read(&oldsighand->count) == 1) {
754 * Now that we nuked the rest of the thread group,
755 * it turns out we are not sharing sighand any more either.
756 * So we can just keep it.
758 kmem_cache_free(sighand_cachep, newsighand);
761 * Move our state over to newsighand and switch it in.
763 spin_lock_init(&newsighand->siglock);
764 atomic_set(&newsighand->count, 1);
765 memcpy(newsighand->action, oldsighand->action,
766 sizeof(newsighand->action));
768 write_lock_irq(&tasklist_lock);
769 spin_lock(&oldsighand->siglock);
770 spin_lock(&newsighand->siglock);
772 current->sighand = newsighand;
775 spin_unlock(&newsighand->siglock);
776 spin_unlock(&oldsighand->siglock);
777 write_unlock_irq(&tasklist_lock);
779 if (atomic_dec_and_test(&oldsighand->count))
780 kmem_cache_free(sighand_cachep, oldsighand);
783 BUG_ON(!thread_group_leader(current));
788 * These functions flushes out all traces of the currently running executable
789 * so that a new one can be started
792 static inline void flush_old_files(struct files_struct * files)
797 spin_lock(&files->file_lock);
799 unsigned long set, i;
803 fdt = files_fdtable(files);
804 if (i >= fdt->max_fds || i >= fdt->max_fdset)
806 set = fdt->close_on_exec->fds_bits[j];
809 fdt->close_on_exec->fds_bits[j] = 0;
810 spin_unlock(&files->file_lock);
811 for ( ; set ; i++,set >>= 1) {
816 spin_lock(&files->file_lock);
819 spin_unlock(&files->file_lock);
822 void get_task_comm(char *buf, struct task_struct *tsk)
824 /* buf must be at least sizeof(tsk->comm) in size */
826 strncpy(buf, tsk->comm, sizeof(tsk->comm));
830 void set_task_comm(struct task_struct *tsk, char *buf)
833 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
837 int flush_old_exec(struct linux_binprm * bprm)
841 struct files_struct *files;
842 char tcomm[sizeof(current->comm)];
845 * Make sure we have a private signal table and that
846 * we are unassociated from the previous thread group.
848 retval = de_thread(current);
853 * Make sure we have private file handles. Ask the
854 * fork helper to do the work for us and the exit
855 * helper to do the cleanup of the old one.
857 files = current->files; /* refcounted so safe to hold */
858 retval = unshare_files();
862 * Release all of the old mmap stuff
864 retval = exec_mmap(bprm->mm);
868 bprm->mm = NULL; /* We're using it now */
870 /* This is the point of no return */
872 put_files_struct(files);
874 current->sas_ss_sp = current->sas_ss_size = 0;
876 if (current->euid == current->uid && current->egid == current->gid)
877 current->mm->dumpable = 1;
879 current->mm->dumpable = suid_dumpable;
881 name = bprm->filename;
883 /* Copies the binary name from after last slash */
884 for (i=0; (ch = *(name++)) != '\0';) {
886 i = 0; /* overwrite what we wrote */
888 if (i < (sizeof(tcomm) - 1))
892 set_task_comm(current, tcomm);
894 current->flags &= ~PF_RANDOMIZE;
897 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
898 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
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 inline 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;
1105 read_lock(&binfmt_lock);
1107 if (retval != -ENOEXEC || bprm->mm == NULL)
1110 read_unlock(&binfmt_lock);
1114 read_unlock(&binfmt_lock);
1115 if (retval != -ENOEXEC || bprm->mm == NULL) {
1119 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1120 if (printable(bprm->buf[0]) &&
1121 printable(bprm->buf[1]) &&
1122 printable(bprm->buf[2]) &&
1123 printable(bprm->buf[3]))
1124 break; /* -ENOEXEC */
1125 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1132 EXPORT_SYMBOL(search_binary_handler);
1135 * sys_execve() executes a new program.
1137 int do_execve(char * filename,
1138 char __user *__user *argv,
1139 char __user *__user *envp,
1140 struct pt_regs * regs)
1142 struct linux_binprm *bprm;
1148 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1151 memset(bprm, 0, sizeof(*bprm));
1153 file = open_exec(filename);
1154 retval = PTR_ERR(file);
1160 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1163 bprm->filename = filename;
1164 bprm->interp = filename;
1165 bprm->mm = mm_alloc();
1170 retval = init_new_context(current, bprm->mm);
1174 bprm->argc = count(argv, bprm->p / sizeof(void *));
1175 if ((retval = bprm->argc) < 0)
1178 bprm->envc = count(envp, bprm->p / sizeof(void *));
1179 if ((retval = bprm->envc) < 0)
1182 retval = security_bprm_alloc(bprm);
1186 retval = prepare_binprm(bprm);
1190 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1194 bprm->exec = bprm->p;
1195 retval = copy_strings(bprm->envc, envp, bprm);
1199 retval = copy_strings(bprm->argc, argv, bprm);
1203 retval = search_binary_handler(bprm,regs);
1205 free_arg_pages(bprm);
1207 /* execve success */
1208 security_bprm_free(bprm);
1209 acct_update_integrals(current);
1210 update_mem_hiwater(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", system_utsname.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) {
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);
1426 mm->core_waiters++; /* let other threads block */
1427 mm->core_startup_done = &startup_done;
1429 /* give other threads a chance to run: */
1433 if (--mm->core_waiters) {
1434 up_write(&mm->mmap_sem);
1435 wait_for_completion(&startup_done);
1437 up_write(&mm->mmap_sem);
1438 BUG_ON(mm->core_waiters);
1441 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1443 char corename[CORENAME_MAX_SIZE + 1];
1444 struct mm_struct *mm = current->mm;
1445 struct linux_binfmt * binfmt;
1446 struct inode * inode;
1449 int fsuid = current->fsuid;
1452 binfmt = current->binfmt;
1453 if (!binfmt || !binfmt->core_dump)
1455 down_write(&mm->mmap_sem);
1456 if (!mm->dumpable) {
1457 up_write(&mm->mmap_sem);
1462 * We cannot trust fsuid as being the "true" uid of the
1463 * process nor do we know its entire history. We only know it
1464 * was tainted so we dump it as root in mode 2.
1466 if (mm->dumpable == 2) { /* Setuid core dump mode */
1467 flag = O_EXCL; /* Stop rewrite attacks */
1468 current->fsuid = 0; /* Dump root private */
1471 init_completion(&mm->core_done);
1472 spin_lock_irq(¤t->sighand->siglock);
1473 current->signal->flags = SIGNAL_GROUP_EXIT;
1474 current->signal->group_exit_code = exit_code;
1475 spin_unlock_irq(¤t->sighand->siglock);
1479 * Clear any false indication of pending signals that might
1480 * be seen by the filesystem code called to write the core file.
1482 current->signal->group_stop_count = 0;
1483 clear_thread_flag(TIF_SIGPENDING);
1485 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1489 * lock_kernel() because format_corename() is controlled by sysctl, which
1490 * uses lock_kernel()
1493 format_corename(corename, core_pattern, signr);
1495 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1498 inode = file->f_dentry->d_inode;
1499 if (inode->i_nlink > 1)
1500 goto close_fail; /* multiple links - don't dump */
1501 if (d_unhashed(file->f_dentry))
1504 if (!S_ISREG(inode->i_mode))
1508 if (!file->f_op->write)
1510 if (do_truncate(file->f_dentry, 0) != 0)
1513 retval = binfmt->core_dump(signr, regs, file);
1516 current->signal->group_exit_code |= 0x80;
1518 filp_close(file, NULL);
1520 current->fsuid = fsuid;
1521 complete_all(&mm->core_done);