netbsd32_execve.c revision 1.39
1/* $NetBSD: netbsd32_execve.c,v 1.39 2018/09/03 16:29:29 riastradh 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30 31__KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.39 2018/09/03 16:29:29 riastradh Exp $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/atomic.h> 36#include <sys/mount.h> 37#include <sys/namei.h> 38#include <sys/stat.h> 39#include <sys/spawn.h> 40#include <sys/uidinfo.h> 41#include <sys/vnode.h> 42#include <sys/file.h> 43#include <sys/filedesc.h> 44#include <sys/syscallargs.h> 45#include <sys/proc.h> 46#include <sys/exec.h> 47 48#include <compat/netbsd32/netbsd32.h> 49#include <compat/netbsd32/netbsd32_syscall.h> 50#include <compat/netbsd32/netbsd32_syscallargs.h> 51 52static int 53netbsd32_execve_fetch_element(char * const *array, size_t index, char **value) 54{ 55 int error; 56 netbsd32_charp const *a32 = (void const *)array; 57 netbsd32_charp e; 58 59 error = copyin(a32 + index, &e, sizeof(e)); 60 if (error) 61 return error; 62 *value = (char *)NETBSD32PTR64(e); 63 return 0; 64} 65 66int 67netbsd32_execve(struct lwp *l, const struct netbsd32_execve_args *uap, register_t *retval) 68{ 69 /* { 70 syscallarg(const netbsd32_charp) path; 71 syscallarg(netbsd32_charpp) argp; 72 syscallarg(netbsd32_charpp) envp; 73 } */ 74 const char *path = SCARG_P32(uap, path); 75 76 return execve1(l, path, SCARG_P32(uap, argp), 77 SCARG_P32(uap, envp), netbsd32_execve_fetch_element); 78} 79 80int 81netbsd32_fexecve(struct lwp *l, const struct netbsd32_fexecve_args *uap, 82 register_t *retval) 83{ 84 /* { 85 syscallarg(int) fd; 86 syscallarg(netbsd32_charpp) argp; 87 syscallarg(netbsd32_charpp) envp; 88 } */ 89 struct sys_fexecve_args ua; 90 91 NETBSD32TO64_UAP(fd); 92 NETBSD32TOP_UAP(argp, char * const); 93 NETBSD32TOP_UAP(envp, char * const); 94 95 return sys_fexecve(l, &ua, retval); 96} 97 98static int 99netbsd32_posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap, 100 const struct netbsd32_posix_spawn_file_actions *ufa, rlim_t lim) 101{ 102 struct posix_spawn_file_actions *fa; 103 struct netbsd32_posix_spawn_file_actions fa32; 104 struct netbsd32_posix_spawn_file_actions_entry *fae32 = NULL, *f32 = NULL; 105 struct posix_spawn_file_actions_entry *fae; 106 char *pbuf = NULL; 107 int error; 108 size_t fal, fal32, slen, i = 0; 109 110 error = copyin(ufa, &fa32, sizeof(fa32)); 111 if (error) 112 return error; 113 114 if (fa32.len == 0) 115 return 0; 116 117 fa = kmem_alloc(sizeof(*fa), KM_SLEEP); 118 fa->len = fa->size = fa32.len; 119 120 if (fa->len > lim) { 121 kmem_free(fa, sizeof(*fa)); 122 return EINVAL; 123 } 124 125 fal = fa->len * sizeof(*fae); 126 fal32 = fa->len * sizeof(*fae32); 127 128 fa->fae = kmem_alloc(fal, KM_SLEEP); 129 fae32 = kmem_alloc(fal32, KM_SLEEP); 130 error = copyin(NETBSD32PTR64(fa32.fae), fae32, fal32); 131 if (error) 132 goto out; 133 134 pbuf = PNBUF_GET(); 135 for (; i < fa->len; i++) { 136 fae = &fa->fae[i]; 137 f32 = &fae32[i]; 138 fae->fae_action = f32->fae_action; 139 fae->fae_fildes = f32->fae_fildes; 140 if (fae->fae_action == FAE_DUP2) 141 fae->fae_data.dup2.newfildes = 142 f32->fae_data.dup2.newfildes; 143 if (fae->fae_action != FAE_OPEN) 144 continue; 145 error = copyinstr(NETBSD32PTR64(f32->fae_path), pbuf, 146 MAXPATHLEN, &slen); 147 if (error) 148 goto out; 149 fae->fae_path = kmem_alloc(slen, KM_SLEEP); 150 memcpy(fae->fae_path, pbuf, slen); 151 fae->fae_oflag = f32->fae_oflag; 152 fae->fae_mode = f32->fae_mode; 153 } 154 PNBUF_PUT(pbuf); 155 if (fae32) 156 kmem_free(fae32, fal32); 157 *fap = fa; 158 return 0; 159 160out: 161 if (fae32) 162 kmem_free(fae32, fal32); 163 if (pbuf) 164 PNBUF_PUT(pbuf); 165 posix_spawn_fa_free(fa, i); 166 return error; 167} 168 169int 170netbsd32_posix_spawn(struct lwp *l, 171 const struct netbsd32_posix_spawn_args *uap, register_t *retval) 172{ 173 /* { 174 syscallarg(netbsd32_pid_tp) pid; 175 syscallarg(const netbsd32_charp) path; 176 syscallarg(const netbsd32_posix_spawn_file_actionsp) file_actions; 177 syscallarg(const netbsd32_posix_spawnattrp) attrp; 178 syscallarg(netbsd32_charpp) argv; 179 syscallarg(netbsd32_charpp) envp; 180 } */ 181 182 int error; 183 struct posix_spawn_file_actions *fa = NULL; 184 struct posix_spawnattr *sa = NULL; 185 pid_t pid; 186 bool child_ok = false; 187 rlim_t max_fileactions; 188 proc_t *p = l->l_proc; 189 190 error = check_posix_spawn(l); 191 if (error) { 192 *retval = error; 193 return 0; 194 } 195 196 /* copy in file_actions struct */ 197 if (SCARG_P32(uap, file_actions) != NULL) { 198 max_fileactions = 2 * uimin(p->p_rlimit[RLIMIT_NOFILE].rlim_cur, 199 maxfiles); 200 error = netbsd32_posix_spawn_fa_alloc(&fa, 201 SCARG_P32(uap, file_actions), max_fileactions); 202 if (error) 203 goto error_exit; 204 } 205 206 /* copyin posix_spawnattr struct */ 207 if (SCARG_P32(uap, attrp) != NULL) { 208 sa = kmem_alloc(sizeof(*sa), KM_SLEEP); 209 error = copyin(SCARG_P32(uap, attrp), sa, sizeof(*sa)); 210 if (error) 211 goto error_exit; 212 } 213 214 /* 215 * Do the spawn 216 */ 217 error = do_posix_spawn(l, &pid, &child_ok, SCARG_P32(uap, path), fa, 218 sa, SCARG_P32(uap, argv), SCARG_P32(uap, envp), 219 netbsd32_execve_fetch_element); 220 if (error) 221 goto error_exit; 222 223 if (error == 0 && SCARG_P32(uap, pid) != NULL) 224 error = copyout(&pid, SCARG_P32(uap, pid), sizeof(pid)); 225 226 *retval = error; 227 return 0; 228 229 error_exit: 230 if (!child_ok) { 231 (void)chgproccnt(kauth_cred_getuid(l->l_cred), -1); 232 atomic_dec_uint(&nprocs); 233 234 if (sa) 235 kmem_free(sa, sizeof(*sa)); 236 if (fa) 237 posix_spawn_fa_free(fa, fa->len); 238 } 239 240 *retval = error; 241 return 0; 242} 243