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