RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / exec.c
blob7ad8d164c5dd3f3523879f9388bad088a749e41b
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/slab.h>
26 #include <linux/file.h>
27 #include <linux/mman.h>
28 #include <linux/a.out.h>
29 #include <linux/stat.h>
30 #include <linux/fcntl.h>
31 #include <linux/smp_lock.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/highmem.h>
35 #include <linux/spinlock.h>
36 #include <linux/key.h>
37 #include <linux/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/pid_namespace.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/tsacct_kern.h>
51 #include <linux/cn_proc.h>
52 #include <linux/audit.h>
54 #include <asm/uaccess.h>
55 #include <asm/mmu_context.h>
57 #ifdef CONFIG_KMOD
58 #include <linux/kmod.h>
59 #endif
61 int core_uses_pid;
62 char core_pattern[CORENAME_MAX_SIZE] = "core";
63 int suid_dumpable = 0;
65 EXPORT_SYMBOL(suid_dumpable);
66 /* The maximal length of core_pattern is also specified in sysctl.c */
68 static struct linux_binfmt *formats;
69 static DEFINE_RWLOCK(binfmt_lock);
71 int register_binfmt(struct linux_binfmt * fmt)
73 struct linux_binfmt ** tmp = &formats;
75 if (!fmt)
76 return -EINVAL;
77 if (fmt->next)
78 return -EBUSY;
79 write_lock(&binfmt_lock);
80 while (*tmp) {
81 if (fmt == *tmp) {
82 write_unlock(&binfmt_lock);
83 return -EBUSY;
85 tmp = &(*tmp)->next;
87 fmt->next = formats;
88 formats = fmt;
89 write_unlock(&binfmt_lock);
90 return 0;
93 EXPORT_SYMBOL(register_binfmt);
95 int unregister_binfmt(struct linux_binfmt * fmt)
97 struct linux_binfmt ** tmp = &formats;
99 write_lock(&binfmt_lock);
100 while (*tmp) {
101 if (fmt == *tmp) {
102 *tmp = fmt->next;
103 fmt->next = NULL;
104 write_unlock(&binfmt_lock);
105 return 0;
107 tmp = &(*tmp)->next;
109 write_unlock(&binfmt_lock);
110 return -EINVAL;
113 EXPORT_SYMBOL(unregister_binfmt);
115 static inline void put_binfmt(struct linux_binfmt * fmt)
117 module_put(fmt->module);
121 * Note that a shared library must be both readable and executable due to
122 * security reasons.
124 * Also note that we take the address to load from from the file itself.
126 asmlinkage long sys_uselib(const char __user * library)
128 struct file * file;
129 struct nameidata nd;
130 int error;
132 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
133 if (error)
134 goto out;
136 error = -EACCES;
137 if (nd.mnt->mnt_flags & MNT_NOEXEC)
138 goto exit;
139 error = -EINVAL;
140 if (!S_ISREG(nd.dentry->d_inode->i_mode))
141 goto exit;
143 error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
144 if (error)
145 goto exit;
147 file = nameidata_to_filp(&nd, O_RDONLY);
148 error = PTR_ERR(file);
149 if (IS_ERR(file))
150 goto out;
152 error = -ENOEXEC;
153 if(file->f_op) {
154 struct linux_binfmt * fmt;
156 read_lock(&binfmt_lock);
157 for (fmt = formats ; fmt ; fmt = fmt->next) {
158 if (!fmt->load_shlib)
159 continue;
160 if (!try_module_get(fmt->module))
161 continue;
162 read_unlock(&binfmt_lock);
163 error = fmt->load_shlib(file);
164 read_lock(&binfmt_lock);
165 put_binfmt(fmt);
166 if (error != -ENOEXEC)
167 break;
169 read_unlock(&binfmt_lock);
171 fput(file);
172 out:
173 return error;
174 exit:
175 release_open_intent(&nd);
176 path_release(&nd);
177 goto out;
181 * count() counts the number of strings in array ARGV.
183 static int count(char __user * __user * argv, int max)
185 int i = 0;
187 if (argv != NULL) {
188 for (;;) {
189 char __user * p;
191 if (get_user(p, argv))
192 return -EFAULT;
193 if (!p)
194 break;
195 argv++;
196 if(++i > max)
197 return -E2BIG;
198 cond_resched();
201 return i;
205 * 'copy_strings()' copies argument/environment strings from user
206 * memory to free pages in kernel mem. These are in a format ready
207 * to be put directly into the top of new user memory.
209 static int copy_strings(int argc, char __user * __user * argv,
210 struct linux_binprm *bprm)
212 struct page *kmapped_page = NULL;
213 char *kaddr = NULL;
214 int ret;
216 while (argc-- > 0) {
217 char __user *str;
218 int len;
219 unsigned long pos;
221 if (get_user(str, argv+argc) ||
222 !(len = strnlen_user(str, bprm->p))) {
223 ret = -EFAULT;
224 goto out;
227 if (bprm->p < len) {
228 ret = -E2BIG;
229 goto out;
232 bprm->p -= len;
233 /* XXX: add architecture specific overflow check here. */
234 pos = bprm->p;
236 while (len > 0) {
237 int i, new, err;
238 int offset, bytes_to_copy;
239 struct page *page;
241 offset = pos % PAGE_SIZE;
242 i = pos/PAGE_SIZE;
243 page = bprm->page[i];
244 new = 0;
245 if (!page) {
246 page = alloc_page(GFP_HIGHUSER);
247 bprm->page[i] = page;
248 if (!page) {
249 ret = -ENOMEM;
250 goto out;
252 new = 1;
255 if (page != kmapped_page) {
256 if (kmapped_page)
257 kunmap(kmapped_page);
258 kmapped_page = page;
259 kaddr = kmap(kmapped_page);
261 if (new && offset)
262 memset(kaddr, 0, offset);
263 bytes_to_copy = PAGE_SIZE - offset;
264 if (bytes_to_copy > len) {
265 bytes_to_copy = len;
266 if (new)
267 memset(kaddr+offset+len, 0,
268 PAGE_SIZE-offset-len);
270 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
271 if (err) {
272 ret = -EFAULT;
273 goto out;
276 pos += bytes_to_copy;
277 str += bytes_to_copy;
278 len -= bytes_to_copy;
281 ret = 0;
282 out:
283 if (kmapped_page)
284 kunmap(kmapped_page);
285 return ret;
289 * Like copy_strings, but get argv and its values from kernel memory.
291 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
293 int r;
294 mm_segment_t oldfs = get_fs();
295 set_fs(KERNEL_DS);
296 r = copy_strings(argc, (char __user * __user *)argv, bprm);
297 set_fs(oldfs);
298 return r;
301 EXPORT_SYMBOL(copy_strings_kernel);
303 #ifdef CONFIG_MMU
305 * This routine is used to map in a page into an address space: needed by
306 * execve() for the initial stack and environment pages.
308 * vma->vm_mm->mmap_sem is held for writing.
310 void install_arg_page(struct vm_area_struct *vma,
311 struct page *page, unsigned long address)
313 struct mm_struct *mm = vma->vm_mm;
314 pte_t * pte;
315 spinlock_t *ptl;
317 if (unlikely(anon_vma_prepare(vma)))
318 goto out;
320 flush_dcache_page(page);
321 pte = get_locked_pte(mm, address, &ptl);
322 if (!pte)
323 goto out;
324 if (!pte_none(*pte)) {
325 pte_unmap_unlock(pte, ptl);
326 goto out;
328 inc_mm_counter(mm, anon_rss);
329 lru_cache_add_active(page);
330 set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
331 page, vma->vm_page_prot))));
332 page_add_new_anon_rmap(page, vma, address);
333 pte_unmap_unlock(pte, ptl);
335 /* no need for flush_tlb */
336 return;
337 out:
338 __free_page(page);
339 force_sig(SIGKILL, current);
342 #define EXTRA_STACK_VM_PAGES 20 /* random */
344 int setup_arg_pages(struct linux_binprm *bprm,
345 unsigned long stack_top,
346 int executable_stack)
348 unsigned long stack_base;
349 struct vm_area_struct *mpnt;
350 struct mm_struct *mm = current->mm;
351 int i, ret;
352 long arg_size;
354 #ifdef CONFIG_STACK_GROWSUP
355 /* Move the argument and environment strings to the bottom of the
356 * stack space.
358 int offset, j;
359 char *to, *from;
361 /* Start by shifting all the pages down */
362 i = 0;
363 for (j = 0; j < MAX_ARG_PAGES; j++) {
364 struct page *page = bprm->page[j];
365 if (!page)
366 continue;
367 bprm->page[i++] = page;
370 /* Now move them within their pages */
371 offset = bprm->p % PAGE_SIZE;
372 to = kmap(bprm->page[0]);
373 for (j = 1; j < i; j++) {
374 memmove(to, to + offset, PAGE_SIZE - offset);
375 from = kmap(bprm->page[j]);
376 memcpy(to + PAGE_SIZE - offset, from, offset);
377 kunmap(bprm->page[j - 1]);
378 to = from;
380 memmove(to, to + offset, PAGE_SIZE - offset);
381 kunmap(bprm->page[j - 1]);
383 /* Limit stack size to 1GB */
384 stack_base = current->signal->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);
389 /* Adjust bprm->p to point to the end of the strings. */
390 bprm->p = stack_base + PAGE_SIZE * i - offset;
392 mm->arg_start = stack_base;
393 arg_size = i << PAGE_SHIFT;
395 /* zero pages that were copied above */
396 while (i < MAX_ARG_PAGES)
397 bprm->page[i++] = NULL;
398 #else
399 stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
400 stack_base = PAGE_ALIGN(stack_base);
401 bprm->p += stack_base;
402 mm->arg_start = bprm->p;
403 arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
404 #endif
406 arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
408 if (bprm->loader)
409 bprm->loader += stack_base;
410 bprm->exec += stack_base;
412 mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
413 if (!mpnt)
414 return -ENOMEM;
416 down_write(&mm->mmap_sem);
418 mpnt->vm_mm = mm;
419 #ifdef CONFIG_STACK_GROWSUP
420 mpnt->vm_start = stack_base;
421 mpnt->vm_end = stack_base + arg_size;
422 #else
423 mpnt->vm_end = stack_top;
424 mpnt->vm_start = mpnt->vm_end - arg_size;
425 #endif
426 /* Adjust stack execute permissions; explicitly enable
427 * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
428 * and leave alone (arch default) otherwise. */
429 if (unlikely(executable_stack == EXSTACK_ENABLE_X))
430 mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
431 else if (executable_stack == EXSTACK_DISABLE_X)
432 mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
433 else
434 mpnt->vm_flags = VM_STACK_FLAGS;
435 mpnt->vm_flags |= mm->def_flags;
436 mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
437 if ((ret = insert_vm_struct(mm, mpnt))) {
438 up_write(&mm->mmap_sem);
439 kmem_cache_free(vm_area_cachep, mpnt);
440 return ret;
442 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
445 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
446 struct page *page = bprm->page[i];
447 if (page) {
448 bprm->page[i] = NULL;
449 install_arg_page(mpnt, page, stack_base);
451 stack_base += PAGE_SIZE;
453 up_write(&mm->mmap_sem);
455 return 0;
458 EXPORT_SYMBOL(setup_arg_pages);
460 #define free_arg_pages(bprm) do { } while (0)
462 #else
464 static inline void free_arg_pages(struct linux_binprm *bprm)
466 int i;
468 for (i = 0; i < MAX_ARG_PAGES; i++) {
469 if (bprm->page[i])
470 __free_page(bprm->page[i]);
471 bprm->page[i] = NULL;
475 #endif /* CONFIG_MMU */
477 struct file *open_exec(const char *name)
479 struct nameidata nd;
480 int err;
481 struct file *file;
483 err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
484 file = ERR_PTR(err);
486 if (!err) {
487 struct inode *inode = nd.dentry->d_inode;
488 file = ERR_PTR(-EACCES);
489 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
490 S_ISREG(inode->i_mode)) {
491 int err = vfs_permission(&nd, MAY_EXEC);
492 file = ERR_PTR(err);
493 if (!err) {
494 file = nameidata_to_filp(&nd, O_RDONLY);
495 if (!IS_ERR(file)) {
496 err = deny_write_access(file);
497 if (err) {
498 fput(file);
499 file = ERR_PTR(err);
502 out:
503 return file;
506 release_open_intent(&nd);
507 path_release(&nd);
509 goto out;
512 EXPORT_SYMBOL(open_exec);
514 int kernel_read(struct file *file, unsigned long offset,
515 char *addr, unsigned long count)
517 mm_segment_t old_fs;
518 loff_t pos = offset;
519 int result;
521 old_fs = get_fs();
522 set_fs(get_ds());
523 /* The cast to a user pointer is valid due to the set_fs() */
524 result = vfs_read(file, (void __user *)addr, count, &pos);
525 set_fs(old_fs);
526 return result;
529 EXPORT_SYMBOL(kernel_read);
531 static int exec_mmap(struct mm_struct *mm)
533 struct task_struct *tsk;
534 struct mm_struct * old_mm, *active_mm;
536 /* Notify parent that we're no longer interested in the old VM */
537 tsk = current;
538 old_mm = current->mm;
539 mm_release(tsk, old_mm);
541 if (old_mm) {
543 * Make sure that if there is a core dump in progress
544 * for the old mm, we get out and die instead of going
545 * through with the exec. We must hold mmap_sem around
546 * checking core_waiters and changing tsk->mm. The
547 * core-inducing thread will increment core_waiters for
548 * each thread whose ->mm == old_mm.
550 down_read(&old_mm->mmap_sem);
551 if (unlikely(old_mm->core_waiters)) {
552 up_read(&old_mm->mmap_sem);
553 return -EINTR;
556 task_lock(tsk);
557 active_mm = tsk->active_mm;
558 tsk->mm = mm;
559 tsk->active_mm = mm;
560 activate_mm(active_mm, mm);
561 task_unlock(tsk);
562 arch_pick_mmap_layout(mm);
563 if (old_mm) {
564 up_read(&old_mm->mmap_sem);
565 BUG_ON(active_mm != old_mm);
566 mmput(old_mm);
567 return 0;
569 mmdrop(active_mm);
570 return 0;
574 * This function makes sure the current process has its own signal table,
575 * so that flush_signal_handlers can later reset the handlers without
576 * disturbing other processes. (Other processes might share the signal
577 * table via the CLONE_SIGHAND option to clone().)
579 static int de_thread(struct task_struct *tsk)
581 struct signal_struct *sig = tsk->signal;
582 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
583 spinlock_t *lock = &oldsighand->siglock;
584 struct task_struct *leader = NULL;
585 int count;
588 * If we don't share sighandlers, then we aren't sharing anything
589 * and we can just re-use it all.
591 if (atomic_read(&oldsighand->count) <= 1) {
592 exit_itimers(sig);
593 return 0;
596 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
597 if (!newsighand)
598 return -ENOMEM;
600 if (thread_group_empty(tsk))
601 goto no_thread_group;
604 * Kill all other threads in the thread group.
605 * We must hold tasklist_lock to call zap_other_threads.
607 read_lock(&tasklist_lock);
608 spin_lock_irq(lock);
609 if (signal_group_exit(sig)) {
611 * Another group action in progress, just
612 * return so that the signal is processed.
614 spin_unlock_irq(lock);
615 read_unlock(&tasklist_lock);
616 kmem_cache_free(sighand_cachep, newsighand);
617 return -EAGAIN;
621 * child_reaper ignores SIGKILL, change it now.
622 * Reparenting needs write_lock on tasklist_lock,
623 * so it is safe to do it under read_lock.
625 if (unlikely(tsk->group_leader == child_reaper(tsk)))
626 tsk->nsproxy->pid_ns->child_reaper = tsk;
628 sig->group_exit_task = tsk;
629 zap_other_threads(tsk);
630 read_unlock(&tasklist_lock);
633 * Account for the thread group leader hanging around:
635 count = 1;
636 if (!thread_group_leader(tsk)) {
637 count = 2;
639 * The SIGALRM timer survives the exec, but needs to point
640 * at us as the new group leader now. We have a race with
641 * a timer firing now getting the old leader, so we need to
642 * synchronize with any firing (by calling del_timer_sync)
643 * before we can safely let the old group leader die.
645 sig->tsk = tsk;
646 spin_unlock_irq(lock);
647 if (hrtimer_cancel(&sig->real_timer))
648 hrtimer_restart(&sig->real_timer);
649 spin_lock_irq(lock);
652 sig->notify_count = count;
653 while (atomic_read(&sig->count) > count) {
654 __set_current_state(TASK_UNINTERRUPTIBLE);
655 spin_unlock_irq(lock);
656 schedule();
657 spin_lock_irq(lock);
659 sig->group_exit_task = NULL;
660 sig->notify_count = 0;
661 spin_unlock_irq(lock);
664 * At this point all other threads have exited, all we have to
665 * do is to wait for the thread group leader to become inactive,
666 * and to assume its PID:
668 if (!thread_group_leader(tsk)) {
670 * Wait for the thread group leader to be a zombie.
671 * It should already be zombie at this point, most
672 * of the time.
674 leader = tsk->group_leader;
675 while (leader->exit_state != EXIT_ZOMBIE)
676 yield();
679 * The only record we have of the real-time age of a
680 * process, regardless of execs it's done, is start_time.
681 * All the past CPU time is accumulated in signal_struct
682 * from sister threads now dead. But in this non-leader
683 * exec, nothing survives from the original leader thread,
684 * whose birth marks the true age of this process now.
685 * When we take on its identity by switching to its PID, we
686 * also take its birthdate (always earlier than our own).
688 tsk->start_time = leader->start_time;
690 write_lock_irq(&tasklist_lock);
692 BUG_ON(leader->tgid != tsk->tgid);
693 BUG_ON(tsk->pid == tsk->tgid);
695 * An exec() starts a new thread group with the
696 * TGID of the previous thread group. Rehash the
697 * two threads with a switched PID, and release
698 * the former thread group leader:
701 /* Become a process group leader with the old leader's pid.
702 * The old leader becomes a thread of the this thread group.
703 * Note: The old leader also uses this pid until release_task
704 * is called. Odd but simple and correct.
706 detach_pid(tsk, PIDTYPE_PID);
707 tsk->pid = leader->pid;
708 attach_pid(tsk, PIDTYPE_PID, find_pid(tsk->pid));
709 transfer_pid(leader, tsk, PIDTYPE_PGID);
710 transfer_pid(leader, tsk, PIDTYPE_SID);
711 list_replace_rcu(&leader->tasks, &tsk->tasks);
713 tsk->group_leader = tsk;
714 leader->group_leader = tsk;
716 tsk->exit_signal = SIGCHLD;
718 BUG_ON(leader->exit_state != EXIT_ZOMBIE);
719 leader->exit_state = EXIT_DEAD;
721 write_unlock_irq(&tasklist_lock);
724 no_thread_group:
725 exit_itimers(sig);
726 if (leader)
727 release_task(leader);
729 if (atomic_read(&oldsighand->count) == 1) {
731 * Now that we nuked the rest of the thread group,
732 * it turns out we are not sharing sighand any more either.
733 * So we can just keep it.
735 kmem_cache_free(sighand_cachep, newsighand);
736 } else {
738 * Move our state over to newsighand and switch it in.
740 atomic_set(&newsighand->count, 1);
741 memcpy(newsighand->action, oldsighand->action,
742 sizeof(newsighand->action));
744 write_lock_irq(&tasklist_lock);
745 spin_lock(&oldsighand->siglock);
746 spin_lock_nested(&newsighand->siglock, SINGLE_DEPTH_NESTING);
748 rcu_assign_pointer(tsk->sighand, newsighand);
749 recalc_sigpending();
751 spin_unlock(&newsighand->siglock);
752 spin_unlock(&oldsighand->siglock);
753 write_unlock_irq(&tasklist_lock);
755 __cleanup_sighand(oldsighand);
758 BUG_ON(!thread_group_leader(tsk));
759 return 0;
763 * These functions flushes out all traces of the currently running executable
764 * so that a new one can be started
767 static void flush_old_files(struct files_struct * files)
769 long j = -1;
770 struct fdtable *fdt;
772 spin_lock(&files->file_lock);
773 for (;;) {
774 unsigned long set, i;
776 j++;
777 i = j * __NFDBITS;
778 fdt = files_fdtable(files);
779 if (i >= fdt->max_fds)
780 break;
781 set = fdt->close_on_exec->fds_bits[j];
782 if (!set)
783 continue;
784 fdt->close_on_exec->fds_bits[j] = 0;
785 spin_unlock(&files->file_lock);
786 for ( ; set ; i++,set >>= 1) {
787 if (set & 1) {
788 sys_close(i);
791 spin_lock(&files->file_lock);
794 spin_unlock(&files->file_lock);
797 void get_task_comm(char *buf, struct task_struct *tsk)
799 /* buf must be at least sizeof(tsk->comm) in size */
800 task_lock(tsk);
801 strncpy(buf, tsk->comm, sizeof(tsk->comm));
802 task_unlock(tsk);
805 void set_task_comm(struct task_struct *tsk, char *buf)
807 task_lock(tsk);
808 strlcpy(tsk->comm, buf, sizeof(tsk->comm));
809 task_unlock(tsk);
812 int flush_old_exec(struct linux_binprm * bprm)
814 char * name;
815 int i, ch, retval;
816 struct files_struct *files;
817 char tcomm[sizeof(current->comm)];
820 * Make sure we have a private signal table and that
821 * we are unassociated from the previous thread group.
823 retval = de_thread(current);
824 if (retval)
825 goto out;
828 * Make sure we have private file handles. Ask the
829 * fork helper to do the work for us and the exit
830 * helper to do the cleanup of the old one.
832 files = current->files; /* refcounted so safe to hold */
833 retval = unshare_files();
834 if (retval)
835 goto out;
837 * Release all of the old mmap stuff
839 retval = exec_mmap(bprm->mm);
840 if (retval)
841 goto mmap_failed;
843 bprm->mm = NULL; /* We're using it now */
845 /* This is the point of no return */
846 put_files_struct(files);
848 current->sas_ss_sp = current->sas_ss_size = 0;
850 if (current->euid == current->uid && current->egid == current->gid)
851 current->mm->dumpable = 1;
852 else
853 current->mm->dumpable = suid_dumpable;
855 name = bprm->filename;
857 /* Copies the binary name from after last slash */
858 for (i=0; (ch = *(name++)) != '\0';) {
859 if (ch == '/')
860 i = 0; /* overwrite what we wrote */
861 else
862 if (i < (sizeof(tcomm) - 1))
863 tcomm[i++] = ch;
865 tcomm[i] = '\0';
866 set_task_comm(current, tcomm);
868 current->flags &= ~PF_RANDOMIZE;
869 flush_thread();
871 /* Set the new mm task size. We have to do that late because it may
872 * depend on TIF_32BIT which is only updated in flush_thread() on
873 * some architectures like powerpc
875 current->mm->task_size = TASK_SIZE;
877 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
878 suid_keys(current);
879 current->mm->dumpable = suid_dumpable;
880 current->pdeath_signal = 0;
881 } else if (file_permission(bprm->file, MAY_READ) ||
882 (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
883 suid_keys(current);
884 current->mm->dumpable = suid_dumpable;
887 /* An exec changes our domain. We are no longer part of the thread
888 group */
890 current->self_exec_id++;
892 flush_signal_handlers(current, 0);
893 flush_old_files(current->files);
895 return 0;
897 mmap_failed:
898 reset_files_struct(current, files);
899 out:
900 return retval;
903 EXPORT_SYMBOL(flush_old_exec);
906 * Fill the binprm structure from the inode.
907 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
909 int prepare_binprm(struct linux_binprm *bprm)
911 int mode;
912 struct inode * inode = bprm->file->f_path.dentry->d_inode;
913 int retval;
915 mode = inode->i_mode;
916 if (bprm->file->f_op == NULL)
917 return -EACCES;
919 bprm->e_uid = current->euid;
920 bprm->e_gid = current->egid;
922 if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
923 /* Set-uid? */
924 if (mode & S_ISUID) {
925 current->personality &= ~PER_CLEAR_ON_SETID;
926 bprm->e_uid = inode->i_uid;
929 /* Set-gid? */
931 * If setgid is set but no group execute bit then this
932 * is a candidate for mandatory locking, not a setgid
933 * executable.
935 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
936 current->personality &= ~PER_CLEAR_ON_SETID;
937 bprm->e_gid = inode->i_gid;
941 /* fill in binprm security blob */
942 retval = security_bprm_set(bprm);
943 if (retval)
944 return retval;
946 memset(bprm->buf,0,BINPRM_BUF_SIZE);
947 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
950 EXPORT_SYMBOL(prepare_binprm);
952 static int unsafe_exec(struct task_struct *p)
954 int unsafe = 0;
955 if (p->ptrace & PT_PTRACED) {
956 if (p->ptrace & PT_PTRACE_CAP)
957 unsafe |= LSM_UNSAFE_PTRACE_CAP;
958 else
959 unsafe |= LSM_UNSAFE_PTRACE;
961 if (atomic_read(&p->fs->count) > 1 ||
962 atomic_read(&p->files->count) > 1 ||
963 atomic_read(&p->sighand->count) > 1)
964 unsafe |= LSM_UNSAFE_SHARE;
966 return unsafe;
969 void compute_creds(struct linux_binprm *bprm)
971 int unsafe;
973 if (bprm->e_uid != current->uid) {
974 suid_keys(current);
975 current->pdeath_signal = 0;
977 exec_keys(current);
979 task_lock(current);
980 unsafe = unsafe_exec(current);
981 security_bprm_apply_creds(bprm, unsafe);
982 task_unlock(current);
983 security_bprm_post_apply_creds(bprm);
985 EXPORT_SYMBOL(compute_creds);
988 * Arguments are '\0' separated strings found at the location bprm->p
989 * points to; chop off the first by relocating brpm->p to right after
990 * the first '\0' encountered.
992 void remove_arg_zero(struct linux_binprm *bprm)
994 if (bprm->argc) {
995 char ch;
997 do {
998 unsigned long offset;
999 unsigned long index;
1000 char *kaddr;
1001 struct page *page;
1003 offset = bprm->p & ~PAGE_MASK;
1004 index = bprm->p >> PAGE_SHIFT;
1006 page = bprm->page[index];
1007 kaddr = kmap_atomic(page, KM_USER0);
1009 /* run through page until we reach end or find NUL */
1010 do {
1011 ch = *(kaddr + offset);
1013 /* discard that character... */
1014 bprm->p++;
1015 offset++;
1016 } while (offset < PAGE_SIZE && ch != '\0');
1018 kunmap_atomic(kaddr, KM_USER0);
1020 /* free the old page */
1021 if (offset == PAGE_SIZE) {
1022 __free_page(page);
1023 bprm->page[index] = NULL;
1025 } while (ch != '\0');
1027 bprm->argc--;
1030 EXPORT_SYMBOL(remove_arg_zero);
1033 * cycle the list of binary formats handler, until one recognizes the image
1035 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
1037 int try,retval;
1038 struct linux_binfmt *fmt;
1039 #ifdef __alpha__
1040 /* handle /sbin/loader.. */
1042 struct exec * eh = (struct exec *) bprm->buf;
1044 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
1045 (eh->fh.f_flags & 0x3000) == 0x3000)
1047 struct file * file;
1048 unsigned long loader;
1050 allow_write_access(bprm->file);
1051 fput(bprm->file);
1052 bprm->file = NULL;
1054 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1056 file = open_exec("/sbin/loader");
1057 retval = PTR_ERR(file);
1058 if (IS_ERR(file))
1059 return retval;
1061 /* Remember if the application is TASO. */
1062 bprm->sh_bang = eh->ah.entry < 0x100000000UL;
1064 bprm->file = file;
1065 bprm->loader = loader;
1066 retval = prepare_binprm(bprm);
1067 if (retval<0)
1068 return retval;
1069 /* should call search_binary_handler recursively here,
1070 but it does not matter */
1073 #endif
1074 retval = security_bprm_check(bprm);
1075 if (retval)
1076 return retval;
1078 /* kernel module loader fixup */
1079 /* so we don't try to load run modprobe in kernel space. */
1080 set_fs(USER_DS);
1082 retval = audit_bprm(bprm);
1083 if (retval)
1084 return retval;
1086 retval = -ENOENT;
1087 for (try=0; try<2; try++) {
1088 read_lock(&binfmt_lock);
1089 for (fmt = formats ; fmt ; fmt = fmt->next) {
1090 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1091 if (!fn)
1092 continue;
1093 if (!try_module_get(fmt->module))
1094 continue;
1095 read_unlock(&binfmt_lock);
1096 retval = fn(bprm, regs);
1097 if (retval >= 0) {
1098 put_binfmt(fmt);
1099 allow_write_access(bprm->file);
1100 if (bprm->file)
1101 fput(bprm->file);
1102 bprm->file = NULL;
1103 current->did_exec = 1;
1104 proc_exec_connector(current);
1105 return retval;
1107 read_lock(&binfmt_lock);
1108 put_binfmt(fmt);
1109 if (retval != -ENOEXEC || bprm->mm == NULL)
1110 break;
1111 if (!bprm->file) {
1112 read_unlock(&binfmt_lock);
1113 return retval;
1116 read_unlock(&binfmt_lock);
1117 if (retval != -ENOEXEC || bprm->mm == NULL) {
1118 break;
1119 #ifdef CONFIG_KMOD
1120 }else{
1121 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1122 if (printable(bprm->buf[0]) &&
1123 printable(bprm->buf[1]) &&
1124 printable(bprm->buf[2]) &&
1125 printable(bprm->buf[3]))
1126 break; /* -ENOEXEC */
1127 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1128 #endif
1131 return retval;
1134 EXPORT_SYMBOL(search_binary_handler);
1137 * sys_execve() executes a new program.
1139 int do_execve(char * filename,
1140 char __user *__user *argv,
1141 char __user *__user *envp,
1142 struct pt_regs * regs)
1144 struct linux_binprm *bprm;
1145 struct file *file;
1146 int retval;
1147 int i;
1149 retval = -ENOMEM;
1150 bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
1151 if (!bprm)
1152 goto out_ret;
1154 file = open_exec(filename);
1155 retval = PTR_ERR(file);
1156 if (IS_ERR(file))
1157 goto out_kfree;
1159 sched_exec();
1161 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1163 bprm->file = file;
1164 bprm->filename = filename;
1165 bprm->interp = filename;
1166 bprm->mm = mm_alloc();
1167 retval = -ENOMEM;
1168 if (!bprm->mm)
1169 goto out_file;
1171 retval = init_new_context(current, bprm->mm);
1172 if (retval < 0)
1173 goto out_mm;
1175 bprm->argc = count(argv, bprm->p / sizeof(void *));
1176 if ((retval = bprm->argc) < 0)
1177 goto out_mm;
1179 bprm->envc = count(envp, bprm->p / sizeof(void *));
1180 if ((retval = bprm->envc) < 0)
1181 goto out_mm;
1183 retval = security_bprm_alloc(bprm);
1184 if (retval)
1185 goto out;
1187 retval = prepare_binprm(bprm);
1188 if (retval < 0)
1189 goto out;
1191 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1192 if (retval < 0)
1193 goto out;
1195 bprm->exec = bprm->p;
1196 retval = copy_strings(bprm->envc, envp, bprm);
1197 if (retval < 0)
1198 goto out;
1200 retval = copy_strings(bprm->argc, argv, bprm);
1201 if (retval < 0)
1202 goto out;
1204 retval = search_binary_handler(bprm,regs);
1205 if (retval >= 0) {
1206 free_arg_pages(bprm);
1208 /* execve success */
1209 security_bprm_free(bprm);
1210 acct_update_integrals(current);
1211 kfree(bprm);
1212 return retval;
1215 out:
1216 /* Something went wrong, return the inode and free the argument pages*/
1217 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1218 struct page * page = bprm->page[i];
1219 if (page)
1220 __free_page(page);
1223 if (bprm->security)
1224 security_bprm_free(bprm);
1226 out_mm:
1227 if (bprm->mm)
1228 mmdrop(bprm->mm);
1230 out_file:
1231 if (bprm->file) {
1232 allow_write_access(bprm->file);
1233 fput(bprm->file);
1236 out_kfree:
1237 kfree(bprm);
1239 out_ret:
1240 return retval;
1243 int set_binfmt(struct linux_binfmt *new)
1245 struct linux_binfmt *old = current->binfmt;
1247 if (new) {
1248 if (!try_module_get(new->module))
1249 return -1;
1251 current->binfmt = new;
1252 if (old)
1253 module_put(old->module);
1254 return 0;
1257 EXPORT_SYMBOL(set_binfmt);
1259 /* format_corename will inspect the pattern parameter, and output a
1260 * name into corename, which must have space for at least
1261 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1263 static int format_corename(char *corename, const char *pattern, long signr)
1265 const char *pat_ptr = pattern;
1266 char *out_ptr = corename;
1267 char *const out_end = corename + CORENAME_MAX_SIZE;
1268 int rc;
1269 int pid_in_pattern = 0;
1270 int ispipe = 0;
1272 if (*pattern == '|')
1273 ispipe = 1;
1275 /* Repeat as long as we have more pattern to process and more output
1276 space */
1277 while (*pat_ptr) {
1278 if (*pat_ptr != '%') {
1279 if (out_ptr == out_end)
1280 goto out;
1281 *out_ptr++ = *pat_ptr++;
1282 } else {
1283 switch (*++pat_ptr) {
1284 case 0:
1285 goto out;
1286 /* Double percent, output one percent */
1287 case '%':
1288 if (out_ptr == out_end)
1289 goto out;
1290 *out_ptr++ = '%';
1291 break;
1292 /* pid */
1293 case 'p':
1294 pid_in_pattern = 1;
1295 rc = snprintf(out_ptr, out_end - out_ptr,
1296 "%d", current->tgid);
1297 if (rc > out_end - out_ptr)
1298 goto out;
1299 out_ptr += rc;
1300 break;
1301 /* uid */
1302 case 'u':
1303 rc = snprintf(out_ptr, out_end - out_ptr,
1304 "%d", current->uid);
1305 if (rc > out_end - out_ptr)
1306 goto out;
1307 out_ptr += rc;
1308 break;
1309 /* gid */
1310 case 'g':
1311 rc = snprintf(out_ptr, out_end - out_ptr,
1312 "%d", current->gid);
1313 if (rc > out_end - out_ptr)
1314 goto out;
1315 out_ptr += rc;
1316 break;
1317 /* signal that caused the coredump */
1318 case 's':
1319 rc = snprintf(out_ptr, out_end - out_ptr,
1320 "%ld", signr);
1321 if (rc > out_end - out_ptr)
1322 goto out;
1323 out_ptr += rc;
1324 break;
1325 /* UNIX time of coredump */
1326 case 't': {
1327 struct timeval tv;
1328 do_gettimeofday(&tv);
1329 rc = snprintf(out_ptr, out_end - out_ptr,
1330 "%lu", tv.tv_sec);
1331 if (rc > out_end - out_ptr)
1332 goto out;
1333 out_ptr += rc;
1334 break;
1336 /* hostname */
1337 case 'h':
1338 down_read(&uts_sem);
1339 rc = snprintf(out_ptr, out_end - out_ptr,
1340 "%s", utsname()->nodename);
1341 up_read(&uts_sem);
1342 if (rc > out_end - out_ptr)
1343 goto out;
1344 out_ptr += rc;
1345 break;
1346 /* executable */
1347 case 'e':
1348 rc = snprintf(out_ptr, out_end - out_ptr,
1349 "%s", current->comm);
1350 if (rc > out_end - out_ptr)
1351 goto out;
1352 out_ptr += rc;
1353 break;
1354 default:
1355 break;
1357 ++pat_ptr;
1360 /* Backward compatibility with core_uses_pid:
1362 * If core_pattern does not include a %p (as is the default)
1363 * and core_uses_pid is set, then .%pid will be appended to
1364 * the filename. Do not do this for piped commands. */
1365 if (!ispipe && !pid_in_pattern
1366 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1367 rc = snprintf(out_ptr, out_end - out_ptr,
1368 ".%d", current->tgid);
1369 if (rc > out_end - out_ptr)
1370 goto out;
1371 out_ptr += rc;
1373 out:
1374 *out_ptr = 0;
1375 return ispipe;
1378 static void zap_process(struct task_struct *start)
1380 struct task_struct *t;
1382 start->signal->flags = SIGNAL_GROUP_EXIT;
1383 start->signal->group_stop_count = 0;
1385 t = start;
1386 do {
1387 if (t != current && t->mm) {
1388 t->mm->core_waiters++;
1389 sigaddset(&t->pending.signal, SIGKILL);
1390 signal_wake_up(t, 1);
1392 } while ((t = next_thread(t)) != start);
1395 static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
1396 int exit_code)
1398 struct task_struct *g, *p;
1399 unsigned long flags;
1400 int err = -EAGAIN;
1402 spin_lock_irq(&tsk->sighand->siglock);
1403 if (!signal_group_exit(tsk->signal)) {
1404 tsk->signal->group_exit_code = exit_code;
1405 zap_process(tsk);
1406 err = 0;
1408 spin_unlock_irq(&tsk->sighand->siglock);
1409 if (err)
1410 return err;
1412 if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
1413 goto done;
1415 rcu_read_lock();
1416 for_each_process(g) {
1417 if (g == tsk->group_leader)
1418 continue;
1420 p = g;
1421 do {
1422 if (p->mm) {
1423 if (p->mm == mm) {
1425 * p->sighand can't disappear, but
1426 * may be changed by de_thread()
1428 lock_task_sighand(p, &flags);
1429 zap_process(p);
1430 unlock_task_sighand(p, &flags);
1432 break;
1434 } while ((p = next_thread(p)) != g);
1436 rcu_read_unlock();
1437 done:
1438 return mm->core_waiters;
1441 static int coredump_wait(int exit_code)
1443 struct task_struct *tsk = current;
1444 struct mm_struct *mm = tsk->mm;
1445 struct completion startup_done;
1446 struct completion *vfork_done;
1447 int core_waiters;
1449 init_completion(&mm->core_done);
1450 init_completion(&startup_done);
1451 mm->core_startup_done = &startup_done;
1453 core_waiters = zap_threads(tsk, mm, exit_code);
1454 up_write(&mm->mmap_sem);
1456 if (unlikely(core_waiters < 0))
1457 goto fail;
1460 * Make sure nobody is waiting for us to release the VM,
1461 * otherwise we can deadlock when we wait on each other
1463 vfork_done = tsk->vfork_done;
1464 if (vfork_done) {
1465 tsk->vfork_done = NULL;
1466 complete(vfork_done);
1469 if (core_waiters)
1470 wait_for_completion(&startup_done);
1471 fail:
1472 BUG_ON(mm->core_waiters);
1473 return core_waiters;
1476 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1478 char corename[CORENAME_MAX_SIZE + 1];
1479 struct mm_struct *mm = current->mm;
1480 struct linux_binfmt * binfmt;
1481 struct inode * inode;
1482 struct file * file;
1483 int retval = 0;
1484 int fsuid = current->fsuid;
1485 int flag = 0;
1486 int ispipe = 0;
1488 audit_core_dumps(signr);
1490 binfmt = current->binfmt;
1491 if (!binfmt || !binfmt->core_dump)
1492 goto fail;
1493 down_write(&mm->mmap_sem);
1494 if (!mm->dumpable) {
1495 up_write(&mm->mmap_sem);
1496 goto fail;
1500 * We cannot trust fsuid as being the "true" uid of the
1501 * process nor do we know its entire history. We only know it
1502 * was tainted so we dump it as root in mode 2.
1504 if (mm->dumpable == 2) { /* Setuid core dump mode */
1505 flag = O_EXCL; /* Stop rewrite attacks */
1506 current->fsuid = 0; /* Dump root private */
1508 mm->dumpable = 0;
1510 retval = coredump_wait(exit_code);
1511 if (retval < 0)
1512 goto fail;
1515 * Clear any false indication of pending signals that might
1516 * be seen by the filesystem code called to write the core file.
1518 clear_thread_flag(TIF_SIGPENDING);
1520 if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1521 goto fail_unlock;
1524 * lock_kernel() because format_corename() is controlled by sysctl, which
1525 * uses lock_kernel()
1527 lock_kernel();
1528 ispipe = format_corename(corename, core_pattern, signr);
1529 unlock_kernel();
1530 if (ispipe) {
1531 /* SIGPIPE can happen, but it's just never processed */
1532 if(call_usermodehelper_pipe(corename+1, NULL, NULL, &file)) {
1533 printk(KERN_INFO "Core dump to %s pipe failed\n",
1534 corename);
1535 goto fail_unlock;
1537 } else
1538 file = filp_open(corename,
1539 O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
1540 0600);
1541 if (IS_ERR(file))
1542 goto fail_unlock;
1543 inode = file->f_path.dentry->d_inode;
1544 if (inode->i_nlink > 1)
1545 goto close_fail; /* multiple links - don't dump */
1546 if (!ispipe && d_unhashed(file->f_path.dentry))
1547 goto close_fail;
1549 /* AK: actually i see no reason to not allow this for named pipes etc.,
1550 but keep the previous behaviour for now. */
1551 if (!ispipe && !S_ISREG(inode->i_mode))
1552 goto close_fail;
1554 * Dont allow local users get cute and trick others to coredump
1555 * into their pre-created files:
1557 if (inode->i_uid != current->fsuid)
1558 goto close_fail;
1559 if (!file->f_op)
1560 goto close_fail;
1561 if (!file->f_op->write)
1562 goto close_fail;
1563 if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
1564 goto close_fail;
1566 retval = binfmt->core_dump(signr, regs, file);
1568 if (retval)
1569 current->signal->group_exit_code |= 0x80;
1570 close_fail:
1571 filp_close(file, NULL);
1572 fail_unlock:
1573 current->fsuid = fsuid;
1574 complete_all(&mm->core_done);
1575 fail:
1576 return retval;