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