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