drivers/net/wireless/p54/eeprom.c: Return -ENOMEM on memory allocation failure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / exec.c
blob3132722205a3559c15b7e315d91a82aa68266a22
1 /*
2 * linux/fs/exec.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * #!-checking implemented by tytso.
9 */
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
22 * formats.
25 #include <linux/slab.h>
26 #include <linux/file.h>
27 #include <linux/fdtable.h>
28 #include <linux/mm.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/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/proc_fs.h>
47 #include <linux/mount.h>
48 #include <linux/security.h>
49 #include <linux/syscalls.h>
50 #include <linux/tsacct_kern.h>
51 #include <linux/cn_proc.h>
52 #include <linux/audit.h>
53 #include <linux/tracehook.h>
54 #include <linux/kmod.h>
55 #include <linux/fsnotify.h>
56 #include <linux/fs_struct.h>
57 #include <linux/pipe_fs_i.h>
59 #include <asm/uaccess.h>
60 #include <asm/mmu_context.h>
61 #include <asm/tlb.h>
62 #include "internal.h"
64 int core_uses_pid;
65 char core_pattern[CORENAME_MAX_SIZE] = "core";
66 unsigned int core_pipe_limit;
67 int suid_dumpable = 0;
69 /* The maximal length of core_pattern is also specified in sysctl.c */
71 static LIST_HEAD(formats);
72 static DEFINE_RWLOCK(binfmt_lock);
74 int __register_binfmt(struct linux_binfmt * fmt, int insert)
76 if (!fmt)
77 return -EINVAL;
78 write_lock(&binfmt_lock);
79 insert ? list_add(&fmt->lh, &formats) :
80 list_add_tail(&fmt->lh, &formats);
81 write_unlock(&binfmt_lock);
82 return 0;
85 EXPORT_SYMBOL(__register_binfmt);
87 void unregister_binfmt(struct linux_binfmt * fmt)
89 write_lock(&binfmt_lock);
90 list_del(&fmt->lh);
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
103 * security reasons.
105 * Also note that we take the address to load from from the file itself.
107 SYSCALL_DEFINE1(uselib, const char __user *, library)
109 struct file *file;
110 char *tmp = getname(library);
111 int error = PTR_ERR(tmp);
113 if (IS_ERR(tmp))
114 goto out;
116 file = do_filp_open(AT_FDCWD, tmp,
117 O_LARGEFILE | O_RDONLY | FMODE_EXEC, 0,
118 MAY_READ | MAY_EXEC | MAY_OPEN);
119 putname(tmp);
120 error = PTR_ERR(file);
121 if (IS_ERR(file))
122 goto out;
124 error = -EINVAL;
125 if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
126 goto exit;
128 error = -EACCES;
129 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
130 goto exit;
132 fsnotify_open(file->f_path.dentry);
134 error = -ENOEXEC;
135 if(file->f_op) {
136 struct linux_binfmt * fmt;
138 read_lock(&binfmt_lock);
139 list_for_each_entry(fmt, &formats, lh) {
140 if (!fmt->load_shlib)
141 continue;
142 if (!try_module_get(fmt->module))
143 continue;
144 read_unlock(&binfmt_lock);
145 error = fmt->load_shlib(file);
146 read_lock(&binfmt_lock);
147 put_binfmt(fmt);
148 if (error != -ENOEXEC)
149 break;
151 read_unlock(&binfmt_lock);
153 exit:
154 fput(file);
155 out:
156 return error;
159 #ifdef CONFIG_MMU
161 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
162 int write)
164 struct page *page;
165 int ret;
167 #ifdef CONFIG_STACK_GROWSUP
168 if (write) {
169 ret = expand_stack_downwards(bprm->vma, pos);
170 if (ret < 0)
171 return NULL;
173 #endif
174 ret = get_user_pages(current, bprm->mm, pos,
175 1, write, 1, &page, NULL);
176 if (ret <= 0)
177 return NULL;
179 if (write) {
180 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
181 struct rlimit *rlim;
184 * We've historically supported up to 32 pages (ARG_MAX)
185 * of argument strings even with small stacks
187 if (size <= ARG_MAX)
188 return page;
191 * Limit to 1/4-th the stack size for the argv+env strings.
192 * This ensures that:
193 * - the remaining binfmt code will not run out of stack space,
194 * - the program will have a reasonable amount of stack left
195 * to work from.
197 rlim = current->signal->rlim;
198 if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
199 put_page(page);
200 return NULL;
204 return page;
207 static void put_arg_page(struct page *page)
209 put_page(page);
212 static void free_arg_page(struct linux_binprm *bprm, int i)
216 static void free_arg_pages(struct linux_binprm *bprm)
220 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
221 struct page *page)
223 flush_cache_page(bprm->vma, pos, page_to_pfn(page));
226 static int __bprm_mm_init(struct linux_binprm *bprm)
228 int err;
229 struct vm_area_struct *vma = NULL;
230 struct mm_struct *mm = bprm->mm;
232 bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
233 if (!vma)
234 return -ENOMEM;
236 down_write(&mm->mmap_sem);
237 vma->vm_mm = mm;
240 * Place the stack at the largest stack address the architecture
241 * supports. Later, we'll move this to an appropriate place. We don't
242 * use STACK_TOP because that can depend on attributes which aren't
243 * configured yet.
245 vma->vm_end = STACK_TOP_MAX;
246 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);
250 if (err)
251 goto err;
253 mm->stack_vm = mm->total_vm = 1;
254 up_write(&mm->mmap_sem);
255 bprm->p = vma->vm_end - sizeof(void *);
256 return 0;
257 err:
258 up_write(&mm->mmap_sem);
259 bprm->vma = NULL;
260 kmem_cache_free(vm_area_cachep, vma);
261 return err;
264 static bool valid_arg_len(struct linux_binprm *bprm, long len)
266 return len <= MAX_ARG_STRLEN;
269 #else
271 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
272 int write)
274 struct page *page;
276 page = bprm->page[pos / PAGE_SIZE];
277 if (!page && write) {
278 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
279 if (!page)
280 return NULL;
281 bprm->page[pos / PAGE_SIZE] = page;
284 return page;
287 static void put_arg_page(struct page *page)
291 static void free_arg_page(struct linux_binprm *bprm, int i)
293 if (bprm->page[i]) {
294 __free_page(bprm->page[i]);
295 bprm->page[i] = NULL;
299 static void free_arg_pages(struct linux_binprm *bprm)
301 int i;
303 for (i = 0; i < MAX_ARG_PAGES; i++)
304 free_arg_page(bprm, i);
307 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
308 struct page *page)
312 static int __bprm_mm_init(struct linux_binprm *bprm)
314 bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
315 return 0;
318 static bool valid_arg_len(struct linux_binprm *bprm, long len)
320 return len <= bprm->p;
323 #endif /* CONFIG_MMU */
326 * Create a new mm_struct and populate it with a temporary stack
327 * vm_area_struct. We don't have enough context at this point to set the stack
328 * flags, permissions, and offset, so we use temporary values. We'll update
329 * them later in setup_arg_pages().
331 int bprm_mm_init(struct linux_binprm *bprm)
333 int err;
334 struct mm_struct *mm = NULL;
336 bprm->mm = mm = mm_alloc();
337 err = -ENOMEM;
338 if (!mm)
339 goto err;
341 err = init_new_context(current, mm);
342 if (err)
343 goto err;
345 err = __bprm_mm_init(bprm);
346 if (err)
347 goto err;
349 return 0;
351 err:
352 if (mm) {
353 bprm->mm = NULL;
354 mmdrop(mm);
357 return err;
361 * count() counts the number of strings in array ARGV.
363 static int count(char __user * __user * argv, int max)
365 int i = 0;
367 if (argv != NULL) {
368 for (;;) {
369 char __user * p;
371 if (get_user(p, argv))
372 return -EFAULT;
373 if (!p)
374 break;
375 argv++;
376 if (i++ >= max)
377 return -E2BIG;
379 if (fatal_signal_pending(current))
380 return -ERESTARTNOHAND;
381 cond_resched();
384 return i;
388 * 'copy_strings()' copies argument/environment strings from the old
389 * processes's memory to the new process's stack. The call to get_user_pages()
390 * ensures the destination page is created and not swapped out.
392 static int copy_strings(int argc, char __user * __user * argv,
393 struct linux_binprm *bprm)
395 struct page *kmapped_page = NULL;
396 char *kaddr = NULL;
397 unsigned long kpos = 0;
398 int ret;
400 while (argc-- > 0) {
401 char __user *str;
402 int len;
403 unsigned long pos;
405 if (get_user(str, argv+argc) ||
406 !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
407 ret = -EFAULT;
408 goto out;
411 if (!valid_arg_len(bprm, len)) {
412 ret = -E2BIG;
413 goto out;
416 /* We're going to work our way backwords. */
417 pos = bprm->p;
418 str += len;
419 bprm->p -= len;
421 while (len > 0) {
422 int offset, bytes_to_copy;
424 if (fatal_signal_pending(current)) {
425 ret = -ERESTARTNOHAND;
426 goto out;
428 cond_resched();
430 offset = pos % PAGE_SIZE;
431 if (offset == 0)
432 offset = PAGE_SIZE;
434 bytes_to_copy = offset;
435 if (bytes_to_copy > len)
436 bytes_to_copy = len;
438 offset -= bytes_to_copy;
439 pos -= bytes_to_copy;
440 str -= bytes_to_copy;
441 len -= bytes_to_copy;
443 if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
444 struct page *page;
446 page = get_arg_page(bprm, pos, 1);
447 if (!page) {
448 ret = -E2BIG;
449 goto out;
452 if (kmapped_page) {
453 flush_kernel_dcache_page(kmapped_page);
454 kunmap(kmapped_page);
455 put_arg_page(kmapped_page);
457 kmapped_page = page;
458 kaddr = kmap(kmapped_page);
459 kpos = pos & PAGE_MASK;
460 flush_arg_page(bprm, kpos, kmapped_page);
462 if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
463 ret = -EFAULT;
464 goto out;
468 ret = 0;
469 out:
470 if (kmapped_page) {
471 flush_kernel_dcache_page(kmapped_page);
472 kunmap(kmapped_page);
473 put_arg_page(kmapped_page);
475 return ret;
479 * Like copy_strings, but get argv and its values from kernel memory.
481 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
483 int r;
484 mm_segment_t oldfs = get_fs();
485 set_fs(KERNEL_DS);
486 r = copy_strings(argc, (char __user * __user *)argv, bprm);
487 set_fs(oldfs);
488 return r;
490 EXPORT_SYMBOL(copy_strings_kernel);
492 #ifdef CONFIG_MMU
495 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
496 * the binfmt code determines where the new stack should reside, we shift it to
497 * its final location. The process proceeds as follows:
499 * 1) Use shift to calculate the new vma endpoints.
500 * 2) Extend vma to cover both the old and new ranges. This ensures the
501 * arguments passed to subsequent functions are consistent.
502 * 3) Move vma's page tables to the new range.
503 * 4) Free up any cleared pgd range.
504 * 5) Shrink the vma to cover only the new range.
506 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
508 struct mm_struct *mm = vma->vm_mm;
509 unsigned long old_start = vma->vm_start;
510 unsigned long old_end = vma->vm_end;
511 unsigned long length = old_end - old_start;
512 unsigned long new_start = old_start - shift;
513 unsigned long new_end = old_end - shift;
514 struct mmu_gather *tlb;
516 BUG_ON(new_start > new_end);
519 * ensure there are no vmas between where we want to go
520 * and where we are
522 if (vma != find_vma(mm, new_start))
523 return -EFAULT;
526 * cover the whole range: [new_start, old_end)
528 vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
531 * move the page tables downwards, on failure we rely on
532 * process cleanup to remove whatever mess we made.
534 if (length != move_page_tables(vma, old_start,
535 vma, new_start, length))
536 return -ENOMEM;
538 lru_add_drain();
539 tlb = tlb_gather_mmu(mm, 0);
540 if (new_end > old_start) {
542 * when the old and new regions overlap clear from new_end.
544 free_pgd_range(tlb, new_end, old_end, new_end,
545 vma->vm_next ? vma->vm_next->vm_start : 0);
546 } else {
548 * otherwise, clean from old_start; this is done to not touch
549 * the address space in [new_end, old_start) some architectures
550 * have constraints on va-space that make this illegal (IA64) -
551 * for the others its just a little faster.
553 free_pgd_range(tlb, old_start, old_end, new_end,
554 vma->vm_next ? vma->vm_next->vm_start : 0);
556 tlb_finish_mmu(tlb, new_end, old_end);
559 * shrink the vma to just the new range.
561 vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
563 return 0;
566 #define EXTRA_STACK_VM_PAGES 20 /* random */
569 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
570 * the stack is optionally relocated, and some extra space is added.
572 int setup_arg_pages(struct linux_binprm *bprm,
573 unsigned long stack_top,
574 int executable_stack)
576 unsigned long ret;
577 unsigned long stack_shift;
578 struct mm_struct *mm = current->mm;
579 struct vm_area_struct *vma = bprm->vma;
580 struct vm_area_struct *prev = NULL;
581 unsigned long vm_flags;
582 unsigned long stack_base;
583 unsigned long stack_size;
584 unsigned long stack_expand;
585 unsigned long rlim_stack;
587 #ifdef CONFIG_STACK_GROWSUP
588 /* Limit stack size to 1GB */
589 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
590 if (stack_base > (1 << 30))
591 stack_base = 1 << 30;
593 /* Make sure we didn't let the argument array grow too large. */
594 if (vma->vm_end - vma->vm_start > stack_base)
595 return -ENOMEM;
597 stack_base = PAGE_ALIGN(stack_top - stack_base);
599 stack_shift = vma->vm_start - stack_base;
600 mm->arg_start = bprm->p - stack_shift;
601 bprm->p = vma->vm_end - stack_shift;
602 #else
603 stack_top = arch_align_stack(stack_top);
604 stack_top = PAGE_ALIGN(stack_top);
606 if (unlikely(stack_top < mmap_min_addr) ||
607 unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
608 return -ENOMEM;
610 stack_shift = vma->vm_end - stack_top;
612 bprm->p -= stack_shift;
613 mm->arg_start = bprm->p;
614 #endif
616 if (bprm->loader)
617 bprm->loader -= stack_shift;
618 bprm->exec -= stack_shift;
620 down_write(&mm->mmap_sem);
621 vm_flags = VM_STACK_FLAGS;
624 * Adjust stack execute permissions; explicitly enable for
625 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
626 * (arch default) otherwise.
628 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
629 vm_flags |= VM_EXEC;
630 else if (executable_stack == EXSTACK_DISABLE_X)
631 vm_flags &= ~VM_EXEC;
632 vm_flags |= mm->def_flags;
634 ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
635 vm_flags);
636 if (ret)
637 goto out_unlock;
638 BUG_ON(prev != vma);
640 /* Move stack pages down in memory. */
641 if (stack_shift) {
642 ret = shift_arg_pages(vma, stack_shift);
643 if (ret)
644 goto out_unlock;
647 stack_expand = EXTRA_STACK_VM_PAGES * PAGE_SIZE;
648 stack_size = vma->vm_end - vma->vm_start;
650 * Align this down to a page boundary as expand_stack
651 * will align it up.
653 rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
654 #ifdef CONFIG_STACK_GROWSUP
655 if (stack_size + stack_expand > rlim_stack)
656 stack_base = vma->vm_start + rlim_stack;
657 else
658 stack_base = vma->vm_end + stack_expand;
659 #else
660 if (stack_size + stack_expand > rlim_stack)
661 stack_base = vma->vm_end - rlim_stack;
662 else
663 stack_base = vma->vm_start - stack_expand;
664 #endif
665 ret = expand_stack(vma, stack_base);
666 if (ret)
667 ret = -EFAULT;
669 out_unlock:
670 up_write(&mm->mmap_sem);
671 return ret;
673 EXPORT_SYMBOL(setup_arg_pages);
675 #endif /* CONFIG_MMU */
677 struct file *open_exec(const char *name)
679 struct file *file;
680 int err;
682 file = do_filp_open(AT_FDCWD, name,
683 O_LARGEFILE | O_RDONLY | FMODE_EXEC, 0,
684 MAY_EXEC | MAY_OPEN);
685 if (IS_ERR(file))
686 goto out;
688 err = -EACCES;
689 if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
690 goto exit;
692 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
693 goto exit;
695 fsnotify_open(file->f_path.dentry);
697 err = deny_write_access(file);
698 if (err)
699 goto exit;
701 out:
702 return file;
704 exit:
705 fput(file);
706 return ERR_PTR(err);
708 EXPORT_SYMBOL(open_exec);
710 int kernel_read(struct file *file, loff_t offset,
711 char *addr, unsigned long count)
713 mm_segment_t old_fs;
714 loff_t pos = offset;
715 int result;
717 old_fs = get_fs();
718 set_fs(get_ds());
719 /* The cast to a user pointer is valid due to the set_fs() */
720 result = vfs_read(file, (void __user *)addr, count, &pos);
721 set_fs(old_fs);
722 return result;
725 EXPORT_SYMBOL(kernel_read);
727 static int exec_mmap(struct mm_struct *mm)
729 struct task_struct *tsk;
730 struct mm_struct * old_mm, *active_mm;
732 /* Notify parent that we're no longer interested in the old VM */
733 tsk = current;
734 old_mm = current->mm;
735 mm_release(tsk, old_mm);
737 if (old_mm) {
739 * Make sure that if there is a core dump in progress
740 * for the old mm, we get out and die instead of going
741 * through with the exec. We must hold mmap_sem around
742 * checking core_state and changing tsk->mm.
744 down_read(&old_mm->mmap_sem);
745 if (unlikely(old_mm->core_state)) {
746 up_read(&old_mm->mmap_sem);
747 return -EINTR;
750 task_lock(tsk);
751 active_mm = tsk->active_mm;
752 tsk->mm = mm;
753 tsk->active_mm = mm;
754 activate_mm(active_mm, mm);
755 task_unlock(tsk);
756 arch_pick_mmap_layout(mm);
757 if (old_mm) {
758 up_read(&old_mm->mmap_sem);
759 BUG_ON(active_mm != old_mm);
760 mm_update_next_owner(old_mm);
761 mmput(old_mm);
762 return 0;
764 mmdrop(active_mm);
765 return 0;
769 * This function makes sure the current process has its own signal table,
770 * so that flush_signal_handlers can later reset the handlers without
771 * disturbing other processes. (Other processes might share the signal
772 * table via the CLONE_SIGHAND option to clone().)
774 static int de_thread(struct task_struct *tsk)
776 struct signal_struct *sig = tsk->signal;
777 struct sighand_struct *oldsighand = tsk->sighand;
778 spinlock_t *lock = &oldsighand->siglock;
779 int count;
781 if (thread_group_empty(tsk))
782 goto no_thread_group;
785 * Kill all other threads in the thread group.
787 spin_lock_irq(lock);
788 if (signal_group_exit(sig)) {
790 * Another group action in progress, just
791 * return so that the signal is processed.
793 spin_unlock_irq(lock);
794 return -EAGAIN;
796 sig->group_exit_task = tsk;
797 zap_other_threads(tsk);
799 /* Account for the thread group leader hanging around: */
800 count = thread_group_leader(tsk) ? 1 : 2;
801 sig->notify_count = count;
802 while (atomic_read(&sig->count) > count) {
803 __set_current_state(TASK_UNINTERRUPTIBLE);
804 spin_unlock_irq(lock);
805 schedule();
806 spin_lock_irq(lock);
808 spin_unlock_irq(lock);
811 * At this point all other threads have exited, all we have to
812 * do is to wait for the thread group leader to become inactive,
813 * and to assume its PID:
815 if (!thread_group_leader(tsk)) {
816 struct task_struct *leader = tsk->group_leader;
818 sig->notify_count = -1; /* for exit_notify() */
819 for (;;) {
820 write_lock_irq(&tasklist_lock);
821 if (likely(leader->exit_state))
822 break;
823 __set_current_state(TASK_UNINTERRUPTIBLE);
824 write_unlock_irq(&tasklist_lock);
825 schedule();
829 * The only record we have of the real-time age of a
830 * process, regardless of execs it's done, is start_time.
831 * All the past CPU time is accumulated in signal_struct
832 * from sister threads now dead. But in this non-leader
833 * exec, nothing survives from the original leader thread,
834 * whose birth marks the true age of this process now.
835 * When we take on its identity by switching to its PID, we
836 * also take its birthdate (always earlier than our own).
838 tsk->start_time = leader->start_time;
840 BUG_ON(!same_thread_group(leader, tsk));
841 BUG_ON(has_group_leader_pid(tsk));
843 * An exec() starts a new thread group with the
844 * TGID of the previous thread group. Rehash the
845 * two threads with a switched PID, and release
846 * the former thread group leader:
849 /* Become a process group leader with the old leader's pid.
850 * The old leader becomes a thread of the this thread group.
851 * Note: The old leader also uses this pid until release_task
852 * is called. Odd but simple and correct.
854 detach_pid(tsk, PIDTYPE_PID);
855 tsk->pid = leader->pid;
856 attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
857 transfer_pid(leader, tsk, PIDTYPE_PGID);
858 transfer_pid(leader, tsk, PIDTYPE_SID);
860 list_replace_rcu(&leader->tasks, &tsk->tasks);
861 list_replace_init(&leader->sibling, &tsk->sibling);
863 tsk->group_leader = tsk;
864 leader->group_leader = tsk;
866 tsk->exit_signal = SIGCHLD;
868 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
869 leader->exit_state = EXIT_DEAD;
870 write_unlock_irq(&tasklist_lock);
872 release_task(leader);
875 sig->group_exit_task = NULL;
876 sig->notify_count = 0;
878 no_thread_group:
879 if (current->mm)
880 setmax_mm_hiwater_rss(&sig->maxrss, current->mm);
882 exit_itimers(sig);
883 flush_itimer_signals();
885 if (atomic_read(&oldsighand->count) != 1) {
886 struct sighand_struct *newsighand;
888 * This ->sighand is shared with the CLONE_SIGHAND
889 * but not CLONE_THREAD task, switch to the new one.
891 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
892 if (!newsighand)
893 return -ENOMEM;
895 atomic_set(&newsighand->count, 1);
896 memcpy(newsighand->action, oldsighand->action,
897 sizeof(newsighand->action));
899 write_lock_irq(&tasklist_lock);
900 spin_lock(&oldsighand->siglock);
901 rcu_assign_pointer(tsk->sighand, newsighand);
902 spin_unlock(&oldsighand->siglock);
903 write_unlock_irq(&tasklist_lock);
905 __cleanup_sighand(oldsighand);
908 BUG_ON(!thread_group_leader(tsk));
909 return 0;
913 * These functions flushes out all traces of the currently running executable
914 * so that a new one can be started
916 static void flush_old_files(struct files_struct * files)
918 long j = -1;
919 struct fdtable *fdt;
921 spin_lock(&files->file_lock);
922 for (;;) {
923 unsigned long set, i;
925 j++;
926 i = j * __NFDBITS;
927 fdt = files_fdtable(files);
928 if (i >= fdt->max_fds)
929 break;
930 set = fdt->close_on_exec->fds_bits[j];
931 if (!set)
932 continue;
933 fdt->close_on_exec->fds_bits[j] = 0;
934 spin_unlock(&files->file_lock);
935 for ( ; set ; i++,set >>= 1) {
936 if (set & 1) {
937 sys_close(i);
940 spin_lock(&files->file_lock);
943 spin_unlock(&files->file_lock);
946 char *get_task_comm(char *buf, struct task_struct *tsk)
948 /* buf must be at least sizeof(tsk->comm) in size */
949 task_lock(tsk);
950 strncpy(buf, tsk->comm, sizeof(tsk->comm));
951 task_unlock(tsk);
952 return buf;
955 void set_task_comm(struct task_struct *tsk, char *buf)
957 task_lock(tsk);
960 * Threads may access current->comm without holding
961 * the task lock, so write the string carefully.
962 * Readers without a lock may see incomplete new
963 * names but are safe from non-terminating string reads.
965 memset(tsk->comm, 0, TASK_COMM_LEN);
966 wmb();
967 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
968 task_unlock(tsk);
969 perf_event_comm(tsk);
972 int flush_old_exec(struct linux_binprm * bprm)
974 int retval;
977 * Make sure we have a private signal table and that
978 * we are unassociated from the previous thread group.
980 retval = de_thread(current);
981 if (retval)
982 goto out;
984 set_mm_exe_file(bprm->mm, bprm->file);
987 * Release all of the old mmap stuff
989 retval = exec_mmap(bprm->mm);
990 if (retval)
991 goto out;
993 bprm->mm = NULL; /* We're using it now */
995 current->flags &= ~PF_RANDOMIZE;
996 flush_thread();
997 current->personality &= ~bprm->per_clear;
999 return 0;
1001 out:
1002 return retval;
1004 EXPORT_SYMBOL(flush_old_exec);
1006 void setup_new_exec(struct linux_binprm * bprm)
1008 int i, ch;
1009 char * name;
1010 char tcomm[sizeof(current->comm)];
1012 arch_pick_mmap_layout(current->mm);
1014 /* This is the point of no return */
1015 current->sas_ss_sp = current->sas_ss_size = 0;
1017 if (current_euid() == current_uid() && current_egid() == current_gid())
1018 set_dumpable(current->mm, 1);
1019 else
1020 set_dumpable(current->mm, suid_dumpable);
1022 name = bprm->filename;
1024 /* Copies the binary name from after last slash */
1025 for (i=0; (ch = *(name++)) != '\0';) {
1026 if (ch == '/')
1027 i = 0; /* overwrite what we wrote */
1028 else
1029 if (i < (sizeof(tcomm) - 1))
1030 tcomm[i++] = ch;
1032 tcomm[i] = '\0';
1033 set_task_comm(current, tcomm);
1035 /* Set the new mm task size. We have to do that late because it may
1036 * depend on TIF_32BIT which is only updated in flush_thread() on
1037 * some architectures like powerpc
1039 current->mm->task_size = TASK_SIZE;
1041 /* install the new credentials */
1042 if (bprm->cred->uid != current_euid() ||
1043 bprm->cred->gid != current_egid()) {
1044 current->pdeath_signal = 0;
1045 } else if (file_permission(bprm->file, MAY_READ) ||
1046 bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP) {
1047 set_dumpable(current->mm, suid_dumpable);
1051 * Flush performance counters when crossing a
1052 * security domain:
1054 if (!get_dumpable(current->mm))
1055 perf_event_exit_task(current);
1057 /* An exec changes our domain. We are no longer part of the thread
1058 group */
1060 current->self_exec_id++;
1062 flush_signal_handlers(current, 0);
1063 flush_old_files(current->files);
1065 EXPORT_SYMBOL(setup_new_exec);
1068 * Prepare credentials and lock ->cred_guard_mutex.
1069 * install_exec_creds() commits the new creds and drops the lock.
1070 * Or, if exec fails before, free_bprm() should release ->cred and
1071 * and unlock.
1073 int prepare_bprm_creds(struct linux_binprm *bprm)
1075 if (mutex_lock_interruptible(&current->cred_guard_mutex))
1076 return -ERESTARTNOINTR;
1078 bprm->cred = prepare_exec_creds();
1079 if (likely(bprm->cred))
1080 return 0;
1082 mutex_unlock(&current->cred_guard_mutex);
1083 return -ENOMEM;
1086 void free_bprm(struct linux_binprm *bprm)
1088 free_arg_pages(bprm);
1089 if (bprm->cred) {
1090 mutex_unlock(&current->cred_guard_mutex);
1091 abort_creds(bprm->cred);
1093 kfree(bprm);
1097 * install the new credentials for this executable
1099 void install_exec_creds(struct linux_binprm *bprm)
1101 security_bprm_committing_creds(bprm);
1103 commit_creds(bprm->cred);
1104 bprm->cred = NULL;
1106 * cred_guard_mutex must be held at least to this point to prevent
1107 * ptrace_attach() from altering our determination of the task's
1108 * credentials; any time after this it may be unlocked.
1110 security_bprm_committed_creds(bprm);
1111 mutex_unlock(&current->cred_guard_mutex);
1113 EXPORT_SYMBOL(install_exec_creds);
1116 * determine how safe it is to execute the proposed program
1117 * - the caller must hold current->cred_guard_mutex to protect against
1118 * PTRACE_ATTACH
1120 int check_unsafe_exec(struct linux_binprm *bprm)
1122 struct task_struct *p = current, *t;
1123 unsigned n_fs;
1124 int res = 0;
1126 bprm->unsafe = tracehook_unsafe_exec(p);
1128 n_fs = 1;
1129 write_lock(&p->fs->lock);
1130 rcu_read_lock();
1131 for (t = next_thread(p); t != p; t = next_thread(t)) {
1132 if (t->fs == p->fs)
1133 n_fs++;
1135 rcu_read_unlock();
1137 if (p->fs->users > n_fs) {
1138 bprm->unsafe |= LSM_UNSAFE_SHARE;
1139 } else {
1140 res = -EAGAIN;
1141 if (!p->fs->in_exec) {
1142 p->fs->in_exec = 1;
1143 res = 1;
1146 write_unlock(&p->fs->lock);
1148 return res;
1152 * Fill the binprm structure from the inode.
1153 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1155 * This may be called multiple times for binary chains (scripts for example).
1157 int prepare_binprm(struct linux_binprm *bprm)
1159 umode_t mode;
1160 struct inode * inode = bprm->file->f_path.dentry->d_inode;
1161 int retval;
1163 mode = inode->i_mode;
1164 if (bprm->file->f_op == NULL)
1165 return -EACCES;
1167 /* clear any previous set[ug]id data from a previous binary */
1168 bprm->cred->euid = current_euid();
1169 bprm->cred->egid = current_egid();
1171 if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
1172 /* Set-uid? */
1173 if (mode & S_ISUID) {
1174 bprm->per_clear |= PER_CLEAR_ON_SETID;
1175 bprm->cred->euid = inode->i_uid;
1178 /* Set-gid? */
1180 * If setgid is set but no group execute bit then this
1181 * is a candidate for mandatory locking, not a setgid
1182 * executable.
1184 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1185 bprm->per_clear |= PER_CLEAR_ON_SETID;
1186 bprm->cred->egid = inode->i_gid;
1190 /* fill in binprm security blob */
1191 retval = security_bprm_set_creds(bprm);
1192 if (retval)
1193 return retval;
1194 bprm->cred_prepared = 1;
1196 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1197 return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1200 EXPORT_SYMBOL(prepare_binprm);
1203 * Arguments are '\0' separated strings found at the location bprm->p
1204 * points to; chop off the first by relocating brpm->p to right after
1205 * the first '\0' encountered.
1207 int remove_arg_zero(struct linux_binprm *bprm)
1209 int ret = 0;
1210 unsigned long offset;
1211 char *kaddr;
1212 struct page *page;
1214 if (!bprm->argc)
1215 return 0;
1217 do {
1218 offset = bprm->p & ~PAGE_MASK;
1219 page = get_arg_page(bprm, bprm->p, 0);
1220 if (!page) {
1221 ret = -EFAULT;
1222 goto out;
1224 kaddr = kmap_atomic(page, KM_USER0);
1226 for (; offset < PAGE_SIZE && kaddr[offset];
1227 offset++, bprm->p++)
1230 kunmap_atomic(kaddr, KM_USER0);
1231 put_arg_page(page);
1233 if (offset == PAGE_SIZE)
1234 free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1235 } while (offset == PAGE_SIZE);
1237 bprm->p++;
1238 bprm->argc--;
1239 ret = 0;
1241 out:
1242 return ret;
1244 EXPORT_SYMBOL(remove_arg_zero);
1247 * cycle the list of binary formats handler, until one recognizes the image
1249 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1251 unsigned int depth = bprm->recursion_depth;
1252 int try,retval;
1253 struct linux_binfmt *fmt;
1255 retval = security_bprm_check(bprm);
1256 if (retval)
1257 return retval;
1259 /* kernel module loader fixup */
1260 /* so we don't try to load run modprobe in kernel space. */
1261 set_fs(USER_DS);
1263 retval = audit_bprm(bprm);
1264 if (retval)
1265 return retval;
1267 retval = -ENOENT;
1268 for (try=0; try<2; try++) {
1269 read_lock(&binfmt_lock);
1270 list_for_each_entry(fmt, &formats, lh) {
1271 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1272 if (!fn)
1273 continue;
1274 if (!try_module_get(fmt->module))
1275 continue;
1276 read_unlock(&binfmt_lock);
1277 retval = fn(bprm, regs);
1279 * Restore the depth counter to its starting value
1280 * in this call, so we don't have to rely on every
1281 * load_binary function to restore it on return.
1283 bprm->recursion_depth = depth;
1284 if (retval >= 0) {
1285 if (depth == 0)
1286 tracehook_report_exec(fmt, bprm, regs);
1287 put_binfmt(fmt);
1288 allow_write_access(bprm->file);
1289 if (bprm->file)
1290 fput(bprm->file);
1291 bprm->file = NULL;
1292 current->did_exec = 1;
1293 proc_exec_connector(current);
1294 return retval;
1296 read_lock(&binfmt_lock);
1297 put_binfmt(fmt);
1298 if (retval != -ENOEXEC || bprm->mm == NULL)
1299 break;
1300 if (!bprm->file) {
1301 read_unlock(&binfmt_lock);
1302 return retval;
1305 read_unlock(&binfmt_lock);
1306 if (retval != -ENOEXEC || bprm->mm == NULL) {
1307 break;
1308 #ifdef CONFIG_MODULES
1309 } else {
1310 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1311 if (printable(bprm->buf[0]) &&
1312 printable(bprm->buf[1]) &&
1313 printable(bprm->buf[2]) &&
1314 printable(bprm->buf[3]))
1315 break; /* -ENOEXEC */
1316 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1317 #endif
1320 return retval;
1323 EXPORT_SYMBOL(search_binary_handler);
1326 * sys_execve() executes a new program.
1328 int do_execve(char * filename,
1329 char __user *__user *argv,
1330 char __user *__user *envp,
1331 struct pt_regs * regs)
1333 struct linux_binprm *bprm;
1334 struct file *file;
1335 struct files_struct *displaced;
1336 bool clear_in_exec;
1337 int retval;
1339 retval = unshare_files(&displaced);
1340 if (retval)
1341 goto out_ret;
1343 retval = -ENOMEM;
1344 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1345 if (!bprm)
1346 goto out_files;
1348 retval = prepare_bprm_creds(bprm);
1349 if (retval)
1350 goto out_free;
1352 retval = check_unsafe_exec(bprm);
1353 if (retval < 0)
1354 goto out_free;
1355 clear_in_exec = retval;
1356 current->in_execve = 1;
1358 file = open_exec(filename);
1359 retval = PTR_ERR(file);
1360 if (IS_ERR(file))
1361 goto out_unmark;
1363 sched_exec();
1365 bprm->file = file;
1366 bprm->filename = filename;
1367 bprm->interp = filename;
1369 retval = bprm_mm_init(bprm);
1370 if (retval)
1371 goto out_file;
1373 bprm->argc = count(argv, MAX_ARG_STRINGS);
1374 if ((retval = bprm->argc) < 0)
1375 goto out;
1377 bprm->envc = count(envp, MAX_ARG_STRINGS);
1378 if ((retval = bprm->envc) < 0)
1379 goto out;
1381 retval = prepare_binprm(bprm);
1382 if (retval < 0)
1383 goto out;
1385 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1386 if (retval < 0)
1387 goto out;
1389 bprm->exec = bprm->p;
1390 retval = copy_strings(bprm->envc, envp, bprm);
1391 if (retval < 0)
1392 goto out;
1394 retval = copy_strings(bprm->argc, argv, bprm);
1395 if (retval < 0)
1396 goto out;
1398 current->flags &= ~PF_KTHREAD;
1399 retval = search_binary_handler(bprm,regs);
1400 if (retval < 0)
1401 goto out;
1403 /* execve succeeded */
1404 current->fs->in_exec = 0;
1405 current->in_execve = 0;
1406 acct_update_integrals(current);
1407 free_bprm(bprm);
1408 if (displaced)
1409 put_files_struct(displaced);
1410 return retval;
1412 out:
1413 if (bprm->mm)
1414 mmput (bprm->mm);
1416 out_file:
1417 if (bprm->file) {
1418 allow_write_access(bprm->file);
1419 fput(bprm->file);
1422 out_unmark:
1423 if (clear_in_exec)
1424 current->fs->in_exec = 0;
1425 current->in_execve = 0;
1427 out_free:
1428 free_bprm(bprm);
1430 out_files:
1431 if (displaced)
1432 reset_files_struct(displaced);
1433 out_ret:
1434 return retval;
1437 void set_binfmt(struct linux_binfmt *new)
1439 struct mm_struct *mm = current->mm;
1441 if (mm->binfmt)
1442 module_put(mm->binfmt->module);
1444 mm->binfmt = new;
1445 if (new)
1446 __module_get(new->module);
1449 EXPORT_SYMBOL(set_binfmt);
1451 /* format_corename will inspect the pattern parameter, and output a
1452 * name into corename, which must have space for at least
1453 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1455 static int format_corename(char *corename, long signr)
1457 const struct cred *cred = current_cred();
1458 const char *pat_ptr = core_pattern;
1459 int ispipe = (*pat_ptr == '|');
1460 char *out_ptr = corename;
1461 char *const out_end = corename + CORENAME_MAX_SIZE;
1462 int rc;
1463 int pid_in_pattern = 0;
1465 /* Repeat as long as we have more pattern to process and more output
1466 space */
1467 while (*pat_ptr) {
1468 if (*pat_ptr != '%') {
1469 if (out_ptr == out_end)
1470 goto out;
1471 *out_ptr++ = *pat_ptr++;
1472 } else {
1473 switch (*++pat_ptr) {
1474 case 0:
1475 goto out;
1476 /* Double percent, output one percent */
1477 case '%':
1478 if (out_ptr == out_end)
1479 goto out;
1480 *out_ptr++ = '%';
1481 break;
1482 /* pid */
1483 case 'p':
1484 pid_in_pattern = 1;
1485 rc = snprintf(out_ptr, out_end - out_ptr,
1486 "%d", task_tgid_vnr(current));
1487 if (rc > out_end - out_ptr)
1488 goto out;
1489 out_ptr += rc;
1490 break;
1491 /* uid */
1492 case 'u':
1493 rc = snprintf(out_ptr, out_end - out_ptr,
1494 "%d", cred->uid);
1495 if (rc > out_end - out_ptr)
1496 goto out;
1497 out_ptr += rc;
1498 break;
1499 /* gid */
1500 case 'g':
1501 rc = snprintf(out_ptr, out_end - out_ptr,
1502 "%d", cred->gid);
1503 if (rc > out_end - out_ptr)
1504 goto out;
1505 out_ptr += rc;
1506 break;
1507 /* signal that caused the coredump */
1508 case 's':
1509 rc = snprintf(out_ptr, out_end - out_ptr,
1510 "%ld", signr);
1511 if (rc > out_end - out_ptr)
1512 goto out;
1513 out_ptr += rc;
1514 break;
1515 /* UNIX time of coredump */
1516 case 't': {
1517 struct timeval tv;
1518 do_gettimeofday(&tv);
1519 rc = snprintf(out_ptr, out_end - out_ptr,
1520 "%lu", tv.tv_sec);
1521 if (rc > out_end - out_ptr)
1522 goto out;
1523 out_ptr += rc;
1524 break;
1526 /* hostname */
1527 case 'h':
1528 down_read(&uts_sem);
1529 rc = snprintf(out_ptr, out_end - out_ptr,
1530 "%s", utsname()->nodename);
1531 up_read(&uts_sem);
1532 if (rc > out_end - out_ptr)
1533 goto out;
1534 out_ptr += rc;
1535 break;
1536 /* executable */
1537 case 'e':
1538 rc = snprintf(out_ptr, out_end - out_ptr,
1539 "%s", current->comm);
1540 if (rc > out_end - out_ptr)
1541 goto out;
1542 out_ptr += rc;
1543 break;
1544 /* core limit size */
1545 case 'c':
1546 rc = snprintf(out_ptr, out_end - out_ptr,
1547 "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
1548 if (rc > out_end - out_ptr)
1549 goto out;
1550 out_ptr += rc;
1551 break;
1552 default:
1553 break;
1555 ++pat_ptr;
1558 /* Backward compatibility with core_uses_pid:
1560 * If core_pattern does not include a %p (as is the default)
1561 * and core_uses_pid is set, then .%pid will be appended to
1562 * the filename. Do not do this for piped commands. */
1563 if (!ispipe && !pid_in_pattern && core_uses_pid) {
1564 rc = snprintf(out_ptr, out_end - out_ptr,
1565 ".%d", task_tgid_vnr(current));
1566 if (rc > out_end - out_ptr)
1567 goto out;
1568 out_ptr += rc;
1570 out:
1571 *out_ptr = 0;
1572 return ispipe;
1575 static int zap_process(struct task_struct *start)
1577 struct task_struct *t;
1578 int nr = 0;
1580 start->signal->flags = SIGNAL_GROUP_EXIT;
1581 start->signal->group_stop_count = 0;
1583 t = start;
1584 do {
1585 if (t != current && t->mm) {
1586 sigaddset(&t->pending.signal, SIGKILL);
1587 signal_wake_up(t, 1);
1588 nr++;
1590 } while_each_thread(start, t);
1592 return nr;
1595 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1596 struct core_state *core_state, int exit_code)
1598 struct task_struct *g, *p;
1599 unsigned long flags;
1600 int nr = -EAGAIN;
1602 spin_lock_irq(&tsk->sighand->siglock);
1603 if (!signal_group_exit(tsk->signal)) {
1604 mm->core_state = core_state;
1605 tsk->signal->group_exit_code = exit_code;
1606 nr = zap_process(tsk);
1608 spin_unlock_irq(&tsk->sighand->siglock);
1609 if (unlikely(nr < 0))
1610 return nr;
1612 if (atomic_read(&mm->mm_users) == nr + 1)
1613 goto done;
1615 * We should find and kill all tasks which use this mm, and we should
1616 * count them correctly into ->nr_threads. We don't take tasklist
1617 * lock, but this is safe wrt:
1619 * fork:
1620 * None of sub-threads can fork after zap_process(leader). All
1621 * processes which were created before this point should be
1622 * visible to zap_threads() because copy_process() adds the new
1623 * process to the tail of init_task.tasks list, and lock/unlock
1624 * of ->siglock provides a memory barrier.
1626 * do_exit:
1627 * The caller holds mm->mmap_sem. This means that the task which
1628 * uses this mm can't pass exit_mm(), so it can't exit or clear
1629 * its ->mm.
1631 * de_thread:
1632 * It does list_replace_rcu(&leader->tasks, &current->tasks),
1633 * we must see either old or new leader, this does not matter.
1634 * However, it can change p->sighand, so lock_task_sighand(p)
1635 * must be used. Since p->mm != NULL and we hold ->mmap_sem
1636 * it can't fail.
1638 * Note also that "g" can be the old leader with ->mm == NULL
1639 * and already unhashed and thus removed from ->thread_group.
1640 * This is OK, __unhash_process()->list_del_rcu() does not
1641 * clear the ->next pointer, we will find the new leader via
1642 * next_thread().
1644 rcu_read_lock();
1645 for_each_process(g) {
1646 if (g == tsk->group_leader)
1647 continue;
1648 if (g->flags & PF_KTHREAD)
1649 continue;
1650 p = g;
1651 do {
1652 if (p->mm) {
1653 if (unlikely(p->mm == mm)) {
1654 lock_task_sighand(p, &flags);
1655 nr += zap_process(p);
1656 unlock_task_sighand(p, &flags);
1658 break;
1660 } while_each_thread(g, p);
1662 rcu_read_unlock();
1663 done:
1664 atomic_set(&core_state->nr_threads, nr);
1665 return nr;
1668 static int coredump_wait(int exit_code, struct core_state *core_state)
1670 struct task_struct *tsk = current;
1671 struct mm_struct *mm = tsk->mm;
1672 struct completion *vfork_done;
1673 int core_waiters;
1675 init_completion(&core_state->startup);
1676 core_state->dumper.task = tsk;
1677 core_state->dumper.next = NULL;
1678 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
1679 up_write(&mm->mmap_sem);
1681 if (unlikely(core_waiters < 0))
1682 goto fail;
1685 * Make sure nobody is waiting for us to release the VM,
1686 * otherwise we can deadlock when we wait on each other
1688 vfork_done = tsk->vfork_done;
1689 if (vfork_done) {
1690 tsk->vfork_done = NULL;
1691 complete(vfork_done);
1694 if (core_waiters)
1695 wait_for_completion(&core_state->startup);
1696 fail:
1697 return core_waiters;
1700 static void coredump_finish(struct mm_struct *mm)
1702 struct core_thread *curr, *next;
1703 struct task_struct *task;
1705 next = mm->core_state->dumper.next;
1706 while ((curr = next) != NULL) {
1707 next = curr->next;
1708 task = curr->task;
1710 * see exit_mm(), curr->task must not see
1711 * ->task == NULL before we read ->next.
1713 smp_mb();
1714 curr->task = NULL;
1715 wake_up_process(task);
1718 mm->core_state = NULL;
1722 * set_dumpable converts traditional three-value dumpable to two flags and
1723 * stores them into mm->flags. It modifies lower two bits of mm->flags, but
1724 * these bits are not changed atomically. So get_dumpable can observe the
1725 * intermediate state. To avoid doing unexpected behavior, get get_dumpable
1726 * return either old dumpable or new one by paying attention to the order of
1727 * modifying the bits.
1729 * dumpable | mm->flags (binary)
1730 * old new | initial interim final
1731 * ---------+-----------------------
1732 * 0 1 | 00 01 01
1733 * 0 2 | 00 10(*) 11
1734 * 1 0 | 01 00 00
1735 * 1 2 | 01 11 11
1736 * 2 0 | 11 10(*) 00
1737 * 2 1 | 11 11 01
1739 * (*) get_dumpable regards interim value of 10 as 11.
1741 void set_dumpable(struct mm_struct *mm, int value)
1743 switch (value) {
1744 case 0:
1745 clear_bit(MMF_DUMPABLE, &mm->flags);
1746 smp_wmb();
1747 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1748 break;
1749 case 1:
1750 set_bit(MMF_DUMPABLE, &mm->flags);
1751 smp_wmb();
1752 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
1753 break;
1754 case 2:
1755 set_bit(MMF_DUMP_SECURELY, &mm->flags);
1756 smp_wmb();
1757 set_bit(MMF_DUMPABLE, &mm->flags);
1758 break;
1762 int get_dumpable(struct mm_struct *mm)
1764 int ret;
1766 ret = mm->flags & 0x3;
1767 return (ret >= 2) ? 2 : ret;
1770 static void wait_for_dump_helpers(struct file *file)
1772 struct pipe_inode_info *pipe;
1774 pipe = file->f_path.dentry->d_inode->i_pipe;
1776 pipe_lock(pipe);
1777 pipe->readers++;
1778 pipe->writers--;
1780 while ((pipe->readers > 1) && (!signal_pending(current))) {
1781 wake_up_interruptible_sync(&pipe->wait);
1782 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1783 pipe_wait(pipe);
1786 pipe->readers--;
1787 pipe->writers++;
1788 pipe_unlock(pipe);
1793 void do_coredump(long signr, int exit_code, struct pt_regs *regs)
1795 struct core_state core_state;
1796 char corename[CORENAME_MAX_SIZE + 1];
1797 struct mm_struct *mm = current->mm;
1798 struct linux_binfmt * binfmt;
1799 struct inode * inode;
1800 const struct cred *old_cred;
1801 struct cred *cred;
1802 int retval = 0;
1803 int flag = 0;
1804 int ispipe = 0;
1805 char **helper_argv = NULL;
1806 int helper_argc = 0;
1807 int dump_count = 0;
1808 static atomic_t core_dump_count = ATOMIC_INIT(0);
1809 struct coredump_params cprm = {
1810 .signr = signr,
1811 .regs = regs,
1812 .limit = current->signal->rlim[RLIMIT_CORE].rlim_cur,
1815 audit_core_dumps(signr);
1817 binfmt = mm->binfmt;
1818 if (!binfmt || !binfmt->core_dump)
1819 goto fail;
1821 cred = prepare_creds();
1822 if (!cred) {
1823 retval = -ENOMEM;
1824 goto fail;
1827 down_write(&mm->mmap_sem);
1829 * If another thread got here first, or we are not dumpable, bail out.
1831 if (mm->core_state || !get_dumpable(mm)) {
1832 up_write(&mm->mmap_sem);
1833 put_cred(cred);
1834 goto fail;
1838 * We cannot trust fsuid as being the "true" uid of the
1839 * process nor do we know its entire history. We only know it
1840 * was tainted so we dump it as root in mode 2.
1842 if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
1843 flag = O_EXCL; /* Stop rewrite attacks */
1844 cred->fsuid = 0; /* Dump root private */
1847 retval = coredump_wait(exit_code, &core_state);
1848 if (retval < 0) {
1849 put_cred(cred);
1850 goto fail;
1853 old_cred = override_creds(cred);
1856 * Clear any false indication of pending signals that might
1857 * be seen by the filesystem code called to write the core file.
1859 clear_thread_flag(TIF_SIGPENDING);
1862 * lock_kernel() because format_corename() is controlled by sysctl, which
1863 * uses lock_kernel()
1865 lock_kernel();
1866 ispipe = format_corename(corename, signr);
1867 unlock_kernel();
1869 if ((!ispipe) && (cprm.limit < binfmt->min_coredump))
1870 goto fail_unlock;
1872 if (ispipe) {
1873 if (cprm.limit == 0) {
1875 * Normally core limits are irrelevant to pipes, since
1876 * we're not writing to the file system, but we use
1877 * cprm.limit of 0 here as a speacial value. Any
1878 * non-zero limit gets set to RLIM_INFINITY below, but
1879 * a limit of 0 skips the dump. This is a consistent
1880 * way to catch recursive crashes. We can still crash
1881 * if the core_pattern binary sets RLIM_CORE = !0
1882 * but it runs as root, and can do lots of stupid things
1883 * Note that we use task_tgid_vnr here to grab the pid
1884 * of the process group leader. That way we get the
1885 * right pid if a thread in a multi-threaded
1886 * core_pattern process dies.
1888 printk(KERN_WARNING
1889 "Process %d(%s) has RLIMIT_CORE set to 0\n",
1890 task_tgid_vnr(current), current->comm);
1891 printk(KERN_WARNING "Aborting core\n");
1892 goto fail_unlock;
1895 dump_count = atomic_inc_return(&core_dump_count);
1896 if (core_pipe_limit && (core_pipe_limit < dump_count)) {
1897 printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
1898 task_tgid_vnr(current), current->comm);
1899 printk(KERN_WARNING "Skipping core dump\n");
1900 goto fail_dropcount;
1903 helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
1904 if (!helper_argv) {
1905 printk(KERN_WARNING "%s failed to allocate memory\n",
1906 __func__);
1907 goto fail_dropcount;
1910 cprm.limit = RLIM_INFINITY;
1912 /* SIGPIPE can happen, but it's just never processed */
1913 if (call_usermodehelper_pipe(helper_argv[0], helper_argv, NULL,
1914 &cprm.file)) {
1915 printk(KERN_INFO "Core dump to %s pipe failed\n",
1916 corename);
1917 goto fail_dropcount;
1919 } else
1920 cprm.file = filp_open(corename,
1921 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
1922 0600);
1923 if (IS_ERR(cprm.file))
1924 goto fail_dropcount;
1925 inode = cprm.file->f_path.dentry->d_inode;
1926 if (inode->i_nlink > 1)
1927 goto close_fail; /* multiple links - don't dump */
1928 if (!ispipe && d_unhashed(cprm.file->f_path.dentry))
1929 goto close_fail;
1931 /* AK: actually i see no reason to not allow this for named pipes etc.,
1932 but keep the previous behaviour for now. */
1933 if (!ispipe && !S_ISREG(inode->i_mode))
1934 goto close_fail;
1936 * Dont allow local users get cute and trick others to coredump
1937 * into their pre-created files:
1938 * Note, this is not relevant for pipes
1940 if (!ispipe && (inode->i_uid != current_fsuid()))
1941 goto close_fail;
1942 if (!cprm.file->f_op)
1943 goto close_fail;
1944 if (!cprm.file->f_op->write)
1945 goto close_fail;
1946 if (!ispipe &&
1947 do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file) != 0)
1948 goto close_fail;
1950 retval = binfmt->core_dump(&cprm);
1952 if (retval)
1953 current->signal->group_exit_code |= 0x80;
1954 close_fail:
1955 if (ispipe && core_pipe_limit)
1956 wait_for_dump_helpers(cprm.file);
1957 filp_close(cprm.file, NULL);
1958 fail_dropcount:
1959 if (dump_count)
1960 atomic_dec(&core_dump_count);
1961 fail_unlock:
1962 if (helper_argv)
1963 argv_free(helper_argv);
1965 revert_creds(old_cred);
1966 put_cred(cred);
1967 coredump_finish(mm);
1968 fail:
1969 return;