root/fs/exec.c

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

DEFINITIONS

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

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