Home | History | Annotate | Line # | Download | only in netbsd32
      1  1.44    martin /*	$NetBSD: netbsd32_execve.c,v 1.44 2021/11/11 17:32:46 martin Exp $	*/
      2   1.1       mrg 
      3   1.1       mrg /*
      4   1.1       mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5   1.1       mrg  * All rights reserved.
      6   1.1       mrg  *
      7   1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1       mrg  * modification, are permitted provided that the following conditions
      9   1.1       mrg  * are met:
     10   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15   1.1       mrg  *
     16   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1       mrg  * SUCH DAMAGE.
     27   1.1       mrg  */
     28   1.6     lukem 
     29   1.6     lukem #include <sys/cdefs.h>
     30  1.25  christos 
     31  1.44    martin __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.44 2021/11/11 17:32:46 martin Exp $");
     32   1.1       mrg 
     33   1.1       mrg #include <sys/param.h>
     34   1.1       mrg #include <sys/systm.h>
     35  1.34    martin #include <sys/atomic.h>
     36   1.1       mrg #include <sys/mount.h>
     37  1.34    martin #include <sys/namei.h>
     38   1.1       mrg #include <sys/stat.h>
     39  1.34    martin #include <sys/spawn.h>
     40  1.34    martin #include <sys/uidinfo.h>
     41   1.1       mrg #include <sys/vnode.h>
     42   1.1       mrg #include <sys/file.h>
     43   1.1       mrg #include <sys/filedesc.h>
     44   1.1       mrg #include <sys/syscallargs.h>
     45   1.1       mrg #include <sys/proc.h>
     46   1.1       mrg #include <sys/exec.h>
     47   1.1       mrg 
     48   1.1       mrg #include <compat/netbsd32/netbsd32.h>
     49   1.1       mrg #include <compat/netbsd32/netbsd32_syscall.h>
     50   1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     51   1.1       mrg 
     52  1.23      cube static int
     53  1.23      cube netbsd32_execve_fetch_element(char * const *array, size_t index, char **value)
     54  1.23      cube {
     55  1.23      cube 	int error;
     56  1.23      cube 	netbsd32_charp const *a32 = (void const *)array;
     57  1.23      cube 	netbsd32_charp e;
     58  1.23      cube 
     59  1.23      cube 	error = copyin(a32 + index, &e, sizeof(e));
     60  1.23      cube 	if (error)
     61  1.23      cube 		return error;
     62  1.23      cube 	*value = (char *)NETBSD32PTR64(e);
     63  1.23      cube 	return 0;
     64  1.23      cube }
     65   1.1       mrg 
     66   1.1       mrg int
     67  1.31       dsl netbsd32_execve(struct lwp *l, const struct netbsd32_execve_args *uap, register_t *retval)
     68   1.1       mrg {
     69  1.31       dsl 	/* {
     70   1.1       mrg 		syscallarg(const netbsd32_charp) path;
     71   1.1       mrg 		syscallarg(netbsd32_charpp) argp;
     72   1.1       mrg 		syscallarg(netbsd32_charpp) envp;
     73  1.31       dsl 	} */
     74   1.1       mrg 
     75  1.41  christos 	return execve1(l, true, SCARG_P32(uap, path), -1, SCARG_P32(uap, argp),
     76  1.29       dsl 	    SCARG_P32(uap, envp), netbsd32_execve_fetch_element);
     77   1.1       mrg }
     78  1.33      matt 
     79  1.33      matt int
     80  1.33      matt netbsd32_fexecve(struct lwp *l, const struct netbsd32_fexecve_args *uap,
     81  1.33      matt 		 register_t *retval)
     82  1.33      matt {
     83  1.33      matt 	/* {
     84  1.33      matt 		syscallarg(int) fd;
     85  1.33      matt 		syscallarg(netbsd32_charpp) argp;
     86  1.33      matt 		syscallarg(netbsd32_charpp) envp;
     87  1.33      matt 	} */
     88  1.33      matt 
     89  1.41  christos 	return execve1(l, false, NULL, SCARG(uap, fd), SCARG_P32(uap, argp),
     90  1.40  christos 	    SCARG_P32(uap, envp), netbsd32_execve_fetch_element);
     91  1.33      matt }
     92  1.34    martin 
     93  1.44    martin static __inline bool
     94  1.44    martin netbsd32_posix_spawn_fae_path(
     95  1.44    martin     struct posix_spawn_file_actions_entry *fae,
     96  1.44    martin     struct netbsd32_posix_spawn_file_actions_entry *fae32,
     97  1.44    martin     char ***pathp, char **pathp32)
     98  1.44    martin {
     99  1.44    martin 	switch (fae->fae_action) {
    100  1.44    martin 	case FAE_OPEN:
    101  1.44    martin 		*pathp = &fae->fae_path;
    102  1.44    martin 		*pathp32 = NETBSD32PTR64(fae32->fae_data.open.path);
    103  1.44    martin 		return true;
    104  1.44    martin 	case FAE_CHDIR:
    105  1.44    martin 		*pathp = &fae->fae_chdir_path;
    106  1.44    martin 		*pathp32 = NETBSD32PTR64(fae32->fae_data.chdir.path);
    107  1.44    martin 		return true;
    108  1.44    martin 	default:
    109  1.44    martin 		return false;
    110  1.44    martin 	}
    111  1.44    martin }
    112  1.44    martin 
    113  1.34    martin static int
    114  1.34    martin netbsd32_posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap,
    115  1.38    martin     const struct netbsd32_posix_spawn_file_actions *ufa, rlim_t lim)
    116  1.34    martin {
    117  1.34    martin 	struct posix_spawn_file_actions *fa;
    118  1.34    martin 	struct netbsd32_posix_spawn_file_actions fa32;
    119  1.34    martin 	struct netbsd32_posix_spawn_file_actions_entry *fae32 = NULL, *f32 = NULL;
    120  1.34    martin 	struct posix_spawn_file_actions_entry *fae;
    121  1.44    martin 	char *pbuf = NULL, **pathp = NULL, *pathp32 = NULL;
    122  1.34    martin 	int error;
    123  1.34    martin 	size_t fal, fal32, slen, i = 0;
    124  1.34    martin 
    125  1.34    martin 	error = copyin(ufa, &fa32, sizeof(fa32));
    126  1.34    martin 	if (error)
    127  1.34    martin 		return error;
    128  1.34    martin 
    129  1.34    martin 	if (fa32.len == 0)
    130  1.34    martin 		return 0;
    131  1.34    martin 
    132  1.34    martin 	fa = kmem_alloc(sizeof(*fa), KM_SLEEP);
    133  1.34    martin 	fa->len = fa->size = fa32.len;
    134  1.34    martin 
    135  1.38    martin 	if (fa->len > lim) {
    136  1.38    martin 		kmem_free(fa, sizeof(*fa));
    137  1.38    martin 		return EINVAL;
    138  1.38    martin 	}
    139  1.38    martin 
    140  1.34    martin 	fal = fa->len * sizeof(*fae);
    141  1.34    martin 	fal32 = fa->len * sizeof(*fae32);
    142  1.34    martin 
    143  1.34    martin 	fa->fae = kmem_alloc(fal, KM_SLEEP);
    144  1.34    martin 	fae32 = kmem_alloc(fal32, KM_SLEEP);
    145  1.34    martin 	error = copyin(NETBSD32PTR64(fa32.fae), fae32, fal32);
    146  1.34    martin 	if (error)
    147  1.34    martin 		goto out;
    148  1.34    martin 
    149  1.34    martin 	pbuf = PNBUF_GET();
    150  1.34    martin 	for (; i < fa->len; i++) {
    151  1.34    martin 		fae = &fa->fae[i];
    152  1.34    martin 		f32 = &fae32[i];
    153  1.43       mrg 		fae->fae_action = (unsigned)f32->fae_action;
    154  1.34    martin 		fae->fae_fildes = f32->fae_fildes;
    155  1.34    martin 		if (fae->fae_action == FAE_DUP2)
    156  1.34    martin 			fae->fae_data.dup2.newfildes =
    157  1.34    martin 			    f32->fae_data.dup2.newfildes;
    158  1.44    martin 		if (!netbsd32_posix_spawn_fae_path(fae, f32, &pathp, &pathp32)
    159  1.44    martin 		    || pathp == NULL || pathp32 == NULL)
    160  1.34    martin 			continue;
    161  1.44    martin 		error = copyinstr(pathp32, pbuf, MAXPATHLEN, &slen);
    162  1.34    martin 		if (error)
    163  1.34    martin 			goto out;
    164  1.44    martin 		*pathp = kmem_alloc(slen, KM_SLEEP);
    165  1.44    martin 		memcpy(*pathp, pbuf, slen);
    166  1.34    martin 		fae->fae_oflag = f32->fae_oflag;
    167  1.34    martin 		fae->fae_mode = f32->fae_mode;
    168  1.34    martin 	}
    169  1.34    martin 	PNBUF_PUT(pbuf);
    170  1.44    martin 	kmem_free(fae32, fal32);
    171  1.34    martin 	*fap = fa;
    172  1.34    martin 	return 0;
    173  1.34    martin 
    174  1.34    martin out:
    175  1.44    martin 	kmem_free(fae32, fal32);
    176  1.34    martin 	if (pbuf)
    177  1.34    martin 		PNBUF_PUT(pbuf);
    178  1.34    martin 	posix_spawn_fa_free(fa, i);
    179  1.34    martin 	return error;
    180  1.34    martin }
    181  1.34    martin 
    182  1.34    martin int
    183  1.34    martin netbsd32_posix_spawn(struct lwp *l,
    184  1.34    martin 	const struct netbsd32_posix_spawn_args *uap, register_t *retval)
    185  1.34    martin {
    186  1.34    martin 	/* {
    187  1.34    martin 	syscallarg(netbsd32_pid_tp) pid;
    188  1.34    martin 	syscallarg(const netbsd32_charp) path;
    189  1.34    martin 	syscallarg(const netbsd32_posix_spawn_file_actionsp) file_actions;
    190  1.34    martin 	syscallarg(const netbsd32_posix_spawnattrp) attrp;
    191  1.34    martin 	syscallarg(netbsd32_charpp) argv;
    192  1.34    martin 	syscallarg(netbsd32_charpp) envp;
    193  1.34    martin 	} */
    194  1.34    martin 
    195  1.34    martin 	int error;
    196  1.34    martin 	struct posix_spawn_file_actions *fa = NULL;
    197  1.34    martin 	struct posix_spawnattr *sa = NULL;
    198  1.34    martin 	pid_t pid;
    199  1.36     rmind 	bool child_ok = false;
    200  1.38    martin 	rlim_t max_fileactions;
    201  1.38    martin 	proc_t *p = l->l_proc;
    202  1.34    martin 
    203  1.42   thorpej 	/* check_posix_spawn() increments nprocs for us. */
    204  1.34    martin 	error = check_posix_spawn(l);
    205  1.34    martin 	if (error) {
    206  1.34    martin 		*retval = error;
    207  1.34    martin 		return 0;
    208  1.34    martin 	}
    209  1.34    martin 
    210  1.34    martin 	/* copy in file_actions struct */
    211  1.34    martin 	if (SCARG_P32(uap, file_actions) != NULL) {
    212  1.39  riastrad 		max_fileactions = 2 * uimin(p->p_rlimit[RLIMIT_NOFILE].rlim_cur,
    213  1.38    martin 		    maxfiles);
    214  1.34    martin 		error = netbsd32_posix_spawn_fa_alloc(&fa,
    215  1.38    martin 		    SCARG_P32(uap, file_actions), max_fileactions);
    216  1.34    martin 		if (error)
    217  1.36     rmind 			goto error_exit;
    218  1.34    martin 	}
    219  1.34    martin 
    220  1.34    martin 	/* copyin posix_spawnattr struct */
    221  1.34    martin 	if (SCARG_P32(uap, attrp) != NULL) {
    222  1.34    martin 		sa = kmem_alloc(sizeof(*sa), KM_SLEEP);
    223  1.34    martin 		error = copyin(SCARG_P32(uap, attrp), sa, sizeof(*sa));
    224  1.34    martin 		if (error)
    225  1.36     rmind 			goto error_exit;
    226  1.34    martin 	}
    227  1.34    martin 
    228  1.34    martin 	/*
    229  1.34    martin 	 * Do the spawn
    230  1.34    martin 	 */
    231  1.36     rmind 	error = do_posix_spawn(l, &pid, &child_ok, SCARG_P32(uap, path), fa,
    232  1.34    martin 	    sa, SCARG_P32(uap, argv), SCARG_P32(uap, envp),
    233  1.34    martin 	    netbsd32_execve_fetch_element);
    234  1.34    martin 	if (error)
    235  1.36     rmind 		goto error_exit;
    236  1.34    martin 
    237  1.34    martin 	if (error == 0 && SCARG_P32(uap, pid) != NULL)
    238  1.34    martin 		error = copyout(&pid, SCARG_P32(uap, pid), sizeof(pid));
    239  1.34    martin 
    240  1.34    martin 	*retval = error;
    241  1.34    martin 	return 0;
    242  1.34    martin 
    243  1.36     rmind  error_exit:
    244  1.36     rmind  	if (!child_ok) {
    245  1.36     rmind 		(void)chgproccnt(kauth_cred_getuid(l->l_cred), -1);
    246  1.36     rmind 		atomic_dec_uint(&nprocs);
    247  1.36     rmind 
    248  1.36     rmind 		if (sa)
    249  1.36     rmind 			kmem_free(sa, sizeof(*sa));
    250  1.36     rmind 		if (fa)
    251  1.36     rmind 			posix_spawn_fa_free(fa, fa->len);
    252  1.36     rmind 	}
    253  1.36     rmind 
    254  1.34    martin 	*retval = error;
    255  1.34    martin 	return 0;
    256  1.34    martin }
    257