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