home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / fs / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-30  |  23.2 KB  |  903 lines

  1. /*
  2.  *  linux/fs/exec.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * #!-checking implemented by tytso.
  9.  */
  10.  
  11. /*
  12.  * Demand-loading implemented 01.12.91 - no need to read anything but
  13.  * the header into memory. The inode of the executable is put into
  14.  * "current->executable", and page faults do the actual loading. Clean.
  15.  *
  16.  * Once more I can proudly say that linux stood up to being changed: it
  17.  * was less than 2 hours work to get demand-loading completely implemented.
  18.  *
  19.  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
  20.  * current->executable is only used by the procfs.  This allows a dispatch
  21.  * table to check for several different types  of binary formats.  We keep
  22.  * trying until we recognize the file or we run out of supported binary
  23.  * formats. 
  24.  */
  25.  
  26. #include <linux/fs.h>
  27. #include <linux/sched.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mm.h>
  30. #include <linux/mman.h>
  31. #include <linux/a.out.h>
  32. #include <linux/errno.h>
  33. #include <linux/signal.h>
  34. #include <linux/string.h>
  35. #include <linux/stat.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/user.h>
  39. #include <linux/malloc.h>
  40. #include <linux/binfmts.h>
  41. #include <linux/personality.h>
  42.  
  43. #include <asm/system.h>
  44. #include <asm/segment.h>
  45. #include <asm/pgtable.h>
  46.  
  47. #include <linux/config.h>
  48.  
  49. asmlinkage int sys_exit(int exit_code);
  50. asmlinkage int sys_brk(unsigned long);
  51.  
  52. static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
  53. static int load_aout_library(int fd);
  54. static int aout_core_dump(long signr, struct pt_regs * regs);
  55.  
  56. extern void dump_thread(struct pt_regs *, struct user *);
  57.  
  58. /*
  59.  * Here are the actual binaries that will be accepted:
  60.  * add more with "register_binfmt()"..
  61.  */
  62. extern struct linux_binfmt elf_format;
  63.  
  64. static struct linux_binfmt aout_format = {
  65. #ifndef CONFIG_BINFMT_ELF
  66.      NULL, NULL, load_aout_binary, load_aout_library, aout_core_dump
  67. #else
  68.      &elf_format, NULL, load_aout_binary, load_aout_library, aout_core_dump
  69. #endif
  70. };
  71.  
  72. static struct linux_binfmt *formats = &aout_format;
  73.  
  74. int register_binfmt(struct linux_binfmt * fmt)
  75. {
  76.     struct linux_binfmt ** tmp = &formats;
  77.  
  78.     if (!fmt)
  79.         return -EINVAL;
  80.     if (fmt->next)
  81.         return -EBUSY;
  82.     while (*tmp) {
  83.         if (fmt == *tmp)
  84.             return -EBUSY;
  85.         tmp = &(*tmp)->next;
  86.     }
  87.     *tmp = fmt;
  88.     return 0;    
  89. }
  90.  
  91. int unregister_binfmt(struct linux_binfmt * fmt)
  92. {
  93.     struct linux_binfmt ** tmp = &formats;
  94.  
  95.     while (*tmp) {
  96.         if (fmt == *tmp) {
  97.             *tmp = fmt->next;
  98.             return 0;
  99.         }
  100.         tmp = &(*tmp)->next;
  101.     }
  102.     return -EINVAL;
  103. }
  104.  
  105. int open_inode(struct inode * inode, int mode)
  106. {
  107.     int error, fd;
  108.     struct file *f, **fpp;
  109.  
  110.     if (!inode->i_op || !inode->i_op->default_file_ops)
  111.         return -EINVAL;
  112.     f = get_empty_filp();
  113.     if (!f)
  114.         return -ENFILE;
  115.     fd = 0;
  116.     fpp = current->files->fd;
  117.     for (;;) {
  118.         if (!*fpp)
  119.             break;
  120.         if (++fd >= NR_OPEN) {
  121.             f->f_count--;
  122.             return -EMFILE;
  123.         }
  124.         fpp++;
  125.     }
  126.     *fpp = f;
  127.     f->f_flags = mode;
  128.     f->f_mode = (mode+1) & O_ACCMODE;
  129.     f->f_inode = inode;
  130.     f->f_pos = 0;
  131.     f->f_reada = 0;
  132.     f->f_op = inode->i_op->default_file_ops;
  133.     if (f->f_op->open) {
  134.         error = f->f_op->open(inode,f);
  135.         if (error) {
  136.             *fpp = NULL;
  137.             f->f_count--;
  138.             return error;
  139.         }
  140.     }
  141.     inode->i_count++;
  142.     return fd;
  143. }
  144.  
  145. /*
  146.  * These are the only things you should do on a core-file: use only these
  147.  * macros to write out all the necessary info.
  148.  */
  149. #define DUMP_WRITE(addr,nr) \
  150. while (file.f_op->write(inode,&file,(char *)(addr),(nr)) != (nr)) goto close_coredump
  151.  
  152. #define DUMP_SEEK(offset) \
  153. if (file.f_op->lseek) { \
  154.     if (file.f_op->lseek(inode,&file,(offset),0) != (offset)) \
  155.          goto close_coredump; \
  156. } else file.f_pos = (offset)        
  157.  
  158. /*
  159.  * Routine writes a core dump image in the current directory.
  160.  * Currently only a stub-function.
  161.  *
  162.  * Note that setuid/setgid files won't make a core-dump if the uid/gid
  163.  * changed due to the set[u|g]id. It's enforced by the "current->dumpable"
  164.  * field, which also makes sure the core-dumps won't be recursive if the
  165.  * dumping of the process results in another error..
  166.  */
  167. static int aout_core_dump(long signr, struct pt_regs * regs)
  168. {
  169.     struct inode * inode = NULL;
  170.     struct file file;
  171.     unsigned short fs;
  172.     int has_dumped = 0;
  173.     char corefile[6+sizeof(current->comm)];
  174.     unsigned long dump_start, dump_size;
  175.     struct user dump;
  176.  
  177.     if (!current->dumpable)
  178.         return 0;
  179.     current->dumpable = 0;
  180.  
  181. /* See if we have enough room to write the upage.  */
  182.     if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
  183.         return 0;
  184.     fs = get_fs();
  185.     set_fs(KERNEL_DS);
  186.     memcpy(corefile,"core.",5);
  187. #if 0
  188.     memcpy(corefile+5,current->comm,sizeof(current->comm));
  189. #else
  190.     corefile[4] = '\0';
  191. #endif
  192.     if (open_namei(corefile,O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)) {
  193.         inode = NULL;
  194.         goto end_coredump;
  195.     }
  196.     if (!S_ISREG(inode->i_mode))
  197.         goto end_coredump;
  198.     if (!inode->i_op || !inode->i_op->default_file_ops)
  199.         goto end_coredump;
  200.     if (get_write_access(inode))
  201.         goto end_coredump;
  202.     file.f_mode = 3;
  203.     file.f_flags = 0;
  204.     file.f_count = 1;
  205.     file.f_inode = inode;
  206.     file.f_pos = 0;
  207.     file.f_reada = 0;
  208.     file.f_op = inode->i_op->default_file_ops;
  209.     if (file.f_op->open)
  210.         if (file.f_op->open(inode,&file))
  211.             goto done_coredump;
  212.     if (!file.f_op->write)
  213.         goto close_coredump;
  214.     has_dumped = 1;
  215.            strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  216.     dump.u_ar0 = (struct pt_regs *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
  217.     dump.signal = signr;
  218.     dump_thread(regs, &dump);
  219.  
  220. /* If the size of the dump file exceeds the rlimit, then see what would happen
  221.    if we wrote the stack, but not the data area.  */
  222.     if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
  223.         current->rlim[RLIMIT_CORE].rlim_cur)
  224.         dump.u_dsize = 0;
  225.  
  226. /* Make sure we have enough room to write the stack and data areas. */
  227.     if ((dump.u_ssize+1) * PAGE_SIZE >
  228.         current->rlim[RLIMIT_CORE].rlim_cur)
  229.         dump.u_ssize = 0;
  230.  
  231.     set_fs(KERNEL_DS);
  232. /* struct user */
  233.     DUMP_WRITE(&dump,sizeof(dump));
  234. /* Now dump all of the user data.  Include malloced stuff as well */
  235.     DUMP_SEEK(PAGE_SIZE);
  236. /* now we start writing out the user space info */
  237.     set_fs(USER_DS);
  238. /* Dump the data area */
  239.     if (dump.u_dsize != 0) {
  240.         dump_start = dump.u_tsize << 12;
  241.         dump_size = dump.u_dsize << 12;
  242.         DUMP_WRITE(dump_start,dump_size);
  243.     }
  244. /* Now prepare to dump the stack area */
  245.     if (dump.u_ssize != 0) {
  246.         dump_start = dump.start_stack;
  247.         dump_size = dump.u_ssize << 12;
  248.         DUMP_WRITE(dump_start,dump_size);
  249.     }
  250. /* Finally dump the task struct.  Not be used by gdb, but could be useful */
  251.     set_fs(KERNEL_DS);
  252.     DUMP_WRITE(current,sizeof(*current));
  253. close_coredump:
  254.     if (file.f_op->release)
  255.         file.f_op->release(inode,&file);
  256. done_coredump:
  257.     put_write_access(inode);
  258. end_coredump:
  259.     set_fs(fs);
  260.     iput(inode);
  261.     return has_dumped;
  262. }
  263.  
  264. /*
  265.  * Note that a shared library must be both readable and executable due to
  266.  * security reasons.
  267.  *
  268.  * Also note that we take the address to load from from the file itself.
  269.  */
  270. asmlinkage int sys_uselib(const char * library)
  271. {
  272.     int fd, retval;
  273.     struct file * file;
  274.     struct linux_binfmt * fmt;
  275.  
  276.     fd = sys_open(library, 0, 0);
  277.     if (fd < 0)
  278.         return fd;
  279.     file = current->files->fd[fd];
  280.     retval = -ENOEXEC;
  281.     if (file && file->f_inode && file->f_op && file->f_op->read) {
  282.         for (fmt = formats ; fmt ; fmt = fmt->next) {
  283.             int (*fn)(int) = fmt->load_shlib;
  284.             if (!fn)
  285.                 break;
  286.             retval = fn(fd);
  287.             if (retval != -ENOEXEC)
  288.                 break;
  289.         }
  290.     }
  291.     sys_close(fd);
  292.       return retval;
  293. }
  294.  
  295. /*
  296.  * create_tables() parses the env- and arg-strings in new user
  297.  * memory and creates the pointer tables from them, and puts their
  298.  * addresses on the "stack", returning the new stack pointer value.
  299.  */
  300. unsigned long * create_tables(char * p,int argc,int envc,int ibcs)
  301. {
  302.     unsigned long *argv,*envp;
  303.     unsigned long * sp;
  304.     struct vm_area_struct *mpnt;
  305.  
  306.     mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
  307.     if (mpnt) {
  308.         mpnt->vm_task = current;
  309.         mpnt->vm_start = PAGE_MASK & (unsigned long) p;
  310.         mpnt->vm_end = TASK_SIZE;
  311.         mpnt->vm_page_prot = PAGE_COPY;
  312.         mpnt->vm_flags = VM_STACK_FLAGS;
  313.         mpnt->vm_ops = NULL;
  314.         mpnt->vm_offset = 0;
  315.         mpnt->vm_inode = NULL;
  316.         mpnt->vm_pte = 0;
  317.         insert_vm_struct(current, mpnt);
  318.     }
  319.     sp = (unsigned long *) (0xfffffffc & (unsigned long) p);
  320.     sp -= envc+1;
  321.     envp = sp;
  322.     sp -= argc+1;
  323.     argv = sp;
  324.     if (!ibcs) {
  325.         put_fs_long((unsigned long)envp,--sp);
  326.         put_fs_long((unsigned long)argv,--sp);
  327.     }
  328.     put_fs_long((unsigned long)argc,--sp);
  329.     current->mm->arg_start = (unsigned long) p;
  330.     while (argc-->0) {
  331.         put_fs_long((unsigned long) p,argv++);
  332.         while (get_fs_byte(p++)) /* nothing */ ;
  333.     }
  334.     put_fs_long(0,argv);
  335.     current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  336.     while (envc-->0) {
  337.         put_fs_long((unsigned long) p,envp++);
  338.         while (get_fs_byte(p++)) /* nothing */ ;
  339.     }
  340.     put_fs_long(0,envp);
  341.     current->mm->env_end = (unsigned long) p;
  342.     return sp;
  343. }
  344.  
  345. /*
  346.  * count() counts the number of arguments/envelopes
  347.  *
  348.  * We also do some limited EFAULT checking: this isn't complete, but
  349.  * it does cover most cases. I'll have to do this correctly some day..
  350.  */
  351. static int count(char ** argv)
  352. {
  353.     int error, i = 0;
  354.     char ** tmp, *p;
  355.  
  356.     if ((tmp = argv) != NULL) {
  357.         error = verify_area(VERIFY_READ, tmp, sizeof(char *));
  358.         if (error)
  359.             return error;
  360.         while ((p = (char *) get_fs_long((unsigned long *) (tmp++))) != NULL) {
  361.             i++;
  362.             error = verify_area(VERIFY_READ, p, 1);
  363.             if (error)
  364.                 return error;
  365.         }
  366.     }
  367.     return i;
  368. }
  369.  
  370. /*
  371.  * 'copy_string()' copies argument/envelope strings from user
  372.  * memory to free pages in kernel mem. These are in a format ready
  373.  * to be put directly into the top of new user memory.
  374.  *
  375.  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
  376.  * whether the string and the string array are from user or kernel segments:
  377.  * 
  378.  * from_kmem     argv *        argv **
  379.  *    0          user space    user space
  380.  *    1          kernel space  user space
  381.  *    2          kernel space  kernel space
  382.  * 
  383.  * We do this by playing games with the fs segment register.  Since it
  384.  * is expensive to load a segment register, we try to avoid calling
  385.  * set_fs() unless we absolutely have to.
  386.  */
  387. unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
  388.         unsigned long p, int from_kmem)
  389. {
  390.     char *tmp, *pag = NULL;
  391.     int len, offset = 0;
  392.     unsigned long old_fs, new_fs;
  393.  
  394.     if (!p)
  395.         return 0;    /* bullet-proofing */
  396.     new_fs = get_ds();
  397.     old_fs = get_fs();
  398.     if (from_kmem==2)
  399.         set_fs(new_fs);
  400.     while (argc-- > 0) {
  401.         if (from_kmem == 1)
  402.             set_fs(new_fs);
  403.         if (!(tmp = (char *)get_fs_long(((unsigned long *)argv)+argc)))
  404.             panic("VFS: argc is wrong");
  405.         if (from_kmem == 1)
  406.             set_fs(old_fs);
  407.         len=0;        /* remember zero-padding */
  408.         do {
  409.             len++;
  410.         } while (get_fs_byte(tmp++));
  411.         if (p < len) {    /* this shouldn't happen - 128kB */
  412.             set_fs(old_fs);
  413.             return 0;
  414.         }
  415.         while (len) {
  416.             --p; --tmp; --len;
  417.             if (--offset < 0) {
  418.                 offset = p % PAGE_SIZE;
  419.                 if (from_kmem==2)
  420.                     set_fs(old_fs);
  421.                 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
  422.                     !(pag = (char *) page[p/PAGE_SIZE] =
  423.                       (unsigned long *) get_free_page(GFP_USER))) 
  424.                     return 0;
  425.                 if (from_kmem==2)
  426.                     set_fs(new_fs);
  427.  
  428.             }
  429.             *(pag + offset) = get_fs_byte(tmp);
  430.         }
  431.     }
  432.     if (from_kmem==2)
  433.         set_fs(old_fs);
  434.     return p;
  435. }
  436.  
  437. unsigned long setup_arg_pages(unsigned long text_size,unsigned long * page)
  438. {
  439.     unsigned long code_limit,data_limit,code_base,data_base;
  440.     int i;
  441.  
  442.     code_limit = TASK_SIZE;
  443.     data_limit = TASK_SIZE;
  444.     code_base = data_base = 0;
  445.     current->mm->start_code = code_base;
  446.     data_base += data_limit;
  447.     for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
  448.         data_base -= PAGE_SIZE;
  449.         if (page[i]) {
  450.             current->mm->rss++;
  451.             put_dirty_page(current,page[i],data_base);
  452.         }
  453.     }
  454.     return data_limit;
  455. }
  456.  
  457. /*
  458.  * Read in the complete executable. This is used for "-N" files
  459.  * that aren't on a block boundary, and for files on filesystems
  460.  * without bmap support.
  461.  */
  462. int read_exec(struct inode *inode, unsigned long offset,
  463.     char * addr, unsigned long count)
  464. {
  465.     struct file file;
  466.     int result = -ENOEXEC;
  467.  
  468.     if (!inode->i_op || !inode->i_op->default_file_ops)
  469.         goto end_readexec;
  470.     file.f_mode = 1;
  471.     file.f_flags = 0;
  472.     file.f_count = 1;
  473.     file.f_inode = inode;
  474.     file.f_pos = 0;
  475.     file.f_reada = 0;
  476.     file.f_op = inode->i_op->default_file_ops;
  477.     if (file.f_op->open)
  478.         if (file.f_op->open(inode,&file))
  479.             goto end_readexec;
  480.     if (!file.f_op || !file.f_op->read)
  481.         goto close_readexec;
  482.     if (file.f_op->lseek) {
  483.         if (file.f_op->lseek(inode,&file,offset,0) != offset)
  484.              goto close_readexec;
  485.     } else
  486.         file.f_pos = offset;
  487.     if (get_fs() == USER_DS) {
  488.         result = verify_area(VERIFY_WRITE, addr, count);
  489.         if (result)
  490.             goto close_readexec;
  491.     }
  492.     result = file.f_op->read(inode, &file, addr, count);
  493. close_readexec:
  494.     if (file.f_op->release)
  495.         file.f_op->release(inode,&file);
  496. end_readexec:
  497.     return result;
  498. }
  499.  
  500.  
  501. /*
  502.  * This function flushes out all traces of the currently running executable so
  503.  * that a new one can be started
  504.  */
  505.  
  506. void flush_old_exec(struct linux_binprm * bprm)
  507. {
  508.     int i;
  509.     int ch;
  510.     char * name;
  511.  
  512.     current->dumpable = 1;
  513.     name = bprm->filename;
  514.     for (i=0; (ch = *(name++)) != '\0';) {
  515.         if (ch == '/')
  516.             i = 0;
  517.         else
  518.             if (i < 15)
  519.                 current->comm[i++] = ch;
  520.     }
  521.     current->comm[i] = '\0';
  522.  
  523.     /* Release all of the old mmap stuff. */
  524.     exit_mmap(current);
  525.  
  526.     flush_thread();
  527.  
  528.     if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
  529.         permission(bprm->inode,MAY_READ))
  530.         current->dumpable = 0;
  531.     current->signal = 0;
  532.     for (i=0 ; i<32 ; i++) {
  533.         current->sigaction[i].sa_mask = 0;
  534.         current->sigaction[i].sa_flags = 0;
  535.         if (current->sigaction[i].sa_handler != SIG_IGN)
  536.             current->sigaction[i].sa_handler = NULL;
  537.     }
  538.     for (i=0 ; i<NR_OPEN ; i++)
  539.         if (FD_ISSET(i,¤t->files->close_on_exec))
  540.             sys_close(i);
  541.     FD_ZERO(¤t->files->close_on_exec);
  542.     clear_page_tables(current);
  543.     if (last_task_used_math == current)
  544.         last_task_used_math = NULL;
  545.     current->used_math = 0;
  546. }
  547.  
  548. /*
  549.  * sys_execve() executes a new program.
  550.  */
  551. int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
  552. {
  553.     struct linux_binprm bprm;
  554.     struct linux_binfmt * fmt;
  555.     unsigned long old_fs;
  556.     int i;
  557.     int retval;
  558.     int sh_bang = 0;
  559.  
  560.     bprm.p = PAGE_SIZE*MAX_ARG_PAGES-4;
  561.     for (i=0 ; i<MAX_ARG_PAGES ; i++)    /* clear page-table */
  562.         bprm.page[i] = 0;
  563.     retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
  564.     if (retval)
  565.         return retval;
  566.     bprm.filename = filename;
  567.     if ((bprm.argc = count(argv)) < 0)
  568.         return bprm.argc;
  569.     if ((bprm.envc = count(envp)) < 0)
  570.         return bprm.envc;
  571.     
  572. restart_interp:
  573.     if (!S_ISREG(bprm.inode->i_mode)) {    /* must be regular file */
  574.         retval = -EACCES;
  575.         goto exec_error2;
  576.     }
  577.     if (IS_NOEXEC(bprm.inode)) {        /* FS mustn't be mounted noexec */
  578.         retval = -EPERM;
  579.         goto exec_error2;
  580.     }
  581.     if (!bprm.inode->i_sb) {
  582.         retval = -EACCES;
  583.         goto exec_error2;
  584.     }
  585.     i = bprm.inode->i_mode;
  586.     if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
  587.         euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
  588.         retval = -EPERM;
  589.         goto exec_error2;
  590.     }
  591.     /* make sure we don't let suid, sgid files be ptraced. */
  592.     if (current->flags & PF_PTRACED) {
  593.         bprm.e_uid = current->euid;
  594.         bprm.e_gid = current->egid;
  595.     } else {
  596.         bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
  597.         bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
  598.     }
  599.     if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
  600.         goto exec_error2;
  601.     if (!(bprm.inode->i_mode & 0111) && fsuser()) {
  602.         retval = -EACCES;
  603.         goto exec_error2;
  604.     }
  605.     /* better not execute files which are being written to */
  606.     if (bprm.inode->i_wcount > 0) {
  607.         retval = -ETXTBSY;
  608.         goto exec_error2;
  609.     }
  610.     memset(bprm.buf,0,sizeof(bprm.buf));
  611.     old_fs = get_fs();
  612.     set_fs(get_ds());
  613.     retval = read_exec(bprm.inode,0,bprm.buf,128);
  614.     set_fs(old_fs);
  615.     if (retval < 0)
  616.         goto exec_error2;
  617.     if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
  618.         /*
  619.          * This section does the #! interpretation.
  620.          * Sorta complicated, but hopefully it will work.  -TYT
  621.          */
  622.  
  623.         char *cp, *interp, *i_name, *i_arg;
  624.  
  625.         iput(bprm.inode);
  626.         bprm.buf[127] = '\0';
  627.         if ((cp = strchr(bprm.buf, '\n')) == NULL)
  628.             cp = bprm.buf+127;
  629.         *cp = '\0';
  630.         while (cp > bprm.buf) {
  631.             cp--;
  632.             if ((*cp == ' ') || (*cp == '\t'))
  633.                 *cp = '\0';
  634.             else
  635.                 break;
  636.         }
  637.         for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
  638.         if (!cp || *cp == '\0') {
  639.             retval = -ENOEXEC; /* No interpreter name found */
  640.             goto exec_error1;
  641.         }
  642.         interp = i_name = cp;
  643.         i_arg = 0;
  644.         for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
  645.              if (*cp == '/')
  646.                 i_name = cp+1;
  647.         }
  648.         while ((*cp == ' ') || (*cp == '\t'))
  649.             *cp++ = '\0';
  650.         if (*cp)
  651.             i_arg = cp;
  652.         /*
  653.          * OK, we've parsed out the interpreter name and
  654.          * (optional) argument.
  655.          */
  656.         if (sh_bang++ == 0) {
  657.             bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
  658.             bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
  659.         }
  660.         /*
  661.          * Splice in (1) the interpreter's name for argv[0]
  662.          *           (2) (optional) argument to interpreter
  663.          *           (3) filename of shell script
  664.          *
  665.          * This is done in reverse order, because of how the
  666.          * user environment and arguments are stored.
  667.          */
  668.         bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
  669.         bprm.argc++;
  670.         if (i_arg) {
  671.             bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
  672.             bprm.argc++;
  673.         }
  674.         bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
  675.         bprm.argc++;
  676.         if (!bprm.p) {
  677.             retval = -E2BIG;
  678.             goto exec_error1;
  679.         }
  680.         /*
  681.          * OK, now restart the process with the interpreter's inode.
  682.          * Note that we use open_namei() as the name is now in kernel
  683.          * space, and we don't need to copy it.
  684.          */
  685.         retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
  686.         if (retval)
  687.             goto exec_error1;
  688.         goto restart_interp;
  689.     }
  690.     if (!sh_bang) {
  691.         bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
  692.         bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
  693.         if (!bprm.p) {
  694.             retval = -E2BIG;
  695.             goto exec_error2;
  696.         }
  697.     }
  698.  
  699.     bprm.sh_bang = sh_bang;
  700.     for (fmt = formats ; fmt ; fmt = fmt->next) {
  701.         int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  702.         if (!fn)
  703.             break;
  704.         retval = fn(&bprm, regs);
  705.         if (retval >= 0) {
  706.             iput(bprm.inode);
  707.             current->did_exec = 1;
  708.             return retval;
  709.         }
  710.         if (retval != -ENOEXEC)
  711.             break;
  712.     }
  713. exec_error2:
  714.     iput(bprm.inode);
  715. exec_error1:
  716.     for (i=0 ; i<MAX_ARG_PAGES ; i++)
  717.         free_page(bprm.page[i]);
  718.     return(retval);
  719. }
  720.  
  721. static void set_brk(unsigned long start, unsigned long end)
  722. {
  723.     start = PAGE_ALIGN(start);
  724.     end = PAGE_ALIGN(end);
  725.     if (end <= start)
  726.         return;
  727.     do_mmap(NULL, start, end - start,
  728.         PROT_READ | PROT_WRITE | PROT_EXEC,
  729.         MAP_FIXED | MAP_PRIVATE, 0);
  730. }
  731.  
  732. /*
  733.  * These are the functions used to load a.out style executables and shared
  734.  * libraries.  There is no binary dependent code anywhere else.
  735.  */
  736.  
  737. static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  738. {
  739.     struct exec ex;
  740.     struct file * file;
  741.     int fd, error;
  742.     unsigned long p = bprm->p;
  743.     unsigned long fd_offset;
  744.  
  745.     ex = *((struct exec *) bprm->buf);        /* exec-header */
  746.     if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
  747.          N_MAGIC(ex) != QMAGIC) ||
  748.         ex.a_trsize || ex.a_drsize ||
  749.         bprm->inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
  750.         return -ENOEXEC;
  751.     }
  752.  
  753.     current->personality = PER_LINUX;
  754.     fd_offset = N_TXTOFF(ex);
  755.     if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
  756.         printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
  757.         return -ENOEXEC;
  758.     }
  759.  
  760.     if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
  761.         (fd_offset < bprm->inode->i_sb->s_blocksize)) {
  762.         printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
  763.         return -ENOEXEC;
  764.     }
  765.  
  766.     /* OK, This is the point of no return */
  767.     flush_old_exec(bprm);
  768.  
  769.     current->mm->brk = ex.a_bss +
  770.         (current->mm->start_brk =
  771.         (current->mm->end_data = ex.a_data +
  772.         (current->mm->end_code = ex.a_text +
  773.         (current->mm->start_code = N_TXTADDR(ex)))));
  774.     current->mm->rss = 0;
  775.     current->mm->mmap = NULL;
  776.     current->suid = current->euid = current->fsuid = bprm->e_uid;
  777.     current->sgid = current->egid = current->fsgid = bprm->e_gid;
  778.     if (N_MAGIC(ex) == OMAGIC) {
  779.         do_mmap(NULL, 0, ex.a_text+ex.a_data,
  780.             PROT_READ|PROT_WRITE|PROT_EXEC,
  781.             MAP_FIXED|MAP_PRIVATE, 0);
  782.         read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data);
  783.     } else {
  784.         if (ex.a_text & 0xfff || ex.a_data & 0xfff)
  785.             printk(KERN_NOTICE "executable not page aligned\n");
  786.         
  787.         fd = open_inode(bprm->inode, O_RDONLY);
  788.         
  789.         if (fd < 0)
  790.             return fd;
  791.         file = current->files->fd[fd];
  792.         if (!file->f_op || !file->f_op->mmap) {
  793.             sys_close(fd);
  794.             do_mmap(NULL, 0, ex.a_text+ex.a_data,
  795.                 PROT_READ|PROT_WRITE|PROT_EXEC,
  796.                 MAP_FIXED|MAP_PRIVATE, 0);
  797.             read_exec(bprm->inode, fd_offset,
  798.                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data);
  799.             goto beyond_if;
  800.         }
  801.  
  802.         error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
  803.             PROT_READ | PROT_EXEC,
  804.             MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  805.             fd_offset);
  806.  
  807.         if (error != N_TXTADDR(ex)) {
  808.             sys_close(fd);
  809.             send_sig(SIGKILL, current, 0);
  810.             return error;
  811.         }
  812.         
  813.          error = do_mmap(file, N_TXTADDR(ex) + ex.a_text, ex.a_data,
  814.                 PROT_READ | PROT_WRITE | PROT_EXEC,
  815.                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  816.                 fd_offset + ex.a_text);
  817.         sys_close(fd);
  818.         if (error != N_TXTADDR(ex) + ex.a_text) {
  819.             send_sig(SIGKILL, current, 0);
  820.             return error;
  821.         }
  822.     }
  823. beyond_if:
  824.     if (current->exec_domain && current->exec_domain->use_count)
  825.         (*current->exec_domain->use_count)--;
  826.     if (current->binfmt && current->binfmt->use_count)
  827.         (*current->binfmt->use_count)--;
  828.     current->exec_domain = lookup_exec_domain(current->personality);
  829.     current->binfmt = &aout_format;
  830.     if (current->exec_domain && current->exec_domain->use_count)
  831.         (*current->exec_domain->use_count)++;
  832.     if (current->binfmt && current->binfmt->use_count)
  833.         (*current->binfmt->use_count)++;
  834.  
  835.     set_brk(current->mm->start_brk, current->mm->brk);
  836.     
  837.     p += setup_arg_pages(ex.a_text,bprm->page);
  838.     p -= MAX_ARG_PAGES*PAGE_SIZE;
  839.     p = (unsigned long)create_tables((char *)p,
  840.                     bprm->argc, bprm->envc,
  841.                     current->personality != PER_LINUX);
  842.     current->mm->start_stack = p;
  843.     start_thread(regs, ex.a_entry, p);
  844.     if (current->flags & PF_PTRACED)
  845.         send_sig(SIGTRAP, current, 0);
  846.     return 0;
  847. }
  848.  
  849.  
  850. static int load_aout_library(int fd)
  851. {
  852.         struct file * file;
  853.     struct exec ex;
  854.     struct  inode * inode;
  855.     unsigned int len;
  856.     unsigned int bss;
  857.     unsigned int start_addr;
  858.     int error;
  859.     
  860.     file = current->files->fd[fd];
  861.     inode = file->f_inode;
  862.     
  863.     set_fs(KERNEL_DS);
  864.     if (file->f_op->read(inode, file, (char *) &ex, sizeof(ex)) != sizeof(ex)) {
  865.         return -EACCES;
  866.     }
  867.     set_fs(USER_DS);
  868.     
  869.     /* We come in here for the regular a.out style of shared libraries */
  870.     if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || ex.a_trsize ||
  871.         ex.a_drsize || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  872.         inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
  873.         return -ENOEXEC;
  874.     }
  875.     if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
  876.         (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
  877.         printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
  878.         return -ENOEXEC;
  879.     }
  880.     
  881.     if (N_FLAGS(ex)) return -ENOEXEC;
  882.  
  883.     /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
  884.        this off to get the starting address for the page */
  885.  
  886.     start_addr =  ex.a_entry & 0xfffff000;
  887.  
  888.     /* Now use mmap to map the library into memory. */
  889.     error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  890.             PROT_READ | PROT_WRITE | PROT_EXEC,
  891.             MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  892.             N_TXTOFF(ex));
  893.     if (error != start_addr)
  894.         return error;
  895.     len = PAGE_ALIGN(ex.a_text + ex.a_data);
  896.     bss = ex.a_text + ex.a_data + ex.a_bss;
  897.     if (bss > len)
  898.         do_mmap(NULL, start_addr + len, bss-len,
  899.             PROT_READ|PROT_WRITE|PROT_EXEC,
  900.             MAP_PRIVATE|MAP_FIXED, 0);
  901.     return 0;
  902. }
  903.