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

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