Merge remote-tracking branch 'origin/clustered' into aks_dev_clus
[projects/modsched/linux.git] / fs / exec.c
1 /*
2  *  linux/fs/exec.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * #!-checking implemented by tytso.
9  */
10 /*
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.
14  *
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.
17  *
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. 
23  */
24
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/swap.h>
32 #include <linux/string.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/perf_event.h>
36 #include <linux/highmem.h>
37 #include <linux/spinlock.h>
38 #include <linux/key.h>
39 #include <linux/personality.h>
40 #include <linux/binfmts.h>
41 #include <linux/utsname.h>
42 #include <linux/pid_namespace.h>
43 #include <linux/module.h>
44 #include <linux/namei.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/tsacct_kern.h>
49 #include <linux/cn_proc.h>
50 #include <linux/audit.h>
51 #include <linux/tracehook.h>
52 #include <linux/kmod.h>
53 #include <linux/fsnotify.h>
54 #include <linux/fs_struct.h>
55 #include <linux/pipe_fs_i.h>
56 #include <linux/oom.h>
57 #include <linux/compat.h>
58 #include <linux/process_server.h>
59
60 #include <asm/uaccess.h>
61 #include <asm/mmu_context.h>
62 #include <asm/tlb.h>
63 #include "internal.h"
64
65 int core_uses_pid;
66 char core_pattern[CORENAME_MAX_SIZE] = "core";
67 unsigned int core_pipe_limit;
68 int suid_dumpable = 0;
69
70 struct core_name {
71         char *corename;
72         int used, size;
73 };
74 static atomic_t call_count = ATOMIC_INIT(1);
75
76 /* The maximal length of core_pattern is also specified in sysctl.c */
77
78 static LIST_HEAD(formats);
79 static DEFINE_RWLOCK(binfmt_lock);
80
81 int __register_binfmt(struct linux_binfmt * fmt, int insert)
82 {
83         if (!fmt)
84                 return -EINVAL;
85         write_lock(&binfmt_lock);
86         insert ? list_add(&fmt->lh, &formats) :
87                  list_add_tail(&fmt->lh, &formats);
88         write_unlock(&binfmt_lock);
89         return 0;       
90 }
91
92 EXPORT_SYMBOL(__register_binfmt);
93
94 void unregister_binfmt(struct linux_binfmt * fmt)
95 {
96         write_lock(&binfmt_lock);
97         list_del(&fmt->lh);
98         write_unlock(&binfmt_lock);
99 }
100
101 EXPORT_SYMBOL(unregister_binfmt);
102
103 static inline void put_binfmt(struct linux_binfmt * fmt)
104 {
105         module_put(fmt->module);
106 }
107
108 /*
109  * Note that a shared library must be both readable and executable due to
110  * security reasons.
111  *
112  * Also note that we take the address to load from from the file itself.
113  */
114 SYSCALL_DEFINE1(uselib, const char __user *, library)
115 {
116         struct file *file;
117         char *tmp = getname(library);
118         int error = PTR_ERR(tmp);
119         static const struct open_flags uselib_flags = {
120                 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
121                 .acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
122                 .intent = LOOKUP_OPEN
123         };
124
125         if (IS_ERR(tmp))
126                 goto out;
127
128         file = do_filp_open(AT_FDCWD, tmp, &uselib_flags, LOOKUP_FOLLOW);
129         putname(tmp);
130         error = PTR_ERR(file);
131         if (IS_ERR(file))
132                 goto out;
133
134         error = -EINVAL;
135         if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
136                 goto exit;
137
138         error = -EACCES;
139         if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
140                 goto exit;
141
142         fsnotify_open(file);
143
144         error = -ENOEXEC;
145         if(file->f_op) {
146                 struct linux_binfmt * fmt;
147
148                 read_lock(&binfmt_lock);
149                 list_for_each_entry(fmt, &formats, lh) {
150                         if (!fmt->load_shlib)
151                                 continue;
152                         if (!try_module_get(fmt->module))
153                                 continue;
154                         read_unlock(&binfmt_lock);
155                         error = fmt->load_shlib(file);
156                         read_lock(&binfmt_lock);
157                         put_binfmt(fmt);
158                         if (error != -ENOEXEC)
159                                 break;
160                 }
161                 read_unlock(&binfmt_lock);
162         }
163 exit:
164         fput(file);
165 out:
166         return error;
167 }
168
169 #ifdef CONFIG_MMU
170 /*
171  * The nascent bprm->mm is not visible until exec_mmap() but it can
172  * use a lot of memory, account these pages in current->mm temporary
173  * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
174  * change the counter back via acct_arg_size(0).
175  */
176 static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
177 {
178         struct mm_struct *mm = current->mm;
179         long diff = (long)(pages - bprm->vma_pages);
180
181         if (!mm || !diff)
182                 return;
183
184         bprm->vma_pages = pages;
185         add_mm_counter(mm, MM_ANONPAGES, diff);
186 }
187
188 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
189                 int write)
190 {
191         struct page *page;
192         int ret;
193
194 #ifdef CONFIG_STACK_GROWSUP
195         if (write) {
196                 ret = expand_downwards(bprm->vma, pos);
197                 if (ret < 0)
198                         return NULL;
199         }
200 #endif
201         ret = get_user_pages(current, bprm->mm, pos,
202                         1, write, 1, &page, NULL);
203         if (ret <= 0)
204                 return NULL;
205
206         if (write) {
207                 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
208                 struct rlimit *rlim;
209
210                 acct_arg_size(bprm, size / PAGE_SIZE);
211
212                 /*
213                  * We've historically supported up to 32 pages (ARG_MAX)
214                  * of argument strings even with small stacks
215                  */
216                 if (size <= ARG_MAX)
217                         return page;
218
219                 /*
220                  * Limit to 1/4-th the stack size for the argv+env strings.
221                  * This ensures that:
222                  *  - the remaining binfmt code will not run out of stack space,
223                  *  - the program will have a reasonable amount of stack left
224                  *    to work from.
225                  */
226                 rlim = current->signal->rlim;
227                 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
228                         put_page(page);
229                         return NULL;
230                 }
231         }
232
233         return page;
234 }
235
236 static void put_arg_page(struct page *page)
237 {
238         put_page(page);
239 }
240
241 static void free_arg_page(struct linux_binprm *bprm, int i)
242 {
243 }
244
245 static void free_arg_pages(struct linux_binprm *bprm)
246 {
247 }
248
249 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
250                 struct page *page)
251 {
252         flush_cache_page(bprm->vma, pos, page_to_pfn(page));
253 }
254
255 static int __bprm_mm_init(struct linux_binprm *bprm)
256 {
257         int err;
258         struct vm_area_struct *vma = NULL;
259         struct mm_struct *mm = bprm->mm;
260
261         bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
262         if (!vma)
263                 return -ENOMEM;
264
265         down_write(&mm->mmap_sem);
266         vma->vm_mm = mm;
267
268         /*
269          * Place the stack at the largest stack address the architecture
270          * supports. Later, we'll move this to an appropriate place. We don't
271          * use STACK_TOP because that can depend on attributes which aren't
272          * configured yet.
273          */
274         BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
275         vma->vm_end = STACK_TOP_MAX;
276         vma->vm_start = vma->vm_end - PAGE_SIZE;
277         vma->vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
278         vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
279         INIT_LIST_HEAD(&vma->anon_vma_chain);
280
281         err = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
282         if (err)
283                 goto err;
284
285         err = insert_vm_struct(mm, vma);
286         if (err)
287                 goto err;
288
289         mm->stack_vm = mm->total_vm = 1;
290         up_write(&mm->mmap_sem);
291         bprm->p = vma->vm_end - sizeof(void *);
292         return 0;
293 err:
294         up_write(&mm->mmap_sem);
295         bprm->vma = NULL;
296         kmem_cache_free(vm_area_cachep, vma);
297         return err;
298 }
299
300 static bool valid_arg_len(struct linux_binprm *bprm, long len)
301 {
302         return len <= MAX_ARG_STRLEN;
303 }
304
305 #else
306
307 static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
308 {
309 }
310
311 static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
312                 int write)
313 {
314         struct page *page;
315
316         page = bprm->page[pos / PAGE_SIZE];
317         if (!page && write) {
318                 page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
319                 if (!page)
320                         return NULL;
321                 bprm->page[pos / PAGE_SIZE] = page;
322         }
323
324         return page;
325 }
326
327 static void put_arg_page(struct page *page)
328 {
329 }
330
331 static void free_arg_page(struct linux_binprm *bprm, int i)
332 {
333         if (bprm->page[i]) {
334                 __free_page(bprm->page[i]);
335                 bprm->page[i] = NULL;
336         }
337 }
338
339 static void free_arg_pages(struct linux_binprm *bprm)
340 {
341         int i;
342
343         for (i = 0; i < MAX_ARG_PAGES; i++)
344                 free_arg_page(bprm, i);
345 }
346
347 static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
348                 struct page *page)
349 {
350 }
351
352 static int __bprm_mm_init(struct linux_binprm *bprm)
353 {
354         bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
355         return 0;
356 }
357
358 static bool valid_arg_len(struct linux_binprm *bprm, long len)
359 {
360         return len <= bprm->p;
361 }
362
363 #endif /* CONFIG_MMU */
364
365 /*
366  * Create a new mm_struct and populate it with a temporary stack
367  * vm_area_struct.  We don't have enough context at this point to set the stack
368  * flags, permissions, and offset, so we use temporary values.  We'll update
369  * them later in setup_arg_pages().
370  */
371 int bprm_mm_init(struct linux_binprm *bprm)
372 {
373         int err;
374         struct mm_struct *mm = NULL;
375
376         bprm->mm = mm = mm_alloc();
377         err = -ENOMEM;
378         if (!mm)
379                 goto err;
380
381         err = init_new_context(current, mm);
382         if (err)
383                 goto err;
384
385         err = __bprm_mm_init(bprm);
386         if (err)
387                 goto err;
388
389         return 0;
390
391 err:
392         if (mm) {
393                 bprm->mm = NULL;
394                 mmdrop(mm);
395         }
396
397         return err;
398 }
399
400 struct user_arg_ptr {
401 #ifdef CONFIG_COMPAT
402         bool is_compat;
403 #endif
404         union {
405                 const char __user *const __user *native;
406 #ifdef CONFIG_COMPAT
407                 compat_uptr_t __user *compat;
408 #endif
409         } ptr;
410 };
411
412 static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
413 {
414         const char __user *native;
415
416 #ifdef CONFIG_COMPAT
417         if (unlikely(argv.is_compat)) {
418                 compat_uptr_t compat;
419
420                 if (get_user(compat, argv.ptr.compat + nr))
421                         return ERR_PTR(-EFAULT);
422
423                 return compat_ptr(compat);
424         }
425 #endif
426
427         if (get_user(native, argv.ptr.native + nr))
428                 return ERR_PTR(-EFAULT);
429
430         return native;
431 }
432
433 /*
434  * count() counts the number of strings in array ARGV.
435  */
436 static int count(struct user_arg_ptr argv, int max)
437 {
438         int i = 0;
439
440         if (argv.ptr.native != NULL) {
441                 for (;;) {
442                         const char __user *p = get_user_arg_ptr(argv, i);
443
444                         if (!p)
445                                 break;
446
447                         if (IS_ERR(p))
448                                 return -EFAULT;
449
450                         if (i++ >= max)
451                                 return -E2BIG;
452
453                         if (fatal_signal_pending(current))
454                                 return -ERESTARTNOHAND;
455                         cond_resched();
456                 }
457         }
458         return i;
459 }
460
461 /*
462  * 'copy_strings()' copies argument/environment strings from the old
463  * processes's memory to the new process's stack.  The call to get_user_pages()
464  * ensures the destination page is created and not swapped out.
465  */
466 static int copy_strings(int argc, struct user_arg_ptr argv,
467                         struct linux_binprm *bprm)
468 {
469         struct page *kmapped_page = NULL;
470         char *kaddr = NULL;
471         unsigned long kpos = 0;
472         int ret;
473
474         while (argc-- > 0) {
475                 const char __user *str;
476                 int len;
477                 unsigned long pos;
478
479                 ret = -EFAULT;
480                 str = get_user_arg_ptr(argv, argc);
481                 if (IS_ERR(str))
482                         goto out;
483
484                 len = strnlen_user(str, MAX_ARG_STRLEN);
485                 if (!len)
486                         goto out;
487
488                 ret = -E2BIG;
489                 if (!valid_arg_len(bprm, len))
490                         goto out;
491
492                 /* We're going to work our way backwords. */
493                 pos = bprm->p;
494                 str += len;
495                 bprm->p -= len;
496
497                 while (len > 0) {
498                         int offset, bytes_to_copy;
499
500                         if (fatal_signal_pending(current)) {
501                                 ret = -ERESTARTNOHAND;
502                                 goto out;
503                         }
504                         cond_resched();
505
506                         offset = pos % PAGE_SIZE;
507                         if (offset == 0)
508                                 offset = PAGE_SIZE;
509
510                         bytes_to_copy = offset;
511                         if (bytes_to_copy > len)
512                                 bytes_to_copy = len;
513
514                         offset -= bytes_to_copy;
515                         pos -= bytes_to_copy;
516                         str -= bytes_to_copy;
517                         len -= bytes_to_copy;
518
519                         if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
520                                 struct page *page;
521
522                                 page = get_arg_page(bprm, pos, 1);
523                                 if (!page) {
524                                         ret = -E2BIG;
525                                         goto out;
526                                 }
527
528                                 if (kmapped_page) {
529                                         flush_kernel_dcache_page(kmapped_page);
530                                         kunmap(kmapped_page);
531                                         put_arg_page(kmapped_page);
532                                 }
533                                 kmapped_page = page;
534                                 kaddr = kmap(kmapped_page);
535                                 kpos = pos & PAGE_MASK;
536                                 flush_arg_page(bprm, kpos, kmapped_page);
537                         }
538                         if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
539                                 ret = -EFAULT;
540                                 goto out;
541                         }
542                 }
543         }
544         ret = 0;
545 out:
546         if (kmapped_page) {
547                 flush_kernel_dcache_page(kmapped_page);
548                 kunmap(kmapped_page);
549                 put_arg_page(kmapped_page);
550         }
551         return ret;
552 }
553
554 /*
555  * Like copy_strings, but get argv and its values from kernel memory.
556  */
557 int copy_strings_kernel(int argc, const char *const *__argv,
558                         struct linux_binprm *bprm)
559 {
560         int r;
561         mm_segment_t oldfs = get_fs();
562         struct user_arg_ptr argv = {
563                 .ptr.native = (const char __user *const  __user *)__argv,
564         };
565
566         set_fs(KERNEL_DS);
567         r = copy_strings(argc, argv, bprm);
568         set_fs(oldfs);
569
570         return r;
571 }
572 EXPORT_SYMBOL(copy_strings_kernel);
573
574 #ifdef CONFIG_MMU
575
576 /*
577  * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX.  Once
578  * the binfmt code determines where the new stack should reside, we shift it to
579  * its final location.  The process proceeds as follows:
580  *
581  * 1) Use shift to calculate the new vma endpoints.
582  * 2) Extend vma to cover both the old and new ranges.  This ensures the
583  *    arguments passed to subsequent functions are consistent.
584  * 3) Move vma's page tables to the new range.
585  * 4) Free up any cleared pgd range.
586  * 5) Shrink the vma to cover only the new range.
587  */
588 static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
589 {
590         struct mm_struct *mm = vma->vm_mm;
591         unsigned long old_start = vma->vm_start;
592         unsigned long old_end = vma->vm_end;
593         unsigned long length = old_end - old_start;
594         unsigned long new_start = old_start - shift;
595         unsigned long new_end = old_end - shift;
596         struct mmu_gather tlb;
597
598         BUG_ON(new_start > new_end);
599
600         /*
601          * ensure there are no vmas between where we want to go
602          * and where we are
603          */
604         if (vma != find_vma(mm, new_start))
605                 return -EFAULT;
606
607         /*
608          * cover the whole range: [new_start, old_end)
609          */
610         if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
611                 return -ENOMEM;
612
613         /*
614          * move the page tables downwards, on failure we rely on
615          * process cleanup to remove whatever mess we made.
616          */
617         if (length != move_page_tables(vma, old_start,
618                                        vma, new_start, length))
619                 return -ENOMEM;
620
621         lru_add_drain();
622         tlb_gather_mmu(&tlb, mm, 0);
623         if (new_end > old_start) {
624                 /*
625                  * when the old and new regions overlap clear from new_end.
626                  */
627                 free_pgd_range(&tlb, new_end, old_end, new_end,
628                         vma->vm_next ? vma->vm_next->vm_start : 0);
629         } else {
630                 /*
631                  * otherwise, clean from old_start; this is done to not touch
632                  * the address space in [new_end, old_start) some architectures
633                  * have constraints on va-space that make this illegal (IA64) -
634                  * for the others its just a little faster.
635                  */
636                 free_pgd_range(&tlb, old_start, old_end, new_end,
637                         vma->vm_next ? vma->vm_next->vm_start : 0);
638         }
639         tlb_finish_mmu(&tlb, new_end, old_end);
640
641         /*
642          * Shrink the vma to just the new range.  Always succeeds.
643          */
644         vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
645
646         return 0;
647 }
648
649 /*
650  * Finalizes the stack vm_area_struct. The flags and permissions are updated,
651  * the stack is optionally relocated, and some extra space is added.
652  */
653 int setup_arg_pages(struct linux_binprm *bprm,
654                     unsigned long stack_top,
655                     int executable_stack)
656 {
657         unsigned long ret;
658         unsigned long stack_shift;
659         struct mm_struct *mm = current->mm;
660         struct vm_area_struct *vma = bprm->vma;
661         struct vm_area_struct *prev = NULL;
662         unsigned long vm_flags;
663         unsigned long stack_base;
664         unsigned long stack_size;
665         unsigned long stack_expand;
666         unsigned long rlim_stack;
667
668 #ifdef CONFIG_STACK_GROWSUP
669         /* Limit stack size to 1GB */
670         stack_base = rlimit_max(RLIMIT_STACK);
671         if (stack_base > (1 << 30))
672                 stack_base = 1 << 30;
673
674         /* Make sure we didn't let the argument array grow too large. */
675         if (vma->vm_end - vma->vm_start > stack_base)
676                 return -ENOMEM;
677
678         stack_base = PAGE_ALIGN(stack_top - stack_base);
679
680         stack_shift = vma->vm_start - stack_base;
681         mm->arg_start = bprm->p - stack_shift;
682         bprm->p = vma->vm_end - stack_shift;
683 #else
684         stack_top = arch_align_stack(stack_top);
685         stack_top = PAGE_ALIGN(stack_top);
686
687         if (unlikely(stack_top < mmap_min_addr) ||
688             unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
689                 return -ENOMEM;
690
691         stack_shift = vma->vm_end - stack_top;
692
693         bprm->p -= stack_shift;
694         mm->arg_start = bprm->p;
695 #endif
696
697         if (bprm->loader)
698                 bprm->loader -= stack_shift;
699         bprm->exec -= stack_shift;
700
701         down_write(&mm->mmap_sem);
702         vm_flags = VM_STACK_FLAGS;
703
704         /*
705          * Adjust stack execute permissions; explicitly enable for
706          * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
707          * (arch default) otherwise.
708          */
709         if (unlikely(executable_stack == EXSTACK_ENABLE_X))
710                 vm_flags |= VM_EXEC;
711         else if (executable_stack == EXSTACK_DISABLE_X)
712                 vm_flags &= ~VM_EXEC;
713         vm_flags |= mm->def_flags;
714         vm_flags |= VM_STACK_INCOMPLETE_SETUP;
715
716         ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
717                         vm_flags);
718         if (ret)
719                 goto out_unlock;
720         BUG_ON(prev != vma);
721
722         /* Move stack pages down in memory. */
723         if (stack_shift) {
724                 ret = shift_arg_pages(vma, stack_shift);
725                 if (ret)
726                         goto out_unlock;
727         }
728
729         /* mprotect_fixup is overkill to remove the temporary stack flags */
730         vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
731
732         stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
733         stack_size = vma->vm_end - vma->vm_start;
734         /*
735          * Align this down to a page boundary as expand_stack
736          * will align it up.
737          */
738         rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
739 #ifdef CONFIG_STACK_GROWSUP
740         if (stack_size + stack_expand > rlim_stack)
741                 stack_base = vma->vm_start + rlim_stack;
742         else
743                 stack_base = vma->vm_end + stack_expand;
744 #else
745         if (stack_size + stack_expand > rlim_stack)
746                 stack_base = vma->vm_end - rlim_stack;
747         else
748                 stack_base = vma->vm_start - stack_expand;
749 #endif
750         current->mm->start_stack = bprm->p;
751         ret = expand_stack(vma, stack_base);
752         if (ret)
753                 ret = -EFAULT;
754
755 out_unlock:
756         up_write(&mm->mmap_sem);
757         return ret;
758 }
759 EXPORT_SYMBOL(setup_arg_pages);
760
761 #endif /* CONFIG_MMU */
762
763 struct file *open_exec(const char *name)
764 {
765         struct file *file;
766         int err;
767         static const struct open_flags open_exec_flags = {
768                 .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
769                 .acc_mode = MAY_EXEC | MAY_OPEN,
770                 .intent = LOOKUP_OPEN
771         };
772
773         file = do_filp_open(AT_FDCWD, name, &open_exec_flags, LOOKUP_FOLLOW);
774         if (IS_ERR(file))
775                 goto out;
776
777         err = -EACCES;
778         if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
779                 goto exit;
780
781         if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
782                 goto exit;
783
784         fsnotify_open(file);
785
786         err = deny_write_access(file);
787         if (err)
788                 goto exit;
789
790 out:
791         return file;
792
793 exit:
794         fput(file);
795         return ERR_PTR(err);
796 }
797 EXPORT_SYMBOL(open_exec);
798
799 int kernel_read(struct file *file, loff_t offset,
800                 char *addr, unsigned long count)
801 {
802         mm_segment_t old_fs;
803         loff_t pos = offset;
804         int result;
805
806         old_fs = get_fs();
807         set_fs(get_ds());
808         /* The cast to a user pointer is valid due to the set_fs() */
809         result = vfs_read(file, (void __user *)addr, count, &pos);
810         set_fs(old_fs);
811         return result;
812 }
813
814 EXPORT_SYMBOL(kernel_read);
815
816 static int exec_mmap(struct mm_struct *mm)
817 {
818         struct task_struct *tsk;
819         struct mm_struct * old_mm, *active_mm;
820
821         /* Notify parent that we're no longer interested in the old VM */
822         tsk = current;
823         old_mm = current->mm;
824         sync_mm_rss(tsk, old_mm);
825         mm_release(tsk, old_mm);
826
827         if (old_mm) {
828                 /*
829                  * Make sure that if there is a core dump in progress
830                  * for the old mm, we get out and die instead of going
831                  * through with the exec.  We must hold mmap_sem around
832                  * checking core_state and changing tsk->mm.
833                  */
834                 down_read(&old_mm->mmap_sem);
835                 if (unlikely(old_mm->core_state)) {
836                         up_read(&old_mm->mmap_sem);
837                         return -EINTR;
838                 }
839         }
840         task_lock(tsk);
841         active_mm = tsk->active_mm;
842         tsk->mm = mm;
843         tsk->active_mm = mm;
844         activate_mm(active_mm, mm);
845         task_unlock(tsk);
846         arch_pick_mmap_layout(mm);
847         if (old_mm) {
848                 up_read(&old_mm->mmap_sem);
849                 BUG_ON(active_mm != old_mm);
850                 mm_update_next_owner(old_mm);
851                 mmput(old_mm);
852                 return 0;
853         }
854         mmdrop(active_mm);
855         return 0;
856 }
857
858 /*
859  * This function makes sure the current process has its own signal table,
860  * so that flush_signal_handlers can later reset the handlers without
861  * disturbing other processes.  (Other processes might share the signal
862  * table via the CLONE_SIGHAND option to clone().)
863  */
864 static int de_thread(struct task_struct *tsk)
865 {
866         struct signal_struct *sig = tsk->signal;
867         struct sighand_struct *oldsighand = tsk->sighand;
868         spinlock_t *lock = &oldsighand->siglock;
869
870         if (thread_group_empty(tsk))
871                 goto no_thread_group;
872
873         /*
874          * Kill all other threads in the thread group.
875          */
876         spin_lock_irq(lock);
877         if (signal_group_exit(sig)) {
878                 /*
879                  * Another group action in progress, just
880                  * return so that the signal is processed.
881                  */
882                 spin_unlock_irq(lock);
883                 return -EAGAIN;
884         }
885
886         sig->group_exit_task = tsk;
887         sig->notify_count = zap_other_threads(tsk);
888         if (!thread_group_leader(tsk))
889                 sig->notify_count--;
890
891         while (sig->notify_count) {
892                 __set_current_state(TASK_UNINTERRUPTIBLE);
893                 spin_unlock_irq(lock);
894                 schedule();
895                 spin_lock_irq(lock);
896         }
897         spin_unlock_irq(lock);
898
899         /*
900          * At this point all other threads have exited, all we have to
901          * do is to wait for the thread group leader to become inactive,
902          * and to assume its PID:
903          */
904         if (!thread_group_leader(tsk)) {
905                 struct task_struct *leader = tsk->group_leader;
906
907                 sig->notify_count = -1; /* for exit_notify() */
908                 for (;;) {
909                         write_lock_irq(&tasklist_lock);
910                         if (likely(leader->exit_state))
911                                 break;
912                         __set_current_state(TASK_UNINTERRUPTIBLE);
913                         write_unlock_irq(&tasklist_lock);
914                         schedule();
915                 }
916
917                 /*
918                  * The only record we have of the real-time age of a
919                  * process, regardless of execs it's done, is start_time.
920                  * All the past CPU time is accumulated in signal_struct
921                  * from sister threads now dead.  But in this non-leader
922                  * exec, nothing survives from the original leader thread,
923                  * whose birth marks the true age of this process now.
924                  * When we take on its identity by switching to its PID, we
925                  * also take its birthdate (always earlier than our own).
926                  */
927                 tsk->start_time = leader->start_time;
928
929                 BUG_ON(!same_thread_group(leader, tsk));
930                 BUG_ON(has_group_leader_pid(tsk));
931                 /*
932                  * An exec() starts a new thread group with the
933                  * TGID of the previous thread group. Rehash the
934                  * two threads with a switched PID, and release
935                  * the former thread group leader:
936                  */
937
938                 /* Become a process group leader with the old leader's pid.
939                  * The old leader becomes a thread of the this thread group.
940                  * Note: The old leader also uses this pid until release_task
941                  *       is called.  Odd but simple and correct.
942                  */
943                 detach_pid(tsk, PIDTYPE_PID);
944                 tsk->pid = leader->pid;
945                 attach_pid(tsk, PIDTYPE_PID,  task_pid(leader));
946                 transfer_pid(leader, tsk, PIDTYPE_PGID);
947                 transfer_pid(leader, tsk, PIDTYPE_SID);
948
949                 list_replace_rcu(&leader->tasks, &tsk->tasks);
950                 list_replace_init(&leader->sibling, &tsk->sibling);
951
952                 tsk->group_leader = tsk;
953                 leader->group_leader = tsk;
954
955                 tsk->exit_signal = SIGCHLD;
956                 leader->exit_signal = -1;
957
958                 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
959                 leader->exit_state = EXIT_DEAD;
960
961                 /*
962                  * We are going to release_task()->ptrace_unlink() silently,
963                  * the tracer can sleep in do_wait(). EXIT_DEAD guarantees
964                  * the tracer wont't block again waiting for this thread.
965                  */
966                 if (unlikely(leader->ptrace))
967                         __wake_up_parent(leader, leader->parent);
968                 write_unlock_irq(&tasklist_lock);
969
970                 release_task(leader);
971         }
972
973         sig->group_exit_task = NULL;
974         sig->notify_count = 0;
975
976 no_thread_group:
977         if (current->mm)
978                 setmax_mm_hiwater_rss(&sig->maxrss, current->mm);
979
980         exit_itimers(sig);
981         flush_itimer_signals();
982
983         if (atomic_read(&oldsighand->count) != 1) {
984                 struct sighand_struct *newsighand;
985                 /*
986                  * This ->sighand is shared with the CLONE_SIGHAND
987                  * but not CLONE_THREAD task, switch to the new one.
988                  */
989                 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
990                 if (!newsighand)
991                         return -ENOMEM;
992
993                 atomic_set(&newsighand->count, 1);
994                 memcpy(newsighand->action, oldsighand->action,
995                        sizeof(newsighand->action));
996
997                 write_lock_irq(&tasklist_lock);
998                 spin_lock(&oldsighand->siglock);
999                 rcu_assign_pointer(tsk->sighand, newsighand);
1000                 spin_unlock(&oldsighand->siglock);
1001                 write_unlock_irq(&tasklist_lock);
1002
1003                 __cleanup_sighand(oldsighand);
1004         }
1005
1006         BUG_ON(!thread_group_leader(tsk));
1007         return 0;
1008 }
1009
1010 /*
1011  * These functions flushes out all traces of the currently running executable
1012  * so that a new one can be started
1013  */
1014 /*static*/ void flush_old_files(struct files_struct * files)
1015 {
1016         long j = -1;
1017         struct fdtable *fdt;
1018
1019         spin_lock(&files->file_lock);
1020         for (;;) {
1021                 unsigned long set, i;
1022
1023                 j++;
1024                 i = j * __NFDBITS;
1025                 fdt = files_fdtable(files);
1026                 if (i >= fdt->max_fds)
1027                         break;
1028                 set = fdt->close_on_exec->fds_bits[j];
1029                 if (!set)
1030                         continue;
1031                 fdt->close_on_exec->fds_bits[j] = 0;
1032                 spin_unlock(&files->file_lock);
1033                 for ( ; set ; i++,set >>= 1) {
1034                         if (set & 1) {
1035                                 sys_close(i);
1036                         }
1037                 }
1038                 spin_lock(&files->file_lock);
1039
1040         }
1041         spin_unlock(&files->file_lock);
1042 }
1043
1044 char *get_task_comm(char *buf, struct task_struct *tsk)
1045 {
1046         /* buf must be at least sizeof(tsk->comm) in size */
1047         task_lock(tsk);
1048         strncpy(buf, tsk->comm, sizeof(tsk->comm));
1049         task_unlock(tsk);
1050         return buf;
1051 }
1052 EXPORT_SYMBOL_GPL(get_task_comm);
1053
1054 void set_task_comm(struct task_struct *tsk, char *buf)
1055 {
1056         task_lock(tsk);
1057
1058         /*
1059          * Threads may access current->comm without holding
1060          * the task lock, so write the string carefully.
1061          * Readers without a lock may see incomplete new
1062          * names but are safe from non-terminating string reads.
1063          */
1064         memset(tsk->comm, 0, TASK_COMM_LEN);
1065         wmb();
1066         strlcpy(tsk->comm, buf, sizeof(tsk->comm));
1067         task_unlock(tsk);
1068         perf_event_comm(tsk);
1069 }
1070
1071 int flush_old_exec(struct linux_binprm * bprm)
1072 {
1073         int retval;
1074
1075         /*
1076          * Make sure we have a private signal table and that
1077          * we are unassociated from the previous thread group.
1078          */
1079         retval = de_thread(current);
1080         if (retval)
1081                 goto out;
1082
1083         set_mm_exe_file(bprm->mm, bprm->file);
1084
1085         /*
1086          * Release all of the old mmap stuff
1087          */
1088         acct_arg_size(bprm, 0);
1089         retval = exec_mmap(bprm->mm);
1090         if (retval)
1091                 goto out;
1092
1093         bprm->mm = NULL;                /* We're using it now */
1094
1095         set_fs(USER_DS);
1096         current->flags &= ~(PF_RANDOMIZE | PF_KTHREAD);
1097         flush_thread();
1098         current->personality &= ~bprm->per_clear;
1099
1100         return 0;
1101
1102 out:
1103         return retval;
1104 }
1105 EXPORT_SYMBOL(flush_old_exec);
1106
1107 void would_dump(struct linux_binprm *bprm, struct file *file)
1108 {
1109         if (inode_permission(file->f_path.dentry->d_inode, MAY_READ) < 0)
1110                 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
1111 }
1112 EXPORT_SYMBOL(would_dump);
1113
1114 void setup_new_exec(struct linux_binprm * bprm)
1115 {
1116         int i, ch;
1117         const char *name;
1118         char tcomm[sizeof(current->comm)];
1119
1120         arch_pick_mmap_layout(current->mm);
1121
1122         /* This is the point of no return */
1123         current->sas_ss_sp = current->sas_ss_size = 0;
1124
1125         if (current_euid() == current_uid() && current_egid() == current_gid())
1126                 set_dumpable(current->mm, 1);
1127         else
1128                 set_dumpable(current->mm, suid_dumpable);
1129
1130         name = bprm->filename;
1131
1132         /* Copies the binary name from after last slash */
1133         for (i=0; (ch = *(name++)) != '\0';) {
1134                 if (ch == '/')
1135                         i = 0; /* overwrite what we wrote */
1136                 else
1137                         if (i < (sizeof(tcomm) - 1))
1138                                 tcomm[i++] = ch;
1139         }
1140         tcomm[i] = '\0';
1141         set_task_comm(current, tcomm);
1142
1143         /* Set the new mm task size. We have to do that late because it may
1144          * depend on TIF_32BIT which is only updated in flush_thread() on
1145          * some architectures like powerpc
1146          */
1147         current->mm->task_size = TASK_SIZE;
1148
1149         /* install the new credentials */
1150         if (bprm->cred->uid != current_euid() ||
1151             bprm->cred->gid != current_egid()) {
1152                 current->pdeath_signal = 0;
1153         } else {
1154                 would_dump(bprm, bprm->file);
1155                 if (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)
1156                         set_dumpable(current->mm, suid_dumpable);
1157         }
1158
1159         /*
1160          * Flush performance counters when crossing a
1161          * security domain:
1162          */
1163         if (!get_dumpable(current->mm))
1164                 perf_event_exit_task(current);
1165
1166         /* An exec changes our domain. We are no longer part of the thread
1167            group */
1168
1169         current->self_exec_id++;
1170                         
1171         flush_signal_handlers(current, 0);
1172         flush_old_files(current->files);
1173 }
1174 EXPORT_SYMBOL(setup_new_exec);
1175
1176 /*
1177  * Prepare credentials and lock ->cred_guard_mutex.
1178  * install_exec_creds() commits the new creds and drops the lock.
1179  * Or, if exec fails before, free_bprm() should release ->cred and
1180  * and unlock.
1181  */
1182 int prepare_bprm_creds(struct linux_binprm *bprm)
1183 {
1184         if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
1185                 return -ERESTARTNOINTR;
1186
1187         bprm->cred = prepare_exec_creds();
1188         if (likely(bprm->cred))
1189                 return 0;
1190
1191         mutex_unlock(&current->signal->cred_guard_mutex);
1192         return -ENOMEM;
1193 }
1194
1195 void free_bprm(struct linux_binprm *bprm)
1196 {
1197         free_arg_pages(bprm);
1198         if (bprm->cred) {
1199                 mutex_unlock(&current->signal->cred_guard_mutex);
1200                 abort_creds(bprm->cred);
1201         }
1202         kfree(bprm);
1203 }
1204
1205 /*
1206  * install the new credentials for this executable
1207  */
1208 void install_exec_creds(struct linux_binprm *bprm)
1209 {
1210         security_bprm_committing_creds(bprm);
1211
1212         commit_creds(bprm->cred);
1213         bprm->cred = NULL;
1214         /*
1215          * cred_guard_mutex must be held at least to this point to prevent
1216          * ptrace_attach() from altering our determination of the task's
1217          * credentials; any time after this it may be unlocked.
1218          */
1219         security_bprm_committed_creds(bprm);
1220         mutex_unlock(&current->signal->cred_guard_mutex);
1221 }
1222 EXPORT_SYMBOL(install_exec_creds);
1223
1224 /*
1225  * determine how safe it is to execute the proposed program
1226  * - the caller must hold ->cred_guard_mutex to protect against
1227  *   PTRACE_ATTACH
1228  */
1229 int check_unsafe_exec(struct linux_binprm *bprm)
1230 {
1231         struct task_struct *p = current, *t;
1232         unsigned n_fs;
1233         int res = 0;
1234
1235         if (p->ptrace) {
1236                 if (p->ptrace & PT_PTRACE_CAP)
1237                         bprm->unsafe |= LSM_UNSAFE_PTRACE_CAP;
1238                 else
1239                         bprm->unsafe |= LSM_UNSAFE_PTRACE;
1240         }
1241
1242         n_fs = 1;
1243         spin_lock(&p->fs->lock);
1244         rcu_read_lock();
1245         for (t = next_thread(p); t != p; t = next_thread(t)) {
1246                 if (t->fs == p->fs)
1247                         n_fs++;
1248         }
1249         rcu_read_unlock();
1250
1251         if (p->fs->users > n_fs) {
1252                 bprm->unsafe |= LSM_UNSAFE_SHARE;
1253         } else {
1254                 res = -EAGAIN;
1255                 if (!p->fs->in_exec) {
1256                         p->fs->in_exec = 1;
1257                         res = 1;
1258                 }
1259         }
1260         spin_unlock(&p->fs->lock);
1261
1262         return res;
1263 }
1264
1265 /* 
1266  * Fill the binprm structure from the inode. 
1267  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
1268  *
1269  * This may be called multiple times for binary chains (scripts for example).
1270  */
1271 int prepare_binprm(struct linux_binprm *bprm)
1272 {
1273         umode_t mode;
1274         struct inode * inode = bprm->file->f_path.dentry->d_inode;
1275         int retval;
1276
1277         mode = inode->i_mode;
1278         if (bprm->file->f_op == NULL)
1279                 return -EACCES;
1280
1281         /* clear any previous set[ug]id data from a previous binary */
1282         bprm->cred->euid = current_euid();
1283         bprm->cred->egid = current_egid();
1284
1285         if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
1286                 /* Set-uid? */
1287                 if (mode & S_ISUID) {
1288                         bprm->per_clear |= PER_CLEAR_ON_SETID;
1289                         bprm->cred->euid = inode->i_uid;
1290                 }
1291
1292                 /* Set-gid? */
1293                 /*
1294                  * If setgid is set but no group execute bit then this
1295                  * is a candidate for mandatory locking, not a setgid
1296                  * executable.
1297                  */
1298                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1299                         bprm->per_clear |= PER_CLEAR_ON_SETID;
1300                         bprm->cred->egid = inode->i_gid;
1301                 }
1302         }
1303
1304         /* fill in binprm security blob */
1305         retval = security_bprm_set_creds(bprm);
1306         if (retval)
1307                 return retval;
1308         bprm->cred_prepared = 1;
1309
1310         memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1311         return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1312 }
1313
1314 EXPORT_SYMBOL(prepare_binprm);
1315
1316 /*
1317  * Arguments are '\0' separated strings found at the location bprm->p
1318  * points to; chop off the first by relocating brpm->p to right after
1319  * the first '\0' encountered.
1320  */
1321 int remove_arg_zero(struct linux_binprm *bprm)
1322 {
1323         int ret = 0;
1324         unsigned long offset;
1325         char *kaddr;
1326         struct page *page;
1327
1328         if (!bprm->argc)
1329                 return 0;
1330
1331         do {
1332                 offset = bprm->p & ~PAGE_MASK;
1333                 page = get_arg_page(bprm, bprm->p, 0);
1334                 if (!page) {
1335                         ret = -EFAULT;
1336                         goto out;
1337                 }
1338                 kaddr = kmap_atomic(page, KM_USER0);
1339
1340                 for (; offset < PAGE_SIZE && kaddr[offset];
1341                                 offset++, bprm->p++)
1342                         ;
1343
1344                 kunmap_atomic(kaddr, KM_USER0);
1345                 put_arg_page(page);
1346
1347                 if (offset == PAGE_SIZE)
1348                         free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
1349         } while (offset == PAGE_SIZE);
1350
1351         bprm->p++;
1352         bprm->argc--;
1353         ret = 0;
1354
1355 out:
1356         return ret;
1357 }
1358 EXPORT_SYMBOL(remove_arg_zero);
1359
1360 /*
1361  * cycle the list of binary formats handler, until one recognizes the image
1362  */
1363 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1364 {
1365         unsigned int depth = bprm->recursion_depth;
1366         int try,retval;
1367         struct linux_binfmt *fmt;
1368         pid_t old_pid;
1369
1370         retval = security_bprm_check(bprm);
1371         if (retval)
1372                 return retval;
1373
1374         retval = audit_bprm(bprm);
1375         if (retval)
1376                 return retval;
1377
1378         /* Need to fetch pid before load_binary changes it */
1379         rcu_read_lock();
1380         old_pid = task_pid_nr_ns(current, task_active_pid_ns(current->parent));
1381         rcu_read_unlock();
1382
1383         retval = -ENOENT;
1384         for (try=0; try<2; try++) {
1385                 read_lock(&binfmt_lock);
1386                 list_for_each_entry(fmt, &formats, lh) {
1387                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1388                         if (!fn)
1389                                 continue;
1390                         if (!try_module_get(fmt->module))
1391                                 continue;
1392                         read_unlock(&binfmt_lock);
1393                         retval = fn(bprm, regs);
1394                         /*
1395                          * Restore the depth counter to its starting value
1396                          * in this call, so we don't have to rely on every
1397                          * load_binary function to restore it on return.
1398                          */
1399                         bprm->recursion_depth = depth;
1400                         if (retval >= 0) {
1401                                 if (depth == 0)
1402                                         ptrace_event(PTRACE_EVENT_EXEC,
1403                                                         old_pid);
1404                                 put_binfmt(fmt);
1405                                 allow_write_access(bprm->file);
1406                                 if (bprm->file)
1407                                         fput(bprm->file);
1408                                 bprm->file = NULL;
1409                                 current->did_exec = 1;
1410                                 proc_exec_connector(current);
1411                                 return retval;
1412                         }
1413                         read_lock(&binfmt_lock);
1414                         put_binfmt(fmt);
1415                         if (retval != -ENOEXEC || bprm->mm == NULL)
1416                                 break;
1417                         if (!bprm->file) {
1418                                 read_unlock(&binfmt_lock);
1419                                 return retval;
1420                         }
1421                 }
1422                 read_unlock(&binfmt_lock);
1423 #ifdef CONFIG_MODULES
1424                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1425                         break;
1426                 } else {
1427 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1428                         if (printable(bprm->buf[0]) &&
1429                             printable(bprm->buf[1]) &&
1430                             printable(bprm->buf[2]) &&
1431                             printable(bprm->buf[3]))
1432                                 break; /* -ENOEXEC */
1433                         if (try)
1434                                 break; /* -ENOEXEC */
1435                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1436                 }
1437 #else
1438                 break;
1439 #endif
1440         }
1441         return retval;
1442 }
1443
1444 EXPORT_SYMBOL(search_binary_handler);
1445
1446 /*
1447  * sys_execve() executes a new program.
1448  */
1449 static int do_execve_common(const char *filename,
1450                                 struct user_arg_ptr argv,
1451                                 struct user_arg_ptr envp,
1452                                 struct pt_regs *regs)
1453 {
1454         struct linux_binprm *bprm;
1455         struct file *file;
1456         struct files_struct *displaced;
1457         bool clear_in_exec;
1458         int retval;
1459         const struct cred *cred = current_cred();
1460
1461         /*
1462          * We move the actual failure in case of RLIMIT_NPROC excess from
1463          * set*uid() to execve() because too many poorly written programs
1464          * don't check setuid() return code.  Here we additionally recheck
1465          * whether NPROC limit is still exceeded.
1466          */
1467         if ((current->flags & PF_NPROC_EXCEEDED) &&
1468             atomic_read(&cred->user->processes) > rlimit(RLIMIT_NPROC)) {
1469                 retval = -EAGAIN;
1470                 goto out_ret;
1471         }
1472
1473         /* We're below the limit (still or again), so we don't want to make
1474          * further execve() calls fail. */
1475         current->flags &= ~PF_NPROC_EXCEEDED;
1476
1477         retval = unshare_files(&displaced);
1478         if (retval)
1479                 goto out_ret;
1480
1481         retval = -ENOMEM;
1482         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1483         if (!bprm)
1484                 goto out_files;
1485
1486         retval = prepare_bprm_creds(bprm);
1487         if (retval) {
1488 printk("%s: prepare_bprm_creds\n", __func__);
1489                 goto out_free;
1490 }
1491
1492         retval = check_unsafe_exec(bprm);
1493         if (retval < 0) {
1494 printk("%s: check_unsafe_exec\n", __func__);
1495                 goto out_free;
1496 }
1497         clear_in_exec = retval;
1498         current->in_execve = 1;
1499
1500         file = open_exec(filename);
1501         retval = PTR_ERR(file);
1502         if (IS_ERR(file)) {
1503 //printk("%s: open_exec\n", __func__);
1504                 goto out_unmark;
1505 }
1506
1507         sched_exec();
1508
1509         bprm->file = file;
1510         bprm->filename = filename;
1511         bprm->interp = filename;
1512
1513         retval = bprm_mm_init(bprm);
1514         if (retval) {
1515 printk("%s: bprm_mm_init\n", __func__);
1516                 goto out_file;
1517 }
1518
1519         bprm->argc = count(argv, MAX_ARG_STRINGS);
1520         if ((retval = bprm->argc) < 0) {
1521 printk("%s: count argv\n", __func__);
1522                 goto out;
1523 }
1524
1525         bprm->envc = count(envp, MAX_ARG_STRINGS);
1526         if ((retval = bprm->envc) < 0) {
1527 printk("%s: count envc\n", __func__);
1528                 goto out;
1529 }
1530
1531         retval = prepare_binprm(bprm);
1532         if (retval < 0) {
1533 printk("%s: prepare_binprm\n", __func__);
1534                 goto out;
1535 }
1536
1537         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1538         if (retval < 0) {
1539 printk("%s: copy_string_kernel\n", __func__);
1540                 goto out;
1541 }
1542
1543     if(!current->executing_for_remote) {
1544         bprm->exec = bprm->p;
1545         retval = copy_strings(bprm->envc, envp, bprm);
1546         if (retval < 0) {
1547 printk("%s: copy_strings bprm->envc\n", __func__);
1548             goto out;
1549 }
1550
1551         retval = copy_strings(bprm->argc, argv, bprm);
1552         if (retval < 0) {
1553 printk("%s: copy_strings bprm->argc\n", __func__);
1554             goto out;
1555 }
1556
1557      }
1558
1559         retval = search_binary_handler(bprm,regs);
1560         if (retval < 0) {
1561 printk("%s: search_binary_handler\n", __func__);
1562                 goto out;
1563 }
1564
1565         /* execve succeeded */
1566         current->fs->in_exec = 0;
1567         current->in_execve = 0;
1568         acct_update_integrals(current);
1569         free_bprm(bprm);
1570         if (displaced)
1571                 put_files_struct(displaced);
1572         return retval;
1573
1574 out:
1575         if (bprm->mm) {
1576                 acct_arg_size(bprm, 0);
1577                 mmput(bprm->mm);
1578         }
1579
1580 out_file:
1581         if (bprm->file) {
1582                 allow_write_access(bprm->file);
1583                 fput(bprm->file);
1584         }
1585
1586 out_unmark:
1587         if (clear_in_exec)
1588                 current->fs->in_exec = 0;
1589         current->in_execve = 0;
1590
1591 out_free:
1592         free_bprm(bprm);
1593
1594 out_files:
1595         if (displaced)
1596                 reset_files_struct(displaced);
1597 out_ret:
1598         return retval;
1599 }
1600
1601 int do_execve(const char *filename,
1602         const char __user *const __user *__argv,
1603         const char __user *const __user *__envp,
1604         struct pt_regs *regs)
1605 {
1606         struct user_arg_ptr argv = { .ptr.native = __argv };
1607         struct user_arg_ptr envp = { .ptr.native = __envp };
1608         return do_execve_common(filename, argv, envp, regs);
1609 }
1610
1611 #ifdef CONFIG_COMPAT
1612 int compat_do_execve(char *filename,
1613         compat_uptr_t __user *__argv,
1614         compat_uptr_t __user *__envp,
1615         struct pt_regs *regs)
1616 {
1617         struct user_arg_ptr argv = {
1618                 .is_compat = true,
1619                 .ptr.compat = __argv,
1620         };
1621         struct user_arg_ptr envp = {
1622                 .is_compat = true,
1623                 .ptr.compat = __envp,
1624         };
1625         return do_execve_common(filename, argv, envp, regs);
1626 }
1627 #endif
1628
1629 void set_binfmt(struct linux_binfmt *new)
1630 {
1631         struct mm_struct *mm = current->mm;
1632
1633         if (mm->binfmt)
1634                 module_put(mm->binfmt->module);
1635
1636         mm->binfmt = new;
1637         if (new)
1638                 __module_get(new->module);
1639 }
1640
1641 EXPORT_SYMBOL(set_binfmt);
1642
1643 static int expand_corename(struct core_name *cn)
1644 {
1645         char *old_corename = cn->corename;
1646
1647         cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
1648         cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
1649
1650         if (!cn->corename) {
1651                 kfree(old_corename);
1652                 return -ENOMEM;
1653         }
1654
1655         return 0;
1656 }
1657
1658 static int cn_printf(struct core_name *cn, const char *fmt, ...)
1659 {
1660         char *cur;
1661         int need;
1662         int ret;
1663         va_list arg;
1664
1665         va_start(arg, fmt);
1666         need = vsnprintf(NULL, 0, fmt, arg);
1667         va_end(arg);
1668
1669         if (likely(need < cn->size - cn->used - 1))
1670                 goto out_printf;
1671
1672         ret = expand_corename(cn);
1673         if (ret)
1674                 goto expand_fail;
1675
1676 out_printf:
1677         cur = cn->corename + cn->used;
1678         va_start(arg, fmt);
1679         vsnprintf(cur, need + 1, fmt, arg);
1680         va_end(arg);
1681         cn->used += need;
1682         return 0;
1683
1684 expand_fail:
1685         return ret;
1686 }
1687
1688 static void cn_escape(char *str)
1689 {
1690         for (; *str; str++)
1691                 if (*str == '/')
1692                         *str = '!';
1693 }
1694
1695 static int cn_print_exe_file(struct core_name *cn)
1696 {
1697         struct file *exe_file;
1698         char *pathbuf, *path;
1699         int ret;
1700
1701         exe_file = get_mm_exe_file(current->mm);
1702         if (!exe_file) {
1703                 char *commstart = cn->corename + cn->used;
1704                 ret = cn_printf(cn, "%s (path unknown)", current->comm);
1705                 cn_escape(commstart);
1706                 return ret;
1707         }
1708
1709         pathbuf = kmalloc(PATH_MAX, GFP_TEMPORARY);
1710         if (!pathbuf) {
1711                 ret = -ENOMEM;
1712                 goto put_exe_file;
1713         }
1714
1715         path = d_path(&exe_file->f_path, pathbuf, PATH_MAX);
1716         if (IS_ERR(path)) {
1717                 ret = PTR_ERR(path);
1718                 goto free_buf;
1719         }
1720
1721         cn_escape(path);
1722
1723         ret = cn_printf(cn, "%s", path);
1724
1725 free_buf:
1726         kfree(pathbuf);
1727 put_exe_file:
1728         fput(exe_file);
1729         return ret;
1730 }
1731
1732 /* format_corename will inspect the pattern parameter, and output a
1733  * name into corename, which must have space for at least
1734  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1735  */
1736 static int format_corename(struct core_name *cn, long signr)
1737 {
1738         const struct cred *cred = current_cred();
1739         const char *pat_ptr = core_pattern;
1740         int ispipe = (*pat_ptr == '|');
1741         int pid_in_pattern = 0;
1742         int err = 0;
1743
1744         cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
1745         cn->corename = kmalloc(cn->size, GFP_KERNEL);
1746         cn->used = 0;
1747
1748         if (!cn->corename)
1749                 return -ENOMEM;
1750
1751         /* Repeat as long as we have more pattern to process and more output
1752            space */
1753         while (*pat_ptr) {
1754                 if (*pat_ptr != '%') {
1755                         if (*pat_ptr == 0)
1756                                 goto out;
1757                         err = cn_printf(cn, "%c", *pat_ptr++);
1758                 } else {
1759                         switch (*++pat_ptr) {
1760                         /* single % at the end, drop that */
1761                         case 0:
1762                                 goto out;
1763                         /* Double percent, output one percent */
1764                         case '%':
1765                                 err = cn_printf(cn, "%c", '%');
1766                                 break;
1767                         /* pid */
1768                         case 'p':
1769                                 pid_in_pattern = 1;
1770                                 err = cn_printf(cn, "%d",
1771                                               task_tgid_vnr(current));
1772                                 break;
1773                         /* uid */
1774                         case 'u':
1775                                 err = cn_printf(cn, "%d", cred->uid);
1776                                 break;
1777                         /* gid */
1778                         case 'g':
1779                                 err = cn_printf(cn, "%d", cred->gid);
1780                                 break;
1781                         /* signal that caused the coredump */
1782                         case 's':
1783                                 err = cn_printf(cn, "%ld", signr);
1784                                 break;
1785                         /* UNIX time of coredump */
1786                         case 't': {
1787                                 struct timeval tv;
1788                                 do_gettimeofday(&tv);
1789                                 err = cn_printf(cn, "%lu", tv.tv_sec);
1790                                 break;
1791                         }
1792                         /* hostname */
1793                         case 'h': {
1794                                 char *namestart = cn->corename + cn->used;
1795                                 down_read(&uts_sem);
1796                                 err = cn_printf(cn, "%s",
1797                                               utsname()->nodename);
1798                                 up_read(&uts_sem);
1799                                 cn_escape(namestart);
1800                                 break;
1801                         }
1802                         /* executable */
1803                         case 'e': {
1804                                 char *commstart = cn->corename + cn->used;
1805                                 err = cn_printf(cn, "%s", current->comm);
1806                                 cn_escape(commstart);
1807                                 break;
1808                         }
1809                         case 'E':
1810                                 err = cn_print_exe_file(cn);
1811                                 break;
1812                         /* core limit size */
1813                         case 'c':
1814                                 err = cn_printf(cn, "%lu",
1815                                               rlimit(RLIMIT_CORE));
1816                                 break;
1817                         default:
1818                                 break;
1819                         }
1820                         ++pat_ptr;
1821                 }
1822
1823                 if (err)
1824                         return err;
1825         }
1826
1827         /* Backward compatibility with core_uses_pid:
1828          *
1829          * If core_pattern does not include a %p (as is the default)
1830          * and core_uses_pid is set, then .%pid will be appended to
1831          * the filename. Do not do this for piped commands. */
1832         if (!ispipe && !pid_in_pattern && core_uses_pid) {
1833                 err = cn_printf(cn, ".%d", task_tgid_vnr(current));
1834                 if (err)
1835                         return err;
1836         }
1837 out:
1838         return ispipe;
1839 }
1840
1841 static int zap_process(struct task_struct *start, int exit_code)
1842 {
1843         struct task_struct *t;
1844         int nr = 0;
1845
1846         start->signal->flags = SIGNAL_GROUP_EXIT;
1847         start->signal->group_exit_code = exit_code;
1848         start->signal->group_stop_count = 0;
1849
1850         t = start;
1851         do {
1852                 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1853                 if (t != current && t->mm) {
1854                         sigaddset(&t->pending.signal, SIGKILL);
1855                         signal_wake_up(t, 1);
1856                         nr++;
1857                 }
1858         } while_each_thread(start, t);
1859
1860         return nr;
1861 }
1862
1863 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1864                                 struct core_state *core_state, int exit_code)
1865 {
1866         struct task_struct *g, *p;
1867         unsigned long flags;
1868         int nr = -EAGAIN;
1869
1870         spin_lock_irq(&tsk->sighand->siglock);
1871         if (!signal_group_exit(tsk->signal)) {
1872                 mm->core_state = core_state;
1873                 nr = zap_process(tsk, exit_code);
1874         }
1875         spin_unlock_irq(&tsk->sighand->siglock);
1876         if (unlikely(nr < 0))
1877                 return nr;
1878
1879         if (atomic_read(&mm->mm_users) == nr + 1)
1880                 goto done;
1881         /*
1882          * We should find and kill all tasks which use this mm, and we should
1883          * count them correctly into ->nr_threads. We don't take tasklist
1884          * lock, but this is safe wrt:
1885          *
1886          * fork:
1887          *      None of sub-threads can fork after zap_process(leader). All
1888          *      processes which were created before this point should be
1889          *      visible to zap_threads() because copy_process() adds the new
1890          *      process to the tail of init_task.tasks list, and lock/unlock
1891          *      of ->siglock provides a memory barrier.
1892          *
1893          * do_exit:
1894          *      The caller holds mm->mmap_sem. This means that the task which
1895          *      uses this mm can't pass exit_mm(), so it can't exit or clear
1896          *      its ->mm.
1897          *
1898          * de_thread:
1899          *      It does list_replace_rcu(&leader->tasks, &current->tasks),
1900          *      we must see either old or new leader, this does not matter.
1901          *      However, it can change p->sighand, so lock_task_sighand(p)
1902          *      must be used. Since p->mm != NULL and we hold ->mmap_sem
1903          *      it can't fail.
1904          *
1905          *      Note also that "g" can be the old leader with ->mm == NULL
1906          *      and already unhashed and thus removed from ->thread_group.
1907          *      This is OK, __unhash_process()->list_del_rcu() does not
1908          *      clear the ->next pointer, we will find the new leader via
1909          *      next_thread().
1910          */
1911         rcu_read_lock();
1912         for_each_process(g) {
1913                 if (g == tsk->group_leader)
1914                         continue;
1915                 if (g->flags & PF_KTHREAD)
1916                         continue;
1917                 p = g;
1918                 do {
1919                         if (p->mm) {
1920                                 if (unlikely(p->mm == mm)) {
1921                                         lock_task_sighand(p, &flags);
1922                                         nr += zap_process(p, exit_code);
1923                                         unlock_task_sighand(p, &flags);
1924                                 }
1925                                 break;
1926                         }
1927                 } while_each_thread(g, p);
1928         }
1929         rcu_read_unlock();
1930 done:
1931         atomic_set(&core_state->nr_threads, nr);
1932         return nr;
1933 }
1934
1935 static int coredump_wait(int exit_code, struct core_state *core_state)
1936 {
1937         struct task_struct *tsk = current;
1938         struct mm_struct *mm = tsk->mm;
1939         struct completion *vfork_done;
1940         int core_waiters = -EBUSY;
1941
1942         init_completion(&core_state->startup);
1943         core_state->dumper.task = tsk;
1944         core_state->dumper.next = NULL;
1945
1946         down_write(&mm->mmap_sem);
1947         if (!mm->core_state)
1948                 core_waiters = zap_threads(tsk, mm, core_state, exit_code);
1949         up_write(&mm->mmap_sem);
1950
1951         if (unlikely(core_waiters < 0))
1952                 goto fail;
1953
1954         /*
1955          * Make sure nobody is waiting for us to release the VM,
1956          * otherwise we can deadlock when we wait on each other
1957          */
1958         vfork_done = tsk->vfork_done;
1959         if (vfork_done) {
1960                 tsk->vfork_done = NULL;
1961                 complete(vfork_done);
1962         }
1963
1964         if (core_waiters)
1965                 wait_for_completion(&core_state->startup);
1966 fail:
1967         return core_waiters;
1968 }
1969
1970 static void coredump_finish(struct mm_struct *mm)
1971 {
1972         struct core_thread *curr, *next;
1973         struct task_struct *task;
1974
1975         next = mm->core_state->dumper.next;
1976         while ((curr = next) != NULL) {
1977                 next = curr->next;
1978                 task = curr->task;
1979                 /*
1980                  * see exit_mm(), curr->task must not see
1981                  * ->task == NULL before we read ->next.
1982                  */
1983                 smp_mb();
1984                 curr->task = NULL;
1985                 wake_up_process(task);
1986         }
1987
1988         mm->core_state = NULL;
1989 }
1990
1991 /*
1992  * set_dumpable converts traditional three-value dumpable to two flags and
1993  * stores them into mm->flags.  It modifies lower two bits of mm->flags, but
1994  * these bits are not changed atomically.  So get_dumpable can observe the
1995  * intermediate state.  To avoid doing unexpected behavior, get get_dumpable
1996  * return either old dumpable or new one by paying attention to the order of
1997  * modifying the bits.
1998  *
1999  * dumpable |   mm->flags (binary)
2000  * old  new | initial interim  final
2001  * ---------+-----------------------
2002  *  0    1  |   00      01      01
2003  *  0    2  |   00      10(*)   11
2004  *  1    0  |   01      00      00
2005  *  1    2  |   01      11      11
2006  *  2    0  |   11      10(*)   00
2007  *  2    1  |   11      11      01
2008  *
2009  * (*) get_dumpable regards interim value of 10 as 11.
2010  */
2011 void set_dumpable(struct mm_struct *mm, int value)
2012 {
2013         switch (value) {
2014         case 0:
2015                 clear_bit(MMF_DUMPABLE, &mm->flags);
2016                 smp_wmb();
2017                 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
2018                 break;
2019         case 1:
2020                 set_bit(MMF_DUMPABLE, &mm->flags);
2021                 smp_wmb();
2022                 clear_bit(MMF_DUMP_SECURELY, &mm->flags);
2023                 break;
2024         case 2:
2025                 set_bit(MMF_DUMP_SECURELY, &mm->flags);
2026                 smp_wmb();
2027                 set_bit(MMF_DUMPABLE, &mm->flags);
2028                 break;
2029         }
2030 }
2031
2032 static int __get_dumpable(unsigned long mm_flags)
2033 {
2034         int ret;
2035
2036         ret = mm_flags & MMF_DUMPABLE_MASK;
2037         return (ret >= 2) ? 2 : ret;
2038 }
2039
2040 int get_dumpable(struct mm_struct *mm)
2041 {
2042         return __get_dumpable(mm->flags);
2043 }
2044
2045 static void wait_for_dump_helpers(struct file *file)
2046 {
2047         struct pipe_inode_info *pipe;
2048
2049         pipe = file->f_path.dentry->d_inode->i_pipe;
2050
2051         pipe_lock(pipe);
2052         pipe->readers++;
2053         pipe->writers--;
2054
2055         while ((pipe->readers > 1) && (!signal_pending(current))) {
2056                 wake_up_interruptible_sync(&pipe->wait);
2057                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
2058                 pipe_wait(pipe);
2059         }
2060
2061         pipe->readers--;
2062         pipe->writers++;
2063         pipe_unlock(pipe);
2064
2065 }
2066
2067
2068 /*
2069  * umh_pipe_setup
2070  * helper function to customize the process used
2071  * to collect the core in userspace.  Specifically
2072  * it sets up a pipe and installs it as fd 0 (stdin)
2073  * for the process.  Returns 0 on success, or
2074  * PTR_ERR on failure.
2075  * Note that it also sets the core limit to 1.  This
2076  * is a special value that we use to trap recursive
2077  * core dumps
2078  */
2079 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
2080 {
2081         struct file *rp, *wp;
2082         struct fdtable *fdt;
2083         struct coredump_params *cp = (struct coredump_params *)info->data;
2084         struct files_struct *cf = current->files;
2085
2086         wp = create_write_pipe(0);
2087         if (IS_ERR(wp))
2088                 return PTR_ERR(wp);
2089
2090         rp = create_read_pipe(wp, 0);
2091         if (IS_ERR(rp)) {
2092                 free_write_pipe(wp);
2093                 return PTR_ERR(rp);
2094         }
2095
2096         cp->file = wp;
2097
2098         sys_close(0);
2099         fd_install(0, rp);
2100         spin_lock(&cf->file_lock);
2101         fdt = files_fdtable(cf);
2102         FD_SET(0, fdt->open_fds);
2103         FD_CLR(0, fdt->close_on_exec);
2104         spin_unlock(&cf->file_lock);
2105
2106         /* and disallow core files too */
2107         current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
2108
2109         return 0;
2110 }
2111
2112 void do_coredump(long signr, int exit_code, struct pt_regs *regs)
2113 {
2114         struct core_state core_state;
2115         struct core_name cn;
2116         struct mm_struct *mm = current->mm;
2117         struct linux_binfmt * binfmt;
2118         const struct cred *old_cred;
2119         struct cred *cred;
2120         int retval = 0;
2121         int flag = 0;
2122         int ispipe;
2123         static atomic_t core_dump_count = ATOMIC_INIT(0);
2124         struct coredump_params cprm = {
2125                 .signr = signr,
2126                 .regs = regs,
2127                 .limit = rlimit(RLIMIT_CORE),
2128                 /*
2129                  * We must use the same mm->flags while dumping core to avoid
2130                  * inconsistency of bit flags, since this flag is not protected
2131                  * by any locks.
2132                  */
2133                 .mm_flags = mm->flags,
2134         };
2135
2136 dump_stack();
2137
2138         audit_core_dumps(signr);
2139
2140         binfmt = mm->binfmt;
2141         if (!binfmt || !binfmt->core_dump)
2142                 goto fail;
2143         if (!__get_dumpable(cprm.mm_flags))
2144                 goto fail;
2145
2146         cred = prepare_creds();
2147         if (!cred)
2148                 goto fail;
2149         /*
2150          *      We cannot trust fsuid as being the "true" uid of the
2151          *      process nor do we know its entire history. We only know it
2152          *      was tainted so we dump it as root in mode 2.
2153          */
2154         if (__get_dumpable(cprm.mm_flags) == 2) {
2155                 /* Setuid core dump mode */
2156                 flag = O_EXCL;          /* Stop rewrite attacks */
2157                 cred->fsuid = 0;        /* Dump root private */
2158         }
2159
2160         retval = coredump_wait(exit_code, &core_state);
2161         if (retval < 0)
2162                 goto fail_creds;
2163
2164         old_cred = override_creds(cred);
2165
2166         /*
2167          * Clear any false indication of pending signals that might
2168          * be seen by the filesystem code called to write the core file.
2169          */
2170         clear_thread_flag(TIF_SIGPENDING);
2171
2172         ispipe = format_corename(&cn, signr);
2173
2174         if (ispipe) {
2175                 int dump_count;
2176                 char **helper_argv;
2177
2178                 if (ispipe < 0) {
2179                         printk(KERN_WARNING "format_corename failed\n");
2180                         printk(KERN_WARNING "Aborting core\n");
2181                         goto fail_corename;
2182                 }
2183
2184                 if (cprm.limit == 1) {
2185                         /*
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.
2198                          */
2199                         printk(KERN_WARNING
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");
2203                         goto fail_unlock;
2204                 }
2205                 cprm.limit = RLIM_INFINITY;
2206
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;
2213                 }
2214
2215                 helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
2216                 if (!helper_argv) {
2217                         printk(KERN_WARNING "%s failed to allocate memory\n",
2218                                __func__);
2219                         goto fail_dropcount;
2220                 }
2221
2222                 retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
2223                                         NULL, UMH_WAIT_EXEC, umh_pipe_setup,
2224                                         NULL, &cprm);
2225                 argv_free(helper_argv);
2226                 if (retval) {
2227                         printk(KERN_INFO "Core dump to %s pipe failed\n",
2228                                cn.corename);
2229                         goto close_fail;
2230                 }
2231         } else {
2232                 struct inode *inode;
2233
2234                 if (cprm.limit < binfmt->min_coredump)
2235                         goto fail_unlock;
2236
2237                 cprm.file = filp_open(cn.corename,
2238                                  O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
2239                                  0600);
2240                 if (IS_ERR(cprm.file))
2241                         goto fail_unlock;
2242
2243                 inode = cprm.file->f_path.dentry->d_inode;
2244                 if (inode->i_nlink > 1)
2245                         goto close_fail;
2246                 if (d_unhashed(cprm.file->f_path.dentry))
2247                         goto close_fail;
2248                 /*
2249                  * AK: actually i see no reason to not allow this for named
2250                  * pipes etc, but keep the previous behaviour for now.
2251                  */
2252                 if (!S_ISREG(inode->i_mode))
2253                         goto close_fail;
2254                 /*
2255                  * Dont allow local users get cute and trick others to coredump
2256                  * into their pre-created files.
2257                  */
2258                 if (inode->i_uid != current_fsuid())
2259                         goto close_fail;
2260                 if (!cprm.file->f_op || !cprm.file->f_op->write)
2261                         goto close_fail;
2262                 if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
2263                         goto close_fail;
2264         }
2265
2266         retval = binfmt->core_dump(&cprm);
2267         if (retval)
2268                 current->signal->group_exit_code |= 0x80;
2269
2270         if (ispipe && core_pipe_limit)
2271                 wait_for_dump_helpers(cprm.file);
2272 close_fail:
2273         if (cprm.file)
2274                 filp_close(cprm.file, NULL);
2275 fail_dropcount:
2276         if (ispipe)
2277                 atomic_dec(&core_dump_count);
2278 fail_unlock:
2279         kfree(cn.corename);
2280 fail_corename:
2281         coredump_finish(mm);
2282         revert_creds(old_cred);
2283 fail_creds:
2284         put_cred(cred);
2285 fail:
2286         return;
2287 }
2288
2289 /*
2290  * Core dumping helper functions.  These are the only things you should
2291  * do on a core-file: use only these functions to write out all the
2292  * necessary info.
2293  */
2294 int dump_write(struct file *file, const void *addr, int nr)
2295 {
2296         return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
2297 }
2298 EXPORT_SYMBOL(dump_write);
2299
2300 int dump_seek(struct file *file, loff_t off)
2301 {
2302         int ret = 1;
2303
2304         if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
2305                 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
2306                         return 0;
2307         } else {
2308                 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
2309
2310                 if (!buf)
2311                         return 0;
2312                 while (off > 0) {
2313                         unsigned long n = off;
2314
2315                         if (n > PAGE_SIZE)
2316                                 n = PAGE_SIZE;
2317                         if (!dump_write(file, buf, n)) {
2318                                 ret = 0;
2319                                 break;
2320                         }
2321                         off -= n;
2322                 }
2323                 free_page((unsigned long)buf);
2324         }
2325         return ret;
2326 }
2327 EXPORT_SYMBOL(dump_seek);
2328