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