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