blob: 5d76f77ca5a7fed8bd89f565a6db7b81621cb842 [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>
Maciej Żenczykowskic9c0ebd2022-01-21 11:19:55 -080033#include <linux/close_range.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070034#include <signal.h>
35#include <stdlib.h>
Dan Albertc972ea72017-10-13 14:32:43 -070036#include <string.h>
Maciej Żenczykowski262b8732022-01-20 14:56:20 -080037#include <sys/resource.h>
Maciej Żenczykowskic9c0ebd2022-01-21 11:19:55 -080038#include <sys/syscall.h>
Elliott Hughes14e3ff92017-10-06 16:58:36 -070039#include <unistd.h>
40
Josh Gaof6e5b582018-06-01 15:30:54 -070041#include <android/fdsan.h>
42
Elliott Hughes14e3ff92017-10-06 16:58:36 -070043#include "private/ScopedSignalBlocker.h"
Elliott Hughes5905d6f2018-01-30 15:09:51 -080044#include "private/SigSetConverter.h"
Elliott Hughes14e3ff92017-10-06 16:58:36 -070045
Maciej Żenczykowski262b8732022-01-20 14:56:20 -080046static 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
53static int cloexec_except_stdioe() {
Maciej Żenczykowskic9c0ebd2022-01-21 11:19:55 -080054 // requires 5.11+ or ACK 5.10-T kernel, otherwise returns ENOSYS or EINVAL
Maciej Żenczykowskib65e1052022-01-21 11:19:55 -080055 if (!close_range(3, ~0U, CLOSE_RANGE_CLOEXEC)) return 0;
Maciej Żenczykowskic9c0ebd2022-01-21 11:19:55 -080056
Maciej Żenczykowski262b8732022-01-20 14:56:20 -080057 // 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
Elliott Hughes14e3ff92017-10-06 16:58:36 -070068enum Action {
69 kOpen,
70 kClose,
Elliott Hughes462ca8b2023-04-04 13:33:28 -070071 kDup2,
72 kChdir,
73 kFchdir,
Elliott Hughes14e3ff92017-10-06 16:58:36 -070074};
75
76struct __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
86 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);
Elliott Hughes462ca8b2023-04-04 13:33:28 -070098 } else if (what == kChdir) {
99 if (chdir(path) == -1) _exit(127);
100 } else if (what == kFchdir) {
101 if (fchdir(fd) == -1) _exit(127);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700102 } else {
Elliott Hughes62d49fd2022-02-16 14:39:07 -0800103 // 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 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700114 }
115 }
116};
117
118struct __posix_spawn_file_actions {
119 __posix_spawn_file_action* head;
120 __posix_spawn_file_action* last;
121
122 void Do() {
123 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
124 action->Do();
125 }
126 }
127};
128
129struct __posix_spawnattr {
130 short flags;
131 pid_t pgroup;
132 sched_param schedparam;
133 int schedpolicy;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800134 SigSetConverter sigmask;
135 SigSetConverter sigdefault;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700136};
137
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800138static 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);
Elliott Hughes3e235912018-02-01 14:21:51 -0800144 const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800145 for (int s = 1; s < _NSIG; ++s) {
146 bool reset = false;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800147 if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800148 reset = true;
149 } else {
Elliott Hughes3e235912018-02-01 14:21:51 -0800150 struct sigaction64 current;
151 if (sigaction64(s, nullptr, &current) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800152 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
153 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800154 if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800155 }
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) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800173 if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800174 }
Maciej Żenczykowski262b8732022-01-20 14:56:20 -0800175
176 if ((flags & POSIX_SPAWN_CLOEXEC_DEFAULT) != 0) {
177 if (cloexec_except_stdioe()) _exit(127);
178 }
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800179}
180
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700181static 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.
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800201 ApplyAttrs(flags, attr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700202 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
213int 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
218int 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
223int 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
228int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
229 free(*attr);
230 *attr = nullptr;
231 return 0;
232}
233
234int 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 |
Maciej Żenczykowski262b8732022-01-20 14:56:20 -0800237 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID | POSIX_SPAWN_CLOEXEC_DEFAULT)) != 0) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700238 return EINVAL;
239 }
240 (*attr)->flags = flags;
241 return 0;
242}
243
244int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
245 *flags = (*attr)->flags;
246 return 0;
247}
248
249int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
250 (*attr)->pgroup = pgroup;
251 return 0;
252}
253
254int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
255 *pgroup = (*attr)->pgroup;
256 return 0;
257}
258
259int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800260 (*attr)->sigmask.sigset = *mask;
261 return 0;
262}
263
264int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
265 (*attr)->sigmask.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700266 return 0;
267}
268
269int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800270 *mask = (*attr)->sigmask.sigset;
271 return 0;
272}
273
274int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
275 *mask = (*attr)->sigmask.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700276 return 0;
277}
278
279int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800280 (*attr)->sigdefault.sigset = *mask;
281 return 0;
282}
283
284int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
285 (*attr)->sigdefault.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700286 return 0;
287}
288
289int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800290 *mask = (*attr)->sigdefault.sigset;
291 return 0;
292}
293
294int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
295 *mask = (*attr)->sigdefault.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700296 return 0;
297}
298
299int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
300 (*attr)->schedparam = *param;
301 return 0;
302}
303
304int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
305 *param = (*attr)->schedparam;
306 return 0;
307}
308
309int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
310 (*attr)->schedpolicy = policy;
311 return 0;
312}
313
314int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
315 *policy = (*attr)->schedpolicy;
316 return 0;
317}
318
319int 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
324int 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
337static 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;
Elliott Hughes462ca8b2023-04-04 13:33:28 -0700349 if (what == kOpen || what == kChdir) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700350 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
374int 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
380int 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
385int 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}
Elliott Hughes462ca8b2023-04-04 13:33:28 -0700389
390int 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
394int 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}