1 /* $NetBSD: kern_exec.c,v 1.58 1994/12/04 03:10:45 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 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 dp = (char *) ALIGN(dp); 334 } 335 336 /* Now check if args & environ fit into new stack */ 337 len = ((argc + envc + 2 + pack.ep_setup_arglen) * sizeof(char *) + 338 sizeof(int) + dp + STACKGAPLEN + szsigcode + 339 sizeof(struct ps_strings)) - argp; 340 len = ALIGN(len); /* make the stack "safely" aligned */ 341 342 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 343 error = ENOMEM; 344 goto bad; 345 } 346 347 /* adjust "active stack depth" for process VSZ */ 348 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 349 350 /* Unmap old program */ 351 #ifdef sparc 352 kill_user_windows(p); /* before stack addresses go away */ 353 #endif 354 /* Kill shared memory and unmap old program */ 355 #ifdef SYSVSHM 356 if (vm->vm_shm) 357 shmexit(p); 358 #endif 359 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 360 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 361 362 /* Now map address space */ 363 vm->vm_taddr = (char *) pack.ep_taddr; 364 vm->vm_tsize = btoc(pack.ep_tsize); 365 vm->vm_daddr = (char *) pack.ep_daddr; 366 vm->vm_dsize = btoc(pack.ep_dsize); 367 vm->vm_ssize = btoc(pack.ep_ssize); 368 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr; 369 370 /* create the new process's VM space by running the vmcmds */ 371 #ifdef DIAGNOSTIC 372 if (pack.ep_vmcmds.evs_used == 0) 373 panic("execve: no vmcmds"); 374 #endif 375 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) { 376 struct exec_vmcmd *vcp; 377 378 vcp = &pack.ep_vmcmds.evs_cmds[i]; 379 error = (*vcp->ev_proc)(p, vcp); 380 } 381 382 /* free the vmspace-creation commands, and release their references */ 383 kill_vmcmds(&pack.ep_vmcmds); 384 385 /* if an error happened, deallocate and punt */ 386 if (error) 387 goto exec_abort; 388 389 /* remember information about the process */ 390 arginfo.ps_nargvstr = argc; 391 arginfo.ps_nenvstr = envc; 392 393 /* Now copy argc, args & environ to new stack */ 394 stack = (char *) (USRSTACK - len); 395 cpp = (char **) stack; 396 397 if (copyout(&argc, cpp++, sizeof(argc))) 398 goto exec_abort; 399 dp = (char *) (cpp + argc + envc + 2 + pack.ep_setup_arglen); 400 401 /* XXX don't copy them out, remap them! */ 402 arginfo.ps_argvstr = dp; /* remember location of argv for later */ 403 for (sp = argp; --argc >= 0; sp += len, dp += len) { 404 len = strlen(sp) + 1; 405 if (copyout(&dp, cpp++, sizeof(dp)) 406 || copyoutstr(sp, dp, len, 0)) 407 goto exec_abort; 408 } 409 np = 0; 410 if (copyout(&np, cpp++, sizeof(np))) 411 goto exec_abort; 412 413 arginfo.ps_envstr = dp; /* remember location of env for later */ 414 for (; --envc >= 0; sp += len, dp += len) { 415 len = strlen(sp) + 1; 416 if (copyout(&dp, cpp++, sizeof(dp)) 417 || copyoutstr(sp, dp, len, 0)) 418 goto exec_abort; 419 } 420 421 if (copyout(&np, cpp++, sizeof(np))) 422 goto exec_abort; 423 424 if (pack.ep_setup != NULL) 425 (*pack.ep_setup)(EXEC_SETUP_ADDARGS, p, &pack, cpp); 426 427 /* copy out the process's ps_strings structure */ 428 if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo))) 429 goto exec_abort; 430 431 #ifdef COPY_SIGCODE 432 /* copy out the process's signal trapoline code */ 433 if (copyout((char *) sigcode, ((char *) PS_STRINGS) - szsigcode, 434 szsigcode)) { 435 goto exec_abort; 436 } 437 #endif 438 439 fdcloseexec(p); /* handle close on exec */ 440 execsigs(p); /* reset catched signals */ 441 442 /* set command name & other accounting info */ 443 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 444 bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len); 445 p->p_comm[len] = 0; 446 p->p_acflag &= ~AFORK; 447 448 /* record proc's vnode, for use by procfs and others */ 449 if (p->p_textvp) 450 vrele(p->p_textvp); 451 VREF(pack.ep_vp); 452 p->p_textvp = pack.ep_vp; 453 454 p->p_flag |= P_EXEC; 455 if (p->p_flag & P_PPWAIT) { 456 p->p_flag &= ~P_PPWAIT; 457 wakeup((caddr_t) p->p_pptr); 458 } 459 460 /* 461 * deal with set[ug]id. 462 * MNT_NOEXEC and P_TRACED have already been used to disable s[ug]id. 463 */ 464 p->p_flag &= ~P_SUGID; 465 if (((attr.va_mode & VSUID) != 0 && 466 p->p_ucred->cr_uid != attr.va_uid) 467 || (attr.va_mode & VSGID) != 0 && 468 p->p_ucred->cr_gid != attr.va_gid) { 469 p->p_ucred = crcopy(cred); 470 #ifdef KTRACE 471 /* 472 * If process is being ktraced, turn off - unless 473 * root set it. 474 */ 475 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) { 476 vrele(p->p_tracep); 477 p->p_tracep = NULL; 478 p->p_traceflag = 0; 479 } 480 #endif 481 if (attr.va_mode & VSUID) 482 p->p_ucred->cr_uid = attr.va_uid; 483 if (attr.va_mode & VSGID) 484 p->p_ucred->cr_gid = attr.va_gid; 485 p->p_flag |= P_SUGID; 486 } 487 p->p_cred->p_svuid = p->p_ucred->cr_uid; 488 p->p_cred->p_svgid = p->p_ucred->cr_gid; 489 490 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 491 492 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 493 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 494 vput(pack.ep_vp); 495 496 /* setup new registers and do misc. setup. */ 497 setregs(p, pack.ep_entry, (u_long) stack, retval); 498 if (pack.ep_setup != NULL) 499 (*pack.ep_setup)(EXEC_SETUP_FINISH, p, &pack, NULL); 500 501 if (p->p_flag & P_TRACED) 502 psignal(p, SIGTRAP); 503 504 p->p_emul = pack.ep_emul; 505 FREE(pack.ep_hdr, M_EXEC); 506 return 0; 507 508 bad: 509 if (pack.ep_setup != NULL) 510 (*pack.ep_setup)(EXEC_SETUP_CLEANUP, p, &pack, dp); 511 /* free the vmspace-creation commands, and release their references */ 512 kill_vmcmds(&pack.ep_vmcmds); 513 /* kill any opened file descriptor, if necessary */ 514 if (pack.ep_flags & EXEC_HASFD) { 515 pack.ep_flags &= ~EXEC_HASFD; 516 (void) fdclose(p, pack.ep_fd); 517 } 518 /* close and put the exec'd file */ 519 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 520 vput(pack.ep_vp); 521 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 522 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 523 524 freehdr: 525 FREE(pack.ep_hdr, M_EXEC); 526 return error; 527 528 exec_abort: 529 /* 530 * the old process doesn't exist anymore. exit gracefully. 531 * get rid of the (new) address space we have created, if any, get rid 532 * of our namei data and vnode, and exit noting failure 533 */ 534 vm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 535 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 536 FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI); 537 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 538 vput(pack.ep_vp); 539 kmem_free_wakeup(exec_map, (vm_offset_t) argp, NCARGS); 540 FREE(pack.ep_hdr, M_EXEC); 541 exit1(p, W_EXITCODE(0, SIGABRT)); 542 exit1(p, -1); 543 544 /* NOTREACHED */ 545 return 0; 546 } 547