netbsd32_execve.c revision 1.4
1/* $NetBSD: netbsd32_execve.c,v 1.4 2001/06/15 17:24:20 thorpej 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 */ 59extern int exec_maxhdrsz; 60extern struct lock exec_lock; 61 62/* 63 * Need to completly reimplement this syscall due to argument copying. 64 */ 65/* ARGSUSED */ 66int 67netbsd32_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 89int 90netbsd32_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 if (!(*pack.ep_es->es_copyargs)(&pack, &arginfo, stack, argp)) { 312#ifdef DEBUG 313 printf("netbsd32_execve: copyargs failed\n"); 314#endif 315 goto exec_abort; 316 } 317 318 /* fill process ps_strings info */ 319 p->p_psstr = (struct ps_strings *)(stack - sizeof(struct ps_strings)); 320 p->p_psargv = offsetof(struct ps_strings, ps_argvstr); 321 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr); 322 p->p_psenv = offsetof(struct ps_strings, ps_envstr); 323 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr); 324 325 /* copy out the process's ps_strings structure */ 326 if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) { 327#ifdef DEBUG 328 printf("netbsd32_execve: ps_strings copyout failed\n"); 329#endif 330 goto exec_abort; 331 } 332 333 /* copy out the process's signal trapoline code */ 334 if (szsigcode) { 335 if (copyout((char *)pack.ep_es->es_emul->e_sigcode, 336 p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode, 337 szsigcode)) { 338#ifdef DEBUG 339 printf("netbsd32_execve: sig trampoline copyout failed\n"); 340#endif 341 goto exec_abort; 342 } 343#ifdef PMAP_NEED_PROCWR 344 /* This is code. Let the pmap do what is needed. */ 345 pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode); 346#endif 347 } 348 349 stopprofclock(p); /* stop profiling */ 350 fdcloseexec(p); /* handle close on exec */ 351 execsigs(p); /* reset catched signals */ 352 p->p_ctxlink = NULL; /* reset ucontext link */ 353 354 /* set command name & other accounting info */ 355 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 356 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len); 357 p->p_comm[len] = 0; 358 p->p_acflag &= ~AFORK; 359 360 /* record proc's vnode, for use by procfs and others */ 361 if (p->p_textvp) 362 vrele(p->p_textvp); 363 VREF(pack.ep_vp); 364 p->p_textvp = pack.ep_vp; 365 366 p->p_flag |= P_EXEC; 367 if (p->p_flag & P_PPWAIT) { 368 p->p_flag &= ~P_PPWAIT; 369 wakeup((caddr_t) p->p_pptr); 370 } 371 372 /* 373 * deal with set[ug]id. 374 * MNT_NOSUID has already been used to disable s[ug]id. 375 */ 376 if ((p->p_flag & P_TRACED) == 0 && 377 378 (((attr.va_mode & S_ISUID) != 0 && 379 p->p_ucred->cr_uid != attr.va_uid) || 380 381 ((attr.va_mode & S_ISGID) != 0 && 382 p->p_ucred->cr_gid != attr.va_gid))) { 383 /* 384 * Mark the process as SUGID before we do 385 * anything that might block. 386 */ 387 p_sugid(p); 388 389 p->p_ucred = crcopy(cred); 390#ifdef KTRACE 391 /* 392 * If process is being ktraced, turn off - unless 393 * root set it. 394 */ 395 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) 396 ktrderef(p); 397#endif 398 if (attr.va_mode & S_ISUID) 399 p->p_ucred->cr_uid = attr.va_uid; 400 if (attr.va_mode & S_ISGID) 401 p->p_ucred->cr_gid = attr.va_gid; 402 } else 403 p->p_flag &= ~P_SUGID; 404 p->p_cred->p_svuid = p->p_ucred->cr_uid; 405 p->p_cred->p_svgid = p->p_ucred->cr_gid; 406 407 doexechooks(p); 408 409 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 410 411 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 412 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 413 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 414 vput(pack.ep_vp); 415 416 /* setup new registers and do misc. setup. */ 417 (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack); 418 419 if (p->p_flag & P_TRACED) 420 psignal(p, SIGTRAP); 421 422 free(pack.ep_hdr, M_EXEC); 423 424 /* 425 * Call emulation specific exec hook. This can setup setup per-process 426 * p->p_emuldata or do any other per-process stuff an emulation needs. 427 * 428 * If we are executing process of different emulation than the 429 * original forked process, call e_proc_exit() of the old emulation 430 * first, then e_proc_exec() of new emulation. If the emulation is 431 * same, the exec hook code should deallocate any old emulation 432 * resources held previously by this process. 433 */ 434 if (p->p_emul && p->p_emul->e_proc_exit 435 && p->p_emul != pack.ep_es->es_emul) 436 (*p->p_emul->e_proc_exit)(p); 437 438 /* 439 * Call exec hook. Emulation code may NOT store reference to anything 440 * from &pack. 441 */ 442 if (pack.ep_es->es_emul->e_proc_exec) 443 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack); 444 445 /* update p_emul, the old value is no longer needed */ 446 p->p_emul = pack.ep_es->es_emul; 447 448#ifdef KTRACE 449 if (KTRPOINT(p, KTR_EMUL)) 450 ktremul(p); 451#endif 452 453 lockmgr(&exec_lock, LK_RELEASE, NULL); 454 455 return (EJUSTRETURN); 456 457bad: 458 /* free the vmspace-creation commands, and release their references */ 459 kill_vmcmds(&pack.ep_vmcmds); 460 /* kill any opened file descriptor, if necessary */ 461 if (pack.ep_flags & EXEC_HASFD) { 462 pack.ep_flags &= ~EXEC_HASFD; 463 (void) fdrelease(p, pack.ep_fd); 464 } 465 /* close and put the exec'd file */ 466 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 467 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 468 vput(pack.ep_vp); 469 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 470 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 471 472freehdr: 473 lockmgr(&exec_lock, LK_RELEASE, NULL); 474 475 free(pack.ep_hdr, M_EXEC); 476 return error; 477 478exec_abort: 479 lockmgr(&exec_lock, LK_RELEASE, NULL); 480 481 /* 482 * the old process doesn't exist anymore. exit gracefully. 483 * get rid of the (new) address space we have created, if any, get rid 484 * of our namei data and vnode, and exit noting failure 485 */ 486 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 487 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 488 if (pack.ep_emul_arg) 489 FREE(pack.ep_emul_arg, M_TEMP); 490 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 491 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 492 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 493 vput(pack.ep_vp); 494 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 495 free(pack.ep_hdr, M_EXEC); 496 exit1(p, W_EXITCODE(0, SIGABRT)); 497 exit1(p, -1); 498 499 /* NOTREACHED */ 500 return 0; 501} 502