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)
802 spin_lock(&files->file_lock);
804 unsigned long set, i;
808 if (i >= files->max_fds || i >= files->max_fdset)
810 set = files->close_on_exec->fds_bits[j];
813 files->close_on_exec->fds_bits[j] = 0;
814 spin_unlock(&files->file_lock);
815 for ( ; set ; i++,set >>= 1) {
820 spin_lock(&files->file_lock);
823 spin_unlock(&files->file_lock);
826 void get_task_comm(char *buf, struct task_struct *tsk)
828 /* buf must be at least sizeof(tsk->comm) in size */
830 strncpy(buf, tsk->comm, sizeof(tsk->comm));
834 void set_task_comm(struct task_struct *tsk, char *buf)
837 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
841 int flush_old_exec(struct linux_binprm * bprm)
845 struct files_struct *files;
846 char tcomm[sizeof(current->comm)];
849 * Make sure we have a private signal table and that
850 * we are unassociated from the previous thread group.
852 retval = de_thread(current);
857 * Make sure we have private file handles. Ask the
858 * fork helper to do the work for us and the exit
859 * helper to do the cleanup of the old one.
861 files = current->files; /* refcounted so safe to hold */
862 retval = unshare_files();
866 * Release all of the old mmap stuff
868 retval = exec_mmap(bprm->mm);
872 bprm->mm = NULL; /* We're using it now */
874 /* This is the point of no return */
876 put_files_struct(files);
878 current->sas_ss_sp = current->sas_ss_size = 0;
880 if (current->euid == current->uid && current->egid == current->gid)
881 current->mm->dumpable = 1;
883 current->mm->dumpable = suid_dumpable;
885 name = bprm->filename;
887 /* Copies the binary name from after last slash */
888 for (i=0; (ch = *(name++)) != '\0';) {
890 i = 0; /* overwrite what we wrote */
892 if (i < (sizeof(tcomm) - 1))
896 set_task_comm(current, tcomm);
898 current->flags &= ~PF_RANDOMIZE;
901 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
902 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
903 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
905 current->mm->dumpable = suid_dumpable;
908 /* An exec changes our domain. We are no longer part of the thread
911 current->self_exec_id++;
913 flush_signal_handlers(current, 0);
914 flush_old_files(current->files);
919 put_files_struct(current->files);
920 current->files = files;
925 EXPORT_SYMBOL(flush_old_exec);
928 * Fill the binprm structure from the inode.
929 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
931 int prepare_binprm(struct linux_binprm *bprm)
934 struct inode * inode = bprm->file->f_dentry->d_inode;
937 mode = inode->i_mode;
939 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
940 * generic_permission lets a non-executable through
942 if (!(mode & 0111)) /* with at least _one_ execute bit set */
944 if (bprm->file->f_op == NULL)
947 bprm->e_uid = current->euid;
948 bprm->e_gid = current->egid;
950 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
952 if (mode & S_ISUID) {
953 current->personality &= ~PER_CLEAR_ON_SETID;
954 bprm->e_uid = inode->i_uid;
959 * If setgid is set but no group execute bit then this
960 * is a candidate for mandatory locking, not a setgid
963 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
964 current->personality &= ~PER_CLEAR_ON_SETID;
965 bprm->e_gid = inode->i_gid;
969 /* fill in binprm security blob */
970 retval = security_bprm_set(bprm);
974 memset(bprm->buf,0,BINPRM_BUF_SIZE);
975 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
978 EXPORT_SYMBOL(prepare_binprm);
980 static inline int unsafe_exec(struct task_struct *p)
983 if (p->ptrace & PT_PTRACED) {
984 if (p->ptrace & PT_PTRACE_CAP)
985 unsafe |= LSM_UNSAFE_PTRACE_CAP;
987 unsafe |= LSM_UNSAFE_PTRACE;
989 if (atomic_read(&p->fs->count) > 1 ||
990 atomic_read(&p->files->count) > 1 ||
991 atomic_read(&p->sighand->count) > 1)
992 unsafe |= LSM_UNSAFE_SHARE;
997 void compute_creds(struct linux_binprm *bprm)
1001 if (bprm->e_uid != current->uid)
1006 unsafe = unsafe_exec(current);
1007 security_bprm_apply_creds(bprm, unsafe);
1008 task_unlock(current);
1009 security_bprm_post_apply_creds(bprm);
1012 EXPORT_SYMBOL(compute_creds);
1014 void remove_arg_zero(struct linux_binprm *bprm)
1017 unsigned long offset;
1021 offset = bprm->p % PAGE_SIZE;
1024 while (bprm->p++, *(kaddr+offset++)) {
1025 if (offset != PAGE_SIZE)
1028 kunmap_atomic(kaddr, KM_USER0);
1030 page = bprm->page[bprm->p/PAGE_SIZE];
1031 kaddr = kmap_atomic(page, KM_USER0);
1033 kunmap_atomic(kaddr, KM_USER0);
1038 EXPORT_SYMBOL(remove_arg_zero);
1041 * cycle the list of binary formats handler, until one recognizes the image
1043 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1046 struct linux_binfmt *fmt;
1048 /* handle /sbin/loader.. */
1050 struct exec * eh = (struct exec *) bprm->buf;
1052 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1053 (eh->fh.f_flags & 0x3000) == 0x3000)
1056 unsigned long loader;
1058 allow_write_access(bprm->file);
1062 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1064 file = open_exec("/sbin/loader");
1065 retval = PTR_ERR(file);
1069 /* Remember if the application is TASO. */
1070 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1073 bprm->loader = loader;
1074 retval = prepare_binprm(bprm);
1077 /* should call search_binary_handler recursively here,
1078 but it does not matter */
1082 retval = security_bprm_check(bprm);
1086 /* kernel module loader fixup */
1087 /* so we don't try to load run modprobe in kernel space. */
1090 for (try=0; try<2; try++) {
1091 read_lock(&binfmt_lock);
1092 for (fmt = formats ; fmt ; fmt = fmt->next) {
1093 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1096 if (!try_module_get(fmt->module))
1098 read_unlock(&binfmt_lock);
1099 retval = fn(bprm, regs);
1102 allow_write_access(bprm->file);
1106 current->did_exec = 1;
1109 read_lock(&binfmt_lock);
1111 if (retval != -ENOEXEC || bprm->mm == NULL)
1114 read_unlock(&binfmt_lock);
1118 read_unlock(&binfmt_lock);
1119 if (retval != -ENOEXEC || bprm->mm == NULL) {
1123 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1124 if (printable(bprm->buf[0]) &&
1125 printable(bprm->buf[1]) &&
1126 printable(bprm->buf[2]) &&
1127 printable(bprm->buf[3]))
1128 break; /* -ENOEXEC */
1129 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1136 EXPORT_SYMBOL(search_binary_handler);
1139 * sys_execve() executes a new program.
1141 int do_execve(char * filename,
1142 char __user *__user *argv,
1143 char __user *__user *envp,
1144 struct pt_regs * regs)
1146 struct linux_binprm *bprm;
1152 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1155 memset(bprm, 0, sizeof(*bprm));
1157 file = open_exec(filename);
1158 retval = PTR_ERR(file);
1164 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1167 bprm->filename = filename;
1168 bprm->interp = filename;
1169 bprm->mm = mm_alloc();
1174 retval = init_new_context(current, bprm->mm);
1178 bprm->argc = count(argv, bprm->p / sizeof(void *));
1179 if ((retval = bprm->argc) < 0)
1182 bprm->envc = count(envp, bprm->p / sizeof(void *));
1183 if ((retval = bprm->envc) < 0)
1186 retval = security_bprm_alloc(bprm);
1190 retval = prepare_binprm(bprm);
1194 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1198 bprm->exec = bprm->p;
1199 retval = copy_strings(bprm->envc, envp, bprm);
1203 retval = copy_strings(bprm->argc, argv, bprm);
1207 retval = search_binary_handler(bprm,regs);
1209 free_arg_pages(bprm);
1211 /* execve success */
1212 security_bprm_free(bprm);
1213 acct_update_integrals(current);
1214 update_mem_hiwater(current);
1220 /* Something went wrong, return the inode and free the argument pages*/
1221 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1222 struct page * page = bprm->page[i];
1228 security_bprm_free(bprm);
1236 allow_write_access(bprm->file);
1247 int set_binfmt(struct linux_binfmt *new)
1249 struct linux_binfmt *old = current->binfmt;
1252 if (!try_module_get(new->module))
1255 current->binfmt = new;
1257 module_put(old->module);
1261 EXPORT_SYMBOL(set_binfmt);
1263 #define CORENAME_MAX_SIZE 64
1265 /* format_corename will inspect the pattern parameter, and output a
1266 * name into corename, which must have space for at least
1267 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1269 static void format_corename(char *corename, const char *pattern, long signr)
1271 const char *pat_ptr = pattern;
1272 char *out_ptr = corename;
1273 char *const out_end = corename + CORENAME_MAX_SIZE;
1275 int pid_in_pattern = 0;
1277 /* Repeat as long as we have more pattern to process and more output
1280 if (*pat_ptr != '%') {
1281 if (out_ptr == out_end)
1283 *out_ptr++ = *pat_ptr++;
1285 switch (*++pat_ptr) {
1288 /* Double percent, output one percent */
1290 if (out_ptr == out_end)
1297 rc = snprintf(out_ptr, out_end - out_ptr,
1298 "%d", current->tgid);
1299 if (rc > out_end - out_ptr)
1305 rc = snprintf(out_ptr, out_end - out_ptr,
1306 "%d", current->uid);
1307 if (rc > out_end - out_ptr)
1313 rc = snprintf(out_ptr, out_end - out_ptr,
1314 "%d", current->gid);
1315 if (rc > out_end - out_ptr)
1319 /* signal that caused the coredump */
1321 rc = snprintf(out_ptr, out_end - out_ptr,
1323 if (rc > out_end - out_ptr)
1327 /* UNIX time of coredump */
1330 do_gettimeofday(&tv);
1331 rc = snprintf(out_ptr, out_end - out_ptr,
1333 if (rc > out_end - out_ptr)
1340 down_read(&uts_sem);
1341 rc = snprintf(out_ptr, out_end - out_ptr,
1342 "%s", system_utsname.nodename);
1344 if (rc > out_end - out_ptr)
1350 rc = snprintf(out_ptr, out_end - out_ptr,
1351 "%s", current->comm);
1352 if (rc > out_end - out_ptr)
1362 /* Backward compatibility with core_uses_pid:
1364 * If core_pattern does not include a %p (as is the default)
1365 * and core_uses_pid is set, then .%pid will be appended to
1368 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1369 rc = snprintf(out_ptr, out_end - out_ptr,
1370 ".%d", current->tgid);
1371 if (rc > out_end - out_ptr)
1379 static void zap_threads (struct mm_struct *mm)
1381 struct task_struct *g, *p;
1382 struct task_struct *tsk = current;
1383 struct completion *vfork_done = tsk->vfork_done;
1387 * Make sure nobody is waiting for us to release the VM,
1388 * otherwise we can deadlock when we wait on each other
1391 tsk->vfork_done = NULL;
1392 complete(vfork_done);
1395 read_lock(&tasklist_lock);
1397 if (mm == p->mm && p != tsk) {
1398 force_sig_specific(SIGKILL, p);
1400 if (unlikely(p->ptrace) &&
1401 unlikely(p->parent->mm == mm))
1404 while_each_thread(g,p);
1406 read_unlock(&tasklist_lock);
1408 if (unlikely(traced)) {
1410 * We are zapping a thread and the thread it ptraces.
1411 * If the tracee went into a ptrace stop for exit tracing,
1412 * we could deadlock since the tracer is waiting for this
1413 * coredump to finish. Detach them so they can both die.
1415 write_lock_irq(&tasklist_lock);
1416 do_each_thread(g,p) {
1417 if (mm == p->mm && p != tsk &&
1418 p->ptrace && p->parent->mm == mm) {
1421 } while_each_thread(g,p);
1422 write_unlock_irq(&tasklist_lock);
1426 static void coredump_wait(struct mm_struct *mm)
1428 DECLARE_COMPLETION(startup_done);
1430 mm->core_waiters++; /* let other threads block */
1431 mm->core_startup_done = &startup_done;
1433 /* give other threads a chance to run: */
1437 if (--mm->core_waiters) {
1438 up_write(&mm->mmap_sem);
1439 wait_for_completion(&startup_done);
1441 up_write(&mm->mmap_sem);
1442 BUG_ON(mm->core_waiters);
1445 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1447 char corename[CORENAME_MAX_SIZE + 1];
1448 struct mm_struct *mm = current->mm;
1449 struct linux_binfmt * binfmt;
1450 struct inode * inode;
1453 int fsuid = current->fsuid;
1456 binfmt = current->binfmt;
1457 if (!binfmt || !binfmt->core_dump)
1459 down_write(&mm->mmap_sem);
1460 if (!mm->dumpable) {
1461 up_write(&mm->mmap_sem);
1466 * We cannot trust fsuid as being the "true" uid of the
1467 * process nor do we know its entire history. We only know it
1468 * was tainted so we dump it as root in mode 2.
1470 if (mm->dumpable == 2) { /* Setuid core dump mode */
1471 flag = O_EXCL; /* Stop rewrite attacks */
1472 current->fsuid = 0; /* Dump root private */
1475 init_completion(&mm->core_done);
1476 spin_lock_irq(¤t->sighand->siglock);
1477 current->signal->flags = SIGNAL_GROUP_EXIT;
1478 current->signal->group_exit_code = exit_code;
1479 spin_unlock_irq(¤t->sighand->siglock);
1483 * Clear any false indication of pending signals that might
1484 * be seen by the filesystem code called to write the core file.
1486 current->signal->group_stop_count = 0;
1487 clear_thread_flag(TIF_SIGPENDING);
1489 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1493 * lock_kernel() because format_corename() is controlled by sysctl, which
1494 * uses lock_kernel()
1497 format_corename(corename, core_pattern, signr);
1499 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1502 inode = file->f_dentry->d_inode;
1503 if (inode->i_nlink > 1)
1504 goto close_fail; /* multiple links - don't dump */
1505 if (d_unhashed(file->f_dentry))
1508 if (!S_ISREG(inode->i_mode))
1512 if (!file->f_op->write)
1514 if (do_truncate(file->f_dentry, 0) != 0)
1517 retval = binfmt->core_dump(signr, regs, file);
1520 current->signal->group_exit_code |= 0x80;
1522 filp_close(file, NULL);
1524 current->fsuid = fsuid;
1525 complete_all(&mm->core_done);