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_COPY;
 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 
 509         current->dumpable = 1;
 510         name = bprm->filename;
 511         for (i=0; (ch = *(name++)) != '\0';) {
 512                 if (ch == '/')
 513                         i = 0;
 514                 else
 515                         if (i < 15)
 516                                 current->comm[i++] = ch;
 517         }
 518         current->comm[i] = '\0';
 519 
 520         /* Release all of the old mmap stuff. */
 521         exit_mmap(current);
 522 
 523         flush_thread();
 524 
 525         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
 526             permission(bprm->inode,MAY_READ))
 527                 current->dumpable = 0;
 528         current->signal = 0;
 529         for (i=0 ; i<32 ; i++) {
 530                 current->sigaction[i].sa_mask = 0;
 531                 current->sigaction[i].sa_flags = 0;
 532                 if (current->sigaction[i].sa_handler != SIG_IGN)
 533                         current->sigaction[i].sa_handler = NULL;
 534         }
 535         for (i=0 ; i<NR_OPEN ; i++)
 536                 if (FD_ISSET(i,&current->files->close_on_exec))
 537                         sys_close(i);
 538         FD_ZERO(&current->files->close_on_exec);
 539         clear_page_tables(current);
 540         if (last_task_used_math == current)
 541                 last_task_used_math = NULL;
 542         current->used_math = 0;
 543 }
 544 
 545 /*
 546  * sys_execve() executes a new program.
 547  */
 548 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 549 {
 550         struct linux_binprm bprm;
 551         struct linux_binfmt * fmt;
 552         unsigned long old_fs;
 553         int i;
 554         int retval;
 555         int sh_bang = 0;
 556 
 557         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-4;
 558         for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 559                 bprm.page[i] = 0;
 560         retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
 561         if (retval)
 562                 return retval;
 563         bprm.filename = filename;
 564         if ((bprm.argc = count(argv)) < 0)
 565                 return bprm.argc;
 566         if ((bprm.envc = count(envp)) < 0)
 567                 return bprm.envc;
 568         
 569 restart_interp:
 570         if (!S_ISREG(bprm.inode->i_mode)) {     /* must be regular file */
 571                 retval = -EACCES;
 572                 goto exec_error2;
 573         }
 574         if (IS_NOEXEC(bprm.inode)) {            /* FS mustn't be mounted noexec */
 575                 retval = -EPERM;
 576                 goto exec_error2;
 577         }
 578         if (!bprm.inode->i_sb) {
 579                 retval = -EACCES;
 580                 goto exec_error2;
 581         }
 582         i = bprm.inode->i_mode;
 583         if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
 584             euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
 585                 retval = -EPERM;
 586                 goto exec_error2;
 587         }
 588         /* make sure we don't let suid, sgid files be ptraced. */
 589         if (current->flags & PF_PTRACED) {
 590                 bprm.e_uid = current->euid;
 591                 bprm.e_gid = current->egid;
 592         } else {
 593                 bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
 594                 bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
 595         }
 596         if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
 597                 goto exec_error2;
 598         if (!(bprm.inode->i_mode & 0111) && fsuser()) {
 599                 retval = -EACCES;
 600                 goto exec_error2;
 601         }
 602         /* better not execute files which are being written to */
 603         if (bprm.inode->i_wcount > 0) {
 604                 retval = -ETXTBSY;
 605                 goto exec_error2;
 606         }
 607         memset(bprm.buf,0,sizeof(bprm.buf));
 608         old_fs = get_fs();
 609         set_fs(get_ds());
 610         retval = read_exec(bprm.inode,0,bprm.buf,128);
 611         set_fs(old_fs);
 612         if (retval < 0)
 613                 goto exec_error2;
 614         if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
 615                 /*
 616                  * This section does the #! interpretation.
 617                  * Sorta complicated, but hopefully it will work.  -TYT
 618                  */
 619 
 620                 char *cp, *interp, *i_name, *i_arg;
 621 
 622                 iput(bprm.inode);
 623                 bprm.buf[127] = '\0';
 624                 if ((cp = strchr(bprm.buf, '\n')) == NULL)
 625                         cp = bprm.buf+127;
 626                 *cp = '\0';
 627                 while (cp > bprm.buf) {
 628                         cp--;
 629                         if ((*cp == ' ') || (*cp == '\t'))
 630                                 *cp = '\0';
 631                         else
 632                                 break;
 633                 }
 634                 for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
 635                 if (!cp || *cp == '\0') {
 636                         retval = -ENOEXEC; /* No interpreter name found */
 637                         goto exec_error1;
 638                 }
 639                 interp = i_name = cp;
 640                 i_arg = 0;
 641                 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
 642                         if (*cp == '/')
 643                                 i_name = cp+1;
 644                 }
 645                 while ((*cp == ' ') || (*cp == '\t'))
 646                         *cp++ = '\0';
 647                 if (*cp)
 648                         i_arg = cp;
 649                 /*
 650                  * OK, we've parsed out the interpreter name and
 651                  * (optional) argument.
 652                  */
 653                 if (sh_bang++ == 0) {
 654                         bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
 655                         bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
 656                 }
 657                 /*
 658                  * Splice in (1) the interpreter's name for argv[0]
 659                  *           (2) (optional) argument to interpreter
 660                  *           (3) filename of shell script
 661                  *
 662                  * This is done in reverse order, because of how the
 663                  * user environment and arguments are stored.
 664                  */
 665                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 666                 bprm.argc++;
 667                 if (i_arg) {
 668                         bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
 669                         bprm.argc++;
 670                 }
 671                 bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
 672                 bprm.argc++;
 673                 if (!bprm.p) {
 674                         retval = -E2BIG;
 675                         goto exec_error1;
 676                 }
 677                 /*
 678                  * OK, now restart the process with the interpreter's inode.
 679                  * Note that we use open_namei() as the name is now in kernel
 680                  * space, and we don't need to copy it.
 681                  */
 682                 retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
 683                 if (retval)
 684                         goto exec_error1;
 685                 goto restart_interp;
 686         }
 687         if (!sh_bang) {
 688                 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
 689                 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
 690                 if (!bprm.p) {
 691                         retval = -E2BIG;
 692                         goto exec_error2;
 693                 }
 694         }
 695 
 696         bprm.sh_bang = sh_bang;
 697         for (fmt = formats ; fmt ; fmt = fmt->next) {
 698                 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
 699                 if (!fn)
 700                         break;
 701                 retval = fn(&bprm, regs);
 702                 if (retval >= 0) {
 703                         iput(bprm.inode);
 704                         current->did_exec = 1;
 705                         return retval;
 706                 }
 707                 if (retval != -ENOEXEC)
 708                         break;
 709         }
 710 exec_error2:
 711         iput(bprm.inode);
 712 exec_error1:
 713         for (i=0 ; i<MAX_ARG_PAGES ; i++)
 714                 free_page(bprm.page[i]);
 715         return(retval);
 716 }
 717 
 718 static void set_brk(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 719 {
 720         start = PAGE_ALIGN(start);
 721         end = PAGE_ALIGN(end);
 722         if (end <= start)
 723                 return;
 724         do_mmap(NULL, start, end - start,
 725                 PROT_READ | PROT_WRITE | PROT_EXEC,
 726                 MAP_FIXED | MAP_PRIVATE, 0);
 727 }
 728 
 729 /*
 730  * These are the functions used to load a.out style executables and shared
 731  * libraries.  There is no binary dependent code anywhere else.
 732  */
 733 
 734 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736         struct exec ex;
 737         struct file * file;
 738         int fd, error;
 739         unsigned long p = bprm->p;
 740         unsigned long fd_offset;
 741 
 742         ex = *((struct exec *) bprm->buf);              /* exec-header */
 743         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
 744              N_MAGIC(ex) != QMAGIC) ||
 745             ex.a_trsize || ex.a_drsize ||
 746             bprm->inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
 747                 return -ENOEXEC;
 748         }
 749 
 750         current->personality = PER_LINUX;
 751         fd_offset = N_TXTOFF(ex);
 752         if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
 753                 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
 754                 return -ENOEXEC;
 755         }
 756 
 757         if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
 758             (fd_offset < bprm->inode->i_sb->s_blocksize)) {
 759                 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
 760                 return -ENOEXEC;
 761         }
 762 
 763         /* OK, This is the point of no return */
 764         flush_old_exec(bprm);
 765 
 766         current->mm->brk = ex.a_bss +
 767                 (current->mm->start_brk =
 768                 (current->mm->end_data = ex.a_data +
 769                 (current->mm->end_code = ex.a_text +
 770                 (current->mm->start_code = N_TXTADDR(ex)))));
 771         current->mm->rss = 0;
 772         current->mm->mmap = NULL;
 773         current->suid = current->euid = current->fsuid = bprm->e_uid;
 774         current->sgid = current->egid = current->fsgid = bprm->e_gid;
 775         if (N_MAGIC(ex) == OMAGIC) {
 776                 do_mmap(NULL, 0, ex.a_text+ex.a_data,
 777                         PROT_READ|PROT_WRITE|PROT_EXEC,
 778                         MAP_FIXED|MAP_PRIVATE, 0);
 779                 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data);
 780         } else {
 781                 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
 782                         printk(KERN_NOTICE "executable not page aligned\n");
 783                 
 784                 fd = open_inode(bprm->inode, O_RDONLY);
 785                 
 786                 if (fd < 0)
 787                         return fd;
 788                 file = current->files->fd[fd];
 789                 if (!file->f_op || !file->f_op->mmap) {
 790                         sys_close(fd);
 791                         do_mmap(NULL, 0, ex.a_text+ex.a_data,
 792                                 PROT_READ|PROT_WRITE|PROT_EXEC,
 793                                 MAP_FIXED|MAP_PRIVATE, 0);
 794                         read_exec(bprm->inode, fd_offset,
 795                                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data);
 796                         goto beyond_if;
 797                 }
 798 
 799                 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
 800                         PROT_READ | PROT_EXEC,
 801                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 802                         fd_offset);
 803 
 804                 if (error != N_TXTADDR(ex)) {
 805                         sys_close(fd);
 806                         send_sig(SIGKILL, current, 0);
 807                         return error;
 808                 }
 809                 
 810                 error = do_mmap(file, N_TXTADDR(ex) + ex.a_text, ex.a_data,
 811                                 PROT_READ | PROT_WRITE | PROT_EXEC,
 812                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 813                                 fd_offset + ex.a_text);
 814                 sys_close(fd);
 815                 if (error != N_TXTADDR(ex) + ex.a_text) {
 816                         send_sig(SIGKILL, current, 0);
 817                         return error;
 818                 }
 819         }
 820 beyond_if:
 821         if (current->exec_domain && current->exec_domain->use_count)
 822                 (*current->exec_domain->use_count)--;
 823         if (current->binfmt && current->binfmt->use_count)
 824                 (*current->binfmt->use_count)--;
 825         current->exec_domain = lookup_exec_domain(current->personality);
 826         current->binfmt = &aout_format;
 827         if (current->exec_domain && current->exec_domain->use_count)
 828                 (*current->exec_domain->use_count)++;
 829         if (current->binfmt && current->binfmt->use_count)
 830                 (*current->binfmt->use_count)++;
 831 
 832         set_brk(current->mm->start_brk, current->mm->brk);
 833         
 834         p += setup_arg_pages(ex.a_text,bprm->page);
 835         p -= MAX_ARG_PAGES*PAGE_SIZE;
 836         p = (unsigned long)create_tables((char *)p,
 837                                         bprm->argc, bprm->envc,
 838                                         current->personality != PER_LINUX);
 839         current->mm->start_stack = p;
 840         start_thread(regs, ex.a_entry, p);
 841         if (current->flags & PF_PTRACED)
 842                 send_sig(SIGTRAP, current, 0);
 843         return 0;
 844 }
 845 
 846 
 847 static int load_aout_library(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 848 {
 849         struct file * file;
 850         struct exec ex;
 851         struct  inode * inode;
 852         unsigned int len;
 853         unsigned int bss;
 854         unsigned int start_addr;
 855         int error;
 856         
 857         file = current->files->fd[fd];
 858         inode = file->f_inode;
 859         
 860         set_fs(KERNEL_DS);
 861         if (file->f_op->read(inode, file, (char *) &ex, sizeof(ex)) != sizeof(ex)) {
 862                 return -EACCES;
 863         }
 864         set_fs(USER_DS);
 865         
 866         /* We come in here for the regular a.out style of shared libraries */
 867         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || ex.a_trsize ||
 868             ex.a_drsize || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 869             inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
 870                 return -ENOEXEC;
 871         }
 872         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
 873             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
 874                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
 875                 return -ENOEXEC;
 876         }
 877         
 878         if (N_FLAGS(ex)) return -ENOEXEC;
 879 
 880         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
 881            this off to get the starting address for the page */
 882 
 883         start_addr =  ex.a_entry & 0xfffff000;
 884 
 885         /* Now use mmap to map the library into memory. */
 886         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
 887                         PROT_READ | PROT_WRITE | PROT_EXEC,
 888                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
 889                         N_TXTOFF(ex));
 890         if (error != start_addr)
 891                 return error;
 892         len = PAGE_ALIGN(ex.a_text + ex.a_data);
 893         bss = ex.a_text + ex.a_data + ex.a_bss;
 894         if (bss > len)
 895                 do_mmap(NULL, start_addr + len, bss-len,
 896                         PROT_READ|PROT_WRITE|PROT_EXEC,
 897                         MAP_PRIVATE|MAP_FIXED, 0);
 898         return 0;
 899 }

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