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

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