1 /* $OpenBSD: kern_exec.c,v 1.82 2003/09/01 18:06:03 henning 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 if (!newerror && !(epp->ep_emul->e_flags & EMUL_ENABLED)) 179 newerror = ENOEXEC; 180 /* make sure the first "interesting" error code is saved. */ 181 if (!newerror || error == ENOEXEC) 182 error = newerror; 183 if (epp->ep_flags & EXEC_DESTR && error != 0) 184 return (error); 185 } 186 if (!error) { 187 /* check that entry point is sane */ 188 if (epp->ep_entry > VM_MAXUSER_ADDRESS) { 189 error = ENOEXEC; 190 } 191 192 /* check limits */ 193 if ((epp->ep_tsize > MAXTSIZ) || 194 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur)) 195 error = ENOMEM; 196 197 if (!error) 198 return (0); 199 } 200 201 /* 202 * free any vmspace-creation commands, 203 * and release their references 204 */ 205 kill_vmcmds(&epp->ep_vmcmds); 206 207 bad2: 208 /* 209 * close the vnode, free the pathname buf, and punt. 210 */ 211 vn_close(vp, FREAD, p->p_ucred, p); 212 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 213 return (error); 214 215 bad1: 216 /* 217 * free the namei pathname buffer, and put the vnode 218 * (which we don't yet have open). 219 */ 220 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 221 vput(vp); 222 return (error); 223 } 224 225 /* 226 * exec system call 227 */ 228 /* ARGSUSED */ 229 int 230 sys_execve(p, v, retval) 231 register struct proc *p; 232 void *v; 233 register_t *retval; 234 { 235 struct sys_execve_args /* { 236 syscallarg(const char *) path; 237 syscallarg(char *const *) argp; 238 syscallarg(char *const *) envp; 239 } */ *uap = v; 240 int error; 241 struct exec_package pack; 242 struct nameidata nid; 243 struct vattr attr; 244 struct ucred *cred = p->p_ucred; 245 char *argp; 246 char * const *cpp, *dp, *sp; 247 long argc, envc; 248 size_t len, sgap; 249 #ifdef MACHINE_STACK_GROWS_UP 250 size_t slen; 251 #endif 252 char *stack; 253 struct ps_strings arginfo; 254 struct vmspace *vm = p->p_vmspace; 255 char **tmpfap; 256 extern struct emul emul_native; 257 258 /* 259 * Cheap solution to complicated problems. 260 * Mark this process as "leave me alone, I'm execing". 261 */ 262 p->p_flag |= P_INEXEC; 263 264 /* init the namei data to point the file user's program name */ 265 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 266 267 /* 268 * initialize the fields of the exec package. 269 */ 270 pack.ep_name = (char *)SCARG(uap, path); 271 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK); 272 pack.ep_hdrlen = exec_maxhdrsz; 273 pack.ep_hdrvalid = 0; 274 pack.ep_ndp = &nid; 275 pack.ep_emul_arg = NULL; 276 VMCMDSET_INIT(&pack.ep_vmcmds); 277 pack.ep_vap = &attr; 278 pack.ep_emul = &emul_native; 279 pack.ep_flags = 0; 280 281 /* see if we can run it. */ 282 if ((error = check_exec(p, &pack)) != 0) { 283 goto freehdr; 284 } 285 286 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 287 288 /* allocate an argument buffer */ 289 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS); 290 #ifdef DIAGNOSTIC 291 if (argp == NULL) 292 panic("execve: argp == NULL"); 293 #endif 294 dp = argp; 295 argc = 0; 296 297 /* copy the fake args list, if there's one, freeing it as we go */ 298 if (pack.ep_flags & EXEC_HASARGL) { 299 tmpfap = pack.ep_fa; 300 while (*tmpfap != NULL) { 301 char *cp; 302 303 cp = *tmpfap; 304 while (*cp) 305 *dp++ = *cp++; 306 dp++; 307 308 free(*tmpfap, M_EXEC); 309 tmpfap++; argc++; 310 } 311 FREE(pack.ep_fa, M_EXEC); 312 pack.ep_flags &= ~EXEC_HASARGL; 313 } 314 315 /* Now get argv & environment */ 316 if (!(cpp = SCARG(uap, argp))) { 317 error = EFAULT; 318 goto bad; 319 } 320 321 if (pack.ep_flags & EXEC_SKIPARG) 322 cpp++; 323 324 while (1) { 325 len = argp + ARG_MAX - dp; 326 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 327 goto bad; 328 if (!sp) 329 break; 330 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 331 if (error == ENAMETOOLONG) 332 error = E2BIG; 333 goto bad; 334 } 335 dp += len; 336 cpp++; 337 argc++; 338 } 339 340 envc = 0; 341 /* environment does not need to be there */ 342 if ((cpp = SCARG(uap, envp)) != NULL ) { 343 while (1) { 344 len = argp + ARG_MAX - dp; 345 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 346 goto bad; 347 if (!sp) 348 break; 349 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 350 if (error == ENAMETOOLONG) 351 error = E2BIG; 352 goto bad; 353 } 354 dp += len; 355 cpp++; 356 envc++; 357 } 358 } 359 360 dp = (char *)ALIGN(dp); 361 362 sgap = STACKGAPLEN; 363 if (stackgap_random != 0) 364 sgap += (arc4random() * ALIGNBYTES) & (stackgap_random - 1); 365 /* Now check if args & environ fit into new stack */ 366 len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) + 367 sizeof(long) + dp + sgap + sizeof(struct ps_strings)) - argp; 368 369 len = ALIGN(len); /* make the stack "safely" aligned */ 370 371 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 372 error = ENOMEM; 373 goto bad; 374 } 375 376 /* adjust "active stack depth" for process VSZ */ 377 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 378 379 /* 380 * Prepare vmspace for remapping. Note that uvmspace_exec can replace 381 * p_vmspace! 382 */ 383 uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 384 385 vm = p->p_vmspace; 386 /* Now map address space */ 387 vm->vm_taddr = (char *)pack.ep_taddr; 388 vm->vm_tsize = btoc(pack.ep_tsize); 389 vm->vm_daddr = (char *)pack.ep_daddr; 390 vm->vm_dsize = btoc(pack.ep_dsize); 391 vm->vm_ssize = btoc(pack.ep_ssize); 392 vm->vm_maxsaddr = (char *)pack.ep_maxsaddr; 393 394 /* create the new process's VM space by running the vmcmds */ 395 #ifdef DIAGNOSTIC 396 if (pack.ep_vmcmds.evs_used == 0) 397 panic("execve: no vmcmds"); 398 #endif 399 error = exec_process_vmcmds(p, &pack); 400 401 /* if an error happened, deallocate and punt */ 402 if (error) 403 goto exec_abort; 404 405 /* remember information about the process */ 406 arginfo.ps_nargvstr = argc; 407 arginfo.ps_nenvstr = envc; 408 409 #ifdef MACHINE_STACK_GROWS_UP 410 stack = (char *)USRSTACK + sizeof(arginfo); 411 slen = len - sizeof(arginfo); 412 #else 413 stack = (char *)(USRSTACK - len); 414 #endif 415 /* Now copy argc, args & environ to new stack */ 416 if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp)) 417 goto exec_abort; 418 419 /* copy out the process's ps_strings structure */ 420 if (copyout(&arginfo, (char *)PS_STRINGS, sizeof(arginfo))) 421 goto exec_abort; 422 423 stopprofclock(p); /* stop profiling */ 424 fdcloseexec(p); /* handle close on exec */ 425 execsigs(p); /* reset catched signals */ 426 427 /* set command name & other accounting info */ 428 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 429 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len); 430 p->p_comm[len] = 0; 431 p->p_acflag &= ~AFORK; 432 433 /* record proc's vnode, for use by procfs and others */ 434 if (p->p_textvp) 435 vrele(p->p_textvp); 436 VREF(pack.ep_vp); 437 p->p_textvp = pack.ep_vp; 438 439 p->p_flag |= P_EXEC; 440 if (p->p_flag & P_PPWAIT) { 441 p->p_flag &= ~P_PPWAIT; 442 wakeup((caddr_t)p->p_pptr); 443 } 444 445 /* 446 * If process does execve() while it has a mismatched real, 447 * effective, or saved uid/gid, we set P_SUGIDEXEC. 448 */ 449 if (p->p_ucred->cr_uid != p->p_cred->p_ruid || 450 p->p_ucred->cr_uid != p->p_cred->p_svuid || 451 p->p_ucred->cr_gid != p->p_cred->p_rgid || 452 p->p_ucred->cr_gid != p->p_cred->p_svgid) 453 p->p_flag |= P_SUGIDEXEC; 454 else 455 p->p_flag &= ~P_SUGIDEXEC; 456 457 /* 458 * deal with set[ug]id. 459 * MNT_NOEXEC has already been used to disable s[ug]id. 460 */ 461 if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) { 462 int i; 463 464 p->p_flag |= P_SUGID; 465 p->p_flag |= P_SUGIDEXEC; 466 467 #ifdef KTRACE 468 /* 469 * If process is being ktraced, turn off - unless 470 * root set it. 471 */ 472 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) { 473 p->p_traceflag = 0; 474 ktrsettracevnode(p, NULL); 475 } 476 #endif 477 p->p_ucred = crcopy(cred); 478 if (attr.va_mode & VSUID) 479 p->p_ucred->cr_uid = attr.va_uid; 480 if (attr.va_mode & VSGID) 481 p->p_ucred->cr_gid = attr.va_gid; 482 483 /* 484 * For set[ug]id processes, a few caveats apply to 485 * stdin, stdout, and stderr. 486 */ 487 for (i = 0; i < 3; i++) { 488 struct file *fp = NULL; 489 490 /* 491 * NOTE - This will never return NULL because of 492 * unmature fds. The file descriptor table is not 493 * shared because we're suid. 494 */ 495 fp = fd_getfile(p->p_fd, i); 496 #ifdef PROCFS 497 /* 498 * Close descriptors that are writing to procfs. 499 */ 500 if (fp && fp->f_type == DTYPE_VNODE && 501 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS && 502 (fp->f_flag & FWRITE)) { 503 fdrelease(p, i); 504 fp = NULL; 505 } 506 #endif 507 508 /* 509 * Ensure that stdin, stdout, and stderr are already 510 * allocated. We do not want userland to accidentally 511 * allocate descriptors in this range which has implied 512 * meaning to libc. 513 */ 514 if (fp == NULL) { 515 short flags = FREAD | (i == 0 ? 0 : FWRITE); 516 struct vnode *vp; 517 int indx; 518 519 if ((error = falloc(p, &fp, &indx)) != 0) 520 goto exec_abort; 521 #ifdef DIAGNOSTIC 522 if (indx != i) 523 panic("sys_execve: falloc indx != i"); 524 #endif 525 if ((error = cdevvp(getnulldev(), &vp)) != 0) { 526 fdremove(p->p_fd, indx); 527 closef(fp, p); 528 goto exec_abort; 529 } 530 if ((error = VOP_OPEN(vp, flags, p->p_ucred, p)) != 0) { 531 fdremove(p->p_fd, indx); 532 closef(fp, p); 533 vrele(vp); 534 goto exec_abort; 535 } 536 if (flags & FWRITE) 537 vp->v_writecount++; 538 fp->f_flag = flags; 539 fp->f_type = DTYPE_VNODE; 540 fp->f_ops = &vnops; 541 fp->f_data = (caddr_t)vp; 542 FILE_SET_MATURE(fp); 543 } 544 } 545 } else 546 p->p_flag &= ~P_SUGID; 547 p->p_cred->p_svuid = p->p_ucred->cr_uid; 548 p->p_cred->p_svgid = p->p_ucred->cr_gid; 549 550 if (p->p_flag & P_SUGIDEXEC) { 551 int i, s = splclock(); 552 553 timeout_del(&p->p_realit_to); 554 timerclear(&p->p_realtimer.it_interval); 555 timerclear(&p->p_realtimer.it_value); 556 for (i = 0; i < sizeof(p->p_stats->p_timer) / 557 sizeof(p->p_stats->p_timer[0]); i++) { 558 timerclear(&p->p_stats->p_timer[i].it_interval); 559 timerclear(&p->p_stats->p_timer[i].it_value); 560 } 561 splx(s); 562 } 563 564 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 565 566 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 567 vn_close(pack.ep_vp, FREAD, cred, p); 568 569 /* 570 * notify others that we exec'd 571 */ 572 KNOTE(&p->p_klist, NOTE_EXEC); 573 574 /* setup new registers and do misc. setup. */ 575 if (pack.ep_emul->e_fixup != NULL) { 576 if ((*pack.ep_emul->e_fixup)(p, &pack) != 0) 577 goto free_pack_abort; 578 } 579 #ifdef MACHINE_STACK_GROWS_UP 580 (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval); 581 #else 582 (*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval); 583 #endif 584 585 /* map the process's signal trampoline code */ 586 if (exec_sigcode_map(p, pack.ep_emul)) 587 goto exec_abort; 588 589 if (p->p_flag & P_TRACED) 590 psignal(p, SIGTRAP); 591 592 free(pack.ep_hdr, M_EXEC); 593 594 /* 595 * Call emulation specific exec hook. This can setup per-process 596 * p->p_emuldata or do any other per-process stuff an emulation needs. 597 * 598 * If we are executing process of different emulation than the 599 * original forked process, call e_proc_exit() of the old emulation 600 * first, then e_proc_exec() of new emulation. If the emulation is 601 * same, the exec hook code should deallocate any old emulation 602 * resources held previously by this process. 603 */ 604 if (p->p_emul && p->p_emul->e_proc_exit && 605 p->p_emul != pack.ep_emul) 606 (*p->p_emul->e_proc_exit)(p); 607 608 /* 609 * Call exec hook. Emulation code may NOT store reference to anything 610 * from &pack. 611 */ 612 if (pack.ep_emul->e_proc_exec) 613 (*pack.ep_emul->e_proc_exec)(p, &pack); 614 615 /* update p_emul, the old value is no longer needed */ 616 p->p_emul = pack.ep_emul; 617 618 #ifdef KTRACE 619 if (KTRPOINT(p, KTR_EMUL)) 620 ktremul(p, p->p_emul->e_name); 621 #endif 622 p->p_flag &= ~P_INEXEC; 623 return (0); 624 625 bad: 626 /* free the vmspace-creation commands, and release their references */ 627 kill_vmcmds(&pack.ep_vmcmds); 628 /* kill any opened file descriptor, if necessary */ 629 if (pack.ep_flags & EXEC_HASFD) { 630 pack.ep_flags &= ~EXEC_HASFD; 631 (void) fdrelease(p, pack.ep_fd); 632 } 633 /* close and put the exec'd file */ 634 vn_close(pack.ep_vp, FREAD, cred, p); 635 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 636 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 637 638 freehdr: 639 free(pack.ep_hdr, M_EXEC); 640 p->p_flag &= ~P_INEXEC; 641 return (error); 642 643 exec_abort: 644 /* 645 * the old process doesn't exist anymore. exit gracefully. 646 * get rid of the (new) address space we have created, if any, get rid 647 * of our namei data and vnode, and exit noting failure 648 */ 649 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 650 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 651 if (pack.ep_emul_arg) 652 FREE(pack.ep_emul_arg, M_TEMP); 653 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 654 vn_close(pack.ep_vp, FREAD, cred, p); 655 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 656 657 free_pack_abort: 658 free(pack.ep_hdr, M_EXEC); 659 exit1(p, W_EXITCODE(0, SIGABRT)); 660 661 /* NOTREACHED */ 662 p->p_flag &= ~P_INEXEC; 663 return (0); 664 } 665 666 667 void * 668 copyargs(pack, arginfo, stack, argp) 669 struct exec_package *pack; 670 struct ps_strings *arginfo; 671 void *stack; 672 void *argp; 673 { 674 char **cpp = stack; 675 char *dp, *sp; 676 size_t len; 677 void *nullp = NULL; 678 long argc = arginfo->ps_nargvstr; 679 int envc = arginfo->ps_nenvstr; 680 681 if (copyout(&argc, cpp++, sizeof(argc))) 682 return (NULL); 683 684 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen); 685 sp = argp; 686 687 /* XXX don't copy them out, remap them! */ 688 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 689 690 for (; --argc >= 0; sp += len, dp += len) 691 if (copyout(&dp, cpp++, sizeof(dp)) || 692 copyoutstr(sp, dp, ARG_MAX, &len)) 693 return (NULL); 694 695 if (copyout(&nullp, cpp++, sizeof(nullp))) 696 return (NULL); 697 698 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 699 700 for (; --envc >= 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 return (cpp); 709 } 710 711 int 712 exec_sigcode_map(struct proc *p, struct emul *e) 713 { 714 vsize_t sz; 715 716 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode; 717 718 /* 719 * If we don't have a sigobject for this emulation, create one. 720 * 721 * sigobject is an anonymous memory object (just like SYSV shared 722 * memory) that we keep a permanent reference to and that we map 723 * in all processes that need this sigcode. The creation is simple, 724 * we create an object, add a permanent reference to it, map it in 725 * kernel space, copy out the sigcode to it and unmap it. 726 * Then we map it with PROT_READ|PROT_EXEC into the process just 727 * the way sys_mmap would map it. 728 */ 729 if (e->e_sigobject == NULL) { 730 vaddr_t va; 731 int r; 732 733 e->e_sigobject = uao_create(sz, 0); 734 uao_reference(e->e_sigobject); /* permanent reference */ 735 736 va = vm_map_min(kernel_map); /* hint */ 737 if ((r = uvm_map(kernel_map, &va, round_page(sz), e->e_sigobject, 738 0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, 739 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) { 740 printf("kernel mapping failed %d\n", r); 741 return (ENOMEM); 742 } 743 memcpy((void *)va, e->e_sigcode, sz); 744 uvm_unmap(kernel_map, va, va + round_page(sz)); 745 } 746 747 /* Just a hint to uvm_mmap where to put it. */ 748 p->p_sigcode = uvm_map_hint(p, VM_PROT_READ|VM_PROT_EXECUTE); 749 uao_reference(e->e_sigobject); 750 if (uvm_map(&p->p_vmspace->vm_map, &p->p_sigcode, round_page(sz), 751 e->e_sigobject, 0, 0, UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, 752 UVM_INH_SHARE, UVM_ADV_RANDOM, 0))) { 753 printf("user mapping failed\n"); 754 return (ENOMEM); 755 } 756 757 return (0); 758 } 759