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