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

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