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