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

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