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