d386912409c045bc237c4e5b8564fb8a19045e77
[linux-2.6.git] / fs / exec.c
1 /*
2  *  linux/fs/exec.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * #!-checking implemented by tytso.
9  */
10 /*
11  * Demand-loading implemented 01.12.91 - no need to read anything but
12  * the header into memory. The inode of the executable is put into
13  * "current->executable", and page faults do the actual loading. Clean.
14  *
15  * Once more I can proudly say that linux stood up to being changed: it
16  * was less than 2 hours work to get demand-loading completely implemented.
17  *
18  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
19  * current->executable is only used by the procfs.  This allows a dispatch
20  * table to check for several different types  of binary formats.  We keep
21  * trying until we recognize the file or we run out of supported binary
22  * formats. 
23  */
24
25 #include <linux/config.h>
26 #include <linux/slab.h>
27 #include <linux/file.h>
28 #include <linux/mman.h>
29 #include <linux/a.out.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/highmem.h>
36 #include <linux/spinlock.h>
37 #include <linux/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/module.h>
42 #include <linux/namei.h>
43 #include <linux/proc_fs.h>
44 #include <linux/ptrace.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/rmap.h>
49 #include <linux/ckrm.h>
50 #include <linux/vs_memory.h>
51
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
54
55 #ifdef CONFIG_KMOD
56 #include <linux/kmod.h>
57 #endif
58
59 int core_uses_pid;
60 char core_pattern[65] = "core";
61 /* The maximal length of core_pattern is also specified in sysctl.c */
62
63 static struct linux_binfmt *formats;
64 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
65
66 int register_binfmt(struct linux_binfmt * fmt)
67 {
68         struct linux_binfmt ** tmp = &formats;
69
70         if (!fmt)
71                 return -EINVAL;
72         if (fmt->next)
73                 return -EBUSY;
74         write_lock(&binfmt_lock);
75         while (*tmp) {
76                 if (fmt == *tmp) {
77                         write_unlock(&binfmt_lock);
78                         return -EBUSY;
79                 }
80                 tmp = &(*tmp)->next;
81         }
82         fmt->next = formats;
83         formats = fmt;
84         write_unlock(&binfmt_lock);
85         return 0;       
86 }
87
88 EXPORT_SYMBOL(register_binfmt);
89
90 int unregister_binfmt(struct linux_binfmt * fmt)
91 {
92         struct linux_binfmt ** tmp = &formats;
93
94         write_lock(&binfmt_lock);
95         while (*tmp) {
96                 if (fmt == *tmp) {
97                         *tmp = fmt->next;
98                         write_unlock(&binfmt_lock);
99                         return 0;
100                 }
101                 tmp = &(*tmp)->next;
102         }
103         write_unlock(&binfmt_lock);
104         return -EINVAL;
105 }
106
107 EXPORT_SYMBOL(unregister_binfmt);
108
109 static inline void put_binfmt(struct linux_binfmt * fmt)
110 {
111         module_put(fmt->module);
112 }
113
114 /*
115  * Note that a shared library must be both readable and executable due to
116  * security reasons.
117  *
118  * Also note that we take the address to load from from the file itself.
119  */
120 asmlinkage long sys_uselib(const char __user * library)
121 {
122         struct file * file;
123         struct nameidata nd;
124         int error;
125
126         nd.intent.open.flags = FMODE_READ;
127         error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
128         if (error)
129                 goto out;
130
131         error = -EINVAL;
132         if (!S_ISREG(nd.dentry->d_inode->i_mode))
133                 goto exit;
134
135         error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
136         if (error)
137                 goto exit;
138
139         file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
140         error = PTR_ERR(file);
141         if (IS_ERR(file))
142                 goto out;
143
144         error = -ENOEXEC;
145         if(file->f_op) {
146                 struct linux_binfmt * fmt;
147
148                 read_lock(&binfmt_lock);
149                 for (fmt = formats ; fmt ; fmt = fmt->next) {
150                         if (!fmt->load_shlib)
151                                 continue;
152                         if (!try_module_get(fmt->module))
153                                 continue;
154                         read_unlock(&binfmt_lock);
155                         error = fmt->load_shlib(file);
156                         read_lock(&binfmt_lock);
157                         put_binfmt(fmt);
158                         if (error != -ENOEXEC)
159                                 break;
160                 }
161                 read_unlock(&binfmt_lock);
162         }
163         fput(file);
164 out:
165         return error;
166 exit:
167         path_release(&nd);
168         goto out;
169 }
170
171 /*
172  * count() counts the number of strings in array ARGV.
173  */
174 static int count(char __user * __user * argv, int max)
175 {
176         int i = 0;
177
178         if (argv != NULL) {
179                 for (;;) {
180                         char __user * p;
181
182                         if (get_user(p, argv))
183                                 return -EFAULT;
184                         if (!p)
185                                 break;
186                         argv++;
187                         if(++i > max)
188                                 return -E2BIG;
189                 }
190         }
191         return i;
192 }
193
194 /*
195  * 'copy_strings()' copies argument/environment strings from user
196  * memory to free pages in kernel mem. These are in a format ready
197  * to be put directly into the top of new user memory.
198  */
199 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
200 {
201         struct page *kmapped_page = NULL;
202         char *kaddr = NULL;
203         int ret;
204
205         while (argc-- > 0) {
206                 char __user *str;
207                 int len;
208                 unsigned long pos;
209
210                 if (get_user(str, argv+argc) ||
211                                 !(len = strnlen_user(str, bprm->p))) {
212                         ret = -EFAULT;
213                         goto out;
214                 }
215
216                 if (bprm->p < len)  {
217                         ret = -E2BIG;
218                         goto out;
219                 }
220
221                 bprm->p -= len;
222                 /* XXX: add architecture specific overflow check here. */
223                 pos = bprm->p;
224
225                 while (len > 0) {
226                         int i, new, err;
227                         int offset, bytes_to_copy;
228                         struct page *page;
229
230                         offset = pos % PAGE_SIZE;
231                         i = pos/PAGE_SIZE;
232                         page = bprm->page[i];
233                         new = 0;
234                         if (!page) {
235                                 page = alloc_page(GFP_HIGHUSER);
236                                 bprm->page[i] = page;
237                                 if (!page) {
238                                         ret = -ENOMEM;
239                                         goto out;
240                                 }
241                                 new = 1;
242                         }
243
244                         if (page != kmapped_page) {
245                                 if (kmapped_page)
246                                         kunmap(kmapped_page);
247                                 kmapped_page = page;
248                                 kaddr = kmap(kmapped_page);
249                         }
250                         if (new && offset)
251                                 memset(kaddr, 0, offset);
252                         bytes_to_copy = PAGE_SIZE - offset;
253                         if (bytes_to_copy > len) {
254                                 bytes_to_copy = len;
255                                 if (new)
256                                         memset(kaddr+offset+len, 0,
257                                                 PAGE_SIZE-offset-len);
258                         }
259                         err = copy_from_user(kaddr+offset, str, bytes_to_copy);
260                         if (err) {
261                                 ret = -EFAULT;
262                                 goto out;
263                         }
264
265                         pos += bytes_to_copy;
266                         str += bytes_to_copy;
267                         len -= bytes_to_copy;
268                 }
269         }
270         ret = 0;
271 out:
272         if (kmapped_page)
273                 kunmap(kmapped_page);
274         return ret;
275 }
276
277 /*
278  * Like copy_strings, but get argv and its values from kernel memory.
279  */
280 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
281 {
282         int r;
283         mm_segment_t oldfs = get_fs();
284         set_fs(KERNEL_DS);
285         r = copy_strings(argc, (char __user * __user *)argv, bprm);
286         set_fs(oldfs);
287         return r;
288 }
289
290 EXPORT_SYMBOL(copy_strings_kernel);
291
292 #ifdef CONFIG_MMU
293 /*
294  * This routine is used to map in a page into an address space: needed by
295  * execve() for the initial stack and environment pages.
296  *
297  * vma->vm_mm->mmap_sem is held for writing.
298  */
299 void install_arg_page(struct vm_area_struct *vma,
300                         struct page *page, unsigned long address)
301 {
302         struct mm_struct *mm = vma->vm_mm;
303         pgd_t * pgd;
304         pmd_t * pmd;
305         pte_t * pte;
306
307         if (unlikely(anon_vma_prepare(vma)))
308                 goto out_sig;
309
310         flush_dcache_page(page);
311         pgd = pgd_offset(mm, address);
312
313         spin_lock(&mm->page_table_lock);
314         pmd = pmd_alloc(mm, pgd, address);
315         if (!pmd)
316                 goto out;
317         pte = pte_alloc_map(mm, pmd, address);
318         if (!pte)
319                 goto out;
320         if (!pte_none(*pte)) {
321                 pte_unmap(pte);
322                 goto out;
323         }
324         // mm->rss++;
325         vx_rsspages_inc(mm);
326         lru_cache_add_active(page);
327         set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(
328                                         page, vma->vm_page_prot))));
329         page_add_anon_rmap(page, vma, address);
330         pte_unmap(pte);
331         spin_unlock(&mm->page_table_lock);
332
333         /* no need for flush_tlb */
334         return;
335 out:
336         spin_unlock(&mm->page_table_lock);
337 out_sig:
338         __free_page(page);
339         force_sig(SIGKILL, current);
340 }
341
342 int setup_arg_pages(struct linux_binprm *bprm, int executable_stack)
343 {
344         unsigned long stack_base;
345         struct vm_area_struct *mpnt;
346         struct mm_struct *mm = current->mm;
347         int i;
348         long arg_size;
349
350 #ifdef CONFIG_STACK_GROWSUP
351         /* Move the argument and environment strings to the bottom of the
352          * stack space.
353          */
354         int offset, j;
355         char *to, *from;
356
357         /* Start by shifting all the pages down */
358         i = 0;
359         for (j = 0; j < MAX_ARG_PAGES; j++) {
360                 struct page *page = bprm->page[j];
361                 if (!page)
362                         continue;
363                 bprm->page[i++] = page;
364         }
365
366         /* Now move them within their pages */
367         offset = bprm->p % PAGE_SIZE;
368         to = kmap(bprm->page[0]);
369         for (j = 1; j < i; j++) {
370                 memmove(to, to + offset, PAGE_SIZE - offset);
371                 from = kmap(bprm->page[j]);
372                 memcpy(to + PAGE_SIZE - offset, from, offset);
373                 kunmap(bprm->page[j - 1]);
374                 to = from;
375         }
376         memmove(to, to + offset, PAGE_SIZE - offset);
377         kunmap(bprm->page[j - 1]);
378
379         /* Adjust bprm->p to point to the end of the strings. */
380         bprm->p = PAGE_SIZE * i - offset;
381
382         /* Limit stack size to 1GB */
383         stack_base = current->rlim[RLIMIT_STACK].rlim_max;
384         if (stack_base > (1 << 30))
385                 stack_base = 1 << 30;
386         stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
387
388         mm->arg_start = stack_base;
389         arg_size = i << PAGE_SHIFT;
390
391         /* zero pages that were copied above */
392         while (i < MAX_ARG_PAGES)
393                 bprm->page[i++] = NULL;
394 #else
395 #ifdef __HAVE_ARCH_ALIGN_STACK
396         stack_base = arch_align_stack(STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE);
397         stack_base = PAGE_ALIGN(stack_base);
398 #else
399         stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
400 #endif
401         mm->arg_start = bprm->p + stack_base;
402         arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
403 #endif
404
405         bprm->p += stack_base;
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         if (security_vm_enough_memory(arg_size >> PAGE_SHIFT) ||
415                 !vx_vmpages_avail(mm, arg_size >> PAGE_SHIFT)) {
416                 kmem_cache_free(vm_area_cachep, mpnt);
417                 return -ENOMEM;
418         }
419
420         memset(mpnt, 0, sizeof(*mpnt));
421
422         down_write(&mm->mmap_sem);
423         {
424                 mpnt->vm_mm = mm;
425 #ifdef CONFIG_STACK_GROWSUP
426                 mpnt->vm_start = stack_base;
427                 mpnt->vm_end = PAGE_MASK &
428                         (PAGE_SIZE - 1 + (unsigned long) bprm->p);
429 #else
430                 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
431                 mpnt->vm_end = STACK_TOP;
432 #endif
433                 /* Adjust stack execute permissions; explicitly enable
434                  * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
435                  * and leave alone (arch default) otherwise. */
436                 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
437                         mpnt->vm_flags = VM_STACK_FLAGS |  VM_EXEC;
438                 else if (executable_stack == EXSTACK_DISABLE_X)
439                         mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
440                 else
441                         mpnt->vm_flags = VM_STACK_FLAGS;
442                 mpnt->vm_flags |= mm->def_flags;
443                 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
444                 insert_vm_struct(mm, mpnt);
445                 // mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
446                 vx_vmpages_sub(mm, mm->total_vm -
447                         ((mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT));
448         }
449
450         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
451                 struct page *page = bprm->page[i];
452                 if (page) {
453                         bprm->page[i] = NULL;
454                         install_arg_page(mpnt, page, stack_base);
455                 }
456                 stack_base += PAGE_SIZE;
457         }
458         up_write(&mm->mmap_sem);
459         
460         return 0;
461 }
462
463 EXPORT_SYMBOL(setup_arg_pages);
464
465 #define free_arg_pages(bprm) do { } while (0)
466
467 #else
468
469 static inline void free_arg_pages(struct linux_binprm *bprm)
470 {
471         int i;
472
473         for (i = 0; i < MAX_ARG_PAGES; i++) {
474                 if (bprm->page[i])
475                         __free_page(bprm->page[i]);
476                 bprm->page[i] = NULL;
477         }
478 }
479
480 #endif /* CONFIG_MMU */
481
482 struct file *open_exec(const char *name)
483 {
484         struct nameidata nd;
485         int err;
486         struct file *file;
487
488         nd.intent.open.flags = FMODE_READ;
489         err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
490         file = ERR_PTR(err);
491
492         if (!err) {
493                 struct inode *inode = nd.dentry->d_inode;
494                 file = ERR_PTR(-EACCES);
495                 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
496                     S_ISREG(inode->i_mode)) {
497                         int err = permission(inode, MAY_EXEC, &nd);
498                         if (!err && !(inode->i_mode & 0111))
499                                 err = -EACCES;
500                         file = ERR_PTR(err);
501                         if (!err) {
502                                 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
503                                 if (!IS_ERR(file)) {
504                                         err = deny_write_access(file);
505                                         if (err) {
506                                                 fput(file);
507                                                 file = ERR_PTR(err);
508                                         }
509                                 }
510 out:
511                                 return file;
512                         }
513                 }
514                 path_release(&nd);
515         }
516         goto out;
517 }
518
519 EXPORT_SYMBOL(open_exec);
520
521 int kernel_read(struct file *file, unsigned long offset,
522         char *addr, unsigned long count)
523 {
524         mm_segment_t old_fs;
525         loff_t pos = offset;
526         int result;
527
528         old_fs = get_fs();
529         set_fs(get_ds());
530         /* The cast to a user pointer is valid due to the set_fs() */
531         result = vfs_read(file, (void __user *)addr, count, &pos);
532         set_fs(old_fs);
533         return result;
534 }
535
536 EXPORT_SYMBOL(kernel_read);
537
538 static int exec_mmap(struct mm_struct *mm)
539 {
540         struct task_struct *tsk;
541         struct mm_struct * old_mm, *active_mm;
542
543         /* Add it to the list of mm's */
544         spin_lock(&mmlist_lock);
545         list_add(&mm->mmlist, &init_mm.mmlist);
546         mmlist_nr++;
547         spin_unlock(&mmlist_lock);
548
549         /* Notify parent that we're no longer interested in the old VM */
550         tsk = current;
551         old_mm = current->mm;
552         mm_release(tsk, old_mm);
553
554         task_lock(tsk);
555         active_mm = tsk->active_mm;
556         tsk->mm = mm;
557         tsk->active_mm = mm;
558         activate_mm(active_mm, mm);
559         task_unlock(tsk);
560         if (old_mm) {
561                 if (active_mm != old_mm) BUG();
562                 mmput(old_mm);
563                 return 0;
564         }
565         mmdrop(active_mm);
566         return 0;
567 }
568
569 /*
570  * This function makes sure the current process has its own signal table,
571  * so that flush_signal_handlers can later reset the handlers without
572  * disturbing other processes.  (Other processes might share the signal
573  * table via the CLONE_SIGHAND option to clone().)
574  */
575 static inline int de_thread(struct task_struct *tsk)
576 {
577         struct signal_struct *newsig, *oldsig = tsk->signal;
578         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
579         spinlock_t *lock = &oldsighand->siglock;
580         int count;
581
582         /*
583          * If we don't share sighandlers, then we aren't sharing anything
584          * and we can just re-use it all.
585          */
586         if (atomic_read(&oldsighand->count) <= 1)
587                 return 0;
588
589         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
590         if (!newsighand)
591                 return -ENOMEM;
592
593         spin_lock_init(&newsighand->siglock);
594         atomic_set(&newsighand->count, 1);
595         memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
596
597         /*
598          * See if we need to allocate a new signal structure
599          */
600         newsig = NULL;
601         if (atomic_read(&oldsig->count) > 1) {
602                 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
603                 if (!newsig) {
604                         kmem_cache_free(sighand_cachep, newsighand);
605                         return -ENOMEM;
606                 }
607                 atomic_set(&newsig->count, 1);
608                 newsig->group_exit = 0;
609                 newsig->group_exit_code = 0;
610                 newsig->group_exit_task = NULL;
611                 newsig->group_stop_count = 0;
612                 newsig->curr_target = NULL;
613                 init_sigpending(&newsig->shared_pending);
614                 INIT_LIST_HEAD(&newsig->posix_timers);
615
616                 newsig->tty = oldsig->tty;
617                 newsig->pgrp = oldsig->pgrp;
618                 newsig->session = oldsig->session;
619                 newsig->leader = oldsig->leader;
620                 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
621         }
622
623         if (thread_group_empty(current))
624                 goto no_thread_group;
625
626         /*
627          * Kill all other threads in the thread group.
628          * We must hold tasklist_lock to call zap_other_threads.
629          */
630         read_lock(&tasklist_lock);
631         spin_lock_irq(lock);
632         if (oldsig->group_exit) {
633                 /*
634                  * Another group action in progress, just
635                  * return so that the signal is processed.
636                  */
637                 spin_unlock_irq(lock);
638                 read_unlock(&tasklist_lock);
639                 kmem_cache_free(sighand_cachep, newsighand);
640                 if (newsig)
641                         kmem_cache_free(signal_cachep, newsig);
642                 return -EAGAIN;
643         }
644         oldsig->group_exit = 1;
645         zap_other_threads(current);
646         read_unlock(&tasklist_lock);
647
648         /*
649          * Account for the thread group leader hanging around:
650          */
651         count = 2;
652         if (current->pid == current->tgid)
653                 count = 1;
654         while (atomic_read(&oldsig->count) > count) {
655                 oldsig->group_exit_task = current;
656                 oldsig->notify_count = count;
657                 __set_current_state(TASK_UNINTERRUPTIBLE);
658                 spin_unlock_irq(lock);
659                 schedule();
660                 spin_lock_irq(lock);
661         }
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 (current->pid != current->tgid) {
670                 struct task_struct *leader = current->group_leader, *parent;
671                 struct dentry *proc_dentry1, *proc_dentry2;
672                 unsigned long state, ptrace;
673
674                 /*
675                  * Wait for the thread group leader to be a zombie.
676                  * It should already be zombie at this point, most
677                  * of the time.
678                  */
679                 while (leader->state != TASK_ZOMBIE)
680                         yield();
681
682                 spin_lock(&leader->proc_lock);
683                 spin_lock(&current->proc_lock);
684                 proc_dentry1 = proc_pid_unhash(current);
685                 proc_dentry2 = proc_pid_unhash(leader);
686                 write_lock_irq(&tasklist_lock);
687
688                 if (leader->tgid != current->tgid)
689                         BUG();
690                 if (current->pid == current->tgid)
691                         BUG();
692                 /*
693                  * An exec() starts a new thread group with the
694                  * TGID of the previous thread group. Rehash the
695                  * two threads with a switched PID, and release
696                  * the former thread group leader:
697                  */
698                 ptrace = leader->ptrace;
699                 parent = leader->parent;
700
701                 ptrace_unlink(current);
702                 ptrace_unlink(leader);
703                 remove_parent(current);
704                 remove_parent(leader);
705
706                 switch_exec_pids(leader, current);
707
708                 current->parent = current->real_parent = leader->real_parent;
709                 leader->parent = leader->real_parent = child_reaper;
710                 current->group_leader = current;
711                 leader->group_leader = leader;
712
713                 add_parent(current, current->parent);
714                 add_parent(leader, leader->parent);
715                 if (ptrace) {
716                         current->ptrace = ptrace;
717                         __ptrace_link(current, parent);
718                 }
719
720                 list_del(&current->tasks);
721                 list_add_tail(&current->tasks, &init_task.tasks);
722                 current->exit_signal = SIGCHLD;
723                 state = leader->state;
724
725                 write_unlock_irq(&tasklist_lock);
726                 spin_unlock(&leader->proc_lock);
727                 spin_unlock(&current->proc_lock);
728                 proc_pid_flush(proc_dentry1);
729                 proc_pid_flush(proc_dentry2);
730
731                 if (state != TASK_ZOMBIE)
732                         BUG();
733                 release_task(leader);
734         }
735
736 no_thread_group:
737
738         write_lock_irq(&tasklist_lock);
739         spin_lock(&oldsighand->siglock);
740         spin_lock(&newsighand->siglock);
741
742         if (current == oldsig->curr_target)
743                 oldsig->curr_target = next_thread(current);
744         if (newsig)
745                 current->signal = newsig;
746         current->sighand = newsighand;
747         init_sigpending(&current->pending);
748         recalc_sigpending();
749
750         spin_unlock(&newsighand->siglock);
751         spin_unlock(&oldsighand->siglock);
752         write_unlock_irq(&tasklist_lock);
753
754         if (newsig && atomic_dec_and_test(&oldsig->count))
755                 kmem_cache_free(signal_cachep, oldsig);
756
757         if (atomic_dec_and_test(&oldsighand->count))
758                 kmem_cache_free(sighand_cachep, oldsighand);
759
760         if (!thread_group_empty(current))
761                 BUG();
762         if (current->tgid != current->pid)
763                 BUG();
764         return 0;
765 }
766         
767 /*
768  * These functions flushes out all traces of the currently running executable
769  * so that a new one can be started
770  */
771
772 static inline void flush_old_files(struct files_struct * files)
773 {
774         long j = -1;
775
776         spin_lock(&files->file_lock);
777         for (;;) {
778                 unsigned long set, i;
779
780                 j++;
781                 i = j * __NFDBITS;
782                 if (i >= files->max_fds || i >= files->max_fdset)
783                         break;
784                 set = files->close_on_exec->fds_bits[j];
785                 if (!set)
786                         continue;
787                 files->close_on_exec->fds_bits[j] = 0;
788                 spin_unlock(&files->file_lock);
789                 for ( ; set ; i++,set >>= 1) {
790                         if (set & 1) {
791                                 sys_close(i);
792                         }
793                 }
794                 spin_lock(&files->file_lock);
795
796         }
797         spin_unlock(&files->file_lock);
798 }
799
800 int flush_old_exec(struct linux_binprm * bprm)
801 {
802         char * name;
803         int i, ch, retval;
804         struct files_struct *files;
805
806         /*
807          * Make sure we have a private signal table and that
808          * we are unassociated from the previous thread group.
809          */
810         retval = de_thread(current);
811         if (retval)
812                 goto out;
813
814         /*
815          * Make sure we have private file handles. Ask the
816          * fork helper to do the work for us and the exit
817          * helper to do the cleanup of the old one.
818          */
819         files = current->files;         /* refcounted so safe to hold */
820         retval = unshare_files();
821         if (retval)
822                 goto out;
823         /*
824          * Release all of the old mmap stuff
825          */
826         retval = exec_mmap(bprm->mm);
827         if (retval)
828                 goto mmap_failed;
829
830         bprm->mm = NULL;                /* We're using it now */
831
832         /* This is the point of no return */
833         steal_locks(files);
834         put_files_struct(files);
835
836         current->sas_ss_sp = current->sas_ss_size = 0;
837
838         if (current->euid == current->uid && current->egid == current->gid)
839                 current->mm->dumpable = 1;
840         name = bprm->filename;
841         for (i=0; (ch = *(name++)) != '\0';) {
842                 if (ch == '/')
843                         i = 0;
844                 else
845                         if (i < 15)
846                                 current->comm[i++] = ch;
847         }
848         current->comm[i] = '\0';
849
850         current->flags &= ~PF_RELOCEXEC;
851         flush_thread();
852
853         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
854             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
855             (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP))
856                 current->mm->dumpable = 0;
857
858         /* An exec changes our domain. We are no longer part of the thread
859            group */
860
861         current->self_exec_id++;
862                         
863         flush_signal_handlers(current, 0);
864         flush_old_files(current->files);
865
866         return 0;
867
868 mmap_failed:
869         put_files_struct(current->files);
870         current->files = files;
871 out:
872         return retval;
873 }
874
875 EXPORT_SYMBOL(flush_old_exec);
876
877 /* 
878  * Fill the binprm structure from the inode. 
879  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
880  */
881 int prepare_binprm(struct linux_binprm *bprm)
882 {
883         int mode;
884         struct inode * inode = bprm->file->f_dentry->d_inode;
885         int retval;
886
887         mode = inode->i_mode;
888         /*
889          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
890          * vfs_permission lets a non-executable through
891          */
892         if (!(mode & 0111))     /* with at least _one_ execute bit set */
893                 return -EACCES;
894         if (bprm->file->f_op == NULL)
895                 return -EACCES;
896
897         bprm->e_uid = current->euid;
898         bprm->e_gid = current->egid;
899
900         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
901                 /* Set-uid? */
902                 if (mode & S_ISUID) {
903                         bprm->e_uid = inode->i_uid;
904 #ifdef __i386__
905                         /* reset personality */
906                         current->personality = PER_LINUX;
907 #endif
908                 }
909
910                 /* Set-gid? */
911                 /*
912                  * If setgid is set but no group execute bit then this
913                  * is a candidate for mandatory locking, not a setgid
914                  * executable.
915                  */
916                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
917                         bprm->e_gid = inode->i_gid;
918 #ifdef __i386__
919                         /* reset personality */
920                         current->personality = PER_LINUX;
921 #endif
922                 }
923         }
924
925         /* fill in binprm security blob */
926         retval = security_bprm_set(bprm);
927         if (retval)
928                 return retval;
929
930         memset(bprm->buf,0,BINPRM_BUF_SIZE);
931         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
932 }
933
934 EXPORT_SYMBOL(prepare_binprm);
935
936 static inline int unsafe_exec(struct task_struct *p)
937 {
938         int unsafe = 0;
939         if (p->ptrace & PT_PTRACED) {
940                 if (p->ptrace & PT_PTRACE_CAP)
941                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
942                 else
943                         unsafe |= LSM_UNSAFE_PTRACE;
944         }
945         if (atomic_read(&p->fs->count) > 1 ||
946             atomic_read(&p->files->count) > 1 ||
947             atomic_read(&p->sighand->count) > 1)
948                 unsafe |= LSM_UNSAFE_SHARE;
949
950         return unsafe;
951 }
952
953 void compute_creds(struct linux_binprm *bprm)
954 {
955         int unsafe;
956         task_lock(current);
957         unsafe = unsafe_exec(current);
958         security_bprm_apply_creds(bprm, unsafe);
959         task_unlock(current);
960 }
961
962 EXPORT_SYMBOL(compute_creds);
963
964 void remove_arg_zero(struct linux_binprm *bprm)
965 {
966         if (bprm->argc) {
967                 unsigned long offset;
968                 char * kaddr;
969                 struct page *page;
970
971                 offset = bprm->p % PAGE_SIZE;
972                 goto inside;
973
974                 while (bprm->p++, *(kaddr+offset++)) {
975                         if (offset != PAGE_SIZE)
976                                 continue;
977                         offset = 0;
978                         kunmap_atomic(kaddr, KM_USER0);
979 inside:
980                         page = bprm->page[bprm->p/PAGE_SIZE];
981                         kaddr = kmap_atomic(page, KM_USER0);
982                 }
983                 kunmap_atomic(kaddr, KM_USER0);
984                 bprm->argc--;
985         }
986 }
987
988 EXPORT_SYMBOL(remove_arg_zero);
989
990 /*
991  * cycle the list of binary formats handler, until one recognizes the image
992  */
993 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
994 {
995         int try,retval=0;
996         struct linux_binfmt *fmt;
997 #ifdef __alpha__
998         /* handle /sbin/loader.. */
999         {
1000             struct exec * eh = (struct exec *) bprm->buf;
1001
1002             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1003                 (eh->fh.f_flags & 0x3000) == 0x3000)
1004             {
1005                 struct file * file;
1006                 unsigned long loader;
1007
1008                 allow_write_access(bprm->file);
1009                 fput(bprm->file);
1010                 bprm->file = NULL;
1011
1012                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1013
1014                 file = open_exec("/sbin/loader");
1015                 retval = PTR_ERR(file);
1016                 if (IS_ERR(file))
1017                         return retval;
1018
1019                 /* Remember if the application is TASO.  */
1020                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1021
1022                 bprm->file = file;
1023                 bprm->loader = loader;
1024                 retval = prepare_binprm(bprm);
1025                 if (retval<0)
1026                         return retval;
1027                 /* should call search_binary_handler recursively here,
1028                    but it does not matter */
1029             }
1030         }
1031 #endif
1032         retval = security_bprm_check(bprm);
1033         if (retval)
1034                 return retval;
1035
1036         /* kernel module loader fixup */
1037         /* so we don't try to load run modprobe in kernel space. */
1038         set_fs(USER_DS);
1039         for (try=0; try<2; try++) {
1040                 read_lock(&binfmt_lock);
1041                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1042                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1043                         if (!fn)
1044                                 continue;
1045                         if (!try_module_get(fmt->module))
1046                                 continue;
1047                         read_unlock(&binfmt_lock);
1048                         retval = fn(bprm, regs);
1049                         if (retval >= 0) {
1050                                 put_binfmt(fmt);
1051                                 allow_write_access(bprm->file);
1052                                 if (bprm->file)
1053                                         fput(bprm->file);
1054                                 bprm->file = NULL;
1055                                 current->did_exec = 1;
1056                                 ckrm_cb_exec(bprm->filename);
1057                                 return retval;
1058                         }
1059                         read_lock(&binfmt_lock);
1060                         put_binfmt(fmt);
1061                         if (retval != -ENOEXEC || bprm->mm == NULL)
1062                                 break;
1063                         if (!bprm->file) {
1064                                 read_unlock(&binfmt_lock);
1065                                 return retval;
1066                         }
1067                 }
1068                 read_unlock(&binfmt_lock);
1069                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1070                         break;
1071 #ifdef CONFIG_KMOD
1072                 }else{
1073 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1074                         if (printable(bprm->buf[0]) &&
1075                             printable(bprm->buf[1]) &&
1076                             printable(bprm->buf[2]) &&
1077                             printable(bprm->buf[3]))
1078                                 break; /* -ENOEXEC */
1079                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1080 #endif
1081                 }
1082         }
1083         return retval;
1084 }
1085
1086 EXPORT_SYMBOL(search_binary_handler);
1087
1088 /*
1089  * sys_execve() executes a new program.
1090  */
1091 int do_execve(char * filename,
1092         char __user *__user *argv,
1093         char __user *__user *envp,
1094         struct pt_regs * regs)
1095 {
1096         struct linux_binprm bprm;
1097         struct file *file;
1098         int retval;
1099         int i;
1100
1101         file = open_exec(filename);
1102
1103         retval = PTR_ERR(file);
1104         if (IS_ERR(file))
1105                 return retval;
1106
1107         sched_balance_exec();
1108
1109         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1110         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1111
1112         bprm.file = file;
1113         bprm.filename = filename;
1114         bprm.interp = filename;
1115         bprm.interp_flags = 0;
1116         bprm.interp_data = 0;
1117         bprm.sh_bang = 0;
1118         bprm.loader = 0;
1119         bprm.exec = 0;
1120         bprm.security = NULL;
1121         bprm.mm = mm_alloc();
1122         retval = -ENOMEM;
1123         if (!bprm.mm)
1124                 goto out_file;
1125
1126         retval = init_new_context(current, bprm.mm);
1127         if (retval < 0)
1128                 goto out_mm;
1129
1130         bprm.argc = count(argv, bprm.p / sizeof(void *));
1131         if ((retval = bprm.argc) < 0)
1132                 goto out_mm;
1133
1134         bprm.envc = count(envp, bprm.p / sizeof(void *));
1135         if ((retval = bprm.envc) < 0)
1136                 goto out_mm;
1137
1138         retval = security_bprm_alloc(&bprm);
1139         if (retval)
1140                 goto out;
1141
1142         retval = prepare_binprm(&bprm);
1143         if (retval < 0)
1144                 goto out;
1145
1146         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1147         if (retval < 0)
1148                 goto out;
1149
1150         bprm.exec = bprm.p;
1151         retval = copy_strings(bprm.envc, envp, &bprm);
1152         if (retval < 0)
1153                 goto out;
1154
1155         retval = copy_strings(bprm.argc, argv, &bprm);
1156         if (retval < 0)
1157                 goto out;
1158
1159         retval = search_binary_handler(&bprm,regs);
1160         if (retval >= 0) {
1161                 free_arg_pages(&bprm);
1162
1163                 /* execve success */
1164                 security_bprm_free(&bprm);
1165                 return retval;
1166         }
1167
1168 out:
1169         /* Something went wrong, return the inode and free the argument pages*/
1170         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1171                 struct page * page = bprm.page[i];
1172                 if (page)
1173                         __free_page(page);
1174         }
1175
1176         if (bprm.security)
1177                 security_bprm_free(&bprm);
1178
1179 out_mm:
1180         if (bprm.mm)
1181                 mmdrop(bprm.mm);
1182
1183 out_file:
1184         if (bprm.file) {
1185                 allow_write_access(bprm.file);
1186                 fput(bprm.file);
1187         }
1188         return retval;
1189 }
1190
1191 EXPORT_SYMBOL(do_execve);
1192
1193 int set_binfmt(struct linux_binfmt *new)
1194 {
1195         struct linux_binfmt *old = current->binfmt;
1196
1197         if (new) {
1198                 if (!try_module_get(new->module))
1199                         return -1;
1200         }
1201         current->binfmt = new;
1202         if (old)
1203                 module_put(old->module);
1204         return 0;
1205 }
1206
1207 EXPORT_SYMBOL(set_binfmt);
1208
1209 #define CORENAME_MAX_SIZE 64
1210
1211 /* format_corename will inspect the pattern parameter, and output a
1212  * name into corename, which must have space for at least
1213  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1214  */
1215 void format_corename(char *corename, const char *pattern, long signr)
1216 {
1217         const char *pat_ptr = pattern;
1218         char *out_ptr = corename;
1219         char *const out_end = corename + CORENAME_MAX_SIZE;
1220         int rc;
1221         int pid_in_pattern = 0;
1222
1223         /* Repeat as long as we have more pattern to process and more output
1224            space */
1225         while (*pat_ptr) {
1226                 if (*pat_ptr != '%') {
1227                         if (out_ptr == out_end)
1228                                 goto out;
1229                         *out_ptr++ = *pat_ptr++;
1230                 } else {
1231                         switch (*++pat_ptr) {
1232                         case 0:
1233                                 goto out;
1234                         /* Double percent, output one percent */
1235                         case '%':
1236                                 if (out_ptr == out_end)
1237                                         goto out;
1238                                 *out_ptr++ = '%';
1239                                 break;
1240                         /* pid */
1241                         case 'p':
1242                                 pid_in_pattern = 1;
1243                                 rc = snprintf(out_ptr, out_end - out_ptr,
1244                                               "%d", current->tgid);
1245                                 if (rc > out_end - out_ptr)
1246                                         goto out;
1247                                 out_ptr += rc;
1248                                 break;
1249                         /* uid */
1250                         case 'u':
1251                                 rc = snprintf(out_ptr, out_end - out_ptr,
1252                                               "%d", current->uid);
1253                                 if (rc > out_end - out_ptr)
1254                                         goto out;
1255                                 out_ptr += rc;
1256                                 break;
1257                         /* gid */
1258                         case 'g':
1259                                 rc = snprintf(out_ptr, out_end - out_ptr,
1260                                               "%d", current->gid);
1261                                 if (rc > out_end - out_ptr)
1262                                         goto out;
1263                                 out_ptr += rc;
1264                                 break;
1265                         /* signal that caused the coredump */
1266                         case 's':
1267                                 rc = snprintf(out_ptr, out_end - out_ptr,
1268                                               "%ld", signr);
1269                                 if (rc > out_end - out_ptr)
1270                                         goto out;
1271                                 out_ptr += rc;
1272                                 break;
1273                         /* UNIX time of coredump */
1274                         case 't': {
1275                                 struct timeval tv;
1276                                 do_gettimeofday(&tv);
1277                                 rc = snprintf(out_ptr, out_end - out_ptr,
1278                                               "%lu", tv.tv_sec);
1279                                 if (rc > out_end - out_ptr)
1280                                         goto out;
1281                                 out_ptr += rc;
1282                                 break;
1283                         }
1284                         /* hostname */
1285                         case 'h':
1286                                 down_read(&uts_sem);
1287                                 rc = snprintf(out_ptr, out_end - out_ptr,
1288                                               "%s", system_utsname.nodename);
1289                                 up_read(&uts_sem);
1290                                 if (rc > out_end - out_ptr)
1291                                         goto out;
1292                                 out_ptr += rc;
1293                                 break;
1294                         /* executable */
1295                         case 'e':
1296                                 rc = snprintf(out_ptr, out_end - out_ptr,
1297                                               "%s", current->comm);
1298                                 if (rc > out_end - out_ptr)
1299                                         goto out;
1300                                 out_ptr += rc;
1301                                 break;
1302                         default:
1303                                 break;
1304                         }
1305                         ++pat_ptr;
1306                 }
1307         }
1308         /* Backward compatibility with core_uses_pid:
1309          *
1310          * If core_pattern does not include a %p (as is the default)
1311          * and core_uses_pid is set, then .%pid will be appended to
1312          * the filename */
1313         if (!pid_in_pattern
1314             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1315                 rc = snprintf(out_ptr, out_end - out_ptr,
1316                               ".%d", current->tgid);
1317                 if (rc > out_end - out_ptr)
1318                         goto out;
1319                 out_ptr += rc;
1320         }
1321       out:
1322         *out_ptr = 0;
1323 }
1324
1325 static void zap_threads (struct mm_struct *mm)
1326 {
1327         struct task_struct *g, *p;
1328         struct task_struct *tsk = current;
1329         struct completion *vfork_done = tsk->vfork_done;
1330
1331         /*
1332          * Make sure nobody is waiting for us to release the VM,
1333          * otherwise we can deadlock when we wait on each other
1334          */
1335         if (vfork_done) {
1336                 tsk->vfork_done = NULL;
1337                 complete(vfork_done);
1338         }
1339
1340         read_lock(&tasklist_lock);
1341         do_each_thread(g,p)
1342                 if (mm == p->mm && p != tsk) {
1343                         force_sig_specific(SIGKILL, p);
1344                         mm->core_waiters++;
1345                 }
1346         while_each_thread(g,p);
1347
1348         read_unlock(&tasklist_lock);
1349 }
1350
1351 static void coredump_wait(struct mm_struct *mm)
1352 {
1353         DECLARE_COMPLETION(startup_done);
1354
1355         mm->core_waiters++; /* let other threads block */
1356         mm->core_startup_done = &startup_done;
1357
1358         /* give other threads a chance to run: */
1359         yield();
1360
1361         zap_threads(mm);
1362         if (--mm->core_waiters) {
1363                 up_write(&mm->mmap_sem);
1364                 wait_for_completion(&startup_done);
1365         } else
1366                 up_write(&mm->mmap_sem);
1367         BUG_ON(mm->core_waiters);
1368 }
1369
1370 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1371 {
1372         char corename[CORENAME_MAX_SIZE + 1];
1373         struct mm_struct *mm = current->mm;
1374         struct linux_binfmt * binfmt;
1375         struct inode * inode;
1376         struct file * file;
1377         int retval = 0;
1378
1379         lock_kernel();
1380         binfmt = current->binfmt;
1381         if (!binfmt || !binfmt->core_dump)
1382                 goto fail;
1383         down_write(&mm->mmap_sem);
1384         if (!mm->dumpable) {
1385                 up_write(&mm->mmap_sem);
1386                 goto fail;
1387         }
1388         mm->dumpable = 0;
1389         init_completion(&mm->core_done);
1390         current->signal->group_exit = 1;
1391         current->signal->group_exit_code = exit_code;
1392         coredump_wait(mm);
1393
1394         if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1395                 goto fail_unlock;
1396
1397         format_corename(corename, core_pattern, signr);
1398         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1399         if (IS_ERR(file))
1400                 goto fail_unlock;
1401         inode = file->f_dentry->d_inode;
1402         if (inode->i_nlink > 1)
1403                 goto close_fail;        /* multiple links - don't dump */
1404         if (d_unhashed(file->f_dentry))
1405                 goto close_fail;
1406
1407         if (!S_ISREG(inode->i_mode))
1408                 goto close_fail;
1409         if (!file->f_op)
1410                 goto close_fail;
1411         if (!file->f_op->write)
1412                 goto close_fail;
1413         if (do_truncate(file->f_dentry, 0) != 0)
1414                 goto close_fail;
1415
1416         retval = binfmt->core_dump(signr, regs, file);
1417
1418         current->signal->group_exit_code |= 0x80;
1419 close_fail:
1420         filp_close(file, NULL);
1421 fail_unlock:
1422         complete_all(&mm->core_done);
1423 fail:
1424         unlock_kernel();
1425         return retval;
1426 }