Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1 | /* Authors: Gregory P. Smith & Jeffrey Yasskin */ |
| 2 | #include "Python.h" |
Kyle Evans | 7992579 | 2020-10-13 15:04:44 -0500 | [diff] [blame] | 3 | #include "pycore_fileutils.h" |
Victor Stinner | 5572ba7 | 2011-05-26 14:10:08 +0200 | [diff] [blame] | 4 | #if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE) |
| 5 | # define _GNU_SOURCE |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 6 | #endif |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 7 | #include <unistd.h> |
Gregory P. Smith | 51ee270 | 2010-12-13 07:59:39 +0000 | [diff] [blame] | 8 | #include <fcntl.h> |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 9 | #ifdef HAVE_SYS_TYPES_H |
| 10 | #include <sys/types.h> |
| 11 | #endif |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 12 | #if defined(HAVE_SYS_STAT_H) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 13 | #include <sys/stat.h> |
| 14 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 15 | #ifdef HAVE_SYS_SYSCALL_H |
| 16 | #include <sys/syscall.h> |
| 17 | #endif |
Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 18 | #if defined(HAVE_SYS_RESOURCE_H) |
| 19 | #include <sys/resource.h> |
| 20 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 21 | #ifdef HAVE_DIRENT_H |
| 22 | #include <dirent.h> |
| 23 | #endif |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 24 | #ifdef HAVE_GRP_H |
| 25 | #include <grp.h> |
| 26 | #endif /* HAVE_GRP_H */ |
| 27 | |
| 28 | #include "posixmodule.h" |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 29 | |
Gregory P. Smith | 3015fb8 | 2018-11-12 22:01:22 -0800 | [diff] [blame] | 30 | #ifdef _Py_MEMORY_SANITIZER |
Gregory P. Smith | 1584a00 | 2018-11-12 12:07:14 -0800 | [diff] [blame] | 31 | # include <sanitizer/msan_interface.h> |
| 32 | #endif |
| 33 | |
Xavier de Gaye | c716f18 | 2016-06-15 11:35:29 +0200 | [diff] [blame] | 34 | #if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64) |
Gregory P. Smith | efeb9da | 2014-04-14 13:31:21 -0700 | [diff] [blame] | 35 | # include <sys/linux-syscalls.h> |
| 36 | # define SYS_getdents64 __NR_getdents64 |
| 37 | #endif |
| 38 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 39 | #if defined(__linux__) && defined(HAVE_VFORK) && defined(HAVE_SIGNAL_H) && \ |
| 40 | defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Gregory P. Smith | be3c3a0 | 2020-10-24 12:07:35 -0700 | [diff] [blame] | 41 | /* If this is ever expanded to non-Linux platforms, verify what calls are |
| 42 | * allowed after vfork(). Ex: setsid() may be disallowed on macOS? */ |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 43 | # include <signal.h> |
| 44 | # define VFORK_USABLE 1 |
| 45 | #endif |
| 46 | |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 47 | #if defined(__sun) && defined(__SVR4) |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 48 | /* readdir64 is used to work around Solaris 9 bug 6395699. */ |
| 49 | # define readdir readdir64 |
| 50 | # define dirent dirent64 |
| 51 | # if !defined(HAVE_DIRFD) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 52 | /* Some versions of Solaris lack dirfd(). */ |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 53 | # define dirfd(dirp) ((dirp)->dd_fd) |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 54 | # define HAVE_DIRFD |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 55 | # endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 56 | #endif |
| 57 | |
David CARLIER | 13b865f | 2020-11-19 07:24:15 +0000 | [diff] [blame] | 58 | #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__DragonFly__) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 59 | # define FD_DIR "/dev/fd" |
| 60 | #else |
| 61 | # define FD_DIR "/proc/self/fd" |
| 62 | #endif |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 63 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 64 | #ifdef NGROUPS_MAX |
| 65 | #define MAX_GROUPS NGROUPS_MAX |
| 66 | #else |
| 67 | #define MAX_GROUPS 64 |
| 68 | #endif |
| 69 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 70 | #define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 71 | |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 72 | typedef struct { |
| 73 | PyObject* disable; |
| 74 | PyObject* enable; |
| 75 | PyObject* isenabled; |
| 76 | } _posixsubprocessstate; |
| 77 | |
| 78 | static struct PyModuleDef _posixsubprocessmodule; |
| 79 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 80 | static inline _posixsubprocessstate* |
| 81 | get_posixsubprocess_state(PyObject *module) |
| 82 | { |
| 83 | void *state = PyModule_GetState(module); |
| 84 | assert(state != NULL); |
| 85 | return (_posixsubprocessstate *)state; |
| 86 | } |
| 87 | |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 88 | /* If gc was disabled, call gc.enable(). Ignore errors. */ |
| 89 | static void |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 90 | _enable_gc(int need_to_reenable_gc, PyObject *gc_module, _posixsubprocessstate *state) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 91 | { |
| 92 | PyObject *result; |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 93 | PyObject *exctype, *val, *tb; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 94 | |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 95 | if (need_to_reenable_gc) { |
| 96 | PyErr_Fetch(&exctype, &val, &tb); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 97 | result = PyObject_CallMethodNoArgs( |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 98 | gc_module, state->enable); |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 99 | if (result == NULL) { |
| 100 | /* We might have created a child process at this point, we |
| 101 | * we have no good way to handle a failure to reenable GC |
| 102 | * and return information about the child process. */ |
| 103 | PyErr_Print(); |
| 104 | } |
| 105 | Py_XDECREF(result); |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 106 | if (exctype != NULL) { |
| 107 | PyErr_Restore(exctype, val, tb); |
| 108 | } |
Martin Panter | afdd513 | 2015-11-30 02:21:41 +0000 | [diff] [blame] | 109 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 113 | /* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 114 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 115 | _pos_int_from_ascii(const char *name) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 116 | { |
| 117 | int num = 0; |
| 118 | while (*name >= '0' && *name <= '9') { |
| 119 | num = num * 10 + (*name - '0'); |
| 120 | ++name; |
| 121 | } |
| 122 | if (*name) |
| 123 | return -1; /* Non digit found, not a number. */ |
| 124 | return num; |
| 125 | } |
| 126 | |
| 127 | |
David CARLIER | 13b865f | 2020-11-19 07:24:15 +0000 | [diff] [blame] | 128 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 129 | /* When /dev/fd isn't mounted it is often a static directory populated |
David CARLIER | 13b865f | 2020-11-19 07:24:15 +0000 | [diff] [blame] | 130 | * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD, OpenBSD and DragonFlyBSD. |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 131 | * NetBSD and OpenBSD have a /proc fs available (though not necessarily |
| 132 | * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs |
| 133 | * that properly supports /dev/fd. |
| 134 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 135 | static int |
Ross Lagerwall | 7f4fdb2 | 2012-03-07 20:06:33 +0200 | [diff] [blame] | 136 | _is_fdescfs_mounted_on_dev_fd(void) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 137 | { |
| 138 | struct stat dev_stat; |
| 139 | struct stat dev_fd_stat; |
| 140 | if (stat("/dev", &dev_stat) != 0) |
| 141 | return 0; |
| 142 | if (stat(FD_DIR, &dev_fd_stat) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 143 | return 0; |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 144 | if (dev_stat.st_dev == dev_fd_stat.st_dev) |
| 145 | return 0; /* / == /dev == /dev/fd means it is static. #fail */ |
| 146 | return 1; |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 151 | /* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 152 | static int |
| 153 | _sanity_check_python_fd_sequence(PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 154 | { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 155 | Py_ssize_t seq_idx; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 156 | long prev_fd = -1; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 157 | for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) { |
| 158 | PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx); |
| 159 | long iter_fd; |
| 160 | if (!PyLong_Check(py_fd)) { |
| 161 | return 1; |
| 162 | } |
| 163 | iter_fd = PyLong_AsLong(py_fd); |
Gregory P. Smith | d0a5b1c | 2015-11-15 21:15:26 -0800 | [diff] [blame] | 164 | if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 165 | /* Negative, overflow, unsorted, too big for a fd. */ |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 166 | return 1; |
| 167 | } |
Gregory P. Smith | d0a5b1c | 2015-11-15 21:15:26 -0800 | [diff] [blame] | 168 | prev_fd = iter_fd; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Is fd found in the sorted Python Sequence? */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 175 | static int |
| 176 | _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 177 | { |
| 178 | /* Binary search. */ |
| 179 | Py_ssize_t search_min = 0; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 180 | Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 181 | if (search_max < 0) |
| 182 | return 0; |
| 183 | do { |
| 184 | long middle = (search_min + search_max) / 2; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 185 | long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle)); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 186 | if (fd == middle_fd) |
| 187 | return 1; |
| 188 | if (fd > middle_fd) |
| 189 | search_min = middle + 1; |
| 190 | else |
| 191 | search_max = middle - 1; |
| 192 | } while (search_min <= search_max); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 196 | static int |
| 197 | make_inheritable(PyObject *py_fds_to_keep, int errpipe_write) |
| 198 | { |
| 199 | Py_ssize_t i, len; |
| 200 | |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 201 | len = PyTuple_GET_SIZE(py_fds_to_keep); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 202 | for (i = 0; i < len; ++i) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 203 | PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 204 | long fd = PyLong_AsLong(fdobj); |
| 205 | assert(!PyErr_Occurred()); |
| 206 | assert(0 <= fd && fd <= INT_MAX); |
| 207 | if (fd == errpipe_write) { |
| 208 | /* errpipe_write is part of py_fds_to_keep. It must be closed at |
| 209 | exec(), but kept open in the child process until exec() is |
| 210 | called. */ |
| 211 | continue; |
| 212 | } |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 213 | if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 219 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 220 | /* Get the maximum file descriptor that could be opened by this process. |
| 221 | * This function is async signal safe for use between fork() and exec(). |
| 222 | */ |
| 223 | static long |
| 224 | safe_get_max_fd(void) |
| 225 | { |
| 226 | long local_max_fd; |
| 227 | #if defined(__NetBSD__) |
| 228 | local_max_fd = fcntl(0, F_MAXFD); |
| 229 | if (local_max_fd >= 0) |
| 230 | return local_max_fd; |
| 231 | #endif |
Gregory P. Smith | f968177 | 2015-04-25 23:43:34 -0700 | [diff] [blame] | 232 | #if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__) |
| 233 | struct rlimit rl; |
| 234 | /* Not on the POSIX async signal safe functions list but likely |
| 235 | * safe. TODO - Someone should audit OpenBSD to make sure. */ |
| 236 | if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) |
| 237 | return (long) rl.rlim_max; |
| 238 | #endif |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 239 | #ifdef _SC_OPEN_MAX |
| 240 | local_max_fd = sysconf(_SC_OPEN_MAX); |
| 241 | if (local_max_fd == -1) |
| 242 | #endif |
| 243 | local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */ |
| 244 | return local_max_fd; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /* Close all file descriptors in the range from start_fd and higher |
| 249 | * except for those in py_fds_to_keep. If the range defined by |
| 250 | * [start_fd, safe_get_max_fd()) is large this will take a long |
| 251 | * time as it calls close() on EVERY possible fd. |
| 252 | * |
| 253 | * It isn't possible to know for sure what the max fd to go up to |
| 254 | * is for processes with the capability of raising their maximum. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 255 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 256 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 257 | _close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 258 | { |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 259 | long end_fd = safe_get_max_fd(); |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 260 | Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 261 | Py_ssize_t keep_seq_idx; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 262 | /* As py_fds_to_keep is sorted we can loop through the list closing |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 263 | * fds in between any in the keep list falling within our range. */ |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 264 | for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) { |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 265 | PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 266 | int keep_fd = PyLong_AsLong(py_keep_fd); |
| 267 | if (keep_fd < start_fd) |
| 268 | continue; |
Kyle Evans | c230fde | 2020-10-11 13:54:11 -0500 | [diff] [blame] | 269 | _Py_closerange(start_fd, keep_fd - 1); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 270 | start_fd = keep_fd + 1; |
| 271 | } |
| 272 | if (start_fd <= end_fd) { |
Kyle Evans | c230fde | 2020-10-11 13:54:11 -0500 | [diff] [blame] | 273 | _Py_closerange(start_fd, end_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) |
| 279 | /* It doesn't matter if d_name has room for NAME_MAX chars; we're using this |
| 280 | * only to read a directory of short file descriptor number names. The kernel |
| 281 | * will return an error if we didn't give it enough space. Highly Unlikely. |
| 282 | * This structure is very old and stable: It will not change unless the kernel |
| 283 | * chooses to break compatibility with all existing binaries. Highly Unlikely. |
| 284 | */ |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 285 | struct linux_dirent64 { |
Gregory P. Smith | 58f07a9 | 2012-06-05 13:26:39 -0700 | [diff] [blame] | 286 | unsigned long long d_ino; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 287 | long long d_off; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 288 | unsigned short d_reclen; /* Length of this linux_dirent */ |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 289 | unsigned char d_type; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 290 | char d_name[256]; /* Filename (null-terminated) */ |
| 291 | }; |
| 292 | |
Gregory P. Smith | a26987a | 2014-06-01 13:46:36 -0700 | [diff] [blame] | 293 | /* Close all open file descriptors in the range from start_fd and higher |
| 294 | * Do not close any in the sorted py_fds_to_keep list. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 295 | * |
| 296 | * This version is async signal safe as it does not make any unsafe C library |
| 297 | * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced |
| 298 | * to resort to making a kernel system call directly but this is the ONLY api |
| 299 | * available that does no harm. opendir/readdir/closedir perform memory |
| 300 | * allocation and locking so while they usually work they are not guaranteed |
| 301 | * to (especially if you have replaced your malloc implementation). A version |
| 302 | * of this function that uses those can be found in the _maybe_unsafe variant. |
| 303 | * |
| 304 | * This is Linux specific because that is all I am ready to test it on. It |
| 305 | * should be easy to add OS specific dirent or dirent64 structures and modify |
| 306 | * it with some cpp #define magic to work on other OSes as well if you want. |
| 307 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 308 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 309 | _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 310 | { |
| 311 | int fd_dir_fd; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 312 | |
Victor Stinner | 160e819 | 2015-03-30 02:18:31 +0200 | [diff] [blame] | 313 | fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 314 | if (fd_dir_fd == -1) { |
| 315 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 316 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 317 | return; |
| 318 | } else { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 319 | char buffer[sizeof(struct linux_dirent64)]; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 320 | int bytes; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 321 | while ((bytes = syscall(SYS_getdents64, fd_dir_fd, |
| 322 | (struct linux_dirent64 *)buffer, |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 323 | sizeof(buffer))) > 0) { |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 324 | struct linux_dirent64 *entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 325 | int offset; |
Gregory P. Smith | 3015fb8 | 2018-11-12 22:01:22 -0800 | [diff] [blame] | 326 | #ifdef _Py_MEMORY_SANITIZER |
Gregory P. Smith | 1584a00 | 2018-11-12 12:07:14 -0800 | [diff] [blame] | 327 | __msan_unpoison(buffer, bytes); |
| 328 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 329 | for (offset = 0; offset < bytes; offset += entry->d_reclen) { |
| 330 | int fd; |
Gregory P. Smith | 255bf5b | 2013-03-03 10:45:05 -0800 | [diff] [blame] | 331 | entry = (struct linux_dirent64 *)(buffer + offset); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 332 | if ((fd = _pos_int_from_ascii(entry->d_name)) < 0) |
| 333 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 334 | if (fd != fd_dir_fd && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 335 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 336 | close(fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 340 | close(fd_dir_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 344 | #define _close_open_fds _close_open_fds_safe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 345 | |
| 346 | #else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 347 | |
| 348 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 349 | /* Close all open file descriptors from start_fd and higher. |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 350 | * Do not close any in the sorted py_fds_to_keep tuple. |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 351 | * |
| 352 | * This function violates the strict use of async signal safe functions. :( |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 353 | * It calls opendir(), readdir() and closedir(). Of these, the one most |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 354 | * likely to ever cause a problem is opendir() as it performs an internal |
| 355 | * malloc(). Practically this should not be a problem. The Java VM makes the |
| 356 | * same calls between fork and exec in its own UNIXProcess_md.c implementation. |
| 357 | * |
| 358 | * readdir_r() is not used because it provides no benefit. It is typically |
| 359 | * implemented as readdir() followed by memcpy(). See also: |
| 360 | * http://womble.decadent.org.uk/readdir_r-advisory.html |
| 361 | */ |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 362 | static void |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 363 | _close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep) |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 364 | { |
| 365 | DIR *proc_fd_dir; |
| 366 | #ifndef HAVE_DIRFD |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 367 | while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 368 | ++start_fd; |
| 369 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 370 | /* Close our lowest fd before we call opendir so that it is likely to |
| 371 | * reuse that fd otherwise we might close opendir's file descriptor in |
| 372 | * our loop. This trick assumes that fd's are allocated on a lowest |
| 373 | * available basis. */ |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 374 | close(start_fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 375 | ++start_fd; |
| 376 | #endif |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 377 | |
David CARLIER | 13b865f | 2020-11-19 07:24:15 +0000 | [diff] [blame] | 378 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
Gregory P. Smith | 4842efc | 2012-01-21 21:01:24 -0800 | [diff] [blame] | 379 | if (!_is_fdescfs_mounted_on_dev_fd()) |
| 380 | proc_fd_dir = NULL; |
| 381 | else |
| 382 | #endif |
| 383 | proc_fd_dir = opendir(FD_DIR); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 384 | if (!proc_fd_dir) { |
| 385 | /* No way to get a list of open fds. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 386 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 387 | } else { |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 388 | struct dirent *dir_entry; |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 389 | #ifdef HAVE_DIRFD |
Gregory P. Smith | e9b7cab | 2012-01-21 15:19:11 -0800 | [diff] [blame] | 390 | int fd_used_by_opendir = dirfd(proc_fd_dir); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 391 | #else |
| 392 | int fd_used_by_opendir = start_fd - 1; |
| 393 | #endif |
| 394 | errno = 0; |
Gregory P. Smith | e3f7848 | 2012-01-21 15:16:17 -0800 | [diff] [blame] | 395 | while ((dir_entry = readdir(proc_fd_dir))) { |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 396 | int fd; |
| 397 | if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0) |
| 398 | continue; /* Not a number. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 399 | if (fd != fd_used_by_opendir && fd >= start_fd && |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 400 | !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) { |
Victor Stinner | e7c7492 | 2015-04-02 16:24:46 +0200 | [diff] [blame] | 401 | close(fd); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 402 | } |
| 403 | errno = 0; |
| 404 | } |
| 405 | if (errno) { |
| 406 | /* readdir error, revert behavior. Highly Unlikely. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 407 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 408 | } |
| 409 | closedir(proc_fd_dir); |
| 410 | } |
| 411 | } |
| 412 | |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 413 | #define _close_open_fds _close_open_fds_maybe_unsafe |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 414 | |
| 415 | #endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */ |
| 416 | |
| 417 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 418 | #ifdef VFORK_USABLE |
| 419 | /* Reset dispositions for all signals to SIG_DFL except for ignored |
| 420 | * signals. This way we ensure that no signal handlers can run |
| 421 | * after we unblock signals in a child created by vfork(). |
| 422 | */ |
| 423 | static void |
| 424 | reset_signal_handlers(const sigset_t *child_sigmask) |
| 425 | { |
| 426 | struct sigaction sa_dfl = {.sa_handler = SIG_DFL}; |
| 427 | for (int sig = 1; sig < _NSIG; sig++) { |
| 428 | /* Dispositions for SIGKILL and SIGSTOP can't be changed. */ |
| 429 | if (sig == SIGKILL || sig == SIGSTOP) { |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | /* There is no need to reset the disposition of signals that will |
| 434 | * remain blocked across execve() since the kernel will do it. */ |
| 435 | if (sigismember(child_sigmask, sig) == 1) { |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | struct sigaction sa; |
| 440 | /* C libraries usually return EINVAL for signals used |
| 441 | * internally (e.g. for thread cancellation), so simply |
| 442 | * skip errors here. */ |
| 443 | if (sigaction(sig, NULL, &sa) == -1) { |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | /* void *h works as these fields are both pointer types already. */ |
| 448 | void *h = (sa.sa_flags & SA_SIGINFO ? (void *)sa.sa_sigaction : |
| 449 | (void *)sa.sa_handler); |
| 450 | if (h == SIG_IGN || h == SIG_DFL) { |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | /* This call can't reasonably fail, but if it does, terminating |
| 455 | * the child seems to be too harsh, so ignore errors. */ |
| 456 | (void) sigaction(sig, &sa_dfl, NULL); |
| 457 | } |
| 458 | } |
| 459 | #endif /* VFORK_USABLE */ |
| 460 | |
| 461 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 462 | /* |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 463 | * This function is code executed in the child process immediately after |
| 464 | * (v)fork to set things up and call exec(). |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 465 | * |
| 466 | * All of the code in this function must only use async-signal-safe functions, |
| 467 | * listed at `man 7 signal` or |
| 468 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 469 | * |
| 470 | * This restriction is documented at |
| 471 | * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html. |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 472 | * |
| 473 | * If this function is called after vfork(), even more care must be taken. |
| 474 | * The lack of preparations that C libraries normally take on fork(), |
| 475 | * as well as sharing the address space with the parent, might make even |
| 476 | * async-signal-safe functions vfork-unsafe. In particular, on Linux, |
| 477 | * set*id() and setgroups() library functions must not be called, since |
| 478 | * they have to interact with the library-level thread list and send |
| 479 | * library-internal signals to implement per-process credentials semantics |
| 480 | * required by POSIX but not supported natively on Linux. Another reason to |
| 481 | * avoid this family of functions is that sharing an address space between |
| 482 | * processes running with different privileges is inherently insecure. |
| 483 | * See bpo-35823 for further discussion and references. |
| 484 | * |
| 485 | * In some C libraries, setrlimit() has the same thread list/signalling |
| 486 | * behavior since resource limits were per-thread attributes before |
| 487 | * Linux 2.6.10. Musl, as of 1.2.1, is known to have this issue |
| 488 | * (https://www.openwall.com/lists/musl/2020/10/15/6). |
| 489 | * |
| 490 | * If vfork-unsafe functionality is desired after vfork(), consider using |
| 491 | * syscall() to obtain it. |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 492 | */ |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 493 | _Py_NO_INLINE static void |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 494 | child_exec(char *const exec_array[], |
| 495 | char *const argv[], |
| 496 | char *const envp[], |
| 497 | const char *cwd, |
| 498 | int p2cread, int p2cwrite, |
| 499 | int c2pread, int c2pwrite, |
| 500 | int errread, int errwrite, |
| 501 | int errpipe_read, int errpipe_write, |
| 502 | int close_fds, int restore_signals, |
| 503 | int call_setsid, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 504 | int call_setgid, gid_t gid, |
| 505 | int call_setgroups, size_t groups_size, const gid_t *groups, |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 506 | int call_setuid, uid_t uid, int child_umask, |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 507 | const void *child_sigmask, |
Benjamin Peterson | 91eef98 | 2012-01-22 20:04:46 -0500 | [diff] [blame] | 508 | PyObject *py_fds_to_keep, |
| 509 | PyObject *preexec_fn, |
| 510 | PyObject *preexec_fn_args_tuple) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 511 | { |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 512 | int i, saved_errno, reached_preexec = 0; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 513 | PyObject *result; |
Gregory P. Smith | 14affb8 | 2010-12-22 05:22:17 +0000 | [diff] [blame] | 514 | const char* err_msg = ""; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 515 | /* Buffer large enough to hold a hex integer. We can't malloc. */ |
| 516 | char hex_errno[sizeof(saved_errno)*2+1]; |
| 517 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 518 | if (make_inheritable(py_fds_to_keep, errpipe_write) < 0) |
| 519 | goto error; |
| 520 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 521 | /* Close parent's pipe ends. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 522 | if (p2cwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 523 | POSIX_CALL(close(p2cwrite)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 524 | if (c2pread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 525 | POSIX_CALL(close(c2pread)); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 526 | if (errread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 527 | POSIX_CALL(close(errread)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 528 | POSIX_CALL(close(errpipe_read)); |
| 529 | |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 530 | /* When duping fds, if there arises a situation where one of the fds is |
| 531 | either 0, 1 or 2, it is possible that it is overwritten (#12607). */ |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 532 | if (c2pwrite == 0) { |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 533 | POSIX_CALL(c2pwrite = dup(c2pwrite)); |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 534 | /* issue32270 */ |
| 535 | if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) { |
| 536 | goto error; |
| 537 | } |
| 538 | } |
| 539 | while (errwrite == 0 || errwrite == 1) { |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 540 | POSIX_CALL(errwrite = dup(errwrite)); |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 541 | /* issue32270 */ |
| 542 | if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) { |
| 543 | goto error; |
| 544 | } |
| 545 | } |
Ross Lagerwall | d98646e | 2011-07-27 07:16:31 +0200 | [diff] [blame] | 546 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 547 | /* Dup fds for child. |
| 548 | dup2() removes the CLOEXEC flag but we must do it ourselves if dup2() |
| 549 | would be a no-op (issue #10806). */ |
| 550 | if (p2cread == 0) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 551 | if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 552 | goto error; |
| 553 | } |
| 554 | else if (p2cread != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 555 | POSIX_CALL(dup2(p2cread, 0)); /* stdin */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 556 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 557 | if (c2pwrite == 1) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 558 | if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 559 | goto error; |
| 560 | } |
| 561 | else if (c2pwrite != -1) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 562 | POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 563 | |
Antoine Pitrou | c9c83ba | 2011-01-03 18:23:55 +0000 | [diff] [blame] | 564 | if (errwrite == 2) { |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 565 | if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 566 | goto error; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 567 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 568 | else if (errwrite != -1) |
| 569 | POSIX_CALL(dup2(errwrite, 2)); /* stderr */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 570 | |
Gregory P. Smith | ce34410 | 2018-09-10 17:46:22 -0700 | [diff] [blame] | 571 | /* We no longer manually close p2cread, c2pwrite, and errwrite here as |
| 572 | * _close_open_fds takes care when it is not already non-inheritable. */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 573 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 574 | if (cwd) |
| 575 | POSIX_CALL(chdir(cwd)); |
| 576 | |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 577 | if (child_umask >= 0) |
| 578 | umask(child_umask); /* umask() always succeeds. */ |
| 579 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 580 | if (restore_signals) |
| 581 | _Py_RestoreSignals(); |
| 582 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 583 | #ifdef VFORK_USABLE |
| 584 | if (child_sigmask) { |
| 585 | reset_signal_handlers(child_sigmask); |
Alexey Izbyshev | 473db47 | 2020-10-24 20:47:38 +0300 | [diff] [blame] | 586 | if ((errno = pthread_sigmask(SIG_SETMASK, child_sigmask, NULL))) { |
| 587 | goto error; |
| 588 | } |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 589 | } |
| 590 | #endif |
| 591 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 592 | #ifdef HAVE_SETSID |
| 593 | if (call_setsid) |
| 594 | POSIX_CALL(setsid()); |
| 595 | #endif |
| 596 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 597 | #ifdef HAVE_SETGROUPS |
| 598 | if (call_setgroups) |
| 599 | POSIX_CALL(setgroups(groups_size, groups)); |
| 600 | #endif /* HAVE_SETGROUPS */ |
| 601 | |
| 602 | #ifdef HAVE_SETREGID |
| 603 | if (call_setgid) |
| 604 | POSIX_CALL(setregid(gid, gid)); |
| 605 | #endif /* HAVE_SETREGID */ |
| 606 | |
| 607 | #ifdef HAVE_SETREUID |
| 608 | if (call_setuid) |
| 609 | POSIX_CALL(setreuid(uid, uid)); |
| 610 | #endif /* HAVE_SETREUID */ |
| 611 | |
| 612 | |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 613 | reached_preexec = 1; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 614 | if (preexec_fn != Py_None && preexec_fn_args_tuple) { |
| 615 | /* This is where the user has asked us to deadlock their program. */ |
| 616 | result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL); |
| 617 | if (result == NULL) { |
| 618 | /* Stringifying the exception or traceback would involve |
| 619 | * memory allocation and thus potential for deadlock. |
| 620 | * We've already faced potential deadlock by calling back |
| 621 | * into Python in the first place, so it probably doesn't |
| 622 | * matter but we avoid it to minimize the possibility. */ |
| 623 | err_msg = "Exception occurred in preexec_fn."; |
| 624 | errno = 0; /* We don't want to report an OSError. */ |
| 625 | goto error; |
| 626 | } |
| 627 | /* Py_DECREF(result); - We're about to exec so why bother? */ |
| 628 | } |
| 629 | |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 630 | /* close FDs after executing preexec_fn, which might open FDs */ |
| 631 | if (close_fds) { |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 632 | /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */ |
Gregory P. Smith | d4dcb70 | 2014-06-01 13:18:28 -0700 | [diff] [blame] | 633 | _close_open_fds(3, py_fds_to_keep); |
Charles-François Natali | 249cdc3 | 2013-08-25 18:24:45 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 636 | /* This loop matches the Lib/os.py _execvpe()'s PATH search when */ |
| 637 | /* given the executable_list generated by Lib/subprocess.py. */ |
| 638 | saved_errno = 0; |
| 639 | for (i = 0; exec_array[i] != NULL; ++i) { |
| 640 | const char *executable = exec_array[i]; |
| 641 | if (envp) { |
| 642 | execve(executable, argv, envp); |
| 643 | } else { |
| 644 | execv(executable, argv); |
| 645 | } |
| 646 | if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) { |
| 647 | saved_errno = errno; |
| 648 | } |
| 649 | } |
| 650 | /* Report the first exec error, not the last. */ |
| 651 | if (saved_errno) |
| 652 | errno = saved_errno; |
| 653 | |
| 654 | error: |
| 655 | saved_errno = errno; |
| 656 | /* Report the posix error to our parent process. */ |
Gregory P. Smith | 12fdca5 | 2012-01-21 12:31:25 -0800 | [diff] [blame] | 657 | /* We ignore all write() return values as the total size of our writes is |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 658 | less than PIPEBUF and we cannot do anything about an error anyways. |
| 659 | Use _Py_write_noraise() to retry write() if it is interrupted by a |
| 660 | signal (fails with EINTR). */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 661 | if (saved_errno) { |
| 662 | char *cur; |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 663 | _Py_write_noraise(errpipe_write, "OSError:", 8); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 664 | cur = hex_errno + sizeof(hex_errno); |
Serhiy Storchaka | 5ae4f49 | 2016-09-27 22:03:51 +0300 | [diff] [blame] | 665 | while (saved_errno != 0 && cur != hex_errno) { |
Gregory P. Smith | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 666 | *--cur = Py_hexdigits[saved_errno % 16]; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 667 | saved_errno /= 16; |
| 668 | } |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 669 | _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur); |
| 670 | _Py_write_noraise(errpipe_write, ":", 1); |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 671 | if (!reached_preexec) { |
| 672 | /* Indicate to the parent that the error happened before exec(). */ |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 673 | _Py_write_noraise(errpipe_write, "noexec", 6); |
Gregory P. Smith | 5591b02 | 2012-10-10 03:34:47 -0700 | [diff] [blame] | 674 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 675 | /* We can't call strerror(saved_errno). It is not async signal safe. |
| 676 | * The parent process will look the error message up. */ |
| 677 | } else { |
Victor Stinner | 185fd33 | 2015-04-01 18:35:53 +0200 | [diff] [blame] | 678 | _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18); |
| 679 | _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg)); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 684 | /* The main purpose of this wrapper function is to isolate vfork() from both |
| 685 | * subprocess_fork_exec() and child_exec(). A child process created via |
| 686 | * vfork() executes on the same stack as the parent process while the latter is |
| 687 | * suspended, so this function should not be inlined to avoid compiler bugs |
| 688 | * that might clobber data needed by the parent later. Additionally, |
| 689 | * child_exec() should not be inlined to avoid spurious -Wclobber warnings from |
| 690 | * GCC (see bpo-35823). |
| 691 | */ |
| 692 | _Py_NO_INLINE static pid_t |
| 693 | do_fork_exec(char *const exec_array[], |
| 694 | char *const argv[], |
| 695 | char *const envp[], |
| 696 | const char *cwd, |
| 697 | int p2cread, int p2cwrite, |
| 698 | int c2pread, int c2pwrite, |
| 699 | int errread, int errwrite, |
| 700 | int errpipe_read, int errpipe_write, |
| 701 | int close_fds, int restore_signals, |
| 702 | int call_setsid, |
| 703 | int call_setgid, gid_t gid, |
| 704 | int call_setgroups, size_t groups_size, const gid_t *groups, |
| 705 | int call_setuid, uid_t uid, int child_umask, |
| 706 | const void *child_sigmask, |
| 707 | PyObject *py_fds_to_keep, |
| 708 | PyObject *preexec_fn, |
| 709 | PyObject *preexec_fn_args_tuple) |
| 710 | { |
| 711 | |
| 712 | pid_t pid; |
| 713 | |
| 714 | #ifdef VFORK_USABLE |
| 715 | if (child_sigmask) { |
| 716 | /* These are checked by our caller; verify them in debug builds. */ |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 717 | assert(!call_setuid); |
| 718 | assert(!call_setgid); |
| 719 | assert(!call_setgroups); |
| 720 | assert(preexec_fn == Py_None); |
| 721 | |
| 722 | pid = vfork(); |
| 723 | } else |
| 724 | #endif |
| 725 | { |
| 726 | pid = fork(); |
| 727 | } |
| 728 | |
| 729 | if (pid != 0) { |
| 730 | return pid; |
| 731 | } |
| 732 | |
| 733 | /* Child process. |
| 734 | * See the comment above child_exec() for restrictions imposed on |
| 735 | * the code below. |
| 736 | */ |
| 737 | |
| 738 | if (preexec_fn != Py_None) { |
| 739 | /* We'll be calling back into Python later so we need to do this. |
| 740 | * This call may not be async-signal-safe but neither is calling |
| 741 | * back into Python. The user asked us to use hope as a strategy |
| 742 | * to avoid deadlock... */ |
| 743 | PyOS_AfterFork_Child(); |
| 744 | } |
| 745 | |
| 746 | child_exec(exec_array, argv, envp, cwd, |
| 747 | p2cread, p2cwrite, c2pread, c2pwrite, |
| 748 | errread, errwrite, errpipe_read, errpipe_write, |
| 749 | close_fds, restore_signals, call_setsid, |
| 750 | call_setgid, gid, call_setgroups, groups_size, groups, |
| 751 | call_setuid, uid, child_umask, child_sigmask, |
| 752 | py_fds_to_keep, preexec_fn, preexec_fn_args_tuple); |
| 753 | _exit(255); |
| 754 | return 0; /* Dead code to avoid a potential compiler warning. */ |
| 755 | } |
| 756 | |
| 757 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 758 | static PyObject * |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 759 | subprocess_fork_exec(PyObject *module, PyObject *args) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 760 | { |
| 761 | PyObject *gc_module = NULL; |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 762 | PyObject *executable_list, *py_fds_to_keep; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 763 | PyObject *env_list, *preexec_fn; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 764 | PyObject *process_args, *converted_args = NULL, *fast_args = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 765 | PyObject *preexec_fn_args_tuple = NULL; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 766 | PyObject *groups_list; |
| 767 | PyObject *uid_object, *gid_object; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 768 | int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite; |
| 769 | int errpipe_read, errpipe_write, close_fds, restore_signals; |
| 770 | int call_setsid; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 771 | int call_setgid = 0, call_setgroups = 0, call_setuid = 0; |
| 772 | uid_t uid; |
| 773 | gid_t gid, *groups = NULL; |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 774 | int child_umask; |
Alexey Izbyshev | c0590c0 | 2020-10-26 03:09:32 +0300 | [diff] [blame] | 775 | PyObject *cwd_obj, *cwd_obj2 = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 776 | const char *cwd; |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 777 | pid_t pid = -1; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 778 | int need_to_reenable_gc = 0; |
| 779 | char *const *exec_array, *const *argv = NULL, *const *envp = NULL; |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 780 | Py_ssize_t arg_num, num_groups = 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 781 | int need_after_fork = 0; |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 782 | int saved_errno = 0; |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 783 | _posixsubprocessstate *state = get_posixsubprocess_state(module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 784 | |
| 785 | if (!PyArg_ParseTuple( |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 786 | args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec", |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 787 | &process_args, &executable_list, |
| 788 | &close_fds, &PyTuple_Type, &py_fds_to_keep, |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 789 | &cwd_obj, &env_list, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 790 | &p2cread, &p2cwrite, &c2pread, &c2pwrite, |
| 791 | &errread, &errwrite, &errpipe_read, &errpipe_write, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 792 | &restore_signals, &call_setsid, |
Gregory P. Smith | f3751ef | 2019-10-12 13:24:56 -0700 | [diff] [blame] | 793 | &gid_object, &groups_list, &uid_object, &child_umask, |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 794 | &preexec_fn)) |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 795 | return NULL; |
| 796 | |
Christian Heimes | 98d90f7 | 2019-08-27 23:36:56 +0200 | [diff] [blame] | 797 | if ((preexec_fn != Py_None) && |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 798 | (PyInterpreterState_Get() != PyInterpreterState_Main())) { |
Christian Heimes | 98d90f7 | 2019-08-27 23:36:56 +0200 | [diff] [blame] | 799 | PyErr_SetString(PyExc_RuntimeError, |
| 800 | "preexec_fn not supported within subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | |
Gregory P. Smith | 361e30c | 2013-12-01 00:12:24 -0800 | [diff] [blame] | 804 | if (close_fds && errpipe_write < 3) { /* precondition */ |
| 805 | PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3"); |
| 806 | return NULL; |
| 807 | } |
Gregory P. Smith | 8facece | 2012-01-21 14:01:08 -0800 | [diff] [blame] | 808 | if (_sanity_check_python_fd_sequence(py_fds_to_keep)) { |
| 809 | PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep"); |
Gregory P. Smith | d4cc7bf | 2010-12-04 11:22:11 +0000 | [diff] [blame] | 810 | return NULL; |
| 811 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 812 | |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 813 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 814 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
| 815 | if (config->_isolated_interpreter) { |
| 816 | PyErr_SetString(PyExc_RuntimeError, |
| 817 | "subprocess not supported for isolated subinterpreters"); |
| 818 | return NULL; |
| 819 | } |
| 820 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 821 | /* We need to call gc.disable() when we'll be calling preexec_fn */ |
| 822 | if (preexec_fn != Py_None) { |
| 823 | PyObject *result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 824 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 825 | gc_module = PyImport_ImportModule("gc"); |
| 826 | if (gc_module == NULL) |
| 827 | return NULL; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 828 | result = PyObject_CallMethodNoArgs( |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 829 | gc_module, state->isenabled); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 830 | if (result == NULL) { |
| 831 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 832 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 833 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 834 | need_to_reenable_gc = PyObject_IsTrue(result); |
| 835 | Py_DECREF(result); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 836 | if (need_to_reenable_gc == -1) { |
| 837 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 838 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 839 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 840 | result = PyObject_CallMethodNoArgs( |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 841 | gc_module, state->disable); |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 842 | if (result == NULL) { |
| 843 | Py_DECREF(gc_module); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 844 | return NULL; |
Gregory P. Smith | 32ec9da | 2010-03-19 16:53:08 +0000 | [diff] [blame] | 845 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 846 | Py_DECREF(result); |
| 847 | } |
| 848 | |
| 849 | exec_array = _PySequence_BytesToCharpArray(executable_list); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 850 | if (!exec_array) |
| 851 | goto cleanup; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 852 | |
| 853 | /* Convert args and env into appropriate arguments for exec() */ |
| 854 | /* These conversions are done in the parent process to avoid allocating |
| 855 | or freeing memory in the child process. */ |
| 856 | if (process_args != Py_None) { |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 857 | Py_ssize_t num_args; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 858 | /* Equivalent to: */ |
| 859 | /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */ |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 860 | fast_args = PySequence_Fast(process_args, "argv must be a tuple"); |
Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 861 | if (fast_args == NULL) |
| 862 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 863 | num_args = PySequence_Fast_GET_SIZE(fast_args); |
| 864 | converted_args = PyTuple_New(num_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 865 | if (converted_args == NULL) |
| 866 | goto cleanup; |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 867 | for (arg_num = 0; arg_num < num_args; ++arg_num) { |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 868 | PyObject *borrowed_arg, *converted_arg; |
Serhiy Storchaka | 66bffd1 | 2017-04-19 21:12:46 +0300 | [diff] [blame] | 869 | if (PySequence_Fast_GET_SIZE(fast_args) != num_args) { |
| 870 | PyErr_SetString(PyExc_RuntimeError, "args changed during iteration"); |
| 871 | goto cleanup; |
| 872 | } |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 873 | borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 874 | if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) |
| 875 | goto cleanup; |
| 876 | PyTuple_SET_ITEM(converted_args, arg_num, converted_arg); |
| 877 | } |
| 878 | |
| 879 | argv = _PySequence_BytesToCharpArray(converted_args); |
| 880 | Py_CLEAR(converted_args); |
Gregory P. Smith | 68f5217 | 2010-03-15 06:07:42 +0000 | [diff] [blame] | 881 | Py_CLEAR(fast_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 882 | if (!argv) |
| 883 | goto cleanup; |
| 884 | } |
| 885 | |
| 886 | if (env_list != Py_None) { |
| 887 | envp = _PySequence_BytesToCharpArray(env_list); |
| 888 | if (!envp) |
| 889 | goto cleanup; |
| 890 | } |
| 891 | |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 892 | if (cwd_obj != Py_None) { |
| 893 | if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0) |
| 894 | goto cleanup; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 895 | cwd = PyBytes_AsString(cwd_obj2); |
Victor Stinner | 0e59cc3 | 2010-04-16 23:49:32 +0000 | [diff] [blame] | 896 | } else { |
| 897 | cwd = NULL; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 900 | if (groups_list != Py_None) { |
| 901 | #ifdef HAVE_SETGROUPS |
| 902 | Py_ssize_t i; |
| 903 | unsigned long gid; |
| 904 | |
| 905 | if (!PyList_Check(groups_list)) { |
| 906 | PyErr_SetString(PyExc_TypeError, |
| 907 | "setgroups argument must be a list"); |
| 908 | goto cleanup; |
| 909 | } |
| 910 | num_groups = PySequence_Size(groups_list); |
| 911 | |
| 912 | if (num_groups < 0) |
| 913 | goto cleanup; |
| 914 | |
| 915 | if (num_groups > MAX_GROUPS) { |
| 916 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 917 | goto cleanup; |
| 918 | } |
| 919 | |
| 920 | if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) { |
| 921 | PyErr_SetString(PyExc_MemoryError, |
| 922 | "failed to allocate memory for group list"); |
| 923 | goto cleanup; |
| 924 | } |
| 925 | |
| 926 | for (i = 0; i < num_groups; i++) { |
| 927 | PyObject *elem; |
| 928 | elem = PySequence_GetItem(groups_list, i); |
| 929 | if (!elem) |
| 930 | goto cleanup; |
| 931 | if (!PyLong_Check(elem)) { |
| 932 | PyErr_SetString(PyExc_TypeError, |
| 933 | "groups must be integers"); |
| 934 | Py_DECREF(elem); |
| 935 | goto cleanup; |
| 936 | } else { |
| 937 | /* In posixmodule.c UnsignedLong is used as a fallback value |
| 938 | * if the value provided does not fit in a Long. Since we are |
| 939 | * already doing the bounds checking on the Python side, we |
| 940 | * can go directly to an UnsignedLong here. */ |
| 941 | if (!_Py_Gid_Converter(elem, &gid)) { |
| 942 | Py_DECREF(elem); |
| 943 | PyErr_SetString(PyExc_ValueError, "invalid group id"); |
| 944 | goto cleanup; |
| 945 | } |
| 946 | groups[i] = gid; |
| 947 | } |
| 948 | Py_DECREF(elem); |
| 949 | } |
| 950 | call_setgroups = 1; |
| 951 | |
| 952 | #else /* HAVE_SETGROUPS */ |
| 953 | PyErr_BadInternalCall(); |
| 954 | goto cleanup; |
| 955 | #endif /* HAVE_SETGROUPS */ |
| 956 | } |
| 957 | |
| 958 | if (gid_object != Py_None) { |
| 959 | #ifdef HAVE_SETREGID |
| 960 | if (!_Py_Gid_Converter(gid_object, &gid)) |
| 961 | goto cleanup; |
| 962 | |
| 963 | call_setgid = 1; |
| 964 | |
| 965 | #else /* HAVE_SETREGID */ |
| 966 | PyErr_BadInternalCall(); |
| 967 | goto cleanup; |
| 968 | #endif /* HAVE_SETREUID */ |
| 969 | } |
| 970 | |
| 971 | if (uid_object != Py_None) { |
| 972 | #ifdef HAVE_SETREUID |
| 973 | if (!_Py_Uid_Converter(uid_object, &uid)) |
| 974 | goto cleanup; |
| 975 | |
| 976 | call_setuid = 1; |
| 977 | |
| 978 | #else /* HAVE_SETREUID */ |
| 979 | PyErr_BadInternalCall(); |
| 980 | goto cleanup; |
| 981 | #endif /* HAVE_SETREUID */ |
| 982 | } |
| 983 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 984 | /* This must be the last thing done before fork() because we do not |
| 985 | * want to call PyOS_BeforeFork() if there is any chance of another |
| 986 | * error leading to the cleanup: code without calling fork(). */ |
| 987 | if (preexec_fn != Py_None) { |
| 988 | preexec_fn_args_tuple = PyTuple_New(0); |
| 989 | if (!preexec_fn_args_tuple) |
| 990 | goto cleanup; |
| 991 | PyOS_BeforeFork(); |
| 992 | need_after_fork = 1; |
| 993 | } |
| 994 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 995 | /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */ |
| 996 | const void *old_sigmask = NULL; |
| 997 | #ifdef VFORK_USABLE |
| 998 | /* Use vfork() only if it's safe. See the comment above child_exec(). */ |
| 999 | sigset_t old_sigs; |
| 1000 | if (preexec_fn == Py_None && |
Gregory P. Smith | be3c3a0 | 2020-10-24 12:07:35 -0700 | [diff] [blame] | 1001 | !call_setuid && !call_setgid && !call_setgroups) { |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1002 | /* Block all signals to ensure that no signal handlers are run in the |
| 1003 | * child process while it shares memory with us. Note that signals |
| 1004 | * used internally by C libraries won't be blocked by |
| 1005 | * pthread_sigmask(), but signal handlers installed by C libraries |
| 1006 | * normally service only signals originating from *within the process*, |
| 1007 | * so it should be sufficient to consider any library function that |
| 1008 | * might send such a signal to be vfork-unsafe and do not call it in |
| 1009 | * the child. |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1010 | */ |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1011 | sigset_t all_sigs; |
| 1012 | sigfillset(&all_sigs); |
Alexey Izbyshev | 473db47 | 2020-10-24 20:47:38 +0300 | [diff] [blame] | 1013 | if ((saved_errno = pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs))) { |
Alexey Izbyshev | 473db47 | 2020-10-24 20:47:38 +0300 | [diff] [blame] | 1014 | goto cleanup; |
| 1015 | } |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1016 | old_sigmask = &old_sigs; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1017 | } |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1018 | #endif |
| 1019 | |
| 1020 | pid = do_fork_exec(exec_array, argv, envp, cwd, |
| 1021 | p2cread, p2cwrite, c2pread, c2pwrite, |
| 1022 | errread, errwrite, errpipe_read, errpipe_write, |
| 1023 | close_fds, restore_signals, call_setsid, |
| 1024 | call_setgid, gid, call_setgroups, num_groups, groups, |
| 1025 | call_setuid, uid, child_umask, old_sigmask, |
| 1026 | py_fds_to_keep, preexec_fn, preexec_fn_args_tuple); |
| 1027 | |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 1028 | /* Parent (original) process */ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1029 | if (pid == -1) { |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 1030 | /* Capture errno for the exception. */ |
| 1031 | saved_errno = errno; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1032 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1033 | |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1034 | #ifdef VFORK_USABLE |
| 1035 | if (old_sigmask) { |
| 1036 | /* vfork() semantics guarantees that the parent is blocked |
| 1037 | * until the child performs _exit() or execve(), so it is safe |
| 1038 | * to unblock signals once we're here. |
| 1039 | * Note that in environments where vfork() is implemented as fork(), |
| 1040 | * such as QEMU user-mode emulation, the parent won't be blocked, |
| 1041 | * but it won't share the address space with the child, |
Alexey Izbyshev | 473db47 | 2020-10-24 20:47:38 +0300 | [diff] [blame] | 1042 | * so it's still safe to unblock the signals. |
| 1043 | * |
| 1044 | * We don't handle errors here because this call can't fail |
| 1045 | * if valid arguments are given, and because there is no good |
| 1046 | * way for the caller to deal with a failure to restore |
| 1047 | * the thread signal mask. */ |
| 1048 | (void) pthread_sigmask(SIG_SETMASK, old_sigmask, NULL); |
Alexey Izbyshev | 976da90 | 2020-10-24 03:47:01 +0300 | [diff] [blame] | 1049 | } |
| 1050 | #endif |
| 1051 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 1052 | if (need_after_fork) |
| 1053 | PyOS_AfterFork_Parent(); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1054 | |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 1055 | cleanup: |
| 1056 | if (saved_errno != 0) { |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 1057 | errno = saved_errno; |
| 1058 | /* We can't call this above as PyOS_AfterFork_Parent() calls back |
| 1059 | * into Python code which would see the unreturned error. */ |
| 1060 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | a20b6ad | 2018-09-13 04:30:10 -0700 | [diff] [blame] | 1061 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1062 | |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 1063 | Py_XDECREF(preexec_fn_args_tuple); |
| 1064 | PyMem_RawFree(groups); |
Alexey Izbyshev | c0590c0 | 2020-10-26 03:09:32 +0300 | [diff] [blame] | 1065 | Py_XDECREF(cwd_obj2); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1066 | if (envp) |
| 1067 | _Py_FreeCharPArray(envp); |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 1068 | Py_XDECREF(converted_args); |
| 1069 | Py_XDECREF(fast_args); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1070 | if (argv) |
| 1071 | _Py_FreeCharPArray(argv); |
Victor Stinner | 8f437aa | 2014-10-05 17:25:19 +0200 | [diff] [blame] | 1072 | if (exec_array) |
| 1073 | _Py_FreeCharPArray(exec_array); |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 1074 | |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 1075 | _enable_gc(need_to_reenable_gc, gc_module, state); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1076 | Py_XDECREF(gc_module); |
Alexey Izbyshev | d3b4e06 | 2020-11-01 08:33:08 +0300 | [diff] [blame] | 1077 | |
| 1078 | return pid == -1 ? NULL : PyLong_FromPid(pid); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | PyDoc_STRVAR(subprocess_fork_exec_doc, |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 1083 | "fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1084 | p2cread, p2cwrite, c2pread, c2pwrite,\n\ |
| 1085 | errread, errwrite, errpipe_read, errpipe_write,\n\ |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 1086 | restore_signals, call_setsid,\n\ |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 1087 | gid, groups_list, uid,\n\ |
Patrick McLean | 2b2ead7 | 2019-09-12 10:15:44 -0700 | [diff] [blame] | 1088 | preexec_fn)\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1089 | \n\ |
| 1090 | Forks a child process, closes parent file descriptors as appropriate in the\n\ |
| 1091 | child and dups the few that are needed before calling exec() in the child\n\ |
| 1092 | process.\n\ |
| 1093 | \n\ |
Orivej Desh | 77abf23 | 2019-09-20 17:01:10 +0000 | [diff] [blame] | 1094 | If close_fds is true, close file descriptors 3 and higher, except those listed\n\ |
| 1095 | in the sorted tuple pass_fds.\n\ |
| 1096 | \n\ |
| 1097 | The preexec_fn, if supplied, will be called immediately before closing file\n\ |
| 1098 | descriptors and exec.\n\ |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1099 | WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\ |
| 1100 | It may trigger infrequent, difficult to debug deadlocks.\n\ |
| 1101 | \n\ |
| 1102 | If an error occurs in the child process before the exec, it is\n\ |
| 1103 | serialized and written to the errpipe_write fd per subprocess.py.\n\ |
| 1104 | \n\ |
| 1105 | Returns: the child process's PID.\n\ |
| 1106 | \n\ |
| 1107 | Raises: Only on an error in the parent process.\n\ |
| 1108 | "); |
| 1109 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1110 | /* module level code ********************************************************/ |
| 1111 | |
| 1112 | PyDoc_STRVAR(module_doc, |
| 1113 | "A POSIX helper for the subprocess module."); |
| 1114 | |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 1115 | static int |
| 1116 | _posixsubprocess_exec(PyObject *module) |
| 1117 | { |
| 1118 | _posixsubprocessstate *state = get_posixsubprocess_state(module); |
| 1119 | |
| 1120 | state->disable = PyUnicode_InternFromString("disable"); |
| 1121 | if (state->disable == NULL) { |
| 1122 | return -1; |
| 1123 | } |
| 1124 | |
| 1125 | state->enable = PyUnicode_InternFromString("enable"); |
| 1126 | if (state->enable == NULL) { |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| 1130 | state->isenabled = PyUnicode_InternFromString("isenabled"); |
| 1131 | if (state->isenabled == NULL) { |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1137 | |
| 1138 | static PyMethodDef module_methods[] = { |
| 1139 | {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc}, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1140 | {NULL, NULL} /* sentinel */ |
| 1141 | }; |
| 1142 | |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 1143 | static PyModuleDef_Slot _posixsubprocess_slots[] = { |
| 1144 | {Py_mod_exec, _posixsubprocess_exec}, |
| 1145 | {0, NULL} |
| 1146 | }; |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1147 | |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 1148 | static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 1149 | Py_VISIT(get_posixsubprocess_state(m)->disable); |
| 1150 | Py_VISIT(get_posixsubprocess_state(m)->enable); |
| 1151 | Py_VISIT(get_posixsubprocess_state(m)->isenabled); |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static int _posixsubprocess_clear(PyObject *m) { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 1156 | Py_CLEAR(get_posixsubprocess_state(m)->disable); |
| 1157 | Py_CLEAR(get_posixsubprocess_state(m)->enable); |
| 1158 | Py_CLEAR(get_posixsubprocess_state(m)->isenabled); |
Dino Viehland | 5a7d2e1 | 2019-09-10 12:01:20 +0100 | [diff] [blame] | 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static void _posixsubprocess_free(void *m) { |
| 1163 | _posixsubprocess_clear((PyObject *)m); |
| 1164 | } |
| 1165 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1166 | static struct PyModuleDef _posixsubprocessmodule = { |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 1167 | PyModuleDef_HEAD_INIT, |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 1168 | .m_name = "_posixsubprocess", |
| 1169 | .m_doc = module_doc, |
| 1170 | .m_size = sizeof(_posixsubprocessstate), |
| 1171 | .m_methods = module_methods, |
| 1172 | .m_slots = _posixsubprocess_slots, |
| 1173 | .m_traverse = _posixsubprocess_traverse, |
| 1174 | .m_clear = _posixsubprocess_clear, |
| 1175 | .m_free = _posixsubprocess_free, |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1176 | }; |
| 1177 | |
| 1178 | PyMODINIT_FUNC |
| 1179 | PyInit__posixsubprocess(void) |
| 1180 | { |
Christian Heimes | 035deee | 2020-11-21 20:28:14 +0100 | [diff] [blame] | 1181 | return PyModuleDef_Init(&_posixsubprocessmodule); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 1182 | } |