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

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