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