1 /* $NetBSD: kern_exec.c,v 1.66 1995/04/10 18:28:09 mycroft Exp $ */ 2 3 /*- 4 * Copyright (C) 1993, 1994 Christopher G. Demetriou 5 * Copyright (C) 1992 Wolfgang Solfrank. 6 * Copyright (C) 1992 TooLs GmbH. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by TooLs GmbH. 20 * 4. The name of TooLs GmbH may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/filedesc.h> 38 #include <sys/kernel.h> 39 #include <sys/proc.h> 40 #include <sys/mount.h> 41 #include <sys/malloc.h> 42 #include <sys/namei.h> 43 #include <sys/vnode.h> 44 #include <sys/file.h> 45 #include <sys/acct.h> 46 #include <sys/exec.h> 47 #include <sys/ktrace.h> 48 #include <sys/resourcevar.h> 49 #include <sys/wait.h> 50 #include <sys/mman.h> 51 #include <sys/signalvar.h> 52 #include <sys/stat.h> 53 54 #include <sys/syscallargs.h> 55 56 #include <vm/vm.h> 57 #include <vm/vm_kern.h> 58 59 #include <machine/cpu.h> 60 #include <machine/reg.h> 61 62 #ifdef COPY_SIGCODE 63 extern char sigcode[], esigcode[]; 64 #endif 65 66 /* 67 * check exec: 68 * given an "executable" described in the exec package's namei info, 69 * see what we can do with it. 70 * 71 * ON ENTRY: 72 * exec package with appropriate namei info 73 * proc pointer of exec'ing proc 74 * NO SELF-LOCKED VNODES 75 * 76 * ON EXIT: 77 * error: nothing held, etc. exec header still allocated. 78 * ok: filled exec package, one locked vnode. 79 * 80 * EXEC SWITCH ENTRY: 81 * Locked vnode to check, exec package, proc. 82 * 83 * EXEC SWITCH EXIT: 84 * ok: return 0, filled exec package, one locked vnode. 85 * error: destructive: 86 * everything deallocated execept exec header. 87 * non-descructive: 88 * error code, locked vnode, exec header unmodified 89 */ 90 int 91 check_exec(p, epp) 92 struct proc *p; 93 struct exec_package *epp; 94 { 95 int error, i; 96 struct vnode *vp; 97 char *cp, *ep, *name; 98 struct nameidata *ndp; 99 int resid; 100 101 ndp = epp->ep_ndp; 102 ndp->ni_cnd.cn_nameiop = LOOKUP; 103 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME; 104 /* first get the vnode */ 105 if (error = namei(ndp)) 106 return error; 107 epp->ep_vp = vp = ndp->ni_vp; 108 109 /* check for regular file */ 110 if (vp->v_type != VREG) { 111 error = EACCES; 112 goto bad1; 113 } 114 115 /* get attributes */ 116 if (error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) 117 goto bad1; 118 119 /* Check mount point */ 120 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 121 error = EACCES; 122 goto bad1; 123 } 124 if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 125 epp->ep_vap->va_mode &= ~(VSUID | VSGID); 126 127 /* check access. for root we have to see if any exec bit on */ 128 if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) 129 goto bad1; 130 if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 131 error = EACCES; 132 goto bad1; 133 } 134 135 /* try to open it */ 136 if (error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) 137 goto bad1; 138 139 /* now we have the file, get the exec header */ 140 if (error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0, 141 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p)) 142 goto bad2; 143 epp->ep_hdrvalid = epp->ep_hdrlen - resid; 144 145 /* 146 * set up the vmcmds for creation of the process 147 * address space 148 */ 149 error = ENOEXEC; 150 for (i = 0; i < nexecs && error != 0; i++) { 151 if (execsw[i].es_check != NULL) 152 error = (*execsw[i].es_check)(p, epp); 153 if (epp->ep_flags & EXEC_DESTR && error != 0) 154 return error; 155 } 156 if (!error) { 157 /* check that entry point is sane */ 158 if (epp->ep_entry > VM_MAXUSER_ADDRESS) 159 error = ENOEXEC; 160 161 /* check limits */ 162 if ((epp->ep_tsize > MAXTSIZ) || 163 (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur)) 164 error = ENOMEM; 165 166 if (!error) 167 return (0); 168 } 169 170 /* 171 * free any vmspace-creation commands, 172 * and release their references 173 */ 174 kill_vmcmds(&epp->ep_vmcmds); 175 176 bad2: 177 /* 178 * unlock and close the vnode, restore the old one, free the 179 * pathname buf, and punt. 180 */ 181 VOP_UNLOCK(vp); 182 vn_close(vp, FREAD, p->p_ucred, p); 183 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 184 return error; 185 186 bad1: 187 /* 188 * free the namei pathname buffer, and put the vnode 189 * (which we don't yet have open). 190 */ 191 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 192 vput(vp); 193 return error; 194 } 195 196 /* 197 * exec system call 198 */ 199 /* ARGSUSED */ 200 execve(p, uap, retval) 201 register struct proc *p; 202 register struct execve_args /* { 203 syscallarg(char *) path; 204 syscallarg(char * *) argp; 205 syscallarg(char * *) envp; 206 } */ *uap; 207 register_t *retval; 208 { 209 int error, i; 210 struct exec_package pack; 211 struct nameidata nid; 212 struct vattr attr; 213 struct ucred *cred = p->p_ucred; 214 char *argp; 215 char **cpp, *dp, *sp, *np; 216 long argc, envc; 217 size_t len; 218 char *stack; 219 struct ps_strings arginfo; 220 struct vmspace *vm = p->p_vmspace; 221 char **tmpfap; 222 int szsigcode; 223 224 /* 225 * figure out the maximum size of an exec header, if necessary. 226 * XXX should be able to keep LKM code from modifying exec switch 227 * when we're still using it, but... 228 */ 229 if (exec_maxhdrsz == 0) { 230 for (i = 0; i < nexecs; i++) 231 if (execsw[i].es_check != NULL 232 && execsw[i].es_hdrsz > exec_maxhdrsz) 233 exec_maxhdrsz = execsw[i].es_hdrsz; 234 } 235 236 /* init the namei data to point the file user's program name */ 237 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 238 239 /* 240 * initialize the fields of the exec package. 241 */ 242 pack.ep_name = SCARG(uap, path); 243 MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK); 244 pack.ep_hdrlen = exec_maxhdrsz; 245 pack.ep_hdrvalid = 0; 246 pack.ep_ndp = &nid; 247 pack.ep_setup = NULL; /* assume no setup function */ 248 pack.ep_setup_arg = NULL; 249 pack.ep_setup_arglen = 0; 250 pack.ep_vmcmds.evs_cnt = 0; 251 pack.ep_vmcmds.evs_used = 0; 252 pack.ep_vap = &attr; 253 pack.ep_emul = EMUL_NETBSD; 254 pack.ep_flags = 0; 255 #ifdef COPY_SIGCODE 256 pack.ep_sigcode = sigcode; 257 pack.ep_esigcode = esigcode; 258 #endif 259 260 /* see if we can run it. */ 261 if (error = check_exec(p, &pack)) 262 goto freehdr; 263 264 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 265 266 /* allocate an argument buffer */ 267 argp = (char *) kmem_alloc_wait(exec_map, NCARGS); 268 #ifdef DIAGNOSTIC 269 if (argp == (vm_offset_t) 0) 270 panic("execve: argp == NULL"); 271 #endif 272 dp = argp; 273 argc = 0; 274 275 /* copy the fake args list, if there's one, freeing it as we go */ 276 if (pack.ep_flags & EXEC_HASARGL) { 277 tmpfap = pack.ep_fa; 278 while (*tmpfap != NULL) { 279 char *cp; 280 281 cp = *tmpfap; 282 while (*cp) 283 *dp++ = *cp++; 284 *dp++; 285 286 FREE(*tmpfap, M_EXEC); 287 tmpfap++; argc++; 288 } 289 FREE(pack.ep_fa, M_EXEC); 290 pack.ep_flags &= ~EXEC_HASARGL; 291 } 292 293 /* Now get argv & environment */ 294 if (!(cpp = SCARG(uap, argp))) { 295 error = EINVAL; 296 goto bad; 297 } 298 299 if (pack.ep_flags & EXEC_SKIPARG) 300 cpp++; 301 302 while (1) { 303 len = argp + ARG_MAX - dp; 304 if (error = copyin(cpp, &sp, sizeof(sp))) 305 goto bad; 306 if (!sp) 307 break; 308 if (error = copyinstr(sp, dp, len, &len)) { 309 if (error == ENAMETOOLONG) 310 error = E2BIG; 311 goto bad; 312 } 313 dp += len; 314 cpp++; 315 argc++; 316 } 317 318 envc = 0; 319 if (cpp = SCARG(uap, envp)) { /* environment need not be there */ 320 while (1) { 321 len = argp + ARG_MAX - dp; 322 if (error = copyin(cpp, &sp, sizeof(sp))) 323 goto bad; 324 if (!sp) 325 break; 326 if (error = copyinstr(sp, dp, len, &len)) { 327 if (error == ENAMETOOLONG) 328 error = E2BIG; 329 goto bad; 330 } 331 dp += len; 332 cpp++; 333 envc++; 334 } 335 } 336 337 dp = (char *) ALIGN(dp); 338 339 #ifdef COPY_SIGCODE 340 szsigcode = pack.ep_esigcode - pack.ep_sigcode; 341 #else 342 szsigcode = 0; 343 #endif 344 345 /* Now check if args & environ fit into new stack */ 346 len = ((argc + envc + 2 + pack.ep_setup_arglen) * sizeof(char *) + 347 sizeof(long) + dp + STACKGAPLEN + szsigcode + 348 sizeof(struct ps_strings)) - argp; 349 #ifdef COMPAT_LINUX 350 /* XXXX need this for envp and argv on stack, and sigcode */ 351 if (pack.ep_emul == EMUL_LINUX) 352 len += 2 * sizeof (char *); 353 #endif 354 len = ALIGN(len); /* make the stack "safely" aligned */ 355 356 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 357 error = ENOMEM; 358 goto bad; 359 } 360 361 /* adjust "active stack depth" for process VSZ */ 362 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 363 364 /* Unmap old program */ 365 #ifdef sparc 366 kill_user_windows(p); /* before stack addresses go away */ 367 #endif 368 /* Kill shared memory and unmap old program */ 369 #ifdef SYSVSHM 370 if (vm->vm_shm) 371 shmexit(p); 372 #endif 373 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 374 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 375 376 /* Now map address space */ 377 vm->vm_taddr = (char *) pack.ep_taddr; 378 vm->vm_tsize = btoc(pack.ep_tsize); 379 vm->vm_daddr = (char *) pack.ep_daddr; 380 vm->vm_dsize = btoc(pack.ep_dsize); 381 vm->vm_ssize = btoc(pack.ep_ssize); 382 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr; 383 384 /* create the new process's VM space by running the vmcmds */ 385 #ifdef DIAGNOSTIC 386 if (pack.ep_vmcmds.evs_used == 0) 387 panic("execve: no vmcmds"); 388 #endif 389 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) { 390 struct exec_vmcmd *vcp; 391 392 vcp = &pack.ep_vmcmds.evs_cmds[i]; 393 error = (*vcp->ev_proc)(p, vcp); 394 } 395 396 /* free the vmspace-creation commands, and release their references */ 397 kill_vmcmds(&pack.ep_vmcmds); 398 399 /* if an error happened, deallocate and punt */ 400 if (error) 401 goto exec_abort; 402 403 /* remember information about the process */ 404 arginfo.ps_nargvstr = argc; 405 arginfo.ps_nenvstr = envc; 406 407 /* Now copy argc, args & environ to new stack */ 408 stack = (char *) (USRSTACK - len); 409 cpp = (char **) stack; 410 411 if (copyout(&argc, cpp++, sizeof(argc))) 412 goto exec_abort; 413 #ifdef COMPAT_LINUX 414 /* XXXX Linux puts argv and envp on stack too, store argv now */ 415 if (pack.ep_emul == EMUL_LINUX) { 416 char **argv_loc = cpp + 2, **stk = (char **) stack; 417 418 if (copyout(&argv_loc, &stk[1], sizeof (argv_loc))) 419 goto exec_abort; 420 /* leave room for envp and argv */ 421 cpp += 2; 422 } 423 #endif 424 dp = (char *) (cpp + argc + envc + 2 + pack.ep_setup_arglen); 425 sp = argp; 426 np = 0; 427 428 /* XXX don't copy them out, remap them! */ 429 arginfo.ps_argvstr = dp; /* remember location of argv for later */ 430 for (; --argc >= 0; sp += len, dp += len) { 431 if (copyout(&dp, cpp++, sizeof(dp)) || 432 copyoutstr(sp, dp, ARG_MAX, &len)) 433 goto exec_abort; 434 } 435 if (copyout(&np, cpp++, sizeof(np))) 436 goto exec_abort; 437 438 #ifdef COMPAT_LINUX 439 /* XXXX Linux puts argv and envp on stack too, store envp now */ 440 if (pack.ep_emul == EMUL_LINUX) { 441 char **envp_loc = cpp, **stk = (char **) stack; 442 443 if (copyout(&envp_loc, &stk[2], sizeof (envp_loc))) 444 goto exec_abort; 445 } 446 #endif 447 448 arginfo.ps_envstr = dp; /* remember location of envp for later */ 449 for (; --envc >= 0; sp += len, dp += len) { 450 if (copyout(&dp, cpp++, sizeof(dp)) || 451 copyoutstr(sp, dp, ARG_MAX, &len)) 452 goto exec_abort; 453 } 454 if (copyout(&np, cpp++, sizeof(np))) 455 goto exec_abort; 456 457 if (pack.ep_setup != NULL) 458 (*pack.ep_setup)(EXEC_SETUP_ADDARGS, p, &pack, cpp); 459 460 /* copy out the process's ps_strings structure */ 461 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo))) 462 goto exec_abort; 463 464 #ifdef COPY_SIGCODE 465 /* copy out the process's signal trapoline code */ 466 if (copyout((char *) pack.ep_sigcode, ((char *) PS_STRINGS) - szsigcode, 467 szsigcode)) { 468 goto exec_abort; 469 } 470 #endif 471 472 fdcloseexec(p); /* handle close on exec */ 473 execsigs(p); /* reset catched signals */ 474 475 /* set command name & other accounting info */ 476 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 477 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len); 478 p->p_comm[len] = 0; 479 p->p_acflag &= ~AFORK; 480 481 /* record proc's vnode, for use by procfs and others */ 482 if (p->p_textvp) 483 vrele(p->p_textvp); 484 VREF(pack.ep_vp); 485 p->p_textvp = pack.ep_vp; 486 487 p->p_flag |= P_EXEC; 488 if (p->p_flag & P_PPWAIT) { 489 p->p_flag &= ~P_PPWAIT; 490 wakeup((caddr_t) p->p_pptr); 491 } 492 493 /* 494 * deal with set[ug]id. 495 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id. 496 */ 497 p->p_flag &= ~P_SUGID; 498 if (((attr.va_mode & VSUID) != 0 && 499 p->p_ucred->cr_uid != attr.va_uid) 500 || (attr.va_mode & VSGID) != 0 && 501 p->p_ucred->cr_gid != attr.va_gid) { 502 p->p_ucred = crcopy(cred); 503 #ifdef KTRACE 504 /* 505 * If process is being ktraced, turn off - unless 506 * root set it. 507 */ 508 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) { 509 vrele(p->p_tracep); 510 p->p_tracep = NULL; 511 p->p_traceflag = 0; 512 } 513 #endif 514 if (attr.va_mode & VSUID) 515 p->p_ucred->cr_uid = attr.va_uid; 516 if (attr.va_mode & VSGID) 517 p->p_ucred->cr_gid = attr.va_gid; 518 p->p_flag |= P_SUGID; 519 } 520 p->p_cred->p_svuid = p->p_ucred->cr_uid; 521 p->p_cred->p_svgid = p->p_ucred->cr_gid; 522 523 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 524 525 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 526 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 527 vput(pack.ep_vp); 528 529 /* setup new registers and do misc. setup. */ 530 setregs(p, pack.ep_entry, (u_long) stack, retval); 531 if (pack.ep_setup != NULL) 532 (*pack.ep_setup)(EXEC_SETUP_FINISH, p, &pack, NULL); 533 534 if (p->p_flag & P_TRACED) 535 psignal(p, SIGTRAP); 536 537 p->p_emul = pack.ep_emul; 538 FREE(pack.ep_hdr, M_EXEC); 539 return 0; 540 541 bad: 542 if (pack.ep_setup != NULL) 543 (*pack.ep_setup)(EXEC_SETUP_CLEANUP, p, &pack, dp); 544 /* free the vmspace-creation commands, and release their references */ 545 kill_vmcmds(&pack.ep_vmcmds); 546 /* kill any opened file descriptor, if necessary */ 547 if (pack.ep_flags & EXEC_HASFD) { 548 pack.ep_flags &= ~EXEC_HASFD; 549 (void) fdrelease(p, pack.ep_fd); 550 } 551 /* close and put the exec'd file */ 552 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 553 vput(pack.ep_vp); 554 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 555 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 556 557 freehdr: 558 FREE(pack.ep_hdr, M_EXEC); 559 return error; 560 561 exec_abort: 562 /* 563 * the old process doesn't exist anymore. exit gracefully. 564 * get rid of the (new) address space we have created, if any, get rid 565 * of our namei data and vnode, and exit noting failure 566 */ 567 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 568 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 569 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 570 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 571 vput(pack.ep_vp); 572 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 573 FREE(pack.ep_hdr, M_EXEC); 574 exit1(p, W_EXITCODE(0, SIGABRT)); 575 exit1(p, -1); 576 577 /* NOTREACHED */ 578 return 0; 579 } 580