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

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