xref: /netbsd-src/sys/compat/netbsd32/netbsd32_execve.c (revision ff9b3386189ab873b5f450bcc9ed559871937e98)
1*ff9b3386Smartin /*	$NetBSD: netbsd32_execve.c,v 1.44 2021/11/11 17:32:46 martin Exp $	*/
2da9e4bd3Smrg 
3da9e4bd3Smrg /*
4da9e4bd3Smrg  * Copyright (c) 1998, 2001 Matthew R. Green
5da9e4bd3Smrg  * All rights reserved.
6da9e4bd3Smrg  *
7da9e4bd3Smrg  * Redistribution and use in source and binary forms, with or without
8da9e4bd3Smrg  * modification, are permitted provided that the following conditions
9da9e4bd3Smrg  * are met:
10da9e4bd3Smrg  * 1. Redistributions of source code must retain the above copyright
11da9e4bd3Smrg  *    notice, this list of conditions and the following disclaimer.
12da9e4bd3Smrg  * 2. Redistributions in binary form must reproduce the above copyright
13da9e4bd3Smrg  *    notice, this list of conditions and the following disclaimer in the
14da9e4bd3Smrg  *    documentation and/or other materials provided with the distribution.
15da9e4bd3Smrg  *
16da9e4bd3Smrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17da9e4bd3Smrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18da9e4bd3Smrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19da9e4bd3Smrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20da9e4bd3Smrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21da9e4bd3Smrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22da9e4bd3Smrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23da9e4bd3Smrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24da9e4bd3Smrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25da9e4bd3Smrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26da9e4bd3Smrg  * SUCH DAMAGE.
27da9e4bd3Smrg  */
28da9e4bd3Smrg 
29dab6ef8bSlukem #include <sys/cdefs.h>
3095e1ffb1Schristos 
31*ff9b3386Smartin __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.44 2021/11/11 17:32:46 martin Exp $");
32da9e4bd3Smrg 
33da9e4bd3Smrg #include <sys/param.h>
34da9e4bd3Smrg #include <sys/systm.h>
3594b761b6Smartin #include <sys/atomic.h>
36da9e4bd3Smrg #include <sys/mount.h>
3794b761b6Smartin #include <sys/namei.h>
38da9e4bd3Smrg #include <sys/stat.h>
3994b761b6Smartin #include <sys/spawn.h>
4094b761b6Smartin #include <sys/uidinfo.h>
41da9e4bd3Smrg #include <sys/vnode.h>
42da9e4bd3Smrg #include <sys/file.h>
43da9e4bd3Smrg #include <sys/filedesc.h>
44da9e4bd3Smrg #include <sys/syscallargs.h>
45da9e4bd3Smrg #include <sys/proc.h>
46da9e4bd3Smrg #include <sys/exec.h>
47da9e4bd3Smrg 
48da9e4bd3Smrg #include <compat/netbsd32/netbsd32.h>
49da9e4bd3Smrg #include <compat/netbsd32/netbsd32_syscall.h>
50da9e4bd3Smrg #include <compat/netbsd32/netbsd32_syscallargs.h>
51da9e4bd3Smrg 
52355bcba7Scube static int
netbsd32_execve_fetch_element(char * const * array,size_t index,char ** value)53355bcba7Scube netbsd32_execve_fetch_element(char * const *array, size_t index, char **value)
54355bcba7Scube {
55355bcba7Scube 	int error;
56355bcba7Scube 	netbsd32_charp const *a32 = (void const *)array;
57355bcba7Scube 	netbsd32_charp e;
585888b16eSelad 
59355bcba7Scube 	error = copyin(a32 + index, &e, sizeof(e));
60355bcba7Scube 	if (error)
61355bcba7Scube 		return error;
62355bcba7Scube 	*value = (char *)NETBSD32PTR64(e);
63355bcba7Scube 	return 0;
64355bcba7Scube }
65da9e4bd3Smrg 
66da9e4bd3Smrg int
netbsd32_execve(struct lwp * l,const struct netbsd32_execve_args * uap,register_t * retval)677e2790cfSdsl netbsd32_execve(struct lwp *l, const struct netbsd32_execve_args *uap, register_t *retval)
68da9e4bd3Smrg {
697e2790cfSdsl 	/* {
70da9e4bd3Smrg 		syscallarg(const netbsd32_charp) path;
71da9e4bd3Smrg 		syscallarg(netbsd32_charpp) argp;
72da9e4bd3Smrg 		syscallarg(netbsd32_charpp) envp;
737e2790cfSdsl 	} */
74da9e4bd3Smrg 
75eb654c05Schristos 	return execve1(l, true, SCARG_P32(uap, path), -1, SCARG_P32(uap, argp),
76d364d308Sdsl 	    SCARG_P32(uap, envp), netbsd32_execve_fetch_element);
77da9e4bd3Smrg }
78f7a1e1f6Smatt 
79f7a1e1f6Smatt int
netbsd32_fexecve(struct lwp * l,const struct netbsd32_fexecve_args * uap,register_t * retval)80f7a1e1f6Smatt netbsd32_fexecve(struct lwp *l, const struct netbsd32_fexecve_args *uap,
81f7a1e1f6Smatt 		 register_t *retval)
82f7a1e1f6Smatt {
83f7a1e1f6Smatt 	/* {
84f7a1e1f6Smatt 		syscallarg(int) fd;
85f7a1e1f6Smatt 		syscallarg(netbsd32_charpp) argp;
86f7a1e1f6Smatt 		syscallarg(netbsd32_charpp) envp;
87f7a1e1f6Smatt 	} */
88f7a1e1f6Smatt 
89eb654c05Schristos 	return execve1(l, false, NULL, SCARG(uap, fd), SCARG_P32(uap, argp),
90df668d1fSchristos 	    SCARG_P32(uap, envp), netbsd32_execve_fetch_element);
91f7a1e1f6Smatt }
9294b761b6Smartin 
93*ff9b3386Smartin static __inline bool
netbsd32_posix_spawn_fae_path(struct posix_spawn_file_actions_entry * fae,struct netbsd32_posix_spawn_file_actions_entry * fae32,char *** pathp,char ** pathp32)94*ff9b3386Smartin netbsd32_posix_spawn_fae_path(
95*ff9b3386Smartin     struct posix_spawn_file_actions_entry *fae,
96*ff9b3386Smartin     struct netbsd32_posix_spawn_file_actions_entry *fae32,
97*ff9b3386Smartin     char ***pathp, char **pathp32)
98*ff9b3386Smartin {
99*ff9b3386Smartin 	switch (fae->fae_action) {
100*ff9b3386Smartin 	case FAE_OPEN:
101*ff9b3386Smartin 		*pathp = &fae->fae_path;
102*ff9b3386Smartin 		*pathp32 = NETBSD32PTR64(fae32->fae_data.open.path);
103*ff9b3386Smartin 		return true;
104*ff9b3386Smartin 	case FAE_CHDIR:
105*ff9b3386Smartin 		*pathp = &fae->fae_chdir_path;
106*ff9b3386Smartin 		*pathp32 = NETBSD32PTR64(fae32->fae_data.chdir.path);
107*ff9b3386Smartin 		return true;
108*ff9b3386Smartin 	default:
109*ff9b3386Smartin 		return false;
110*ff9b3386Smartin 	}
111*ff9b3386Smartin }
112*ff9b3386Smartin 
11394b761b6Smartin static int
netbsd32_posix_spawn_fa_alloc(struct posix_spawn_file_actions ** fap,const struct netbsd32_posix_spawn_file_actions * ufa,rlim_t lim)11494b761b6Smartin netbsd32_posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap,
1152934fa70Smartin     const struct netbsd32_posix_spawn_file_actions *ufa, rlim_t lim)
11694b761b6Smartin {
11794b761b6Smartin 	struct posix_spawn_file_actions *fa;
11894b761b6Smartin 	struct netbsd32_posix_spawn_file_actions fa32;
11994b761b6Smartin 	struct netbsd32_posix_spawn_file_actions_entry *fae32 = NULL, *f32 = NULL;
12094b761b6Smartin 	struct posix_spawn_file_actions_entry *fae;
121*ff9b3386Smartin 	char *pbuf = NULL, **pathp = NULL, *pathp32 = NULL;
12294b761b6Smartin 	int error;
12394b761b6Smartin 	size_t fal, fal32, slen, i = 0;
12494b761b6Smartin 
12594b761b6Smartin 	error = copyin(ufa, &fa32, sizeof(fa32));
12694b761b6Smartin 	if (error)
12794b761b6Smartin 		return error;
12894b761b6Smartin 
12994b761b6Smartin 	if (fa32.len == 0)
13094b761b6Smartin 		return 0;
13194b761b6Smartin 
13294b761b6Smartin 	fa = kmem_alloc(sizeof(*fa), KM_SLEEP);
13394b761b6Smartin 	fa->len = fa->size = fa32.len;
13494b761b6Smartin 
1352934fa70Smartin 	if (fa->len > lim) {
1362934fa70Smartin 		kmem_free(fa, sizeof(*fa));
1372934fa70Smartin 		return EINVAL;
1382934fa70Smartin 	}
1392934fa70Smartin 
14094b761b6Smartin 	fal = fa->len * sizeof(*fae);
14194b761b6Smartin 	fal32 = fa->len * sizeof(*fae32);
14294b761b6Smartin 
14394b761b6Smartin 	fa->fae = kmem_alloc(fal, KM_SLEEP);
14494b761b6Smartin 	fae32 = kmem_alloc(fal32, KM_SLEEP);
14594b761b6Smartin 	error = copyin(NETBSD32PTR64(fa32.fae), fae32, fal32);
14694b761b6Smartin 	if (error)
14794b761b6Smartin 		goto out;
14894b761b6Smartin 
14994b761b6Smartin 	pbuf = PNBUF_GET();
15094b761b6Smartin 	for (; i < fa->len; i++) {
15194b761b6Smartin 		fae = &fa->fae[i];
15294b761b6Smartin 		f32 = &fae32[i];
153cb095f84Smrg 		fae->fae_action = (unsigned)f32->fae_action;
15494b761b6Smartin 		fae->fae_fildes = f32->fae_fildes;
15594b761b6Smartin 		if (fae->fae_action == FAE_DUP2)
15694b761b6Smartin 			fae->fae_data.dup2.newfildes =
15794b761b6Smartin 			    f32->fae_data.dup2.newfildes;
158*ff9b3386Smartin 		if (!netbsd32_posix_spawn_fae_path(fae, f32, &pathp, &pathp32)
159*ff9b3386Smartin 		    || pathp == NULL || pathp32 == NULL)
16094b761b6Smartin 			continue;
161*ff9b3386Smartin 		error = copyinstr(pathp32, pbuf, MAXPATHLEN, &slen);
16294b761b6Smartin 		if (error)
16394b761b6Smartin 			goto out;
164*ff9b3386Smartin 		*pathp = kmem_alloc(slen, KM_SLEEP);
165*ff9b3386Smartin 		memcpy(*pathp, pbuf, slen);
16694b761b6Smartin 		fae->fae_oflag = f32->fae_oflag;
16794b761b6Smartin 		fae->fae_mode = f32->fae_mode;
16894b761b6Smartin 	}
16994b761b6Smartin 	PNBUF_PUT(pbuf);
17094b761b6Smartin 	kmem_free(fae32, fal32);
17194b761b6Smartin 	*fap = fa;
17294b761b6Smartin 	return 0;
17394b761b6Smartin 
17494b761b6Smartin out:
17594b761b6Smartin 	kmem_free(fae32, fal32);
17694b761b6Smartin 	if (pbuf)
17794b761b6Smartin 		PNBUF_PUT(pbuf);
17894b761b6Smartin 	posix_spawn_fa_free(fa, i);
17994b761b6Smartin 	return error;
18094b761b6Smartin }
18194b761b6Smartin 
18294b761b6Smartin int
netbsd32_posix_spawn(struct lwp * l,const struct netbsd32_posix_spawn_args * uap,register_t * retval)18394b761b6Smartin netbsd32_posix_spawn(struct lwp *l,
18494b761b6Smartin 	const struct netbsd32_posix_spawn_args *uap, register_t *retval)
18594b761b6Smartin {
18694b761b6Smartin 	/* {
18794b761b6Smartin 	syscallarg(netbsd32_pid_tp) pid;
18894b761b6Smartin 	syscallarg(const netbsd32_charp) path;
18994b761b6Smartin 	syscallarg(const netbsd32_posix_spawn_file_actionsp) file_actions;
19094b761b6Smartin 	syscallarg(const netbsd32_posix_spawnattrp) attrp;
19194b761b6Smartin 	syscallarg(netbsd32_charpp) argv;
19294b761b6Smartin 	syscallarg(netbsd32_charpp) envp;
19394b761b6Smartin 	} */
19494b761b6Smartin 
19594b761b6Smartin 	int error;
19694b761b6Smartin 	struct posix_spawn_file_actions *fa = NULL;
19794b761b6Smartin 	struct posix_spawnattr *sa = NULL;
19894b761b6Smartin 	pid_t pid;
199b10bf469Srmind 	bool child_ok = false;
2002934fa70Smartin 	rlim_t max_fileactions;
2012934fa70Smartin 	proc_t *p = l->l_proc;
20294b761b6Smartin 
203a29147faSthorpej 	/* check_posix_spawn() increments nprocs for us. */
20494b761b6Smartin 	error = check_posix_spawn(l);
20594b761b6Smartin 	if (error) {
20694b761b6Smartin 		*retval = error;
20794b761b6Smartin 		return 0;
20894b761b6Smartin 	}
20994b761b6Smartin 
21094b761b6Smartin 	/* copy in file_actions struct */
21194b761b6Smartin 	if (SCARG_P32(uap, file_actions) != NULL) {
212d1579b2dSriastradh 		max_fileactions = 2 * uimin(p->p_rlimit[RLIMIT_NOFILE].rlim_cur,
2132934fa70Smartin 		    maxfiles);
21494b761b6Smartin 		error = netbsd32_posix_spawn_fa_alloc(&fa,
2152934fa70Smartin 		    SCARG_P32(uap, file_actions), max_fileactions);
21694b761b6Smartin 		if (error)
217b10bf469Srmind 			goto error_exit;
21894b761b6Smartin 	}
21994b761b6Smartin 
22094b761b6Smartin 	/* copyin posix_spawnattr struct */
22194b761b6Smartin 	if (SCARG_P32(uap, attrp) != NULL) {
22294b761b6Smartin 		sa = kmem_alloc(sizeof(*sa), KM_SLEEP);
22394b761b6Smartin 		error = copyin(SCARG_P32(uap, attrp), sa, sizeof(*sa));
22494b761b6Smartin 		if (error)
225b10bf469Srmind 			goto error_exit;
22694b761b6Smartin 	}
22794b761b6Smartin 
22894b761b6Smartin 	/*
22994b761b6Smartin 	 * Do the spawn
23094b761b6Smartin 	 */
231b10bf469Srmind 	error = do_posix_spawn(l, &pid, &child_ok, SCARG_P32(uap, path), fa,
23294b761b6Smartin 	    sa, SCARG_P32(uap, argv), SCARG_P32(uap, envp),
23394b761b6Smartin 	    netbsd32_execve_fetch_element);
23494b761b6Smartin 	if (error)
235b10bf469Srmind 		goto error_exit;
23694b761b6Smartin 
23794b761b6Smartin 	if (error == 0 && SCARG_P32(uap, pid) != NULL)
23894b761b6Smartin 		error = copyout(&pid, SCARG_P32(uap, pid), sizeof(pid));
23994b761b6Smartin 
24094b761b6Smartin 	*retval = error;
24194b761b6Smartin 	return 0;
24294b761b6Smartin 
243b10bf469Srmind  error_exit:
244b10bf469Srmind  	if (!child_ok) {
24594b761b6Smartin 		(void)chgproccnt(kauth_cred_getuid(l->l_cred), -1);
24694b761b6Smartin 		atomic_dec_uint(&nprocs);
24794b761b6Smartin 
24894b761b6Smartin 		if (sa)
24994b761b6Smartin 			kmem_free(sa, sizeof(*sa));
25094b761b6Smartin 		if (fa)
25194b761b6Smartin 			posix_spawn_fa_free(fa, fa->len);
252b10bf469Srmind 	}
253b10bf469Srmind 
25494b761b6Smartin 	*retval = error;
25594b761b6Smartin 	return 0;
25694b761b6Smartin }
257