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

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