4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * #!-checking implemented by tytso.
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
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/swap.h>
39 #include <linux/utsname.h>
40 #define __NO_VERSION__
41 #include <linux/module.h>
43 #include <asm/uaccess.h>
44 #include <asm/pgalloc.h>
45 #include <asm/mmu_context.h>
48 #include <linux/kmod.h>
52 char core_pattern[65] = "core";
53 /* The maximal length of core_pattern is also specified in sysctl.c */
55 static struct linux_binfmt *formats;
56 static rwlock_t binfmt_lock = RW_LOCK_UNLOCKED;
58 int register_binfmt(struct linux_binfmt * fmt)
60 struct linux_binfmt ** tmp = &formats;
66 write_lock(&binfmt_lock);
69 write_unlock(&binfmt_lock);
76 write_unlock(&binfmt_lock);
80 int unregister_binfmt(struct linux_binfmt * fmt)
82 struct linux_binfmt ** tmp = &formats;
84 write_lock(&binfmt_lock);
88 write_unlock(&binfmt_lock);
93 write_unlock(&binfmt_lock);
97 static inline void put_binfmt(struct linux_binfmt * fmt)
100 __MOD_DEC_USE_COUNT(fmt->module);
104 * Note that a shared library must be both readable and executable due to
107 * Also note that we take the address to load from from the file itself.
109 asmlinkage long sys_uselib(const char * library)
115 error = user_path_walk(library, &nd);
120 if (!S_ISREG(nd.dentry->d_inode->i_mode))
123 error = permission(nd.dentry->d_inode, MAY_READ | MAY_EXEC);
127 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
128 error = PTR_ERR(file);
133 if(file->f_op && file->f_op->read) {
134 struct linux_binfmt * fmt;
136 read_lock(&binfmt_lock);
137 for (fmt = formats ; fmt ; fmt = fmt->next) {
138 if (!fmt->load_shlib)
140 if (!try_inc_mod_count(fmt->module))
142 read_unlock(&binfmt_lock);
143 error = fmt->load_shlib(file);
144 read_lock(&binfmt_lock);
146 if (error != -ENOEXEC)
149 read_unlock(&binfmt_lock);
160 * count() counts the number of arguments/envelopes
162 static int count(char ** argv, int max)
170 if (get_user(p, argv))
183 * 'copy_strings()' copies argument/envelope strings from user
184 * memory to free pages in kernel mem. These are in a format ready
185 * to be put directly into the top of new user memory.
187 int copy_strings(int argc,char ** argv, struct linux_binprm *bprm)
189 struct page *kmapped_page = NULL;
198 if (get_user(str, argv+argc) ||
199 !(len = strnlen_user(str, bprm->p))) {
210 /* XXX: add architecture specific overflow check here. */
215 int offset, bytes_to_copy;
218 offset = pos % PAGE_SIZE;
220 page = bprm->page[i];
223 page = alloc_page(GFP_HIGHUSER);
224 bprm->page[i] = page;
232 if (page != kmapped_page) {
234 kunmap(kmapped_page);
236 kaddr = kmap(kmapped_page);
239 memset(kaddr, 0, offset);
240 bytes_to_copy = PAGE_SIZE - offset;
241 if (bytes_to_copy > len) {
244 memset(kaddr+offset+len, 0,
245 PAGE_SIZE-offset-len);
247 err = copy_from_user(kaddr+offset, str, bytes_to_copy);
253 pos += bytes_to_copy;
254 str += bytes_to_copy;
255 len -= bytes_to_copy;
261 kunmap(kmapped_page);
266 * Like copy_strings, but get argv and its values from kernel memory.
268 int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
271 mm_segment_t oldfs = get_fs();
273 r = copy_strings(argc, argv, bprm);
279 * This routine is used to map in a page into an address space: needed by
280 * execve() for the initial stack and environment pages.
282 * tsk->mmap_sem is held for writing.
284 void put_dirty_page(struct task_struct * tsk, struct page *page, unsigned long address)
290 if (page_count(page) != 1)
291 printk(KERN_ERR "mem_map disagrees with %p at %08lx\n", page, address);
292 pgd = pgd_offset(tsk->mm, address);
294 spin_lock(&tsk->mm->page_table_lock);
295 pmd = pmd_alloc(tsk->mm, pgd, address);
298 pte = pte_alloc(tsk->mm, pmd, address);
304 flush_dcache_page(page);
305 flush_page_to_ram(page);
306 set_pte(pte, pte_mkdirty(pte_mkwrite(mk_pte(page, PAGE_COPY))));
308 spin_unlock(&tsk->mm->page_table_lock);
310 /* no need for flush_tlb */
313 spin_unlock(&tsk->mm->page_table_lock);
315 force_sig(SIGKILL, tsk);
319 int setup_arg_pages(struct linux_binprm *bprm)
321 unsigned long stack_base;
322 struct vm_area_struct *mpnt;
325 stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
327 bprm->p += stack_base;
329 bprm->loader += stack_base;
330 bprm->exec += stack_base;
332 mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
336 down_write(¤t->mm->mmap_sem);
338 mpnt->vm_mm = current->mm;
339 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
340 mpnt->vm_end = STACK_TOP;
341 mpnt->vm_page_prot = PAGE_COPY;
342 mpnt->vm_flags = VM_STACK_FLAGS;
345 mpnt->vm_file = NULL;
346 mpnt->vm_private_data = (void *) 0;
347 insert_vm_struct(current->mm, mpnt);
348 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
351 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
352 struct page *page = bprm->page[i];
354 bprm->page[i] = NULL;
355 put_dirty_page(current,page,stack_base);
357 stack_base += PAGE_SIZE;
359 up_write(¤t->mm->mmap_sem);
364 struct file *open_exec(const char *name)
371 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd);
374 inode = nd.dentry->d_inode;
375 file = ERR_PTR(-EACCES);
376 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
377 S_ISREG(inode->i_mode)) {
378 int err = permission(inode, MAY_EXEC);
379 if (!err && !(inode->i_mode & 0111))
383 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
385 err = deny_write_access(file);
400 int kernel_read(struct file *file, unsigned long offset,
401 char * addr, unsigned long count)
405 int result = -ENOSYS;
407 if (!file->f_op->read)
411 result = file->f_op->read(file, addr, count, &pos);
417 static int exec_mmap(void)
419 struct mm_struct * mm, * old_mm;
421 old_mm = current->mm;
422 if (old_mm && atomic_read(&old_mm->mm_users) == 1) {
430 struct mm_struct *active_mm;
432 if (init_new_context(current, mm)) {
437 /* Add it to the list of mm's */
438 spin_lock(&mmlist_lock);
439 list_add(&mm->mmlist, &init_mm.mmlist);
441 spin_unlock(&mmlist_lock);
444 active_mm = current->active_mm;
446 current->active_mm = mm;
447 task_unlock(current);
448 activate_mm(active_mm, mm);
451 if (active_mm != old_mm) BUG();
462 * This function makes sure the current process has its own signal table,
463 * so that flush_signal_handlers can later reset the handlers without
464 * disturbing other processes. (Other processes might share the signal
465 * table via the CLONE_SIGNAL option to clone().)
468 static inline int make_private_signals(void)
470 struct signal_struct * newsig;
472 if (atomic_read(¤t->sig->count) <= 1)
474 newsig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
477 spin_lock_init(&newsig->siglock);
478 atomic_set(&newsig->count, 1);
479 memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
480 spin_lock_irq(¤t->sigmask_lock);
481 current->sig = newsig;
482 spin_unlock_irq(¤t->sigmask_lock);
487 * If make_private_signals() made a copy of the signal table, decrement the
488 * refcount of the original table, and free it if necessary.
489 * We don't do that in make_private_signals() so that we can back off
490 * in flush_old_exec() if an error occurs after calling make_private_signals().
493 static inline void release_old_signals(struct signal_struct * oldsig)
495 if (current->sig == oldsig)
497 if (atomic_dec_and_test(&oldsig->count))
498 kmem_cache_free(sigact_cachep, oldsig);
502 * These functions flushes out all traces of the currently running executable
503 * so that a new one can be started
506 static inline void flush_old_files(struct files_struct * files)
510 write_lock(&files->file_lock);
512 unsigned long set, i;
516 if (i >= files->max_fds || i >= files->max_fdset)
518 set = files->close_on_exec->fds_bits[j];
521 files->close_on_exec->fds_bits[j] = 0;
522 write_unlock(&files->file_lock);
523 for ( ; set ; i++,set >>= 1) {
528 write_lock(&files->file_lock);
531 write_unlock(&files->file_lock);
535 * An execve() will automatically "de-thread" the process.
536 * Note: we don't have to hold the tasklist_lock to test
537 * whether we migth need to do this. If we're not part of
538 * a thread group, there is no way we can become one
539 * dynamically. And if we are, we only need to protect the
540 * unlink - even if we race with the last other thread exit,
541 * at worst the list_del_init() might end up being a no-op.
543 static inline void de_thread(struct task_struct *tsk)
545 if (!list_empty(&tsk->thread_group)) {
546 write_lock_irq(&tasklist_lock);
547 list_del_init(&tsk->thread_group);
548 write_unlock_irq(&tasklist_lock);
551 /* Minor oddity: this might stay the same. */
552 tsk->tgid = tsk->pid;
555 int flush_old_exec(struct linux_binprm * bprm)
559 struct signal_struct * oldsig;
562 * Make sure we have a private signal table
564 oldsig = current->sig;
565 retval = make_private_signals();
566 if (retval) goto flush_failed;
569 * Release all of the old mmap stuff
571 retval = exec_mmap();
572 if (retval) goto mmap_failed;
574 /* This is the point of no return */
575 release_old_signals(oldsig);
577 current->sas_ss_sp = current->sas_ss_size = 0;
579 if (current->euid == current->uid && current->egid == current->gid)
580 current->mm->dumpable = 1;
581 name = bprm->filename;
582 for (i=0; (ch = *(name++)) != '\0';) {
587 current->comm[i++] = ch;
589 current->comm[i] = '\0';
595 if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
596 permission(bprm->file->f_dentry->d_inode,MAY_READ))
597 current->mm->dumpable = 0;
599 /* An exec changes our domain. We are no longer part of the thread
602 current->self_exec_id++;
604 flush_signal_handlers(current);
605 flush_old_files(current->files);
611 spin_lock_irq(¤t->sigmask_lock);
612 if (current->sig != oldsig) {
613 kmem_cache_free(sigact_cachep, current->sig);
614 current->sig = oldsig;
616 spin_unlock_irq(¤t->sigmask_lock);
621 * We mustn't allow tracing of suid binaries, unless
622 * the tracer has the capability to trace anything..
624 static inline int must_not_trace_exec(struct task_struct * p)
626 return (p->ptrace & PT_PTRACED) && !(p->ptrace & PT_PTRACE_CAP);
630 * Fill the binprm structure from the inode.
631 * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
633 int prepare_binprm(struct linux_binprm *bprm)
636 struct inode * inode = bprm->file->f_dentry->d_inode;
638 mode = inode->i_mode;
640 * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
641 * vfs_permission lets a non-executable through
643 if (!(mode & 0111)) /* with at least _one_ execute bit set */
645 if (bprm->file->f_op == NULL)
648 bprm->e_uid = current->euid;
649 bprm->e_gid = current->egid;
651 if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
654 bprm->e_uid = inode->i_uid;
658 * If setgid is set but no group execute bit then this
659 * is a candidate for mandatory locking, not a setgid
662 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
663 bprm->e_gid = inode->i_gid;
666 /* We don't have VFS support for capabilities yet */
667 cap_clear(bprm->cap_inheritable);
668 cap_clear(bprm->cap_permitted);
669 cap_clear(bprm->cap_effective);
671 /* To support inheritance of root-permissions and suid-root
672 * executables under compatibility mode, we raise all three
673 * capability sets for the file.
675 * If only the real uid is 0, we only raise the inheritable
676 * and permitted sets of the executable file.
679 if (!issecure(SECURE_NOROOT)) {
680 if (bprm->e_uid == 0 || current->uid == 0) {
681 cap_set_full(bprm->cap_inheritable);
682 cap_set_full(bprm->cap_permitted);
684 if (bprm->e_uid == 0)
685 cap_set_full(bprm->cap_effective);
688 memset(bprm->buf,0,BINPRM_BUF_SIZE);
689 return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
693 * This function is used to produce the new IDs and capabilities
694 * from the old ones and the file's capabilities.
696 * The formula used for evolving capabilities is:
699 * (***) pP' = (fP & X) | (fI & pI)
700 * pE' = pP' & fE [NB. fE is 0 or ~0]
702 * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
703 * ' indicates post-exec(), and X is the global 'cap_bset'.
707 void compute_creds(struct linux_binprm *bprm)
709 kernel_cap_t new_permitted, working;
712 new_permitted = cap_intersect(bprm->cap_permitted, cap_bset);
713 working = cap_intersect(bprm->cap_inheritable,
714 current->cap_inheritable);
715 new_permitted = cap_combine(new_permitted, working);
717 if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
718 !cap_issubset(new_permitted, current->cap_permitted)) {
719 current->mm->dumpable = 0;
722 if (must_not_trace_exec(current)
723 || atomic_read(¤t->fs->count) > 1
724 || atomic_read(¤t->files->count) > 1
725 || atomic_read(¤t->sig->count) > 1) {
726 if(!capable(CAP_SETUID)) {
727 bprm->e_uid = current->uid;
728 bprm->e_gid = current->gid;
730 if(!capable(CAP_SETPCAP)) {
731 new_permitted = cap_intersect(new_permitted,
732 current->cap_permitted);
739 /* For init, we want to retain the capabilities set
740 * in the init_task struct. Thus we skip the usual
741 * capability rules */
742 if (current->pid != 1) {
743 current->cap_permitted = new_permitted;
744 current->cap_effective =
745 cap_intersect(new_permitted, bprm->cap_effective);
748 /* AUD: Audit candidate if current->cap_effective is set */
750 current->suid = current->euid = current->fsuid = bprm->e_uid;
751 current->sgid = current->egid = current->fsgid = bprm->e_gid;
755 current->keep_capabilities = 0;
759 void remove_arg_zero(struct linux_binprm *bprm)
762 unsigned long offset;
766 offset = bprm->p % PAGE_SIZE;
769 while (bprm->p++, *(kaddr+offset++)) {
770 if (offset != PAGE_SIZE)
775 page = bprm->page[bprm->p/PAGE_SIZE];
784 * cycle the list of binary formats handler, until one recognizes the image
786 int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
789 struct linux_binfmt *fmt;
791 /* handle /sbin/loader.. */
793 struct exec * eh = (struct exec *) bprm->buf;
795 if (!bprm->loader && eh->fh.f_magic == 0x183 &&
796 (eh->fh.f_flags & 0x3000) == 0x3000)
799 unsigned long loader;
801 allow_write_access(bprm->file);
805 loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
807 file = open_exec("/sbin/loader");
808 retval = PTR_ERR(file);
812 /* Remember if the application is TASO. */
813 bprm->sh_bang = eh->ah.entry < 0x100000000;
816 bprm->loader = loader;
817 retval = prepare_binprm(bprm);
820 /* should call search_binary_handler recursively here,
821 but it does not matter */
825 /* kernel module loader fixup */
826 /* so we don't try to load run modprobe in kernel space. */
828 for (try=0; try<2; try++) {
829 read_lock(&binfmt_lock);
830 for (fmt = formats ; fmt ; fmt = fmt->next) {
831 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
834 if (!try_inc_mod_count(fmt->module))
836 read_unlock(&binfmt_lock);
837 retval = fn(bprm, regs);
840 allow_write_access(bprm->file);
844 current->did_exec = 1;
847 read_lock(&binfmt_lock);
849 if (retval != -ENOEXEC)
852 read_unlock(&binfmt_lock);
856 read_unlock(&binfmt_lock);
857 if (retval != -ENOEXEC) {
861 #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
863 if (printable(bprm->buf[0]) &&
864 printable(bprm->buf[1]) &&
865 printable(bprm->buf[2]) &&
866 printable(bprm->buf[3]))
867 break; /* -ENOEXEC */
868 sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
869 request_module(modname);
878 * sys_execve() executes a new program.
880 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
882 struct linux_binprm bprm;
887 file = open_exec(filename);
889 retval = PTR_ERR(file);
893 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
894 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
897 bprm.filename = filename;
901 if ((bprm.argc = count(argv, bprm.p / sizeof(void *))) < 0) {
902 allow_write_access(file);
907 if ((bprm.envc = count(envp, bprm.p / sizeof(void *))) < 0) {
908 allow_write_access(file);
913 retval = prepare_binprm(&bprm);
917 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
922 retval = copy_strings(bprm.envc, envp, &bprm);
926 retval = copy_strings(bprm.argc, argv, &bprm);
930 retval = search_binary_handler(&bprm,regs);
936 /* Something went wrong, return the inode and free the argument pages*/
937 allow_write_access(bprm.file);
941 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
942 struct page * page = bprm.page[i];
950 void set_binfmt(struct linux_binfmt *new)
952 struct linux_binfmt *old = current->binfmt;
953 if (new && new->module)
954 __MOD_INC_USE_COUNT(new->module);
955 current->binfmt = new;
956 if (old && old->module)
957 __MOD_DEC_USE_COUNT(old->module);
960 #define CORENAME_MAX_SIZE 64
962 /* format_corename will inspect the pattern parameter, and output a
963 * name into corename, which must have space for at least
964 * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
966 void format_corename(char *corename, const char *pattern, long signr)
968 const char *pat_ptr = pattern;
969 char *out_ptr = corename;
970 char *const out_end = corename + CORENAME_MAX_SIZE;
972 int pid_in_pattern = 0;
974 /* Repeat as long as we have more pattern to process and more output
977 if (*pat_ptr != '%') {
978 if (out_ptr == out_end)
980 *out_ptr++ = *pat_ptr++;
982 switch (*++pat_ptr) {
985 /* Double percent, output one percent */
987 if (out_ptr == out_end)
994 rc = snprintf(out_ptr, out_end - out_ptr,
996 if (rc > out_end - out_ptr)
1002 rc = snprintf(out_ptr, out_end - out_ptr,
1003 "%d", current->uid);
1004 if (rc > out_end - out_ptr)
1010 rc = snprintf(out_ptr, out_end - out_ptr,
1011 "%d", current->gid);
1012 if (rc > out_end - out_ptr)
1016 /* signal that caused the coredump */
1018 rc = snprintf(out_ptr, out_end - out_ptr,
1020 if (rc > out_end - out_ptr)
1024 /* UNIX time of coredump */
1027 do_gettimeofday(&tv);
1028 rc = snprintf(out_ptr, out_end - out_ptr,
1030 if (rc > out_end - out_ptr)
1037 down_read(&uts_sem);
1038 rc = snprintf(out_ptr, out_end - out_ptr,
1039 "%s", system_utsname.nodename);
1041 if (rc > out_end - out_ptr)
1047 rc = snprintf(out_ptr, out_end - out_ptr,
1048 "%s", current->comm);
1049 if (rc > out_end - out_ptr)
1059 /* Backward compatibility with core_uses_pid:
1061 * If core_pattern does not include a %p (as is the default)
1062 * and core_uses_pid is set, then .%pid will be appended to
1065 && (core_uses_pid || atomic_read(¤t->mm->mm_users) != 1)) {
1066 rc = snprintf(out_ptr, out_end - out_ptr,
1067 ".%d", current->pid);
1068 if (rc > out_end - out_ptr)
1076 int do_coredump(long signr, struct pt_regs * regs)
1078 struct linux_binfmt * binfmt;
1079 char corename[CORENAME_MAX_SIZE + 1];
1081 struct inode * inode;
1085 binfmt = current->binfmt;
1086 if (!binfmt || !binfmt->core_dump)
1088 if (!current->mm->dumpable)
1090 current->mm->dumpable = 0;
1091 if (current->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
1094 format_corename(corename, core_pattern, signr);
1095 file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW, 0600);
1098 inode = file->f_dentry->d_inode;
1099 if (inode->i_nlink > 1)
1100 goto close_fail; /* multiple links - don't dump */
1101 if (d_unhashed(file->f_dentry))
1104 if (!S_ISREG(inode->i_mode))
1108 if (!file->f_op->write)
1110 if (do_truncate(file->f_dentry, 0) != 0)
1113 retval = binfmt->core_dump(signr, regs, file);
1116 filp_close(file, NULL);