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