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         }
 332         sp = (unsigned long *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
 333 #ifdef __alpha__
 334 /* whee.. test-programs are so much fun. */
 335         put_user(0, --sp);
 336         put_user(0, --sp);
 337         if (bprm->loader) {
 338                 put_user(0, --sp);
 339                 put_user(0x3eb, --sp);
 340                 put_user(bprm->loader, --sp);
 341                 put_user(0x3ea, --sp);
 342         }
 343         put_user(bprm->exec, --sp);
 344         put_user(0x3e9, --sp);
 345 #endif
 346         sp -= envc+1;
 347         envp = sp;
 348         sp -= argc+1;
 349         argv = sp;
 350 #ifdef __i386__
 351         if (!ibcs) {
 352                 put_user(envp,--sp);
 353                 put_user(argv,--sp);
 354         }
 355 #endif
 356         put_user(argc,--sp);
 357         current->mm->arg_start = (unsigned long) p;
 358         while (argc-->0) {
 359                 put_user(p,argv++);
 360                 while (get_user(p++)) /* nothing */ ;
 361         }
 362         put_user(NULL,argv);
 363         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
 364         while (envc-->0) {
 365                 put_user(p,envp++);
 366                 while (get_user(p++)) /* nothing */ ;
 367         }
 368         put_user(NULL,envp);
 369         current->mm->env_end = (unsigned long) p;
 370         return sp;
 371 }
 372 
 373 /*
 374  * count() counts the number of arguments/envelopes
 375  *
 376  * We also do some limited EFAULT checking: this isn't complete, but
 377  * it does cover most cases. I'll have to do this correctly some day..
 378  */
 379 static int count(char ** argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 380 {
 381         int error, i = 0;
 382         char ** tmp, *p;
 383 
 384         if ((tmp = argv) != NULL) {
 385                 error = verify_area(VERIFY_READ, tmp, sizeof(char *));
 386                 if (error)
 387                         return error;
 388                 while ((p = get_user(tmp++)) != NULL) {
 389                         i++;
 390                         error = verify_area(VERIFY_READ, p, 1);
 391                         if (error)
 392                                 return error;
 393                 }
 394         }
 395         return i;
 396 }
 397 
 398 /*
 399  * 'copy_string()' copies argument/envelope strings from user
 400  * memory to free pages in kernel mem. These are in a format ready
 401  * to be put directly into the top of new user memory.
 402  *
 403  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
 404  * whether the string and the string array are from user or kernel segments:
 405  * 
 406  * from_kmem     argv *        argv **
 407  *    0          user space    user space
 408  *    1          kernel space  user space
 409  *    2          kernel space  kernel space
 410  * 
 411  * We do this by playing games with the fs segment register.  Since it
 412  * is expensive to load a segment register, we try to avoid calling
 413  * set_fs() unless we absolutely have to.
 414  */
 415 unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
     /* [previous][next][first][last][top][bottom][index][help] */
 416                 unsigned long p, int from_kmem)
 417 {
 418         char *tmp, *pag = NULL;
 419         int len, offset = 0;
 420         unsigned long old_fs, new_fs;
 421 
 422         if (!p)
 423                 return 0;       /* bullet-proofing */
 424         new_fs = get_ds();
 425         old_fs = get_fs();
 426         if (from_kmem==2)
 427                 set_fs(new_fs);
 428         while (argc-- > 0) {
 429                 if (from_kmem == 1)
 430                         set_fs(new_fs);
 431                 if (!(tmp = get_user(argv+argc)))
 432                         panic("VFS: argc is wrong");
 433                 if (from_kmem == 1)
 434                         set_fs(old_fs);
 435                 len=0;          /* remember zero-padding */
 436                 do {
 437                         len++;
 438                 } while (get_user(tmp++));
 439                 if (p < len) {  /* this shouldn't happen - 128kB */
 440                         set_fs(old_fs);
 441                         return 0;
 442                 }
 443                 while (len) {
 444                         --p; --tmp; --len;
 445                         if (--offset < 0) {
 446                                 offset = p % PAGE_SIZE;
 447                                 if (from_kmem==2)
 448                                         set_fs(old_fs);
 449                                 if (!(pag = (char *) page[p/PAGE_SIZE]) &&
 450                                     !(pag = (char *) page[p/PAGE_SIZE] =
 451                                       (unsigned long *) get_free_page(GFP_USER))) 
 452                                         return 0;
 453                                 if (from_kmem==2)
 454                                         set_fs(new_fs);
 455 
 456                         }
 457                         *(pag + offset) = get_user(tmp);
 458                 }
 459         }
 460         if (from_kmem==2)
 461                 set_fs(old_fs);
 462         return p;
 463 }
 464 
 465 unsigned long setup_arg_pages(unsigned long text_size, unsigned long * page)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467         unsigned long data_base;
 468         int i;
 469 
 470         data_base = STACK_TOP;
 471         for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
 472                 data_base -= PAGE_SIZE;
 473                 if (page[i]) {
 474                         current->mm->rss++;
 475                         put_dirty_page(current,page[i],data_base);
 476                 }
 477         }
 478         return STACK_TOP;
 479 }
 480 
 481 /*
 482  * Read in the complete executable. This is used for "-N" files
 483  * that aren't on a block boundary, and for files on filesystems
 484  * without bmap support.
 485  */
 486 int read_exec(struct inode *inode, unsigned long offset,
     /* [previous][next][first][last][top][bottom][index][help] */
 487         char * addr, unsigned long count, int to_kmem)
 488 {
 489         struct file file;
 490         int result = -ENOEXEC;
 491 
 492         if (!inode->i_op || !inode->i_op->default_file_ops)
 493                 goto end_readexec;
 494         file.f_mode = 1;
 495         file.f_flags = 0;
 496         file.f_count = 1;
 497         file.f_inode = inode;
 498         file.f_pos = 0;
 499         file.f_reada = 0;
 500         file.f_op = inode->i_op->default_file_ops;
 501         if (file.f_op->open)
 502                 if (file.f_op->open(inode,&file))
 503                         goto end_readexec;
 504         if (!file.f_op || !file.f_op->read)
 505                 goto close_readexec;
 506         if (file.f_op->lseek) {
 507                 if (file.f_op->lseek(inode,&file,offset,0) != offset)
 508                         goto close_readexec;
 509         } else
 510                 file.f_pos = offset;
 511         if (to_kmem) {
 512                 unsigned long old_fs = get_fs();
 513                 set_fs(get_ds());
 514                 result = file.f_op->read(inode, &file, addr, count);
 515                 set_fs(old_fs);
 516         } else {
 517                 result = verify_area(VERIFY_WRITE, addr, count);
 518                 if (result)
 519                         goto close_readexec;
 520                 result = file.f_op->read(inode, &file, addr, count);
 521         }
 522 close_readexec:
 523         if (file.f_op->release)
 524                 file.f_op->release(inode,&file);
 525 end_readexec:
 526         return result;
 527 }
 528 
 529 static void exec_mmap(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 530 {
 531         /*
 532          * The clear_page_tables done later on exec does the right thing
 533          * to the page directory when shared, except for graceful abort
 534          * (the oom is wrong there, too, IMHO)
 535          */
 536         if (current->mm->count > 1) {
 537                 struct mm_struct *mm = kmalloc(sizeof(*mm), GFP_KERNEL);
 538                 if (!mm) {
 539                         /* this is wrong, I think. */
 540                         oom(current);
 541                         return;
 542                 }
 543                 *mm = *current->mm;
 544                 mm->count = 1;
 545                 mm->mmap = NULL;
 546                 mm->mmap_avl = NULL;
 547                 current->mm->count--;
 548                 current->mm = mm;
 549                 new_page_tables(current);
 550                 return;
 551         }
 552         exit_mmap(current->mm);
 553         clear_page_tables(current);
 554 }
 555 
 556 /*
 557  * This function flushes out all traces of the currently running executable so
 558  * that a new one can be started
 559  */
 560 
 561 void flush_old_exec(struct linux_binprm * bprm)
     /* [previous][next][first][last][top][bottom][index][help] */
 562 {
 563         int i;
 564         int ch;
 565         char * name;
 566 
 567         if (current->euid == current->uid && current->egid == current->gid)
 568                 current->dumpable = 1;
 569         name = bprm->filename;
 570         for (i=0; (ch = *(name++)) != '\0';) {
 571                 if (ch == '/')
 572                         i = 0;
 573                 else
 574                         if (i < 15)
 575                                 current->comm[i++] = ch;
 576         }
 577         current->comm[i] = '\0';
 578 
 579         /* Release all of the old mmap stuff. */
 580         exec_mmap();
 581 
 582         flush_thread();
 583 
 584         if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
 585             permission(bprm->inode,MAY_READ))
 586                 current->dumpable = 0;
 587         current->signal = 0;
 588         for (i=0 ; i<32 ; i++) {
 589                 current->sig->action[i].sa_mask = 0;
 590                 current->sig->action[i].sa_flags = 0;
 591                 if (current->sig->action[i].sa_handler != SIG_IGN)
 592                         current->sig->action[i].sa_handler = NULL;
 593         }
 594         for (i=0 ; i<NR_OPEN ; i++)
 595                 if (FD_ISSET(i,&current->files->close_on_exec))
 596                         sys_close(i);
 597         FD_ZERO(&current->files->close_on_exec);
 598         if (last_task_used_math == current)
 599                 last_task_used_math = NULL;
 600         current->used_math = 0;
 601 }
 602 
 603 /*
 604  * sys_execve() executes a new program.
 605  */
 606 int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608         struct linux_binprm bprm;
 609         struct linux_binfmt * fmt;
 610         int i;
 611         int retval;
 612         int sh_bang = 0;
 613 #ifdef __alpha__
 614         int loader = 0;
 615 #endif
 616 
 617         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
 618         for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
 619                 bprm.page[i] = 0;
 620         retval = open_namei(filename, 0, 0, &bprm.inode, NULL);
 621         if (retval)
 622                 return retval;
 623         bprm.filename = filename;
 624         bprm.loader = 0;
 625         bprm.exec = 0;
 626         if ((bprm.argc = count(argv)) < 0)
 627                 return bprm.argc;
 628         if ((bprm.envc = count(envp)) < 0)
 629                 return bprm.envc;
 630         
 631 restart_interp:
 632         if (!S_ISREG(bprm.inode->i_mode)) {     /* must be regular file */
 633                 retval = -EACCES;
 634                 goto exec_error2;
 635         }
 636         if (IS_NOEXEC(bprm.inode)) {            /* FS mustn't be mounted noexec */
 637                 retval = -EPERM;
 638                 goto exec_error2;
 639         }
 640         if (!bprm.inode->i_sb) {
 641                 retval = -EACCES;
 642                 goto exec_error2;
 643         }
 644         i = bprm.inode->i_mode;
 645         if (IS_NOSUID(bprm.inode) && (((i & S_ISUID) && bprm.inode->i_uid != current->
 646             euid) || ((i & S_ISGID) && !in_group_p(bprm.inode->i_gid))) && !suser()) {
 647                 retval = -EPERM;
 648                 goto exec_error2;
 649         }
 650         /* make sure we don't let suid, sgid files be ptraced. */
 651         if (current->flags & PF_PTRACED) {
 652                 bprm.e_uid = current->euid;
 653                 bprm.e_gid = current->egid;
 654         } else {
 655                 bprm.e_uid = (i & S_ISUID) ? bprm.inode->i_uid : current->euid;
 656                 bprm.e_gid = (i & S_ISGID) ? bprm.inode->i_gid : current->egid;
 657         }
 658         if ((retval = permission(bprm.inode, MAY_EXEC)) != 0)
 659                 goto exec_error2;
 660         if (!(bprm.inode->i_mode & 0111) && fsuser()) {
 661                 retval = -EACCES;
 662                 goto exec_error2;
 663         }
 664         /* better not execute files which are being written to */
 665         if (bprm.inode->i_wcount > 0) {
 666                 retval = -ETXTBSY;
 667                 goto exec_error2;
 668         }
 669         memset(bprm.buf,0,sizeof(bprm.buf));
 670         retval = read_exec(bprm.inode,0,bprm.buf,128,1);
 671         if (retval < 0)
 672                 goto exec_error2;
 673         if ((bprm.buf[0] == '#') && (bprm.buf[1] == '!') && (!sh_bang)) {
 674                 /*
 675                  * This section does the #! interpretation.
 676                  * Sorta complicated, but hopefully it will work.  -TYT
 677                  */
 678 
 679                 char *cp, *interp, *i_name, *i_arg;
 680 
 681                 iput(bprm.inode);
 682                 bprm.buf[127] = '\0';
 683                 if ((cp = strchr(bprm.buf, '\n')) == NULL)
 684                         cp = bprm.buf+127;
 685                 *cp = '\0';
 686                 while (cp > bprm.buf) {
 687                         cp--;
 688                         if ((*cp == ' ') || (*cp == '\t'))
 689                                 *cp = '\0';
 690                         else
 691                                 break;
 692                 }
 693                 for (cp = bprm.buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
 694                 if (!cp || *cp == '\0') {
 695                         retval = -ENOEXEC; /* No interpreter name found */
 696                         goto exec_error1;
 697                 }
 698                 interp = i_name = cp;
 699                 i_arg = 0;
 700                 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
 701                         if (*cp == '/')
 702                                 i_name = cp+1;
 703                 }
 704                 while ((*cp == ' ') || (*cp == '\t'))
 705                         *cp++ = '\0';
 706                 if (*cp)
 707                         i_arg = cp;
 708                 /*
 709                  * OK, we've parsed out the interpreter name and
 710                  * (optional) argument.
 711                  */
 712                 if (sh_bang++ == 0) {
 713                         bprm.p = copy_strings(bprm.envc, envp, bprm.page, bprm.p, 0);
 714                         bprm.p = copy_strings(--bprm.argc, argv+1, bprm.page, bprm.p, 0);
 715                 }
 716                 /*
 717                  * Splice in (1) the interpreter's name for argv[0]
 718                  *           (2) (optional) argument to interpreter
 719                  *           (3) filename of shell script
 720                  *
 721                  * This is done in reverse order, because of how the
 722                  * user environment and arguments are stored.
 723                  */
 724                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 725                 bprm.argc++;
 726                 if (i_arg) {
 727                         bprm.p = copy_strings(1, &i_arg, bprm.page, bprm.p, 2);
 728                         bprm.argc++;
 729                 }
 730                 bprm.p = copy_strings(1, &i_name, bprm.page, bprm.p, 2);
 731                 bprm.argc++;
 732                 if (!bprm.p) {
 733                         retval = -E2BIG;
 734                         goto exec_error1;
 735                 }
 736                 /*
 737                  * OK, now restart the process with the interpreter's inode.
 738                  * Note that we use open_namei() as the name is now in kernel
 739                  * space, and we don't need to copy it.
 740                  */
 741                 retval = open_namei(interp, 0, 0, &bprm.inode, NULL);
 742                 if (retval)
 743                         goto exec_error1;
 744                 goto restart_interp;
 745         }
 746 #ifdef __alpha__
 747 /* handle /sbin/loader.. */
 748         if (!loader && (((struct exec *) bprm.buf)->fh.f_flags & 0x3000)) {
 749                 char * dynloader[] = { "/sbin/loader" };
 750                 iput(bprm.inode);
 751                 loader = 1;
 752                 bprm.p = copy_strings(1, dynloader, bprm.page, bprm.p, 2);
 753                 bprm.loader = bprm.p;
 754                 retval = open_namei(dynloader[0], 0, 0, &bprm.inode, NULL);
 755                 if (retval)
 756                         goto exec_error1;
 757                 goto restart_interp;
 758         }
 759 #endif
 760         if (!sh_bang) {
 761                 bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
 762                 bprm.exec = bprm.p;
 763                 bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
 764                 bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
 765                 if (!bprm.p) {
 766                         retval = -E2BIG;
 767                         goto exec_error2;
 768                 }
 769         }
 770 
 771         bprm.sh_bang = sh_bang;
 772         for (fmt = formats ; fmt ; fmt = fmt->next) {
 773                 int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
 774                 if (!fn)
 775                         break;
 776                 retval = fn(&bprm, regs);
 777                 if (retval >= 0) {
 778                         iput(bprm.inode);
 779                         current->did_exec = 1;
 780                         return retval;
 781                 }
 782                 if (retval != -ENOEXEC)
 783                         break;
 784         }
 785 exec_error2:
 786         iput(bprm.inode);
 787 exec_error1:
 788         for (i=0 ; i<MAX_ARG_PAGES ; i++)
 789                 free_page(bprm.page[i]);
 790         return(retval);
 791 }
 792 
 793 static void set_brk(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 794 {
 795         start = PAGE_ALIGN(start);
 796         end = PAGE_ALIGN(end);
 797         if (end <= start)
 798                 return;
 799         do_mmap(NULL, start, end - start,
 800                 PROT_READ | PROT_WRITE | PROT_EXEC,
 801                 MAP_FIXED | MAP_PRIVATE, 0);
 802 }
 803 
 804 /*
 805  * These are the functions used to load a.out style executables and shared
 806  * libraries.  There is no binary dependent code anywhere else.
 807  */
 808 
 809 static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 810 {
 811         struct exec ex;
 812         struct file * file;
 813         int fd;
 814         unsigned long error;
 815         unsigned long p = bprm->p;
 816         unsigned long fd_offset;
 817 
 818         ex = *((struct exec *) bprm->buf);              /* exec-header */
 819         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC && 
 820              N_MAGIC(ex) != QMAGIC) ||
 821             N_TRSIZE(ex) || N_DRSIZE(ex) ||
 822             bprm->inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 823                 return -ENOEXEC;
 824         }
 825 
 826         current->personality = PER_LINUX;
 827         fd_offset = N_TXTOFF(ex);
 828 
 829 #ifdef __i386__
 830         if (N_MAGIC(ex) == ZMAGIC && fd_offset != BLOCK_SIZE) {
 831                 printk(KERN_NOTICE "N_TXTOFF != BLOCK_SIZE. See a.out.h.\n");
 832                 return -ENOEXEC;
 833         }
 834 
 835         if (N_MAGIC(ex) == ZMAGIC && ex.a_text &&
 836             (fd_offset < bprm->inode->i_sb->s_blocksize)) {
 837                 printk(KERN_NOTICE "N_TXTOFF < BLOCK_SIZE. Please convert binary.\n");
 838                 return -ENOEXEC;
 839         }
 840 #endif
 841 
 842         /* OK, This is the point of no return */
 843         flush_old_exec(bprm);
 844 
 845         current->mm->end_code = ex.a_text +
 846                 (current->mm->start_code = N_TXTADDR(ex));
 847         current->mm->end_data = ex.a_data +
 848                 (current->mm->start_data = N_DATADDR(ex));
 849         current->mm->brk = ex.a_bss +
 850                 (current->mm->start_brk = N_BSSADDR(ex));
 851 
 852         current->mm->rss = 0;
 853         current->mm->mmap = NULL;
 854         current->suid = current->euid = current->fsuid = bprm->e_uid;
 855         current->sgid = current->egid = current->fsgid = bprm->e_gid;
 856         if (N_MAGIC(ex) == OMAGIC) {
 857 #ifdef __alpha__
 858                 do_mmap(NULL, N_TXTADDR(ex) & PAGE_MASK,
 859                         ex.a_text+ex.a_data + PAGE_SIZE - 1,
 860                         PROT_READ|PROT_WRITE|PROT_EXEC,
 861                         MAP_FIXED|MAP_PRIVATE, 0);
 862                 read_exec(bprm->inode, fd_offset, (char *) N_TXTADDR(ex),
 863                           ex.a_text+ex.a_data, 0);
 864 #else
 865                 do_mmap(NULL, 0, ex.a_text+ex.a_data,
 866                         PROT_READ|PROT_WRITE|PROT_EXEC,
 867                         MAP_FIXED|MAP_PRIVATE, 0);
 868                 read_exec(bprm->inode, 32, (char *) 0, ex.a_text+ex.a_data, 0);
 869 #endif
 870         } else {
 871                 if (ex.a_text & 0xfff || ex.a_data & 0xfff)
 872                         printk(KERN_NOTICE "executable not page aligned\n");
 873                 
 874                 fd = open_inode(bprm->inode, O_RDONLY);
 875                 
 876                 if (fd < 0)
 877                         return fd;
 878                 file = current->files->fd[fd];
 879                 if (!file->f_op || !file->f_op->mmap) {
 880                         sys_close(fd);
 881                         do_mmap(NULL, 0, ex.a_text+ex.a_data,
 882                                 PROT_READ|PROT_WRITE|PROT_EXEC,
 883                                 MAP_FIXED|MAP_PRIVATE, 0);
 884                         read_exec(bprm->inode, fd_offset,
 885                                   (char *) N_TXTADDR(ex), ex.a_text+ex.a_data, 0);
 886                         goto beyond_if;
 887                 }
 888 
 889                 error = do_mmap(file, N_TXTADDR(ex), ex.a_text,
 890                         PROT_READ | PROT_EXEC,
 891                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 892                         fd_offset);
 893 
 894                 if (error != N_TXTADDR(ex)) {
 895                         sys_close(fd);
 896                         send_sig(SIGKILL, current, 0);
 897                         return error;
 898                 }
 899                 
 900                 error = do_mmap(file, N_DATADDR(ex), ex.a_data,
 901                                 PROT_READ | PROT_WRITE | PROT_EXEC,
 902                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
 903                                 fd_offset + ex.a_text);
 904                 sys_close(fd);
 905                 if (error != N_DATADDR(ex)) {
 906                         send_sig(SIGKILL, current, 0);
 907                         return error;
 908                 }
 909         }
 910 beyond_if:
 911         if (current->exec_domain && current->exec_domain->use_count)
 912                 (*current->exec_domain->use_count)--;
 913         if (current->binfmt && current->binfmt->use_count)
 914                 (*current->binfmt->use_count)--;
 915         current->exec_domain = lookup_exec_domain(current->personality);
 916         current->binfmt = &aout_format;
 917         if (current->exec_domain && current->exec_domain->use_count)
 918                 (*current->exec_domain->use_count)++;
 919         if (current->binfmt && current->binfmt->use_count)
 920                 (*current->binfmt->use_count)++;
 921 
 922         set_brk(current->mm->start_brk, current->mm->brk);
 923 
 924         fd_offset = setup_arg_pages(ex.a_text,bprm->page) - MAX_ARG_PAGES*PAGE_SIZE;
 925         p += fd_offset;
 926         if (bprm->loader)
 927                 bprm->loader += fd_offset;
 928         bprm->exec += fd_offset;
 929         
 930         p = (unsigned long)create_tables((char *)p, bprm,
 931                                         current->personality != PER_LINUX);
 932         current->mm->start_stack = p;
 933 #ifdef __alpha__
 934         regs->gp = ex.a_gpvalue;
 935 #endif
 936         start_thread(regs, ex.a_entry, p);
 937         if (current->flags & PF_PTRACED)
 938                 send_sig(SIGTRAP, current, 0);
 939         return 0;
 940 }
 941 
 942 
 943 static int load_aout_library(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 944 {
 945         struct file * file;
 946         struct exec ex;
 947         struct  inode * inode;
 948         unsigned int len;
 949         unsigned int bss;
 950         unsigned int start_addr;
 951         unsigned long error;
 952         
 953         file = current->files->fd[fd];
 954         inode = file->f_inode;
 955         
 956         if (!file || !file->f_op)
 957                 return -EACCES;
 958 
 959         /* Seek into the file */
 960         if (file->f_op->lseek) {
 961                 if ((error = file->f_op->lseek(inode, file, 0, 0)) != 0)
 962                         return -ENOEXEC;
 963         } else
 964                 file->f_pos = 0;
 965 
 966         set_fs(KERNEL_DS);
 967         error = file->f_op->read(inode, file, (char *) &ex, sizeof(ex));
 968         set_fs(USER_DS);
 969         if (error != sizeof(ex))
 970                 return -ENOEXEC;
 971 
 972         /* We come in here for the regular a.out style of shared libraries */
 973         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
 974             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
 975             inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
 976                 return -ENOEXEC;
 977         }
 978         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) && 
 979             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
 980                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
 981                 return -ENOEXEC;
 982         }
 983         
 984         if (N_FLAGS(ex)) return -ENOEXEC;
 985 
 986         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
 987            this off to get the starting address for the page */
 988 
 989         start_addr =  ex.a_entry & 0xfffff000;
 990 
 991         /* Now use mmap to map the library into memory. */
 992         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
 993                         PROT_READ | PROT_WRITE | PROT_EXEC,
 994                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
 995                         N_TXTOFF(ex));
 996         if (error != start_addr)
 997                 return error;
 998         len = PAGE_ALIGN(ex.a_text + ex.a_data);
 999         bss = ex.a_text + ex.a_data + ex.a_bss;
1000         if (bss > len)
1001                 do_mmap(NULL, start_addr + len, bss-len,
1002                         PROT_READ|PROT_WRITE|PROT_EXEC,
1003                         MAP_PRIVATE|MAP_FIXED, 0);
1004         return 0;
1005 }

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