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