Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_execve.c revision 1.32.34.3
      1  1.32.34.3      yamt /*	$NetBSD: netbsd32_execve.c,v 1.32.34.3 2014/05/22 11:40:17 yamt 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.32.34.3      yamt __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.32.34.3 2014/05/22 11:40:17 yamt Exp $");
     32        1.1       mrg 
     33        1.1       mrg #include <sys/param.h>
     34        1.1       mrg #include <sys/systm.h>
     35  1.32.34.1      yamt #include <sys/atomic.h>
     36        1.1       mrg #include <sys/mount.h>
     37  1.32.34.1      yamt #include <sys/namei.h>
     38        1.1       mrg #include <sys/stat.h>
     39  1.32.34.1      yamt #include <sys/spawn.h>
     40  1.32.34.1      yamt #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.29       dsl 	const char *path = SCARG_P32(uap, path);
     75        1.1       mrg 
     76       1.29       dsl 	return execve1(l, path, SCARG_P32(uap, argp),
     77       1.29       dsl 	    SCARG_P32(uap, envp), netbsd32_execve_fetch_element);
     78        1.1       mrg }
     79  1.32.34.1      yamt 
     80  1.32.34.1      yamt int
     81  1.32.34.1      yamt netbsd32_fexecve(struct lwp *l, const struct netbsd32_fexecve_args *uap,
     82  1.32.34.1      yamt 		 register_t *retval)
     83  1.32.34.1      yamt {
     84  1.32.34.1      yamt 	/* {
     85  1.32.34.1      yamt 		syscallarg(int) fd;
     86  1.32.34.1      yamt 		syscallarg(netbsd32_charpp) argp;
     87  1.32.34.1      yamt 		syscallarg(netbsd32_charpp) envp;
     88  1.32.34.1      yamt 	} */
     89  1.32.34.1      yamt 	struct sys_fexecve_args ua;
     90  1.32.34.1      yamt 
     91  1.32.34.1      yamt 	NETBSD32TO64_UAP(fd);
     92  1.32.34.1      yamt 	NETBSD32TOP_UAP(argp, char * const);
     93  1.32.34.1      yamt 	NETBSD32TOP_UAP(envp, char * const);
     94  1.32.34.1      yamt 
     95  1.32.34.1      yamt 	return sys_fexecve(l, &ua, retval);
     96  1.32.34.1      yamt }
     97  1.32.34.1      yamt 
     98  1.32.34.1      yamt static int
     99  1.32.34.1      yamt netbsd32_posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap,
    100  1.32.34.3      yamt     const struct netbsd32_posix_spawn_file_actions *ufa, rlim_t lim)
    101  1.32.34.1      yamt {
    102  1.32.34.1      yamt 	struct posix_spawn_file_actions *fa;
    103  1.32.34.1      yamt 	struct netbsd32_posix_spawn_file_actions fa32;
    104  1.32.34.1      yamt 	struct netbsd32_posix_spawn_file_actions_entry *fae32 = NULL, *f32 = NULL;
    105  1.32.34.1      yamt 	struct posix_spawn_file_actions_entry *fae;
    106  1.32.34.1      yamt 	char *pbuf = NULL;
    107  1.32.34.1      yamt 	int error;
    108  1.32.34.1      yamt 	size_t fal, fal32, slen, i = 0;
    109  1.32.34.1      yamt 
    110  1.32.34.1      yamt 	error = copyin(ufa, &fa32, sizeof(fa32));
    111  1.32.34.1      yamt 	if (error)
    112  1.32.34.1      yamt 		return error;
    113  1.32.34.1      yamt 
    114  1.32.34.1      yamt 	if (fa32.len == 0)
    115  1.32.34.1      yamt 		return 0;
    116  1.32.34.1      yamt 
    117  1.32.34.1      yamt 	fa = kmem_alloc(sizeof(*fa), KM_SLEEP);
    118  1.32.34.1      yamt 	fa->len = fa->size = fa32.len;
    119  1.32.34.1      yamt 
    120  1.32.34.3      yamt 	if (fa->len > lim) {
    121  1.32.34.3      yamt 		kmem_free(fa, sizeof(*fa));
    122  1.32.34.3      yamt 		return EINVAL;
    123  1.32.34.3      yamt 	}
    124  1.32.34.3      yamt 
    125  1.32.34.1      yamt 	fal = fa->len * sizeof(*fae);
    126  1.32.34.1      yamt 	fal32 = fa->len * sizeof(*fae32);
    127  1.32.34.1      yamt 
    128  1.32.34.1      yamt 	fa->fae = kmem_alloc(fal, KM_SLEEP);
    129  1.32.34.1      yamt 	fae32 = kmem_alloc(fal32, KM_SLEEP);
    130  1.32.34.1      yamt 	error = copyin(NETBSD32PTR64(fa32.fae), fae32, fal32);
    131  1.32.34.1      yamt 	if (error)
    132  1.32.34.1      yamt 		goto out;
    133  1.32.34.1      yamt 
    134  1.32.34.1      yamt 	pbuf = PNBUF_GET();
    135  1.32.34.1      yamt 	for (; i < fa->len; i++) {
    136  1.32.34.1      yamt 		fae = &fa->fae[i];
    137  1.32.34.1      yamt 		f32 = &fae32[i];
    138  1.32.34.1      yamt 		fae->fae_action = f32->fae_action;
    139  1.32.34.1      yamt 		fae->fae_fildes = f32->fae_fildes;
    140  1.32.34.1      yamt 		if (fae->fae_action == FAE_DUP2)
    141  1.32.34.1      yamt 			fae->fae_data.dup2.newfildes =
    142  1.32.34.1      yamt 			    f32->fae_data.dup2.newfildes;
    143  1.32.34.1      yamt 		if (fae->fae_action != FAE_OPEN)
    144  1.32.34.1      yamt 			continue;
    145  1.32.34.1      yamt 		error = copyinstr(NETBSD32PTR64(f32->fae_path), pbuf,
    146  1.32.34.1      yamt 		    MAXPATHLEN, &slen);
    147  1.32.34.1      yamt 		if (error)
    148  1.32.34.1      yamt 			goto out;
    149  1.32.34.2      yamt 		fae->fae_path = kmem_alloc(slen, KM_SLEEP);
    150  1.32.34.1      yamt 		memcpy(fae->fae_path, pbuf, slen);
    151  1.32.34.1      yamt 		fae->fae_oflag = f32->fae_oflag;
    152  1.32.34.1      yamt 		fae->fae_mode = f32->fae_mode;
    153  1.32.34.1      yamt 	}
    154  1.32.34.1      yamt 	PNBUF_PUT(pbuf);
    155  1.32.34.1      yamt 	if (fae32)
    156  1.32.34.1      yamt 		kmem_free(fae32, fal32);
    157  1.32.34.1      yamt 	*fap = fa;
    158  1.32.34.1      yamt 	return 0;
    159  1.32.34.1      yamt 
    160  1.32.34.1      yamt out:
    161  1.32.34.1      yamt 	if (fae32)
    162  1.32.34.1      yamt 		kmem_free(fae32, fal32);
    163  1.32.34.1      yamt 	if (pbuf)
    164  1.32.34.1      yamt 		PNBUF_PUT(pbuf);
    165  1.32.34.1      yamt 	posix_spawn_fa_free(fa, i);
    166  1.32.34.1      yamt 	return error;
    167  1.32.34.1      yamt }
    168  1.32.34.1      yamt 
    169  1.32.34.1      yamt int
    170  1.32.34.1      yamt netbsd32_posix_spawn(struct lwp *l,
    171  1.32.34.1      yamt 	const struct netbsd32_posix_spawn_args *uap, register_t *retval)
    172  1.32.34.1      yamt {
    173  1.32.34.1      yamt 	/* {
    174  1.32.34.1      yamt 	syscallarg(netbsd32_pid_tp) pid;
    175  1.32.34.1      yamt 	syscallarg(const netbsd32_charp) path;
    176  1.32.34.1      yamt 	syscallarg(const netbsd32_posix_spawn_file_actionsp) file_actions;
    177  1.32.34.1      yamt 	syscallarg(const netbsd32_posix_spawnattrp) attrp;
    178  1.32.34.1      yamt 	syscallarg(netbsd32_charpp) argv;
    179  1.32.34.1      yamt 	syscallarg(netbsd32_charpp) envp;
    180  1.32.34.1      yamt 	} */
    181  1.32.34.1      yamt 
    182  1.32.34.1      yamt 	int error;
    183  1.32.34.1      yamt 	struct posix_spawn_file_actions *fa = NULL;
    184  1.32.34.1      yamt 	struct posix_spawnattr *sa = NULL;
    185  1.32.34.1      yamt 	pid_t pid;
    186  1.32.34.1      yamt 	bool child_ok = false;
    187  1.32.34.3      yamt 	rlim_t max_fileactions;
    188  1.32.34.3      yamt 	proc_t *p = l->l_proc;
    189  1.32.34.1      yamt 
    190  1.32.34.1      yamt 	error = check_posix_spawn(l);
    191  1.32.34.1      yamt 	if (error) {
    192  1.32.34.1      yamt 		*retval = error;
    193  1.32.34.1      yamt 		return 0;
    194  1.32.34.1      yamt 	}
    195  1.32.34.1      yamt 
    196  1.32.34.1      yamt 	/* copy in file_actions struct */
    197  1.32.34.1      yamt 	if (SCARG_P32(uap, file_actions) != NULL) {
    198  1.32.34.3      yamt 		max_fileactions = 2 * min(p->p_rlimit[RLIMIT_NOFILE].rlim_cur,
    199  1.32.34.3      yamt 		    maxfiles);
    200  1.32.34.1      yamt 		error = netbsd32_posix_spawn_fa_alloc(&fa,
    201  1.32.34.3      yamt 		    SCARG_P32(uap, file_actions), max_fileactions);
    202  1.32.34.1      yamt 		if (error)
    203  1.32.34.1      yamt 			goto error_exit;
    204  1.32.34.1      yamt 	}
    205  1.32.34.1      yamt 
    206  1.32.34.1      yamt 	/* copyin posix_spawnattr struct */
    207  1.32.34.1      yamt 	if (SCARG_P32(uap, attrp) != NULL) {
    208  1.32.34.1      yamt 		sa = kmem_alloc(sizeof(*sa), KM_SLEEP);
    209  1.32.34.1      yamt 		error = copyin(SCARG_P32(uap, attrp), sa, sizeof(*sa));
    210  1.32.34.1      yamt 		if (error)
    211  1.32.34.1      yamt 			goto error_exit;
    212  1.32.34.1      yamt 	}
    213  1.32.34.1      yamt 
    214  1.32.34.1      yamt 	/*
    215  1.32.34.1      yamt 	 * Do the spawn
    216  1.32.34.1      yamt 	 */
    217  1.32.34.1      yamt 	error = do_posix_spawn(l, &pid, &child_ok, SCARG_P32(uap, path), fa,
    218  1.32.34.1      yamt 	    sa, SCARG_P32(uap, argv), SCARG_P32(uap, envp),
    219  1.32.34.1      yamt 	    netbsd32_execve_fetch_element);
    220  1.32.34.1      yamt 	if (error)
    221  1.32.34.1      yamt 		goto error_exit;
    222  1.32.34.1      yamt 
    223  1.32.34.1      yamt 	if (error == 0 && SCARG_P32(uap, pid) != NULL)
    224  1.32.34.1      yamt 		error = copyout(&pid, SCARG_P32(uap, pid), sizeof(pid));
    225  1.32.34.1      yamt 
    226  1.32.34.1      yamt 	*retval = error;
    227  1.32.34.1      yamt 	return 0;
    228  1.32.34.1      yamt 
    229  1.32.34.1      yamt  error_exit:
    230  1.32.34.1      yamt  	if (!child_ok) {
    231  1.32.34.1      yamt 		(void)chgproccnt(kauth_cred_getuid(l->l_cred), -1);
    232  1.32.34.1      yamt 		atomic_dec_uint(&nprocs);
    233  1.32.34.1      yamt 
    234  1.32.34.1      yamt 		if (sa)
    235  1.32.34.1      yamt 			kmem_free(sa, sizeof(*sa));
    236  1.32.34.1      yamt 		if (fa)
    237  1.32.34.1      yamt 			posix_spawn_fa_free(fa, fa->len);
    238  1.32.34.1      yamt 	}
    239  1.32.34.1      yamt 
    240  1.32.34.1      yamt 	*retval = error;
    241  1.32.34.1      yamt 	return 0;
    242  1.32.34.1      yamt }
    243