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 /* 23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* Copyright (c) 1988 AT&T */ 27 /* All Rights Reserved */ 28 /* 29 * Copyright 2015 Garrett D'Amore <garrett@damore.org> 30 * Copyright 2019 Joyent, Inc. 31 * Copyright 2026 Oxide Computer Company 32 */ 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/sysmacros.h> 37 #include <sys/systm.h> 38 #include <sys/signal.h> 39 #include <sys/cred_impl.h> 40 #include <sys/policy.h> 41 #include <sys/user.h> 42 #include <sys/errno.h> 43 #include <sys/file.h> 44 #include <sys/vfs.h> 45 #include <sys/vnode.h> 46 #include <sys/mman.h> 47 #include <sys/acct.h> 48 #include <sys/cpuvar.h> 49 #include <sys/proc.h> 50 #include <sys/cmn_err.h> 51 #include <sys/debug.h> 52 #include <sys/pathname.h> 53 #include <sys/vm.h> 54 #include <sys/lgrp.h> 55 #include <sys/vtrace.h> 56 #include <sys/exec.h> 57 #include <sys/execx.h> 58 #include <sys/exechdr.h> 59 #include <sys/kmem.h> 60 #include <sys/prsystm.h> 61 #include <sys/modctl.h> 62 #include <sys/vmparam.h> 63 #include <sys/door.h> 64 #include <sys/schedctl.h> 65 #include <sys/utrap.h> 66 #include <sys/systeminfo.h> 67 #include <sys/stack.h> 68 #include <sys/rctl.h> 69 #include <sys/dtrace.h> 70 #include <sys/lwpchan_impl.h> 71 #include <sys/pool.h> 72 #include <sys/sdt.h> 73 #include <sys/brand.h> 74 #include <sys/klpd.h> 75 #include <sys/random.h> 76 77 #include <c2/audit.h> 78 79 #include <vm/hat.h> 80 #include <vm/anon.h> 81 #include <vm/as.h> 82 #include <vm/seg.h> 83 #include <vm/seg_vn.h> 84 #include <vm/seg_hole.h> 85 86 #define PRIV_RESET 0x01 /* needs to reset privs */ 87 #define PRIV_SETID 0x02 /* needs to change uids */ 88 #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */ 89 #define PRIV_INCREASE 0x08 /* child runs with more privs */ 90 #define MAC_FLAGS 0x10 /* need to adjust MAC flags */ 91 #define PRIV_FORCED 0x20 /* has forced privileges */ 92 93 static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *, 94 priv_set_t *, cred_t *, const char *); 95 static int hold_execsw(struct execsw *); 96 97 uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */ 98 uint_t auxv_hwcap_2 = 0; /* AT_SUN_HWCAP2 */ 99 uint_t auxv_hwcap_3 = 0; /* AT_SUN_HWCAP3 */ 100 #if defined(_SYSCALL32_IMPL) 101 uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */ 102 uint_t auxv_hwcap32_2 = 0; /* 32-bit version of auxv_hwcap2 */ 103 uint_t auxv_hwcap32_3 = 0; /* 32-bit version of auxv_hwcap3 */ 104 #endif 105 106 #define PSUIDFLAGS (SNOCD|SUGID) 107 108 /* 109 * These are consumed within the specific exec modules, but are defined here 110 * because 111 * 112 * 1) The exec modules are unloadable, which would make this near useless. 113 * 114 * 2) We want them to be common across all of them, should more than ELF come 115 * to support them. 116 * 117 * All must be powers of 2. 118 */ 119 size_t aslr_max_brk_skew = 16 * 1024 * 1024; /* 16MB */ 120 #pragma weak exec_stackgap = aslr_max_stack_skew /* Old, compatible name */ 121 size_t aslr_max_stack_skew = 64 * 1024; /* 64KB */ 122 123 /* 124 * Size of guard segment for 64-bit processes and minimum size it can be shrunk 125 * to in the case of grow() operations. These are kept as variables in case 126 * they need to be tuned in an emergency. 127 */ 128 size_t stack_guard_seg_sz = 256 * 1024 * 1024; 129 size_t stack_guard_min_sz = 64 * 1024 * 1024; 130 131 /* 132 * exece() - system call wrapper around exec_common() 133 */ 134 int 135 exece(uintptr_t file, const char **argp, const char **envp, int flags) 136 { 137 int error; 138 139 if ((flags & ~EXEC_DESCRIPTOR) != 0) 140 return (set_errno(EINVAL)); 141 142 if ((flags & EXEC_DESCRIPTOR) != 0) { 143 /* 144 * If EXEC_DESCRIPTOR is specified, then the `file` 145 * parameter is the number of a file descriptor in the current 146 * process. 147 */ 148 char *path = NULL; 149 size_t allocsize; 150 int fd = (int)file; 151 vnode_t *vp = NULL; 152 153 if ((error = fgetstartvp(fd, NULL, &vp)) != 0) 154 return (set_errno(error)); 155 156 mutex_enter(&vp->v_lock); 157 if (vp->v_path != NULL && vp->v_path != vn_vpath_empty) { 158 allocsize = strlen(vp->v_path) + 1; 159 path = kmem_alloc(allocsize, KM_NOSLEEP); 160 if (path == NULL) { 161 mutex_exit(&vp->v_lock); 162 VN_RELE(vp); 163 return (set_errno(ENOMEM)); 164 } 165 bcopy(vp->v_path, path, allocsize); 166 } 167 mutex_exit(&vp->v_lock); 168 169 /* 170 * In the unlikely event that the descriptor's path is not 171 * cached, we fall back to using a constructed one. 172 */ 173 if (path == NULL) { 174 /* 8 for "/dev/fd/", 10 for %d, + \0 == 19 */ 175 allocsize = 20; 176 path = kmem_alloc(allocsize, KM_NOSLEEP); 177 if (path == NULL) { 178 VN_RELE(vp); 179 return (set_errno(ENOMEM)); 180 } 181 (void) snprintf(path, allocsize, "/dev/fd/%d", fd); 182 } 183 184 error = exec_common(path, argp, envp, vp, EBA_NONE, 185 UIO_USERSPACE); 186 VN_RELE(vp); 187 kmem_free(path, allocsize); 188 } else { 189 const char *fname = (const char *)file; 190 191 error = exec_common(fname, argp, envp, NULL, EBA_NONE, 192 UIO_USERSPACE); 193 } 194 195 return (error ? (set_errno(error)) : 0); 196 } 197 198 int 199 exec_common(const char *fname, const char **argp, const char **envp, 200 vnode_t *vp, int brand_action, uio_seg_t seg) 201 { 202 vnode_t *dir = NULL, *tmpvp = NULL; 203 proc_t *p = ttoproc(curthread); 204 klwp_t *lwp = ttolwp(curthread); 205 struct user *up = PTOU(p); 206 size_t execsz; /* temporary count of exec size */ 207 int i; 208 int error; 209 char exec_file[MAXCOMLEN+1]; 210 struct pathname pn; 211 struct pathname resolvepn; 212 struct uarg args; 213 struct execa ua; 214 k_sigset_t savedmask; 215 lwpdir_t *lwpdir = NULL; 216 tidhash_t *tidhash; 217 lwpdir_t *old_lwpdir = NULL; 218 uint_t old_lwpdir_sz; 219 tidhash_t *old_tidhash; 220 uint_t old_tidhash_sz; 221 ret_tidhash_t *ret_tidhash; 222 lwpent_t *lep; 223 boolean_t brandme = B_FALSE; 224 225 /* 226 * exec() is not supported for the /proc agent lwp. 227 */ 228 if (curthread == p->p_agenttp) 229 return (ENOTSUP); 230 231 if (brand_action != EBA_NONE) { 232 /* 233 * Brand actions are not supported for processes that are not 234 * running in a branded zone. 235 */ 236 if (!ZONE_IS_BRANDED(p->p_zone)) 237 return (ENOTSUP); 238 239 if (brand_action == EBA_NATIVE) { 240 /* Only branded processes can be unbranded */ 241 if (!PROC_IS_BRANDED(p)) 242 return (ENOTSUP); 243 } else { 244 /* Only unbranded processes can be branded */ 245 if (PROC_IS_BRANDED(p)) 246 return (ENOTSUP); 247 brandme = B_TRUE; 248 } 249 } else { 250 /* 251 * If this is a native zone, or if the process is already 252 * branded, then we don't need to do anything. If this is 253 * a native process in a branded zone, we need to brand the 254 * process as it exec()s the new binary. 255 */ 256 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p)) 257 brandme = B_TRUE; 258 } 259 260 /* 261 * Inform /proc that an exec() has started. 262 * Hold signals that are ignored by default so that we will 263 * not be interrupted by a signal that will be ignored after 264 * successful completion of gexec(). 265 */ 266 mutex_enter(&p->p_lock); 267 prexecstart(); 268 schedctl_finish_sigblock(curthread); 269 savedmask = curthread->t_hold; 270 sigorset(&curthread->t_hold, &ignoredefault); 271 mutex_exit(&p->p_lock); 272 273 if (vp != NULL) { 274 /* 275 * When a vnode is passed in we take an extra hold here and 276 * release it before returning. This means that callers don't 277 * need to account for the reference changing over the call. 278 */ 279 VN_HOLD(vp); 280 pn_alloc(&pn); 281 pn_alloc(&resolvepn); 282 VERIFY0(pn_set(&pn, fname)); 283 VERIFY0(pn_set(&resolvepn, fname)); 284 } else { 285 /* 286 * Look up path name and remember last component for later. 287 * To help coreadm expand its %d token, we attempt to save 288 * the directory containing the executable in p_execdir. The 289 * first call to lookuppn() may fail and return EINVAL because 290 * dirvpp is non-NULL. In that case, we make a second call to 291 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL, 292 * but coreadm is allowed to expand %d to the empty string and 293 * there are other cases in which that failure may occur. 294 */ 295 if ((error = pn_get((char *)fname, seg, &pn)) != 0) 296 goto out; 297 pn_alloc(&resolvepn); 298 error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp); 299 if (error != 0) { 300 pn_free(&resolvepn); 301 pn_free(&pn); 302 if (error != EINVAL) 303 goto out; 304 305 dir = NULL; 306 if ((error = pn_get((char *)fname, seg, 307 &pn)) != 0) { 308 goto out; 309 } 310 pn_alloc(&resolvepn); 311 if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP, 312 &vp)) != 0) { 313 pn_free(&resolvepn); 314 pn_free(&pn); 315 goto out; 316 } 317 } 318 } 319 320 if (vp == NULL) { 321 if (dir != NULL) 322 VN_RELE(dir); 323 error = ENOENT; 324 pn_free(&resolvepn); 325 pn_free(&pn); 326 goto out; 327 } 328 329 if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) { 330 if (dir != NULL) 331 VN_RELE(dir); 332 pn_free(&resolvepn); 333 pn_free(&pn); 334 VN_RELE(vp); 335 goto out; 336 } 337 338 /* 339 * We do not allow executing files in attribute directories. 340 * We test this by determining whether the resolved path 341 * contains a "/" when we're in an attribute directory; 342 * only if the pathname does not contain a "/" the resolved path 343 * points to a file in the current working (attribute) directory. 344 */ 345 if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 && 346 strchr(resolvepn.pn_path, '/') == NULL) { 347 if (dir != NULL) 348 VN_RELE(dir); 349 error = EACCES; 350 pn_free(&resolvepn); 351 pn_free(&pn); 352 VN_RELE(vp); 353 goto out; 354 } 355 356 bzero(exec_file, MAXCOMLEN+1); 357 (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN); 358 bzero(&args, sizeof (args)); 359 args.argseg = seg; 360 args.pathname = resolvepn.pn_path; 361 /* don't free resolvepn until we are done with args */ 362 pn_free(&pn); 363 364 /* 365 * If we're running in a profile shell, then call pfexecd. 366 */ 367 if ((CR_FLAGS(p->p_cred) & PRIV_PFEXEC) != 0) { 368 error = pfexec_call(p->p_cred, &resolvepn, &args.pfcred, 369 &args.scrubenv); 370 371 /* Returning errno in case we're not allowed to execute. */ 372 if (error > 0) { 373 if (dir != NULL) 374 VN_RELE(dir); 375 pn_free(&resolvepn); 376 VN_RELE(vp); 377 goto out; 378 } 379 380 /* Don't change the credentials when using old ptrace. */ 381 if (args.pfcred != NULL && 382 (p->p_proc_flag & P_PR_PTRACE) != 0) { 383 crfree(args.pfcred); 384 args.pfcred = NULL; 385 args.scrubenv = B_FALSE; 386 } 387 } 388 389 /* 390 * Specific exec handlers, or policies determined via 391 * /etc/system may override the historical default. 392 */ 393 args.stk_prot = PROT_ZFOD; 394 args.dat_prot = PROT_ZFOD; 395 396 CPU_STATS_ADD_K(sys, sysexec, 1); 397 DTRACE_PROC1(exec, char *, args.pathname); 398 399 ua.fname = fname; 400 ua.argp = argp; 401 ua.envp = envp; 402 403 /* If necessary, brand this process before we start the exec. */ 404 if (brandme) 405 brand_setbrand(p); 406 407 if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz, 408 exec_file, p->p_cred, brand_action)) != 0) { 409 if (brandme) 410 brand_clearbrand(p, B_FALSE); 411 VN_RELE(vp); 412 if (dir != NULL) 413 VN_RELE(dir); 414 pn_free(&resolvepn); 415 goto fail; 416 } 417 418 /* 419 * Free floating point registers (sun4u only) 420 */ 421 ASSERT(lwp != NULL); 422 lwp_freeregs(lwp, 1); 423 424 /* 425 * Free thread and process context ops. 426 */ 427 if (curthread->t_ctx) 428 freectx(curthread, 1); 429 if (p->p_pctx) 430 freepctx(p, 1); 431 432 /* 433 * Remember file name for accounting; clear any cached DTrace predicate. 434 */ 435 up->u_acflag &= ~AFORK; 436 bcopy(exec_file, up->u_comm, MAXCOMLEN+1); 437 curthread->t_predcache = 0; 438 439 /* 440 * Clear contract template state 441 */ 442 lwp_ctmpl_clear(lwp); 443 444 /* 445 * Save the directory in which we found the executable for expanding 446 * the %d token used in core file patterns. 447 */ 448 mutex_enter(&p->p_lock); 449 tmpvp = p->p_execdir; 450 p->p_execdir = dir; 451 if (p->p_execdir != NULL) 452 VN_HOLD(p->p_execdir); 453 mutex_exit(&p->p_lock); 454 455 if (tmpvp != NULL) 456 VN_RELE(tmpvp); 457 458 /* 459 * Reset stack state to the user stack, clear set of signals 460 * caught on the signal stack, and reset list of signals that 461 * restart system calls; the new program's environment should 462 * not be affected by detritus from the old program. Any 463 * pending held signals remain held, so don't clear t_hold. 464 */ 465 mutex_enter(&p->p_lock); 466 lwp->lwp_oldcontext = 0; 467 lwp->lwp_ustack = 0; 468 lwp->lwp_old_stk_ctl = 0; 469 sigemptyset(&up->u_signodefer); 470 sigemptyset(&up->u_sigonstack); 471 sigemptyset(&up->u_sigresethand); 472 lwp->lwp_sigaltstack.ss_sp = 0; 473 lwp->lwp_sigaltstack.ss_size = 0; 474 lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 475 476 /* 477 * Make saved resource limit == current resource limit. 478 */ 479 for (i = 0; i < RLIM_NLIMITS; i++) { 480 /*CONSTCOND*/ 481 if (RLIM_SAVED(i)) { 482 (void) rctl_rlimit_get(rctlproc_legacy[i], p, 483 &up->u_saved_rlimit[i]); 484 } 485 } 486 487 /* 488 * If the action was to catch the signal, then the action 489 * must be reset to SIG_DFL. 490 */ 491 sigdefault(p); 492 /* 493 * This is the point at which exec commits to the new program. 494 * SEXECED is set here, the new address space is in place and the 495 * uarea and signal state have just been reset; the process is about 496 * to return to userland running its new image. SSPAWNING is the 497 * complement of SEXECED and a child carries it only while under 498 * construction. Therefore we clear it as SEXECED is set, under 499 * p_lock. Clearing it earlier would expose a window in which /proc 500 * sees a process with neither kas nor SSPAWNING that has not 501 * finished exec. Any later would leave a fully exec'd process still 502 * flagged as spawning. 503 */ 504 p->p_flag &= ~(SNOWAIT|SJCTL|SSPAWNING); 505 p->p_flag |= (SEXECED|SMSACCT|SMSFORK); 506 up->u_signal[SIGCLD - 1] = SIG_DFL; 507 508 /* 509 * Delete the dot4 sigqueues/signotifies. 510 */ 511 sigqfree(p); 512 513 mutex_exit(&p->p_lock); 514 515 mutex_enter(&p->p_pflock); 516 p->p_prof.pr_base = NULL; 517 p->p_prof.pr_size = 0; 518 p->p_prof.pr_off = 0; 519 p->p_prof.pr_scale = 0; 520 p->p_prof.pr_samples = 0; 521 mutex_exit(&p->p_pflock); 522 523 ASSERT(curthread->t_schedctl == NULL); 524 525 #if defined(__sparc) 526 if (p->p_utraps != NULL) 527 utrap_free(p); 528 #endif /* __sparc */ 529 530 /* 531 * Close all close-on-exec files. 532 */ 533 close_exec(P_FINFO(p)); 534 TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up); 535 536 /* Unbrand ourself if necessary. */ 537 if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE)) 538 brand_clearbrand(p, B_FALSE); 539 540 setregs(&args); 541 542 /* Mark this as an executable vnode */ 543 mutex_enter(&vp->v_lock); 544 vp->v_flag |= VVMEXEC; 545 mutex_exit(&vp->v_lock); 546 547 VN_RELE(vp); 548 if (dir != NULL) 549 VN_RELE(dir); 550 pn_free(&resolvepn); 551 552 /* 553 * Allocate a new lwp directory and lwpid hash table if necessary. 554 */ 555 if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) { 556 lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP); 557 lwpdir->ld_next = lwpdir + 1; 558 tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP); 559 if (p->p_lwpdir != NULL) 560 lep = p->p_lwpdir[curthread->t_dslot].ld_entry; 561 else 562 lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 563 } 564 565 if (PROC_IS_BRANDED(p)) 566 BROP(p)->b_exec(); 567 568 mutex_enter(&p->p_lock); 569 prbarrier(p); 570 571 /* 572 * Reset lwp id to the default value of 1. 573 * This is a single-threaded process now 574 * and lwp #1 is lwp_wait()able by default. 575 * The t_unpark flag should not be inherited. 576 */ 577 ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0); 578 curthread->t_tid = 1; 579 kpreempt_disable(); 580 ASSERT(curthread->t_lpl != NULL); 581 p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid; 582 kpreempt_enable(); 583 if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) { 584 lgrp_update_trthr_migrations(1); 585 } 586 curthread->t_unpark = 0; 587 curthread->t_proc_flag |= TP_TWAIT; 588 curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */ 589 p->p_lwpdaemon = 0; /* but oh well ... */ 590 p->p_lwpid = 1; 591 592 /* 593 * Install the newly-allocated lwp directory and lwpid hash table 594 * and insert the current thread into the new hash table. 595 */ 596 if (lwpdir != NULL) { 597 old_lwpdir = p->p_lwpdir; 598 old_lwpdir_sz = p->p_lwpdir_sz; 599 old_tidhash = p->p_tidhash; 600 old_tidhash_sz = p->p_tidhash_sz; 601 p->p_lwpdir = p->p_lwpfree = lwpdir; 602 p->p_lwpdir_sz = 2; 603 lep->le_thread = curthread; 604 lep->le_lwpid = curthread->t_tid; 605 lep->le_start = curthread->t_start; 606 lwp_hash_in(p, lep, tidhash, 2, 0); 607 p->p_tidhash = tidhash; 608 p->p_tidhash_sz = 2; 609 } 610 ret_tidhash = p->p_ret_tidhash; 611 p->p_ret_tidhash = NULL; 612 613 /* 614 * Restore the saved signal mask and 615 * inform /proc that the exec() has finished. 616 */ 617 curthread->t_hold = savedmask; 618 prexecend(); 619 mutex_exit(&p->p_lock); 620 if (old_lwpdir) { 621 kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t)); 622 kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t)); 623 } 624 while (ret_tidhash != NULL) { 625 ret_tidhash_t *next = ret_tidhash->rth_next; 626 kmem_free(ret_tidhash->rth_tidhash, 627 ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t)); 628 kmem_free(ret_tidhash, sizeof (*ret_tidhash)); 629 ret_tidhash = next; 630 } 631 632 ASSERT(error == 0); 633 DTRACE_PROC(exec__success); 634 return (0); 635 636 fail: 637 DTRACE_PROC1(exec__failure, int, error); 638 out: /* error return */ 639 mutex_enter(&p->p_lock); 640 curthread->t_hold = savedmask; 641 prexecend(); 642 mutex_exit(&p->p_lock); 643 ASSERT(error != 0); 644 return (error); 645 } 646 647 648 /* 649 * Perform generic exec duties and switchout to object-file specific 650 * handler. 651 */ 652 int 653 gexec( 654 struct vnode **vpp, 655 struct execa *uap, 656 struct uarg *args, 657 struct intpdata *idatap, 658 int level, 659 size_t *execsz, 660 caddr_t exec_file, 661 struct cred *cred, 662 int brand_action) 663 { 664 struct vnode *vp, *execvp = NULL; 665 proc_t *pp = ttoproc(curthread); 666 struct execsw *eswp; 667 int error = 0; 668 int suidflags = 0; 669 ssize_t resid; 670 uid_t uid, gid; 671 struct vattr vattr; 672 char magbuf[MAGIC_BYTES]; 673 int setid; 674 cred_t *oldcred, *newcred = NULL; 675 int privflags = 0; 676 int setidfl; 677 priv_set_t fset; 678 secflagset_t old_secflags; 679 680 secflags_copy(&old_secflags, &pp->p_secflags.psf_effective); 681 682 /* 683 * If the SNOCD or SUGID flag is set, turn it off and remember the 684 * previous setting so we can restore it if we encounter an error. 685 */ 686 if (level == 0 && (pp->p_flag & PSUIDFLAGS)) { 687 mutex_enter(&pp->p_lock); 688 suidflags = pp->p_flag & PSUIDFLAGS; 689 pp->p_flag &= ~PSUIDFLAGS; 690 mutex_exit(&pp->p_lock); 691 } 692 693 if ((error = execpermissions(*vpp, &vattr, args)) != 0) 694 goto bad_noclose; 695 696 /* need to open vnode for stateful file systems */ 697 if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0) 698 goto bad_noclose; 699 vp = *vpp; 700 701 /* 702 * Note: to support binary compatibility with SunOS a.out 703 * executables, we read in the first four bytes, as the 704 * magic number is in bytes 2-3. 705 */ 706 if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf), 707 (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 708 goto bad; 709 if (resid != 0) 710 goto bad; 711 712 if ((eswp = findexec_by_hdr(magbuf)) == NULL) 713 goto bad; 714 715 if (level == 0 && 716 (privflags = execsetid(vp, &vattr, &uid, &gid, &fset, 717 args->pfcred == NULL ? cred : args->pfcred, args->pathname)) != 0) { 718 719 /* Pfcred is a credential with a ref count of 1 */ 720 721 if (args->pfcred != NULL) { 722 privflags |= PRIV_INCREASE|PRIV_RESET; 723 newcred = cred = args->pfcred; 724 } else { 725 newcred = cred = crdup(cred); 726 } 727 728 /* If we can, drop the PA bit */ 729 if ((privflags & PRIV_RESET) != 0) 730 priv_adjust_PA(cred); 731 732 if (privflags & PRIV_SETID) { 733 cred->cr_uid = uid; 734 cred->cr_gid = gid; 735 cred->cr_suid = uid; 736 cred->cr_sgid = gid; 737 } 738 739 if (privflags & MAC_FLAGS) { 740 if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT)) 741 CR_FLAGS(cred) &= ~NET_MAC_AWARE; 742 CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT; 743 } 744 745 /* 746 * Implement the privilege updates: 747 * 748 * Restrict with L: 749 * 750 * I' = I & L 751 * 752 * E' = P' = (I' + F) & A 753 * 754 * But if running under ptrace, we cap I and F with P. 755 */ 756 if ((privflags & (PRIV_RESET|PRIV_FORCED)) != 0) { 757 if ((privflags & PRIV_INCREASE) != 0 && 758 (pp->p_proc_flag & P_PR_PTRACE) != 0) { 759 priv_intersect(&CR_OPPRIV(cred), 760 &CR_IPRIV(cred)); 761 priv_intersect(&CR_OPPRIV(cred), &fset); 762 } 763 priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred)); 764 CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred); 765 if (privflags & PRIV_FORCED) { 766 priv_set_PA(cred); 767 priv_union(&fset, &CR_EPRIV(cred)); 768 priv_union(&fset, &CR_PPRIV(cred)); 769 } 770 priv_adjust_PA(cred); 771 } 772 } else if (level == 0 && args->pfcred != NULL) { 773 newcred = cred = args->pfcred; 774 privflags |= PRIV_INCREASE; 775 /* pfcred is not forced to adhere to these settings */ 776 priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred)); 777 CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred); 778 priv_adjust_PA(cred); 779 } 780 781 /* The new image gets the inheritable secflags as its secflags */ 782 secflags_promote(pp); 783 784 /* SunOS 4.x buy-back */ 785 if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) && 786 (vattr.va_mode & (VSUID|VSGID))) { 787 char path[MAXNAMELEN]; 788 refstr_t *mntpt = NULL; 789 int ret = -1; 790 791 bzero(path, sizeof (path)); 792 zone_hold(pp->p_zone); 793 794 ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path, 795 sizeof (path), cred); 796 797 /* fallback to mountpoint if a path can't be found */ 798 if ((ret != 0) || (ret == 0 && path[0] == '\0')) 799 mntpt = vfs_getmntpoint(vp->v_vfsp); 800 801 if (mntpt == NULL) 802 zcmn_err(pp->p_zone->zone_id, CE_NOTE, 803 "!uid %d: setuid execution not allowed, " 804 "file=%s", cred->cr_uid, path); 805 else 806 zcmn_err(pp->p_zone->zone_id, CE_NOTE, 807 "!uid %d: setuid execution not allowed, " 808 "fs=%s, file=%s", cred->cr_uid, 809 ZONE_PATH_TRANSLATE(refstr_value(mntpt), 810 pp->p_zone), exec_file); 811 812 if (!INGLOBALZONE(pp)) { 813 /* zone_rootpath always has trailing / */ 814 if (mntpt == NULL) 815 cmn_err(CE_NOTE, "!zone: %s, uid: %d " 816 "setuid execution not allowed, file=%s%s", 817 pp->p_zone->zone_name, cred->cr_uid, 818 pp->p_zone->zone_rootpath, path + 1); 819 else 820 cmn_err(CE_NOTE, "!zone: %s, uid: %d " 821 "setuid execution not allowed, fs=%s, " 822 "file=%s", pp->p_zone->zone_name, 823 cred->cr_uid, refstr_value(mntpt), 824 exec_file); 825 } 826 827 if (mntpt != NULL) 828 refstr_rele(mntpt); 829 830 zone_rele(pp->p_zone); 831 } 832 833 /* 834 * execsetid() told us whether or not we had to change the 835 * credentials of the process. In privflags, it told us 836 * whether we gained any privileges or executed a set-uid executable. 837 */ 838 setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE|PRIV_FORCED)); 839 840 /* 841 * Use /etc/system variable to determine if the stack 842 * should be marked as executable by default. 843 */ 844 if ((noexec_user_stack != 0) || 845 secflag_enabled(pp, PROC_SEC_NOEXECSTACK)) 846 args->stk_prot &= ~PROT_EXEC; 847 848 args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */ 849 args->ex_vp = vp; 850 851 /* 852 * Traditionally, the setid flags told the sub processes whether 853 * the file just executed was set-uid or set-gid; this caused 854 * some confusion as the 'setid' flag did not match the SUGID 855 * process flag which is only set when the uids/gids do not match. 856 * A script set-gid/set-uid to the real uid/gid would start with 857 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH. 858 * Now we flag those cases where the calling process cannot 859 * be trusted to influence the newly exec'ed process, either 860 * because it runs with more privileges or when the uids/gids 861 * do in fact not match. 862 * This also makes the runtime linker agree with the on exec 863 * values of SNOCD and SUGID. 864 */ 865 setidfl = 0; 866 if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid && 867 !supgroupmember(cred->cr_gid, cred))) { 868 setidfl |= EXECSETID_UGIDS; 869 } 870 if (setid & PRIV_SETUGID) 871 setidfl |= EXECSETID_SETID; 872 if (setid & PRIV_FORCED) 873 setidfl |= EXECSETID_PRIVS; 874 875 execvp = pp->p_exec; 876 if (execvp) 877 VN_HOLD(execvp); 878 879 error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz, 880 setidfl, exec_file, cred, brand_action); 881 rw_exit(eswp->exec_lock); 882 if (error != 0) { 883 if (execvp) 884 VN_RELE(execvp); 885 /* 886 * If this process's p_exec has been set to the vp of 887 * the executable by exec_func, we will return without 888 * calling VOP_CLOSE because proc_exit will close it 889 * on exit. 890 */ 891 if (pp->p_exec == vp) 892 goto bad_noclose; 893 else 894 goto bad; 895 } 896 897 if (level == 0) { 898 uid_t oruid; 899 900 if (execvp != NULL) { 901 /* 902 * Close the previous executable only if we are 903 * at level 0. 904 */ 905 (void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0, 906 cred, NULL); 907 } 908 909 mutex_enter(&pp->p_crlock); 910 911 oruid = pp->p_cred->cr_ruid; 912 913 if (newcred != NULL) { 914 /* 915 * Free the old credentials, and set the new ones. 916 * Do this for both the process and the (single) thread. 917 */ 918 crfree(pp->p_cred); 919 pp->p_cred = cred; /* cred already held for proc */ 920 crhold(cred); /* hold new cred for thread */ 921 /* 922 * DTrace accesses t_cred in probe context. t_cred 923 * must always be either NULL, or point to a valid, 924 * allocated cred structure. 925 */ 926 oldcred = curthread->t_cred; 927 curthread->t_cred = cred; 928 crfree(oldcred); 929 930 if (priv_basic_test >= 0 && 931 !PRIV_ISASSERT(&CR_IPRIV(newcred), 932 priv_basic_test)) { 933 pid_t pid = pp->p_pid; 934 char *fn = PTOU(pp)->u_comm; 935 936 cmn_err(CE_WARN, "%s[%d]: exec: basic_test " 937 "privilege removed from E/I", fn, pid); 938 } 939 } 940 /* 941 * On emerging from a successful exec(), the saved 942 * uid and gid equal the effective uid and gid. 943 */ 944 cred->cr_suid = cred->cr_uid; 945 cred->cr_sgid = cred->cr_gid; 946 947 /* 948 * If the real and effective ids do not match, this 949 * is a setuid process that should not dump core. 950 * The group comparison is tricky; we prevent the code 951 * from flagging SNOCD when executing with an effective gid 952 * which is a supplementary group. 953 */ 954 if (cred->cr_ruid != cred->cr_uid || 955 (cred->cr_rgid != cred->cr_gid && 956 !supgroupmember(cred->cr_gid, cred)) || 957 (privflags & PRIV_INCREASE) != 0) 958 suidflags = PSUIDFLAGS; 959 else 960 suidflags = 0; 961 962 mutex_exit(&pp->p_crlock); 963 if (newcred != NULL && oruid != newcred->cr_ruid) { 964 /* Note that the process remains in the same zone. */ 965 mutex_enter(&pidlock); 966 upcount_dec(oruid, crgetzoneid(newcred)); 967 upcount_inc(newcred->cr_ruid, crgetzoneid(newcred)); 968 mutex_exit(&pidlock); 969 } 970 if (suidflags) { 971 mutex_enter(&pp->p_lock); 972 pp->p_flag |= suidflags; 973 mutex_exit(&pp->p_lock); 974 } 975 if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) { 976 /* 977 * If process is traced via /proc, arrange to 978 * invalidate the associated /proc vnode. 979 */ 980 if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE)) 981 args->traceinval = 1; 982 } 983 if (pp->p_proc_flag & P_PR_PTRACE) 984 psignal(pp, SIGTRAP); 985 if (args->traceinval) 986 prinvalidate(&pp->p_user); 987 } 988 if (execvp) 989 VN_RELE(execvp); 990 return (0); 991 992 bad: 993 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL); 994 995 bad_noclose: 996 if (newcred != NULL) 997 crfree(newcred); 998 if (error == 0) 999 error = ENOEXEC; 1000 1001 mutex_enter(&pp->p_lock); 1002 if (suidflags) { 1003 pp->p_flag |= suidflags; 1004 } 1005 /* 1006 * Restore the effective secflags, to maintain the invariant they 1007 * never change for a given process 1008 */ 1009 secflags_copy(&pp->p_secflags.psf_effective, &old_secflags); 1010 mutex_exit(&pp->p_lock); 1011 1012 return (error); 1013 } 1014 1015 extern char *execswnames[]; 1016 1017 struct execsw * 1018 allocate_execsw(char *name, char *magic, size_t magic_size) 1019 { 1020 int i, j; 1021 char *ename; 1022 char *magicp; 1023 1024 mutex_enter(&execsw_lock); 1025 for (i = 0; i < nexectype; i++) { 1026 if (execswnames[i] == NULL) { 1027 ename = kmem_alloc(strlen(name) + 1, KM_SLEEP); 1028 (void) strcpy(ename, name); 1029 execswnames[i] = ename; 1030 /* 1031 * Set the magic number last so that we 1032 * don't need to hold the execsw_lock in 1033 * findexectype(). 1034 */ 1035 magicp = kmem_alloc(magic_size, KM_SLEEP); 1036 for (j = 0; j < magic_size; j++) 1037 magicp[j] = magic[j]; 1038 execsw[i].exec_magic = magicp; 1039 mutex_exit(&execsw_lock); 1040 return (&execsw[i]); 1041 } 1042 } 1043 mutex_exit(&execsw_lock); 1044 return (NULL); 1045 } 1046 1047 /* 1048 * Find the exec switch table entry with the corresponding magic string. 1049 */ 1050 struct execsw * 1051 findexecsw(char *magic) 1052 { 1053 struct execsw *eswp; 1054 1055 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 1056 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 1057 if (magic && eswp->exec_maglen != 0 && 1058 bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) 1059 return (eswp); 1060 } 1061 return (NULL); 1062 } 1063 1064 /* 1065 * Find the execsw[] index for the given exec header string by looking for the 1066 * magic string at a specified offset and length for each kind of executable 1067 * file format until one matches. If no execsw[] entry is found, try to 1068 * autoload a module for this magic string. 1069 */ 1070 struct execsw * 1071 findexec_by_hdr(char *header) 1072 { 1073 struct execsw *eswp; 1074 1075 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 1076 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 1077 if (header && eswp->exec_maglen != 0 && 1078 bcmp(&header[eswp->exec_magoff], eswp->exec_magic, 1079 eswp->exec_maglen) == 0) { 1080 if (hold_execsw(eswp) != 0) 1081 return (NULL); 1082 return (eswp); 1083 } 1084 } 1085 return (NULL); /* couldn't find the type */ 1086 } 1087 1088 /* 1089 * Find the execsw[] index for the given magic string. If no execsw[] entry 1090 * is found, try to autoload a module for this magic string. 1091 */ 1092 struct execsw * 1093 findexec_by_magic(char *magic) 1094 { 1095 struct execsw *eswp; 1096 1097 for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 1098 ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 1099 if (magic && eswp->exec_maglen != 0 && 1100 bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) { 1101 if (hold_execsw(eswp) != 0) 1102 return (NULL); 1103 return (eswp); 1104 } 1105 } 1106 return (NULL); /* couldn't find the type */ 1107 } 1108 1109 static int 1110 hold_execsw(struct execsw *eswp) 1111 { 1112 char *name; 1113 1114 rw_enter(eswp->exec_lock, RW_READER); 1115 while (!LOADED_EXEC(eswp)) { 1116 rw_exit(eswp->exec_lock); 1117 name = execswnames[eswp-execsw]; 1118 ASSERT(name); 1119 if (modload("exec", name) == -1) 1120 return (-1); 1121 rw_enter(eswp->exec_lock, RW_READER); 1122 } 1123 return (0); 1124 } 1125 1126 static int 1127 execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp, 1128 priv_set_t *fset, cred_t *cr, const char *pathname) 1129 { 1130 proc_t *pp = ttoproc(curthread); 1131 uid_t uid, gid; 1132 int privflags = 0; 1133 1134 /* 1135 * Remember credentials. 1136 */ 1137 uid = cr->cr_uid; 1138 gid = cr->cr_gid; 1139 1140 /* Will try to reset the PRIV_AWARE bit later. */ 1141 if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE) 1142 privflags |= PRIV_RESET; 1143 1144 if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) { 1145 /* 1146 * If it's a set-uid root program we perform the 1147 * forced privilege look-aside. This has three possible 1148 * outcomes: 1149 * no look aside information -> treat as before 1150 * look aside in Limit set -> apply forced privs 1151 * look aside not in Limit set -> ignore set-uid root 1152 * 1153 * Ordinary set-uid root execution only allowed if the limit 1154 * set holds all unsafe privileges. 1155 */ 1156 if (vattrp->va_mode & VSUID) { 1157 if (vattrp->va_uid == 0) { 1158 int res = get_forced_privs(cr, pathname, fset); 1159 1160 switch (res) { 1161 case -1: 1162 if (priv_issubset(&priv_unsafe, 1163 &CR_LPRIV(cr))) { 1164 uid = vattrp->va_uid; 1165 privflags |= PRIV_SETUGID; 1166 } 1167 break; 1168 case 0: 1169 privflags |= PRIV_FORCED|PRIV_INCREASE; 1170 break; 1171 default: 1172 break; 1173 } 1174 } else { 1175 uid = vattrp->va_uid; 1176 privflags |= PRIV_SETUGID; 1177 } 1178 } 1179 if (vattrp->va_mode & VSGID) { 1180 gid = vattrp->va_gid; 1181 privflags |= PRIV_SETUGID; 1182 } 1183 } 1184 1185 /* 1186 * Do we need to change our credential anyway? 1187 * This is the case when E != I or P != I, as 1188 * we need to do the assignments (with F empty and A full) 1189 * Or when I is not a subset of L; in that case we need to 1190 * enforce L. 1191 * 1192 * I' = L & I 1193 * 1194 * E' = P' = (I' + F) & A 1195 * or 1196 * E' = P' = I' 1197 */ 1198 if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) || 1199 !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) || 1200 !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr))) 1201 privflags |= PRIV_RESET; 1202 1203 /* Child has more privileges than parent */ 1204 if (!priv_issubset(&CR_IPRIV(cr), &CR_PPRIV(cr))) 1205 privflags |= PRIV_INCREASE; 1206 1207 /* If MAC-aware flag(s) are on, need to update cred to remove. */ 1208 if ((CR_FLAGS(cr) & NET_MAC_AWARE) || 1209 (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT)) 1210 privflags |= MAC_FLAGS; 1211 /* 1212 * Set setuid/setgid protections if no ptrace() compatibility. 1213 * For privileged processes, honor setuid/setgid even in 1214 * the presence of ptrace() compatibility. 1215 */ 1216 if (((pp->p_proc_flag & P_PR_PTRACE) == 0 || 1217 PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) && 1218 (cr->cr_uid != uid || 1219 cr->cr_gid != gid || 1220 cr->cr_suid != uid || 1221 cr->cr_sgid != gid)) { 1222 *uidp = uid; 1223 *gidp = gid; 1224 privflags |= PRIV_SETID; 1225 } 1226 return (privflags); 1227 } 1228 1229 int 1230 execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args) 1231 { 1232 int error; 1233 proc_t *p = ttoproc(curthread); 1234 1235 vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE; 1236 if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL)) 1237 return (error); 1238 /* 1239 * Check the access mode. 1240 * If VPROC, ask /proc if the file is an object file. 1241 */ 1242 if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 || 1243 !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) || 1244 (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 || 1245 (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) { 1246 if (error == 0) 1247 error = EACCES; 1248 return (error); 1249 } 1250 1251 if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) && 1252 (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) { 1253 /* 1254 * If process is under ptrace(3C) compatibility, 1255 * fail the exec(2). 1256 */ 1257 if (p->p_proc_flag & P_PR_PTRACE) 1258 goto bad; 1259 /* 1260 * Process is traced via /proc. 1261 * Arrange to invalidate the /proc vnode. 1262 */ 1263 args->traceinval = 1; 1264 } 1265 return (0); 1266 bad: 1267 if (error == 0) 1268 error = ENOEXEC; 1269 return (error); 1270 } 1271 1272 /* 1273 * Map a section of an executable file into the user's 1274 * address space. 1275 */ 1276 int 1277 execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen, 1278 off_t offset, int prot, int page, uint_t szc) 1279 { 1280 int error = 0; 1281 off_t oldoffset; 1282 caddr_t zfodbase, oldaddr; 1283 size_t end, oldlen; 1284 size_t zfoddiff; 1285 label_t ljb; 1286 proc_t *p = ttoproc(curthread); 1287 1288 oldaddr = addr; 1289 addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1290 if (len) { 1291 oldlen = len; 1292 len += ((size_t)oldaddr - (size_t)addr); 1293 oldoffset = offset; 1294 offset = (off_t)((uintptr_t)offset & PAGEMASK); 1295 if (page) { 1296 spgcnt_t prefltmem, availm, npages; 1297 int preread; 1298 uint_t mflag = MAP_PRIVATE | MAP_FIXED; 1299 1300 if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) { 1301 mflag |= MAP_TEXT; 1302 } else { 1303 mflag |= MAP_INITDATA; 1304 } 1305 1306 if (valid_usr_range(addr, len, prot, p->p_as, 1307 p->p_as->a_userlimit) != RANGE_OKAY) { 1308 error = ENOMEM; 1309 goto bad; 1310 } 1311 if (error = VOP_MAP(vp, (offset_t)offset, 1312 p->p_as, &addr, len, prot, PROT_ALL, 1313 mflag, CRED(), NULL)) 1314 goto bad; 1315 1316 /* 1317 * If the segment can fit, then we prefault 1318 * the entire segment in. This is based on the 1319 * model that says the best working set of a 1320 * small program is all of its pages. 1321 */ 1322 npages = (spgcnt_t)btopr(len); 1323 prefltmem = freemem - desfree; 1324 preread = 1325 (npages < prefltmem && len < PGTHRESH) ? 1 : 0; 1326 1327 /* 1328 * If we aren't prefaulting the segment, 1329 * increment "deficit", if necessary to ensure 1330 * that pages will become available when this 1331 * process starts executing. 1332 */ 1333 availm = freemem - lotsfree; 1334 if (preread == 0 && npages > availm && 1335 deficit < lotsfree) { 1336 deficit += MIN((pgcnt_t)(npages - availm), 1337 lotsfree - deficit); 1338 } 1339 1340 if (preread) { 1341 TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD, 1342 "execmap preread:freemem %d size %lu", 1343 freemem, len); 1344 (void) as_fault(p->p_as->a_hat, p->p_as, 1345 (caddr_t)addr, len, F_INVAL, S_READ); 1346 } 1347 } else { 1348 if (valid_usr_range(addr, len, prot, p->p_as, 1349 p->p_as->a_userlimit) != RANGE_OKAY) { 1350 error = ENOMEM; 1351 goto bad; 1352 } 1353 1354 if (error = as_map(p->p_as, addr, len, 1355 segvn_create, zfod_argsp)) 1356 goto bad; 1357 /* 1358 * Read in the segment in one big chunk. 1359 */ 1360 if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr, 1361 oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0, 1362 (rlim64_t)0, CRED(), (ssize_t *)0)) 1363 goto bad; 1364 /* 1365 * Now set protections. 1366 */ 1367 if (prot != PROT_ZFOD) { 1368 (void) as_setprot(p->p_as, (caddr_t)addr, 1369 len, prot); 1370 } 1371 } 1372 } 1373 1374 if (zfodlen) { 1375 struct as *as = curproc->p_as; 1376 struct seg *seg; 1377 uint_t zprot = 0; 1378 1379 end = (size_t)addr + len; 1380 zfodbase = (caddr_t)roundup(end, PAGESIZE); 1381 zfoddiff = (uintptr_t)zfodbase - end; 1382 if (zfoddiff) { 1383 /* 1384 * Before we go to zero the remaining space on the last 1385 * page, make sure we have write permission. 1386 * 1387 * Normal illumos binaries don't even hit the case 1388 * where we have to change permission on the last page 1389 * since their protection is typically either 1390 * PROT_USER | PROT_WRITE | PROT_READ 1391 * or 1392 * PROT_ZFOD (same as PROT_ALL). 1393 * 1394 * We need to be careful how we zero-fill the last page 1395 * if the segment protection does not include 1396 * PROT_WRITE. Using as_setprot() can cause the VM 1397 * segment code to call segvn_vpage(), which must 1398 * allocate a page struct for each page in the segment. 1399 * If we have a very large segment, this may fail, so 1400 * we have to check for that, even though we ignore 1401 * other return values from as_setprot. 1402 */ 1403 1404 AS_LOCK_ENTER(as, RW_READER); 1405 seg = as_segat(curproc->p_as, (caddr_t)end); 1406 if (seg != NULL) 1407 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1, 1408 &zprot); 1409 AS_LOCK_EXIT(as); 1410 1411 if (seg != NULL && (zprot & PROT_WRITE) == 0) { 1412 if (as_setprot(as, (caddr_t)end, zfoddiff - 1, 1413 zprot | PROT_WRITE) == ENOMEM) { 1414 error = ENOMEM; 1415 goto bad; 1416 } 1417 } 1418 1419 if (on_fault(&ljb)) { 1420 no_fault(); 1421 if (seg != NULL && (zprot & PROT_WRITE) == 0) 1422 (void) as_setprot(as, (caddr_t)end, 1423 zfoddiff - 1, zprot); 1424 error = EFAULT; 1425 goto bad; 1426 } 1427 uzero((void *)end, zfoddiff); 1428 no_fault(); 1429 if (seg != NULL && (zprot & PROT_WRITE) == 0) 1430 (void) as_setprot(as, (caddr_t)end, 1431 zfoddiff - 1, zprot); 1432 } 1433 if (zfodlen > zfoddiff) { 1434 struct segvn_crargs crargs = 1435 SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL); 1436 1437 zfodlen -= zfoddiff; 1438 if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as, 1439 p->p_as->a_userlimit) != RANGE_OKAY) { 1440 error = ENOMEM; 1441 goto bad; 1442 } 1443 if (szc > 0) { 1444 /* 1445 * ASSERT alignment because the mapelfexec() 1446 * caller for the szc > 0 case extended zfod 1447 * so it's end is pgsz aligned. 1448 */ 1449 size_t pgsz = page_get_pagesize(szc); 1450 ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz)); 1451 1452 if (IS_P2ALIGNED(zfodbase, pgsz)) { 1453 crargs.szc = szc; 1454 } else { 1455 crargs.szc = AS_MAP_HEAP; 1456 } 1457 } else { 1458 crargs.szc = AS_MAP_NO_LPOOB; 1459 } 1460 if (error = as_map(p->p_as, (caddr_t)zfodbase, 1461 zfodlen, segvn_create, &crargs)) 1462 goto bad; 1463 if (prot != PROT_ZFOD) { 1464 (void) as_setprot(p->p_as, (caddr_t)zfodbase, 1465 zfodlen, prot); 1466 } 1467 } 1468 } 1469 return (0); 1470 bad: 1471 return (error); 1472 } 1473 1474 void 1475 setexecenv(struct execenv *ep) 1476 { 1477 proc_t *p = ttoproc(curthread); 1478 klwp_t *lwp = ttolwp(curthread); 1479 struct vnode *vp; 1480 1481 p->p_bssbase = ep->ex_bssbase; 1482 p->p_brkbase = ep->ex_brkbase; 1483 p->p_brksize = ep->ex_brksize; 1484 if (p->p_exec) 1485 VN_RELE(p->p_exec); /* out with the old */ 1486 vp = p->p_exec = ep->ex_vp; 1487 if (vp != NULL) 1488 VN_HOLD(vp); /* in with the new */ 1489 1490 lwp->lwp_sigaltstack.ss_sp = 0; 1491 lwp->lwp_sigaltstack.ss_size = 0; 1492 lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 1493 } 1494 1495 int 1496 execopen(struct vnode **vpp, int *fdp) 1497 { 1498 struct vnode *vp = *vpp; 1499 file_t *fp; 1500 int error = 0; 1501 int filemode = FREAD; 1502 1503 VN_HOLD(vp); /* open reference */ 1504 if (error = falloc(NULL, filemode, &fp, fdp)) { 1505 VN_RELE(vp); 1506 *fdp = -1; /* just in case falloc changed value */ 1507 return (error); 1508 } 1509 if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) { 1510 VN_RELE(vp); 1511 setf(*fdp, NULL); 1512 unfalloc(fp); 1513 *fdp = -1; 1514 return (error); 1515 } 1516 *vpp = vp; /* vnode should not have changed */ 1517 fp->f_vnode = vp; 1518 mutex_exit(&fp->f_tlock); 1519 setf(*fdp, fp); 1520 return (0); 1521 } 1522 1523 int 1524 execclose(int fd) 1525 { 1526 return (closeandsetf(fd, NULL)); 1527 } 1528 1529 1530 /* 1531 * noexec stub function. 1532 */ 1533 /*ARGSUSED*/ 1534 int 1535 noexec( 1536 struct vnode *vp, 1537 struct execa *uap, 1538 struct uarg *args, 1539 struct intpdata *idatap, 1540 int level, 1541 size_t *execsz, 1542 int setid, 1543 caddr_t exec_file, 1544 struct cred *cred) 1545 { 1546 cmn_err(CE_WARN, "missing exec capability for %s", uap->fname); 1547 return (ENOEXEC); 1548 } 1549 1550 /* 1551 * Support routines for building a user stack. 1552 * 1553 * execve(path, argv, envp) must construct a new stack with the specified 1554 * arguments and environment variables (see exec_args() for a description 1555 * of the user stack layout). To do this, we copy the arguments and 1556 * environment variables from the old user address space into the kernel, 1557 * free the old as, create the new as, and copy our buffered information 1558 * to the new stack. Our kernel buffer has the following structure: 1559 * 1560 * +-----------------------+ <--- stk_base + stk_size 1561 * | string offsets | 1562 * +-----------------------+ <--- stk_offp 1563 * | | 1564 * | STK_AVAIL() space | 1565 * | | 1566 * +-----------------------+ <--- stk_strp 1567 * | strings | 1568 * +-----------------------+ <--- stk_base 1569 * 1570 * When we add a string, we store the string's contents (including the null 1571 * terminator) at stk_strp, and we store the offset of the string relative to 1572 * stk_base at --stk_offp. At strings are added, stk_strp increases and 1573 * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just 1574 * the difference between these pointers. If we run out of space, we return 1575 * an error and exec_args() starts all over again with a buffer twice as large. 1576 * When we're all done, the kernel buffer looks like this: 1577 * 1578 * +-----------------------+ <--- stk_base + stk_size 1579 * | argv[0] offset | 1580 * +-----------------------+ 1581 * | ... | 1582 * +-----------------------+ 1583 * | argv[argc-1] offset | 1584 * +-----------------------+ 1585 * | envp[0] offset | 1586 * +-----------------------+ 1587 * | ... | 1588 * +-----------------------+ 1589 * | envp[envc-1] offset | 1590 * +-----------------------+ 1591 * | AT_SUN_PLATFORM offset| 1592 * +-----------------------+ 1593 * | AT_SUN_EXECNAME offset| 1594 * +-----------------------+ <--- stk_offp 1595 * | | 1596 * | STK_AVAIL() space | 1597 * | | 1598 * +-----------------------+ <--- stk_strp 1599 * | AT_SUN_EXECNAME offset| 1600 * +-----------------------+ 1601 * | AT_SUN_PLATFORM offset| 1602 * +-----------------------+ 1603 * | envp[envc-1] string | 1604 * +-----------------------+ 1605 * | ... | 1606 * +-----------------------+ 1607 * | envp[0] string | 1608 * +-----------------------+ 1609 * | argv[argc-1] string | 1610 * +-----------------------+ 1611 * | ... | 1612 * +-----------------------+ 1613 * | argv[0] string | 1614 * +-----------------------+ <--- stk_base 1615 */ 1616 1617 #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp) 1618 1619 /* 1620 * Add a string to the stack. 1621 */ 1622 static int 1623 stk_add(uarg_t *args, const char *sp, enum uio_seg segflg) 1624 { 1625 int error; 1626 size_t len; 1627 1628 if (STK_AVAIL(args) < sizeof (int)) 1629 return (E2BIG); 1630 *--args->stk_offp = args->stk_strp - args->stk_base; 1631 1632 if (segflg == UIO_USERSPACE) { 1633 error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len); 1634 if (error != 0) 1635 return (error); 1636 } else { 1637 len = strlen(sp) + 1; 1638 if (len > STK_AVAIL(args)) 1639 return (E2BIG); 1640 bcopy(sp, args->stk_strp, len); 1641 } 1642 1643 args->stk_strp += len; 1644 1645 return (0); 1646 } 1647 1648 static int 1649 stk_getptr(uarg_t *args, char *src, char **dst) 1650 { 1651 int error; 1652 1653 if (args->argseg == UIO_SYSSPACE) { 1654 *dst = *(char **)src; 1655 return (0); 1656 } 1657 1658 if (args->from_model == DATAMODEL_NATIVE) { 1659 ulong_t ptr; 1660 error = fulword(src, &ptr); 1661 *dst = (caddr_t)ptr; 1662 } else { 1663 uint32_t ptr; 1664 error = fuword32(src, &ptr); 1665 *dst = (caddr_t)(uintptr_t)ptr; 1666 } 1667 return (error); 1668 } 1669 1670 static int 1671 stk_putptr(uarg_t *args, char *addr, char *value) 1672 { 1673 if (args->to_model == DATAMODEL_NATIVE) 1674 return (sulword(addr, (ulong_t)value)); 1675 else 1676 return (suword32(addr, (uint32_t)(uintptr_t)value)); 1677 } 1678 1679 static int 1680 stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 1681 { 1682 char *sp; 1683 int argc, error; 1684 int argv_empty = 0; 1685 size_t ptrsize = args->from_ptrsize; 1686 size_t size, pad; 1687 char *argv = (char *)uap->argp; 1688 char *envp = (char *)uap->envp; 1689 1690 /* 1691 * Copy interpreter's name and argument to argv[0] and argv[1]. 1692 * In the rare case that we have nested interpreters then those names 1693 * and arguments are also copied to the subsequent slots in argv. 1694 */ 1695 if (intp != NULL && intp->intp_name[0] != NULL) { 1696 int i; 1697 1698 for (i = 0; i < INTP_MAXDEPTH; i++) { 1699 if (intp->intp_name[i] == NULL) 1700 break; 1701 error = stk_add(args, intp->intp_name[i], UIO_SYSSPACE); 1702 if (error != 0) 1703 return (error); 1704 if (intp->intp_arg[i] != NULL) { 1705 error = stk_add(args, intp->intp_arg[i], 1706 UIO_SYSSPACE); 1707 if (error != 0) 1708 return (error); 1709 } 1710 } 1711 1712 if (args->fname != NULL) 1713 error = stk_add(args, args->fname, UIO_SYSSPACE); 1714 else 1715 error = stk_add(args, uap->fname, args->argseg); 1716 if (error) 1717 return (error); 1718 1719 /* 1720 * Check for an empty argv[]. 1721 */ 1722 if (stk_getptr(args, argv, &sp)) 1723 return (EFAULT); 1724 if (sp == NULL) 1725 argv_empty = 1; 1726 1727 argv += ptrsize; /* ignore original argv[0] */ 1728 } 1729 1730 if (argv_empty == 0) { 1731 /* 1732 * Add argv[] strings to the stack. 1733 */ 1734 for (;;) { 1735 if (stk_getptr(args, argv, &sp)) 1736 return (EFAULT); 1737 if (sp == NULL) 1738 break; 1739 if ((error = stk_add(args, sp, args->argseg)) != 0) 1740 return (error); 1741 argv += ptrsize; 1742 } 1743 } 1744 argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 1745 args->arglen = args->stk_strp - args->stk_base; 1746 1747 /* 1748 * Add environ[] strings to the stack. 1749 */ 1750 if (envp != NULL) { 1751 for (;;) { 1752 char *tmp = args->stk_strp; 1753 if (stk_getptr(args, envp, &sp)) 1754 return (EFAULT); 1755 if (sp == NULL) 1756 break; 1757 if ((error = stk_add(args, sp, args->argseg)) != 0) 1758 return (error); 1759 if (args->scrubenv && strncmp(tmp, "LD_", 3) == 0) { 1760 /* Undo the copied string */ 1761 args->stk_strp = tmp; 1762 *(args->stk_offp++) = 0; 1763 } 1764 envp += ptrsize; 1765 } 1766 } 1767 args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 1768 args->ne = args->na - argc; 1769 1770 /* 1771 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and 1772 * AT_SUN_EMULATOR strings to the stack. 1773 */ 1774 if (auxvpp != NULL && *auxvpp != NULL) { 1775 if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0) 1776 return (error); 1777 if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0) 1778 return (error); 1779 if (args->brandname != NULL && 1780 (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0) 1781 return (error); 1782 if (args->emulator != NULL && 1783 (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0) 1784 return (error); 1785 } 1786 1787 /* 1788 * Compute the size of the stack. This includes all the pointers, 1789 * the space reserved for the aux vector, and all the strings. 1790 * The total number of pointers is args->na (which is argc + envc) 1791 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL 1792 * after the last argument (i.e. argv[argc]); (3) the NULL after the 1793 * last environment variable (i.e. envp[envc]); and (4) the NULL after 1794 * all the strings, at the very top of the stack. 1795 */ 1796 size = (args->na + 4) * args->to_ptrsize + args->auxsize + 1797 (args->stk_strp - args->stk_base); 1798 1799 /* 1800 * Pad the string section with zeroes to align the stack size. 1801 */ 1802 pad = P2NPHASE(size, args->stk_align); 1803 1804 if (STK_AVAIL(args) < pad) 1805 return (E2BIG); 1806 1807 args->usrstack_size = size + pad; 1808 1809 while (pad-- != 0) 1810 *args->stk_strp++ = 0; 1811 1812 args->nc = args->stk_strp - args->stk_base; 1813 1814 return (0); 1815 } 1816 1817 static int 1818 stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up) 1819 { 1820 size_t ptrsize = args->to_ptrsize; 1821 ssize_t pslen; 1822 char *kstrp = args->stk_base; 1823 char *ustrp = usrstack - args->nc - ptrsize; 1824 char *usp = usrstack - args->usrstack_size; 1825 int *offp = (int *)(args->stk_base + args->stk_size); 1826 int envc = args->ne; 1827 int argc = args->na - envc; 1828 int i; 1829 1830 /* 1831 * Record argc for /proc. 1832 */ 1833 up->u_argc = argc; 1834 1835 /* 1836 * Put argc on the stack. Note that even though it's an int, 1837 * it always consumes ptrsize bytes (for alignment). 1838 */ 1839 if (stk_putptr(args, usp, (char *)(uintptr_t)argc)) 1840 return (-1); 1841 1842 /* 1843 * Add argc space (ptrsize) to usp and record argv for /proc. 1844 */ 1845 up->u_argv = (uintptr_t)(usp += ptrsize); 1846 1847 /* 1848 * Put the argv[] pointers on the stack. 1849 */ 1850 for (i = 0; i < argc; i++, usp += ptrsize) 1851 if (stk_putptr(args, usp, &ustrp[*--offp])) 1852 return (-1); 1853 1854 /* 1855 * Copy arguments to u_psargs. 1856 */ 1857 pslen = MIN(args->arglen, PSARGSZ) - 1; 1858 for (i = 0; i < pslen; i++) 1859 up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]); 1860 while (i < PSARGSZ) 1861 up->u_psargs[i++] = '\0'; 1862 1863 /* 1864 * Add space for argv[]'s NULL terminator (ptrsize) to usp and 1865 * record envp for /proc. 1866 */ 1867 up->u_envp = (uintptr_t)(usp += ptrsize); 1868 1869 /* 1870 * Put the envp[] pointers on the stack. 1871 */ 1872 for (i = 0; i < envc; i++, usp += ptrsize) 1873 if (stk_putptr(args, usp, &ustrp[*--offp])) 1874 return (-1); 1875 1876 /* 1877 * Add space for envp[]'s NULL terminator (ptrsize) to usp and 1878 * remember where the stack ends, which is also where auxv begins. 1879 */ 1880 args->stackend = usp += ptrsize; 1881 1882 /* 1883 * Put all the argv[], envp[], and auxv strings on the stack. 1884 */ 1885 if (copyout(args->stk_base, ustrp, args->nc)) 1886 return (-1); 1887 1888 /* 1889 * Fill in the aux vector now that we know the user stack addresses 1890 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and 1891 * AT_SUN_EMULATOR strings. 1892 */ 1893 if (auxvpp != NULL && *auxvpp != NULL) { 1894 if (args->to_model == DATAMODEL_NATIVE) { 1895 auxv_t **a = (auxv_t **)auxvpp; 1896 ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp]) 1897 ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp]) 1898 if (args->brandname != NULL) 1899 ADDAUX(*a, 1900 AT_SUN_BRANDNAME, (long)&ustrp[*--offp]) 1901 if (args->emulator != NULL) 1902 ADDAUX(*a, 1903 AT_SUN_EMULATOR, (long)&ustrp[*--offp]) 1904 } else { 1905 auxv32_t **a = (auxv32_t **)auxvpp; 1906 ADDAUX(*a, 1907 AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp]) 1908 ADDAUX(*a, 1909 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]) 1910 if (args->brandname != NULL) 1911 ADDAUX(*a, AT_SUN_BRANDNAME, 1912 (int)(uintptr_t)&ustrp[*--offp]) 1913 if (args->emulator != NULL) 1914 ADDAUX(*a, AT_SUN_EMULATOR, 1915 (int)(uintptr_t)&ustrp[*--offp]) 1916 } 1917 } 1918 1919 return (0); 1920 } 1921 1922 /* 1923 * Though the actual stack base is constant, slew the %sp by a random aligned 1924 * amount in [0,aslr_max_stack_skew). Mostly, this makes life slightly more 1925 * complicated for buffer overflows hoping to overwrite the return address. 1926 * 1927 * On some platforms this helps avoid cache thrashing when identical processes 1928 * simultaneously share caches that don't provide enough associativity 1929 * (e.g. sun4v systems). In this case stack slewing makes the same hot stack 1930 * variables in different processes live in different cache sets increasing 1931 * effective associativity. 1932 */ 1933 size_t 1934 exec_get_spslew(void) 1935 { 1936 #ifdef sun4v 1937 static uint_t sp_color_stride = 16; 1938 static uint_t sp_color_mask = 0x1f; 1939 static uint_t sp_current_color = (uint_t)-1; 1940 #endif 1941 size_t off; 1942 1943 ASSERT(ISP2(aslr_max_stack_skew)); 1944 1945 if ((aslr_max_stack_skew == 0) || 1946 !secflag_enabled(curproc, PROC_SEC_ASLR)) { 1947 #ifdef sun4v 1948 uint_t spcolor = atomic_inc_32_nv(&sp_current_color); 1949 return ((size_t)((spcolor & sp_color_mask) * 1950 SA(sp_color_stride))); 1951 #else 1952 return (0); 1953 #endif 1954 } 1955 1956 (void) random_get_pseudo_bytes((uint8_t *)&off, sizeof (off)); 1957 return (SA(P2PHASE(off, aslr_max_stack_skew))); 1958 } 1959 1960 /* 1961 * Initialize a new user stack with the specified arguments and environment. 1962 * The initial user stack layout is as follows: 1963 * 1964 * User Stack 1965 * +---------------+ 1966 * | | 1967 * | stack guard | 1968 * | (64-bit only) | 1969 * | | 1970 * +...............+ <--- stack limit (base - curproc->p_stk_ctl) 1971 * . . 1972 * . . 1973 * . . 1974 * +---------------+ <--- curproc->p_usrstack 1975 * | | 1976 * | slew | 1977 * | | 1978 * +---------------+ 1979 * | NULL | 1980 * +---------------+ 1981 * | | 1982 * | auxv strings | 1983 * | | 1984 * +---------------+ 1985 * | | 1986 * | envp strings | 1987 * | | 1988 * +---------------+ 1989 * | | 1990 * | argv strings | 1991 * | | 1992 * +---------------+ <--- ustrp 1993 * | | 1994 * | aux vector | 1995 * | | 1996 * +---------------+ <--- auxv 1997 * | NULL | 1998 * +---------------+ 1999 * | envp[envc-1] | 2000 * +---------------+ 2001 * | ... | 2002 * +---------------+ 2003 * | envp[0] | 2004 * +---------------+ <--- envp[] 2005 * | NULL | 2006 * +---------------+ 2007 * | argv[argc-1] | 2008 * +---------------+ 2009 * | ... | 2010 * +---------------+ 2011 * | argv[0] | 2012 * +---------------+ <--- argv[] 2013 * | argc | 2014 * +---------------+ <--- stack base 2015 * 2016 * In 64-bit processes, a stack guard segment is allocated at the address 2017 * immediately below where the stack limit ends. This protects new library 2018 * mappings (such as the linker) from being placed in relatively dangerous 2019 * proximity to the stack. 2020 */ 2021 int 2022 exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 2023 { 2024 size_t size; 2025 int error; 2026 proc_t *p = ttoproc(curthread); 2027 user_t *up = PTOU(p); 2028 char *usrstack; 2029 rctl_entity_p_t e; 2030 struct as *as; 2031 extern int use_stk_lpg; 2032 size_t sp_slew; 2033 #if defined(_LP64) 2034 const size_t sg_sz = (stack_guard_seg_sz & PAGEMASK); 2035 #endif /* defined(_LP64) */ 2036 2037 args->from_model = p->p_model; 2038 if (args->argseg == UIO_SYSSPACE) { 2039 /* 2040 * The argument and environment vectors are in kernel memory, 2041 * so pointers within them are always native sized regardless 2042 * of the process data model. 2043 */ 2044 args->from_ptrsize = sizeof (char *); 2045 } else if (p->p_model == DATAMODEL_NATIVE) { 2046 args->from_ptrsize = sizeof (long); 2047 } else { 2048 args->from_ptrsize = sizeof (int32_t); 2049 } 2050 2051 if (args->to_model == DATAMODEL_NATIVE) { 2052 args->to_ptrsize = sizeof (long); 2053 args->ncargs = NCARGS; 2054 args->stk_align = STACK_ALIGN; 2055 if (args->addr32) 2056 usrstack = (char *)USRSTACK64_32; 2057 else 2058 usrstack = (char *)USRSTACK; 2059 } else { 2060 args->to_ptrsize = sizeof (int32_t); 2061 args->ncargs = NCARGS32; 2062 args->stk_align = STACK_ALIGN32; 2063 usrstack = (char *)USRSTACK32; 2064 } 2065 2066 ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0); 2067 2068 #if defined(__sparc) 2069 /* 2070 * Make sure user register windows are empty before 2071 * attempting to make a new stack. 2072 */ 2073 (void) flush_user_windows_to_stack(NULL); 2074 #endif 2075 2076 for (size = PAGESIZE; ; size *= 2) { 2077 args->stk_size = size; 2078 args->stk_base = kmem_alloc(size, KM_SLEEP); 2079 args->stk_strp = args->stk_base; 2080 args->stk_offp = (int *)(args->stk_base + size); 2081 error = stk_copyin(uap, args, intp, auxvpp); 2082 if (error == 0) 2083 break; 2084 kmem_free(args->stk_base, size); 2085 if (error != E2BIG && error != ENAMETOOLONG) 2086 return (error); 2087 if (size >= args->ncargs) 2088 return (E2BIG); 2089 } 2090 2091 size = args->usrstack_size; 2092 2093 ASSERT(error == 0); 2094 ASSERT(P2PHASE(size, args->stk_align) == 0); 2095 ASSERT((ssize_t)STK_AVAIL(args) >= 0); 2096 2097 if (size > args->ncargs) { 2098 kmem_free(args->stk_base, args->stk_size); 2099 return (E2BIG); 2100 } 2101 2102 /* 2103 * Leave only the current lwp and force the other lwps to exit. 2104 * If another lwp beat us to the punch by calling exit(), bail out. 2105 */ 2106 if ((error = exitlwps(0)) != 0) { 2107 kmem_free(args->stk_base, args->stk_size); 2108 return (error); 2109 } 2110 2111 /* 2112 * Revoke any doors created by the process. 2113 */ 2114 if (p->p_door_list) 2115 door_exit(); 2116 2117 /* 2118 * Release schedctl data structures. 2119 */ 2120 if (p->p_pagep) 2121 schedctl_proc_cleanup(); 2122 2123 /* 2124 * Clean up any DTrace helpers for the process. 2125 */ 2126 if (p->p_dtrace_helpers != NULL) { 2127 ASSERT(dtrace_helpers_cleanup != NULL); 2128 (*dtrace_helpers_cleanup)(p); 2129 } 2130 2131 mutex_enter(&p->p_lock); 2132 /* 2133 * Cleanup the DTrace provider associated with this process. 2134 */ 2135 if (p->p_dtrace_probes) { 2136 ASSERT(dtrace_fasttrap_exec_ptr != NULL); 2137 dtrace_fasttrap_exec_ptr(p); 2138 } 2139 mutex_exit(&p->p_lock); 2140 2141 /* 2142 * discard the lwpchan cache. 2143 */ 2144 if (p->p_lcp != NULL) 2145 lwpchan_destroy_cache(1); 2146 2147 /* 2148 * Delete the POSIX timers. 2149 */ 2150 if (p->p_itimer != NULL) 2151 timer_exit(); 2152 2153 /* 2154 * Delete the ITIMER_REALPROF interval timer. 2155 * The other ITIMER_* interval timers are specified 2156 * to be inherited across exec(). 2157 */ 2158 delete_itimer_realprof(); 2159 2160 if (AU_AUDITING()) 2161 audit_exec(args->stk_base, args->stk_base + args->arglen, 2162 args->na - args->ne, args->ne, args->pfcred); 2163 2164 /* 2165 * Ensure that we don't change resource associations while we 2166 * change address spaces. 2167 */ 2168 mutex_enter(&p->p_lock); 2169 pool_barrier_enter(); 2170 mutex_exit(&p->p_lock); 2171 2172 /* 2173 * Destroy the old address space and create a new one. 2174 * From here on, any errors are fatal to the exec()ing process. 2175 * On error we return -1, which means the caller must SIGKILL 2176 * the process. 2177 */ 2178 relvm(); 2179 2180 mutex_enter(&p->p_lock); 2181 pool_barrier_exit(); 2182 mutex_exit(&p->p_lock); 2183 2184 up->u_execsw = args->execswp; 2185 2186 p->p_brkbase = NULL; 2187 p->p_brksize = 0; 2188 p->p_brkpageszc = 0; 2189 p->p_stksize = 0; 2190 p->p_stkpageszc = 0; 2191 p->p_stkg_start = 0; 2192 p->p_stkg_end = 0; 2193 p->p_model = args->to_model; 2194 p->p_usrstack = usrstack; 2195 p->p_stkprot = args->stk_prot; 2196 p->p_datprot = args->dat_prot; 2197 2198 /* 2199 * Reset resource controls such that all controls are again active as 2200 * well as appropriate to the potentially new address model for the 2201 * process. 2202 */ 2203 e.rcep_p.proc = p; 2204 e.rcep_t = RCENTITY_PROCESS; 2205 rctl_set_reset(p->p_rctls, p, &e); 2206 2207 /* Too early to call map_pgsz for the heap */ 2208 if (use_stk_lpg) { 2209 p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0)); 2210 } 2211 2212 mutex_enter(&p->p_lock); 2213 p->p_flag |= SAUTOLPG; /* kernel controls page sizes */ 2214 mutex_exit(&p->p_lock); 2215 2216 sp_slew = exec_get_spslew(); 2217 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0); 2218 /* Be certain we don't underflow */ 2219 VERIFY((curproc->p_usrstack - (size + sp_slew)) < curproc->p_usrstack); 2220 exec_set_sp(size + sp_slew); 2221 2222 as = as_alloc(); 2223 p->p_as = as; 2224 as->a_proc = p; 2225 if (p->p_model == DATAMODEL_ILP32 || args->addr32) 2226 as->a_userlimit = (caddr_t)USERLIMIT32; 2227 (void) hat_setup(as->a_hat, HAT_ALLOC); 2228 hat_join_srd(as->a_hat, args->ex_vp); 2229 2230 /* Write out the contents of the new stack. */ 2231 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up); 2232 kmem_free(args->stk_base, args->stk_size); 2233 2234 #if defined(_LP64) 2235 /* Add stack guard segment (if needed) after successful copyout */ 2236 if (error == 0 && p->p_model == DATAMODEL_LP64 && sg_sz != 0) { 2237 seghole_crargs_t sca; 2238 caddr_t addr_end = (caddr_t)(((uintptr_t)usrstack - 2239 p->p_stk_ctl) & PAGEMASK); 2240 caddr_t addr_start = addr_end - sg_sz; 2241 2242 DTRACE_PROBE4(stack__guard__chk, proc_t *, p, 2243 caddr_t, addr_start, caddr_t, addr_end, size_t, sg_sz); 2244 2245 if (addr_end >= usrstack || addr_start >= addr_end || 2246 valid_usr_range(addr_start, sg_sz, PROT_NONE, as, 2247 as->a_userlimit) != RANGE_OKAY) { 2248 return (E2BIG); 2249 } 2250 2251 /* Create un-mappable area in AS with seg_hole */ 2252 sca.name = "stack_guard"; 2253 error = as_map(as, addr_start, sg_sz, seghole_create, &sca); 2254 if (error == 0) { 2255 p->p_stkg_start = (uintptr_t)addr_start; 2256 p->p_stkg_end = (uintptr_t)addr_start + sg_sz; 2257 } 2258 } 2259 #endif /* defined(_LP64) */ 2260 2261 return (error); 2262 } 2263