blob: e73828f32b1c8c60c5f92514d4ad7bf20943c7a0 [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 {
72 if (dup2(fd, new_fd) == -1) _exit(127);
73 }
74 }
75};
76
77struct __posix_spawn_file_actions {
78 __posix_spawn_file_action* head;
79 __posix_spawn_file_action* last;
80
81 void Do() {
82 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
83 action->Do();
84 }
85 }
86};
87
88struct __posix_spawnattr {
89 short flags;
90 pid_t pgroup;
91 sched_param schedparam;
92 int schedpolicy;
Elliott Hughes5905d6f2018-01-30 15:09:51 -080093 SigSetConverter sigmask;
94 SigSetConverter sigdefault;
Elliott Hughes14e3ff92017-10-06 16:58:36 -070095};
96
Elliott Hughes7bfacaa2017-11-28 19:58:00 -080097static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
98 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
99 // shall be set to their default actions in the child process."
100 // POSIX: "Signals set to be caught by the calling process shall be
101 // set to the default action in the child process."
102 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
Elliott Hughes3e235912018-02-01 14:21:51 -0800103 const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800104 for (int s = 1; s < _NSIG; ++s) {
105 bool reset = false;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800106 if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800107 reset = true;
108 } else {
Elliott Hughes3e235912018-02-01 14:21:51 -0800109 struct sigaction64 current;
110 if (sigaction64(s, nullptr, &current) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800111 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
112 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800113 if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800114 }
115
116 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
117 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
118
119 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
120 // to set both.
121 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
122 if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
123 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
124 if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
125 }
126
127 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
128 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
129 }
130
131 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800132 if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800133 }
134}
135
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700136static int posix_spawn(pid_t* pid_ptr,
137 const char* path,
138 const posix_spawn_file_actions_t* actions,
139 const posix_spawnattr_t* attr,
140 char* const argv[],
141 char* const env[],
142 int exec_fn(const char* path, char* const argv[], char* const env[])) {
143 // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
144 // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
145
146 ScopedSignalBlocker ssb;
147
148 short flags = attr ? (*attr)->flags : 0;
149 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
150
151 pid_t pid = use_vfork ? vfork() : fork();
152 if (pid == -1) return errno;
153
154 if (pid == 0) {
155 // Child.
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800156 ApplyAttrs(flags, attr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700157 if (actions) (*actions)->Do();
158 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
159 exec_fn(path, argv, env ? env : environ);
160 _exit(127);
161 }
162
163 // Parent.
164 if (pid_ptr) *pid_ptr = pid;
165 return 0;
166}
167
168int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
169 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
170 return posix_spawn(pid, path, actions, attr, argv, env, execve);
171}
172
173int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
174 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
175 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
176}
177
178int posix_spawnattr_init(posix_spawnattr_t* attr) {
179 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
180 return (*attr == nullptr) ? errno : 0;
181}
182
183int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
184 free(*attr);
185 *attr = nullptr;
186 return 0;
187}
188
189int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
190 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
191 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
192 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
193 return EINVAL;
194 }
195 (*attr)->flags = flags;
196 return 0;
197}
198
199int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
200 *flags = (*attr)->flags;
201 return 0;
202}
203
204int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
205 (*attr)->pgroup = pgroup;
206 return 0;
207}
208
209int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
210 *pgroup = (*attr)->pgroup;
211 return 0;
212}
213
214int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800215 (*attr)->sigmask.sigset = *mask;
216 return 0;
217}
218
219int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
220 (*attr)->sigmask.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700221 return 0;
222}
223
224int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800225 *mask = (*attr)->sigmask.sigset;
226 return 0;
227}
228
229int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
230 *mask = (*attr)->sigmask.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700231 return 0;
232}
233
234int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800235 (*attr)->sigdefault.sigset = *mask;
236 return 0;
237}
238
239int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
240 (*attr)->sigdefault.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700241 return 0;
242}
243
244int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800245 *mask = (*attr)->sigdefault.sigset;
246 return 0;
247}
248
249int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
250 *mask = (*attr)->sigdefault.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700251 return 0;
252}
253
254int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
255 (*attr)->schedparam = *param;
256 return 0;
257}
258
259int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
260 *param = (*attr)->schedparam;
261 return 0;
262}
263
264int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
265 (*attr)->schedpolicy = policy;
266 return 0;
267}
268
269int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
270 *policy = (*attr)->schedpolicy;
271 return 0;
272}
273
274int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
275 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
276 return (*actions == nullptr) ? errno : 0;
277}
278
279int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
280 __posix_spawn_file_action* a = (*actions)->head;
281 while (a) {
282 __posix_spawn_file_action* last = a;
283 a = a->next;
284 free(last->path);
285 free(last);
286 }
287 free(*actions);
288 *actions = nullptr;
289 return 0;
290}
291
292static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
293 Action what,
294 int fd,
295 int new_fd,
296 const char* path,
297 int flags,
298 mode_t mode) {
299 __posix_spawn_file_action* action =
300 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
301 if (action == nullptr) return errno;
302
303 action->next = nullptr;
304 if (path != nullptr) {
305 action->path = strdup(path);
306 if (action->path == nullptr) {
307 free(action);
308 return errno;
309 }
310 } else {
311 action->path = nullptr;
312 }
313 action->what = what;
314 action->fd = fd;
315 action->new_fd = new_fd;
316 action->flags = flags;
317 action->mode = mode;
318
319 if ((*actions)->head == nullptr) {
320 (*actions)->head = (*actions)->last = action;
321 } else {
322 (*actions)->last->next = action;
323 (*actions)->last = action;
324 }
325
326 return 0;
327}
328
329int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
330 int fd, const char* path, int flags, mode_t mode) {
331 if (fd < 0) return EBADF;
332 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
333}
334
335int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
336 if (fd < 0) return EBADF;
337 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
338}
339
340int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
341 if (fd < 0 || new_fd < 0) return EBADF;
342 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
343}