[PATCH] b44: support ETHTOOL_GPERMADDR
[linux-2.6/sactl.git] / fs / exec.c
blob14dd03907ccb58ed1b8c9cbdce76b6246d4ce727
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;
801 struct fdtable *fdt;
803 spin_lock(&files->file_lock);
804 for (;;) {
805 unsigned long set, i;
807 j++;
808 i = j * __NFDBITS;
809 fdt = files_fdtable(files);
810 if (i >= fdt->max_fds || i >= fdt->max_fdset)
811 break;
812 set = fdt->close_on_exec->fds_bits[j];
813 if (!set)
814 continue;
815 fdt->close_on_exec->fds_bits[j] = 0;
816 spin_unlock(&files->file_lock);
817 for ( ; set ; i++,set >>= 1) {
818 if (set & 1) {
819 sys_close(i);
822 spin_lock(&files->file_lock);
825 spin_unlock(&files->file_lock);
828 void get_task_comm(char *buf, struct task_struct *tsk)
830 /* buf must be at least sizeof(tsk->comm) in size */
831 task_lock(tsk);
832 strncpy(buf, tsk->comm, sizeof(tsk->comm));
833 task_unlock(tsk);
836 void set_task_comm(struct task_struct *tsk, char *buf)
838 task_lock(tsk);
839 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
840 task_unlock(tsk);
843 int flush_old_exec(struct linux_binprm * bprm)
845 char * name;
846 int i, ch, retval;
847 struct files_struct *files;
848 char tcomm[sizeof(current->comm)];
851 * Make sure we have a private signal table and that
852 * we are unassociated from the previous thread group.
854 retval = de_thread(current);
855 if (retval)
856 goto out;
859 * Make sure we have private file handles. Ask the
860 * fork helper to do the work for us and the exit
861 * helper to do the cleanup of the old one.
863 files = current->files; /* refcounted so safe to hold */
864 retval = unshare_files();
865 if (retval)
866 goto out;
868 * Release all of the old mmap stuff
870 retval = exec_mmap(bprm->mm);
871 if (retval)
872 goto mmap_failed;
874 bprm->mm = NULL; /* We're using it now */
876 /* This is the point of no return */
877 steal_locks(files);
878 put_files_struct(files);
880 current->sas_ss_sp = current->sas_ss_size = 0;
882 if (current->euid == current->uid && current->egid == current->gid)
883 current->mm->dumpable = 1;
884 else
885 current->mm->dumpable = suid_dumpable;
887 name = bprm->filename;
889 /* Copies the binary name from after last slash */
890 for (i=0; (ch = *(name++)) != '\0';) {
891 if (ch == '/')
892 i = 0; /* overwrite what we wrote */
893 else
894 if (i < (sizeof(tcomm) - 1))
895 tcomm[i++] = ch;
897 tcomm[i] = '\0';
898 set_task_comm(current, tcomm);
900 current->flags &= ~PF_RANDOMIZE;
901 flush_thread();
903 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
904 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL) ||
905 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
906 suid_keys(current);
907 current->mm->dumpable = suid_dumpable;
910 /* An exec changes our domain. We are no longer part of the thread
911 group */
913 current->self_exec_id++;
915 flush_signal_handlers(current, 0);
916 flush_old_files(current->files);
918 return 0;
920 mmap_failed:
921 put_files_struct(current->files);
922 current->files = files;
923 out:
924 return retval;
927 EXPORT_SYMBOL(flush_old_exec);
930 * Fill the binprm structure from the inode.
931 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
933 int prepare_binprm(struct linux_binprm *bprm)
935 int mode;
936 struct inode * inode = bprm->file->f_dentry->d_inode;
937 int retval;
939 mode = inode->i_mode;
941 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
942 * generic_permission lets a non-executable through
944 if (!(mode & 0111)) /* with at least _one_ execute bit set */
945 return -EACCES;
946 if (bprm->file->f_op == NULL)
947 return -EACCES;
949 bprm->e_uid = current->euid;
950 bprm->e_gid = current->egid;
952 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
953 /* Set-uid? */
954 if (mode & S_ISUID) {
955 current->personality &= ~PER_CLEAR_ON_SETID;
956 bprm->e_uid = inode->i_uid;
959 /* Set-gid? */
961 * If setgid is set but no group execute bit then this
962 * is a candidate for mandatory locking, not a setgid
963 * executable.
965 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
966 current->personality &= ~PER_CLEAR_ON_SETID;
967 bprm->e_gid = inode->i_gid;
971 /* fill in binprm security blob */
972 retval = security_bprm_set(bprm);
973 if (retval)
974 return retval;
976 memset(bprm->buf,0,BINPRM_BUF_SIZE);
977 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
980 EXPORT_SYMBOL(prepare_binprm);
982 static inline int unsafe_exec(struct task_struct *p)
984 int unsafe = 0;
985 if (p->ptrace & PT_PTRACED) {
986 if (p->ptrace & PT_PTRACE_CAP)
987 unsafe |= LSM_UNSAFE_PTRACE_CAP;
988 else
989 unsafe |= LSM_UNSAFE_PTRACE;
991 if (atomic_read(&p->fs->count) > 1 ||
992 atomic_read(&p->files->count) > 1 ||
993 atomic_read(&p->sighand->count) > 1)
994 unsafe |= LSM_UNSAFE_SHARE;
996 return unsafe;
999 void compute_creds(struct linux_binprm *bprm)
1001 int unsafe;
1003 if (bprm->e_uid != current->uid)
1004 suid_keys(current);
1005 exec_keys(current);
1007 task_lock(current);
1008 unsafe = unsafe_exec(current);
1009 security_bprm_apply_creds(bprm, unsafe);
1010 task_unlock(current);
1011 security_bprm_post_apply_creds(bprm);
1014 EXPORT_SYMBOL(compute_creds);
1016 void remove_arg_zero(struct linux_binprm *bprm)
1018 if (bprm->argc) {
1019 unsigned long offset;
1020 char * kaddr;
1021 struct page *page;
1023 offset = bprm->p % PAGE_SIZE;
1024 goto inside;
1026 while (bprm->p++, *(kaddr+offset++)) {
1027 if (offset != PAGE_SIZE)
1028 continue;
1029 offset = 0;
1030 kunmap_atomic(kaddr, KM_USER0);
1031 inside:
1032 page = bprm->page[bprm->p/PAGE_SIZE];
1033 kaddr = kmap_atomic(page, KM_USER0);
1035 kunmap_atomic(kaddr, KM_USER0);
1036 bprm->argc--;
1040 EXPORT_SYMBOL(remove_arg_zero);
1043 * cycle the list of binary formats handler, until one recognizes the image
1045 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1047 int try,retval;
1048 struct linux_binfmt *fmt;
1049 #ifdef __alpha__
1050 /* handle /sbin/loader.. */
1052 struct exec * eh = (struct exec *) bprm->buf;
1054 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1055 (eh->fh.f_flags & 0x3000) == 0x3000)
1057 struct file * file;
1058 unsigned long loader;
1060 allow_write_access(bprm->file);
1061 fput(bprm->file);
1062 bprm->file = NULL;
1064 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1066 file = open_exec("/sbin/loader");
1067 retval = PTR_ERR(file);
1068 if (IS_ERR(file))
1069 return retval;
1071 /* Remember if the application is TASO. */
1072 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1074 bprm->file = file;
1075 bprm->loader = loader;
1076 retval = prepare_binprm(bprm);
1077 if (retval<0)
1078 return retval;
1079 /* should call search_binary_handler recursively here,
1080 but it does not matter */
1083 #endif
1084 retval = security_bprm_check(bprm);
1085 if (retval)
1086 return retval;
1088 /* kernel module loader fixup */
1089 /* so we don't try to load run modprobe in kernel space. */
1090 set_fs(USER_DS);
1091 retval = -ENOENT;
1092 for (try=0; try<2; try++) {
1093 read_lock(&binfmt_lock);
1094 for (fmt = formats ; fmt ; fmt = fmt->next) {
1095 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1096 if (!fn)
1097 continue;
1098 if (!try_module_get(fmt->module))
1099 continue;
1100 read_unlock(&binfmt_lock);
1101 retval = fn(bprm, regs);
1102 if (retval >= 0) {
1103 put_binfmt(fmt);
1104 allow_write_access(bprm->file);
1105 if (bprm->file)
1106 fput(bprm->file);
1107 bprm->file = NULL;
1108 current->did_exec = 1;
1109 return retval;
1111 read_lock(&binfmt_lock);
1112 put_binfmt(fmt);
1113 if (retval != -ENOEXEC || bprm->mm == NULL)
1114 break;
1115 if (!bprm->file) {
1116 read_unlock(&binfmt_lock);
1117 return retval;
1120 read_unlock(&binfmt_lock);
1121 if (retval != -ENOEXEC || bprm->mm == NULL) {
1122 break;
1123 #ifdef CONFIG_KMOD
1124 }else{
1125 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1126 if (printable(bprm->buf[0]) &&
1127 printable(bprm->buf[1]) &&
1128 printable(bprm->buf[2]) &&
1129 printable(bprm->buf[3]))
1130 break; /* -ENOEXEC */
1131 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1132 #endif
1135 return retval;
1138 EXPORT_SYMBOL(search_binary_handler);
1141 * sys_execve() executes a new program.
1143 int do_execve(char * filename,
1144 char __user *__user *argv,
1145 char __user *__user *envp,
1146 struct pt_regs * regs)
1148 struct linux_binprm *bprm;
1149 struct file *file;
1150 int retval;
1151 int i;
1153 retval = -ENOMEM;
1154 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1155 if (!bprm)
1156 goto out_ret;
1157 memset(bprm, 0, sizeof(*bprm));
1159 file = open_exec(filename);
1160 retval = PTR_ERR(file);
1161 if (IS_ERR(file))
1162 goto out_kfree;
1164 sched_exec();
1166 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1168 bprm->file = file;
1169 bprm->filename = filename;
1170 bprm->interp = filename;
1171 bprm->mm = mm_alloc();
1172 retval = -ENOMEM;
1173 if (!bprm->mm)
1174 goto out_file;
1176 retval = init_new_context(current, bprm->mm);
1177 if (retval < 0)
1178 goto out_mm;
1180 bprm->argc = count(argv, bprm->p / sizeof(void *));
1181 if ((retval = bprm->argc) < 0)
1182 goto out_mm;
1184 bprm->envc = count(envp, bprm->p / sizeof(void *));
1185 if ((retval = bprm->envc) < 0)
1186 goto out_mm;
1188 retval = security_bprm_alloc(bprm);
1189 if (retval)
1190 goto out;
1192 retval = prepare_binprm(bprm);
1193 if (retval < 0)
1194 goto out;
1196 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1197 if (retval < 0)
1198 goto out;
1200 bprm->exec = bprm->p;
1201 retval = copy_strings(bprm->envc, envp, bprm);
1202 if (retval < 0)
1203 goto out;
1205 retval = copy_strings(bprm->argc, argv, bprm);
1206 if (retval < 0)
1207 goto out;
1209 retval = search_binary_handler(bprm,regs);
1210 if (retval >= 0) {
1211 free_arg_pages(bprm);
1213 /* execve success */
1214 security_bprm_free(bprm);
1215 acct_update_integrals(current);
1216 update_mem_hiwater(current);
1217 kfree(bprm);
1218 return retval;
1221 out:
1222 /* Something went wrong, return the inode and free the argument pages*/
1223 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1224 struct page * page = bprm->page[i];
1225 if (page)
1226 __free_page(page);
1229 if (bprm->security)
1230 security_bprm_free(bprm);
1232 out_mm:
1233 if (bprm->mm)
1234 mmdrop(bprm->mm);
1236 out_file:
1237 if (bprm->file) {
1238 allow_write_access(bprm->file);
1239 fput(bprm->file);
1242 out_kfree:
1243 kfree(bprm);
1245 out_ret:
1246 return retval;
1249 int set_binfmt(struct linux_binfmt *new)
1251 struct linux_binfmt *old = current->binfmt;
1253 if (new) {
1254 if (!try_module_get(new->module))
1255 return -1;
1257 current->binfmt = new;
1258 if (old)
1259 module_put(old->module);
1260 return 0;
1263 EXPORT_SYMBOL(set_binfmt);
1265 #define CORENAME_MAX_SIZE 64
1267 /* format_corename will inspect the pattern parameter, and output a
1268 * name into corename, which must have space for at least
1269 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1271 static void format_corename(char *corename, const char *pattern, long signr)
1273 const char *pat_ptr = pattern;
1274 char *out_ptr = corename;
1275 char *const out_end = corename + CORENAME_MAX_SIZE;
1276 int rc;
1277 int pid_in_pattern = 0;
1279 /* Repeat as long as we have more pattern to process and more output
1280 space */
1281 while (*pat_ptr) {
1282 if (*pat_ptr != '%') {
1283 if (out_ptr == out_end)
1284 goto out;
1285 *out_ptr++ = *pat_ptr++;
1286 } else {
1287 switch (*++pat_ptr) {
1288 case 0:
1289 goto out;
1290 /* Double percent, output one percent */
1291 case '%':
1292 if (out_ptr == out_end)
1293 goto out;
1294 *out_ptr++ = '%';
1295 break;
1296 /* pid */
1297 case 'p':
1298 pid_in_pattern = 1;
1299 rc = snprintf(out_ptr, out_end - out_ptr,
1300 "%d", current->tgid);
1301 if (rc > out_end - out_ptr)
1302 goto out;
1303 out_ptr += rc;
1304 break;
1305 /* uid */
1306 case 'u':
1307 rc = snprintf(out_ptr, out_end - out_ptr,
1308 "%d", current->uid);
1309 if (rc > out_end - out_ptr)
1310 goto out;
1311 out_ptr += rc;
1312 break;
1313 /* gid */
1314 case 'g':
1315 rc = snprintf(out_ptr, out_end - out_ptr,
1316 "%d", current->gid);
1317 if (rc > out_end - out_ptr)
1318 goto out;
1319 out_ptr += rc;
1320 break;
1321 /* signal that caused the coredump */
1322 case 's':
1323 rc = snprintf(out_ptr, out_end - out_ptr,
1324 "%ld", signr);
1325 if (rc > out_end - out_ptr)
1326 goto out;
1327 out_ptr += rc;
1328 break;
1329 /* UNIX time of coredump */
1330 case 't': {
1331 struct timeval tv;
1332 do_gettimeofday(&tv);
1333 rc = snprintf(out_ptr, out_end - out_ptr,
1334 "%lu", tv.tv_sec);
1335 if (rc > out_end - out_ptr)
1336 goto out;
1337 out_ptr += rc;
1338 break;
1340 /* hostname */
1341 case 'h':
1342 down_read(&uts_sem);
1343 rc = snprintf(out_ptr, out_end - out_ptr,
1344 "%s", system_utsname.nodename);
1345 up_read(&uts_sem);
1346 if (rc > out_end - out_ptr)
1347 goto out;
1348 out_ptr += rc;
1349 break;
1350 /* executable */
1351 case 'e':
1352 rc = snprintf(out_ptr, out_end - out_ptr,
1353 "%s", current->comm);
1354 if (rc > out_end - out_ptr)
1355 goto out;
1356 out_ptr += rc;
1357 break;
1358 default:
1359 break;
1361 ++pat_ptr;
1364 /* Backward compatibility with core_uses_pid:
1366 * If core_pattern does not include a %p (as is the default)
1367 * and core_uses_pid is set, then .%pid will be appended to
1368 * the filename */
1369 if (!pid_in_pattern
1370 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1371 rc = snprintf(out_ptr, out_end - out_ptr,
1372 ".%d", current->tgid);
1373 if (rc > out_end - out_ptr)
1374 goto out;
1375 out_ptr += rc;
1377 out:
1378 *out_ptr = 0;
1381 static void zap_threads (struct mm_struct *mm)
1383 struct task_struct *g, *p;
1384 struct task_struct *tsk = current;
1385 struct completion *vfork_done = tsk->vfork_done;
1386 int traced = 0;
1389 * Make sure nobody is waiting for us to release the VM,
1390 * otherwise we can deadlock when we wait on each other
1392 if (vfork_done) {
1393 tsk->vfork_done = NULL;
1394 complete(vfork_done);
1397 read_lock(&tasklist_lock);
1398 do_each_thread(g,p)
1399 if (mm == p->mm && p != tsk) {
1400 force_sig_specific(SIGKILL, p);
1401 mm->core_waiters++;
1402 if (unlikely(p->ptrace) &&
1403 unlikely(p->parent->mm == mm))
1404 traced = 1;
1406 while_each_thread(g,p);
1408 read_unlock(&tasklist_lock);
1410 if (unlikely(traced)) {
1412 * We are zapping a thread and the thread it ptraces.
1413 * If the tracee went into a ptrace stop for exit tracing,
1414 * we could deadlock since the tracer is waiting for this
1415 * coredump to finish. Detach them so they can both die.
1417 write_lock_irq(&tasklist_lock);
1418 do_each_thread(g,p) {
1419 if (mm == p->mm && p != tsk &&
1420 p->ptrace && p->parent->mm == mm) {
1421 __ptrace_unlink(p);
1423 } while_each_thread(g,p);
1424 write_unlock_irq(&tasklist_lock);
1428 static void coredump_wait(struct mm_struct *mm)
1430 DECLARE_COMPLETION(startup_done);
1432 mm->core_waiters++; /* let other threads block */
1433 mm->core_startup_done = &startup_done;
1435 /* give other threads a chance to run: */
1436 yield();
1438 zap_threads(mm);
1439 if (--mm->core_waiters) {
1440 up_write(&mm->mmap_sem);
1441 wait_for_completion(&startup_done);
1442 } else
1443 up_write(&mm->mmap_sem);
1444 BUG_ON(mm->core_waiters);
1447 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1449 char corename[CORENAME_MAX_SIZE + 1];
1450 struct mm_struct *mm = current->mm;
1451 struct linux_binfmt * binfmt;
1452 struct inode * inode;
1453 struct file * file;
1454 int retval = 0;
1455 int fsuid = current->fsuid;
1456 int flag = 0;
1458 binfmt = current->binfmt;
1459 if (!binfmt || !binfmt->core_dump)
1460 goto fail;
1461 down_write(&mm->mmap_sem);
1462 if (!mm->dumpable) {
1463 up_write(&mm->mmap_sem);
1464 goto fail;
1468 * We cannot trust fsuid as being the "true" uid of the
1469 * process nor do we know its entire history. We only know it
1470 * was tainted so we dump it as root in mode 2.
1472 if (mm->dumpable == 2) { /* Setuid core dump mode */
1473 flag = O_EXCL; /* Stop rewrite attacks */
1474 current->fsuid = 0; /* Dump root private */
1476 mm->dumpable = 0;
1477 init_completion(&mm->core_done);
1478 spin_lock_irq(&current->sighand->siglock);
1479 current->signal->flags = SIGNAL_GROUP_EXIT;
1480 current->signal->group_exit_code = exit_code;
1481 spin_unlock_irq(&current->sighand->siglock);
1482 coredump_wait(mm);
1485 * Clear any false indication of pending signals that might
1486 * be seen by the filesystem code called to write the core file.
1488 current->signal->group_stop_count = 0;
1489 clear_thread_flag(TIF_SIGPENDING);
1491 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1492 goto fail_unlock;
1495 * lock_kernel() because format_corename() is controlled by sysctl, which
1496 * uses lock_kernel()
1498 lock_kernel();
1499 format_corename(corename, core_pattern, signr);
1500 unlock_kernel();
1501 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
1502 if (IS_ERR(file))
1503 goto fail_unlock;
1504 inode = file->f_dentry->d_inode;
1505 if (inode->i_nlink > 1)
1506 goto close_fail; /* multiple links - don't dump */
1507 if (d_unhashed(file->f_dentry))
1508 goto close_fail;
1510 if (!S_ISREG(inode->i_mode))
1511 goto close_fail;
1512 if (!file->f_op)
1513 goto close_fail;
1514 if (!file->f_op->write)
1515 goto close_fail;
1516 if (do_truncate(file->f_dentry, 0) != 0)
1517 goto close_fail;
1519 retval = binfmt->core_dump(signr, regs, file);
1521 if (retval)
1522 current->signal->group_exit_code |= 0x80;
1523 close_fail:
1524 filp_close(file, NULL);
1525 fail_unlock:
1526 current->fsuid = fsuid;
1527 complete_all(&mm->core_done);
1528 fail:
1529 return retval;