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_events.h>
51 #include <linux/ckrm_mem_inline.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 ckrm_task_change_mm(tsk, old_mm, mm);
569 if (active_mm != old_mm) BUG();
578 * This function makes sure the current process has its own signal table,
579 * so that flush_signal_handlers can later reset the handlers without
580 * disturbing other processes. (Other processes might share the signal
581 * table via the CLONE_SIGHAND option to clone().)
583 static inline int de_thread(struct task_struct *tsk)
585 struct signal_struct *sig = tsk->signal;
586 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
587 spinlock_t *lock = &oldsighand->siglock;
591 * If we don't share sighandlers, then we aren't sharing anything
592 * and we can just re-use it all.
594 if (atomic_read(&oldsighand->count) <= 1) {
595 BUG_ON(atomic_read(&sig->count) != 1);
600 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
604 if (thread_group_empty(current))
605 goto no_thread_group;
608 * Kill all other threads in the thread group.
609 * We must hold tasklist_lock to call zap_other_threads.
611 read_lock(&tasklist_lock);
613 if (sig->group_exit) {
615 * Another group action in progress, just
616 * return so that the signal is processed.
618 spin_unlock_irq(lock);
619 read_unlock(&tasklist_lock);
620 kmem_cache_free(sighand_cachep, newsighand);
624 zap_other_threads(current);
625 read_unlock(&tasklist_lock);
628 * Account for the thread group leader hanging around:
631 if (current->pid == current->tgid)
633 while (atomic_read(&sig->count) > count) {
634 sig->group_exit_task = current;
635 sig->notify_count = count;
636 __set_current_state(TASK_UNINTERRUPTIBLE);
637 spin_unlock_irq(lock);
641 sig->group_exit_task = NULL;
642 sig->notify_count = 0;
643 spin_unlock_irq(lock);
646 * At this point all other threads have exited, all we have to
647 * do is to wait for the thread group leader to become inactive,
648 * and to assume its PID:
650 if (current->pid != current->tgid) {
651 struct task_struct *leader = current->group_leader, *parent;
652 struct dentry *proc_dentry1, *proc_dentry2;
653 unsigned long exit_state, ptrace;
656 * Wait for the thread group leader to be a zombie.
657 * It should already be zombie at this point, most
660 while (leader->exit_state != EXIT_ZOMBIE)
663 spin_lock(&leader->proc_lock);
664 spin_lock(¤t->proc_lock);
665 proc_dentry1 = proc_pid_unhash(current);
666 proc_dentry2 = proc_pid_unhash(leader);
667 write_lock_irq(&tasklist_lock);
669 if (leader->tgid != current->tgid)
671 if (current->pid == current->tgid)
674 * An exec() starts a new thread group with the
675 * TGID of the previous thread group. Rehash the
676 * two threads with a switched PID, and release
677 * the former thread group leader:
679 ptrace = leader->ptrace;
680 parent = leader->parent;
682 ptrace_unlink(current);
683 ptrace_unlink(leader);
684 remove_parent(current);
685 remove_parent(leader);
687 switch_exec_pids(leader, current);
689 current->parent = current->real_parent = leader->real_parent;
690 leader->parent = leader->real_parent = child_reaper;
691 current->group_leader = current;
692 leader->group_leader = leader;
694 add_parent(current, current->parent);
695 add_parent(leader, leader->parent);
697 current->ptrace = ptrace;
698 __ptrace_link(current, parent);
701 list_del(¤t->tasks);
702 list_add_tail(¤t->tasks, &init_task.tasks);
703 current->exit_signal = SIGCHLD;
704 exit_state = leader->exit_state;
706 write_unlock_irq(&tasklist_lock);
707 spin_unlock(&leader->proc_lock);
708 spin_unlock(¤t->proc_lock);
709 proc_pid_flush(proc_dentry1);
710 proc_pid_flush(proc_dentry2);
712 if (exit_state != EXIT_ZOMBIE)
714 release_task(leader);
718 * Now there are really no other threads at all,
719 * so it's safe to stop telling them to kill themselves.
724 BUG_ON(atomic_read(&sig->count) != 1);
727 if (atomic_read(&oldsighand->count) == 1) {
729 * Now that we nuked the rest of the thread group,
730 * it turns out we are not sharing sighand any more either.
731 * So we can just keep it.
733 kmem_cache_free(sighand_cachep, newsighand);
736 * Move our state over to newsighand and switch it in.
738 spin_lock_init(&newsighand->siglock);
739 atomic_set(&newsighand->count, 1);
740 memcpy(newsighand->action, oldsighand->action,
741 sizeof(newsighand->action));
742 write_lock_irq(&tasklist_lock);
743 spin_lock(&oldsighand->siglock);
744 spin_lock(&newsighand->siglock);
745 current->sighand = newsighand;
747 spin_unlock(&newsighand->siglock);
748 spin_unlock(&oldsighand->siglock);
749 write_unlock_irq(&tasklist_lock);
751 if (atomic_dec_and_test(&oldsighand->count))
752 kmem_cache_free(sighand_cachep, oldsighand);
755 if (!thread_group_empty(current))
757 if (current->tgid != current->pid)
763 * These functions flushes out all traces of the currently running executable
764 * so that a new one can be started
767 static inline void flush_old_files(struct files_struct * files)
771 spin_lock(&files->file_lock);
773 unsigned long set, i;
777 if (i >= files->max_fds || i >= files->max_fdset)
779 set = files->close_on_exec->fds_bits[j];
782 files->close_on_exec->fds_bits[j] = 0;
783 spin_unlock(&files->file_lock);
784 for ( ; set ; i++,set >>= 1) {
789 spin_lock(&files->file_lock);
792 spin_unlock(&files->file_lock);
795 void get_task_comm(char *buf, struct task_struct *tsk)
797 /* buf must be at least sizeof(tsk->comm) in size */
799 memcpy(buf, tsk->comm, sizeof(tsk->comm));
803 void set_task_comm(struct task_struct *tsk, char *buf)
806 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
810 int flush_old_exec(struct linux_binprm * bprm)
814 struct files_struct *files;
815 char tcomm[sizeof(current->comm)];
818 * Make sure we have a private signal table and that
819 * we are unassociated from the previous thread group.
821 retval = de_thread(current);
826 * Make sure we have private file handles. Ask the
827 * fork helper to do the work for us and the exit
828 * helper to do the cleanup of the old one.
830 files = current->files; /* refcounted so safe to hold */
831 retval = unshare_files();
835 * Release all of the old mmap stuff
837 retval = exec_mmap(bprm->mm);
841 bprm->mm = NULL; /* We're using it now */
843 /* This is the point of no return */
845 put_files_struct(files);
847 current->sas_ss_sp = current->sas_ss_size = 0;
849 if (current->euid == current->uid && current->egid == current->gid)
850 current->mm->dumpable = 1;
852 current->mm->dumpable = suid_dumpable;
854 name = bprm->filename;
855 for (i=0; (ch = *(name++)) != '\0';) {
859 if (i < (sizeof(tcomm) - 1))
863 set_task_comm(current, tcomm);
865 current->flags &= ~PF_RELOCEXEC;
868 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
869 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
870 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
872 current->mm->dumpable = suid_dumpable;
875 /* An exec changes our domain. We are no longer part of the thread
878 current->self_exec_id++;
880 flush_signal_handlers(current, 0);
881 flush_old_files(current->files);
886 put_files_struct(current->files);
887 current->files = files;
892 EXPORT_SYMBOL(flush_old_exec);
895 * Fill the binprm structure from the inode.
896 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
898 int prepare_binprm(struct linux_binprm *bprm)
901 struct inode * inode = bprm->file->f_dentry->d_inode;
904 mode = inode->i_mode;
906 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
907 * generic_permission lets a non-executable through
909 if (!(mode & 0111)) /* with at least _one_ execute bit set */
911 if (bprm->file->f_op == NULL)
914 bprm->e_uid = current->euid;
915 bprm->e_gid = current->egid;
917 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
919 if (mode & S_ISUID) {
920 current->personality &= ~PER_CLEAR_ON_SETID;
921 bprm->e_uid = inode->i_uid;
926 * If setgid is set but no group execute bit then this
927 * is a candidate for mandatory locking, not a setgid
930 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
931 current->personality &= ~PER_CLEAR_ON_SETID;
932 bprm->e_gid = inode->i_gid;
936 /* fill in binprm security blob */
937 retval = security_bprm_set(bprm);
941 memset(bprm->buf,0,BINPRM_BUF_SIZE);
942 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
945 EXPORT_SYMBOL(prepare_binprm);
947 static inline int unsafe_exec(struct task_struct *p)
950 if (p->ptrace & PT_PTRACED) {
951 if (p->ptrace & PT_PTRACE_CAP)
952 unsafe |= LSM_UNSAFE_PTRACE_CAP;
954 unsafe |= LSM_UNSAFE_PTRACE;
956 if (atomic_read(&p->fs->count) > 1 ||
957 atomic_read(&p->files->count) > 1 ||
958 atomic_read(&p->sighand->count) > 1)
959 unsafe |= LSM_UNSAFE_SHARE;
964 void compute_creds(struct linux_binprm *bprm)
968 if (bprm->e_uid != current->uid)
973 unsafe = unsafe_exec(current);
974 security_bprm_apply_creds(bprm, unsafe);
975 task_unlock(current);
978 EXPORT_SYMBOL(compute_creds);
980 void remove_arg_zero(struct linux_binprm *bprm)
983 unsigned long offset;
987 offset = bprm->p % PAGE_SIZE;
990 while (bprm->p++, *(kaddr+offset++)) {
991 if (offset != PAGE_SIZE)
994 kunmap_atomic(kaddr, KM_USER0);
996 page = bprm->page[bprm->p/PAGE_SIZE];
997 kaddr = kmap_atomic(page, KM_USER0);
999 kunmap_atomic(kaddr, KM_USER0);
1004 EXPORT_SYMBOL(remove_arg_zero);
1007 * cycle the list of binary formats handler, until one recognizes the image
1009 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1012 struct linux_binfmt *fmt;
1014 /* handle /sbin/loader.. */
1016 struct exec * eh = (struct exec *) bprm->buf;
1018 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1019 (eh->fh.f_flags & 0x3000) == 0x3000)
1022 unsigned long loader;
1024 allow_write_access(bprm->file);
1028 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1030 file = open_exec("/sbin/loader");
1031 retval = PTR_ERR(file);
1035 /* Remember if the application is TASO. */
1036 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1039 bprm->loader = loader;
1040 retval = prepare_binprm(bprm);
1043 /* should call search_binary_handler recursively here,
1044 but it does not matter */
1048 retval = security_bprm_check(bprm);
1052 /* kernel module loader fixup */
1053 /* so we don't try to load run modprobe in kernel space. */
1056 for (try=0; try<2; try++) {
1057 read_lock(&binfmt_lock);
1058 for (fmt = formats ; fmt ; fmt = fmt->next) {
1059 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1062 if (!try_module_get(fmt->module))
1064 read_unlock(&binfmt_lock);
1065 retval = fn(bprm, regs);
1068 allow_write_access(bprm->file);
1072 current->did_exec = 1;
1073 ckrm_cb_exec(bprm->filename);
1076 read_lock(&binfmt_lock);
1078 if (retval != -ENOEXEC || bprm->mm == NULL)
1081 read_unlock(&binfmt_lock);
1085 read_unlock(&binfmt_lock);
1086 if (retval != -ENOEXEC || bprm->mm == NULL) {
1090 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1091 if (printable(bprm->buf[0]) &&
1092 printable(bprm->buf[1]) &&
1093 printable(bprm->buf[2]) &&
1094 printable(bprm->buf[3]))
1095 break; /* -ENOEXEC */
1096 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1103 EXPORT_SYMBOL(search_binary_handler);
1106 * sys_execve() executes a new program.
1108 int do_execve(char * filename,
1109 char __user *__user *argv,
1110 char __user *__user *envp,
1111 struct pt_regs * regs)
1113 struct linux_binprm *bprm;
1119 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1122 memset(bprm, 0, sizeof(*bprm));
1124 file = open_exec(filename);
1125 retval = PTR_ERR(file);
1131 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1134 bprm->filename = filename;
1135 bprm->interp = filename;
1136 bprm->mm = mm_alloc();
1141 retval = init_new_context(current, bprm->mm);
1145 bprm->argc = count(argv, bprm->p / sizeof(void *));
1146 if ((retval = bprm->argc) < 0)
1149 bprm->envc = count(envp, bprm->p / sizeof(void *));
1150 if ((retval = bprm->envc) < 0)
1153 retval = security_bprm_alloc(bprm);
1157 retval = prepare_binprm(bprm);
1161 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1165 bprm->exec = bprm->p;
1166 retval = copy_strings(bprm->envc, envp, bprm);
1170 retval = copy_strings(bprm->argc, argv, bprm);
1174 retval = search_binary_handler(bprm,regs);
1176 free_arg_pages(bprm);
1178 /* execve success */
1179 security_bprm_free(bprm);
1185 /* Something went wrong, return the inode and free the argument pages*/
1186 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1187 struct page * page = bprm->page[i];
1193 security_bprm_free(bprm);
1201 allow_write_access(bprm->file);
1212 int set_binfmt(struct linux_binfmt *new)
1214 struct linux_binfmt *old = current->binfmt;
1217 if (!try_module_get(new->module))
1220 current->binfmt = new;
1222 module_put(old->module);
1226 EXPORT_SYMBOL(set_binfmt);
1228 #define CORENAME_MAX_SIZE 64
1230 /* format_corename will inspect the pattern parameter, and output a
1231 * name into corename, which must have space for at least
1232 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1234 static void format_corename(char *corename, const char *pattern, long signr)
1236 const char *pat_ptr = pattern;
1237 char *out_ptr = corename;
1238 char *const out_end = corename + CORENAME_MAX_SIZE;
1240 int pid_in_pattern = 0;
1242 /* Repeat as long as we have more pattern to process and more output
1245 if (*pat_ptr != '%') {
1246 if (out_ptr == out_end)
1248 *out_ptr++ = *pat_ptr++;
1250 switch (*++pat_ptr) {
1253 /* Double percent, output one percent */
1255 if (out_ptr == out_end)
1262 rc = snprintf(out_ptr, out_end - out_ptr,
1263 "%d", current->tgid);
1264 if (rc > out_end - out_ptr)
1270 rc = snprintf(out_ptr, out_end - out_ptr,
1271 "%d", current->uid);
1272 if (rc > out_end - out_ptr)
1278 rc = snprintf(out_ptr, out_end - out_ptr,
1279 "%d", current->gid);
1280 if (rc > out_end - out_ptr)
1284 /* signal that caused the coredump */
1286 rc = snprintf(out_ptr, out_end - out_ptr,
1288 if (rc > out_end - out_ptr)
1292 /* UNIX time of coredump */
1295 do_gettimeofday(&tv);
1296 rc = snprintf(out_ptr, out_end - out_ptr,
1298 if (rc > out_end - out_ptr)
1305 down_read(&uts_sem);
1306 rc = snprintf(out_ptr, out_end - out_ptr,
1307 "%s", system_utsname.nodename);
1309 if (rc > out_end - out_ptr)
1315 rc = snprintf(out_ptr, out_end - out_ptr,
1316 "%s", current->comm);
1317 if (rc > out_end - out_ptr)
1327 /* Backward compatibility with core_uses_pid:
1329 * If core_pattern does not include a %p (as is the default)
1330 * and core_uses_pid is set, then .%pid will be appended to
1333 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1334 rc = snprintf(out_ptr, out_end - out_ptr,
1335 ".%d", current->tgid);
1336 if (rc > out_end - out_ptr)
1344 static void zap_threads (struct mm_struct *mm)
1346 struct task_struct *g, *p;
1347 struct task_struct *tsk = current;
1348 struct completion *vfork_done = tsk->vfork_done;
1352 * Make sure nobody is waiting for us to release the VM,
1353 * otherwise we can deadlock when we wait on each other
1356 tsk->vfork_done = NULL;
1357 complete(vfork_done);
1360 read_lock(&tasklist_lock);
1362 if (mm == p->mm && p != tsk) {
1363 force_sig_specific(SIGKILL, p);
1365 if (unlikely(p->ptrace) &&
1366 unlikely(p->parent->mm == mm))
1369 while_each_thread(g,p);
1371 read_unlock(&tasklist_lock);
1373 while (unlikely(traced)) {
1375 * We are zapping a thread and the thread it ptraces.
1376 * The tracee won't come out of TASK_TRACED state until
1377 * its ptracer detaches. That happens when the ptracer
1378 * dies, but it synchronizes with us and so won't get
1379 * that far until we finish the core dump. If we're
1380 * waiting for the tracee to synchronize but it stays
1381 * blocked in TASK_TRACED, then we deadlock. So, for
1382 * this weirdo case we have to do another round with
1383 * tasklist_lock write-locked to __ptrace_unlink the
1384 * children that might cause this deadlock. That will
1385 * wake them up to process their pending SIGKILL.
1387 * First, give everyone we just killed a chance to run
1388 * so they can all get into the coredump synchronization.
1389 * That should leave only the TASK_TRACED stragglers for
1390 * us to wake up. If a ptracer is still running, we'll
1391 * have to come around again after letting it finish.
1395 write_lock_irq(&tasklist_lock);
1396 do_each_thread(g,p) {
1397 if (mm != p->mm || p == tsk ||
1398 !p->ptrace || p->parent->mm != mm)
1400 if ((p->parent->flags & (PF_SIGNALED|PF_EXITING)) ||
1401 (p->parent->state & (TASK_TRACED|TASK_STOPPED))) {
1403 * The parent is in the process of exiting
1404 * itself, or else it's stopped right now.
1405 * It cannot be in a ptrace call, and would
1406 * have to read_lock tasklist_lock before
1407 * it could start one, so we are safe here.
1412 * Blargh! The ptracer is not dying
1413 * yet, so we cannot be sure that it
1414 * isn't in the middle of a ptrace call.
1415 * We'll have to let it run to get into
1416 * coredump_wait and come around another
1417 * time to detach its tracee.
1421 } while_each_thread(g,p);
1422 write_unlock_irq(&tasklist_lock);
1426 static void coredump_wait(struct mm_struct *mm)
1428 DECLARE_COMPLETION(startup_done);
1430 mm->core_waiters++; /* let other threads block */
1431 mm->core_startup_done = &startup_done;
1433 /* give other threads a chance to run: */
1437 if (--mm->core_waiters) {
1438 up_write(&mm->mmap_sem);
1439 wait_for_completion(&startup_done);
1441 up_write(&mm->mmap_sem);
1442 BUG_ON(mm->core_waiters);
1445 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1447 char corename[CORENAME_MAX_SIZE + 1];
1448 struct mm_struct *mm = current->mm;
1449 struct linux_binfmt * binfmt;
1450 struct inode * inode;
1453 int fsuid = current->fsuid;
1456 binfmt = current->binfmt;
1457 if (!binfmt || !binfmt->core_dump)
1459 if (current->tux_exit)
1460 current->tux_exit();
1461 down_write(&mm->mmap_sem);
1462 if (!mm->dumpable) {
1463 up_write(&mm->mmap_sem);
1468 * We cannot trust fsuid as being the "true" uid of the
1469 * process nor do we know its entire history. We only know it
1470 * was tainted so we dump it as root in mode 2.
1472 if (mm->dumpable == 2) { /* Setuid core dump mode */
1473 flag = O_EXCL; /* Stop rewrite attacks */
1474 current->fsuid = 0; /* Dump root private */
1477 init_completion(&mm->core_done);
1478 current->signal->group_exit = 1;
1479 current->signal->group_exit_code = exit_code;
1482 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1486 * lock_kernel() because format_corename() is controlled by sysctl, which
1487 * uses lock_kernel()
1490 format_corename(corename, core_pattern, signr);
1492 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1495 inode = file->f_dentry->d_inode;
1496 if (inode->i_nlink > 1)
1497 goto close_fail; /* multiple links - don't dump */
1498 if (d_unhashed(file->f_dentry))
1501 if (!S_ISREG(inode->i_mode))
1505 if (!file->f_op->write)
1507 if (do_truncate(file->f_dentry, 0) != 0)
1510 retval = binfmt->core_dump(signr, regs, file);
1513 current->signal->group_exit_code |= 0x80;
1515 filp_close(file, NULL);
1517 current->fsuid = fsuid;
1518 complete_all(&mm->core_done);