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