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

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