4aa484b1f57cf2e995360b1eb4b1e7848e76bdf9
[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_events.h>
51 #include <linux/ckrm_mem_inline.h>
52 #include <linux/vs_memory.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         ckrm_task_change_mm(tsk, old_mm, mm);
568         if (old_mm) {
569                 if (active_mm != old_mm) BUG();
570                 mmput(old_mm);
571                 return 0;
572         }
573         mmdrop(active_mm);
574         return 0;
575 }
576
577 /*
578  * This function makes sure the current process has its own signal table,
579  * so that flush_signal_handlers can later reset the handlers without
580  * disturbing other processes.  (Other processes might share the signal
581  * table via the CLONE_SIGHAND option to clone().)
582  */
583 static inline int de_thread(struct task_struct *tsk)
584 {
585         struct signal_struct *sig = tsk->signal;
586         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
587         spinlock_t *lock = &oldsighand->siglock;
588         int count;
589
590         /*
591          * If we don't share sighandlers, then we aren't sharing anything
592          * and we can just re-use it all.
593          */
594         if (atomic_read(&oldsighand->count) <= 1) {
595                 BUG_ON(atomic_read(&sig->count) != 1);
596                 exit_itimers(sig);
597                 return 0;
598         }
599
600         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
601         if (!newsighand)
602                 return -ENOMEM;
603
604         if (thread_group_empty(current))
605                 goto no_thread_group;
606
607         /*
608          * Kill all other threads in the thread group.
609          * We must hold tasklist_lock to call zap_other_threads.
610          */
611         read_lock(&tasklist_lock);
612         spin_lock_irq(lock);
613         if (sig->group_exit) {
614                 /*
615                  * Another group action in progress, just
616                  * return so that the signal is processed.
617                  */
618                 spin_unlock_irq(lock);
619                 read_unlock(&tasklist_lock);
620                 kmem_cache_free(sighand_cachep, newsighand);
621                 return -EAGAIN;
622         }
623         sig->group_exit = 1;
624         zap_other_threads(current);
625         read_unlock(&tasklist_lock);
626
627         /*
628          * Account for the thread group leader hanging around:
629          */
630         count = 2;
631         if (current->pid == current->tgid)
632                 count = 1;
633         while (atomic_read(&sig->count) > count) {
634                 sig->group_exit_task = current;
635                 sig->notify_count = count;
636                 __set_current_state(TASK_UNINTERRUPTIBLE);
637                 spin_unlock_irq(lock);
638                 schedule();
639                 spin_lock_irq(lock);
640         }
641         sig->group_exit_task = NULL;
642         sig->notify_count = 0;
643         spin_unlock_irq(lock);
644
645         /*
646          * At this point all other threads have exited, all we have to
647          * do is to wait for the thread group leader to become inactive,
648          * and to assume its PID:
649          */
650         if (current->pid != current->tgid) {
651                 struct task_struct *leader = current->group_leader, *parent;
652                 struct dentry *proc_dentry1, *proc_dentry2;
653                 unsigned long exit_state, ptrace;
654
655                 /*
656                  * Wait for the thread group leader to be a zombie.
657                  * It should already be zombie at this point, most
658                  * of the time.
659                  */
660                 while (leader->exit_state != EXIT_ZOMBIE)
661                         yield();
662
663                 spin_lock(&leader->proc_lock);
664                 spin_lock(&current->proc_lock);
665                 proc_dentry1 = proc_pid_unhash(current);
666                 proc_dentry2 = proc_pid_unhash(leader);
667                 write_lock_irq(&tasklist_lock);
668
669                 if (leader->tgid != current->tgid)
670                         BUG();
671                 if (current->pid == current->tgid)
672                         BUG();
673                 /*
674                  * An exec() starts a new thread group with the
675                  * TGID of the previous thread group. Rehash the
676                  * two threads with a switched PID, and release
677                  * the former thread group leader:
678                  */
679                 ptrace = leader->ptrace;
680                 parent = leader->parent;
681
682                 ptrace_unlink(current);
683                 ptrace_unlink(leader);
684                 remove_parent(current);
685                 remove_parent(leader);
686
687                 switch_exec_pids(leader, current);
688
689                 current->parent = current->real_parent = leader->real_parent;
690                 leader->parent = leader->real_parent = child_reaper;
691                 current->group_leader = current;
692                 leader->group_leader = leader;
693
694                 add_parent(current, current->parent);
695                 add_parent(leader, leader->parent);
696                 if (ptrace) {
697                         current->ptrace = ptrace;
698                         __ptrace_link(current, parent);
699                 }
700
701                 list_del(&current->tasks);
702                 list_add_tail(&current->tasks, &init_task.tasks);
703                 current->exit_signal = SIGCHLD;
704                 exit_state = leader->exit_state;
705
706                 write_unlock_irq(&tasklist_lock);
707                 spin_unlock(&leader->proc_lock);
708                 spin_unlock(&current->proc_lock);
709                 proc_pid_flush(proc_dentry1);
710                 proc_pid_flush(proc_dentry2);
711
712                 if (exit_state != EXIT_ZOMBIE)
713                         BUG();
714                 release_task(leader);
715         }
716
717         /*
718          * Now there are really no other threads at all,
719          * so it's safe to stop telling them to kill themselves.
720          */
721         sig->group_exit = 0;
722
723 no_thread_group:
724         BUG_ON(atomic_read(&sig->count) != 1);
725         exit_itimers(sig);
726
727         if (atomic_read(&oldsighand->count) == 1) {
728                 /*
729                  * Now that we nuked the rest of the thread group,
730                  * it turns out we are not sharing sighand any more either.
731                  * So we can just keep it.
732                  */
733                 kmem_cache_free(sighand_cachep, newsighand);
734         } else {
735                 /*
736                  * Move our state over to newsighand and switch it in.
737                  */
738                 spin_lock_init(&newsighand->siglock);
739                 atomic_set(&newsighand->count, 1);
740                 memcpy(newsighand->action, oldsighand->action,
741                        sizeof(newsighand->action));
742                 write_lock_irq(&tasklist_lock);
743                 spin_lock(&oldsighand->siglock);
744                 spin_lock(&newsighand->siglock);
745                 current->sighand = newsighand;
746                 recalc_sigpending();
747                 spin_unlock(&newsighand->siglock);
748                 spin_unlock(&oldsighand->siglock);
749                 write_unlock_irq(&tasklist_lock);
750
751                 if (atomic_dec_and_test(&oldsighand->count))
752                         kmem_cache_free(sighand_cachep, oldsighand);
753         }
754
755         if (!thread_group_empty(current))
756                 BUG();
757         if (current->tgid != current->pid)
758                 BUG();
759         return 0;
760 }
761         
762 /*
763  * These functions flushes out all traces of the currently running executable
764  * so that a new one can be started
765  */
766
767 static inline void flush_old_files(struct files_struct * files)
768 {
769         long j = -1;
770
771         spin_lock(&files->file_lock);
772         for (;;) {
773                 unsigned long set, i;
774
775                 j++;
776                 i = j * __NFDBITS;
777                 if (i >= files->max_fds || i >= files->max_fdset)
778                         break;
779                 set = files->close_on_exec->fds_bits[j];
780                 if (!set)
781                         continue;
782                 files->close_on_exec->fds_bits[j] = 0;
783                 spin_unlock(&files->file_lock);
784                 for ( ; set ; i++,set >>= 1) {
785                         if (set & 1) {
786                                 sys_close(i);
787                         }
788                 }
789                 spin_lock(&files->file_lock);
790
791         }
792         spin_unlock(&files->file_lock);
793 }
794
795 void get_task_comm(char *buf, struct task_struct *tsk)
796 {
797         /* buf must be at least sizeof(tsk->comm) in size */
798         task_lock(tsk);
799         strncpy(buf, tsk->comm, sizeof(tsk->comm));
800         task_unlock(tsk);
801 }
802
803 void set_task_comm(struct task_struct *tsk, char *buf)
804 {
805         task_lock(tsk);
806         strlcpy(tsk->comm, buf, sizeof(tsk->comm));
807         task_unlock(tsk);
808 }
809
810 int flush_old_exec(struct linux_binprm * bprm)
811 {
812         char * name;
813         int i, ch, retval;
814         struct files_struct *files;
815         char tcomm[sizeof(current->comm)];
816
817         /*
818          * Make sure we have a private signal table and that
819          * we are unassociated from the previous thread group.
820          */
821         retval = de_thread(current);
822         if (retval)
823                 goto out;
824
825         /*
826          * Make sure we have private file handles. Ask the
827          * fork helper to do the work for us and the exit
828          * helper to do the cleanup of the old one.
829          */
830         files = current->files;         /* refcounted so safe to hold */
831         retval = unshare_files();
832         if (retval)
833                 goto out;
834         /*
835          * Release all of the old mmap stuff
836          */
837         retval = exec_mmap(bprm->mm);
838         if (retval)
839                 goto mmap_failed;
840
841         bprm->mm = NULL;                /* We're using it now */
842
843         /* This is the point of no return */
844         steal_locks(files);
845         put_files_struct(files);
846
847         current->sas_ss_sp = current->sas_ss_size = 0;
848
849         if (current->euid == current->uid && current->egid == current->gid)
850                 current->mm->dumpable = 1;
851         else
852                 current->mm->dumpable = suid_dumpable;
853                 
854         name = bprm->filename;
855         for (i=0; (ch = *(name++)) != '\0';) {
856                 if (ch == '/')
857                         i = 0;
858                 else
859                         if (i < (sizeof(tcomm) - 1))
860                                 tcomm[i++] = ch;
861         }
862         tcomm[i] = '\0';
863         set_task_comm(current, tcomm);
864
865         current->flags &= ~PF_RELOCEXEC;
866         flush_thread();
867
868         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
869             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
870             (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
871                 suid_keys(current);
872                 current->mm->dumpable = suid_dumpable;
873         }
874
875         /* An exec changes our domain. We are no longer part of the thread
876            group */
877
878         current->self_exec_id++;
879                         
880         flush_signal_handlers(current, 0);
881         flush_old_files(current->files);
882
883         return 0;
884
885 mmap_failed:
886         put_files_struct(current->files);
887         current->files = files;
888 out:
889         return retval;
890 }
891
892 EXPORT_SYMBOL(flush_old_exec);
893
894 /* 
895  * Fill the binprm structure from the inode. 
896  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
897  */
898 int prepare_binprm(struct linux_binprm *bprm)
899 {
900         int mode;
901         struct inode * inode = bprm->file->f_dentry->d_inode;
902         int retval;
903
904         mode = inode->i_mode;
905         /*
906          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
907          * generic_permission lets a non-executable through
908          */
909         if (!(mode & 0111))     /* with at least _one_ execute bit set */
910                 return -EACCES;
911         if (bprm->file->f_op == NULL)
912                 return -EACCES;
913
914         bprm->e_uid = current->euid;
915         bprm->e_gid = current->egid;
916
917         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
918                 /* Set-uid? */
919                 if (mode & S_ISUID) {
920                         current->personality &= ~PER_CLEAR_ON_SETID;
921                         bprm->e_uid = inode->i_uid;
922                 }
923
924                 /* Set-gid? */
925                 /*
926                  * If setgid is set but no group execute bit then this
927                  * is a candidate for mandatory locking, not a setgid
928                  * executable.
929                  */
930                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
931                         current->personality &= ~PER_CLEAR_ON_SETID;
932                         bprm->e_gid = inode->i_gid;
933                 }
934         }
935
936         /* fill in binprm security blob */
937         retval = security_bprm_set(bprm);
938         if (retval)
939                 return retval;
940
941         memset(bprm->buf,0,BINPRM_BUF_SIZE);
942         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
943 }
944
945 EXPORT_SYMBOL(prepare_binprm);
946
947 static inline int unsafe_exec(struct task_struct *p)
948 {
949         int unsafe = 0;
950         if (p->ptrace & PT_PTRACED) {
951                 if (p->ptrace & PT_PTRACE_CAP)
952                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
953                 else
954                         unsafe |= LSM_UNSAFE_PTRACE;
955         }
956         if (atomic_read(&p->fs->count) > 1 ||
957             atomic_read(&p->files->count) > 1 ||
958             atomic_read(&p->sighand->count) > 1)
959                 unsafe |= LSM_UNSAFE_SHARE;
960
961         return unsafe;
962 }
963
964 void compute_creds(struct linux_binprm *bprm)
965 {
966         int unsafe;
967
968         if (bprm->e_uid != current->uid)
969                 suid_keys(current);
970         exec_keys(current);
971
972         task_lock(current);
973         unsafe = unsafe_exec(current);
974         security_bprm_apply_creds(bprm, unsafe);
975         task_unlock(current);
976 }
977
978 EXPORT_SYMBOL(compute_creds);
979
980 void remove_arg_zero(struct linux_binprm *bprm)
981 {
982         if (bprm->argc) {
983                 unsigned long offset;
984                 char * kaddr;
985                 struct page *page;
986
987                 offset = bprm->p % PAGE_SIZE;
988                 goto inside;
989
990                 while (bprm->p++, *(kaddr+offset++)) {
991                         if (offset != PAGE_SIZE)
992                                 continue;
993                         offset = 0;
994                         kunmap_atomic(kaddr, KM_USER0);
995 inside:
996                         page = bprm->page[bprm->p/PAGE_SIZE];
997                         kaddr = kmap_atomic(page, KM_USER0);
998                 }
999                 kunmap_atomic(kaddr, KM_USER0);
1000                 bprm->argc--;
1001         }
1002 }
1003
1004 EXPORT_SYMBOL(remove_arg_zero);
1005
1006 /*
1007  * cycle the list of binary formats handler, until one recognizes the image
1008  */
1009 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1010 {
1011         int try,retval;
1012         struct linux_binfmt *fmt;
1013 #ifdef __alpha__
1014         /* handle /sbin/loader.. */
1015         {
1016             struct exec * eh = (struct exec *) bprm->buf;
1017
1018             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1019                 (eh->fh.f_flags & 0x3000) == 0x3000)
1020             {
1021                 struct file * file;
1022                 unsigned long loader;
1023
1024                 allow_write_access(bprm->file);
1025                 fput(bprm->file);
1026                 bprm->file = NULL;
1027
1028                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1029
1030                 file = open_exec("/sbin/loader");
1031                 retval = PTR_ERR(file);
1032                 if (IS_ERR(file))
1033                         return retval;
1034
1035                 /* Remember if the application is TASO.  */
1036                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1037
1038                 bprm->file = file;
1039                 bprm->loader = loader;
1040                 retval = prepare_binprm(bprm);
1041                 if (retval<0)
1042                         return retval;
1043                 /* should call search_binary_handler recursively here,
1044                    but it does not matter */
1045             }
1046         }
1047 #endif
1048         retval = security_bprm_check(bprm);
1049         if (retval)
1050                 return retval;
1051
1052         /* kernel module loader fixup */
1053         /* so we don't try to load run modprobe in kernel space. */
1054         set_fs(USER_DS);
1055         retval = -ENOENT;
1056         for (try=0; try<2; try++) {
1057                 read_lock(&binfmt_lock);
1058                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1059                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1060                         if (!fn)
1061                                 continue;
1062                         if (!try_module_get(fmt->module))
1063                                 continue;
1064                         read_unlock(&binfmt_lock);
1065                         retval = fn(bprm, regs);
1066                         if (retval >= 0) {
1067                                 put_binfmt(fmt);
1068                                 allow_write_access(bprm->file);
1069                                 if (bprm->file)
1070                                         fput(bprm->file);
1071                                 bprm->file = NULL;
1072                                 current->did_exec = 1;
1073                                 ckrm_cb_exec(bprm->filename);
1074                                 return retval;
1075                         }
1076                         read_lock(&binfmt_lock);
1077                         put_binfmt(fmt);
1078                         if (retval != -ENOEXEC || bprm->mm == NULL)
1079                                 break;
1080                         if (!bprm->file) {
1081                                 read_unlock(&binfmt_lock);
1082                                 return retval;
1083                         }
1084                 }
1085                 read_unlock(&binfmt_lock);
1086                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1087                         break;
1088 #ifdef CONFIG_KMOD
1089                 }else{
1090 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1091                         if (printable(bprm->buf[0]) &&
1092                             printable(bprm->buf[1]) &&
1093                             printable(bprm->buf[2]) &&
1094                             printable(bprm->buf[3]))
1095                                 break; /* -ENOEXEC */
1096                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1097 #endif
1098                 }
1099         }
1100         return retval;
1101 }
1102
1103 EXPORT_SYMBOL(search_binary_handler);
1104
1105 /*
1106  * sys_execve() executes a new program.
1107  */
1108 int do_execve(char * filename,
1109         char __user *__user *argv,
1110         char __user *__user *envp,
1111         struct pt_regs * regs)
1112 {
1113         struct linux_binprm *bprm;
1114         struct file *file;
1115         int retval;
1116         int i;
1117
1118         retval = -ENOMEM;
1119         bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1120         if (!bprm)
1121                 goto out_ret;
1122         memset(bprm, 0, sizeof(*bprm));
1123
1124         file = open_exec(filename);
1125         retval = PTR_ERR(file);
1126         if (IS_ERR(file))
1127                 goto out_kfree;
1128
1129         sched_exec();
1130
1131         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1132
1133         bprm->file = file;
1134         bprm->filename = filename;
1135         bprm->interp = filename;
1136         bprm->mm = mm_alloc();
1137         retval = -ENOMEM;
1138         if (!bprm->mm)
1139                 goto out_file;
1140
1141         retval = init_new_context(current, bprm->mm);
1142         if (retval < 0)
1143                 goto out_mm;
1144
1145         bprm->argc = count(argv, bprm->p / sizeof(void *));
1146         if ((retval = bprm->argc) < 0)
1147                 goto out_mm;
1148
1149         bprm->envc = count(envp, bprm->p / sizeof(void *));
1150         if ((retval = bprm->envc) < 0)
1151                 goto out_mm;
1152
1153         retval = security_bprm_alloc(bprm);
1154         if (retval)
1155                 goto out;
1156
1157         retval = prepare_binprm(bprm);
1158         if (retval < 0)
1159                 goto out;
1160
1161         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1162         if (retval < 0)
1163                 goto out;
1164
1165         bprm->exec = bprm->p;
1166         retval = copy_strings(bprm->envc, envp, bprm);
1167         if (retval < 0)
1168                 goto out;
1169
1170         retval = copy_strings(bprm->argc, argv, bprm);
1171         if (retval < 0)
1172                 goto out;
1173
1174         retval = search_binary_handler(bprm,regs);
1175         if (retval >= 0) {
1176                 free_arg_pages(bprm);
1177
1178                 /* execve success */
1179                 security_bprm_free(bprm);
1180                 kfree(bprm);
1181                 return retval;
1182         }
1183
1184 out:
1185         /* Something went wrong, return the inode and free the argument pages*/
1186         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1187                 struct page * page = bprm->page[i];
1188                 if (page)
1189                         __free_page(page);
1190         }
1191
1192         if (bprm->security)
1193                 security_bprm_free(bprm);
1194
1195 out_mm:
1196         if (bprm->mm)
1197                 mmdrop(bprm->mm);
1198
1199 out_file:
1200         if (bprm->file) {
1201                 allow_write_access(bprm->file);
1202                 fput(bprm->file);
1203         }
1204
1205 out_kfree:
1206         kfree(bprm);
1207
1208 out_ret:
1209         return retval;
1210 }
1211
1212 int set_binfmt(struct linux_binfmt *new)
1213 {
1214         struct linux_binfmt *old = current->binfmt;
1215
1216         if (new) {
1217                 if (!try_module_get(new->module))
1218                         return -1;
1219         }
1220         current->binfmt = new;
1221         if (old)
1222                 module_put(old->module);
1223         return 0;
1224 }
1225
1226 EXPORT_SYMBOL(set_binfmt);
1227
1228 #define CORENAME_MAX_SIZE 64
1229
1230 /* format_corename will inspect the pattern parameter, and output a
1231  * name into corename, which must have space for at least
1232  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1233  */
1234 static void format_corename(char *corename, const char *pattern, long signr)
1235 {
1236         const char *pat_ptr = pattern;
1237         char *out_ptr = corename;
1238         char *const out_end = corename + CORENAME_MAX_SIZE;
1239         int rc;
1240         int pid_in_pattern = 0;
1241
1242         /* Repeat as long as we have more pattern to process and more output
1243            space */
1244         while (*pat_ptr) {
1245                 if (*pat_ptr != '%') {
1246                         if (out_ptr == out_end)
1247                                 goto out;
1248                         *out_ptr++ = *pat_ptr++;
1249                 } else {
1250                         switch (*++pat_ptr) {
1251                         case 0:
1252                                 goto out;
1253                         /* Double percent, output one percent */
1254                         case '%':
1255                                 if (out_ptr == out_end)
1256                                         goto out;
1257                                 *out_ptr++ = '%';
1258                                 break;
1259                         /* pid */
1260                         case 'p':
1261                                 pid_in_pattern = 1;
1262                                 rc = snprintf(out_ptr, out_end - out_ptr,
1263                                               "%d", current->tgid);
1264                                 if (rc > out_end - out_ptr)
1265                                         goto out;
1266                                 out_ptr += rc;
1267                                 break;
1268                         /* uid */
1269                         case 'u':
1270                                 rc = snprintf(out_ptr, out_end - out_ptr,
1271                                               "%d", current->uid);
1272                                 if (rc > out_end - out_ptr)
1273                                         goto out;
1274                                 out_ptr += rc;
1275                                 break;
1276                         /* gid */
1277                         case 'g':
1278                                 rc = snprintf(out_ptr, out_end - out_ptr,
1279                                               "%d", current->gid);
1280                                 if (rc > out_end - out_ptr)
1281                                         goto out;
1282                                 out_ptr += rc;
1283                                 break;
1284                         /* signal that caused the coredump */
1285                         case 's':
1286                                 rc = snprintf(out_ptr, out_end - out_ptr,
1287                                               "%ld", signr);
1288                                 if (rc > out_end - out_ptr)
1289                                         goto out;
1290                                 out_ptr += rc;
1291                                 break;
1292                         /* UNIX time of coredump */
1293                         case 't': {
1294                                 struct timeval tv;
1295                                 do_gettimeofday(&tv);
1296                                 rc = snprintf(out_ptr, out_end - out_ptr,
1297                                               "%lu", tv.tv_sec);
1298                                 if (rc > out_end - out_ptr)
1299                                         goto out;
1300                                 out_ptr += rc;
1301                                 break;
1302                         }
1303                         /* hostname */
1304                         case 'h':
1305                                 down_read(&uts_sem);
1306                                 rc = snprintf(out_ptr, out_end - out_ptr,
1307                                               "%s", system_utsname.nodename);
1308                                 up_read(&uts_sem);
1309                                 if (rc > out_end - out_ptr)
1310                                         goto out;
1311                                 out_ptr += rc;
1312                                 break;
1313                         /* executable */
1314                         case 'e':
1315                                 rc = snprintf(out_ptr, out_end - out_ptr,
1316                                               "%s", current->comm);
1317                                 if (rc > out_end - out_ptr)
1318                                         goto out;
1319                                 out_ptr += rc;
1320                                 break;
1321                         default:
1322                                 break;
1323                         }
1324                         ++pat_ptr;
1325                 }
1326         }
1327         /* Backward compatibility with core_uses_pid:
1328          *
1329          * If core_pattern does not include a %p (as is the default)
1330          * and core_uses_pid is set, then .%pid will be appended to
1331          * the filename */
1332         if (!pid_in_pattern
1333             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1334                 rc = snprintf(out_ptr, out_end - out_ptr,
1335                               ".%d", current->tgid);
1336                 if (rc > out_end - out_ptr)
1337                         goto out;
1338                 out_ptr += rc;
1339         }
1340       out:
1341         *out_ptr = 0;
1342 }
1343
1344 static void zap_threads (struct mm_struct *mm)
1345 {
1346         struct task_struct *g, *p;
1347         struct task_struct *tsk = current;
1348         struct completion *vfork_done = tsk->vfork_done;
1349         int traced = 0;
1350
1351         /*
1352          * Make sure nobody is waiting for us to release the VM,
1353          * otherwise we can deadlock when we wait on each other
1354          */
1355         if (vfork_done) {
1356                 tsk->vfork_done = NULL;
1357                 complete(vfork_done);
1358         }
1359
1360         read_lock(&tasklist_lock);
1361         do_each_thread(g,p)
1362                 if (mm == p->mm && p != tsk) {
1363                         force_sig_specific(SIGKILL, p);
1364                         mm->core_waiters++;
1365                         if (unlikely(p->ptrace) &&
1366                             unlikely(p->parent->mm == mm))
1367                                 traced = 1;
1368                 }
1369         while_each_thread(g,p);
1370
1371         read_unlock(&tasklist_lock);
1372
1373         while (unlikely(traced)) {
1374                 /*
1375                  * We are zapping a thread and the thread it ptraces.
1376                  * The tracee won't come out of TASK_TRACED state until
1377                  * its ptracer detaches.  That happens when the ptracer
1378                  * dies, but it synchronizes with us and so won't get
1379                  * that far until we finish the core dump.  If we're
1380                  * waiting for the tracee to synchronize but it stays
1381                  * blocked in TASK_TRACED, then we deadlock.  So, for
1382                  * this weirdo case we have to do another round with
1383                  * tasklist_lock write-locked to __ptrace_unlink the
1384                  * children that might cause this deadlock.  That will
1385                  * wake them up to process their pending SIGKILL.
1386                  *
1387                  * First, give everyone we just killed a chance to run
1388                  * so they can all get into the coredump synchronization.
1389                  * That should leave only the TASK_TRACED stragglers for
1390                  * us to wake up.  If a ptracer is still running, we'll
1391                  * have to come around again after letting it finish.
1392                  */
1393                 yield();
1394                 traced = 0;
1395                 write_lock_irq(&tasklist_lock);
1396                 do_each_thread(g,p) {
1397                         if (mm != p->mm || p == tsk ||
1398                             !p->ptrace || p->parent->mm != mm)
1399                                 continue;
1400                         if ((p->parent->flags & (PF_SIGNALED|PF_EXITING)) ||
1401                             (p->parent->state & (TASK_TRACED|TASK_STOPPED))) {
1402                                 /*
1403                                  * The parent is in the process of exiting
1404                                  * itself, or else it's stopped right now.
1405                                  * It cannot be in a ptrace call, and would
1406                                  * have to read_lock tasklist_lock before
1407                                  * it could start one, so we are safe here.
1408                                  */
1409                                 __ptrace_unlink(p);
1410                         } else {
1411                                 /*
1412                                  * Blargh!  The ptracer is not dying
1413                                  * yet, so we cannot be sure that it
1414                                  * isn't in the middle of a ptrace call.
1415                                  * We'll have to let it run to get into
1416                                  * coredump_wait and come around another
1417                                  * time to detach its tracee.
1418                                  */
1419                                 traced = 1;
1420                         }
1421                 } while_each_thread(g,p);
1422                 write_unlock_irq(&tasklist_lock);
1423         }
1424 }
1425
1426 static void coredump_wait(struct mm_struct *mm)
1427 {
1428         DECLARE_COMPLETION(startup_done);
1429
1430         mm->core_waiters++; /* let other threads block */
1431         mm->core_startup_done = &startup_done;
1432
1433         /* give other threads a chance to run: */
1434         yield();
1435
1436         zap_threads(mm);
1437         if (--mm->core_waiters) {
1438                 up_write(&mm->mmap_sem);
1439                 wait_for_completion(&startup_done);
1440         } else
1441                 up_write(&mm->mmap_sem);
1442         BUG_ON(mm->core_waiters);
1443 }
1444
1445 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1446 {
1447         char corename[CORENAME_MAX_SIZE + 1];
1448         struct mm_struct *mm = current->mm;
1449         struct linux_binfmt * binfmt;
1450         struct inode * inode;
1451         struct file * file;
1452         int retval = 0;
1453         int fsuid = current->fsuid;
1454         int flag = 0;
1455
1456         binfmt = current->binfmt;
1457         if (!binfmt || !binfmt->core_dump)
1458                 goto fail;
1459         if (current->tux_exit)
1460                 current->tux_exit();
1461         down_write(&mm->mmap_sem);
1462         if (!mm->dumpable) {
1463                 up_write(&mm->mmap_sem);
1464                 goto fail;
1465         }
1466
1467         /*
1468          *      We cannot trust fsuid as being the "true" uid of the
1469          *      process nor do we know its entire history. We only know it
1470          *      was tainted so we dump it as root in mode 2.
1471          */
1472         if (mm->dumpable == 2) {        /* Setuid core dump mode */
1473                 flag = O_EXCL;          /* Stop rewrite attacks */
1474                 current->fsuid = 0;     /* Dump root private */
1475         }
1476         mm->dumpable = 0;
1477         init_completion(&mm->core_done);
1478         current->signal->group_exit = 1;
1479         current->signal->group_exit_code = exit_code;
1480         coredump_wait(mm);
1481
1482         if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1483                 goto fail_unlock;
1484
1485         /*
1486          * lock_kernel() because format_corename() is controlled by sysctl, which
1487          * uses lock_kernel()
1488          */
1489         lock_kernel();
1490         format_corename(corename, core_pattern, signr);
1491         unlock_kernel();
1492         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1493         if (IS_ERR(file))
1494                 goto fail_unlock;
1495         inode = file->f_dentry->d_inode;
1496         if (inode->i_nlink > 1)
1497                 goto close_fail;        /* multiple links - don't dump */
1498         if (d_unhashed(file->f_dentry))
1499                 goto close_fail;
1500
1501         if (!S_ISREG(inode->i_mode))
1502                 goto close_fail;
1503         if (!file->f_op)
1504                 goto close_fail;
1505         if (!file->f_op->write)
1506                 goto close_fail;
1507         if (do_truncate(file->f_dentry, 0) != 0)
1508                 goto close_fail;
1509
1510         retval = binfmt->core_dump(signr, regs, file);
1511
1512         if (retval)
1513                 current->signal->group_exit_code |= 0x80;
1514 close_fail:
1515         filp_close(file, NULL);
1516 fail_unlock:
1517         current->fsuid = fsuid;
1518         complete_all(&mm->core_done);
1519 fail:
1520         return retval;
1521 }