| 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 | * $FreeBSD: src/sys/kern/kern_exec.c,v 1.107.2.15 2002/07/30 15:40:46 nectar Exp $ |
| 27 | * $DragonFly: src/sys/kern/kern_exec.c,v 1.29 2004/11/12 00:09:23 dillon Exp $ |
| 28 | */ |
| 29 | |
| 30 | #include <sys/param.h> |
| 31 | #include <sys/systm.h> |
| 32 | #include <sys/sysproto.h> |
| 33 | #include <sys/kernel.h> |
| 34 | #include <sys/mount.h> |
| 35 | #include <sys/filedesc.h> |
| 36 | #include <sys/fcntl.h> |
| 37 | #include <sys/acct.h> |
| 38 | #include <sys/exec.h> |
| 39 | #include <sys/imgact.h> |
| 40 | #include <sys/imgact_elf.h> |
| 41 | #include <sys/kern_syscall.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <sys/malloc.h> |
| 44 | #include <sys/proc.h> |
| 45 | #include <sys/signalvar.h> |
| 46 | #include <sys/pioctl.h> |
| 47 | #include <sys/nlookup.h> |
| 48 | #include <sys/sfbuf.h> |
| 49 | #include <sys/sysent.h> |
| 50 | #include <sys/shm.h> |
| 51 | #include <sys/sysctl.h> |
| 52 | #include <sys/vnode.h> |
| 53 | #include <sys/vmmeter.h> |
| 54 | #include <sys/aio.h> |
| 55 | |
| 56 | #include <vm/vm.h> |
| 57 | #include <vm/vm_param.h> |
| 58 | #include <sys/lock.h> |
| 59 | #include <vm/pmap.h> |
| 60 | #include <vm/vm_page.h> |
| 61 | #include <vm/vm_map.h> |
| 62 | #include <vm/vm_kern.h> |
| 63 | #include <vm/vm_extern.h> |
| 64 | #include <vm/vm_object.h> |
| 65 | #include <vm/vm_pager.h> |
| 66 | |
| 67 | #include <sys/user.h> |
| 68 | #include <machine/reg.h> |
| 69 | |
| 70 | #include <sys/thread2.h> |
| 71 | |
| 72 | MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); |
| 73 | |
| 74 | static register_t *exec_copyout_strings (struct image_params *); |
| 75 | |
| 76 | /* XXX This should be vm_size_t. */ |
| 77 | static u_long ps_strings = PS_STRINGS; |
| 78 | SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); |
| 79 | |
| 80 | /* XXX This should be vm_size_t. */ |
| 81 | static u_long usrstack = USRSTACK; |
| 82 | SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); |
| 83 | |
| 84 | u_long ps_arg_cache_limit = PAGE_SIZE / 16; |
| 85 | SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, |
| 86 | &ps_arg_cache_limit, 0, ""); |
| 87 | |
| 88 | int ps_argsopen = 1; |
| 89 | SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); |
| 90 | |
| 91 | void print_execve_args(struct image_args *args); |
| 92 | int debug_execve_args = 0; |
| 93 | SYSCTL_INT(_kern, OID_AUTO, debug_execve_args, CTLFLAG_RW, &debug_execve_args, |
| 94 | 0, ""); |
| 95 | |
| 96 | void |
| 97 | print_execve_args(struct image_args *args) |
| 98 | { |
| 99 | char *cp; |
| 100 | int ndx; |
| 101 | |
| 102 | cp = args->begin_argv; |
| 103 | for (ndx = 0; ndx < args->argc; ndx++) { |
| 104 | printf("\targv[%d]: %s\n", ndx, cp); |
| 105 | while (*cp++ != '\0'); |
| 106 | } |
| 107 | for (ndx = 0; ndx < args->envc; ndx++) { |
| 108 | printf("\tenvv[%d]: %s\n", ndx, cp); |
| 109 | while (*cp++ != '\0'); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Each of the items is a pointer to a `const struct execsw', hence the |
| 115 | * double pointer here. |
| 116 | */ |
| 117 | static const struct execsw **execsw; |
| 118 | |
| 119 | int |
| 120 | kern_execve(struct nlookupdata *nd, struct image_args *args) |
| 121 | { |
| 122 | struct thread *td = curthread; |
| 123 | struct proc *p = td->td_proc; |
| 124 | register_t *stack_base; |
| 125 | int error, len, i; |
| 126 | struct image_params image_params, *imgp; |
| 127 | struct vattr attr; |
| 128 | int (*img_first) (struct image_params *); |
| 129 | |
| 130 | if (debug_execve_args) { |
| 131 | printf("%s()\n", __func__); |
| 132 | print_execve_args(args); |
| 133 | } |
| 134 | |
| 135 | KKASSERT(p); |
| 136 | imgp = &image_params; |
| 137 | |
| 138 | /* |
| 139 | * Lock the process and set the P_INEXEC flag to indicate that |
| 140 | * it should be left alone until we're done here. This is |
| 141 | * necessary to avoid race conditions - e.g. in ptrace() - |
| 142 | * that might allow a local user to illicitly obtain elevated |
| 143 | * privileges. |
| 144 | */ |
| 145 | p->p_flag |= P_INEXEC; |
| 146 | |
| 147 | /* |
| 148 | * Initialize part of the common data |
| 149 | */ |
| 150 | imgp->proc = p; |
| 151 | imgp->args = args; |
| 152 | imgp->attr = &attr; |
| 153 | imgp->entry_addr = 0; |
| 154 | imgp->resident = 0; |
| 155 | imgp->vmspace_destroyed = 0; |
| 156 | imgp->interpreted = 0; |
| 157 | imgp->interpreter_name[0] = 0; |
| 158 | imgp->auxargs = NULL; |
| 159 | imgp->vp = NULL; |
| 160 | imgp->firstpage = NULL; |
| 161 | imgp->ps_strings = 0; |
| 162 | imgp->image_header = NULL; |
| 163 | |
| 164 | interpret: |
| 165 | |
| 166 | /* |
| 167 | * Translate the file name to a vnode. Unlock the cache entry to |
| 168 | * improve parallelism for programs exec'd in parallel. |
| 169 | */ |
| 170 | if ((error = nlookup(nd)) != 0) |
| 171 | goto exec_fail; |
| 172 | error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp); |
| 173 | KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); |
| 174 | nd->nl_flags &= ~NLC_NCPISLOCKED; |
| 175 | cache_unlock(nd->nl_ncp); |
| 176 | if (error) |
| 177 | goto exec_fail; |
| 178 | |
| 179 | /* |
| 180 | * Check file permissions (also 'opens' file) |
| 181 | */ |
| 182 | error = exec_check_permissions(imgp); |
| 183 | if (error) { |
| 184 | VOP_UNLOCK(imgp->vp, 0, td); |
| 185 | goto exec_fail_dealloc; |
| 186 | } |
| 187 | |
| 188 | error = exec_map_first_page(imgp); |
| 189 | VOP_UNLOCK(imgp->vp, 0, td); |
| 190 | if (error) |
| 191 | goto exec_fail_dealloc; |
| 192 | |
| 193 | if (debug_execve_args && imgp->interpreted) { |
| 194 | printf(" target is interpreted -- recursive pass\n"); |
| 195 | printf(" interpreter: %s\n", imgp->interpreter_name); |
| 196 | print_execve_args(args); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * If the current process has a special image activator it |
| 201 | * wants to try first, call it. For example, emulating shell |
| 202 | * scripts differently. |
| 203 | */ |
| 204 | error = -1; |
| 205 | if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) |
| 206 | error = img_first(imgp); |
| 207 | |
| 208 | /* |
| 209 | * If the vnode has a registered vmspace, exec the vmspace |
| 210 | */ |
| 211 | if (error == -1 && imgp->vp->v_resident) { |
| 212 | error = exec_resident_imgact(imgp); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Loop through the list of image activators, calling each one. |
| 217 | * An activator returns -1 if there is no match, 0 on success, |
| 218 | * and an error otherwise. |
| 219 | */ |
| 220 | for (i = 0; error == -1 && execsw[i]; ++i) { |
| 221 | if (execsw[i]->ex_imgact == NULL || |
| 222 | execsw[i]->ex_imgact == img_first) { |
| 223 | continue; |
| 224 | } |
| 225 | error = (*execsw[i]->ex_imgact)(imgp); |
| 226 | } |
| 227 | |
| 228 | if (error) { |
| 229 | if (error == -1) |
| 230 | error = ENOEXEC; |
| 231 | goto exec_fail_dealloc; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Special interpreter operation, cleanup and loop up to try to |
| 236 | * activate the interpreter. |
| 237 | */ |
| 238 | if (imgp->interpreted) { |
| 239 | exec_unmap_first_page(imgp); |
| 240 | nlookup_done(nd); |
| 241 | vrele(imgp->vp); |
| 242 | imgp->vp = NULL; |
| 243 | error = nlookup_init(nd, imgp->interpreter_name, UIO_SYSSPACE, |
| 244 | NLC_FOLLOW); |
| 245 | if (error) |
| 246 | goto exec_fail; |
| 247 | goto interpret; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Copy out strings (args and env) and initialize stack base |
| 252 | */ |
| 253 | stack_base = exec_copyout_strings(imgp); |
| 254 | p->p_vmspace->vm_minsaddr = (char *)stack_base; |
| 255 | |
| 256 | /* |
| 257 | * If custom stack fixup routine present for this process |
| 258 | * let it do the stack setup. If we are running a resident |
| 259 | * image there is no auxinfo or other image activator context |
| 260 | * so don't try to add fixups to the stack. |
| 261 | * |
| 262 | * Else stuff argument count as first item on stack |
| 263 | */ |
| 264 | if (p->p_sysent->sv_fixup && imgp->resident == 0) |
| 265 | (*p->p_sysent->sv_fixup)(&stack_base, imgp); |
| 266 | else |
| 267 | suword(--stack_base, imgp->args->argc); |
| 268 | |
| 269 | /* |
| 270 | * For security and other reasons, the file descriptor table cannot |
| 271 | * be shared after an exec. |
| 272 | */ |
| 273 | if (p->p_fd->fd_refcnt > 1) { |
| 274 | struct filedesc *tmp; |
| 275 | |
| 276 | tmp = fdcopy(p); |
| 277 | fdfree(p); |
| 278 | p->p_fd = tmp; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * For security and other reasons, signal handlers cannot |
| 283 | * be shared after an exec. The new proces gets a copy of the old |
| 284 | * handlers. In execsigs(), the new process will have its signals |
| 285 | * reset. |
| 286 | */ |
| 287 | if (p->p_procsig->ps_refcnt > 1) { |
| 288 | struct procsig *newprocsig; |
| 289 | |
| 290 | MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), |
| 291 | M_SUBPROC, M_WAITOK); |
| 292 | bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig)); |
| 293 | p->p_procsig->ps_refcnt--; |
| 294 | p->p_procsig = newprocsig; |
| 295 | p->p_procsig->ps_refcnt = 1; |
| 296 | if (p->p_sigacts == &p->p_addr->u_sigacts) |
| 297 | panic("shared procsig but private sigacts?"); |
| 298 | |
| 299 | p->p_addr->u_sigacts = *p->p_sigacts; |
| 300 | p->p_sigacts = &p->p_addr->u_sigacts; |
| 301 | } |
| 302 | |
| 303 | /* Stop profiling */ |
| 304 | stopprofclock(p); |
| 305 | |
| 306 | /* close files on exec */ |
| 307 | fdcloseexec(p); |
| 308 | |
| 309 | /* reset caught signals */ |
| 310 | execsigs(p); |
| 311 | |
| 312 | /* name this process - nameiexec(p, ndp) */ |
| 313 | len = min(nd->nl_ncp->nc_nlen, MAXCOMLEN); |
| 314 | bcopy(nd->nl_ncp->nc_name, p->p_comm, len); |
| 315 | p->p_comm[len] = 0; |
| 316 | |
| 317 | /* |
| 318 | * mark as execed, wakeup the process that vforked (if any) and tell |
| 319 | * it that it now has its own resources back |
| 320 | */ |
| 321 | p->p_flag |= P_EXEC; |
| 322 | if (p->p_pptr && (p->p_flag & P_PPWAIT)) { |
| 323 | p->p_flag &= ~P_PPWAIT; |
| 324 | wakeup((caddr_t)p->p_pptr); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Implement image setuid/setgid. |
| 329 | * |
| 330 | * Don't honor setuid/setgid if the filesystem prohibits it or if |
| 331 | * the process is being traced. |
| 332 | */ |
| 333 | if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) || |
| 334 | ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) && |
| 335 | (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && |
| 336 | (p->p_flag & P_TRACED) == 0) { |
| 337 | /* |
| 338 | * Turn off syscall tracing for set-id programs, except for |
| 339 | * root. Record any set-id flags first to make sure that |
| 340 | * we do not regain any tracing during a possible block. |
| 341 | */ |
| 342 | setsugid(); |
| 343 | if (p->p_tracep && suser(td)) { |
| 344 | struct vnode *vtmp; |
| 345 | |
| 346 | if ((vtmp = p->p_tracep) != NULL) { |
| 347 | p->p_tracep = NULL; |
| 348 | p->p_traceflag = 0; |
| 349 | vrele(vtmp); |
| 350 | } |
| 351 | } |
| 352 | /* Close any file descriptors 0..2 that reference procfs */ |
| 353 | setugidsafety(p); |
| 354 | /* Make sure file descriptors 0..2 are in use. */ |
| 355 | error = fdcheckstd(p); |
| 356 | if (error != 0) |
| 357 | goto exec_fail_dealloc; |
| 358 | /* |
| 359 | * Set the new credentials. |
| 360 | */ |
| 361 | cratom(&p->p_ucred); |
| 362 | if (attr.va_mode & VSUID) |
| 363 | change_euid(attr.va_uid); |
| 364 | if (attr.va_mode & VSGID) |
| 365 | p->p_ucred->cr_gid = attr.va_gid; |
| 366 | |
| 367 | /* |
| 368 | * Clear local varsym variables |
| 369 | */ |
| 370 | varsymset_clean(&p->p_varsymset); |
| 371 | } else { |
| 372 | if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid && |
| 373 | p->p_ucred->cr_gid == p->p_ucred->cr_rgid) |
| 374 | p->p_flag &= ~P_SUGID; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Implement correct POSIX saved-id behavior. |
| 379 | */ |
| 380 | if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid || |
| 381 | p->p_ucred->cr_svgid != p->p_ucred->cr_gid) { |
| 382 | cratom(&p->p_ucred); |
| 383 | p->p_ucred->cr_svuid = p->p_ucred->cr_uid; |
| 384 | p->p_ucred->cr_svgid = p->p_ucred->cr_gid; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Store the vp for use in procfs |
| 389 | */ |
| 390 | if (p->p_textvp) /* release old reference */ |
| 391 | vrele(p->p_textvp); |
| 392 | p->p_textvp = imgp->vp; |
| 393 | vref(p->p_textvp); |
| 394 | |
| 395 | /* |
| 396 | * Notify others that we exec'd, and clear the P_INEXEC flag |
| 397 | * as we're now a bona fide freshly-execed process. |
| 398 | */ |
| 399 | KNOTE(&p->p_klist, NOTE_EXEC); |
| 400 | p->p_flag &= ~P_INEXEC; |
| 401 | |
| 402 | /* |
| 403 | * If tracing the process, trap to debugger so breakpoints |
| 404 | * can be set before the program executes. |
| 405 | */ |
| 406 | STOPEVENT(p, S_EXEC, 0); |
| 407 | |
| 408 | if (p->p_flag & P_TRACED) |
| 409 | psignal(p, SIGTRAP); |
| 410 | |
| 411 | /* clear "fork but no exec" flag, as we _are_ execing */ |
| 412 | p->p_acflag &= ~AFORK; |
| 413 | |
| 414 | /* Set values passed into the program in registers. */ |
| 415 | setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, |
| 416 | imgp->ps_strings); |
| 417 | |
| 418 | /* Free any previous argument cache */ |
| 419 | if (p->p_args && --p->p_args->ar_ref == 0) |
| 420 | FREE(p->p_args, M_PARGS); |
| 421 | p->p_args = NULL; |
| 422 | |
| 423 | /* Cache arguments if they fit inside our allowance */ |
| 424 | i = imgp->args->begin_envv - imgp->args->begin_argv; |
| 425 | if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { |
| 426 | MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, |
| 427 | M_PARGS, M_WAITOK); |
| 428 | p->p_args->ar_ref = 1; |
| 429 | p->p_args->ar_length = i; |
| 430 | bcopy(imgp->args->begin_argv, p->p_args->ar_args, i); |
| 431 | } |
| 432 | |
| 433 | exec_fail_dealloc: |
| 434 | |
| 435 | /* |
| 436 | * free various allocated resources |
| 437 | */ |
| 438 | if (imgp->firstpage) |
| 439 | exec_unmap_first_page(imgp); |
| 440 | |
| 441 | if (imgp->vp) { |
| 442 | vrele(imgp->vp); |
| 443 | imgp->vp = NULL; |
| 444 | } |
| 445 | |
| 446 | if (error == 0) { |
| 447 | ++mycpu->gd_cnt.v_exec; |
| 448 | return (0); |
| 449 | } |
| 450 | |
| 451 | exec_fail: |
| 452 | /* we're done here, clear P_INEXEC */ |
| 453 | p->p_flag &= ~P_INEXEC; |
| 454 | if (imgp->vmspace_destroyed) { |
| 455 | /* sorry, no more process anymore. exit gracefully */ |
| 456 | exit1(W_EXITCODE(0, SIGABRT)); |
| 457 | /* NOT REACHED */ |
| 458 | return(0); |
| 459 | } else { |
| 460 | return(error); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * execve() system call. |
| 466 | */ |
| 467 | int |
| 468 | execve(struct execve_args *uap) |
| 469 | { |
| 470 | struct nlookupdata nd; |
| 471 | struct image_args args; |
| 472 | int error; |
| 473 | |
| 474 | error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW); |
| 475 | if (error == 0) { |
| 476 | error = exec_copyin_args(&args, uap->fname, PATH_USERSPACE, |
| 477 | uap->argv, uap->envv); |
| 478 | } |
| 479 | if (error == 0) |
| 480 | error = kern_execve(&nd, &args); |
| 481 | nlookup_done(&nd); |
| 482 | exec_free_args(&args); |
| 483 | |
| 484 | /* |
| 485 | * The syscall result is returned in registers to the new program. |
| 486 | * Linux will register %edx as an atexit function and we must be |
| 487 | * sure to set it to 0. XXX |
| 488 | */ |
| 489 | if (error == 0) |
| 490 | uap->sysmsg_result64 = 0; |
| 491 | |
| 492 | return (error); |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | exec_map_first_page(struct image_params *imgp) |
| 497 | { |
| 498 | int rv, i; |
| 499 | int initial_pagein; |
| 500 | vm_page_t ma[VM_INITIAL_PAGEIN]; |
| 501 | vm_page_t m; |
| 502 | vm_object_t object; |
| 503 | int error; |
| 504 | |
| 505 | if (imgp->firstpage) |
| 506 | exec_unmap_first_page(imgp); |
| 507 | |
| 508 | /* |
| 509 | * XXX the callers should really use vn_open so we don't have to |
| 510 | * do this junk. |
| 511 | */ |
| 512 | if ((error = VOP_GETVOBJECT(imgp->vp, &object)) != 0) { |
| 513 | if (vn_canvmio(imgp->vp) == TRUE) { |
| 514 | error = vfs_object_create(imgp->vp, curthread); |
| 515 | if (error == 0) |
| 516 | error = VOP_GETVOBJECT(imgp->vp, &object); |
| 517 | } |
| 518 | } |
| 519 | if (error) |
| 520 | return (EIO); |
| 521 | |
| 522 | /* |
| 523 | * We shouldn't need protection for vm_page_grab() but we certainly |
| 524 | * need it for the lookup loop below (lookup/busy race), since |
| 525 | * an interrupt can unbusy and free the page before our busy check. |
| 526 | */ |
| 527 | crit_enter(); |
| 528 | m = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); |
| 529 | |
| 530 | if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { |
| 531 | ma[0] = m; |
| 532 | initial_pagein = VM_INITIAL_PAGEIN; |
| 533 | if (initial_pagein > object->size) |
| 534 | initial_pagein = object->size; |
| 535 | for (i = 1; i < initial_pagein; i++) { |
| 536 | if ((m = vm_page_lookup(object, i)) != NULL) { |
| 537 | if ((m->flags & PG_BUSY) || m->busy) |
| 538 | break; |
| 539 | if (m->valid) |
| 540 | break; |
| 541 | vm_page_busy(m); |
| 542 | } else { |
| 543 | m = vm_page_alloc(object, i, VM_ALLOC_NORMAL); |
| 544 | if (m == NULL) |
| 545 | break; |
| 546 | } |
| 547 | ma[i] = m; |
| 548 | } |
| 549 | initial_pagein = i; |
| 550 | |
| 551 | /* |
| 552 | * get_pages unbusies all the requested pages except the |
| 553 | * primary page (at index 0 in this case). |
| 554 | */ |
| 555 | rv = vm_pager_get_pages(object, ma, initial_pagein, 0); |
| 556 | m = vm_page_lookup(object, 0); |
| 557 | |
| 558 | if (rv != VM_PAGER_OK || m == NULL || m->valid == 0) { |
| 559 | if (m) { |
| 560 | vm_page_protect(m, VM_PROT_NONE); |
| 561 | vm_page_free(m); |
| 562 | } |
| 563 | crit_exit(); |
| 564 | return EIO; |
| 565 | } |
| 566 | } |
| 567 | vm_page_hold(m); |
| 568 | vm_page_wakeup(m); /* unbusy the page */ |
| 569 | crit_exit(); |
| 570 | |
| 571 | imgp->firstpage = sf_buf_alloc(m, SFBA_QUICK); |
| 572 | imgp->image_header = (void *)sf_buf_kva(imgp->firstpage); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | void |
| 578 | exec_unmap_first_page(imgp) |
| 579 | struct image_params *imgp; |
| 580 | { |
| 581 | vm_page_t m; |
| 582 | |
| 583 | crit_enter(); |
| 584 | if (imgp->firstpage != NULL) { |
| 585 | m = sf_buf_page(imgp->firstpage); |
| 586 | sf_buf_free(imgp->firstpage); |
| 587 | imgp->firstpage = NULL; |
| 588 | imgp->image_header = NULL; |
| 589 | vm_page_unhold(m); |
| 590 | } |
| 591 | crit_exit(); |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Destroy old address space, and allocate a new stack |
| 596 | * The new stack is only SGROWSIZ large because it is grown |
| 597 | * automatically in trap.c. |
| 598 | */ |
| 599 | int |
| 600 | exec_new_vmspace(struct image_params *imgp, struct vmspace *vmcopy) |
| 601 | { |
| 602 | int error; |
| 603 | struct vmspace *vmspace = imgp->proc->p_vmspace; |
| 604 | vm_offset_t stack_addr = USRSTACK - maxssiz; |
| 605 | vm_map_t map; |
| 606 | |
| 607 | imgp->vmspace_destroyed = 1; |
| 608 | |
| 609 | /* |
| 610 | * Prevent a pending AIO from modifying the new address space. |
| 611 | */ |
| 612 | aio_proc_rundown(imgp->proc); |
| 613 | |
| 614 | /* |
| 615 | * Blow away entire process VM, if address space not shared, |
| 616 | * otherwise, create a new VM space so that other threads are |
| 617 | * not disrupted. If we are execing a resident vmspace we |
| 618 | * create a duplicate of it and remap the stack. |
| 619 | * |
| 620 | * The exitingcnt test is not strictly necessary but has been |
| 621 | * included for code sanity (to make the code more deterministic). |
| 622 | */ |
| 623 | map = &vmspace->vm_map; |
| 624 | if (vmcopy) { |
| 625 | vmspace_exec(imgp->proc, vmcopy); |
| 626 | vmspace = imgp->proc->p_vmspace; |
| 627 | pmap_remove_pages(vmspace_pmap(vmspace), stack_addr, USRSTACK); |
| 628 | map = &vmspace->vm_map; |
| 629 | } else if (vmspace->vm_refcnt == 1 && vmspace->vm_exitingcnt == 0) { |
| 630 | shmexit(vmspace); |
| 631 | if (vmspace->vm_upcalls) |
| 632 | upc_release(vmspace, imgp->proc); |
| 633 | pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); |
| 634 | vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); |
| 635 | } else { |
| 636 | vmspace_exec(imgp->proc, NULL); |
| 637 | vmspace = imgp->proc->p_vmspace; |
| 638 | map = &vmspace->vm_map; |
| 639 | } |
| 640 | |
| 641 | /* Allocate a new stack */ |
| 642 | error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz, |
| 643 | VM_PROT_ALL, VM_PROT_ALL, 0); |
| 644 | if (error) |
| 645 | return (error); |
| 646 | |
| 647 | /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the |
| 648 | * VM_STACK case, but they are still used to monitor the size of the |
| 649 | * process stack so we can check the stack rlimit. |
| 650 | */ |
| 651 | vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; |
| 652 | vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz; |
| 653 | |
| 654 | return(0); |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Copy out argument and environment strings from the old process |
| 659 | * address space into the temporary string buffer. |
| 660 | */ |
| 661 | int |
| 662 | exec_copyin_args(struct image_args *args, char *fname, |
| 663 | enum exec_path_segflg segflg, char **argv, char **envv) |
| 664 | { |
| 665 | char *argp, *envp; |
| 666 | int error = 0; |
| 667 | size_t length; |
| 668 | |
| 669 | bzero(args, sizeof(*args)); |
| 670 | args->buf = (char *) kmem_alloc_wait(exec_map, PATH_MAX + ARG_MAX); |
| 671 | if (args->buf == NULL) |
| 672 | return (ENOMEM); |
| 673 | args->begin_argv = args->buf; |
| 674 | args->endp = args->begin_argv; |
| 675 | args->space = ARG_MAX; |
| 676 | |
| 677 | args->fname = args->buf + ARG_MAX; |
| 678 | |
| 679 | /* |
| 680 | * Copy the file name. |
| 681 | */ |
| 682 | if (segflg == PATH_SYSSPACE) { |
| 683 | error = copystr(fname, args->fname, PATH_MAX, &length); |
| 684 | } else if (segflg == PATH_USERSPACE) { |
| 685 | error = copyinstr(fname, args->fname, PATH_MAX, &length); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * extract argument strings |
| 690 | */ |
| 691 | |
| 692 | if (argv && error == 0) { |
| 693 | /* |
| 694 | * The argv0 argument for execv() is allowed to be NULL, |
| 695 | * in which case we use our filename as argv[0]. |
| 696 | * This guarantees that |
| 697 | * the interpreter knows what file to open in the case |
| 698 | * that we exec an interpreted file. |
| 699 | */ |
| 700 | argp = (caddr_t) (intptr_t) fuword(argv); |
| 701 | if (argp == NULL) { |
| 702 | length = strlen(args->fname) + 1; |
| 703 | KKASSERT(length <= args->space); |
| 704 | bcopy(args->fname, args->endp, length); |
| 705 | args->space -= length; |
| 706 | args->endp += length; |
| 707 | args->argc++; |
| 708 | argv++; |
| 709 | } |
| 710 | while ((argp = (caddr_t) (intptr_t) fuword(argv++))) { |
| 711 | if (argp == (caddr_t) -1) { |
| 712 | error = EFAULT; |
| 713 | goto cleanup; |
| 714 | } |
| 715 | error = copyinstr(argp, args->endp, |
| 716 | args->space, &length); |
| 717 | if (error == ENAMETOOLONG) |
| 718 | error = E2BIG; |
| 719 | if (error) |
| 720 | goto cleanup; |
| 721 | args->space -= length; |
| 722 | args->endp += length; |
| 723 | args->argc++; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | args->begin_envv = args->endp; |
| 728 | |
| 729 | /* |
| 730 | * extract environment strings |
| 731 | */ |
| 732 | if (envv && error == 0) { |
| 733 | while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { |
| 734 | if (envp == (caddr_t) -1) { |
| 735 | error = EFAULT; |
| 736 | goto cleanup; |
| 737 | } |
| 738 | error = copyinstr(envp, args->endp, args->space, |
| 739 | &length); |
| 740 | if (error == ENAMETOOLONG) |
| 741 | error = E2BIG; |
| 742 | if (error) |
| 743 | goto cleanup; |
| 744 | args->space -= length; |
| 745 | args->endp += length; |
| 746 | args->envc++; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | cleanup: |
| 751 | return (error); |
| 752 | } |
| 753 | |
| 754 | void |
| 755 | exec_free_args(struct image_args *args) |
| 756 | { |
| 757 | if (args->buf) { |
| 758 | kmem_free_wakeup(exec_map, |
| 759 | (vm_offset_t)args->buf, PATH_MAX + ARG_MAX); |
| 760 | args->buf = NULL; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Copy strings out to the new process address space, constructing |
| 766 | * new arg and env vector tables. Return a pointer to the base |
| 767 | * so that it can be used as the initial stack pointer. |
| 768 | */ |
| 769 | register_t * |
| 770 | exec_copyout_strings(struct image_params *imgp) |
| 771 | { |
| 772 | int argc, envc; |
| 773 | char **vectp; |
| 774 | char *stringp, *destp; |
| 775 | register_t *stack_base; |
| 776 | struct ps_strings *arginfo; |
| 777 | int szsigcode; |
| 778 | |
| 779 | /* |
| 780 | * Calculate string base and vector table pointers. |
| 781 | * Also deal with signal trampoline code for this exec type. |
| 782 | */ |
| 783 | arginfo = (struct ps_strings *)PS_STRINGS; |
| 784 | szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); |
| 785 | destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - |
| 786 | roundup((ARG_MAX - imgp->args->space), sizeof(char *)); |
| 787 | |
| 788 | /* |
| 789 | * install sigcode |
| 790 | */ |
| 791 | if (szsigcode) |
| 792 | copyout(imgp->proc->p_sysent->sv_sigcode, |
| 793 | ((caddr_t)arginfo - szsigcode), szsigcode); |
| 794 | |
| 795 | /* |
| 796 | * If we have a valid auxargs ptr, prepare some room |
| 797 | * on the stack. |
| 798 | * |
| 799 | * The '+ 2' is for the null pointers at the end of each of the |
| 800 | * arg and env vector sets, and 'AT_COUNT*2' is room for the |
| 801 | * ELF Auxargs data. |
| 802 | */ |
| 803 | if (imgp->auxargs) { |
| 804 | vectp = (char **)(destp - (imgp->args->argc + |
| 805 | imgp->args->envc + 2 + AT_COUNT * 2) * sizeof(char*)); |
| 806 | } else { |
| 807 | vectp = (char **)(destp - (imgp->args->argc + |
| 808 | imgp->args->envc + 2) * sizeof(char*)); |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * NOTE: don't bother aligning the stack here for GCC 2.x, it will |
| 813 | * be done in crt1.o. Note that GCC 3.x aligns the stack in main. |
| 814 | */ |
| 815 | |
| 816 | /* |
| 817 | * vectp also becomes our initial stack base |
| 818 | */ |
| 819 | stack_base = (register_t *)vectp; |
| 820 | |
| 821 | stringp = imgp->args->begin_argv; |
| 822 | argc = imgp->args->argc; |
| 823 | envc = imgp->args->envc; |
| 824 | |
| 825 | /* |
| 826 | * Copy out strings - arguments and environment. |
| 827 | */ |
| 828 | copyout(stringp, destp, ARG_MAX - imgp->args->space); |
| 829 | |
| 830 | /* |
| 831 | * Fill in "ps_strings" struct for ps, w, etc. |
| 832 | */ |
| 833 | suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); |
| 834 | suword(&arginfo->ps_nargvstr, argc); |
| 835 | |
| 836 | /* |
| 837 | * Fill in argument portion of vector table. |
| 838 | */ |
| 839 | for (; argc > 0; --argc) { |
| 840 | suword(vectp++, (long)(intptr_t)destp); |
| 841 | while (*stringp++ != 0) |
| 842 | destp++; |
| 843 | destp++; |
| 844 | } |
| 845 | |
| 846 | /* a null vector table pointer separates the argp's from the envp's */ |
| 847 | suword(vectp++, 0); |
| 848 | |
| 849 | suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); |
| 850 | suword(&arginfo->ps_nenvstr, envc); |
| 851 | |
| 852 | /* |
| 853 | * Fill in environment portion of vector table. |
| 854 | */ |
| 855 | for (; envc > 0; --envc) { |
| 856 | suword(vectp++, (long)(intptr_t)destp); |
| 857 | while (*stringp++ != 0) |
| 858 | destp++; |
| 859 | destp++; |
| 860 | } |
| 861 | |
| 862 | /* end of vector table is a null pointer */ |
| 863 | suword(vectp, 0); |
| 864 | |
| 865 | return (stack_base); |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * Check permissions of file to execute. |
| 870 | * Return 0 for success or error code on failure. |
| 871 | */ |
| 872 | int |
| 873 | exec_check_permissions(imgp) |
| 874 | struct image_params *imgp; |
| 875 | { |
| 876 | struct proc *p = imgp->proc; |
| 877 | struct vnode *vp = imgp->vp; |
| 878 | struct vattr *attr = imgp->attr; |
| 879 | struct thread *td = p->p_thread; |
| 880 | int error; |
| 881 | |
| 882 | /* Get file attributes */ |
| 883 | error = VOP_GETATTR(vp, attr, td); |
| 884 | if (error) |
| 885 | return (error); |
| 886 | |
| 887 | /* |
| 888 | * 1) Check if file execution is disabled for the filesystem that this |
| 889 | * file resides on. |
| 890 | * 2) Insure that at least one execute bit is on - otherwise root |
| 891 | * will always succeed, and we don't want to happen unless the |
| 892 | * file really is executable. |
| 893 | * 3) Insure that the file is a regular file. |
| 894 | */ |
| 895 | if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || |
| 896 | ((attr->va_mode & 0111) == 0) || |
| 897 | (attr->va_type != VREG)) { |
| 898 | return (EACCES); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Zero length files can't be exec'd |
| 903 | */ |
| 904 | if (attr->va_size == 0) |
| 905 | return (ENOEXEC); |
| 906 | |
| 907 | /* |
| 908 | * Check for execute permission to file based on current credentials. |
| 909 | */ |
| 910 | error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td); |
| 911 | if (error) |
| 912 | return (error); |
| 913 | |
| 914 | /* |
| 915 | * Check number of open-for-writes on the file and deny execution |
| 916 | * if there are any. |
| 917 | */ |
| 918 | if (vp->v_writecount) |
| 919 | return (ETXTBSY); |
| 920 | |
| 921 | /* |
| 922 | * Call filesystem specific open routine (which does nothing in the |
| 923 | * general case). |
| 924 | */ |
| 925 | error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL, td); |
| 926 | if (error) |
| 927 | return (error); |
| 928 | |
| 929 | return (0); |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Exec handler registration |
| 934 | */ |
| 935 | int |
| 936 | exec_register(execsw_arg) |
| 937 | const struct execsw *execsw_arg; |
| 938 | { |
| 939 | const struct execsw **es, **xs, **newexecsw; |
| 940 | int count = 2; /* New slot and trailing NULL */ |
| 941 | |
| 942 | if (execsw) |
| 943 | for (es = execsw; *es; es++) |
| 944 | count++; |
| 945 | newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); |
| 946 | if (newexecsw == NULL) |
| 947 | return ENOMEM; |
| 948 | xs = newexecsw; |
| 949 | if (execsw) |
| 950 | for (es = execsw; *es; es++) |
| 951 | *xs++ = *es; |
| 952 | *xs++ = execsw_arg; |
| 953 | *xs = NULL; |
| 954 | if (execsw) |
| 955 | free(execsw, M_TEMP); |
| 956 | execsw = newexecsw; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | int |
| 961 | exec_unregister(execsw_arg) |
| 962 | const struct execsw *execsw_arg; |
| 963 | { |
| 964 | const struct execsw **es, **xs, **newexecsw; |
| 965 | int count = 1; |
| 966 | |
| 967 | if (execsw == NULL) |
| 968 | panic("unregister with no handlers left?"); |
| 969 | |
| 970 | for (es = execsw; *es; es++) { |
| 971 | if (*es == execsw_arg) |
| 972 | break; |
| 973 | } |
| 974 | if (*es == NULL) |
| 975 | return ENOENT; |
| 976 | for (es = execsw; *es; es++) |
| 977 | if (*es != execsw_arg) |
| 978 | count++; |
| 979 | newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); |
| 980 | if (newexecsw == NULL) |
| 981 | return ENOMEM; |
| 982 | xs = newexecsw; |
| 983 | for (es = execsw; *es; es++) |
| 984 | if (*es != execsw_arg) |
| 985 | *xs++ = *es; |
| 986 | *xs = NULL; |
| 987 | if (execsw) |
| 988 | free(execsw, M_TEMP); |
| 989 | execsw = newexecsw; |
| 990 | return 0; |
| 991 | } |