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