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>
28 #include <linux/freezer.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/swap.h>
33 #include <linux/string.h>
34 #include <linux/init.h>
35 #include <linux/pagemap.h>
36 #include <linux/perf_event.h>
37 #include <linux/highmem.h>
38 #include <linux/spinlock.h>
39 #include <linux/key.h>
40 #include <linux/personality.h>
41 #include <linux/binfmts.h>
42 #include <linux/utsname.h>
43 #include <linux/pid_namespace.h>
44 #include <linux/module.h>
45 #include <linux/namei.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>
52 #include <linux/tracehook.h>
53 #include <linux/kmod.h>
54 #include <linux/fsnotify.h>
55 #include <linux/fs_struct.h>
56 #include <linux/pipe_fs_i.h>
57 #include <linux/oom.h>
58 #include <linux/compat.h>
60 #include <trace/events/fs.h>
62 #include <asm/uaccess.h>
63 #include <asm/mmu_context.h>
67 #include <trace/events/task.h>
70 #include <trace/events/sched.h>
73 char core_pattern[CORENAME_MAX_SIZE] = "core";
74 unsigned int core_pipe_limit;
75 int suid_dumpable = 0;
81 static atomic_t call_count = ATOMIC_INIT(1);
83 /* The maximal length of core_pattern is also specified in sysctl.c */
85 static LIST_HEAD(formats);
86 static DEFINE_RWLOCK(binfmt_lock);
88 void __register_binfmt(struct linux_binfmt * fmt, int insert)
91 write_lock(&binfmt_lock);
92 insert ? list_add(&fmt->lh, &formats) :
93 list_add_tail(&fmt->lh, &formats);
94 write_unlock(&binfmt_lock);
97 EXPORT_SYMBOL(__register_binfmt);
99 void unregister_binfmt(struct linux_binfmt * fmt)
101 write_lock(&binfmt_lock);
103 write_unlock(&binfmt_lock);
106 EXPORT_SYMBOL(unregister_binfmt);
108 static inline void put_binfmt(struct linux_binfmt * fmt)
110 module_put(fmt->module);
114 * Note that a shared library must be both readable and executable due to
117 * Also note that we take the address to load from from the file itself.
119 SYSCALL_DEFINE1(uselib, const char __user *, library)
122 char *tmp = getname(library);
123 int error = PTR_ERR(tmp);
124 static const struct open_flags uselib_flags = {
125 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
126 .acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
127 .intent = LOOKUP_OPEN
133 file = do_filp_open(AT_FDCWD, tmp, &uselib_flags, LOOKUP_FOLLOW);
135 error = PTR_ERR(file);
140 if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
144 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
151 struct linux_binfmt * fmt;
153 read_lock(&binfmt_lock);
154 list_for_each_entry(fmt, &formats, lh) {
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);
176 * The nascent bprm->mm is not visible until exec_mmap() but it can
177 * use a lot of memory, account these pages in current->mm temporary
178 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
179 * change the counter back via acct_arg_size(0).
181 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
183 struct mm_struct *mm = current->mm;
184 long diff = (long)(pages - bprm->vma_pages);
189 bprm->vma_pages = pages;
190 add_mm_counter(mm, MM_ANONPAGES, diff);
193 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
199 #ifdef CONFIG_STACK_GROWSUP
201 ret = expand_downwards(bprm->vma, pos);
206 ret = get_user_pages(current, bprm->mm, pos,
207 1, write, 1, &page, NULL);
212 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
215 acct_arg_size(bprm, size / PAGE_SIZE);
218 * We've historically supported up to 32 pages (ARG_MAX)
219 * of argument strings even with small stacks
225 * Limit to 1/4-th the stack size for the argv+env strings.
227 * - the remaining binfmt code will not run out of stack space,
228 * - the program will have a reasonable amount of stack left
231 rlim = current->signal->rlim;
232 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
241 static void put_arg_page(struct page *page)
246 static void free_arg_page(struct linux_binprm *bprm, int i)
250 static void free_arg_pages(struct linux_binprm *bprm)
254 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
257 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
260 static int __bprm_mm_init(struct linux_binprm *bprm)
263 struct vm_area_struct *vma = NULL;
264 struct mm_struct *mm = bprm->mm;
266 bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
270 down_write(&mm->mmap_sem);
274 * Place the stack at the largest stack address the architecture
275 * supports. Later, we'll move this to an appropriate place. We don't
276 * use STACK_TOP because that can depend on attributes which aren't
279 BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
280 vma->vm_end = STACK_TOP_MAX;
281 vma->vm_start = vma->vm_end - PAGE_SIZE;
282 vma->vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
283 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
284 INIT_LIST_HEAD(&vma->anon_vma_chain);
286 err = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
290 err = insert_vm_struct(mm, vma);
294 mm->stack_vm = mm->total_vm = 1;
295 up_write(&mm->mmap_sem);
296 bprm->p = vma->vm_end - sizeof(void *);
299 up_write(&mm->mmap_sem);
301 kmem_cache_free(vm_area_cachep, vma);
305 static bool valid_arg_len(struct linux_binprm *bprm, long len)
307 return len <= MAX_ARG_STRLEN;
312 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
316 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
321 page = bprm->page[pos / PAGE_SIZE];
322 if (!page && write) {
323 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
326 bprm->page[pos / PAGE_SIZE] = page;
332 static void put_arg_page(struct page *page)
336 static void free_arg_page(struct linux_binprm *bprm, int i)
339 __free_page(bprm->page[i]);
340 bprm->page[i] = NULL;
344 static void free_arg_pages(struct linux_binprm *bprm)
348 for (i = 0; i < MAX_ARG_PAGES; i++)
349 free_arg_page(bprm, i);
352 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
357 static int __bprm_mm_init(struct linux_binprm *bprm)
359 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
363 static bool valid_arg_len(struct linux_binprm *bprm, long len)
365 return len <= bprm->p;
368 #endif /* CONFIG_MMU */
371 * Create a new mm_struct and populate it with a temporary stack
372 * vm_area_struct. We don't have enough context at this point to set the stack
373 * flags, permissions, and offset, so we use temporary values. We'll update
374 * them later in setup_arg_pages().
376 int bprm_mm_init(struct linux_binprm *bprm)
379 struct mm_struct *mm = NULL;
381 bprm->mm = mm = mm_alloc();
386 err = init_new_context(current, mm);
390 err = __bprm_mm_init(bprm);
405 struct user_arg_ptr {
410 const char __user *const __user *native;
412 compat_uptr_t __user *compat;
417 static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
419 const char __user *native;
422 if (unlikely(argv.is_compat)) {
423 compat_uptr_t compat;
425 if (get_user(compat, argv.ptr.compat + nr))
426 return ERR_PTR(-EFAULT);
428 return compat_ptr(compat);
432 if (get_user(native, argv.ptr.native + nr))
433 return ERR_PTR(-EFAULT);
439 * count() counts the number of strings in array ARGV.
441 static int count(struct user_arg_ptr argv, int max)
445 if (argv.ptr.native != NULL) {
447 const char __user *p = get_user_arg_ptr(argv, i);
458 if (fatal_signal_pending(current))
459 return -ERESTARTNOHAND;
467 * 'copy_strings()' copies argument/environment strings from the old
468 * processes's memory to the new process's stack. The call to get_user_pages()
469 * ensures the destination page is created and not swapped out.
471 static int copy_strings(int argc, struct user_arg_ptr argv,
472 struct linux_binprm *bprm)
474 struct page *kmapped_page = NULL;
476 unsigned long kpos = 0;
480 const char __user *str;
485 str = get_user_arg_ptr(argv, argc);
489 len = strnlen_user(str, MAX_ARG_STRLEN);
494 if (!valid_arg_len(bprm, len))
497 /* We're going to work our way backwords. */
503 int offset, bytes_to_copy;
505 if (fatal_signal_pending(current)) {
506 ret = -ERESTARTNOHAND;
511 offset = pos % PAGE_SIZE;
515 bytes_to_copy = offset;
516 if (bytes_to_copy > len)
519 offset -= bytes_to_copy;
520 pos -= bytes_to_copy;
521 str -= bytes_to_copy;
522 len -= bytes_to_copy;
524 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
527 page = get_arg_page(bprm, pos, 1);
534 flush_kernel_dcache_page(kmapped_page);
535 kunmap(kmapped_page);
536 put_arg_page(kmapped_page);
539 kaddr = kmap(kmapped_page);
540 kpos = pos & PAGE_MASK;
541 flush_arg_page(bprm, kpos, kmapped_page);
543 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
552 flush_kernel_dcache_page(kmapped_page);
553 kunmap(kmapped_page);
554 put_arg_page(kmapped_page);
560 * Like copy_strings, but get argv and its values from kernel memory.
562 int copy_strings_kernel(int argc, const char *const *__argv,
563 struct linux_binprm *bprm)
566 mm_segment_t oldfs = get_fs();
567 struct user_arg_ptr argv = {
568 .ptr.native = (const char __user *const __user *)__argv,
572 r = copy_strings(argc, argv, bprm);
577 EXPORT_SYMBOL(copy_strings_kernel);
582 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
583 * the binfmt code determines where the new stack should reside, we shift it to
584 * its final location. The process proceeds as follows:
586 * 1) Use shift to calculate the new vma endpoints.
587 * 2) Extend vma to cover both the old and new ranges. This ensures the
588 * arguments passed to subsequent functions are consistent.
589 * 3) Move vma's page tables to the new range.
590 * 4) Free up any cleared pgd range.
591 * 5) Shrink the vma to cover only the new range.
593 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
595 struct mm_struct *mm = vma->vm_mm;
596 unsigned long old_start = vma->vm_start;
597 unsigned long old_end = vma->vm_end;
598 unsigned long length = old_end - old_start;
599 unsigned long new_start = old_start - shift;
600 unsigned long new_end = old_end - shift;
601 struct mmu_gather tlb;
603 BUG_ON(new_start > new_end);
606 * ensure there are no vmas between where we want to go
609 if (vma != find_vma(mm, new_start))
613 * cover the whole range: [new_start, old_end)
615 if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
619 * move the page tables downwards, on failure we rely on
620 * process cleanup to remove whatever mess we made.
622 if (length != move_page_tables(vma, old_start,
623 vma, new_start, length))
627 tlb_gather_mmu(&tlb, mm, 0);
628 if (new_end > old_start) {
630 * when the old and new regions overlap clear from new_end.
632 free_pgd_range(&tlb, new_end, old_end, new_end,
633 vma->vm_next ? vma->vm_next->vm_start : 0);
636 * otherwise, clean from old_start; this is done to not touch
637 * the address space in [new_end, old_start) some architectures
638 * have constraints on va-space that make this illegal (IA64) -
639 * for the others its just a little faster.
641 free_pgd_range(&tlb, old_start, old_end, new_end,
642 vma->vm_next ? vma->vm_next->vm_start : 0);
644 tlb_finish_mmu(&tlb, new_end, old_end);
647 * Shrink the vma to just the new range. Always succeeds.
649 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
655 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
656 * the stack is optionally relocated, and some extra space is added.
658 int setup_arg_pages(struct linux_binprm *bprm,
659 unsigned long stack_top,
660 int executable_stack)
663 unsigned long stack_shift;
664 struct mm_struct *mm = current->mm;
665 struct vm_area_struct *vma = bprm->vma;
666 struct vm_area_struct *prev = NULL;
667 unsigned long vm_flags;
668 unsigned long stack_base;
669 unsigned long stack_size;
670 unsigned long stack_expand;
671 unsigned long rlim_stack;
673 #ifdef CONFIG_STACK_GROWSUP
674 /* Limit stack size to 1GB */
675 stack_base = rlimit_max(RLIMIT_STACK);
676 if (stack_base > (1 << 30))
677 stack_base = 1 << 30;
679 /* Make sure we didn't let the argument array grow too large. */
680 if (vma->vm_end - vma->vm_start > stack_base)
683 stack_base = PAGE_ALIGN(stack_top - stack_base);
685 stack_shift = vma->vm_start - stack_base;
686 mm->arg_start = bprm->p - stack_shift;
687 bprm->p = vma->vm_end - stack_shift;
689 stack_top = arch_align_stack(stack_top);
690 stack_top = PAGE_ALIGN(stack_top);
692 if (unlikely(stack_top < mmap_min_addr) ||
693 unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
696 stack_shift = vma->vm_end - stack_top;
698 bprm->p -= stack_shift;
699 mm->arg_start = bprm->p;
703 bprm->loader -= stack_shift;
704 bprm->exec -= stack_shift;
706 down_write(&mm->mmap_sem);
707 vm_flags = VM_STACK_FLAGS;
710 * Adjust stack execute permissions; explicitly enable for
711 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
712 * (arch default) otherwise.
714 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
716 else if (executable_stack == EXSTACK_DISABLE_X)
717 vm_flags &= ~VM_EXEC;
718 vm_flags |= mm->def_flags;
719 vm_flags |= VM_STACK_INCOMPLETE_SETUP;
721 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
727 /* Move stack pages down in memory. */
729 ret = shift_arg_pages(vma, stack_shift);
734 /* mprotect_fixup is overkill to remove the temporary stack flags */
735 vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
737 stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
738 stack_size = vma->vm_end - vma->vm_start;
740 * Align this down to a page boundary as expand_stack
743 rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
744 #ifdef CONFIG_STACK_GROWSUP
745 if (stack_size + stack_expand > rlim_stack)
746 stack_base = vma->vm_start + rlim_stack;
748 stack_base = vma->vm_end + stack_expand;
750 if (stack_size + stack_expand > rlim_stack)
751 stack_base = vma->vm_end - rlim_stack;
753 stack_base = vma->vm_start - stack_expand;
755 current->mm->start_stack = bprm->p;
756 ret = expand_stack(vma, stack_base);
761 up_write(&mm->mmap_sem);
764 EXPORT_SYMBOL(setup_arg_pages);
766 #endif /* CONFIG_MMU */
768 struct file *open_exec(const char *name)
772 static const struct open_flags open_exec_flags = {
773 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
774 .acc_mode = MAY_EXEC | MAY_OPEN,
775 .intent = LOOKUP_OPEN
778 file = do_filp_open(AT_FDCWD, name, &open_exec_flags, LOOKUP_FOLLOW);
783 if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
786 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
791 trace_open_exec(name);
793 err = deny_write_access(file);
804 EXPORT_SYMBOL(open_exec);
806 int kernel_read(struct file *file, loff_t offset,
807 char *addr, unsigned long count)
815 /* The cast to a user pointer is valid due to the set_fs() */
816 result = vfs_read(file, (void __user *)addr, count, &pos);
821 EXPORT_SYMBOL(kernel_read);
823 static int exec_mmap(struct mm_struct *mm)
825 struct task_struct *tsk;
826 struct mm_struct * old_mm, *active_mm;
828 /* Notify parent that we're no longer interested in the old VM */
830 old_mm = current->mm;
832 mm_release(tsk, old_mm);
836 * Make sure that if there is a core dump in progress
837 * for the old mm, we get out and die instead of going
838 * through with the exec. We must hold mmap_sem around
839 * checking core_state and changing tsk->mm.
841 down_read(&old_mm->mmap_sem);
842 if (unlikely(old_mm->core_state)) {
843 up_read(&old_mm->mmap_sem);
848 active_mm = tsk->active_mm;
851 activate_mm(active_mm, mm);
853 arch_pick_mmap_layout(mm);
855 up_read(&old_mm->mmap_sem);
856 BUG_ON(active_mm != old_mm);
857 setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
858 mm_update_next_owner(old_mm);
867 * This function makes sure the current process has its own signal table,
868 * so that flush_signal_handlers can later reset the handlers without
869 * disturbing other processes. (Other processes might share the signal
870 * table via the CLONE_SIGHAND option to clone().)
872 static int de_thread(struct task_struct *tsk)
874 struct signal_struct *sig = tsk->signal;
875 struct sighand_struct *oldsighand = tsk->sighand;
876 spinlock_t *lock = &oldsighand->siglock;
878 if (thread_group_empty(tsk))
879 goto no_thread_group;
882 * Kill all other threads in the thread group.
885 if (signal_group_exit(sig)) {
887 * Another group action in progress, just
888 * return so that the signal is processed.
890 spin_unlock_irq(lock);
894 sig->group_exit_task = tsk;
895 sig->notify_count = zap_other_threads(tsk);
896 if (!thread_group_leader(tsk))
899 while (sig->notify_count) {
900 __set_current_state(TASK_UNINTERRUPTIBLE);
901 spin_unlock_irq(lock);
905 spin_unlock_irq(lock);
908 * At this point all other threads have exited, all we have to
909 * do is to wait for the thread group leader to become inactive,
910 * and to assume its PID:
912 if (!thread_group_leader(tsk)) {
913 struct task_struct *leader = tsk->group_leader;
915 sig->notify_count = -1; /* for exit_notify() */
917 write_lock_irq(&tasklist_lock);
918 if (likely(leader->exit_state))
920 __set_current_state(TASK_UNINTERRUPTIBLE);
921 write_unlock_irq(&tasklist_lock);
926 * The only record we have of the real-time age of a
927 * process, regardless of execs it's done, is start_time.
928 * All the past CPU time is accumulated in signal_struct
929 * from sister threads now dead. But in this non-leader
930 * exec, nothing survives from the original leader thread,
931 * whose birth marks the true age of this process now.
932 * When we take on its identity by switching to its PID, we
933 * also take its birthdate (always earlier than our own).
935 tsk->start_time = leader->start_time;
937 BUG_ON(!same_thread_group(leader, tsk));
938 BUG_ON(has_group_leader_pid(tsk));
940 * An exec() starts a new thread group with the
941 * TGID of the previous thread group. Rehash the
942 * two threads with a switched PID, and release
943 * the former thread group leader:
946 /* Become a process group leader with the old leader's pid.
947 * The old leader becomes a thread of the this thread group.
948 * Note: The old leader also uses this pid until release_task
949 * is called. Odd but simple and correct.
951 detach_pid(tsk, PIDTYPE_PID);
952 tsk->pid = leader->pid;
953 attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
954 transfer_pid(leader, tsk, PIDTYPE_PGID);
955 transfer_pid(leader, tsk, PIDTYPE_SID);
957 list_replace_rcu(&leader->tasks, &tsk->tasks);
958 list_replace_init(&leader->sibling, &tsk->sibling);
960 tsk->group_leader = tsk;
961 leader->group_leader = tsk;
963 tsk->exit_signal = SIGCHLD;
964 leader->exit_signal = -1;
966 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
967 leader->exit_state = EXIT_DEAD;
970 * We are going to release_task()->ptrace_unlink() silently,
971 * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
972 * the tracer wont't block again waiting for this thread.
974 if (unlikely(leader->ptrace))
975 __wake_up_parent(leader, leader->parent);
976 write_unlock_irq(&tasklist_lock);
978 release_task(leader);
981 sig->group_exit_task = NULL;
982 sig->notify_count = 0;
985 /* we have changed execution domain */
986 tsk->exit_signal = SIGCHLD;
989 flush_itimer_signals();
991 if (atomic_read(&oldsighand->count) != 1) {
992 struct sighand_struct *newsighand;
994 * This ->sighand is shared with the CLONE_SIGHAND
995 * but not CLONE_THREAD task, switch to the new one.
997 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1001 atomic_set(&newsighand->count, 1);
1002 memcpy(newsighand->action, oldsighand->action,
1003 sizeof(newsighand->action));
1005 write_lock_irq(&tasklist_lock);
1006 spin_lock(&oldsighand->siglock);
1007 rcu_assign_pointer(tsk->sighand, newsighand);
1008 spin_unlock(&oldsighand->siglock);
1009 write_unlock_irq(&tasklist_lock);
1011 __cleanup_sighand(oldsighand);
1014 BUG_ON(!thread_group_leader(tsk));
1019 * These functions flushes out all traces of the currently running executable
1020 * so that a new one can be started
1022 static void flush_old_files(struct files_struct * files)
1025 struct fdtable *fdt;
1027 spin_lock(&files->file_lock);
1029 unsigned long set, i;
1033 fdt = files_fdtable(files);
1034 if (i >= fdt->max_fds)
1036 set = fdt->close_on_exec[j];
1039 fdt->close_on_exec[j] = 0;
1040 spin_unlock(&files->file_lock);
1041 for ( ; set ; i++,set >>= 1) {
1046 spin_lock(&files->file_lock);
1049 spin_unlock(&files->file_lock);
1052 char *get_task_comm(char *buf, struct task_struct *tsk)
1054 /* buf must be at least sizeof(tsk->comm) in size */
1056 strncpy(buf, tsk->comm, sizeof(tsk->comm));
1060 EXPORT_SYMBOL_GPL(get_task_comm);
1062 void set_task_comm(struct task_struct *tsk, char *buf)
1066 trace_task_rename(tsk, buf);
1069 * Threads may access current->comm without holding
1070 * the task lock, so write the string carefully.
1071 * Readers without a lock may see incomplete new
1072 * names but are safe from non-terminating string reads.
1074 memset(tsk->comm, 0, TASK_COMM_LEN);
1076 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
1078 perf_event_comm(tsk);
1081 static void filename_to_taskname(char *tcomm, const char *fn, unsigned int len)
1085 /* Copies the binary name from after last slash */
1086 for (i = 0; (ch = *(fn++)) != '\0';) {
1088 i = 0; /* overwrite what we wrote */
1096 int flush_old_exec(struct linux_binprm * bprm)
1101 * Make sure we have a private signal table and that
1102 * we are unassociated from the previous thread group.
1104 retval = de_thread(current);
1108 set_mm_exe_file(bprm->mm, bprm->file);
1110 filename_to_taskname(bprm->tcomm, bprm->filename, sizeof(bprm->tcomm));
1112 * Release all of the old mmap stuff
1114 acct_arg_size(bprm, 0);
1115 retval = exec_mmap(bprm->mm);
1119 bprm->mm = NULL; /* We're using it now */
1122 current->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC | PF_KTHREAD);
1124 current->personality &= ~bprm->per_clear;
1131 EXPORT_SYMBOL(flush_old_exec);
1133 void would_dump(struct linux_binprm *bprm, struct file *file)
1135 if (inode_permission(file->f_path.dentry->d_inode, MAY_READ) < 0)
1136 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1138 EXPORT_SYMBOL(would_dump);
1140 void setup_new_exec(struct linux_binprm * bprm)
1142 arch_pick_mmap_layout(current->mm);
1144 /* This is the point of no return */
1145 current->sas_ss_sp = current->sas_ss_size = 0;
1147 if (current_euid() == current_uid() && current_egid() == current_gid())
1148 set_dumpable(current->mm, 1);
1150 set_dumpable(current->mm, suid_dumpable);
1152 set_task_comm(current, bprm->tcomm);
1154 /* Set the new mm task size. We have to do that late because it may
1155 * depend on TIF_32BIT which is only updated in flush_thread() on
1156 * some architectures like powerpc
1158 current->mm->task_size = TASK_SIZE;
1160 /* install the new credentials */
1161 if (bprm->cred->uid != current_euid() ||
1162 bprm->cred->gid != current_egid()) {
1163 current->pdeath_signal = 0;
1165 would_dump(bprm, bprm->file);
1166 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
1167 set_dumpable(current->mm, suid_dumpable);
1171 * Flush performance counters when crossing a
1174 if (!get_dumpable(current->mm))
1175 perf_event_exit_task(current);
1177 /* An exec changes our domain. We are no longer part of the thread
1180 current->self_exec_id++;
1182 flush_signal_handlers(current, 0);
1183 flush_old_files(current->files);
1185 EXPORT_SYMBOL(setup_new_exec);
1188 * Prepare credentials and lock ->cred_guard_mutex.
1189 * install_exec_creds() commits the new creds and drops the lock.
1190 * Or, if exec fails before, free_bprm() should release ->cred and
1193 int prepare_bprm_creds(struct linux_binprm *bprm)
1195 if (mutex_lock_interruptible(¤t->signal->cred_guard_mutex))
1196 return -ERESTARTNOINTR;
1198 bprm->cred = prepare_exec_creds();
1199 if (likely(bprm->cred))
1202 mutex_unlock(¤t->signal->cred_guard_mutex);
1206 void free_bprm(struct linux_binprm *bprm)
1208 free_arg_pages(bprm);
1210 mutex_unlock(¤t->signal->cred_guard_mutex);
1211 abort_creds(bprm->cred);
1217 * install the new credentials for this executable
1219 void install_exec_creds(struct linux_binprm *bprm)
1221 security_bprm_committing_creds(bprm);
1223 commit_creds(bprm->cred);
1226 * cred_guard_mutex must be held at least to this point to prevent
1227 * ptrace_attach() from altering our determination of the task's
1228 * credentials; any time after this it may be unlocked.
1230 security_bprm_committed_creds(bprm);
1231 mutex_unlock(¤t->signal->cred_guard_mutex);
1233 EXPORT_SYMBOL(install_exec_creds);
1236 * determine how safe it is to execute the proposed program
1237 * - the caller must hold ->cred_guard_mutex to protect against
1240 static int check_unsafe_exec(struct linux_binprm *bprm)
1242 struct task_struct *p = current, *t;
1247 if (p->ptrace & PT_PTRACE_CAP)
1248 bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
1250 bprm->unsafe |= LSM_UNSAFE_PTRACE;
1254 * This isn't strictly necessary, but it makes it harder for LSMs to
1257 if (current->no_new_privs)
1258 bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS;
1261 spin_lock(&p->fs->lock);
1263 for (t = next_thread(p); t != p; t = next_thread(t)) {
1269 if (p->fs->users > n_fs) {
1270 bprm->unsafe |= LSM_UNSAFE_SHARE;
1273 if (!p->fs->in_exec) {
1278 spin_unlock(&p->fs->lock);
1284 * Fill the binprm structure from the inode.
1285 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1287 * This may be called multiple times for binary chains (scripts for example).
1289 int prepare_binprm(struct linux_binprm *bprm)
1292 struct inode * inode = bprm->file->f_path.dentry->d_inode;
1295 mode = inode->i_mode;
1296 if (bprm->file->f_op == NULL)
1299 /* clear any previous set[ug]id data from a previous binary */
1300 bprm->cred->euid = current_euid();
1301 bprm->cred->egid = current_egid();
1303 if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
1304 !current->no_new_privs) {
1306 if (mode & S_ISUID) {
1307 bprm->per_clear |= PER_CLEAR_ON_SETID;
1308 bprm->cred->euid = inode->i_uid;
1313 * If setgid is set but no group execute bit then this
1314 * is a candidate for mandatory locking, not a setgid
1317 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1318 bprm->per_clear |= PER_CLEAR_ON_SETID;
1319 bprm->cred->egid = inode->i_gid;
1323 /* fill in binprm security blob */
1324 retval = security_bprm_set_creds(bprm);
1327 bprm->cred_prepared = 1;
1329 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1330 return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1333 EXPORT_SYMBOL(prepare_binprm);
1336 * Arguments are '\0' separated strings found at the location bprm->p
1337 * points to; chop off the first by relocating brpm->p to right after
1338 * the first '\0' encountered.
1340 int remove_arg_zero(struct linux_binprm *bprm)
1343 unsigned long offset;
1351 offset = bprm->p & ~PAGE_MASK;
1352 page = get_arg_page(bprm, bprm->p, 0);
1357 kaddr = kmap_atomic(page);
1359 for (; offset < PAGE_SIZE && kaddr[offset];
1360 offset++, bprm->p++)
1363 kunmap_atomic(kaddr);
1366 if (offset == PAGE_SIZE)
1367 free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1368 } while (offset == PAGE_SIZE);
1377 EXPORT_SYMBOL(remove_arg_zero);
1380 * cycle the list of binary formats handler, until one recognizes the image
1382 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1384 unsigned int depth = bprm->recursion_depth;
1386 struct linux_binfmt *fmt;
1387 pid_t old_pid, old_vpid;
1389 retval = security_bprm_check(bprm);
1393 retval = audit_bprm(bprm);
1397 /* Need to fetch pid before load_binary changes it */
1398 old_pid = current->pid;
1400 old_vpid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1404 for (try=0; try<2; try++) {
1405 read_lock(&binfmt_lock);
1406 list_for_each_entry(fmt, &formats, lh) {
1407 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1410 if (!try_module_get(fmt->module))
1412 read_unlock(&binfmt_lock);
1413 retval = fn(bprm, regs);
1415 * Restore the depth counter to its starting value
1416 * in this call, so we don't have to rely on every
1417 * load_binary function to restore it on return.
1419 bprm->recursion_depth = depth;
1422 trace_sched_process_exec(current, old_pid, bprm);
1423 ptrace_event(PTRACE_EVENT_EXEC, old_vpid);
1426 allow_write_access(bprm->file);
1430 current->did_exec = 1;
1431 proc_exec_connector(current);
1434 read_lock(&binfmt_lock);
1436 if (retval != -ENOEXEC || bprm->mm == NULL)
1439 read_unlock(&binfmt_lock);
1443 read_unlock(&binfmt_lock);
1444 #ifdef CONFIG_MODULES
1445 if (retval != -ENOEXEC || bprm->mm == NULL) {
1448 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1449 if (printable(bprm->buf[0]) &&
1450 printable(bprm->buf[1]) &&
1451 printable(bprm->buf[2]) &&
1452 printable(bprm->buf[3]))
1453 break; /* -ENOEXEC */
1455 break; /* -ENOEXEC */
1456 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1465 EXPORT_SYMBOL(search_binary_handler);
1468 * sys_execve() executes a new program.
1470 static int do_execve_common(const char *filename,
1471 struct user_arg_ptr argv,
1472 struct user_arg_ptr envp,
1473 struct pt_regs *regs)
1475 struct linux_binprm *bprm;
1477 struct files_struct *displaced;
1480 const struct cred *cred = current_cred();
1483 * We move the actual failure in case of RLIMIT_NPROC excess from
1484 * set*uid() to execve() because too many poorly written programs
1485 * don't check setuid() return code. Here we additionally recheck
1486 * whether NPROC limit is still exceeded.
1488 if ((current->flags & PF_NPROC_EXCEEDED) &&
1489 atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
1494 /* We're below the limit (still or again), so we don't want to make
1495 * further execve() calls fail. */
1496 current->flags &= ~PF_NPROC_EXCEEDED;
1498 retval = unshare_files(&displaced);
1503 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1507 retval = prepare_bprm_creds(bprm);
1511 retval = check_unsafe_exec(bprm);
1514 clear_in_exec = retval;
1515 current->in_execve = 1;
1517 file = open_exec(filename);
1518 retval = PTR_ERR(file);
1525 bprm->filename = filename;
1526 bprm->interp = filename;
1528 retval = bprm_mm_init(bprm);
1532 bprm->argc = count(argv, MAX_ARG_STRINGS);
1533 if ((retval = bprm->argc) < 0)
1536 bprm->envc = count(envp, MAX_ARG_STRINGS);
1537 if ((retval = bprm->envc) < 0)
1540 retval = prepare_binprm(bprm);
1544 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1548 bprm->exec = bprm->p;
1549 retval = copy_strings(bprm->envc, envp, bprm);
1553 retval = copy_strings(bprm->argc, argv, bprm);
1557 retval = search_binary_handler(bprm,regs);
1561 /* execve succeeded */
1562 current->fs->in_exec = 0;
1563 current->in_execve = 0;
1564 acct_update_integrals(current);
1567 put_files_struct(displaced);
1572 acct_arg_size(bprm, 0);
1578 allow_write_access(bprm->file);
1584 current->fs->in_exec = 0;
1585 current->in_execve = 0;
1592 reset_files_struct(displaced);
1597 int do_execve(const char *filename,
1598 const char __user *const __user *__argv,
1599 const char __user *const __user *__envp,
1600 struct pt_regs *regs)
1602 struct user_arg_ptr argv = { .ptr.native = __argv };
1603 struct user_arg_ptr envp = { .ptr.native = __envp };
1604 return do_execve_common(filename, argv, envp, regs);
1607 #ifdef CONFIG_COMPAT
1608 int compat_do_execve(char *filename,
1609 compat_uptr_t __user *__argv,
1610 compat_uptr_t __user *__envp,
1611 struct pt_regs *regs)
1613 struct user_arg_ptr argv = {
1615 .ptr.compat = __argv,
1617 struct user_arg_ptr envp = {
1619 .ptr.compat = __envp,
1621 return do_execve_common(filename, argv, envp, regs);
1625 void set_binfmt(struct linux_binfmt *new)
1627 struct mm_struct *mm = current->mm;
1630 module_put(mm->binfmt->module);
1634 __module_get(new->module);
1637 EXPORT_SYMBOL(set_binfmt);
1639 static int expand_corename(struct core_name *cn)
1641 char *old_corename = cn->corename;
1643 cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
1644 cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
1646 if (!cn->corename) {
1647 kfree(old_corename);
1654 static int cn_printf(struct core_name *cn, const char *fmt, ...)
1662 need = vsnprintf(NULL, 0, fmt, arg);
1665 if (likely(need < cn->size - cn->used - 1))
1668 ret = expand_corename(cn);
1673 cur = cn->corename + cn->used;
1675 vsnprintf(cur, need + 1, fmt, arg);
1684 static void cn_escape(char *str)
1691 static int cn_print_exe_file(struct core_name *cn)
1693 struct file *exe_file;
1694 char *pathbuf, *path;
1697 exe_file = get_mm_exe_file(current->mm);
1699 char *commstart = cn->corename + cn->used;
1700 ret = cn_printf(cn, "%s (path unknown)", current->comm);
1701 cn_escape(commstart);
1705 pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
1711 path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1713 ret = PTR_ERR(path);
1719 ret = cn_printf(cn, "%s", path);
1728 /* format_corename will inspect the pattern parameter, and output a
1729 * name into corename, which must have space for at least
1730 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1732 static int format_corename(struct core_name *cn, long signr)
1734 const struct cred *cred = current_cred();
1735 const char *pat_ptr = core_pattern;
1736 int ispipe = (*pat_ptr == '|');
1737 int pid_in_pattern = 0;
1740 cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
1741 cn->corename = kmalloc(cn->size, GFP_KERNEL);
1747 /* Repeat as long as we have more pattern to process and more output
1750 if (*pat_ptr != '%') {
1753 err = cn_printf(cn, "%c", *pat_ptr++);
1755 switch (*++pat_ptr) {
1756 /* single % at the end, drop that */
1759 /* Double percent, output one percent */
1761 err = cn_printf(cn, "%c", '%');
1766 err = cn_printf(cn, "%d",
1767 task_tgid_vnr(current));
1771 err = cn_printf(cn, "%d", cred->uid);
1775 err = cn_printf(cn, "%d", cred->gid);
1777 /* signal that caused the coredump */
1779 err = cn_printf(cn, "%ld", signr);
1781 /* UNIX time of coredump */
1784 do_gettimeofday(&tv);
1785 err = cn_printf(cn, "%lu", tv.tv_sec);
1790 char *namestart = cn->corename + cn->used;
1791 down_read(&uts_sem);
1792 err = cn_printf(cn, "%s",
1793 utsname()->nodename);
1795 cn_escape(namestart);
1800 char *commstart = cn->corename + cn->used;
1801 err = cn_printf(cn, "%s", current->comm);
1802 cn_escape(commstart);
1806 err = cn_print_exe_file(cn);
1808 /* core limit size */
1810 err = cn_printf(cn, "%lu",
1811 rlimit(RLIMIT_CORE));
1823 /* Backward compatibility with core_uses_pid:
1825 * If core_pattern does not include a %p (as is the default)
1826 * and core_uses_pid is set, then .%pid will be appended to
1827 * the filename. Do not do this for piped commands. */
1828 if (!ispipe && !pid_in_pattern && core_uses_pid) {
1829 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
1837 static int zap_process(struct task_struct *start, int exit_code)
1839 struct task_struct *t;
1842 start->signal->group_exit_code = exit_code;
1843 start->signal->group_stop_count = 0;
1847 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1848 if (t != current && t->mm) {
1849 sigaddset(&t->pending.signal, SIGKILL);
1850 signal_wake_up(t, 1);
1853 } while_each_thread(start, t);
1858 static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1859 struct core_state *core_state, int exit_code)
1861 struct task_struct *g, *p;
1862 unsigned long flags;
1865 spin_lock_irq(&tsk->sighand->siglock);
1866 if (!signal_group_exit(tsk->signal)) {
1867 mm->core_state = core_state;
1868 nr = zap_process(tsk, exit_code);
1869 tsk->signal->group_exit_task = tsk;
1870 /* ignore all signals except SIGKILL, see prepare_signal() */
1871 tsk->signal->flags = SIGNAL_GROUP_COREDUMP;
1872 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
1874 spin_unlock_irq(&tsk->sighand->siglock);
1875 if (unlikely(nr < 0))
1878 if (atomic_read(&mm->mm_users) == nr + 1)
1881 * We should find and kill all tasks which use this mm, and we should
1882 * count them correctly into ->nr_threads. We don't take tasklist
1883 * lock, but this is safe wrt:
1886 * None of sub-threads can fork after zap_process(leader). All
1887 * processes which were created before this point should be
1888 * visible to zap_threads() because copy_process() adds the new
1889 * process to the tail of init_task.tasks list, and lock/unlock
1890 * of ->siglock provides a memory barrier.
1893 * The caller holds mm->mmap_sem. This means that the task which
1894 * uses this mm can't pass exit_mm(), so it can't exit or clear
1898 * It does list_replace_rcu(&leader->tasks, ¤t->tasks),
1899 * we must see either old or new leader, this does not matter.
1900 * However, it can change p->sighand, so lock_task_sighand(p)
1901 * must be used. Since p->mm != NULL and we hold ->mmap_sem
1904 * Note also that "g" can be the old leader with ->mm == NULL
1905 * and already unhashed and thus removed from ->thread_group.
1906 * This is OK, __unhash_process()->list_del_rcu() does not
1907 * clear the ->next pointer, we will find the new leader via
1911 for_each_process(g) {
1912 if (g == tsk->group_leader)
1914 if (g->flags & PF_KTHREAD)
1919 if (unlikely(p->mm == mm)) {
1920 lock_task_sighand(p, &flags);
1921 nr += zap_process(p, exit_code);
1922 p->signal->flags = SIGNAL_GROUP_EXIT;
1923 unlock_task_sighand(p, &flags);
1927 } while_each_thread(g, p);
1931 atomic_set(&core_state->nr_threads, nr);
1935 static int coredump_wait(int exit_code, struct core_state *core_state)
1937 struct task_struct *tsk = current;
1938 struct mm_struct *mm = tsk->mm;
1939 int core_waiters = -EBUSY;
1941 init_completion(&core_state->startup);
1942 core_state->dumper.task = tsk;
1943 core_state->dumper.next = NULL;
1945 down_write(&mm->mmap_sem);
1946 if (!mm->core_state)
1947 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
1948 up_write(&mm->mmap_sem);
1950 if (core_waiters > 0)
1951 wait_for_completion(&core_state->startup);
1953 return core_waiters;
1956 static void coredump_finish(struct mm_struct *mm, bool core_dumped)
1958 struct core_thread *curr, *next;
1959 struct task_struct *task;
1961 spin_lock_irq(¤t->sighand->siglock);
1962 if (core_dumped && !__fatal_signal_pending(current))
1963 current->signal->group_exit_code |= 0x80;
1964 current->signal->group_exit_task = NULL;
1965 current->signal->flags = SIGNAL_GROUP_EXIT;
1966 spin_unlock_irq(¤t->sighand->siglock);
1968 next = mm->core_state->dumper.next;
1969 while ((curr = next) != NULL) {
1973 * see exit_mm(), curr->task must not see
1974 * ->task == NULL before we read ->next.
1978 wake_up_process(task);
1981 mm->core_state = NULL;
1985 * set_dumpable converts traditional three-value dumpable to two flags and
1986 * stores them into mm->flags. It modifies lower two bits of mm->flags, but
1987 * these bits are not changed atomically. So get_dumpable can observe the
1988 * intermediate state. To avoid doing unexpected behavior, get get_dumpable
1989 * return either old dumpable or new one by paying attention to the order of
1990 * modifying the bits.
1992 * dumpable | mm->flags (binary)
1993 * old new | initial interim final
1994 * ---------+-----------------------
2002 * (*) get_dumpable regards interim value of 10 as 11.
2004 void set_dumpable(struct mm_struct *mm, int value)
2008 clear_bit(MMF_DUMPABLE, &mm->flags);
2010 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
2013 set_bit(MMF_DUMPABLE, &mm->flags);
2015 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
2018 set_bit(MMF_DUMP_SECURELY, &mm->flags);
2020 set_bit(MMF_DUMPABLE, &mm->flags);
2025 static int __get_dumpable(unsigned long mm_flags)
2029 ret = mm_flags & MMF_DUMPABLE_MASK;
2030 return (ret >= 2) ? 2 : ret;
2033 int get_dumpable(struct mm_struct *mm)
2035 return __get_dumpable(mm->flags);
2038 static void wait_for_dump_helpers(struct file *file)
2040 struct pipe_inode_info *pipe;
2042 pipe = file->f_path.dentry->d_inode->i_pipe;
2048 while (pipe->readers > 1) {
2049 unsigned long flags;
2051 wake_up_interruptible_sync(&pipe->wait);
2052 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
2059 if (fatal_signal_pending(current))
2062 /* Clear fake signal from freeze_task(). */
2063 spin_lock_irqsave(¤t->sighand->siglock, flags);
2064 recalc_sigpending();
2065 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
2077 * helper function to customize the process used
2078 * to collect the core in userspace. Specifically
2079 * it sets up a pipe and installs it as fd 0 (stdin)
2080 * for the process. Returns 0 on success, or
2081 * PTR_ERR on failure.
2082 * Note that it also sets the core limit to 1. This
2083 * is a special value that we use to trap recursive
2086 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
2088 struct file *rp, *wp;
2089 struct fdtable *fdt;
2090 struct coredump_params *cp = (struct coredump_params *)info->data;
2091 struct files_struct *cf = current->files;
2093 wp = create_write_pipe(0);
2097 rp = create_read_pipe(wp, 0);
2099 free_write_pipe(wp);
2107 spin_lock(&cf->file_lock);
2108 fdt = files_fdtable(cf);
2109 __set_open_fd(0, fdt);
2110 __clear_close_on_exec(0, fdt);
2111 spin_unlock(&cf->file_lock);
2113 /* and disallow core files too */
2114 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
2119 void do_coredump(long signr, int exit_code, struct pt_regs *regs)
2121 struct core_state core_state;
2122 struct core_name cn;
2123 struct mm_struct *mm = current->mm;
2124 struct linux_binfmt * binfmt;
2125 const struct cred *old_cred;
2130 bool core_dumped = false;
2131 static atomic_t core_dump_count = ATOMIC_INIT(0);
2132 struct coredump_params cprm = {
2135 .limit = rlimit(RLIMIT_CORE),
2137 * We must use the same mm->flags while dumping core to avoid
2138 * inconsistency of bit flags, since this flag is not protected
2141 .mm_flags = mm->flags,
2144 audit_core_dumps(signr);
2146 binfmt = mm->binfmt;
2147 if (!binfmt || !binfmt->core_dump)
2149 if (!__get_dumpable(cprm.mm_flags))
2152 cred = prepare_creds();
2156 * We cannot trust fsuid as being the "true" uid of the
2157 * process nor do we know its entire history. We only know it
2158 * was tainted so we dump it as root in mode 2.
2160 if (__get_dumpable(cprm.mm_flags) == 2) {
2161 /* Setuid core dump mode */
2162 flag = O_EXCL; /* Stop rewrite attacks */
2163 cred->fsuid = 0; /* Dump root private */
2166 retval = coredump_wait(exit_code, &core_state);
2170 old_cred = override_creds(cred);
2172 ispipe = format_corename(&cn, signr);
2179 printk(KERN_WARNING "format_corename failed\n");
2180 printk(KERN_WARNING "Aborting core\n");
2184 if (cprm.limit == 1) {
2186 * Normally core limits are irrelevant to pipes, since
2187 * we're not writing to the file system, but we use
2188 * cprm.limit of 1 here as a speacial value. Any
2189 * non-1 limit gets set to RLIM_INFINITY below, but
2190 * a limit of 0 skips the dump. This is a consistent
2191 * way to catch recursive crashes. We can still crash
2192 * if the core_pattern binary sets RLIM_CORE = !1
2193 * but it runs as root, and can do lots of stupid things
2194 * Note that we use task_tgid_vnr here to grab the pid
2195 * of the process group leader. That way we get the
2196 * right pid if a thread in a multi-threaded
2197 * core_pattern process dies.
2200 "Process %d(%s) has RLIMIT_CORE set to 1\n",
2201 task_tgid_vnr(current), current->comm);
2202 printk(KERN_WARNING "Aborting core\n");
2205 cprm.limit = RLIM_INFINITY;
2207 dump_count = atomic_inc_return(&core_dump_count);
2208 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
2209 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
2210 task_tgid_vnr(current), current->comm);
2211 printk(KERN_WARNING "Skipping core dump\n");
2212 goto fail_dropcount;
2215 helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
2217 printk(KERN_WARNING "%s failed to allocate memory\n",
2219 goto fail_dropcount;
2222 retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
2223 NULL, UMH_WAIT_EXEC, umh_pipe_setup,
2225 argv_free(helper_argv);
2227 printk(KERN_INFO "Core dump to %s pipe failed\n",
2232 struct inode *inode;
2234 if (cprm.limit < binfmt->min_coredump)
2237 cprm.file = filp_open(cn.corename,
2238 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
2240 if (IS_ERR(cprm.file))
2243 inode = cprm.file->f_path.dentry->d_inode;
2244 if (inode->i_nlink > 1)
2246 if (d_unhashed(cprm.file->f_path.dentry))
2249 * AK: actually i see no reason to not allow this for named
2250 * pipes etc, but keep the previous behaviour for now.
2252 if (!S_ISREG(inode->i_mode))
2255 * Dont allow local users get cute and trick others to coredump
2256 * into their pre-created files.
2258 if (inode->i_uid != current_fsuid())
2260 if (!cprm.file->f_op || !cprm.file->f_op->write)
2262 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
2266 core_dumped = binfmt->core_dump(&cprm);
2268 if (ispipe && core_pipe_limit)
2269 wait_for_dump_helpers(cprm.file);
2272 filp_close(cprm.file, NULL);
2275 atomic_dec(&core_dump_count);
2279 coredump_finish(mm, core_dumped);
2280 revert_creds(old_cred);
2288 * Core dumping helper functions. These are the only things you should
2289 * do on a core-file: use only these functions to write out all the
2292 int dump_write(struct file *file, const void *addr, int nr)
2294 return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
2296 EXPORT_SYMBOL(dump_write);
2298 int dump_seek(struct file *file, loff_t off)
2302 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
2303 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
2306 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
2311 unsigned long n = off;
2315 if (!dump_write(file, buf, n)) {
2321 free_page((unsigned long)buf);
2325 EXPORT_SYMBOL(dump_seek);