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