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