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/vs_memory.h>
51 #include <asm/uaccess.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 * vma->vm_mm->mmap_sem is held for writing.
298 void install_arg_page(struct vm_area_struct *vma,
299 struct page *page, unsigned long address)
301 struct mm_struct *mm = vma->vm_mm;
306 if (unlikely(anon_vma_prepare(vma)))
309 flush_dcache_page(page);
310 pgd = pgd_offset(mm, address);
312 spin_lock(&mm->page_table_lock);
313 pmd = pmd_alloc(mm, pgd, address);
316 pte = pte_alloc_map(mm, pmd, address);
319 if (!pte_none(*pte)) {
325 lru_cache_add_active(page);
326 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
327 page, vma->vm_page_prot))));
328 page_add_anon_rmap(page, vma, address);
330 spin_unlock(&mm->page_table_lock);
332 /* no need for flush_tlb */
335 spin_unlock(&mm->page_table_lock);
338 force_sig(SIGKILL, current);
341 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
343 unsigned long stack_base;
344 struct vm_area_struct *mpnt;
345 struct mm_struct *mm = current->mm;
349 #ifdef CONFIG_STACK_GROWSUP
350 /* Move the argument and environment strings to the bottom of the
356 /* Start by shifting all the pages down */
358 for (j = 0; j < MAX_ARG_PAGES; j++) {
359 struct page *page = bprm->page[j];
362 bprm->page[i++] = page;
365 /* Now move them within their pages */
366 offset = bprm->p % PAGE_SIZE;
367 to = kmap(bprm->page[0]);
368 for (j = 1; j < i; j++) {
369 memmove(to, to + offset, PAGE_SIZE - offset);
370 from = kmap(bprm->page[j]);
371 memcpy(to + PAGE_SIZE - offset, from, offset);
372 kunmap(bprm->page[j - 1]);
375 memmove(to, to + offset, PAGE_SIZE - offset);
376 kunmap(bprm->page[j - 1]);
378 /* Adjust bprm->p to point to the end of the strings. */
379 bprm->p = PAGE_SIZE * i - offset;
381 /* Limit stack size to 1GB */
382 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
383 if (stack_base > (1 << 30))
384 stack_base = 1 << 30;
385 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
387 mm->arg_start = stack_base;
388 arg_size = i << PAGE_SHIFT;
390 /* zero pages that were copied above */
391 while (i < MAX_ARG_PAGES)
392 bprm->page[i++] = NULL;
394 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
395 mm->arg_start = bprm->p + stack_base;
396 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
399 bprm->p += stack_base;
401 bprm->loader += stack_base;
402 bprm->exec += stack_base;
404 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
408 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
409 !vx_vmpages_avail(mm, 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_flags |= mm->def_flags;
437 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
438 insert_vm_struct(mm, mpnt);
439 // mm->stack_vm = mm->total_vm = vma_pages(mpnt);
440 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
441 mm->stack_vm = mm->total_vm;
444 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
445 struct page *page = bprm->page[i];
447 bprm->page[i] = NULL;
448 install_arg_page(mpnt, page, stack_base);
450 stack_base += PAGE_SIZE;
452 up_write(&mm->mmap_sem);
457 EXPORT_SYMBOL(setup_arg_pages);
459 #define free_arg_pages(bprm) do { } while (0)
463 static inline void free_arg_pages(struct linux_binprm *bprm)
467 for (i = 0; i < MAX_ARG_PAGES; i++) {
469 __free_page(bprm->page[i]);
470 bprm->page[i] = NULL;
474 #endif /* CONFIG_MMU */
476 struct file *open_exec(const char *name)
482 nd.intent.open.flags = FMODE_READ;
483 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
487 struct inode *inode = nd.dentry->d_inode;
488 file = ERR_PTR(-EACCES);
489 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
490 S_ISREG(inode->i_mode)) {
491 int err = permission(inode, MAY_EXEC, &nd);
492 if (!err && !(inode->i_mode & 0111))
496 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
498 err = deny_write_access(file);
513 EXPORT_SYMBOL(open_exec);
515 int kernel_read(struct file *file, unsigned long offset,
516 char *addr, unsigned long count)
524 /* The cast to a user pointer is valid due to the set_fs() */
525 result = vfs_read(file, (void __user *)addr, count, &pos);
530 EXPORT_SYMBOL(kernel_read);
532 static int exec_mmap(struct mm_struct *mm)
534 struct task_struct *tsk;
535 struct mm_struct * old_mm, *active_mm;
537 /* Add it to the list of mm's */
538 spin_lock(&mmlist_lock);
539 list_add(&mm->mmlist, &init_mm.mmlist);
541 spin_unlock(&mmlist_lock);
543 /* Notify parent that we're no longer interested in the old VM */
545 old_mm = current->mm;
546 mm_release(tsk, old_mm);
549 active_mm = tsk->active_mm;
552 activate_mm(active_mm, mm);
554 arch_pick_mmap_layout(mm);
556 if (active_mm != old_mm) BUG();
565 * This function makes sure the current process has its own signal table,
566 * so that flush_signal_handlers can later reset the handlers without
567 * disturbing other processes. (Other processes might share the signal
568 * table via the CLONE_SIGHAND option to clone().)
570 static inline int de_thread(struct task_struct *tsk)
572 struct signal_struct *newsig, *oldsig = tsk->signal;
573 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
574 spinlock_t *lock = &oldsighand->siglock;
578 * If we don't share sighandlers, then we aren't sharing anything
579 * and we can just re-use it all.
581 if (atomic_read(&oldsighand->count) <= 1)
584 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
588 spin_lock_init(&newsighand->siglock);
589 atomic_set(&newsighand->count, 1);
590 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
593 * See if we need to allocate a new signal structure
596 if (atomic_read(&oldsig->count) > 1) {
597 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
599 kmem_cache_free(sighand_cachep, newsighand);
602 atomic_set(&newsig->count, 1);
603 newsig->group_exit = 0;
604 newsig->group_exit_code = 0;
605 newsig->group_exit_task = NULL;
606 newsig->group_stop_count = 0;
607 newsig->curr_target = NULL;
608 init_sigpending(&newsig->shared_pending);
609 INIT_LIST_HEAD(&newsig->posix_timers);
611 newsig->tty = oldsig->tty;
612 newsig->pgrp = oldsig->pgrp;
613 newsig->session = oldsig->session;
614 newsig->leader = oldsig->leader;
615 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
618 if (thread_group_empty(current))
619 goto no_thread_group;
622 * Kill all other threads in the thread group.
623 * We must hold tasklist_lock to call zap_other_threads.
625 read_lock(&tasklist_lock);
627 if (oldsig->group_exit) {
629 * Another group action in progress, just
630 * return so that the signal is processed.
632 spin_unlock_irq(lock);
633 read_unlock(&tasklist_lock);
634 kmem_cache_free(sighand_cachep, newsighand);
636 kmem_cache_free(signal_cachep, newsig);
639 oldsig->group_exit = 1;
640 zap_other_threads(current);
641 read_unlock(&tasklist_lock);
644 * Account for the thread group leader hanging around:
647 if (current->pid == current->tgid)
649 while (atomic_read(&oldsig->count) > count) {
650 oldsig->group_exit_task = current;
651 oldsig->notify_count = count;
652 __set_current_state(TASK_UNINTERRUPTIBLE);
653 spin_unlock_irq(lock);
657 spin_unlock_irq(lock);
660 * At this point all other threads have exited, all we have to
661 * do is to wait for the thread group leader to become inactive,
662 * and to assume its PID:
664 if (current->pid != current->tgid) {
665 struct task_struct *leader = current->group_leader, *parent;
666 struct dentry *proc_dentry1, *proc_dentry2;
667 unsigned long state, ptrace;
670 * Wait for the thread group leader to be a zombie.
671 * It should already be zombie at this point, most
674 while (leader->state != TASK_ZOMBIE)
677 spin_lock(&leader->proc_lock);
678 spin_lock(¤t->proc_lock);
679 proc_dentry1 = proc_pid_unhash(current);
680 proc_dentry2 = proc_pid_unhash(leader);
681 write_lock_irq(&tasklist_lock);
683 if (leader->tgid != current->tgid)
685 if (current->pid == current->tgid)
688 * An exec() starts a new thread group with the
689 * TGID of the previous thread group. Rehash the
690 * two threads with a switched PID, and release
691 * the former thread group leader:
693 ptrace = leader->ptrace;
694 parent = leader->parent;
696 ptrace_unlink(current);
697 ptrace_unlink(leader);
698 remove_parent(current);
699 remove_parent(leader);
701 switch_exec_pids(leader, current);
703 current->parent = current->real_parent = leader->real_parent;
704 leader->parent = leader->real_parent = child_reaper;
705 current->group_leader = current;
706 leader->group_leader = leader;
708 add_parent(current, current->parent);
709 add_parent(leader, leader->parent);
711 current->ptrace = ptrace;
712 __ptrace_link(current, parent);
715 list_del(¤t->tasks);
716 list_add_tail(¤t->tasks, &init_task.tasks);
717 current->exit_signal = SIGCHLD;
718 state = leader->state;
720 write_unlock_irq(&tasklist_lock);
721 spin_unlock(&leader->proc_lock);
722 spin_unlock(¤t->proc_lock);
723 proc_pid_flush(proc_dentry1);
724 proc_pid_flush(proc_dentry2);
726 if (state != TASK_ZOMBIE)
728 release_task(leader);
733 write_lock_irq(&tasklist_lock);
734 spin_lock(&oldsighand->siglock);
735 spin_lock(&newsighand->siglock);
737 if (current == oldsig->curr_target)
738 oldsig->curr_target = next_thread(current);
740 current->signal = newsig;
741 current->sighand = newsighand;
742 init_sigpending(¤t->pending);
745 spin_unlock(&newsighand->siglock);
746 spin_unlock(&oldsighand->siglock);
747 write_unlock_irq(&tasklist_lock);
749 if (newsig && atomic_dec_and_test(&oldsig->count)) {
750 exit_itimers(oldsig);
751 kmem_cache_free(signal_cachep, oldsig);
754 if (atomic_dec_and_test(&oldsighand->count))
755 kmem_cache_free(sighand_cachep, oldsighand);
757 if (!thread_group_empty(current))
759 if (current->tgid != current->pid)
765 * These functions flushes out all traces of the currently running executable
766 * so that a new one can be started
769 static inline void flush_old_files(struct files_struct * files)
773 spin_lock(&files->file_lock);
775 unsigned long set, i;
779 if (i >= files->max_fds || i >= files->max_fdset)
781 set = files->close_on_exec->fds_bits[j];
784 files->close_on_exec->fds_bits[j] = 0;
785 spin_unlock(&files->file_lock);
786 for ( ; set ; i++,set >>= 1) {
791 spin_lock(&files->file_lock);
794 spin_unlock(&files->file_lock);
797 void get_task_comm(char *buf, struct task_struct *tsk)
799 /* buf must be at least sizeof(tsk->comm) in size */
801 memcpy(buf, tsk->comm, sizeof(tsk->comm));
805 void set_task_comm(struct task_struct *tsk, char *buf)
808 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
812 int flush_old_exec(struct linux_binprm * bprm)
816 struct files_struct *files;
817 char tcomm[sizeof(current->comm)];
820 * Make sure we have a private signal table and that
821 * we are unassociated from the previous thread group.
823 retval = de_thread(current);
828 * Make sure we have private file handles. Ask the
829 * fork helper to do the work for us and the exit
830 * helper to do the cleanup of the old one.
832 files = current->files; /* refcounted so safe to hold */
833 retval = unshare_files();
837 * Release all of the old mmap stuff
839 retval = exec_mmap(bprm->mm);
843 bprm->mm = NULL; /* We're using it now */
845 /* This is the point of no return */
847 put_files_struct(files);
849 current->sas_ss_sp = current->sas_ss_size = 0;
851 if (current->euid == current->uid && current->egid == current->gid)
852 current->mm->dumpable = 1;
853 name = bprm->filename;
854 for (i=0; (ch = *(name++)) != '\0';) {
858 if (i < (sizeof(tcomm) - 1))
862 set_task_comm(current, tcomm);
866 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
867 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
868 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP))
869 current->mm->dumpable = 0;
871 /* An exec changes our domain. We are no longer part of the thread
874 current->self_exec_id++;
876 flush_signal_handlers(current, 0);
877 flush_old_files(current->files);
882 put_files_struct(current->files);
883 current->files = files;
888 EXPORT_SYMBOL(flush_old_exec);
891 * Fill the binprm structure from the inode.
892 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
894 int prepare_binprm(struct linux_binprm *bprm)
897 struct inode * inode = bprm->file->f_dentry->d_inode;
900 mode = inode->i_mode;
902 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
903 * vfs_permission lets a non-executable through
905 if (!(mode & 0111)) /* with at least _one_ execute bit set */
907 if (bprm->file->f_op == NULL)
910 bprm->e_uid = current->euid;
911 bprm->e_gid = current->egid;
913 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
915 if (mode & S_ISUID) {
916 current->personality &= ~PER_CLEAR_ON_SETID;
917 bprm->e_uid = inode->i_uid;
922 * If setgid is set but no group execute bit then this
923 * is a candidate for mandatory locking, not a setgid
926 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
927 current->personality &= ~PER_CLEAR_ON_SETID;
928 bprm->e_gid = inode->i_gid;
932 /* fill in binprm security blob */
933 retval = security_bprm_set(bprm);
937 memset(bprm->buf,0,BINPRM_BUF_SIZE);
938 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
941 EXPORT_SYMBOL(prepare_binprm);
943 static inline int unsafe_exec(struct task_struct *p)
946 if (p->ptrace & PT_PTRACED) {
947 if (p->ptrace & PT_PTRACE_CAP)
948 unsafe |= LSM_UNSAFE_PTRACE_CAP;
950 unsafe |= LSM_UNSAFE_PTRACE;
952 if (atomic_read(&p->fs->count) > 1 ||
953 atomic_read(&p->files->count) > 1 ||
954 atomic_read(&p->sighand->count) > 1)
955 unsafe |= LSM_UNSAFE_SHARE;
960 void compute_creds(struct linux_binprm *bprm)
964 unsafe = unsafe_exec(current);
965 security_bprm_apply_creds(bprm, unsafe);
966 task_unlock(current);
969 EXPORT_SYMBOL(compute_creds);
971 void remove_arg_zero(struct linux_binprm *bprm)
974 unsigned long offset;
978 offset = bprm->p % PAGE_SIZE;
981 while (bprm->p++, *(kaddr+offset++)) {
982 if (offset != PAGE_SIZE)
985 kunmap_atomic(kaddr, KM_USER0);
987 page = bprm->page[bprm->p/PAGE_SIZE];
988 kaddr = kmap_atomic(page, KM_USER0);
990 kunmap_atomic(kaddr, KM_USER0);
995 EXPORT_SYMBOL(remove_arg_zero);
998 * cycle the list of binary formats handler, until one recognizes the image
1000 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1003 struct linux_binfmt *fmt;
1005 /* handle /sbin/loader.. */
1007 struct exec * eh = (struct exec *) bprm->buf;
1009 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1010 (eh->fh.f_flags & 0x3000) == 0x3000)
1013 unsigned long loader;
1015 allow_write_access(bprm->file);
1019 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1021 file = open_exec("/sbin/loader");
1022 retval = PTR_ERR(file);
1026 /* Remember if the application is TASO. */
1027 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1030 bprm->loader = loader;
1031 retval = prepare_binprm(bprm);
1034 /* should call search_binary_handler recursively here,
1035 but it does not matter */
1039 retval = security_bprm_check(bprm);
1043 /* kernel module loader fixup */
1044 /* so we don't try to load run modprobe in kernel space. */
1046 for (try=0; try<2; try++) {
1047 read_lock(&binfmt_lock);
1048 for (fmt = formats ; fmt ; fmt = fmt->next) {
1049 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1052 if (!try_module_get(fmt->module))
1054 read_unlock(&binfmt_lock);
1055 retval = fn(bprm, regs);
1058 allow_write_access(bprm->file);
1062 current->did_exec = 1;
1065 read_lock(&binfmt_lock);
1067 if (retval != -ENOEXEC || bprm->mm == NULL)
1070 read_unlock(&binfmt_lock);
1074 read_unlock(&binfmt_lock);
1075 if (retval != -ENOEXEC || bprm->mm == NULL) {
1079 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1080 if (printable(bprm->buf[0]) &&
1081 printable(bprm->buf[1]) &&
1082 printable(bprm->buf[2]) &&
1083 printable(bprm->buf[3]))
1084 break; /* -ENOEXEC */
1085 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1092 EXPORT_SYMBOL(search_binary_handler);
1095 * sys_execve() executes a new program.
1097 int do_execve(char * filename,
1098 char __user *__user *argv,
1099 char __user *__user *envp,
1100 struct pt_regs * regs)
1102 struct linux_binprm *bprm;
1107 file = open_exec(filename);
1109 retval = PTR_ERR(file);
1116 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1119 memset(bprm, 0, sizeof(*bprm));
1121 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1124 bprm->filename = filename;
1125 bprm->interp = filename;
1126 bprm->mm = mm_alloc();
1130 retval = init_new_context(current, bprm->mm);
1134 bprm->argc = count(argv, bprm->p / sizeof(void *));
1135 if ((retval = bprm->argc) < 0)
1138 bprm->envc = count(envp, bprm->p / sizeof(void *));
1139 if ((retval = bprm->envc) < 0)
1142 retval = security_bprm_alloc(bprm);
1146 retval = prepare_binprm(bprm);
1150 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1154 bprm->exec = bprm->p;
1155 retval = copy_strings(bprm->envc, envp, bprm);
1159 retval = copy_strings(bprm->argc, argv, bprm);
1163 retval = search_binary_handler(bprm,regs);
1165 free_arg_pages(bprm);
1167 /* execve success */
1168 security_bprm_free(bprm);
1174 /* Something went wrong, return the inode and free the argument pages*/
1175 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1176 struct page * page = bprm->page[i];
1182 security_bprm_free(bprm);
1190 allow_write_access(bprm->file);
1199 EXPORT_SYMBOL(do_execve);
1201 int set_binfmt(struct linux_binfmt *new)
1203 struct linux_binfmt *old = current->binfmt;
1206 if (!try_module_get(new->module))
1209 current->binfmt = new;
1211 module_put(old->module);
1215 EXPORT_SYMBOL(set_binfmt);
1217 #define CORENAME_MAX_SIZE 64
1219 /* format_corename will inspect the pattern parameter, and output a
1220 * name into corename, which must have space for at least
1221 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1223 static void format_corename(char *corename, const char *pattern, long signr)
1225 const char *pat_ptr = pattern;
1226 char *out_ptr = corename;
1227 char *const out_end = corename + CORENAME_MAX_SIZE;
1229 int pid_in_pattern = 0;
1231 /* Repeat as long as we have more pattern to process and more output
1234 if (*pat_ptr != '%') {
1235 if (out_ptr == out_end)
1237 *out_ptr++ = *pat_ptr++;
1239 switch (*++pat_ptr) {
1242 /* Double percent, output one percent */
1244 if (out_ptr == out_end)
1251 rc = snprintf(out_ptr, out_end - out_ptr,
1252 "%d", current->tgid);
1253 if (rc > out_end - out_ptr)
1259 rc = snprintf(out_ptr, out_end - out_ptr,
1260 "%d", current->uid);
1261 if (rc > out_end - out_ptr)
1267 rc = snprintf(out_ptr, out_end - out_ptr,
1268 "%d", current->gid);
1269 if (rc > out_end - out_ptr)
1273 /* signal that caused the coredump */
1275 rc = snprintf(out_ptr, out_end - out_ptr,
1277 if (rc > out_end - out_ptr)
1281 /* UNIX time of coredump */
1284 do_gettimeofday(&tv);
1285 rc = snprintf(out_ptr, out_end - out_ptr,
1287 if (rc > out_end - out_ptr)
1294 down_read(&uts_sem);
1295 rc = snprintf(out_ptr, out_end - out_ptr,
1296 "%s", system_utsname.nodename);
1298 if (rc > out_end - out_ptr)
1304 rc = snprintf(out_ptr, out_end - out_ptr,
1305 "%s", current->comm);
1306 if (rc > out_end - out_ptr)
1316 /* Backward compatibility with core_uses_pid:
1318 * If core_pattern does not include a %p (as is the default)
1319 * and core_uses_pid is set, then .%pid will be appended to
1322 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1323 rc = snprintf(out_ptr, out_end - out_ptr,
1324 ".%d", current->tgid);
1325 if (rc > out_end - out_ptr)
1333 static void zap_threads (struct mm_struct *mm)
1335 struct task_struct *g, *p;
1336 struct task_struct *tsk = current;
1337 struct completion *vfork_done = tsk->vfork_done;
1340 * Make sure nobody is waiting for us to release the VM,
1341 * otherwise we can deadlock when we wait on each other
1344 tsk->vfork_done = NULL;
1345 complete(vfork_done);
1348 read_lock(&tasklist_lock);
1350 if (mm == p->mm && p != tsk) {
1351 force_sig_specific(SIGKILL, p);
1354 while_each_thread(g,p);
1356 read_unlock(&tasklist_lock);
1359 static void coredump_wait(struct mm_struct *mm)
1361 DECLARE_COMPLETION(startup_done);
1363 mm->core_waiters++; /* let other threads block */
1364 mm->core_startup_done = &startup_done;
1366 /* give other threads a chance to run: */
1370 if (--mm->core_waiters) {
1371 up_write(&mm->mmap_sem);
1372 wait_for_completion(&startup_done);
1374 up_write(&mm->mmap_sem);
1375 BUG_ON(mm->core_waiters);
1378 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1380 char corename[CORENAME_MAX_SIZE + 1];
1381 struct mm_struct *mm = current->mm;
1382 struct linux_binfmt * binfmt;
1383 struct inode * inode;
1387 binfmt = current->binfmt;
1388 if (!binfmt || !binfmt->core_dump)
1390 down_write(&mm->mmap_sem);
1391 if (!mm->dumpable) {
1392 up_write(&mm->mmap_sem);
1396 init_completion(&mm->core_done);
1397 current->signal->group_exit = 1;
1398 current->signal->group_exit_code = exit_code;
1401 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1405 * lock_kernel() because format_corename() is controlled by sysctl, which
1406 * uses lock_kernel()
1409 format_corename(corename, core_pattern, signr);
1411 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1414 inode = file->f_dentry->d_inode;
1415 if (inode->i_nlink > 1)
1416 goto close_fail; /* multiple links - don't dump */
1417 if (d_unhashed(file->f_dentry))
1420 if (!S_ISREG(inode->i_mode))
1424 if (!file->f_op->write)
1426 if (do_truncate(file->f_dentry, 0) != 0)
1429 retval = binfmt->core_dump(signr, regs, file);
1431 current->signal->group_exit_code |= 0x80;
1433 filp_close(file, NULL);
1435 complete_all(&mm->core_done);