root/fs/exec.c

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

DEFINITIONS

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

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