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