blob: 2067a1a920013524a2b7e0d6454b1d8169705711 [file] [log] [blame]
Elliott Hughes14e3ff92017-10-06 16:58:36 -07001/*
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
Dan Albertc972ea72017-10-13 14:32:43 -070031#include <errno.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070032#include <fcntl.h>
33#include <signal.h>
34#include <stdlib.h>
Dan Albertc972ea72017-10-13 14:32:43 -070035#include <string.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070036#include <unistd.h>
37
Josh Gaof6e5b582018-06-01 15:30:54 -070038#include <android/fdsan.h>
39
Elliott Hughes14e3ff92017-10-06 16:58:36 -070040#include "private/ScopedSignalBlocker.h"
Elliott Hughes5905d6f2018-01-30 15:09:51 -080041#include "private/SigSetConverter.h"
Elliott Hughes14e3ff92017-10-06 16:58:36 -070042
43enum Action {
44 kOpen,
45 kClose,
46 kDup2
47};
48
49struct __posix_spawn_file_action {
50 __posix_spawn_file_action* next;
51
52 Action what;
53 int fd;
54 int new_fd;
55 char* path;
56 int flags;
57 mode_t mode;
58
59 void Do() {
60 if (what == kOpen) {
61 fd = open(path, flags, mode);
62 if (fd == -1) _exit(127);
63 // If it didn't land where we wanted it, move it.
64 if (fd != new_fd) {
65 if (dup2(fd, new_fd) == -1) _exit(127);
66 close(fd);
67 }
68 } else if (what == kClose) {
69 // Failure to close is ignored.
70 close(fd);
71 } else {
Elliott Hughes62d49fd2022-02-16 14:39:07 -080072 // It's a dup2.
73 if (fd == new_fd) {
74 // dup2(2) is a no-op if fd == new_fd, but POSIX suggests that we should
75 // manually remove the O_CLOEXEC flag in that case (because otherwise
76 // what use is the dup?).
77 // See https://www.austingroupbugs.net/view.php?id=411 for details.
78 int flags = fcntl(fd, F_GETFD, 0);
79 if (flags == -1 || fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) == -1) _exit(127);
80 } else {
81 if (dup2(fd, new_fd) == -1) _exit(127);
82 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -070083 }
84 }
85};
86
87struct __posix_spawn_file_actions {
88 __posix_spawn_file_action* head;
89 __posix_spawn_file_action* last;
90
91 void Do() {
92 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
93 action->Do();
94 }
95 }
96};
97
98struct __posix_spawnattr {
99 short flags;
100 pid_t pgroup;
101 sched_param schedparam;
102 int schedpolicy;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800103 SigSetConverter sigmask;
104 SigSetConverter sigdefault;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700105};
106
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800107static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
108 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
109 // shall be set to their default actions in the child process."
110 // POSIX: "Signals set to be caught by the calling process shall be
111 // set to the default action in the child process."
112 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
Elliott Hughes3e235912018-02-01 14:21:51 -0800113 const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800114 for (int s = 1; s < _NSIG; ++s) {
115 bool reset = false;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800116 if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800117 reset = true;
118 } else {
Elliott Hughes3e235912018-02-01 14:21:51 -0800119 struct sigaction64 current;
120 if (sigaction64(s, nullptr, &current) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800121 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
122 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800123 if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800124 }
125
126 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
127 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
128
129 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
130 // to set both.
131 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
132 if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
133 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
134 if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
135 }
136
137 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
138 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
139 }
140
141 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800142 if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800143 }
144}
145
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700146static int posix_spawn(pid_t* pid_ptr,
147 const char* path,
148 const posix_spawn_file_actions_t* actions,
149 const posix_spawnattr_t* attr,
150 char* const argv[],
151 char* const env[],
152 int exec_fn(const char* path, char* const argv[], char* const env[])) {
153 // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
154 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
155
156 ScopedSignalBlocker ssb;
157
158 short flags = attr ? (*attr)->flags : 0;
159 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
160
161 pid_t pid = use_vfork ? vfork() : fork();
162 if (pid == -1) return errno;
163
164 if (pid == 0) {
165 // Child.
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800166 ApplyAttrs(flags, attr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700167 if (actions) (*actions)->Do();
168 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
169 exec_fn(path, argv, env ? env : environ);
170 _exit(127);
171 }
172
173 // Parent.
174 if (pid_ptr) *pid_ptr = pid;
175 return 0;
176}
177
178int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
179 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
180 return posix_spawn(pid, path, actions, attr, argv, env, execve);
181}
182
183int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
184 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
185 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
186}
187
188int posix_spawnattr_init(posix_spawnattr_t* attr) {
189 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
190 return (*attr == nullptr) ? errno : 0;
191}
192
193int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
194 free(*attr);
195 *attr = nullptr;
196 return 0;
197}
198
199int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
200 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
201 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
202 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
203 return EINVAL;
204 }
205 (*attr)->flags = flags;
206 return 0;
207}
208
209int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
210 *flags = (*attr)->flags;
211 return 0;
212}
213
214int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
215 (*attr)->pgroup = pgroup;
216 return 0;
217}
218
219int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
220 *pgroup = (*attr)->pgroup;
221 return 0;
222}
223
224int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800225 (*attr)->sigmask.sigset = *mask;
226 return 0;
227}
228
229int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
230 (*attr)->sigmask.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700231 return 0;
232}
233
234int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800235 *mask = (*attr)->sigmask.sigset;
236 return 0;
237}
238
239int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
240 *mask = (*attr)->sigmask.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700241 return 0;
242}
243
244int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800245 (*attr)->sigdefault.sigset = *mask;
246 return 0;
247}
248
249int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
250 (*attr)->sigdefault.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700251 return 0;
252}
253
254int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800255 *mask = (*attr)->sigdefault.sigset;
256 return 0;
257}
258
259int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
260 *mask = (*attr)->sigdefault.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700261 return 0;
262}
263
264int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
265 (*attr)->schedparam = *param;
266 return 0;
267}
268
269int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
270 *param = (*attr)->schedparam;
271 return 0;
272}
273
274int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
275 (*attr)->schedpolicy = policy;
276 return 0;
277}
278
279int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
280 *policy = (*attr)->schedpolicy;
281 return 0;
282}
283
284int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
285 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
286 return (*actions == nullptr) ? errno : 0;
287}
288
289int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
290 __posix_spawn_file_action* a = (*actions)->head;
291 while (a) {
292 __posix_spawn_file_action* last = a;
293 a = a->next;
294 free(last->path);
295 free(last);
296 }
297 free(*actions);
298 *actions = nullptr;
299 return 0;
300}
301
302static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
303 Action what,
304 int fd,
305 int new_fd,
306 const char* path,
307 int flags,
308 mode_t mode) {
309 __posix_spawn_file_action* action =
310 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
311 if (action == nullptr) return errno;
312
313 action->next = nullptr;
314 if (path != nullptr) {
315 action->path = strdup(path);
316 if (action->path == nullptr) {
317 free(action);
318 return errno;
319 }
320 } else {
321 action->path = nullptr;
322 }
323 action->what = what;
324 action->fd = fd;
325 action->new_fd = new_fd;
326 action->flags = flags;
327 action->mode = mode;
328
329 if ((*actions)->head == nullptr) {
330 (*actions)->head = (*actions)->last = action;
331 } else {
332 (*actions)->last->next = action;
333 (*actions)->last = action;
334 }
335
336 return 0;
337}
338
339int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
340 int fd, const char* path, int flags, mode_t mode) {
341 if (fd < 0) return EBADF;
342 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
343}
344
345int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
346 if (fd < 0) return EBADF;
347 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
348}
349
350int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
351 if (fd < 0 || new_fd < 0) return EBADF;
352 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
353}