1 /* This file handles the EXEC system call. It performs the work as follows: 2 * - see if the permissions allow the file to be executed 3 * - read the header and extract the sizes 4 * - fetch the initial args and environment from the user space 5 * - allocate the memory for the new process 6 * - copy the initial stack from PM to the process 7 * - read in the text and data segments and copy to the process 8 * - take care of setuid and setgid bits 9 * - fix up 'mproc' table 10 * - tell kernel about EXEC 11 * - save offset to initial argc (for ps) 12 * 13 * The entry points into this file are: 14 * pm_exec: perform the EXEC system call 15 */ 16 17 #include "fs.h" 18 #include <sys/stat.h> 19 #include <sys/mman.h> 20 #include <minix/callnr.h> 21 #include <minix/endpoint.h> 22 #include <minix/com.h> 23 #include <minix/u64.h> 24 #include <signal.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <sys/dirent.h> 28 #include <sys/exec.h> 29 #include <sys/param.h> 30 #include "path.h" 31 #include "vnode.h" 32 #include "file.h" 33 #include <minix/vfsif.h> 34 #include <machine/vmparam.h> 35 #include <assert.h> 36 #include <fcntl.h> 37 38 #define _KERNEL /* for ELF_AUX_ENTRIES */ 39 #include <libexec.h> 40 41 /* fields only used by elf and in VFS */ 42 struct vfs_exec_info { 43 struct exec_info args; /* libexec exec args */ 44 struct vnode *vp; /* Exec file's vnode */ 45 struct vmnt *vmp; /* Exec file's vmnt */ 46 struct stat sb; /* Exec file's stat structure */ 47 int userflags; /* exec() flags from userland */ 48 int is_dyn; /* Dynamically linked executable */ 49 int elf_main_fd; /* Dyn: FD of main program execuatble */ 50 char execname[PATH_MAX]; /* Full executable invocation */ 51 int vmfd; 52 int vmfd_used; 53 }; 54 55 static int patch_stack(struct vnode *vp, char stack[ARG_MAX], 56 size_t *stk_bytes, char path[PATH_MAX], vir_bytes *vsp); 57 static int is_script(struct vfs_exec_info *execi); 58 static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg, 59 vir_bytes *vsp, char replace); 60 static void clo_exec(struct fproc *rfp); 61 static int stack_prepare_elf(struct vfs_exec_info *execi, 62 char *curstack, size_t *frame_len, vir_bytes *vsp); 63 static int map_header(struct vfs_exec_info *execi); 64 static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes); 65 66 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */ 67 68 /* Array of loaders for different object file formats */ 69 typedef int (*exechook_t)(struct vfs_exec_info *execpackage); 70 typedef int (*stackhook_t)(struct vfs_exec_info *execi, char *curstack, 71 size_t *frame_len, vir_bytes *vsp); 72 struct exec_loaders { 73 libexec_exec_loadfunc_t load_object; /* load executable into memory */ 74 stackhook_t setup_stack; /* prepare stack before argc and argv push */ 75 }; 76 77 static const struct exec_loaders exec_loaders[] = { 78 { libexec_load_elf, stack_prepare_elf }, 79 { NULL, NULL } 80 }; 81 82 #define lock_exec() lock_proc(fproc_addr(VM_PROC_NR)) 83 #define unlock_exec() unlock_proc(fproc_addr(VM_PROC_NR)) 84 85 extern struct minix_kerninfo *_minix_kerninfo; 86 87 /*===========================================================================* 88 * get_read_vp * 89 *===========================================================================*/ 90 static int get_read_vp(struct vfs_exec_info *execi, 91 char *fullpath, int copyprogname, int sugid, struct lookup *resolve, struct fproc *fp) 92 { 93 /* Make the executable that we want to exec() into the binary pointed 94 * to by 'fullpath.' This function fills in necessary details in the execi 95 * structure, such as opened vnode. It unlocks and releases the vnode if 96 * it was already there. This makes it easy to change the executable 97 * during the exec(), which is often necessary, by calling this function 98 * more than once. This is specifically necessary when we discover the 99 * executable is actually a script or a dynamically linked executable. 100 */ 101 int r; 102 103 /* Caller wants to switch vp to the file in 'fullpath.' 104 * unlock and put it first if there is any there. 105 */ 106 if(execi->vp) { 107 unlock_vnode(execi->vp); 108 put_vnode(execi->vp); 109 execi->vp = NULL; 110 } 111 112 /* Remember/overwrite the executable name if requested. */ 113 if(copyprogname) { 114 char *cp = strrchr(fullpath, '/'); 115 if(cp) cp++; 116 else cp = fullpath; 117 strlcpy(execi->args.progname, cp, sizeof(execi->args.progname)); 118 execi->args.progname[sizeof(execi->args.progname)-1] = '\0'; 119 } 120 121 /* Open executable */ 122 if ((execi->vp = eat_path(resolve, fp)) == NULL) 123 return err_code; 124 125 unlock_vmnt(execi->vmp); 126 127 if (!S_ISREG(execi->vp->v_mode)) 128 return ENOEXEC; 129 else if ((r = forbidden(fp, execi->vp, X_BIT)) != OK) 130 return r; 131 else 132 r = req_stat(execi->vp->v_fs_e, execi->vp->v_inode_nr, 133 VFS_PROC_NR, (vir_bytes) &(execi->sb)); 134 135 if (r != OK) return r; 136 137 /* If caller wants us to, honour suid/guid mode bits. */ 138 if (sugid) { 139 /* Deal with setuid/setgid executables */ 140 if (execi->vp->v_mode & I_SET_UID_BIT) { 141 execi->args.new_uid = execi->vp->v_uid; 142 execi->args.allow_setuid = 1; 143 } 144 if (execi->vp->v_mode & I_SET_GID_BIT) { 145 execi->args.new_gid = execi->vp->v_gid; 146 execi->args.allow_setuid = 1; 147 } 148 } 149 150 /* Read in first chunk of file. */ 151 if((r=map_header(execi)) != OK) 152 return r; 153 154 return OK; 155 } 156 157 #define FAILCHECK(expr) if((r=(expr)) != OK) { goto pm_execfinal; } while(0) 158 #define Get_read_vp(e,f,p,s,rs,fp) do { \ 159 r=get_read_vp(&e,f,p,s,rs,fp); if(r != OK) { FAILCHECK(r); } \ 160 } while(0) 161 162 static int vfs_memmap(struct exec_info *execi, 163 vir_bytes vaddr, vir_bytes len, vir_bytes foffset, u16_t clearend, 164 int protflags) 165 { 166 struct vfs_exec_info *vi = (struct vfs_exec_info *) execi->opaque; 167 struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp; 168 int r; 169 u16_t flags = 0; 170 171 if(protflags & PROT_WRITE) 172 flags |= MVM_WRITABLE; 173 174 r = minix_vfs_mmap(execi->proc_e, foffset, len, 175 vp->v_dev, vp->v_inode_nr, vi->vmfd, vaddr, clearend, flags); 176 if(r == OK) { 177 vi->vmfd_used = 1; 178 } 179 180 return r; 181 } 182 183 /*===========================================================================* 184 * pm_exec * 185 *===========================================================================*/ 186 int pm_exec(vir_bytes path, size_t path_len, vir_bytes frame, size_t frame_len, 187 vir_bytes *pc, vir_bytes *newsp, vir_bytes *UNUSED(ps_str)) 188 { 189 /* Perform the execve(name, argv, envp) call. The user library builds a 190 * complete stack image, including pointers, args, environ, etc. The stack 191 * is copied to a buffer inside VFS, and then to the new core image. 192 * 193 * ps_str is not currently used, but may be if the ps_strings structure has to 194 * be moved to another location. 195 */ 196 int r; 197 vir_bytes vsp; 198 static char mbuf[ARG_MAX]; /* buffer for stack and zeroes */ 199 struct vfs_exec_info execi; 200 int i; 201 static char fullpath[PATH_MAX], 202 elf_interpreter[PATH_MAX], 203 firstexec[PATH_MAX], 204 finalexec[PATH_MAX]; 205 struct lookup resolve; 206 struct fproc *vmfp = fproc_addr(VM_PROC_NR); 207 stackhook_t makestack = NULL; 208 struct filp *newfilp = NULL; 209 210 lock_exec(); 211 212 /* unset execi values are 0. */ 213 memset(&execi, 0, sizeof(execi)); 214 execi.vmfd = -1; 215 216 /* passed from exec() libc code */ 217 execi.userflags = 0; 218 execi.args.stack_high = _minix_kerninfo->kinfo->user_sp; 219 execi.args.stack_size = DEFAULT_STACK_LIMIT; 220 221 fp->text_size = 0; 222 fp->data_size = 0; 223 224 lookup_init(&resolve, fullpath, PATH_NOFLAGS, &execi.vmp, &execi.vp); 225 226 resolve.l_vmnt_lock = VMNT_READ; 227 resolve.l_vnode_lock = VNODE_READ; 228 229 /* Fetch the stack from the user before destroying the old core image. */ 230 if (frame_len > ARG_MAX) 231 FAILCHECK(ENOMEM); /* stack too big */ 232 233 r = sys_datacopy_wrapper(fp->fp_endpoint, (vir_bytes) frame, SELF, (vir_bytes) mbuf, 234 (size_t) frame_len); 235 if (r != OK) { /* can't fetch stack (e.g. bad virtual addr) */ 236 printf("VFS: pm_exec: sys_datacopy failed\n"); 237 FAILCHECK(r); 238 } 239 240 /* Compute the current virtual stack pointer, has to be done before calling 241 * patch_stack, which needs it, and will adapt as required. */ 242 vsp = execi.args.stack_high - frame_len; 243 244 /* The default is to keep the original user and group IDs */ 245 execi.args.new_uid = fp->fp_effuid; 246 execi.args.new_gid = fp->fp_effgid; 247 248 /* Get the exec file name. */ 249 FAILCHECK(fetch_name(path, path_len, fullpath)); 250 strlcpy(finalexec, fullpath, PATH_MAX); 251 strlcpy(firstexec, fullpath, PATH_MAX); 252 253 /* Get_read_vp will return an opened vn in execi. 254 * if necessary it releases the existing vp so we can 255 * switch after we find out what's inside the file. 256 * It reads the start of the file. 257 */ 258 Get_read_vp(execi, fullpath, 1, 1, &resolve, fp); 259 260 /* If this is a script (i.e. has a #!/interpreter line), 261 * retrieve the name of the interpreter and open that 262 * executable instead. 263 */ 264 if(is_script(&execi)) { 265 /* patch_stack will add interpreter name and 266 * args to stack and retrieve the new binary 267 * name into fullpath. 268 */ 269 FAILCHECK(fetch_name(path, path_len, fullpath)); 270 FAILCHECK(patch_stack(execi.vp, mbuf, &frame_len, fullpath, &vsp)); 271 272 strlcpy(finalexec, fullpath, PATH_MAX); 273 strlcpy(firstexec, fullpath, PATH_MAX); 274 Get_read_vp(execi, fullpath, 1, 0, &resolve, fp); 275 } 276 277 /* If this is a dynamically linked executable, retrieve 278 * the name of that interpreter in elf_interpreter and open that 279 * executable instead. But open the current executable in an 280 * fd for the current process. 281 */ 282 if(elf_has_interpreter(execi.args.hdr, execi.args.hdr_len, 283 elf_interpreter, sizeof(elf_interpreter))) { 284 /* Switch the executable vnode to the interpreter */ 285 execi.is_dyn = 1; 286 287 /* The interpreter (loader) needs an fd to the main program, 288 * which is currently in finalexec 289 */ 290 if ((r = execi.elf_main_fd = 291 common_open(finalexec, O_RDONLY, 0, TRUE /*for_exec*/)) < 0) { 292 printf("VFS: exec: dynamic: open main exec failed %s (%d)\n", 293 fullpath, r); 294 FAILCHECK(r); 295 } 296 297 /* ld.so is linked at 0, but it can relocate itself; we 298 * want it higher to trap NULL pointer dereferences. 299 * Let's put it below the stack, and reserve 10MB for ld.so. 300 */ 301 execi.args.load_offset = 302 execi.args.stack_high - execi.args.stack_size - 0xa00000; 303 304 /* Remember it */ 305 strlcpy(execi.execname, finalexec, PATH_MAX); 306 307 /* The executable we need to execute first (loader) 308 * is in elf_interpreter, and has to be in fullpath to 309 * be looked up 310 */ 311 strlcpy(fullpath, elf_interpreter, PATH_MAX); 312 strlcpy(firstexec, elf_interpreter, PATH_MAX); 313 Get_read_vp(execi, fullpath, 0, 0, &resolve, fp); 314 } 315 316 /* We also want an FD for VM to mmap() the process in if possible. */ 317 { 318 struct vnode *vp = execi.vp; 319 assert(vp); 320 if ((vp->v_vmnt->m_fs_flags & RES_HASPEEK) && 321 major(vp->v_dev) != MEMORY_MAJOR) { 322 int newfd = -1; 323 if(get_fd(vmfp, 0, R_BIT, &newfd, &newfilp) == OK) { 324 assert(newfd >= 0 && newfd < OPEN_MAX); 325 assert(!vmfp->fp_filp[newfd]); 326 newfilp->filp_count = 1; 327 newfilp->filp_vno = vp; 328 newfilp->filp_flags = O_RDONLY; 329 vmfp->fp_filp[newfd] = newfilp; 330 /* dup_vnode(vp); */ 331 execi.vmfd = newfd; 332 execi.args.memmap = vfs_memmap; 333 } 334 } 335 } 336 337 /* callback functions and data */ 338 execi.args.copymem = read_seg; 339 execi.args.clearproc = libexec_clearproc_vm_procctl; 340 execi.args.clearmem = libexec_clear_sys_memset; 341 execi.args.allocmem_prealloc_cleared = libexec_alloc_mmap_prealloc_cleared; 342 execi.args.allocmem_prealloc_junk = libexec_alloc_mmap_prealloc_junk; 343 execi.args.allocmem_ondemand = libexec_alloc_mmap_ondemand; 344 execi.args.opaque = &execi; 345 346 execi.args.proc_e = fp->fp_endpoint; 347 execi.args.frame_len = frame_len; 348 execi.args.filesize = execi.vp->v_size; 349 350 for (i = 0; exec_loaders[i].load_object != NULL; i++) { 351 r = (*exec_loaders[i].load_object)(&execi.args); 352 /* Loaded successfully, so no need to try other loaders */ 353 if (r == OK) { makestack = exec_loaders[i].setup_stack; break; } 354 } 355 356 FAILCHECK(r); 357 358 /* Inform PM */ 359 FAILCHECK(libexec_pm_newexec(fp->fp_endpoint, &execi.args)); 360 361 /* Save off PC */ 362 *pc = execi.args.pc; 363 364 /* call a stack-setup function if this executable type wants it */ 365 if(makestack) FAILCHECK(makestack(&execi, mbuf, &frame_len, &vsp)); 366 367 /* Copy the stack from VFS to new core image. */ 368 FAILCHECK(sys_datacopy_wrapper(SELF, (vir_bytes) mbuf, fp->fp_endpoint, 369 (vir_bytes) vsp, (phys_bytes)frame_len)); 370 371 /* Return new stack pointer to caller */ 372 *newsp = vsp; 373 374 clo_exec(fp); 375 376 if (execi.args.allow_setuid) { 377 /* If after loading the image we're still allowed to run with 378 * setuid or setgid, change credentials now */ 379 fp->fp_effuid = execi.args.new_uid; 380 fp->fp_effgid = execi.args.new_gid; 381 } 382 383 /* Remember the new name of the process */ 384 strlcpy(fp->fp_name, execi.args.progname, PROC_NAME_LEN); 385 fp->text_size = execi.args.text_size; 386 fp->data_size = execi.args.data_size; 387 388 pm_execfinal: 389 if(newfilp) unlock_filp(newfilp); 390 else if (execi.vp != NULL) { 391 unlock_vnode(execi.vp); 392 put_vnode(execi.vp); 393 } 394 395 if(execi.vmfd >= 0 && !execi.vmfd_used) { 396 if(OK != close_fd(vmfp, execi.vmfd)) { 397 printf("VFS: unexpected close fail of vm fd\n"); 398 } 399 } 400 401 unlock_exec(); 402 403 return(r); 404 } 405 406 /* This is a copy-paste of the same macro in minix/lib/libc/sys/stack_utils.c. 407 * Keep it synchronized. */ 408 #define STACK_MIN_SZ \ 409 ( \ 410 sizeof(int) + sizeof(void *) * 2 + \ 411 sizeof(AuxInfo) * PMEF_AUXVECTORS + PMEF_EXECNAMELEN1 + \ 412 sizeof(struct ps_strings) \ 413 ) 414 415 static int stack_prepare_elf(struct vfs_exec_info *execi, char *frame, size_t *frame_size, 416 vir_bytes *vsp) 417 { 418 AuxInfo *aux_vec, *aux_vec_end; 419 vir_bytes vap; /* Address in proc space of the first AuxVec. */ 420 Elf_Ehdr const * const elf_header = (Elf_Ehdr *) execi->args.hdr; 421 struct ps_strings const * const psp = (struct ps_strings *) 422 (frame + (*frame_size - sizeof(struct ps_strings))); 423 424 size_t const execname_len = strlen(execi->execname); 425 426 if (!execi->is_dyn) 427 return OK; 428 429 if (execi->args.hdr_len < sizeof(*elf_header)) { 430 printf("VFS: malformed ELF headers for exec\n"); 431 return ENOEXEC; 432 } 433 434 if (*frame_size < STACK_MIN_SZ) { 435 printf("VFS: malformed stack for exec(), smaller than minimum" 436 " possible size.\n"); 437 return ENOEXEC; 438 } 439 440 /* Find first Aux vector in the stack frame. */ 441 vap = (vir_bytes)(psp->ps_envstr + (psp->ps_nenvstr + 1)); 442 aux_vec = (AuxInfo *) (frame + (vap - *vsp)); 443 aux_vec_end = aux_vec + PMEF_AUXVECTORS; 444 445 if (((char *)aux_vec < frame) || 446 ((char *)aux_vec > (frame + *frame_size))) { 447 printf("VFS: malformed stack for exec(), first AuxVector is" 448 " not on the stack.\n"); 449 return ENOEXEC; 450 } 451 452 if (((char *)aux_vec_end < frame) || 453 ((char *)aux_vec_end > (frame + *frame_size))) { 454 printf("VFS: malformed stack for exec(), last AuxVector is" 455 " not on the stack.\n"); 456 return ENOEXEC; 457 } 458 459 /* Userland provides a fully filled stack frame, with argc, argv, envp 460 * and then all the argv and envp strings; consistent with ELF ABI, 461 * except for a list of Aux vectors that should be between envp points 462 * and the start of the strings. 463 * 464 * It would take some very unpleasant hackery to insert the aux vectors 465 * before the strings, and correct all the pointers, so the exec code 466 * in libc makes space for us. 467 */ 468 469 #define AUXINFO(a, type, value) \ 470 do { \ 471 if (a < aux_vec_end) { \ 472 a->a_type = type; \ 473 a->a_v = value; \ 474 a++; \ 475 } else { \ 476 printf("VFS: No more room for ELF AuxVec type %d, skipping it for %s\n", type, execi->execname); \ 477 (aux_vec_end - 1)->a_type = AT_NULL; \ 478 (aux_vec_end - 1)->a_v = 0; \ 479 } \ 480 } while(0) 481 482 AUXINFO(aux_vec, AT_BASE, execi->args.load_base); 483 AUXINFO(aux_vec, AT_ENTRY, execi->args.pc); 484 AUXINFO(aux_vec, AT_EXECFD, execi->elf_main_fd); 485 #if 0 486 AUXINFO(aux_vec, AT_PHDR, XXX ); /* should be &phdr[0] */ 487 AUXINFO(aux_vec, AT_PHENT, elf_header->e_phentsize); 488 AUXINFO(aux_vec, AT_PHNUM, elf_header->e_phnum); 489 490 AUXINFO(aux_vec, AT_RUID, XXX); 491 AUXINFO(aux_vec, AT_RGID, XXX); 492 #endif 493 AUXINFO(aux_vec, AT_EUID, execi->args.new_uid); 494 AUXINFO(aux_vec, AT_EGID, execi->args.new_gid); 495 AUXINFO(aux_vec, AT_PAGESZ, PAGE_SIZE); 496 497 if(execname_len < PMEF_EXECNAMELEN1) { 498 char *spacestart; 499 vir_bytes userp; 500 501 /* Empty space starts after aux_vec table; we can put the name 502 * here. */ 503 spacestart = (char *) aux_vec + 2 * sizeof(AuxInfo); 504 strlcpy(spacestart, execi->execname, PMEF_EXECNAMELEN1); 505 memset(spacestart + execname_len, '\0', 506 PMEF_EXECNAMELEN1 - execname_len); 507 508 /* What will the address of the string for the user be */ 509 userp = *vsp + (spacestart - frame); 510 511 /* Move back to where the AT_NULL is */ 512 AUXINFO(aux_vec, AT_SUN_EXECNAME, userp); 513 } 514 515 /* Always terminate with AT_NULL */ 516 AUXINFO(aux_vec, AT_NULL, 0); 517 518 return OK; 519 } 520 521 /*===========================================================================* 522 * is_script * 523 *===========================================================================*/ 524 static int is_script(struct vfs_exec_info *execi) 525 { 526 /* Is Interpreted script? */ 527 assert(execi->args.hdr != NULL); 528 529 return(execi->args.hdr[0] == '#' && execi->args.hdr[1] == '!' 530 && execi->args.hdr_len >= 2); 531 } 532 533 /*===========================================================================* 534 * patch_stack * 535 *===========================================================================*/ 536 static int patch_stack(vp, stack, stk_bytes, path, vsp) 537 struct vnode *vp; /* pointer for open script file */ 538 char stack[ARG_MAX]; /* pointer to stack image within VFS */ 539 size_t *stk_bytes; /* size of initial stack */ 540 char path[PATH_MAX]; /* path to script file */ 541 vir_bytes *vsp; 542 { 543 /* Patch the argument vector to include the path name of the script to be 544 * interpreted, and all strings on the #! line. Returns the path name of 545 * the interpreter. 546 */ 547 enum { INSERT=FALSE, REPLACE=TRUE }; 548 int n, r; 549 off_t pos, new_pos; 550 char *sp, *interp = NULL; 551 size_t cum_io; 552 char buf[PAGE_SIZE]; 553 554 /* Make 'path' the new argv[0]. */ 555 if (!insert_arg(stack, stk_bytes, path, vsp, REPLACE)) return(ENOMEM); 556 557 pos = 0; /* Read from the start of the file */ 558 559 /* Issue request */ 560 r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, pos, READING, VFS_PROC_NR, 561 (vir_bytes) buf, sizeof(buf), &new_pos, &cum_io); 562 563 if (r != OK) return(r); 564 565 n = vp->v_size; 566 if (n > sizeof(buf)) 567 n = sizeof(buf); 568 if (n < 2) return ENOEXEC; 569 570 sp = &(buf[2]); /* just behind the #! */ 571 n -= 2; 572 if (n > PATH_MAX) n = PATH_MAX; 573 574 /* Use the 'path' variable for temporary storage */ 575 memcpy(path, sp, n); 576 577 if ((sp = memchr(path, '\n', n)) == NULL) /* must be a proper line */ 578 return(ENOEXEC); 579 580 /* Move sp backwards through script[], prepending each string to stack. */ 581 for (;;) { 582 /* skip spaces behind argument. */ 583 while (sp > path && (*--sp == ' ' || *sp == '\t')) {} 584 if (sp == path) break; 585 586 sp[1] = 0; 587 /* Move to the start of the argument. */ 588 while (sp > path && sp[-1] != ' ' && sp[-1] != '\t') --sp; 589 590 interp = sp; 591 if (!insert_arg(stack, stk_bytes, sp, vsp, INSERT)) { 592 printf("VFS: patch_stack: insert_arg failed\n"); 593 return(ENOMEM); 594 } 595 } 596 597 if(!interp) 598 return ENOEXEC; 599 600 if (interp != path) 601 memmove(path, interp, strlen(interp)+1); 602 603 return(OK); 604 } 605 606 /*===========================================================================* 607 * insert_arg * 608 *===========================================================================*/ 609 static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg, 610 vir_bytes *vsp, char replace) 611 { 612 /* Patch the stack so that arg will become argv[0]. Be careful, the 613 * stack may be filled with garbage, although it normally looks like 614 * this: 615 * nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL 616 * followed by the strings "pointed" to by the argv[i] and the envp[i]. 617 * The * pointers are in the new process address space. 618 * 619 * Return true iff the operation succeeded. 620 */ 621 struct ps_strings *psp; 622 int offset; 623 size_t old_bytes = *stk_bytes; 624 625 int const arg_len = strlen(arg) + 1; 626 627 /* Offset to argv[0][0] in the stack frame. */ 628 int const a0 = (int)(((char **)stack)[1] - *vsp); 629 630 /* Check that argv[0] points within the stack frame. */ 631 if ((a0 < 0) || (a0 >= old_bytes)) { 632 printf("vfs:: argv[0][] not within stack range!! %i\n", a0); 633 return FALSE; 634 } 635 636 if (!replace) { 637 /* Prepending arg adds one pointer, one string and a zero byte. */ 638 offset = arg_len + PTRSIZE; 639 } else { 640 /* replacing argv[0] with arg adds the difference in length of 641 * the two strings. Make sure we don't go beyond the stack size 642 * when computing the length of the current argv[0]. */ 643 offset = arg_len - strnlen(stack + a0, ARG_MAX - a0 - 1); 644 } 645 646 /* As ps_strings follows the strings, ensure the offset is word aligned. */ 647 offset = offset + (PTRSIZE - ((PTRSIZE + offset) % PTRSIZE)); 648 649 /* The stack will grow (or shrink) by offset bytes. */ 650 if ((*stk_bytes += offset) > ARG_MAX) { 651 printf("vfs:: offset too big!! %zu (max %d)\n", *stk_bytes, 652 ARG_MAX); 653 return FALSE; 654 } 655 656 /* Reposition the strings by offset bytes */ 657 memmove(stack + a0 + offset, stack + a0, old_bytes - a0); 658 659 /* Put arg in the new space, leaving padding in front of it. */ 660 strlcpy(stack + a0 + offset - arg_len, arg, arg_len); 661 662 if (!replace) { 663 /* Make space for a new argv[0]. */ 664 memmove(stack + 2 * PTRSIZE, 665 stack + 1 * PTRSIZE, a0 - 2 * PTRSIZE); 666 667 ((char **) stack)[0]++; /* nargs++; */ 668 } 669 670 /* set argv[0] correctly */ 671 ((char **) stack)[1] = (char *) a0 - arg_len + *vsp; 672 673 /* Update stack pointer in the process address space. */ 674 *vsp -= offset; 675 676 /* Update argv and envp in ps_strings */ 677 psp = (struct ps_strings *) (stack + *stk_bytes - sizeof(struct ps_strings)); 678 psp->ps_argvstr -= (offset / PTRSIZE); 679 if (!replace) { 680 psp->ps_nargvstr++; 681 } 682 psp->ps_envstr = psp->ps_argvstr + psp->ps_nargvstr + 1; 683 684 return TRUE; 685 } 686 687 /*===========================================================================* 688 * read_seg * 689 *===========================================================================*/ 690 static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes) 691 { 692 /* 693 * The byte count on read is usually smaller than the segment count, because 694 * a segment is padded out to a click multiple, and the data segment is only 695 * partially initialized. 696 */ 697 int r; 698 off_t new_pos; 699 size_t cum_io; 700 struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp; 701 702 /* Make sure that the file is big enough */ 703 if (off + seg_bytes > LONG_MAX) return(EIO); 704 if ((unsigned long) vp->v_size < off+seg_bytes) return(EIO); 705 706 if ((r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, off, READING, 707 execi->proc_e, (vir_bytes) seg_addr, seg_bytes, 708 &new_pos, &cum_io)) != OK) { 709 printf("VFS: read_seg: req_readwrite failed (data)\n"); 710 return(r); 711 } 712 713 if (r == OK && cum_io != seg_bytes) 714 printf("VFS: read_seg segment has not been read properly\n"); 715 716 return(r); 717 } 718 719 720 /*===========================================================================* 721 * clo_exec * 722 *===========================================================================*/ 723 static void clo_exec(struct fproc *rfp) 724 { 725 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec). 726 */ 727 int i; 728 729 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */ 730 for (i = 0; i < OPEN_MAX; i++) 731 if ( FD_ISSET(i, &rfp->fp_cloexec_set)) 732 (void) close_fd(rfp, i); 733 } 734 735 /*===========================================================================* 736 * map_header * 737 *===========================================================================*/ 738 static int map_header(struct vfs_exec_info *execi) 739 { 740 int r; 741 size_t cum_io; 742 off_t pos, new_pos; 743 static char hdr[PAGE_SIZE]; /* Assume that header is not larger than a page */ 744 745 pos = 0; /* Read from the start of the file */ 746 747 /* How much is sensible to read */ 748 execi->args.hdr_len = MIN(execi->vp->v_size, sizeof(hdr)); 749 execi->args.hdr = hdr; 750 751 r = req_readwrite(execi->vp->v_fs_e, execi->vp->v_inode_nr, 752 pos, READING, VFS_PROC_NR, (vir_bytes) hdr, 753 execi->args.hdr_len, &new_pos, &cum_io); 754 if (r != OK) { 755 printf("VFS: exec: map_header: req_readwrite failed\n"); 756 return(r); 757 } 758 759 return(OK); 760 } 761