1 /*- 2 * Copyright (c) 1993, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_capsicum.h" 31 #include "opt_hwpmc_hooks.h" 32 #include "opt_ktrace.h" 33 #include "opt_vm.h" 34 35 #include <sys/param.h> 36 #include <sys/capsicum.h> 37 #include <sys/systm.h> 38 #include <sys/eventhandler.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/sysproto.h> 42 #include <sys/signalvar.h> 43 #include <sys/kernel.h> 44 #include <sys/mount.h> 45 #include <sys/filedesc.h> 46 #include <sys/fcntl.h> 47 #include <sys/acct.h> 48 #include <sys/exec.h> 49 #include <sys/imgact.h> 50 #include <sys/imgact_elf.h> 51 #include <sys/wait.h> 52 #include <sys/malloc.h> 53 #include <sys/priv.h> 54 #include <sys/proc.h> 55 #include <sys/pioctl.h> 56 #include <sys/namei.h> 57 #include <sys/resourcevar.h> 58 #include <sys/rwlock.h> 59 #include <sys/sched.h> 60 #include <sys/sdt.h> 61 #include <sys/sf_buf.h> 62 #include <sys/syscallsubr.h> 63 #include <sys/sysent.h> 64 #include <sys/shm.h> 65 #include <sys/sysctl.h> 66 #include <sys/vnode.h> 67 #include <sys/stat.h> 68 #ifdef KTRACE 69 #include <sys/ktrace.h> 70 #endif 71 72 #include <vm/vm.h> 73 #include <vm/vm_param.h> 74 #include <vm/pmap.h> 75 #include <vm/vm_page.h> 76 #include <vm/vm_map.h> 77 #include <vm/vm_kern.h> 78 #include <vm/vm_extern.h> 79 #include <vm/vm_object.h> 80 #include <vm/vm_pager.h> 81 82 #ifdef HWPMC_HOOKS 83 #include <sys/pmckern.h> 84 #endif 85 86 #include <machine/reg.h> 87 88 #include <security/audit/audit.h> 89 #include <security/mac/mac_framework.h> 90 91 #ifdef KDTRACE_HOOKS 92 #include <sys/dtrace_bsd.h> 93 dtrace_execexit_func_t dtrace_fasttrap_exec; 94 #endif 95 96 SDT_PROVIDER_DECLARE(proc); 97 SDT_PROBE_DEFINE1(proc, , , exec, "char *"); 98 SDT_PROBE_DEFINE1(proc, , , exec__failure, "int"); 99 SDT_PROBE_DEFINE1(proc, , , exec__success, "char *"); 100 101 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 102 103 int coredump_pack_fileinfo = 1; 104 SYSCTL_INT(_kern, OID_AUTO, coredump_pack_fileinfo, CTLFLAG_RWTUN, 105 &coredump_pack_fileinfo, 0, 106 "Enable file path packing in 'procstat -f' coredump notes"); 107 108 int coredump_pack_vmmapinfo = 1; 109 SYSCTL_INT(_kern, OID_AUTO, coredump_pack_vmmapinfo, CTLFLAG_RWTUN, 110 &coredump_pack_vmmapinfo, 0, 111 "Enable file path packing in 'procstat -v' coredump notes"); 112 113 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 114 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 115 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 116 static int do_execve(struct thread *td, struct image_args *args, 117 struct mac *mac_p); 118 119 /* XXX This should be vm_size_t. */ 120 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 121 NULL, 0, sysctl_kern_ps_strings, "LU", ""); 122 123 /* XXX This should be vm_size_t. */ 124 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD| 125 CTLFLAG_CAPRD, NULL, 0, sysctl_kern_usrstack, "LU", ""); 126 127 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 128 NULL, 0, sysctl_kern_stackprot, "I", ""); 129 130 u_long ps_arg_cache_limit = PAGE_SIZE / 16; 131 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 132 &ps_arg_cache_limit, 0, ""); 133 134 static int disallow_high_osrel; 135 SYSCTL_INT(_kern, OID_AUTO, disallow_high_osrel, CTLFLAG_RW, 136 &disallow_high_osrel, 0, 137 "Disallow execution of binaries built for higher version of the world"); 138 139 static int map_at_zero = 0; 140 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RWTUN, &map_at_zero, 0, 141 "Permit processes to map an object at virtual address 0."); 142 143 static int 144 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 145 { 146 struct proc *p; 147 int error; 148 149 p = curproc; 150 #ifdef SCTL_MASK32 151 if (req->flags & SCTL_MASK32) { 152 unsigned int val; 153 val = (unsigned int)p->p_sysent->sv_psstrings; 154 error = SYSCTL_OUT(req, &val, sizeof(val)); 155 } else 156 #endif 157 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 158 sizeof(p->p_sysent->sv_psstrings)); 159 return error; 160 } 161 162 static int 163 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 164 { 165 struct proc *p; 166 int error; 167 168 p = curproc; 169 #ifdef SCTL_MASK32 170 if (req->flags & SCTL_MASK32) { 171 unsigned int val; 172 val = (unsigned int)p->p_sysent->sv_usrstack; 173 error = SYSCTL_OUT(req, &val, sizeof(val)); 174 } else 175 #endif 176 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 177 sizeof(p->p_sysent->sv_usrstack)); 178 return error; 179 } 180 181 static int 182 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 183 { 184 struct proc *p; 185 186 p = curproc; 187 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 188 sizeof(p->p_sysent->sv_stackprot))); 189 } 190 191 /* 192 * Each of the items is a pointer to a `const struct execsw', hence the 193 * double pointer here. 194 */ 195 static const struct execsw **execsw; 196 197 #ifndef _SYS_SYSPROTO_H_ 198 struct execve_args { 199 char *fname; 200 char **argv; 201 char **envv; 202 }; 203 #endif 204 205 int 206 sys_execve(struct thread *td, struct execve_args *uap) 207 { 208 struct image_args args; 209 struct vmspace *oldvmspace; 210 int error; 211 212 error = pre_execve(td, &oldvmspace); 213 if (error != 0) 214 return (error); 215 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 216 uap->argv, uap->envv); 217 if (error == 0) 218 error = kern_execve(td, &args, NULL); 219 post_execve(td, error, oldvmspace); 220 return (error); 221 } 222 223 #ifndef _SYS_SYSPROTO_H_ 224 struct fexecve_args { 225 int fd; 226 char **argv; 227 char **envv; 228 } 229 #endif 230 int 231 sys_fexecve(struct thread *td, struct fexecve_args *uap) 232 { 233 struct image_args args; 234 struct vmspace *oldvmspace; 235 int error; 236 237 error = pre_execve(td, &oldvmspace); 238 if (error != 0) 239 return (error); 240 error = exec_copyin_args(&args, NULL, UIO_SYSSPACE, 241 uap->argv, uap->envv); 242 if (error == 0) { 243 args.fd = uap->fd; 244 error = kern_execve(td, &args, NULL); 245 } 246 post_execve(td, error, oldvmspace); 247 return (error); 248 } 249 250 #ifndef _SYS_SYSPROTO_H_ 251 struct __mac_execve_args { 252 char *fname; 253 char **argv; 254 char **envv; 255 struct mac *mac_p; 256 }; 257 #endif 258 259 int 260 sys___mac_execve(struct thread *td, struct __mac_execve_args *uap) 261 { 262 #ifdef MAC 263 struct image_args args; 264 struct vmspace *oldvmspace; 265 int error; 266 267 error = pre_execve(td, &oldvmspace); 268 if (error != 0) 269 return (error); 270 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 271 uap->argv, uap->envv); 272 if (error == 0) 273 error = kern_execve(td, &args, uap->mac_p); 274 post_execve(td, error, oldvmspace); 275 return (error); 276 #else 277 return (ENOSYS); 278 #endif 279 } 280 281 int 282 pre_execve(struct thread *td, struct vmspace **oldvmspace) 283 { 284 struct proc *p; 285 int error; 286 287 KASSERT(td == curthread, ("non-current thread %p", td)); 288 error = 0; 289 p = td->td_proc; 290 if ((p->p_flag & P_HADTHREADS) != 0) { 291 PROC_LOCK(p); 292 if (thread_single(p, SINGLE_BOUNDARY) != 0) 293 error = ERESTART; 294 PROC_UNLOCK(p); 295 } 296 KASSERT(error != 0 || (td->td_pflags & TDP_EXECVMSPC) == 0, 297 ("nested execve")); 298 *oldvmspace = p->p_vmspace; 299 return (error); 300 } 301 302 void 303 post_execve(struct thread *td, int error, struct vmspace *oldvmspace) 304 { 305 struct proc *p; 306 307 KASSERT(td == curthread, ("non-current thread %p", td)); 308 p = td->td_proc; 309 if ((p->p_flag & P_HADTHREADS) != 0) { 310 PROC_LOCK(p); 311 /* 312 * If success, we upgrade to SINGLE_EXIT state to 313 * force other threads to suicide. 314 */ 315 if (error == 0) 316 thread_single(p, SINGLE_EXIT); 317 else 318 thread_single_end(p, SINGLE_BOUNDARY); 319 PROC_UNLOCK(p); 320 } 321 if ((td->td_pflags & TDP_EXECVMSPC) != 0) { 322 KASSERT(p->p_vmspace != oldvmspace, 323 ("oldvmspace still used")); 324 vmspace_free(oldvmspace); 325 td->td_pflags &= ~TDP_EXECVMSPC; 326 } 327 } 328 329 /* 330 * XXX: kern_execve has the astonishing property of not always returning to 331 * the caller. If sufficiently bad things happen during the call to 332 * do_execve(), it can end up calling exit1(); as a result, callers must 333 * avoid doing anything which they might need to undo (e.g., allocating 334 * memory). 335 */ 336 int 337 kern_execve(struct thread *td, struct image_args *args, struct mac *mac_p) 338 { 339 340 AUDIT_ARG_ARGV(args->begin_argv, args->argc, 341 args->begin_envv - args->begin_argv); 342 AUDIT_ARG_ENVV(args->begin_envv, args->envc, 343 args->endp - args->begin_envv); 344 return (do_execve(td, args, mac_p)); 345 } 346 347 /* 348 * In-kernel implementation of execve(). All arguments are assumed to be 349 * userspace pointers from the passed thread. 350 */ 351 static int 352 do_execve(td, args, mac_p) 353 struct thread *td; 354 struct image_args *args; 355 struct mac *mac_p; 356 { 357 struct proc *p = td->td_proc; 358 struct nameidata nd; 359 struct ucred *oldcred; 360 struct uidinfo *euip = NULL; 361 register_t *stack_base; 362 int error, i; 363 struct image_params image_params, *imgp; 364 struct vattr attr; 365 int (*img_first)(struct image_params *); 366 struct pargs *oldargs = NULL, *newargs = NULL; 367 struct sigacts *oldsigacts = NULL, *newsigacts = NULL; 368 #ifdef KTRACE 369 struct vnode *tracevp = NULL; 370 struct ucred *tracecred = NULL; 371 #endif 372 struct vnode *oldtextvp = NULL, *newtextvp; 373 cap_rights_t rights; 374 int credential_changing; 375 int textset; 376 #ifdef MAC 377 struct label *interpvplabel = NULL; 378 int will_transition; 379 #endif 380 #ifdef HWPMC_HOOKS 381 struct pmckern_procexec pe; 382 #endif 383 static const char fexecv_proc_title[] = "(fexecv)"; 384 385 imgp = &image_params; 386 387 /* 388 * Lock the process and set the P_INEXEC flag to indicate that 389 * it should be left alone until we're done here. This is 390 * necessary to avoid race conditions - e.g. in ptrace() - 391 * that might allow a local user to illicitly obtain elevated 392 * privileges. 393 */ 394 PROC_LOCK(p); 395 KASSERT((p->p_flag & P_INEXEC) == 0, 396 ("%s(): process already has P_INEXEC flag", __func__)); 397 p->p_flag |= P_INEXEC; 398 PROC_UNLOCK(p); 399 400 /* 401 * Initialize part of the common data 402 */ 403 bzero(imgp, sizeof(*imgp)); 404 imgp->proc = p; 405 imgp->attr = &attr; 406 imgp->args = args; 407 oldcred = p->p_ucred; 408 409 #ifdef MAC 410 error = mac_execve_enter(imgp, mac_p); 411 if (error) 412 goto exec_fail; 413 #endif 414 415 /* 416 * Translate the file name. namei() returns a vnode pointer 417 * in ni_vp among other things. 418 * 419 * XXXAUDIT: It would be desirable to also audit the name of the 420 * interpreter if this is an interpreted binary. 421 */ 422 if (args->fname != NULL) { 423 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME 424 | AUDITVNODE1, UIO_SYSSPACE, args->fname, td); 425 } 426 427 SDT_PROBE1(proc, , , exec, args->fname); 428 429 interpret: 430 if (args->fname != NULL) { 431 #ifdef CAPABILITY_MODE 432 /* 433 * While capability mode can't reach this point via direct 434 * path arguments to execve(), we also don't allow 435 * interpreters to be used in capability mode (for now). 436 * Catch indirect lookups and return a permissions error. 437 */ 438 if (IN_CAPABILITY_MODE(td)) { 439 error = ECAPMODE; 440 goto exec_fail; 441 } 442 #endif 443 error = namei(&nd); 444 if (error) 445 goto exec_fail; 446 447 newtextvp = nd.ni_vp; 448 imgp->vp = newtextvp; 449 } else { 450 AUDIT_ARG_FD(args->fd); 451 /* 452 * Descriptors opened only with O_EXEC or O_RDONLY are allowed. 453 */ 454 error = fgetvp_exec(td, args->fd, 455 cap_rights_init(&rights, CAP_FEXECVE), &newtextvp); 456 if (error) 457 goto exec_fail; 458 vn_lock(newtextvp, LK_EXCLUSIVE | LK_RETRY); 459 AUDIT_ARG_VNODE1(newtextvp); 460 imgp->vp = newtextvp; 461 } 462 463 /* 464 * Check file permissions (also 'opens' file) 465 */ 466 error = exec_check_permissions(imgp); 467 if (error) 468 goto exec_fail_dealloc; 469 470 imgp->object = imgp->vp->v_object; 471 if (imgp->object != NULL) 472 vm_object_reference(imgp->object); 473 474 /* 475 * Set VV_TEXT now so no one can write to the executable while we're 476 * activating it. 477 * 478 * Remember if this was set before and unset it in case this is not 479 * actually an executable image. 480 */ 481 textset = VOP_IS_TEXT(imgp->vp); 482 VOP_SET_TEXT(imgp->vp); 483 484 error = exec_map_first_page(imgp); 485 if (error) 486 goto exec_fail_dealloc; 487 488 imgp->proc->p_osrel = 0; 489 490 /* 491 * Implement image setuid/setgid. 492 * 493 * Determine new credentials before attempting image activators 494 * so that it can be used by process_exec handlers to determine 495 * credential/setid changes. 496 * 497 * Don't honor setuid/setgid if the filesystem prohibits it or if 498 * the process is being traced. 499 * 500 * We disable setuid/setgid/etc in capability mode on the basis 501 * that most setugid applications are not written with that 502 * environment in mind, and will therefore almost certainly operate 503 * incorrectly. In principle there's no reason that setugid 504 * applications might not be useful in capability mode, so we may want 505 * to reconsider this conservative design choice in the future. 506 * 507 * XXXMAC: For the time being, use NOSUID to also prohibit 508 * transitions on the file system. 509 */ 510 credential_changing = 0; 511 credential_changing |= (attr.va_mode & S_ISUID) && 512 oldcred->cr_uid != attr.va_uid; 513 credential_changing |= (attr.va_mode & S_ISGID) && 514 oldcred->cr_gid != attr.va_gid; 515 #ifdef MAC 516 will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp, 517 interpvplabel, imgp); 518 credential_changing |= will_transition; 519 #endif 520 521 if (credential_changing && 522 #ifdef CAPABILITY_MODE 523 ((oldcred->cr_flags & CRED_FLAG_CAPMODE) == 0) && 524 #endif 525 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 526 (p->p_flag & P_TRACED) == 0) { 527 imgp->credential_setid = true; 528 VOP_UNLOCK(imgp->vp, 0); 529 imgp->newcred = crdup(oldcred); 530 if (attr.va_mode & S_ISUID) { 531 euip = uifind(attr.va_uid); 532 change_euid(imgp->newcred, euip); 533 } 534 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 535 if (attr.va_mode & S_ISGID) 536 change_egid(imgp->newcred, attr.va_gid); 537 /* 538 * Implement correct POSIX saved-id behavior. 539 * 540 * XXXMAC: Note that the current logic will save the 541 * uid and gid if a MAC domain transition occurs, even 542 * though maybe it shouldn't. 543 */ 544 change_svuid(imgp->newcred, imgp->newcred->cr_uid); 545 change_svgid(imgp->newcred, imgp->newcred->cr_gid); 546 } else { 547 /* 548 * Implement correct POSIX saved-id behavior. 549 * 550 * XXX: It's not clear that the existing behavior is 551 * POSIX-compliant. A number of sources indicate that the 552 * saved uid/gid should only be updated if the new ruid is 553 * not equal to the old ruid, or the new euid is not equal 554 * to the old euid and the new euid is not equal to the old 555 * ruid. The FreeBSD code always updates the saved uid/gid. 556 * Also, this code uses the new (replaced) euid and egid as 557 * the source, which may or may not be the right ones to use. 558 */ 559 if (oldcred->cr_svuid != oldcred->cr_uid || 560 oldcred->cr_svgid != oldcred->cr_gid) { 561 VOP_UNLOCK(imgp->vp, 0); 562 imgp->newcred = crdup(oldcred); 563 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 564 change_svuid(imgp->newcred, imgp->newcred->cr_uid); 565 change_svgid(imgp->newcred, imgp->newcred->cr_gid); 566 } 567 } 568 /* The new credentials are installed into the process later. */ 569 570 /* 571 * Do the best to calculate the full path to the image file. 572 */ 573 if (args->fname != NULL && args->fname[0] == '/') 574 imgp->execpath = args->fname; 575 else { 576 VOP_UNLOCK(imgp->vp, 0); 577 if (vn_fullpath(td, imgp->vp, &imgp->execpath, 578 &imgp->freepath) != 0) 579 imgp->execpath = args->fname; 580 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 581 } 582 583 /* 584 * If the current process has a special image activator it 585 * wants to try first, call it. For example, emulating shell 586 * scripts differently. 587 */ 588 error = -1; 589 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 590 error = img_first(imgp); 591 592 /* 593 * Loop through the list of image activators, calling each one. 594 * An activator returns -1 if there is no match, 0 on success, 595 * and an error otherwise. 596 */ 597 for (i = 0; error == -1 && execsw[i]; ++i) { 598 if (execsw[i]->ex_imgact == NULL || 599 execsw[i]->ex_imgact == img_first) { 600 continue; 601 } 602 error = (*execsw[i]->ex_imgact)(imgp); 603 } 604 605 if (error) { 606 if (error == -1) { 607 if (textset == 0) 608 VOP_UNSET_TEXT(imgp->vp); 609 error = ENOEXEC; 610 } 611 goto exec_fail_dealloc; 612 } 613 614 /* 615 * Special interpreter operation, cleanup and loop up to try to 616 * activate the interpreter. 617 */ 618 if (imgp->interpreted) { 619 exec_unmap_first_page(imgp); 620 /* 621 * VV_TEXT needs to be unset for scripts. There is a short 622 * period before we determine that something is a script where 623 * VV_TEXT will be set. The vnode lock is held over this 624 * entire period so nothing should illegitimately be blocked. 625 */ 626 VOP_UNSET_TEXT(imgp->vp); 627 /* free name buffer and old vnode */ 628 if (args->fname != NULL) 629 NDFREE(&nd, NDF_ONLY_PNBUF); 630 #ifdef MAC 631 mac_execve_interpreter_enter(newtextvp, &interpvplabel); 632 #endif 633 if (imgp->opened) { 634 VOP_CLOSE(newtextvp, FREAD, td->td_ucred, td); 635 imgp->opened = 0; 636 } 637 vput(newtextvp); 638 vm_object_deallocate(imgp->object); 639 imgp->object = NULL; 640 imgp->credential_setid = false; 641 if (imgp->newcred != NULL) { 642 crfree(imgp->newcred); 643 imgp->newcred = NULL; 644 } 645 imgp->execpath = NULL; 646 free(imgp->freepath, M_TEMP); 647 imgp->freepath = NULL; 648 /* set new name to that of the interpreter */ 649 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 650 UIO_SYSSPACE, imgp->interpreter_name, td); 651 args->fname = imgp->interpreter_name; 652 goto interpret; 653 } 654 655 /* 656 * NB: We unlock the vnode here because it is believed that none 657 * of the sv_copyout_strings/sv_fixup operations require the vnode. 658 */ 659 VOP_UNLOCK(imgp->vp, 0); 660 661 if (disallow_high_osrel && 662 P_OSREL_MAJOR(p->p_osrel) > P_OSREL_MAJOR(__FreeBSD_version)) { 663 error = ENOEXEC; 664 uprintf("Osrel %d for image %s too high\n", p->p_osrel, 665 imgp->execpath != NULL ? imgp->execpath : "<unresolved>"); 666 vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 667 goto exec_fail_dealloc; 668 } 669 670 /* ABI enforces the use of Capsicum. Switch into capabilities mode. */ 671 if (SV_PROC_FLAG(p, SV_CAPSICUM)) 672 sys_cap_enter(td, NULL); 673 674 /* 675 * Copy out strings (args and env) and initialize stack base 676 */ 677 if (p->p_sysent->sv_copyout_strings) 678 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 679 else 680 stack_base = exec_copyout_strings(imgp); 681 682 /* 683 * If custom stack fixup routine present for this process 684 * let it do the stack setup. 685 * Else stuff argument count as first item on stack 686 */ 687 if (p->p_sysent->sv_fixup != NULL) 688 (*p->p_sysent->sv_fixup)(&stack_base, imgp); 689 else 690 suword(--stack_base, imgp->args->argc); 691 692 if (args->fdp != NULL) { 693 /* Install a brand new file descriptor table. */ 694 fdinstall_remapped(td, args->fdp); 695 args->fdp = NULL; 696 } else { 697 /* 698 * Keep on using the existing file descriptor table. For 699 * security and other reasons, the file descriptor table 700 * cannot be shared after an exec. 701 */ 702 fdunshare(td); 703 /* close files on exec */ 704 fdcloseexec(td); 705 } 706 707 /* 708 * Malloc things before we need locks. 709 */ 710 i = imgp->args->begin_envv - imgp->args->begin_argv; 711 /* Cache arguments if they fit inside our allowance */ 712 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 713 newargs = pargs_alloc(i); 714 bcopy(imgp->args->begin_argv, newargs->ar_args, i); 715 } 716 717 /* 718 * For security and other reasons, signal handlers cannot 719 * be shared after an exec. The new process gets a copy of the old 720 * handlers. In execsigs(), the new process will have its signals 721 * reset. 722 */ 723 if (sigacts_shared(p->p_sigacts)) { 724 oldsigacts = p->p_sigacts; 725 newsigacts = sigacts_alloc(); 726 sigacts_copy(newsigacts, oldsigacts); 727 } 728 729 vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 730 731 PROC_LOCK(p); 732 if (oldsigacts) 733 p->p_sigacts = newsigacts; 734 /* Stop profiling */ 735 stopprofclock(p); 736 737 /* reset caught signals */ 738 execsigs(p); 739 740 /* name this process - nameiexec(p, ndp) */ 741 bzero(p->p_comm, sizeof(p->p_comm)); 742 if (args->fname) 743 bcopy(nd.ni_cnd.cn_nameptr, p->p_comm, 744 min(nd.ni_cnd.cn_namelen, MAXCOMLEN)); 745 else if (vn_commname(newtextvp, p->p_comm, sizeof(p->p_comm)) != 0) 746 bcopy(fexecv_proc_title, p->p_comm, sizeof(fexecv_proc_title)); 747 bcopy(p->p_comm, td->td_name, sizeof(td->td_name)); 748 #ifdef KTR 749 sched_clear_tdname(td); 750 #endif 751 752 /* 753 * mark as execed, wakeup the process that vforked (if any) and tell 754 * it that it now has its own resources back 755 */ 756 p->p_flag |= P_EXEC; 757 if ((p->p_flag2 & P2_NOTRACE_EXEC) == 0) 758 p->p_flag2 &= ~P2_NOTRACE; 759 if (p->p_flag & P_PPWAIT) { 760 p->p_flag &= ~(P_PPWAIT | P_PPTRACE); 761 cv_broadcast(&p->p_pwait); 762 } 763 764 /* 765 * Implement image setuid/setgid installation. 766 */ 767 if (imgp->credential_setid) { 768 /* 769 * Turn off syscall tracing for set-id programs, except for 770 * root. Record any set-id flags first to make sure that 771 * we do not regain any tracing during a possible block. 772 */ 773 setsugid(p); 774 775 #ifdef KTRACE 776 if (p->p_tracecred != NULL && 777 priv_check_cred(p->p_tracecred, PRIV_DEBUG_DIFFCRED, 0)) 778 ktrprocexec(p, &tracecred, &tracevp); 779 #endif 780 /* 781 * Close any file descriptors 0..2 that reference procfs, 782 * then make sure file descriptors 0..2 are in use. 783 * 784 * Both fdsetugidsafety() and fdcheckstd() may call functions 785 * taking sleepable locks, so temporarily drop our locks. 786 */ 787 PROC_UNLOCK(p); 788 VOP_UNLOCK(imgp->vp, 0); 789 fdsetugidsafety(td); 790 error = fdcheckstd(td); 791 vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 792 if (error != 0) 793 goto exec_fail_dealloc; 794 PROC_LOCK(p); 795 #ifdef MAC 796 if (will_transition) { 797 mac_vnode_execve_transition(oldcred, imgp->newcred, 798 imgp->vp, interpvplabel, imgp); 799 } 800 #endif 801 } else { 802 if (oldcred->cr_uid == oldcred->cr_ruid && 803 oldcred->cr_gid == oldcred->cr_rgid) 804 p->p_flag &= ~P_SUGID; 805 } 806 /* 807 * Set the new credentials. 808 */ 809 if (imgp->newcred != NULL) 810 proc_set_cred(p, imgp->newcred); 811 812 /* 813 * Store the vp for use in procfs. This vnode was referenced by namei 814 * or fgetvp_exec. 815 */ 816 oldtextvp = p->p_textvp; 817 p->p_textvp = newtextvp; 818 819 #ifdef KDTRACE_HOOKS 820 /* 821 * Tell the DTrace fasttrap provider about the exec if it 822 * has declared an interest. 823 */ 824 if (dtrace_fasttrap_exec) 825 dtrace_fasttrap_exec(p); 826 #endif 827 828 /* 829 * Notify others that we exec'd, and clear the P_INEXEC flag 830 * as we're now a bona fide freshly-execed process. 831 */ 832 KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 833 p->p_flag &= ~P_INEXEC; 834 835 /* clear "fork but no exec" flag, as we _are_ execing */ 836 p->p_acflag &= ~AFORK; 837 838 /* 839 * Free any previous argument cache and replace it with 840 * the new argument cache, if any. 841 */ 842 oldargs = p->p_args; 843 p->p_args = newargs; 844 newargs = NULL; 845 846 #ifdef HWPMC_HOOKS 847 /* 848 * Check if system-wide sampling is in effect or if the 849 * current process is using PMCs. If so, do exec() time 850 * processing. This processing needs to happen AFTER the 851 * P_INEXEC flag is cleared. 852 * 853 * The proc lock needs to be released before taking the PMC 854 * SX. 855 */ 856 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) { 857 PROC_UNLOCK(p); 858 VOP_UNLOCK(imgp->vp, 0); 859 pe.pm_credentialschanged = credential_changing; 860 pe.pm_entryaddr = imgp->entry_addr; 861 862 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe); 863 vn_lock(imgp->vp, LK_SHARED | LK_RETRY); 864 } else 865 PROC_UNLOCK(p); 866 #else /* !HWPMC_HOOKS */ 867 PROC_UNLOCK(p); 868 #endif 869 870 /* Set values passed into the program in registers. */ 871 if (p->p_sysent->sv_setregs) 872 (*p->p_sysent->sv_setregs)(td, imgp, 873 (u_long)(uintptr_t)stack_base); 874 else 875 exec_setregs(td, imgp, (u_long)(uintptr_t)stack_base); 876 877 vfs_mark_atime(imgp->vp, td->td_ucred); 878 879 SDT_PROBE1(proc, , , exec__success, args->fname); 880 881 exec_fail_dealloc: 882 if (imgp->firstpage != NULL) 883 exec_unmap_first_page(imgp); 884 885 if (imgp->vp != NULL) { 886 if (args->fname) 887 NDFREE(&nd, NDF_ONLY_PNBUF); 888 if (imgp->opened) 889 VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td); 890 if (error != 0) 891 vput(imgp->vp); 892 else 893 VOP_UNLOCK(imgp->vp, 0); 894 } 895 896 if (imgp->object != NULL) 897 vm_object_deallocate(imgp->object); 898 899 free(imgp->freepath, M_TEMP); 900 901 if (error == 0) { 902 PROC_LOCK(p); 903 td->td_dbgflags |= TDB_EXEC; 904 PROC_UNLOCK(p); 905 906 /* 907 * Stop the process here if its stop event mask has 908 * the S_EXEC bit set. 909 */ 910 STOPEVENT(p, S_EXEC, 0); 911 } else { 912 exec_fail: 913 /* we're done here, clear P_INEXEC */ 914 PROC_LOCK(p); 915 p->p_flag &= ~P_INEXEC; 916 PROC_UNLOCK(p); 917 918 SDT_PROBE1(proc, , , exec__failure, error); 919 } 920 921 if (imgp->newcred != NULL) 922 crfree(oldcred); 923 #ifdef MAC 924 mac_execve_exit(imgp); 925 mac_execve_interpreter_exit(interpvplabel); 926 #endif 927 exec_free_args(args); 928 929 /* 930 * Handle deferred decrement of ref counts. 931 */ 932 if (oldtextvp != NULL) 933 vrele(oldtextvp); 934 #ifdef KTRACE 935 if (tracevp != NULL) 936 vrele(tracevp); 937 if (tracecred != NULL) 938 crfree(tracecred); 939 #endif 940 pargs_drop(oldargs); 941 pargs_drop(newargs); 942 if (oldsigacts != NULL) 943 sigacts_free(oldsigacts); 944 if (euip != NULL) 945 uifree(euip); 946 947 if (error && imgp->vmspace_destroyed) { 948 /* sorry, no more process anymore. exit gracefully */ 949 exit1(td, 0, SIGABRT); 950 /* NOT REACHED */ 951 } 952 953 #ifdef KTRACE 954 if (error == 0) 955 ktrprocctor(p); 956 #endif 957 958 return (error); 959 } 960 961 int 962 exec_map_first_page(imgp) 963 struct image_params *imgp; 964 { 965 int rv, i, after, initial_pagein; 966 vm_page_t ma[VM_INITIAL_PAGEIN]; 967 vm_object_t object; 968 969 if (imgp->firstpage != NULL) 970 exec_unmap_first_page(imgp); 971 972 object = imgp->vp->v_object; 973 if (object == NULL) 974 return (EACCES); 975 VM_OBJECT_WLOCK(object); 976 #if VM_NRESERVLEVEL > 0 977 vm_object_color(object, 0); 978 #endif 979 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL); 980 if (ma[0]->valid != VM_PAGE_BITS_ALL) { 981 if (!vm_pager_has_page(object, 0, NULL, &after)) { 982 vm_page_lock(ma[0]); 983 vm_page_free(ma[0]); 984 vm_page_unlock(ma[0]); 985 vm_page_xunbusy(ma[0]); 986 VM_OBJECT_WUNLOCK(object); 987 return (EIO); 988 } 989 initial_pagein = min(after, VM_INITIAL_PAGEIN); 990 KASSERT(initial_pagein <= object->size, 991 ("%s: initial_pagein %d object->size %ju", 992 __func__, initial_pagein, (uintmax_t )object->size)); 993 for (i = 1; i < initial_pagein; i++) { 994 if ((ma[i] = vm_page_next(ma[i - 1])) != NULL) { 995 if (ma[i]->valid) 996 break; 997 if (vm_page_tryxbusy(ma[i])) 998 break; 999 } else { 1000 ma[i] = vm_page_alloc(object, i, 1001 VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED); 1002 if (ma[i] == NULL) 1003 break; 1004 } 1005 } 1006 initial_pagein = i; 1007 rv = vm_pager_get_pages(object, ma, initial_pagein, NULL, NULL); 1008 if (rv != VM_PAGER_OK) { 1009 for (i = 0; i < initial_pagein; i++) { 1010 vm_page_lock(ma[i]); 1011 vm_page_free(ma[i]); 1012 vm_page_unlock(ma[i]); 1013 vm_page_xunbusy(ma[i]); 1014 } 1015 VM_OBJECT_WUNLOCK(object); 1016 return (EIO); 1017 } 1018 for (i = 1; i < initial_pagein; i++) 1019 vm_page_readahead_finish(ma[i]); 1020 } 1021 vm_page_xunbusy(ma[0]); 1022 vm_page_lock(ma[0]); 1023 vm_page_hold(ma[0]); 1024 vm_page_activate(ma[0]); 1025 vm_page_unlock(ma[0]); 1026 VM_OBJECT_WUNLOCK(object); 1027 1028 imgp->firstpage = sf_buf_alloc(ma[0], 0); 1029 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 1030 1031 return (0); 1032 } 1033 1034 void 1035 exec_unmap_first_page(imgp) 1036 struct image_params *imgp; 1037 { 1038 vm_page_t m; 1039 1040 if (imgp->firstpage != NULL) { 1041 m = sf_buf_page(imgp->firstpage); 1042 sf_buf_free(imgp->firstpage); 1043 imgp->firstpage = NULL; 1044 vm_page_lock(m); 1045 vm_page_unhold(m); 1046 vm_page_unlock(m); 1047 } 1048 } 1049 1050 /* 1051 * Destroy old address space, and allocate a new stack 1052 * The new stack is only SGROWSIZ large because it is grown 1053 * automatically in trap.c. 1054 */ 1055 int 1056 exec_new_vmspace(imgp, sv) 1057 struct image_params *imgp; 1058 struct sysentvec *sv; 1059 { 1060 int error; 1061 struct proc *p = imgp->proc; 1062 struct vmspace *vmspace = p->p_vmspace; 1063 vm_object_t obj; 1064 struct rlimit rlim_stack; 1065 vm_offset_t sv_minuser, stack_addr; 1066 vm_map_t map; 1067 u_long ssiz; 1068 1069 imgp->vmspace_destroyed = 1; 1070 imgp->sysent = sv; 1071 1072 /* May be called with Giant held */ 1073 EVENTHANDLER_INVOKE(process_exec, p, imgp); 1074 1075 /* 1076 * Blow away entire process VM, if address space not shared, 1077 * otherwise, create a new VM space so that other threads are 1078 * not disrupted 1079 */ 1080 map = &vmspace->vm_map; 1081 if (map_at_zero) 1082 sv_minuser = sv->sv_minuser; 1083 else 1084 sv_minuser = MAX(sv->sv_minuser, PAGE_SIZE); 1085 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv_minuser && 1086 vm_map_max(map) == sv->sv_maxuser) { 1087 shmexit(vmspace); 1088 pmap_remove_pages(vmspace_pmap(vmspace)); 1089 vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 1090 } else { 1091 error = vmspace_exec(p, sv_minuser, sv->sv_maxuser); 1092 if (error) 1093 return (error); 1094 vmspace = p->p_vmspace; 1095 map = &vmspace->vm_map; 1096 } 1097 1098 /* Map a shared page */ 1099 obj = sv->sv_shared_page_obj; 1100 if (obj != NULL) { 1101 vm_object_reference(obj); 1102 error = vm_map_fixed(map, obj, 0, 1103 sv->sv_shared_page_base, sv->sv_shared_page_len, 1104 VM_PROT_READ | VM_PROT_EXECUTE, 1105 VM_PROT_READ | VM_PROT_EXECUTE, 1106 MAP_INHERIT_SHARE | MAP_ACC_NO_CHARGE); 1107 if (error) { 1108 vm_object_deallocate(obj); 1109 return (error); 1110 } 1111 } 1112 1113 /* Allocate a new stack */ 1114 if (imgp->stack_sz != 0) { 1115 ssiz = trunc_page(imgp->stack_sz); 1116 PROC_LOCK(p); 1117 lim_rlimit_proc(p, RLIMIT_STACK, &rlim_stack); 1118 PROC_UNLOCK(p); 1119 if (ssiz > rlim_stack.rlim_max) 1120 ssiz = rlim_stack.rlim_max; 1121 if (ssiz > rlim_stack.rlim_cur) { 1122 rlim_stack.rlim_cur = ssiz; 1123 kern_setrlimit(curthread, RLIMIT_STACK, &rlim_stack); 1124 } 1125 } else if (sv->sv_maxssiz != NULL) { 1126 ssiz = *sv->sv_maxssiz; 1127 } else { 1128 ssiz = maxssiz; 1129 } 1130 stack_addr = sv->sv_usrstack - ssiz; 1131 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz, 1132 obj != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 1133 sv->sv_stackprot, 1134 VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 1135 if (error) 1136 return (error); 1137 1138 /* 1139 * vm_ssize and vm_maxsaddr are somewhat antiquated concepts, but they 1140 * are still used to enforce the stack rlimit on the process stack. 1141 */ 1142 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 1143 vmspace->vm_maxsaddr = (char *)stack_addr; 1144 1145 return (0); 1146 } 1147 1148 /* 1149 * Copy out argument and environment strings from the old process address 1150 * space into the temporary string buffer. 1151 */ 1152 int 1153 exec_copyin_args(struct image_args *args, char *fname, 1154 enum uio_seg segflg, char **argv, char **envv) 1155 { 1156 u_long argp, envp; 1157 int error; 1158 size_t length; 1159 1160 bzero(args, sizeof(*args)); 1161 if (argv == NULL) 1162 return (EFAULT); 1163 1164 /* 1165 * Allocate demand-paged memory for the file name, argument, and 1166 * environment strings. 1167 */ 1168 error = exec_alloc_args(args); 1169 if (error != 0) 1170 return (error); 1171 1172 /* 1173 * Copy the file name. 1174 */ 1175 if (fname != NULL) { 1176 args->fname = args->buf; 1177 error = (segflg == UIO_SYSSPACE) ? 1178 copystr(fname, args->fname, PATH_MAX, &length) : 1179 copyinstr(fname, args->fname, PATH_MAX, &length); 1180 if (error != 0) 1181 goto err_exit; 1182 } else 1183 length = 0; 1184 1185 args->begin_argv = args->buf + length; 1186 args->endp = args->begin_argv; 1187 args->stringspace = ARG_MAX; 1188 1189 /* 1190 * extract arguments first 1191 */ 1192 for (;;) { 1193 error = fueword(argv++, &argp); 1194 if (error == -1) { 1195 error = EFAULT; 1196 goto err_exit; 1197 } 1198 if (argp == 0) 1199 break; 1200 error = copyinstr((void *)(uintptr_t)argp, args->endp, 1201 args->stringspace, &length); 1202 if (error != 0) { 1203 if (error == ENAMETOOLONG) 1204 error = E2BIG; 1205 goto err_exit; 1206 } 1207 args->stringspace -= length; 1208 args->endp += length; 1209 args->argc++; 1210 } 1211 1212 args->begin_envv = args->endp; 1213 1214 /* 1215 * extract environment strings 1216 */ 1217 if (envv) { 1218 for (;;) { 1219 error = fueword(envv++, &envp); 1220 if (error == -1) { 1221 error = EFAULT; 1222 goto err_exit; 1223 } 1224 if (envp == 0) 1225 break; 1226 error = copyinstr((void *)(uintptr_t)envp, 1227 args->endp, args->stringspace, &length); 1228 if (error != 0) { 1229 if (error == ENAMETOOLONG) 1230 error = E2BIG; 1231 goto err_exit; 1232 } 1233 args->stringspace -= length; 1234 args->endp += length; 1235 args->envc++; 1236 } 1237 } 1238 1239 return (0); 1240 1241 err_exit: 1242 exec_free_args(args); 1243 return (error); 1244 } 1245 1246 int 1247 exec_copyin_data_fds(struct thread *td, struct image_args *args, 1248 const void *data, size_t datalen, const int *fds, size_t fdslen) 1249 { 1250 struct filedesc *ofdp; 1251 const char *p; 1252 int *kfds; 1253 int error; 1254 1255 memset(args, '\0', sizeof(*args)); 1256 ofdp = td->td_proc->p_fd; 1257 if (datalen >= ARG_MAX || fdslen > ofdp->fd_lastfile + 1) 1258 return (E2BIG); 1259 error = exec_alloc_args(args); 1260 if (error != 0) 1261 return (error); 1262 1263 args->begin_argv = args->buf; 1264 args->stringspace = ARG_MAX; 1265 1266 if (datalen > 0) { 1267 /* 1268 * Argument buffer has been provided. Copy it into the 1269 * kernel as a single string and add a terminating null 1270 * byte. 1271 */ 1272 error = copyin(data, args->begin_argv, datalen); 1273 if (error != 0) 1274 goto err_exit; 1275 args->begin_argv[datalen] = '\0'; 1276 args->endp = args->begin_argv + datalen + 1; 1277 args->stringspace -= datalen + 1; 1278 1279 /* 1280 * Traditional argument counting. Count the number of 1281 * null bytes. 1282 */ 1283 for (p = args->begin_argv; p < args->endp; ++p) 1284 if (*p == '\0') 1285 ++args->argc; 1286 } else { 1287 /* No argument buffer provided. */ 1288 args->endp = args->begin_argv; 1289 } 1290 /* There are no environment variables. */ 1291 args->begin_envv = args->endp; 1292 1293 /* Create new file descriptor table. */ 1294 kfds = malloc(fdslen * sizeof(int), M_TEMP, M_WAITOK); 1295 error = copyin(fds, kfds, fdslen * sizeof(int)); 1296 if (error != 0) { 1297 free(kfds, M_TEMP); 1298 goto err_exit; 1299 } 1300 error = fdcopy_remapped(ofdp, kfds, fdslen, &args->fdp); 1301 free(kfds, M_TEMP); 1302 if (error != 0) 1303 goto err_exit; 1304 1305 return (0); 1306 err_exit: 1307 exec_free_args(args); 1308 return (error); 1309 } 1310 1311 /* 1312 * Allocate temporary demand-paged, zero-filled memory for the file name, 1313 * argument, and environment strings. Returns zero if the allocation succeeds 1314 * and ENOMEM otherwise. 1315 */ 1316 int 1317 exec_alloc_args(struct image_args *args) 1318 { 1319 1320 args->buf = (char *)kmap_alloc_wait(exec_map, PATH_MAX + ARG_MAX); 1321 return (args->buf != NULL ? 0 : ENOMEM); 1322 } 1323 1324 void 1325 exec_free_args(struct image_args *args) 1326 { 1327 1328 if (args->buf != NULL) { 1329 kmap_free_wakeup(exec_map, (vm_offset_t)args->buf, 1330 PATH_MAX + ARG_MAX); 1331 args->buf = NULL; 1332 } 1333 if (args->fname_buf != NULL) { 1334 free(args->fname_buf, M_TEMP); 1335 args->fname_buf = NULL; 1336 } 1337 if (args->fdp != NULL) 1338 fdescfree_remapped(args->fdp); 1339 } 1340 1341 /* 1342 * Copy strings out to the new process address space, constructing new arg 1343 * and env vector tables. Return a pointer to the base so that it can be used 1344 * as the initial stack pointer. 1345 */ 1346 register_t * 1347 exec_copyout_strings(imgp) 1348 struct image_params *imgp; 1349 { 1350 int argc, envc; 1351 char **vectp; 1352 char *stringp; 1353 uintptr_t destp; 1354 register_t *stack_base; 1355 struct ps_strings *arginfo; 1356 struct proc *p; 1357 size_t execpath_len; 1358 int szsigcode, szps; 1359 char canary[sizeof(long) * 8]; 1360 1361 szps = sizeof(pagesizes[0]) * MAXPAGESIZES; 1362 /* 1363 * Calculate string base and vector table pointers. 1364 * Also deal with signal trampoline code for this exec type. 1365 */ 1366 if (imgp->execpath != NULL && imgp->auxargs != NULL) 1367 execpath_len = strlen(imgp->execpath) + 1; 1368 else 1369 execpath_len = 0; 1370 p = imgp->proc; 1371 szsigcode = 0; 1372 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1373 if (p->p_sysent->sv_sigcode_base == 0) { 1374 if (p->p_sysent->sv_szsigcode != NULL) 1375 szsigcode = *(p->p_sysent->sv_szsigcode); 1376 } 1377 destp = (uintptr_t)arginfo; 1378 1379 /* 1380 * install sigcode 1381 */ 1382 if (szsigcode != 0) { 1383 destp -= szsigcode; 1384 destp = rounddown2(destp, sizeof(void *)); 1385 copyout(p->p_sysent->sv_sigcode, (void *)destp, szsigcode); 1386 } 1387 1388 /* 1389 * Copy the image path for the rtld. 1390 */ 1391 if (execpath_len != 0) { 1392 destp -= execpath_len; 1393 imgp->execpathp = destp; 1394 copyout(imgp->execpath, (void *)destp, execpath_len); 1395 } 1396 1397 /* 1398 * Prepare the canary for SSP. 1399 */ 1400 arc4rand(canary, sizeof(canary), 0); 1401 destp -= sizeof(canary); 1402 imgp->canary = destp; 1403 copyout(canary, (void *)destp, sizeof(canary)); 1404 imgp->canarylen = sizeof(canary); 1405 1406 /* 1407 * Prepare the pagesizes array. 1408 */ 1409 destp -= szps; 1410 destp = rounddown2(destp, sizeof(void *)); 1411 imgp->pagesizes = destp; 1412 copyout(pagesizes, (void *)destp, szps); 1413 imgp->pagesizeslen = szps; 1414 1415 destp -= ARG_MAX - imgp->args->stringspace; 1416 destp = rounddown2(destp, sizeof(void *)); 1417 1418 /* 1419 * If we have a valid auxargs ptr, prepare some room 1420 * on the stack. 1421 */ 1422 if (imgp->auxargs) { 1423 /* 1424 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1425 * lower compatibility. 1426 */ 1427 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1428 (AT_COUNT * 2); 1429 /* 1430 * The '+ 2' is for the null pointers at the end of each of 1431 * the arg and env vector sets,and imgp->auxarg_size is room 1432 * for argument of Runtime loader. 1433 */ 1434 vectp = (char **)(destp - (imgp->args->argc + 1435 imgp->args->envc + 2 + imgp->auxarg_size) 1436 * sizeof(char *)); 1437 } else { 1438 /* 1439 * The '+ 2' is for the null pointers at the end of each of 1440 * the arg and env vector sets 1441 */ 1442 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc 1443 + 2) * sizeof(char *)); 1444 } 1445 1446 /* 1447 * vectp also becomes our initial stack base 1448 */ 1449 stack_base = (register_t *)vectp; 1450 1451 stringp = imgp->args->begin_argv; 1452 argc = imgp->args->argc; 1453 envc = imgp->args->envc; 1454 1455 /* 1456 * Copy out strings - arguments and environment. 1457 */ 1458 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace); 1459 1460 /* 1461 * Fill in "ps_strings" struct for ps, w, etc. 1462 */ 1463 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 1464 suword32(&arginfo->ps_nargvstr, argc); 1465 1466 /* 1467 * Fill in argument portion of vector table. 1468 */ 1469 for (; argc > 0; --argc) { 1470 suword(vectp++, (long)(intptr_t)destp); 1471 while (*stringp++ != 0) 1472 destp++; 1473 destp++; 1474 } 1475 1476 /* a null vector table pointer separates the argp's from the envp's */ 1477 suword(vectp++, 0); 1478 1479 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 1480 suword32(&arginfo->ps_nenvstr, envc); 1481 1482 /* 1483 * Fill in environment portion of vector table. 1484 */ 1485 for (; envc > 0; --envc) { 1486 suword(vectp++, (long)(intptr_t)destp); 1487 while (*stringp++ != 0) 1488 destp++; 1489 destp++; 1490 } 1491 1492 /* end of vector table is a null pointer */ 1493 suword(vectp, 0); 1494 1495 return (stack_base); 1496 } 1497 1498 /* 1499 * Check permissions of file to execute. 1500 * Called with imgp->vp locked. 1501 * Return 0 for success or error code on failure. 1502 */ 1503 int 1504 exec_check_permissions(imgp) 1505 struct image_params *imgp; 1506 { 1507 struct vnode *vp = imgp->vp; 1508 struct vattr *attr = imgp->attr; 1509 struct thread *td; 1510 int error, writecount; 1511 1512 td = curthread; 1513 1514 /* Get file attributes */ 1515 error = VOP_GETATTR(vp, attr, td->td_ucred); 1516 if (error) 1517 return (error); 1518 1519 #ifdef MAC 1520 error = mac_vnode_check_exec(td->td_ucred, imgp->vp, imgp); 1521 if (error) 1522 return (error); 1523 #endif 1524 1525 /* 1526 * 1) Check if file execution is disabled for the filesystem that 1527 * this file resides on. 1528 * 2) Ensure that at least one execute bit is on. Otherwise, a 1529 * privileged user will always succeed, and we don't want this 1530 * to happen unless the file really is executable. 1531 * 3) Ensure that the file is a regular file. 1532 */ 1533 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 1534 (attr->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0 || 1535 (attr->va_type != VREG)) 1536 return (EACCES); 1537 1538 /* 1539 * Zero length files can't be exec'd 1540 */ 1541 if (attr->va_size == 0) 1542 return (ENOEXEC); 1543 1544 /* 1545 * Check for execute permission to file based on current credentials. 1546 */ 1547 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 1548 if (error) 1549 return (error); 1550 1551 /* 1552 * Check number of open-for-writes on the file and deny execution 1553 * if there are any. 1554 */ 1555 error = VOP_GET_WRITECOUNT(vp, &writecount); 1556 if (error != 0) 1557 return (error); 1558 if (writecount != 0) 1559 return (ETXTBSY); 1560 1561 /* 1562 * Call filesystem specific open routine (which does nothing in the 1563 * general case). 1564 */ 1565 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 1566 if (error == 0) 1567 imgp->opened = 1; 1568 return (error); 1569 } 1570 1571 /* 1572 * Exec handler registration 1573 */ 1574 int 1575 exec_register(execsw_arg) 1576 const struct execsw *execsw_arg; 1577 { 1578 const struct execsw **es, **xs, **newexecsw; 1579 int count = 2; /* New slot and trailing NULL */ 1580 1581 if (execsw) 1582 for (es = execsw; *es; es++) 1583 count++; 1584 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1585 xs = newexecsw; 1586 if (execsw) 1587 for (es = execsw; *es; es++) 1588 *xs++ = *es; 1589 *xs++ = execsw_arg; 1590 *xs = NULL; 1591 if (execsw) 1592 free(execsw, M_TEMP); 1593 execsw = newexecsw; 1594 return (0); 1595 } 1596 1597 int 1598 exec_unregister(execsw_arg) 1599 const struct execsw *execsw_arg; 1600 { 1601 const struct execsw **es, **xs, **newexecsw; 1602 int count = 1; 1603 1604 if (execsw == NULL) 1605 panic("unregister with no handlers left?\n"); 1606 1607 for (es = execsw; *es; es++) { 1608 if (*es == execsw_arg) 1609 break; 1610 } 1611 if (*es == NULL) 1612 return (ENOENT); 1613 for (es = execsw; *es; es++) 1614 if (*es != execsw_arg) 1615 count++; 1616 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1617 xs = newexecsw; 1618 for (es = execsw; *es; es++) 1619 if (*es != execsw_arg) 1620 *xs++ = *es; 1621 *xs = NULL; 1622 if (execsw) 1623 free(execsw, M_TEMP); 1624 execsw = newexecsw; 1625 return (0); 1626 } 1627