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