1 /* $NetBSD: netbsd32_execve.c,v 1.5 2001/07/29 21:28:20 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #if defined(_KERNEL_OPT) 32 #include "opt_ktrace.h" 33 #endif 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/mount.h> 39 #include <sys/stat.h> 40 #include <sys/wait.h> 41 #include <sys/ktrace.h> 42 #include <sys/vnode.h> 43 #include <sys/file.h> 44 #include <sys/filedesc.h> 45 #include <sys/namei.h> 46 47 #include <uvm/uvm_extern.h> 48 49 #include <sys/syscallargs.h> 50 #include <sys/proc.h> 51 #include <sys/acct.h> 52 #include <sys/exec.h> 53 54 #include <compat/netbsd32/netbsd32.h> 55 #include <compat/netbsd32/netbsd32_syscall.h> 56 #include <compat/netbsd32/netbsd32_syscallargs.h> 57 58 /* this is provided by kern/kern_exec.c */ 59 extern int exec_maxhdrsz; 60 extern struct lock exec_lock; 61 62 /* 63 * Need to completly reimplement this syscall due to argument copying. 64 */ 65 /* ARGSUSED */ 66 int 67 netbsd32_execve(p, v, retval) 68 struct proc *p; 69 void *v; 70 register_t *retval; 71 { 72 struct netbsd32_execve_args /* { 73 syscallarg(const netbsd32_charp) path; 74 syscallarg(netbsd32_charpp) argp; 75 syscallarg(netbsd32_charpp) envp; 76 } */ *uap = v; 77 struct sys_execve_args ua; 78 caddr_t sg; 79 80 NETBSD32TOP_UAP(path, const char); 81 NETBSD32TOP_UAP(argp, char *); 82 NETBSD32TOP_UAP(envp, char *); 83 sg = stackgap_init(p->p_emul); 84 CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path)); 85 86 return netbsd32_execve2(p, &ua, retval); 87 } 88 89 int 90 netbsd32_execve2(p, uap, retval) 91 struct proc *p; 92 struct sys_execve_args *uap; 93 register_t *retval; 94 { 95 /* Function args */ 96 int error, i; 97 struct exec_package pack; 98 struct nameidata nid; 99 struct vattr attr; 100 struct ucred *cred = p->p_ucred; 101 char *argp; 102 netbsd32_charp const *cpp; 103 char *dp; 104 netbsd32_charp sp; 105 long argc, envc; 106 size_t len; 107 char *stack; 108 struct ps_strings arginfo; 109 struct vmspace *vm; 110 char **tmpfap; 111 int szsigcode; 112 struct exec_vmcmd *base_vcp = NULL; 113 114 /* 115 * Init the namei data to point the file user's program name. 116 * This is done here rather than in check_exec(), so that it's 117 * possible to override this settings if any of makecmd/probe 118 * functions call check_exec() recursively - for example, 119 * see exec_script_makecmds(). 120 */ 121 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 122 123 /* 124 * initialize the fields of the exec package. 125 */ 126 pack.ep_name = SCARG(uap, path); 127 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK); 128 pack.ep_hdrlen = exec_maxhdrsz; 129 pack.ep_hdrvalid = 0; 130 pack.ep_ndp = &nid; 131 pack.ep_emul_arg = NULL; 132 pack.ep_vmcmds.evs_cnt = 0; 133 pack.ep_vmcmds.evs_used = 0; 134 pack.ep_vap = &attr; 135 pack.ep_flags = 0; 136 137 lockmgr(&exec_lock, LK_SHARED, NULL); 138 139 /* see if we can run it. */ 140 if ((error = check_exec(p, &pack)) != 0) 141 goto freehdr; 142 143 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 144 145 /* allocate an argument buffer */ 146 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS); 147 #ifdef DIAGNOSTIC 148 if (argp == (vaddr_t) 0) 149 panic("netbsd32_execve: argp == NULL"); 150 #endif 151 dp = argp; 152 argc = 0; 153 154 /* copy the fake args list, if there's one, freeing it as we go */ 155 if (pack.ep_flags & EXEC_HASARGL) { 156 tmpfap = pack.ep_fa; 157 while (*tmpfap != NULL) { 158 char *cp; 159 160 cp = *tmpfap; 161 while (*cp) 162 *dp++ = *cp++; 163 dp++; 164 165 FREE(*tmpfap, M_EXEC); 166 tmpfap++; argc++; 167 } 168 FREE(pack.ep_fa, M_EXEC); 169 pack.ep_flags &= ~EXEC_HASARGL; 170 } 171 172 /* Now get argv & environment */ 173 if (!(cpp = (netbsd32_charp *)SCARG(uap, argp))) { 174 error = EINVAL; 175 goto bad; 176 } 177 178 if (pack.ep_flags & EXEC_SKIPARG) 179 cpp++; 180 181 while (1) { 182 len = argp + ARG_MAX - dp; 183 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 184 goto bad; 185 if (!sp) 186 break; 187 if ((error = copyinstr((char *)(u_long)sp, dp, 188 len, &len)) != 0) { 189 if (error == ENAMETOOLONG) 190 error = E2BIG; 191 goto bad; 192 } 193 dp += len; 194 cpp++; 195 argc++; 196 } 197 198 envc = 0; 199 /* environment need not be there */ 200 if ((cpp = (netbsd32_charp *)SCARG(uap, envp)) != NULL ) { 201 while (1) { 202 len = argp + ARG_MAX - dp; 203 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 204 goto bad; 205 if (!sp) 206 break; 207 if ((error = copyinstr((char *)(u_long)sp, 208 dp, len, &len)) != 0) { 209 if (error == ENAMETOOLONG) 210 error = E2BIG; 211 goto bad; 212 } 213 dp += len; 214 cpp++; 215 envc++; 216 } 217 } 218 219 dp = (char *) ALIGN(dp); 220 221 szsigcode = pack.ep_es->es_emul->e_esigcode - 222 pack.ep_es->es_emul->e_sigcode; 223 224 /* Now check if args & environ fit into new stack */ 225 if (pack.ep_flags & EXEC_32) 226 len = ((argc + envc + 2 + pack.ep_es->es_arglen) * 227 sizeof(int) + sizeof(int) + dp + STACKGAPLEN + 228 szsigcode + sizeof(struct ps_strings)) - argp; 229 else 230 len = ((argc + envc + 2 + pack.ep_es->es_arglen) * 231 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN + 232 szsigcode + sizeof(struct ps_strings)) - argp; 233 234 len = ALIGN(len); /* make the stack "safely" aligned */ 235 236 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 237 error = ENOMEM; 238 goto bad; 239 } 240 241 /* adjust "active stack depth" for process VSZ */ 242 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 243 244 /* 245 * Do whatever is necessary to prepare the address space 246 * for remapping. Note that this might replace the current 247 * vmspace with another! 248 */ 249 uvmspace_exec(p, VM_MIN_ADDRESS, (vaddr_t)pack.ep_minsaddr); 250 251 /* Now map address space */ 252 vm = p->p_vmspace; 253 vm->vm_taddr = (char *) pack.ep_taddr; 254 vm->vm_tsize = btoc(pack.ep_tsize); 255 vm->vm_daddr = (char *) pack.ep_daddr; 256 vm->vm_dsize = btoc(pack.ep_dsize); 257 vm->vm_ssize = btoc(pack.ep_ssize); 258 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr; 259 vm->vm_minsaddr = (char *) pack.ep_minsaddr; 260 261 /* create the new process's VM space by running the vmcmds */ 262 #ifdef DIAGNOSTIC 263 if (pack.ep_vmcmds.evs_used == 0) 264 panic("netbsd32_execve: no vmcmds"); 265 #endif 266 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) { 267 struct exec_vmcmd *vcp; 268 269 vcp = &pack.ep_vmcmds.evs_cmds[i]; 270 if (vcp->ev_flags & VMCMD_RELATIVE) { 271 #ifdef DIAGNOSTIC 272 if (base_vcp == NULL) 273 panic("netbsd32_execve: relative vmcmd with no base"); 274 if (vcp->ev_flags & VMCMD_BASE) 275 panic("netbsd32_execve: illegal base & relative vmcmd"); 276 #endif 277 vcp->ev_addr += base_vcp->ev_addr; 278 } 279 error = (*vcp->ev_proc)(p, vcp); 280 #ifdef DEBUG 281 if (error) { 282 int j; 283 284 for (j = 0; j <= i; j++) 285 printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", j, 286 vcp[j-i].ev_addr, vcp[j-i].ev_len, 287 vcp[j-i].ev_offset); 288 } 289 #endif 290 if (vcp->ev_flags & VMCMD_BASE) 291 base_vcp = vcp; 292 } 293 294 /* free the vmspace-creation commands, and release their references */ 295 kill_vmcmds(&pack.ep_vmcmds); 296 297 /* if an error happened, deallocate and punt */ 298 if (error) { 299 #ifdef DEBUG 300 printf("netbsd32_execve: vmcmd %i failed: %d\n", i-1, error); 301 #endif 302 goto exec_abort; 303 } 304 305 /* remember information about the process */ 306 arginfo.ps_nargvstr = argc; 307 arginfo.ps_nenvstr = envc; 308 309 stack = (char *) (vm->vm_minsaddr - len); 310 /* Now copy argc, args & environ to new stack */ 311 error = (*pack.ep_es->es_copyargs)(&pack, &arginfo, 312 &stack, argp); 313 if (error) { 314 #ifdef DEBUG 315 printf("netbsd32_execve: copyargs failed\n"); 316 #endif 317 goto exec_abort; 318 } 319 /* restore the stack back to its original point */ 320 stack = (char *) (vm->vm_minsaddr - len); 321 322 /* fill process ps_strings info */ 323 p->p_psstr = (struct ps_strings *)(stack - sizeof(struct ps_strings)); 324 p->p_psargv = offsetof(struct ps_strings, ps_argvstr); 325 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr); 326 p->p_psenv = offsetof(struct ps_strings, ps_envstr); 327 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr); 328 329 /* copy out the process's ps_strings structure */ 330 if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) { 331 #ifdef DEBUG 332 printf("netbsd32_execve: ps_strings copyout failed\n"); 333 #endif 334 goto exec_abort; 335 } 336 337 /* copy out the process's signal trapoline code */ 338 if (szsigcode) { 339 if (copyout((char *)pack.ep_es->es_emul->e_sigcode, 340 p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode, 341 szsigcode)) { 342 #ifdef DEBUG 343 printf("netbsd32_execve: sig trampoline copyout failed\n"); 344 #endif 345 goto exec_abort; 346 } 347 #ifdef PMAP_NEED_PROCWR 348 /* This is code. Let the pmap do what is needed. */ 349 pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode); 350 #endif 351 } 352 353 stopprofclock(p); /* stop profiling */ 354 fdcloseexec(p); /* handle close on exec */ 355 execsigs(p); /* reset catched signals */ 356 p->p_ctxlink = NULL; /* reset ucontext link */ 357 358 /* set command name & other accounting info */ 359 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 360 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len); 361 p->p_comm[len] = 0; 362 p->p_acflag &= ~AFORK; 363 364 /* record proc's vnode, for use by procfs and others */ 365 if (p->p_textvp) 366 vrele(p->p_textvp); 367 VREF(pack.ep_vp); 368 p->p_textvp = pack.ep_vp; 369 370 p->p_flag |= P_EXEC; 371 if (p->p_flag & P_PPWAIT) { 372 p->p_flag &= ~P_PPWAIT; 373 wakeup((caddr_t) p->p_pptr); 374 } 375 376 /* 377 * deal with set[ug]id. 378 * MNT_NOSUID has already been used to disable s[ug]id. 379 */ 380 if ((p->p_flag & P_TRACED) == 0 && 381 382 (((attr.va_mode & S_ISUID) != 0 && 383 p->p_ucred->cr_uid != attr.va_uid) || 384 385 ((attr.va_mode & S_ISGID) != 0 && 386 p->p_ucred->cr_gid != attr.va_gid))) { 387 /* 388 * Mark the process as SUGID before we do 389 * anything that might block. 390 */ 391 p_sugid(p); 392 393 p->p_ucred = crcopy(cred); 394 #ifdef KTRACE 395 /* 396 * If process is being ktraced, turn off - unless 397 * root set it. 398 */ 399 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) 400 ktrderef(p); 401 #endif 402 if (attr.va_mode & S_ISUID) 403 p->p_ucred->cr_uid = attr.va_uid; 404 if (attr.va_mode & S_ISGID) 405 p->p_ucred->cr_gid = attr.va_gid; 406 } else 407 p->p_flag &= ~P_SUGID; 408 p->p_cred->p_svuid = p->p_ucred->cr_uid; 409 p->p_cred->p_svgid = p->p_ucred->cr_gid; 410 411 doexechooks(p); 412 413 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 414 415 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 416 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 417 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 418 vput(pack.ep_vp); 419 420 /* setup new registers and do misc. setup. */ 421 (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack); 422 423 if (p->p_flag & P_TRACED) 424 psignal(p, SIGTRAP); 425 426 free(pack.ep_hdr, M_EXEC); 427 428 /* 429 * Call emulation specific exec hook. This can setup setup per-process 430 * p->p_emuldata or do any other per-process stuff an emulation needs. 431 * 432 * If we are executing process of different emulation than the 433 * original forked process, call e_proc_exit() of the old emulation 434 * first, then e_proc_exec() of new emulation. If the emulation is 435 * same, the exec hook code should deallocate any old emulation 436 * resources held previously by this process. 437 */ 438 if (p->p_emul && p->p_emul->e_proc_exit 439 && p->p_emul != pack.ep_es->es_emul) 440 (*p->p_emul->e_proc_exit)(p); 441 442 /* 443 * Call exec hook. Emulation code may NOT store reference to anything 444 * from &pack. 445 */ 446 if (pack.ep_es->es_emul->e_proc_exec) 447 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack); 448 449 /* update p_emul, the old value is no longer needed */ 450 p->p_emul = pack.ep_es->es_emul; 451 452 #ifdef KTRACE 453 if (KTRPOINT(p, KTR_EMUL)) 454 ktremul(p); 455 #endif 456 457 lockmgr(&exec_lock, LK_RELEASE, NULL); 458 459 return (EJUSTRETURN); 460 461 bad: 462 /* free the vmspace-creation commands, and release their references */ 463 kill_vmcmds(&pack.ep_vmcmds); 464 /* kill any opened file descriptor, if necessary */ 465 if (pack.ep_flags & EXEC_HASFD) { 466 pack.ep_flags &= ~EXEC_HASFD; 467 (void) fdrelease(p, pack.ep_fd); 468 } 469 /* close and put the exec'd file */ 470 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 471 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 472 vput(pack.ep_vp); 473 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 474 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 475 476 freehdr: 477 lockmgr(&exec_lock, LK_RELEASE, NULL); 478 479 free(pack.ep_hdr, M_EXEC); 480 return error; 481 482 exec_abort: 483 lockmgr(&exec_lock, LK_RELEASE, NULL); 484 485 /* 486 * the old process doesn't exist anymore. exit gracefully. 487 * get rid of the (new) address space we have created, if any, get rid 488 * of our namei data and vnode, and exit noting failure 489 */ 490 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 491 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 492 if (pack.ep_emul_arg) 493 FREE(pack.ep_emul_arg, M_TEMP); 494 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 495 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 496 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 497 vput(pack.ep_vp); 498 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 499 free(pack.ep_hdr, M_EXEC); 500 exit1(p, W_EXITCODE(error, SIGABRT)); 501 502 /* NOTREACHED */ 503 return 0; 504 } 505