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/fdtable.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/smp_lock.h>
32 #include <linux/swap.h>
33 #include <linux/string.h>
34 #include <linux/init.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/utsname.h>
41 #include <linux/pid_namespace.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/tsacct_kern.h>
50 #include <linux/cn_proc.h>
51 #include <linux/audit.h>
53 #include <asm/uaccess.h>
54 #include <asm/mmu_context.h>
58 #include <linux/kmod.h>
62 /* for /sbin/loader handling in search_binary_handler() */
63 #include <linux/a.out.h>
67 char core_pattern[CORENAME_MAX_SIZE] = "core";
68 int suid_dumpable = 0;
70 /* The maximal length of core_pattern is also specified in sysctl.c */
72 static LIST_HEAD(formats);
73 static DEFINE_RWLOCK(binfmt_lock);
75 int register_binfmt(struct linux_binfmt * fmt)
79 write_lock(&binfmt_lock);
80 list_add(&fmt->lh, &formats);
81 write_unlock(&binfmt_lock);
85 EXPORT_SYMBOL(register_binfmt);
87 void unregister_binfmt(struct linux_binfmt * fmt)
89 write_lock(&binfmt_lock);
91 write_unlock(&binfmt_lock);
94 EXPORT_SYMBOL(unregister_binfmt);
96 static inline void put_binfmt(struct linux_binfmt * fmt)
98 module_put(fmt->module);
102 * Note that a shared library must be both readable and executable due to
105 * Also note that we take the address to load from from the file itself.
107 asmlinkage long sys_uselib(const char __user * library)
113 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
118 if (!S_ISREG(nd.path.dentry->d_inode->i_mode))
121 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
125 file = nameidata_to_filp(&nd, O_RDONLY|O_LARGEFILE);
126 error = PTR_ERR(file);
132 struct linux_binfmt * fmt;
134 read_lock(&binfmt_lock);
135 list_for_each_entry(fmt, &formats, lh) {
136 if (!fmt->load_shlib)
138 if (!try_module_get(fmt->module))
140 read_unlock(&binfmt_lock);
141 error = fmt->load_shlib(file);
142 read_lock(&binfmt_lock);
144 if (error != -ENOEXEC)
147 read_unlock(&binfmt_lock);
153 release_open_intent(&nd);
160 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
166 #ifdef CONFIG_STACK_GROWSUP
168 ret = expand_stack_downwards(bprm->vma, pos);
173 ret = get_user_pages(current, bprm->mm, pos,
174 1, write, 1, &page, NULL);
179 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
183 * We've historically supported up to 32 pages (ARG_MAX)
184 * of argument strings even with small stacks
190 * Limit to 1/4-th the stack size for the argv+env strings.
192 * - the remaining binfmt code will not run out of stack space,
193 * - the program will have a reasonable amount of stack left
196 rlim = current->signal->rlim;
197 if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
206 static void put_arg_page(struct page *page)
211 static void free_arg_page(struct linux_binprm *bprm, int i)
215 static void free_arg_pages(struct linux_binprm *bprm)
219 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
222 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
225 static int __bprm_mm_init(struct linux_binprm *bprm)
228 struct vm_area_struct *vma = NULL;
229 struct mm_struct *mm = bprm->mm;
231 bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
235 down_write(&mm->mmap_sem);
239 * Place the stack at the largest stack address the architecture
240 * supports. Later, we'll move this to an appropriate place. We don't
241 * use STACK_TOP because that can depend on attributes which aren't
244 vma->vm_end = STACK_TOP_MAX;
245 vma->vm_start = vma->vm_end - PAGE_SIZE;
247 vma->vm_flags = VM_STACK_FLAGS;
248 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
249 err = insert_vm_struct(mm, vma);
251 up_write(&mm->mmap_sem);
255 mm->stack_vm = mm->total_vm = 1;
256 up_write(&mm->mmap_sem);
258 bprm->p = vma->vm_end - sizeof(void *);
265 kmem_cache_free(vm_area_cachep, vma);
271 static bool valid_arg_len(struct linux_binprm *bprm, long len)
273 return len <= MAX_ARG_STRLEN;
278 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
283 page = bprm->page[pos / PAGE_SIZE];
284 if (!page && write) {
285 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
288 bprm->page[pos / PAGE_SIZE] = page;
294 static void put_arg_page(struct page *page)
298 static void free_arg_page(struct linux_binprm *bprm, int i)
301 __free_page(bprm->page[i]);
302 bprm->page[i] = NULL;
306 static void free_arg_pages(struct linux_binprm *bprm)
310 for (i = 0; i < MAX_ARG_PAGES; i++)
311 free_arg_page(bprm, i);
314 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
319 static int __bprm_mm_init(struct linux_binprm *bprm)
321 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
325 static bool valid_arg_len(struct linux_binprm *bprm, long len)
327 return len <= bprm->p;
330 #endif /* CONFIG_MMU */
333 * Create a new mm_struct and populate it with a temporary stack
334 * vm_area_struct. We don't have enough context at this point to set the stack
335 * flags, permissions, and offset, so we use temporary values. We'll update
336 * them later in setup_arg_pages().
338 int bprm_mm_init(struct linux_binprm *bprm)
341 struct mm_struct *mm = NULL;
343 bprm->mm = mm = mm_alloc();
348 err = init_new_context(current, mm);
352 err = __bprm_mm_init(bprm);
368 * count() counts the number of strings in array ARGV.
370 static int count(char __user * __user * argv, int max)
378 if (get_user(p, argv))
392 * 'copy_strings()' copies argument/environment strings from the old
393 * processes's memory to the new process's stack. The call to get_user_pages()
394 * ensures the destination page is created and not swapped out.
396 static int copy_strings(int argc, char __user * __user * argv,
397 struct linux_binprm *bprm)
399 struct page *kmapped_page = NULL;
401 unsigned long kpos = 0;
409 if (get_user(str, argv+argc) ||
410 !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
415 if (!valid_arg_len(bprm, len)) {
420 /* We're going to work our way backwords. */
426 int offset, bytes_to_copy;
428 offset = pos % PAGE_SIZE;
432 bytes_to_copy = offset;
433 if (bytes_to_copy > len)
436 offset -= bytes_to_copy;
437 pos -= bytes_to_copy;
438 str -= bytes_to_copy;
439 len -= bytes_to_copy;
441 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
444 page = get_arg_page(bprm, pos, 1);
451 flush_kernel_dcache_page(kmapped_page);
452 kunmap(kmapped_page);
453 put_arg_page(kmapped_page);
456 kaddr = kmap(kmapped_page);
457 kpos = pos & PAGE_MASK;
458 flush_arg_page(bprm, kpos, kmapped_page);
460 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
469 flush_kernel_dcache_page(kmapped_page);
470 kunmap(kmapped_page);
471 put_arg_page(kmapped_page);
477 * Like copy_strings, but get argv and its values from kernel memory.
479 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
482 mm_segment_t oldfs = get_fs();
484 r = copy_strings(argc, (char __user * __user *)argv, bprm);
488 EXPORT_SYMBOL(copy_strings_kernel);
493 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
494 * the binfmt code determines where the new stack should reside, we shift it to
495 * its final location. The process proceeds as follows:
497 * 1) Use shift to calculate the new vma endpoints.
498 * 2) Extend vma to cover both the old and new ranges. This ensures the
499 * arguments passed to subsequent functions are consistent.
500 * 3) Move vma's page tables to the new range.
501 * 4) Free up any cleared pgd range.
502 * 5) Shrink the vma to cover only the new range.
504 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
506 struct mm_struct *mm = vma->vm_mm;
507 unsigned long old_start = vma->vm_start;
508 unsigned long old_end = vma->vm_end;
509 unsigned long length = old_end - old_start;
510 unsigned long new_start = old_start - shift;
511 unsigned long new_end = old_end - shift;
512 struct mmu_gather *tlb;
514 BUG_ON(new_start > new_end);
517 * ensure there are no vmas between where we want to go
520 if (vma != find_vma(mm, new_start))
524 * cover the whole range: [new_start, old_end)
526 vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
529 * move the page tables downwards, on failure we rely on
530 * process cleanup to remove whatever mess we made.
532 if (length != move_page_tables(vma, old_start,
533 vma, new_start, length))
537 tlb = tlb_gather_mmu(mm, 0);
538 if (new_end > old_start) {
540 * when the old and new regions overlap clear from new_end.
542 free_pgd_range(tlb, new_end, old_end, new_end,
543 vma->vm_next ? vma->vm_next->vm_start : 0);
546 * otherwise, clean from old_start; this is done to not touch
547 * the address space in [new_end, old_start) some architectures
548 * have constraints on va-space that make this illegal (IA64) -
549 * for the others its just a little faster.
551 free_pgd_range(tlb, old_start, old_end, new_end,
552 vma->vm_next ? vma->vm_next->vm_start : 0);
554 tlb_finish_mmu(tlb, new_end, old_end);
557 * shrink the vma to just the new range.
559 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
564 #define EXTRA_STACK_VM_PAGES 20 /* random */
567 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
568 * the stack is optionally relocated, and some extra space is added.
570 int setup_arg_pages(struct linux_binprm *bprm,
571 unsigned long stack_top,
572 int executable_stack)
575 unsigned long stack_shift;
576 struct mm_struct *mm = current->mm;
577 struct vm_area_struct *vma = bprm->vma;
578 struct vm_area_struct *prev = NULL;
579 unsigned long vm_flags;
580 unsigned long stack_base;
582 #ifdef CONFIG_STACK_GROWSUP
583 /* Limit stack size to 1GB */
584 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
585 if (stack_base > (1 << 30))
586 stack_base = 1 << 30;
588 /* Make sure we didn't let the argument array grow too large. */
589 if (vma->vm_end - vma->vm_start > stack_base)
592 stack_base = PAGE_ALIGN(stack_top - stack_base);
594 stack_shift = vma->vm_start - stack_base;
595 mm->arg_start = bprm->p - stack_shift;
596 bprm->p = vma->vm_end - stack_shift;
598 stack_top = arch_align_stack(stack_top);
599 stack_top = PAGE_ALIGN(stack_top);
600 stack_shift = vma->vm_end - stack_top;
602 bprm->p -= stack_shift;
603 mm->arg_start = bprm->p;
607 bprm->loader -= stack_shift;
608 bprm->exec -= stack_shift;
610 down_write(&mm->mmap_sem);
611 vm_flags = VM_STACK_FLAGS;
614 * Adjust stack execute permissions; explicitly enable for
615 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
616 * (arch default) otherwise.
618 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
620 else if (executable_stack == EXSTACK_DISABLE_X)
621 vm_flags &= ~VM_EXEC;
622 vm_flags |= mm->def_flags;
624 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
630 /* Move stack pages down in memory. */
632 ret = shift_arg_pages(vma, stack_shift);
634 up_write(&mm->mmap_sem);
639 #ifdef CONFIG_STACK_GROWSUP
640 stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
642 stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
644 ret = expand_stack(vma, stack_base);
649 up_write(&mm->mmap_sem);
652 EXPORT_SYMBOL(setup_arg_pages);
654 #endif /* CONFIG_MMU */
656 struct file *open_exec(const char *name)
662 err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
666 struct inode *inode = nd.path.dentry->d_inode;
667 file = ERR_PTR(-EACCES);
668 if (S_ISREG(inode->i_mode)) {
669 int err = vfs_permission(&nd, MAY_EXEC);
672 file = nameidata_to_filp(&nd,
673 O_RDONLY|O_LARGEFILE);
675 err = deny_write_access(file);
685 release_open_intent(&nd);
691 EXPORT_SYMBOL(open_exec);
693 int kernel_read(struct file *file, unsigned long offset,
694 char *addr, unsigned long count)
702 /* The cast to a user pointer is valid due to the set_fs() */
703 result = vfs_read(file, (void __user *)addr, count, &pos);
708 EXPORT_SYMBOL(kernel_read);
710 static int exec_mmap(struct mm_struct *mm)
712 struct task_struct *tsk;
713 struct mm_struct * old_mm, *active_mm;
715 /* Notify parent that we're no longer interested in the old VM */
717 old_mm = current->mm;
718 mm_release(tsk, old_mm);
722 * Make sure that if there is a core dump in progress
723 * for the old mm, we get out and die instead of going
724 * through with the exec. We must hold mmap_sem around
725 * checking core_waiters and changing tsk->mm. The
726 * core-inducing thread will increment core_waiters for
727 * each thread whose ->mm == old_mm.
729 down_read(&old_mm->mmap_sem);
730 if (unlikely(old_mm->core_waiters)) {
731 up_read(&old_mm->mmap_sem);
736 active_mm = tsk->active_mm;
739 activate_mm(active_mm, mm);
741 mm_update_next_owner(old_mm);
742 arch_pick_mmap_layout(mm);
744 up_read(&old_mm->mmap_sem);
745 BUG_ON(active_mm != old_mm);
754 * This function makes sure the current process has its own signal table,
755 * so that flush_signal_handlers can later reset the handlers without
756 * disturbing other processes. (Other processes might share the signal
757 * table via the CLONE_SIGHAND option to clone().)
759 static int de_thread(struct task_struct *tsk)
761 struct signal_struct *sig = tsk->signal;
762 struct sighand_struct *oldsighand = tsk->sighand;
763 spinlock_t *lock = &oldsighand->siglock;
764 struct task_struct *leader = NULL;
767 if (thread_group_empty(tsk))
768 goto no_thread_group;
771 * Kill all other threads in the thread group.
774 if (signal_group_exit(sig)) {
776 * Another group action in progress, just
777 * return so that the signal is processed.
779 spin_unlock_irq(lock);
782 sig->group_exit_task = tsk;
783 zap_other_threads(tsk);
785 /* Account for the thread group leader hanging around: */
786 count = thread_group_leader(tsk) ? 1 : 2;
787 sig->notify_count = count;
788 while (atomic_read(&sig->count) > count) {
789 __set_current_state(TASK_UNINTERRUPTIBLE);
790 spin_unlock_irq(lock);
794 spin_unlock_irq(lock);
797 * At this point all other threads have exited, all we have to
798 * do is to wait for the thread group leader to become inactive,
799 * and to assume its PID:
801 if (!thread_group_leader(tsk)) {
802 leader = tsk->group_leader;
804 sig->notify_count = -1; /* for exit_notify() */
806 write_lock_irq(&tasklist_lock);
807 if (likely(leader->exit_state))
809 __set_current_state(TASK_UNINTERRUPTIBLE);
810 write_unlock_irq(&tasklist_lock);
814 if (unlikely(task_child_reaper(tsk) == leader))
815 task_active_pid_ns(tsk)->child_reaper = tsk;
817 * The only record we have of the real-time age of a
818 * process, regardless of execs it's done, is start_time.
819 * All the past CPU time is accumulated in signal_struct
820 * from sister threads now dead. But in this non-leader
821 * exec, nothing survives from the original leader thread,
822 * whose birth marks the true age of this process now.
823 * When we take on its identity by switching to its PID, we
824 * also take its birthdate (always earlier than our own).
826 tsk->start_time = leader->start_time;
828 BUG_ON(!same_thread_group(leader, tsk));
829 BUG_ON(has_group_leader_pid(tsk));
831 * An exec() starts a new thread group with the
832 * TGID of the previous thread group. Rehash the
833 * two threads with a switched PID, and release
834 * the former thread group leader:
837 /* Become a process group leader with the old leader's pid.
838 * The old leader becomes a thread of the this thread group.
839 * Note: The old leader also uses this pid until release_task
840 * is called. Odd but simple and correct.
842 detach_pid(tsk, PIDTYPE_PID);
843 tsk->pid = leader->pid;
844 attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
845 transfer_pid(leader, tsk, PIDTYPE_PGID);
846 transfer_pid(leader, tsk, PIDTYPE_SID);
847 list_replace_rcu(&leader->tasks, &tsk->tasks);
849 tsk->group_leader = tsk;
850 leader->group_leader = tsk;
852 tsk->exit_signal = SIGCHLD;
854 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
855 leader->exit_state = EXIT_DEAD;
857 write_unlock_irq(&tasklist_lock);
860 sig->group_exit_task = NULL;
861 sig->notify_count = 0;
865 flush_itimer_signals();
867 release_task(leader);
869 if (atomic_read(&oldsighand->count) != 1) {
870 struct sighand_struct *newsighand;
872 * This ->sighand is shared with the CLONE_SIGHAND
873 * but not CLONE_THREAD task, switch to the new one.
875 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
879 atomic_set(&newsighand->count, 1);
880 memcpy(newsighand->action, oldsighand->action,
881 sizeof(newsighand->action));
883 write_lock_irq(&tasklist_lock);
884 spin_lock(&oldsighand->siglock);
885 rcu_assign_pointer(tsk->sighand, newsighand);
886 spin_unlock(&oldsighand->siglock);
887 write_unlock_irq(&tasklist_lock);
889 __cleanup_sighand(oldsighand);
892 BUG_ON(!thread_group_leader(tsk));
897 * These functions flushes out all traces of the currently running executable
898 * so that a new one can be started
900 static void flush_old_files(struct files_struct * files)
905 spin_lock(&files->file_lock);
907 unsigned long set, i;
911 fdt = files_fdtable(files);
912 if (i >= fdt->max_fds)
914 set = fdt->close_on_exec->fds_bits[j];
917 fdt->close_on_exec->fds_bits[j] = 0;
918 spin_unlock(&files->file_lock);
919 for ( ; set ; i++,set >>= 1) {
924 spin_lock(&files->file_lock);
927 spin_unlock(&files->file_lock);
930 char *get_task_comm(char *buf, struct task_struct *tsk)
932 /* buf must be at least sizeof(tsk->comm) in size */
934 strncpy(buf, tsk->comm, sizeof(tsk->comm));
939 void set_task_comm(struct task_struct *tsk, char *buf)
942 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
946 int flush_old_exec(struct linux_binprm * bprm)
950 char tcomm[sizeof(current->comm)];
953 * Make sure we have a private signal table and that
954 * we are unassociated from the previous thread group.
956 retval = de_thread(current);
960 set_mm_exe_file(bprm->mm, bprm->file);
963 * Release all of the old mmap stuff
965 retval = exec_mmap(bprm->mm);
969 bprm->mm = NULL; /* We're using it now */
971 /* This is the point of no return */
972 current->sas_ss_sp = current->sas_ss_size = 0;
974 if (current->euid == current->uid && current->egid == current->gid)
975 set_dumpable(current->mm, 1);
977 set_dumpable(current->mm, suid_dumpable);
979 name = bprm->filename;
981 /* Copies the binary name from after last slash */
982 for (i=0; (ch = *(name++)) != '\0';) {
984 i = 0; /* overwrite what we wrote */
986 if (i < (sizeof(tcomm) - 1))
990 set_task_comm(current, tcomm);
992 current->flags &= ~PF_RANDOMIZE;
995 /* Set the new mm task size. We have to do that late because it may
996 * depend on TIF_32BIT which is only updated in flush_thread() on
997 * some architectures like powerpc
999 current->mm->task_size = TASK_SIZE;
1001 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
1003 set_dumpable(current->mm, suid_dumpable);
1004 current->pdeath_signal = 0;
1005 } else if (file_permission(bprm->file, MAY_READ) ||
1006 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
1008 set_dumpable(current->mm, suid_dumpable);
1011 /* An exec changes our domain. We are no longer part of the thread
1014 current->self_exec_id++;
1016 flush_signal_handlers(current, 0);
1017 flush_old_files(current->files);
1025 EXPORT_SYMBOL(flush_old_exec);
1028 * Fill the binprm structure from the inode.
1029 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1031 int prepare_binprm(struct linux_binprm *bprm)
1034 struct inode * inode = bprm->file->f_path.dentry->d_inode;
1037 mode = inode->i_mode;
1038 if (bprm->file->f_op == NULL)
1041 bprm->e_uid = current->euid;
1042 bprm->e_gid = current->egid;
1044 if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
1046 if (mode & S_ISUID) {
1047 current->personality &= ~PER_CLEAR_ON_SETID;
1048 bprm->e_uid = inode->i_uid;
1053 * If setgid is set but no group execute bit then this
1054 * is a candidate for mandatory locking, not a setgid
1057 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1058 current->personality &= ~PER_CLEAR_ON_SETID;
1059 bprm->e_gid = inode->i_gid;
1063 /* fill in binprm security blob */
1064 retval = security_bprm_set(bprm);
1068 memset(bprm->buf,0,BINPRM_BUF_SIZE);
1069 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
1072 EXPORT_SYMBOL(prepare_binprm);
1074 static int unsafe_exec(struct task_struct *p)
1077 if (p->ptrace & PT_PTRACED) {
1078 if (p->ptrace & PT_PTRACE_CAP)
1079 unsafe |= LSM_UNSAFE_PTRACE_CAP;
1081 unsafe |= LSM_UNSAFE_PTRACE;
1083 if (atomic_read(&p->fs->count) > 1 ||
1084 atomic_read(&p->files->count) > 1 ||
1085 atomic_read(&p->sighand->count) > 1)
1086 unsafe |= LSM_UNSAFE_SHARE;
1091 void compute_creds(struct linux_binprm *bprm)
1095 if (bprm->e_uid != current->uid) {
1097 current->pdeath_signal = 0;
1102 unsafe = unsafe_exec(current);
1103 security_bprm_apply_creds(bprm, unsafe);
1104 task_unlock(current);
1105 security_bprm_post_apply_creds(bprm);
1107 EXPORT_SYMBOL(compute_creds);
1110 * Arguments are '\0' separated strings found at the location bprm->p
1111 * points to; chop off the first by relocating brpm->p to right after
1112 * the first '\0' encountered.
1114 int remove_arg_zero(struct linux_binprm *bprm)
1117 unsigned long offset;
1125 offset = bprm->p & ~PAGE_MASK;
1126 page = get_arg_page(bprm, bprm->p, 0);
1131 kaddr = kmap_atomic(page, KM_USER0);
1133 for (; offset < PAGE_SIZE && kaddr[offset];
1134 offset++, bprm->p++)
1137 kunmap_atomic(kaddr, KM_USER0);
1140 if (offset == PAGE_SIZE)
1141 free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1142 } while (offset == PAGE_SIZE);
1151 EXPORT_SYMBOL(remove_arg_zero);
1154 * cycle the list of binary formats handler, until one recognizes the image
1156 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1159 struct linux_binfmt *fmt;
1161 /* handle /sbin/loader.. */
1163 struct exec * eh = (struct exec *) bprm->buf;
1165 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1166 (eh->fh.f_flags & 0x3000) == 0x3000)
1169 unsigned long loader;
1171 allow_write_access(bprm->file);
1175 loader = bprm->vma->vm_end - sizeof(void *);
1177 file = open_exec("/sbin/loader");
1178 retval = PTR_ERR(file);
1182 /* Remember if the application is TASO. */
1183 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1186 bprm->loader = loader;
1187 retval = prepare_binprm(bprm);
1190 /* should call search_binary_handler recursively here,
1191 but it does not matter */
1195 retval = security_bprm_check(bprm);
1199 /* kernel module loader fixup */
1200 /* so we don't try to load run modprobe in kernel space. */
1203 retval = audit_bprm(bprm);
1208 for (try=0; try<2; try++) {
1209 read_lock(&binfmt_lock);
1210 list_for_each_entry(fmt, &formats, lh) {
1211 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1214 if (!try_module_get(fmt->module))
1216 read_unlock(&binfmt_lock);
1217 retval = fn(bprm, regs);
1220 allow_write_access(bprm->file);
1224 current->did_exec = 1;
1225 proc_exec_connector(current);
1228 read_lock(&binfmt_lock);
1230 if (retval != -ENOEXEC || bprm->mm == NULL)
1233 read_unlock(&binfmt_lock);
1237 read_unlock(&binfmt_lock);
1238 if (retval != -ENOEXEC || bprm->mm == NULL) {
1242 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1243 if (printable(bprm->buf[0]) &&
1244 printable(bprm->buf[1]) &&
1245 printable(bprm->buf[2]) &&
1246 printable(bprm->buf[3]))
1247 break; /* -ENOEXEC */
1248 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1255 EXPORT_SYMBOL(search_binary_handler);
1257 void free_bprm(struct linux_binprm *bprm)
1259 free_arg_pages(bprm);
1264 * sys_execve() executes a new program.
1266 int do_execve(char * filename,
1267 char __user *__user *argv,
1268 char __user *__user *envp,
1269 struct pt_regs * regs)
1271 struct linux_binprm *bprm;
1273 struct files_struct *displaced;
1276 retval = unshare_files(&displaced);
1281 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1285 file = open_exec(filename);
1286 retval = PTR_ERR(file);
1293 bprm->filename = filename;
1294 bprm->interp = filename;
1296 retval = bprm_mm_init(bprm);
1300 bprm->argc = count(argv, MAX_ARG_STRINGS);
1301 if ((retval = bprm->argc) < 0)
1304 bprm->envc = count(envp, MAX_ARG_STRINGS);
1305 if ((retval = bprm->envc) < 0)
1308 retval = security_bprm_alloc(bprm);
1312 retval = prepare_binprm(bprm);
1316 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1320 bprm->exec = bprm->p;
1321 retval = copy_strings(bprm->envc, envp, bprm);
1325 retval = copy_strings(bprm->argc, argv, bprm);
1329 retval = search_binary_handler(bprm,regs);
1331 /* execve success */
1332 security_bprm_free(bprm);
1333 acct_update_integrals(current);
1336 put_files_struct(displaced);
1342 security_bprm_free(bprm);
1350 allow_write_access(bprm->file);
1358 reset_files_struct(displaced);
1363 int set_binfmt(struct linux_binfmt *new)
1365 struct linux_binfmt *old = current->binfmt;
1368 if (!try_module_get(new->module))
1371 current->binfmt = new;
1373 module_put(old->module);
1377 EXPORT_SYMBOL(set_binfmt);
1379 /* format_corename will inspect the pattern parameter, and output a
1380 * name into corename, which must have space for at least
1381 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1383 static int format_corename(char *corename, const char *pattern, long signr)
1385 const char *pat_ptr = pattern;
1386 char *out_ptr = corename;
1387 char *const out_end = corename + CORENAME_MAX_SIZE;
1389 int pid_in_pattern = 0;
1392 if (*pattern == '|')
1395 /* Repeat as long as we have more pattern to process and more output
1398 if (*pat_ptr != '%') {
1399 if (out_ptr == out_end)
1401 *out_ptr++ = *pat_ptr++;
1403 switch (*++pat_ptr) {
1406 /* Double percent, output one percent */
1408 if (out_ptr == out_end)
1415 rc = snprintf(out_ptr, out_end - out_ptr,
1416 "%d", task_tgid_vnr(current));
1417 if (rc > out_end - out_ptr)
1423 rc = snprintf(out_ptr, out_end - out_ptr,
1424 "%d", current->uid);
1425 if (rc > out_end - out_ptr)
1431 rc = snprintf(out_ptr, out_end - out_ptr,
1432 "%d", current->gid);
1433 if (rc > out_end - out_ptr)
1437 /* signal that caused the coredump */
1439 rc = snprintf(out_ptr, out_end - out_ptr,
1441 if (rc > out_end - out_ptr)
1445 /* UNIX time of coredump */
1448 do_gettimeofday(&tv);
1449 rc = snprintf(out_ptr, out_end - out_ptr,
1451 if (rc > out_end - out_ptr)
1458 down_read(&uts_sem);
1459 rc = snprintf(out_ptr, out_end - out_ptr,
1460 "%s", utsname()->nodename);
1462 if (rc > out_end - out_ptr)
1468 rc = snprintf(out_ptr, out_end - out_ptr,
1469 "%s", current->comm);
1470 if (rc > out_end - out_ptr)
1474 /* core limit size */
1476 rc = snprintf(out_ptr, out_end - out_ptr,
1477 "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
1478 if (rc > out_end - out_ptr)
1488 /* Backward compatibility with core_uses_pid:
1490 * If core_pattern does not include a %p (as is the default)
1491 * and core_uses_pid is set, then .%pid will be appended to
1492 * the filename. Do not do this for piped commands. */
1493 if (!ispipe && !pid_in_pattern
1494 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1495 rc = snprintf(out_ptr, out_end - out_ptr,
1496 ".%d", task_tgid_vnr(current));
1497 if (rc > out_end - out_ptr)
1506 static void zap_process(struct task_struct *start)
1508 struct task_struct *t;
1510 start->signal->flags = SIGNAL_GROUP_EXIT;
1511 start->signal->group_stop_count = 0;
1515 if (t != current && t->mm) {
1516 t->mm->core_waiters++;
1517 sigaddset(&t->pending.signal, SIGKILL);
1518 signal_wake_up(t, 1);
1520 } while ((t = next_thread(t)) != start);
1523 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1526 struct task_struct *g, *p;
1527 unsigned long flags;
1530 spin_lock_irq(&tsk->sighand->siglock);
1531 if (!signal_group_exit(tsk->signal)) {
1532 tsk->signal->group_exit_code = exit_code;
1536 spin_unlock_irq(&tsk->sighand->siglock);
1540 if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
1544 for_each_process(g) {
1545 if (g == tsk->group_leader)
1553 * p->sighand can't disappear, but
1554 * may be changed by de_thread()
1556 lock_task_sighand(p, &flags);
1558 unlock_task_sighand(p, &flags);
1562 } while ((p = next_thread(p)) != g);
1566 return mm->core_waiters;
1569 static int coredump_wait(int exit_code)
1571 struct task_struct *tsk = current;
1572 struct mm_struct *mm = tsk->mm;
1573 struct completion startup_done;
1574 struct completion *vfork_done;
1577 init_completion(&mm->core_done);
1578 init_completion(&startup_done);
1579 mm->core_startup_done = &startup_done;
1581 core_waiters = zap_threads(tsk, mm, exit_code);
1582 up_write(&mm->mmap_sem);
1584 if (unlikely(core_waiters < 0))
1588 * Make sure nobody is waiting for us to release the VM,
1589 * otherwise we can deadlock when we wait on each other
1591 vfork_done = tsk->vfork_done;
1593 tsk->vfork_done = NULL;
1594 complete(vfork_done);
1598 wait_for_completion(&startup_done);
1600 BUG_ON(mm->core_waiters);
1601 return core_waiters;
1605 * set_dumpable converts traditional three-value dumpable to two flags and
1606 * stores them into mm->flags. It modifies lower two bits of mm->flags, but
1607 * these bits are not changed atomically. So get_dumpable can observe the
1608 * intermediate state. To avoid doing unexpected behavior, get get_dumpable
1609 * return either old dumpable or new one by paying attention to the order of
1610 * modifying the bits.
1612 * dumpable | mm->flags (binary)
1613 * old new | initial interim final
1614 * ---------+-----------------------
1622 * (*) get_dumpable regards interim value of 10 as 11.
1624 void set_dumpable(struct mm_struct *mm, int value)
1628 clear_bit(MMF_DUMPABLE, &mm->flags);
1630 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1633 set_bit(MMF_DUMPABLE, &mm->flags);
1635 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1638 set_bit(MMF_DUMP_SECURELY, &mm->flags);
1640 set_bit(MMF_DUMPABLE, &mm->flags);
1645 int get_dumpable(struct mm_struct *mm)
1649 ret = mm->flags & 0x3;
1650 return (ret >= 2) ? 2 : ret;
1653 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1655 char corename[CORENAME_MAX_SIZE + 1];
1656 struct mm_struct *mm = current->mm;
1657 struct linux_binfmt * binfmt;
1658 struct inode * inode;
1661 int fsuid = current->fsuid;
1664 unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
1665 char **helper_argv = NULL;
1666 int helper_argc = 0;
1669 audit_core_dumps(signr);
1671 binfmt = current->binfmt;
1672 if (!binfmt || !binfmt->core_dump)
1674 down_write(&mm->mmap_sem);
1676 * If another thread got here first, or we are not dumpable, bail out.
1678 if (mm->core_waiters || !get_dumpable(mm)) {
1679 up_write(&mm->mmap_sem);
1684 * We cannot trust fsuid as being the "true" uid of the
1685 * process nor do we know its entire history. We only know it
1686 * was tainted so we dump it as root in mode 2.
1688 if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
1689 flag = O_EXCL; /* Stop rewrite attacks */
1690 current->fsuid = 0; /* Dump root private */
1693 retval = coredump_wait(exit_code);
1698 * Clear any false indication of pending signals that might
1699 * be seen by the filesystem code called to write the core file.
1701 clear_thread_flag(TIF_SIGPENDING);
1704 * lock_kernel() because format_corename() is controlled by sysctl, which
1705 * uses lock_kernel()
1708 ispipe = format_corename(corename, core_pattern, signr);
1711 * Don't bother to check the RLIMIT_CORE value if core_pattern points
1712 * to a pipe. Since we're not writing directly to the filesystem
1713 * RLIMIT_CORE doesn't really apply, as no actual core file will be
1714 * created unless the pipe reader choses to write out the core file
1715 * at which point file size limits and permissions will be imposed
1716 * as it does with any other process
1718 if ((!ispipe) && (core_limit < binfmt->min_coredump))
1722 helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
1723 /* Terminate the string before the first option */
1724 delimit = strchr(corename, ' ');
1727 delimit = strrchr(helper_argv[0], '/');
1731 delimit = helper_argv[0];
1732 if (!strcmp(delimit, current->comm)) {
1733 printk(KERN_NOTICE "Recursive core dump detected, "
1738 core_limit = RLIM_INFINITY;
1740 /* SIGPIPE can happen, but it's just never processed */
1741 if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
1743 printk(KERN_INFO "Core dump to %s pipe failed\n",
1748 file = filp_open(corename,
1749 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
1753 inode = file->f_path.dentry->d_inode;
1754 if (inode->i_nlink > 1)
1755 goto close_fail; /* multiple links - don't dump */
1756 if (!ispipe && d_unhashed(file->f_path.dentry))
1759 /* AK: actually i see no reason to not allow this for named pipes etc.,
1760 but keep the previous behaviour for now. */
1761 if (!ispipe && !S_ISREG(inode->i_mode))
1764 * Dont allow local users get cute and trick others to coredump
1765 * into their pre-created files:
1767 if (inode->i_uid != current->fsuid)
1771 if (!file->f_op->write)
1773 if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
1776 retval = binfmt->core_dump(signr, regs, file, core_limit);
1779 current->signal->group_exit_code |= 0x80;
1781 filp_close(file, NULL);
1784 argv_free(helper_argv);
1786 current->fsuid = fsuid;
1787 complete_all(&mm->core_done);