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>
50 #include <linux/ckrm_mem.h>
52 #include <asm/uaccess.h>
53 #include <asm/pgalloc.h>
54 #include <asm/mmu_context.h>
57 #include <linux/kmod.h>
61 char core_pattern[65] = "core";
62 /* The maximal length of core_pattern is also specified in sysctl.c */
64 static struct linux_binfmt *formats;
65 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
67 int register_binfmt(struct linux_binfmt * fmt)
69 struct linux_binfmt ** tmp = &formats;
75 write_lock(&binfmt_lock);
78 write_unlock(&binfmt_lock);
85 write_unlock(&binfmt_lock);
89 EXPORT_SYMBOL(register_binfmt);
91 int unregister_binfmt(struct linux_binfmt * fmt)
93 struct linux_binfmt ** tmp = &formats;
95 write_lock(&binfmt_lock);
99 write_unlock(&binfmt_lock);
104 write_unlock(&binfmt_lock);
108 EXPORT_SYMBOL(unregister_binfmt);
110 static inline void put_binfmt(struct linux_binfmt * fmt)
112 module_put(fmt->module);
116 * Note that a shared library must be both readable and executable due to
119 * Also note that we take the address to load from from the file itself.
121 asmlinkage long sys_uselib(const char __user * library)
127 nd.intent.open.flags = FMODE_READ;
128 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
133 if (!S_ISREG(nd.dentry->d_inode->i_mode))
136 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
140 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
141 error = PTR_ERR(file);
147 struct linux_binfmt * fmt;
149 read_lock(&binfmt_lock);
150 for (fmt = formats ; fmt ; fmt = fmt->next) {
151 if (!fmt->load_shlib)
153 if (!try_module_get(fmt->module))
155 read_unlock(&binfmt_lock);
156 error = fmt->load_shlib(file);
157 read_lock(&binfmt_lock);
159 if (error != -ENOEXEC)
162 read_unlock(&binfmt_lock);
173 * count() counts the number of strings in array ARGV.
175 static int count(char __user * __user * argv, int max)
183 if (get_user(p, argv))
196 * 'copy_strings()' copies argument/environment strings from user
197 * memory to free pages in kernel mem. These are in a format ready
198 * to be put directly into the top of new user memory.
200 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
202 struct page *kmapped_page = NULL;
211 if (get_user(str, argv+argc) ||
212 !(len = strnlen_user(str, bprm->p))) {
223 /* XXX: add architecture specific overflow check here. */
228 int offset, bytes_to_copy;
231 offset = pos % PAGE_SIZE;
233 page = bprm->page[i];
236 page = alloc_page(GFP_HIGHUSER);
237 bprm->page[i] = page;
245 if (page != kmapped_page) {
247 kunmap(kmapped_page);
249 kaddr = kmap(kmapped_page);
252 memset(kaddr, 0, offset);
253 bytes_to_copy = PAGE_SIZE - offset;
254 if (bytes_to_copy > len) {
257 memset(kaddr+offset+len, 0,
258 PAGE_SIZE-offset-len);
260 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
266 pos += bytes_to_copy;
267 str += bytes_to_copy;
268 len -= bytes_to_copy;
274 kunmap(kmapped_page);
279 * Like copy_strings, but get argv and its values from kernel memory.
281 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
284 mm_segment_t oldfs = get_fs();
286 r = copy_strings(argc, (char __user * __user *)argv, bprm);
291 EXPORT_SYMBOL(copy_strings_kernel);
295 * This routine is used to map in a page into an address space: needed by
296 * execve() for the initial stack and environment pages.
298 * vma->vm_mm->mmap_sem is held for writing.
300 void install_arg_page(struct vm_area_struct *vma,
301 struct page *page, unsigned long address)
303 struct mm_struct *mm = vma->vm_mm;
308 if (unlikely(anon_vma_prepare(vma)))
311 flush_dcache_page(page);
312 pgd = pgd_offset(mm, address);
314 spin_lock(&mm->page_table_lock);
315 pmd = pmd_alloc(mm, pgd, address);
318 pte = pte_alloc_map(mm, pmd, address);
321 if (!pte_none(*pte)) {
326 lru_cache_add_active(page);
327 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
328 page, vma->vm_page_prot))));
329 page_add_anon_rmap(page, vma, address);
331 spin_unlock(&mm->page_table_lock);
333 /* no need for flush_tlb */
336 spin_unlock(&mm->page_table_lock);
339 force_sig(SIGKILL, current);
342 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
344 unsigned long stack_base;
345 struct vm_area_struct *mpnt;
346 struct mm_struct *mm = current->mm;
350 #ifdef CONFIG_STACK_GROWSUP
351 /* Move the argument and environment strings to the bottom of the
357 /* Start by shifting all the pages down */
359 for (j = 0; j < MAX_ARG_PAGES; j++) {
360 struct page *page = bprm->page[j];
363 bprm->page[i++] = page;
366 /* Now move them within their pages */
367 offset = bprm->p % PAGE_SIZE;
368 to = kmap(bprm->page[0]);
369 for (j = 1; j < i; j++) {
370 memmove(to, to + offset, PAGE_SIZE - offset);
371 from = kmap(bprm->page[j]);
372 memcpy(to + PAGE_SIZE - offset, from, offset);
373 kunmap(bprm->page[j - 1]);
376 memmove(to, to + offset, PAGE_SIZE - offset);
377 kunmap(bprm->page[j - 1]);
379 /* Adjust bprm->p to point to the end of the strings. */
380 bprm->p = PAGE_SIZE * i - offset;
382 /* Limit stack size to 1GB */
383 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
384 if (stack_base > (1 << 30))
385 stack_base = 1 << 30;
386 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
388 mm->arg_start = stack_base;
389 arg_size = i << PAGE_SHIFT;
391 /* zero pages that were copied above */
392 while (i < MAX_ARG_PAGES)
393 bprm->page[i++] = NULL;
395 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
396 mm->arg_start = bprm->p + stack_base;
397 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
400 bprm->p += stack_base;
402 bprm->loader += stack_base;
403 bprm->exec += stack_base;
405 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
409 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
410 kmem_cache_free(vm_area_cachep, mpnt);
414 memset(mpnt, 0, sizeof(*mpnt));
416 down_write(&mm->mmap_sem);
419 #ifdef CONFIG_STACK_GROWSUP
420 mpnt->vm_start = stack_base;
421 mpnt->vm_end = PAGE_MASK &
422 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
424 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
425 mpnt->vm_end = STACK_TOP;
427 /* Adjust stack execute permissions; explicitly enable
428 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
429 * and leave alone (arch default) otherwise. */
430 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
431 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
432 else if (executable_stack == EXSTACK_DISABLE_X)
433 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
435 mpnt->vm_flags = VM_STACK_FLAGS;
436 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
437 insert_vm_struct(mm, mpnt);
438 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
441 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
442 struct page *page = bprm->page[i];
444 bprm->page[i] = NULL;
445 install_arg_page(mpnt, page, stack_base);
447 stack_base += PAGE_SIZE;
449 up_write(&mm->mmap_sem);
454 EXPORT_SYMBOL(setup_arg_pages);
456 #define free_arg_pages(bprm) do { } while (0)
460 static inline void free_arg_pages(struct linux_binprm *bprm)
464 for (i = 0; i < MAX_ARG_PAGES; i++) {
466 __free_page(bprm->page[i]);
467 bprm->page[i] = NULL;
471 #endif /* CONFIG_MMU */
473 struct file *open_exec(const char *name)
479 nd.intent.open.flags = FMODE_READ;
480 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
484 struct inode *inode = nd.dentry->d_inode;
485 file = ERR_PTR(-EACCES);
486 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
487 S_ISREG(inode->i_mode)) {
488 int err = permission(inode, MAY_EXEC, &nd);
489 if (!err && !(inode->i_mode & 0111))
493 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
495 err = deny_write_access(file);
510 EXPORT_SYMBOL(open_exec);
512 int kernel_read(struct file *file, unsigned long offset,
513 char *addr, unsigned long count)
521 /* The cast to a user pointer is valid due to the set_fs() */
522 result = vfs_read(file, (void __user *)addr, count, &pos);
527 EXPORT_SYMBOL(kernel_read);
529 static int exec_mmap(struct mm_struct *mm)
531 struct task_struct *tsk;
532 struct mm_struct * old_mm, *active_mm;
534 /* Add it to the list of mm's */
535 spin_lock(&mmlist_lock);
536 list_add(&mm->mmlist, &init_mm.mmlist);
538 spin_unlock(&mmlist_lock);
540 /* Notify parent that we're no longer interested in the old VM */
542 old_mm = current->mm;
543 mm_release(tsk, old_mm);
546 active_mm = tsk->active_mm;
549 activate_mm(active_mm, mm);
551 #ifdef CONFIG_CKRM_RES_MEM
553 spin_lock(&old_mm->peertask_lock);
554 list_del(&tsk->mm_peers);
555 ckrm_mem_evaluate_mm(old_mm);
556 spin_unlock(&old_mm->peertask_lock);
558 spin_lock(&mm->peertask_lock);
559 list_add_tail(&tsk->mm_peers, &mm->tasklist);
560 ckrm_mem_evaluate_mm(mm);
561 spin_unlock(&mm->peertask_lock);
564 if (active_mm != old_mm) BUG();
573 * This function makes sure the current process has its own signal table,
574 * so that flush_signal_handlers can later reset the handlers without
575 * disturbing other processes. (Other processes might share the signal
576 * table via the CLONE_SIGHAND option to clone().)
578 static inline int de_thread(struct task_struct *tsk)
580 struct signal_struct *newsig, *oldsig = tsk->signal;
581 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
582 spinlock_t *lock = &oldsighand->siglock;
586 * If we don't share sighandlers, then we aren't sharing anything
587 * and we can just re-use it all.
589 if (atomic_read(&oldsighand->count) <= 1)
592 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
596 spin_lock_init(&newsighand->siglock);
597 atomic_set(&newsighand->count, 1);
598 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
601 * See if we need to allocate a new signal structure
604 if (atomic_read(&oldsig->count) > 1) {
605 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
607 kmem_cache_free(sighand_cachep, newsighand);
610 atomic_set(&newsig->count, 1);
611 newsig->group_exit = 0;
612 newsig->group_exit_code = 0;
613 newsig->group_exit_task = NULL;
614 newsig->group_stop_count = 0;
615 newsig->curr_target = NULL;
616 init_sigpending(&newsig->shared_pending);
617 INIT_LIST_HEAD(&newsig->posix_timers);
619 newsig->tty = oldsig->tty;
620 newsig->pgrp = oldsig->pgrp;
621 newsig->session = oldsig->session;
622 newsig->leader = oldsig->leader;
623 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
626 if (thread_group_empty(current))
627 goto no_thread_group;
630 * Kill all other threads in the thread group.
631 * We must hold tasklist_lock to call zap_other_threads.
633 read_lock(&tasklist_lock);
635 if (oldsig->group_exit) {
637 * Another group action in progress, just
638 * return so that the signal is processed.
640 spin_unlock_irq(lock);
641 read_unlock(&tasklist_lock);
642 kmem_cache_free(sighand_cachep, newsighand);
644 kmem_cache_free(signal_cachep, newsig);
647 oldsig->group_exit = 1;
648 zap_other_threads(current);
649 read_unlock(&tasklist_lock);
652 * Account for the thread group leader hanging around:
655 if (current->pid == current->tgid)
657 while (atomic_read(&oldsig->count) > count) {
658 oldsig->group_exit_task = current;
659 oldsig->notify_count = count;
660 __set_current_state(TASK_UNINTERRUPTIBLE);
661 spin_unlock_irq(lock);
665 spin_unlock_irq(lock);
668 * At this point all other threads have exited, all we have to
669 * do is to wait for the thread group leader to become inactive,
670 * and to assume its PID:
672 if (current->pid != current->tgid) {
673 struct task_struct *leader = current->group_leader, *parent;
674 struct dentry *proc_dentry1, *proc_dentry2;
675 unsigned long state, ptrace;
678 * Wait for the thread group leader to be a zombie.
679 * It should already be zombie at this point, most
682 while (leader->state != TASK_ZOMBIE)
685 spin_lock(&leader->proc_lock);
686 spin_lock(¤t->proc_lock);
687 proc_dentry1 = proc_pid_unhash(current);
688 proc_dentry2 = proc_pid_unhash(leader);
689 write_lock_irq(&tasklist_lock);
691 if (leader->tgid != current->tgid)
693 if (current->pid == current->tgid)
696 * An exec() starts a new thread group with the
697 * TGID of the previous thread group. Rehash the
698 * two threads with a switched PID, and release
699 * the former thread group leader:
701 ptrace = leader->ptrace;
702 parent = leader->parent;
704 ptrace_unlink(current);
705 ptrace_unlink(leader);
706 remove_parent(current);
707 remove_parent(leader);
709 switch_exec_pids(leader, current);
711 current->parent = current->real_parent = leader->real_parent;
712 leader->parent = leader->real_parent = child_reaper;
713 current->group_leader = current;
714 leader->group_leader = leader;
716 add_parent(current, current->parent);
717 add_parent(leader, leader->parent);
719 current->ptrace = ptrace;
720 __ptrace_link(current, parent);
723 list_del(¤t->tasks);
724 list_add_tail(¤t->tasks, &init_task.tasks);
725 current->exit_signal = SIGCHLD;
726 state = leader->state;
728 write_unlock_irq(&tasklist_lock);
729 spin_unlock(&leader->proc_lock);
730 spin_unlock(¤t->proc_lock);
731 proc_pid_flush(proc_dentry1);
732 proc_pid_flush(proc_dentry2);
734 if (state != TASK_ZOMBIE)
736 release_task(leader);
741 write_lock_irq(&tasklist_lock);
742 spin_lock(&oldsighand->siglock);
743 spin_lock(&newsighand->siglock);
745 if (current == oldsig->curr_target)
746 oldsig->curr_target = next_thread(current);
748 current->signal = newsig;
749 current->sighand = newsighand;
750 init_sigpending(¤t->pending);
753 spin_unlock(&newsighand->siglock);
754 spin_unlock(&oldsighand->siglock);
755 write_unlock_irq(&tasklist_lock);
757 if (newsig && atomic_dec_and_test(&oldsig->count))
758 kmem_cache_free(signal_cachep, oldsig);
760 if (atomic_dec_and_test(&oldsighand->count))
761 kmem_cache_free(sighand_cachep, oldsighand);
763 if (!thread_group_empty(current))
765 if (current->tgid != current->pid)
771 * These functions flushes out all traces of the currently running executable
772 * so that a new one can be started
775 static inline void flush_old_files(struct files_struct * files)
779 spin_lock(&files->file_lock);
781 unsigned long set, i;
785 if (i >= files->max_fds || i >= files->max_fdset)
787 set = files->close_on_exec->fds_bits[j];
790 files->close_on_exec->fds_bits[j] = 0;
791 spin_unlock(&files->file_lock);
792 for ( ; set ; i++,set >>= 1) {
797 spin_lock(&files->file_lock);
800 spin_unlock(&files->file_lock);
803 int flush_old_exec(struct linux_binprm * bprm)
807 struct files_struct *files;
810 * Make sure we have a private signal table and that
811 * we are unassociated from the previous thread group.
813 retval = de_thread(current);
818 * Make sure we have private file handles. Ask the
819 * fork helper to do the work for us and the exit
820 * helper to do the cleanup of the old one.
822 files = current->files; /* refcounted so safe to hold */
823 retval = unshare_files();
827 * Release all of the old mmap stuff
829 retval = exec_mmap(bprm->mm);
833 bprm->mm = NULL; /* We're using it now */
835 /* This is the point of no return */
837 put_files_struct(files);
839 current->sas_ss_sp = current->sas_ss_size = 0;
841 if (current->euid == current->uid && current->egid == current->gid)
842 current->mm->dumpable = 1;
843 name = bprm->filename;
844 for (i=0; (ch = *(name++)) != '\0';) {
849 current->comm[i++] = ch;
851 current->comm[i] = '\0';
855 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
856 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
857 current->mm->dumpable = 0;
859 /* An exec changes our domain. We are no longer part of the thread
862 current->self_exec_id++;
864 flush_signal_handlers(current, 0);
865 flush_old_files(current->files);
870 put_files_struct(current->files);
871 current->files = files;
876 EXPORT_SYMBOL(flush_old_exec);
879 * Fill the binprm structure from the inode.
880 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
882 int prepare_binprm(struct linux_binprm *bprm)
885 struct inode * inode = bprm->file->f_dentry->d_inode;
888 mode = inode->i_mode;
890 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
891 * vfs_permission lets a non-executable through
893 if (!(mode & 0111)) /* with at least _one_ execute bit set */
895 if (bprm->file->f_op == NULL)
898 bprm->e_uid = current->euid;
899 bprm->e_gid = current->egid;
901 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
904 bprm->e_uid = inode->i_uid;
908 * If setgid is set but no group execute bit then this
909 * is a candidate for mandatory locking, not a setgid
912 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
913 bprm->e_gid = inode->i_gid;
916 /* fill in binprm security blob */
917 retval = security_bprm_set(bprm);
921 memset(bprm->buf,0,BINPRM_BUF_SIZE);
922 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
925 EXPORT_SYMBOL(prepare_binprm);
927 static inline int unsafe_exec(struct task_struct *p)
930 if (p->ptrace & PT_PTRACED) {
931 if (p->ptrace & PT_PTRACE_CAP)
932 unsafe |= LSM_UNSAFE_PTRACE_CAP;
934 unsafe |= LSM_UNSAFE_PTRACE;
936 if (atomic_read(&p->fs->count) > 1 ||
937 atomic_read(&p->files->count) > 1 ||
938 atomic_read(&p->sighand->count) > 1)
939 unsafe |= LSM_UNSAFE_SHARE;
944 void compute_creds(struct linux_binprm *bprm)
948 unsafe = unsafe_exec(current);
949 security_bprm_apply_creds(bprm, unsafe);
950 task_unlock(current);
953 EXPORT_SYMBOL(compute_creds);
955 void remove_arg_zero(struct linux_binprm *bprm)
958 unsigned long offset;
962 offset = bprm->p % PAGE_SIZE;
965 while (bprm->p++, *(kaddr+offset++)) {
966 if (offset != PAGE_SIZE)
969 kunmap_atomic(kaddr, KM_USER0);
971 page = bprm->page[bprm->p/PAGE_SIZE];
972 kaddr = kmap_atomic(page, KM_USER0);
974 kunmap_atomic(kaddr, KM_USER0);
979 EXPORT_SYMBOL(remove_arg_zero);
982 * cycle the list of binary formats handler, until one recognizes the image
984 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
987 struct linux_binfmt *fmt;
989 /* handle /sbin/loader.. */
991 struct exec * eh = (struct exec *) bprm->buf;
993 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
994 (eh->fh.f_flags & 0x3000) == 0x3000)
997 unsigned long loader;
999 allow_write_access(bprm->file);
1003 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1005 file = open_exec("/sbin/loader");
1006 retval = PTR_ERR(file);
1010 /* Remember if the application is TASO. */
1011 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1014 bprm->loader = loader;
1015 retval = prepare_binprm(bprm);
1018 /* should call search_binary_handler recursively here,
1019 but it does not matter */
1023 retval = security_bprm_check(bprm);
1027 /* kernel module loader fixup */
1028 /* so we don't try to load run modprobe in kernel space. */
1030 for (try=0; try<2; try++) {
1031 read_lock(&binfmt_lock);
1032 for (fmt = formats ; fmt ; fmt = fmt->next) {
1033 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1036 if (!try_module_get(fmt->module))
1038 read_unlock(&binfmt_lock);
1039 retval = fn(bprm, regs);
1042 allow_write_access(bprm->file);
1046 current->did_exec = 1;
1047 ckrm_cb_exec(bprm->filename);
1050 read_lock(&binfmt_lock);
1052 if (retval != -ENOEXEC || bprm->mm == NULL)
1055 read_unlock(&binfmt_lock);
1059 read_unlock(&binfmt_lock);
1060 if (retval != -ENOEXEC || bprm->mm == NULL) {
1064 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1065 if (printable(bprm->buf[0]) &&
1066 printable(bprm->buf[1]) &&
1067 printable(bprm->buf[2]) &&
1068 printable(bprm->buf[3]))
1069 break; /* -ENOEXEC */
1070 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1077 EXPORT_SYMBOL(search_binary_handler);
1080 * sys_execve() executes a new program.
1082 int do_execve(char * filename,
1083 char __user *__user *argv,
1084 char __user *__user *envp,
1085 struct pt_regs * regs)
1087 struct linux_binprm bprm;
1092 file = open_exec(filename);
1094 retval = PTR_ERR(file);
1098 sched_balance_exec();
1100 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1101 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1104 bprm.filename = filename;
1105 bprm.interp = filename;
1109 bprm.security = NULL;
1110 bprm.mm = mm_alloc();
1115 retval = init_new_context(current, bprm.mm);
1119 bprm.argc = count(argv, bprm.p / sizeof(void *));
1120 if ((retval = bprm.argc) < 0)
1123 bprm.envc = count(envp, bprm.p / sizeof(void *));
1124 if ((retval = bprm.envc) < 0)
1127 retval = security_bprm_alloc(&bprm);
1131 retval = prepare_binprm(&bprm);
1135 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1140 retval = copy_strings(bprm.envc, envp, &bprm);
1144 retval = copy_strings(bprm.argc, argv, &bprm);
1148 retval = search_binary_handler(&bprm,regs);
1150 free_arg_pages(&bprm);
1152 /* execve success */
1153 security_bprm_free(&bprm);
1158 /* Something went wrong, return the inode and free the argument pages*/
1159 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1160 struct page * page = bprm.page[i];
1166 security_bprm_free(&bprm);
1174 allow_write_access(bprm.file);
1180 EXPORT_SYMBOL(do_execve);
1182 int set_binfmt(struct linux_binfmt *new)
1184 struct linux_binfmt *old = current->binfmt;
1187 if (!try_module_get(new->module))
1190 current->binfmt = new;
1192 module_put(old->module);
1196 EXPORT_SYMBOL(set_binfmt);
1198 #define CORENAME_MAX_SIZE 64
1200 /* format_corename will inspect the pattern parameter, and output a
1201 * name into corename, which must have space for at least
1202 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1204 void format_corename(char *corename, const char *pattern, long signr)
1206 const char *pat_ptr = pattern;
1207 char *out_ptr = corename;
1208 char *const out_end = corename + CORENAME_MAX_SIZE;
1210 int pid_in_pattern = 0;
1212 /* Repeat as long as we have more pattern to process and more output
1215 if (*pat_ptr != '%') {
1216 if (out_ptr == out_end)
1218 *out_ptr++ = *pat_ptr++;
1220 switch (*++pat_ptr) {
1223 /* Double percent, output one percent */
1225 if (out_ptr == out_end)
1232 rc = snprintf(out_ptr, out_end - out_ptr,
1233 "%d", current->tgid);
1234 if (rc > out_end - out_ptr)
1240 rc = snprintf(out_ptr, out_end - out_ptr,
1241 "%d", current->uid);
1242 if (rc > out_end - out_ptr)
1248 rc = snprintf(out_ptr, out_end - out_ptr,
1249 "%d", current->gid);
1250 if (rc > out_end - out_ptr)
1254 /* signal that caused the coredump */
1256 rc = snprintf(out_ptr, out_end - out_ptr,
1258 if (rc > out_end - out_ptr)
1262 /* UNIX time of coredump */
1265 do_gettimeofday(&tv);
1266 rc = snprintf(out_ptr, out_end - out_ptr,
1268 if (rc > out_end - out_ptr)
1275 down_read(&uts_sem);
1276 rc = snprintf(out_ptr, out_end - out_ptr,
1277 "%s", system_utsname.nodename);
1279 if (rc > out_end - out_ptr)
1285 rc = snprintf(out_ptr, out_end - out_ptr,
1286 "%s", current->comm);
1287 if (rc > out_end - out_ptr)
1297 /* Backward compatibility with core_uses_pid:
1299 * If core_pattern does not include a %p (as is the default)
1300 * and core_uses_pid is set, then .%pid will be appended to
1303 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1304 rc = snprintf(out_ptr, out_end - out_ptr,
1305 ".%d", current->tgid);
1306 if (rc > out_end - out_ptr)
1314 static void zap_threads (struct mm_struct *mm)
1316 struct task_struct *g, *p;
1317 struct task_struct *tsk = current;
1318 struct completion *vfork_done = tsk->vfork_done;
1321 * Make sure nobody is waiting for us to release the VM,
1322 * otherwise we can deadlock when we wait on each other
1325 tsk->vfork_done = NULL;
1326 complete(vfork_done);
1329 read_lock(&tasklist_lock);
1331 if (mm == p->mm && p != tsk) {
1332 force_sig_specific(SIGKILL, p);
1335 while_each_thread(g,p);
1337 read_unlock(&tasklist_lock);
1340 static void coredump_wait(struct mm_struct *mm)
1342 DECLARE_COMPLETION(startup_done);
1344 mm->core_waiters++; /* let other threads block */
1345 mm->core_startup_done = &startup_done;
1347 /* give other threads a chance to run: */
1351 if (--mm->core_waiters) {
1352 up_write(&mm->mmap_sem);
1353 wait_for_completion(&startup_done);
1355 up_write(&mm->mmap_sem);
1356 BUG_ON(mm->core_waiters);
1359 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1361 char corename[CORENAME_MAX_SIZE + 1];
1362 struct mm_struct *mm = current->mm;
1363 struct linux_binfmt * binfmt;
1364 struct inode * inode;
1369 binfmt = current->binfmt;
1370 if (!binfmt || !binfmt->core_dump)
1372 down_write(&mm->mmap_sem);
1373 if (!mm->dumpable) {
1374 up_write(&mm->mmap_sem);
1378 init_completion(&mm->core_done);
1379 current->signal->group_exit = 1;
1380 current->signal->group_exit_code = exit_code;
1383 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1386 format_corename(corename, core_pattern, signr);
1387 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1390 inode = file->f_dentry->d_inode;
1391 if (inode->i_nlink > 1)
1392 goto close_fail; /* multiple links - don't dump */
1393 if (d_unhashed(file->f_dentry))
1396 if (!S_ISREG(inode->i_mode))
1400 if (!file->f_op->write)
1402 if (do_truncate(file->f_dentry, 0) != 0)
1405 retval = binfmt->core_dump(signr, regs, file);
1407 current->signal->group_exit_code |= 0x80;
1409 filp_close(file, NULL);
1411 complete_all(&mm->core_done);