5057a300ac209272bca57a33a6fab5e3e7a233b2
[linux-2.6.git] / fs / exec.c
1 /*
2  *  linux/fs/exec.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * #!-checking implemented by tytso.
9  */
10 /*
11  * Demand-loading implemented 01.12.91 - no need to read anything but
12  * the header into memory. The inode of the executable is put into
13  * "current->executable", and page faults do the actual loading. Clean.
14  *
15  * Once more I can proudly say that linux stood up to being changed: it
16  * was less than 2 hours work to get demand-loading completely implemented.
17  *
18  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
19  * current->executable is only used by the procfs.  This allows a dispatch
20  * table to check for several different types  of binary formats.  We keep
21  * trying until we recognize the file or we run out of supported binary
22  * formats. 
23  */
24
25 #include <linux/slab.h>
26 #include <linux/file.h>
27 #include <linux/mman.h>
28 #include <linux/a.out.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/highmem.h>
35 #include <linux/spinlock.h>
36 #include <linux/key.h>
37 #include <linux/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/module.h>
42 #include <linux/namei.h>
43 #include <linux/proc_fs.h>
44 #include <linux/tracehook.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/rmap.h>
49 #include <linux/acct.h>
50 #include <linux/cn_proc.h>
51 #include <linux/audit.h>
52 #include <linux/vs_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 = tracehook_unsafe_exec(p);
966         if (atomic_read(&p->fs->count) > 1 ||
967             atomic_read(&p->files->count) > 1 ||
968             atomic_read(&p->sighand->count) > 1)
969                 unsafe |= LSM_UNSAFE_SHARE;
970
971         return unsafe;
972 }
973
974 void compute_creds(struct linux_binprm *bprm)
975 {
976         int unsafe;
977
978         if (bprm->e_uid != current->uid)
979                 suid_keys(current);
980         exec_keys(current);
981
982         task_lock(current);
983         unsafe = unsafe_exec(current);
984         security_bprm_apply_creds(bprm, unsafe);
985         task_unlock(current);
986         security_bprm_post_apply_creds(bprm);
987 }
988
989 EXPORT_SYMBOL(compute_creds);
990
991 void remove_arg_zero(struct linux_binprm *bprm)
992 {
993         if (bprm->argc) {
994                 unsigned long offset;
995                 char * kaddr;
996                 struct page *page;
997
998                 offset = bprm->p % PAGE_SIZE;
999                 goto inside;
1000
1001                 while (bprm->p++, *(kaddr+offset++)) {
1002                         if (offset != PAGE_SIZE)
1003                                 continue;
1004                         offset = 0;
1005                         kunmap_atomic(kaddr, KM_USER0);
1006 inside:
1007                         page = bprm->page[bprm->p/PAGE_SIZE];
1008                         kaddr = kmap_atomic(page, KM_USER0);
1009                 }
1010                 kunmap_atomic(kaddr, KM_USER0);
1011                 bprm->argc--;
1012         }
1013 }
1014
1015 EXPORT_SYMBOL(remove_arg_zero);
1016
1017 /*
1018  * cycle the list of binary formats handler, until one recognizes the image
1019  */
1020 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1021 {
1022         int try,retval;
1023         struct linux_binfmt *fmt;
1024 #ifdef __alpha__
1025         /* handle /sbin/loader.. */
1026         {
1027             struct exec * eh = (struct exec *) bprm->buf;
1028
1029             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1030                 (eh->fh.f_flags & 0x3000) == 0x3000)
1031             {
1032                 struct file * file;
1033                 unsigned long loader;
1034
1035                 allow_write_access(bprm->file);
1036                 fput(bprm->file);
1037                 bprm->file = NULL;
1038
1039                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1040
1041                 file = open_exec("/sbin/loader");
1042                 retval = PTR_ERR(file);
1043                 if (IS_ERR(file))
1044                         return retval;
1045
1046                 /* Remember if the application is TASO.  */
1047                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1048
1049                 bprm->file = file;
1050                 bprm->loader = loader;
1051                 retval = prepare_binprm(bprm);
1052                 if (retval<0)
1053                         return retval;
1054                 /* should call search_binary_handler recursively here,
1055                    but it does not matter */
1056             }
1057         }
1058 #endif
1059         retval = security_bprm_check(bprm);
1060         if (retval)
1061                 return retval;
1062
1063         /* kernel module loader fixup */
1064         /* so we don't try to load run modprobe in kernel space. */
1065         set_fs(USER_DS);
1066
1067         retval = audit_bprm(bprm);
1068         if (retval)
1069                 return retval;
1070
1071         retval = -ENOENT;
1072         for (try=0; try<2; try++) {
1073                 read_lock(&binfmt_lock);
1074                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1075                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1076                         if (!fn)
1077                                 continue;
1078                         if (!try_module_get(fmt->module))
1079                                 continue;
1080                         read_unlock(&binfmt_lock);
1081                         retval = fn(bprm, regs);
1082                         if (retval >= 0) {
1083                                 put_binfmt(fmt);
1084                                 allow_write_access(bprm->file);
1085                                 if (bprm->file)
1086                                         fput(bprm->file);
1087                                 bprm->file = NULL;
1088                                 current->did_exec = 1;
1089                                 proc_exec_connector(current);
1090                                 tracehook_report_exec(bprm, regs);
1091                                 return retval;
1092                         }
1093                         read_lock(&binfmt_lock);
1094                         put_binfmt(fmt);
1095                         if (retval != -ENOEXEC || bprm->mm == NULL)
1096                                 break;
1097                         if (!bprm->file) {
1098                                 read_unlock(&binfmt_lock);
1099                                 return retval;
1100                         }
1101                 }
1102                 read_unlock(&binfmt_lock);
1103                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1104                         break;
1105 #ifdef CONFIG_KMOD
1106                 }else{
1107 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1108                         if (printable(bprm->buf[0]) &&
1109                             printable(bprm->buf[1]) &&
1110                             printable(bprm->buf[2]) &&
1111                             printable(bprm->buf[3]))
1112                                 break; /* -ENOEXEC */
1113                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1114 #endif
1115                 }
1116         }
1117         return retval;
1118 }
1119
1120 EXPORT_SYMBOL(search_binary_handler);
1121
1122 /*
1123  * sys_execve() executes a new program.
1124  */
1125 int do_execve(char * filename,
1126         char __user *__user *argv,
1127         char __user *__user *envp,
1128         struct pt_regs * regs)
1129 {
1130         struct linux_binprm *bprm;
1131         struct file *file;
1132         int retval;
1133         int i;
1134
1135         retval = -ENOMEM;
1136         bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1137         if (!bprm)
1138                 goto out_ret;
1139
1140         file = open_exec(filename);
1141         retval = PTR_ERR(file);
1142         if (IS_ERR(file))
1143                 goto out_kfree;
1144
1145         sched_exec();
1146
1147         bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1148
1149         bprm->file = file;
1150         bprm->filename = filename;
1151         bprm->interp = filename;
1152         bprm->mm = mm_alloc();
1153         retval = -ENOMEM;
1154         if (!bprm->mm)
1155                 goto out_file;
1156
1157         retval = init_new_context(current, bprm->mm);
1158         if (retval < 0)
1159                 goto out_mm;
1160
1161         bprm->argc = count(argv, bprm->p / sizeof(void *));
1162         if ((retval = bprm->argc) < 0)
1163                 goto out_mm;
1164
1165         bprm->envc = count(envp, bprm->p / sizeof(void *));
1166         if ((retval = bprm->envc) < 0)
1167                 goto out_mm;
1168
1169         retval = security_bprm_alloc(bprm);
1170         if (retval)
1171                 goto out;
1172
1173         retval = prepare_binprm(bprm);
1174         if (retval < 0)
1175                 goto out;
1176
1177         retval = copy_strings_kernel(1, &bprm->filename, bprm);
1178         if (retval < 0)
1179                 goto out;
1180
1181         bprm->exec = bprm->p;
1182         retval = copy_strings(bprm->envc, envp, bprm);
1183         if (retval < 0)
1184                 goto out;
1185
1186         retval = copy_strings(bprm->argc, argv, bprm);
1187         if (retval < 0)
1188                 goto out;
1189
1190         retval = search_binary_handler(bprm,regs);
1191         if (retval >= 0) {
1192                 free_arg_pages(bprm);
1193
1194                 /* execve success */
1195                 security_bprm_free(bprm);
1196                 acct_update_integrals(current);
1197                 kfree(bprm);
1198                 return retval;
1199         }
1200
1201 out:
1202         /* Something went wrong, return the inode and free the argument pages*/
1203         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1204                 struct page * page = bprm->page[i];
1205                 if (page)
1206                         __free_page(page);
1207         }
1208
1209         if (bprm->security)
1210                 security_bprm_free(bprm);
1211
1212 out_mm:
1213         if (bprm->mm)
1214                 mmdrop(bprm->mm);
1215
1216 out_file:
1217         if (bprm->file) {
1218                 allow_write_access(bprm->file);
1219                 fput(bprm->file);
1220         }
1221
1222 out_kfree:
1223         kfree(bprm);
1224
1225 out_ret:
1226         return retval;
1227 }
1228
1229 int set_binfmt(struct linux_binfmt *new)
1230 {
1231         struct linux_binfmt *old = current->binfmt;
1232
1233         if (new) {
1234                 if (!try_module_get(new->module))
1235                         return -1;
1236         }
1237         current->binfmt = new;
1238         if (old)
1239                 module_put(old->module);
1240         return 0;
1241 }
1242
1243 EXPORT_SYMBOL(set_binfmt);
1244
1245 #define CORENAME_MAX_SIZE 64
1246
1247 /* format_corename will inspect the pattern parameter, and output a
1248  * name into corename, which must have space for at least
1249  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1250  */
1251 static void format_corename(char *corename, const char *pattern, long signr)
1252 {
1253         const char *pat_ptr = pattern;
1254         char *out_ptr = corename;
1255         char *const out_end = corename + CORENAME_MAX_SIZE;
1256         int rc;
1257         int pid_in_pattern = 0;
1258
1259         /* Repeat as long as we have more pattern to process and more output
1260            space */
1261         while (*pat_ptr) {
1262                 if (*pat_ptr != '%') {
1263                         if (out_ptr == out_end)
1264                                 goto out;
1265                         *out_ptr++ = *pat_ptr++;
1266                 } else {
1267                         switch (*++pat_ptr) {
1268                         case 0:
1269                                 goto out;
1270                         /* Double percent, output one percent */
1271                         case '%':
1272                                 if (out_ptr == out_end)
1273                                         goto out;
1274                                 *out_ptr++ = '%';
1275                                 break;
1276                         /* pid */
1277                         case 'p':
1278                                 pid_in_pattern = 1;
1279                                 rc = snprintf(out_ptr, out_end - out_ptr,
1280                                               "%d", current->tgid);
1281                                 if (rc > out_end - out_ptr)
1282                                         goto out;
1283                                 out_ptr += rc;
1284                                 break;
1285                         /* uid */
1286                         case 'u':
1287                                 rc = snprintf(out_ptr, out_end - out_ptr,
1288                                               "%d", current->uid);
1289                                 if (rc > out_end - out_ptr)
1290                                         goto out;
1291                                 out_ptr += rc;
1292                                 break;
1293                         /* gid */
1294                         case 'g':
1295                                 rc = snprintf(out_ptr, out_end - out_ptr,
1296                                               "%d", current->gid);
1297                                 if (rc > out_end - out_ptr)
1298                                         goto out;
1299                                 out_ptr += rc;
1300                                 break;
1301                         /* signal that caused the coredump */
1302                         case 's':
1303                                 rc = snprintf(out_ptr, out_end - out_ptr,
1304                                               "%ld", signr);
1305                                 if (rc > out_end - out_ptr)
1306                                         goto out;
1307                                 out_ptr += rc;
1308                                 break;
1309                         /* UNIX time of coredump */
1310                         case 't': {
1311                                 struct timeval tv;
1312                                 do_gettimeofday(&tv);
1313                                 rc = snprintf(out_ptr, out_end - out_ptr,
1314                                               "%lu", tv.tv_sec);
1315                                 if (rc > out_end - out_ptr)
1316                                         goto out;
1317                                 out_ptr += rc;
1318                                 break;
1319                         }
1320                         /* hostname */
1321                         case 'h':
1322                                 down_read(&uts_sem);
1323                                 rc = snprintf(out_ptr, out_end - out_ptr,
1324                                               "%s", vx_new_uts(nodename));
1325                                 up_read(&uts_sem);
1326                                 if (rc > out_end - out_ptr)
1327                                         goto out;
1328                                 out_ptr += rc;
1329                                 break;
1330                         /* executable */
1331                         case 'e':
1332                                 rc = snprintf(out_ptr, out_end - out_ptr,
1333                                               "%s", current->comm);
1334                                 if (rc > out_end - out_ptr)
1335                                         goto out;
1336                                 out_ptr += rc;
1337                                 break;
1338                         default:
1339                                 break;
1340                         }
1341                         ++pat_ptr;
1342                 }
1343         }
1344         /* Backward compatibility with core_uses_pid:
1345          *
1346          * If core_pattern does not include a %p (as is the default)
1347          * and core_uses_pid is set, then .%pid will be appended to
1348          * the filename */
1349         if (!pid_in_pattern
1350             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1351                 rc = snprintf(out_ptr, out_end - out_ptr,
1352                               ".%d", current->tgid);
1353                 if (rc > out_end - out_ptr)
1354                         goto out;
1355                 out_ptr += rc;
1356         }
1357       out:
1358         *out_ptr = 0;
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
1470         binfmt = current->binfmt;
1471         if (!binfmt || !binfmt->core_dump)
1472                 goto fail;
1473         if (current->tux_exit)
1474                 current->tux_exit();
1475         down_write(&mm->mmap_sem);
1476         if (!mm->dumpable) {
1477                 up_write(&mm->mmap_sem);
1478                 goto fail;
1479         }
1480
1481         /*
1482          *      We cannot trust fsuid as being the "true" uid of the
1483          *      process nor do we know its entire history. We only know it
1484          *      was tainted so we dump it as root in mode 2.
1485          */
1486         if (mm->dumpable == 2) {        /* Setuid core dump mode */
1487                 flag = O_EXCL;          /* Stop rewrite attacks */
1488                 current->fsuid = 0;     /* Dump root private */
1489         }
1490         mm->dumpable = 0;
1491
1492         retval = coredump_wait(exit_code);
1493         if (retval < 0)
1494                 goto fail;
1495
1496         /*
1497          * Clear any false indication of pending signals that might
1498          * be seen by the filesystem code called to write the core file.
1499          */
1500         clear_thread_flag(TIF_SIGPENDING);
1501
1502         if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1503                 goto fail_unlock;
1504
1505         /*
1506          * lock_kernel() because format_corename() is controlled by sysctl, which
1507          * uses lock_kernel()
1508          */
1509         lock_kernel();
1510         format_corename(corename, core_pattern, signr);
1511         unlock_kernel();
1512         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1513         if (IS_ERR(file))
1514                 goto fail_unlock;
1515         inode = file->f_dentry->d_inode;
1516         if (inode->i_nlink > 1)
1517                 goto close_fail;        /* multiple links - don't dump */
1518         if (d_unhashed(file->f_dentry))
1519                 goto close_fail;
1520
1521         if (!S_ISREG(inode->i_mode))
1522                 goto close_fail;
1523         if (!file->f_op)
1524                 goto close_fail;
1525         if (!file->f_op->write)
1526                 goto close_fail;
1527         if (do_truncate(file->f_dentry, 0, 0, file) != 0)
1528                 goto close_fail;
1529
1530         retval = binfmt->core_dump(signr, regs, file);
1531
1532         if (retval)
1533                 current->signal->group_exit_code |= 0x80;
1534 close_fail:
1535         filp_close(file, NULL);
1536 fail_unlock:
1537         current->fsuid = fsuid;
1538         complete_all(&mm->core_done);
1539 fail:
1540         return retval;
1541 }