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