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