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. exec_mmap
  12. flush_old_exec
  13. do_execve
  14. set_brk
  15. load_aout_binary
  16. 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 
  54 extern void dump_thread(struct pt_regs *, struct user *);
  55 
  56 /*
  57  * Here are the actual binaries that will be accepted:
  58  * add more with "register_binfmt()"..
  59  */
  60 extern struct linux_binfmt elf_format;
  61 
  62 static struct linux_binfmt aout_format = {
  63 #ifndef CONFIG_BINFMT_ELF
  64         NULL, NULL, load_aout_binary, load_aout_library, aout_core_dump
  65 #else
  66         &elf_format, NULL, load_aout_binary, load_aout_library, aout_core_dump
  67 #endif
  68 };
  69 
  70 static struct linux_binfmt *formats = &aout_format;
  71 
  72 int register_binfmt(struct linux_binfmt * fmt)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74         struct linux_binfmt ** tmp = &formats;
  75 
  76         if (!fmt)
  77                 return -EINVAL;
  78         if (fmt->next)
  79                 return -EBUSY;
  80         while (*tmp) {
  81                 if (fmt == *tmp)
  82                         return -EBUSY;
  83                 tmp = &(*tmp)->next;
  84         }
  85         fmt->next = formats;
  86         formats = 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 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 #ifdef __alpha__
 176 #       define START_DATA(u)    (u.start_data)
 177 #else
 178 #       define START_DATA(u)    (u.u_tsize << PAGE_SHIFT)
 179 #endif
 180 
 181         if (!current->dumpable)
 182                 return 0;
 183         current->dumpable = 0;
 184 
 185 /* See if we have enough room to write the upage.  */
 186         if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)
 187                 return 0;
 188         fs = get_fs();
 189         set_fs(KERNEL_DS);
 190         memcpy(corefile,"core.",5);
 191 #if 0
 192         memcpy(corefile+5,current->comm,sizeof(current->comm));
 193 #else
 194         corefile[4] = '\0';
 195 #endif
 196         if (open_namei(corefile,O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)) {
 197                 inode = NULL;
 198                 goto end_coredump;
 199         }
 200         if (!S_ISREG(inode->i_mode))
 201                 goto end_coredump;
 202         if (!inode->i_op || !inode->i_op->default_file_ops)
 203                 goto end_coredump;
 204         if (get_write_access(inode))
 205                 goto end_coredump;
 206         file.f_mode = 3;
 207         file.f_flags = 0;
 208         file.f_count = 1;
 209         file.f_inode = inode;
 210         file.f_pos = 0;
 211         file.f_reada = 0;
 212         file.f_op = inode->i_op->default_file_ops;
 213         if (file.f_op->open)
 214                 if (file.f_op->open(inode,&file))
 215                         goto done_coredump;
 216         if (!file.f_op->write)
 217                 goto close_coredump;
 218         has_dumped = 1;
 219         strncpy(dump.u_comm, current->comm, sizeof(current->comm));
 220         dump.u_ar0 = (void *)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
 221         dump.signal = signr;
 222         dump_thread(regs, &dump);
 223 
 224 /* If the size of the dump file exceeds the rlimit, then see what would happen
 225    if we wrote the stack, but not the data area.  */
 226         if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >
 227             current->rlim[RLIMIT_CORE].rlim_cur)
 228                 dump.u_dsize = 0;
 229 
 230 /* Make sure we have enough room to write the stack and data areas. */
 231         if ((dump.u_ssize+1) * PAGE_SIZE >
 232             current->rlim[RLIMIT_CORE].rlim_cur)
 233                 dump.u_ssize = 0;
 234 
 235 /* make sure we actually have a data and stack area to dump */
 236         set_fs(USER_DS);
 237         if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
 238                 dump.u_dsize = 0;
 239         if (verify_area(VERIFY_READ, (void *) dump.start_stack, dump.u_ssize << PAGE_SHIFT))
 240                 dump.u_ssize = 0;
 241 
 242         set_fs(KERNEL_DS);
 243 /* struct user */
 244         DUMP_WRITE(&dump,sizeof(dump));
 245 /* Now dump all of the user data.  Include malloced stuff as well */
 246         DUMP_SEEK(PAGE_SIZE);
 247 /* now we start writing out the user space info */
 248         set_fs(USER_DS);
 249 /* Dump the data area */
 250         if (dump.u_dsize != 0) {
 251                 dump_start = START_DATA(dump);
 252                 dump_size = dump.u_dsize << PAGE_SHIFT;
 253                 DUMP_WRITE(dump_start,dump_size);
 254         }
 255 /* Now prepare to dump the stack area */
 256         if (dump.u_ssize != 0) {
 257                 dump_start = dump.start_stack;
 258                 dump_size = dump.u_ssize << PAGE_SHIFT;
 259                 DUMP_WRITE(dump_start,dump_size);
 260         }
 261 /* Finally dump the task struct.  Not be used by gdb, but could be useful */
 262         set_fs(KERNEL_DS);
 263         DUMP_WRITE(current,sizeof(*current));
 264 close_coredump:
 265         if (file.f_op->release)
 266                 file.f_op->release(inode,&file);
 267 done_coredump:
 268         put_write_access(inode);
 269 end_coredump:
 270         set_fs(fs);
 271         iput(inode);
 272         return has_dumped;
 273 }
 274 
 275 /*
 276  * Note that a shared library must be both readable and executable due to
 277  * security reasons.
 278  *
 279  * Also note that we take the address to load from from the file itself.
 280  */
 281 asmlinkage int sys_uselib(const char * library)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         int fd, retval;
 284         struct file * file;
 285         struct linux_binfmt * fmt;
 286 
 287         fd = sys_open(library, 0, 0);
 288         if (fd < 0)
 289                 return fd;
 290         file = current->files->fd[fd];
 291         retval = -ENOEXEC;
 292         if (file && file->f_inode && file->f_op && file->f_op->read) {
 293                 for (fmt = formats ; fmt ; fmt = fmt->next) {
 294                         int (*fn)(int) = fmt->load_shlib;
 295                         if (!fn)
 296                                 break;
 297                         retval = fn(fd);
 298                         if (retval != -ENOEXEC)
 299                                 break;
 300                 }
 301         }
 302         sys_close(fd);
 303         return retval;
 304 }
 305 
 306 /*
 307  * create_tables() parses the env- and arg-strings in new user
 308  * memory and creates the pointer tables from them, and puts their
 309  * addresses on the "stack", returning the new stack pointer value.
 310  */
 311 unsigned long * create_tables(char * p, struct linux_binprm * bprm, int ibcs)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         unsigned long *argv,*envp;
 314         unsigned long * sp;
 315         struct vm_area_struct *mpnt;
 316         int argc = bprm->argc;
 317         int envc = bprm->envc;
 318 
 319         mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 320         if (mpnt) {
 321                 mpnt->vm_mm = current->mm;
 322                 mpnt->vm_start = PAGE_MASK & (unsigned long) p;
 323                 mpnt->vm_end = STACK_TOP;
 324                 mpnt->vm_page_prot = PAGE_COPY;
 325                 mpnt->vm_flags = VM_STACK_FLAGS;
 326                 mpnt->vm_ops = NULL;
 327                 mpnt->vm_offset = 0;
 328                 mpnt->vm_inode = NULL;
 329                 mpnt->vm_pte = 0;
 330                 insert_vm_struct(current, mpnt);
 331                 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
 332         }
 333         sp = (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
 334 #ifdef __alpha__
 335 /* whee.. test-programs are so much fun. */
 336         put_user(0, --sp);
 337         put_user(0, --sp);
 338         if (bprm->loader) {
 339                 put_user(0, --sp);
 340                 put_user(0x3eb, --sp);
 341                 put_user(bprm->loader, --sp);
 342                 put_user(0x3ea, --sp);
 343         }
 344         put_user(bprm->exec, --sp);
 345         put_user(0x3e9, --sp);
 346 #endif
 347         sp -= envc+1;
 348         envp = sp;
 349         sp -= argc+1;
 350         argv = sp;
 351 #ifdef __i386__
 352         if (!ibcs) {
 353                 put_user(envp,--sp);
 354                 put_user(argv,--sp);
 355         }
 356 #endif
 357         put_user(argc,--sp);
 358         current->mm->arg_start = (unsigned long) p;
 359         while (argc-->0) {
 360                 put_user(p,argv++);
 361                 while (get_user(p++)) /* nothing */ ;
 362         }
 363         put_user(NULL,argv);
 364         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
 365         while (envc-->0) {
 366                 put_user(p,envp++);
 367                 while (get_user(p++)) /* nothing */ ;
 368         }
 369         put_user(NULL,envp);
 370         current->mm->env_end = (unsigned long) p;
 371         return sp;
 372 }
 373 
 374 /*
 375  * count() counts the number of arguments/envelopes
 376  *
 377  * We also do some limited EFAULT checking: this isn't complete, but
 378  * it does cover most cases. I'll have to do this correctly some day..
 379  */
 380 static int count(char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 381 {
 382         int error, i = 0;
 383         char ** tmp, *p;
 384 
 385         if ((tmp = argv) != NULL) {
 386                 error = verify_area(VERIFY_READ, tmp, sizeof(char *));
 387                 if (error)
 388                         return error;
 389                 while ((p = get_user(tmp++)) != NULL) {
 390                         i++;
 391                         error = verify_area(VERIFY_READ, p, 1);
 392                         if (error)
 393                                 return error;
 394                 }
 395         }
 396         return i;
 397 }
 398 
 399 /*
 400  * 'copy_string()' copies argument/envelope strings from user
 401  * memory to free pages in kernel mem. These are in a format ready
 402  * to be put directly into the top of new user memory.
 403  *
 404  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
 405  * whether the string and the string array are from user or kernel segments:
 406  * 
 407  * from_kmem     argv *        argv **
 408  *    0          user space    user space
 409  *    1          kernel space  user space
 410  *    2          kernel space  kernel space
 411  * 
 412  * We do this by playing games with the fs segment register.  Since it
 413  * is expensive to load a segment register, we try to avoid calling
 414  * set_fs() unless we absolutely have to.
 415  */
 416 unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
     /* [previous][next][first][last][top][bottom][index][help] */
 417                 unsigned long p, int from_kmem)
 418 {
 419         char *tmp, *pag = NULL;
 420         int len, offset = 0;
 421         unsigned long old_fs, new_fs;
 422 
 423         if (!p)
 424                 return 0;       /* bullet-proofing */
 425         new_fs = get_ds();
 426         old_fs = get_fs();
 427         if (from_kmem==2)
 428                 set_fs(new_fs);
 429         while (argc-- > 0) {
 430                 if (from_kmem == 1)
 431                         set_fs(new_fs);
 432                 if (!(tmp = get_user(argv+argc)))
 433                         panic("VFS: argc is wrong");
 434                 if (from_kmem == 1)
 435                         set_fs(old_fs);
 436                 len=0;          /* remember zero-padding */
 437                 do {
 438                         len++;
 439                 } while (get_user(tmp++));
 440                 if (p < len) {  /* this shouldn't happen - 128kB */
 441                         set_fs(old_fs);
 442                         return 0;
 443                 }
 444                 while (len) {
 445                         --p; --tmp; --len;
 446                         if (--offset < 0) {
 447                                 offset = p % PAGE_SIZE;
 448                                 if (from_kmem==2)
 449                                         set_fs(old_fs);
 450                                 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
 451                                     !(pag = (char *) page[p/PAGE_SIZE] =
 452                                       (unsigned long *) get_free_page(GFP_USER))) 
 453                                         return 0;
 454                                 if (from_kmem==2)
 455                                         set_fs(new_fs);
 456 
 457                         }
 458                         *(pag + offset) = get_user(tmp);
 459                 }
 460         }
 461         if (from_kmem==2)
 462                 set_fs(old_fs);
 463         return p;
 464 }
 465 
 466 unsigned long setup_arg_pages(unsigned long text_size, unsigned long * page)
     /* [previous][next][first][last][top][bottom][index][help] */
 467 {
 468         unsigned long data_base;
 469         int i;
 470 
 471         data_base = STACK_TOP;
 472         for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
 473                 data_base -= PAGE_SIZE;
 474                 if (page[i]) {
 475                         current->mm->rss++;
 476                         put_dirty_page(current,page[i],data_base);
 477                 }
 478         }
 479         return STACK_TOP;
 480 }
 481 
 482 /*
 483  * Read in the complete executable. This is used for "-N" files
 484  * that aren't on a block boundary, and for files on filesystems
 485  * without bmap support.
 486  */
 487 int read_exec(struct inode *inode, unsigned long offset,
     /* [previous][next][first][last][top][bottom][index][help] */
 488         char * addr, unsigned long count, int to_kmem)
 489 {
 490         struct file file;
 491         int result = -ENOEXEC;
 492 
 493         if (!inode->i_op || !inode->i_op->default_file_ops)
 494                 goto end_readexec;
 495         file.f_mode = 1;
 496         file.f_flags = 0;
 497         file.f_count = 1;
 498         file.f_inode = inode;
 499         file.f_pos = 0;
 500         file.f_reada = 0;
 501         file.f_op = inode->i_op->default_file_ops;
 502         if (file.f_op->open)
 503                 if (file.f_op->open(inode,&file))
 504                         goto end_readexec;
 505         if (!file.f_op || !file.f_op->read)
 506                 goto close_readexec;
 507         if (file.f_op->lseek) {
 508                 if (file.f_op->lseek(inode,&file,offset,0) != offset)
 509                         goto close_readexec;
 510         } else
 511                 file.f_pos = offset;
 512         if (to_kmem) {
 513                 unsigned long old_fs = get_fs();
 514                 set_fs(get_ds());
 515                 result = file.f_op->read(inode, &file, addr, count);
 516                 set_fs(old_fs);
 517         } else {
 518                 result = verify_area(VERIFY_WRITE, addr, count);
 519                 if (result)
 520                         goto close_readexec;
 521                 result = file.f_op->read(inode, &file, addr, count);
 522         }
 523 close_readexec:
 524         if (file.f_op->release)
 525                 file.f_op->release(inode,&file);
 526 end_readexec:
 527         return result;
 528 }
 529 
 530 static void exec_mmap(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 531 {
 532         /*
 533          * The clear_page_tables done later on exec does the right thing
 534          * to the page directory when shared, except for graceful abort
 535          * (the oom is wrong there, too, IMHO)
 536          */
 537         if (current->mm->count > 1) {
 538                 struct mm_struct *mm = kmalloc(sizeof(*mm), GFP_KERNEL);
 539                 if (!mm) {
 540                         /* this is wrong, I think. */
 541                         oom(current);
 542                         return;
 543                 }
 544                 *mm = *current->mm;
 545                 mm->def_flags = 0;      /* should future lockings be kept? */
 546                 mm->count = 1;
 547                 mm->mmap = NULL;
 548                 mm->mmap_avl = NULL;
 549                 mm->total_vm = 0;
 550                 mm->rss = 0;
 551                 current->mm->count--;
 552                 current->mm = mm;
 553                 new_page_tables(current);
 554                 return;
 555         }
 556         exit_mmap(current->mm);
 557         clear_page_tables(current);
 558 }
 559 
 560 /*
 561  * This function flushes out all traces of the currently running executable so
 562  * that a new one can be started
 563  */
 564 
 565 void flush_old_exec(struct linux_binprm * bprm)
     /* [previous][next][first][last][top][bottom][index][help] */
 566 {
 567         int i;
 568         int ch;
 569         char * name;
 570 
 571         if (current->euid == current->uid && current->egid == current->gid)
 572                 current->dumpable = 1;
 573         name = bprm->filename;
 574         for (i=0; (ch = *(name++)) != '\0';) {
 575                 if (ch == '/')
 576                         i = 0;
 577                 else
 578                         if (i < 15)
 579                                 current->comm[i++] = ch;
 580         }
 581         current->comm[i] = '\0';
 582 
 583         /* Release all of the old mmap stuff. */
 584         exec_mmap();
 585 
 586         flush_thread();
 587 
 588         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
 589             permission(bprm->inode,MAY_READ))
 590                 current->dumpable = 0;
 591         current->signal = 0;
 592         for (i=0 ; i<32 ; i++) {
 593                 current->sig->action[i].sa_mask = 0;
 594                 current->sig->action[i].sa_flags = 0;
 595                 if (current->sig->action[i].sa_handler != SIG_IGN)
 596                         current->sig->action[i].sa_handler = NULL;
 597         }
 598         for (i=0 ; i<NR_OPEN ; i++)
 599                 if (FD_ISSET(i,&current->files->close_on_exec))
 600                         sys_close(i);
 601         FD_ZERO(&current->files->close_on_exec);
 602         if (last_task_used_math == current)
 603                 last_task_used_math = NULL;
 604         current->used_math = 0;
 605 }
 606 
 607 /*
 608  * sys_execve() executes a new program.
 609  */
 610 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 611 {
 612         struct linux_binprm bprm;
 613         struct linux_binfmt * fmt;
 614         int i;
 615         int retval;
 616         int sh_bang = 0;
 617 #ifdef __alpha__
 618         int loader = 0;
 619 #endif
 620 
 621         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
 622         for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 623                 bprm.page[i] = 0;
 624         retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
 625         if (retval)
 626                 return retval;
 627         bprm.filename = filename;
 628         bprm.loader = 0;
 629         bprm.exec = 0;
 630         if ((bprm.argc = count(argv)) < 0)
 631                 return bprm.argc;
 632         if ((bprm.envc = count(envp)) < 0)
 633                 return bprm.envc;
 634         
 635 restart_interp:
 636         if (!S_ISREG(bprm.inode->i_mode)) {     /* must be regular file */
 637                 retval = -EACCES;
 638                 goto exec_error2;
 639         }
 640         if (IS_NOEXEC(bprm.inode)) {            /* FS mustn't be mounted noexec */
 641                 retval = -EPERM;
 642                 goto exec_error2;
 643         }
 644         if (!bprm.inode->i_sb) {
 645                 retval = -EACCES;
 646                 goto exec_error2;
 647         }
 648         i = bprm.inode->i_mode;
 649         if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
 650             euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
 651                 retval = -EPERM;
 652                 goto exec_error2;
 653         }
 654         /* make sure we don't let suid, sgid files be ptraced. */
 655         if (current->flags & PF_PTRACED) {
 656                 bprm.e_uid = current->euid;
 657                 bprm.e_gid = current->egid;
 658         } else {
 659                 bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
 660                 bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
 661         }
 662         if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
 663                 goto exec_error2;
 664         if (!(bprm.inode->i_mode & 0111) && fsuser()) {
 665                 retval = -EACCES;
 666                 goto exec_error2;
 667         }
 668         /* better not execute files which are being written to */
 669         if (bprm.inode->i_writecount > 0) {
 670                 retval = -ETXTBSY;
 671                 goto exec_error2;
 672         }
 673         memset(bprm.buf,0,sizeof(bprm.buf));
 674         retval = read_exec(bprm.inode,0,bprm.buf,128,1);
 675         if (retval < 0)
 676                 goto exec_error2;
 677         if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
 678                 /*
 679                  * This section does the #! interpretation.
 680                  * Sorta complicated, but hopefully it will work.  -TYT
 681                  */
 682 
 683                 char *cp, *interp, *i_name, *i_arg;
 684 
 685                 iput(bprm.inode);
 686                 bprm.buf[127] = '\0';
 687                 if ((cp = strchr(bprm.buf, '\n')) == NULL)
 688                         cp = bprm.buf+127;
 689                 *cp = '\0';
 690                 while (cp > bprm.buf) {
 691                         cp--;
 692                         if ((*cp == ' ') || (*cp == '\t'))
 693                                 *cp = '\0';
 694                         else
 695                                 break;
 696                 }
 697                 for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
 698                 if (!cp || *cp == '\0') {
 699                         retval = -ENOEXEC; /* No interpreter name found */
 700                         goto exec_error1;
 701                 }
 702                 interp = i_name = cp;
 703                 i_arg = 0;
 704                 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
 705                         if (*cp == '/')
 706                                 i_name = cp+1;
 707                 }
 708                 while ((*cp == ' ') || (*cp == '\t'))
 709                         *cp++ = '\0';
 710                 if (*cp)
 711                         i_arg = cp;
 712                 /*
 713                  * OK, we've parsed out the interpreter name and
 714                  * (optional) argument.
 715                  */
 716                 if (sh_bang++ == 0) {
 717                         bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
 718                         bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
 719                 }
 720                 /*
 721                  * Splice in (1) the interpreter's name for argv[0]
 722                  *           (2) (optional) argument to interpreter
 723                  *           (3) filename of shell script
 724                  *
 725                  * This is done in reverse order, because of how the
 726                  * user environment and arguments are stored.
 727                  */
 728                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 729                 bprm.argc++;
 730                 if (i_arg) {
 731                         bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
 732                         bprm.argc++;
 733                 }
 734                 bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
 735                 bprm.argc++;
 736                 if (!bprm.p) {
 737                         retval = -E2BIG;
 738                         goto exec_error1;
 739                 }
 740                 /*
 741                  * OK, now restart the process with the interpreter's inode.
 742                  * Note that we use open_namei() as the name is now in kernel
 743                  * space, and we don't need to copy it.
 744                  */
 745                 retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
 746                 if (retval)
 747                         goto exec_error1;
 748                 goto restart_interp;
 749         }
 750 #ifdef __alpha__
 751         /* handle /sbin/loader.. */
 752         {
 753             struct exec * eh = (struct exec *) bprm.buf;
 754 
 755             if (!loader && eh->fh.f_magic == 0x183 &&
 756                 (eh->fh.f_flags & 0x3000) == 0x3000)
 757             {
 758                 char * dynloader[] = { "/sbin/loader" };
 759                 iput(bprm.inode);
 760                 loader = 1;
 761                 bprm.p = copy_strings(1, dynloader, bprm.page, bprm.p, 2);
 762                 bprm.loader = bprm.p;
 763                 retval = open_namei(dynloader[0], 0, 0, &bprm.inode, NULL);
 764                 if (retval)
 765                         goto exec_error1;
 766                 goto restart_interp;
 767             }
 768         }
 769 #endif
 770         if (!sh_bang) {
 771                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 772                 bprm.exec = bprm.p;
 773                 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
 774                 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
 775                 if (!bprm.p) {
 776                         retval = -E2BIG;
 777                         goto exec_error2;
 778                 }
 779         }
 780 
 781         bprm.sh_bang = sh_bang;
 782         for (fmt = formats ; fmt ; fmt = fmt->next) {
 783                 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
 784                 if (!fn)
 785                         break;
 786                 retval = fn(&bprm, regs);
 787                 if (retval >= 0) {
 788                         iput(bprm.inode);
 789                         current->did_exec = 1;
 790                         return retval;
 791                 }
 792                 if (retval != -ENOEXEC)
 793                         break;
 794         }
 795 exec_error2:
 796         iput(bprm.inode);
 797 exec_error1:
 798         for (i=0 ; i<MAX_ARG_PAGES ; i++)
 799                 free_page(bprm.page[i]);
 800         return(retval);
 801 }
 802 
 803 static void set_brk(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805         start = PAGE_ALIGN(start);
 806         end = PAGE_ALIGN(end);
 807         if (end <= start)
 808                 return;
 809         do_mmap(NULL, start, end - start,
 810                 PROT_READ | PROT_WRITE | PROT_EXEC,
 811                 MAP_FIXED | MAP_PRIVATE, 0);
 812 }
 813 
 814 /*
 815  * These are the functions used to load a.out style executables and shared
 816  * libraries.  There is no binary dependent code anywhere else.
 817  */
 818 
 819 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821         struct exec ex;
 822         struct file * file;
 823         int fd;
 824         unsigned long error;
 825         unsigned long p = bprm->p;
 826         unsigned long fd_offset;
 827         unsigned long rlim;
 828 
 829         ex = *((struct exec *) bprm->buf);              /* exec-header */
 830         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
 831              N_MAGIC(ex) != QMAGIC) ||
 832             N_TRSIZE(ex) || N_DRSIZE(ex) ||
 833             bprm->inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 834                 return -ENOEXEC;
 835         }
 836 
 837         current->personality = PER_LINUX;
 838         fd_offset = N_TXTOFF(ex);
 839 
 840 #ifdef __i386__
 841         if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
 842                 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
 843                 return -ENOEXEC;
 844         }
 845 
 846         if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
 847             (fd_offset < bprm->inode->i_sb->s_blocksize)) {
 848                 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
 849                 return -ENOEXEC;
 850         }
 851 #endif
 852 
 853         /* Check initial limits. This avoids letting people circumvent
 854          * size limits imposed on them by creating programs with large
 855          * arrays in the data or bss.
 856          */
 857         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
 858         if (rlim >= RLIM_INFINITY)
 859                 rlim = ~0;
 860         if (ex.a_data + ex.a_bss > rlim)
 861                 return -ENOMEM;
 862 
 863         /* OK, This is the point of no return */
 864         flush_old_exec(bprm);
 865 
 866         current->mm->end_code = ex.a_text +
 867                 (current->mm->start_code = N_TXTADDR(ex));
 868         current->mm->end_data = ex.a_data +
 869                 (current->mm->start_data = N_DATADDR(ex));
 870         current->mm->brk = ex.a_bss +
 871                 (current->mm->start_brk = N_BSSADDR(ex));
 872 
 873         current->mm->rss = 0;
 874         current->mm->mmap = NULL;
 875         current->suid = current->euid = current->fsuid = bprm->e_uid;
 876         current->sgid = current->egid = current->fsgid = bprm->e_gid;
 877         if (N_MAGIC(ex) == OMAGIC) {
 878 #ifdef __alpha__
 879                 do_mmap(NULL, N_TXTADDR(ex) & PAGE_MASK,
 880                         ex.a_text+ex.a_data + PAGE_SIZE - 1,
 881                         PROT_READ|PROT_WRITE|PROT_EXEC,
 882                         MAP_FIXED|MAP_PRIVATE, 0);
 883                 read_exec(bprm->inode, fd_offset, (char *) N_TXTADDR(ex),
 884                           ex.a_text+ex.a_data, 0);
 885 #else
 886                 do_mmap(NULL, 0, ex.a_text+ex.a_data,
 887                         PROT_READ|PROT_WRITE|PROT_EXEC,
 888                         MAP_FIXED|MAP_PRIVATE, 0);
 889                 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data, 0);
 890 #endif
 891         } else {
 892                 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
 893                         printk(KERN_NOTICE "executable not page aligned\n");
 894                 
 895                 fd = open_inode(bprm->inode, O_RDONLY);
 896                 
 897                 if (fd < 0)
 898                         return fd;
 899                 file = current->files->fd[fd];
 900                 if (!file->f_op || !file->f_op->mmap) {
 901                         sys_close(fd);
 902                         do_mmap(NULL, 0, ex.a_text+ex.a_data,
 903                                 PROT_READ|PROT_WRITE|PROT_EXEC,
 904                                 MAP_FIXED|MAP_PRIVATE, 0);
 905                         read_exec(bprm->inode, fd_offset,
 906                                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data, 0);
 907                         goto beyond_if;
 908                 }
 909 
 910                 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
 911                         PROT_READ | PROT_EXEC,
 912                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 913                         fd_offset);
 914 
 915                 if (error != N_TXTADDR(ex)) {
 916                         sys_close(fd);
 917                         send_sig(SIGKILL, current, 0);
 918                         return error;
 919                 }
 920                 
 921                 error = do_mmap(file, N_DATADDR(ex), ex.a_data,
 922                                 PROT_READ | PROT_WRITE | PROT_EXEC,
 923                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 924                                 fd_offset + ex.a_text);
 925                 sys_close(fd);
 926                 if (error != N_DATADDR(ex)) {
 927                         send_sig(SIGKILL, current, 0);
 928                         return error;
 929                 }
 930         }
 931 beyond_if:
 932         if (current->exec_domain && current->exec_domain->use_count)
 933                 (*current->exec_domain->use_count)--;
 934         if (current->binfmt && current->binfmt->use_count)
 935                 (*current->binfmt->use_count)--;
 936         current->exec_domain = lookup_exec_domain(current->personality);
 937         current->binfmt = &aout_format;
 938         if (current->exec_domain && current->exec_domain->use_count)
 939                 (*current->exec_domain->use_count)++;
 940         if (current->binfmt && current->binfmt->use_count)
 941                 (*current->binfmt->use_count)++;
 942 
 943         set_brk(current->mm->start_brk, current->mm->brk);
 944 
 945         fd_offset = setup_arg_pages(ex.a_text,bprm->page) - MAX_ARG_PAGES*PAGE_SIZE;
 946         p += fd_offset;
 947         if (bprm->loader)
 948                 bprm->loader += fd_offset;
 949         bprm->exec += fd_offset;
 950         
 951         p = (unsigned long)create_tables((char *)p, bprm,
 952                                         current->personality != PER_LINUX);
 953         current->mm->start_stack = p;
 954 #ifdef __alpha__
 955         regs->gp = ex.a_gpvalue;
 956 #endif
 957         start_thread(regs, ex.a_entry, p);
 958         if (current->flags & PF_PTRACED)
 959                 send_sig(SIGTRAP, current, 0);
 960         return 0;
 961 }
 962 
 963 
 964 static int load_aout_library(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 965 {
 966         struct file * file;
 967         struct exec ex;
 968         struct  inode * inode;
 969         unsigned int len;
 970         unsigned int bss;
 971         unsigned int start_addr;
 972         unsigned long error;
 973         
 974         file = current->files->fd[fd];
 975         inode = file->f_inode;
 976         
 977         if (!file || !file->f_op)
 978                 return -EACCES;
 979 
 980         /* Seek into the file */
 981         if (file->f_op->lseek) {
 982                 if ((error = file->f_op->lseek(inode, file, 0, 0)) != 0)
 983                         return -ENOEXEC;
 984         } else
 985                 file->f_pos = 0;
 986 
 987         set_fs(KERNEL_DS);
 988         error = file->f_op->read(inode, file, (char *) &ex, sizeof(ex));
 989         set_fs(USER_DS);
 990         if (error != sizeof(ex))
 991                 return -ENOEXEC;
 992 
 993         /* We come in here for the regular a.out style of shared libraries */
 994         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
 995             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 996             inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 997                 return -ENOEXEC;
 998         }
 999         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
1000             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
1001                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
1002                 return -ENOEXEC;
1003         }
1004         
1005         if (N_FLAGS(ex)) return -ENOEXEC;
1006 
1007         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
1008            this off to get the starting address for the page */
1009 
1010         start_addr =  ex.a_entry & 0xfffff000;
1011 
1012         /* Now use mmap to map the library into memory. */
1013         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
1014                         PROT_READ | PROT_WRITE | PROT_EXEC,
1015                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1016                         N_TXTOFF(ex));
1017         if (error != start_addr)
1018                 return error;
1019         len = PAGE_ALIGN(ex.a_text + ex.a_data);
1020         bss = ex.a_text + ex.a_data + ex.a_bss;
1021         if (bss > len)
1022                 do_mmap(NULL, start_addr + len, bss-len,
1023                         PROT_READ|PROT_WRITE|PROT_EXEC,
1024                         MAP_PRIVATE|MAP_FIXED, 0);
1025         return 0;
1026 }

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