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/key.h>
38 #include <linux/personality.h>
39 #include <linux/binfmts.h>
40 #include <linux/swap.h>
41 #include <linux/utsname.h>
42 #include <linux/module.h>
43 #include <linux/namei.h>
44 #include <linux/proc_fs.h>
45 #include <linux/ptrace.h>
46 #include <linux/mount.h>
47 #include <linux/security.h>
48 #include <linux/syscalls.h>
49 #include <linux/rmap.h>
50 #include <linux/ckrm.h>
51 #include <linux/ckrm_mem.h>
52 #include <linux/vs_memory.h>
54 #include <asm/uaccess.h>
55 #include <asm/mmu_context.h>
58 #include <linux/kmod.h>
62 char core_pattern[65] = "core";
63 int suid_dumpable = 0;
65 EXPORT_SYMBOL(suid_dumpable);
66 /* The maximal length of core_pattern is also specified in sysctl.c */
68 static struct linux_binfmt *formats;
69 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
71 int register_binfmt(struct linux_binfmt * fmt)
73 struct linux_binfmt ** tmp = &formats;
79 write_lock(&binfmt_lock);
82 write_unlock(&binfmt_lock);
89 write_unlock(&binfmt_lock);
93 EXPORT_SYMBOL(register_binfmt);
95 int unregister_binfmt(struct linux_binfmt * fmt)
97 struct linux_binfmt ** tmp = &formats;
99 write_lock(&binfmt_lock);
103 write_unlock(&binfmt_lock);
108 write_unlock(&binfmt_lock);
112 EXPORT_SYMBOL(unregister_binfmt);
114 static inline void put_binfmt(struct linux_binfmt * fmt)
116 module_put(fmt->module);
120 * Note that a shared library must be both readable and executable due to
123 * Also note that we take the address to load from from the file itself.
125 asmlinkage long sys_uselib(const char __user * library)
131 nd.intent.open.flags = FMODE_READ;
132 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
137 if (!S_ISREG(nd.dentry->d_inode->i_mode))
140 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
144 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
145 error = PTR_ERR(file);
151 struct linux_binfmt * fmt;
153 read_lock(&binfmt_lock);
154 for (fmt = formats ; fmt ; fmt = fmt->next) {
155 if (!fmt->load_shlib)
157 if (!try_module_get(fmt->module))
159 read_unlock(&binfmt_lock);
160 error = fmt->load_shlib(file);
161 read_lock(&binfmt_lock);
163 if (error != -ENOEXEC)
166 read_unlock(&binfmt_lock);
177 * count() counts the number of strings in array ARGV.
179 static int count(char __user * __user * argv, int max)
187 if (get_user(p, argv))
200 * 'copy_strings()' copies argument/environment strings from user
201 * memory to free pages in kernel mem. These are in a format ready
202 * to be put directly into the top of new user memory.
204 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
206 struct page *kmapped_page = NULL;
215 if (get_user(str, argv+argc) ||
216 !(len = strnlen_user(str, bprm->p))) {
227 /* XXX: add architecture specific overflow check here. */
232 int offset, bytes_to_copy;
235 offset = pos % PAGE_SIZE;
237 page = bprm->page[i];
240 page = alloc_page(GFP_HIGHUSER);
241 bprm->page[i] = page;
249 if (page != kmapped_page) {
251 kunmap(kmapped_page);
253 kaddr = kmap(kmapped_page);
256 memset(kaddr, 0, offset);
257 bytes_to_copy = PAGE_SIZE - offset;
258 if (bytes_to_copy > len) {
261 memset(kaddr+offset+len, 0,
262 PAGE_SIZE-offset-len);
264 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
270 pos += bytes_to_copy;
271 str += bytes_to_copy;
272 len -= bytes_to_copy;
278 kunmap(kmapped_page);
283 * Like copy_strings, but get argv and its values from kernel memory.
285 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
288 mm_segment_t oldfs = get_fs();
290 r = copy_strings(argc, (char __user * __user *)argv, bprm);
295 EXPORT_SYMBOL(copy_strings_kernel);
299 * This routine is used to map in a page into an address space: needed by
300 * execve() for the initial stack and environment pages.
302 * vma->vm_mm->mmap_sem is held for writing.
304 void install_arg_page(struct vm_area_struct *vma,
305 struct page *page, unsigned long address)
307 struct mm_struct *mm = vma->vm_mm;
312 if (unlikely(anon_vma_prepare(vma)))
315 flush_dcache_page(page);
316 pgd = pgd_offset(mm, address);
318 spin_lock(&mm->page_table_lock);
319 pmd = pmd_alloc(mm, pgd, address);
322 pte = pte_alloc_map(mm, pmd, address);
325 if (!pte_none(*pte)) {
331 lru_cache_add_active(page);
332 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
333 page, vma->vm_page_prot))));
334 page_add_anon_rmap(page, vma, address);
336 spin_unlock(&mm->page_table_lock);
338 /* no need for flush_tlb */
341 spin_unlock(&mm->page_table_lock);
344 force_sig(SIGKILL, current);
347 #define EXTRA_STACK_VM_PAGES 20 /* random */
349 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
351 unsigned long stack_base;
352 struct vm_area_struct *mpnt;
353 struct mm_struct *mm = current->mm;
357 #ifdef CONFIG_STACK_GROWSUP
358 /* Move the argument and environment strings to the bottom of the
364 /* Start by shifting all the pages down */
366 for (j = 0; j < MAX_ARG_PAGES; j++) {
367 struct page *page = bprm->page[j];
370 bprm->page[i++] = page;
373 /* Now move them within their pages */
374 offset = bprm->p % PAGE_SIZE;
375 to = kmap(bprm->page[0]);
376 for (j = 1; j < i; j++) {
377 memmove(to, to + offset, PAGE_SIZE - offset);
378 from = kmap(bprm->page[j]);
379 memcpy(to + PAGE_SIZE - offset, from, offset);
380 kunmap(bprm->page[j - 1]);
383 memmove(to, to + offset, PAGE_SIZE - offset);
384 kunmap(bprm->page[j - 1]);
386 /* Limit stack size to 1GB */
387 stack_base = current->signal->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 /* Adjust bprm->p to point to the end of the strings. */
393 bprm->p = stack_base + PAGE_SIZE * i - offset;
395 mm->arg_start = stack_base;
396 arg_size = i << PAGE_SHIFT;
398 /* zero pages that were copied above */
399 while (i < MAX_ARG_PAGES)
400 bprm->page[i++] = NULL;
402 #ifdef __HAVE_ARCH_ALIGN_STACK
403 stack_base = arch_align_stack(STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE);
404 stack_base = PAGE_ALIGN(stack_base);
406 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
408 bprm->p += stack_base;
409 mm->arg_start = bprm->p;
410 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
413 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
416 bprm->loader += stack_base;
417 bprm->exec += stack_base;
419 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
423 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
424 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
425 kmem_cache_free(vm_area_cachep, mpnt);
429 memset(mpnt, 0, sizeof(*mpnt));
431 down_write(&mm->mmap_sem);
434 #ifdef CONFIG_STACK_GROWSUP
435 mpnt->vm_start = stack_base;
436 mpnt->vm_end = stack_base + arg_size;
438 mpnt->vm_end = STACK_TOP;
439 mpnt->vm_start = mpnt->vm_end - arg_size;
441 /* Adjust stack execute permissions; explicitly enable
442 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
443 * and leave alone (arch default) otherwise. */
444 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
445 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
446 else if (executable_stack == EXSTACK_DISABLE_X)
447 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
449 mpnt->vm_flags = VM_STACK_FLAGS;
450 mpnt->vm_flags |= mm->def_flags;
451 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
452 if ((ret = insert_vm_struct(mm, mpnt))) {
453 up_write(&mm->mmap_sem);
454 kmem_cache_free(vm_area_cachep, mpnt);
457 // mm->stack_vm = mm->total_vm = vma_pages(mpnt);
458 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
459 mm->stack_vm = mm->total_vm;
462 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
463 struct page *page = bprm->page[i];
465 bprm->page[i] = NULL;
466 install_arg_page(mpnt, page, stack_base);
468 stack_base += PAGE_SIZE;
470 up_write(&mm->mmap_sem);
475 EXPORT_SYMBOL(setup_arg_pages);
477 #define free_arg_pages(bprm) do { } while (0)
481 static inline void free_arg_pages(struct linux_binprm *bprm)
485 for (i = 0; i < MAX_ARG_PAGES; i++) {
487 __free_page(bprm->page[i]);
488 bprm->page[i] = NULL;
492 #endif /* CONFIG_MMU */
494 struct file *open_exec(const char *name)
500 nd.intent.open.flags = FMODE_READ;
501 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
505 struct inode *inode = nd.dentry->d_inode;
506 file = ERR_PTR(-EACCES);
507 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
508 S_ISREG(inode->i_mode)) {
509 int err = permission(inode, MAY_EXEC, &nd);
510 if (!err && !(inode->i_mode & 0111))
514 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
516 err = deny_write_access(file);
531 EXPORT_SYMBOL(open_exec);
533 int kernel_read(struct file *file, unsigned long offset,
534 char *addr, unsigned long count)
542 /* The cast to a user pointer is valid due to the set_fs() */
543 result = vfs_read(file, (void __user *)addr, count, &pos);
548 EXPORT_SYMBOL(kernel_read);
550 static int exec_mmap(struct mm_struct *mm)
552 struct task_struct *tsk;
553 struct mm_struct * old_mm, *active_mm;
555 /* Notify parent that we're no longer interested in the old VM */
557 old_mm = current->mm;
558 mm_release(tsk, old_mm);
561 active_mm = tsk->active_mm;
564 activate_mm(active_mm, mm);
566 arch_pick_mmap_layout(mm);
567 #ifdef CONFIG_CKRM_RES_MEM
569 spin_lock(&old_mm->peertask_lock);
570 list_del(&tsk->mm_peers);
571 ckrm_mem_evaluate_mm(old_mm);
572 spin_unlock(&old_mm->peertask_lock);
574 spin_lock(&mm->peertask_lock);
575 list_add_tail(&tsk->mm_peers, &mm->tasklist);
576 ckrm_mem_evaluate_mm(mm);
577 spin_unlock(&mm->peertask_lock);
580 if (active_mm != old_mm) BUG();
589 * This function makes sure the current process has its own signal table,
590 * so that flush_signal_handlers can later reset the handlers without
591 * disturbing other processes. (Other processes might share the signal
592 * table via the CLONE_SIGHAND option to clone().)
594 static inline int de_thread(struct task_struct *tsk)
596 struct signal_struct *sig = tsk->signal;
597 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
598 spinlock_t *lock = &oldsighand->siglock;
602 * If we don't share sighandlers, then we aren't sharing anything
603 * and we can just re-use it all.
605 if (atomic_read(&oldsighand->count) <= 1) {
606 BUG_ON(atomic_read(&sig->count) != 1);
611 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
615 if (thread_group_empty(current))
616 goto no_thread_group;
619 * Kill all other threads in the thread group.
620 * We must hold tasklist_lock to call zap_other_threads.
622 read_lock(&tasklist_lock);
624 if (sig->group_exit) {
626 * Another group action in progress, just
627 * return so that the signal is processed.
629 spin_unlock_irq(lock);
630 read_unlock(&tasklist_lock);
631 kmem_cache_free(sighand_cachep, newsighand);
635 zap_other_threads(current);
636 read_unlock(&tasklist_lock);
639 * Account for the thread group leader hanging around:
642 if (current->pid == current->tgid)
644 while (atomic_read(&sig->count) > count) {
645 sig->group_exit_task = current;
646 sig->notify_count = count;
647 __set_current_state(TASK_UNINTERRUPTIBLE);
648 spin_unlock_irq(lock);
652 sig->group_exit_task = NULL;
653 sig->notify_count = 0;
654 spin_unlock_irq(lock);
657 * At this point all other threads have exited, all we have to
658 * do is to wait for the thread group leader to become inactive,
659 * and to assume its PID:
661 if (current->pid != current->tgid) {
662 struct task_struct *leader = current->group_leader, *parent;
663 struct dentry *proc_dentry1, *proc_dentry2;
664 unsigned long exit_state, ptrace;
667 * Wait for the thread group leader to be a zombie.
668 * It should already be zombie at this point, most
671 while (leader->exit_state != EXIT_ZOMBIE)
674 spin_lock(&leader->proc_lock);
675 spin_lock(¤t->proc_lock);
676 proc_dentry1 = proc_pid_unhash(current);
677 proc_dentry2 = proc_pid_unhash(leader);
678 write_lock_irq(&tasklist_lock);
680 if (leader->tgid != current->tgid)
682 if (current->pid == current->tgid)
685 * An exec() starts a new thread group with the
686 * TGID of the previous thread group. Rehash the
687 * two threads with a switched PID, and release
688 * the former thread group leader:
690 ptrace = leader->ptrace;
691 parent = leader->parent;
693 ptrace_unlink(current);
694 ptrace_unlink(leader);
695 remove_parent(current);
696 remove_parent(leader);
698 switch_exec_pids(leader, current);
700 current->parent = current->real_parent = leader->real_parent;
701 leader->parent = leader->real_parent = child_reaper;
702 current->group_leader = current;
703 leader->group_leader = leader;
705 add_parent(current, current->parent);
706 add_parent(leader, leader->parent);
708 current->ptrace = ptrace;
709 __ptrace_link(current, parent);
712 list_del(¤t->tasks);
713 list_add_tail(¤t->tasks, &init_task.tasks);
714 current->exit_signal = SIGCHLD;
715 exit_state = leader->exit_state;
717 write_unlock_irq(&tasklist_lock);
718 spin_unlock(&leader->proc_lock);
719 spin_unlock(¤t->proc_lock);
720 proc_pid_flush(proc_dentry1);
721 proc_pid_flush(proc_dentry2);
723 if (exit_state != EXIT_ZOMBIE)
725 release_task(leader);
729 * Now there are really no other threads at all,
730 * so it's safe to stop telling them to kill themselves.
735 BUG_ON(atomic_read(&sig->count) != 1);
738 if (atomic_read(&oldsighand->count) == 1) {
740 * Now that we nuked the rest of the thread group,
741 * it turns out we are not sharing sighand any more either.
742 * So we can just keep it.
744 kmem_cache_free(sighand_cachep, newsighand);
747 * Move our state over to newsighand and switch it in.
749 spin_lock_init(&newsighand->siglock);
750 atomic_set(&newsighand->count, 1);
751 memcpy(newsighand->action, oldsighand->action,
752 sizeof(newsighand->action));
754 write_lock_irq(&tasklist_lock);
755 spin_lock(&oldsighand->siglock);
756 spin_lock(&newsighand->siglock);
758 current->sighand = newsighand;
761 spin_unlock(&newsighand->siglock);
762 spin_unlock(&oldsighand->siglock);
763 write_unlock_irq(&tasklist_lock);
765 if (atomic_dec_and_test(&oldsighand->count))
766 kmem_cache_free(sighand_cachep, oldsighand);
769 if (!thread_group_empty(current))
771 if (current->tgid != current->pid)
777 * These functions flushes out all traces of the currently running executable
778 * so that a new one can be started
781 static inline void flush_old_files(struct files_struct * files)
785 spin_lock(&files->file_lock);
787 unsigned long set, i;
791 if (i >= files->max_fds || i >= files->max_fdset)
793 set = files->close_on_exec->fds_bits[j];
796 files->close_on_exec->fds_bits[j] = 0;
797 spin_unlock(&files->file_lock);
798 for ( ; set ; i++,set >>= 1) {
803 spin_lock(&files->file_lock);
806 spin_unlock(&files->file_lock);
809 void get_task_comm(char *buf, struct task_struct *tsk)
811 /* buf must be at least sizeof(tsk->comm) in size */
813 memcpy(buf, tsk->comm, sizeof(tsk->comm));
817 void set_task_comm(struct task_struct *tsk, char *buf)
820 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
824 int flush_old_exec(struct linux_binprm * bprm)
828 struct files_struct *files;
829 char tcomm[sizeof(current->comm)];
832 * Make sure we have a private signal table and that
833 * we are unassociated from the previous thread group.
835 retval = de_thread(current);
840 * Make sure we have private file handles. Ask the
841 * fork helper to do the work for us and the exit
842 * helper to do the cleanup of the old one.
844 files = current->files; /* refcounted so safe to hold */
845 retval = unshare_files();
849 * Release all of the old mmap stuff
851 retval = exec_mmap(bprm->mm);
855 bprm->mm = NULL; /* We're using it now */
857 /* This is the point of no return */
859 put_files_struct(files);
861 current->sas_ss_sp = current->sas_ss_size = 0;
863 if (current->euid == current->uid && current->egid == current->gid)
864 current->mm->dumpable = 1;
866 current->mm->dumpable = suid_dumpable;
868 name = bprm->filename;
869 for (i=0; (ch = *(name++)) != '\0';) {
873 if (i < (sizeof(tcomm) - 1))
877 set_task_comm(current, tcomm);
879 current->flags &= ~PF_RELOCEXEC;
882 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
883 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
884 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
886 current->mm->dumpable = suid_dumpable;
889 /* An exec changes our domain. We are no longer part of the thread
892 current->self_exec_id++;
894 flush_signal_handlers(current, 0);
895 flush_old_files(current->files);
900 put_files_struct(current->files);
901 current->files = files;
906 EXPORT_SYMBOL(flush_old_exec);
909 * Fill the binprm structure from the inode.
910 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
912 int prepare_binprm(struct linux_binprm *bprm)
915 struct inode * inode = bprm->file->f_dentry->d_inode;
918 mode = inode->i_mode;
920 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
921 * generic_permission lets a non-executable through
923 if (!(mode & 0111)) /* with at least _one_ execute bit set */
925 if (bprm->file->f_op == NULL)
928 bprm->e_uid = current->euid;
929 bprm->e_gid = current->egid;
931 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
933 if (mode & S_ISUID) {
934 current->personality &= ~PER_CLEAR_ON_SETID;
935 bprm->e_uid = inode->i_uid;
940 * If setgid is set but no group execute bit then this
941 * is a candidate for mandatory locking, not a setgid
944 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
945 current->personality &= ~PER_CLEAR_ON_SETID;
946 bprm->e_gid = inode->i_gid;
950 /* fill in binprm security blob */
951 retval = security_bprm_set(bprm);
955 memset(bprm->buf,0,BINPRM_BUF_SIZE);
956 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
959 EXPORT_SYMBOL(prepare_binprm);
961 static inline int unsafe_exec(struct task_struct *p)
964 if (p->ptrace & PT_PTRACED) {
965 if (p->ptrace & PT_PTRACE_CAP)
966 unsafe |= LSM_UNSAFE_PTRACE_CAP;
968 unsafe |= LSM_UNSAFE_PTRACE;
970 if (atomic_read(&p->fs->count) > 1 ||
971 atomic_read(&p->files->count) > 1 ||
972 atomic_read(&p->sighand->count) > 1)
973 unsafe |= LSM_UNSAFE_SHARE;
978 void compute_creds(struct linux_binprm *bprm)
982 if (bprm->e_uid != current->uid)
987 unsafe = unsafe_exec(current);
988 security_bprm_apply_creds(bprm, unsafe);
989 task_unlock(current);
992 EXPORT_SYMBOL(compute_creds);
994 void remove_arg_zero(struct linux_binprm *bprm)
997 unsigned long offset;
1001 offset = bprm->p % PAGE_SIZE;
1004 while (bprm->p++, *(kaddr+offset++)) {
1005 if (offset != PAGE_SIZE)
1008 kunmap_atomic(kaddr, KM_USER0);
1010 page = bprm->page[bprm->p/PAGE_SIZE];
1011 kaddr = kmap_atomic(page, KM_USER0);
1013 kunmap_atomic(kaddr, KM_USER0);
1018 EXPORT_SYMBOL(remove_arg_zero);
1021 * cycle the list of binary formats handler, until one recognizes the image
1023 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1026 struct linux_binfmt *fmt;
1028 /* handle /sbin/loader.. */
1030 struct exec * eh = (struct exec *) bprm->buf;
1032 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1033 (eh->fh.f_flags & 0x3000) == 0x3000)
1036 unsigned long loader;
1038 allow_write_access(bprm->file);
1042 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1044 file = open_exec("/sbin/loader");
1045 retval = PTR_ERR(file);
1049 /* Remember if the application is TASO. */
1050 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1053 bprm->loader = loader;
1054 retval = prepare_binprm(bprm);
1057 /* should call search_binary_handler recursively here,
1058 but it does not matter */
1062 retval = security_bprm_check(bprm);
1066 /* kernel module loader fixup */
1067 /* so we don't try to load run modprobe in kernel space. */
1070 for (try=0; try<2; try++) {
1071 read_lock(&binfmt_lock);
1072 for (fmt = formats ; fmt ; fmt = fmt->next) {
1073 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1076 if (!try_module_get(fmt->module))
1078 read_unlock(&binfmt_lock);
1079 retval = fn(bprm, regs);
1082 allow_write_access(bprm->file);
1086 current->did_exec = 1;
1087 ckrm_cb_exec(bprm->filename);
1090 read_lock(&binfmt_lock);
1092 if (retval != -ENOEXEC || bprm->mm == NULL)
1095 read_unlock(&binfmt_lock);
1099 read_unlock(&binfmt_lock);
1100 if (retval != -ENOEXEC || bprm->mm == NULL) {
1104 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1105 if (printable(bprm->buf[0]) &&
1106 printable(bprm->buf[1]) &&
1107 printable(bprm->buf[2]) &&
1108 printable(bprm->buf[3]))
1109 break; /* -ENOEXEC */
1110 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1117 EXPORT_SYMBOL(search_binary_handler);
1120 * sys_execve() executes a new program.
1122 int do_execve(char * filename,
1123 char __user *__user *argv,
1124 char __user *__user *envp,
1125 struct pt_regs * regs)
1127 struct linux_binprm *bprm;
1133 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1136 memset(bprm, 0, sizeof(*bprm));
1138 file = open_exec(filename);
1139 retval = PTR_ERR(file);
1145 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1148 bprm->filename = filename;
1149 bprm->interp = filename;
1150 bprm->mm = mm_alloc();
1155 retval = init_new_context(current, bprm->mm);
1159 bprm->argc = count(argv, bprm->p / sizeof(void *));
1160 if ((retval = bprm->argc) < 0)
1163 bprm->envc = count(envp, bprm->p / sizeof(void *));
1164 if ((retval = bprm->envc) < 0)
1167 retval = security_bprm_alloc(bprm);
1171 retval = prepare_binprm(bprm);
1175 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1179 bprm->exec = bprm->p;
1180 retval = copy_strings(bprm->envc, envp, bprm);
1184 retval = copy_strings(bprm->argc, argv, bprm);
1188 retval = search_binary_handler(bprm,regs);
1190 free_arg_pages(bprm);
1192 /* execve success */
1193 security_bprm_free(bprm);
1199 /* Something went wrong, return the inode and free the argument pages*/
1200 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1201 struct page * page = bprm->page[i];
1207 security_bprm_free(bprm);
1215 allow_write_access(bprm->file);
1226 int set_binfmt(struct linux_binfmt *new)
1228 struct linux_binfmt *old = current->binfmt;
1231 if (!try_module_get(new->module))
1234 current->binfmt = new;
1236 module_put(old->module);
1240 EXPORT_SYMBOL(set_binfmt);
1242 #define CORENAME_MAX_SIZE 64
1244 /* format_corename will inspect the pattern parameter, and output a
1245 * name into corename, which must have space for at least
1246 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1248 static void format_corename(char *corename, const char *pattern, long signr)
1250 const char *pat_ptr = pattern;
1251 char *out_ptr = corename;
1252 char *const out_end = corename + CORENAME_MAX_SIZE;
1254 int pid_in_pattern = 0;
1256 /* Repeat as long as we have more pattern to process and more output
1259 if (*pat_ptr != '%') {
1260 if (out_ptr == out_end)
1262 *out_ptr++ = *pat_ptr++;
1264 switch (*++pat_ptr) {
1267 /* Double percent, output one percent */
1269 if (out_ptr == out_end)
1276 rc = snprintf(out_ptr, out_end - out_ptr,
1277 "%d", current->tgid);
1278 if (rc > out_end - out_ptr)
1284 rc = snprintf(out_ptr, out_end - out_ptr,
1285 "%d", current->uid);
1286 if (rc > out_end - out_ptr)
1292 rc = snprintf(out_ptr, out_end - out_ptr,
1293 "%d", current->gid);
1294 if (rc > out_end - out_ptr)
1298 /* signal that caused the coredump */
1300 rc = snprintf(out_ptr, out_end - out_ptr,
1302 if (rc > out_end - out_ptr)
1306 /* UNIX time of coredump */
1309 do_gettimeofday(&tv);
1310 rc = snprintf(out_ptr, out_end - out_ptr,
1312 if (rc > out_end - out_ptr)
1319 down_read(&uts_sem);
1320 rc = snprintf(out_ptr, out_end - out_ptr,
1321 "%s", system_utsname.nodename);
1323 if (rc > out_end - out_ptr)
1329 rc = snprintf(out_ptr, out_end - out_ptr,
1330 "%s", current->comm);
1331 if (rc > out_end - out_ptr)
1341 /* Backward compatibility with core_uses_pid:
1343 * If core_pattern does not include a %p (as is the default)
1344 * and core_uses_pid is set, then .%pid will be appended to
1347 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1348 rc = snprintf(out_ptr, out_end - out_ptr,
1349 ".%d", current->tgid);
1350 if (rc > out_end - out_ptr)
1358 static void zap_threads (struct mm_struct *mm)
1360 struct task_struct *g, *p;
1361 struct task_struct *tsk = current;
1362 struct completion *vfork_done = tsk->vfork_done;
1366 * Make sure nobody is waiting for us to release the VM,
1367 * otherwise we can deadlock when we wait on each other
1370 tsk->vfork_done = NULL;
1371 complete(vfork_done);
1374 read_lock(&tasklist_lock);
1376 if (mm == p->mm && p != tsk) {
1377 force_sig_specific(SIGKILL, p);
1379 if (unlikely(p->ptrace) &&
1380 unlikely(p->parent->mm == mm))
1383 while_each_thread(g,p);
1385 read_unlock(&tasklist_lock);
1387 while (unlikely(traced)) {
1389 * We are zapping a thread and the thread it ptraces.
1390 * The tracee won't come out of TASK_TRACED state until
1391 * its ptracer detaches. That happens when the ptracer
1392 * dies, but it synchronizes with us and so won't get
1393 * that far until we finish the core dump. If we're
1394 * waiting for the tracee to synchronize but it stays
1395 * blocked in TASK_TRACED, then we deadlock. So, for
1396 * this weirdo case we have to do another round with
1397 * tasklist_lock write-locked to __ptrace_unlink the
1398 * children that might cause this deadlock. That will
1399 * wake them up to process their pending SIGKILL.
1401 * First, give everyone we just killed a chance to run
1402 * so they can all get into the coredump synchronization.
1403 * That should leave only the TASK_TRACED stragglers for
1404 * us to wake up. If a ptracer is still running, we'll
1405 * have to come around again after letting it finish.
1409 write_lock_irq(&tasklist_lock);
1410 do_each_thread(g,p) {
1411 if (mm != p->mm || p == tsk ||
1412 !p->ptrace || p->parent->mm != mm)
1414 if ((p->parent->flags & (PF_SIGNALED|PF_EXITING)) ||
1415 (p->parent->state & (TASK_TRACED|TASK_STOPPED))) {
1417 * The parent is in the process of exiting
1418 * itself, or else it's stopped right now.
1419 * It cannot be in a ptrace call, and would
1420 * have to read_lock tasklist_lock before
1421 * it could start one, so we are safe here.
1426 * Blargh! The ptracer is not dying
1427 * yet, so we cannot be sure that it
1428 * isn't in the middle of a ptrace call.
1429 * We'll have to let it run to get into
1430 * coredump_wait and come around another
1431 * time to detach its tracee.
1435 } while_each_thread(g,p);
1436 write_unlock_irq(&tasklist_lock);
1440 static void coredump_wait(struct mm_struct *mm)
1442 DECLARE_COMPLETION(startup_done);
1444 mm->core_waiters++; /* let other threads block */
1445 mm->core_startup_done = &startup_done;
1447 /* give other threads a chance to run: */
1451 if (--mm->core_waiters) {
1452 up_write(&mm->mmap_sem);
1453 wait_for_completion(&startup_done);
1455 up_write(&mm->mmap_sem);
1456 BUG_ON(mm->core_waiters);
1459 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1461 char corename[CORENAME_MAX_SIZE + 1];
1462 struct mm_struct *mm = current->mm;
1463 struct linux_binfmt * binfmt;
1464 struct inode * inode;
1467 int fsuid = current->fsuid;
1470 binfmt = current->binfmt;
1471 if (!binfmt || !binfmt->core_dump)
1473 if (current->tux_exit)
1474 current->tux_exit();
1475 down_write(&mm->mmap_sem);
1476 if (!mm->dumpable) {
1477 up_write(&mm->mmap_sem);
1482 * We cannot trust fsuid as being the "true" uid of the
1483 * process nor do we know its entire history. We only know it
1484 * was tainted so we dump it as root in mode 2.
1486 if (mm->dumpable == 2) { /* Setuid core dump mode */
1487 flag = O_EXCL; /* Stop rewrite attacks */
1488 current->fsuid = 0; /* Dump root private */
1491 init_completion(&mm->core_done);
1492 current->signal->group_exit = 1;
1493 current->signal->group_exit_code = exit_code;
1496 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1500 * lock_kernel() because format_corename() is controlled by sysctl, which
1501 * uses lock_kernel()
1504 format_corename(corename, core_pattern, signr);
1506 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1509 inode = file->f_dentry->d_inode;
1510 if (inode->i_nlink > 1)
1511 goto close_fail; /* multiple links - don't dump */
1512 if (d_unhashed(file->f_dentry))
1515 if (!S_ISREG(inode->i_mode))
1519 if (!file->f_op->write)
1521 if (do_truncate(file->f_dentry, 0) != 0)
1524 retval = binfmt->core_dump(signr, regs, file);
1527 current->signal->group_exit_code |= 0x80;
1529 filp_close(file, NULL);
1531 current->fsuid = fsuid;
1532 complete_all(&mm->core_done);