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