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