Merge to VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / exec.c
1 /*
2  *  linux/fs/exec.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * #!-checking implemented by tytso.
9  */
10 /*
11  * Demand-loading implemented 01.12.91 - no need to read anything but
12  * the header into memory. The inode of the executable is put into
13  * "current->executable", and page faults do the actual loading. Clean.
14  *
15  * Once more I can proudly say that linux stood up to being changed: it
16  * was less than 2 hours work to get demand-loading completely implemented.
17  *
18  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
19  * current->executable is only used by the procfs.  This allows a dispatch
20  * table to check for several different types  of binary formats.  We keep
21  * trying until we recognize the file or we run out of supported binary
22  * formats. 
23  */
24
25 #include <linux/config.h>
26 #include <linux/slab.h>
27 #include <linux/file.h>
28 #include <linux/mman.h>
29 #include <linux/a.out.h>
30 #include <linux/stat.h>
31 #include <linux/fcntl.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <linux/pagemap.h>
35 #include <linux/highmem.h>
36 #include <linux/spinlock.h>
37 #include <linux/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         arch_pick_mmap_layout(mm);
561         if (old_mm) {
562                 if (active_mm != old_mm) BUG();
563                 mmput(old_mm);
564                 return 0;
565         }
566         mmdrop(active_mm);
567         return 0;
568 }
569
570 /*
571  * This function makes sure the current process has its own signal table,
572  * so that flush_signal_handlers can later reset the handlers without
573  * disturbing other processes.  (Other processes might share the signal
574  * table via the CLONE_SIGHAND option to clone().)
575  */
576 static inline int de_thread(struct task_struct *tsk)
577 {
578         struct signal_struct *newsig, *oldsig = tsk->signal;
579         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
580         spinlock_t *lock = &oldsighand->siglock;
581         int count;
582
583         /*
584          * If we don't share sighandlers, then we aren't sharing anything
585          * and we can just re-use it all.
586          */
587         if (atomic_read(&oldsighand->count) <= 1)
588                 return 0;
589
590         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
591         if (!newsighand)
592                 return -ENOMEM;
593
594         spin_lock_init(&newsighand->siglock);
595         atomic_set(&newsighand->count, 1);
596         memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
597
598         /*
599          * See if we need to allocate a new signal structure
600          */
601         newsig = NULL;
602         if (atomic_read(&oldsig->count) > 1) {
603                 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
604                 if (!newsig) {
605                         kmem_cache_free(sighand_cachep, newsighand);
606                         return -ENOMEM;
607                 }
608                 atomic_set(&newsig->count, 1);
609                 newsig->group_exit = 0;
610                 newsig->group_exit_code = 0;
611                 newsig->group_exit_task = NULL;
612                 newsig->group_stop_count = 0;
613                 newsig->curr_target = NULL;
614                 init_sigpending(&newsig->shared_pending);
615                 INIT_LIST_HEAD(&newsig->posix_timers);
616
617                 newsig->tty = oldsig->tty;
618                 newsig->pgrp = oldsig->pgrp;
619                 newsig->session = oldsig->session;
620                 newsig->leader = oldsig->leader;
621                 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
622         }
623
624         if (thread_group_empty(current))
625                 goto no_thread_group;
626
627         /*
628          * Kill all other threads in the thread group.
629          * We must hold tasklist_lock to call zap_other_threads.
630          */
631         read_lock(&tasklist_lock);
632         spin_lock_irq(lock);
633         if (oldsig->group_exit) {
634                 /*
635                  * Another group action in progress, just
636                  * return so that the signal is processed.
637                  */
638                 spin_unlock_irq(lock);
639                 read_unlock(&tasklist_lock);
640                 kmem_cache_free(sighand_cachep, newsighand);
641                 if (newsig)
642                         kmem_cache_free(signal_cachep, newsig);
643                 return -EAGAIN;
644         }
645         oldsig->group_exit = 1;
646         zap_other_threads(current);
647         read_unlock(&tasklist_lock);
648
649         /*
650          * Account for the thread group leader hanging around:
651          */
652         count = 2;
653         if (current->pid == current->tgid)
654                 count = 1;
655         while (atomic_read(&oldsig->count) > count) {
656                 oldsig->group_exit_task = current;
657                 oldsig->notify_count = count;
658                 __set_current_state(TASK_UNINTERRUPTIBLE);
659                 spin_unlock_irq(lock);
660                 schedule();
661                 spin_lock_irq(lock);
662         }
663         spin_unlock_irq(lock);
664
665         /*
666          * At this point all other threads have exited, all we have to
667          * do is to wait for the thread group leader to become inactive,
668          * and to assume its PID:
669          */
670         if (current->pid != current->tgid) {
671                 struct task_struct *leader = current->group_leader, *parent;
672                 struct dentry *proc_dentry1, *proc_dentry2;
673                 unsigned long state, ptrace;
674
675                 /*
676                  * Wait for the thread group leader to be a zombie.
677                  * It should already be zombie at this point, most
678                  * of the time.
679                  */
680                 while (leader->state != TASK_ZOMBIE)
681                         yield();
682
683                 spin_lock(&leader->proc_lock);
684                 spin_lock(&current->proc_lock);
685                 proc_dentry1 = proc_pid_unhash(current);
686                 proc_dentry2 = proc_pid_unhash(leader);
687                 write_lock_irq(&tasklist_lock);
688
689                 if (leader->tgid != current->tgid)
690                         BUG();
691                 if (current->pid == current->tgid)
692                         BUG();
693                 /*
694                  * An exec() starts a new thread group with the
695                  * TGID of the previous thread group. Rehash the
696                  * two threads with a switched PID, and release
697                  * the former thread group leader:
698                  */
699                 ptrace = leader->ptrace;
700                 parent = leader->parent;
701
702                 ptrace_unlink(current);
703                 ptrace_unlink(leader);
704                 remove_parent(current);
705                 remove_parent(leader);
706
707                 switch_exec_pids(leader, current);
708
709                 current->parent = current->real_parent = leader->real_parent;
710                 leader->parent = leader->real_parent = child_reaper;
711                 current->group_leader = current;
712                 leader->group_leader = leader;
713
714                 add_parent(current, current->parent);
715                 add_parent(leader, leader->parent);
716                 if (ptrace) {
717                         current->ptrace = ptrace;
718                         __ptrace_link(current, parent);
719                 }
720
721                 list_del(&current->tasks);
722                 list_add_tail(&current->tasks, &init_task.tasks);
723                 current->exit_signal = SIGCHLD;
724                 state = leader->state;
725
726                 write_unlock_irq(&tasklist_lock);
727                 spin_unlock(&leader->proc_lock);
728                 spin_unlock(&current->proc_lock);
729                 proc_pid_flush(proc_dentry1);
730                 proc_pid_flush(proc_dentry2);
731
732                 if (state != TASK_ZOMBIE)
733                         BUG();
734                 release_task(leader);
735         }
736
737 no_thread_group:
738
739         write_lock_irq(&tasklist_lock);
740         spin_lock(&oldsighand->siglock);
741         spin_lock(&newsighand->siglock);
742
743         if (current == oldsig->curr_target)
744                 oldsig->curr_target = next_thread(current);
745         if (newsig)
746                 current->signal = newsig;
747         current->sighand = newsighand;
748         init_sigpending(&current->pending);
749         recalc_sigpending();
750
751         spin_unlock(&newsighand->siglock);
752         spin_unlock(&oldsighand->siglock);
753         write_unlock_irq(&tasklist_lock);
754
755         if (newsig && atomic_dec_and_test(&oldsig->count))
756                 kmem_cache_free(signal_cachep, oldsig);
757
758         if (atomic_dec_and_test(&oldsighand->count))
759                 kmem_cache_free(sighand_cachep, oldsighand);
760
761         if (!thread_group_empty(current))
762                 BUG();
763         if (current->tgid != current->pid)
764                 BUG();
765         return 0;
766 }
767         
768 /*
769  * These functions flushes out all traces of the currently running executable
770  * so that a new one can be started
771  */
772
773 static inline void flush_old_files(struct files_struct * files)
774 {
775         long j = -1;
776
777         spin_lock(&files->file_lock);
778         for (;;) {
779                 unsigned long set, i;
780
781                 j++;
782                 i = j * __NFDBITS;
783                 if (i >= files->max_fds || i >= files->max_fdset)
784                         break;
785                 set = files->close_on_exec->fds_bits[j];
786                 if (!set)
787                         continue;
788                 files->close_on_exec->fds_bits[j] = 0;
789                 spin_unlock(&files->file_lock);
790                 for ( ; set ; i++,set >>= 1) {
791                         if (set & 1) {
792                                 sys_close(i);
793                         }
794                 }
795                 spin_lock(&files->file_lock);
796
797         }
798         spin_unlock(&files->file_lock);
799 }
800
801 int flush_old_exec(struct linux_binprm * bprm)
802 {
803         char * name;
804         int i, ch, retval;
805         struct files_struct *files;
806
807         /*
808          * Make sure we have a private signal table and that
809          * we are unassociated from the previous thread group.
810          */
811         retval = de_thread(current);
812         if (retval)
813                 goto out;
814
815         /*
816          * Make sure we have private file handles. Ask the
817          * fork helper to do the work for us and the exit
818          * helper to do the cleanup of the old one.
819          */
820         files = current->files;         /* refcounted so safe to hold */
821         retval = unshare_files();
822         if (retval)
823                 goto out;
824         /*
825          * Release all of the old mmap stuff
826          */
827         retval = exec_mmap(bprm->mm);
828         if (retval)
829                 goto mmap_failed;
830
831         bprm->mm = NULL;                /* We're using it now */
832
833         /* This is the point of no return */
834         steal_locks(files);
835         put_files_struct(files);
836
837         current->sas_ss_sp = current->sas_ss_size = 0;
838
839         if (current->euid == current->uid && current->egid == current->gid)
840                 current->mm->dumpable = 1;
841         name = bprm->filename;
842         for (i=0; (ch = *(name++)) != '\0';) {
843                 if (ch == '/')
844                         i = 0;
845                 else
846                         if (i < 15)
847                                 current->comm[i++] = ch;
848         }
849         current->comm[i] = '\0';
850
851         current->flags &= ~PF_RELOCEXEC;
852         flush_thread();
853
854         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
855             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
856             (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP))
857                 current->mm->dumpable = 0;
858
859         /* An exec changes our domain. We are no longer part of the thread
860            group */
861
862         current->self_exec_id++;
863                         
864         flush_signal_handlers(current, 0);
865         flush_old_files(current->files);
866
867         return 0;
868
869 mmap_failed:
870         put_files_struct(current->files);
871         current->files = files;
872 out:
873         return retval;
874 }
875
876 EXPORT_SYMBOL(flush_old_exec);
877
878 /* 
879  * Fill the binprm structure from the inode. 
880  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
881  */
882 int prepare_binprm(struct linux_binprm *bprm)
883 {
884         int mode;
885         struct inode * inode = bprm->file->f_dentry->d_inode;
886         int retval;
887
888         mode = inode->i_mode;
889         /*
890          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
891          * vfs_permission lets a non-executable through
892          */
893         if (!(mode & 0111))     /* with at least _one_ execute bit set */
894                 return -EACCES;
895         if (bprm->file->f_op == NULL)
896                 return -EACCES;
897
898         bprm->e_uid = current->euid;
899         bprm->e_gid = current->egid;
900
901         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
902                 /* Set-uid? */
903                 if (mode & S_ISUID) {
904                         current->personality &= ~PER_CLEAR_ON_SETID;
905                         bprm->e_uid = inode->i_uid;
906                 }
907
908                 /* Set-gid? */
909                 /*
910                  * If setgid is set but no group execute bit then this
911                  * is a candidate for mandatory locking, not a setgid
912                  * executable.
913                  */
914                 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
915                         current->personality &= ~PER_CLEAR_ON_SETID;
916                         bprm->e_gid = inode->i_gid;
917                 }
918         }
919
920         /* fill in binprm security blob */
921         retval = security_bprm_set(bprm);
922         if (retval)
923                 return retval;
924
925         memset(bprm->buf,0,BINPRM_BUF_SIZE);
926         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
927 }
928
929 EXPORT_SYMBOL(prepare_binprm);
930
931 static inline int unsafe_exec(struct task_struct *p)
932 {
933         int unsafe = 0;
934         if (p->ptrace & PT_PTRACED) {
935                 if (p->ptrace & PT_PTRACE_CAP)
936                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
937                 else
938                         unsafe |= LSM_UNSAFE_PTRACE;
939         }
940         if (atomic_read(&p->fs->count) > 1 ||
941             atomic_read(&p->files->count) > 1 ||
942             atomic_read(&p->sighand->count) > 1)
943                 unsafe |= LSM_UNSAFE_SHARE;
944
945         return unsafe;
946 }
947
948 void compute_creds(struct linux_binprm *bprm)
949 {
950         int unsafe;
951         task_lock(current);
952         unsafe = unsafe_exec(current);
953         security_bprm_apply_creds(bprm, unsafe);
954         task_unlock(current);
955 }
956
957 EXPORT_SYMBOL(compute_creds);
958
959 void remove_arg_zero(struct linux_binprm *bprm)
960 {
961         if (bprm->argc) {
962                 unsigned long offset;
963                 char * kaddr;
964                 struct page *page;
965
966                 offset = bprm->p % PAGE_SIZE;
967                 goto inside;
968
969                 while (bprm->p++, *(kaddr+offset++)) {
970                         if (offset != PAGE_SIZE)
971                                 continue;
972                         offset = 0;
973                         kunmap_atomic(kaddr, KM_USER0);
974 inside:
975                         page = bprm->page[bprm->p/PAGE_SIZE];
976                         kaddr = kmap_atomic(page, KM_USER0);
977                 }
978                 kunmap_atomic(kaddr, KM_USER0);
979                 bprm->argc--;
980         }
981 }
982
983 EXPORT_SYMBOL(remove_arg_zero);
984
985 /*
986  * cycle the list of binary formats handler, until one recognizes the image
987  */
988 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
989 {
990         int try,retval=0;
991         struct linux_binfmt *fmt;
992 #ifdef __alpha__
993         /* handle /sbin/loader.. */
994         {
995             struct exec * eh = (struct exec *) bprm->buf;
996
997             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
998                 (eh->fh.f_flags & 0x3000) == 0x3000)
999             {
1000                 struct file * file;
1001                 unsigned long loader;
1002
1003                 allow_write_access(bprm->file);
1004                 fput(bprm->file);
1005                 bprm->file = NULL;
1006
1007                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1008
1009                 file = open_exec("/sbin/loader");
1010                 retval = PTR_ERR(file);
1011                 if (IS_ERR(file))
1012                         return retval;
1013
1014                 /* Remember if the application is TASO.  */
1015                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1016
1017                 bprm->file = file;
1018                 bprm->loader = loader;
1019                 retval = prepare_binprm(bprm);
1020                 if (retval<0)
1021                         return retval;
1022                 /* should call search_binary_handler recursively here,
1023                    but it does not matter */
1024             }
1025         }
1026 #endif
1027         retval = security_bprm_check(bprm);
1028         if (retval)
1029                 return retval;
1030
1031         /* kernel module loader fixup */
1032         /* so we don't try to load run modprobe in kernel space. */
1033         set_fs(USER_DS);
1034         for (try=0; try<2; try++) {
1035                 read_lock(&binfmt_lock);
1036                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1037                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1038                         if (!fn)
1039                                 continue;
1040                         if (!try_module_get(fmt->module))
1041                                 continue;
1042                         read_unlock(&binfmt_lock);
1043                         retval = fn(bprm, regs);
1044                         if (retval >= 0) {
1045                                 put_binfmt(fmt);
1046                                 allow_write_access(bprm->file);
1047                                 if (bprm->file)
1048                                         fput(bprm->file);
1049                                 bprm->file = NULL;
1050                                 current->did_exec = 1;
1051                                 ckrm_cb_exec(bprm->filename);
1052                                 return retval;
1053                         }
1054                         read_lock(&binfmt_lock);
1055                         put_binfmt(fmt);
1056                         if (retval != -ENOEXEC || bprm->mm == NULL)
1057                                 break;
1058                         if (!bprm->file) {
1059                                 read_unlock(&binfmt_lock);
1060                                 return retval;
1061                         }
1062                 }
1063                 read_unlock(&binfmt_lock);
1064                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1065                         break;
1066 #ifdef CONFIG_KMOD
1067                 }else{
1068 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1069                         if (printable(bprm->buf[0]) &&
1070                             printable(bprm->buf[1]) &&
1071                             printable(bprm->buf[2]) &&
1072                             printable(bprm->buf[3]))
1073                                 break; /* -ENOEXEC */
1074                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1075 #endif
1076                 }
1077         }
1078         return retval;
1079 }
1080
1081 EXPORT_SYMBOL(search_binary_handler);
1082
1083 /*
1084  * sys_execve() executes a new program.
1085  */
1086 int do_execve(char * filename,
1087         char __user *__user *argv,
1088         char __user *__user *envp,
1089         struct pt_regs * regs)
1090 {
1091         struct linux_binprm bprm;
1092         struct file *file;
1093         int retval;
1094         int i;
1095
1096         file = open_exec(filename);
1097
1098         retval = PTR_ERR(file);
1099         if (IS_ERR(file))
1100                 return retval;
1101
1102         sched_balance_exec();
1103
1104         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1105         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1106
1107         bprm.file = file;
1108         bprm.filename = filename;
1109         bprm.interp = filename;
1110         bprm.interp_flags = 0;
1111         bprm.interp_data = 0;
1112         bprm.sh_bang = 0;
1113         bprm.loader = 0;
1114         bprm.exec = 0;
1115         bprm.security = NULL;
1116         bprm.mm = mm_alloc();
1117         retval = -ENOMEM;
1118         if (!bprm.mm)
1119                 goto out_file;
1120
1121         retval = init_new_context(current, bprm.mm);
1122         if (retval < 0)
1123                 goto out_mm;
1124
1125         bprm.argc = count(argv, bprm.p / sizeof(void *));
1126         if ((retval = bprm.argc) < 0)
1127                 goto out_mm;
1128
1129         bprm.envc = count(envp, bprm.p / sizeof(void *));
1130         if ((retval = bprm.envc) < 0)
1131                 goto out_mm;
1132
1133         retval = security_bprm_alloc(&bprm);
1134         if (retval)
1135                 goto out;
1136
1137         retval = prepare_binprm(&bprm);
1138         if (retval < 0)
1139                 goto out;
1140
1141         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1142         if (retval < 0)
1143                 goto out;
1144
1145         bprm.exec = bprm.p;
1146         retval = copy_strings(bprm.envc, envp, &bprm);
1147         if (retval < 0)
1148                 goto out;
1149
1150         retval = copy_strings(bprm.argc, argv, &bprm);
1151         if (retval < 0)
1152                 goto out;
1153
1154         retval = search_binary_handler(&bprm,regs);
1155         if (retval >= 0) {
1156                 free_arg_pages(&bprm);
1157
1158                 /* execve success */
1159                 security_bprm_free(&bprm);
1160                 return retval;
1161         }
1162
1163 out:
1164         /* Something went wrong, return the inode and free the argument pages*/
1165         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1166                 struct page * page = bprm.page[i];
1167                 if (page)
1168                         __free_page(page);
1169         }
1170
1171         if (bprm.security)
1172                 security_bprm_free(&bprm);
1173
1174 out_mm:
1175         if (bprm.mm)
1176                 mmdrop(bprm.mm);
1177
1178 out_file:
1179         if (bprm.file) {
1180                 allow_write_access(bprm.file);
1181                 fput(bprm.file);
1182         }
1183         return retval;
1184 }
1185
1186 EXPORT_SYMBOL(do_execve);
1187
1188 int set_binfmt(struct linux_binfmt *new)
1189 {
1190         struct linux_binfmt *old = current->binfmt;
1191
1192         if (new) {
1193                 if (!try_module_get(new->module))
1194                         return -1;
1195         }
1196         current->binfmt = new;
1197         if (old)
1198                 module_put(old->module);
1199         return 0;
1200 }
1201
1202 EXPORT_SYMBOL(set_binfmt);
1203
1204 #define CORENAME_MAX_SIZE 64
1205
1206 /* format_corename will inspect the pattern parameter, and output a
1207  * name into corename, which must have space for at least
1208  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1209  */
1210 void format_corename(char *corename, const char *pattern, long signr)
1211 {
1212         const char *pat_ptr = pattern;
1213         char *out_ptr = corename;
1214         char *const out_end = corename + CORENAME_MAX_SIZE;
1215         int rc;
1216         int pid_in_pattern = 0;
1217
1218         /* Repeat as long as we have more pattern to process and more output
1219            space */
1220         while (*pat_ptr) {
1221                 if (*pat_ptr != '%') {
1222                         if (out_ptr == out_end)
1223                                 goto out;
1224                         *out_ptr++ = *pat_ptr++;
1225                 } else {
1226                         switch (*++pat_ptr) {
1227                         case 0:
1228                                 goto out;
1229                         /* Double percent, output one percent */
1230                         case '%':
1231                                 if (out_ptr == out_end)
1232                                         goto out;
1233                                 *out_ptr++ = '%';
1234                                 break;
1235                         /* pid */
1236                         case 'p':
1237                                 pid_in_pattern = 1;
1238                                 rc = snprintf(out_ptr, out_end - out_ptr,
1239                                               "%d", current->tgid);
1240                                 if (rc > out_end - out_ptr)
1241                                         goto out;
1242                                 out_ptr += rc;
1243                                 break;
1244                         /* uid */
1245                         case 'u':
1246                                 rc = snprintf(out_ptr, out_end - out_ptr,
1247                                               "%d", current->uid);
1248                                 if (rc > out_end - out_ptr)
1249                                         goto out;
1250                                 out_ptr += rc;
1251                                 break;
1252                         /* gid */
1253                         case 'g':
1254                                 rc = snprintf(out_ptr, out_end - out_ptr,
1255                                               "%d", current->gid);
1256                                 if (rc > out_end - out_ptr)
1257                                         goto out;
1258                                 out_ptr += rc;
1259                                 break;
1260                         /* signal that caused the coredump */
1261                         case 's':
1262                                 rc = snprintf(out_ptr, out_end - out_ptr,
1263                                               "%ld", signr);
1264                                 if (rc > out_end - out_ptr)
1265                                         goto out;
1266                                 out_ptr += rc;
1267                                 break;
1268                         /* UNIX time of coredump */
1269                         case 't': {
1270                                 struct timeval tv;
1271                                 do_gettimeofday(&tv);
1272                                 rc = snprintf(out_ptr, out_end - out_ptr,
1273                                               "%lu", tv.tv_sec);
1274                                 if (rc > out_end - out_ptr)
1275                                         goto out;
1276                                 out_ptr += rc;
1277                                 break;
1278                         }
1279                         /* hostname */
1280                         case 'h':
1281                                 down_read(&uts_sem);
1282                                 rc = snprintf(out_ptr, out_end - out_ptr,
1283                                               "%s", system_utsname.nodename);
1284                                 up_read(&uts_sem);
1285                                 if (rc > out_end - out_ptr)
1286                                         goto out;
1287                                 out_ptr += rc;
1288                                 break;
1289                         /* executable */
1290                         case 'e':
1291                                 rc = snprintf(out_ptr, out_end - out_ptr,
1292                                               "%s", current->comm);
1293                                 if (rc > out_end - out_ptr)
1294                                         goto out;
1295                                 out_ptr += rc;
1296                                 break;
1297                         default:
1298                                 break;
1299                         }
1300                         ++pat_ptr;
1301                 }
1302         }
1303         /* Backward compatibility with core_uses_pid:
1304          *
1305          * If core_pattern does not include a %p (as is the default)
1306          * and core_uses_pid is set, then .%pid will be appended to
1307          * the filename */
1308         if (!pid_in_pattern
1309             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1310                 rc = snprintf(out_ptr, out_end - out_ptr,
1311                               ".%d", current->tgid);
1312                 if (rc > out_end - out_ptr)
1313                         goto out;
1314                 out_ptr += rc;
1315         }
1316       out:
1317         *out_ptr = 0;
1318 }
1319
1320 static void zap_threads (struct mm_struct *mm)
1321 {
1322         struct task_struct *g, *p;
1323         struct task_struct *tsk = current;
1324         struct completion *vfork_done = tsk->vfork_done;
1325
1326         /*
1327          * Make sure nobody is waiting for us to release the VM,
1328          * otherwise we can deadlock when we wait on each other
1329          */
1330         if (vfork_done) {
1331                 tsk->vfork_done = NULL;
1332                 complete(vfork_done);
1333         }
1334
1335         read_lock(&tasklist_lock);
1336         do_each_thread(g,p)
1337                 if (mm == p->mm && p != tsk) {
1338                         force_sig_specific(SIGKILL, p);
1339                         mm->core_waiters++;
1340                 }
1341         while_each_thread(g,p);
1342
1343         read_unlock(&tasklist_lock);
1344 }
1345
1346 static void coredump_wait(struct mm_struct *mm)
1347 {
1348         DECLARE_COMPLETION(startup_done);
1349
1350         mm->core_waiters++; /* let other threads block */
1351         mm->core_startup_done = &startup_done;
1352
1353         /* give other threads a chance to run: */
1354         yield();
1355
1356         zap_threads(mm);
1357         if (--mm->core_waiters) {
1358                 up_write(&mm->mmap_sem);
1359                 wait_for_completion(&startup_done);
1360         } else
1361                 up_write(&mm->mmap_sem);
1362         BUG_ON(mm->core_waiters);
1363 }
1364
1365 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1366 {
1367         char corename[CORENAME_MAX_SIZE + 1];
1368         struct mm_struct *mm = current->mm;
1369         struct linux_binfmt * binfmt;
1370         struct inode * inode;
1371         struct file * file;
1372         int retval = 0;
1373
1374         lock_kernel();
1375         binfmt = current->binfmt;
1376         if (!binfmt || !binfmt->core_dump)
1377                 goto fail;
1378         down_write(&mm->mmap_sem);
1379         if (!mm->dumpable) {
1380                 up_write(&mm->mmap_sem);
1381                 goto fail;
1382         }
1383         mm->dumpable = 0;
1384         init_completion(&mm->core_done);
1385         current->signal->group_exit = 1;
1386         current->signal->group_exit_code = exit_code;
1387         coredump_wait(mm);
1388
1389         if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1390                 goto fail_unlock;
1391
1392         format_corename(corename, core_pattern, signr);
1393         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1394         if (IS_ERR(file))
1395                 goto fail_unlock;
1396         inode = file->f_dentry->d_inode;
1397         if (inode->i_nlink > 1)
1398                 goto close_fail;        /* multiple links - don't dump */
1399         if (d_unhashed(file->f_dentry))
1400                 goto close_fail;
1401
1402         if (!S_ISREG(inode->i_mode))
1403                 goto close_fail;
1404         if (!file->f_op)
1405                 goto close_fail;
1406         if (!file->f_op->write)
1407                 goto close_fail;
1408         if (do_truncate(file->f_dentry, 0) != 0)
1409                 goto close_fail;
1410
1411         retval = binfmt->core_dump(signr, regs, file);
1412
1413         current->signal->group_exit_code |= 0x80;
1414 close_fail:
1415         filp_close(file, NULL);
1416 fail_unlock:
1417         complete_all(&mm->core_done);
1418 fail:
1419         unlock_kernel();
1420         return retval;
1421 }