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/slab.h>
26 #include <linux/file.h>
27 #include <linux/mman.h>
28 #include <linux/a.out.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/highmem.h>
35 #include <linux/spinlock.h>
36 #include <linux/key.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/acct.h>
50 #include <linux/cn_proc.h>
51 #include <linux/audit.h>
52 #include <linux/vs_memory.h>
53 #include <linux/vs_cvirt.h>
55 #include <asm/uaccess.h>
56 #include <asm/mmu_context.h>
59 #include <linux/kmod.h>
63 char core_pattern[65] = "core";
64 int suid_dumpable = 0;
66 EXPORT_SYMBOL(suid_dumpable);
67 /* The maximal length of core_pattern is also specified in sysctl.c */
69 static struct linux_binfmt *formats;
70 static DEFINE_RWLOCK(binfmt_lock);
72 int register_binfmt(struct linux_binfmt * fmt)
74 struct linux_binfmt ** tmp = &formats;
80 write_lock(&binfmt_lock);
83 write_unlock(&binfmt_lock);
90 write_unlock(&binfmt_lock);
94 EXPORT_SYMBOL(register_binfmt);
96 int unregister_binfmt(struct linux_binfmt * fmt)
98 struct linux_binfmt ** tmp = &formats;
100 write_lock(&binfmt_lock);
104 write_unlock(&binfmt_lock);
109 write_unlock(&binfmt_lock);
113 EXPORT_SYMBOL(unregister_binfmt);
115 static inline void put_binfmt(struct linux_binfmt * fmt)
117 module_put(fmt->module);
121 * Note that a shared library must be both readable and executable due to
124 * Also note that we take the address to load from from the file itself.
126 asmlinkage long sys_uselib(const char __user * library)
132 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
137 if (!S_ISREG(nd.dentry->d_inode->i_mode))
140 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
144 file = nameidata_to_filp(&nd, 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);
172 release_open_intent(&nd);
178 * count() counts the number of strings in array ARGV.
180 static int count(char __user * __user * argv, int max)
188 if (get_user(p, argv))
202 * 'copy_strings()' copies argument/environment strings from user
203 * memory to free pages in kernel mem. These are in a format ready
204 * to be put directly into the top of new user memory.
206 static int copy_strings(int argc, char __user * __user * argv,
207 struct linux_binprm *bprm)
209 struct page *kmapped_page = NULL;
218 if (get_user(str, argv+argc) ||
219 !(len = strnlen_user(str, bprm->p))) {
230 /* XXX: add architecture specific overflow check here. */
235 int offset, bytes_to_copy;
238 offset = pos % PAGE_SIZE;
240 page = bprm->page[i];
243 page = alloc_page(GFP_HIGHUSER);
244 bprm->page[i] = page;
252 if (page != kmapped_page) {
254 kunmap(kmapped_page);
256 kaddr = kmap(kmapped_page);
259 memset(kaddr, 0, offset);
260 bytes_to_copy = PAGE_SIZE - offset;
261 if (bytes_to_copy > len) {
264 memset(kaddr+offset+len, 0,
265 PAGE_SIZE-offset-len);
267 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
273 pos += bytes_to_copy;
274 str += bytes_to_copy;
275 len -= bytes_to_copy;
281 kunmap(kmapped_page);
286 * Like copy_strings, but get argv and its values from kernel memory.
288 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
291 mm_segment_t oldfs = get_fs();
293 r = copy_strings(argc, (char __user * __user *)argv, bprm);
298 EXPORT_SYMBOL(copy_strings_kernel);
302 * This routine is used to map in a page into an address space: needed by
303 * execve() for the initial stack and environment pages.
305 * vma->vm_mm->mmap_sem is held for writing.
307 void install_arg_page(struct vm_area_struct *vma,
308 struct page *page, unsigned long address)
310 struct mm_struct *mm = vma->vm_mm;
314 if (unlikely(anon_vma_prepare(vma)))
317 flush_dcache_page(page);
318 pte = get_locked_pte(mm, address, &ptl);
321 if (!pte_none(*pte)) {
322 pte_unmap_unlock(pte, ptl);
325 inc_mm_counter(mm, anon_rss);
326 lru_cache_add_active(page);
327 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
328 page, vma->vm_page_prot))));
329 page_add_new_anon_rmap(page, vma, address);
330 pte_unmap_unlock(pte, ptl);
332 /* no need for flush_tlb */
336 force_sig(SIGKILL, current);
339 #define EXTRA_STACK_VM_PAGES 20 /* random */
341 int setup_arg_pages(struct linux_binprm *bprm,
342 unsigned long stack_top,
343 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 /* Limit stack size to 1GB */
381 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
382 if (stack_base > (1 << 30))
383 stack_base = 1 << 30;
384 stack_base = PAGE_ALIGN(stack_top - stack_base);
386 /* Adjust bprm->p to point to the end of the strings. */
387 bprm->p = stack_base + PAGE_SIZE * i - offset;
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 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
397 stack_base = PAGE_ALIGN(stack_base);
398 bprm->p += stack_base;
399 mm->arg_start = bprm->p;
400 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
403 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
406 bprm->loader += stack_base;
407 bprm->exec += stack_base;
409 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
413 memset(mpnt, 0, sizeof(*mpnt));
415 down_write(&mm->mmap_sem);
418 #ifdef CONFIG_STACK_GROWSUP
419 mpnt->vm_start = stack_base;
420 mpnt->vm_end = stack_base + arg_size;
422 mpnt->vm_end = stack_top;
423 mpnt->vm_start = mpnt->vm_end - arg_size;
425 /* Adjust stack execute permissions; explicitly enable
426 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
427 * and leave alone (arch default) otherwise. */
428 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
429 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
430 else if (executable_stack == EXSTACK_DISABLE_X)
431 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
433 mpnt->vm_flags = VM_STACK_FLAGS;
434 mpnt->vm_flags |= mm->def_flags;
435 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
436 if ((ret = insert_vm_struct(mm, mpnt))) {
437 up_write(&mm->mmap_sem);
438 kmem_cache_free(vm_area_cachep, mpnt);
441 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
442 mm->stack_vm = mm->total_vm;
445 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
446 struct page *page = bprm->page[i];
448 bprm->page[i] = NULL;
449 install_arg_page(mpnt, page, stack_base);
451 stack_base += PAGE_SIZE;
453 up_write(&mm->mmap_sem);
458 EXPORT_SYMBOL(setup_arg_pages);
460 #define free_arg_pages(bprm) do { } while (0)
464 static inline void free_arg_pages(struct linux_binprm *bprm)
468 for (i = 0; i < MAX_ARG_PAGES; i++) {
470 __free_page(bprm->page[i]);
471 bprm->page[i] = NULL;
475 #endif /* CONFIG_MMU */
477 struct file *open_exec(const char *name)
483 err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
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 = vfs_permission(&nd, MAY_EXEC);
494 file = nameidata_to_filp(&nd, O_RDONLY);
496 err = deny_write_access(file);
506 release_open_intent(&nd);
512 EXPORT_SYMBOL(open_exec);
514 int kernel_read(struct file *file, unsigned long offset,
515 char *addr, unsigned long count)
523 /* The cast to a user pointer is valid due to the set_fs() */
524 result = vfs_read(file, (void __user *)addr, count, &pos);
529 EXPORT_SYMBOL(kernel_read);
531 static int exec_mmap(struct mm_struct *mm)
533 struct task_struct *tsk;
534 struct mm_struct * old_mm, *active_mm;
536 /* Notify parent that we're no longer interested in the old VM */
538 old_mm = current->mm;
539 mm_release(tsk, old_mm);
543 * Make sure that if there is a core dump in progress
544 * for the old mm, we get out and die instead of going
545 * through with the exec. We must hold mmap_sem around
546 * checking core_waiters and changing tsk->mm. The
547 * core-inducing thread will increment core_waiters for
548 * each thread whose ->mm == old_mm.
550 down_read(&old_mm->mmap_sem);
551 if (unlikely(old_mm->core_waiters)) {
552 up_read(&old_mm->mmap_sem);
557 active_mm = tsk->active_mm;
560 activate_mm(active_mm, mm);
562 arch_pick_mmap_layout(mm);
564 up_read(&old_mm->mmap_sem);
565 BUG_ON(active_mm != old_mm);
574 * This function makes sure the current process has its own signal table,
575 * so that flush_signal_handlers can later reset the handlers without
576 * disturbing other processes. (Other processes might share the signal
577 * table via the CLONE_SIGHAND option to clone().)
579 static int de_thread(struct task_struct *tsk)
581 struct signal_struct *sig = tsk->signal;
582 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
583 spinlock_t *lock = &oldsighand->siglock;
584 struct task_struct *leader = NULL;
588 * If we don't share sighandlers, then we aren't sharing anything
589 * and we can just re-use it all.
591 if (atomic_read(&oldsighand->count) <= 1) {
592 BUG_ON(atomic_read(&sig->count) != 1);
597 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
601 if (thread_group_empty(current))
602 goto no_thread_group;
605 * Kill all other threads in the thread group.
606 * We must hold tasklist_lock to call zap_other_threads.
608 read_lock(&tasklist_lock);
610 if (sig->flags & SIGNAL_GROUP_EXIT) {
612 * Another group action in progress, just
613 * return so that the signal is processed.
615 spin_unlock_irq(lock);
616 read_unlock(&tasklist_lock);
617 kmem_cache_free(sighand_cachep, newsighand);
622 * child_reaper ignores SIGKILL, change it now.
623 * Reparenting needs write_lock on tasklist_lock,
624 * so it is safe to do it under read_lock.
626 if (unlikely(current->group_leader == child_reaper))
627 child_reaper = current;
629 zap_other_threads(current);
630 read_unlock(&tasklist_lock);
633 * Account for the thread group leader hanging around:
636 if (!thread_group_leader(current)) {
639 * The SIGALRM timer survives the exec, but needs to point
640 * at us as the new group leader now. We have a race with
641 * a timer firing now getting the old leader, so we need to
642 * synchronize with any firing (by calling del_timer_sync)
643 * before we can safely let the old group leader die.
646 spin_unlock_irq(lock);
647 if (hrtimer_cancel(&sig->real_timer))
648 hrtimer_restart(&sig->real_timer);
651 while (atomic_read(&sig->count) > count) {
652 sig->group_exit_task = current;
653 sig->notify_count = count;
654 __set_current_state(TASK_UNINTERRUPTIBLE);
655 spin_unlock_irq(lock);
659 sig->group_exit_task = NULL;
660 sig->notify_count = 0;
661 spin_unlock_irq(lock);
664 * At this point all other threads have exited, all we have to
665 * do is to wait for the thread group leader to become inactive,
666 * and to assume its PID:
668 if (!thread_group_leader(current)) {
670 * Wait for the thread group leader to be a zombie.
671 * It should already be zombie at this point, most
674 leader = current->group_leader;
675 while (leader->exit_state != EXIT_ZOMBIE)
679 * The only record we have of the real-time age of a
680 * process, regardless of execs it's done, is start_time.
681 * All the past CPU time is accumulated in signal_struct
682 * from sister threads now dead. But in this non-leader
683 * exec, nothing survives from the original leader thread,
684 * whose birth marks the true age of this process now.
685 * When we take on its identity by switching to its PID, we
686 * also take its birthdate (always earlier than our own).
688 current->start_time = leader->start_time;
690 write_lock_irq(&tasklist_lock);
692 BUG_ON(leader->tgid != current->tgid);
693 BUG_ON(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:
701 /* Become a process group leader with the old leader's pid.
702 * Note: The old leader also uses thispid until release_task
703 * is called. Odd but simple and correct.
705 detach_pid(current, PIDTYPE_PID);
706 current->pid = leader->pid;
707 attach_pid(current, PIDTYPE_PID, current->pid);
708 attach_pid(current, PIDTYPE_PGID, current->signal->pgrp);
709 attach_pid(current, PIDTYPE_SID, current->signal->session);
710 list_replace_rcu(&leader->tasks, ¤t->tasks);
712 current->group_leader = current;
713 leader->group_leader = current;
715 /* Reduce leader to a thread */
716 detach_pid(leader, PIDTYPE_PGID);
717 detach_pid(leader, PIDTYPE_SID);
719 current->exit_signal = SIGCHLD;
721 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
722 leader->exit_state = EXIT_DEAD;
724 write_unlock_irq(&tasklist_lock);
728 * There may be one thread left which is just exiting,
729 * but it's safe to stop telling the group to kill themselves.
736 release_task(leader);
738 BUG_ON(atomic_read(&sig->count) != 1);
740 if (atomic_read(&oldsighand->count) == 1) {
742 * Now that we nuked the rest of the thread group,
743 * it turns out we are not sharing sighand any more either.
744 * So we can just keep it.
746 kmem_cache_free(sighand_cachep, newsighand);
749 * Move our state over to newsighand and switch it in.
751 atomic_set(&newsighand->count, 1);
752 memcpy(newsighand->action, oldsighand->action,
753 sizeof(newsighand->action));
755 write_lock_irq(&tasklist_lock);
756 spin_lock(&oldsighand->siglock);
757 spin_lock_nested(&newsighand->siglock, SINGLE_DEPTH_NESTING);
759 rcu_assign_pointer(current->sighand, newsighand);
762 spin_unlock(&newsighand->siglock);
763 spin_unlock(&oldsighand->siglock);
764 write_unlock_irq(&tasklist_lock);
766 if (atomic_dec_and_test(&oldsighand->count))
767 kmem_cache_free(sighand_cachep, oldsighand);
770 BUG_ON(!thread_group_leader(current));
775 * These functions flushes out all traces of the currently running executable
776 * so that a new one can be started
779 static void flush_old_files(struct files_struct * files)
784 spin_lock(&files->file_lock);
786 unsigned long set, i;
790 fdt = files_fdtable(files);
791 if (i >= fdt->max_fds || i >= fdt->max_fdset)
793 set = fdt->close_on_exec->fds_bits[j];
796 fdt->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 strncpy(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 */
858 put_files_struct(files);
860 current->sas_ss_sp = current->sas_ss_size = 0;
862 if (current->euid == current->uid && current->egid == current->gid)
863 current->mm->dumpable = 1;
865 current->mm->dumpable = suid_dumpable;
867 name = bprm->filename;
869 /* Copies the binary name from after last slash */
870 for (i=0; (ch = *(name++)) != '\0';) {
872 i = 0; /* overwrite what we wrote */
874 if (i < (sizeof(tcomm) - 1))
878 set_task_comm(current, tcomm);
880 current->flags &= ~PF_RANDOMIZE;
883 /* Set the new mm task size. We have to do that late because it may
884 * depend on TIF_32BIT which is only updated in flush_thread() on
885 * some architectures like powerpc
887 current->mm->task_size = TASK_SIZE;
889 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
890 file_permission(bprm->file, MAY_READ) ||
891 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
893 current->mm->dumpable = suid_dumpable;
896 /* An exec changes our domain. We are no longer part of the thread
899 current->self_exec_id++;
901 flush_signal_handlers(current, 0);
902 flush_old_files(current->files);
907 put_files_struct(current->files);
908 current->files = files;
913 EXPORT_SYMBOL(flush_old_exec);
916 * Fill the binprm structure from the inode.
917 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
919 int prepare_binprm(struct linux_binprm *bprm)
922 struct inode * inode = bprm->file->f_dentry->d_inode;
925 mode = inode->i_mode;
926 if (bprm->file->f_op == NULL)
929 bprm->e_uid = current->euid;
930 bprm->e_gid = current->egid;
932 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
934 if (mode & S_ISUID) {
935 current->personality &= ~PER_CLEAR_ON_SETID;
936 bprm->e_uid = inode->i_uid;
941 * If setgid is set but no group execute bit then this
942 * is a candidate for mandatory locking, not a setgid
945 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
946 current->personality &= ~PER_CLEAR_ON_SETID;
947 bprm->e_gid = inode->i_gid;
951 /* fill in binprm security blob */
952 retval = security_bprm_set(bprm);
956 memset(bprm->buf,0,BINPRM_BUF_SIZE);
957 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
960 EXPORT_SYMBOL(prepare_binprm);
962 static int unsafe_exec(struct task_struct *p)
965 if (p->ptrace & PT_PTRACED) {
966 if (p->ptrace & PT_PTRACE_CAP)
967 unsafe |= LSM_UNSAFE_PTRACE_CAP;
969 unsafe |= LSM_UNSAFE_PTRACE;
971 if (atomic_read(&p->fs->count) > 1 ||
972 atomic_read(&p->files->count) > 1 ||
973 atomic_read(&p->sighand->count) > 1)
974 unsafe |= LSM_UNSAFE_SHARE;
979 void compute_creds(struct linux_binprm *bprm)
983 if (bprm->e_uid != current->uid)
988 unsafe = unsafe_exec(current);
989 security_bprm_apply_creds(bprm, unsafe);
990 task_unlock(current);
991 security_bprm_post_apply_creds(bprm);
994 EXPORT_SYMBOL(compute_creds);
996 void remove_arg_zero(struct linux_binprm *bprm)
999 unsigned long offset;
1003 offset = bprm->p % PAGE_SIZE;
1006 while (bprm->p++, *(kaddr+offset++)) {
1007 if (offset != PAGE_SIZE)
1010 kunmap_atomic(kaddr, KM_USER0);
1012 page = bprm->page[bprm->p/PAGE_SIZE];
1013 kaddr = kmap_atomic(page, KM_USER0);
1015 kunmap_atomic(kaddr, KM_USER0);
1020 EXPORT_SYMBOL(remove_arg_zero);
1023 * cycle the list of binary formats handler, until one recognizes the image
1025 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1028 struct linux_binfmt *fmt;
1030 /* handle /sbin/loader.. */
1032 struct exec * eh = (struct exec *) bprm->buf;
1034 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1035 (eh->fh.f_flags & 0x3000) == 0x3000)
1038 unsigned long loader;
1040 allow_write_access(bprm->file);
1044 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1046 file = open_exec("/sbin/loader");
1047 retval = PTR_ERR(file);
1051 /* Remember if the application is TASO. */
1052 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1055 bprm->loader = loader;
1056 retval = prepare_binprm(bprm);
1059 /* should call search_binary_handler recursively here,
1060 but it does not matter */
1064 retval = security_bprm_check(bprm);
1068 /* kernel module loader fixup */
1069 /* so we don't try to load run modprobe in kernel space. */
1072 retval = audit_bprm(bprm);
1077 for (try=0; try<2; try++) {
1078 read_lock(&binfmt_lock);
1079 for (fmt = formats ; fmt ; fmt = fmt->next) {
1080 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1083 if (!try_module_get(fmt->module))
1085 read_unlock(&binfmt_lock);
1086 retval = fn(bprm, regs);
1089 allow_write_access(bprm->file);
1093 current->did_exec = 1;
1094 proc_exec_connector(current);
1097 read_lock(&binfmt_lock);
1099 if (retval != -ENOEXEC || bprm->mm == NULL)
1102 read_unlock(&binfmt_lock);
1106 read_unlock(&binfmt_lock);
1107 if (retval != -ENOEXEC || bprm->mm == NULL) {
1111 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1112 if (printable(bprm->buf[0]) &&
1113 printable(bprm->buf[1]) &&
1114 printable(bprm->buf[2]) &&
1115 printable(bprm->buf[3]))
1116 break; /* -ENOEXEC */
1117 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1124 EXPORT_SYMBOL(search_binary_handler);
1127 * sys_execve() executes a new program.
1129 int do_execve(char * filename,
1130 char __user *__user *argv,
1131 char __user *__user *envp,
1132 struct pt_regs * regs)
1134 struct linux_binprm *bprm;
1140 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1144 file = open_exec(filename);
1145 retval = PTR_ERR(file);
1151 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1154 bprm->filename = filename;
1155 bprm->interp = filename;
1156 bprm->mm = mm_alloc();
1161 retval = init_new_context(current, bprm->mm);
1165 bprm->argc = count(argv, bprm->p / sizeof(void *));
1166 if ((retval = bprm->argc) < 0)
1169 bprm->envc = count(envp, bprm->p / sizeof(void *));
1170 if ((retval = bprm->envc) < 0)
1173 retval = security_bprm_alloc(bprm);
1177 retval = prepare_binprm(bprm);
1181 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1185 bprm->exec = bprm->p;
1186 retval = copy_strings(bprm->envc, envp, bprm);
1190 retval = copy_strings(bprm->argc, argv, bprm);
1194 retval = search_binary_handler(bprm,regs);
1196 free_arg_pages(bprm);
1198 /* execve success */
1199 security_bprm_free(bprm);
1200 acct_update_integrals(current);
1206 /* Something went wrong, return the inode and free the argument pages*/
1207 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1208 struct page * page = bprm->page[i];
1214 security_bprm_free(bprm);
1222 allow_write_access(bprm->file);
1233 int set_binfmt(struct linux_binfmt *new)
1235 struct linux_binfmt *old = current->binfmt;
1238 if (!try_module_get(new->module))
1241 current->binfmt = new;
1243 module_put(old->module);
1247 EXPORT_SYMBOL(set_binfmt);
1249 #define CORENAME_MAX_SIZE 64
1251 /* format_corename will inspect the pattern parameter, and output a
1252 * name into corename, which must have space for at least
1253 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1255 static void format_corename(char *corename, const char *pattern, long signr)
1257 const char *pat_ptr = pattern;
1258 char *out_ptr = corename;
1259 char *const out_end = corename + CORENAME_MAX_SIZE;
1261 int pid_in_pattern = 0;
1263 /* Repeat as long as we have more pattern to process and more output
1266 if (*pat_ptr != '%') {
1267 if (out_ptr == out_end)
1269 *out_ptr++ = *pat_ptr++;
1271 switch (*++pat_ptr) {
1274 /* Double percent, output one percent */
1276 if (out_ptr == out_end)
1283 rc = snprintf(out_ptr, out_end - out_ptr,
1284 "%d", current->tgid);
1285 if (rc > out_end - out_ptr)
1291 rc = snprintf(out_ptr, out_end - out_ptr,
1292 "%d", current->uid);
1293 if (rc > out_end - out_ptr)
1299 rc = snprintf(out_ptr, out_end - out_ptr,
1300 "%d", current->gid);
1301 if (rc > out_end - out_ptr)
1305 /* signal that caused the coredump */
1307 rc = snprintf(out_ptr, out_end - out_ptr,
1309 if (rc > out_end - out_ptr)
1313 /* UNIX time of coredump */
1316 do_gettimeofday(&tv);
1317 rc = snprintf(out_ptr, out_end - out_ptr,
1319 if (rc > out_end - out_ptr)
1326 down_read(&uts_sem);
1327 rc = snprintf(out_ptr, out_end - out_ptr,
1328 "%s", vx_new_uts(nodename));
1330 if (rc > out_end - out_ptr)
1336 rc = snprintf(out_ptr, out_end - out_ptr,
1337 "%s", current->comm);
1338 if (rc > out_end - out_ptr)
1348 /* Backward compatibility with core_uses_pid:
1350 * If core_pattern does not include a %p (as is the default)
1351 * and core_uses_pid is set, then .%pid will be appended to
1354 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1355 rc = snprintf(out_ptr, out_end - out_ptr,
1356 ".%d", current->tgid);
1357 if (rc > out_end - out_ptr)
1365 static void zap_process(struct task_struct *start)
1367 struct task_struct *t;
1369 start->signal->flags = SIGNAL_GROUP_EXIT;
1370 start->signal->group_stop_count = 0;
1374 if (t != current && t->mm) {
1375 t->mm->core_waiters++;
1376 sigaddset(&t->pending.signal, SIGKILL);
1377 signal_wake_up(t, 1);
1379 } while ((t = next_thread(t)) != start);
1382 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1385 struct task_struct *g, *p;
1386 unsigned long flags;
1389 spin_lock_irq(&tsk->sighand->siglock);
1390 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
1391 tsk->signal->group_exit_code = exit_code;
1395 spin_unlock_irq(&tsk->sighand->siglock);
1399 if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
1403 for_each_process(g) {
1404 if (g == tsk->group_leader)
1412 * p->sighand can't disappear, but
1413 * may be changed by de_thread()
1415 lock_task_sighand(p, &flags);
1417 unlock_task_sighand(p, &flags);
1421 } while ((p = next_thread(p)) != g);
1425 return mm->core_waiters;
1428 static int coredump_wait(int exit_code)
1430 struct task_struct *tsk = current;
1431 struct mm_struct *mm = tsk->mm;
1432 struct completion startup_done;
1433 struct completion *vfork_done;
1436 init_completion(&mm->core_done);
1437 init_completion(&startup_done);
1438 mm->core_startup_done = &startup_done;
1440 core_waiters = zap_threads(tsk, mm, exit_code);
1441 up_write(&mm->mmap_sem);
1443 if (unlikely(core_waiters < 0))
1447 * Make sure nobody is waiting for us to release the VM,
1448 * otherwise we can deadlock when we wait on each other
1450 vfork_done = tsk->vfork_done;
1452 tsk->vfork_done = NULL;
1453 complete(vfork_done);
1457 wait_for_completion(&startup_done);
1459 BUG_ON(mm->core_waiters);
1460 return core_waiters;
1463 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1465 char corename[CORENAME_MAX_SIZE + 1];
1466 struct mm_struct *mm = current->mm;
1467 struct linux_binfmt * binfmt;
1468 struct inode * inode;
1471 int fsuid = current->fsuid;
1474 binfmt = current->binfmt;
1475 if (!binfmt || !binfmt->core_dump)
1477 if (current->tux_exit)
1478 current->tux_exit();
1479 down_write(&mm->mmap_sem);
1480 if (!mm->dumpable) {
1481 up_write(&mm->mmap_sem);
1486 * We cannot trust fsuid as being the "true" uid of the
1487 * process nor do we know its entire history. We only know it
1488 * was tainted so we dump it as root in mode 2.
1490 if (mm->dumpable == 2) { /* Setuid core dump mode */
1491 flag = O_EXCL; /* Stop rewrite attacks */
1492 current->fsuid = 0; /* Dump root private */
1496 retval = coredump_wait(exit_code);
1501 * Clear any false indication of pending signals that might
1502 * be seen by the filesystem code called to write the core file.
1504 clear_thread_flag(TIF_SIGPENDING);
1506 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1510 * lock_kernel() because format_corename() is controlled by sysctl, which
1511 * uses lock_kernel()
1514 format_corename(corename, core_pattern, signr);
1516 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1519 inode = file->f_dentry->d_inode;
1520 if (inode->i_nlink > 1)
1521 goto close_fail; /* multiple links - don't dump */
1522 if (d_unhashed(file->f_dentry))
1525 if (!S_ISREG(inode->i_mode))
1529 if (!file->f_op->write)
1531 if (do_truncate(file->f_dentry, 0, 0, file) != 0)
1534 retval = binfmt->core_dump(signr, regs, file);
1537 current->signal->group_exit_code |= 0x80;
1539 filp_close(file, NULL);
1541 current->fsuid = fsuid;
1542 complete_all(&mm->core_done);