root/fs/exec.c

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

DEFINITIONS

This source file includes following definitions.
  1. register_binfmt
  2. unregister_binfmt
  3. open_inode
  4. aout_core_dump
  5. sys_uselib
  6. create_tables
  7. count
  8. copy_strings
  9. setup_arg_pages
  10. read_exec
  11. flush_old_exec
  12. do_execve
  13. set_brk
  14. load_aout_binary
  15. load_aout_library

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

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