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