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