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