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