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 vx_rsspages_inc(tsk->mm);
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 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
415 kmem_cache_free(vm_area_cachep, mpnt);
419 down_write(&mm->mmap_sem);
422 #ifdef CONFIG_STACK_GROWSUP
423 mpnt->vm_start = stack_base;
424 mpnt->vm_end = PAGE_MASK &
425 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
427 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
428 mpnt->vm_end = STACK_TOP;
430 /* Adjust stack execute permissions; explicitly enable
431 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
432 * and leave alone (arch default) otherwise. */
433 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
434 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
435 else if (executable_stack == EXSTACK_DISABLE_X)
436 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
438 mpnt->vm_flags = VM_STACK_FLAGS;
439 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
442 mpnt->vm_file = NULL;
443 INIT_LIST_HEAD(&mpnt->shared);
444 mpnt->vm_private_data = (void *) 0;
445 insert_vm_struct(mm, mpnt);
446 // mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
447 vx_vmpages_sub(mm, mm->total_vm -
448 ((mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT));
451 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
452 struct page *page = bprm->page[i];
454 bprm->page[i] = NULL;
455 put_dirty_page(current, page, stack_base,
458 stack_base += PAGE_SIZE;
460 up_write(&mm->mmap_sem);
465 EXPORT_SYMBOL(setup_arg_pages);
467 #define free_arg_pages(bprm) do { } while (0)
471 static inline void free_arg_pages(struct linux_binprm *bprm)
475 for (i = 0; i < MAX_ARG_PAGES; i++) {
477 __free_page(bprm->page[i]);
478 bprm->page[i] = NULL;
482 #endif /* CONFIG_MMU */
484 struct file *open_exec(const char *name)
490 nd.intent.open.flags = FMODE_READ;
491 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
495 struct inode *inode = nd.dentry->d_inode;
496 file = ERR_PTR(-EACCES);
497 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
498 S_ISREG(inode->i_mode)) {
499 int err = permission(inode, MAY_EXEC, &nd);
500 if (!err && !(inode->i_mode & 0111))
504 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
506 err = deny_write_access(file);
521 EXPORT_SYMBOL(open_exec);
523 int kernel_read(struct file *file, unsigned long offset,
524 char *addr, unsigned long count)
532 /* The cast to a user pointer is valid due to the set_fs() */
533 result = vfs_read(file, (void __user *)addr, count, &pos);
538 EXPORT_SYMBOL(kernel_read);
540 static int exec_mmap(struct mm_struct *mm)
542 struct task_struct *tsk;
543 struct mm_struct * old_mm, *active_mm;
545 /* Add it to the list of mm's */
546 spin_lock(&mmlist_lock);
547 list_add(&mm->mmlist, &init_mm.mmlist);
549 spin_unlock(&mmlist_lock);
551 /* Notify parent that we're no longer interested in the old VM */
553 old_mm = current->mm;
554 mm_release(tsk, old_mm);
557 active_mm = tsk->active_mm;
560 activate_mm(active_mm, mm);
563 if (active_mm != old_mm) BUG();
572 * This function makes sure the current process has its own signal table,
573 * so that flush_signal_handlers can later reset the handlers without
574 * disturbing other processes. (Other processes might share the signal
575 * table via the CLONE_SIGHAND option to clone().)
577 static inline int de_thread(struct task_struct *tsk)
579 struct signal_struct *newsig, *oldsig = tsk->signal;
580 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
581 spinlock_t *lock = &oldsighand->siglock;
585 * If we don't share sighandlers, then we aren't sharing anything
586 * and we can just re-use it all.
588 if (atomic_read(&oldsighand->count) <= 1)
591 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
595 spin_lock_init(&newsighand->siglock);
596 atomic_set(&newsighand->count, 1);
597 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
600 * See if we need to allocate a new signal structure
603 if (atomic_read(&oldsig->count) > 1) {
604 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
606 kmem_cache_free(sighand_cachep, newsighand);
609 atomic_set(&newsig->count, 1);
610 newsig->group_exit = 0;
611 newsig->group_exit_code = 0;
612 newsig->group_exit_task = NULL;
613 newsig->group_stop_count = 0;
614 newsig->curr_target = NULL;
615 init_sigpending(&newsig->shared_pending);
616 INIT_LIST_HEAD(&newsig->posix_timers);
618 newsig->tty = oldsig->tty;
619 newsig->pgrp = oldsig->pgrp;
620 newsig->session = oldsig->session;
621 newsig->leader = oldsig->leader;
622 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
625 if (thread_group_empty(current))
626 goto no_thread_group;
629 * Kill all other threads in the thread group.
630 * We must hold tasklist_lock to call zap_other_threads.
632 read_lock(&tasklist_lock);
634 if (oldsig->group_exit) {
636 * Another group action in progress, just
637 * return so that the signal is processed.
639 spin_unlock_irq(lock);
640 read_unlock(&tasklist_lock);
641 kmem_cache_free(sighand_cachep, newsighand);
643 kmem_cache_free(signal_cachep, newsig);
646 oldsig->group_exit = 1;
647 zap_other_threads(current);
648 read_unlock(&tasklist_lock);
651 * Account for the thread group leader hanging around:
654 if (current->pid == current->tgid)
656 while (atomic_read(&oldsig->count) > count) {
657 oldsig->group_exit_task = current;
658 oldsig->notify_count = count;
659 __set_current_state(TASK_UNINTERRUPTIBLE);
660 spin_unlock_irq(lock);
664 spin_unlock_irq(lock);
667 * At this point all other threads have exited, all we have to
668 * do is to wait for the thread group leader to become inactive,
669 * and to assume its PID:
671 if (current->pid != current->tgid) {
672 struct task_struct *leader = current->group_leader, *parent;
673 struct dentry *proc_dentry1, *proc_dentry2;
674 unsigned long state, ptrace;
677 * Wait for the thread group leader to be a zombie.
678 * It should already be zombie at this point, most
681 while (leader->state != TASK_ZOMBIE)
684 spin_lock(&leader->proc_lock);
685 spin_lock(¤t->proc_lock);
686 proc_dentry1 = proc_pid_unhash(current);
687 proc_dentry2 = proc_pid_unhash(leader);
688 write_lock_irq(&tasklist_lock);
690 if (leader->tgid != current->tgid)
692 if (current->pid == current->tgid)
695 * An exec() starts a new thread group with the
696 * TGID of the previous thread group. Rehash the
697 * two threads with a switched PID, and release
698 * the former thread group leader:
700 ptrace = leader->ptrace;
701 parent = leader->parent;
703 ptrace_unlink(current);
704 ptrace_unlink(leader);
705 remove_parent(current);
706 remove_parent(leader);
708 switch_exec_pids(leader, current);
710 current->parent = current->real_parent = leader->real_parent;
711 leader->parent = leader->real_parent = child_reaper;
712 current->group_leader = current;
713 leader->group_leader = leader;
715 add_parent(current, current->parent);
716 add_parent(leader, leader->parent);
718 current->ptrace = ptrace;
719 __ptrace_link(current, parent);
722 list_del(¤t->tasks);
723 list_add_tail(¤t->tasks, &init_task.tasks);
724 current->exit_signal = SIGCHLD;
725 state = leader->state;
727 write_unlock_irq(&tasklist_lock);
728 spin_unlock(&leader->proc_lock);
729 spin_unlock(¤t->proc_lock);
730 proc_pid_flush(proc_dentry1);
731 proc_pid_flush(proc_dentry2);
733 if (state != TASK_ZOMBIE)
735 release_task(leader);
740 write_lock_irq(&tasklist_lock);
741 spin_lock(&oldsighand->siglock);
742 spin_lock(&newsighand->siglock);
744 if (current == oldsig->curr_target)
745 oldsig->curr_target = next_thread(current);
747 current->signal = newsig;
748 current->sighand = newsighand;
749 init_sigpending(¤t->pending);
752 spin_unlock(&newsighand->siglock);
753 spin_unlock(&oldsighand->siglock);
754 write_unlock_irq(&tasklist_lock);
756 if (newsig && atomic_dec_and_test(&oldsig->count))
757 kmem_cache_free(signal_cachep, oldsig);
759 if (atomic_dec_and_test(&oldsighand->count))
760 kmem_cache_free(sighand_cachep, oldsighand);
762 if (!thread_group_empty(current))
764 if (current->tgid != current->pid)
770 * These functions flushes out all traces of the currently running executable
771 * so that a new one can be started
774 static inline void flush_old_files(struct files_struct * files)
778 spin_lock(&files->file_lock);
780 unsigned long set, i;
784 if (i >= files->max_fds || i >= files->max_fdset)
786 set = files->close_on_exec->fds_bits[j];
789 files->close_on_exec->fds_bits[j] = 0;
790 spin_unlock(&files->file_lock);
791 for ( ; set ; i++,set >>= 1) {
796 spin_lock(&files->file_lock);
799 spin_unlock(&files->file_lock);
802 int flush_old_exec(struct linux_binprm * bprm)
806 struct files_struct *files;
809 * Make sure we have a private signal table and that
810 * we are unassociated from the previous thread group.
812 retval = de_thread(current);
817 * Make sure we have private file handles. Ask the
818 * fork helper to do the work for us and the exit
819 * helper to do the cleanup of the old one.
821 files = current->files; /* refcounted so safe to hold */
822 retval = unshare_files();
826 * Release all of the old mmap stuff
828 retval = exec_mmap(bprm->mm);
832 bprm->mm = NULL; /* We're using it now */
834 /* This is the point of no return */
836 put_files_struct(files);
838 current->sas_ss_sp = current->sas_ss_size = 0;
840 if (current->euid == current->uid && current->egid == current->gid)
841 current->mm->dumpable = 1;
842 name = bprm->filename;
843 for (i=0; (ch = *(name++)) != '\0';) {
848 current->comm[i++] = ch;
850 current->comm[i] = '\0';
854 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
855 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
856 current->mm->dumpable = 0;
858 /* An exec changes our domain. We are no longer part of the thread
861 current->self_exec_id++;
863 flush_signal_handlers(current, 0);
864 flush_old_files(current->files);
869 put_files_struct(current->files);
870 current->files = files;
875 EXPORT_SYMBOL(flush_old_exec);
878 * Fill the binprm structure from the inode.
879 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
881 int prepare_binprm(struct linux_binprm *bprm)
884 struct inode * inode = bprm->file->f_dentry->d_inode;
887 mode = inode->i_mode;
889 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
890 * vfs_permission lets a non-executable through
892 if (!(mode & 0111)) /* with at least _one_ execute bit set */
894 if (bprm->file->f_op == NULL)
897 bprm->e_uid = current->euid;
898 bprm->e_gid = current->egid;
900 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
903 bprm->e_uid = inode->i_uid;
907 * If setgid is set but no group execute bit then this
908 * is a candidate for mandatory locking, not a setgid
911 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
912 bprm->e_gid = inode->i_gid;
915 /* fill in binprm security blob */
916 retval = security_bprm_set(bprm);
920 memset(bprm->buf,0,BINPRM_BUF_SIZE);
921 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
924 EXPORT_SYMBOL(prepare_binprm);
926 static inline int unsafe_exec(struct task_struct *p)
929 if (p->ptrace & PT_PTRACED) {
930 if (p->ptrace & PT_PTRACE_CAP)
931 unsafe |= LSM_UNSAFE_PTRACE_CAP;
933 unsafe |= LSM_UNSAFE_PTRACE;
935 if (atomic_read(&p->fs->count) > 1 ||
936 atomic_read(&p->files->count) > 1 ||
937 atomic_read(&p->sighand->count) > 1)
938 unsafe |= LSM_UNSAFE_SHARE;
943 void compute_creds(struct linux_binprm *bprm)
947 unsafe = unsafe_exec(current);
948 security_bprm_apply_creds(bprm, unsafe);
949 task_unlock(current);
952 EXPORT_SYMBOL(compute_creds);
954 void remove_arg_zero(struct linux_binprm *bprm)
957 unsigned long offset;
961 offset = bprm->p % PAGE_SIZE;
964 while (bprm->p++, *(kaddr+offset++)) {
965 if (offset != PAGE_SIZE)
968 kunmap_atomic(kaddr, KM_USER0);
970 page = bprm->page[bprm->p/PAGE_SIZE];
971 kaddr = kmap_atomic(page, KM_USER0);
973 kunmap_atomic(kaddr, KM_USER0);
978 EXPORT_SYMBOL(remove_arg_zero);
981 * cycle the list of binary formats handler, until one recognizes the image
983 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
986 struct linux_binfmt *fmt;
988 /* handle /sbin/loader.. */
990 struct exec * eh = (struct exec *) bprm->buf;
992 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
993 (eh->fh.f_flags & 0x3000) == 0x3000)
996 unsigned long loader;
998 allow_write_access(bprm->file);
1002 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1004 file = open_exec("/sbin/loader");
1005 retval = PTR_ERR(file);
1009 /* Remember if the application is TASO. */
1010 bprm->sh_bang = eh->ah.entry < 0x100000000;
1013 bprm->loader = loader;
1014 retval = prepare_binprm(bprm);
1017 /* should call search_binary_handler recursively here,
1018 but it does not matter */
1022 retval = security_bprm_check(bprm);
1026 /* kernel module loader fixup */
1027 /* so we don't try to load run modprobe in kernel space. */
1029 for (try=0; try<2; try++) {
1030 read_lock(&binfmt_lock);
1031 for (fmt = formats ; fmt ; fmt = fmt->next) {
1032 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1035 if (!try_module_get(fmt->module))
1037 read_unlock(&binfmt_lock);
1038 retval = fn(bprm, regs);
1041 allow_write_access(bprm->file);
1045 current->did_exec = 1;
1048 read_lock(&binfmt_lock);
1050 if (retval != -ENOEXEC || bprm->mm == NULL)
1053 read_unlock(&binfmt_lock);
1057 read_unlock(&binfmt_lock);
1058 if (retval != -ENOEXEC || bprm->mm == NULL) {
1062 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1063 if (printable(bprm->buf[0]) &&
1064 printable(bprm->buf[1]) &&
1065 printable(bprm->buf[2]) &&
1066 printable(bprm->buf[3]))
1067 break; /* -ENOEXEC */
1068 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1075 EXPORT_SYMBOL(search_binary_handler);
1078 * sys_execve() executes a new program.
1080 int do_execve(char * filename,
1081 char __user *__user *argv,
1082 char __user *__user *envp,
1083 struct pt_regs * regs)
1085 struct linux_binprm bprm;
1090 sched_balance_exec();
1092 file = open_exec(filename);
1094 retval = PTR_ERR(file);
1098 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1099 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1102 bprm.filename = filename;
1103 bprm.interp = filename;
1107 bprm.security = NULL;
1108 bprm.mm = mm_alloc();
1113 retval = init_new_context(current, bprm.mm);
1117 bprm.argc = count(argv, bprm.p / sizeof(void *));
1118 if ((retval = bprm.argc) < 0)
1121 bprm.envc = count(envp, bprm.p / sizeof(void *));
1122 if ((retval = bprm.envc) < 0)
1125 retval = security_bprm_alloc(&bprm);
1129 retval = prepare_binprm(&bprm);
1133 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1138 retval = copy_strings(bprm.envc, envp, &bprm);
1142 retval = copy_strings(bprm.argc, argv, &bprm);
1146 retval = search_binary_handler(&bprm,regs);
1148 free_arg_pages(&bprm);
1150 /* execve success */
1151 security_bprm_free(&bprm);
1156 /* Something went wrong, return the inode and free the argument pages*/
1157 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1158 struct page * page = bprm.page[i];
1164 security_bprm_free(&bprm);
1172 allow_write_access(bprm.file);
1178 EXPORT_SYMBOL(do_execve);
1180 int set_binfmt(struct linux_binfmt *new)
1182 struct linux_binfmt *old = current->binfmt;
1185 if (!try_module_get(new->module))
1188 current->binfmt = new;
1190 module_put(old->module);
1194 EXPORT_SYMBOL(set_binfmt);
1196 #define CORENAME_MAX_SIZE 64
1198 /* format_corename will inspect the pattern parameter, and output a
1199 * name into corename, which must have space for at least
1200 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1202 void format_corename(char *corename, const char *pattern, long signr)
1204 const char *pat_ptr = pattern;
1205 char *out_ptr = corename;
1206 char *const out_end = corename + CORENAME_MAX_SIZE;
1208 int pid_in_pattern = 0;
1210 /* Repeat as long as we have more pattern to process and more output
1213 if (*pat_ptr != '%') {
1214 if (out_ptr == out_end)
1216 *out_ptr++ = *pat_ptr++;
1218 switch (*++pat_ptr) {
1221 /* Double percent, output one percent */
1223 if (out_ptr == out_end)
1230 rc = snprintf(out_ptr, out_end - out_ptr,
1231 "%d", current->tgid);
1232 if (rc > out_end - out_ptr)
1238 rc = snprintf(out_ptr, out_end - out_ptr,
1239 "%d", current->uid);
1240 if (rc > out_end - out_ptr)
1246 rc = snprintf(out_ptr, out_end - out_ptr,
1247 "%d", current->gid);
1248 if (rc > out_end - out_ptr)
1252 /* signal that caused the coredump */
1254 rc = snprintf(out_ptr, out_end - out_ptr,
1256 if (rc > out_end - out_ptr)
1260 /* UNIX time of coredump */
1263 do_gettimeofday(&tv);
1264 rc = snprintf(out_ptr, out_end - out_ptr,
1266 if (rc > out_end - out_ptr)
1273 down_read(&uts_sem);
1274 rc = snprintf(out_ptr, out_end - out_ptr,
1275 "%s", system_utsname.nodename);
1277 if (rc > out_end - out_ptr)
1283 rc = snprintf(out_ptr, out_end - out_ptr,
1284 "%s", current->comm);
1285 if (rc > out_end - out_ptr)
1295 /* Backward compatibility with core_uses_pid:
1297 * If core_pattern does not include a %p (as is the default)
1298 * and core_uses_pid is set, then .%pid will be appended to
1301 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1302 rc = snprintf(out_ptr, out_end - out_ptr,
1303 ".%d", current->tgid);
1304 if (rc > out_end - out_ptr)
1312 static void zap_threads (struct mm_struct *mm)
1314 struct task_struct *g, *p;
1315 struct task_struct *tsk = current;
1316 struct completion *vfork_done = tsk->vfork_done;
1319 * Make sure nobody is waiting for us to release the VM,
1320 * otherwise we can deadlock when we wait on each other
1323 tsk->vfork_done = NULL;
1324 complete(vfork_done);
1327 read_lock(&tasklist_lock);
1329 if (mm == p->mm && p != tsk) {
1330 force_sig_specific(SIGKILL, p);
1333 while_each_thread(g,p);
1335 read_unlock(&tasklist_lock);
1338 static void coredump_wait(struct mm_struct *mm)
1340 DECLARE_COMPLETION(startup_done);
1342 mm->core_waiters++; /* let other threads block */
1343 mm->core_startup_done = &startup_done;
1345 /* give other threads a chance to run: */
1349 if (--mm->core_waiters) {
1350 up_write(&mm->mmap_sem);
1351 wait_for_completion(&startup_done);
1353 up_write(&mm->mmap_sem);
1354 BUG_ON(mm->core_waiters);
1357 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1359 char corename[CORENAME_MAX_SIZE + 1];
1360 struct mm_struct *mm = current->mm;
1361 struct linux_binfmt * binfmt;
1362 struct inode * inode;
1367 binfmt = current->binfmt;
1368 if (!binfmt || !binfmt->core_dump)
1370 down_write(&mm->mmap_sem);
1371 if (!mm->dumpable) {
1372 up_write(&mm->mmap_sem);
1376 init_completion(&mm->core_done);
1377 current->signal->group_exit = 1;
1378 current->signal->group_exit_code = exit_code;
1381 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1384 format_corename(corename, core_pattern, signr);
1385 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1388 inode = file->f_dentry->d_inode;
1389 if (inode->i_nlink > 1)
1390 goto close_fail; /* multiple links - don't dump */
1391 if (d_unhashed(file->f_dentry))
1394 if (!S_ISREG(inode->i_mode))
1398 if (!file->f_op->write)
1400 if (do_truncate(file->f_dentry, 0) != 0)
1403 retval = binfmt->core_dump(signr, regs, file);
1405 current->signal->group_exit_code |= 0x80;
1407 filp_close(file, NULL);
1409 complete_all(&mm->core_done);