[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / exec.c
blob225afb0d94e5583974c83ef45126b7ed886765ee
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/personality.h>
38 #include <linux/binfmts.h>
39 #include <linux/swap.h>
40 #include <linux/utsname.h>
41 #include <linux/module.h>
42 #include <linux/namei.h>
43 #include <linux/proc_fs.h>
44 #include <linux/ptrace.h>
45 #include <linux/mount.h>
46 #include <linux/security.h>
47 #include <linux/syscalls.h>
48 #include <linux/rmap-locking.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgalloc.h>
52 #include <asm/mmu_context.h>
54 #ifdef CONFIG_KMOD
55 #include <linux/kmod.h>
56 #endif
58 int core_uses_pid;
59 char core_pattern[65] = "core";
60 /* The maximal length of core_pattern is also specified in sysctl.c */
62 static struct linux_binfmt *formats;
63 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
65 int register_binfmt(struct linux_binfmt * fmt)
67 struct linux_binfmt ** tmp = &formats;
69 if (!fmt)
70 return -EINVAL;
71 if (fmt->next)
72 return -EBUSY;
73 write_lock(&binfmt_lock);
74 while (*tmp) {
75 if (fmt == *tmp) {
76 write_unlock(&binfmt_lock);
77 return -EBUSY;
79 tmp = &(*tmp)->next;
81 fmt->next = formats;
82 formats = fmt;
83 write_unlock(&binfmt_lock);
84 return 0;
87 EXPORT_SYMBOL(register_binfmt);
89 int unregister_binfmt(struct linux_binfmt * fmt)
91 struct linux_binfmt ** tmp = &formats;
93 write_lock(&binfmt_lock);
94 while (*tmp) {
95 if (fmt == *tmp) {
96 *tmp = fmt->next;
97 write_unlock(&binfmt_lock);
98 return 0;
100 tmp = &(*tmp)->next;
102 write_unlock(&binfmt_lock);
103 return -EINVAL;
106 EXPORT_SYMBOL(unregister_binfmt);
108 static inline void put_binfmt(struct linux_binfmt * fmt)
110 module_put(fmt->module);
114 * Note that a shared library must be both readable and executable due to
115 * security reasons.
117 * Also note that we take the address to load from from the file itself.
119 asmlinkage long sys_uselib(const char __user * library)
121 struct file * file;
122 struct nameidata nd;
123 int error;
125 nd.intent.open.flags = FMODE_READ;
126 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
127 if (error)
128 goto out;
130 error = -EINVAL;
131 if (!S_ISREG(nd.dentry->d_inode->i_mode))
132 goto exit;
134 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC, &nd);
135 if (error)
136 goto exit;
138 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
139 error = PTR_ERR(file);
140 if (IS_ERR(file))
141 goto out;
143 error = -ENOEXEC;
144 if(file->f_op) {
145 struct linux_binfmt * fmt;
147 read_lock(&binfmt_lock);
148 for (fmt = formats ; fmt ; fmt = fmt->next) {
149 if (!fmt->load_shlib)
150 continue;
151 if (!try_module_get(fmt->module))
152 continue;
153 read_unlock(&binfmt_lock);
154 error = fmt->load_shlib(file);
155 read_lock(&binfmt_lock);
156 put_binfmt(fmt);
157 if (error != -ENOEXEC)
158 break;
160 read_unlock(&binfmt_lock);
162 fput(file);
163 out:
164 return error;
165 exit:
166 path_release(&nd);
167 goto out;
171 * count() counts the number of strings in array ARGV.
173 static int count(char __user * __user * argv, int max)
175 int i = 0;
177 if (argv != NULL) {
178 for (;;) {
179 char __user * p;
181 if (get_user(p, argv))
182 return -EFAULT;
183 if (!p)
184 break;
185 argv++;
186 if(++i > max)
187 return -E2BIG;
190 return i;
194 * 'copy_strings()' copies argument/environment strings from user
195 * memory to free pages in kernel mem. These are in a format ready
196 * to be put directly into the top of new user memory.
198 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
200 struct page *kmapped_page = NULL;
201 char *kaddr = NULL;
202 int ret;
204 while (argc-- > 0) {
205 char __user *str;
206 int len;
207 unsigned long pos;
209 if (get_user(str, argv+argc) ||
210 !(len = strnlen_user(str, bprm->p))) {
211 ret = -EFAULT;
212 goto out;
215 if (bprm->p < len) {
216 ret = -E2BIG;
217 goto out;
220 bprm->p -= len;
221 /* XXX: add architecture specific overflow check here. */
222 pos = bprm->p;
224 while (len > 0) {
225 int i, new, err;
226 int offset, bytes_to_copy;
227 struct page *page;
229 offset = pos % PAGE_SIZE;
230 i = pos/PAGE_SIZE;
231 page = bprm->page[i];
232 new = 0;
233 if (!page) {
234 page = alloc_page(GFP_HIGHUSER);
235 bprm->page[i] = page;
236 if (!page) {
237 ret = -ENOMEM;
238 goto out;
240 new = 1;
243 if (page != kmapped_page) {
244 if (kmapped_page)
245 kunmap(kmapped_page);
246 kmapped_page = page;
247 kaddr = kmap(kmapped_page);
249 if (new && offset)
250 memset(kaddr, 0, offset);
251 bytes_to_copy = PAGE_SIZE - offset;
252 if (bytes_to_copy > len) {
253 bytes_to_copy = len;
254 if (new)
255 memset(kaddr+offset+len, 0,
256 PAGE_SIZE-offset-len);
258 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
259 if (err) {
260 ret = -EFAULT;
261 goto out;
264 pos += bytes_to_copy;
265 str += bytes_to_copy;
266 len -= bytes_to_copy;
269 ret = 0;
270 out:
271 if (kmapped_page)
272 kunmap(kmapped_page);
273 return ret;
277 * Like copy_strings, but get argv and its values from kernel memory.
279 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
281 int r;
282 mm_segment_t oldfs = get_fs();
283 set_fs(KERNEL_DS);
284 r = copy_strings(argc, (char __user * __user *)argv, bprm);
285 set_fs(oldfs);
286 return r;
289 EXPORT_SYMBOL(copy_strings_kernel);
291 #ifdef CONFIG_MMU
293 * This routine is used to map in a page into an address space: needed by
294 * execve() for the initial stack and environment pages.
296 * tsk->mmap_sem is held for writing.
298 void put_dirty_page(struct task_struct *tsk, struct page *page,
299 unsigned long address, pgprot_t prot)
301 pgd_t * pgd;
302 pmd_t * pmd;
303 pte_t * pte;
304 struct pte_chain *pte_chain;
306 if (page_count(page) != 1)
307 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n",
308 page, address);
310 pgd = pgd_offset(tsk->mm, address);
311 pte_chain = pte_chain_alloc(GFP_KERNEL);
312 if (!pte_chain)
313 goto out_sig;
314 spin_lock(&tsk->mm->page_table_lock);
315 pmd = pmd_alloc(tsk->mm, pgd, address);
316 if (!pmd)
317 goto out;
318 pte = pte_alloc_map(tsk->mm, pmd, address);
319 if (!pte)
320 goto out;
321 if (!pte_none(*pte)) {
322 pte_unmap(pte);
323 goto out;
325 lru_cache_add_active(page);
326 flush_dcache_page(page);
327 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, prot))));
328 pte_chain = page_add_rmap(page, pte, pte_chain);
329 pte_unmap(pte);
330 tsk->mm->rss++;
331 spin_unlock(&tsk->mm->page_table_lock);
333 /* no need for flush_tlb */
334 pte_chain_free(pte_chain);
335 return;
336 out:
337 spin_unlock(&tsk->mm->page_table_lock);
338 out_sig:
339 __free_page(page);
340 force_sig(SIGKILL, tsk);
341 pte_chain_free(pte_chain);
342 return;
345 int setup_arg_pages(struct linux_binprm *bprm)
347 unsigned long stack_base;
348 struct vm_area_struct *mpnt;
349 struct mm_struct *mm = current->mm;
350 int i;
351 long arg_size;
353 #ifdef CONFIG_STACK_GROWSUP
354 /* Move the argument and environment strings to the bottom of the
355 * stack space.
357 int offset, j;
358 char *to, *from;
360 /* Start by shifting all the pages down */
361 i = 0;
362 for (j = 0; j < MAX_ARG_PAGES; j++) {
363 struct page *page = bprm->page[j];
364 if (!page)
365 continue;
366 bprm->page[i++] = page;
369 /* Now move them within their pages */
370 offset = bprm->p % PAGE_SIZE;
371 to = kmap(bprm->page[0]);
372 for (j = 1; j < i; j++) {
373 memmove(to, to + offset, PAGE_SIZE - offset);
374 from = kmap(bprm->page[j]);
375 memcpy(to + PAGE_SIZE - offset, from, offset);
376 kunmap(bprm->page[j - 1]);
377 to = from;
379 memmove(to, to + offset, PAGE_SIZE - offset);
380 kunmap(bprm->page[j - 1]);
382 /* Adjust bprm->p to point to the end of the strings. */
383 bprm->p = PAGE_SIZE * i - offset;
385 /* Limit stack size to 1GB */
386 stack_base = current->rlim[RLIMIT_STACK].rlim_max;
387 if (stack_base > (1 << 30))
388 stack_base = 1 << 30;
389 stack_base = PAGE_ALIGN(STACK_TOP - stack_base);
391 mm->arg_start = stack_base;
392 arg_size = i << PAGE_SHIFT;
394 /* zero pages that were copied above */
395 while (i < MAX_ARG_PAGES)
396 bprm->page[i++] = NULL;
397 #else
398 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
399 mm->arg_start = bprm->p + stack_base;
400 arg_size = STACK_TOP - (PAGE_MASK & (unsigned long) mm->arg_start);
401 #endif
403 bprm->p += stack_base;
404 if (bprm->loader)
405 bprm->loader += stack_base;
406 bprm->exec += stack_base;
408 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
409 if (!mpnt)
410 return -ENOMEM;
412 if (security_vm_enough_memory(arg_size >> PAGE_SHIFT)) {
413 kmem_cache_free(vm_area_cachep, mpnt);
414 return -ENOMEM;
417 down_write(&mm->mmap_sem);
419 mpnt->vm_mm = mm;
420 #ifdef CONFIG_STACK_GROWSUP
421 mpnt->vm_start = stack_base;
422 mpnt->vm_end = PAGE_MASK &
423 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
424 #else
425 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
426 mpnt->vm_end = STACK_TOP;
427 #endif
428 mpnt->vm_page_prot = protection_map[VM_STACK_FLAGS & 0x7];
429 mpnt->vm_flags = VM_STACK_FLAGS;
430 mpnt->vm_ops = NULL;
431 mpnt->vm_pgoff = 0;
432 mpnt->vm_file = NULL;
433 INIT_LIST_HEAD(&mpnt->shared);
434 mpnt->vm_private_data = (void *) 0;
435 insert_vm_struct(mm, mpnt);
436 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
439 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
440 struct page *page = bprm->page[i];
441 if (page) {
442 bprm->page[i] = NULL;
443 put_dirty_page(current, page, stack_base,
444 mpnt->vm_page_prot);
446 stack_base += PAGE_SIZE;
448 up_write(&mm->mmap_sem);
450 return 0;
453 EXPORT_SYMBOL(setup_arg_pages);
455 #define free_arg_pages(bprm) do { } while (0)
457 #else
459 static inline void free_arg_pages(struct linux_binprm *bprm)
461 int i;
463 for (i = 0; i < MAX_ARG_PAGES; i++) {
464 if (bprm->page[i])
465 __free_page(bprm->page[i]);
466 bprm->page[i] = NULL;
470 #endif /* CONFIG_MMU */
472 struct file *open_exec(const char *name)
474 struct nameidata nd;
475 int err;
476 struct file *file;
478 nd.intent.open.flags = FMODE_READ;
479 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
480 file = ERR_PTR(err);
482 if (!err) {
483 struct inode *inode = nd.dentry->d_inode;
484 file = ERR_PTR(-EACCES);
485 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
486 S_ISREG(inode->i_mode)) {
487 int err = permission(inode, MAY_EXEC, &nd);
488 if (!err && !(inode->i_mode & 0111))
489 err = -EACCES;
490 file = ERR_PTR(err);
491 if (!err) {
492 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
493 if (!IS_ERR(file)) {
494 err = deny_write_access(file);
495 if (err) {
496 fput(file);
497 file = ERR_PTR(err);
500 out:
501 return file;
504 path_release(&nd);
506 goto out;
509 EXPORT_SYMBOL(open_exec);
511 int kernel_read(struct file *file, unsigned long offset,
512 char *addr, unsigned long count)
514 mm_segment_t old_fs;
515 loff_t pos = offset;
516 int result;
518 old_fs = get_fs();
519 set_fs(get_ds());
520 /* The cast to a user pointer is valid due to the set_fs() */
521 result = vfs_read(file, (void __user *)addr, count, &pos);
522 set_fs(old_fs);
523 return result;
526 EXPORT_SYMBOL(kernel_read);
528 static int exec_mmap(struct mm_struct *mm)
530 struct task_struct *tsk;
531 struct mm_struct * old_mm, *active_mm;
533 /* Add it to the list of mm's */
534 spin_lock(&mmlist_lock);
535 list_add(&mm->mmlist, &init_mm.mmlist);
536 mmlist_nr++;
537 spin_unlock(&mmlist_lock);
539 /* Notify parent that we're no longer interested in the old VM */
540 tsk = current;
541 old_mm = current->mm;
542 mm_release(tsk, old_mm);
544 task_lock(tsk);
545 active_mm = tsk->active_mm;
546 tsk->mm = mm;
547 tsk->active_mm = mm;
548 activate_mm(active_mm, mm);
549 task_unlock(tsk);
550 if (old_mm) {
551 if (active_mm != old_mm) BUG();
552 mmput(old_mm);
553 return 0;
555 mmdrop(active_mm);
556 return 0;
560 * This function makes sure the current process has its own signal table,
561 * so that flush_signal_handlers can later reset the handlers without
562 * disturbing other processes. (Other processes might share the signal
563 * table via the CLONE_SIGHAND option to clone().)
565 static inline int de_thread(struct task_struct *tsk)
567 struct signal_struct *newsig, *oldsig = tsk->signal;
568 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
569 spinlock_t *lock = &oldsighand->siglock;
570 int count;
573 * If we don't share sighandlers, then we aren't sharing anything
574 * and we can just re-use it all.
576 if (atomic_read(&oldsighand->count) <= 1)
577 return 0;
579 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
580 if (!newsighand)
581 return -ENOMEM;
583 spin_lock_init(&newsighand->siglock);
584 atomic_set(&newsighand->count, 1);
585 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
588 * See if we need to allocate a new signal structure
590 newsig = NULL;
591 if (atomic_read(&oldsig->count) > 1) {
592 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
593 if (!newsig) {
594 kmem_cache_free(sighand_cachep, newsighand);
595 return -ENOMEM;
597 atomic_set(&newsig->count, 1);
598 newsig->group_exit = 0;
599 newsig->group_exit_code = 0;
600 newsig->group_exit_task = NULL;
601 newsig->group_stop_count = 0;
602 newsig->curr_target = NULL;
603 init_sigpending(&newsig->shared_pending);
606 if (thread_group_empty(current))
607 goto no_thread_group;
610 * Kill all other threads in the thread group.
611 * We must hold tasklist_lock to call zap_other_threads.
613 read_lock(&tasklist_lock);
614 spin_lock_irq(lock);
615 if (oldsig->group_exit) {
617 * Another group action in progress, just
618 * return so that the signal is processed.
620 spin_unlock_irq(lock);
621 read_unlock(&tasklist_lock);
622 kmem_cache_free(sighand_cachep, newsighand);
623 if (newsig)
624 kmem_cache_free(signal_cachep, newsig);
625 return -EAGAIN;
627 oldsig->group_exit = 1;
628 zap_other_threads(current);
629 read_unlock(&tasklist_lock);
632 * Account for the thread group leader hanging around:
634 count = 2;
635 if (current->pid == current->tgid)
636 count = 1;
637 while (atomic_read(&oldsig->count) > count) {
638 oldsig->group_exit_task = current;
639 oldsig->notify_count = count;
640 __set_current_state(TASK_UNINTERRUPTIBLE);
641 spin_unlock_irq(lock);
642 schedule();
643 spin_lock_irq(lock);
645 spin_unlock_irq(lock);
648 * At this point all other threads have exited, all we have to
649 * do is to wait for the thread group leader to become inactive,
650 * and to assume its PID:
652 if (current->pid != current->tgid) {
653 struct task_struct *leader = current->group_leader, *parent;
654 struct dentry *proc_dentry1, *proc_dentry2;
655 unsigned long state, ptrace;
658 * Wait for the thread group leader to be a zombie.
659 * It should already be zombie at this point, most
660 * of the time.
662 while (leader->state != TASK_ZOMBIE)
663 yield();
665 spin_lock(&leader->proc_lock);
666 spin_lock(&current->proc_lock);
667 proc_dentry1 = proc_pid_unhash(current);
668 proc_dentry2 = proc_pid_unhash(leader);
669 write_lock_irq(&tasklist_lock);
671 if (leader->tgid != current->tgid)
672 BUG();
673 if (current->pid == current->tgid)
674 BUG();
676 * An exec() starts a new thread group with the
677 * TGID of the previous thread group. Rehash the
678 * two threads with a switched PID, and release
679 * the former thread group leader:
681 ptrace = leader->ptrace;
682 parent = leader->parent;
684 ptrace_unlink(current);
685 ptrace_unlink(leader);
686 remove_parent(current);
687 remove_parent(leader);
689 switch_exec_pids(leader, current);
691 current->parent = current->real_parent = leader->real_parent;
692 leader->parent = leader->real_parent = child_reaper;
693 current->group_leader = current;
694 leader->group_leader = leader;
696 add_parent(current, current->parent);
697 add_parent(leader, leader->parent);
698 if (ptrace) {
699 current->ptrace = ptrace;
700 __ptrace_link(current, parent);
703 list_del(&current->tasks);
704 list_add_tail(&current->tasks, &init_task.tasks);
705 current->exit_signal = SIGCHLD;
706 state = leader->state;
708 write_unlock_irq(&tasklist_lock);
709 spin_unlock(&leader->proc_lock);
710 spin_unlock(&current->proc_lock);
711 proc_pid_flush(proc_dentry1);
712 proc_pid_flush(proc_dentry2);
714 if (state != TASK_ZOMBIE)
715 BUG();
716 release_task(leader);
719 no_thread_group:
721 write_lock_irq(&tasklist_lock);
722 spin_lock(&oldsighand->siglock);
723 spin_lock(&newsighand->siglock);
725 if (current == oldsig->curr_target)
726 oldsig->curr_target = next_thread(current);
727 if (newsig)
728 current->signal = newsig;
729 current->sighand = newsighand;
730 init_sigpending(&current->pending);
731 recalc_sigpending();
733 spin_unlock(&newsighand->siglock);
734 spin_unlock(&oldsighand->siglock);
735 write_unlock_irq(&tasklist_lock);
737 if (newsig && atomic_dec_and_test(&oldsig->count))
738 kmem_cache_free(signal_cachep, oldsig);
740 if (atomic_dec_and_test(&oldsighand->count))
741 kmem_cache_free(sighand_cachep, oldsighand);
743 if (!thread_group_empty(current))
744 BUG();
745 if (current->tgid != current->pid)
746 BUG();
747 return 0;
751 * These functions flushes out all traces of the currently running executable
752 * so that a new one can be started
755 static inline void flush_old_files(struct files_struct * files)
757 long j = -1;
759 spin_lock(&files->file_lock);
760 for (;;) {
761 unsigned long set, i;
763 j++;
764 i = j * __NFDBITS;
765 if (i >= files->max_fds || i >= files->max_fdset)
766 break;
767 set = files->close_on_exec->fds_bits[j];
768 if (!set)
769 continue;
770 files->close_on_exec->fds_bits[j] = 0;
771 spin_unlock(&files->file_lock);
772 for ( ; set ; i++,set >>= 1) {
773 if (set & 1) {
774 sys_close(i);
777 spin_lock(&files->file_lock);
780 spin_unlock(&files->file_lock);
783 int flush_old_exec(struct linux_binprm * bprm)
785 char * name;
786 int i, ch, retval;
787 struct files_struct *files;
790 * Make sure we have a private signal table and that
791 * we are unassociated from the previous thread group.
793 retval = de_thread(current);
794 if (retval)
795 goto out;
798 * Make sure we have private file handles. Ask the
799 * fork helper to do the work for us and the exit
800 * helper to do the cleanup of the old one.
802 files = current->files; /* refcounted so safe to hold */
803 retval = unshare_files();
804 if (retval)
805 goto out;
807 * Release all of the old mmap stuff
809 retval = exec_mmap(bprm->mm);
810 if (retval)
811 goto mmap_failed;
813 bprm->mm = NULL; /* We're using it now */
815 /* This is the point of no return */
816 steal_locks(files);
817 put_files_struct(files);
819 current->sas_ss_sp = current->sas_ss_size = 0;
821 if (current->euid == current->uid && current->egid == current->gid)
822 current->mm->dumpable = 1;
823 name = bprm->filename;
824 for (i=0; (ch = *(name++)) != '\0';) {
825 if (ch == '/')
826 i = 0;
827 else
828 if (i < 15)
829 current->comm[i++] = ch;
831 current->comm[i] = '\0';
833 flush_thread();
835 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
836 permission(bprm->file->f_dentry->d_inode,MAY_READ, NULL))
837 current->mm->dumpable = 0;
839 /* An exec changes our domain. We are no longer part of the thread
840 group */
842 current->self_exec_id++;
844 flush_signal_handlers(current, 0);
845 flush_old_files(current->files);
846 exit_itimers(current);
848 return 0;
850 mmap_failed:
851 put_files_struct(current->files);
852 current->files = files;
853 out:
854 return retval;
857 EXPORT_SYMBOL(flush_old_exec);
860 * We mustn't allow tracing of suid binaries, unless
861 * the tracer has the capability to trace anything..
863 static inline int must_not_trace_exec(struct task_struct * p)
865 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
869 * Fill the binprm structure from the inode.
870 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
872 int prepare_binprm(struct linux_binprm *bprm)
874 int mode;
875 struct inode * inode = bprm->file->f_dentry->d_inode;
876 int retval;
878 mode = inode->i_mode;
880 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
881 * vfs_permission lets a non-executable through
883 if (!(mode & 0111)) /* with at least _one_ execute bit set */
884 return -EACCES;
885 if (bprm->file->f_op == NULL)
886 return -EACCES;
888 bprm->e_uid = current->euid;
889 bprm->e_gid = current->egid;
891 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
892 /* Set-uid? */
893 if (mode & S_ISUID)
894 bprm->e_uid = inode->i_uid;
896 /* Set-gid? */
898 * If setgid is set but no group execute bit then this
899 * is a candidate for mandatory locking, not a setgid
900 * executable.
902 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
903 bprm->e_gid = inode->i_gid;
906 /* fill in binprm security blob */
907 retval = security_bprm_set(bprm);
908 if (retval)
909 return retval;
911 memset(bprm->buf,0,BINPRM_BUF_SIZE);
912 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
915 EXPORT_SYMBOL(prepare_binprm);
918 * This function is used to produce the new IDs and capabilities
919 * from the old ones and the file's capabilities.
921 * The formula used for evolving capabilities is:
923 * pI' = pI
924 * (***) pP' = (fP & X) | (fI & pI)
925 * pE' = pP' & fE [NB. fE is 0 or ~0]
927 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
928 * ' indicates post-exec(), and X is the global 'cap_bset'.
932 void compute_creds(struct linux_binprm *bprm)
934 task_lock(current);
935 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid) {
936 current->mm->dumpable = 0;
938 if (must_not_trace_exec(current)
939 || atomic_read(&current->fs->count) > 1
940 || atomic_read(&current->files->count) > 1
941 || atomic_read(&current->sighand->count) > 1) {
942 if(!capable(CAP_SETUID)) {
943 bprm->e_uid = current->uid;
944 bprm->e_gid = current->gid;
949 current->suid = current->euid = current->fsuid = bprm->e_uid;
950 current->sgid = current->egid = current->fsgid = bprm->e_gid;
952 task_unlock(current);
954 security_bprm_compute_creds(bprm);
957 EXPORT_SYMBOL(compute_creds);
959 void remove_arg_zero(struct linux_binprm *bprm)
961 if (bprm->argc) {
962 unsigned long offset;
963 char * kaddr;
964 struct page *page;
966 offset = bprm->p % PAGE_SIZE;
967 goto inside;
969 while (bprm->p++, *(kaddr+offset++)) {
970 if (offset != PAGE_SIZE)
971 continue;
972 offset = 0;
973 kunmap_atomic(kaddr, KM_USER0);
974 inside:
975 page = bprm->page[bprm->p/PAGE_SIZE];
976 kaddr = kmap_atomic(page, KM_USER0);
978 kunmap_atomic(kaddr, KM_USER0);
979 bprm->argc--;
983 EXPORT_SYMBOL(remove_arg_zero);
986 * cycle the list of binary formats handler, until one recognizes the image
988 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
990 int try,retval=0;
991 struct linux_binfmt *fmt;
992 #ifdef __alpha__
993 /* handle /sbin/loader.. */
995 struct exec * eh = (struct exec *) bprm->buf;
997 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
998 (eh->fh.f_flags & 0x3000) == 0x3000)
1000 struct file * file;
1001 unsigned long loader;
1003 allow_write_access(bprm->file);
1004 fput(bprm->file);
1005 bprm->file = NULL;
1007 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1009 file = open_exec("/sbin/loader");
1010 retval = PTR_ERR(file);
1011 if (IS_ERR(file))
1012 return retval;
1014 /* Remember if the application is TASO. */
1015 bprm->sh_bang = eh->ah.entry < 0x100000000;
1017 bprm->file = file;
1018 bprm->loader = loader;
1019 retval = prepare_binprm(bprm);
1020 if (retval<0)
1021 return retval;
1022 /* should call search_binary_handler recursively here,
1023 but it does not matter */
1026 #endif
1027 retval = security_bprm_check(bprm);
1028 if (retval)
1029 return retval;
1031 /* kernel module loader fixup */
1032 /* so we don't try to load run modprobe in kernel space. */
1033 set_fs(USER_DS);
1034 for (try=0; try<2; try++) {
1035 read_lock(&binfmt_lock);
1036 for (fmt = formats ; fmt ; fmt = fmt->next) {
1037 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
1038 if (!fn)
1039 continue;
1040 if (!try_module_get(fmt->module))
1041 continue;
1042 read_unlock(&binfmt_lock);
1043 retval = fn(bprm, regs);
1044 if (retval >= 0) {
1045 put_binfmt(fmt);
1046 allow_write_access(bprm->file);
1047 if (bprm->file)
1048 fput(bprm->file);
1049 bprm->file = NULL;
1050 current->did_exec = 1;
1051 return retval;
1053 read_lock(&binfmt_lock);
1054 put_binfmt(fmt);
1055 if (retval != -ENOEXEC || bprm->mm == NULL)
1056 break;
1057 if (!bprm->file) {
1058 read_unlock(&binfmt_lock);
1059 return retval;
1062 read_unlock(&binfmt_lock);
1063 if (retval != -ENOEXEC || bprm->mm == NULL) {
1064 break;
1065 #ifdef CONFIG_KMOD
1066 }else{
1067 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1068 if (printable(bprm->buf[0]) &&
1069 printable(bprm->buf[1]) &&
1070 printable(bprm->buf[2]) &&
1071 printable(bprm->buf[3]))
1072 break; /* -ENOEXEC */
1073 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1074 #endif
1077 return retval;
1080 EXPORT_SYMBOL(search_binary_handler);
1083 * sys_execve() executes a new program.
1085 int do_execve(char * filename,
1086 char __user *__user *argv,
1087 char __user *__user *envp,
1088 struct pt_regs * regs)
1090 struct linux_binprm bprm;
1091 struct file *file;
1092 int retval;
1093 int i;
1095 sched_balance_exec();
1097 file = open_exec(filename);
1099 retval = PTR_ERR(file);
1100 if (IS_ERR(file))
1101 return retval;
1103 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1104 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1106 bprm.file = file;
1107 bprm.filename = filename;
1108 bprm.interp = filename;
1109 bprm.sh_bang = 0;
1110 bprm.loader = 0;
1111 bprm.exec = 0;
1112 bprm.security = NULL;
1113 bprm.mm = mm_alloc();
1114 retval = -ENOMEM;
1115 if (!bprm.mm)
1116 goto out_file;
1118 retval = init_new_context(current, bprm.mm);
1119 if (retval < 0)
1120 goto out_mm;
1122 bprm.argc = count(argv, bprm.p / sizeof(void *));
1123 if ((retval = bprm.argc) < 0)
1124 goto out_mm;
1126 bprm.envc = count(envp, bprm.p / sizeof(void *));
1127 if ((retval = bprm.envc) < 0)
1128 goto out_mm;
1130 retval = security_bprm_alloc(&bprm);
1131 if (retval)
1132 goto out;
1134 retval = prepare_binprm(&bprm);
1135 if (retval < 0)
1136 goto out;
1138 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1139 if (retval < 0)
1140 goto out;
1142 bprm.exec = bprm.p;
1143 retval = copy_strings(bprm.envc, envp, &bprm);
1144 if (retval < 0)
1145 goto out;
1147 retval = copy_strings(bprm.argc, argv, &bprm);
1148 if (retval < 0)
1149 goto out;
1151 retval = search_binary_handler(&bprm,regs);
1152 if (retval >= 0) {
1153 free_arg_pages(&bprm);
1155 /* execve success */
1156 security_bprm_free(&bprm);
1157 return retval;
1160 out:
1161 /* Something went wrong, return the inode and free the argument pages*/
1162 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1163 struct page * page = bprm.page[i];
1164 if (page)
1165 __free_page(page);
1168 if (bprm.security)
1169 security_bprm_free(&bprm);
1171 out_mm:
1172 if (bprm.mm)
1173 mmdrop(bprm.mm);
1175 out_file:
1176 if (bprm.file) {
1177 allow_write_access(bprm.file);
1178 fput(bprm.file);
1180 return retval;
1183 EXPORT_SYMBOL(do_execve);
1185 int set_binfmt(struct linux_binfmt *new)
1187 struct linux_binfmt *old = current->binfmt;
1189 if (new) {
1190 if (!try_module_get(new->module))
1191 return -1;
1193 current->binfmt = new;
1194 if (old)
1195 module_put(old->module);
1196 return 0;
1199 EXPORT_SYMBOL(set_binfmt);
1201 #define CORENAME_MAX_SIZE 64
1203 /* format_corename will inspect the pattern parameter, and output a
1204 * name into corename, which must have space for at least
1205 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1207 void format_corename(char *corename, const char *pattern, long signr)
1209 const char *pat_ptr = pattern;
1210 char *out_ptr = corename;
1211 char *const out_end = corename + CORENAME_MAX_SIZE;
1212 int rc;
1213 int pid_in_pattern = 0;
1215 /* Repeat as long as we have more pattern to process and more output
1216 space */
1217 while (*pat_ptr) {
1218 if (*pat_ptr != '%') {
1219 if (out_ptr == out_end)
1220 goto out;
1221 *out_ptr++ = *pat_ptr++;
1222 } else {
1223 switch (*++pat_ptr) {
1224 case 0:
1225 goto out;
1226 /* Double percent, output one percent */
1227 case '%':
1228 if (out_ptr == out_end)
1229 goto out;
1230 *out_ptr++ = '%';
1231 break;
1232 /* pid */
1233 case 'p':
1234 pid_in_pattern = 1;
1235 rc = snprintf(out_ptr, out_end - out_ptr,
1236 "%d", current->tgid);
1237 if (rc > out_end - out_ptr)
1238 goto out;
1239 out_ptr += rc;
1240 break;
1241 /* uid */
1242 case 'u':
1243 rc = snprintf(out_ptr, out_end - out_ptr,
1244 "%d", current->uid);
1245 if (rc > out_end - out_ptr)
1246 goto out;
1247 out_ptr += rc;
1248 break;
1249 /* gid */
1250 case 'g':
1251 rc = snprintf(out_ptr, out_end - out_ptr,
1252 "%d", current->gid);
1253 if (rc > out_end - out_ptr)
1254 goto out;
1255 out_ptr += rc;
1256 break;
1257 /* signal that caused the coredump */
1258 case 's':
1259 rc = snprintf(out_ptr, out_end - out_ptr,
1260 "%ld", signr);
1261 if (rc > out_end - out_ptr)
1262 goto out;
1263 out_ptr += rc;
1264 break;
1265 /* UNIX time of coredump */
1266 case 't': {
1267 struct timeval tv;
1268 do_gettimeofday(&tv);
1269 rc = snprintf(out_ptr, out_end - out_ptr,
1270 "%lu", tv.tv_sec);
1271 if (rc > out_end - out_ptr)
1272 goto out;
1273 out_ptr += rc;
1274 break;
1276 /* hostname */
1277 case 'h':
1278 down_read(&uts_sem);
1279 rc = snprintf(out_ptr, out_end - out_ptr,
1280 "%s", system_utsname.nodename);
1281 up_read(&uts_sem);
1282 if (rc > out_end - out_ptr)
1283 goto out;
1284 out_ptr += rc;
1285 break;
1286 /* executable */
1287 case 'e':
1288 rc = snprintf(out_ptr, out_end - out_ptr,
1289 "%s", current->comm);
1290 if (rc > out_end - out_ptr)
1291 goto out;
1292 out_ptr += rc;
1293 break;
1294 default:
1295 break;
1297 ++pat_ptr;
1300 /* Backward compatibility with core_uses_pid:
1302 * If core_pattern does not include a %p (as is the default)
1303 * and core_uses_pid is set, then .%pid will be appended to
1304 * the filename */
1305 if (!pid_in_pattern
1306 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1307 rc = snprintf(out_ptr, out_end - out_ptr,
1308 ".%d", current->tgid);
1309 if (rc > out_end - out_ptr)
1310 goto out;
1311 out_ptr += rc;
1313 out:
1314 *out_ptr = 0;
1317 static void zap_threads (struct mm_struct *mm)
1319 struct task_struct *g, *p;
1320 struct task_struct *tsk = current;
1321 struct completion *vfork_done = tsk->vfork_done;
1324 * Make sure nobody is waiting for us to release the VM,
1325 * otherwise we can deadlock when we wait on each other
1327 if (vfork_done) {
1328 tsk->vfork_done = NULL;
1329 complete(vfork_done);
1332 read_lock(&tasklist_lock);
1333 do_each_thread(g,p)
1334 if (mm == p->mm && p != tsk) {
1335 force_sig_specific(SIGKILL, p);
1336 mm->core_waiters++;
1338 while_each_thread(g,p);
1340 read_unlock(&tasklist_lock);
1343 static void coredump_wait(struct mm_struct *mm)
1345 DECLARE_COMPLETION(startup_done);
1347 mm->core_waiters++; /* let other threads block */
1348 mm->core_startup_done = &startup_done;
1350 /* give other threads a chance to run: */
1351 yield();
1353 zap_threads(mm);
1354 if (--mm->core_waiters) {
1355 up_write(&mm->mmap_sem);
1356 wait_for_completion(&startup_done);
1357 } else
1358 up_write(&mm->mmap_sem);
1359 BUG_ON(mm->core_waiters);
1362 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1364 char corename[CORENAME_MAX_SIZE + 1];
1365 struct mm_struct *mm = current->mm;
1366 struct linux_binfmt * binfmt;
1367 struct inode * inode;
1368 struct file * file;
1369 int retval = 0;
1371 lock_kernel();
1372 binfmt = current->binfmt;
1373 if (!binfmt || !binfmt->core_dump)
1374 goto fail;
1375 down_write(&mm->mmap_sem);
1376 if (!mm->dumpable) {
1377 up_write(&mm->mmap_sem);
1378 goto fail;
1380 mm->dumpable = 0;
1381 init_completion(&mm->core_done);
1382 current->signal->group_exit = 1;
1383 current->signal->group_exit_code = exit_code;
1384 coredump_wait(mm);
1386 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1387 goto fail_unlock;
1389 format_corename(corename, core_pattern, signr);
1390 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE, 0600);
1391 if (IS_ERR(file))
1392 goto fail_unlock;
1393 inode = file->f_dentry->d_inode;
1394 if (inode->i_nlink > 1)
1395 goto close_fail; /* multiple links - don't dump */
1396 if (d_unhashed(file->f_dentry))
1397 goto close_fail;
1399 if (!S_ISREG(inode->i_mode))
1400 goto close_fail;
1401 if (!file->f_op)
1402 goto close_fail;
1403 if (!file->f_op->write)
1404 goto close_fail;
1405 if (do_truncate(file->f_dentry, 0) != 0)
1406 goto close_fail;
1408 retval = binfmt->core_dump(signr, regs, file);
1410 current->signal->group_exit_code |= 0x80;
1411 close_fail:
1412 filp_close(file, NULL);
1413 fail_unlock:
1414 complete_all(&mm->core_done);
1415 fail:
1416 unlock_kernel();
1417 return retval;