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/vs_memory.h>
51 #include <linux/ckrm_mem.h>
53 #include <asm/uaccess.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)) {
327 lru_cache_add_active(page);
328 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
329 page, vma->vm_page_prot))));
330 page_add_anon_rmap(page, vma, address);
332 spin_unlock(&mm->page_table_lock);
334 /* no need for flush_tlb */
337 spin_unlock(&mm->page_table_lock);
340 force_sig(SIGKILL, current);
343 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
345 unsigned long stack_base;
346 struct vm_area_struct *mpnt;
347 struct mm_struct *mm = current->mm;
351 #ifdef CONFIG_STACK_GROWSUP
352 /* Move the argument and environment strings to the bottom of the
358 /* Start by shifting all the pages down */
360 for (j = 0; j < MAX_ARG_PAGES; j++) {
361 struct page *page = bprm->page[j];
364 bprm->page[i++] = page;
367 /* Now move them within their pages */
368 offset = bprm->p % PAGE_SIZE;
369 to = kmap(bprm->page[0]);
370 for (j = 1; j < i; j++) {
371 memmove(to, to + offset, PAGE_SIZE - offset);
372 from = kmap(bprm->page[j]);
373 memcpy(to + PAGE_SIZE - offset, from, offset);
374 kunmap(bprm->page[j - 1]);
377 memmove(to, to + offset, PAGE_SIZE - offset);
378 kunmap(bprm->page[j - 1]);
380 /* Adjust bprm->p to point to the end of the strings. */
381 bprm->p = PAGE_SIZE * i - offset;
383 /* Limit stack size to 1GB */
384 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
385 if (stack_base > (1 << 30))
386 stack_base = 1 << 30;
387 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
389 mm->arg_start = stack_base;
390 arg_size = i << PAGE_SHIFT;
392 /* zero pages that were copied above */
393 while (i < MAX_ARG_PAGES)
394 bprm->page[i++] = NULL;
396 #ifdef __HAVE_ARCH_ALIGN_STACK
397 stack_base = arch_align_stack(STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE);
398 stack_base = PAGE_ALIGN(stack_base);
400 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
402 mm->arg_start = bprm->p + stack_base;
403 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
406 bprm->p += stack_base;
408 bprm->loader += stack_base;
409 bprm->exec += stack_base;
411 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
415 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
416 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
417 kmem_cache_free(vm_area_cachep, mpnt);
421 memset(mpnt, 0, sizeof(*mpnt));
423 down_write(&mm->mmap_sem);
426 #ifdef CONFIG_STACK_GROWSUP
427 mpnt->vm_start = stack_base;
428 mpnt->vm_end = PAGE_MASK &
429 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
431 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
432 mpnt->vm_end = STACK_TOP;
434 /* Adjust stack execute permissions; explicitly enable
435 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
436 * and leave alone (arch default) otherwise. */
437 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
438 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
439 else if (executable_stack == EXSTACK_DISABLE_X)
440 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
442 mpnt->vm_flags = VM_STACK_FLAGS;
443 mpnt->vm_flags |= mm->def_flags;
444 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
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 install_arg_page(mpnt, page, stack_base);
457 stack_base += PAGE_SIZE;
459 up_write(&mm->mmap_sem);
464 EXPORT_SYMBOL(setup_arg_pages);
466 #define free_arg_pages(bprm) do { } while (0)
470 static inline void free_arg_pages(struct linux_binprm *bprm)
474 for (i = 0; i < MAX_ARG_PAGES; i++) {
476 __free_page(bprm->page[i]);
477 bprm->page[i] = NULL;
481 #endif /* CONFIG_MMU */
483 struct file *open_exec(const char *name)
489 nd.intent.open.flags = FMODE_READ;
490 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
494 struct inode *inode = nd.dentry->d_inode;
495 file = ERR_PTR(-EACCES);
496 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
497 S_ISREG(inode->i_mode)) {
498 int err = permission(inode, MAY_EXEC, &nd);
499 if (!err && !(inode->i_mode & 0111))
503 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
505 err = deny_write_access(file);
520 EXPORT_SYMBOL(open_exec);
522 int kernel_read(struct file *file, unsigned long offset,
523 char *addr, unsigned long count)
531 /* The cast to a user pointer is valid due to the set_fs() */
532 result = vfs_read(file, (void __user *)addr, count, &pos);
537 EXPORT_SYMBOL(kernel_read);
539 static int exec_mmap(struct mm_struct *mm)
541 struct task_struct *tsk;
542 struct mm_struct * old_mm, *active_mm;
544 /* Add it to the list of mm's */
545 spin_lock(&mmlist_lock);
546 list_add(&mm->mmlist, &init_mm.mmlist);
548 spin_unlock(&mmlist_lock);
550 /* Notify parent that we're no longer interested in the old VM */
552 old_mm = current->mm;
553 mm_release(tsk, old_mm);
556 active_mm = tsk->active_mm;
559 activate_mm(active_mm, mm);
561 arch_pick_mmap_layout(mm);
562 #ifdef CONFIG_CKRM_RES_MEM
564 spin_lock(&old_mm->peertask_lock);
565 list_del(&tsk->mm_peers);
566 ckrm_mem_evaluate_mm(old_mm);
567 spin_unlock(&old_mm->peertask_lock);
569 spin_lock(&mm->peertask_lock);
570 list_add_tail(&tsk->mm_peers, &mm->tasklist);
571 ckrm_mem_evaluate_mm(mm);
572 spin_unlock(&mm->peertask_lock);
575 if (active_mm != old_mm) BUG();
584 * This function makes sure the current process has its own signal table,
585 * so that flush_signal_handlers can later reset the handlers without
586 * disturbing other processes. (Other processes might share the signal
587 * table via the CLONE_SIGHAND option to clone().)
589 static inline int de_thread(struct task_struct *tsk)
591 struct signal_struct *newsig, *oldsig = tsk->signal;
592 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
593 spinlock_t *lock = &oldsighand->siglock;
597 * If we don't share sighandlers, then we aren't sharing anything
598 * and we can just re-use it all.
600 if (atomic_read(&oldsighand->count) <= 1)
603 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
607 spin_lock_init(&newsighand->siglock);
608 atomic_set(&newsighand->count, 1);
609 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
612 * See if we need to allocate a new signal structure
615 if (atomic_read(&oldsig->count) > 1) {
616 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
618 kmem_cache_free(sighand_cachep, newsighand);
621 atomic_set(&newsig->count, 1);
622 newsig->group_exit = 0;
623 newsig->group_exit_code = 0;
624 newsig->group_exit_task = NULL;
625 newsig->group_stop_count = 0;
626 newsig->curr_target = NULL;
627 init_sigpending(&newsig->shared_pending);
628 INIT_LIST_HEAD(&newsig->posix_timers);
630 newsig->tty = oldsig->tty;
631 newsig->pgrp = oldsig->pgrp;
632 newsig->session = oldsig->session;
633 newsig->leader = oldsig->leader;
634 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
637 if (thread_group_empty(current))
638 goto no_thread_group;
641 * Kill all other threads in the thread group.
642 * We must hold tasklist_lock to call zap_other_threads.
644 read_lock(&tasklist_lock);
646 if (oldsig->group_exit) {
648 * Another group action in progress, just
649 * return so that the signal is processed.
651 spin_unlock_irq(lock);
652 read_unlock(&tasklist_lock);
653 kmem_cache_free(sighand_cachep, newsighand);
655 kmem_cache_free(signal_cachep, newsig);
658 oldsig->group_exit = 1;
659 zap_other_threads(current);
660 read_unlock(&tasklist_lock);
663 * Account for the thread group leader hanging around:
666 if (current->pid == current->tgid)
668 while (atomic_read(&oldsig->count) > count) {
669 oldsig->group_exit_task = current;
670 oldsig->notify_count = count;
671 __set_current_state(TASK_UNINTERRUPTIBLE);
672 spin_unlock_irq(lock);
676 spin_unlock_irq(lock);
679 * At this point all other threads have exited, all we have to
680 * do is to wait for the thread group leader to become inactive,
681 * and to assume its PID:
683 if (current->pid != current->tgid) {
684 struct task_struct *leader = current->group_leader, *parent;
685 struct dentry *proc_dentry1, *proc_dentry2;
686 unsigned long state, ptrace;
689 * Wait for the thread group leader to be a zombie.
690 * It should already be zombie at this point, most
693 while (leader->state != TASK_ZOMBIE)
696 spin_lock(&leader->proc_lock);
697 spin_lock(¤t->proc_lock);
698 proc_dentry1 = proc_pid_unhash(current);
699 proc_dentry2 = proc_pid_unhash(leader);
700 write_lock_irq(&tasklist_lock);
702 if (leader->tgid != current->tgid)
704 if (current->pid == current->tgid)
707 * An exec() starts a new thread group with the
708 * TGID of the previous thread group. Rehash the
709 * two threads with a switched PID, and release
710 * the former thread group leader:
712 ptrace = leader->ptrace;
713 parent = leader->parent;
715 ptrace_unlink(current);
716 ptrace_unlink(leader);
717 remove_parent(current);
718 remove_parent(leader);
720 switch_exec_pids(leader, current);
722 current->parent = current->real_parent = leader->real_parent;
723 leader->parent = leader->real_parent = child_reaper;
724 current->group_leader = current;
725 leader->group_leader = leader;
727 add_parent(current, current->parent);
728 add_parent(leader, leader->parent);
730 current->ptrace = ptrace;
731 __ptrace_link(current, parent);
734 list_del(¤t->tasks);
735 list_add_tail(¤t->tasks, &init_task.tasks);
736 current->exit_signal = SIGCHLD;
737 state = leader->state;
739 write_unlock_irq(&tasklist_lock);
740 spin_unlock(&leader->proc_lock);
741 spin_unlock(¤t->proc_lock);
742 proc_pid_flush(proc_dentry1);
743 proc_pid_flush(proc_dentry2);
745 if (state != TASK_ZOMBIE)
747 release_task(leader);
752 write_lock_irq(&tasklist_lock);
753 spin_lock(&oldsighand->siglock);
754 spin_lock(&newsighand->siglock);
756 if (current == oldsig->curr_target)
757 oldsig->curr_target = next_thread(current);
759 current->signal = newsig;
760 current->sighand = newsighand;
761 init_sigpending(¤t->pending);
764 spin_unlock(&newsighand->siglock);
765 spin_unlock(&oldsighand->siglock);
766 write_unlock_irq(&tasklist_lock);
768 if (newsig && atomic_dec_and_test(&oldsig->count))
769 kmem_cache_free(signal_cachep, oldsig);
771 if (atomic_dec_and_test(&oldsighand->count))
772 kmem_cache_free(sighand_cachep, oldsighand);
774 if (!thread_group_empty(current))
776 if (current->tgid != current->pid)
782 * These functions flushes out all traces of the currently running executable
783 * so that a new one can be started
786 static inline void flush_old_files(struct files_struct * files)
790 spin_lock(&files->file_lock);
792 unsigned long set, i;
796 if (i >= files->max_fds || i >= files->max_fdset)
798 set = files->close_on_exec->fds_bits[j];
801 files->close_on_exec->fds_bits[j] = 0;
802 spin_unlock(&files->file_lock);
803 for ( ; set ; i++,set >>= 1) {
808 spin_lock(&files->file_lock);
811 spin_unlock(&files->file_lock);
814 int flush_old_exec(struct linux_binprm * bprm)
818 struct files_struct *files;
821 * Make sure we have a private signal table and that
822 * we are unassociated from the previous thread group.
824 retval = de_thread(current);
829 * Make sure we have private file handles. Ask the
830 * fork helper to do the work for us and the exit
831 * helper to do the cleanup of the old one.
833 files = current->files; /* refcounted so safe to hold */
834 retval = unshare_files();
838 * Release all of the old mmap stuff
840 retval = exec_mmap(bprm->mm);
844 bprm->mm = NULL; /* We're using it now */
846 /* This is the point of no return */
848 put_files_struct(files);
850 current->sas_ss_sp = current->sas_ss_size = 0;
852 if (current->euid == current->uid && current->egid == current->gid)
853 current->mm->dumpable = 1;
854 name = bprm->filename;
855 for (i=0; (ch = *(name++)) != '\0';) {
860 current->comm[i++] = ch;
862 current->comm[i] = '\0';
864 current->flags &= ~PF_RELOCEXEC;
867 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
868 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
869 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP))
870 current->mm->dumpable = 0;
872 /* An exec changes our domain. We are no longer part of the thread
875 current->self_exec_id++;
877 flush_signal_handlers(current, 0);
878 flush_old_files(current->files);
883 put_files_struct(current->files);
884 current->files = files;
889 EXPORT_SYMBOL(flush_old_exec);
892 * Fill the binprm structure from the inode.
893 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
895 int prepare_binprm(struct linux_binprm *bprm)
898 struct inode * inode = bprm->file->f_dentry->d_inode;
901 mode = inode->i_mode;
903 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
904 * vfs_permission lets a non-executable through
906 if (!(mode & 0111)) /* with at least _one_ execute bit set */
908 if (bprm->file->f_op == NULL)
911 bprm->e_uid = current->euid;
912 bprm->e_gid = current->egid;
914 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
916 if (mode & S_ISUID) {
917 current->personality &= ~PER_CLEAR_ON_SETID;
918 bprm->e_uid = inode->i_uid;
923 * If setgid is set but no group execute bit then this
924 * is a candidate for mandatory locking, not a setgid
927 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
928 current->personality &= ~PER_CLEAR_ON_SETID;
929 bprm->e_gid = inode->i_gid;
933 /* fill in binprm security blob */
934 retval = security_bprm_set(bprm);
938 memset(bprm->buf,0,BINPRM_BUF_SIZE);
939 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
942 EXPORT_SYMBOL(prepare_binprm);
944 static inline int unsafe_exec(struct task_struct *p)
947 if (p->ptrace & PT_PTRACED) {
948 if (p->ptrace & PT_PTRACE_CAP)
949 unsafe |= LSM_UNSAFE_PTRACE_CAP;
951 unsafe |= LSM_UNSAFE_PTRACE;
953 if (atomic_read(&p->fs->count) > 1 ||
954 atomic_read(&p->files->count) > 1 ||
955 atomic_read(&p->sighand->count) > 1)
956 unsafe |= LSM_UNSAFE_SHARE;
961 void compute_creds(struct linux_binprm *bprm)
965 unsafe = unsafe_exec(current);
966 security_bprm_apply_creds(bprm, unsafe);
967 task_unlock(current);
970 EXPORT_SYMBOL(compute_creds);
972 void remove_arg_zero(struct linux_binprm *bprm)
975 unsigned long offset;
979 offset = bprm->p % PAGE_SIZE;
982 while (bprm->p++, *(kaddr+offset++)) {
983 if (offset != PAGE_SIZE)
986 kunmap_atomic(kaddr, KM_USER0);
988 page = bprm->page[bprm->p/PAGE_SIZE];
989 kaddr = kmap_atomic(page, KM_USER0);
991 kunmap_atomic(kaddr, KM_USER0);
996 EXPORT_SYMBOL(remove_arg_zero);
999 * cycle the list of binary formats handler, until one recognizes the image
1001 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1004 struct linux_binfmt *fmt;
1006 /* handle /sbin/loader.. */
1008 struct exec * eh = (struct exec *) bprm->buf;
1010 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1011 (eh->fh.f_flags & 0x3000) == 0x3000)
1014 unsigned long loader;
1016 allow_write_access(bprm->file);
1020 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1022 file = open_exec("/sbin/loader");
1023 retval = PTR_ERR(file);
1027 /* Remember if the application is TASO. */
1028 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1031 bprm->loader = loader;
1032 retval = prepare_binprm(bprm);
1035 /* should call search_binary_handler recursively here,
1036 but it does not matter */
1040 retval = security_bprm_check(bprm);
1044 /* kernel module loader fixup */
1045 /* so we don't try to load run modprobe in kernel space. */
1047 for (try=0; try<2; try++) {
1048 read_lock(&binfmt_lock);
1049 for (fmt = formats ; fmt ; fmt = fmt->next) {
1050 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1053 if (!try_module_get(fmt->module))
1055 read_unlock(&binfmt_lock);
1056 retval = fn(bprm, regs);
1059 allow_write_access(bprm->file);
1063 current->did_exec = 1;
1064 ckrm_cb_exec(bprm->filename);
1067 read_lock(&binfmt_lock);
1069 if (retval != -ENOEXEC || bprm->mm == NULL)
1072 read_unlock(&binfmt_lock);
1076 read_unlock(&binfmt_lock);
1077 if (retval != -ENOEXEC || bprm->mm == NULL) {
1081 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1082 if (printable(bprm->buf[0]) &&
1083 printable(bprm->buf[1]) &&
1084 printable(bprm->buf[2]) &&
1085 printable(bprm->buf[3]))
1086 break; /* -ENOEXEC */
1087 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1094 EXPORT_SYMBOL(search_binary_handler);
1097 * sys_execve() executes a new program.
1099 int do_execve(char * filename,
1100 char __user *__user *argv,
1101 char __user *__user *envp,
1102 struct pt_regs * regs)
1104 struct linux_binprm bprm;
1109 file = open_exec(filename);
1111 retval = PTR_ERR(file);
1115 sched_balance_exec();
1117 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1118 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1121 bprm.filename = filename;
1122 bprm.interp = filename;
1123 bprm.interp_flags = 0;
1124 bprm.interp_data = 0;
1128 bprm.security = NULL;
1129 bprm.mm = mm_alloc();
1134 retval = init_new_context(current, bprm.mm);
1138 bprm.argc = count(argv, bprm.p / sizeof(void *));
1139 if ((retval = bprm.argc) < 0)
1142 bprm.envc = count(envp, bprm.p / sizeof(void *));
1143 if ((retval = bprm.envc) < 0)
1146 retval = security_bprm_alloc(&bprm);
1150 retval = prepare_binprm(&bprm);
1154 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1159 retval = copy_strings(bprm.envc, envp, &bprm);
1163 retval = copy_strings(bprm.argc, argv, &bprm);
1167 retval = search_binary_handler(&bprm,regs);
1169 free_arg_pages(&bprm);
1171 /* execve success */
1172 security_bprm_free(&bprm);
1177 /* Something went wrong, return the inode and free the argument pages*/
1178 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1179 struct page * page = bprm.page[i];
1185 security_bprm_free(&bprm);
1193 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 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;
1388 binfmt = current->binfmt;
1389 if (!binfmt || !binfmt->core_dump)
1391 down_write(&mm->mmap_sem);
1392 if (!mm->dumpable) {
1393 up_write(&mm->mmap_sem);
1397 init_completion(&mm->core_done);
1398 current->signal->group_exit = 1;
1399 current->signal->group_exit_code = exit_code;
1402 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1405 format_corename(corename, core_pattern, signr);
1406 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1409 inode = file->f_dentry->d_inode;
1410 if (inode->i_nlink > 1)
1411 goto close_fail; /* multiple links - don't dump */
1412 if (d_unhashed(file->f_dentry))
1415 if (!S_ISREG(inode->i_mode))
1419 if (!file->f_op->write)
1421 if (do_truncate(file->f_dentry, 0) != 0)
1424 retval = binfmt->core_dump(signr, regs, file);
1426 current->signal->group_exit_code |= 0x80;
1428 filp_close(file, NULL);
1430 complete_all(&mm->core_done);