1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* Copyright (c) 1988 AT&T */ 29 /* All Rights Reserved */ 30 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/sysmacros.h> 35 #include <sys/systm.h> 36 #include <sys/signal.h> 37 #include <sys/cred_impl.h> 38 #include <sys/policy.h> 39 #include <sys/user.h> 40 #include <sys/errno.h> 41 #include <sys/file.h> 42 #include <sys/vfs.h> 43 #include <sys/vnode.h> 44 #include <sys/mman.h> 45 #include <sys/acct.h> 46 #include <sys/cpuvar.h> 47 #include <sys/proc.h> 48 #include <sys/cmn_err.h> 49 #include <sys/debug.h> 50 #include <sys/pathname.h> 51 #include <sys/vm.h> 52 #include <sys/lgrp.h> 53 #include <sys/vtrace.h> 54 #include <sys/exec.h> 55 #include <sys/exechdr.h> 56 #include <sys/kmem.h> 57 #include <sys/prsystm.h> 58 #include <sys/modctl.h> 59 #include <sys/vmparam.h> 60 #include <sys/schedctl.h> 61 #include <sys/utrap.h> 62 #include <sys/systeminfo.h> 63 #include <sys/stack.h> 64 #include <sys/rctl.h> 65 #include <sys/dtrace.h> 66 #include <sys/lwpchan_impl.h> 67 #include <sys/pool.h> 68 #include <sys/sdt.h> 69 #include <sys/brand.h> 70 71 #include <c2/audit.h> 72 73 #include <vm/hat.h> 74 #include <vm/anon.h> 75 #include <vm/as.h> 76 #include <vm/seg.h> 77 #include <vm/seg_vn.h> 78 79 #define PRIV_RESET 0x01 /* needs to reset privs */ 80 #define PRIV_SETID 0x02 /* needs to change uids */ 81 #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */ 82 #define PRIV_INCREASE 0x08 /* child runs with more privs */ 83 #define MAC_FLAGS 0x10 /* need to adjust MAC flags */ 84 85 static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *); 86 static int hold_execsw(struct execsw *); 87 88 uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */ 89 #if defined(_SYSCALL32_IMPL) 90 uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */ 91 #endif 92 93 #define PSUIDFLAGS (SNOCD|SUGID) 94 95 /* 96 * exec() - wrapper around exece providing NULL environment pointer 97 */ 98 int 99 exec(const char *fname, const char **argp) 100 { 101 return (exece(fname, argp, NULL)); 102 } 103 104 /* 105 * exece() - system call wrapper around exec_common() 106 */ 107 int 108 exece(const char *fname, const char **argp, const char **envp) 109 { 110 int error; 111 112 error = exec_common(fname, argp, envp, EBA_NONE); 113 return (error ? (set_errno(error)) : 0); 114 } 115 116 int 117 exec_common(const char *fname, const char **argp, const char **envp, 118 int brand_action) 119 { 120 vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL; 121 proc_t *p = ttoproc(curthread); 122 klwp_t *lwp = ttolwp(curthread); 123 struct user *up = PTOU(p); 124 long execsz; /* temporary count of exec size */ 125 int i; 126 int error; 127 char exec_file[MAXCOMLEN+1]; 128 struct pathname pn; 129 struct pathname resolvepn; 130 struct uarg args; 131 struct execa ua; 132 k_sigset_t savedmask; 133 lwpdir_t *lwpdir = NULL; 134 lwpdir_t **tidhash; 135 lwpdir_t *old_lwpdir = NULL; 136 uint_t old_lwpdir_sz; 137 lwpdir_t **old_tidhash; 138 uint_t old_tidhash_sz; 139 lwpent_t *lep; 140 int brandme = 0; 141 142 /* 143 * exec() is not supported for the /proc agent lwp. 144 */ 145 if (curthread == p->p_agenttp) 146 return (ENOTSUP); 147 148 if ((error = secpolicy_basic_exec(CRED())) != 0) 149 return (error); 150 151 if (brand_action != EBA_NONE) { 152 /* 153 * Brand actions are not supported for processes that are not 154 * running in a branded zone. 155 */ 156 if (!ZONE_IS_BRANDED(p->p_zone)) 157 return (ENOTSUP); 158 159 if (brand_action == EBA_NATIVE) { 160 /* Only branded processes can be unbranded */ 161 if (!PROC_IS_BRANDED(p)) 162 return (ENOTSUP); 163 } else { 164 /* Only unbranded processes can be branded */ 165 if (PROC_IS_BRANDED(p)) 166 return (ENOTSUP); 167 brandme = 1; 168 } 169 } else { 170 /* 171 * If this is a native zone, or if the process is already 172 * branded, then we don't need to do anything. If this is 173 * a native process in a branded zone, we need to brand the 174 * process as it exec()s the new binary. 175 */ 176 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p)) 177 brandme = 1; 178 } 179 180 /* 181 * Inform /proc that an exec() has started. 182 * Hold signals that are ignored by default so that we will 183 * not be interrupted by a signal that will be ignored after 184 * successful completion of gexec(). 185 */ 186 mutex_enter(&p->p_lock); 187 prexecstart(); 188 schedctl_finish_sigblock(curthread); 189 savedmask = curthread->t_hold; 190 sigorset(&curthread->t_hold, &ignoredefault); 191 mutex_exit(&p->p_lock); 192 193 /* 194 * Look up path name and remember last component for later. 195 * To help coreadm expand its %d token, we attempt to save 196 * the directory containing the executable in p_execdir. The 197 * first call to lookuppn() may fail and return EINVAL because 198 * dirvpp is non-NULL. In that case, we make a second call to 199 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL, 200 * but coreadm is allowed to expand %d to the empty string and 201 * there are other cases in which that failure may occur. 202 */ 203 if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 204 goto out; 205 pn_alloc(&resolvepn); 206 if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) { 207 pn_free(&resolvepn); 208 pn_free(&pn); 209 if (error != EINVAL) 210 goto out; 211 212 dir = NULL; 213 if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 214 goto out; 215 pn_alloc(&resolvepn); 216 if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP, 217 &vp)) != 0) { 218 pn_free(&resolvepn); 219 pn_free(&pn); 220 goto out; 221 } 222 } 223 if (vp == NULL) { 224 if (dir != NULL) 225 VN_RELE(dir); 226 error = ENOENT; 227 pn_free(&resolvepn); 228 pn_free(&pn); 229 goto out; 230 } 231 232 /* 233 * We do not allow executing files in attribute directories. 234 * We test this by determining whether the resolved path 235 * contains a "/" when we're in an attribute directory; 236 * only if the pathname does not contain a "/" the resolved path 237 * points to a file in the current working (attribute) directory. 238 */ 239 if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 && 240 strchr(resolvepn.pn_path, '/') == NULL) { 241 if (dir != NULL) 242 VN_RELE(dir); 243 error = EACCES; 244 pn_free(&resolvepn); 245 pn_free(&pn); 246 VN_RELE(vp); 247 goto out; 248 } 249 250 bzero(exec_file, MAXCOMLEN+1); 251 (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN); 252 bzero(&args, sizeof (args)); 253 args.pathname = resolvepn.pn_path; 254 /* don't free resolvepn until we are done with args */ 255 pn_free(&pn); 256 257 /* 258 * Specific exec handlers, or policies determined via 259 * /etc/system may override the historical default. 260 */ 261 args.stk_prot = PROT_ZFOD; 262 args.dat_prot = PROT_ZFOD; 263 264 CPU_STATS_ADD_K(sys, sysexec, 1); 265 DTRACE_PROC1(exec, char *, args.pathname); 266 267 ua.fname = fname; 268 ua.argp = argp; 269 ua.envp = envp; 270 271 /* If necessary, brand this process before we start the exec. */ 272 if (brandme != 0) 273 brand_setbrand(p); 274 275 if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz, 276 exec_file, p->p_cred, brand_action)) != 0) { 277 if (brandme != 0) 278 BROP(p)->b_proc_exit(p, lwp); 279 VN_RELE(vp); 280 if (dir != NULL) 281 VN_RELE(dir); 282 pn_free(&resolvepn); 283 goto fail; 284 } 285 286 /* 287 * Free floating point registers (sun4u only) 288 */ 289 ASSERT(lwp != NULL); 290 lwp_freeregs(lwp, 1); 291 292 /* 293 * Free thread and process context ops. 294 */ 295 if (curthread->t_ctx) 296 freectx(curthread, 1); 297 if (p->p_pctx) 298 freepctx(p, 1); 299 300 /* 301 * Remember file name for accounting; clear any cached DTrace predicate. 302 */ 303 up->u_acflag &= ~AFORK; 304 bcopy(exec_file, up->u_comm, MAXCOMLEN+1); 305 curthread->t_predcache = NULL; 306 307 /* 308 * Clear contract template state 309 */ 310 lwp_ctmpl_clear(lwp); 311 312 /* 313 * Save the directory in which we found the executable for expanding 314 * the %d token used in core file patterns. 315 */ 316 mutex_enter(&p->p_lock); 317 tmpvp = p->p_execdir; 318 p->p_execdir = dir; 319 if (p->p_execdir != NULL) 320 VN_HOLD(p->p_execdir); 321 mutex_exit(&p->p_lock); 322 323 if (tmpvp != NULL) 324 VN_RELE(tmpvp); 325 326 /* 327 * Reset stack state to the user stack, clear set of signals 328 * caught on the signal stack, and reset list of signals that 329 * restart system calls; the new program's environment should 330 * not be affected by detritus from the old program. Any 331 * pending held signals remain held, so don't clear t_hold. 332 */ 333 mutex_enter(&p->p_lock); 334 lwp->lwp_oldcontext = 0; 335 lwp->lwp_ustack = 0; 336 lwp->lwp_old_stk_ctl = 0; 337 sigemptyset(&up->u_signodefer); 338 sigemptyset(&up->u_sigonstack); 339 sigemptyset(&up->u_sigresethand); 340 lwp->lwp_sigaltstack.ss_sp = 0; 341 lwp->lwp_sigaltstack.ss_size = 0; 342 lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 343 344 /* 345 * Make saved resource limit == current resource limit. 346 */ 347 for (i = 0; i < RLIM_NLIMITS; i++) { 348 /*CONSTCOND*/ 349 if (RLIM_SAVED(i)) { 350 (void) rctl_rlimit_get(rctlproc_legacy[i], p, 351 &up->u_saved_rlimit[i]); 352 } 353 } 354 355 /* 356 * If the action was to catch the signal, then the action 357 * must be reset to SIG_DFL. 358 */ 359 sigdefault(p); 360 p->p_flag &= ~(SNOWAIT|SJCTL); 361 p->p_flag |= (SEXECED|SMSACCT|SMSFORK); 362 up->u_signal[SIGCLD - 1] = SIG_DFL; 363 364 /* 365 * Delete the dot4 sigqueues/signotifies. 366 */ 367 sigqfree(p); 368 369 mutex_exit(&p->p_lock); 370 371 mutex_enter(&p->p_pflock); 372 p->p_prof.pr_base = NULL; 373 p->p_prof.pr_size = 0; 374 p->p_prof.pr_off = 0; 375 p->p_prof.pr_scale = 0; 376 p->p_prof.pr_samples = 0; 377 mutex_exit(&p->p_pflock); 378 379 ASSERT(curthread->t_schedctl == NULL); 380 381 #if defined(__sparc) 382 if (p->p_utraps != NULL) 383 utrap_free(p); 384 #endif /* __sparc */ 385 386 /* 387 * Close all close-on-exec files. 388 */ 389 close_exec(P_FINFO(p)); 390 TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up); 391 392 /* Unbrand ourself if requested. */ 393 if (brand_action == EBA_NATIVE) 394 BROP(p)->b_proc_exit(p, lwp); 395 ASSERT((brand_action != EBA_NATIVE) || !PROC_IS_BRANDED(p)); 396 397 setregs(&args); 398 399 /* Mark this as an executable vnode */ 400 mutex_enter(&vp->v_lock); 401 vp->v_flag |= VVMEXEC; 402 mutex_exit(&vp->v_lock); 403 404 VN_RELE(vp); 405 if (dir != NULL) 406 VN_RELE(dir); 407 pn_free(&resolvepn); 408 409 /* 410 * Allocate a new lwp directory and lwpid hash table if necessary. 411 */ 412 if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) { 413 lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP); 414 lwpdir->ld_next = lwpdir + 1; 415 tidhash = kmem_zalloc(2 * sizeof (lwpdir_t *), KM_SLEEP); 416 if (p->p_lwpdir != NULL) 417 lep = p->p_lwpdir[curthread->t_dslot].ld_entry; 418 else 419 lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 420 } 421 422 if (PROC_IS_BRANDED(p)) 423 BROP(p)->b_exec(); 424 425 mutex_enter(&p->p_lock); 426 prbarrier(p); 427 428 /* 429 * Reset lwp id to the default value of 1. 430 * This is a single-threaded process now 431 * and lwp #1 is lwp_wait()able by default. 432 * The t_unpark flag should not be inherited. 433 */ 434 ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0); 435 curthread->t_tid = 1; 436 kpreempt_disable(); 437 ASSERT(curthread->t_lpl != NULL); 438 p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid; 439 kpreempt_enable(); 440 if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) { 441 lgrp_update_trthr_migrations(1); 442 } 443 curthread->t_unpark = 0; 444 curthread->t_proc_flag |= TP_TWAIT; 445 curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */ 446 p->p_lwpdaemon = 0; /* but oh well ... */ 447 p->p_lwpid = 1; 448 449 /* 450 * Install the newly-allocated lwp directory and lwpid hash table 451 * and insert the current thread into the new hash table. 452 */ 453 if (lwpdir != NULL) { 454 old_lwpdir = p->p_lwpdir; 455 old_lwpdir_sz = p->p_lwpdir_sz; 456 old_tidhash = p->p_tidhash; 457 old_tidhash_sz = p->p_tidhash_sz; 458 p->p_lwpdir = p->p_lwpfree = lwpdir; 459 p->p_lwpdir_sz = 2; 460 p->p_tidhash = tidhash; 461 p->p_tidhash_sz = 2; 462 lep->le_thread = curthread; 463 lep->le_lwpid = curthread->t_tid; 464 lep->le_start = curthread->t_start; 465 lwp_hash_in(p, lep); 466 } 467 468 /* 469 * Restore the saved signal mask and 470 * inform /proc that the exec() has finished. 471 */ 472 curthread->t_hold = savedmask; 473 prexecend(); 474 mutex_exit(&p->p_lock); 475 if (old_lwpdir) { 476 kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t)); 477 kmem_free(old_tidhash, old_tidhash_sz * sizeof (lwpdir_t *)); 478 } 479 480 ASSERT(error == 0); 481 DTRACE_PROC(exec__success); 482 return (0); 483 484 fail: 485 DTRACE_PROC1(exec__failure, int, error); 486 out: /* error return */ 487 mutex_enter(&p->p_lock); 488 curthread->t_hold = savedmask; 489 prexecend(); 490 mutex_exit(&p->p_lock); 491 ASSERT(error != 0); 492 return (error); 493 } 494 495 496 /* 497 * Perform generic exec duties and switchout to object-file specific 498 * handler. 499 */ 500 int 501 gexec( 502 struct vnode **vpp, 503 struct execa *uap, 504 struct uarg *args, 505 struct intpdata *idatap, 506 int level, 507 long *execsz, 508 caddr_t exec_file, 509 struct cred *cred, 510 int brand_action) 511 { 512 struct vnode *vp; 513 proc_t *pp = ttoproc(curthread); 514 struct execsw *eswp; 515 int error = 0; 516 int suidflags = 0; 517 ssize_t resid; 518 uid_t uid, gid; 519 struct vattr vattr; 520 char magbuf[MAGIC_BYTES]; 521 int setid; 522 cred_t *oldcred, *newcred = NULL; 523 int privflags = 0; 524 int setidfl; 525 526 /* 527 * If the SNOCD or SUGID flag is set, turn it off and remember the 528 * previous setting so we can restore it if we encounter an error. 529 */ 530 if (level == 0 && (pp->p_flag & PSUIDFLAGS)) { 531 mutex_enter(&pp->p_lock); 532 suidflags = pp->p_flag & PSUIDFLAGS; 533 pp->p_flag &= ~PSUIDFLAGS; 534 mutex_exit(&pp->p_lock); 535 } 536 537 if ((error = execpermissions(*vpp, &vattr, args)) != 0) 538 goto bad; 539 540 /* need to open vnode for stateful file systems like rfs */ 541 if ((error = VOP_OPEN(vpp, FREAD, CRED())) != 0) 542 goto bad; 543 vp = *vpp; 544 545 /* 546 * Note: to support binary compatibility with SunOS a.out 547 * executables, we read in the first four bytes, as the 548 * magic number is in bytes 2-3. 549 */ 550 if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf), 551 (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 552 goto bad; 553 if (resid != 0) 554 goto bad; 555 556 if ((eswp = findexec_by_hdr(magbuf)) == NULL) 557 goto bad; 558 559 if (level == 0 && 560 (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) { 561 562 newcred = cred = crdup(cred); 563 564 /* If we can, drop the PA bit */ 565 if ((privflags & PRIV_RESET) != 0) 566 priv_adjust_PA(cred); 567 568 if (privflags & PRIV_SETID) { 569 cred->cr_uid = uid; 570 cred->cr_gid = gid; 571 cred->cr_suid = uid; 572 cred->cr_sgid = gid; 573 } 574 575 if (privflags & MAC_FLAGS) { 576 if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT)) 577 CR_FLAGS(cred) &= ~NET_MAC_AWARE; 578 CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT; 579 } 580 581 /* 582 * Implement the privilege updates: 583 * 584 * Restrict with L: 585 * 586 * I' = I & L 587 * 588 * E' = P' = (I' + F) & A 589 * 590 * But if running under ptrace, we cap I with P. 591 */ 592 if ((privflags & PRIV_RESET) != 0) { 593 if ((privflags & PRIV_INCREASE) != 0 && 594 (pp->p_proc_flag & P_PR_PTRACE) != 0) 595 priv_intersect(&CR_OPPRIV(cred), 596 &CR_IPRIV(cred)); 597 priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred)); 598 CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred); 599 priv_adjust_PA(cred); 600 } 601 } 602 603 /* SunOS 4.x buy-back */ 604 if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) && 605 (vattr.va_mode & (VSUID|VSGID))) { 606 cmn_err(CE_NOTE, 607 "!%s, uid %d: setuid execution not allowed, dev=%lx", 608 exec_file, cred->cr_uid, vp->v_vfsp->vfs_dev); 609 } 610 611 /* 612 * execsetid() told us whether or not we had to change the 613 * credentials of the process. In privflags, it told us 614 * whether we gained any privileges or executed a set-uid executable. 615 */ 616 setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE)); 617 618 /* 619 * Use /etc/system variable to determine if the stack 620 * should be marked as executable by default. 621 */ 622 if (noexec_user_stack) 623 args->stk_prot &= ~PROT_EXEC; 624 625 args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */ 626 627 /* 628 * Traditionally, the setid flags told the sub processes whether 629 * the file just executed was set-uid or set-gid; this caused 630 * some confusion as the 'setid' flag did not match the SUGID 631 * process flag which is only set when the uids/gids do not match. 632 * A script set-gid/set-uid to the real uid/gid would start with 633 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH. 634 * Now we flag those cases where the calling process cannot 635 * be trusted to influence the newly exec'ed process, either 636 * because it runs with more privileges or when the uids/gids 637 * do in fact not match. 638 * This also makes the runtime linker agree with the on exec 639 * values of SNOCD and SUGID. 640 */ 641 setidfl = 0; 642 if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid && 643 !supgroupmember(cred->cr_gid, cred))) { 644 setidfl |= EXECSETID_UGIDS; 645 } 646 if (setid & PRIV_SETUGID) 647 setidfl |= EXECSETID_SETID; 648 if (setid & PRIV_INCREASE) 649 setidfl |= EXECSETID_PRIVS; 650 651 error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz, 652 setidfl, exec_file, cred, brand_action); 653 rw_exit(eswp->exec_lock); 654 if (error != 0) { 655 if (newcred != NULL) 656 crfree(newcred); 657 goto bad; 658 } 659 660 if (level == 0) { 661 mutex_enter(&pp->p_crlock); 662 if (newcred != NULL) { 663 /* 664 * Free the old credentials, and set the new ones. 665 * Do this for both the process and the (single) thread. 666 */ 667 crfree(pp->p_cred); 668 pp->p_cred = cred; /* cred already held for proc */ 669 crhold(cred); /* hold new cred for thread */ 670 /* 671 * DTrace accesses t_cred in probe context. t_cred 672 * must always be either NULL, or point to a valid, 673 * allocated cred structure. 674 */ 675 oldcred = curthread->t_cred; 676 curthread->t_cred = cred; 677 crfree(oldcred); 678 } 679 /* 680 * On emerging from a successful exec(), the saved 681 * uid and gid equal the effective uid and gid. 682 */ 683 cred->cr_suid = cred->cr_uid; 684 cred->cr_sgid = cred->cr_gid; 685 686 /* 687 * If the real and effective ids do not match, this 688 * is a setuid process that should not dump core. 689 * The group comparison is tricky; we prevent the code 690 * from flagging SNOCD when executing with an effective gid 691 * which is a supplementary group. 692 */ 693 if (cred->cr_ruid != cred->cr_uid || 694 (cred->cr_rgid != cred->cr_gid && 695 !supgroupmember(cred->cr_gid, cred)) || 696 (privflags & PRIV_INCREASE) != 0) 697 suidflags = PSUIDFLAGS; 698 else 699 suidflags = 0; 700 701 mutex_exit(&pp->p_crlock); 702 if (suidflags) { 703 mutex_enter(&pp->p_lock); 704 pp->p_flag |= suidflags; 705 mutex_exit(&pp->p_lock); 706 } 707 if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) { 708 /* 709 * If process is traced via /proc, arrange to 710 * invalidate the associated /proc vnode. 711 */ 712 if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE)) 713 args->traceinval = 1; 714 } 715 if (pp->p_proc_flag & P_PR_PTRACE) 716 psignal(pp, SIGTRAP); 717 if (args->traceinval) 718 prinvalidate(&pp->p_user); 719 } 720 721 return (0); 722 bad: 723 if (error == 0) 724 error = ENOEXEC; 725 726 if (suidflags) { 727 mutex_enter(&pp->p_lock); 728 pp->p_flag |= suidflags; 729 mutex_exit(&pp->p_lock); 730 } 731 return (error); 732 } 733 734 extern char *execswnames[]; 735 736 struct execsw * 737 allocate_execsw(char *name, char *magic, size_t magic_size) 738 { 739 int i, j; 740 char *ename; 741 char *magicp; 742 743 mutex_enter(&execsw_lock); 744 for (i = 0; i < nexectype; i++) { 745 if (execswnames[i] == NULL) { 746 ename = kmem_alloc(strlen(name) + 1, KM_SLEEP); 747 (void) strcpy(ename, name); 748 execswnames[i] = ename; 749 /* 750 * Set the magic number last so that we 751 * don't need to hold the execsw_lock in 752 * findexectype(). 753 */ 754 magicp = kmem_alloc(magic_size, KM_SLEEP); 755 for (j = 0; j < magic_size; j++) 756 magicp[j] = magic[j]; 757 execsw[i].exec_magic = magicp; 758 mutex_exit(&execsw_lock); 759 return (&execsw[i]); 760 } 761 } 762 mutex_exit(&execsw_lock); 763 return (NULL); 764 } 765 766 /* 767 * Find the exec switch table entry with the corresponding magic string. 768 */ 769 struct execsw * 770 findexecsw(char *magic) 771 { 772 struct execsw *eswp; 773 774 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 775 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 776 if (magic && eswp->exec_maglen != 0 && 777 bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) 778 return (eswp); 779 } 780 return (NULL); 781 } 782 783 /* 784 * Find the execsw[] index for the given exec header string by looking for the 785 * magic string at a specified offset and length for each kind of executable 786 * file format until one matches. If no execsw[] entry is found, try to 787 * autoload a module for this magic string. 788 */ 789 struct execsw * 790 findexec_by_hdr(char *header) 791 { 792 struct execsw *eswp; 793 794 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 795 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 796 if (header && eswp->exec_maglen != 0 && 797 bcmp(&header[eswp->exec_magoff], eswp->exec_magic, 798 eswp->exec_maglen) == 0) { 799 if (hold_execsw(eswp) != 0) 800 return (NULL); 801 return (eswp); 802 } 803 } 804 return (NULL); /* couldn't find the type */ 805 } 806 807 /* 808 * Find the execsw[] index for the given magic string. If no execsw[] entry 809 * is found, try to autoload a module for this magic string. 810 */ 811 struct execsw * 812 findexec_by_magic(char *magic) 813 { 814 struct execsw *eswp; 815 816 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 817 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 818 if (magic && eswp->exec_maglen != 0 && 819 bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) { 820 if (hold_execsw(eswp) != 0) 821 return (NULL); 822 return (eswp); 823 } 824 } 825 return (NULL); /* couldn't find the type */ 826 } 827 828 static int 829 hold_execsw(struct execsw *eswp) 830 { 831 char *name; 832 833 rw_enter(eswp->exec_lock, RW_READER); 834 while (!LOADED_EXEC(eswp)) { 835 rw_exit(eswp->exec_lock); 836 name = execswnames[eswp-execsw]; 837 ASSERT(name); 838 if (modload("exec", name) == -1) 839 return (-1); 840 rw_enter(eswp->exec_lock, RW_READER); 841 } 842 return (0); 843 } 844 845 static int 846 execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp) 847 { 848 proc_t *pp = ttoproc(curthread); 849 uid_t uid, gid; 850 cred_t *cr = pp->p_cred; 851 int privflags = 0; 852 853 /* 854 * Remember credentials. 855 */ 856 uid = cr->cr_uid; 857 gid = cr->cr_gid; 858 859 /* Will try to reset the PRIV_AWARE bit later. */ 860 if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE) 861 privflags |= PRIV_RESET; 862 863 if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) { 864 /* 865 * Set-uid root execution only allowed if the limit set 866 * holds all unsafe privileges. 867 */ 868 if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 || 869 priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) { 870 uid = vattrp->va_uid; 871 privflags |= PRIV_SETUGID; 872 } 873 if (vattrp->va_mode & VSGID) { 874 gid = vattrp->va_gid; 875 privflags |= PRIV_SETUGID; 876 } 877 } 878 879 /* 880 * Do we need to change our credential anyway? 881 * This is the case when E != I or P != I, as 882 * we need to do the assignments (with F empty and A full) 883 * Or when I is not a subset of L; in that case we need to 884 * enforce L. 885 * 886 * I' = L & I 887 * 888 * E' = P' = (I' + F) & A 889 * or 890 * E' = P' = I' 891 */ 892 if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) || 893 !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) || 894 !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr))) 895 privflags |= PRIV_RESET; 896 897 /* If MAC-aware flag(s) are on, need to update cred to remove. */ 898 if ((CR_FLAGS(cr) & NET_MAC_AWARE) || 899 (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT)) 900 privflags |= MAC_FLAGS; 901 902 /* 903 * When we introduce the "forced" set then we will need 904 * to set PRIV_INCREASE here if I not a subset of P. 905 * If the "allowed" set is introduced we will need to do 906 * a similar thing; however, it seems more reasonable to 907 * have the allowed set reduce "L": script language interpreters 908 * would typically have an allowed set of "all". 909 */ 910 911 /* 912 * Set setuid/setgid protections if no ptrace() compatibility. 913 * For privileged processes, honor setuid/setgid even in 914 * the presence of ptrace() compatibility. 915 */ 916 if (((pp->p_proc_flag & P_PR_PTRACE) == 0 || 917 PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) && 918 (cr->cr_uid != uid || 919 cr->cr_gid != gid || 920 cr->cr_suid != uid || 921 cr->cr_sgid != gid)) { 922 *uidp = uid; 923 *gidp = gid; 924 privflags |= PRIV_SETID; 925 } 926 return (privflags); 927 } 928 929 int 930 execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args) 931 { 932 int error; 933 proc_t *p = ttoproc(curthread); 934 935 vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE; 936 if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred)) 937 return (error); 938 /* 939 * Check the access mode. 940 * If VPROC, ask /proc if the file is an object file. 941 */ 942 if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred)) != 0 || 943 !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) || 944 (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 || 945 (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) { 946 if (error == 0) 947 error = EACCES; 948 return (error); 949 } 950 951 if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) && 952 (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred))) { 953 /* 954 * If process is under ptrace(2) compatibility, 955 * fail the exec(2). 956 */ 957 if (p->p_proc_flag & P_PR_PTRACE) 958 goto bad; 959 /* 960 * Process is traced via /proc. 961 * Arrange to invalidate the /proc vnode. 962 */ 963 args->traceinval = 1; 964 } 965 return (0); 966 bad: 967 if (error == 0) 968 error = ENOEXEC; 969 return (error); 970 } 971 972 /* 973 * Map a section of an executable file into the user's 974 * address space. 975 */ 976 int 977 execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen, 978 off_t offset, int prot, int page, uint_t szc) 979 { 980 int error = 0; 981 off_t oldoffset; 982 caddr_t zfodbase, oldaddr; 983 size_t end, oldlen; 984 size_t zfoddiff; 985 label_t ljb; 986 proc_t *p = ttoproc(curthread); 987 988 oldaddr = addr; 989 addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 990 if (len) { 991 oldlen = len; 992 len += ((size_t)oldaddr - (size_t)addr); 993 oldoffset = offset; 994 offset = (off_t)((uintptr_t)offset & PAGEMASK); 995 if (page) { 996 spgcnt_t prefltmem, availm, npages; 997 int preread; 998 uint_t mflag = MAP_PRIVATE | MAP_FIXED; 999 1000 if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) { 1001 mflag |= MAP_TEXT; 1002 } else { 1003 mflag |= MAP_INITDATA; 1004 } 1005 1006 if (valid_usr_range(addr, len, prot, p->p_as, 1007 p->p_as->a_userlimit) != RANGE_OKAY) { 1008 error = ENOMEM; 1009 goto bad; 1010 } 1011 if (error = VOP_MAP(vp, (offset_t)offset, 1012 p->p_as, &addr, len, prot, PROT_ALL, 1013 mflag, CRED())) 1014 goto bad; 1015 1016 /* 1017 * If the segment can fit, then we prefault 1018 * the entire segment in. This is based on the 1019 * model that says the best working set of a 1020 * small program is all of its pages. 1021 */ 1022 npages = (spgcnt_t)btopr(len); 1023 prefltmem = freemem - desfree; 1024 preread = 1025 (npages < prefltmem && len < PGTHRESH) ? 1 : 0; 1026 1027 /* 1028 * If we aren't prefaulting the segment, 1029 * increment "deficit", if necessary to ensure 1030 * that pages will become available when this 1031 * process starts executing. 1032 */ 1033 availm = freemem - lotsfree; 1034 if (preread == 0 && npages > availm && 1035 deficit < lotsfree) { 1036 deficit += MIN((pgcnt_t)(npages - availm), 1037 lotsfree - deficit); 1038 } 1039 1040 if (preread) { 1041 TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD, 1042 "execmap preread:freemem %d size %lu", 1043 freemem, len); 1044 (void) as_fault(p->p_as->a_hat, p->p_as, 1045 (caddr_t)addr, len, F_INVAL, S_READ); 1046 } 1047 } else { 1048 if (valid_usr_range(addr, len, prot, p->p_as, 1049 p->p_as->a_userlimit) != RANGE_OKAY) { 1050 error = ENOMEM; 1051 goto bad; 1052 } 1053 1054 if (error = as_map(p->p_as, addr, len, 1055 segvn_create, zfod_argsp)) 1056 goto bad; 1057 /* 1058 * Read in the segment in one big chunk. 1059 */ 1060 if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr, 1061 oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0, 1062 (rlim64_t)0, CRED(), (ssize_t *)0)) 1063 goto bad; 1064 /* 1065 * Now set protections. 1066 */ 1067 if (prot != PROT_ZFOD) { 1068 (void) as_setprot(p->p_as, (caddr_t)addr, 1069 len, prot); 1070 } 1071 } 1072 } 1073 1074 if (zfodlen) { 1075 struct as *as = curproc->p_as; 1076 struct seg *seg; 1077 uint_t zprot = 0; 1078 1079 end = (size_t)addr + len; 1080 zfodbase = (caddr_t)roundup(end, PAGESIZE); 1081 zfoddiff = (uintptr_t)zfodbase - end; 1082 if (zfoddiff) { 1083 /* 1084 * Before we go to zero the remaining space on the last 1085 * page, make sure we have write permission. 1086 */ 1087 1088 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1089 seg = as_segat(curproc->p_as, (caddr_t)end); 1090 if (seg != NULL) 1091 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1, 1092 &zprot); 1093 AS_LOCK_EXIT(as, &as->a_lock); 1094 1095 if (seg != NULL && (zprot & PROT_WRITE) == 0) { 1096 (void) as_setprot(as, (caddr_t)end, 1097 zfoddiff - 1, zprot | PROT_WRITE); 1098 } 1099 1100 if (on_fault(&ljb)) { 1101 no_fault(); 1102 if (seg != NULL && (zprot & PROT_WRITE) == 0) 1103 (void) as_setprot(as, (caddr_t)end, 1104 zfoddiff - 1, zprot); 1105 error = EFAULT; 1106 goto bad; 1107 } 1108 uzero((void *)end, zfoddiff); 1109 no_fault(); 1110 if (seg != NULL && (zprot & PROT_WRITE) == 0) 1111 (void) as_setprot(as, (caddr_t)end, 1112 zfoddiff - 1, zprot); 1113 } 1114 if (zfodlen > zfoddiff) { 1115 struct segvn_crargs crargs = 1116 SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL); 1117 1118 zfodlen -= zfoddiff; 1119 if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as, 1120 p->p_as->a_userlimit) != RANGE_OKAY) { 1121 error = ENOMEM; 1122 goto bad; 1123 } 1124 if (szc > 0) { 1125 /* 1126 * ASSERT alignment because the mapelfexec() 1127 * caller for the szc > 0 case extended zfod 1128 * so it's end is pgsz aligned. 1129 */ 1130 size_t pgsz = page_get_pagesize(szc); 1131 ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz)); 1132 1133 if (IS_P2ALIGNED(zfodbase, pgsz)) { 1134 crargs.szc = szc; 1135 } else { 1136 crargs.szc = AS_MAP_HEAP; 1137 } 1138 } else { 1139 crargs.szc = AS_MAP_NO_LPOOB; 1140 } 1141 if (error = as_map(p->p_as, (caddr_t)zfodbase, 1142 zfodlen, segvn_create, &crargs)) 1143 goto bad; 1144 if (prot != PROT_ZFOD) { 1145 (void) as_setprot(p->p_as, (caddr_t)zfodbase, 1146 zfodlen, prot); 1147 } 1148 } 1149 } 1150 return (0); 1151 bad: 1152 return (error); 1153 } 1154 1155 void 1156 setexecenv(struct execenv *ep) 1157 { 1158 proc_t *p = ttoproc(curthread); 1159 klwp_t *lwp = ttolwp(curthread); 1160 struct vnode *vp; 1161 1162 p->p_bssbase = ep->ex_bssbase; 1163 p->p_brkbase = ep->ex_brkbase; 1164 p->p_brksize = ep->ex_brksize; 1165 if (p->p_exec) 1166 VN_RELE(p->p_exec); /* out with the old */ 1167 vp = p->p_exec = ep->ex_vp; 1168 if (vp != NULL) 1169 VN_HOLD(vp); /* in with the new */ 1170 1171 lwp->lwp_sigaltstack.ss_sp = 0; 1172 lwp->lwp_sigaltstack.ss_size = 0; 1173 lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 1174 } 1175 1176 int 1177 execopen(struct vnode **vpp, int *fdp) 1178 { 1179 struct vnode *vp = *vpp; 1180 file_t *fp; 1181 int error = 0; 1182 int filemode = FREAD; 1183 1184 VN_HOLD(vp); /* open reference */ 1185 if (error = falloc(NULL, filemode, &fp, fdp)) { 1186 VN_RELE(vp); 1187 *fdp = -1; /* just in case falloc changed value */ 1188 return (error); 1189 } 1190 if (error = VOP_OPEN(&vp, filemode, CRED())) { 1191 VN_RELE(vp); 1192 setf(*fdp, NULL); 1193 unfalloc(fp); 1194 *fdp = -1; 1195 return (error); 1196 } 1197 *vpp = vp; /* vnode should not have changed */ 1198 fp->f_vnode = vp; 1199 mutex_exit(&fp->f_tlock); 1200 setf(*fdp, fp); 1201 return (0); 1202 } 1203 1204 int 1205 execclose(int fd) 1206 { 1207 return (closeandsetf(fd, NULL)); 1208 } 1209 1210 1211 /* 1212 * noexec stub function. 1213 */ 1214 /*ARGSUSED*/ 1215 int 1216 noexec( 1217 struct vnode *vp, 1218 struct execa *uap, 1219 struct uarg *args, 1220 struct intpdata *idatap, 1221 int level, 1222 long *execsz, 1223 int setid, 1224 caddr_t exec_file, 1225 struct cred *cred) 1226 { 1227 cmn_err(CE_WARN, "missing exec capability for %s", uap->fname); 1228 return (ENOEXEC); 1229 } 1230 1231 /* 1232 * Support routines for building a user stack. 1233 * 1234 * execve(path, argv, envp) must construct a new stack with the specified 1235 * arguments and environment variables (see exec_args() for a description 1236 * of the user stack layout). To do this, we copy the arguments and 1237 * environment variables from the old user address space into the kernel, 1238 * free the old as, create the new as, and copy our buffered information 1239 * to the new stack. Our kernel buffer has the following structure: 1240 * 1241 * +-----------------------+ <--- stk_base + stk_size 1242 * | string offsets | 1243 * +-----------------------+ <--- stk_offp 1244 * | | 1245 * | STK_AVAIL() space | 1246 * | | 1247 * +-----------------------+ <--- stk_strp 1248 * | strings | 1249 * +-----------------------+ <--- stk_base 1250 * 1251 * When we add a string, we store the string's contents (including the null 1252 * terminator) at stk_strp, and we store the offset of the string relative to 1253 * stk_base at --stk_offp. At strings are added, stk_strp increases and 1254 * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just 1255 * the difference between these pointers. If we run out of space, we return 1256 * an error and exec_args() starts all over again with a buffer twice as large. 1257 * When we're all done, the kernel buffer looks like this: 1258 * 1259 * +-----------------------+ <--- stk_base + stk_size 1260 * | argv[0] offset | 1261 * +-----------------------+ 1262 * | ... | 1263 * +-----------------------+ 1264 * | argv[argc-1] offset | 1265 * +-----------------------+ 1266 * | envp[0] offset | 1267 * +-----------------------+ 1268 * | ... | 1269 * +-----------------------+ 1270 * | envp[envc-1] offset | 1271 * +-----------------------+ 1272 * | AT_SUN_PLATFORM offset| 1273 * +-----------------------+ 1274 * | AT_SUN_EXECNAME offset| 1275 * +-----------------------+ <--- stk_offp 1276 * | | 1277 * | STK_AVAIL() space | 1278 * | | 1279 * +-----------------------+ <--- stk_strp 1280 * | AT_SUN_EXECNAME offset| 1281 * +-----------------------+ 1282 * | AT_SUN_PLATFORM offset| 1283 * +-----------------------+ 1284 * | envp[envc-1] string | 1285 * +-----------------------+ 1286 * | ... | 1287 * +-----------------------+ 1288 * | envp[0] string | 1289 * +-----------------------+ 1290 * | argv[argc-1] string | 1291 * +-----------------------+ 1292 * | ... | 1293 * +-----------------------+ 1294 * | argv[0] string | 1295 * +-----------------------+ <--- stk_base 1296 */ 1297 1298 #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp) 1299 1300 /* 1301 * Add a string to the stack. 1302 */ 1303 static int 1304 stk_add(uarg_t *args, const char *sp, enum uio_seg segflg) 1305 { 1306 int error; 1307 size_t len; 1308 1309 if (STK_AVAIL(args) < sizeof (int)) 1310 return (E2BIG); 1311 *--args->stk_offp = args->stk_strp - args->stk_base; 1312 1313 if (segflg == UIO_USERSPACE) { 1314 error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len); 1315 if (error != 0) 1316 return (error); 1317 } else { 1318 len = strlen(sp) + 1; 1319 if (len > STK_AVAIL(args)) 1320 return (E2BIG); 1321 bcopy(sp, args->stk_strp, len); 1322 } 1323 1324 args->stk_strp += len; 1325 1326 return (0); 1327 } 1328 1329 static int 1330 stk_getptr(uarg_t *args, char *src, char **dst) 1331 { 1332 int error; 1333 1334 if (args->from_model == DATAMODEL_NATIVE) { 1335 ulong_t ptr; 1336 error = fulword(src, &ptr); 1337 *dst = (caddr_t)ptr; 1338 } else { 1339 uint32_t ptr; 1340 error = fuword32(src, &ptr); 1341 *dst = (caddr_t)(uintptr_t)ptr; 1342 } 1343 return (error); 1344 } 1345 1346 static int 1347 stk_putptr(uarg_t *args, char *addr, char *value) 1348 { 1349 if (args->to_model == DATAMODEL_NATIVE) 1350 return (sulword(addr, (ulong_t)value)); 1351 else 1352 return (suword32(addr, (uint32_t)(uintptr_t)value)); 1353 } 1354 1355 static int 1356 stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 1357 { 1358 char *sp; 1359 int argc, error; 1360 int argv_empty = 0; 1361 size_t ptrsize = args->from_ptrsize; 1362 size_t size, pad; 1363 char *argv = (char *)uap->argp; 1364 char *envp = (char *)uap->envp; 1365 1366 /* 1367 * Copy interpreter's name and argument to argv[0] and argv[1]. 1368 */ 1369 if (intp != NULL && intp->intp_name != NULL) { 1370 if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0) 1371 return (error); 1372 if (intp->intp_arg != NULL && 1373 (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0) 1374 return (error); 1375 if (args->fname != NULL) 1376 error = stk_add(args, args->fname, UIO_SYSSPACE); 1377 else 1378 error = stk_add(args, uap->fname, UIO_USERSPACE); 1379 if (error) 1380 return (error); 1381 1382 /* 1383 * Check for an empty argv[]. 1384 */ 1385 if (stk_getptr(args, argv, &sp)) 1386 return (EFAULT); 1387 if (sp == NULL) 1388 argv_empty = 1; 1389 1390 argv += ptrsize; /* ignore original argv[0] */ 1391 } 1392 1393 if (argv_empty == 0) { 1394 /* 1395 * Add argv[] strings to the stack. 1396 */ 1397 for (;;) { 1398 if (stk_getptr(args, argv, &sp)) 1399 return (EFAULT); 1400 if (sp == NULL) 1401 break; 1402 if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 1403 return (error); 1404 argv += ptrsize; 1405 } 1406 } 1407 argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 1408 args->arglen = args->stk_strp - args->stk_base; 1409 1410 /* 1411 * Add environ[] strings to the stack. 1412 */ 1413 if (envp != NULL) { 1414 for (;;) { 1415 if (stk_getptr(args, envp, &sp)) 1416 return (EFAULT); 1417 if (sp == NULL) 1418 break; 1419 if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 1420 return (error); 1421 envp += ptrsize; 1422 } 1423 } 1424 args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 1425 args->ne = args->na - argc; 1426 1427 /* 1428 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and 1429 * AT_SUN_EMULATOR strings to the stack. 1430 */ 1431 if (auxvpp != NULL && *auxvpp != NULL) { 1432 if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0) 1433 return (error); 1434 if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0) 1435 return (error); 1436 if (args->brandname != NULL && 1437 (error = stk_add(args, args->brandname, 1438 UIO_SYSSPACE)) != 0) 1439 return (error); 1440 if (args->emulator != NULL && 1441 (error = stk_add(args, args->emulator, 1442 UIO_SYSSPACE)) != 0) 1443 return (error); 1444 } 1445 1446 /* 1447 * Compute the size of the stack. This includes all the pointers, 1448 * the space reserved for the aux vector, and all the strings. 1449 * The total number of pointers is args->na (which is argc + envc) 1450 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL 1451 * after the last argument (i.e. argv[argc]); (3) the NULL after the 1452 * last environment variable (i.e. envp[envc]); and (4) the NULL after 1453 * all the strings, at the very top of the stack. 1454 */ 1455 size = (args->na + 4) * args->to_ptrsize + args->auxsize + 1456 (args->stk_strp - args->stk_base); 1457 1458 /* 1459 * Pad the string section with zeroes to align the stack size. 1460 */ 1461 pad = P2NPHASE(size, args->stk_align); 1462 1463 if (STK_AVAIL(args) < pad) 1464 return (E2BIG); 1465 1466 args->usrstack_size = size + pad; 1467 1468 while (pad-- != 0) 1469 *args->stk_strp++ = 0; 1470 1471 args->nc = args->stk_strp - args->stk_base; 1472 1473 return (0); 1474 } 1475 1476 static int 1477 stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up) 1478 { 1479 size_t ptrsize = args->to_ptrsize; 1480 ssize_t pslen; 1481 char *kstrp = args->stk_base; 1482 char *ustrp = usrstack - args->nc - ptrsize; 1483 char *usp = usrstack - args->usrstack_size; 1484 int *offp = (int *)(args->stk_base + args->stk_size); 1485 int envc = args->ne; 1486 int argc = args->na - envc; 1487 int i; 1488 1489 /* 1490 * Record argc for /proc. 1491 */ 1492 up->u_argc = argc; 1493 1494 /* 1495 * Put argc on the stack. Note that even though it's an int, 1496 * it always consumes ptrsize bytes (for alignment). 1497 */ 1498 if (stk_putptr(args, usp, (char *)(uintptr_t)argc)) 1499 return (-1); 1500 1501 /* 1502 * Add argc space (ptrsize) to usp and record argv for /proc. 1503 */ 1504 up->u_argv = (uintptr_t)(usp += ptrsize); 1505 1506 /* 1507 * Put the argv[] pointers on the stack. 1508 */ 1509 for (i = 0; i < argc; i++, usp += ptrsize) 1510 if (stk_putptr(args, usp, &ustrp[*--offp])) 1511 return (-1); 1512 1513 /* 1514 * Copy arguments to u_psargs. 1515 */ 1516 pslen = MIN(args->arglen, PSARGSZ) - 1; 1517 for (i = 0; i < pslen; i++) 1518 up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]); 1519 while (i < PSARGSZ) 1520 up->u_psargs[i++] = '\0'; 1521 1522 /* 1523 * Add space for argv[]'s NULL terminator (ptrsize) to usp and 1524 * record envp for /proc. 1525 */ 1526 up->u_envp = (uintptr_t)(usp += ptrsize); 1527 1528 /* 1529 * Put the envp[] pointers on the stack. 1530 */ 1531 for (i = 0; i < envc; i++, usp += ptrsize) 1532 if (stk_putptr(args, usp, &ustrp[*--offp])) 1533 return (-1); 1534 1535 /* 1536 * Add space for envp[]'s NULL terminator (ptrsize) to usp and 1537 * remember where the stack ends, which is also where auxv begins. 1538 */ 1539 args->stackend = usp += ptrsize; 1540 1541 /* 1542 * Put all the argv[], envp[], and auxv strings on the stack. 1543 */ 1544 if (copyout(args->stk_base, ustrp, args->nc)) 1545 return (-1); 1546 1547 /* 1548 * Fill in the aux vector now that we know the user stack addresses 1549 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and 1550 * AT_SUN_EMULATOR strings. 1551 */ 1552 if (auxvpp != NULL && *auxvpp != NULL) { 1553 if (args->to_model == DATAMODEL_NATIVE) { 1554 auxv_t **a = (auxv_t **)auxvpp; 1555 ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp]) 1556 ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp]) 1557 if (args->brandname != NULL) 1558 ADDAUX(*a, 1559 AT_SUN_BRANDNAME, (long)&ustrp[*--offp]) 1560 if (args->emulator != NULL) 1561 ADDAUX(*a, 1562 AT_SUN_EMULATOR, (long)&ustrp[*--offp]) 1563 } else { 1564 auxv32_t **a = (auxv32_t **)auxvpp; 1565 ADDAUX(*a, 1566 AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp]) 1567 ADDAUX(*a, 1568 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]) 1569 if (args->brandname != NULL) 1570 ADDAUX(*a, AT_SUN_BRANDNAME, 1571 (int)(uintptr_t)&ustrp[*--offp]) 1572 if (args->emulator != NULL) 1573 ADDAUX(*a, AT_SUN_EMULATOR, 1574 (int)(uintptr_t)&ustrp[*--offp]) 1575 } 1576 } 1577 1578 return (0); 1579 } 1580 1581 /* 1582 * Initialize a new user stack with the specified arguments and environment. 1583 * The initial user stack layout is as follows: 1584 * 1585 * User Stack 1586 * +---------------+ <--- curproc->p_usrstack 1587 * | | 1588 * | slew | 1589 * | | 1590 * +---------------+ 1591 * | NULL | 1592 * +---------------+ 1593 * | | 1594 * | auxv strings | 1595 * | | 1596 * +---------------+ 1597 * | | 1598 * | envp strings | 1599 * | | 1600 * +---------------+ 1601 * | | 1602 * | argv strings | 1603 * | | 1604 * +---------------+ <--- ustrp 1605 * | | 1606 * | aux vector | 1607 * | | 1608 * +---------------+ <--- auxv 1609 * | NULL | 1610 * +---------------+ 1611 * | envp[envc-1] | 1612 * +---------------+ 1613 * | ... | 1614 * +---------------+ 1615 * | envp[0] | 1616 * +---------------+ <--- envp[] 1617 * | NULL | 1618 * +---------------+ 1619 * | argv[argc-1] | 1620 * +---------------+ 1621 * | ... | 1622 * +---------------+ 1623 * | argv[0] | 1624 * +---------------+ <--- argv[] 1625 * | argc | 1626 * +---------------+ <--- stack base 1627 */ 1628 int 1629 exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 1630 { 1631 size_t size; 1632 int error; 1633 proc_t *p = ttoproc(curthread); 1634 user_t *up = PTOU(p); 1635 char *usrstack; 1636 rctl_entity_p_t e; 1637 struct as *as; 1638 extern int use_stk_lpg; 1639 size_t sp_slew; 1640 1641 args->from_model = p->p_model; 1642 if (p->p_model == DATAMODEL_NATIVE) { 1643 args->from_ptrsize = sizeof (long); 1644 } else { 1645 args->from_ptrsize = sizeof (int32_t); 1646 } 1647 1648 if (args->to_model == DATAMODEL_NATIVE) { 1649 args->to_ptrsize = sizeof (long); 1650 args->ncargs = NCARGS; 1651 args->stk_align = STACK_ALIGN; 1652 usrstack = (char *)USRSTACK; 1653 } else { 1654 args->to_ptrsize = sizeof (int32_t); 1655 args->ncargs = NCARGS32; 1656 args->stk_align = STACK_ALIGN32; 1657 usrstack = (char *)USRSTACK32; 1658 } 1659 1660 ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0); 1661 1662 #if defined(__sparc) 1663 /* 1664 * Make sure user register windows are empty before 1665 * attempting to make a new stack. 1666 */ 1667 (void) flush_user_windows_to_stack(NULL); 1668 #endif 1669 1670 for (size = PAGESIZE; ; size *= 2) { 1671 args->stk_size = size; 1672 args->stk_base = kmem_alloc(size, KM_SLEEP); 1673 args->stk_strp = args->stk_base; 1674 args->stk_offp = (int *)(args->stk_base + size); 1675 error = stk_copyin(uap, args, intp, auxvpp); 1676 if (error == 0) 1677 break; 1678 kmem_free(args->stk_base, size); 1679 if (error != E2BIG && error != ENAMETOOLONG) 1680 return (error); 1681 if (size >= args->ncargs) 1682 return (E2BIG); 1683 } 1684 1685 size = args->usrstack_size; 1686 1687 ASSERT(error == 0); 1688 ASSERT(P2PHASE(size, args->stk_align) == 0); 1689 ASSERT((ssize_t)STK_AVAIL(args) >= 0); 1690 1691 if (size > args->ncargs) { 1692 kmem_free(args->stk_base, args->stk_size); 1693 return (E2BIG); 1694 } 1695 1696 /* 1697 * Leave only the current lwp and force the other lwps to exit. 1698 * If another lwp beat us to the punch by calling exit(), bail out. 1699 */ 1700 if ((error = exitlwps(0)) != 0) { 1701 kmem_free(args->stk_base, args->stk_size); 1702 return (error); 1703 } 1704 1705 /* 1706 * Revoke any doors created by the process. 1707 */ 1708 if (p->p_door_list) 1709 door_exit(); 1710 1711 /* 1712 * Release schedctl data structures. 1713 */ 1714 if (p->p_pagep) 1715 schedctl_proc_cleanup(); 1716 1717 /* 1718 * Clean up any DTrace helpers for the process. 1719 */ 1720 if (p->p_dtrace_helpers != NULL) { 1721 ASSERT(dtrace_helpers_cleanup != NULL); 1722 (*dtrace_helpers_cleanup)(); 1723 } 1724 1725 mutex_enter(&p->p_lock); 1726 /* 1727 * Cleanup the DTrace provider associated with this process. 1728 */ 1729 if (p->p_dtrace_probes) { 1730 ASSERT(dtrace_fasttrap_exec_ptr != NULL); 1731 dtrace_fasttrap_exec_ptr(p); 1732 } 1733 mutex_exit(&p->p_lock); 1734 1735 /* 1736 * discard the lwpchan cache. 1737 */ 1738 if (p->p_lcp != NULL) 1739 lwpchan_destroy_cache(1); 1740 1741 /* 1742 * Delete the POSIX timers. 1743 */ 1744 if (p->p_itimer != NULL) 1745 timer_exit(); 1746 1747 #ifdef C2_AUDIT 1748 if (audit_active) 1749 audit_exec(args->stk_base, args->stk_base + args->arglen, 1750 args->na - args->ne, args->ne); 1751 #endif 1752 1753 /* 1754 * Ensure that we don't change resource associations while we 1755 * change address spaces. 1756 */ 1757 mutex_enter(&p->p_lock); 1758 pool_barrier_enter(); 1759 mutex_exit(&p->p_lock); 1760 1761 /* 1762 * Destroy the old address space and create a new one. 1763 * From here on, any errors are fatal to the exec()ing process. 1764 * On error we return -1, which means the caller must SIGKILL 1765 * the process. 1766 */ 1767 relvm(); 1768 1769 mutex_enter(&p->p_lock); 1770 pool_barrier_exit(); 1771 mutex_exit(&p->p_lock); 1772 1773 up->u_execsw = args->execswp; 1774 1775 p->p_brkbase = NULL; 1776 p->p_brksize = 0; 1777 p->p_brkpageszc = 0; 1778 p->p_stksize = 0; 1779 p->p_stkpageszc = 0; 1780 p->p_model = args->to_model; 1781 p->p_usrstack = usrstack; 1782 p->p_stkprot = args->stk_prot; 1783 p->p_datprot = args->dat_prot; 1784 1785 /* 1786 * Reset resource controls such that all controls are again active as 1787 * well as appropriate to the potentially new address model for the 1788 * process. 1789 */ 1790 e.rcep_p.proc = p; 1791 e.rcep_t = RCENTITY_PROCESS; 1792 rctl_set_reset(p->p_rctls, p, &e); 1793 1794 /* Too early to call map_pgsz for the heap */ 1795 if (use_stk_lpg) { 1796 p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0)); 1797 } 1798 1799 mutex_enter(&p->p_lock); 1800 p->p_flag |= SAUTOLPG; /* kernel controls page sizes */ 1801 mutex_exit(&p->p_lock); 1802 1803 /* 1804 * Some platforms may choose to randomize real stack start by adding a 1805 * small slew (not more than a few hundred bytes) to the top of the 1806 * stack. This helps avoid cache thrashing when identical processes 1807 * simultaneously share caches that don't provide enough associativity 1808 * (e.g. sun4v systems). In this case stack slewing makes the same hot 1809 * stack variables in different processes to live in different cache 1810 * sets increasing effective associativity. 1811 */ 1812 sp_slew = exec_get_spslew(); 1813 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0); 1814 exec_set_sp(size + sp_slew); 1815 1816 as = as_alloc(); 1817 p->p_as = as; 1818 as->a_proc = p; 1819 if (p->p_model == DATAMODEL_ILP32) 1820 as->a_userlimit = (caddr_t)USERLIMIT32; 1821 (void) hat_setup(as->a_hat, HAT_ALLOC); 1822 1823 /* 1824 * Finally, write out the contents of the new stack. 1825 */ 1826 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up); 1827 kmem_free(args->stk_base, args->stk_size); 1828 return (error); 1829 } 1830