1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS 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; LOSS
22 * 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 OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <spawn.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <linux/close_range.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/resource.h>
38 #include <sys/syscall.h>
39 #include <unistd.h>
40
41 #include <android/fdsan.h>
42
43 #include "private/ScopedSignalBlocker.h"
44 #include "private/SigSetConverter.h"
45
set_cloexec(int i)46 static int set_cloexec(int i) {
47 int v = fcntl(i, F_GETFD);
48 if (v == -1) return -1; // almost certainly: errno == EBADF
49 return fcntl(i, F_SETFD, v | FD_CLOEXEC);
50 }
51
52 // mark all open fds except stdin/out/err as close-on-exec
cloexec_except_stdioe()53 static int cloexec_except_stdioe() {
54 // requires 5.11+ or ACK 5.10-T kernel, otherwise returns ENOSYS or EINVAL
55 if (!close_range(3, ~0U, CLOSE_RANGE_CLOEXEC)) return 0;
56
57 // unfortunately getrlimit can lie:
58 // - both soft and hard limits can be lowered to 0, with fds still open, so it can underestimate
59 // - in practice it usually is some really large value (like 32K or more)
60 // even though only a handful of small fds are actually open (ie. < 500),
61 // this results in poor performance when trying to act on all possibly open fds
62 struct rlimit m;
63 int max = getrlimit(RLIMIT_NOFILE, &m) ? 1000000 : m.rlim_max;
64 for (int i = 3; i < max; ++i) set_cloexec(i);
65 return 0;
66 }
67
68 enum Action {
69 kOpen,
70 kClose,
71 kDup2,
72 kChdir,
73 kFchdir,
74 };
75
76 struct __posix_spawn_file_action {
77 __posix_spawn_file_action* next;
78
79 Action what;
80 int fd;
81 int new_fd;
82 char* path;
83 int flags;
84 mode_t mode;
85
Do__posix_spawn_file_action86 void Do() {
87 if (what == kOpen) {
88 fd = open(path, flags, mode);
89 if (fd == -1) _exit(127);
90 // If it didn't land where we wanted it, move it.
91 if (fd != new_fd) {
92 if (dup2(fd, new_fd) == -1) _exit(127);
93 close(fd);
94 }
95 } else if (what == kClose) {
96 // Failure to close is ignored.
97 close(fd);
98 } else if (what == kChdir) {
99 if (chdir(path) == -1) _exit(127);
100 } else if (what == kFchdir) {
101 if (fchdir(fd) == -1) _exit(127);
102 } else {
103 // It's a dup2.
104 if (fd == new_fd) {
105 // dup2(2) is a no-op if fd == new_fd, but POSIX suggests that we should
106 // manually remove the O_CLOEXEC flag in that case (because otherwise
107 // what use is the dup?).
108 // See https://www.austingroupbugs.net/view.php?id=411 for details.
109 int flags = fcntl(fd, F_GETFD, 0);
110 if (flags == -1 || fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) == -1) _exit(127);
111 } else {
112 if (dup2(fd, new_fd) == -1) _exit(127);
113 }
114 }
115 }
116 };
117
118 struct __posix_spawn_file_actions {
119 __posix_spawn_file_action* head;
120 __posix_spawn_file_action* last;
121
Do__posix_spawn_file_actions122 void Do() {
123 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
124 action->Do();
125 }
126 }
127 };
128
129 struct __posix_spawnattr {
130 short flags;
131 pid_t pgroup;
132 sched_param schedparam;
133 int schedpolicy;
134 SigSetConverter sigmask;
135 SigSetConverter sigdefault;
136 };
137
ApplyAttrs(short flags,const posix_spawnattr_t * attr)138 static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
139 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
140 // shall be set to their default actions in the child process."
141 // POSIX: "Signals set to be caught by the calling process shall be
142 // set to the default action in the child process."
143 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
144 const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
145 for (int s = 1; s < _NSIG; ++s) {
146 bool reset = false;
147 if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
148 reset = true;
149 } else {
150 struct sigaction64 current;
151 if (sigaction64(s, nullptr, ¤t) == -1) _exit(127);
152 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
153 }
154 if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
155 }
156
157 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
158 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
159
160 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
161 // to set both.
162 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
163 if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
164 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
165 if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
166 }
167
168 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
169 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
170 }
171
172 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
173 if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
174 }
175
176 if ((flags & POSIX_SPAWN_CLOEXEC_DEFAULT) != 0) {
177 if (cloexec_except_stdioe()) _exit(127);
178 }
179 }
180
posix_spawn(pid_t * pid_ptr,const char * path,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[],int exec_fn (const char * path,char * const argv[],char * const env[]))181 static int posix_spawn(pid_t* pid_ptr,
182 const char* path,
183 const posix_spawn_file_actions_t* actions,
184 const posix_spawnattr_t* attr,
185 char* const argv[],
186 char* const env[],
187 int exec_fn(const char* path, char* const argv[], char* const env[])) {
188 // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
189 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
190
191 ScopedSignalBlocker ssb;
192
193 short flags = attr ? (*attr)->flags : 0;
194 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
195
196 pid_t pid = use_vfork ? vfork() : fork();
197 if (pid == -1) return errno;
198
199 if (pid == 0) {
200 // Child.
201 ApplyAttrs(flags, attr);
202 if (actions) (*actions)->Do();
203 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
204 exec_fn(path, argv, env ? env : environ);
205 _exit(127);
206 }
207
208 // Parent.
209 if (pid_ptr) *pid_ptr = pid;
210 return 0;
211 }
212
posix_spawn(pid_t * pid,const char * path,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[])213 int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
214 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
215 return posix_spawn(pid, path, actions, attr, argv, env, execve);
216 }
217
posix_spawnp(pid_t * pid,const char * file,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[])218 int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
219 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
220 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
221 }
222
posix_spawnattr_init(posix_spawnattr_t * attr)223 int posix_spawnattr_init(posix_spawnattr_t* attr) {
224 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
225 return (*attr == nullptr) ? errno : 0;
226 }
227
posix_spawnattr_destroy(posix_spawnattr_t * attr)228 int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
229 free(*attr);
230 *attr = nullptr;
231 return 0;
232 }
233
posix_spawnattr_setflags(posix_spawnattr_t * attr,short flags)234 int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
235 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
236 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
237 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID | POSIX_SPAWN_CLOEXEC_DEFAULT)) != 0) {
238 return EINVAL;
239 }
240 (*attr)->flags = flags;
241 return 0;
242 }
243
posix_spawnattr_getflags(const posix_spawnattr_t * attr,short * flags)244 int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
245 *flags = (*attr)->flags;
246 return 0;
247 }
248
posix_spawnattr_setpgroup(posix_spawnattr_t * attr,pid_t pgroup)249 int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
250 (*attr)->pgroup = pgroup;
251 return 0;
252 }
253
posix_spawnattr_getpgroup(const posix_spawnattr_t * attr,pid_t * pgroup)254 int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
255 *pgroup = (*attr)->pgroup;
256 return 0;
257 }
258
posix_spawnattr_setsigmask(posix_spawnattr_t * attr,const sigset_t * mask)259 int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
260 (*attr)->sigmask.sigset = *mask;
261 return 0;
262 }
263
posix_spawnattr_setsigmask64(posix_spawnattr_t * attr,const sigset64_t * mask)264 int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
265 (*attr)->sigmask.sigset64 = *mask;
266 return 0;
267 }
268
posix_spawnattr_getsigmask(const posix_spawnattr_t * attr,sigset_t * mask)269 int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
270 *mask = (*attr)->sigmask.sigset;
271 return 0;
272 }
273
posix_spawnattr_getsigmask64(const posix_spawnattr_t * attr,sigset64_t * mask)274 int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
275 *mask = (*attr)->sigmask.sigset64;
276 return 0;
277 }
278
posix_spawnattr_setsigdefault(posix_spawnattr_t * attr,const sigset_t * mask)279 int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
280 (*attr)->sigdefault.sigset = *mask;
281 return 0;
282 }
283
posix_spawnattr_setsigdefault64(posix_spawnattr_t * attr,const sigset64_t * mask)284 int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
285 (*attr)->sigdefault.sigset64 = *mask;
286 return 0;
287 }
288
posix_spawnattr_getsigdefault(const posix_spawnattr_t * attr,sigset_t * mask)289 int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
290 *mask = (*attr)->sigdefault.sigset;
291 return 0;
292 }
293
posix_spawnattr_getsigdefault64(const posix_spawnattr_t * attr,sigset64_t * mask)294 int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
295 *mask = (*attr)->sigdefault.sigset64;
296 return 0;
297 }
298
posix_spawnattr_setschedparam(posix_spawnattr_t * attr,const struct sched_param * param)299 int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
300 (*attr)->schedparam = *param;
301 return 0;
302 }
303
posix_spawnattr_getschedparam(const posix_spawnattr_t * attr,struct sched_param * param)304 int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
305 *param = (*attr)->schedparam;
306 return 0;
307 }
308
posix_spawnattr_setschedpolicy(posix_spawnattr_t * attr,int policy)309 int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
310 (*attr)->schedpolicy = policy;
311 return 0;
312 }
313
posix_spawnattr_getschedpolicy(const posix_spawnattr_t * attr,int * policy)314 int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
315 *policy = (*attr)->schedpolicy;
316 return 0;
317 }
318
posix_spawn_file_actions_init(posix_spawn_file_actions_t * actions)319 int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
320 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
321 return (*actions == nullptr) ? errno : 0;
322 }
323
posix_spawn_file_actions_destroy(posix_spawn_file_actions_t * actions)324 int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
325 __posix_spawn_file_action* a = (*actions)->head;
326 while (a) {
327 __posix_spawn_file_action* last = a;
328 a = a->next;
329 free(last->path);
330 free(last);
331 }
332 free(*actions);
333 *actions = nullptr;
334 return 0;
335 }
336
posix_spawn_add_file_action(posix_spawn_file_actions_t * actions,Action what,int fd,int new_fd,const char * path,int flags,mode_t mode)337 static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
338 Action what,
339 int fd,
340 int new_fd,
341 const char* path,
342 int flags,
343 mode_t mode) {
344 __posix_spawn_file_action* action =
345 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
346 if (action == nullptr) return errno;
347
348 action->next = nullptr;
349 if (what == kOpen || what == kChdir) {
350 action->path = strdup(path);
351 if (action->path == nullptr) {
352 free(action);
353 return errno;
354 }
355 } else {
356 action->path = nullptr;
357 }
358 action->what = what;
359 action->fd = fd;
360 action->new_fd = new_fd;
361 action->flags = flags;
362 action->mode = mode;
363
364 if ((*actions)->head == nullptr) {
365 (*actions)->head = (*actions)->last = action;
366 } else {
367 (*actions)->last->next = action;
368 (*actions)->last = action;
369 }
370
371 return 0;
372 }
373
posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * actions,int fd,const char * path,int flags,mode_t mode)374 int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
375 int fd, const char* path, int flags, mode_t mode) {
376 if (fd < 0) return EBADF;
377 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
378 }
379
posix_spawn_file_actions_addclose(posix_spawn_file_actions_t * actions,int fd)380 int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
381 if (fd < 0) return EBADF;
382 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
383 }
384
posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t * actions,int fd,int new_fd)385 int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
386 if (fd < 0 || new_fd < 0) return EBADF;
387 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
388 }
389
posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t * actions,const char * path)390 int posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t* actions, const char* path) {
391 return posix_spawn_add_file_action(actions, kChdir, -1, -1, path, 0, 0);
392 }
393
posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t * actions,int fd)394 int posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t* actions, int fd) {
395 if (fd < 0) return EBADF;
396 return posix_spawn_add_file_action(actions, kFchdir, fd, -1, nullptr, 0, 0);
397 }
398