blob: f1b8eb06dbefbbdd1f0954fbc2a60de66aaf7449 [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"
44
45enum Action {
46 kOpen,
47 kClose,
Elliott Hughes462ca8b2023-04-04 13:33:28 -070048 kDup2,
49 kChdir,
50 kFchdir,
Elliott Hughes14e3ff92017-10-06 16:58:36 -070051};
52
53struct __posix_spawn_file_action {
54 __posix_spawn_file_action* next;
55
56 Action what;
57 int fd;
58 int new_fd;
59 char* path;
60 int flags;
61 mode_t mode;
62
63 void Do() {
64 if (what == kOpen) {
65 fd = open(path, flags, mode);
66 if (fd == -1) _exit(127);
67 // If it didn't land where we wanted it, move it.
68 if (fd != new_fd) {
69 if (dup2(fd, new_fd) == -1) _exit(127);
70 close(fd);
71 }
72 } else if (what == kClose) {
73 // Failure to close is ignored.
74 close(fd);
Elliott Hughes462ca8b2023-04-04 13:33:28 -070075 } else if (what == kChdir) {
76 if (chdir(path) == -1) _exit(127);
77 } else if (what == kFchdir) {
78 if (fchdir(fd) == -1) _exit(127);
Elliott Hughes14e3ff92017-10-06 16:58:36 -070079 } else {
Elliott Hughes62d49fd2022-02-16 14:39:07 -080080 // It's a dup2.
81 if (fd == new_fd) {
82 // dup2(2) is a no-op if fd == new_fd, but POSIX suggests that we should
83 // manually remove the O_CLOEXEC flag in that case (because otherwise
84 // what use is the dup?).
85 // See https://www.austingroupbugs.net/view.php?id=411 for details.
86 int flags = fcntl(fd, F_GETFD, 0);
87 if (flags == -1 || fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) == -1) _exit(127);
88 } else {
89 if (dup2(fd, new_fd) == -1) _exit(127);
90 }
Elliott Hughes14e3ff92017-10-06 16:58:36 -070091 }
92 }
93};
94
95struct __posix_spawn_file_actions {
96 __posix_spawn_file_action* head;
97 __posix_spawn_file_action* last;
98
99 void Do() {
100 for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
101 action->Do();
102 }
103 }
104};
105
106struct __posix_spawnattr {
107 short flags;
108 pid_t pgroup;
109 sched_param schedparam;
110 int schedpolicy;
Elliott Hughes215baed2023-07-17 17:15:01 -0700111 union {
112 sigset_t sigset;
113 sigset64_t sigset64;
114 } sigmask, sigdefault;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700115};
116
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800117static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
118 // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
119 // shall be set to their default actions in the child process."
120 // POSIX: "Signals set to be caught by the calling process shall be
121 // set to the default action in the child process."
122 bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
Elliott Hughes3e235912018-02-01 14:21:51 -0800123 const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800124 for (int s = 1; s < _NSIG; ++s) {
125 bool reset = false;
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800126 if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800127 reset = true;
128 } else {
Elliott Hughes3e235912018-02-01 14:21:51 -0800129 struct sigaction64 current;
130 if (sigaction64(s, nullptr, &current) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800131 reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
132 }
Elliott Hughes3e235912018-02-01 14:21:51 -0800133 if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800134 }
135
136 if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
137 if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
138
139 // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
140 // to set both.
141 if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
142 if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
143 } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
144 if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
145 }
146
147 if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
148 if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
149 }
150
151 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800152 if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800153 }
Maciej Żenczykowski262b8732022-01-20 14:56:20 -0800154
155 if ((flags & POSIX_SPAWN_CLOEXEC_DEFAULT) != 0) {
Maciej Żenczykowskibf8fc352025-06-06 11:15:05 -0700156 // mark all open fds except stdin/out/err as close-on-exec
157 if (close_range(3, ~0U, CLOSE_RANGE_CLOEXEC)) _exit(127);
Maciej Żenczykowski262b8732022-01-20 14:56:20 -0800158 }
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800159}
160
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700161static int posix_spawn(pid_t* pid_ptr,
162 const char* path,
163 const posix_spawn_file_actions_t* actions,
164 const posix_spawnattr_t* attr,
165 char* const argv[],
166 char* const env[],
167 int exec_fn(const char* path, char* const argv[], char* const env[])) {
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000168 // See https://man7.org/linux/man-pages/man3/posix_spawn.3.html
Elliott Hughes5f3b8e02024-08-14 14:50:59 +0000169 // and https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/posix_spawn.html
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700170
171 ScopedSignalBlocker ssb;
172
173 short flags = attr ? (*attr)->flags : 0;
174 bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
175
176 pid_t pid = use_vfork ? vfork() : fork();
177 if (pid == -1) return errno;
178
179 if (pid == 0) {
180 // Child.
Elliott Hughes7bfacaa2017-11-28 19:58:00 -0800181 ApplyAttrs(flags, attr);
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700182 if (actions) (*actions)->Do();
183 if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
184 exec_fn(path, argv, env ? env : environ);
185 _exit(127);
186 }
187
188 // Parent.
189 if (pid_ptr) *pid_ptr = pid;
190 return 0;
191}
192
193int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
194 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
195 return posix_spawn(pid, path, actions, attr, argv, env, execve);
196}
197
198int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
199 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
200 return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
201}
202
203int posix_spawnattr_init(posix_spawnattr_t* attr) {
204 *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
205 return (*attr == nullptr) ? errno : 0;
206}
207
208int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
209 free(*attr);
210 *attr = nullptr;
211 return 0;
212}
213
214int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
215 if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
216 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
Maciej Żenczykowski262b8732022-01-20 14:56:20 -0800217 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID | POSIX_SPAWN_CLOEXEC_DEFAULT)) != 0) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700218 return EINVAL;
219 }
220 (*attr)->flags = flags;
221 return 0;
222}
223
224int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
225 *flags = (*attr)->flags;
226 return 0;
227}
228
229int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
230 (*attr)->pgroup = pgroup;
231 return 0;
232}
233
234int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
235 *pgroup = (*attr)->pgroup;
236 return 0;
237}
238
239int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800240 (*attr)->sigmask.sigset = *mask;
241 return 0;
242}
243
244int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
245 (*attr)->sigmask.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700246 return 0;
247}
248
249int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800250 *mask = (*attr)->sigmask.sigset;
251 return 0;
252}
253
254int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
255 *mask = (*attr)->sigmask.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700256 return 0;
257}
258
259int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800260 (*attr)->sigdefault.sigset = *mask;
261 return 0;
262}
263
264int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
265 (*attr)->sigdefault.sigset64 = *mask;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700266 return 0;
267}
268
269int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800270 *mask = (*attr)->sigdefault.sigset;
271 return 0;
272}
273
274int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
275 *mask = (*attr)->sigdefault.sigset64;
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700276 return 0;
277}
278
279int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
280 (*attr)->schedparam = *param;
281 return 0;
282}
283
284int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
285 *param = (*attr)->schedparam;
286 return 0;
287}
288
289int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
290 (*attr)->schedpolicy = policy;
291 return 0;
292}
293
294int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
295 *policy = (*attr)->schedpolicy;
296 return 0;
297}
298
299int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
300 *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
301 return (*actions == nullptr) ? errno : 0;
302}
303
304int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
305 __posix_spawn_file_action* a = (*actions)->head;
306 while (a) {
307 __posix_spawn_file_action* last = a;
308 a = a->next;
309 free(last->path);
310 free(last);
311 }
312 free(*actions);
313 *actions = nullptr;
314 return 0;
315}
316
317static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
318 Action what,
319 int fd,
320 int new_fd,
321 const char* path,
322 int flags,
323 mode_t mode) {
324 __posix_spawn_file_action* action =
325 reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
326 if (action == nullptr) return errno;
327
328 action->next = nullptr;
Elliott Hughes462ca8b2023-04-04 13:33:28 -0700329 if (what == kOpen || what == kChdir) {
Elliott Hughes14e3ff92017-10-06 16:58:36 -0700330 action->path = strdup(path);
331 if (action->path == nullptr) {
332 free(action);
333 return errno;
334 }
335 } else {
336 action->path = nullptr;
337 }
338 action->what = what;
339 action->fd = fd;
340 action->new_fd = new_fd;
341 action->flags = flags;
342 action->mode = mode;
343
344 if ((*actions)->head == nullptr) {
345 (*actions)->head = (*actions)->last = action;
346 } else {
347 (*actions)->last->next = action;
348 (*actions)->last = action;
349 }
350
351 return 0;
352}
353
354int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
355 int fd, const char* path, int flags, mode_t mode) {
356 if (fd < 0) return EBADF;
357 return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
358}
359
360int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
361 if (fd < 0) return EBADF;
362 return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
363}
364
365int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
366 if (fd < 0 || new_fd < 0) return EBADF;
367 return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
368}
Elliott Hughes462ca8b2023-04-04 13:33:28 -0700369
370int posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t* actions, const char* path) {
371 return posix_spawn_add_file_action(actions, kChdir, -1, -1, path, 0, 0);
372}
373
374int posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t* actions, int fd) {
375 if (fd < 0) return EBADF;
376 return posix_spawn_add_file_action(actions, kFchdir, fd, -1, nullptr, 0, 0);
377}