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 #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,&current->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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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,
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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,
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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,&current->close_on_exec))
 527                         sys_close(i);
 528         FD_ZERO(&current->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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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                         return 0;
 696                 }
 697                 fmt++;
 698         } while (retval == -ENOEXEC);
 699 exec_error2:
 700         iput(bprm.inode);
 701 exec_error1:
 702         for (i=0 ; i<MAX_ARG_PAGES ; i++)
 703                 free_page(bprm.page[i]);
 704         return(retval);
 705 }
 706 
 707 /*
 708  * sys_execve() executes a new program.
 709  */
 710 asmlinkage int sys_execve(struct pt_regs regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 711 {
 712         int error;
 713         char * filename;
 714 
 715         error = getname((char *) regs.ebx, &filename);
 716         if (error)
 717                 return error;
 718         error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
 719         putname(filename);
 720         return error;
 721 }
 722 
 723 /*
 724  * These are  the prototypes for the  functions in the  dispatch table, as
 725  * well as the  dispatch  table itself.
 726  */
 727 
 728 extern int load_aout_binary(struct linux_binprm *,
 729                             struct pt_regs * regs);
 730 extern int load_aout_library(int fd);
 731 
 732 #ifdef CONFIG_BINFMT_ELF
 733 extern int load_elf_binary(struct linux_binprm *,
 734                             struct pt_regs * regs);
 735 extern int load_elf_library(int fd);
 736 #endif
 737 
 738 #ifdef CONFIG_BINFMT_COFF
 739 extern int load_coff_binary(struct linux_binprm *,
 740                             struct pt_regs * regs);
 741 extern int load_coff_library(int fd);
 742 #endif
 743 
 744 /* Here are the actual binaries that will be accepted  */
 745 struct linux_binfmt formats[] = {
 746         {load_aout_binary, load_aout_library},
 747 #ifdef CONFIG_BINFMT_ELF
 748         {load_elf_binary, load_elf_library},
 749 #endif
 750 #ifdef CONFIG_BINFMT_COFF
 751         {load_coff_binary, load_coff_library},
 752 #endif
 753         {NULL, NULL}
 754 };
 755 
 756 /*
 757  * These are the functions used to load a.out style executables and shared
 758  * libraries.  There is no binary dependent code anywhere else.
 759  */
 760 
 761 int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 762 {
 763         struct exec ex;
 764         struct file * file;
 765         int fd, error;
 766         unsigned long p = bprm->p;
 767 
 768         ex = *((struct exec *) bprm->buf);              /* exec-header */
 769         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
 770              N_MAGIC(ex) != QMAGIC) ||
 771             ex.a_trsize || ex.a_drsize ||
 772             bprm->inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
 773                 return -ENOEXEC;
 774         }
 775 
 776         if (N_MAGIC(ex) == ZMAGIC &&
 777             (N_TXTOFF(ex) < bprm->inode->i_sb->s_blocksize)) {
 778                 printk("N_TXTOFF < BLOCK_SIZE. Please convert binary.");
 779                 return -ENOEXEC;
 780         }
 781 
 782         if (N_TXTOFF(ex) != BLOCK_SIZE && N_MAGIC(ex) == ZMAGIC) {
 783                 printk("N_TXTOFF != BLOCK_SIZE. See a.out.h.");
 784                 return -ENOEXEC;
 785         }
 786         
 787         /* OK, This is the point of no return */
 788         flush_old_exec(bprm);
 789 
 790         current->end_code = N_TXTADDR(ex) + ex.a_text;
 791         current->end_data = ex.a_data + current->end_code;
 792         current->start_brk = current->brk = current->end_data;
 793         current->start_code += N_TXTADDR(ex);
 794         current->rss = 0;
 795         current->suid = current->euid = bprm->e_uid;
 796         current->mmap = NULL;
 797         current->executable = NULL;  /* for OMAGIC files */
 798         current->sgid = current->egid = bprm->e_gid;
 799         if (N_MAGIC(ex) == OMAGIC) {
 800                 do_mmap(NULL, 0, ex.a_text+ex.a_data,
 801                         PROT_READ|PROT_WRITE|PROT_EXEC,
 802                         MAP_FIXED|MAP_PRIVATE, 0);
 803                 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data);
 804         } else {
 805                 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
 806                         printk("%s: executable not page aligned\n", current->comm);
 807                 
 808                 fd = open_inode(bprm->inode, O_RDONLY);
 809                 
 810                 if (fd < 0)
 811                         return fd;
 812                 file = current->filp[fd];
 813                 if (!file->f_op || !file->f_op->mmap) {
 814                         sys_close(fd);
 815                         do_mmap(NULL, 0, ex.a_text+ex.a_data,
 816                                 PROT_READ|PROT_WRITE|PROT_EXEC,
 817                                 MAP_FIXED|MAP_PRIVATE, 0);
 818                         read_exec(bprm->inode, N_TXTOFF(ex),
 819                                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data);
 820                         goto beyond_if;
 821                 }
 822                 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
 823                                 PROT_READ | PROT_EXEC,
 824                                 MAP_FIXED | MAP_SHARED, N_TXTOFF(ex));
 825 
 826                 if (error != N_TXTADDR(ex)) {
 827                         sys_close(fd);
 828                         send_sig(SIGSEGV, current, 0);
 829                         return 0;
 830                 };
 831                 
 832                 error = do_mmap(file, N_TXTADDR(ex) + ex.a_text, ex.a_data,
 833                                 PROT_READ | PROT_WRITE | PROT_EXEC,
 834                                 MAP_FIXED | MAP_PRIVATE, N_TXTOFF(ex) + ex.a_text);
 835                 sys_close(fd);
 836                 if (error != N_TXTADDR(ex) + ex.a_text) {
 837                         send_sig(SIGSEGV, current, 0);
 838                         return 0;
 839                 };
 840                 current->executable = bprm->inode;
 841                 bprm->inode->i_count++;
 842         }
 843 beyond_if:
 844         sys_brk(current->brk+ex.a_bss);
 845         
 846         p += change_ldt(ex.a_text,bprm->page);
 847         p -= MAX_ARG_PAGES*PAGE_SIZE;
 848         p = (unsigned long) create_tables((char *)p,bprm->argc,bprm->envc,0);
 849         current->start_stack = p;
 850         regs->eip = ex.a_entry;         /* eip, magic happens :-) */
 851         regs->esp = p;                  /* stack pointer */
 852         if (current->flags & PF_PTRACED)
 853                 send_sig(SIGTRAP, current, 0);
 854         return 0;
 855 }
 856 
 857 
 858 int load_aout_library(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860         struct file * file;
 861         struct exec ex;
 862         struct  inode * inode;
 863         unsigned int len;
 864         unsigned int bss;
 865         unsigned int start_addr;
 866         int error;
 867         
 868         file = current->filp[fd];
 869         inode = file->f_inode;
 870         
 871         set_fs(KERNEL_DS);
 872         if (file->f_op->read(inode, file, (char *) &ex, sizeof(ex)) != sizeof(ex)) {
 873                 return -EACCES;
 874         }
 875         set_fs(USER_DS);
 876         
 877         /* We come in here for the regular a.out style of shared libraries */
 878         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || ex.a_trsize ||
 879             ex.a_drsize || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 880             inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
 881                 return -ENOEXEC;
 882         }
 883         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
 884             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
 885                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
 886                 return -ENOEXEC;
 887         }
 888         
 889         if (N_FLAGS(ex)) return -ENOEXEC;
 890 
 891         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
 892            this off to get the starting address for the page */
 893 
 894         start_addr =  ex.a_entry & 0xfffff000;
 895 
 896         /* Now use mmap to map the library into memory. */
 897         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
 898                         PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED | MAP_PRIVATE,
 899                         N_TXTOFF(ex));
 900         if (error != start_addr)
 901                 return error;
 902         len = PAGE_ALIGN(ex.a_text + ex.a_data);
 903         bss = ex.a_text + ex.a_data + ex.a_bss;
 904         if (bss > len)
 905                 do_mmap(NULL, start_addr + len, bss-len,
 906                         PROT_READ|PROT_WRITE|PROT_EXEC,
 907                         MAP_PRIVATE|MAP_FIXED, 0);
 908         return 0;
 909 }

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