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

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