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/acct.h>
51 #include <linux/cn_proc.h>
53 #include <asm/uaccess.h>
54 #include <asm/mmu_context.h>
57 #include <linux/kmod.h>
61 char core_pattern[65] = "core";
62 int suid_dumpable = 0;
64 EXPORT_SYMBOL(suid_dumpable);
65 /* The maximal length of core_pattern is also specified in sysctl.c */
67 static struct linux_binfmt *formats;
68 static DEFINE_RWLOCK(binfmt_lock);
70 int register_binfmt(struct linux_binfmt * fmt)
72 struct linux_binfmt ** tmp = &formats;
78 write_lock(&binfmt_lock);
81 write_unlock(&binfmt_lock);
88 write_unlock(&binfmt_lock);
92 EXPORT_SYMBOL(register_binfmt);
94 int unregister_binfmt(struct linux_binfmt * fmt)
96 struct linux_binfmt ** tmp = &formats;
98 write_lock(&binfmt_lock);
102 write_unlock(&binfmt_lock);
107 write_unlock(&binfmt_lock);
111 EXPORT_SYMBOL(unregister_binfmt);
113 static inline void put_binfmt(struct linux_binfmt * fmt)
115 module_put(fmt->module);
119 * Note that a shared library must be both readable and executable due to
122 * Also note that we take the address to load from from the file itself.
124 asmlinkage long sys_uselib(const char __user * library)
130 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
135 if (!S_ISREG(nd.dentry->d_inode->i_mode))
138 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
142 file = nameidata_to_filp(&nd, O_RDONLY);
143 error = PTR_ERR(file);
149 struct linux_binfmt * fmt;
151 read_lock(&binfmt_lock);
152 for (fmt = formats ; fmt ; fmt = fmt->next) {
153 if (!fmt->load_shlib)
155 if (!try_module_get(fmt->module))
157 read_unlock(&binfmt_lock);
158 error = fmt->load_shlib(file);
159 read_lock(&binfmt_lock);
161 if (error != -ENOEXEC)
164 read_unlock(&binfmt_lock);
170 release_open_intent(&nd);
176 * count() counts the number of strings in array ARGV.
178 static int count(char __user * __user * argv, int max)
186 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 static int copy_strings(int argc, char __user * __user * argv,
205 struct linux_binprm *bprm)
207 struct page *kmapped_page = NULL;
216 if (get_user(str, argv+argc) ||
217 !(len = strnlen_user(str, bprm->p))) {
228 /* XXX: add architecture specific overflow check here. */
233 int offset, bytes_to_copy;
236 offset = pos % PAGE_SIZE;
238 page = bprm->page[i];
241 page = alloc_page(GFP_HIGHUSER);
242 bprm->page[i] = page;
250 if (page != kmapped_page) {
252 kunmap(kmapped_page);
254 kaddr = kmap(kmapped_page);
257 memset(kaddr, 0, offset);
258 bytes_to_copy = PAGE_SIZE - offset;
259 if (bytes_to_copy > len) {
262 memset(kaddr+offset+len, 0,
263 PAGE_SIZE-offset-len);
265 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
271 pos += bytes_to_copy;
272 str += bytes_to_copy;
273 len -= bytes_to_copy;
279 kunmap(kmapped_page);
284 * Like copy_strings, but get argv and its values from kernel memory.
286 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
289 mm_segment_t oldfs = get_fs();
291 r = copy_strings(argc, (char __user * __user *)argv, bprm);
296 EXPORT_SYMBOL(copy_strings_kernel);
300 * This routine is used to map in a page into an address space: needed by
301 * execve() for the initial stack and environment pages.
303 * vma->vm_mm->mmap_sem is held for writing.
305 void install_arg_page(struct vm_area_struct *vma,
306 struct page *page, unsigned long address)
308 struct mm_struct *mm = vma->vm_mm;
315 if (unlikely(anon_vma_prepare(vma)))
318 flush_dcache_page(page);
319 pgd = pgd_offset(mm, address);
320 pud = pud_alloc(mm, pgd, address);
323 pmd = pmd_alloc(mm, pud, address);
326 pte = pte_alloc_map_lock(mm, pmd, address, &ptl);
329 if (!pte_none(*pte)) {
330 pte_unmap_unlock(pte, ptl);
333 inc_mm_counter(mm, anon_rss);
334 lru_cache_add_active(page);
335 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
336 page, vma->vm_page_prot))));
337 page_add_anon_rmap(page, vma, address);
338 pte_unmap_unlock(pte, ptl);
340 /* no need for flush_tlb */
344 force_sig(SIGKILL, current);
347 #define EXTRA_STACK_VM_PAGES 20 /* random */
349 int setup_arg_pages(struct linux_binprm *bprm,
350 unsigned long stack_top,
351 int executable_stack)
353 unsigned long stack_base;
354 struct vm_area_struct *mpnt;
355 struct mm_struct *mm = current->mm;
359 #ifdef CONFIG_STACK_GROWSUP
360 /* Move the argument and environment strings to the bottom of the
366 /* Start by shifting all the pages down */
368 for (j = 0; j < MAX_ARG_PAGES; j++) {
369 struct page *page = bprm->page[j];
372 bprm->page[i++] = page;
375 /* Now move them within their pages */
376 offset = bprm->p % PAGE_SIZE;
377 to = kmap(bprm->page[0]);
378 for (j = 1; j < i; j++) {
379 memmove(to, to + offset, PAGE_SIZE - offset);
380 from = kmap(bprm->page[j]);
381 memcpy(to + PAGE_SIZE - offset, from, offset);
382 kunmap(bprm->page[j - 1]);
385 memmove(to, to + offset, PAGE_SIZE - offset);
386 kunmap(bprm->page[j - 1]);
388 /* Limit stack size to 1GB */
389 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
390 if (stack_base > (1 << 30))
391 stack_base = 1 << 30;
392 stack_base = PAGE_ALIGN(stack_top - stack_base);
394 /* Adjust bprm->p to point to the end of the strings. */
395 bprm->p = stack_base + PAGE_SIZE * i - offset;
397 mm->arg_start = stack_base;
398 arg_size = i << PAGE_SHIFT;
400 /* zero pages that were copied above */
401 while (i < MAX_ARG_PAGES)
402 bprm->page[i++] = NULL;
404 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
405 stack_base = PAGE_ALIGN(stack_base);
406 bprm->p += stack_base;
407 mm->arg_start = bprm->p;
408 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
411 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
414 bprm->loader += stack_base;
415 bprm->exec += stack_base;
417 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
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 = stack_base + arg_size;
430 mpnt->vm_end = stack_top;
431 mpnt->vm_start = mpnt->vm_end - arg_size;
433 /* Adjust stack execute permissions; explicitly enable
434 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
435 * and leave alone (arch default) otherwise. */
436 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
437 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
438 else if (executable_stack == EXSTACK_DISABLE_X)
439 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
441 mpnt->vm_flags = VM_STACK_FLAGS;
442 mpnt->vm_flags |= mm->def_flags;
443 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
444 if ((ret = insert_vm_struct(mm, mpnt))) {
445 up_write(&mm->mmap_sem);
446 kmem_cache_free(vm_area_cachep, mpnt);
449 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
452 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
453 struct page *page = bprm->page[i];
455 bprm->page[i] = NULL;
456 install_arg_page(mpnt, page, stack_base);
458 stack_base += PAGE_SIZE;
460 up_write(&mm->mmap_sem);
465 EXPORT_SYMBOL(setup_arg_pages);
467 #define free_arg_pages(bprm) do { } while (0)
471 static inline void free_arg_pages(struct linux_binprm *bprm)
475 for (i = 0; i < MAX_ARG_PAGES; i++) {
477 __free_page(bprm->page[i]);
478 bprm->page[i] = NULL;
482 #endif /* CONFIG_MMU */
484 struct file *open_exec(const char *name)
490 err = path_lookup_open(name, LOOKUP_FOLLOW, &nd, FMODE_READ);
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 = vfs_permission(&nd, MAY_EXEC);
499 if (!err && !(inode->i_mode & 0111))
503 file = nameidata_to_filp(&nd, O_RDONLY);
505 err = deny_write_access(file);
515 release_open_intent(&nd);
521 EXPORT_SYMBOL(open_exec);
523 int kernel_read(struct file *file, unsigned long offset,
524 char *addr, unsigned long count)
532 /* The cast to a user pointer is valid due to the set_fs() */
533 result = vfs_read(file, (void __user *)addr, count, &pos);
538 EXPORT_SYMBOL(kernel_read);
540 static int exec_mmap(struct mm_struct *mm)
542 struct task_struct *tsk;
543 struct mm_struct * old_mm, *active_mm;
545 /* Notify parent that we're no longer interested in the old VM */
547 old_mm = current->mm;
548 mm_release(tsk, old_mm);
552 * Make sure that if there is a core dump in progress
553 * for the old mm, we get out and die instead of going
554 * through with the exec. We must hold mmap_sem around
555 * checking core_waiters and changing tsk->mm. The
556 * core-inducing thread will increment core_waiters for
557 * each thread whose ->mm == old_mm.
559 down_read(&old_mm->mmap_sem);
560 if (unlikely(old_mm->core_waiters)) {
561 up_read(&old_mm->mmap_sem);
566 active_mm = tsk->active_mm;
569 activate_mm(active_mm, mm);
571 arch_pick_mmap_layout(mm);
573 up_read(&old_mm->mmap_sem);
574 if (active_mm != old_mm) BUG();
583 * This function makes sure the current process has its own signal table,
584 * so that flush_signal_handlers can later reset the handlers without
585 * disturbing other processes. (Other processes might share the signal
586 * table via the CLONE_SIGHAND option to clone().)
588 static inline int de_thread(struct task_struct *tsk)
590 struct signal_struct *sig = tsk->signal;
591 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
592 spinlock_t *lock = &oldsighand->siglock;
593 struct task_struct *leader = NULL;
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) {
601 BUG_ON(atomic_read(&sig->count) != 1);
606 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
610 if (thread_group_empty(current))
611 goto no_thread_group;
614 * Kill all other threads in the thread group.
615 * We must hold tasklist_lock to call zap_other_threads.
617 read_lock(&tasklist_lock);
619 if (sig->flags & SIGNAL_GROUP_EXIT) {
621 * Another group action in progress, just
622 * return so that the signal is processed.
624 spin_unlock_irq(lock);
625 read_unlock(&tasklist_lock);
626 kmem_cache_free(sighand_cachep, newsighand);
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.
645 sig->real_timer.data = (unsigned long)current;
646 spin_unlock_irq(lock);
647 if (del_timer_sync(&sig->real_timer))
648 add_timer(&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)) {
669 struct task_struct *parent;
670 struct dentry *proc_dentry1, *proc_dentry2;
671 unsigned long exit_state, ptrace;
674 * Wait for the thread group leader to be a zombie.
675 * It should already be zombie at this point, most
678 leader = current->group_leader;
679 while (leader->exit_state != EXIT_ZOMBIE)
682 spin_lock(&leader->proc_lock);
683 spin_lock(¤t->proc_lock);
684 proc_dentry1 = proc_pid_unhash(current);
685 proc_dentry2 = proc_pid_unhash(leader);
686 write_lock_irq(&tasklist_lock);
688 BUG_ON(leader->tgid != current->tgid);
689 BUG_ON(current->pid == current->tgid);
691 * An exec() starts a new thread group with the
692 * TGID of the previous thread group. Rehash the
693 * two threads with a switched PID, and release
694 * the former thread group leader:
696 ptrace = leader->ptrace;
697 parent = leader->parent;
698 if (unlikely(ptrace) && unlikely(parent == current)) {
700 * Joker was ptracing his own group leader,
701 * and now he wants to be his own parent!
702 * We can't have that.
707 ptrace_unlink(current);
708 ptrace_unlink(leader);
709 remove_parent(current);
710 remove_parent(leader);
712 switch_exec_pids(leader, current);
714 current->parent = current->real_parent = leader->real_parent;
715 leader->parent = leader->real_parent = child_reaper;
716 current->group_leader = current;
717 leader->group_leader = leader;
719 add_parent(current, current->parent);
720 add_parent(leader, leader->parent);
722 current->ptrace = ptrace;
723 __ptrace_link(current, parent);
726 list_del(¤t->tasks);
727 list_add_tail(¤t->tasks, &init_task.tasks);
728 current->exit_signal = SIGCHLD;
729 exit_state = leader->exit_state;
731 write_unlock_irq(&tasklist_lock);
732 spin_unlock(&leader->proc_lock);
733 spin_unlock(¤t->proc_lock);
734 proc_pid_flush(proc_dentry1);
735 proc_pid_flush(proc_dentry2);
737 BUG_ON(exit_state != EXIT_ZOMBIE);
741 * There may be one thread left which is just exiting,
742 * but it's safe to stop telling the group to kill themselves.
749 release_task(leader);
751 BUG_ON(atomic_read(&sig->count) != 1);
753 if (atomic_read(&oldsighand->count) == 1) {
755 * Now that we nuked the rest of the thread group,
756 * it turns out we are not sharing sighand any more either.
757 * So we can just keep it.
759 kmem_cache_free(sighand_cachep, newsighand);
762 * Move our state over to newsighand and switch it in.
764 spin_lock_init(&newsighand->siglock);
765 atomic_set(&newsighand->count, 1);
766 memcpy(newsighand->action, oldsighand->action,
767 sizeof(newsighand->action));
769 write_lock_irq(&tasklist_lock);
770 spin_lock(&oldsighand->siglock);
771 spin_lock(&newsighand->siglock);
773 current->sighand = newsighand;
776 spin_unlock(&newsighand->siglock);
777 spin_unlock(&oldsighand->siglock);
778 write_unlock_irq(&tasklist_lock);
780 if (atomic_dec_and_test(&oldsighand->count))
781 kmem_cache_free(sighand_cachep, oldsighand);
784 BUG_ON(!thread_group_leader(current));
789 * These functions flushes out all traces of the currently running executable
790 * so that a new one can be started
793 static inline void flush_old_files(struct files_struct * files)
798 spin_lock(&files->file_lock);
800 unsigned long set, i;
804 fdt = files_fdtable(files);
805 if (i >= fdt->max_fds || i >= fdt->max_fdset)
807 set = fdt->close_on_exec->fds_bits[j];
810 fdt->close_on_exec->fds_bits[j] = 0;
811 spin_unlock(&files->file_lock);
812 for ( ; set ; i++,set >>= 1) {
817 spin_lock(&files->file_lock);
820 spin_unlock(&files->file_lock);
823 void get_task_comm(char *buf, struct task_struct *tsk)
825 /* buf must be at least sizeof(tsk->comm) in size */
827 strncpy(buf, tsk->comm, sizeof(tsk->comm));
831 void set_task_comm(struct task_struct *tsk, char *buf)
834 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
838 int flush_old_exec(struct linux_binprm * bprm)
842 struct files_struct *files;
843 char tcomm[sizeof(current->comm)];
846 * Make sure we have a private signal table and that
847 * we are unassociated from the previous thread group.
849 retval = de_thread(current);
854 * Make sure we have private file handles. Ask the
855 * fork helper to do the work for us and the exit
856 * helper to do the cleanup of the old one.
858 files = current->files; /* refcounted so safe to hold */
859 retval = unshare_files();
863 * Release all of the old mmap stuff
865 retval = exec_mmap(bprm->mm);
869 bprm->mm = NULL; /* We're using it now */
871 /* This is the point of no return */
873 put_files_struct(files);
875 current->sas_ss_sp = current->sas_ss_size = 0;
877 if (current->euid == current->uid && current->egid == current->gid)
878 current->mm->dumpable = 1;
880 current->mm->dumpable = suid_dumpable;
882 name = bprm->filename;
884 /* Copies the binary name from after last slash */
885 for (i=0; (ch = *(name++)) != '\0';) {
887 i = 0; /* overwrite what we wrote */
889 if (i < (sizeof(tcomm) - 1))
893 set_task_comm(current, tcomm);
895 current->flags &= ~PF_RANDOMIZE;
898 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
899 file_permission(bprm->file, MAY_READ) ||
900 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
902 current->mm->dumpable = suid_dumpable;
905 /* An exec changes our domain. We are no longer part of the thread
908 current->self_exec_id++;
910 flush_signal_handlers(current, 0);
911 flush_old_files(current->files);
916 put_files_struct(current->files);
917 current->files = files;
922 EXPORT_SYMBOL(flush_old_exec);
925 * Fill the binprm structure from the inode.
926 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
928 int prepare_binprm(struct linux_binprm *bprm)
931 struct inode * inode = bprm->file->f_dentry->d_inode;
934 mode = inode->i_mode;
936 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
937 * generic_permission lets a non-executable through
939 if (!(mode & 0111)) /* with at least _one_ execute bit set */
941 if (bprm->file->f_op == NULL)
944 bprm->e_uid = current->euid;
945 bprm->e_gid = current->egid;
947 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
949 if (mode & S_ISUID) {
950 current->personality &= ~PER_CLEAR_ON_SETID;
951 bprm->e_uid = inode->i_uid;
956 * If setgid is set but no group execute bit then this
957 * is a candidate for mandatory locking, not a setgid
960 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
961 current->personality &= ~PER_CLEAR_ON_SETID;
962 bprm->e_gid = inode->i_gid;
966 /* fill in binprm security blob */
967 retval = security_bprm_set(bprm);
971 memset(bprm->buf,0,BINPRM_BUF_SIZE);
972 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
975 EXPORT_SYMBOL(prepare_binprm);
977 static inline int unsafe_exec(struct task_struct *p)
980 if (p->ptrace & PT_PTRACED) {
981 if (p->ptrace & PT_PTRACE_CAP)
982 unsafe |= LSM_UNSAFE_PTRACE_CAP;
984 unsafe |= LSM_UNSAFE_PTRACE;
986 if (atomic_read(&p->fs->count) > 1 ||
987 atomic_read(&p->files->count) > 1 ||
988 atomic_read(&p->sighand->count) > 1)
989 unsafe |= LSM_UNSAFE_SHARE;
994 void compute_creds(struct linux_binprm *bprm)
998 if (bprm->e_uid != current->uid)
1003 unsafe = unsafe_exec(current);
1004 security_bprm_apply_creds(bprm, unsafe);
1005 task_unlock(current);
1006 security_bprm_post_apply_creds(bprm);
1009 EXPORT_SYMBOL(compute_creds);
1011 void remove_arg_zero(struct linux_binprm *bprm)
1014 unsigned long offset;
1018 offset = bprm->p % PAGE_SIZE;
1021 while (bprm->p++, *(kaddr+offset++)) {
1022 if (offset != PAGE_SIZE)
1025 kunmap_atomic(kaddr, KM_USER0);
1027 page = bprm->page[bprm->p/PAGE_SIZE];
1028 kaddr = kmap_atomic(page, KM_USER0);
1030 kunmap_atomic(kaddr, KM_USER0);
1035 EXPORT_SYMBOL(remove_arg_zero);
1038 * cycle the list of binary formats handler, until one recognizes the image
1040 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1043 struct linux_binfmt *fmt;
1045 /* handle /sbin/loader.. */
1047 struct exec * eh = (struct exec *) bprm->buf;
1049 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1050 (eh->fh.f_flags & 0x3000) == 0x3000)
1053 unsigned long loader;
1055 allow_write_access(bprm->file);
1059 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1061 file = open_exec("/sbin/loader");
1062 retval = PTR_ERR(file);
1066 /* Remember if the application is TASO. */
1067 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1070 bprm->loader = loader;
1071 retval = prepare_binprm(bprm);
1074 /* should call search_binary_handler recursively here,
1075 but it does not matter */
1079 retval = security_bprm_check(bprm);
1083 /* kernel module loader fixup */
1084 /* so we don't try to load run modprobe in kernel space. */
1087 for (try=0; try<2; try++) {
1088 read_lock(&binfmt_lock);
1089 for (fmt = formats ; fmt ; fmt = fmt->next) {
1090 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1093 if (!try_module_get(fmt->module))
1095 read_unlock(&binfmt_lock);
1096 retval = fn(bprm, regs);
1099 allow_write_access(bprm->file);
1103 current->did_exec = 1;
1104 proc_exec_connector(current);
1107 read_lock(&binfmt_lock);
1109 if (retval != -ENOEXEC || bprm->mm == NULL)
1112 read_unlock(&binfmt_lock);
1116 read_unlock(&binfmt_lock);
1117 if (retval != -ENOEXEC || bprm->mm == NULL) {
1121 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1122 if (printable(bprm->buf[0]) &&
1123 printable(bprm->buf[1]) &&
1124 printable(bprm->buf[2]) &&
1125 printable(bprm->buf[3]))
1126 break; /* -ENOEXEC */
1127 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1134 EXPORT_SYMBOL(search_binary_handler);
1137 * sys_execve() executes a new program.
1139 int do_execve(char * filename,
1140 char __user *__user *argv,
1141 char __user *__user *envp,
1142 struct pt_regs * regs)
1144 struct linux_binprm *bprm;
1150 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1153 memset(bprm, 0, sizeof(*bprm));
1155 file = open_exec(filename);
1156 retval = PTR_ERR(file);
1162 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1165 bprm->filename = filename;
1166 bprm->interp = filename;
1167 bprm->mm = mm_alloc();
1172 retval = init_new_context(current, bprm->mm);
1176 bprm->argc = count(argv, bprm->p / sizeof(void *));
1177 if ((retval = bprm->argc) < 0)
1180 bprm->envc = count(envp, bprm->p / sizeof(void *));
1181 if ((retval = bprm->envc) < 0)
1184 retval = security_bprm_alloc(bprm);
1188 retval = prepare_binprm(bprm);
1192 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1196 bprm->exec = bprm->p;
1197 retval = copy_strings(bprm->envc, envp, bprm);
1201 retval = copy_strings(bprm->argc, argv, bprm);
1205 retval = search_binary_handler(bprm,regs);
1207 free_arg_pages(bprm);
1209 /* execve success */
1210 security_bprm_free(bprm);
1211 acct_update_integrals(current);
1217 /* Something went wrong, return the inode and free the argument pages*/
1218 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1219 struct page * page = bprm->page[i];
1225 security_bprm_free(bprm);
1233 allow_write_access(bprm->file);
1244 int set_binfmt(struct linux_binfmt *new)
1246 struct linux_binfmt *old = current->binfmt;
1249 if (!try_module_get(new->module))
1252 current->binfmt = new;
1254 module_put(old->module);
1258 EXPORT_SYMBOL(set_binfmt);
1260 #define CORENAME_MAX_SIZE 64
1262 /* format_corename will inspect the pattern parameter, and output a
1263 * name into corename, which must have space for at least
1264 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1266 static void format_corename(char *corename, const char *pattern, long signr)
1268 const char *pat_ptr = pattern;
1269 char *out_ptr = corename;
1270 char *const out_end = corename + CORENAME_MAX_SIZE;
1272 int pid_in_pattern = 0;
1274 /* Repeat as long as we have more pattern to process and more output
1277 if (*pat_ptr != '%') {
1278 if (out_ptr == out_end)
1280 *out_ptr++ = *pat_ptr++;
1282 switch (*++pat_ptr) {
1285 /* Double percent, output one percent */
1287 if (out_ptr == out_end)
1294 rc = snprintf(out_ptr, out_end - out_ptr,
1295 "%d", current->tgid);
1296 if (rc > out_end - out_ptr)
1302 rc = snprintf(out_ptr, out_end - out_ptr,
1303 "%d", current->uid);
1304 if (rc > out_end - out_ptr)
1310 rc = snprintf(out_ptr, out_end - out_ptr,
1311 "%d", current->gid);
1312 if (rc > out_end - out_ptr)
1316 /* signal that caused the coredump */
1318 rc = snprintf(out_ptr, out_end - out_ptr,
1320 if (rc > out_end - out_ptr)
1324 /* UNIX time of coredump */
1327 do_gettimeofday(&tv);
1328 rc = snprintf(out_ptr, out_end - out_ptr,
1330 if (rc > out_end - out_ptr)
1337 down_read(&uts_sem);
1338 rc = snprintf(out_ptr, out_end - out_ptr,
1339 "%s", system_utsname.nodename);
1341 if (rc > out_end - out_ptr)
1347 rc = snprintf(out_ptr, out_end - out_ptr,
1348 "%s", current->comm);
1349 if (rc > out_end - out_ptr)
1359 /* Backward compatibility with core_uses_pid:
1361 * If core_pattern does not include a %p (as is the default)
1362 * and core_uses_pid is set, then .%pid will be appended to
1365 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1366 rc = snprintf(out_ptr, out_end - out_ptr,
1367 ".%d", current->tgid);
1368 if (rc > out_end - out_ptr)
1376 static void zap_threads (struct mm_struct *mm)
1378 struct task_struct *g, *p;
1379 struct task_struct *tsk = current;
1380 struct completion *vfork_done = tsk->vfork_done;
1384 * Make sure nobody is waiting for us to release the VM,
1385 * otherwise we can deadlock when we wait on each other
1388 tsk->vfork_done = NULL;
1389 complete(vfork_done);
1392 read_lock(&tasklist_lock);
1394 if (mm == p->mm && p != tsk) {
1395 force_sig_specific(SIGKILL, p);
1397 if (unlikely(p->ptrace) &&
1398 unlikely(p->parent->mm == mm))
1401 while_each_thread(g,p);
1403 read_unlock(&tasklist_lock);
1405 if (unlikely(traced)) {
1407 * We are zapping a thread and the thread it ptraces.
1408 * If the tracee went into a ptrace stop for exit tracing,
1409 * we could deadlock since the tracer is waiting for this
1410 * coredump to finish. Detach them so they can both die.
1412 write_lock_irq(&tasklist_lock);
1413 do_each_thread(g,p) {
1414 if (mm == p->mm && p != tsk &&
1415 p->ptrace && p->parent->mm == mm) {
1418 } while_each_thread(g,p);
1419 write_unlock_irq(&tasklist_lock);
1423 static void coredump_wait(struct mm_struct *mm)
1425 DECLARE_COMPLETION(startup_done);
1428 mm->core_startup_done = &startup_done;
1431 core_waiters = mm->core_waiters;
1432 up_write(&mm->mmap_sem);
1435 wait_for_completion(&startup_done);
1436 BUG_ON(mm->core_waiters);
1439 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1441 char corename[CORENAME_MAX_SIZE + 1];
1442 struct mm_struct *mm = current->mm;
1443 struct linux_binfmt * binfmt;
1444 struct inode * inode;
1447 int fsuid = current->fsuid;
1450 binfmt = current->binfmt;
1451 if (!binfmt || !binfmt->core_dump)
1453 down_write(&mm->mmap_sem);
1454 if (!mm->dumpable) {
1455 up_write(&mm->mmap_sem);
1460 * We cannot trust fsuid as being the "true" uid of the
1461 * process nor do we know its entire history. We only know it
1462 * was tainted so we dump it as root in mode 2.
1464 if (mm->dumpable == 2) { /* Setuid core dump mode */
1465 flag = O_EXCL; /* Stop rewrite attacks */
1466 current->fsuid = 0; /* Dump root private */
1471 spin_lock_irq(¤t->sighand->siglock);
1472 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
1473 current->signal->flags = SIGNAL_GROUP_EXIT;
1474 current->signal->group_exit_code = exit_code;
1477 spin_unlock_irq(¤t->sighand->siglock);
1479 up_write(&mm->mmap_sem);
1483 init_completion(&mm->core_done);
1487 * Clear any false indication of pending signals that might
1488 * be seen by the filesystem code called to write the core file.
1490 current->signal->group_stop_count = 0;
1491 clear_thread_flag(TIF_SIGPENDING);
1493 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1497 * lock_kernel() because format_corename() is controlled by sysctl, which
1498 * uses lock_kernel()
1501 format_corename(corename, core_pattern, signr);
1503 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1506 inode = file->f_dentry->d_inode;
1507 if (inode->i_nlink > 1)
1508 goto close_fail; /* multiple links - don't dump */
1509 if (d_unhashed(file->f_dentry))
1512 if (!S_ISREG(inode->i_mode))
1516 if (!file->f_op->write)
1518 if (do_truncate(file->f_dentry, 0, file) != 0)
1521 retval = binfmt->core_dump(signr, regs, file);
1524 current->signal->group_exit_code |= 0x80;
1526 filp_close(file, NULL);
1528 current->fsuid = fsuid;
1529 complete_all(&mm->core_done);