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