1 /* $OpenBSD: kern_exec.c,v 1.80 2003/06/21 00:42:58 tedu Exp $ */ 2 /* $NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $ */ 3 4 /*- 5 * Copyright (C) 1993, 1994 Christopher G. Demetriou 6 * Copyright (C) 1992 Wolfgang Solfrank. 7 * Copyright (C) 1992 TooLs GmbH. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/filedesc.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/mount.h> 42 #include <sys/malloc.h> 43 #include <sys/namei.h> 44 #include <sys/vnode.h> 45 #include <sys/file.h> 46 #include <sys/acct.h> 47 #include <sys/exec.h> 48 #include <sys/ktrace.h> 49 #include <sys/resourcevar.h> 50 #include <sys/wait.h> 51 #include <sys/mman.h> 52 #include <sys/signalvar.h> 53 #include <sys/stat.h> 54 #include <sys/conf.h> 55 #ifdef SYSVSHM 56 #include <sys/shm.h> 57 #endif 58 59 #include <sys/syscallargs.h> 60 61 #include <uvm/uvm_extern.h> 62 63 #include <machine/cpu.h> 64 #include <machine/reg.h> 65 66 #include <dev/rndvar.h> 67 68 /* 69 * Map the shared signal code. 70 */ 71 int exec_sigcode_map(struct proc *, struct emul *); 72 73 /* 74 * stackgap_random specifies if the stackgap should have a random size added 75 * to it. Must be a n^2. If non-zero, the stack gap will be calculated as: 76 * (arc4random() * ALIGNBYTES) & (stackgap_random - 1) + STACKGAPLEN. 77 */ 78 int stackgap_random = 64*1024; 79 80 /* 81 * check exec: 82 * given an "executable" described in the exec package's namei info, 83 * see what we can do with it. 84 * 85 * ON ENTRY: 86 * exec package with appropriate namei info 87 * proc pointer of exec'ing proc 88 * NO SELF-LOCKED VNODES 89 * 90 * ON EXIT: 91 * error: nothing held, etc. exec header still allocated. 92 * ok: filled exec package, one locked vnode. 93 * 94 * EXEC SWITCH ENTRY: 95 * Locked vnode to check, exec package, proc. 96 * 97 * EXEC SWITCH EXIT: 98 * ok: return 0, filled exec package, one locked vnode. 99 * error: destructive: 100 * everything deallocated execept exec header. 101 * non-destructive: 102 * error code, locked vnode, exec header unmodified 103 */ 104 int 105 check_exec(p, epp) 106 struct proc *p; 107 struct exec_package *epp; 108 { 109 int error, i; 110 struct vnode *vp; 111 struct nameidata *ndp; 112 size_t resid; 113 114 ndp = epp->ep_ndp; 115 ndp->ni_cnd.cn_nameiop = LOOKUP; 116 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME; 117 /* first get the vnode */ 118 if ((error = namei(ndp)) != 0) 119 return (error); 120 epp->ep_vp = vp = ndp->ni_vp; 121 122 /* check for regular file */ 123 if (vp->v_type == VDIR) { 124 error = EISDIR; 125 goto bad1; 126 } 127 if (vp->v_type != VREG) { 128 error = EACCES; 129 goto bad1; 130 } 131 132 /* get attributes */ 133 if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0) 134 goto bad1; 135 136 /* Check mount point */ 137 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 138 error = EACCES; 139 goto bad1; 140 } 141 142 if ((vp->v_mount->mnt_flag & MNT_NOSUID)) 143 epp->ep_vap->va_mode &= ~(VSUID | VSGID); 144 145 /* check access. for root we have to see if any exec bit on */ 146 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 147 goto bad1; 148 if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 149 error = EACCES; 150 goto bad1; 151 } 152 153 /* try to open it */ 154 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0) 155 goto bad1; 156 157 /* unlock vp, we need it unlocked from here */ 158 VOP_UNLOCK(vp, 0, p); 159 160 /* now we have the file, get the exec header */ 161 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0, 162 UIO_SYSSPACE, 0, p->p_ucred, &resid, p); 163 if (error) 164 goto bad2; 165 epp->ep_hdrvalid = epp->ep_hdrlen - resid; 166 167 /* 168 * set up the vmcmds for creation of the process 169 * address space 170 */ 171 error = ENOEXEC; 172 for (i = 0; i < nexecs && error != 0; i++) { 173 int newerror; 174 175 if (execsw[i].es_check == NULL) 176 continue; 177 newerror = (*execsw[i].es_check)(p, epp); 178 /* make sure the first "interesting" error code is saved. */ 179 if (!newerror || error == ENOEXEC) 180 error = newerror; 181 if (epp->ep_flags & EXEC_DESTR && error != 0) 182 return (error); 183 } 184 if (!error) { 185 /* check that entry point is sane */ 186 if (epp->ep_entry > VM_MAXUSER_ADDRESS) { 187 error = ENOEXEC; 188 } 189 190 /* check limits */ 191 if ((epp->ep_tsize > MAXTSIZ) || 192 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur)) 193 error = ENOMEM; 194 195 if (!error) 196 return (0); 197 } 198 199 /* 200 * free any vmspace-creation commands, 201 * and release their references 202 */ 203 kill_vmcmds(&epp->ep_vmcmds); 204 205 bad2: 206 /* 207 * close the vnode, free the pathname buf, and punt. 208 */ 209 vn_close(vp, FREAD, p->p_ucred, p); 210 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 211 return (error); 212 213 bad1: 214 /* 215 * free the namei pathname buffer, and put the vnode 216 * (which we don't yet have open). 217 */ 218 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 219 vput(vp); 220 return (error); 221 } 222 223 /* 224 * exec system call 225 */ 226 /* ARGSUSED */ 227 int 228 sys_execve(p, v, retval) 229 register struct proc *p; 230 void *v; 231 register_t *retval; 232 { 233 struct sys_execve_args /* { 234 syscallarg(char *) path; 235 syscallarg(char * *) argp; 236 syscallarg(char * *) envp; 237 } */ *uap = v; 238 int error, i; 239 struct exec_package pack; 240 struct nameidata nid; 241 struct vattr attr; 242 struct ucred *cred = p->p_ucred; 243 char *argp; 244 char * const *cpp, *dp, *sp; 245 long argc, envc; 246 size_t len, sgap; 247 #ifdef MACHINE_STACK_GROWS_UP 248 size_t slen; 249 #endif 250 char *stack; 251 struct ps_strings arginfo; 252 struct vmspace *vm = p->p_vmspace; 253 char **tmpfap; 254 extern struct emul emul_native; 255 256 /* 257 * Cheap solution to complicated problems. 258 * Mark this process as "leave me alone, I'm execing". 259 */ 260 p->p_flag |= P_INEXEC; 261 262 /* 263 * figure out the maximum size of an exec header, if necessary. 264 * XXX should be able to keep LKM code from modifying exec switch 265 * when we're still using it, but... 266 */ 267 if (exec_maxhdrsz == 0) { 268 for (i = 0; i < nexecs; i++) 269 if (execsw[i].es_check != NULL 270 && execsw[i].es_hdrsz > exec_maxhdrsz) 271 exec_maxhdrsz = execsw[i].es_hdrsz; 272 } 273 274 /* init the namei data to point the file user's program name */ 275 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 276 277 /* 278 * initialize the fields of the exec package. 279 */ 280 pack.ep_name = (char *)SCARG(uap, path); 281 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK); 282 pack.ep_hdrlen = exec_maxhdrsz; 283 pack.ep_hdrvalid = 0; 284 pack.ep_ndp = &nid; 285 pack.ep_emul_arg = NULL; 286 VMCMDSET_INIT(&pack.ep_vmcmds); 287 pack.ep_vap = &attr; 288 pack.ep_emul = &emul_native; 289 pack.ep_flags = 0; 290 291 /* see if we can run it. */ 292 if ((error = check_exec(p, &pack)) != 0) { 293 goto freehdr; 294 } 295 296 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 297 298 /* allocate an argument buffer */ 299 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS); 300 #ifdef DIAGNOSTIC 301 if (argp == NULL) 302 panic("execve: argp == NULL"); 303 #endif 304 dp = argp; 305 argc = 0; 306 307 /* copy the fake args list, if there's one, freeing it as we go */ 308 if (pack.ep_flags & EXEC_HASARGL) { 309 tmpfap = pack.ep_fa; 310 while (*tmpfap != NULL) { 311 char *cp; 312 313 cp = *tmpfap; 314 while (*cp) 315 *dp++ = *cp++; 316 dp++; 317 318 free(*tmpfap, M_EXEC); 319 tmpfap++; argc++; 320 } 321 FREE(pack.ep_fa, M_EXEC); 322 pack.ep_flags &= ~EXEC_HASARGL; 323 } 324 325 /* Now get argv & environment */ 326 if (!(cpp = SCARG(uap, argp))) { 327 error = EFAULT; 328 goto bad; 329 } 330 331 if (pack.ep_flags & EXEC_SKIPARG) 332 cpp++; 333 334 while (1) { 335 len = argp + ARG_MAX - dp; 336 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 337 goto bad; 338 if (!sp) 339 break; 340 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 341 if (error == ENAMETOOLONG) 342 error = E2BIG; 343 goto bad; 344 } 345 dp += len; 346 cpp++; 347 argc++; 348 } 349 350 envc = 0; 351 /* environment does not need to be there */ 352 if ((cpp = SCARG(uap, envp)) != NULL ) { 353 while (1) { 354 len = argp + ARG_MAX - dp; 355 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 356 goto bad; 357 if (!sp) 358 break; 359 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 360 if (error == ENAMETOOLONG) 361 error = E2BIG; 362 goto bad; 363 } 364 dp += len; 365 cpp++; 366 envc++; 367 } 368 } 369 370 dp = (char *)ALIGN(dp); 371 372 sgap = STACKGAPLEN; 373 if (stackgap_random != 0) 374 sgap += (arc4random() * ALIGNBYTES) & (stackgap_random - 1); 375 /* Now check if args & environ fit into new stack */ 376 len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) + 377 sizeof(long) + dp + sgap + sizeof(struct ps_strings)) - argp; 378 379 len = ALIGN(len); /* make the stack "safely" aligned */ 380 381 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 382 error = ENOMEM; 383 goto bad; 384 } 385 386 /* adjust "active stack depth" for process VSZ */ 387 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 388 389 /* 390 * Prepare vmspace for remapping. Note that uvmspace_exec can replace 391 * p_vmspace! 392 */ 393 uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 394 395 vm = p->p_vmspace; 396 /* Now map address space */ 397 vm->vm_taddr = (char *)pack.ep_taddr; 398 vm->vm_tsize = btoc(pack.ep_tsize); 399 vm->vm_daddr = (char *)pack.ep_daddr; 400 vm->vm_dsize = btoc(pack.ep_dsize); 401 vm->vm_ssize = btoc(pack.ep_ssize); 402 vm->vm_maxsaddr = (char *)pack.ep_maxsaddr; 403 404 /* create the new process's VM space by running the vmcmds */ 405 #ifdef DIAGNOSTIC 406 if (pack.ep_vmcmds.evs_used == 0) 407 panic("execve: no vmcmds"); 408 #endif 409 error = exec_process_vmcmds(p, &pack); 410 411 /* if an error happened, deallocate and punt */ 412 if (error) 413 goto exec_abort; 414 415 /* remember information about the process */ 416 arginfo.ps_nargvstr = argc; 417 arginfo.ps_nenvstr = envc; 418 419 #ifdef MACHINE_STACK_GROWS_UP 420 stack = (char *)USRSTACK + sizeof(arginfo); 421 slen = len - sizeof(arginfo); 422 #else 423 stack = (char *)(USRSTACK - len); 424 #endif 425 /* Now copy argc, args & environ to new stack */ 426 if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp)) 427 goto exec_abort; 428 429 /* copy out the process's ps_strings structure */ 430 if (copyout(&arginfo, (char *)PS_STRINGS, sizeof(arginfo))) 431 goto exec_abort; 432 433 stopprofclock(p); /* stop profiling */ 434 fdcloseexec(p); /* handle close on exec */ 435 execsigs(p); /* reset catched signals */ 436 437 /* set command name & other accounting info */ 438 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 439 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len); 440 p->p_comm[len] = 0; 441 p->p_acflag &= ~AFORK; 442 443 /* record proc's vnode, for use by procfs and others */ 444 if (p->p_textvp) 445 vrele(p->p_textvp); 446 VREF(pack.ep_vp); 447 p->p_textvp = pack.ep_vp; 448 449 p->p_flag |= P_EXEC; 450 if (p->p_flag & P_PPWAIT) { 451 p->p_flag &= ~P_PPWAIT; 452 wakeup((caddr_t)p->p_pptr); 453 } 454 455 /* 456 * If process does execve() while it has a mismatched real, 457 * effective, or saved uid/gid, we set P_SUGIDEXEC. 458 */ 459 if (p->p_ucred->cr_uid != p->p_cred->p_ruid || 460 p->p_ucred->cr_uid != p->p_cred->p_svuid || 461 p->p_ucred->cr_gid != p->p_cred->p_rgid || 462 p->p_ucred->cr_gid != p->p_cred->p_svgid) 463 p->p_flag |= P_SUGIDEXEC; 464 else 465 p->p_flag &= ~P_SUGIDEXEC; 466 467 /* 468 * deal with set[ug]id. 469 * MNT_NOEXEC has already been used to disable s[ug]id. 470 */ 471 if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) { 472 int i; 473 474 p->p_flag |= P_SUGID; 475 p->p_flag |= P_SUGIDEXEC; 476 477 #ifdef KTRACE 478 /* 479 * If process is being ktraced, turn off - unless 480 * root set it. 481 */ 482 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) { 483 p->p_traceflag = 0; 484 ktrsettracevnode(p, NULL); 485 } 486 #endif 487 p->p_ucred = crcopy(cred); 488 if (attr.va_mode & VSUID) 489 p->p_ucred->cr_uid = attr.va_uid; 490 if (attr.va_mode & VSGID) 491 p->p_ucred->cr_gid = attr.va_gid; 492 493 /* 494 * For set[ug]id processes, a few caveats apply to 495 * stdin, stdout, and stderr. 496 */ 497 for (i = 0; i < 3; i++) { 498 struct file *fp = NULL; 499 500 /* 501 * NOTE - This will never return NULL because of 502 * unmature fds. The file descriptor table is not 503 * shared because we're suid. 504 */ 505 fp = fd_getfile(p->p_fd, i); 506 #ifdef PROCFS 507 /* 508 * Close descriptors that are writing to procfs. 509 */ 510 if (fp && fp->f_type == DTYPE_VNODE && 511 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS && 512 (fp->f_flag & FWRITE)) { 513 fdrelease(p, i); 514 fp = NULL; 515 } 516 #endif 517 518 /* 519 * Ensure that stdin, stdout, and stderr are already 520 * allocated. We do not want userland to accidentally 521 * allocate descriptors in this range which has implied 522 * meaning to libc. 523 */ 524 if (fp == NULL) { 525 short flags = FREAD | (i == 0 ? 0 : FWRITE); 526 struct vnode *vp; 527 int indx; 528 529 if ((error = falloc(p, &fp, &indx)) != 0) 530 goto exec_abort; 531 #ifdef DIAGNOSTIC 532 if (indx != i) 533 panic("sys_execve: falloc indx != i"); 534 #endif 535 if ((error = cdevvp(getnulldev(), &vp)) != 0) { 536 fdremove(p->p_fd, indx); 537 closef(fp, p); 538 goto exec_abort; 539 } 540 if ((error = VOP_OPEN(vp, flags, p->p_ucred, p)) != 0) { 541 fdremove(p->p_fd, indx); 542 closef(fp, p); 543 vrele(vp); 544 goto exec_abort; 545 } 546 if (flags & FWRITE) 547 vp->v_writecount++; 548 fp->f_flag = flags; 549 fp->f_type = DTYPE_VNODE; 550 fp->f_ops = &vnops; 551 fp->f_data = (caddr_t)vp; 552 FILE_SET_MATURE(fp); 553 } 554 } 555 } else 556 p->p_flag &= ~P_SUGID; 557 p->p_cred->p_svuid = p->p_ucred->cr_uid; 558 p->p_cred->p_svgid = p->p_ucred->cr_gid; 559 560 if (p->p_flag & P_SUGIDEXEC) { 561 int i, s = splclock(); 562 563 timeout_del(&p->p_realit_to); 564 timerclear(&p->p_realtimer.it_interval); 565 timerclear(&p->p_realtimer.it_value); 566 for (i = 0; i < sizeof(p->p_stats->p_timer) / 567 sizeof(p->p_stats->p_timer[0]); i++) { 568 timerclear(&p->p_stats->p_timer[i].it_interval); 569 timerclear(&p->p_stats->p_timer[i].it_value); 570 } 571 splx(s); 572 } 573 574 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 575 576 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 577 vn_close(pack.ep_vp, FREAD, cred, p); 578 579 /* 580 * notify others that we exec'd 581 */ 582 KNOTE(&p->p_klist, NOTE_EXEC); 583 584 /* setup new registers and do misc. setup. */ 585 if (pack.ep_emul->e_fixup != NULL) { 586 if ((*pack.ep_emul->e_fixup)(p, &pack) != 0) 587 goto free_pack_abort; 588 } 589 #ifdef MACHINE_STACK_GROWS_UP 590 (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval); 591 #else 592 (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval); 593 #endif 594 595 /* map the process's signal trampoline code */ 596 if (exec_sigcode_map(p, pack.ep_emul)) 597 goto exec_abort; 598 599 if (p->p_flag & P_TRACED) 600 psignal(p, SIGTRAP); 601 602 free(pack.ep_hdr, M_EXEC); 603 604 /* 605 * Call emulation specific exec hook. This can setup per-process 606 * p->p_emuldata or do any other per-process stuff an emulation needs. 607 * 608 * If we are executing process of different emulation than the 609 * original forked process, call e_proc_exit() of the old emulation 610 * first, then e_proc_exec() of new emulation. If the emulation is 611 * same, the exec hook code should deallocate any old emulation 612 * resources held previously by this process. 613 */ 614 if (p->p_emul && p->p_emul->e_proc_exit && 615 p->p_emul != pack.ep_emul) 616 (*p->p_emul->e_proc_exit)(p); 617 618 /* 619 * Call exec hook. Emulation code may NOT store reference to anything 620 * from &pack. 621 */ 622 if (pack.ep_emul->e_proc_exec) 623 (*pack.ep_emul->e_proc_exec)(p, &pack); 624 625 /* update p_emul, the old value is no longer needed */ 626 p->p_emul = pack.ep_emul; 627 628 #ifdef KTRACE 629 if (KTRPOINT(p, KTR_EMUL)) 630 ktremul(p, p->p_emul->e_name); 631 #endif 632 p->p_flag &= ~P_INEXEC; 633 return (0); 634 635 bad: 636 /* free the vmspace-creation commands, and release their references */ 637 kill_vmcmds(&pack.ep_vmcmds); 638 /* kill any opened file descriptor, if necessary */ 639 if (pack.ep_flags & EXEC_HASFD) { 640 pack.ep_flags &= ~EXEC_HASFD; 641 (void) fdrelease(p, pack.ep_fd); 642 } 643 /* close and put the exec'd file */ 644 vn_close(pack.ep_vp, FREAD, cred, p); 645 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 646 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 647 648 freehdr: 649 free(pack.ep_hdr, M_EXEC); 650 p->p_flag &= ~P_INEXEC; 651 return (error); 652 653 exec_abort: 654 /* 655 * the old process doesn't exist anymore. exit gracefully. 656 * get rid of the (new) address space we have created, if any, get rid 657 * of our namei data and vnode, and exit noting failure 658 */ 659 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 660 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 661 if (pack.ep_emul_arg) 662 FREE(pack.ep_emul_arg, M_TEMP); 663 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 664 vn_close(pack.ep_vp, FREAD, cred, p); 665 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 666 667 free_pack_abort: 668 free(pack.ep_hdr, M_EXEC); 669 exit1(p, W_EXITCODE(0, SIGABRT)); 670 671 /* NOTREACHED */ 672 p->p_flag &= ~P_INEXEC; 673 return (0); 674 } 675 676 677 void * 678 copyargs(pack, arginfo, stack, argp) 679 struct exec_package *pack; 680 struct ps_strings *arginfo; 681 void *stack; 682 void *argp; 683 { 684 char **cpp = stack; 685 char *dp, *sp; 686 size_t len; 687 void *nullp = NULL; 688 long argc = arginfo->ps_nargvstr; 689 int envc = arginfo->ps_nenvstr; 690 691 if (copyout(&argc, cpp++, sizeof(argc))) 692 return (NULL); 693 694 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen); 695 sp = argp; 696 697 /* XXX don't copy them out, remap them! */ 698 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 699 700 for (; --argc >= 0; sp += len, dp += len) 701 if (copyout(&dp, cpp++, sizeof(dp)) || 702 copyoutstr(sp, dp, ARG_MAX, &len)) 703 return (NULL); 704 705 if (copyout(&nullp, cpp++, sizeof(nullp))) 706 return (NULL); 707 708 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 709 710 for (; --envc >= 0; sp += len, dp += len) 711 if (copyout(&dp, cpp++, sizeof(dp)) || 712 copyoutstr(sp, dp, ARG_MAX, &len)) 713 return (NULL); 714 715 if (copyout(&nullp, cpp++, sizeof(nullp))) 716 return (NULL); 717 718 return (cpp); 719 } 720 721 int 722 exec_sigcode_map(struct proc *p, struct emul *e) 723 { 724 vsize_t sz; 725 726 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode; 727 728 /* 729 * If we don't have a sigobject for this emulation, create one. 730 * 731 * sigobject is an anonymous memory object (just like SYSV shared 732 * memory) that we keep a permanent reference to and that we map 733 * in all processes that need this sigcode. The creation is simple, 734 * we create an object, add a permanent reference to it, map it in 735 * kernel space, copy out the sigcode to it and unmap it. 736 * Then we map it with PROT_READ|PROT_EXEC into the process just 737 * the way sys_mmap would map it. 738 */ 739 if (e->e_sigobject == NULL) { 740 vaddr_t va; 741 int r; 742 743 e->e_sigobject = uao_create(sz, 0); 744 uao_reference(e->e_sigobject); /* permanent reference */ 745 746 va = vm_map_min(kernel_map); /* hint */ 747 if ((r = uvm_map(kernel_map, &va, round_page(sz), e->e_sigobject, 748 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, 749 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) { 750 printf("kernel mapping failed %d\n", r); 751 return (ENOMEM); 752 } 753 memcpy((void *)va, e->e_sigcode, sz); 754 uvm_unmap(kernel_map, va, va + round_page(sz)); 755 } 756 757 /* Just a hint to uvm_mmap where to put it. */ 758 p->p_sigcode = uvm_map_hint(p, VM_PROT_READ|VM_PROT_EXECUTE); 759 uao_reference(e->e_sigobject); 760 if (uvm_map(&p->p_vmspace->vm_map, &p->p_sigcode, round_page(sz), 761 e->e_sigobject, 0, 0, UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, 762 UVM_INH_SHARE, UVM_ADV_RANDOM, 0))) { 763 printf("user mapping failed\n"); 764 return (ENOMEM); 765 } 766 767 return (0); 768 } 769