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/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/module.h>
42 #include <linux/namei.h>
43 #include <linux/proc_fs.h>
44 #include <linux/ptrace.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/rmap.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgalloc.h>
52 #include <asm/mmu_context.h>
55 #include <linux/kmod.h>
59 char core_pattern[65] = "core";
60 /* The maximal length of core_pattern is also specified in sysctl.c */
62 static struct linux_binfmt *formats;
63 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
65 int register_binfmt(struct linux_binfmt * fmt)
67 struct linux_binfmt ** tmp = &formats;
73 write_lock(&binfmt_lock);
76 write_unlock(&binfmt_lock);
83 write_unlock(&binfmt_lock);
87 EXPORT_SYMBOL(register_binfmt);
89 int unregister_binfmt(struct linux_binfmt * fmt)
91 struct linux_binfmt ** tmp = &formats;
93 write_lock(&binfmt_lock);
97 write_unlock(&binfmt_lock);
102 write_unlock(&binfmt_lock);
106 EXPORT_SYMBOL(unregister_binfmt);
108 static inline void put_binfmt(struct linux_binfmt * fmt)
110 module_put(fmt->module);
114 * Note that a shared library must be both readable and executable due to
117 * Also note that we take the address to load from from the file itself.
119 asmlinkage long sys_uselib(const char __user * library)
125 nd.intent.open.flags = FMODE_READ;
126 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
131 if (!S_ISREG(nd.dentry->d_inode->i_mode))
134 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
138 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
139 error = PTR_ERR(file);
145 struct linux_binfmt * fmt;
147 read_lock(&binfmt_lock);
148 for (fmt = formats ; fmt ; fmt = fmt->next) {
149 if (!fmt->load_shlib)
151 if (!try_module_get(fmt->module))
153 read_unlock(&binfmt_lock);
154 error = fmt->load_shlib(file);
155 read_lock(&binfmt_lock);
157 if (error != -ENOEXEC)
160 read_unlock(&binfmt_lock);
171 * count() counts the number of strings in array ARGV.
173 static int count(char __user * __user * argv, int max)
181 if (get_user(p, argv))
194 * 'copy_strings()' copies argument/environment strings from user
195 * memory to free pages in kernel mem. These are in a format ready
196 * to be put directly into the top of new user memory.
198 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
200 struct page *kmapped_page = NULL;
209 if (get_user(str, argv+argc) ||
210 !(len = strnlen_user(str, bprm->p))) {
221 /* XXX: add architecture specific overflow check here. */
226 int offset, bytes_to_copy;
229 offset = pos % PAGE_SIZE;
231 page = bprm->page[i];
234 page = alloc_page(GFP_HIGHUSER);
235 bprm->page[i] = page;
243 if (page != kmapped_page) {
245 kunmap(kmapped_page);
247 kaddr = kmap(kmapped_page);
250 memset(kaddr, 0, offset);
251 bytes_to_copy = PAGE_SIZE - offset;
252 if (bytes_to_copy > len) {
255 memset(kaddr+offset+len, 0,
256 PAGE_SIZE-offset-len);
258 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
264 pos += bytes_to_copy;
265 str += bytes_to_copy;
266 len -= bytes_to_copy;
272 kunmap(kmapped_page);
277 * Like copy_strings, but get argv and its values from kernel memory.
279 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
282 mm_segment_t oldfs = get_fs();
284 r = copy_strings(argc, (char __user * __user *)argv, bprm);
289 EXPORT_SYMBOL(copy_strings_kernel);
293 * This routine is used to map in a page into an address space: needed by
294 * execve() for the initial stack and environment pages.
296 * tsk->mmap_sem is held for writing.
298 void put_dirty_page(struct task_struct *tsk, struct page *page,
299 unsigned long address, pgprot_t prot)
304 struct pte_chain *pte_chain;
306 if (page_count(page) != 1)
307 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n",
310 pgd = pgd_offset(tsk->mm, address);
311 pte_chain = pte_chain_alloc(GFP_KERNEL);
314 spin_lock(&tsk->mm->page_table_lock);
315 pmd = pmd_alloc(tsk->mm, pgd, address);
318 pte = pte_alloc_map(tsk->mm, pmd, address);
321 if (!pte_none(*pte)) {
325 lru_cache_add_active(page);
326 flush_dcache_page(page);
327 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, prot))));
328 pte_chain = page_add_rmap(page, pte, pte_chain);
331 spin_unlock(&tsk->mm->page_table_lock);
333 /* no need for flush_tlb */
334 pte_chain_free(pte_chain);
337 spin_unlock(&tsk->mm->page_table_lock);
340 force_sig(SIGKILL, tsk);
341 pte_chain_free(pte_chain);
345 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
347 unsigned long stack_base;
348 struct vm_area_struct *mpnt;
349 struct mm_struct *mm = current->mm;
353 #ifdef CONFIG_STACK_GROWSUP
354 /* Move the argument and environment strings to the bottom of the
360 /* Start by shifting all the pages down */
362 for (j = 0; j < MAX_ARG_PAGES; j++) {
363 struct page *page = bprm->page[j];
366 bprm->page[i++] = page;
369 /* Now move them within their pages */
370 offset = bprm->p % PAGE_SIZE;
371 to = kmap(bprm->page[0]);
372 for (j = 1; j < i; j++) {
373 memmove(to, to + offset, PAGE_SIZE - offset);
374 from = kmap(bprm->page[j]);
375 memcpy(to + PAGE_SIZE - offset, from, offset);
376 kunmap(bprm->page[j - 1]);
379 memmove(to, to + offset, PAGE_SIZE - offset);
380 kunmap(bprm->page[j - 1]);
382 /* Adjust bprm->p to point to the end of the strings. */
383 bprm->p = PAGE_SIZE * i - offset;
385 /* Limit stack size to 1GB */
386 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
387 if (stack_base > (1 << 30))
388 stack_base = 1 << 30;
389 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
391 mm->arg_start = stack_base;
392 arg_size = i << PAGE_SHIFT;
394 /* zero pages that were copied above */
395 while (i < MAX_ARG_PAGES)
396 bprm->page[i++] = NULL;
398 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
399 mm->arg_start = bprm->p + stack_base;
400 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
403 bprm->p += stack_base;
405 bprm->loader += stack_base;
406 bprm->exec += stack_base;
408 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
412 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
413 kmem_cache_free(vm_area_cachep, mpnt);
417 down_write(&mm->mmap_sem);
420 #ifdef CONFIG_STACK_GROWSUP
421 mpnt->vm_start = stack_base;
422 mpnt->vm_end = PAGE_MASK &
423 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
425 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
426 mpnt->vm_end = STACK_TOP;
428 /* Adjust stack execute permissions; explicitly enable
429 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
430 * and leave alone (arch default) otherwise. */
431 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
432 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
433 else if (executable_stack == EXSTACK_DISABLE_X)
434 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
436 mpnt->vm_flags = VM_STACK_FLAGS;
437 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
440 mpnt->vm_file = NULL;
441 INIT_LIST_HEAD(&mpnt->shared);
442 mpnt->vm_private_data = (void *) 0;
443 insert_vm_struct(mm, mpnt);
444 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
447 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
448 struct page *page = bprm->page[i];
450 bprm->page[i] = NULL;
451 put_dirty_page(current, page, stack_base,
454 stack_base += PAGE_SIZE;
456 up_write(&mm->mmap_sem);
461 EXPORT_SYMBOL(setup_arg_pages);
463 #define free_arg_pages(bprm) do { } while (0)
467 static inline void free_arg_pages(struct linux_binprm *bprm)
471 for (i = 0; i < MAX_ARG_PAGES; i++) {
473 __free_page(bprm->page[i]);
474 bprm->page[i] = NULL;
478 #endif /* CONFIG_MMU */
480 struct file *open_exec(const char *name)
486 nd.intent.open.flags = FMODE_READ;
487 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
491 struct inode *inode = nd.dentry->d_inode;
492 file = ERR_PTR(-EACCES);
493 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
494 S_ISREG(inode->i_mode)) {
495 int err = permission(inode, MAY_EXEC, &nd);
496 if (!err && !(inode->i_mode & 0111))
500 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
502 err = deny_write_access(file);
517 EXPORT_SYMBOL(open_exec);
519 int kernel_read(struct file *file, unsigned long offset,
520 char *addr, unsigned long count)
528 /* The cast to a user pointer is valid due to the set_fs() */
529 result = vfs_read(file, (void __user *)addr, count, &pos);
534 EXPORT_SYMBOL(kernel_read);
536 static int exec_mmap(struct mm_struct *mm)
538 struct task_struct *tsk;
539 struct mm_struct * old_mm, *active_mm;
541 /* Add it to the list of mm's */
542 spin_lock(&mmlist_lock);
543 list_add(&mm->mmlist, &init_mm.mmlist);
545 spin_unlock(&mmlist_lock);
547 /* Notify parent that we're no longer interested in the old VM */
549 old_mm = current->mm;
550 mm_release(tsk, old_mm);
553 active_mm = tsk->active_mm;
556 activate_mm(active_mm, mm);
559 if (active_mm != old_mm) BUG();
568 * This function makes sure the current process has its own signal table,
569 * so that flush_signal_handlers can later reset the handlers without
570 * disturbing other processes. (Other processes might share the signal
571 * table via the CLONE_SIGHAND option to clone().)
573 static inline int de_thread(struct task_struct *tsk)
575 struct signal_struct *newsig, *oldsig = tsk->signal;
576 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
577 spinlock_t *lock = &oldsighand->siglock;
581 * If we don't share sighandlers, then we aren't sharing anything
582 * and we can just re-use it all.
584 if (atomic_read(&oldsighand->count) <= 1)
587 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
591 spin_lock_init(&newsighand->siglock);
592 atomic_set(&newsighand->count, 1);
593 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
596 * See if we need to allocate a new signal structure
599 if (atomic_read(&oldsig->count) > 1) {
600 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
602 kmem_cache_free(sighand_cachep, newsighand);
605 atomic_set(&newsig->count, 1);
606 newsig->group_exit = 0;
607 newsig->group_exit_code = 0;
608 newsig->group_exit_task = NULL;
609 newsig->group_stop_count = 0;
610 newsig->curr_target = NULL;
611 init_sigpending(&newsig->shared_pending);
612 INIT_LIST_HEAD(&newsig->posix_timers);
614 newsig->tty = oldsig->tty;
615 newsig->pgrp = oldsig->pgrp;
616 newsig->session = oldsig->session;
617 newsig->leader = oldsig->leader;
618 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
621 if (thread_group_empty(current))
622 goto no_thread_group;
625 * Kill all other threads in the thread group.
626 * We must hold tasklist_lock to call zap_other_threads.
628 read_lock(&tasklist_lock);
630 if (oldsig->group_exit) {
632 * Another group action in progress, just
633 * return so that the signal is processed.
635 spin_unlock_irq(lock);
636 read_unlock(&tasklist_lock);
637 kmem_cache_free(sighand_cachep, newsighand);
639 kmem_cache_free(signal_cachep, newsig);
642 oldsig->group_exit = 1;
643 zap_other_threads(current);
644 read_unlock(&tasklist_lock);
647 * Account for the thread group leader hanging around:
650 if (current->pid == current->tgid)
652 while (atomic_read(&oldsig->count) > count) {
653 oldsig->group_exit_task = current;
654 oldsig->notify_count = count;
655 __set_current_state(TASK_UNINTERRUPTIBLE);
656 spin_unlock_irq(lock);
660 spin_unlock_irq(lock);
663 * At this point all other threads have exited, all we have to
664 * do is to wait for the thread group leader to become inactive,
665 * and to assume its PID:
667 if (current->pid != current->tgid) {
668 struct task_struct *leader = current->group_leader, *parent;
669 struct dentry *proc_dentry1, *proc_dentry2;
670 unsigned long state, ptrace;
673 * Wait for the thread group leader to be a zombie.
674 * It should already be zombie at this point, most
677 while (leader->state != TASK_ZOMBIE)
680 spin_lock(&leader->proc_lock);
681 spin_lock(¤t->proc_lock);
682 proc_dentry1 = proc_pid_unhash(current);
683 proc_dentry2 = proc_pid_unhash(leader);
684 write_lock_irq(&tasklist_lock);
686 if (leader->tgid != current->tgid)
688 if (current->pid == current->tgid)
691 * An exec() starts a new thread group with the
692 * TGID of the previous thread group. Rehash the
693 * two threads with a switched PID, and release
694 * the former thread group leader:
696 ptrace = leader->ptrace;
697 parent = leader->parent;
699 ptrace_unlink(current);
700 ptrace_unlink(leader);
701 remove_parent(current);
702 remove_parent(leader);
704 switch_exec_pids(leader, current);
706 current->parent = current->real_parent = leader->real_parent;
707 leader->parent = leader->real_parent = child_reaper;
708 current->group_leader = current;
709 leader->group_leader = leader;
711 add_parent(current, current->parent);
712 add_parent(leader, leader->parent);
714 current->ptrace = ptrace;
715 __ptrace_link(current, parent);
718 list_del(¤t->tasks);
719 list_add_tail(¤t->tasks, &init_task.tasks);
720 current->exit_signal = SIGCHLD;
721 state = leader->state;
723 write_unlock_irq(&tasklist_lock);
724 spin_unlock(&leader->proc_lock);
725 spin_unlock(¤t->proc_lock);
726 proc_pid_flush(proc_dentry1);
727 proc_pid_flush(proc_dentry2);
729 if (state != TASK_ZOMBIE)
731 release_task(leader);
736 write_lock_irq(&tasklist_lock);
737 spin_lock(&oldsighand->siglock);
738 spin_lock(&newsighand->siglock);
740 if (current == oldsig->curr_target)
741 oldsig->curr_target = next_thread(current);
743 current->signal = newsig;
744 current->sighand = newsighand;
745 init_sigpending(¤t->pending);
748 spin_unlock(&newsighand->siglock);
749 spin_unlock(&oldsighand->siglock);
750 write_unlock_irq(&tasklist_lock);
752 if (newsig && atomic_dec_and_test(&oldsig->count))
753 kmem_cache_free(signal_cachep, oldsig);
755 if (atomic_dec_and_test(&oldsighand->count))
756 kmem_cache_free(sighand_cachep, oldsighand);
758 if (!thread_group_empty(current))
760 if (current->tgid != current->pid)
766 * These functions flushes out all traces of the currently running executable
767 * so that a new one can be started
770 static inline void flush_old_files(struct files_struct * files)
774 spin_lock(&files->file_lock);
776 unsigned long set, i;
780 if (i >= files->max_fds || i >= files->max_fdset)
782 set = files->close_on_exec->fds_bits[j];
785 files->close_on_exec->fds_bits[j] = 0;
786 spin_unlock(&files->file_lock);
787 for ( ; set ; i++,set >>= 1) {
792 spin_lock(&files->file_lock);
795 spin_unlock(&files->file_lock);
798 int flush_old_exec(struct linux_binprm * bprm)
802 struct files_struct *files;
805 * Make sure we have a private signal table and that
806 * we are unassociated from the previous thread group.
808 retval = de_thread(current);
813 * Make sure we have private file handles. Ask the
814 * fork helper to do the work for us and the exit
815 * helper to do the cleanup of the old one.
817 files = current->files; /* refcounted so safe to hold */
818 retval = unshare_files();
822 * Release all of the old mmap stuff
824 retval = exec_mmap(bprm->mm);
828 bprm->mm = NULL; /* We're using it now */
830 /* This is the point of no return */
832 put_files_struct(files);
834 current->sas_ss_sp = current->sas_ss_size = 0;
836 if (current->euid == current->uid && current->egid == current->gid)
837 current->mm->dumpable = 1;
838 name = bprm->filename;
839 for (i=0; (ch = *(name++)) != '\0';) {
844 current->comm[i++] = ch;
846 current->comm[i] = '\0';
850 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
851 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
852 current->mm->dumpable = 0;
854 /* An exec changes our domain. We are no longer part of the thread
857 current->self_exec_id++;
859 flush_signal_handlers(current, 0);
860 flush_old_files(current->files);
865 put_files_struct(current->files);
866 current->files = files;
871 EXPORT_SYMBOL(flush_old_exec);
874 * Fill the binprm structure from the inode.
875 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
877 int prepare_binprm(struct linux_binprm *bprm)
880 struct inode * inode = bprm->file->f_dentry->d_inode;
883 mode = inode->i_mode;
885 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
886 * vfs_permission lets a non-executable through
888 if (!(mode & 0111)) /* with at least _one_ execute bit set */
890 if (bprm->file->f_op == NULL)
893 bprm->e_uid = current->euid;
894 bprm->e_gid = current->egid;
896 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
899 bprm->e_uid = inode->i_uid;
903 * If setgid is set but no group execute bit then this
904 * is a candidate for mandatory locking, not a setgid
907 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
908 bprm->e_gid = inode->i_gid;
911 /* fill in binprm security blob */
912 retval = security_bprm_set(bprm);
916 memset(bprm->buf,0,BINPRM_BUF_SIZE);
917 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
920 EXPORT_SYMBOL(prepare_binprm);
922 static inline int unsafe_exec(struct task_struct *p)
925 if (p->ptrace & PT_PTRACED) {
926 if (p->ptrace & PT_PTRACE_CAP)
927 unsafe |= LSM_UNSAFE_PTRACE_CAP;
929 unsafe |= LSM_UNSAFE_PTRACE;
931 if (atomic_read(&p->fs->count) > 1 ||
932 atomic_read(&p->files->count) > 1 ||
933 atomic_read(&p->sighand->count) > 1)
934 unsafe |= LSM_UNSAFE_SHARE;
939 void compute_creds(struct linux_binprm *bprm)
943 unsafe = unsafe_exec(current);
944 security_bprm_apply_creds(bprm, unsafe);
945 task_unlock(current);
948 EXPORT_SYMBOL(compute_creds);
950 void remove_arg_zero(struct linux_binprm *bprm)
953 unsigned long offset;
957 offset = bprm->p % PAGE_SIZE;
960 while (bprm->p++, *(kaddr+offset++)) {
961 if (offset != PAGE_SIZE)
964 kunmap_atomic(kaddr, KM_USER0);
966 page = bprm->page[bprm->p/PAGE_SIZE];
967 kaddr = kmap_atomic(page, KM_USER0);
969 kunmap_atomic(kaddr, KM_USER0);
974 EXPORT_SYMBOL(remove_arg_zero);
977 * cycle the list of binary formats handler, until one recognizes the image
979 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
982 struct linux_binfmt *fmt;
984 /* handle /sbin/loader.. */
986 struct exec * eh = (struct exec *) bprm->buf;
988 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
989 (eh->fh.f_flags & 0x3000) == 0x3000)
992 unsigned long loader;
994 allow_write_access(bprm->file);
998 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1000 file = open_exec("/sbin/loader");
1001 retval = PTR_ERR(file);
1005 /* Remember if the application is TASO. */
1006 bprm->sh_bang = eh->ah.entry < 0x100000000;
1009 bprm->loader = loader;
1010 retval = prepare_binprm(bprm);
1013 /* should call search_binary_handler recursively here,
1014 but it does not matter */
1018 retval = security_bprm_check(bprm);
1022 /* kernel module loader fixup */
1023 /* so we don't try to load run modprobe in kernel space. */
1025 for (try=0; try<2; try++) {
1026 read_lock(&binfmt_lock);
1027 for (fmt = formats ; fmt ; fmt = fmt->next) {
1028 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1031 if (!try_module_get(fmt->module))
1033 read_unlock(&binfmt_lock);
1034 retval = fn(bprm, regs);
1037 allow_write_access(bprm->file);
1041 current->did_exec = 1;
1044 read_lock(&binfmt_lock);
1046 if (retval != -ENOEXEC || bprm->mm == NULL)
1049 read_unlock(&binfmt_lock);
1053 read_unlock(&binfmt_lock);
1054 if (retval != -ENOEXEC || bprm->mm == NULL) {
1058 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1059 if (printable(bprm->buf[0]) &&
1060 printable(bprm->buf[1]) &&
1061 printable(bprm->buf[2]) &&
1062 printable(bprm->buf[3]))
1063 break; /* -ENOEXEC */
1064 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1071 EXPORT_SYMBOL(search_binary_handler);
1074 * sys_execve() executes a new program.
1076 int do_execve(char * filename,
1077 char __user *__user *argv,
1078 char __user *__user *envp,
1079 struct pt_regs * regs)
1081 struct linux_binprm bprm;
1086 sched_balance_exec();
1088 file = open_exec(filename);
1090 retval = PTR_ERR(file);
1094 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1095 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1098 bprm.filename = filename;
1099 bprm.interp = filename;
1103 bprm.security = NULL;
1104 bprm.mm = mm_alloc();
1109 retval = init_new_context(current, bprm.mm);
1113 bprm.argc = count(argv, bprm.p / sizeof(void *));
1114 if ((retval = bprm.argc) < 0)
1117 bprm.envc = count(envp, bprm.p / sizeof(void *));
1118 if ((retval = bprm.envc) < 0)
1121 retval = security_bprm_alloc(&bprm);
1125 retval = prepare_binprm(&bprm);
1129 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1134 retval = copy_strings(bprm.envc, envp, &bprm);
1138 retval = copy_strings(bprm.argc, argv, &bprm);
1142 retval = search_binary_handler(&bprm,regs);
1144 free_arg_pages(&bprm);
1146 /* execve success */
1147 security_bprm_free(&bprm);
1152 /* Something went wrong, return the inode and free the argument pages*/
1153 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1154 struct page * page = bprm.page[i];
1160 security_bprm_free(&bprm);
1168 allow_write_access(bprm.file);
1174 EXPORT_SYMBOL(do_execve);
1176 int set_binfmt(struct linux_binfmt *new)
1178 struct linux_binfmt *old = current->binfmt;
1181 if (!try_module_get(new->module))
1184 current->binfmt = new;
1186 module_put(old->module);
1190 EXPORT_SYMBOL(set_binfmt);
1192 #define CORENAME_MAX_SIZE 64
1194 /* format_corename will inspect the pattern parameter, and output a
1195 * name into corename, which must have space for at least
1196 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1198 void format_corename(char *corename, const char *pattern, long signr)
1200 const char *pat_ptr = pattern;
1201 char *out_ptr = corename;
1202 char *const out_end = corename + CORENAME_MAX_SIZE;
1204 int pid_in_pattern = 0;
1206 /* Repeat as long as we have more pattern to process and more output
1209 if (*pat_ptr != '%') {
1210 if (out_ptr == out_end)
1212 *out_ptr++ = *pat_ptr++;
1214 switch (*++pat_ptr) {
1217 /* Double percent, output one percent */
1219 if (out_ptr == out_end)
1226 rc = snprintf(out_ptr, out_end - out_ptr,
1227 "%d", current->tgid);
1228 if (rc > out_end - out_ptr)
1234 rc = snprintf(out_ptr, out_end - out_ptr,
1235 "%d", current->uid);
1236 if (rc > out_end - out_ptr)
1242 rc = snprintf(out_ptr, out_end - out_ptr,
1243 "%d", current->gid);
1244 if (rc > out_end - out_ptr)
1248 /* signal that caused the coredump */
1250 rc = snprintf(out_ptr, out_end - out_ptr,
1252 if (rc > out_end - out_ptr)
1256 /* UNIX time of coredump */
1259 do_gettimeofday(&tv);
1260 rc = snprintf(out_ptr, out_end - out_ptr,
1262 if (rc > out_end - out_ptr)
1269 down_read(&uts_sem);
1270 rc = snprintf(out_ptr, out_end - out_ptr,
1271 "%s", system_utsname.nodename);
1273 if (rc > out_end - out_ptr)
1279 rc = snprintf(out_ptr, out_end - out_ptr,
1280 "%s", current->comm);
1281 if (rc > out_end - out_ptr)
1291 /* Backward compatibility with core_uses_pid:
1293 * If core_pattern does not include a %p (as is the default)
1294 * and core_uses_pid is set, then .%pid will be appended to
1297 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1298 rc = snprintf(out_ptr, out_end - out_ptr,
1299 ".%d", current->tgid);
1300 if (rc > out_end - out_ptr)
1308 static void zap_threads (struct mm_struct *mm)
1310 struct task_struct *g, *p;
1311 struct task_struct *tsk = current;
1312 struct completion *vfork_done = tsk->vfork_done;
1315 * Make sure nobody is waiting for us to release the VM,
1316 * otherwise we can deadlock when we wait on each other
1319 tsk->vfork_done = NULL;
1320 complete(vfork_done);
1323 read_lock(&tasklist_lock);
1325 if (mm == p->mm && p != tsk) {
1326 force_sig_specific(SIGKILL, p);
1329 while_each_thread(g,p);
1331 read_unlock(&tasklist_lock);
1334 static void coredump_wait(struct mm_struct *mm)
1336 DECLARE_COMPLETION(startup_done);
1338 mm->core_waiters++; /* let other threads block */
1339 mm->core_startup_done = &startup_done;
1341 /* give other threads a chance to run: */
1345 if (--mm->core_waiters) {
1346 up_write(&mm->mmap_sem);
1347 wait_for_completion(&startup_done);
1349 up_write(&mm->mmap_sem);
1350 BUG_ON(mm->core_waiters);
1353 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1355 char corename[CORENAME_MAX_SIZE + 1];
1356 struct mm_struct *mm = current->mm;
1357 struct linux_binfmt * binfmt;
1358 struct inode * inode;
1363 binfmt = current->binfmt;
1364 if (!binfmt || !binfmt->core_dump)
1366 down_write(&mm->mmap_sem);
1367 if (!mm->dumpable) {
1368 up_write(&mm->mmap_sem);
1372 init_completion(&mm->core_done);
1373 current->signal->group_exit = 1;
1374 current->signal->group_exit_code = exit_code;
1377 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1380 format_corename(corename, core_pattern, signr);
1381 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1384 inode = file->f_dentry->d_inode;
1385 if (inode->i_nlink > 1)
1386 goto close_fail; /* multiple links - don't dump */
1387 if (d_unhashed(file->f_dentry))
1390 if (!S_ISREG(inode->i_mode))
1394 if (!file->f_op->write)
1396 if (do_truncate(file->f_dentry, 0) != 0)
1399 retval = binfmt->core_dump(signr, regs, file);
1401 current->signal->group_exit_code |= 0x80;
1403 filp_close(file, NULL);
1405 complete_all(&mm->core_done);