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 nd.intent.open.flags = FMODE_READ;
130 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
135 if (!S_ISREG(nd.dentry->d_inode->i_mode))
138 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
142 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
143 error = PTR_ERR(file);
149 struct linux_binfmt * fmt;
151 read_lock(&binfmt_lock);
152 for (fmt = formats ; fmt ; fmt = fmt->next) {
153 if (!fmt->load_shlib)
155 if (!try_module_get(fmt->module))
157 read_unlock(&binfmt_lock);
158 error = fmt->load_shlib(file);
159 read_lock(&binfmt_lock);
161 if (error != -ENOEXEC)
164 read_unlock(&binfmt_lock);
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 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
425 kmem_cache_free(vm_area_cachep, mpnt);
429 memset(mpnt, 0, sizeof(*mpnt));
431 down_write(&mm->mmap_sem);
434 #ifdef CONFIG_STACK_GROWSUP
435 mpnt->vm_start = stack_base;
436 mpnt->vm_end = stack_base + arg_size;
438 mpnt->vm_end = stack_top;
439 mpnt->vm_start = mpnt->vm_end - arg_size;
441 /* Adjust stack execute permissions; explicitly enable
442 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
443 * and leave alone (arch default) otherwise. */
444 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
445 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
446 else if (executable_stack == EXSTACK_DISABLE_X)
447 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
449 mpnt->vm_flags = VM_STACK_FLAGS;
450 mpnt->vm_flags |= mm->def_flags;
451 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
452 if ((ret = insert_vm_struct(mm, mpnt))) {
453 up_write(&mm->mmap_sem);
454 kmem_cache_free(vm_area_cachep, mpnt);
457 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
460 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
461 struct page *page = bprm->page[i];
463 bprm->page[i] = NULL;
464 install_arg_page(mpnt, page, stack_base);
466 stack_base += PAGE_SIZE;
468 up_write(&mm->mmap_sem);
473 EXPORT_SYMBOL(setup_arg_pages);
475 #define free_arg_pages(bprm) do { } while (0)
479 static inline void free_arg_pages(struct linux_binprm *bprm)
483 for (i = 0; i < MAX_ARG_PAGES; i++) {
485 __free_page(bprm->page[i]);
486 bprm->page[i] = NULL;
490 #endif /* CONFIG_MMU */
492 struct file *open_exec(const char *name)
498 nd.intent.open.flags = FMODE_READ;
499 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
503 struct inode *inode = nd.dentry->d_inode;
504 file = ERR_PTR(-EACCES);
505 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
506 S_ISREG(inode->i_mode)) {
507 int err = permission(inode, MAY_EXEC, &nd);
508 if (!err && !(inode->i_mode & 0111))
512 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
514 err = deny_write_access(file);
529 EXPORT_SYMBOL(open_exec);
531 int kernel_read(struct file *file, unsigned long offset,
532 char *addr, unsigned long count)
540 /* The cast to a user pointer is valid due to the set_fs() */
541 result = vfs_read(file, (void __user *)addr, count, &pos);
546 EXPORT_SYMBOL(kernel_read);
548 static int exec_mmap(struct mm_struct *mm)
550 struct task_struct *tsk;
551 struct mm_struct * old_mm, *active_mm;
553 /* Notify parent that we're no longer interested in the old VM */
555 old_mm = current->mm;
556 mm_release(tsk, old_mm);
560 * Make sure that if there is a core dump in progress
561 * for the old mm, we get out and die instead of going
562 * through with the exec. We must hold mmap_sem around
563 * checking core_waiters and changing tsk->mm. The
564 * core-inducing thread will increment core_waiters for
565 * each thread whose ->mm == old_mm.
567 down_read(&old_mm->mmap_sem);
568 if (unlikely(old_mm->core_waiters)) {
569 up_read(&old_mm->mmap_sem);
574 active_mm = tsk->active_mm;
577 activate_mm(active_mm, mm);
579 arch_pick_mmap_layout(mm);
581 up_read(&old_mm->mmap_sem);
582 if (active_mm != old_mm) BUG();
591 * This function makes sure the current process has its own signal table,
592 * so that flush_signal_handlers can later reset the handlers without
593 * disturbing other processes. (Other processes might share the signal
594 * table via the CLONE_SIGHAND option to clone().)
596 static inline int de_thread(struct task_struct *tsk)
598 struct signal_struct *sig = tsk->signal;
599 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
600 spinlock_t *lock = &oldsighand->siglock;
604 * If we don't share sighandlers, then we aren't sharing anything
605 * and we can just re-use it all.
607 if (atomic_read(&oldsighand->count) <= 1) {
608 BUG_ON(atomic_read(&sig->count) != 1);
613 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
617 if (thread_group_empty(current))
618 goto no_thread_group;
621 * Kill all other threads in the thread group.
622 * We must hold tasklist_lock to call zap_other_threads.
624 read_lock(&tasklist_lock);
626 if (sig->flags & SIGNAL_GROUP_EXIT) {
628 * Another group action in progress, just
629 * return so that the signal is processed.
631 spin_unlock_irq(lock);
632 read_unlock(&tasklist_lock);
633 kmem_cache_free(sighand_cachep, newsighand);
636 zap_other_threads(current);
637 read_unlock(&tasklist_lock);
640 * Account for the thread group leader hanging around:
643 if (thread_group_leader(current))
647 * The SIGALRM timer survives the exec, but needs to point
648 * at us as the new group leader now. We have a race with
649 * a timer firing now getting the old leader, so we need to
650 * synchronize with any firing (by calling del_timer_sync)
651 * before we can safely let the old group leader die.
653 sig->real_timer.data = (unsigned long)current;
654 if (del_timer_sync(&sig->real_timer))
655 add_timer(&sig->real_timer);
657 while (atomic_read(&sig->count) > count) {
658 sig->group_exit_task = current;
659 sig->notify_count = count;
660 __set_current_state(TASK_UNINTERRUPTIBLE);
661 spin_unlock_irq(lock);
665 sig->group_exit_task = NULL;
666 sig->notify_count = 0;
667 sig->real_timer.data = (unsigned long)current;
668 spin_unlock_irq(lock);
671 * At this point all other threads have exited, all we have to
672 * do is to wait for the thread group leader to become inactive,
673 * and to assume its PID:
675 if (!thread_group_leader(current)) {
676 struct task_struct *leader = current->group_leader, *parent;
677 struct dentry *proc_dentry1, *proc_dentry2;
678 unsigned long exit_state, ptrace;
681 * Wait for the thread group leader to be a zombie.
682 * It should already be zombie at this point, most
685 while (leader->exit_state != EXIT_ZOMBIE)
688 spin_lock(&leader->proc_lock);
689 spin_lock(¤t->proc_lock);
690 proc_dentry1 = proc_pid_unhash(current);
691 proc_dentry2 = proc_pid_unhash(leader);
692 write_lock_irq(&tasklist_lock);
694 BUG_ON(leader->tgid != current->tgid);
695 BUG_ON(current->pid == current->tgid);
697 * An exec() starts a new thread group with the
698 * TGID of the previous thread group. Rehash the
699 * two threads with a switched PID, and release
700 * the former thread group leader:
702 ptrace = leader->ptrace;
703 parent = leader->parent;
704 if (unlikely(ptrace) && unlikely(parent == current)) {
706 * Joker was ptracing his own group leader,
707 * and now he wants to be his own parent!
708 * We can't have that.
713 ptrace_unlink(current);
714 ptrace_unlink(leader);
715 remove_parent(current);
716 remove_parent(leader);
718 switch_exec_pids(leader, current);
720 current->parent = current->real_parent = leader->real_parent;
721 leader->parent = leader->real_parent = child_reaper;
722 current->group_leader = current;
723 leader->group_leader = leader;
725 add_parent(current, current->parent);
726 add_parent(leader, leader->parent);
728 current->ptrace = ptrace;
729 __ptrace_link(current, parent);
732 list_del(¤t->tasks);
733 list_add_tail(¤t->tasks, &init_task.tasks);
734 current->exit_signal = SIGCHLD;
735 exit_state = leader->exit_state;
737 write_unlock_irq(&tasklist_lock);
738 spin_unlock(&leader->proc_lock);
739 spin_unlock(¤t->proc_lock);
740 proc_pid_flush(proc_dentry1);
741 proc_pid_flush(proc_dentry2);
743 BUG_ON(exit_state != EXIT_ZOMBIE);
744 release_task(leader);
748 * Now there are really no other threads at all,
749 * so it's safe to stop telling them to kill themselves.
754 BUG_ON(atomic_read(&sig->count) != 1);
757 if (atomic_read(&oldsighand->count) == 1) {
759 * Now that we nuked the rest of the thread group,
760 * it turns out we are not sharing sighand any more either.
761 * So we can just keep it.
763 kmem_cache_free(sighand_cachep, newsighand);
766 * Move our state over to newsighand and switch it in.
768 spin_lock_init(&newsighand->siglock);
769 atomic_set(&newsighand->count, 1);
770 memcpy(newsighand->action, oldsighand->action,
771 sizeof(newsighand->action));
773 write_lock_irq(&tasklist_lock);
774 spin_lock(&oldsighand->siglock);
775 spin_lock(&newsighand->siglock);
777 current->sighand = newsighand;
780 spin_unlock(&newsighand->siglock);
781 spin_unlock(&oldsighand->siglock);
782 write_unlock_irq(&tasklist_lock);
784 if (atomic_dec_and_test(&oldsighand->count))
785 kmem_cache_free(sighand_cachep, oldsighand);
788 BUG_ON(!thread_group_empty(current));
789 BUG_ON(!thread_group_leader(current));
794 * These functions flushes out all traces of the currently running executable
795 * so that a new one can be started
798 static inline void flush_old_files(struct files_struct * files)
803 spin_lock(&files->file_lock);
805 unsigned long set, i;
809 fdt = files_fdtable(files);
810 if (i >= fdt->max_fds || i >= fdt->max_fdset)
812 set = fdt->close_on_exec->fds_bits[j];
815 fdt->close_on_exec->fds_bits[j] = 0;
816 spin_unlock(&files->file_lock);
817 for ( ; set ; i++,set >>= 1) {
822 spin_lock(&files->file_lock);
825 spin_unlock(&files->file_lock);
828 void get_task_comm(char *buf, struct task_struct *tsk)
830 /* buf must be at least sizeof(tsk->comm) in size */
832 strncpy(buf, tsk->comm, sizeof(tsk->comm));
836 void set_task_comm(struct task_struct *tsk, char *buf)
839 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
843 int flush_old_exec(struct linux_binprm * bprm)
847 struct files_struct *files;
848 char tcomm[sizeof(current->comm)];
851 * Make sure we have a private signal table and that
852 * we are unassociated from the previous thread group.
854 retval = de_thread(current);
859 * Make sure we have private file handles. Ask the
860 * fork helper to do the work for us and the exit
861 * helper to do the cleanup of the old one.
863 files = current->files; /* refcounted so safe to hold */
864 retval = unshare_files();
868 * Release all of the old mmap stuff
870 retval = exec_mmap(bprm->mm);
874 bprm->mm = NULL; /* We're using it now */
876 /* This is the point of no return */
878 put_files_struct(files);
880 current->sas_ss_sp = current->sas_ss_size = 0;
882 if (current->euid == current->uid && current->egid == current->gid)
883 current->mm->dumpable = 1;
885 current->mm->dumpable = suid_dumpable;
887 name = bprm->filename;
889 /* Copies the binary name from after last slash */
890 for (i=0; (ch = *(name++)) != '\0';) {
892 i = 0; /* overwrite what we wrote */
894 if (i < (sizeof(tcomm) - 1))
898 set_task_comm(current, tcomm);
900 current->flags &= ~PF_RANDOMIZE;
903 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
904 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
905 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
907 current->mm->dumpable = suid_dumpable;
910 /* An exec changes our domain. We are no longer part of the thread
913 current->self_exec_id++;
915 flush_signal_handlers(current, 0);
916 flush_old_files(current->files);
921 put_files_struct(current->files);
922 current->files = files;
927 EXPORT_SYMBOL(flush_old_exec);
930 * Fill the binprm structure from the inode.
931 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
933 int prepare_binprm(struct linux_binprm *bprm)
936 struct inode * inode = bprm->file->f_dentry->d_inode;
939 mode = inode->i_mode;
941 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
942 * generic_permission lets a non-executable through
944 if (!(mode & 0111)) /* with at least _one_ execute bit set */
946 if (bprm->file->f_op == NULL)
949 bprm->e_uid = current->euid;
950 bprm->e_gid = current->egid;
952 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
954 if (mode & S_ISUID) {
955 current->personality &= ~PER_CLEAR_ON_SETID;
956 bprm->e_uid = inode->i_uid;
961 * If setgid is set but no group execute bit then this
962 * is a candidate for mandatory locking, not a setgid
965 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
966 current->personality &= ~PER_CLEAR_ON_SETID;
967 bprm->e_gid = inode->i_gid;
971 /* fill in binprm security blob */
972 retval = security_bprm_set(bprm);
976 memset(bprm->buf,0,BINPRM_BUF_SIZE);
977 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
980 EXPORT_SYMBOL(prepare_binprm);
982 static inline int unsafe_exec(struct task_struct *p)
985 if (p->ptrace & PT_PTRACED) {
986 if (p->ptrace & PT_PTRACE_CAP)
987 unsafe |= LSM_UNSAFE_PTRACE_CAP;
989 unsafe |= LSM_UNSAFE_PTRACE;
991 if (atomic_read(&p->fs->count) > 1 ||
992 atomic_read(&p->files->count) > 1 ||
993 atomic_read(&p->sighand->count) > 1)
994 unsafe |= LSM_UNSAFE_SHARE;
999 void compute_creds(struct linux_binprm *bprm)
1003 if (bprm->e_uid != current->uid)
1008 unsafe = unsafe_exec(current);
1009 security_bprm_apply_creds(bprm, unsafe);
1010 task_unlock(current);
1011 security_bprm_post_apply_creds(bprm);
1014 EXPORT_SYMBOL(compute_creds);
1016 void remove_arg_zero(struct linux_binprm *bprm)
1019 unsigned long offset;
1023 offset = bprm->p % PAGE_SIZE;
1026 while (bprm->p++, *(kaddr+offset++)) {
1027 if (offset != PAGE_SIZE)
1030 kunmap_atomic(kaddr, KM_USER0);
1032 page = bprm->page[bprm->p/PAGE_SIZE];
1033 kaddr = kmap_atomic(page, KM_USER0);
1035 kunmap_atomic(kaddr, KM_USER0);
1040 EXPORT_SYMBOL(remove_arg_zero);
1043 * cycle the list of binary formats handler, until one recognizes the image
1045 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1048 struct linux_binfmt *fmt;
1050 /* handle /sbin/loader.. */
1052 struct exec * eh = (struct exec *) bprm->buf;
1054 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1055 (eh->fh.f_flags & 0x3000) == 0x3000)
1058 unsigned long loader;
1060 allow_write_access(bprm->file);
1064 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1066 file = open_exec("/sbin/loader");
1067 retval = PTR_ERR(file);
1071 /* Remember if the application is TASO. */
1072 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1075 bprm->loader = loader;
1076 retval = prepare_binprm(bprm);
1079 /* should call search_binary_handler recursively here,
1080 but it does not matter */
1084 retval = security_bprm_check(bprm);
1088 /* kernel module loader fixup */
1089 /* so we don't try to load run modprobe in kernel space. */
1092 for (try=0; try<2; try++) {
1093 read_lock(&binfmt_lock);
1094 for (fmt = formats ; fmt ; fmt = fmt->next) {
1095 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1098 if (!try_module_get(fmt->module))
1100 read_unlock(&binfmt_lock);
1101 retval = fn(bprm, regs);
1104 allow_write_access(bprm->file);
1108 current->did_exec = 1;
1111 read_lock(&binfmt_lock);
1113 if (retval != -ENOEXEC || bprm->mm == NULL)
1116 read_unlock(&binfmt_lock);
1120 read_unlock(&binfmt_lock);
1121 if (retval != -ENOEXEC || bprm->mm == NULL) {
1125 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1126 if (printable(bprm->buf[0]) &&
1127 printable(bprm->buf[1]) &&
1128 printable(bprm->buf[2]) &&
1129 printable(bprm->buf[3]))
1130 break; /* -ENOEXEC */
1131 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1138 EXPORT_SYMBOL(search_binary_handler);
1141 * sys_execve() executes a new program.
1143 int do_execve(char * filename,
1144 char __user *__user *argv,
1145 char __user *__user *envp,
1146 struct pt_regs * regs)
1148 struct linux_binprm *bprm;
1154 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1157 memset(bprm, 0, sizeof(*bprm));
1159 file = open_exec(filename);
1160 retval = PTR_ERR(file);
1166 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1169 bprm->filename = filename;
1170 bprm->interp = filename;
1171 bprm->mm = mm_alloc();
1176 retval = init_new_context(current, bprm->mm);
1180 bprm->argc = count(argv, bprm->p / sizeof(void *));
1181 if ((retval = bprm->argc) < 0)
1184 bprm->envc = count(envp, bprm->p / sizeof(void *));
1185 if ((retval = bprm->envc) < 0)
1188 retval = security_bprm_alloc(bprm);
1192 retval = prepare_binprm(bprm);
1196 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1200 bprm->exec = bprm->p;
1201 retval = copy_strings(bprm->envc, envp, bprm);
1205 retval = copy_strings(bprm->argc, argv, bprm);
1209 retval = search_binary_handler(bprm,regs);
1211 free_arg_pages(bprm);
1213 /* execve success */
1214 security_bprm_free(bprm);
1215 acct_update_integrals(current);
1216 update_mem_hiwater(current);
1222 /* Something went wrong, return the inode and free the argument pages*/
1223 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1224 struct page * page = bprm->page[i];
1230 security_bprm_free(bprm);
1238 allow_write_access(bprm->file);
1249 int set_binfmt(struct linux_binfmt *new)
1251 struct linux_binfmt *old = current->binfmt;
1254 if (!try_module_get(new->module))
1257 current->binfmt = new;
1259 module_put(old->module);
1263 EXPORT_SYMBOL(set_binfmt);
1265 #define CORENAME_MAX_SIZE 64
1267 /* format_corename will inspect the pattern parameter, and output a
1268 * name into corename, which must have space for at least
1269 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1271 static void format_corename(char *corename, const char *pattern, long signr)
1273 const char *pat_ptr = pattern;
1274 char *out_ptr = corename;
1275 char *const out_end = corename + CORENAME_MAX_SIZE;
1277 int pid_in_pattern = 0;
1279 /* Repeat as long as we have more pattern to process and more output
1282 if (*pat_ptr != '%') {
1283 if (out_ptr == out_end)
1285 *out_ptr++ = *pat_ptr++;
1287 switch (*++pat_ptr) {
1290 /* Double percent, output one percent */
1292 if (out_ptr == out_end)
1299 rc = snprintf(out_ptr, out_end - out_ptr,
1300 "%d", current->tgid);
1301 if (rc > out_end - out_ptr)
1307 rc = snprintf(out_ptr, out_end - out_ptr,
1308 "%d", current->uid);
1309 if (rc > out_end - out_ptr)
1315 rc = snprintf(out_ptr, out_end - out_ptr,
1316 "%d", current->gid);
1317 if (rc > out_end - out_ptr)
1321 /* signal that caused the coredump */
1323 rc = snprintf(out_ptr, out_end - out_ptr,
1325 if (rc > out_end - out_ptr)
1329 /* UNIX time of coredump */
1332 do_gettimeofday(&tv);
1333 rc = snprintf(out_ptr, out_end - out_ptr,
1335 if (rc > out_end - out_ptr)
1342 down_read(&uts_sem);
1343 rc = snprintf(out_ptr, out_end - out_ptr,
1344 "%s", system_utsname.nodename);
1346 if (rc > out_end - out_ptr)
1352 rc = snprintf(out_ptr, out_end - out_ptr,
1353 "%s", current->comm);
1354 if (rc > out_end - out_ptr)
1364 /* Backward compatibility with core_uses_pid:
1366 * If core_pattern does not include a %p (as is the default)
1367 * and core_uses_pid is set, then .%pid will be appended to
1370 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1371 rc = snprintf(out_ptr, out_end - out_ptr,
1372 ".%d", current->tgid);
1373 if (rc > out_end - out_ptr)
1381 static void zap_threads (struct mm_struct *mm)
1383 struct task_struct *g, *p;
1384 struct task_struct *tsk = current;
1385 struct completion *vfork_done = tsk->vfork_done;
1389 * Make sure nobody is waiting for us to release the VM,
1390 * otherwise we can deadlock when we wait on each other
1393 tsk->vfork_done = NULL;
1394 complete(vfork_done);
1397 read_lock(&tasklist_lock);
1399 if (mm == p->mm && p != tsk) {
1400 force_sig_specific(SIGKILL, p);
1402 if (unlikely(p->ptrace) &&
1403 unlikely(p->parent->mm == mm))
1406 while_each_thread(g,p);
1408 read_unlock(&tasklist_lock);
1410 if (unlikely(traced)) {
1412 * We are zapping a thread and the thread it ptraces.
1413 * If the tracee went into a ptrace stop for exit tracing,
1414 * we could deadlock since the tracer is waiting for this
1415 * coredump to finish. Detach them so they can both die.
1417 write_lock_irq(&tasklist_lock);
1418 do_each_thread(g,p) {
1419 if (mm == p->mm && p != tsk &&
1420 p->ptrace && p->parent->mm == mm) {
1423 } while_each_thread(g,p);
1424 write_unlock_irq(&tasklist_lock);
1428 static void coredump_wait(struct mm_struct *mm)
1430 DECLARE_COMPLETION(startup_done);
1432 mm->core_waiters++; /* let other threads block */
1433 mm->core_startup_done = &startup_done;
1435 /* give other threads a chance to run: */
1439 if (--mm->core_waiters) {
1440 up_write(&mm->mmap_sem);
1441 wait_for_completion(&startup_done);
1443 up_write(&mm->mmap_sem);
1444 BUG_ON(mm->core_waiters);
1447 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1449 char corename[CORENAME_MAX_SIZE + 1];
1450 struct mm_struct *mm = current->mm;
1451 struct linux_binfmt * binfmt;
1452 struct inode * inode;
1455 int fsuid = current->fsuid;
1458 binfmt = current->binfmt;
1459 if (!binfmt || !binfmt->core_dump)
1461 down_write(&mm->mmap_sem);
1462 if (!mm->dumpable) {
1463 up_write(&mm->mmap_sem);
1468 * We cannot trust fsuid as being the "true" uid of the
1469 * process nor do we know its entire history. We only know it
1470 * was tainted so we dump it as root in mode 2.
1472 if (mm->dumpable == 2) { /* Setuid core dump mode */
1473 flag = O_EXCL; /* Stop rewrite attacks */
1474 current->fsuid = 0; /* Dump root private */
1477 init_completion(&mm->core_done);
1478 spin_lock_irq(¤t->sighand->siglock);
1479 current->signal->flags = SIGNAL_GROUP_EXIT;
1480 current->signal->group_exit_code = exit_code;
1481 spin_unlock_irq(¤t->sighand->siglock);
1485 * Clear any false indication of pending signals that might
1486 * be seen by the filesystem code called to write the core file.
1488 current->signal->group_stop_count = 0;
1489 clear_thread_flag(TIF_SIGPENDING);
1491 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1495 * lock_kernel() because format_corename() is controlled by sysctl, which
1496 * uses lock_kernel()
1499 format_corename(corename, core_pattern, signr);
1501 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1504 inode = file->f_dentry->d_inode;
1505 if (inode->i_nlink > 1)
1506 goto close_fail; /* multiple links - don't dump */
1507 if (d_unhashed(file->f_dentry))
1510 if (!S_ISREG(inode->i_mode))
1514 if (!file->f_op->write)
1516 if (do_truncate(file->f_dentry, 0) != 0)
1519 retval = binfmt->core_dump(signr, regs, file);
1522 current->signal->group_exit_code |= 0x80;
1524 filp_close(file, NULL);
1526 current->fsuid = fsuid;
1527 complete_all(&mm->core_done);