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>
49 #include <linux/ckrm.h>
51 #include <asm/uaccess.h>
52 #include <asm/pgalloc.h>
53 #include <asm/mmu_context.h>
56 #include <linux/kmod.h>
60 char core_pattern[65] = "core";
61 /* The maximal length of core_pattern is also specified in sysctl.c */
63 static struct linux_binfmt *formats;
64 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
66 int register_binfmt(struct linux_binfmt * fmt)
68 struct linux_binfmt ** tmp = &formats;
74 write_lock(&binfmt_lock);
77 write_unlock(&binfmt_lock);
84 write_unlock(&binfmt_lock);
88 EXPORT_SYMBOL(register_binfmt);
90 int unregister_binfmt(struct linux_binfmt * fmt)
92 struct linux_binfmt ** tmp = &formats;
94 write_lock(&binfmt_lock);
98 write_unlock(&binfmt_lock);
103 write_unlock(&binfmt_lock);
107 EXPORT_SYMBOL(unregister_binfmt);
109 static inline void put_binfmt(struct linux_binfmt * fmt)
111 module_put(fmt->module);
115 * Note that a shared library must be both readable and executable due to
118 * Also note that we take the address to load from from the file itself.
120 asmlinkage long sys_uselib(const char __user * library)
126 nd.intent.open.flags = FMODE_READ;
127 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
132 if (!S_ISREG(nd.dentry->d_inode->i_mode))
135 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
139 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
140 error = PTR_ERR(file);
146 struct linux_binfmt * fmt;
148 read_lock(&binfmt_lock);
149 for (fmt = formats ; fmt ; fmt = fmt->next) {
150 if (!fmt->load_shlib)
152 if (!try_module_get(fmt->module))
154 read_unlock(&binfmt_lock);
155 error = fmt->load_shlib(file);
156 read_lock(&binfmt_lock);
158 if (error != -ENOEXEC)
161 read_unlock(&binfmt_lock);
172 * count() counts the number of strings in array ARGV.
174 static int count(char __user * __user * argv, int max)
182 if (get_user(p, argv))
195 * 'copy_strings()' copies argument/environment strings from user
196 * memory to free pages in kernel mem. These are in a format ready
197 * to be put directly into the top of new user memory.
199 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
201 struct page *kmapped_page = NULL;
210 if (get_user(str, argv+argc) ||
211 !(len = strnlen_user(str, bprm->p))) {
222 /* XXX: add architecture specific overflow check here. */
227 int offset, bytes_to_copy;
230 offset = pos % PAGE_SIZE;
232 page = bprm->page[i];
235 page = alloc_page(GFP_HIGHUSER);
236 bprm->page[i] = page;
244 if (page != kmapped_page) {
246 kunmap(kmapped_page);
248 kaddr = kmap(kmapped_page);
251 memset(kaddr, 0, offset);
252 bytes_to_copy = PAGE_SIZE - offset;
253 if (bytes_to_copy > len) {
256 memset(kaddr+offset+len, 0,
257 PAGE_SIZE-offset-len);
259 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
265 pos += bytes_to_copy;
266 str += bytes_to_copy;
267 len -= bytes_to_copy;
273 kunmap(kmapped_page);
278 * Like copy_strings, but get argv and its values from kernel memory.
280 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
283 mm_segment_t oldfs = get_fs();
285 r = copy_strings(argc, (char __user * __user *)argv, bprm);
290 EXPORT_SYMBOL(copy_strings_kernel);
294 * This routine is used to map in a page into an address space: needed by
295 * execve() for the initial stack and environment pages.
297 * tsk->mmap_sem is held for writing.
299 void put_dirty_page(struct task_struct *tsk, struct page *page,
300 unsigned long address, pgprot_t prot)
305 struct pte_chain *pte_chain;
307 if (page_count(page) != 1)
308 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n",
311 pgd = pgd_offset(tsk->mm, address);
312 pte_chain = pte_chain_alloc(GFP_KERNEL);
315 spin_lock(&tsk->mm->page_table_lock);
316 pmd = pmd_alloc(tsk->mm, pgd, address);
319 pte = pte_alloc_map(tsk->mm, pmd, address);
322 if (!pte_none(*pte)) {
326 lru_cache_add_active(page);
327 flush_dcache_page(page);
328 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, prot))));
329 pte_chain = page_add_rmap(page, pte, pte_chain);
332 spin_unlock(&tsk->mm->page_table_lock);
334 /* no need for flush_tlb */
335 pte_chain_free(pte_chain);
338 spin_unlock(&tsk->mm->page_table_lock);
341 force_sig(SIGKILL, tsk);
342 pte_chain_free(pte_chain);
346 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
348 unsigned long stack_base;
349 struct vm_area_struct *mpnt;
350 struct mm_struct *mm = current->mm;
354 #ifdef CONFIG_STACK_GROWSUP
355 /* Move the argument and environment strings to the bottom of the
361 /* Start by shifting all the pages down */
363 for (j = 0; j < MAX_ARG_PAGES; j++) {
364 struct page *page = bprm->page[j];
367 bprm->page[i++] = page;
370 /* Now move them within their pages */
371 offset = bprm->p % PAGE_SIZE;
372 to = kmap(bprm->page[0]);
373 for (j = 1; j < i; j++) {
374 memmove(to, to + offset, PAGE_SIZE - offset);
375 from = kmap(bprm->page[j]);
376 memcpy(to + PAGE_SIZE - offset, from, offset);
377 kunmap(bprm->page[j - 1]);
380 memmove(to, to + offset, PAGE_SIZE - offset);
381 kunmap(bprm->page[j - 1]);
383 /* Adjust bprm->p to point to the end of the strings. */
384 bprm->p = PAGE_SIZE * i - offset;
386 /* Limit stack size to 1GB */
387 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
388 if (stack_base > (1 << 30))
389 stack_base = 1 << 30;
390 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
392 mm->arg_start = stack_base;
393 arg_size = i << PAGE_SHIFT;
395 /* zero pages that were copied above */
396 while (i < MAX_ARG_PAGES)
397 bprm->page[i++] = NULL;
399 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
400 mm->arg_start = bprm->p + stack_base;
401 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
404 bprm->p += stack_base;
406 bprm->loader += stack_base;
407 bprm->exec += stack_base;
409 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
413 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
414 kmem_cache_free(vm_area_cachep, mpnt);
418 down_write(&mm->mmap_sem);
421 #ifdef CONFIG_STACK_GROWSUP
422 mpnt->vm_start = stack_base;
423 mpnt->vm_end = PAGE_MASK &
424 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
426 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
427 mpnt->vm_end = STACK_TOP;
429 /* Adjust stack execute permissions; explicitly enable
430 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
431 * and leave alone (arch default) otherwise. */
432 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
433 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
434 else if (executable_stack == EXSTACK_DISABLE_X)
435 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
437 mpnt->vm_flags = VM_STACK_FLAGS;
438 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
441 mpnt->vm_file = NULL;
442 INIT_LIST_HEAD(&mpnt->shared);
443 mpnt->vm_private_data = (void *) 0;
444 insert_vm_struct(mm, mpnt);
445 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
448 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
449 struct page *page = bprm->page[i];
451 bprm->page[i] = NULL;
452 put_dirty_page(current, page, stack_base,
455 stack_base += PAGE_SIZE;
457 up_write(&mm->mmap_sem);
462 EXPORT_SYMBOL(setup_arg_pages);
464 #define free_arg_pages(bprm) do { } while (0)
468 static inline void free_arg_pages(struct linux_binprm *bprm)
472 for (i = 0; i < MAX_ARG_PAGES; i++) {
474 __free_page(bprm->page[i]);
475 bprm->page[i] = NULL;
479 #endif /* CONFIG_MMU */
481 struct file *open_exec(const char *name)
487 nd.intent.open.flags = FMODE_READ;
488 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
492 struct inode *inode = nd.dentry->d_inode;
493 file = ERR_PTR(-EACCES);
494 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
495 S_ISREG(inode->i_mode)) {
496 int err = permission(inode, MAY_EXEC, &nd);
497 if (!err && !(inode->i_mode & 0111))
501 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
503 err = deny_write_access(file);
518 EXPORT_SYMBOL(open_exec);
520 int kernel_read(struct file *file, unsigned long offset,
521 char *addr, unsigned long count)
529 /* The cast to a user pointer is valid due to the set_fs() */
530 result = vfs_read(file, (void __user *)addr, count, &pos);
535 EXPORT_SYMBOL(kernel_read);
537 static int exec_mmap(struct mm_struct *mm)
539 struct task_struct *tsk;
540 struct mm_struct * old_mm, *active_mm;
542 /* Add it to the list of mm's */
543 spin_lock(&mmlist_lock);
544 list_add(&mm->mmlist, &init_mm.mmlist);
546 spin_unlock(&mmlist_lock);
548 /* Notify parent that we're no longer interested in the old VM */
550 old_mm = current->mm;
551 mm_release(tsk, old_mm);
554 active_mm = tsk->active_mm;
557 activate_mm(active_mm, mm);
560 if (active_mm != old_mm) BUG();
569 * This function makes sure the current process has its own signal table,
570 * so that flush_signal_handlers can later reset the handlers without
571 * disturbing other processes. (Other processes might share the signal
572 * table via the CLONE_SIGHAND option to clone().)
574 static inline int de_thread(struct task_struct *tsk)
576 struct signal_struct *newsig, *oldsig = tsk->signal;
577 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
578 spinlock_t *lock = &oldsighand->siglock;
582 * If we don't share sighandlers, then we aren't sharing anything
583 * and we can just re-use it all.
585 if (atomic_read(&oldsighand->count) <= 1)
588 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
592 spin_lock_init(&newsighand->siglock);
593 atomic_set(&newsighand->count, 1);
594 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
597 * See if we need to allocate a new signal structure
600 if (atomic_read(&oldsig->count) > 1) {
601 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
603 kmem_cache_free(sighand_cachep, newsighand);
606 atomic_set(&newsig->count, 1);
607 newsig->group_exit = 0;
608 newsig->group_exit_code = 0;
609 newsig->group_exit_task = NULL;
610 newsig->group_stop_count = 0;
611 newsig->curr_target = NULL;
612 init_sigpending(&newsig->shared_pending);
613 INIT_LIST_HEAD(&newsig->posix_timers);
615 newsig->tty = oldsig->tty;
616 newsig->pgrp = oldsig->pgrp;
617 newsig->session = oldsig->session;
618 newsig->leader = oldsig->leader;
619 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
622 if (thread_group_empty(current))
623 goto no_thread_group;
626 * Kill all other threads in the thread group.
627 * We must hold tasklist_lock to call zap_other_threads.
629 read_lock(&tasklist_lock);
631 if (oldsig->group_exit) {
633 * Another group action in progress, just
634 * return so that the signal is processed.
636 spin_unlock_irq(lock);
637 read_unlock(&tasklist_lock);
638 kmem_cache_free(sighand_cachep, newsighand);
640 kmem_cache_free(signal_cachep, newsig);
643 oldsig->group_exit = 1;
644 zap_other_threads(current);
645 read_unlock(&tasklist_lock);
648 * Account for the thread group leader hanging around:
651 if (current->pid == current->tgid)
653 while (atomic_read(&oldsig->count) > count) {
654 oldsig->group_exit_task = current;
655 oldsig->notify_count = count;
656 __set_current_state(TASK_UNINTERRUPTIBLE);
657 spin_unlock_irq(lock);
661 spin_unlock_irq(lock);
664 * At this point all other threads have exited, all we have to
665 * do is to wait for the thread group leader to become inactive,
666 * and to assume its PID:
668 if (current->pid != current->tgid) {
669 struct task_struct *leader = current->group_leader, *parent;
670 struct dentry *proc_dentry1, *proc_dentry2;
671 unsigned long state, ptrace;
674 * Wait for the thread group leader to be a zombie.
675 * It should already be zombie at this point, most
678 while (leader->state != TASK_ZOMBIE)
681 spin_lock(&leader->proc_lock);
682 spin_lock(¤t->proc_lock);
683 proc_dentry1 = proc_pid_unhash(current);
684 proc_dentry2 = proc_pid_unhash(leader);
685 write_lock_irq(&tasklist_lock);
687 if (leader->tgid != current->tgid)
689 if (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;
700 ptrace_unlink(current);
701 ptrace_unlink(leader);
702 remove_parent(current);
703 remove_parent(leader);
705 switch_exec_pids(leader, current);
707 current->parent = current->real_parent = leader->real_parent;
708 leader->parent = leader->real_parent = child_reaper;
709 current->group_leader = current;
710 leader->group_leader = leader;
712 add_parent(current, current->parent);
713 add_parent(leader, leader->parent);
715 current->ptrace = ptrace;
716 __ptrace_link(current, parent);
719 list_del(¤t->tasks);
720 list_add_tail(¤t->tasks, &init_task.tasks);
721 current->exit_signal = SIGCHLD;
722 state = leader->state;
724 write_unlock_irq(&tasklist_lock);
725 spin_unlock(&leader->proc_lock);
726 spin_unlock(¤t->proc_lock);
727 proc_pid_flush(proc_dentry1);
728 proc_pid_flush(proc_dentry2);
730 if (state != TASK_ZOMBIE)
732 release_task(leader);
737 write_lock_irq(&tasklist_lock);
738 spin_lock(&oldsighand->siglock);
739 spin_lock(&newsighand->siglock);
741 if (current == oldsig->curr_target)
742 oldsig->curr_target = next_thread(current);
744 current->signal = newsig;
745 current->sighand = newsighand;
746 init_sigpending(¤t->pending);
749 spin_unlock(&newsighand->siglock);
750 spin_unlock(&oldsighand->siglock);
751 write_unlock_irq(&tasklist_lock);
753 if (newsig && atomic_dec_and_test(&oldsig->count))
754 kmem_cache_free(signal_cachep, oldsig);
756 if (atomic_dec_and_test(&oldsighand->count))
757 kmem_cache_free(sighand_cachep, oldsighand);
759 if (!thread_group_empty(current))
761 if (current->tgid != current->pid)
767 * These functions flushes out all traces of the currently running executable
768 * so that a new one can be started
771 static inline void flush_old_files(struct files_struct * files)
775 spin_lock(&files->file_lock);
777 unsigned long set, i;
781 if (i >= files->max_fds || i >= files->max_fdset)
783 set = files->close_on_exec->fds_bits[j];
786 files->close_on_exec->fds_bits[j] = 0;
787 spin_unlock(&files->file_lock);
788 for ( ; set ; i++,set >>= 1) {
793 spin_lock(&files->file_lock);
796 spin_unlock(&files->file_lock);
799 int flush_old_exec(struct linux_binprm * bprm)
803 struct files_struct *files;
806 * Make sure we have a private signal table and that
807 * we are unassociated from the previous thread group.
809 retval = de_thread(current);
814 * Make sure we have private file handles. Ask the
815 * fork helper to do the work for us and the exit
816 * helper to do the cleanup of the old one.
818 files = current->files; /* refcounted so safe to hold */
819 retval = unshare_files();
823 * Release all of the old mmap stuff
825 retval = exec_mmap(bprm->mm);
829 bprm->mm = NULL; /* We're using it now */
831 /* This is the point of no return */
833 put_files_struct(files);
835 current->sas_ss_sp = current->sas_ss_size = 0;
837 if (current->euid == current->uid && current->egid == current->gid)
838 current->mm->dumpable = 1;
839 name = bprm->filename;
840 for (i=0; (ch = *(name++)) != '\0';) {
845 current->comm[i++] = ch;
847 current->comm[i] = '\0';
851 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
852 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
853 current->mm->dumpable = 0;
855 /* An exec changes our domain. We are no longer part of the thread
858 current->self_exec_id++;
860 flush_signal_handlers(current, 0);
861 flush_old_files(current->files);
866 put_files_struct(current->files);
867 current->files = files;
872 EXPORT_SYMBOL(flush_old_exec);
875 * Fill the binprm structure from the inode.
876 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
878 int prepare_binprm(struct linux_binprm *bprm)
881 struct inode * inode = bprm->file->f_dentry->d_inode;
884 mode = inode->i_mode;
886 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
887 * vfs_permission lets a non-executable through
889 if (!(mode & 0111)) /* with at least _one_ execute bit set */
891 if (bprm->file->f_op == NULL)
894 bprm->e_uid = current->euid;
895 bprm->e_gid = current->egid;
897 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
900 bprm->e_uid = inode->i_uid;
904 * If setgid is set but no group execute bit then this
905 * is a candidate for mandatory locking, not a setgid
908 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
909 bprm->e_gid = inode->i_gid;
912 /* fill in binprm security blob */
913 retval = security_bprm_set(bprm);
917 memset(bprm->buf,0,BINPRM_BUF_SIZE);
918 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
921 EXPORT_SYMBOL(prepare_binprm);
923 static inline int unsafe_exec(struct task_struct *p)
926 if (p->ptrace & PT_PTRACED) {
927 if (p->ptrace & PT_PTRACE_CAP)
928 unsafe |= LSM_UNSAFE_PTRACE_CAP;
930 unsafe |= LSM_UNSAFE_PTRACE;
932 if (atomic_read(&p->fs->count) > 1 ||
933 atomic_read(&p->files->count) > 1 ||
934 atomic_read(&p->sighand->count) > 1)
935 unsafe |= LSM_UNSAFE_SHARE;
940 void compute_creds(struct linux_binprm *bprm)
944 unsafe = unsafe_exec(current);
945 security_bprm_apply_creds(bprm, unsafe);
946 task_unlock(current);
949 EXPORT_SYMBOL(compute_creds);
951 void remove_arg_zero(struct linux_binprm *bprm)
954 unsigned long offset;
958 offset = bprm->p % PAGE_SIZE;
961 while (bprm->p++, *(kaddr+offset++)) {
962 if (offset != PAGE_SIZE)
965 kunmap_atomic(kaddr, KM_USER0);
967 page = bprm->page[bprm->p/PAGE_SIZE];
968 kaddr = kmap_atomic(page, KM_USER0);
970 kunmap_atomic(kaddr, KM_USER0);
975 EXPORT_SYMBOL(remove_arg_zero);
978 * cycle the list of binary formats handler, until one recognizes the image
980 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
983 struct linux_binfmt *fmt;
985 /* handle /sbin/loader.. */
987 struct exec * eh = (struct exec *) bprm->buf;
989 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
990 (eh->fh.f_flags & 0x3000) == 0x3000)
993 unsigned long loader;
995 allow_write_access(bprm->file);
999 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1001 file = open_exec("/sbin/loader");
1002 retval = PTR_ERR(file);
1006 /* Remember if the application is TASO. */
1007 bprm->sh_bang = eh->ah.entry < 0x100000000;
1010 bprm->loader = loader;
1011 retval = prepare_binprm(bprm);
1014 /* should call search_binary_handler recursively here,
1015 but it does not matter */
1019 retval = security_bprm_check(bprm);
1023 /* kernel module loader fixup */
1024 /* so we don't try to load run modprobe in kernel space. */
1026 for (try=0; try<2; try++) {
1027 read_lock(&binfmt_lock);
1028 for (fmt = formats ; fmt ; fmt = fmt->next) {
1029 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1032 if (!try_module_get(fmt->module))
1034 read_unlock(&binfmt_lock);
1035 retval = fn(bprm, regs);
1038 allow_write_access(bprm->file);
1042 current->did_exec = 1;
1045 read_lock(&binfmt_lock);
1047 if (retval != -ENOEXEC || bprm->mm == NULL)
1050 read_unlock(&binfmt_lock);
1054 read_unlock(&binfmt_lock);
1055 if (retval != -ENOEXEC || bprm->mm == NULL) {
1059 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1060 if (printable(bprm->buf[0]) &&
1061 printable(bprm->buf[1]) &&
1062 printable(bprm->buf[2]) &&
1063 printable(bprm->buf[3]))
1064 break; /* -ENOEXEC */
1065 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1072 EXPORT_SYMBOL(search_binary_handler);
1075 * sys_execve() executes a new program.
1077 int do_execve(char * filename,
1078 char __user *__user *argv,
1079 char __user *__user *envp,
1080 struct pt_regs * regs)
1082 struct linux_binprm bprm;
1087 sched_balance_exec();
1089 file = open_exec(filename);
1091 retval = PTR_ERR(file);
1095 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1096 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1099 bprm.filename = filename;
1100 bprm.interp = filename;
1104 bprm.security = NULL;
1105 bprm.mm = mm_alloc();
1110 retval = init_new_context(current, bprm.mm);
1114 bprm.argc = count(argv, bprm.p / sizeof(void *));
1115 if ((retval = bprm.argc) < 0)
1118 bprm.envc = count(envp, bprm.p / sizeof(void *));
1119 if ((retval = bprm.envc) < 0)
1122 retval = security_bprm_alloc(&bprm);
1126 retval = prepare_binprm(&bprm);
1130 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1135 retval = copy_strings(bprm.envc, envp, &bprm);
1139 retval = copy_strings(bprm.argc, argv, &bprm);
1143 retval = search_binary_handler(&bprm,regs);
1145 free_arg_pages(&bprm);
1147 ckrm_cb_exec(filename);
1149 /* execve success */
1150 security_bprm_free(&bprm);
1155 /* Something went wrong, return the inode and free the argument pages*/
1156 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1157 struct page * page = bprm.page[i];
1163 security_bprm_free(&bprm);
1171 allow_write_access(bprm.file);
1177 EXPORT_SYMBOL(do_execve);
1179 int set_binfmt(struct linux_binfmt *new)
1181 struct linux_binfmt *old = current->binfmt;
1184 if (!try_module_get(new->module))
1187 current->binfmt = new;
1189 module_put(old->module);
1193 EXPORT_SYMBOL(set_binfmt);
1195 #define CORENAME_MAX_SIZE 64
1197 /* format_corename will inspect the pattern parameter, and output a
1198 * name into corename, which must have space for at least
1199 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1201 void format_corename(char *corename, const char *pattern, long signr)
1203 const char *pat_ptr = pattern;
1204 char *out_ptr = corename;
1205 char *const out_end = corename + CORENAME_MAX_SIZE;
1207 int pid_in_pattern = 0;
1209 /* Repeat as long as we have more pattern to process and more output
1212 if (*pat_ptr != '%') {
1213 if (out_ptr == out_end)
1215 *out_ptr++ = *pat_ptr++;
1217 switch (*++pat_ptr) {
1220 /* Double percent, output one percent */
1222 if (out_ptr == out_end)
1229 rc = snprintf(out_ptr, out_end - out_ptr,
1230 "%d", current->tgid);
1231 if (rc > out_end - out_ptr)
1237 rc = snprintf(out_ptr, out_end - out_ptr,
1238 "%d", current->uid);
1239 if (rc > out_end - out_ptr)
1245 rc = snprintf(out_ptr, out_end - out_ptr,
1246 "%d", current->gid);
1247 if (rc > out_end - out_ptr)
1251 /* signal that caused the coredump */
1253 rc = snprintf(out_ptr, out_end - out_ptr,
1255 if (rc > out_end - out_ptr)
1259 /* UNIX time of coredump */
1262 do_gettimeofday(&tv);
1263 rc = snprintf(out_ptr, out_end - out_ptr,
1265 if (rc > out_end - out_ptr)
1272 down_read(&uts_sem);
1273 rc = snprintf(out_ptr, out_end - out_ptr,
1274 "%s", system_utsname.nodename);
1276 if (rc > out_end - out_ptr)
1282 rc = snprintf(out_ptr, out_end - out_ptr,
1283 "%s", current->comm);
1284 if (rc > out_end - out_ptr)
1294 /* Backward compatibility with core_uses_pid:
1296 * If core_pattern does not include a %p (as is the default)
1297 * and core_uses_pid is set, then .%pid will be appended to
1300 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1301 rc = snprintf(out_ptr, out_end - out_ptr,
1302 ".%d", current->tgid);
1303 if (rc > out_end - out_ptr)
1311 static void zap_threads (struct mm_struct *mm)
1313 struct task_struct *g, *p;
1314 struct task_struct *tsk = current;
1315 struct completion *vfork_done = tsk->vfork_done;
1318 * Make sure nobody is waiting for us to release the VM,
1319 * otherwise we can deadlock when we wait on each other
1322 tsk->vfork_done = NULL;
1323 complete(vfork_done);
1326 read_lock(&tasklist_lock);
1328 if (mm == p->mm && p != tsk) {
1329 force_sig_specific(SIGKILL, p);
1332 while_each_thread(g,p);
1334 read_unlock(&tasklist_lock);
1337 static void coredump_wait(struct mm_struct *mm)
1339 DECLARE_COMPLETION(startup_done);
1341 mm->core_waiters++; /* let other threads block */
1342 mm->core_startup_done = &startup_done;
1344 /* give other threads a chance to run: */
1348 if (--mm->core_waiters) {
1349 up_write(&mm->mmap_sem);
1350 wait_for_completion(&startup_done);
1352 up_write(&mm->mmap_sem);
1353 BUG_ON(mm->core_waiters);
1356 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1358 char corename[CORENAME_MAX_SIZE + 1];
1359 struct mm_struct *mm = current->mm;
1360 struct linux_binfmt * binfmt;
1361 struct inode * inode;
1366 binfmt = current->binfmt;
1367 if (!binfmt || !binfmt->core_dump)
1369 down_write(&mm->mmap_sem);
1370 if (!mm->dumpable) {
1371 up_write(&mm->mmap_sem);
1375 init_completion(&mm->core_done);
1376 current->signal->group_exit = 1;
1377 current->signal->group_exit_code = exit_code;
1380 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1383 format_corename(corename, core_pattern, signr);
1384 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1387 inode = file->f_dentry->d_inode;
1388 if (inode->i_nlink > 1)
1389 goto close_fail; /* multiple links - don't dump */
1390 if (d_unhashed(file->f_dentry))
1393 if (!S_ISREG(inode->i_mode))
1397 if (!file->f_op->write)
1399 if (do_truncate(file->f_dentry, 0) != 0)
1402 retval = binfmt->core_dump(signr, regs, file);
1404 current->signal->group_exit_code |= 0x80;
1406 filp_close(file, NULL);
1408 complete_all(&mm->core_done);