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

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