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