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

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