Fix prototype of SMP version of synchronize_irq.
[linux-2.6/linux-mips.git] / fs / exec.c
blobd84c9a2c4b8facd926f05ea76fb59f553c719b36
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/rmap-locking.h>
49 #include <asm/uaccess.h>
50 #include <asm/pgalloc.h>
51 #include <asm/mmu_context.h>
53 #ifdef CONFIG_KMOD
54 #include <linux/kmod.h>
55 #endif
57 int core_uses_pid;
58 char core_pattern[65] = "core";
59 /* The maximal length of core_pattern is also specified in sysctl.c */
61 static struct linux_binfmt *formats;
62 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
64 int register_binfmt(struct linux_binfmt * fmt)
66 struct linux_binfmt ** tmp = &formats;
68 if (!fmt)
69 return -EINVAL;
70 if (fmt->next)
71 return -EBUSY;
72 write_lock(&binfmt_lock);
73 while (*tmp) {
74 if (fmt == *tmp) {
75 write_unlock(&binfmt_lock);
76 return -EBUSY;
78 tmp = &(*tmp)->next;
80 fmt->next = formats;
81 formats = fmt;
82 write_unlock(&binfmt_lock);
83 return 0;
86 int unregister_binfmt(struct linux_binfmt * fmt)
88 struct linux_binfmt ** tmp = &formats;
90 write_lock(&binfmt_lock);
91 while (*tmp) {
92 if (fmt == *tmp) {
93 *tmp = fmt->next;
94 write_unlock(&binfmt_lock);
95 return 0;
97 tmp = &(*tmp)->next;
99 write_unlock(&binfmt_lock);
100 return -EINVAL;
103 static inline void put_binfmt(struct linux_binfmt * fmt)
105 module_put(fmt->module);
109 * Note that a shared library must be both readable and executable due to
110 * security reasons.
112 * Also note that we take the address to load from from the file itself.
114 asmlinkage long sys_uselib(const char __user * library)
116 struct file * file;
117 struct nameidata nd;
118 int error;
120 error = user_path_walk(library, &nd);
121 if (error)
122 goto out;
124 error = -EINVAL;
125 if (!S_ISREG(nd.dentry->d_inode->i_mode))
126 goto exit;
128 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC);
129 if (error)
130 goto exit;
132 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
133 error = PTR_ERR(file);
134 if (IS_ERR(file))
135 goto out;
137 error = -ENOEXEC;
138 if(file->f_op) {
139 struct linux_binfmt * fmt;
141 read_lock(&binfmt_lock);
142 for (fmt = formats ; fmt ; fmt = fmt->next) {
143 if (!fmt->load_shlib)
144 continue;
145 if (!try_module_get(fmt->module))
146 continue;
147 read_unlock(&binfmt_lock);
148 error = fmt->load_shlib(file);
149 read_lock(&binfmt_lock);
150 put_binfmt(fmt);
151 if (error != -ENOEXEC)
152 break;
154 read_unlock(&binfmt_lock);
156 fput(file);
157 out:
158 return error;
159 exit:
160 path_release(&nd);
161 goto out;
165 * count() counts the number of strings in array ARGV.
167 static int count(char __user * __user * argv, int max)
169 int i = 0;
171 if (argv != NULL) {
172 for (;;) {
173 char __user * p;
175 if (get_user(p, argv))
176 return -EFAULT;
177 if (!p)
178 break;
179 argv++;
180 if(++i > max)
181 return -E2BIG;
184 return i;
188 * 'copy_strings()' copies argument/environment strings from user
189 * memory to free pages in kernel mem. These are in a format ready
190 * to be put directly into the top of new user memory.
192 int copy_strings(int argc,char __user * __user * argv, struct linux_binprm *bprm)
194 struct page *kmapped_page = NULL;
195 char *kaddr = NULL;
196 int ret;
198 while (argc-- > 0) {
199 char __user *str;
200 int len;
201 unsigned long pos;
203 if (get_user(str, argv+argc) ||
204 !(len = strnlen_user(str, bprm->p))) {
205 ret = -EFAULT;
206 goto out;
209 if (bprm->p < len) {
210 ret = -E2BIG;
211 goto out;
214 bprm->p -= len;
215 /* XXX: add architecture specific overflow check here. */
216 pos = bprm->p;
218 while (len > 0) {
219 int i, new, err;
220 int offset, bytes_to_copy;
221 struct page *page;
223 offset = pos % PAGE_SIZE;
224 i = pos/PAGE_SIZE;
225 page = bprm->page[i];
226 new = 0;
227 if (!page) {
228 page = alloc_page(GFP_HIGHUSER);
229 bprm->page[i] = page;
230 if (!page) {
231 ret = -ENOMEM;
232 goto out;
234 new = 1;
237 if (page != kmapped_page) {
238 if (kmapped_page)
239 kunmap(kmapped_page);
240 kmapped_page = page;
241 kaddr = kmap(kmapped_page);
243 if (new && offset)
244 memset(kaddr, 0, offset);
245 bytes_to_copy = PAGE_SIZE - offset;
246 if (bytes_to_copy > len) {
247 bytes_to_copy = len;
248 if (new)
249 memset(kaddr+offset+len, 0,
250 PAGE_SIZE-offset-len);
252 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
253 if (err) {
254 ret = -EFAULT;
255 goto out;
258 pos += bytes_to_copy;
259 str += bytes_to_copy;
260 len -= bytes_to_copy;
263 ret = 0;
264 out:
265 if (kmapped_page)
266 kunmap(kmapped_page);
267 return ret;
271 * Like copy_strings, but get argv and its values from kernel memory.
273 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
275 int r;
276 mm_segment_t oldfs = get_fs();
277 set_fs(KERNEL_DS);
278 r = copy_strings(argc, (char __user * __user *)argv, bprm);
279 set_fs(oldfs);
280 return r;
283 #ifdef CONFIG_MMU
285 * This routine is used to map in a page into an address space: needed by
286 * execve() for the initial stack and environment pages.
288 * tsk->mmap_sem is held for writing.
290 void put_dirty_page(struct task_struct *tsk, struct page *page,
291 unsigned long address, pgprot_t prot)
293 pgd_t * pgd;
294 pmd_t * pmd;
295 pte_t * pte;
296 struct pte_chain *pte_chain;
298 if (page_count(page) != 1)
299 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n",
300 page, address);
302 pgd = pgd_offset(tsk->mm, address);
303 pte_chain = pte_chain_alloc(GFP_KERNEL);
304 if (!pte_chain)
305 goto out_sig;
306 spin_lock(&tsk->mm->page_table_lock);
307 pmd = pmd_alloc(tsk->mm, pgd, address);
308 if (!pmd)
309 goto out;
310 pte = pte_alloc_map(tsk->mm, pmd, address);
311 if (!pte)
312 goto out;
313 if (!pte_none(*pte)) {
314 pte_unmap(pte);
315 goto out;
317 lru_cache_add_active(page);
318 flush_dcache_page(page);
319 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, prot))));
320 pte_chain = page_add_rmap(page, pte, pte_chain);
321 pte_unmap(pte);
322 tsk->mm->rss++;
323 spin_unlock(&tsk->mm->page_table_lock);
325 /* no need for flush_tlb */
326 pte_chain_free(pte_chain);
327 return;
328 out:
329 spin_unlock(&tsk->mm->page_table_lock);
330 out_sig:
331 __free_page(page);
332 force_sig(SIGKILL, tsk);
333 pte_chain_free(pte_chain);
334 return;
337 int setup_arg_pages(struct linux_binprm *bprm)
339 unsigned long stack_base;
340 struct vm_area_struct *mpnt;
341 struct mm_struct *mm = current->mm;
342 int i;
344 #ifdef CONFIG_STACK_GROWSUP
345 /* Move the argument and environment strings to the bottom of the
346 * stack space.
348 int offset, j;
349 char *to, *from;
351 /* Start by shifting all the pages down */
352 i = 0;
353 for (j = 0; j < MAX_ARG_PAGES; j++) {
354 struct page *page = bprm->page[j];
355 if (!page)
356 continue;
357 bprm->page[i++] = page;
360 /* Now move them within their pages */
361 offset = bprm->p % PAGE_SIZE;
362 to = kmap(bprm->page[0]);
363 for (j = 1; j < i; j++) {
364 memmove(to, to + offset, PAGE_SIZE - offset);
365 from = kmap(bprm->page[j]);
366 memcpy(to + PAGE_SIZE - offset, from, offset);
367 kunmap(bprm->page[j - 1]);
368 to = from;
370 memmove(to, to + offset, PAGE_SIZE - offset);
371 kunmap(bprm->page[j - 1]);
373 /* Adjust bprm->p to point to the end of the strings. */
374 bprm->p = PAGE_SIZE * i - offset;
375 stack_base = STACK_TOP - current->rlim[RLIMIT_STACK].rlim_max;
376 mm->arg_start = stack_base;
378 /* zero pages that were copied above */
379 while (i < MAX_ARG_PAGES)
380 bprm->page[i++] = NULL;
381 #else
382 stack_base = STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
383 mm->arg_start = bprm->p + stack_base;
384 #endif
386 bprm->p += stack_base;
387 if (bprm->loader)
388 bprm->loader += stack_base;
389 bprm->exec += stack_base;
391 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
392 if (!mpnt)
393 return -ENOMEM;
395 if (!vm_enough_memory((STACK_TOP - (PAGE_MASK & (unsigned long) bprm->p))>>PAGE_SHIFT)) {
396 kmem_cache_free(vm_area_cachep, mpnt);
397 return -ENOMEM;
400 down_write(&mm->mmap_sem);
402 mpnt->vm_mm = mm;
403 #ifdef CONFIG_STACK_GROWSUP
404 mpnt->vm_start = stack_base;
405 mpnt->vm_end = PAGE_MASK &
406 (PAGE_SIZE - 1 + (unsigned long) bprm->p);
407 #else
408 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
409 mpnt->vm_end = STACK_TOP;
410 #endif
411 mpnt->vm_page_prot = protection_map[VM_STACK_FLAGS & 0x7];
412 mpnt->vm_flags = VM_STACK_FLAGS;
413 mpnt->vm_ops = NULL;
414 mpnt->vm_pgoff = 0;
415 mpnt->vm_file = NULL;
416 INIT_LIST_HEAD(&mpnt->shared);
417 mpnt->vm_private_data = (void *) 0;
418 insert_vm_struct(mm, mpnt);
419 mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
422 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
423 struct page *page = bprm->page[i];
424 if (page) {
425 bprm->page[i] = NULL;
426 put_dirty_page(current, page, stack_base,
427 mpnt->vm_page_prot);
429 stack_base += PAGE_SIZE;
431 up_write(&mm->mmap_sem);
433 return 0;
436 #define free_arg_pages(bprm) do { } while (0)
438 #else
440 static inline void free_arg_pages(struct linux_binprm *bprm)
442 int i;
444 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
445 if (bprm->page[i])
446 __free_page(bprm->page[i]);
447 bprm->page[i] = NULL;
451 #endif /* CONFIG_MMU */
453 struct file *open_exec(const char *name)
455 struct nameidata nd;
456 int err = path_lookup(name, LOOKUP_FOLLOW, &nd);
457 struct file *file = ERR_PTR(err);
459 if (!err) {
460 struct inode *inode = nd.dentry->d_inode;
461 file = ERR_PTR(-EACCES);
462 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
463 S_ISREG(inode->i_mode)) {
464 int err = permission(inode, MAY_EXEC);
465 if (!err && !(inode->i_mode & 0111))
466 err = -EACCES;
467 file = ERR_PTR(err);
468 if (!err) {
469 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
470 if (!IS_ERR(file)) {
471 err = deny_write_access(file);
472 if (err) {
473 fput(file);
474 file = ERR_PTR(err);
477 out:
478 return file;
481 path_release(&nd);
483 goto out;
486 int kernel_read(struct file *file, unsigned long offset,
487 char *addr, unsigned long count)
489 mm_segment_t old_fs;
490 loff_t pos = offset;
491 int result;
493 old_fs = get_fs();
494 set_fs(get_ds());
495 /* The cast to a user pointer is valid due to the set_fs() */
496 result = vfs_read(file, (void __user *)addr, count, &pos);
497 set_fs(old_fs);
498 return result;
501 static int exec_mmap(struct mm_struct *mm)
503 struct task_struct *tsk;
504 struct mm_struct * old_mm, *active_mm;
506 /* Add it to the list of mm's */
507 spin_lock(&mmlist_lock);
508 list_add(&mm->mmlist, &init_mm.mmlist);
509 mmlist_nr++;
510 spin_unlock(&mmlist_lock);
512 /* Notify parent that we're no longer interested in the old VM */
513 tsk = current;
514 old_mm = current->mm;
515 mm_release(tsk, old_mm);
517 task_lock(tsk);
518 active_mm = tsk->active_mm;
519 tsk->mm = mm;
520 tsk->active_mm = mm;
521 activate_mm(active_mm, mm);
522 task_unlock(tsk);
523 if (old_mm) {
524 if (active_mm != old_mm) BUG();
525 mmput(old_mm);
526 return 0;
528 mmdrop(active_mm);
529 return 0;
533 * This function makes sure the current process has its own signal table,
534 * so that flush_signal_handlers can later reset the handlers without
535 * disturbing other processes. (Other processes might share the signal
536 * table via the CLONE_SIGHAND option to clone().)
538 static inline int de_thread(struct task_struct *tsk)
540 struct signal_struct *newsig, *oldsig = tsk->signal;
541 struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
542 spinlock_t *lock = &oldsighand->siglock;
543 int count;
546 * If we don't share sighandlers, then we aren't sharing anything
547 * and we can just re-use it all.
549 if (atomic_read(&oldsighand->count) <= 1)
550 return 0;
552 newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
553 if (!newsighand)
554 return -ENOMEM;
556 spin_lock_init(&newsighand->siglock);
557 atomic_set(&newsighand->count, 1);
558 memcpy(newsighand->action, oldsighand->action, sizeof(newsighand->action));
561 * See if we need to allocate a new signal structure
563 newsig = NULL;
564 if (atomic_read(&oldsig->count) > 1) {
565 newsig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
566 if (!newsig) {
567 kmem_cache_free(sighand_cachep, newsighand);
568 return -ENOMEM;
570 atomic_set(&newsig->count, 1);
571 newsig->group_exit = 0;
572 newsig->group_exit_code = 0;
573 newsig->group_exit_task = NULL;
574 newsig->group_stop_count = 0;
575 newsig->curr_target = NULL;
576 init_sigpending(&newsig->shared_pending);
579 if (thread_group_empty(current))
580 goto no_thread_group;
583 * Kill all other threads in the thread group.
584 * We must hold tasklist_lock to call zap_other_threads.
586 read_lock(&tasklist_lock);
587 spin_lock_irq(lock);
588 if (oldsig->group_exit) {
590 * Another group action in progress, just
591 * return so that the signal is processed.
593 spin_unlock_irq(lock);
594 read_unlock(&tasklist_lock);
595 kmem_cache_free(sighand_cachep, newsighand);
596 if (newsig)
597 kmem_cache_free(signal_cachep, newsig);
598 return -EAGAIN;
600 oldsig->group_exit = 1;
601 zap_other_threads(current);
602 read_unlock(&tasklist_lock);
605 * Account for the thread group leader hanging around:
607 count = 2;
608 if (current->pid == current->tgid)
609 count = 1;
610 while (atomic_read(&oldsig->count) > count) {
611 oldsig->group_exit_task = current;
612 oldsig->notify_count = count;
613 __set_current_state(TASK_UNINTERRUPTIBLE);
614 spin_unlock_irq(lock);
615 schedule();
616 spin_lock_irq(lock);
617 if (oldsig->group_exit_task)
618 BUG();
620 spin_unlock_irq(lock);
623 * At this point all other threads have exited, all we have to
624 * do is to wait for the thread group leader to become inactive,
625 * and to assume its PID:
627 if (current->pid != current->tgid) {
628 struct task_struct *leader = current->group_leader, *parent;
629 struct dentry *proc_dentry1, *proc_dentry2;
630 unsigned long state, ptrace;
633 * Wait for the thread group leader to be a zombie.
634 * It should already be zombie at this point, most
635 * of the time.
637 while (leader->state != TASK_ZOMBIE)
638 yield();
640 spin_lock(&leader->proc_lock);
641 spin_lock(&current->proc_lock);
642 proc_dentry1 = proc_pid_unhash(current);
643 proc_dentry2 = proc_pid_unhash(leader);
644 write_lock_irq(&tasklist_lock);
646 if (leader->tgid != current->tgid)
647 BUG();
648 if (current->pid == current->tgid)
649 BUG();
651 * An exec() starts a new thread group with the
652 * TGID of the previous thread group. Rehash the
653 * two threads with a switched PID, and release
654 * the former thread group leader:
656 ptrace = leader->ptrace;
657 parent = leader->parent;
659 ptrace_unlink(current);
660 ptrace_unlink(leader);
661 remove_parent(current);
662 remove_parent(leader);
664 switch_exec_pids(leader, current);
666 current->parent = current->real_parent = leader->real_parent;
667 leader->parent = leader->real_parent = child_reaper;
668 current->group_leader = current;
669 leader->group_leader = leader;
671 add_parent(current, current->parent);
672 add_parent(leader, leader->parent);
673 if (ptrace) {
674 current->ptrace = ptrace;
675 __ptrace_link(current, parent);
678 list_del(&current->tasks);
679 list_add_tail(&current->tasks, &init_task.tasks);
680 current->exit_signal = SIGCHLD;
681 state = leader->state;
683 write_unlock_irq(&tasklist_lock);
684 spin_unlock(&leader->proc_lock);
685 spin_unlock(&current->proc_lock);
686 proc_pid_flush(proc_dentry1);
687 proc_pid_flush(proc_dentry2);
689 if (state != TASK_ZOMBIE)
690 BUG();
691 release_task(leader);
694 no_thread_group:
696 write_lock_irq(&tasklist_lock);
697 spin_lock(&oldsighand->siglock);
698 spin_lock(&newsighand->siglock);
700 if (current == oldsig->curr_target)
701 oldsig->curr_target = next_thread(current);
702 if (newsig)
703 current->signal = newsig;
704 current->sighand = newsighand;
705 init_sigpending(&current->pending);
706 recalc_sigpending();
708 spin_unlock(&newsighand->siglock);
709 spin_unlock(&oldsighand->siglock);
710 write_unlock_irq(&tasklist_lock);
712 if (newsig && atomic_dec_and_test(&oldsig->count))
713 kmem_cache_free(signal_cachep, oldsig);
715 if (atomic_dec_and_test(&oldsighand->count))
716 kmem_cache_free(sighand_cachep, oldsighand);
718 if (!thread_group_empty(current))
719 BUG();
720 if (current->tgid != current->pid)
721 BUG();
722 return 0;
726 * These functions flushes out all traces of the currently running executable
727 * so that a new one can be started
730 static inline void flush_old_files(struct files_struct * files)
732 long j = -1;
734 spin_lock(&files->file_lock);
735 for (;;) {
736 unsigned long set, i;
738 j++;
739 i = j * __NFDBITS;
740 if (i >= files->max_fds || i >= files->max_fdset)
741 break;
742 set = files->close_on_exec->fds_bits[j];
743 if (!set)
744 continue;
745 files->close_on_exec->fds_bits[j] = 0;
746 spin_unlock(&files->file_lock);
747 for ( ; set ; i++,set >>= 1) {
748 if (set & 1) {
749 sys_close(i);
752 spin_lock(&files->file_lock);
755 spin_unlock(&files->file_lock);
758 int flush_old_exec(struct linux_binprm * bprm)
760 char * name;
761 int i, ch, retval;
764 * Release all of the old mmap stuff
766 retval = exec_mmap(bprm->mm);
767 if (retval)
768 goto out;
770 * Make sure we have a private signal table and that
771 * we are unassociated from the previous thread group.
773 retval = de_thread(current);
774 if (retval)
775 goto out;
777 /* This is the point of no return */
779 current->sas_ss_sp = current->sas_ss_size = 0;
781 if (current->euid == current->uid && current->egid == current->gid)
782 current->mm->dumpable = 1;
783 name = bprm->filename;
784 for (i=0; (ch = *(name++)) != '\0';) {
785 if (ch == '/')
786 i = 0;
787 else
788 if (i < 15)
789 current->comm[i++] = ch;
791 current->comm[i] = '\0';
793 flush_thread();
795 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
796 permission(bprm->file->f_dentry->d_inode,MAY_READ))
797 current->mm->dumpable = 0;
799 /* An exec changes our domain. We are no longer part of the thread
800 group */
802 current->self_exec_id++;
804 flush_signal_handlers(current, 0);
805 flush_old_files(current->files);
806 exit_itimers(current);
808 return 0;
810 out:
811 return retval;
815 * We mustn't allow tracing of suid binaries, unless
816 * the tracer has the capability to trace anything..
818 static inline int must_not_trace_exec(struct task_struct * p)
820 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
824 * Fill the binprm structure from the inode.
825 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
827 int prepare_binprm(struct linux_binprm *bprm)
829 int mode;
830 struct inode * inode = bprm->file->f_dentry->d_inode;
831 int retval;
833 mode = inode->i_mode;
835 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
836 * vfs_permission lets a non-executable through
838 if (!(mode & 0111)) /* with at least _one_ execute bit set */
839 return -EACCES;
840 if (bprm->file->f_op == NULL)
841 return -EACCES;
843 bprm->e_uid = current->euid;
844 bprm->e_gid = current->egid;
846 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
847 /* Set-uid? */
848 if (mode & S_ISUID)
849 bprm->e_uid = inode->i_uid;
851 /* Set-gid? */
853 * If setgid is set but no group execute bit then this
854 * is a candidate for mandatory locking, not a setgid
855 * executable.
857 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
858 bprm->e_gid = inode->i_gid;
861 /* fill in binprm security blob */
862 retval = security_bprm_set(bprm);
863 if (retval)
864 return retval;
866 memset(bprm->buf,0,BINPRM_BUF_SIZE);
867 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
871 * This function is used to produce the new IDs and capabilities
872 * from the old ones and the file's capabilities.
874 * The formula used for evolving capabilities is:
876 * pI' = pI
877 * (***) pP' = (fP & X) | (fI & pI)
878 * pE' = pP' & fE [NB. fE is 0 or ~0]
880 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
881 * ' indicates post-exec(), and X is the global 'cap_bset'.
885 void compute_creds(struct linux_binprm *bprm)
887 task_lock(current);
888 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid) {
889 current->mm->dumpable = 0;
891 if (must_not_trace_exec(current)
892 || atomic_read(&current->fs->count) > 1
893 || atomic_read(&current->files->count) > 1
894 || atomic_read(&current->sighand->count) > 1) {
895 if(!capable(CAP_SETUID)) {
896 bprm->e_uid = current->uid;
897 bprm->e_gid = current->gid;
902 current->suid = current->euid = current->fsuid = bprm->e_uid;
903 current->sgid = current->egid = current->fsgid = bprm->e_gid;
905 task_unlock(current);
907 security_bprm_compute_creds(bprm);
910 void remove_arg_zero(struct linux_binprm *bprm)
912 if (bprm->argc) {
913 unsigned long offset;
914 char * kaddr;
915 struct page *page;
917 offset = bprm->p % PAGE_SIZE;
918 goto inside;
920 while (bprm->p++, *(kaddr+offset++)) {
921 if (offset != PAGE_SIZE)
922 continue;
923 offset = 0;
924 kunmap(page);
925 inside:
926 page = bprm->page[bprm->p/PAGE_SIZE];
927 kaddr = kmap(page);
929 kunmap(page);
930 bprm->argc--;
935 * cycle the list of binary formats handler, until one recognizes the image
937 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
939 int try,retval=0;
940 struct linux_binfmt *fmt;
941 #ifdef __alpha__
942 /* handle /sbin/loader.. */
944 struct exec * eh = (struct exec *) bprm->buf;
946 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
947 (eh->fh.f_flags & 0x3000) == 0x3000)
949 struct file * file;
950 unsigned long loader;
952 allow_write_access(bprm->file);
953 fput(bprm->file);
954 bprm->file = NULL;
956 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
958 file = open_exec("/sbin/loader");
959 retval = PTR_ERR(file);
960 if (IS_ERR(file))
961 return retval;
963 /* Remember if the application is TASO. */
964 bprm->sh_bang = eh->ah.entry < 0x100000000;
966 bprm->file = file;
967 bprm->loader = loader;
968 retval = prepare_binprm(bprm);
969 if (retval<0)
970 return retval;
971 /* should call search_binary_handler recursively here,
972 but it does not matter */
975 #endif
976 retval = security_bprm_check(bprm);
977 if (retval)
978 return retval;
980 /* kernel module loader fixup */
981 /* so we don't try to load run modprobe in kernel space. */
982 set_fs(USER_DS);
983 for (try=0; try<2; try++) {
984 read_lock(&binfmt_lock);
985 for (fmt = formats ; fmt ; fmt = fmt->next) {
986 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
987 if (!fn)
988 continue;
989 if (!try_module_get(fmt->module))
990 continue;
991 read_unlock(&binfmt_lock);
992 retval = fn(bprm, regs);
993 if (retval >= 0) {
994 put_binfmt(fmt);
995 allow_write_access(bprm->file);
996 if (bprm->file)
997 fput(bprm->file);
998 bprm->file = NULL;
999 current->did_exec = 1;
1000 return retval;
1002 read_lock(&binfmt_lock);
1003 put_binfmt(fmt);
1004 if (retval != -ENOEXEC)
1005 break;
1006 if (!bprm->file) {
1007 read_unlock(&binfmt_lock);
1008 return retval;
1011 read_unlock(&binfmt_lock);
1012 if (retval != -ENOEXEC) {
1013 break;
1014 #ifdef CONFIG_KMOD
1015 }else{
1016 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
1017 if (printable(bprm->buf[0]) &&
1018 printable(bprm->buf[1]) &&
1019 printable(bprm->buf[2]) &&
1020 printable(bprm->buf[3]))
1021 break; /* -ENOEXEC */
1022 request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
1023 #endif
1026 return retval;
1030 * sys_execve() executes a new program.
1032 int do_execve(char * filename,
1033 char __user *__user *argv,
1034 char __user *__user *envp,
1035 struct pt_regs * regs)
1037 struct linux_binprm bprm;
1038 struct file *file;
1039 int retval;
1040 int i;
1042 sched_balance_exec();
1044 file = open_exec(filename);
1046 retval = PTR_ERR(file);
1047 if (IS_ERR(file))
1048 return retval;
1050 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1051 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1053 bprm.file = file;
1054 bprm.filename = filename;
1055 bprm.sh_bang = 0;
1056 bprm.loader = 0;
1057 bprm.exec = 0;
1058 bprm.security = NULL;
1059 bprm.mm = mm_alloc();
1060 retval = -ENOMEM;
1061 if (!bprm.mm)
1062 goto out_file;
1064 retval = init_new_context(current, bprm.mm);
1065 if (retval < 0)
1066 goto out_mm;
1068 bprm.argc = count(argv, bprm.p / sizeof(void *));
1069 if ((retval = bprm.argc) < 0)
1070 goto out_mm;
1072 bprm.envc = count(envp, bprm.p / sizeof(void *));
1073 if ((retval = bprm.envc) < 0)
1074 goto out_mm;
1076 retval = security_bprm_alloc(&bprm);
1077 if (retval)
1078 goto out;
1080 retval = prepare_binprm(&bprm);
1081 if (retval < 0)
1082 goto out;
1084 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1085 if (retval < 0)
1086 goto out;
1088 bprm.exec = bprm.p;
1089 retval = copy_strings(bprm.envc, envp, &bprm);
1090 if (retval < 0)
1091 goto out;
1093 retval = copy_strings(bprm.argc, argv, &bprm);
1094 if (retval < 0)
1095 goto out;
1097 retval = search_binary_handler(&bprm,regs);
1098 if (retval >= 0) {
1099 free_arg_pages(&bprm);
1101 /* execve success */
1102 security_bprm_free(&bprm);
1103 return retval;
1106 out:
1107 /* Something went wrong, return the inode and free the argument pages*/
1108 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1109 struct page * page = bprm.page[i];
1110 if (page)
1111 __free_page(page);
1114 if (bprm.security)
1115 security_bprm_free(&bprm);
1117 out_mm:
1118 mmdrop(bprm.mm);
1120 out_file:
1121 if (bprm.file) {
1122 allow_write_access(bprm.file);
1123 fput(bprm.file);
1125 return retval;
1128 int set_binfmt(struct linux_binfmt *new)
1130 struct linux_binfmt *old = current->binfmt;
1132 if (new) {
1133 if (!try_module_get(new->module))
1134 return -1;
1136 current->binfmt = new;
1137 if (old)
1138 module_put(old->module);
1139 return 0;
1142 #define CORENAME_MAX_SIZE 64
1144 /* format_corename will inspect the pattern parameter, and output a
1145 * name into corename, which must have space for at least
1146 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
1148 void format_corename(char *corename, const char *pattern, long signr)
1150 const char *pat_ptr = pattern;
1151 char *out_ptr = corename;
1152 char *const out_end = corename + CORENAME_MAX_SIZE;
1153 int rc;
1154 int pid_in_pattern = 0;
1156 /* Repeat as long as we have more pattern to process and more output
1157 space */
1158 while (*pat_ptr) {
1159 if (*pat_ptr != '%') {
1160 if (out_ptr == out_end)
1161 goto out;
1162 *out_ptr++ = *pat_ptr++;
1163 } else {
1164 switch (*++pat_ptr) {
1165 case 0:
1166 goto out;
1167 /* Double percent, output one percent */
1168 case '%':
1169 if (out_ptr == out_end)
1170 goto out;
1171 *out_ptr++ = '%';
1172 break;
1173 /* pid */
1174 case 'p':
1175 pid_in_pattern = 1;
1176 rc = snprintf(out_ptr, out_end - out_ptr,
1177 "%d", current->tgid);
1178 if (rc > out_end - out_ptr)
1179 goto out;
1180 out_ptr += rc;
1181 break;
1182 /* uid */
1183 case 'u':
1184 rc = snprintf(out_ptr, out_end - out_ptr,
1185 "%d", current->uid);
1186 if (rc > out_end - out_ptr)
1187 goto out;
1188 out_ptr += rc;
1189 break;
1190 /* gid */
1191 case 'g':
1192 rc = snprintf(out_ptr, out_end - out_ptr,
1193 "%d", current->gid);
1194 if (rc > out_end - out_ptr)
1195 goto out;
1196 out_ptr += rc;
1197 break;
1198 /* signal that caused the coredump */
1199 case 's':
1200 rc = snprintf(out_ptr, out_end - out_ptr,
1201 "%ld", signr);
1202 if (rc > out_end - out_ptr)
1203 goto out;
1204 out_ptr += rc;
1205 break;
1206 /* UNIX time of coredump */
1207 case 't': {
1208 struct timeval tv;
1209 do_gettimeofday(&tv);
1210 rc = snprintf(out_ptr, out_end - out_ptr,
1211 "%lu", tv.tv_sec);
1212 if (rc > out_end - out_ptr)
1213 goto out;
1214 out_ptr += rc;
1215 break;
1217 /* hostname */
1218 case 'h':
1219 down_read(&uts_sem);
1220 rc = snprintf(out_ptr, out_end - out_ptr,
1221 "%s", system_utsname.nodename);
1222 up_read(&uts_sem);
1223 if (rc > out_end - out_ptr)
1224 goto out;
1225 out_ptr += rc;
1226 break;
1227 /* executable */
1228 case 'e':
1229 rc = snprintf(out_ptr, out_end - out_ptr,
1230 "%s", current->comm);
1231 if (rc > out_end - out_ptr)
1232 goto out;
1233 out_ptr += rc;
1234 break;
1235 default:
1236 break;
1238 ++pat_ptr;
1241 /* Backward compatibility with core_uses_pid:
1243 * If core_pattern does not include a %p (as is the default)
1244 * and core_uses_pid is set, then .%pid will be appended to
1245 * the filename */
1246 if (!pid_in_pattern
1247 && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
1248 rc = snprintf(out_ptr, out_end - out_ptr,
1249 ".%d", current->tgid);
1250 if (rc > out_end - out_ptr)
1251 goto out;
1252 out_ptr += rc;
1254 out:
1255 *out_ptr = 0;
1258 static void zap_threads (struct mm_struct *mm)
1260 struct task_struct *g, *p;
1262 read_lock(&tasklist_lock);
1263 do_each_thread(g,p)
1264 if (mm == p->mm && p != current) {
1265 force_sig_specific(SIGKILL, p);
1266 mm->core_waiters++;
1268 while_each_thread(g,p);
1270 read_unlock(&tasklist_lock);
1273 static void coredump_wait(struct mm_struct *mm)
1275 DECLARE_COMPLETION(startup_done);
1277 mm->core_waiters++; /* let other threads block */
1278 mm->core_startup_done = &startup_done;
1280 /* give other threads a chance to run: */
1281 yield();
1283 zap_threads(mm);
1284 if (--mm->core_waiters) {
1285 up_write(&mm->mmap_sem);
1286 wait_for_completion(&startup_done);
1287 } else
1288 up_write(&mm->mmap_sem);
1289 BUG_ON(mm->core_waiters);
1292 int do_coredump(long signr, int exit_code, struct pt_regs * regs)
1294 char corename[CORENAME_MAX_SIZE + 1];
1295 struct mm_struct *mm = current->mm;
1296 struct linux_binfmt * binfmt;
1297 struct inode * inode;
1298 struct file * file;
1299 int retval = 0;
1301 lock_kernel();
1302 binfmt = current->binfmt;
1303 if (!binfmt || !binfmt->core_dump)
1304 goto fail;
1305 down_write(&mm->mmap_sem);
1306 if (!mm->dumpable) {
1307 up_write(&mm->mmap_sem);
1308 goto fail;
1310 mm->dumpable = 0;
1311 init_completion(&mm->core_done);
1312 current->signal->group_exit = 1;
1313 current->signal->group_exit_code = exit_code;
1314 coredump_wait(mm);
1316 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1317 goto fail_unlock;
1319 format_corename(corename, core_pattern, signr);
1320 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW, 0600);
1321 if (IS_ERR(file))
1322 goto fail_unlock;
1323 inode = file->f_dentry->d_inode;
1324 if (inode->i_nlink > 1)
1325 goto close_fail; /* multiple links - don't dump */
1326 if (d_unhashed(file->f_dentry))
1327 goto close_fail;
1329 if (!S_ISREG(inode->i_mode))
1330 goto close_fail;
1331 if (!file->f_op)
1332 goto close_fail;
1333 if (!file->f_op->write)
1334 goto close_fail;
1335 if (do_truncate(file->f_dentry, 0) != 0)
1336 goto close_fail;
1338 retval = binfmt->core_dump(signr, regs, file);
1340 current->signal->group_exit_code |= 0x80;
1341 close_fail:
1342 filp_close(file, NULL);
1343 fail_unlock:
1344 complete_all(&mm->core_done);
1345 fail:
1346 unlock_kernel();
1347 return retval;