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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by David Greenman 16 * 4. The name of the developer may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $Id: kern_exec.c,v 1.23 1995/10/08 00:06:01 swallace Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/signalvar.h> 37 #include <sys/kernel.h> 38 #include <sys/mount.h> 39 #include <sys/filedesc.h> 40 #include <sys/fcntl.h> 41 #include <sys/acct.h> 42 #include <sys/exec.h> 43 #include <sys/imgact.h> 44 #include <sys/wait.h> 45 #include <sys/malloc.h> 46 #include <sys/sysent.h> 47 #include <sys/syslog.h> 48 #include <sys/shm.h> 49 50 #include <vm/vm.h> 51 #include <vm/vm_kern.h> 52 53 #include <machine/reg.h> 54 55 int *exec_copyout_strings __P((struct image_params *)); 56 57 static int exec_check_permissions(struct image_params *); 58 59 /* 60 * execsw_set is constructed for us by the linker. Each of the items 61 * is a pointer to a `const struct execsw', hence the double pointer here. 62 */ 63 const struct execsw **execsw = (const struct execsw **)&execsw_set.ls_items[0]; 64 65 struct execve_args { 66 char *fname; 67 char **argv; 68 char **envv; 69 }; 70 71 /* 72 * execve() system call. 73 */ 74 int 75 execve(p, uap, retval) 76 struct proc *p; 77 register struct execve_args *uap; 78 int *retval; 79 { 80 struct nameidata nd, *ndp; 81 int *stack_base; 82 int error, len, i; 83 struct image_params image_params, *iparams; 84 struct vnode *vnodep; 85 struct vattr attr; 86 char *image_header; 87 88 iparams = &image_params; 89 bzero((caddr_t)iparams, sizeof(struct image_params)); 90 image_header = (char *)0; 91 92 /* 93 * Initialize a few constants in the common area 94 */ 95 iparams->proc = p; 96 iparams->uap = uap; 97 iparams->attr = &attr; 98 99 /* 100 * Allocate temporary demand zeroed space for argument and 101 * environment strings 102 */ 103 iparams->stringbase = (char *)vm_map_min(exec_map); 104 error = vm_map_find(exec_map, NULL, 0, (vm_offset_t *)&iparams->stringbase, 105 ARG_MAX, TRUE); 106 if (error) { 107 log(LOG_WARNING, "execve: failed to allocate string space\n"); 108 return (error); 109 } 110 111 if (!iparams->stringbase) { 112 error = ENOMEM; 113 goto exec_fail; 114 } 115 iparams->stringp = iparams->stringbase; 116 iparams->stringspace = ARG_MAX; 117 118 /* 119 * Translate the file name. namei() returns a vnode pointer 120 * in ni_vp amoung other things. 121 */ 122 ndp = &nd; 123 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 124 UIO_USERSPACE, uap->fname, p); 125 126 interpret: 127 128 error = namei(ndp); 129 if (error) { 130 vm_map_remove(exec_map, (vm_offset_t)iparams->stringbase, 131 (vm_offset_t)iparams->stringbase + ARG_MAX); 132 goto exec_fail; 133 } 134 135 iparams->vnodep = vnodep = ndp->ni_vp; 136 137 if (vnodep == NULL) { 138 error = ENOEXEC; 139 goto exec_fail_dealloc; 140 } 141 142 /* 143 * Check file permissions (also 'opens' file) 144 */ 145 error = exec_check_permissions(iparams); 146 147 /* 148 * Lose the lock on the vnode. It's no longer needed, and must not 149 * exist for the pagefault paging to work below. 150 */ 151 VOP_UNLOCK(vnodep); 152 153 if (error) 154 goto exec_fail_dealloc; 155 156 /* 157 * Map the image header (first page) of the file into 158 * kernel address space 159 */ 160 error = vm_mmap(kernel_map, /* map */ 161 (vm_offset_t *)&image_header, /* address */ 162 PAGE_SIZE, /* size */ 163 VM_PROT_READ, /* protection */ 164 VM_PROT_READ, /* max protection */ 165 0, /* flags */ 166 (caddr_t)vnodep, /* vnode */ 167 0); /* offset */ 168 if (error) { 169 uprintf("mmap failed: %d\n",error); 170 goto exec_fail_dealloc; 171 } 172 iparams->image_header = image_header; 173 174 /* 175 * Loop through list of image activators, calling each one. 176 * If there is no match, the activator returns -1. If there 177 * is a match, but there was an error during the activation, 178 * the error is returned. Otherwise 0 means success. If the 179 * image is interpreted, loop back up and try activating 180 * the interpreter. 181 */ 182 for (i = 0; execsw[i]; ++i) { 183 if (execsw[i]->ex_imgact) 184 error = (*execsw[i]->ex_imgact)(iparams); 185 else 186 continue; 187 188 if (error == -1) 189 continue; 190 if (error) 191 goto exec_fail_dealloc; 192 if (iparams->interpreted) { 193 /* free old vnode and name buffer */ 194 vrele(ndp->ni_vp); 195 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 196 if (vm_map_remove(kernel_map, (vm_offset_t)image_header, 197 (vm_offset_t)image_header + PAGE_SIZE)) 198 panic("execve: header dealloc failed (1)"); 199 200 /* set new name to that of the interpreter */ 201 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 202 UIO_SYSSPACE, iparams->interpreter_name, p); 203 goto interpret; 204 } 205 break; 206 } 207 /* If we made it through all the activators and none matched, exit. */ 208 if (error == -1) { 209 error = ENOEXEC; 210 goto exec_fail_dealloc; 211 } 212 213 /* 214 * Copy out strings (args and env) and initialize stack base 215 */ 216 stack_base = exec_copyout_strings(iparams); 217 p->p_vmspace->vm_minsaddr = (char *)stack_base; 218 219 /* 220 * If custom stack fixup routine present for this process 221 * let it do the stack setup. 222 * Else stuff argument count as first item on stack 223 */ 224 if (p->p_sysent->sv_fixup) 225 (*p->p_sysent->sv_fixup)(&stack_base, iparams); 226 else 227 suword(--stack_base, iparams->argc); 228 229 /* close files on exec */ 230 fdcloseexec(p); 231 232 /* reset caught signals */ 233 execsigs(p); 234 235 /* name this process - nameiexec(p, ndp) */ 236 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 237 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 238 p->p_comm[len] = 0; 239 240 /* 241 * mark as executable, wakeup any process that was vforked and tell 242 * it that it now has it's own resources back 243 */ 244 p->p_flag |= P_EXEC; 245 if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 246 p->p_flag &= ~P_PPWAIT; 247 wakeup((caddr_t)p->p_pptr); 248 } 249 250 /* implement set userid/groupid */ 251 p->p_flag &= ~P_SUGID; 252 253 /* 254 * Turn off kernel tracing for set-id programs, except for 255 * root. 256 */ 257 if (p->p_tracep && (attr.va_mode & (VSUID | VSGID)) && 258 suser(p->p_ucred, &p->p_acflag)) { 259 p->p_traceflag = 0; 260 vrele(p->p_tracep); 261 p->p_tracep = 0; 262 } 263 if ((attr.va_mode & VSUID) && (p->p_flag & P_TRACED) == 0) { 264 p->p_ucred = crcopy(p->p_ucred); 265 p->p_ucred->cr_uid = attr.va_uid; 266 p->p_flag |= P_SUGID; 267 } 268 if ((attr.va_mode & VSGID) && (p->p_flag & P_TRACED) == 0) { 269 p->p_ucred = crcopy(p->p_ucred); 270 p->p_ucred->cr_groups[0] = attr.va_gid; 271 p->p_flag |= P_SUGID; 272 } 273 274 /* 275 * Implement correct POSIX saved uid behavior. 276 */ 277 p->p_cred->p_svuid = p->p_ucred->cr_uid; 278 p->p_cred->p_svgid = p->p_ucred->cr_gid; 279 280 /* 281 * Store the vp for use in procfs 282 */ 283 if (p->p_textvp) /* release old reference */ 284 vrele(p->p_textvp); 285 VREF(ndp->ni_vp); 286 p->p_textvp = ndp->ni_vp; 287 288 /* 289 * If tracing the process, trap to debugger so breakpoints 290 * can be set before the program executes. 291 */ 292 if (p->p_flag & P_TRACED) 293 psignal(p, SIGTRAP); 294 295 /* clear "fork but no exec" flag, as we _are_ execing */ 296 p->p_acflag &= ~AFORK; 297 298 /* Set entry address */ 299 setregs(p, iparams->entry_addr, (u_long)stack_base); 300 301 /* 302 * free various allocated resources 303 */ 304 if (vm_map_remove(exec_map, (vm_offset_t)iparams->stringbase, 305 (vm_offset_t)iparams->stringbase + ARG_MAX)) 306 panic("execve: string buffer dealloc failed (1)"); 307 if (vm_map_remove(kernel_map, (vm_offset_t)image_header, 308 (vm_offset_t)image_header + PAGE_SIZE)) 309 panic("execve: header dealloc failed (2)"); 310 vrele(ndp->ni_vp); 311 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 312 313 return (0); 314 315 exec_fail_dealloc: 316 if (iparams->stringbase && iparams->stringbase != (char *)-1) 317 if (vm_map_remove(exec_map, (vm_offset_t)iparams->stringbase, 318 (vm_offset_t)iparams->stringbase + ARG_MAX)) 319 panic("execve: string buffer dealloc failed (2)"); 320 if (iparams->image_header && iparams->image_header != (char *)-1) 321 if (vm_map_remove(kernel_map, (vm_offset_t)image_header, 322 (vm_offset_t)image_header + PAGE_SIZE)) 323 panic("execve: header dealloc failed (3)"); 324 if (ndp->ni_vp) 325 vrele(ndp->ni_vp); 326 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 327 328 exec_fail: 329 if (iparams->vmspace_destroyed) { 330 /* sorry, no more process anymore. exit gracefully */ 331 exit1(p, W_EXITCODE(0, SIGABRT)); 332 /* NOT REACHED */ 333 return(0); 334 } else { 335 return(error); 336 } 337 } 338 339 /* 340 * Destroy old address space, and allocate a new stack 341 * The new stack is only SGROWSIZ large because it is grown 342 * automatically in trap.c. 343 */ 344 int 345 exec_new_vmspace(iparams) 346 struct image_params *iparams; 347 { 348 int error; 349 struct vmspace *vmspace = iparams->proc->p_vmspace; 350 caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 351 352 iparams->vmspace_destroyed = 1; 353 354 /* Blow away entire process VM */ 355 #ifdef SYSVSHM 356 if (vmspace->vm_shm) 357 shmexit(iparams->proc); 358 #endif 359 vm_map_remove(&vmspace->vm_map, 0, USRSTACK); 360 361 /* Allocate a new stack */ 362 error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr, 363 SGROWSIZ, FALSE); 364 if (error) 365 return(error); 366 367 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 368 369 /* Initialize maximum stack address */ 370 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 371 372 return(0); 373 } 374 375 /* 376 * Copy out argument and environment strings from the old process 377 * address space into the temporary string buffer. 378 */ 379 int 380 exec_extract_strings(iparams) 381 struct image_params *iparams; 382 { 383 char **argv, **envv; 384 char *argp, *envp; 385 int error, length; 386 387 /* 388 * extract arguments first 389 */ 390 391 argv = iparams->uap->argv; 392 393 if (argv) { 394 while ((argp = (caddr_t) fuword(argv++))) { 395 if (argp == (caddr_t) -1) 396 return (EFAULT); 397 if ((error = copyinstr(argp, iparams->stringp, 398 iparams->stringspace, &length))) { 399 if (error == ENAMETOOLONG) 400 return(E2BIG); 401 return (error); 402 } 403 iparams->stringspace -= length; 404 iparams->stringp += length; 405 iparams->argc++; 406 } 407 } 408 409 /* 410 * extract environment strings 411 */ 412 413 envv = iparams->uap->envv; 414 415 if (envv) { 416 while ((envp = (caddr_t) fuword(envv++))) { 417 if (envp == (caddr_t) -1) 418 return (EFAULT); 419 if ((error = copyinstr(envp, iparams->stringp, 420 iparams->stringspace, &length))) { 421 if (error == ENAMETOOLONG) 422 return(E2BIG); 423 return (error); 424 } 425 iparams->stringspace -= length; 426 iparams->stringp += length; 427 iparams->envc++; 428 } 429 } 430 431 return (0); 432 } 433 434 /* 435 * Copy strings out to the new process address space, constructing 436 * new arg and env vector tables. Return a pointer to the base 437 * so that it can be used as the initial stack pointer. 438 */ 439 int * 440 exec_copyout_strings(iparams) 441 struct image_params *iparams; 442 { 443 int argc, envc; 444 char **vectp; 445 char *stringp, *destp; 446 int *stack_base; 447 struct ps_strings *arginfo; 448 449 /* 450 * Calculate string base and vector table pointers. 451 */ 452 arginfo = PS_STRINGS; 453 destp = (caddr_t)arginfo - roundup((ARG_MAX - iparams->stringspace), sizeof(char *)); 454 /* 455 * The '+ 2' is for the null pointers at the end of each of the 456 * arg and env vector sets 457 */ 458 vectp = (char **) (destp - 459 (iparams->argc + iparams->envc + 2) * sizeof(char *)); 460 461 /* 462 * vectp also becomes our initial stack base 463 */ 464 stack_base = (int *)vectp; 465 466 stringp = iparams->stringbase; 467 argc = iparams->argc; 468 envc = iparams->envc; 469 470 /* 471 * Copy out strings - arguments and environment. 472 */ 473 copyout(stringp, destp, ARG_MAX - iparams->stringspace); 474 475 /* 476 * Fill in "ps_strings" struct for ps, w, etc. 477 */ 478 suword(&arginfo->ps_argvstr, (int)destp); 479 suword(&arginfo->ps_nargvstr, argc); 480 481 /* 482 * Fill in argument portion of vector table. 483 */ 484 for (; argc > 0; --argc) { 485 suword(vectp++, (int)destp); 486 while (*stringp++ != 0) 487 destp++; 488 destp++; 489 } 490 491 /* a null vector table pointer seperates the argp's from the envp's */ 492 suword(vectp++, NULL); 493 494 suword(&arginfo->ps_envstr, (int)destp); 495 suword(&arginfo->ps_nenvstr, envc); 496 497 /* 498 * Fill in environment portion of vector table. 499 */ 500 for (; envc > 0; --envc) { 501 suword(vectp++, (int)destp); 502 while (*stringp++ != 0) 503 destp++; 504 destp++; 505 } 506 507 /* end of vector table is a null pointer */ 508 suword(vectp, NULL); 509 510 return (stack_base); 511 } 512 513 /* 514 * Check permissions of file to execute. 515 * Return 0 for success or error code on failure. 516 */ 517 static int 518 exec_check_permissions(iparams) 519 struct image_params *iparams; 520 { 521 struct proc *p = iparams->proc; 522 struct vnode *vnodep = iparams->vnodep; 523 struct vattr *attr = iparams->attr; 524 int error; 525 526 /* 527 * Check number of open-for-writes on the file and deny execution 528 * if there are any. 529 */ 530 if (vnodep->v_writecount) { 531 return (ETXTBSY); 532 } 533 534 /* Get file attributes */ 535 error = VOP_GETATTR(vnodep, attr, p->p_ucred, p); 536 if (error) 537 return (error); 538 539 /* 540 * 1) Check if file execution is disabled for the filesystem that this 541 * file resides on. 542 * 2) Insure that at least one execute bit is on - otherwise root 543 * will always succeed, and we don't want to happen unless the 544 * file really is executable. 545 * 3) Insure that the file is a regular file. 546 */ 547 if ((vnodep->v_mount->mnt_flag & MNT_NOEXEC) || 548 ((attr->va_mode & 0111) == 0) || 549 (attr->va_type != VREG)) { 550 return (EACCES); 551 } 552 553 /* 554 * Zero length files can't be exec'd 555 */ 556 if (attr->va_size == 0) 557 return (ENOEXEC); 558 559 /* 560 * Disable setuid/setgid if the filesystem prohibits it or if 561 * the process is being traced. 562 */ 563 if ((vnodep->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 564 attr->va_mode &= ~(VSUID | VSGID); 565 566 /* 567 * Check for execute permission to file based on current credentials. 568 * Then call filesystem specific open routine (which does nothing 569 * in the general case). 570 */ 571 error = VOP_ACCESS(vnodep, VEXEC, p->p_ucred, p); 572 if (error) 573 return (error); 574 575 error = VOP_OPEN(vnodep, FREAD, p->p_ucred, p); 576 if (error) 577 return (error); 578 579 return (0); 580 } 581