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