This commit was manufactured by cvs2svn to create tag
[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
51 #include <asm/uaccess.h>
52 #include <asm/pgalloc.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_page_prot = protection_map[mpnt->vm_flags & 0x7];
443                 insert_vm_struct(mm, mpnt);
444                 // mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
445                 vx_vmpages_sub(mm, mm->total_vm -
446                         ((mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT));
447         }
448
449         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
450                 struct page *page = bprm->page[i];
451                 if (page) {
452                         bprm->page[i] = NULL;
453                         install_arg_page(mpnt, page, stack_base);
454                 }
455                 stack_base += PAGE_SIZE;
456         }
457         up_write(&mm->mmap_sem);
458         
459         return 0;
460 }
461
462 EXPORT_SYMBOL(setup_arg_pages);
463
464 #define free_arg_pages(bprm) do { } while (0)
465
466 #else
467
468 static inline void free_arg_pages(struct linux_binprm *bprm)
469 {
470         int i;
471
472         for (i = 0; i < MAX_ARG_PAGES; i++) {
473                 if (bprm->page[i])
474                         __free_page(bprm->page[i]);
475                 bprm->page[i] = NULL;
476         }
477 }
478
479 #endif /* CONFIG_MMU */
480
481 struct file *open_exec(const char *name)
482 {
483         struct nameidata nd;
484         int err;
485         struct file *file;
486
487         nd.intent.open.flags = FMODE_READ;
488         err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
489         file = ERR_PTR(err);
490
491         if (!err) {
492                 struct inode *inode = nd.dentry->d_inode;
493                 file = ERR_PTR(-EACCES);
494                 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
495                     S_ISREG(inode->i_mode)) {
496                         int err = permission(inode, MAY_EXEC, &nd);
497                         if (!err && !(inode->i_mode & 0111))
498                                 err = -EACCES;
499                         file = ERR_PTR(err);
500                         if (!err) {
501                                 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
502                                 if (!IS_ERR(file)) {
503                                         err = deny_write_access(file);
504                                         if (err) {
505                                                 fput(file);
506                                                 file = ERR_PTR(err);
507                                         }
508                                 }
509 out:
510                                 return file;
511                         }
512                 }
513                 path_release(&nd);
514         }
515         goto out;
516 }
517
518 EXPORT_SYMBOL(open_exec);
519
520 int kernel_read(struct file *file, unsigned long offset,
521         char *addr, unsigned long count)
522 {
523         mm_segment_t old_fs;
524         loff_t pos = offset;
525         int result;
526
527         old_fs = get_fs();
528         set_fs(get_ds());
529         /* The cast to a user pointer is valid due to the set_fs() */
530         result = vfs_read(file, (void __user *)addr, count, &pos);
531         set_fs(old_fs);
532         return result;
533 }
534
535 EXPORT_SYMBOL(kernel_read);
536
537 static int exec_mmap(struct mm_struct *mm)
538 {
539         struct task_struct *tsk;
540         struct mm_struct * old_mm, *active_mm;
541
542         /* Add it to the list of mm's */
543         spin_lock(&mmlist_lock);
544         list_add(&mm->mmlist, &init_mm.mmlist);
545         mmlist_nr++;
546         spin_unlock(&mmlist_lock);
547
548         /* Notify parent that we're no longer interested in the old VM */
549         tsk = current;
550         old_mm = current->mm;
551         mm_release(tsk, old_mm);
552
553         task_lock(tsk);
554         active_mm = tsk->active_mm;
555         tsk->mm = mm;
556         tsk->active_mm = mm;
557         activate_mm(active_mm, mm);
558         task_unlock(tsk);
559         if (old_mm) {
560                 if (active_mm != old_mm) BUG();
561                 mmput(old_mm);
562                 return 0;
563         }
564         mmdrop(active_mm);
565         return 0;
566 }
567
568 /*
569  * This function makes sure the current process has its own signal table,
570  * so that flush_signal_handlers can later reset the handlers without
571  * disturbing other processes.  (Other processes might share the signal
572  * table via the CLONE_SIGHAND option to clone().)
573  */
574 static inline int de_thread(struct task_struct *tsk)
575 {
576         struct signal_struct *newsig, *oldsig = tsk->signal;
577         struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
578         spinlock_t *lock = &oldsighand->siglock;
579         int count;
580
581         /*
582          * If we don't share sighandlers, then we aren't sharing anything
583          * and we can just re-use it all.
584          */
585         if (atomic_read(&oldsighand->count) <= 1)
586                 return 0;
587
588         newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
589         if (!newsighand)
590                 return -ENOMEM;
591
592         spin_lock_init(&newsighand->siglock);
593         atomic_set(&newsighand->count, 1);
594         memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
595
596         /*
597          * See if we need to allocate a new signal structure
598          */
599         newsig = NULL;
600         if (atomic_read(&oldsig->count) > 1) {
601                 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
602                 if (!newsig) {
603                         kmem_cache_free(sighand_cachep, newsighand);
604                         return -ENOMEM;
605                 }
606                 atomic_set(&newsig->count, 1);
607                 newsig->group_exit = 0;
608                 newsig->group_exit_code = 0;
609                 newsig->group_exit_task = NULL;
610                 newsig->group_stop_count = 0;
611                 newsig->curr_target = NULL;
612                 init_sigpending(&newsig->shared_pending);
613                 INIT_LIST_HEAD(&newsig->posix_timers);
614
615                 newsig->tty = oldsig->tty;
616                 newsig->pgrp = oldsig->pgrp;
617                 newsig->session = oldsig->session;
618                 newsig->leader = oldsig->leader;
619                 newsig->tty_old_pgrp = oldsig->tty_old_pgrp;
620         }
621
622         if (thread_group_empty(current))
623                 goto no_thread_group;
624
625         /*
626          * Kill all other threads in the thread group.
627          * We must hold tasklist_lock to call zap_other_threads.
628          */
629         read_lock(&tasklist_lock);
630         spin_lock_irq(lock);
631         if (oldsig->group_exit) {
632                 /*
633                  * Another group action in progress, just
634                  * return so that the signal is processed.
635                  */
636                 spin_unlock_irq(lock);
637                 read_unlock(&tasklist_lock);
638                 kmem_cache_free(sighand_cachep, newsighand);
639                 if (newsig)
640                         kmem_cache_free(signal_cachep, newsig);
641                 return -EAGAIN;
642         }
643         oldsig->group_exit = 1;
644         zap_other_threads(current);
645         read_unlock(&tasklist_lock);
646
647         /*
648          * Account for the thread group leader hanging around:
649          */
650         count = 2;
651         if (current->pid == current->tgid)
652                 count = 1;
653         while (atomic_read(&oldsig->count) > count) {
654                 oldsig->group_exit_task = current;
655                 oldsig->notify_count = count;
656                 __set_current_state(TASK_UNINTERRUPTIBLE);
657                 spin_unlock_irq(lock);
658                 schedule();
659                 spin_lock_irq(lock);
660         }
661         spin_unlock_irq(lock);
662
663         /*
664          * At this point all other threads have exited, all we have to
665          * do is to wait for the thread group leader to become inactive,
666          * and to assume its PID:
667          */
668         if (current->pid != current->tgid) {
669                 struct task_struct *leader = current->group_leader, *parent;
670                 struct dentry *proc_dentry1, *proc_dentry2;
671                 unsigned long state, ptrace;
672
673                 /*
674                  * Wait for the thread group leader to be a zombie.
675                  * It should already be zombie at this point, most
676                  * of the time.
677                  */
678                 while (leader->state != TASK_ZOMBIE)
679                         yield();
680
681                 spin_lock(&leader->proc_lock);
682                 spin_lock(&current->proc_lock);
683                 proc_dentry1 = proc_pid_unhash(current);
684                 proc_dentry2 = proc_pid_unhash(leader);
685                 write_lock_irq(&tasklist_lock);
686
687                 if (leader->tgid != current->tgid)
688                         BUG();
689                 if (current->pid == current->tgid)
690                         BUG();
691                 /*
692                  * An exec() starts a new thread group with the
693                  * TGID of the previous thread group. Rehash the
694                  * two threads with a switched PID, and release
695                  * the former thread group leader:
696                  */
697                 ptrace = leader->ptrace;
698                 parent = leader->parent;
699
700                 ptrace_unlink(current);
701                 ptrace_unlink(leader);
702                 remove_parent(current);
703                 remove_parent(leader);
704
705                 switch_exec_pids(leader, current);
706
707                 current->parent = current->real_parent = leader->real_parent;
708                 leader->parent = leader->real_parent = child_reaper;
709                 current->group_leader = current;
710                 leader->group_leader = leader;
711
712                 add_parent(current, current->parent);
713                 add_parent(leader, leader->parent);
714                 if (ptrace) {
715                         current->ptrace = ptrace;
716                         __ptrace_link(current, parent);
717                 }
718
719                 list_del(&current->tasks);
720                 list_add_tail(&current->tasks, &init_task.tasks);
721                 current->exit_signal = SIGCHLD;
722                 state = leader->state;
723
724                 write_unlock_irq(&tasklist_lock);
725                 spin_unlock(&leader->proc_lock);
726                 spin_unlock(&current->proc_lock);
727                 proc_pid_flush(proc_dentry1);
728                 proc_pid_flush(proc_dentry2);
729
730                 if (state != TASK_ZOMBIE)
731                         BUG();
732                 release_task(leader);
733         }
734
735 no_thread_group:
736
737         write_lock_irq(&tasklist_lock);
738         spin_lock(&oldsighand->siglock);
739         spin_lock(&newsighand->siglock);
740
741         if (current == oldsig->curr_target)
742                 oldsig->curr_target = next_thread(current);
743         if (newsig)
744                 current->signal = newsig;
745         current->sighand = newsighand;
746         init_sigpending(&current->pending);
747         recalc_sigpending();
748
749         spin_unlock(&newsighand->siglock);
750         spin_unlock(&oldsighand->siglock);
751         write_unlock_irq(&tasklist_lock);
752
753         if (newsig && atomic_dec_and_test(&oldsig->count))
754                 kmem_cache_free(signal_cachep, oldsig);
755
756         if (atomic_dec_and_test(&oldsighand->count))
757                 kmem_cache_free(sighand_cachep, oldsighand);
758
759         if (!thread_group_empty(current))
760                 BUG();
761         if (current->tgid != current->pid)
762                 BUG();
763         return 0;
764 }
765         
766 /*
767  * These functions flushes out all traces of the currently running executable
768  * so that a new one can be started
769  */
770
771 static inline void flush_old_files(struct files_struct * files)
772 {
773         long j = -1;
774
775         spin_lock(&files->file_lock);
776         for (;;) {
777                 unsigned long set, i;
778
779                 j++;
780                 i = j * __NFDBITS;
781                 if (i >= files->max_fds || i >= files->max_fdset)
782                         break;
783                 set = files->close_on_exec->fds_bits[j];
784                 if (!set)
785                         continue;
786                 files->close_on_exec->fds_bits[j] = 0;
787                 spin_unlock(&files->file_lock);
788                 for ( ; set ; i++,set >>= 1) {
789                         if (set & 1) {
790                                 sys_close(i);
791                         }
792                 }
793                 spin_lock(&files->file_lock);
794
795         }
796         spin_unlock(&files->file_lock);
797 }
798
799 int flush_old_exec(struct linux_binprm * bprm)
800 {
801         char * name;
802         int i, ch, retval;
803         struct files_struct *files;
804
805         /*
806          * Make sure we have a private signal table and that
807          * we are unassociated from the previous thread group.
808          */
809         retval = de_thread(current);
810         if (retval)
811                 goto out;
812
813         /*
814          * Make sure we have private file handles. Ask the
815          * fork helper to do the work for us and the exit
816          * helper to do the cleanup of the old one.
817          */
818         files = current->files;         /* refcounted so safe to hold */
819         retval = unshare_files();
820         if (retval)
821                 goto out;
822         /*
823          * Release all of the old mmap stuff
824          */
825         retval = exec_mmap(bprm->mm);
826         if (retval)
827                 goto mmap_failed;
828
829         bprm->mm = NULL;                /* We're using it now */
830
831         /* This is the point of no return */
832         steal_locks(files);
833         put_files_struct(files);
834
835         current->sas_ss_sp = current->sas_ss_size = 0;
836
837         if (current->euid == current->uid && current->egid == current->gid)
838                 current->mm->dumpable = 1;
839         name = bprm->filename;
840         for (i=0; (ch = *(name++)) != '\0';) {
841                 if (ch == '/')
842                         i = 0;
843                 else
844                         if (i < 15)
845                                 current->comm[i++] = ch;
846         }
847         current->comm[i] = '\0';
848
849         current->flags &= ~PF_RELOCEXEC;
850         flush_thread();
851
852         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
853             permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
854                 current->mm->dumpable = 0;
855
856         /* An exec changes our domain. We are no longer part of the thread
857            group */
858
859         current->self_exec_id++;
860                         
861         flush_signal_handlers(current, 0);
862         flush_old_files(current->files);
863
864         return 0;
865
866 mmap_failed:
867         put_files_struct(current->files);
868         current->files = files;
869 out:
870         return retval;
871 }
872
873 EXPORT_SYMBOL(flush_old_exec);
874
875 /* 
876  * Fill the binprm structure from the inode. 
877  * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
878  */
879 int prepare_binprm(struct linux_binprm *bprm)
880 {
881         int mode;
882         struct inode * inode = bprm->file->f_dentry->d_inode;
883         int retval;
884
885         mode = inode->i_mode;
886         /*
887          * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
888          * vfs_permission lets a non-executable through
889          */
890         if (!(mode & 0111))     /* with at least _one_ execute bit set */
891                 return -EACCES;
892         if (bprm->file->f_op == NULL)
893                 return -EACCES;
894
895         bprm->e_uid = current->euid;
896         bprm->e_gid = current->egid;
897
898         if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
899                 /* Set-uid? */
900                 if (mode & S_ISUID) {
901                         bprm->e_uid = inode->i_uid;
902 #ifdef __i386__
903                         /* reset personality */
904                         current->personality = PER_LINUX;
905 #endif
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                         bprm->e_gid = inode->i_gid;
916 #ifdef __i386__
917                         /* reset personality */
918                         current->personality = PER_LINUX;
919 #endif
920                 }
921         }
922
923         /* fill in binprm security blob */
924         retval = security_bprm_set(bprm);
925         if (retval)
926                 return retval;
927
928         memset(bprm->buf,0,BINPRM_BUF_SIZE);
929         return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
930 }
931
932 EXPORT_SYMBOL(prepare_binprm);
933
934 static inline int unsafe_exec(struct task_struct *p)
935 {
936         int unsafe = 0;
937         if (p->ptrace & PT_PTRACED) {
938                 if (p->ptrace & PT_PTRACE_CAP)
939                         unsafe |= LSM_UNSAFE_PTRACE_CAP;
940                 else
941                         unsafe |= LSM_UNSAFE_PTRACE;
942         }
943         if (atomic_read(&p->fs->count) > 1 ||
944             atomic_read(&p->files->count) > 1 ||
945             atomic_read(&p->sighand->count) > 1)
946                 unsafe |= LSM_UNSAFE_SHARE;
947
948         return unsafe;
949 }
950
951 void compute_creds(struct linux_binprm *bprm)
952 {
953         int unsafe;
954         task_lock(current);
955         unsafe = unsafe_exec(current);
956         security_bprm_apply_creds(bprm, unsafe);
957         task_unlock(current);
958 }
959
960 EXPORT_SYMBOL(compute_creds);
961
962 void remove_arg_zero(struct linux_binprm *bprm)
963 {
964         if (bprm->argc) {
965                 unsigned long offset;
966                 char * kaddr;
967                 struct page *page;
968
969                 offset = bprm->p % PAGE_SIZE;
970                 goto inside;
971
972                 while (bprm->p++, *(kaddr+offset++)) {
973                         if (offset != PAGE_SIZE)
974                                 continue;
975                         offset = 0;
976                         kunmap_atomic(kaddr, KM_USER0);
977 inside:
978                         page = bprm->page[bprm->p/PAGE_SIZE];
979                         kaddr = kmap_atomic(page, KM_USER0);
980                 }
981                 kunmap_atomic(kaddr, KM_USER0);
982                 bprm->argc--;
983         }
984 }
985
986 EXPORT_SYMBOL(remove_arg_zero);
987
988 /*
989  * cycle the list of binary formats handler, until one recognizes the image
990  */
991 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
992 {
993         int try,retval=0;
994         struct linux_binfmt *fmt;
995 #ifdef __alpha__
996         /* handle /sbin/loader.. */
997         {
998             struct exec * eh = (struct exec *) bprm->buf;
999
1000             if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1001                 (eh->fh.f_flags & 0x3000) == 0x3000)
1002             {
1003                 struct file * file;
1004                 unsigned long loader;
1005
1006                 allow_write_access(bprm->file);
1007                 fput(bprm->file);
1008                 bprm->file = NULL;
1009
1010                 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1011
1012                 file = open_exec("/sbin/loader");
1013                 retval = PTR_ERR(file);
1014                 if (IS_ERR(file))
1015                         return retval;
1016
1017                 /* Remember if the application is TASO.  */
1018                 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1019
1020                 bprm->file = file;
1021                 bprm->loader = loader;
1022                 retval = prepare_binprm(bprm);
1023                 if (retval<0)
1024                         return retval;
1025                 /* should call search_binary_handler recursively here,
1026                    but it does not matter */
1027             }
1028         }
1029 #endif
1030         retval = security_bprm_check(bprm);
1031         if (retval)
1032                 return retval;
1033
1034         /* kernel module loader fixup */
1035         /* so we don't try to load run modprobe in kernel space. */
1036         set_fs(USER_DS);
1037         for (try=0; try<2; try++) {
1038                 read_lock(&binfmt_lock);
1039                 for (fmt = formats ; fmt ; fmt = fmt->next) {
1040                         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1041                         if (!fn)
1042                                 continue;
1043                         if (!try_module_get(fmt->module))
1044                                 continue;
1045                         read_unlock(&binfmt_lock);
1046                         retval = fn(bprm, regs);
1047                         if (retval >= 0) {
1048                                 put_binfmt(fmt);
1049                                 allow_write_access(bprm->file);
1050                                 if (bprm->file)
1051                                         fput(bprm->file);
1052                                 bprm->file = NULL;
1053                                 current->did_exec = 1;
1054                                 return retval;
1055                         }
1056                         read_lock(&binfmt_lock);
1057                         put_binfmt(fmt);
1058                         if (retval != -ENOEXEC || bprm->mm == NULL)
1059                                 break;
1060                         if (!bprm->file) {
1061                                 read_unlock(&binfmt_lock);
1062                                 return retval;
1063                         }
1064                 }
1065                 read_unlock(&binfmt_lock);
1066                 if (retval != -ENOEXEC || bprm->mm == NULL) {
1067                         break;
1068 #ifdef CONFIG_KMOD
1069                 }else{
1070 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1071                         if (printable(bprm->buf[0]) &&
1072                             printable(bprm->buf[1]) &&
1073                             printable(bprm->buf[2]) &&
1074                             printable(bprm->buf[3]))
1075                                 break; /* -ENOEXEC */
1076                         request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1077 #endif
1078                 }
1079         }
1080         return retval;
1081 }
1082
1083 EXPORT_SYMBOL(search_binary_handler);
1084
1085 /*
1086  * sys_execve() executes a new program.
1087  */
1088 int do_execve(char * filename,
1089         char __user *__user *argv,
1090         char __user *__user *envp,
1091         struct pt_regs * regs)
1092 {
1093         struct linux_binprm bprm;
1094         struct file *file;
1095         int retval;
1096         int i;
1097
1098         sched_balance_exec();
1099
1100         file = open_exec(filename);
1101
1102         retval = PTR_ERR(file);
1103         if (IS_ERR(file))
1104                 return retval;
1105
1106         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1107         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1108
1109         bprm.file = file;
1110         bprm.filename = filename;
1111         bprm.interp = filename;
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                 ckrm_cb_exec(filename);
1159
1160                 /* execve success */
1161                 security_bprm_free(&bprm);
1162                 return retval;
1163         }
1164
1165 out:
1166         /* Something went wrong, return the inode and free the argument pages*/
1167         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1168                 struct page * page = bprm.page[i];
1169                 if (page)
1170                         __free_page(page);
1171         }
1172
1173         if (bprm.security)
1174                 security_bprm_free(&bprm);
1175
1176 out_mm:
1177         if (bprm.mm)
1178                 mmdrop(bprm.mm);
1179
1180 out_file:
1181         if (bprm.file) {
1182                 allow_write_access(bprm.file);
1183                 fput(bprm.file);
1184         }
1185         return retval;
1186 }
1187
1188 EXPORT_SYMBOL(do_execve);
1189
1190 int set_binfmt(struct linux_binfmt *new)
1191 {
1192         struct linux_binfmt *old = current->binfmt;
1193
1194         if (new) {
1195                 if (!try_module_get(new->module))
1196                         return -1;
1197         }
1198         current->binfmt = new;
1199         if (old)
1200                 module_put(old->module);
1201         return 0;
1202 }
1203
1204 EXPORT_SYMBOL(set_binfmt);
1205
1206 #define CORENAME_MAX_SIZE 64
1207
1208 /* format_corename will inspect the pattern parameter, and output a
1209  * name into corename, which must have space for at least
1210  * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1211  */
1212 void format_corename(char *corename, const char *pattern, long signr)
1213 {
1214         const char *pat_ptr = pattern;
1215         char *out_ptr = corename;
1216         char *const out_end = corename + CORENAME_MAX_SIZE;
1217         int rc;
1218         int pid_in_pattern = 0;
1219
1220         /* Repeat as long as we have more pattern to process and more output
1221            space */
1222         while (*pat_ptr) {
1223                 if (*pat_ptr != '%') {
1224                         if (out_ptr == out_end)
1225                                 goto out;
1226                         *out_ptr++ = *pat_ptr++;
1227                 } else {
1228                         switch (*++pat_ptr) {
1229                         case 0:
1230                                 goto out;
1231                         /* Double percent, output one percent */
1232                         case '%':
1233                                 if (out_ptr == out_end)
1234                                         goto out;
1235                                 *out_ptr++ = '%';
1236                                 break;
1237                         /* pid */
1238                         case 'p':
1239                                 pid_in_pattern = 1;
1240                                 rc = snprintf(out_ptr, out_end - out_ptr,
1241                                               "%d", current->tgid);
1242                                 if (rc > out_end - out_ptr)
1243                                         goto out;
1244                                 out_ptr += rc;
1245                                 break;
1246                         /* uid */
1247                         case 'u':
1248                                 rc = snprintf(out_ptr, out_end - out_ptr,
1249                                               "%d", current->uid);
1250                                 if (rc > out_end - out_ptr)
1251                                         goto out;
1252                                 out_ptr += rc;
1253                                 break;
1254                         /* gid */
1255                         case 'g':
1256                                 rc = snprintf(out_ptr, out_end - out_ptr,
1257                                               "%d", current->gid);
1258                                 if (rc > out_end - out_ptr)
1259                                         goto out;
1260                                 out_ptr += rc;
1261                                 break;
1262                         /* signal that caused the coredump */
1263                         case 's':
1264                                 rc = snprintf(out_ptr, out_end - out_ptr,
1265                                               "%ld", signr);
1266                                 if (rc > out_end - out_ptr)
1267                                         goto out;
1268                                 out_ptr += rc;
1269                                 break;
1270                         /* UNIX time of coredump */
1271                         case 't': {
1272                                 struct timeval tv;
1273                                 do_gettimeofday(&tv);
1274                                 rc = snprintf(out_ptr, out_end - out_ptr,
1275                                               "%lu", tv.tv_sec);
1276                                 if (rc > out_end - out_ptr)
1277                                         goto out;
1278                                 out_ptr += rc;
1279                                 break;
1280                         }
1281                         /* hostname */
1282                         case 'h':
1283                                 down_read(&uts_sem);
1284                                 rc = snprintf(out_ptr, out_end - out_ptr,
1285                                               "%s", system_utsname.nodename);
1286                                 up_read(&uts_sem);
1287                                 if (rc > out_end - out_ptr)
1288                                         goto out;
1289                                 out_ptr += rc;
1290                                 break;
1291                         /* executable */
1292                         case 'e':
1293                                 rc = snprintf(out_ptr, out_end - out_ptr,
1294                                               "%s", current->comm);
1295                                 if (rc > out_end - out_ptr)
1296                                         goto out;
1297                                 out_ptr += rc;
1298                                 break;
1299                         default:
1300                                 break;
1301                         }
1302                         ++pat_ptr;
1303                 }
1304         }
1305         /* Backward compatibility with core_uses_pid:
1306          *
1307          * If core_pattern does not include a %p (as is the default)
1308          * and core_uses_pid is set, then .%pid will be appended to
1309          * the filename */
1310         if (!pid_in_pattern
1311             && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1312                 rc = snprintf(out_ptr, out_end - out_ptr,
1313                               ".%d", current->tgid);
1314                 if (rc > out_end - out_ptr)
1315                         goto out;
1316                 out_ptr += rc;
1317         }
1318       out:
1319         *out_ptr = 0;
1320 }
1321
1322 static void zap_threads (struct mm_struct *mm)
1323 {
1324         struct task_struct *g, *p;
1325         struct task_struct *tsk = current;
1326         struct completion *vfork_done = tsk->vfork_done;
1327
1328         /*
1329          * Make sure nobody is waiting for us to release the VM,
1330          * otherwise we can deadlock when we wait on each other
1331          */
1332         if (vfork_done) {
1333                 tsk->vfork_done = NULL;
1334                 complete(vfork_done);
1335         }
1336
1337         read_lock(&tasklist_lock);
1338         do_each_thread(g,p)
1339                 if (mm == p->mm && p != tsk) {
1340                         force_sig_specific(SIGKILL, p);
1341                         mm->core_waiters++;
1342                 }
1343         while_each_thread(g,p);
1344
1345         read_unlock(&tasklist_lock);
1346 }
1347
1348 static void coredump_wait(struct mm_struct *mm)
1349 {
1350         DECLARE_COMPLETION(startup_done);
1351
1352         mm->core_waiters++; /* let other threads block */
1353         mm->core_startup_done = &startup_done;
1354
1355         /* give other threads a chance to run: */
1356         yield();
1357
1358         zap_threads(mm);
1359         if (--mm->core_waiters) {
1360                 up_write(&mm->mmap_sem);
1361                 wait_for_completion(&startup_done);
1362         } else
1363                 up_write(&mm->mmap_sem);
1364         BUG_ON(mm->core_waiters);
1365 }
1366
1367 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1368 {
1369         char corename[CORENAME_MAX_SIZE + 1];
1370         struct mm_struct *mm = current->mm;
1371         struct linux_binfmt * binfmt;
1372         struct inode * inode;
1373         struct file * file;
1374         int retval = 0;
1375
1376         lock_kernel();
1377         binfmt = current->binfmt;
1378         if (!binfmt || !binfmt->core_dump)
1379                 goto fail;
1380         down_write(&mm->mmap_sem);
1381         if (!mm->dumpable) {
1382                 up_write(&mm->mmap_sem);
1383                 goto fail;
1384         }
1385         mm->dumpable = 0;
1386         init_completion(&mm->core_done);
1387         current->signal->group_exit = 1;
1388         current->signal->group_exit_code = exit_code;
1389         coredump_wait(mm);
1390
1391         if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1392                 goto fail_unlock;
1393
1394         format_corename(corename, core_pattern, signr);
1395         file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1396         if (IS_ERR(file))
1397                 goto fail_unlock;
1398         inode = file->f_dentry->d_inode;
1399         if (inode->i_nlink > 1)
1400                 goto close_fail;        /* multiple links - don't dump */
1401         if (d_unhashed(file->f_dentry))
1402                 goto close_fail;
1403
1404         if (!S_ISREG(inode->i_mode))
1405                 goto close_fail;
1406         if (!file->f_op)
1407                 goto close_fail;
1408         if (!file->f_op->write)
1409                 goto close_fail;
1410         if (do_truncate(file->f_dentry, 0) != 0)
1411                 goto close_fail;
1412
1413         retval = binfmt->core_dump(signr, regs, file);
1414
1415         current->signal->group_exit_code |= 0x80;
1416 close_fail:
1417         filp_close(file, NULL);
1418 fail_unlock:
1419         complete_all(&mm->core_done);
1420 fail:
1421         unlock_kernel();
1422         return retval;
1423 }