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