NTFS: Fix two nasty runlist merging bugs that had gone unnoticed so far.
[linux-2.6/cjktty.git] / fs / exec.c
blob222ab1c572d884762bae366086bbe145f8e0f65b
1 /*
2 * linux/fs/exec.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * #!-checking implemented by tytso.
9 */
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.
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.
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.
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/key.h>
38 #include <linux/personality.h>
39 #include <linux/binfmts.h>
40 #include <linux/swap.h>
41 #include <linux/utsname.h>
42 #include <linux/module.h>
43 #include <linux/namei.h>
44 #include <linux/proc_fs.h>
45 #include <linux/ptrace.h>
46 #include <linux/mount.h>
47 #include <linux/security.h>
48 #include <linux/syscalls.h>
49 #include <linux/rmap.h>
50 #include <linux/acct.h>
52 #include <asm/uaccess.h>
53 #include <asm/mmu_context.h>
55 #ifdef CONFIG_KMOD
56 #include <linux/kmod.h>
57 #endif
59 int core_uses_pid;
60 char core_pattern[65] = "core";
61 int suid_dumpable = 0;
63 EXPORT_SYMBOL(suid_dumpable);
64 /* The maximal length of core_pattern is also specified in sysctl.c */
66 static struct linux_binfmt *formats;
67 static DEFINE_RWLOCK(binfmt_lock);
69 int register_binfmt(struct linux_binfmt * fmt)
71 struct linux_binfmt ** tmp = &formats;
73 if (!fmt)
74 return -EINVAL;
75 if (fmt->next)
76 return -EBUSY;
77 write_lock(&binfmt_lock);
78 while (*tmp) {
79 if (fmt == *tmp) {
80 write_unlock(&binfmt_lock);
81 return -EBUSY;
83 tmp = &(*tmp)->next;
85 fmt->next = formats;
86 formats = fmt;
87 write_unlock(&binfmt_lock);
88 return 0;
91 EXPORT_SYMBOL(register_binfmt);
93 int unregister_binfmt(struct linux_binfmt * fmt)
95 struct linux_binfmt ** tmp = &formats;
97 write_lock(&binfmt_lock);
98 while (*tmp) {
99 if (fmt == *tmp) {
100 *tmp = fmt->next;
101 write_unlock(&binfmt_lock);
102 return 0;
104 tmp = &(*tmp)->next;
106 write_unlock(&binfmt_lock);
107 return -EINVAL;
110 EXPORT_SYMBOL(unregister_binfmt);
112 static inline void put_binfmt(struct linux_binfmt * fmt)
114 module_put(fmt->module);
118 * Note that a shared library must be both readable and executable due to
119 * security reasons.
121 * Also note that we take the address to load from from the file itself.
123 asmlinkage long sys_uselib(const char __user * library)
125 struct file * file;
126 struct nameidata nd;
127 int error;
129 nd.intent.open.flags = FMODE_READ;
130 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
131 if (error)
132 goto out;
134 error = -EINVAL;
135 if (!S_ISREG(nd.dentry->d_inode->i_mode))
136 goto exit;
138 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
139 if (error)
140 goto exit;
142 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
143 error = PTR_ERR(file);
144 if (IS_ERR(file))
145 goto out;
147 error = -ENOEXEC;
148 if(file->f_op) {
149 struct linux_binfmt * fmt;
151 read_lock(&binfmt_lock);
152 for (fmt = formats ; fmt ; fmt = fmt->next) {
153 if (!fmt->load_shlib)
154 continue;
155 if (!try_module_get(fmt->module))
156 continue;
157 read_unlock(&binfmt_lock);
158 error = fmt->load_shlib(file);
159 read_lock(&binfmt_lock);
160 put_binfmt(fmt);
161 if (error != -ENOEXEC)
162 break;
164 read_unlock(&binfmt_lock);
166 fput(file);
167 out:
168 return error;
169 exit:
170 path_release(&nd);
171 goto out;
175 * count() counts the number of strings in array ARGV.
177 static int count(char __user * __user * argv, int max)
179 int i = 0;
181 if (argv != NULL) {
182 for (;;) {
183 char __user * p;
185 if (get_user(p, argv))
186 return -EFAULT;
187 if (!p)
188 break;
189 argv++;
190 if(++i > max)
191 return -E2BIG;
192 cond_resched();
195 return i;
199 * 'copy_strings()' copies argument/environment strings from user
200 * memory to free pages in kernel mem. These are in a format ready
201 * to be put directly into the top of new user memory.
203 static int copy_strings(int argc, char __user * __user * argv,
204 struct linux_binprm *bprm)
206 struct page *kmapped_page = NULL;
207 char *kaddr = NULL;
208 int ret;
210 while (argc-- > 0) {
211 char __user *str;
212 int len;
213 unsigned long pos;
215 if (get_user(str, argv+argc) ||
216 !(len = strnlen_user(str, bprm->p))) {
217 ret = -EFAULT;
218 goto out;
221 if (bprm->p < len) {
222 ret = -E2BIG;
223 goto out;
226 bprm->p -= len;
227 /* XXX: add architecture specific overflow check here. */
228 pos = bprm->p;
230 while (len > 0) {
231 int i, new, err;
232 int offset, bytes_to_copy;
233 struct page *page;
235 offset = pos % PAGE_SIZE;
236 i = pos/PAGE_SIZE;
237 page = bprm->page[i];
238 new = 0;
239 if (!page) {
240 page = alloc_page(GFP_HIGHUSER);
241 bprm->page[i] = page;
242 if (!page) {
243 ret = -ENOMEM;
244 goto out;
246 new = 1;
249 if (page != kmapped_page) {
250 if (kmapped_page)
251 kunmap(kmapped_page);
252 kmapped_page = page;
253 kaddr = kmap(kmapped_page);
255 if (new && offset)
256 memset(kaddr, 0, offset);
257 bytes_to_copy = PAGE_SIZE - offset;
258 if (bytes_to_copy > len) {
259 bytes_to_copy = len;
260 if (new)
261 memset(kaddr+offset+len, 0,
262 PAGE_SIZE-offset-len);
264 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
265 if (err) {
266 ret = -EFAULT;
267 goto out;
270 pos += bytes_to_copy;
271 str += bytes_to_copy;
272 len -= bytes_to_copy;
275 ret = 0;
276 out:
277 if (kmapped_page)
278 kunmap(kmapped_page);
279 return ret;
283 * Like copy_strings, but get argv and its values from kernel memory.
285 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
287 int r;
288 mm_segment_t oldfs = get_fs();
289 set_fs(KERNEL_DS);
290 r = copy_strings(argc, (char __user * __user *)argv, bprm);
291 set_fs(oldfs);
292 return r;
295 EXPORT_SYMBOL(copy_strings_kernel);
297 #ifdef CONFIG_MMU
299 * This routine is used to map in a page into an address space: needed by
300 * execve() for the initial stack and environment pages.
302 * vma->vm_mm->mmap_sem is held for writing.
304 void install_arg_page(struct vm_area_struct *vma,
305 struct page *page, unsigned long address)
307 struct mm_struct *mm = vma->vm_mm;
308 pgd_t * pgd;
309 pud_t * pud;
310 pmd_t * pmd;
311 pte_t * pte;
313 if (unlikely(anon_vma_prepare(vma)))
314 goto out_sig;
316 flush_dcache_page(page);
317 pgd = pgd_offset(mm, address);
319 spin_lock(&mm->page_table_lock);
320 pud = pud_alloc(mm, pgd, address);
321 if (!pud)
322 goto out;
323 pmd = pmd_alloc(mm, pud, address);
324 if (!pmd)
325 goto out;
326 pte = pte_alloc_map(mm, pmd, address);
327 if (!pte)
328 goto out;
329 if (!pte_none(*pte)) {
330 pte_unmap(pte);
331 goto out;
333 inc_mm_counter(mm, rss);
334 lru_cache_add_active(page);
335 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
336 page, vma->vm_page_prot))));
337 page_add_anon_rmap(page, vma, address);
338 pte_unmap(pte);
339 spin_unlock(&mm->page_table_lock);
341 /* no need for flush_tlb */
342 return;
343 out:
344 spin_unlock(&mm->page_table_lock);
345 out_sig:
346 __free_page(page);
347 force_sig(SIGKILL, current);
350 #define EXTRA_STACK_VM_PAGES 20 /* random */
352 int setup_arg_pages(struct linux_binprm *bprm,
353 unsigned long stack_top,
354 int executable_stack)
356 unsigned long stack_base;
357 struct vm_area_struct *mpnt;
358 struct mm_struct *mm = current->mm;
359 int i, ret;
360 long arg_size;
362 #ifdef CONFIG_STACK_GROWSUP
363 /* Move the argument and environment strings to the bottom of the
364 * stack space.
366 int offset, j;
367 char *to, *from;
369 /* Start by shifting all the pages down */
370 i = 0;
371 for (j = 0; j < MAX_ARG_PAGES; j++) {
372 struct page *page = bprm->page[j];
373 if (!page)
374 continue;
375 bprm->page[i++] = page;
378 /* Now move them within their pages */
379 offset = bprm->p % PAGE_SIZE;
380 to = kmap(bprm->page[0]);
381 for (j = 1; j < i; j++) {
382 memmove(to, to + offset, PAGE_SIZE - offset);
383 from = kmap(bprm->page[j]);
384 memcpy(to + PAGE_SIZE - offset, from, offset);
385 kunmap(bprm->page[j - 1]);
386 to = from;
388 memmove(to, to + offset, PAGE_SIZE - offset);
389 kunmap(bprm->page[j - 1]);
391 /* Limit stack size to 1GB */
392 stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
393 if (stack_base > (1 << 30))
394 stack_base = 1 << 30;
395 stack_base = PAGE_ALIGN(stack_top - stack_base);
397 /* Adjust bprm->p to point to the end of the strings. */
398 bprm->p = stack_base + PAGE_SIZE * i - offset;
400 mm->arg_start = stack_base;
401 arg_size = i << PAGE_SHIFT;
403 /* zero pages that were copied above */
404 while (i < MAX_ARG_PAGES)
405 bprm->page[i++] = NULL;
406 #else
407 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
408 stack_base = PAGE_ALIGN(stack_base);
409 bprm->p += stack_base;
410 mm->arg_start = bprm->p;
411 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
412 #endif
414 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
416 if (bprm->loader)
417 bprm->loader += stack_base;
418 bprm->exec += stack_base;
420 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
421 if (!mpnt)
422 return -ENOMEM;
424 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
425 kmem_cache_free(vm_area_cachep, mpnt);
426 return -ENOMEM;
429 memset(mpnt, 0, sizeof(*mpnt));
431 down_write(&mm->mmap_sem);
433 mpnt->vm_mm = mm;
434 #ifdef CONFIG_STACK_GROWSUP
435 mpnt->vm_start = stack_base;
436 mpnt->vm_end = stack_base + arg_size;
437 #else
438 mpnt->vm_end = stack_top;
439 mpnt->vm_start = mpnt->vm_end - arg_size;
440 #endif
441 /* Adjust stack execute permissions; explicitly enable
442 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
443 * and leave alone (arch default) otherwise. */
444 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
445 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
446 else if (executable_stack == EXSTACK_DISABLE_X)
447 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
448 else
449 mpnt->vm_flags = VM_STACK_FLAGS;
450 mpnt->vm_flags |= mm->def_flags;
451 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
452 if ((ret = insert_vm_struct(mm, mpnt))) {
453 up_write(&mm->mmap_sem);
454 kmem_cache_free(vm_area_cachep, mpnt);
455 return ret;
457 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
460 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
461 struct page *page = bprm->page[i];
462 if (page) {
463 bprm->page[i] = NULL;
464 install_arg_page(mpnt, page, stack_base);
466 stack_base += PAGE_SIZE;
468 up_write(&mm->mmap_sem);
470 return 0;
473 EXPORT_SYMBOL(setup_arg_pages);
475 #define free_arg_pages(bprm) do { } while (0)
477 #else
479 static inline void free_arg_pages(struct linux_binprm *bprm)
481 int i;
483 for (i = 0; i < MAX_ARG_PAGES; i++) {
484 if (bprm->page[i])
485 __free_page(bprm->page[i]);
486 bprm->page[i] = NULL;
490 #endif /* CONFIG_MMU */
492 struct file *open_exec(const char *name)
494 struct nameidata nd;
495 int err;
496 struct file *file;
498 nd.intent.open.flags = FMODE_READ;
499 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
500 file = ERR_PTR(err);
502 if (!err) {
503 struct inode *inode = nd.dentry->d_inode;
504 file = ERR_PTR(-EACCES);
505 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
506 S_ISREG(inode->i_mode)) {
507 int err = permission(inode, MAY_EXEC, &nd);
508 if (!err && !(inode->i_mode & 0111))
509 err = -EACCES;
510 file = ERR_PTR(err);
511 if (!err) {
512 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
513 if (!IS_ERR(file)) {
514 err = deny_write_access(file);
515 if (err) {
516 fput(file);
517 file = ERR_PTR(err);
520 out:
521 return file;
524 path_release(&nd);
526 goto out;
529 EXPORT_SYMBOL(open_exec);
531 int kernel_read(struct file *file, unsigned long offset,
532 char *addr, unsigned long count)
534 mm_segment_t old_fs;
535 loff_t pos = offset;
536 int result;
538 old_fs = get_fs();
539 set_fs(get_ds());
540 /* The cast to a user pointer is valid due to the set_fs() */
541 result = vfs_read(file, (void __user *)addr, count, &pos);
542 set_fs(old_fs);
543 return result;
546 EXPORT_SYMBOL(kernel_read);
548 static int exec_mmap(struct mm_struct *mm)
550 struct task_struct *tsk;
551 struct mm_struct * old_mm, *active_mm;
553 /* Notify parent that we're no longer interested in the old VM */
554 tsk = current;
555 old_mm = current->mm;
556 mm_release(tsk, old_mm);
558 if (old_mm) {
560 * Make sure that if there is a core dump in progress
561 * for the old mm, we get out and die instead of going
562 * through with the exec. We must hold mmap_sem around
563 * checking core_waiters and changing tsk->mm. The
564 * core-inducing thread will increment core_waiters for
565 * each thread whose ->mm == old_mm.
567 down_read(&old_mm->mmap_sem);
568 if (unlikely(old_mm->core_waiters)) {
569 up_read(&old_mm->mmap_sem);
570 return -EINTR;
573 task_lock(tsk);
574 active_mm = tsk->active_mm;
575 tsk->mm = mm;
576 tsk->active_mm = mm;
577 activate_mm(active_mm, mm);
578 task_unlock(tsk);
579 arch_pick_mmap_layout(mm);
580 if (old_mm) {
581 up_read(&old_mm->mmap_sem);
582 if (active_mm != old_mm) BUG();
583 mmput(old_mm);
584 return 0;
586 mmdrop(active_mm);
587 return 0;
591 * This function makes sure the current process has its own signal table,
592 * so that flush_signal_handlers can later reset the handlers without
593 * disturbing other processes. (Other processes might share the signal
594 * table via the CLONE_SIGHAND option to clone().)
596 static inline int de_thread(struct task_struct *tsk)
598 struct signal_struct *sig = tsk->signal;
599 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
600 spinlock_t *lock = &oldsighand->siglock;
601 int count;
604 * If we don't share sighandlers, then we aren't sharing anything
605 * and we can just re-use it all.
607 if (atomic_read(&oldsighand->count) <= 1) {
608 BUG_ON(atomic_read(&sig->count) != 1);
609 exit_itimers(sig);
610 return 0;
613 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
614 if (!newsighand)
615 return -ENOMEM;
617 if (thread_group_empty(current))
618 goto no_thread_group;
621 * Kill all other threads in the thread group.
622 * We must hold tasklist_lock to call zap_other_threads.
624 read_lock(&tasklist_lock);
625 spin_lock_irq(lock);
626 if (sig->flags & SIGNAL_GROUP_EXIT) {
628 * Another group action in progress, just
629 * return so that the signal is processed.
631 spin_unlock_irq(lock);
632 read_unlock(&tasklist_lock);
633 kmem_cache_free(sighand_cachep, newsighand);
634 return -EAGAIN;
636 zap_other_threads(current);
637 read_unlock(&tasklist_lock);
640 * Account for the thread group leader hanging around:
642 count = 2;
643 if (thread_group_leader(current))
644 count = 1;
645 else {
647 * The SIGALRM timer survives the exec, but needs to point
648 * at us as the new group leader now. We have a race with
649 * a timer firing now getting the old leader, so we need to
650 * synchronize with any firing (by calling del_timer_sync)
651 * before we can safely let the old group leader die.
653 sig->real_timer.data = (unsigned long)current;
654 if (del_timer_sync(&sig->real_timer))
655 add_timer(&sig->real_timer);
657 while (atomic_read(&sig->count) > count) {
658 sig->group_exit_task = current;
659 sig->notify_count = count;
660 __set_current_state(TASK_UNINTERRUPTIBLE);
661 spin_unlock_irq(lock);
662 schedule();
663 spin_lock_irq(lock);
665 sig->group_exit_task = NULL;
666 sig->notify_count = 0;
667 sig->real_timer.data = (unsigned long)current;
668 spin_unlock_irq(lock);
671 * At this point all other threads have exited, all we have to
672 * do is to wait for the thread group leader to become inactive,
673 * and to assume its PID:
675 if (!thread_group_leader(current)) {
676 struct task_struct *leader = current->group_leader, *parent;
677 struct dentry *proc_dentry1, *proc_dentry2;
678 unsigned long exit_state, ptrace;
681 * Wait for the thread group leader to be a zombie.
682 * It should already be zombie at this point, most
683 * of the time.
685 while (leader->exit_state != EXIT_ZOMBIE)
686 yield();
688 spin_lock(&leader->proc_lock);
689 spin_lock(&current->proc_lock);
690 proc_dentry1 = proc_pid_unhash(current);
691 proc_dentry2 = proc_pid_unhash(leader);
692 write_lock_irq(&tasklist_lock);
694 BUG_ON(leader->tgid != current->tgid);
695 BUG_ON(current->pid == current->tgid);
697 * An exec() starts a new thread group with the
698 * TGID of the previous thread group. Rehash the
699 * two threads with a switched PID, and release
700 * the former thread group leader:
702 ptrace = leader->ptrace;
703 parent = leader->parent;
704 if (unlikely(ptrace) && unlikely(parent == current)) {
706 * Joker was ptracing his own group leader,
707 * and now he wants to be his own parent!
708 * We can't have that.
710 ptrace = 0;
713 ptrace_unlink(current);
714 ptrace_unlink(leader);
715 remove_parent(current);
716 remove_parent(leader);
718 switch_exec_pids(leader, current);
720 current->parent = current->real_parent = leader->real_parent;
721 leader->parent = leader->real_parent = child_reaper;
722 current->group_leader = current;
723 leader->group_leader = leader;
725 add_parent(current, current->parent);
726 add_parent(leader, leader->parent);
727 if (ptrace) {
728 current->ptrace = ptrace;
729 __ptrace_link(current, parent);
732 list_del(&current->tasks);
733 list_add_tail(&current->tasks, &init_task.tasks);
734 current->exit_signal = SIGCHLD;
735 exit_state = leader->exit_state;
737 write_unlock_irq(&tasklist_lock);
738 spin_unlock(&leader->proc_lock);
739 spin_unlock(&current->proc_lock);
740 proc_pid_flush(proc_dentry1);
741 proc_pid_flush(proc_dentry2);
743 BUG_ON(exit_state != EXIT_ZOMBIE);
744 release_task(leader);
748 * Now there are really no other threads at all,
749 * so it's safe to stop telling them to kill themselves.
751 sig->flags = 0;
753 no_thread_group:
754 BUG_ON(atomic_read(&sig->count) != 1);
755 exit_itimers(sig);
757 if (atomic_read(&oldsighand->count) == 1) {
759 * Now that we nuked the rest of the thread group,
760 * it turns out we are not sharing sighand any more either.
761 * So we can just keep it.
763 kmem_cache_free(sighand_cachep, newsighand);
764 } else {
766 * Move our state over to newsighand and switch it in.
768 spin_lock_init(&newsighand->siglock);
769 atomic_set(&newsighand->count, 1);
770 memcpy(newsighand->action, oldsighand->action,
771 sizeof(newsighand->action));
773 write_lock_irq(&tasklist_lock);
774 spin_lock(&oldsighand->siglock);
775 spin_lock(&newsighand->siglock);
777 current->sighand = newsighand;
778 recalc_sigpending();
780 spin_unlock(&newsighand->siglock);
781 spin_unlock(&oldsighand->siglock);
782 write_unlock_irq(&tasklist_lock);
784 if (atomic_dec_and_test(&oldsighand->count))
785 kmem_cache_free(sighand_cachep, oldsighand);
788 BUG_ON(!thread_group_empty(current));
789 BUG_ON(!thread_group_leader(current));
790 return 0;
794 * These functions flushes out all traces of the currently running executable
795 * so that a new one can be started
798 static inline void flush_old_files(struct files_struct * files)
800 long j = -1;
802 spin_lock(&files->file_lock);
803 for (;;) {
804 unsigned long set, i;
806 j++;
807 i = j * __NFDBITS;
808 if (i >= files->max_fds || i >= files->max_fdset)
809 break;
810 set = files->close_on_exec->fds_bits[j];
811 if (!set)
812 continue;
813 files->close_on_exec->fds_bits[j] = 0;
814 spin_unlock(&files->file_lock);
815 for ( ; set ; i++,set >>= 1) {
816 if (set & 1) {
817 sys_close(i);
820 spin_lock(&files->file_lock);
823 spin_unlock(&files->file_lock);
826 void get_task_comm(char *buf, struct task_struct *tsk)
828 /* buf must be at least sizeof(tsk->comm) in size */
829 task_lock(tsk);
830 strncpy(buf, tsk->comm, sizeof(tsk->comm));
831 task_unlock(tsk);
834 void set_task_comm(struct task_struct *tsk, char *buf)
836 task_lock(tsk);
837 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
838 task_unlock(tsk);
841 int flush_old_exec(struct linux_binprm * bprm)
843 char * name;
844 int i, ch, retval;
845 struct files_struct *files;
846 char tcomm[sizeof(current->comm)];
849 * Make sure we have a private signal table and that
850 * we are unassociated from the previous thread group.
852 retval = de_thread(current);
853 if (retval)
854 goto out;
857 * Make sure we have private file handles. Ask the
858 * fork helper to do the work for us and the exit
859 * helper to do the cleanup of the old one.
861 files = current->files; /* refcounted so safe to hold */
862 retval = unshare_files();
863 if (retval)
864 goto out;
866 * Release all of the old mmap stuff
868 retval = exec_mmap(bprm->mm);
869 if (retval)
870 goto mmap_failed;
872 bprm->mm = NULL; /* We're using it now */
874 /* This is the point of no return */
875 steal_locks(files);
876 put_files_struct(files);
878 current->sas_ss_sp = current->sas_ss_size = 0;
880 if (current->euid == current->uid && current->egid == current->gid)
881 current->mm->dumpable = 1;
882 else
883 current->mm->dumpable = suid_dumpable;
885 name = bprm->filename;
887 /* Copies the binary name from after last slash */
888 for (i=0; (ch = *(name++)) != '\0';) {
889 if (ch == '/')
890 i = 0; /* overwrite what we wrote */
891 else
892 if (i < (sizeof(tcomm) - 1))
893 tcomm[i++] = ch;
895 tcomm[i] = '\0';
896 set_task_comm(current, tcomm);
898 current->flags &= ~PF_RANDOMIZE;
899 flush_thread();
901 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
902 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
903 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
904 suid_keys(current);
905 current->mm->dumpable = suid_dumpable;
908 /* An exec changes our domain. We are no longer part of the thread
909 group */
911 current->self_exec_id++;
913 flush_signal_handlers(current, 0);
914 flush_old_files(current->files);
916 return 0;
918 mmap_failed:
919 put_files_struct(current->files);
920 current->files = files;
921 out:
922 return retval;
925 EXPORT_SYMBOL(flush_old_exec);
928 * Fill the binprm structure from the inode.
929 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
931 int prepare_binprm(struct linux_binprm *bprm)
933 int mode;
934 struct inode * inode = bprm->file->f_dentry->d_inode;
935 int retval;
937 mode = inode->i_mode;
939 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
940 * generic_permission lets a non-executable through
942 if (!(mode & 0111)) /* with at least _one_ execute bit set */
943 return -EACCES;
944 if (bprm->file->f_op == NULL)
945 return -EACCES;
947 bprm->e_uid = current->euid;
948 bprm->e_gid = current->egid;
950 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
951 /* Set-uid? */
952 if (mode & S_ISUID) {
953 current->personality &= ~PER_CLEAR_ON_SETID;
954 bprm->e_uid = inode->i_uid;
957 /* Set-gid? */
959 * If setgid is set but no group execute bit then this
960 * is a candidate for mandatory locking, not a setgid
961 * executable.
963 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
964 current->personality &= ~PER_CLEAR_ON_SETID;
965 bprm->e_gid = inode->i_gid;
969 /* fill in binprm security blob */
970 retval = security_bprm_set(bprm);
971 if (retval)
972 return retval;
974 memset(bprm->buf,0,BINPRM_BUF_SIZE);
975 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
978 EXPORT_SYMBOL(prepare_binprm);
980 static inline int unsafe_exec(struct task_struct *p)
982 int unsafe = 0;
983 if (p->ptrace & PT_PTRACED) {
984 if (p->ptrace & PT_PTRACE_CAP)
985 unsafe |= LSM_UNSAFE_PTRACE_CAP;
986 else
987 unsafe |= LSM_UNSAFE_PTRACE;
989 if (atomic_read(&p->fs->count) > 1 ||
990 atomic_read(&p->files->count) > 1 ||
991 atomic_read(&p->sighand->count) > 1)
992 unsafe |= LSM_UNSAFE_SHARE;
994 return unsafe;
997 void compute_creds(struct linux_binprm *bprm)
999 int unsafe;
1001 if (bprm->e_uid != current->uid)
1002 suid_keys(current);
1003 exec_keys(current);
1005 task_lock(current);
1006 unsafe = unsafe_exec(current);
1007 security_bprm_apply_creds(bprm, unsafe);
1008 task_unlock(current);
1009 security_bprm_post_apply_creds(bprm);
1012 EXPORT_SYMBOL(compute_creds);
1014 void remove_arg_zero(struct linux_binprm *bprm)
1016 if (bprm->argc) {
1017 unsigned long offset;
1018 char * kaddr;
1019 struct page *page;
1021 offset = bprm->p % PAGE_SIZE;
1022 goto inside;
1024 while (bprm->p++, *(kaddr+offset++)) {
1025 if (offset != PAGE_SIZE)
1026 continue;
1027 offset = 0;
1028 kunmap_atomic(kaddr, KM_USER0);
1029 inside:
1030 page = bprm->page[bprm->p/PAGE_SIZE];
1031 kaddr = kmap_atomic(page, KM_USER0);
1033 kunmap_atomic(kaddr, KM_USER0);
1034 bprm->argc--;
1038 EXPORT_SYMBOL(remove_arg_zero);
1041 * cycle the list of binary formats handler, until one recognizes the image
1043 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1045 int try,retval;
1046 struct linux_binfmt *fmt;
1047 #ifdef __alpha__
1048 /* handle /sbin/loader.. */
1050 struct exec * eh = (struct exec *) bprm->buf;
1052 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1053 (eh->fh.f_flags & 0x3000) == 0x3000)
1055 struct file * file;
1056 unsigned long loader;
1058 allow_write_access(bprm->file);
1059 fput(bprm->file);
1060 bprm->file = NULL;
1062 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1064 file = open_exec("/sbin/loader");
1065 retval = PTR_ERR(file);
1066 if (IS_ERR(file))
1067 return retval;
1069 /* Remember if the application is TASO. */
1070 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1072 bprm->file = file;
1073 bprm->loader = loader;
1074 retval = prepare_binprm(bprm);
1075 if (retval<0)
1076 return retval;
1077 /* should call search_binary_handler recursively here,
1078 but it does not matter */
1081 #endif
1082 retval = security_bprm_check(bprm);
1083 if (retval)
1084 return retval;
1086 /* kernel module loader fixup */
1087 /* so we don't try to load run modprobe in kernel space. */
1088 set_fs(USER_DS);
1089 retval = -ENOENT;
1090 for (try=0; try<2; try++) {
1091 read_lock(&binfmt_lock);
1092 for (fmt = formats ; fmt ; fmt = fmt->next) {
1093 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1094 if (!fn)
1095 continue;
1096 if (!try_module_get(fmt->module))
1097 continue;
1098 read_unlock(&binfmt_lock);
1099 retval = fn(bprm, regs);
1100 if (retval >= 0) {
1101 put_binfmt(fmt);
1102 allow_write_access(bprm->file);
1103 if (bprm->file)
1104 fput(bprm->file);
1105 bprm->file = NULL;
1106 current->did_exec = 1;
1107 return retval;
1109 read_lock(&binfmt_lock);
1110 put_binfmt(fmt);
1111 if (retval != -ENOEXEC || bprm->mm == NULL)
1112 break;
1113 if (!bprm->file) {
1114 read_unlock(&binfmt_lock);
1115 return retval;
1118 read_unlock(&binfmt_lock);
1119 if (retval != -ENOEXEC || bprm->mm == NULL) {
1120 break;
1121 #ifdef CONFIG_KMOD
1122 }else{
1123 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1124 if (printable(bprm->buf[0]) &&
1125 printable(bprm->buf[1]) &&
1126 printable(bprm->buf[2]) &&
1127 printable(bprm->buf[3]))
1128 break; /* -ENOEXEC */
1129 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1130 #endif
1133 return retval;
1136 EXPORT_SYMBOL(search_binary_handler);
1139 * sys_execve() executes a new program.
1141 int do_execve(char * filename,
1142 char __user *__user *argv,
1143 char __user *__user *envp,
1144 struct pt_regs * regs)
1146 struct linux_binprm *bprm;
1147 struct file *file;
1148 int retval;
1149 int i;
1151 retval = -ENOMEM;
1152 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1153 if (!bprm)
1154 goto out_ret;
1155 memset(bprm, 0, sizeof(*bprm));
1157 file = open_exec(filename);
1158 retval = PTR_ERR(file);
1159 if (IS_ERR(file))
1160 goto out_kfree;
1162 sched_exec();
1164 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1166 bprm->file = file;
1167 bprm->filename = filename;
1168 bprm->interp = filename;
1169 bprm->mm = mm_alloc();
1170 retval = -ENOMEM;
1171 if (!bprm->mm)
1172 goto out_file;
1174 retval = init_new_context(current, bprm->mm);
1175 if (retval < 0)
1176 goto out_mm;
1178 bprm->argc = count(argv, bprm->p / sizeof(void *));
1179 if ((retval = bprm->argc) < 0)
1180 goto out_mm;
1182 bprm->envc = count(envp, bprm->p / sizeof(void *));
1183 if ((retval = bprm->envc) < 0)
1184 goto out_mm;
1186 retval = security_bprm_alloc(bprm);
1187 if (retval)
1188 goto out;
1190 retval = prepare_binprm(bprm);
1191 if (retval < 0)
1192 goto out;
1194 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1195 if (retval < 0)
1196 goto out;
1198 bprm->exec = bprm->p;
1199 retval = copy_strings(bprm->envc, envp, bprm);
1200 if (retval < 0)
1201 goto out;
1203 retval = copy_strings(bprm->argc, argv, bprm);
1204 if (retval < 0)
1205 goto out;
1207 retval = search_binary_handler(bprm,regs);
1208 if (retval >= 0) {
1209 free_arg_pages(bprm);
1211 /* execve success */
1212 security_bprm_free(bprm);
1213 acct_update_integrals(current);
1214 update_mem_hiwater(current);
1215 kfree(bprm);
1216 return retval;
1219 out:
1220 /* Something went wrong, return the inode and free the argument pages*/
1221 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1222 struct page * page = bprm->page[i];
1223 if (page)
1224 __free_page(page);
1227 if (bprm->security)
1228 security_bprm_free(bprm);
1230 out_mm:
1231 if (bprm->mm)
1232 mmdrop(bprm->mm);
1234 out_file:
1235 if (bprm->file) {
1236 allow_write_access(bprm->file);
1237 fput(bprm->file);
1240 out_kfree:
1241 kfree(bprm);
1243 out_ret:
1244 return retval;
1247 int set_binfmt(struct linux_binfmt *new)
1249 struct linux_binfmt *old = current->binfmt;
1251 if (new) {
1252 if (!try_module_get(new->module))
1253 return -1;
1255 current->binfmt = new;
1256 if (old)
1257 module_put(old->module);
1258 return 0;
1261 EXPORT_SYMBOL(set_binfmt);
1263 #define CORENAME_MAX_SIZE 64
1265 /* format_corename will inspect the pattern parameter, and output a
1266 * name into corename, which must have space for at least
1267 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1269 static void format_corename(char *corename, const char *pattern, long signr)
1271 const char *pat_ptr = pattern;
1272 char *out_ptr = corename;
1273 char *const out_end = corename + CORENAME_MAX_SIZE;
1274 int rc;
1275 int pid_in_pattern = 0;
1277 /* Repeat as long as we have more pattern to process and more output
1278 space */
1279 while (*pat_ptr) {
1280 if (*pat_ptr != '%') {
1281 if (out_ptr == out_end)
1282 goto out;
1283 *out_ptr++ = *pat_ptr++;
1284 } else {
1285 switch (*++pat_ptr) {
1286 case 0:
1287 goto out;
1288 /* Double percent, output one percent */
1289 case '%':
1290 if (out_ptr == out_end)
1291 goto out;
1292 *out_ptr++ = '%';
1293 break;
1294 /* pid */
1295 case 'p':
1296 pid_in_pattern = 1;
1297 rc = snprintf(out_ptr, out_end - out_ptr,
1298 "%d", current->tgid);
1299 if (rc > out_end - out_ptr)
1300 goto out;
1301 out_ptr += rc;
1302 break;
1303 /* uid */
1304 case 'u':
1305 rc = snprintf(out_ptr, out_end - out_ptr,
1306 "%d", current->uid);
1307 if (rc > out_end - out_ptr)
1308 goto out;
1309 out_ptr += rc;
1310 break;
1311 /* gid */
1312 case 'g':
1313 rc = snprintf(out_ptr, out_end - out_ptr,
1314 "%d", current->gid);
1315 if (rc > out_end - out_ptr)
1316 goto out;
1317 out_ptr += rc;
1318 break;
1319 /* signal that caused the coredump */
1320 case 's':
1321 rc = snprintf(out_ptr, out_end - out_ptr,
1322 "%ld", signr);
1323 if (rc > out_end - out_ptr)
1324 goto out;
1325 out_ptr += rc;
1326 break;
1327 /* UNIX time of coredump */
1328 case 't': {
1329 struct timeval tv;
1330 do_gettimeofday(&tv);
1331 rc = snprintf(out_ptr, out_end - out_ptr,
1332 "%lu", tv.tv_sec);
1333 if (rc > out_end - out_ptr)
1334 goto out;
1335 out_ptr += rc;
1336 break;
1338 /* hostname */
1339 case 'h':
1340 down_read(&uts_sem);
1341 rc = snprintf(out_ptr, out_end - out_ptr,
1342 "%s", system_utsname.nodename);
1343 up_read(&uts_sem);
1344 if (rc > out_end - out_ptr)
1345 goto out;
1346 out_ptr += rc;
1347 break;
1348 /* executable */
1349 case 'e':
1350 rc = snprintf(out_ptr, out_end - out_ptr,
1351 "%s", current->comm);
1352 if (rc > out_end - out_ptr)
1353 goto out;
1354 out_ptr += rc;
1355 break;
1356 default:
1357 break;
1359 ++pat_ptr;
1362 /* Backward compatibility with core_uses_pid:
1364 * If core_pattern does not include a %p (as is the default)
1365 * and core_uses_pid is set, then .%pid will be appended to
1366 * the filename */
1367 if (!pid_in_pattern
1368 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1369 rc = snprintf(out_ptr, out_end - out_ptr,
1370 ".%d", current->tgid);
1371 if (rc > out_end - out_ptr)
1372 goto out;
1373 out_ptr += rc;
1375 out:
1376 *out_ptr = 0;
1379 static void zap_threads (struct mm_struct *mm)
1381 struct task_struct *g, *p;
1382 struct task_struct *tsk = current;
1383 struct completion *vfork_done = tsk->vfork_done;
1384 int traced = 0;
1387 * Make sure nobody is waiting for us to release the VM,
1388 * otherwise we can deadlock when we wait on each other
1390 if (vfork_done) {
1391 tsk->vfork_done = NULL;
1392 complete(vfork_done);
1395 read_lock(&tasklist_lock);
1396 do_each_thread(g,p)
1397 if (mm == p->mm && p != tsk) {
1398 force_sig_specific(SIGKILL, p);
1399 mm->core_waiters++;
1400 if (unlikely(p->ptrace) &&
1401 unlikely(p->parent->mm == mm))
1402 traced = 1;
1404 while_each_thread(g,p);
1406 read_unlock(&tasklist_lock);
1408 if (unlikely(traced)) {
1410 * We are zapping a thread and the thread it ptraces.
1411 * If the tracee went into a ptrace stop for exit tracing,
1412 * we could deadlock since the tracer is waiting for this
1413 * coredump to finish. Detach them so they can both die.
1415 write_lock_irq(&tasklist_lock);
1416 do_each_thread(g,p) {
1417 if (mm == p->mm && p != tsk &&
1418 p->ptrace && p->parent->mm == mm) {
1419 __ptrace_unlink(p);
1421 } while_each_thread(g,p);
1422 write_unlock_irq(&tasklist_lock);
1426 static void coredump_wait(struct mm_struct *mm)
1428 DECLARE_COMPLETION(startup_done);
1430 mm->core_waiters++; /* let other threads block */
1431 mm->core_startup_done = &startup_done;
1433 /* give other threads a chance to run: */
1434 yield();
1436 zap_threads(mm);
1437 if (--mm->core_waiters) {
1438 up_write(&mm->mmap_sem);
1439 wait_for_completion(&startup_done);
1440 } else
1441 up_write(&mm->mmap_sem);
1442 BUG_ON(mm->core_waiters);
1445 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1447 char corename[CORENAME_MAX_SIZE + 1];
1448 struct mm_struct *mm = current->mm;
1449 struct linux_binfmt * binfmt;
1450 struct inode * inode;
1451 struct file * file;
1452 int retval = 0;
1453 int fsuid = current->fsuid;
1454 int flag = 0;
1456 binfmt = current->binfmt;
1457 if (!binfmt || !binfmt->core_dump)
1458 goto fail;
1459 down_write(&mm->mmap_sem);
1460 if (!mm->dumpable) {
1461 up_write(&mm->mmap_sem);
1462 goto fail;
1466 * We cannot trust fsuid as being the "true" uid of the
1467 * process nor do we know its entire history. We only know it
1468 * was tainted so we dump it as root in mode 2.
1470 if (mm->dumpable == 2) { /* Setuid core dump mode */
1471 flag = O_EXCL; /* Stop rewrite attacks */
1472 current->fsuid = 0; /* Dump root private */
1474 mm->dumpable = 0;
1475 init_completion(&mm->core_done);
1476 spin_lock_irq(&current->sighand->siglock);
1477 current->signal->flags = SIGNAL_GROUP_EXIT;
1478 current->signal->group_exit_code = exit_code;
1479 spin_unlock_irq(&current->sighand->siglock);
1480 coredump_wait(mm);
1483 * Clear any false indication of pending signals that might
1484 * be seen by the filesystem code called to write the core file.
1486 current->signal->group_stop_count = 0;
1487 clear_thread_flag(TIF_SIGPENDING);
1489 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1490 goto fail_unlock;
1493 * lock_kernel() because format_corename() is controlled by sysctl, which
1494 * uses lock_kernel()
1496 lock_kernel();
1497 format_corename(corename, core_pattern, signr);
1498 unlock_kernel();
1499 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1500 if (IS_ERR(file))
1501 goto fail_unlock;
1502 inode = file->f_dentry->d_inode;
1503 if (inode->i_nlink > 1)
1504 goto close_fail; /* multiple links - don't dump */
1505 if (d_unhashed(file->f_dentry))
1506 goto close_fail;
1508 if (!S_ISREG(inode->i_mode))
1509 goto close_fail;
1510 if (!file->f_op)
1511 goto close_fail;
1512 if (!file->f_op->write)
1513 goto close_fail;
1514 if (do_truncate(file->f_dentry, 0) != 0)
1515 goto close_fail;
1517 retval = binfmt->core_dump(signr, regs, file);
1519 if (retval)
1520 current->signal->group_exit_code |= 0x80;
1521 close_fail:
1522 filp_close(file, NULL);
1523 fail_unlock:
1524 current->fsuid = fsuid;
1525 complete_all(&mm->core_done);
1526 fail:
1527 return retval;