root/fs/exec.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. open_inode
  2. core_dump
  3. sys_uselib
  4. create_tables
  5. count
  6. copy_strings
  7. change_ldt
  8. read_exec
  9. flush_old_exec
  10. do_execve
  11. sys_execve
  12. load_aout_binary
  13. load_aout_library

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

/* [previous][next][first][last][top][bottom][index][help] */