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 << PAGE_SHIFT;
 239                 dump_size = dump.u_dsize << PAGE_SHIFT;
 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 << PAGE_SHIFT;
 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 #ifdef __alpha__
 321 /* whee.. test-programs are so much fun. */
 322         put_user(0, --sp);
 323         put_user(0, --sp);
 324         if (bprm->loader) {
 325                 put_user(0, --sp);
 326                 put_user(0x3eb, --sp);
 327                 put_user(bprm->loader, --sp);
 328                 put_user(0x3ea, --sp);
 329         }
 330         put_user(bprm->exec, --sp);
 331         put_user(0x3e9, --sp);
 332 #endif
 333         sp -= envc+1;
 334         envp = sp;
 335         sp -= argc+1;
 336         argv = sp;
 337 #ifdef __i386__
 338         if (!ibcs) {
 339                 put_user(envp,--sp);
 340                 put_user(argv,--sp);
 341         }
 342 #endif
 343         put_user(argc,--sp);
 344         current->mm->arg_start = (unsigned long) p;
 345         while (argc-->0) {
 346                 put_user(p,argv++);
 347                 while (get_user(p++)) /* nothing */ ;
 348         }
 349         put_user(NULL,argv);
 350         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
 351         while (envc-->0) {
 352                 put_user(p,envp++);
 353                 while (get_user(p++)) /* nothing */ ;
 354         }
 355         put_user(NULL,envp);
 356         current->mm->env_end = (unsigned long) p;
 357         return sp;
 358 }
 359 
 360 /*
 361  * count() counts the number of arguments/envelopes
 362  *
 363  * We also do some limited EFAULT checking: this isn't complete, but
 364  * it does cover most cases. I'll have to do this correctly some day..
 365  */
 366 static int count(char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 367 {
 368         int error, i = 0;
 369         char ** tmp, *p;
 370 
 371         if ((tmp = argv) != NULL) {
 372                 error = verify_area(VERIFY_READ, tmp, sizeof(char *));
 373                 if (error)
 374                         return error;
 375                 while ((p = get_user(tmp++)) != NULL) {
 376                         i++;
 377                         error = verify_area(VERIFY_READ, p, 1);
 378                         if (error)
 379                                 return error;
 380                 }
 381         }
 382         return i;
 383 }
 384 
 385 /*
 386  * 'copy_string()' copies argument/envelope strings from user
 387  * memory to free pages in kernel mem. These are in a format ready
 388  * to be put directly into the top of new user memory.
 389  *
 390  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
 391  * whether the string and the string array are from user or kernel segments:
 392  * 
 393  * from_kmem     argv *        argv **
 394  *    0          user space    user space
 395  *    1          kernel space  user space
 396  *    2          kernel space  kernel space
 397  * 
 398  * We do this by playing games with the fs segment register.  Since it
 399  * is expensive to load a segment register, we try to avoid calling
 400  * set_fs() unless we absolutely have to.
 401  */
 402 unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
     /* [previous][next][first][last][top][bottom][index][help] */
 403                 unsigned long p, int from_kmem)
 404 {
 405         char *tmp, *pag = NULL;
 406         int len, offset = 0;
 407         unsigned long old_fs, new_fs;
 408 
 409         if (!p)
 410                 return 0;       /* bullet-proofing */
 411         new_fs = get_ds();
 412         old_fs = get_fs();
 413         if (from_kmem==2)
 414                 set_fs(new_fs);
 415         while (argc-- > 0) {
 416                 if (from_kmem == 1)
 417                         set_fs(new_fs);
 418                 if (!(tmp = get_user(argv+argc)))
 419                         panic("VFS: argc is wrong");
 420                 if (from_kmem == 1)
 421                         set_fs(old_fs);
 422                 len=0;          /* remember zero-padding */
 423                 do {
 424                         len++;
 425                 } while (get_user(tmp++));
 426                 if (p < len) {  /* this shouldn't happen - 128kB */
 427                         set_fs(old_fs);
 428                         return 0;
 429                 }
 430                 while (len) {
 431                         --p; --tmp; --len;
 432                         if (--offset < 0) {
 433                                 offset = p % PAGE_SIZE;
 434                                 if (from_kmem==2)
 435                                         set_fs(old_fs);
 436                                 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
 437                                     !(pag = (char *) page[p/PAGE_SIZE] =
 438                                       (unsigned long *) get_free_page(GFP_USER))) 
 439                                         return 0;
 440                                 if (from_kmem==2)
 441                                         set_fs(new_fs);
 442 
 443                         }
 444                         *(pag + offset) = get_user(tmp);
 445                 }
 446         }
 447         if (from_kmem==2)
 448                 set_fs(old_fs);
 449         return p;
 450 }
 451 
 452 unsigned long setup_arg_pages(unsigned long text_size,unsigned long * page)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454         unsigned long code_limit,data_limit,code_base,data_base;
 455         int i;
 456 
 457         code_limit = STACK_TOP;
 458         data_limit = STACK_TOP;
 459         code_base = data_base = 0;
 460         current->mm->start_code = code_base;
 461         data_base += data_limit;
 462         for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
 463                 data_base -= PAGE_SIZE;
 464                 if (page[i]) {
 465                         current->mm->rss++;
 466                         put_dirty_page(current,page[i],data_base);
 467                 }
 468         }
 469         return data_limit;
 470 }
 471 
 472 /*
 473  * Read in the complete executable. This is used for "-N" files
 474  * that aren't on a block boundary, and for files on filesystems
 475  * without bmap support.
 476  */
 477 int read_exec(struct inode *inode, unsigned long offset,
     /* [previous][next][first][last][top][bottom][index][help] */
 478         char * addr, unsigned long count, int to_kmem)
 479 {
 480         struct file file;
 481         int result = -ENOEXEC;
 482 
 483         if (!inode->i_op || !inode->i_op->default_file_ops)
 484                 goto end_readexec;
 485         file.f_mode = 1;
 486         file.f_flags = 0;
 487         file.f_count = 1;
 488         file.f_inode = inode;
 489         file.f_pos = 0;
 490         file.f_reada = 0;
 491         file.f_op = inode->i_op->default_file_ops;
 492         if (file.f_op->open)
 493                 if (file.f_op->open(inode,&file))
 494                         goto end_readexec;
 495         if (!file.f_op || !file.f_op->read)
 496                 goto close_readexec;
 497         if (file.f_op->lseek) {
 498                 if (file.f_op->lseek(inode,&file,offset,0) != offset)
 499                         goto close_readexec;
 500         } else
 501                 file.f_pos = offset;
 502         if (to_kmem) {
 503                 unsigned long old_fs = get_fs();
 504                 set_fs(get_ds());
 505                 result = file.f_op->read(inode, &file, addr, count);
 506                 set_fs(old_fs);
 507         } else {
 508                 result = verify_area(VERIFY_WRITE, addr, count);
 509                 if (result)
 510                         goto close_readexec;
 511                 result = file.f_op->read(inode, &file, addr, count);
 512         }
 513 close_readexec:
 514         if (file.f_op->release)
 515                 file.f_op->release(inode,&file);
 516 end_readexec:
 517         return result;
 518 }
 519 
 520 
 521 /*
 522  * This function flushes out all traces of the currently running executable so
 523  * that a new one can be started
 524  */
 525 
 526 void flush_old_exec(struct linux_binprm * bprm)
     /* [previous][next][first][last][top][bottom][index][help] */
 527 {
 528         int i;
 529         int ch;
 530         char * name;
 531 
 532         if (current->euid == current->uid && current->egid == current->gid)
 533                 current->dumpable = 1;
 534         name = bprm->filename;
 535         for (i=0; (ch = *(name++)) != '\0';) {
 536                 if (ch == '/')
 537                         i = 0;
 538                 else
 539                         if (i < 15)
 540                                 current->comm[i++] = ch;
 541         }
 542         current->comm[i] = '\0';
 543 
 544         /* Release all of the old mmap stuff. */
 545         exit_mmap(current);
 546 
 547         flush_thread();
 548 
 549         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
 550             permission(bprm->inode,MAY_READ))
 551                 current->dumpable = 0;
 552         current->signal = 0;
 553         for (i=0 ; i<32 ; i++) {
 554                 current->sigaction[i].sa_mask = 0;
 555                 current->sigaction[i].sa_flags = 0;
 556                 if (current->sigaction[i].sa_handler != SIG_IGN)
 557                         current->sigaction[i].sa_handler = NULL;
 558         }
 559         for (i=0 ; i<NR_OPEN ; i++)
 560                 if (FD_ISSET(i,&current->files->close_on_exec))
 561                         sys_close(i);
 562         FD_ZERO(&current->files->close_on_exec);
 563         clear_page_tables(current);
 564         if (last_task_used_math == current)
 565                 last_task_used_math = NULL;
 566         current->used_math = 0;
 567 }
 568 
 569 /*
 570  * sys_execve() executes a new program.
 571  */
 572 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 573 {
 574         struct linux_binprm bprm;
 575         struct linux_binfmt * fmt;
 576         int i;
 577         int retval;
 578         int sh_bang = 0;
 579         int loader = 0;
 580 
 581         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
 582         for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 583                 bprm.page[i] = 0;
 584         retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
 585         if (retval)
 586                 return retval;
 587         bprm.filename = filename;
 588         bprm.loader = 0;
 589         bprm.exec = 0;
 590         if ((bprm.argc = count(argv)) < 0)
 591                 return bprm.argc;
 592         if ((bprm.envc = count(envp)) < 0)
 593                 return bprm.envc;
 594         
 595 restart_interp:
 596         if (!S_ISREG(bprm.inode->i_mode)) {     /* must be regular file */
 597                 retval = -EACCES;
 598                 goto exec_error2;
 599         }
 600         if (IS_NOEXEC(bprm.inode)) {            /* FS mustn't be mounted noexec */
 601                 retval = -EPERM;
 602                 goto exec_error2;
 603         }
 604         if (!bprm.inode->i_sb) {
 605                 retval = -EACCES;
 606                 goto exec_error2;
 607         }
 608         i = bprm.inode->i_mode;
 609         if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
 610             euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
 611                 retval = -EPERM;
 612                 goto exec_error2;
 613         }
 614         /* make sure we don't let suid, sgid files be ptraced. */
 615         if (current->flags & PF_PTRACED) {
 616                 bprm.e_uid = current->euid;
 617                 bprm.e_gid = current->egid;
 618         } else {
 619                 bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
 620                 bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
 621         }
 622         if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
 623                 goto exec_error2;
 624         if (!(bprm.inode->i_mode & 0111) && fsuser()) {
 625                 retval = -EACCES;
 626                 goto exec_error2;
 627         }
 628         /* better not execute files which are being written to */
 629         if (bprm.inode->i_wcount > 0) {
 630                 retval = -ETXTBSY;
 631                 goto exec_error2;
 632         }
 633         memset(bprm.buf,0,sizeof(bprm.buf));
 634         retval = read_exec(bprm.inode,0,bprm.buf,128,1);
 635         if (retval < 0)
 636                 goto exec_error2;
 637         if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
 638                 /*
 639                  * This section does the #! interpretation.
 640                  * Sorta complicated, but hopefully it will work.  -TYT
 641                  */
 642 
 643                 char *cp, *interp, *i_name, *i_arg;
 644 
 645                 iput(bprm.inode);
 646                 bprm.buf[127] = '\0';
 647                 if ((cp = strchr(bprm.buf, '\n')) == NULL)
 648                         cp = bprm.buf+127;
 649                 *cp = '\0';
 650                 while (cp > bprm.buf) {
 651                         cp--;
 652                         if ((*cp == ' ') || (*cp == '\t'))
 653                                 *cp = '\0';
 654                         else
 655                                 break;
 656                 }
 657                 for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
 658                 if (!cp || *cp == '\0') {
 659                         retval = -ENOEXEC; /* No interpreter name found */
 660                         goto exec_error1;
 661                 }
 662                 interp = i_name = cp;
 663                 i_arg = 0;
 664                 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
 665                         if (*cp == '/')
 666                                 i_name = cp+1;
 667                 }
 668                 while ((*cp == ' ') || (*cp == '\t'))
 669                         *cp++ = '\0';
 670                 if (*cp)
 671                         i_arg = cp;
 672                 /*
 673                  * OK, we've parsed out the interpreter name and
 674                  * (optional) argument.
 675                  */
 676                 if (sh_bang++ == 0) {
 677                         bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
 678                         bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
 679                 }
 680                 /*
 681                  * Splice in (1) the interpreter's name for argv[0]
 682                  *           (2) (optional) argument to interpreter
 683                  *           (3) filename of shell script
 684                  *
 685                  * This is done in reverse order, because of how the
 686                  * user environment and arguments are stored.
 687                  */
 688                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 689                 bprm.argc++;
 690                 if (i_arg) {
 691                         bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
 692                         bprm.argc++;
 693                 }
 694                 bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
 695                 bprm.argc++;
 696                 if (!bprm.p) {
 697                         retval = -E2BIG;
 698                         goto exec_error1;
 699                 }
 700                 /*
 701                  * OK, now restart the process with the interpreter's inode.
 702                  * Note that we use open_namei() as the name is now in kernel
 703                  * space, and we don't need to copy it.
 704                  */
 705                 retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
 706                 if (retval)
 707                         goto exec_error1;
 708                 goto restart_interp;
 709         }
 710 #ifdef __alpha__
 711 /* handle /sbin/loader.. */
 712         if (!loader && (((struct exec *) bprm.buf)->fh.f_flags & 0x3000)) {
 713                 char * dynloader[] = { "/sbin/loader" };
 714                 iput(bprm.inode);
 715                 loader = 1;
 716                 bprm.p = copy_strings(1, dynloader, bprm.page, bprm.p, 2);
 717                 bprm.loader = bprm.p;
 718                 retval = open_namei(dynloader[0], 0, 0, &bprm.inode, NULL);
 719                 if (retval)
 720                         goto exec_error1;
 721                 goto restart_interp;
 722         }
 723 #endif
 724         if (!sh_bang) {
 725                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 726                 bprm.exec = bprm.p;
 727                 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
 728                 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
 729                 if (!bprm.p) {
 730                         retval = -E2BIG;
 731                         goto exec_error2;
 732                 }
 733         }
 734 
 735         bprm.sh_bang = sh_bang;
 736         for (fmt = formats ; fmt ; fmt = fmt->next) {
 737                 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
 738                 if (!fn)
 739                         break;
 740                 retval = fn(&bprm, regs);
 741                 if (retval >= 0) {
 742                         iput(bprm.inode);
 743                         current->did_exec = 1;
 744                         return retval;
 745                 }
 746                 if (retval != -ENOEXEC)
 747                         break;
 748         }
 749 exec_error2:
 750         iput(bprm.inode);
 751 exec_error1:
 752         for (i=0 ; i<MAX_ARG_PAGES ; i++)
 753                 free_page(bprm.page[i]);
 754         return(retval);
 755 }
 756 
 757 static void set_brk(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 758 {
 759         start = PAGE_ALIGN(start);
 760         end = PAGE_ALIGN(end);
 761         if (end <= start)
 762                 return;
 763         do_mmap(NULL, start, end - start,
 764                 PROT_READ | PROT_WRITE | PROT_EXEC,
 765                 MAP_FIXED | MAP_PRIVATE, 0);
 766 }
 767 
 768 /*
 769  * These are the functions used to load a.out style executables and shared
 770  * libraries.  There is no binary dependent code anywhere else.
 771  */
 772 
 773 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 774 {
 775         struct exec ex;
 776         struct file * file;
 777         int fd;
 778         unsigned long error;
 779         unsigned long p = bprm->p;
 780         unsigned long fd_offset;
 781 
 782         ex = *((struct exec *) bprm->buf);              /* exec-header */
 783         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
 784              N_MAGIC(ex) != QMAGIC) ||
 785             N_TRSIZE(ex) || N_DRSIZE(ex) ||
 786             bprm->inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 787                 return -ENOEXEC;
 788         }
 789 
 790         current->personality = PER_LINUX;
 791         fd_offset = N_TXTOFF(ex);
 792 
 793 #ifdef __i386__
 794         if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
 795                 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
 796                 return -ENOEXEC;
 797         }
 798 
 799         if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
 800             (fd_offset < bprm->inode->i_sb->s_blocksize)) {
 801                 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
 802                 return -ENOEXEC;
 803         }
 804 #endif
 805 
 806         /* OK, This is the point of no return */
 807         flush_old_exec(bprm);
 808 
 809         current->mm->end_code = ex.a_text +
 810                 (current->mm->start_code = N_TXTADDR(ex));
 811         current->mm->end_data = ex.a_data +
 812                 (current->mm->start_data = N_DATADDR(ex));
 813         current->mm->brk = ex.a_bss +
 814                 (current->mm->start_brk = N_BSSADDR(ex));
 815 
 816         current->mm->rss = 0;
 817         current->mm->mmap = NULL;
 818         current->suid = current->euid = current->fsuid = bprm->e_uid;
 819         current->sgid = current->egid = current->fsgid = bprm->e_gid;
 820         if (N_MAGIC(ex) == OMAGIC) {
 821                 do_mmap(NULL, 0, ex.a_text+ex.a_data,
 822                         PROT_READ|PROT_WRITE|PROT_EXEC,
 823                         MAP_FIXED|MAP_PRIVATE, 0);
 824                 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data, 0);
 825         } else {
 826                 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
 827                         printk(KERN_NOTICE "executable not page aligned\n");
 828                 
 829                 fd = open_inode(bprm->inode, O_RDONLY);
 830                 
 831                 if (fd < 0)
 832                         return fd;
 833                 file = current->files->fd[fd];
 834                 if (!file->f_op || !file->f_op->mmap) {
 835                         sys_close(fd);
 836                         do_mmap(NULL, 0, ex.a_text+ex.a_data,
 837                                 PROT_READ|PROT_WRITE|PROT_EXEC,
 838                                 MAP_FIXED|MAP_PRIVATE, 0);
 839                         read_exec(bprm->inode, fd_offset,
 840                                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data, 0);
 841                         goto beyond_if;
 842                 }
 843 
 844                 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
 845                         PROT_READ | PROT_EXEC,
 846                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 847                         fd_offset);
 848 
 849                 if (error != N_TXTADDR(ex)) {
 850                         sys_close(fd);
 851                         send_sig(SIGKILL, current, 0);
 852                         return error;
 853                 }
 854                 
 855                 error = do_mmap(file, N_DATADDR(ex), ex.a_data,
 856                                 PROT_READ | PROT_WRITE | PROT_EXEC,
 857                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 858                                 fd_offset + ex.a_text);
 859                 sys_close(fd);
 860                 if (error != N_DATADDR(ex)) {
 861                         send_sig(SIGKILL, current, 0);
 862                         return error;
 863                 }
 864         }
 865 beyond_if:
 866         if (current->exec_domain && current->exec_domain->use_count)
 867                 (*current->exec_domain->use_count)--;
 868         if (current->binfmt && current->binfmt->use_count)
 869                 (*current->binfmt->use_count)--;
 870         current->exec_domain = lookup_exec_domain(current->personality);
 871         current->binfmt = &aout_format;
 872         if (current->exec_domain && current->exec_domain->use_count)
 873                 (*current->exec_domain->use_count)++;
 874         if (current->binfmt && current->binfmt->use_count)
 875                 (*current->binfmt->use_count)++;
 876 
 877         set_brk(current->mm->start_brk, current->mm->brk);
 878 
 879         fd_offset = setup_arg_pages(ex.a_text,bprm->page) - MAX_ARG_PAGES*PAGE_SIZE;
 880         p += fd_offset;
 881         if (bprm->loader)
 882                 bprm->loader += fd_offset;
 883         bprm->exec += fd_offset;
 884         
 885         p = (unsigned long)create_tables((char *)p, bprm,
 886                                         current->personality != PER_LINUX);
 887         current->mm->start_stack = p;
 888 #ifdef __alpha__
 889         regs->gp = ex.a_gpvalue;
 890 #endif
 891         start_thread(regs, ex.a_entry, p);
 892         if (current->flags & PF_PTRACED)
 893                 send_sig(SIGTRAP, current, 0);
 894         return 0;
 895 }
 896 
 897 
 898 static int load_aout_library(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 899 {
 900         struct file * file;
 901         struct exec ex;
 902         struct  inode * inode;
 903         unsigned int len;
 904         unsigned int bss;
 905         unsigned int start_addr;
 906         unsigned long error;
 907         
 908         file = current->files->fd[fd];
 909         inode = file->f_inode;
 910         
 911         set_fs(KERNEL_DS);
 912         if (file->f_op->read(inode, file, (char *) &ex, sizeof(ex)) != sizeof(ex)) {
 913                 return -EACCES;
 914         }
 915         set_fs(USER_DS);
 916         
 917         /* We come in here for the regular a.out style of shared libraries */
 918         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
 919             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 920             inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 921                 return -ENOEXEC;
 922         }
 923         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
 924             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
 925                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
 926                 return -ENOEXEC;
 927         }
 928         
 929         if (N_FLAGS(ex)) return -ENOEXEC;
 930 
 931         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
 932            this off to get the starting address for the page */
 933 
 934         start_addr =  ex.a_entry & 0xfffff000;
 935 
 936         /* Now use mmap to map the library into memory. */
 937         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
 938                         PROT_READ | PROT_WRITE | PROT_EXEC,
 939                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
 940                         N_TXTOFF(ex));
 941         if (error != start_addr)
 942                 return error;
 943         len = PAGE_ALIGN(ex.a_text + ex.a_data);
 944         bss = ex.a_text + ex.a_data + ex.a_bss;
 945         if (bss > len)
 946                 do_mmap(NULL, start_addr + len, bss-len,
 947                         PROT_READ|PROT_WRITE|PROT_EXEC,
 948                         MAP_PRIVATE|MAP_FIXED, 0);
 949         return 0;
 950 }

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