upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.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/config.h>
26 #include <linux/slab.h>
27 #include <linux/file.h>
28 #include <linux/mman.h>
29 #include <linux/a.out.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/highmem.h>
36 #include <linux/spinlock.h>
37 #include <linux/key.h>
38 #include <linux/personality.h>
39 #include <linux/binfmts.h>
40 #include <linux/swap.h>
41 #include <linux/utsname.h>
42 #include <linux/module.h>
43 #include <linux/namei.h>
44 #include <linux/proc_fs.h>
45 #include <linux/ptrace.h>
46 #include <linux/mount.h>
47 #include <linux/security.h>
48 #include <linux/syscalls.h>
49 #include <linux/rmap.h>
50 #include <linux/ckrm.h>
51 #include <linux/vs_memory.h>
52 #include <linux/ckrm_mem.h>
53
54 #include <asm/uaccess.h>
55 #include <asm/mmu_context.h>
56
57 #ifdef CONFIG_KMOD
58 #include <linux/kmod.h>
59 #endif
60
61 int core_uses_pid;
62 char core_pattern[65] = "core";
63 int suid_dumpable = 0;
64
65 EXPORT_SYMBOL(suid_dumpable);
66 /* The maximal length of core_pattern is also specified in sysctl.c */
67
68 static struct linux_binfmt *formats;
69 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
70
71 int register_binfmt(struct linux_binfmt * fmt)
72 {
73         struct linux_binfmt ** tmp = &formats;
74
75         if (!fmt)
76                 return -EINVAL;
77         if (fmt->next)
78                 return -EBUSY;
79         write_lock(&binfmt_lock);
80         while (*tmp) {
81                 if (fmt == *tmp) {
82                         write_unlock(&binfmt_lock);
83                         return -EBUSY;
84                 }
85                 tmp = &(*tmp)->next;
86         }
87         fmt->next = formats;
88         formats = fmt;
89         write_unlock(&binfmt_lock);
90         return 0;       
91 }
92
93 EXPORT_SYMBOL(register_binfmt);
94
95 int unregister_binfmt(struct linux_binfmt * fmt)
96 {
97         struct linux_binfmt ** tmp = &formats;
98
99         write_lock(&binfmt_lock);
100         while (*tmp) {
101                 if (fmt == *tmp) {
102                         *tmp = fmt->next;
103                         write_unlock(&binfmt_lock);
104                         return 0;
105                 }
106                 tmp = &(*tmp)->next;
107         }
108         write_unlock(&binfmt_lock);
109         return -EINVAL;
110 }
111
112 EXPORT_SYMBOL(unregister_binfmt);
113
114 static inline void put_binfmt(struct linux_binfmt * fmt)
115 {
116         module_put(fmt->module);
117 }
118
119 /*
120  * Note that a shared library must be both readable and executable due to
121  * security reasons.
122  *
123  * Also note that we take the address to load from from the file itself.
124  */
125 asmlinkage long sys_uselib(const char __user * library)
126 {
127         struct file * file;
128         struct nameidata nd;
129         int error;
130
131         nd.intent.open.flags = FMODE_READ;
132         error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
133         if (error)
134                 goto out;
135
136         error = -EINVAL;
137         if (!S_ISREG(nd.dentry->d_inode->i_mode))
138                 goto exit;
139
140         error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
141         if (error)
142                 goto exit;
143
144         file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
145         error = PTR_ERR(file);
146         if (IS_ERR(file))
147                 goto out;
148
149         error = -ENOEXEC;
150         if(file->f_op) {
151                 struct linux_binfmt * fmt;
152
153                 read_lock(&binfmt_lock);
154                 for (fmt = formats ; fmt ; fmt = fmt->next) {
155                         if (!fmt->load_shlib)
156                                 continue;
157                         if (!try_module_get(fmt->module))
158                                 continue;
159                         read_unlock(&binfmt_lock);
160                         error = fmt->load_shlib(file);
161                         read_lock(&binfmt_lock);
162                         put_binfmt(fmt);
163                         if (error != -ENOEXEC)
164                                 break;
165                 }
166                 read_unlock(&binfmt_lock);
167         }
168         fput(file);
169 out:
170         return error;
171 exit:
172         path_release(&nd);
173         goto out;
174 }
175
176 /*
177  * count() counts the number of strings in array ARGV.
178  */
179 static int count(char __user * __user * argv, int max)
180 {
181         int i = 0;
182
183         if (argv != NULL) {
184                 for (;;) {
185                         char __user * p;
186
187                         if (get_user(p, argv))
188                                 return -EFAULT;
189                         if (!p)
190                                 break;
191                         argv++;
192                         if(++i > max)
193                                 return -E2BIG;
194                 }
195         }
196         return i;
197 }
198
199 /*
200  * 'copy_strings()' copies argument/environment strings from user
201  * memory to free pages in kernel mem. These are in a format ready
202  * to be put directly into the top of new user memory.
203  */
204 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
205 {
206         struct page *kmapped_page = NULL;
207         char *kaddr = NULL;
208         int ret;
209
210         while (argc-- > 0) {
211                 char __user *str;
212                 int len;
213                 unsigned long pos;
214
215                 if (get_user(str, argv+argc) ||
216                                 !(len = strnlen_user(str, bprm->p))) {
217                         ret = -EFAULT;
218                         goto out;
219                 }
220
221                 if (bprm->p < len)  {
222                         ret = -E2BIG;
223                         goto out;
224                 }
225
226                 bprm->p -= len;
227                 /* XXX: add architecture specific overflow check here. */
228                 pos = bprm->p;
229
230                 while (len > 0) {
231                         int i, new, err;
232                         int offset, bytes_to_copy;
233                         struct page *page;
234
235                         offset = pos % PAGE_SIZE;
236                         i = pos/PAGE_SIZE;
237                         page = bprm->page[i];
238                         new = 0;
239                         if (!page) {
240                                 page = alloc_page(GFP_HIGHUSER);
241                                 bprm->page[i] = page;
242                                 if (!page) {
243                                         ret = -ENOMEM;
244                                         goto out;
245                                 }
246                                 new = 1;
247                         }
248
249                         if (page != kmapped_page) {
250                                 if (kmapped_page)
251                                         kunmap(kmapped_page);
252                                 kmapped_page = page;
253                                 kaddr = kmap(kmapped_page);
254                         }
255                         if (new && offset)
256                                 memset(kaddr, 0, offset);
257                         bytes_to_copy = PAGE_SIZE - offset;
258                         if (bytes_to_copy > len) {
259                                 bytes_to_copy = len;
260                                 if (new)
261                                         memset(kaddr+offset+len, 0,
262                                                 PAGE_SIZE-offset-len);
263                         }
264                         err = copy_from_user(kaddr+offset, str, bytes_to_copy);
265                         if (err) {
266                                 ret = -EFAULT;
267                                 goto out;
268                         }
269
270                         pos += bytes_to_copy;
271                         str += bytes_to_copy;
272                         len -= bytes_to_copy;
273                 }
274         }
275         ret = 0;
276 out:
277         if (kmapped_page)
278                 kunmap(kmapped_page);
279         return ret;
280 }
281
282 /*
283  * Like copy_strings, but get argv and its values from kernel memory.
284  */
285 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
286 {
287         int r;
288         mm_segment_t oldfs = get_fs();
289         set_fs(KERNEL_DS);
290         r = copy_strings(argc, (char __user * __user *)argv, bprm);
291         set_fs(oldfs);
292         return r;
293 }
294
295 EXPORT_SYMBOL(copy_strings_kernel);
296
297 #ifdef CONFIG_MMU
298 /*
299  * This routine is used to map in a page into an address space: needed by
300  * execve() for the initial stack and environment pages.
301  *
302  * vma->vm_mm->mmap_sem is held for writing.
303  */
304 void install_arg_page(struct vm_area_struct *vma,
305                         struct page *page, unsigned long address)
306 {
307         struct mm_struct *mm = vma->vm_mm;
308         pgd_t * pgd;
309         pmd_t * pmd;
310         pte_t * pte;
311
312         if (unlikely(anon_vma_prepare(vma)))
313                 goto out_sig;
314
315         flush_dcache_page(page);
316         pgd = pgd_offset(mm, address);
317
318         spin_lock(&mm->page_table_lock);
319         pmd = pmd_alloc(mm, pgd, address);
320         if (!pmd)
321                 goto out;
322         pte = pte_alloc_map(mm, pmd, address);
323         if (!pte)
324                 goto out;
325         if (!pte_none(*pte)) {
326                 pte_unmap(pte);
327                 goto out;
328         }
329         // mm->rss++;
330         vx_rsspages_inc(mm);
331         lru_cache_add_active(page);
332         set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
333                                         page, vma->vm_page_prot))));
334         page_add_anon_rmap(page, vma, address);
335         pte_unmap(pte);
336         spin_unlock(&mm->page_table_lock);
337
338         /* no need for flush_tlb */
339         return;
340 out:
341         spin_unlock(&mm->page_table_lock);
342 out_sig:
343         __free_page(page);
344         force_sig(SIGKILL, current);
345 }
346
347 #define EXTRA_STACK_VM_PAGES    20      /* random */
348
349 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
350 {
351         unsigned long stack_base;
352         struct vm_area_struct *mpnt;
353         struct mm_struct *mm = current->mm;
354         int i, ret;
355         long arg_size;
356
357 #ifdef CONFIG_STACK_GROWSUP
358         /* Move the argument and environment strings to the bottom of the
359          * stack space.
360          */
361         int offset, j;
362         char *to, *from;
363
364         /* Start by shifting all the pages down */
365         i = 0;
366         for (j = 0; j < MAX_ARG_PAGES; j++) {
367                 struct page *page = bprm->page[j];
368                 if (!page)
369                         continue;
370                 bprm->page[i++] = page;
371         }
372
373         /* Now move them within their pages */
374         offset = bprm->p % PAGE_SIZE;
375         to = kmap(bprm->page[0]);
376         for (j = 1; j < i; j++) {
377                 memmove(to, to + offset, PAGE_SIZE - offset);
378                 from = kmap(bprm->page[j]);
379                 memcpy(to + PAGE_SIZE - offset, from, offset);
380                 kunmap(bprm->page[j - 1]);
381                 to = from;
382         }
383         memmove(to, to + offset, PAGE_SIZE - offset);
384         kunmap(bprm->page[j - 1]);
385
386         /* Limit stack size to 1GB */
387         stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
388         if (stack_base > (1 << 30))
389                 stack_base = 1 << 30;
390         stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
391
392         /* Adjust bprm->p to point to the end of the strings. */
393         bprm->p = stack_base + PAGE_SIZE * i - offset;
394
395         mm->arg_start = stack_base;
396         arg_size = i << PAGE_SHIFT;
397
398         /* zero pages that were copied above */
399         while (i < MAX_ARG_PAGES)
400                 bprm->page[i++] = NULL;
401 #else
402 #ifdef __HAVE_ARCH_ALIGN_STACK
403         stack_base = arch_align_stack(STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE);
404         stack_base = PAGE_ALIGN(stack_base);
405 #else
406         stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
407 #endif
408         bprm->p += stack_base;
409         mm->arg_start = bprm->p;
410         arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
411 #endif
412
413         arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
414
415         if (bprm->loader)
416                 bprm->loader += stack_base;
417         bprm->exec += stack_base;
418
419         mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
420         if (!mpnt)
421                 return -ENOMEM;
422
423         if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
424                 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
425                 kmem_cache_free(vm_area_cachep, mpnt);
426                 return -ENOMEM;
427         }
428
429         memset(mpnt, 0, sizeof(*mpnt));
430
431         down_write(&mm->mmap_sem);
432         {
433                 mpnt->vm_mm = mm;
434 #ifdef CONFIG_STACK_GROWSUP
435                 mpnt->vm_start = stack_base;
436                 mpnt->vm_end = stack_base + arg_size;
437 #else
438                 mpnt->vm_end = STACK_TOP;
439                 mpnt->vm_start = mpnt->vm_end - arg_size;
440 #endif
441                 /* Adjust stack execute permissions; explicitly enable
442                  * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
443                  * and leave alone (arch default) otherwise. */
444                 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
445                         mpnt->vm_flags = VM_STACK_FLAGS |  VM_EXEC;
446                 else if (executable_stack == EXSTACK_DISABLE_X)
447                         mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
448                 else
449                         mpnt->vm_flags = VM_STACK_FLAGS;
450                 mpnt->vm_flags |= mm->def_flags;
451                 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
452                 if ((ret = insert_vm_struct(mm, mpnt))) {
453                         up_write(&mm->mmap_sem);
454                         kmem_cache_free(vm_area_cachep, mpnt);
455                         return ret;
456                 }
457                 // mm->stack_vm = mm->total_vm = vma_pages(mpnt);
458                 vx_vmpages_sub(mm, mm->total_vm - vma_pages(mpnt));
459                 mm->stack_vm = mm->total_vm;
460         }
461
462         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
463                 struct page *page = bprm->page[i];
464                 if (page) {
465                         bprm->page[i] = NULL;
466                         install_arg_page(mpnt, page, stack_base);
467                 }
468                 stack_base += PAGE_SIZE;
469         }
470         up_write(&mm->mmap_sem);
471         
472         return 0;
473 }
474
475 EXPORT_SYMBOL(setup_arg_pages);
476
477 #define free_arg_pages(bprm) do { } while (0)
478
479 #else
480
481 static inline void free_arg_pages(struct linux_binprm *bprm)
482 {
483         int i;
484
485         for (i = 0; i < MAX_ARG_PAGES; i++) {
486                 if (bprm->page[i])
487                         __free_page(bprm->page[i]);
488                 bprm->page[i] = NULL;
489         }
490 }
491
492 #endif /* CONFIG_MMU */
493
494 struct file *open_exec(const char *name)
495 {
496         struct nameidata nd;
497         int err;
498         struct file *file;
499
500         nd.intent.open.flags = FMODE_READ;
501         err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
502         file = ERR_PTR(err);
503
504         if (!err) {
505                 struct inode *inode = nd.dentry->d_inode;
506                 file = ERR_PTR(-EACCES);
507                 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
508                     S_ISREG(inode->i_mode)) {
509                         int err = permission(inode, MAY_EXEC, &nd);
510                         if (!err && !(inode->i_mode & 0111))
511                                 err = -EACCES;
512                         file = ERR_PTR(err);
513                         if (!err) {
514                                 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
515                                 if (!IS_ERR(file)) {
516                                         err = deny_write_access(file);
517                                         if (err) {
518                                                 fput(file);
519                                                 file = ERR_PTR(err);
520                                         }
521                                 }
522 out:
523                                 return file;
524                         }
525                 }
526                 path_release(&nd);
527         }
528         goto out;
529 }
530
531 EXPORT_SYMBOL(open_exec);
532
533 int kernel_read(struct file *file, unsigned long offset,
534         char *addr, unsigned long count)
535 {
536         mm_segment_t old_fs;
537         loff_t pos = offset;
538         int result;
539
540         old_fs = get_fs();
541         set_fs(get_ds());
542         /* The cast to a user pointer is valid due to the set_fs() */
543         result = vfs_read(file, (void __user *)addr, count, &pos);
544         set_fs(old_fs);
545         return result;
546 }
547
548 EXPORT_SYMBOL(kernel_read);
549
550 static int exec_mmap(struct mm_struct *mm)
551 {
552         struct task_struct *tsk;
553         struct mm_struct * old_mm, *active_mm;
554
555         /* Notify parent that we're no longer interested in the old VM */
556         tsk = current;
557         old_mm = current->mm;
558         mm_release(tsk, old_mm);
559
560         task_lock(tsk);
561         active_mm = tsk->active_mm;
562         tsk->mm = mm;
563         tsk->active_mm = mm;
564         activate_mm(active_mm, mm);
565         task_unlock(tsk);
566         arch_pick_mmap_layout(mm);
567 #ifdef CONFIG_CKRM_RES_MEM
568         if (old_mm) {
569                 spin_lock(&old_mm->peertask_lock);
570                 list_del(&tsk->mm_peers);
571                 ckrm_mem_evaluate_mm(old_mm);
572                 spin_unlock(&old_mm->peertask_lock);
573         }
574         spin_lock(&mm->peertask_lock);
575         list_add_tail(&tsk->mm_peers, &mm->tasklist);
576         ckrm_mem_evaluate_mm(mm);
577         spin_unlock(&mm->peertask_lock);
578 #endif
579         if (old_mm) {
580                 if (active_mm != old_mm) BUG();
581                 mmput(old_mm);
582                 return 0;
583         }
584         mmdrop(active_mm);
585         return 0;
586 }
587
588 /*
589  * This function makes sure the current process has its own signal table,
590  * so that flush_signal_handlers can later reset the handlers without
591  * disturbing other processes.  (Other processes might share the signal
592  * table via the CLONE_SIGHAND option to clone().)
593  */
594 static inline int de_thread(struct task_struct *tsk)
595 {
596         struct signal_struct *sig = tsk->signal;
597         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
598         spinlock_t *lock = &oldsighand->siglock;
599         int count;
600
601         /*
602          * If we don't share sighandlers, then we aren't sharing anything
603          * and we can just re-use it all.
604          */
605         if (atomic_read(&oldsighand->count) <= 1) {
606                 BUG_ON(atomic_read(&sig->count) != 1);
607                 exit_itimers(sig);
608                 return 0;
609         }
610
611         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
612         if (!newsighand)
613                 return -ENOMEM;
614
615         if (thread_group_empty(current))
616                 goto no_thread_group;
617
618         /*
619          * Kill all other threads in the thread group.
620          * We must hold tasklist_lock to call zap_other_threads.
621          */
622         read_lock(&tasklist_lock);
623         spin_lock_irq(lock);
624         if (sig->group_exit) {
625                 /*
626                  * Another group action in progress, just
627                  * return so that the signal is processed.
628                  */
629                 spin_unlock_irq(lock);
630                 read_unlock(&tasklist_lock);
631                 kmem_cache_free(sighand_cachep, newsighand);
632                 return -EAGAIN;
633         }
634         sig->group_exit = 1;
635         zap_other_threads(current);
636         read_unlock(&tasklist_lock);
637
638         /*
639          * Account for the thread group leader hanging around:
640          */
641         count = 2;
642         if (current->pid == current->tgid)
643                 count = 1;
644         while (atomic_read(&sig->count) > count) {
645                 sig->group_exit_task = current;
646                 sig->notify_count = count;
647                 __set_current_state(TASK_UNINTERRUPTIBLE);
648                 spin_unlock_irq(lock);
649                 schedule();
650                 spin_lock_irq(lock);
651         }
652         sig->group_exit_task = NULL;
653         sig->notify_count = 0;
654         spin_unlock_irq(lock);
655
656         /*
657          * At this point all other threads have exited, all we have to
658          * do is to wait for the thread group leader to become inactive,
659          * and to assume its PID:
660          */
661         if (current->pid != current->tgid) {
662                 struct task_struct *leader = current->group_leader, *parent;
663                 struct dentry *proc_dentry1, *proc_dentry2;
664                 unsigned long exit_state, ptrace;
665
666                 /*
667                  * Wait for the thread group leader to be a zombie.
668                  * It should already be zombie at this point, most
669                  * of the time.
670                  */
671                 while (leader->exit_state != EXIT_ZOMBIE)
672                         yield();
673
674                 spin_lock(&leader->proc_lock);
675                 spin_lock(&current->proc_lock);
676                 proc_dentry1 = proc_pid_unhash(current);
677                 proc_dentry2 = proc_pid_unhash(leader);
678                 write_lock_irq(&tasklist_lock);
679
680                 if (leader->tgid != current->tgid)
681                         BUG();
682                 if (current->pid == current->tgid)
683                         BUG();
684                 /*
685                  * An exec() starts a new thread group with the
686                  * TGID of the previous thread group. Rehash the
687                  * two threads with a switched PID, and release
688                  * the former thread group leader:
689                  */
690                 ptrace = leader->ptrace;
691                 parent = leader->parent;
692
693                 ptrace_unlink(current);
694                 ptrace_unlink(leader);
695                 remove_parent(current);
696                 remove_parent(leader);
697
698                 switch_exec_pids(leader, current);
699
700                 current->parent = current->real_parent = leader->real_parent;
701                 leader->parent = leader->real_parent = child_reaper;
702                 current->group_leader = current;
703                 leader->group_leader = leader;
704
705                 add_parent(current, current->parent);
706                 add_parent(leader, leader->parent);
707                 if (ptrace) {
708                         current->ptrace = ptrace;
709                         __ptrace_link(current, parent);
710                 }
711
712                 list_del(&current->tasks);
713                 list_add_tail(&current->tasks, &init_task.tasks);
714                 current->exit_signal = SIGCHLD;
715                 exit_state = leader->exit_state;
716
717                 write_unlock_irq(&tasklist_lock);
718                 spin_unlock(&leader->proc_lock);
719                 spin_unlock(&current->proc_lock);
720                 proc_pid_flush(proc_dentry1);
721                 proc_pid_flush(proc_dentry2);
722
723                 if (exit_state != EXIT_ZOMBIE)
724                         BUG();
725                 release_task(leader);
726         }
727
728         /*
729          * Now there are really no other threads at all,
730          * so it's safe to stop telling them to kill themselves.
731          */
732         sig->group_exit = 0;
733
734 no_thread_group:
735         BUG_ON(atomic_read(&sig->count) != 1);
736         exit_itimers(sig);
737
738         if (atomic_read(&oldsighand->count) == 1) {
739                 /*
740                  * Now that we nuked the rest of the thread group,
741                  * it turns out we are not sharing sighand any more either.
742                  * So we can just keep it.
743                  */
744                 kmem_cache_free(sighand_cachep, newsighand);
745         } else {
746                 /*
747                  * Move our state over to newsighand and switch it in.
748                  */
749                 spin_lock_init(&newsighand->siglock);
750                 atomic_set(&newsighand->count, 1);
751                 memcpy(newsighand->action, oldsighand->action,
752                        sizeof(newsighand->action));
753
754                 write_lock_irq(&tasklist_lock);
755                 spin_lock(&oldsighand->siglock);
756                 spin_lock(&newsighand->siglock);
757
758                 current->sighand = newsighand;
759                 recalc_sigpending();
760
761                 spin_unlock(&newsighand->siglock);
762                 spin_unlock(&oldsighand->siglock);
763                 write_unlock_irq(&tasklist_lock);
764
765                 if (atomic_dec_and_test(&oldsighand->count))
766                         kmem_cache_free(sighand_cachep, oldsighand);
767         }
768
769         if (!thread_group_empty(current))
770                 BUG();
771         if (current->tgid != current->pid)
772                 BUG();
773         return 0;
774 }
775         
776 /*
777  * These functions flushes out all traces of the currently running executable
778  * so that a new one can be started
779  */
780
781 static inline void flush_old_files(struct files_struct * files)
782 {
783         long j = -1;
784
785         spin_lock(&files->file_lock);
786         for (;;) {
787                 unsigned long set, i;
788
789                 j++;
790                 i = j * __NFDBITS;
791                 if (i >= files->max_fds || i >= files->max_fdset)
792                         break;
793                 set = files->close_on_exec->fds_bits[j];
794                 if (!set)
795                         continue;
796                 files->close_on_exec->fds_bits[j] = 0;
797                 spin_unlock(&files->file_lock);
798                 for ( ; set ; i++,set >>= 1) {
799                         if (set & 1) {
800                                 sys_close(i);
801                         }
802                 }
803                 spin_lock(&files->file_lock);
804
805         }
806         spin_unlock(&files->file_lock);
807 }
808
809 void get_task_comm(char *buf, struct task_struct *tsk)
810 {
811         /* buf must be at least sizeof(tsk->comm) in size */
812         task_lock(tsk);
813         memcpy(buf, tsk->comm, sizeof(tsk->comm));
814         task_unlock(tsk);
815 }
816
817 void set_task_comm(struct task_struct *tsk, char *buf)
818 {
819         task_lock(tsk);
820         strlcpy(tsk->comm, buf, sizeof(tsk->comm));
821         task_unlock(tsk);
822 }
823
824 int flush_old_exec(struct linux_binprm * bprm)
825 {
826         char * name;
827         int i, ch, retval;
828         struct files_struct *files;
829         char tcomm[sizeof(current->comm)];
830
831         /*
832          * Make sure we have a private signal table and that
833          * we are unassociated from the previous thread group.
834          */
835         retval = de_thread(current);
836         if (retval)
837                 goto out;
838
839         /*
840          * Make sure we have private file handles. Ask the
841          * fork helper to do the work for us and the exit
842          * helper to do the cleanup of the old one.
843          */
844         files = current->files;         /* refcounted so safe to hold */
845         retval = unshare_files();
846         if (retval)
847                 goto out;
848         /*
849          * Release all of the old mmap stuff
850          */
851         retval = exec_mmap(bprm->mm);
852         if (retval)
853                 goto mmap_failed;
854
855         bprm->mm = NULL;                /* We're using it now */
856
857         /* This is the point of no return */
858         steal_locks(files);
859         put_files_struct(files);
860
861         current->sas_ss_sp = current->sas_ss_size = 0;
862
863         if (current->euid == current->uid && current->egid == current->gid)
864                 current->mm->dumpable = 1;
865         else
866                 current->mm->dumpable = suid_dumpable;
867                 
868         name = bprm->filename;
869         for (i=0; (ch = *(name++)) != '\0';) {
870                 if (ch == '/')
871                         i = 0;
872                 else
873                         if (i < (sizeof(tcomm) - 1))
874                                 tcomm[i++] = ch;
875         }
876         tcomm[i] = '\0';
877         set_task_comm(current, tcomm);
878
879         current->flags &= ~PF_RELOCEXEC;
880         flush_thread();
881
882         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
883             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
884             (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
885                 suid_keys(current);
886                 current->mm->dumpable = suid_dumpable;
887         }
888
889         /* An exec changes our domain. We are no longer part of the thread
890            group */
891
892         current->self_exec_id++;
893                         
894         flush_signal_handlers(current, 0);
895         flush_old_files(current->files);
896
897         return 0;
898
899 mmap_failed:
900         put_files_struct(current->files);
901         current->files = files;
902 out:
903         return retval;
904 }
905
906 EXPORT_SYMBOL(flush_old_exec);
907
908 /* 
909  * Fill the binprm structure from the inode. 
910  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
911  */
912 int prepare_binprm(struct linux_binprm *bprm)
913 {
914         int mode;
915         struct inode * inode = bprm->file->f_dentry->d_inode;
916         int retval;
917
918         mode = inode->i_mode;
919         /*
920          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
921          * generic_permission lets a non-executable through
922          */
923         if (!(mode & 0111))     /* with at least _one_ execute bit set */
924                 return -EACCES;
925         if (bprm->file->f_op == NULL)
926                 return -EACCES;
927
928         bprm->e_uid = current->euid;
929         bprm->e_gid = current->egid;
930
931         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
932                 /* Set-uid? */
933                 if (mode & S_ISUID) {
934                         current->personality &= ~PER_CLEAR_ON_SETID;
935                         bprm->e_uid = inode->i_uid;
936                 }
937
938                 /* Set-gid? */
939                 /*
940                  * If setgid is set but no group execute bit then this
941                  * is a candidate for mandatory locking, not a setgid
942                  * executable.
943                  */
944                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
945                         current->personality &= ~PER_CLEAR_ON_SETID;
946                         bprm->e_gid = inode->i_gid;
947                 }
948         }
949
950         /* fill in binprm security blob */
951         retval = security_bprm_set(bprm);
952         if (retval)
953                 return retval;
954
955         memset(bprm->buf,0,BINPRM_BUF_SIZE);
956         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
957 }
958
959 EXPORT_SYMBOL(prepare_binprm);
960
961 static inline int unsafe_exec(struct task_struct *p)
962 {
963         int unsafe = 0;
964         if (p->ptrace & PT_PTRACED) {
965                 if (p->ptrace & PT_PTRACE_CAP)
966                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
967                 else
968                         unsafe |= LSM_UNSAFE_PTRACE;
969         }
970         if (atomic_read(&p->fs->count) > 1 ||
971             atomic_read(&p->files->count) > 1 ||
972             atomic_read(&p->sighand->count) > 1)
973                 unsafe |= LSM_UNSAFE_SHARE;
974
975         return unsafe;
976 }
977
978 void compute_creds(struct linux_binprm *bprm)
979 {
980         int unsafe;
981
982         if (bprm->e_uid != current->uid)
983                 suid_keys(current);
984         exec_keys(current);
985
986         task_lock(current);
987         unsafe = unsafe_exec(current);
988         security_bprm_apply_creds(bprm, unsafe);
989         task_unlock(current);
990 }
991
992 EXPORT_SYMBOL(compute_creds);
993
994 void remove_arg_zero(struct linux_binprm *bprm)
995 {
996         if (bprm->argc) {
997                 unsigned long offset;
998                 char * kaddr;
999                 struct page *page;
1000
1001                 offset = bprm->p % PAGE_SIZE;
1002                 goto inside;
1003
1004                 while (bprm->p++, *(kaddr+offset++)) {
1005                         if (offset != PAGE_SIZE)
1006                                 continue;
1007                         offset = 0;
1008                         kunmap_atomic(kaddr, KM_USER0);
1009 inside:
1010                         page = bprm->page[bprm->p/PAGE_SIZE];
1011                         kaddr = kmap_atomic(page, KM_USER0);
1012                 }
1013                 kunmap_atomic(kaddr, KM_USER0);
1014                 bprm->argc--;
1015         }
1016 }
1017
1018 EXPORT_SYMBOL(remove_arg_zero);
1019
1020 /*
1021  * cycle the list of binary formats handler, until one recognizes the image
1022  */
1023 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1024 {
1025         int try,retval;
1026         struct linux_binfmt *fmt;
1027 #ifdef __alpha__
1028         /* handle /sbin/loader.. */
1029         {
1030             struct exec * eh = (struct exec *) bprm->buf;
1031
1032             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1033                 (eh->fh.f_flags & 0x3000) == 0x3000)
1034             {
1035                 struct file * file;
1036                 unsigned long loader;
1037
1038                 allow_write_access(bprm->file);
1039                 fput(bprm->file);
1040                 bprm->file = NULL;
1041
1042                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1043
1044                 file = open_exec("/sbin/loader");
1045                 retval = PTR_ERR(file);
1046                 if (IS_ERR(file))
1047                         return retval;
1048
1049                 /* Remember if the application is TASO.  */
1050                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1051
1052                 bprm->file = file;
1053                 bprm->loader = loader;
1054                 retval = prepare_binprm(bprm);
1055                 if (retval<0)
1056                         return retval;
1057                 /* should call search_binary_handler recursively here,
1058                    but it does not matter */
1059             }
1060         }
1061 #endif
1062         retval = security_bprm_check(bprm);
1063         if (retval)
1064                 return retval;
1065
1066         /* kernel module loader fixup */
1067         /* so we don't try to load run modprobe in kernel space. */
1068         set_fs(USER_DS);
1069         retval = -ENOENT;
1070         for (try=0; try<2; try++) {
1071                 read_lock(&binfmt_lock);
1072                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1073                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1074                         if (!fn)
1075                                 continue;
1076                         if (!try_module_get(fmt->module))
1077                                 continue;
1078                         read_unlock(&binfmt_lock);
1079                         retval = fn(bprm, regs);
1080                         if (retval >= 0) {
1081                                 put_binfmt(fmt);
1082                                 allow_write_access(bprm->file);
1083                                 if (bprm->file)
1084                                         fput(bprm->file);
1085                                 bprm->file = NULL;
1086                                 current->did_exec = 1;
1087                                 ckrm_cb_exec(bprm->filename);
1088                                 return retval;
1089                         }
1090                         read_lock(&binfmt_lock);
1091                         put_binfmt(fmt);
1092                         if (retval != -ENOEXEC || bprm->mm == NULL)
1093                                 break;
1094                         if (!bprm->file) {
1095                                 read_unlock(&binfmt_lock);
1096                                 return retval;
1097                         }
1098                 }
1099                 read_unlock(&binfmt_lock);
1100                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1101                         break;
1102 #ifdef CONFIG_KMOD
1103                 }else{
1104 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1105                         if (printable(bprm->buf[0]) &&
1106                             printable(bprm->buf[1]) &&
1107                             printable(bprm->buf[2]) &&
1108                             printable(bprm->buf[3]))
1109                                 break; /* -ENOEXEC */
1110                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1111 #endif
1112                 }
1113         }
1114         return retval;
1115 }
1116
1117 EXPORT_SYMBOL(search_binary_handler);
1118
1119 /*
1120  * sys_execve() executes a new program.
1121  */
1122 int do_execve(char * filename,
1123         char __user *__user *argv,
1124         char __user *__user *envp,
1125         struct pt_regs * regs)
1126 {
1127         struct linux_binprm *bprm;
1128         struct file *file;
1129         int retval;
1130         int i;
1131
1132         retval = -ENOMEM;
1133         bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1134         if (!bprm)
1135                 goto out_ret;
1136         memset(bprm, 0, sizeof(*bprm));
1137
1138         file = open_exec(filename);
1139         retval = PTR_ERR(file);
1140         if (IS_ERR(file))
1141                 goto out_kfree;
1142
1143         sched_exec();
1144
1145         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1146
1147         bprm->file = file;
1148         bprm->filename = filename;
1149         bprm->interp = filename;
1150         bprm->mm = mm_alloc();
1151         retval = -ENOMEM;
1152         if (!bprm->mm)
1153                 goto out_file;
1154
1155         retval = init_new_context(current, bprm->mm);
1156         if (retval < 0)
1157                 goto out_mm;
1158
1159         bprm->argc = count(argv, bprm->p / sizeof(void *));
1160         if ((retval = bprm->argc) < 0)
1161                 goto out_mm;
1162
1163         bprm->envc = count(envp, bprm->p / sizeof(void *));
1164         if ((retval = bprm->envc) < 0)
1165                 goto out_mm;
1166
1167         retval = security_bprm_alloc(bprm);
1168         if (retval)
1169                 goto out;
1170
1171         retval = prepare_binprm(bprm);
1172         if (retval < 0)
1173                 goto out;
1174
1175         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1176         if (retval < 0)
1177                 goto out;
1178
1179         bprm->exec = bprm->p;
1180         retval = copy_strings(bprm->envc, envp, bprm);
1181         if (retval < 0)
1182                 goto out;
1183
1184         retval = copy_strings(bprm->argc, argv, bprm);
1185         if (retval < 0)
1186                 goto out;
1187
1188         retval = search_binary_handler(bprm,regs);
1189         if (retval >= 0) {
1190                 free_arg_pages(bprm);
1191
1192                 /* execve success */
1193                 security_bprm_free(bprm);
1194                 kfree(bprm);
1195                 return retval;
1196         }
1197
1198 out:
1199         /* Something went wrong, return the inode and free the argument pages*/
1200         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1201                 struct page * page = bprm->page[i];
1202                 if (page)
1203                         __free_page(page);
1204         }
1205
1206         if (bprm->security)
1207                 security_bprm_free(bprm);
1208
1209 out_mm:
1210         if (bprm->mm)
1211                 mmdrop(bprm->mm);
1212
1213 out_file:
1214         if (bprm->file) {
1215                 allow_write_access(bprm->file);
1216                 fput(bprm->file);
1217         }
1218
1219 out_kfree:
1220         kfree(bprm);
1221
1222 out_ret:
1223         return retval;
1224 }
1225
1226 int set_binfmt(struct linux_binfmt *new)
1227 {
1228         struct linux_binfmt *old = current->binfmt;
1229
1230         if (new) {
1231                 if (!try_module_get(new->module))
1232                         return -1;
1233         }
1234         current->binfmt = new;
1235         if (old)
1236                 module_put(old->module);
1237         return 0;
1238 }
1239
1240 EXPORT_SYMBOL(set_binfmt);
1241
1242 #define CORENAME_MAX_SIZE 64
1243
1244 /* format_corename will inspect the pattern parameter, and output a
1245  * name into corename, which must have space for at least
1246  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1247  */
1248 static void format_corename(char *corename, const char *pattern, long signr)
1249 {
1250         const char *pat_ptr = pattern;
1251         char *out_ptr = corename;
1252         char *const out_end = corename + CORENAME_MAX_SIZE;
1253         int rc;
1254         int pid_in_pattern = 0;
1255
1256         /* Repeat as long as we have more pattern to process and more output
1257            space */
1258         while (*pat_ptr) {
1259                 if (*pat_ptr != '%') {
1260                         if (out_ptr == out_end)
1261                                 goto out;
1262                         *out_ptr++ = *pat_ptr++;
1263                 } else {
1264                         switch (*++pat_ptr) {
1265                         case 0:
1266                                 goto out;
1267                         /* Double percent, output one percent */
1268                         case '%':
1269                                 if (out_ptr == out_end)
1270                                         goto out;
1271                                 *out_ptr++ = '%';
1272                                 break;
1273                         /* pid */
1274                         case 'p':
1275                                 pid_in_pattern = 1;
1276                                 rc = snprintf(out_ptr, out_end - out_ptr,
1277                                               "%d", current->tgid);
1278                                 if (rc > out_end - out_ptr)
1279                                         goto out;
1280                                 out_ptr += rc;
1281                                 break;
1282                         /* uid */
1283                         case 'u':
1284                                 rc = snprintf(out_ptr, out_end - out_ptr,
1285                                               "%d", current->uid);
1286                                 if (rc > out_end - out_ptr)
1287                                         goto out;
1288                                 out_ptr += rc;
1289                                 break;
1290                         /* gid */
1291                         case 'g':
1292                                 rc = snprintf(out_ptr, out_end - out_ptr,
1293                                               "%d", current->gid);
1294                                 if (rc > out_end - out_ptr)
1295                                         goto out;
1296                                 out_ptr += rc;
1297                                 break;
1298                         /* signal that caused the coredump */
1299                         case 's':
1300                                 rc = snprintf(out_ptr, out_end - out_ptr,
1301                                               "%ld", signr);
1302                                 if (rc > out_end - out_ptr)
1303                                         goto out;
1304                                 out_ptr += rc;
1305                                 break;
1306                         /* UNIX time of coredump */
1307                         case 't': {
1308                                 struct timeval tv;
1309                                 do_gettimeofday(&tv);
1310                                 rc = snprintf(out_ptr, out_end - out_ptr,
1311                                               "%lu", tv.tv_sec);
1312                                 if (rc > out_end - out_ptr)
1313                                         goto out;
1314                                 out_ptr += rc;
1315                                 break;
1316                         }
1317                         /* hostname */
1318                         case 'h':
1319                                 down_read(&uts_sem);
1320                                 rc = snprintf(out_ptr, out_end - out_ptr,
1321                                               "%s", system_utsname.nodename);
1322                                 up_read(&uts_sem);
1323                                 if (rc > out_end - out_ptr)
1324                                         goto out;
1325                                 out_ptr += rc;
1326                                 break;
1327                         /* executable */
1328                         case 'e':
1329                                 rc = snprintf(out_ptr, out_end - out_ptr,
1330                                               "%s", current->comm);
1331                                 if (rc > out_end - out_ptr)
1332                                         goto out;
1333                                 out_ptr += rc;
1334                                 break;
1335                         default:
1336                                 break;
1337                         }
1338                         ++pat_ptr;
1339                 }
1340         }
1341         /* Backward compatibility with core_uses_pid:
1342          *
1343          * If core_pattern does not include a %p (as is the default)
1344          * and core_uses_pid is set, then .%pid will be appended to
1345          * the filename */
1346         if (!pid_in_pattern
1347             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1348                 rc = snprintf(out_ptr, out_end - out_ptr,
1349                               ".%d", current->tgid);
1350                 if (rc > out_end - out_ptr)
1351                         goto out;
1352                 out_ptr += rc;
1353         }
1354       out:
1355         *out_ptr = 0;
1356 }
1357
1358 static void zap_threads (struct mm_struct *mm)
1359 {
1360         struct task_struct *g, *p;
1361         struct task_struct *tsk = current;
1362         struct completion *vfork_done = tsk->vfork_done;
1363         int traced = 0;
1364
1365         /*
1366          * Make sure nobody is waiting for us to release the VM,
1367          * otherwise we can deadlock when we wait on each other
1368          */
1369         if (vfork_done) {
1370                 tsk->vfork_done = NULL;
1371                 complete(vfork_done);
1372         }
1373
1374         read_lock(&tasklist_lock);
1375         do_each_thread(g,p)
1376                 if (mm == p->mm && p != tsk) {
1377                         force_sig_specific(SIGKILL, p);
1378                         mm->core_waiters++;
1379                         if (unlikely(p->ptrace) &&
1380                             unlikely(p->parent->mm == mm))
1381                                 traced = 1;
1382                 }
1383         while_each_thread(g,p);
1384
1385         read_unlock(&tasklist_lock);
1386
1387         while (unlikely(traced)) {
1388                 /*
1389                  * We are zapping a thread and the thread it ptraces.
1390                  * The tracee won't come out of TASK_TRACED state until
1391                  * its ptracer detaches.  That happens when the ptracer
1392                  * dies, but it synchronizes with us and so won't get
1393                  * that far until we finish the core dump.  If we're
1394                  * waiting for the tracee to synchronize but it stays
1395                  * blocked in TASK_TRACED, then we deadlock.  So, for
1396                  * this weirdo case we have to do another round with
1397                  * tasklist_lock write-locked to __ptrace_unlink the
1398                  * children that might cause this deadlock.  That will
1399                  * wake them up to process their pending SIGKILL.
1400                  *
1401                  * First, give everyone we just killed a chance to run
1402                  * so they can all get into the coredump synchronization.
1403                  * That should leave only the TASK_TRACED stragglers for
1404                  * us to wake up.  If a ptracer is still running, we'll
1405                  * have to come around again after letting it finish.
1406                  */
1407                 yield();
1408                 traced = 0;
1409                 write_lock_irq(&tasklist_lock);
1410                 do_each_thread(g,p) {
1411                         if (mm != p->mm || p == tsk ||
1412                             !p->ptrace || p->parent->mm != mm)
1413                                 continue;
1414                         if ((p->parent->flags & (PF_SIGNALED|PF_EXITING)) ||
1415                             (p->parent->state & (TASK_TRACED|TASK_STOPPED))) {
1416                                 /*
1417                                  * The parent is in the process of exiting
1418                                  * itself, or else it's stopped right now.
1419                                  * It cannot be in a ptrace call, and would
1420                                  * have to read_lock tasklist_lock before
1421                                  * it could start one, so we are safe here.
1422                                  */
1423                                 __ptrace_unlink(p);
1424                         } else {
1425                                 /*
1426                                  * Blargh!  The ptracer is not dying
1427                                  * yet, so we cannot be sure that it
1428                                  * isn't in the middle of a ptrace call.
1429                                  * We'll have to let it run to get into
1430                                  * coredump_wait and come around another
1431                                  * time to detach its tracee.
1432                                  */
1433                                 traced = 1;
1434                         }
1435                 } while_each_thread(g,p);
1436                 write_unlock_irq(&tasklist_lock);
1437         }
1438 }
1439
1440 static void coredump_wait(struct mm_struct *mm)
1441 {
1442         DECLARE_COMPLETION(startup_done);
1443
1444         mm->core_waiters++; /* let other threads block */
1445         mm->core_startup_done = &startup_done;
1446
1447         /* give other threads a chance to run: */
1448         yield();
1449
1450         zap_threads(mm);
1451         if (--mm->core_waiters) {
1452                 up_write(&mm->mmap_sem);
1453                 wait_for_completion(&startup_done);
1454         } else
1455                 up_write(&mm->mmap_sem);
1456         BUG_ON(mm->core_waiters);
1457 }
1458
1459 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1460 {
1461         char corename[CORENAME_MAX_SIZE + 1];
1462         struct mm_struct *mm = current->mm;
1463         struct linux_binfmt * binfmt;
1464         struct inode * inode;
1465         struct file * file;
1466         int retval = 0;
1467         int fsuid = current->fsuid;
1468         int flag = 0;
1469
1470         binfmt = current->binfmt;
1471         if (!binfmt || !binfmt->core_dump)
1472                 goto fail;
1473         if (current->tux_exit)
1474                 current->tux_exit();
1475         down_write(&mm->mmap_sem);
1476         if (!mm->dumpable) {
1477                 up_write(&mm->mmap_sem);
1478                 goto fail;
1479         }
1480
1481         /*
1482          *      We cannot trust fsuid as being the "true" uid of the
1483          *      process nor do we know its entire history. We only know it
1484          *      was tainted so we dump it as root in mode 2.
1485          */
1486         if (mm->dumpable == 2) {        /* Setuid core dump mode */
1487                 flag = O_EXCL;          /* Stop rewrite attacks */
1488                 current->fsuid = 0;     /* Dump root private */
1489         }
1490         mm->dumpable = 0;
1491         init_completion(&mm->core_done);
1492         current->signal->group_exit = 1;
1493         current->signal->group_exit_code = exit_code;
1494         coredump_wait(mm);
1495
1496         if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1497                 goto fail_unlock;
1498
1499         /*
1500          * lock_kernel() because format_corename() is controlled by sysctl, which
1501          * uses lock_kernel()
1502          */
1503         lock_kernel();
1504         format_corename(corename, core_pattern, signr);
1505         unlock_kernel();
1506         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1507         if (IS_ERR(file))
1508                 goto fail_unlock;
1509         inode = file->f_dentry->d_inode;
1510         if (inode->i_nlink > 1)
1511                 goto close_fail;        /* multiple links - don't dump */
1512         if (d_unhashed(file->f_dentry))
1513                 goto close_fail;
1514
1515         if (!S_ISREG(inode->i_mode))
1516                 goto close_fail;
1517         if (!file->f_op)
1518                 goto close_fail;
1519         if (!file->f_op->write)
1520                 goto close_fail;
1521         if (do_truncate(file->f_dentry, 0) != 0)
1522                 goto close_fail;
1523
1524         retval = binfmt->core_dump(signr, regs, file);
1525
1526         if (retval)
1527                 current->signal->group_exit_code |= 0x80;
1528 close_fail:
1529         filp_close(file, NULL);
1530 fail_unlock:
1531         current->fsuid = fsuid;
1532         complete_all(&mm->core_done);
1533 fail:
1534         return retval;
1535 }