blob: 8baea314f4e409ab120c47f52f403cdbd76edb1b [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Kyle Evans79925792020-10-13 15:04:44 -05003#include "pycore_fileutils.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02004#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00006#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00007#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00008#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08009#ifdef HAVE_SYS_TYPES_H
10#include <sys/types.h>
11#endif
Gregory P. Smithf3751ef2019-10-12 13:24:56 -070012#if defined(HAVE_SYS_STAT_H)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080013#include <sys/stat.h>
14#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080015#ifdef HAVE_SYS_SYSCALL_H
16#include <sys/syscall.h>
17#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -070018#if defined(HAVE_SYS_RESOURCE_H)
19#include <sys/resource.h>
20#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080021#ifdef HAVE_DIRENT_H
22#include <dirent.h>
23#endif
Patrick McLean2b2ead72019-09-12 10:15:44 -070024#ifdef HAVE_GRP_H
25#include <grp.h>
26#endif /* HAVE_GRP_H */
27
28#include "posixmodule.h"
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000029
Gregory P. Smith3015fb82018-11-12 22:01:22 -080030#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -080031# include <sanitizer/msan_interface.h>
32#endif
33
Xavier de Gayec716f182016-06-15 11:35:29 +020034#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070035# include <sys/linux-syscalls.h>
36# define SYS_getdents64 __NR_getdents64
37#endif
38
Alexey Izbyshev976da902020-10-24 03:47:01 +030039#if defined(__linux__) && defined(HAVE_VFORK) && defined(HAVE_SIGNAL_H) && \
40 defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Gregory P. Smithbe3c3a02020-10-24 12:07:35 -070041/* 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 Izbyshev976da902020-10-24 03:47:01 +030043# include <signal.h>
44# define VFORK_USABLE 1
45#endif
46
Jakub Kulík6f9bc722018-12-31 03:16:40 +010047#if defined(__sun) && defined(__SVR4)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080048/* 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. Smith8facece2012-01-21 14:01:08 -080052/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080053# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080054# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080055# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080056#endif
57
Gregory P. Smith4842efc2012-01-21 21:01:24 -080058#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
59# define FD_DIR "/dev/fd"
60#else
61# define FD_DIR "/proc/self/fd"
62#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000063
Patrick McLean2b2ead72019-09-12 10:15:44 -070064#ifdef NGROUPS_MAX
65#define MAX_GROUPS NGROUPS_MAX
66#else
67#define MAX_GROUPS 64
68#endif
69
Victor Stinnerdaf45552013-08-28 00:53:59 +020070#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000071
Dino Viehland5a7d2e12019-09-10 12:01:20 +010072typedef struct {
73 PyObject* disable;
74 PyObject* enable;
75 PyObject* isenabled;
76} _posixsubprocessstate;
77
78static struct PyModuleDef _posixsubprocessmodule;
79
Hai Shif707d942020-03-16 21:15:01 +080080static inline _posixsubprocessstate*
81get_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. Smithfb94c5f2010-03-14 06:49:55 +000089
Martin Panterafdd5132015-11-30 02:21:41 +000090/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050091static int
Martin Panterafdd5132015-11-30 02:21:41 +000092_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000093{
94 PyObject *result;
Martin Panterafdd5132015-11-30 02:21:41 +000095 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020096
Martin Panterafdd5132015-11-30 02:21:41 +000097 if (need_to_reenable_gc) {
98 PyErr_Fetch(&exctype, &val, &tb);
Petr Viktorinffd97532020-02-11 17:46:57 +010099 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100100 gc_module, _posixsubprocessstate_global->enable);
Martin Panterafdd5132015-11-30 02:21:41 +0000101 if (exctype != NULL) {
102 PyErr_Restore(exctype, val, tb);
103 }
104 if (result == NULL) {
105 return 1;
106 }
107 Py_DECREF(result);
108 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000109 return 0;
110}
111
112
Gregory P. Smith8facece2012-01-21 14:01:08 -0800113/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500114static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200115_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800116{
117 int num = 0;
118 while (*name >= '0' && *name <= '9') {
119 num = num * 10 + (*name - '0');
120 ++name;
121 }
122 if (*name)
123 return -1; /* Non digit found, not a number. */
124 return num;
125}
126
127
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800128#if defined(__FreeBSD__)
129/* When /dev/fd isn't mounted it is often a static directory populated
130 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
131 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
132 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
133 * that properly supports /dev/fd.
134 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500135static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +0200136_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800137{
138 struct stat dev_stat;
139 struct stat dev_fd_stat;
140 if (stat("/dev", &dev_stat) != 0)
141 return 0;
142 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200143 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800144 if (dev_stat.st_dev == dev_fd_stat.st_dev)
145 return 0; /* / == /dev == /dev/fd means it is static. #fail */
146 return 1;
147}
148#endif
149
150
Gregory P. Smith8facece2012-01-21 14:01:08 -0800151/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500152static int
153_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800154{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300155 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800156 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300157 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
158 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
159 long iter_fd;
160 if (!PyLong_Check(py_fd)) {
161 return 1;
162 }
163 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800164 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300165 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800166 return 1;
167 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800168 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800169 }
170 return 0;
171}
172
173
174/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500175static int
176_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800177{
178 /* Binary search. */
179 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300180 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800181 if (search_max < 0)
182 return 0;
183 do {
184 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300185 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800186 if (fd == middle_fd)
187 return 1;
188 if (fd > middle_fd)
189 search_min = middle + 1;
190 else
191 search_max = middle - 1;
192 } while (search_min <= search_max);
193 return 0;
194}
195
Victor Stinnerdaf45552013-08-28 00:53:59 +0200196static int
197make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
198{
199 Py_ssize_t i, len;
200
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300201 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200202 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300203 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200204 long fd = PyLong_AsLong(fdobj);
205 assert(!PyErr_Occurred());
206 assert(0 <= fd && fd <= INT_MAX);
207 if (fd == errpipe_write) {
208 /* errpipe_write is part of py_fds_to_keep. It must be closed at
209 exec(), but kept open in the child process until exec() is
210 called. */
211 continue;
212 }
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300213 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200214 return -1;
215 }
216 return 0;
217}
218
Gregory P. Smith8facece2012-01-21 14:01:08 -0800219
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700220/* Get the maximum file descriptor that could be opened by this process.
221 * This function is async signal safe for use between fork() and exec().
222 */
223static long
224safe_get_max_fd(void)
225{
226 long local_max_fd;
227#if defined(__NetBSD__)
228 local_max_fd = fcntl(0, F_MAXFD);
229 if (local_max_fd >= 0)
230 return local_max_fd;
231#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700232#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
233 struct rlimit rl;
234 /* Not on the POSIX async signal safe functions list but likely
235 * safe. TODO - Someone should audit OpenBSD to make sure. */
236 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
237 return (long) rl.rlim_max;
238#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700239#ifdef _SC_OPEN_MAX
240 local_max_fd = sysconf(_SC_OPEN_MAX);
241 if (local_max_fd == -1)
242#endif
243 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
244 return local_max_fd;
245}
246
247
248/* Close all file descriptors in the range from start_fd and higher
249 * except for those in py_fds_to_keep. If the range defined by
250 * [start_fd, safe_get_max_fd()) is large this will take a long
251 * time as it calls close() on EVERY possible fd.
252 *
253 * It isn't possible to know for sure what the max fd to go up to
254 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800255 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500256static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700257_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800258{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700259 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300260 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800261 Py_ssize_t keep_seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800262 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600263 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800264 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300265 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800266 int keep_fd = PyLong_AsLong(py_keep_fd);
267 if (keep_fd < start_fd)
268 continue;
Kyle Evansc230fde2020-10-11 13:54:11 -0500269 _Py_closerange(start_fd, keep_fd - 1);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800270 start_fd = keep_fd + 1;
271 }
272 if (start_fd <= end_fd) {
Kyle Evansc230fde2020-10-11 13:54:11 -0500273 _Py_closerange(start_fd, end_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800274 }
275}
276
277
278#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
279/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
280 * only to read a directory of short file descriptor number names. The kernel
281 * will return an error if we didn't give it enough space. Highly Unlikely.
282 * This structure is very old and stable: It will not change unless the kernel
283 * chooses to break compatibility with all existing binaries. Highly Unlikely.
284 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800285struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700286 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800287 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800288 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800289 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800290 char d_name[256]; /* Filename (null-terminated) */
291};
292
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700293/* Close all open file descriptors in the range from start_fd and higher
294 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800295 *
296 * This version is async signal safe as it does not make any unsafe C library
297 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
298 * to resort to making a kernel system call directly but this is the ONLY api
299 * available that does no harm. opendir/readdir/closedir perform memory
300 * allocation and locking so while they usually work they are not guaranteed
301 * to (especially if you have replaced your malloc implementation). A version
302 * of this function that uses those can be found in the _maybe_unsafe variant.
303 *
304 * This is Linux specific because that is all I am ready to test it on. It
305 * should be easy to add OS specific dirent or dirent64 structures and modify
306 * it with some cpp #define magic to work on other OSes as well if you want.
307 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500308static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700309_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800310{
311 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200312
Victor Stinner160e8192015-03-30 02:18:31 +0200313 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800314 if (fd_dir_fd == -1) {
315 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700316 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800317 return;
318 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800319 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800320 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800321 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
322 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800323 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800324 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800325 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800326#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800327 __msan_unpoison(buffer, bytes);
328#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
330 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800331 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800332 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
333 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700334 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800335 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200336 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800337 }
338 }
339 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200340 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800341 }
342}
343
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700344#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800345
346#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
347
348
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700349/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300350 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800351 *
352 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800353 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800354 * likely to ever cause a problem is opendir() as it performs an internal
355 * malloc(). Practically this should not be a problem. The Java VM makes the
356 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
357 *
358 * readdir_r() is not used because it provides no benefit. It is typically
359 * implemented as readdir() followed by memcpy(). See also:
360 * http://womble.decadent.org.uk/readdir_r-advisory.html
361 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500362static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700363_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800364{
365 DIR *proc_fd_dir;
366#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700367 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800368 ++start_fd;
369 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800370 /* Close our lowest fd before we call opendir so that it is likely to
371 * reuse that fd otherwise we might close opendir's file descriptor in
372 * our loop. This trick assumes that fd's are allocated on a lowest
373 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200374 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800375 ++start_fd;
376#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800377
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800378#if defined(__FreeBSD__)
379 if (!_is_fdescfs_mounted_on_dev_fd())
380 proc_fd_dir = NULL;
381 else
382#endif
383 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800384 if (!proc_fd_dir) {
385 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700386 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800387 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800388 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800389#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800390 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800391#else
392 int fd_used_by_opendir = start_fd - 1;
393#endif
394 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800395 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800396 int fd;
397 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
398 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700399 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800400 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200401 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800402 }
403 errno = 0;
404 }
405 if (errno) {
406 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700407 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800408 }
409 closedir(proc_fd_dir);
410 }
411}
412
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700413#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800414
415#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
416
417
Alexey Izbyshev976da902020-10-24 03:47:01 +0300418#ifdef VFORK_USABLE
419/* Reset dispositions for all signals to SIG_DFL except for ignored
420 * signals. This way we ensure that no signal handlers can run
421 * after we unblock signals in a child created by vfork().
422 */
423static void
424reset_signal_handlers(const sigset_t *child_sigmask)
425{
426 struct sigaction sa_dfl = {.sa_handler = SIG_DFL};
427 for (int sig = 1; sig < _NSIG; sig++) {
428 /* Dispositions for SIGKILL and SIGSTOP can't be changed. */
429 if (sig == SIGKILL || sig == SIGSTOP) {
430 continue;
431 }
432
433 /* There is no need to reset the disposition of signals that will
434 * remain blocked across execve() since the kernel will do it. */
435 if (sigismember(child_sigmask, sig) == 1) {
436 continue;
437 }
438
439 struct sigaction sa;
440 /* C libraries usually return EINVAL for signals used
441 * internally (e.g. for thread cancellation), so simply
442 * skip errors here. */
443 if (sigaction(sig, NULL, &sa) == -1) {
444 continue;
445 }
446
447 /* void *h works as these fields are both pointer types already. */
448 void *h = (sa.sa_flags & SA_SIGINFO ? (void *)sa.sa_sigaction :
449 (void *)sa.sa_handler);
450 if (h == SIG_IGN || h == SIG_DFL) {
451 continue;
452 }
453
454 /* This call can't reasonably fail, but if it does, terminating
455 * the child seems to be too harsh, so ignore errors. */
456 (void) sigaction(sig, &sa_dfl, NULL);
457 }
458}
459#endif /* VFORK_USABLE */
460
461
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000462/*
Alexey Izbyshev976da902020-10-24 03:47:01 +0300463 * This function is code executed in the child process immediately after
464 * (v)fork to set things up and call exec().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000465 *
466 * All of the code in this function must only use async-signal-safe functions,
467 * listed at `man 7 signal` or
468 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
469 *
470 * This restriction is documented at
471 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
Alexey Izbyshev976da902020-10-24 03:47:01 +0300472 *
473 * If this function is called after vfork(), even more care must be taken.
474 * The lack of preparations that C libraries normally take on fork(),
475 * as well as sharing the address space with the parent, might make even
476 * async-signal-safe functions vfork-unsafe. In particular, on Linux,
477 * set*id() and setgroups() library functions must not be called, since
478 * they have to interact with the library-level thread list and send
479 * library-internal signals to implement per-process credentials semantics
480 * required by POSIX but not supported natively on Linux. Another reason to
481 * avoid this family of functions is that sharing an address space between
482 * processes running with different privileges is inherently insecure.
483 * See bpo-35823 for further discussion and references.
484 *
485 * In some C libraries, setrlimit() has the same thread list/signalling
486 * behavior since resource limits were per-thread attributes before
487 * Linux 2.6.10. Musl, as of 1.2.1, is known to have this issue
488 * (https://www.openwall.com/lists/musl/2020/10/15/6).
489 *
490 * If vfork-unsafe functionality is desired after vfork(), consider using
491 * syscall() to obtain it.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000492 */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300493_Py_NO_INLINE static void
Benjamin Peterson91eef982012-01-22 20:04:46 -0500494child_exec(char *const exec_array[],
495 char *const argv[],
496 char *const envp[],
497 const char *cwd,
498 int p2cread, int p2cwrite,
499 int c2pread, int c2pwrite,
500 int errread, int errwrite,
501 int errpipe_read, int errpipe_write,
502 int close_fds, int restore_signals,
503 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700504 int call_setgid, gid_t gid,
505 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700506 int call_setuid, uid_t uid, int child_umask,
Alexey Izbyshev976da902020-10-24 03:47:01 +0300507 const void *child_sigmask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500508 PyObject *py_fds_to_keep,
509 PyObject *preexec_fn,
510 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000511{
Victor Stinner185fd332015-04-01 18:35:53 +0200512 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000513 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000514 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000515 /* Buffer large enough to hold a hex integer. We can't malloc. */
516 char hex_errno[sizeof(saved_errno)*2+1];
517
Victor Stinnerdaf45552013-08-28 00:53:59 +0200518 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
519 goto error;
520
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000521 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200522 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200524 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000525 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200526 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000527 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000528 POSIX_CALL(close(errpipe_read));
529
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200530 /* When duping fds, if there arises a situation where one of the fds is
531 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700532 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200533 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700534 /* issue32270 */
535 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
536 goto error;
537 }
538 }
539 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200540 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700541 /* issue32270 */
542 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
543 goto error;
544 }
545 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200546
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000547 /* Dup fds for child.
548 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
549 would be a no-op (issue #10806). */
550 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300551 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200552 goto error;
553 }
554 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000555 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200556
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000557 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300558 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200559 goto error;
560 }
561 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000562 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200563
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000564 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300565 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200566 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000567 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200568 else if (errwrite != -1)
569 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000570
Gregory P. Smithce344102018-09-10 17:46:22 -0700571 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
572 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000573
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000574 if (cwd)
575 POSIX_CALL(chdir(cwd));
576
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700577 if (child_umask >= 0)
578 umask(child_umask); /* umask() always succeeds. */
579
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000580 if (restore_signals)
581 _Py_RestoreSignals();
582
Alexey Izbyshev976da902020-10-24 03:47:01 +0300583#ifdef VFORK_USABLE
584 if (child_sigmask) {
585 reset_signal_handlers(child_sigmask);
Alexey Izbyshev473db472020-10-24 20:47:38 +0300586 if ((errno = pthread_sigmask(SIG_SETMASK, child_sigmask, NULL))) {
587 goto error;
588 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300589 }
590#endif
591
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000592#ifdef HAVE_SETSID
593 if (call_setsid)
594 POSIX_CALL(setsid());
595#endif
596
Patrick McLean2b2ead72019-09-12 10:15:44 -0700597#ifdef HAVE_SETGROUPS
598 if (call_setgroups)
599 POSIX_CALL(setgroups(groups_size, groups));
600#endif /* HAVE_SETGROUPS */
601
602#ifdef HAVE_SETREGID
603 if (call_setgid)
604 POSIX_CALL(setregid(gid, gid));
605#endif /* HAVE_SETREGID */
606
607#ifdef HAVE_SETREUID
608 if (call_setuid)
609 POSIX_CALL(setreuid(uid, uid));
610#endif /* HAVE_SETREUID */
611
612
Gregory P. Smith5591b022012-10-10 03:34:47 -0700613 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000614 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
615 /* This is where the user has asked us to deadlock their program. */
616 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
617 if (result == NULL) {
618 /* Stringifying the exception or traceback would involve
619 * memory allocation and thus potential for deadlock.
620 * We've already faced potential deadlock by calling back
621 * into Python in the first place, so it probably doesn't
622 * matter but we avoid it to minimize the possibility. */
623 err_msg = "Exception occurred in preexec_fn.";
624 errno = 0; /* We don't want to report an OSError. */
625 goto error;
626 }
627 /* Py_DECREF(result); - We're about to exec so why bother? */
628 }
629
Charles-François Natali249cdc32013-08-25 18:24:45 +0200630 /* close FDs after executing preexec_fn, which might open FDs */
631 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200632 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700633 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200634 }
635
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000636 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
637 /* given the executable_list generated by Lib/subprocess.py. */
638 saved_errno = 0;
639 for (i = 0; exec_array[i] != NULL; ++i) {
640 const char *executable = exec_array[i];
641 if (envp) {
642 execve(executable, argv, envp);
643 } else {
644 execv(executable, argv);
645 }
646 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
647 saved_errno = errno;
648 }
649 }
650 /* Report the first exec error, not the last. */
651 if (saved_errno)
652 errno = saved_errno;
653
654error:
655 saved_errno = errno;
656 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800657 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200658 less than PIPEBUF and we cannot do anything about an error anyways.
659 Use _Py_write_noraise() to retry write() if it is interrupted by a
660 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000661 if (saved_errno) {
662 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200663 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000664 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300665 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000666 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000667 saved_errno /= 16;
668 }
Victor Stinner185fd332015-04-01 18:35:53 +0200669 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
670 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700671 if (!reached_preexec) {
672 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200673 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700674 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000675 /* We can't call strerror(saved_errno). It is not async signal safe.
676 * The parent process will look the error message up. */
677 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200678 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
679 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000680 }
681}
682
683
Alexey Izbyshev976da902020-10-24 03:47:01 +0300684/* The main purpose of this wrapper function is to isolate vfork() from both
685 * subprocess_fork_exec() and child_exec(). A child process created via
686 * vfork() executes on the same stack as the parent process while the latter is
687 * suspended, so this function should not be inlined to avoid compiler bugs
688 * that might clobber data needed by the parent later. Additionally,
689 * child_exec() should not be inlined to avoid spurious -Wclobber warnings from
690 * GCC (see bpo-35823).
691 */
692_Py_NO_INLINE static pid_t
693do_fork_exec(char *const exec_array[],
694 char *const argv[],
695 char *const envp[],
696 const char *cwd,
697 int p2cread, int p2cwrite,
698 int c2pread, int c2pwrite,
699 int errread, int errwrite,
700 int errpipe_read, int errpipe_write,
701 int close_fds, int restore_signals,
702 int call_setsid,
703 int call_setgid, gid_t gid,
704 int call_setgroups, size_t groups_size, const gid_t *groups,
705 int call_setuid, uid_t uid, int child_umask,
706 const void *child_sigmask,
707 PyObject *py_fds_to_keep,
708 PyObject *preexec_fn,
709 PyObject *preexec_fn_args_tuple)
710{
711
712 pid_t pid;
713
714#ifdef VFORK_USABLE
715 if (child_sigmask) {
716 /* These are checked by our caller; verify them in debug builds. */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300717 assert(!call_setuid);
718 assert(!call_setgid);
719 assert(!call_setgroups);
720 assert(preexec_fn == Py_None);
721
722 pid = vfork();
723 } else
724#endif
725 {
726 pid = fork();
727 }
728
729 if (pid != 0) {
730 return pid;
731 }
732
733 /* Child process.
734 * See the comment above child_exec() for restrictions imposed on
735 * the code below.
736 */
737
738 if (preexec_fn != Py_None) {
739 /* We'll be calling back into Python later so we need to do this.
740 * This call may not be async-signal-safe but neither is calling
741 * back into Python. The user asked us to use hope as a strategy
742 * to avoid deadlock... */
743 PyOS_AfterFork_Child();
744 }
745
746 child_exec(exec_array, argv, envp, cwd,
747 p2cread, p2cwrite, c2pread, c2pwrite,
748 errread, errwrite, errpipe_read, errpipe_write,
749 close_fds, restore_signals, call_setsid,
750 call_setgid, gid, call_setgroups, groups_size, groups,
751 call_setuid, uid, child_umask, child_sigmask,
752 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
753 _exit(255);
754 return 0; /* Dead code to avoid a potential compiler warning. */
755}
756
757
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000758static PyObject *
759subprocess_fork_exec(PyObject* self, PyObject *args)
760{
761 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200762 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000763 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000764 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000765 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700766 PyObject *groups_list;
767 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000768 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
769 int errpipe_read, errpipe_write, close_fds, restore_signals;
770 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700771 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
772 uid_t uid;
773 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700774 int child_umask;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000775 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000776 const char *cwd;
777 pid_t pid;
778 int need_to_reenable_gc = 0;
779 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700780 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200781 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700782 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000783
784 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700785 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300786 &process_args, &executable_list,
787 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000788 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000789 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
790 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700791 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700792 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700793 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000794 return NULL;
795
Christian Heimes98d90f72019-08-27 23:36:56 +0200796 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100797 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200798 PyErr_SetString(PyExc_RuntimeError,
799 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700800 return NULL;
801 }
802
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800803 if (close_fds && errpipe_write < 3) { /* precondition */
804 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
805 return NULL;
806 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800807 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
808 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000809 return NULL;
810 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000811
Victor Stinner252346a2020-05-01 11:33:44 +0200812 PyInterpreterState *interp = PyInterpreterState_Get();
813 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
814 if (config->_isolated_interpreter) {
815 PyErr_SetString(PyExc_RuntimeError,
816 "subprocess not supported for isolated subinterpreters");
817 return NULL;
818 }
819
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000820 /* We need to call gc.disable() when we'll be calling preexec_fn */
821 if (preexec_fn != Py_None) {
822 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200823
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000824 gc_module = PyImport_ImportModule("gc");
825 if (gc_module == NULL)
826 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100827 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100828 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000829 if (result == NULL) {
830 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000831 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000832 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000833 need_to_reenable_gc = PyObject_IsTrue(result);
834 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000835 if (need_to_reenable_gc == -1) {
836 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000837 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000838 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100839 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100840 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000841 if (result == NULL) {
842 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000843 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000844 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000845 Py_DECREF(result);
846 }
847
848 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200849 if (!exec_array)
850 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000851
852 /* Convert args and env into appropriate arguments for exec() */
853 /* These conversions are done in the parent process to avoid allocating
854 or freeing memory in the child process. */
855 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000856 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000857 /* Equivalent to: */
858 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000859 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200860 if (fast_args == NULL)
861 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000862 num_args = PySequence_Fast_GET_SIZE(fast_args);
863 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000864 if (converted_args == NULL)
865 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000866 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000867 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300868 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
869 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
870 goto cleanup;
871 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000872 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000873 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
874 goto cleanup;
875 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
876 }
877
878 argv = _PySequence_BytesToCharpArray(converted_args);
879 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000880 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000881 if (!argv)
882 goto cleanup;
883 }
884
885 if (env_list != Py_None) {
886 envp = _PySequence_BytesToCharpArray(env_list);
887 if (!envp)
888 goto cleanup;
889 }
890
Victor Stinner0e59cc32010-04-16 23:49:32 +0000891 if (cwd_obj != Py_None) {
892 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
893 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000894 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000895 } else {
896 cwd = NULL;
897 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000898 }
899
Patrick McLean2b2ead72019-09-12 10:15:44 -0700900 if (groups_list != Py_None) {
901#ifdef HAVE_SETGROUPS
902 Py_ssize_t i;
903 unsigned long gid;
904
905 if (!PyList_Check(groups_list)) {
906 PyErr_SetString(PyExc_TypeError,
907 "setgroups argument must be a list");
908 goto cleanup;
909 }
910 num_groups = PySequence_Size(groups_list);
911
912 if (num_groups < 0)
913 goto cleanup;
914
915 if (num_groups > MAX_GROUPS) {
916 PyErr_SetString(PyExc_ValueError, "too many groups");
917 goto cleanup;
918 }
919
920 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
921 PyErr_SetString(PyExc_MemoryError,
922 "failed to allocate memory for group list");
923 goto cleanup;
924 }
925
926 for (i = 0; i < num_groups; i++) {
927 PyObject *elem;
928 elem = PySequence_GetItem(groups_list, i);
929 if (!elem)
930 goto cleanup;
931 if (!PyLong_Check(elem)) {
932 PyErr_SetString(PyExc_TypeError,
933 "groups must be integers");
934 Py_DECREF(elem);
935 goto cleanup;
936 } else {
937 /* In posixmodule.c UnsignedLong is used as a fallback value
938 * if the value provided does not fit in a Long. Since we are
939 * already doing the bounds checking on the Python side, we
940 * can go directly to an UnsignedLong here. */
941 if (!_Py_Gid_Converter(elem, &gid)) {
942 Py_DECREF(elem);
943 PyErr_SetString(PyExc_ValueError, "invalid group id");
944 goto cleanup;
945 }
946 groups[i] = gid;
947 }
948 Py_DECREF(elem);
949 }
950 call_setgroups = 1;
951
952#else /* HAVE_SETGROUPS */
953 PyErr_BadInternalCall();
954 goto cleanup;
955#endif /* HAVE_SETGROUPS */
956 }
957
958 if (gid_object != Py_None) {
959#ifdef HAVE_SETREGID
960 if (!_Py_Gid_Converter(gid_object, &gid))
961 goto cleanup;
962
963 call_setgid = 1;
964
965#else /* HAVE_SETREGID */
966 PyErr_BadInternalCall();
967 goto cleanup;
968#endif /* HAVE_SETREUID */
969 }
970
971 if (uid_object != Py_None) {
972#ifdef HAVE_SETREUID
973 if (!_Py_Uid_Converter(uid_object, &uid))
974 goto cleanup;
975
976 call_setuid = 1;
977
978#else /* HAVE_SETREUID */
979 PyErr_BadInternalCall();
980 goto cleanup;
981#endif /* HAVE_SETREUID */
982 }
983
Gregory P. Smith163468a2017-05-29 10:03:41 -0700984 /* This must be the last thing done before fork() because we do not
985 * want to call PyOS_BeforeFork() if there is any chance of another
986 * error leading to the cleanup: code without calling fork(). */
987 if (preexec_fn != Py_None) {
988 preexec_fn_args_tuple = PyTuple_New(0);
989 if (!preexec_fn_args_tuple)
990 goto cleanup;
991 PyOS_BeforeFork();
992 need_after_fork = 1;
993 }
994
Alexey Izbyshev976da902020-10-24 03:47:01 +0300995 /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */
996 const void *old_sigmask = NULL;
997#ifdef VFORK_USABLE
998 /* Use vfork() only if it's safe. See the comment above child_exec(). */
999 sigset_t old_sigs;
1000 if (preexec_fn == Py_None &&
Gregory P. Smithbe3c3a02020-10-24 12:07:35 -07001001 !call_setuid && !call_setgid && !call_setgroups) {
Alexey Izbyshev976da902020-10-24 03:47:01 +03001002 /* Block all signals to ensure that no signal handlers are run in the
1003 * child process while it shares memory with us. Note that signals
1004 * used internally by C libraries won't be blocked by
1005 * pthread_sigmask(), but signal handlers installed by C libraries
1006 * normally service only signals originating from *within the process*,
1007 * so it should be sufficient to consider any library function that
1008 * might send such a signal to be vfork-unsafe and do not call it in
1009 * the child.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001010 */
Alexey Izbyshev976da902020-10-24 03:47:01 +03001011 sigset_t all_sigs;
1012 sigfillset(&all_sigs);
Alexey Izbyshev473db472020-10-24 20:47:38 +03001013 if ((saved_errno = pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs))) {
1014 errno = saved_errno;
1015 PyErr_SetFromErrno(PyExc_OSError);
1016 goto cleanup;
1017 }
Alexey Izbyshev976da902020-10-24 03:47:01 +03001018 old_sigmask = &old_sigs;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001019 }
Alexey Izbyshev976da902020-10-24 03:47:01 +03001020#endif
1021
1022 pid = do_fork_exec(exec_array, argv, envp, cwd,
1023 p2cread, p2cwrite, c2pread, c2pwrite,
1024 errread, errwrite, errpipe_read, errpipe_write,
1025 close_fds, restore_signals, call_setsid,
1026 call_setgid, gid, call_setgroups, num_groups, groups,
1027 call_setuid, uid, child_umask, old_sigmask,
1028 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
1029
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001030 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001031 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001032 /* Capture errno for the exception. */
1033 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001034 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001035
Alexey Izbyshev976da902020-10-24 03:47:01 +03001036#ifdef VFORK_USABLE
1037 if (old_sigmask) {
1038 /* vfork() semantics guarantees that the parent is blocked
1039 * until the child performs _exit() or execve(), so it is safe
1040 * to unblock signals once we're here.
1041 * Note that in environments where vfork() is implemented as fork(),
1042 * such as QEMU user-mode emulation, the parent won't be blocked,
1043 * but it won't share the address space with the child,
Alexey Izbyshev473db472020-10-24 20:47:38 +03001044 * so it's still safe to unblock the signals.
1045 *
1046 * We don't handle errors here because this call can't fail
1047 * if valid arguments are given, and because there is no good
1048 * way for the caller to deal with a failure to restore
1049 * the thread signal mask. */
1050 (void) pthread_sigmask(SIG_SETMASK, old_sigmask, NULL);
Alexey Izbyshev976da902020-10-24 03:47:01 +03001051 }
1052#endif
1053
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001054 Py_XDECREF(cwd_obj2);
1055
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001056 if (need_after_fork)
1057 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001058 if (envp)
1059 _Py_FreeCharPArray(envp);
1060 if (argv)
1061 _Py_FreeCharPArray(argv);
1062 _Py_FreeCharPArray(exec_array);
1063
1064 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +00001065 if (_enable_gc(need_to_reenable_gc, gc_module)) {
1066 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001067 }
Christian Heimes0d3350d2020-06-12 18:18:43 +02001068 PyMem_RawFree(groups);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001069 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001070 Py_XDECREF(gc_module);
1071
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001072 if (pid == -1) {
1073 errno = saved_errno;
1074 /* We can't call this above as PyOS_AfterFork_Parent() calls back
1075 * into Python code which would see the unreturned error. */
1076 PyErr_SetFromErrno(PyExc_OSError);
1077 return NULL; /* fork() failed. */
1078 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001079
1080 return PyLong_FromPid(pid);
1081
1082cleanup:
1083 if (envp)
1084 _Py_FreeCharPArray(envp);
1085 if (argv)
1086 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +02001087 if (exec_array)
1088 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -07001089
1090 PyMem_RawFree(groups);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001091 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +00001092 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001093 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +00001094 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001095 Py_XDECREF(gc_module);
1096 return NULL;
1097}
1098
1099
1100PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +00001101"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001102 p2cread, p2cwrite, c2pread, c2pwrite,\n\
1103 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001104 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001105 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001106 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001107\n\
1108Forks a child process, closes parent file descriptors as appropriate in the\n\
1109child and dups the few that are needed before calling exec() in the child\n\
1110process.\n\
1111\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001112If close_fds is true, close file descriptors 3 and higher, except those listed\n\
1113in the sorted tuple pass_fds.\n\
1114\n\
1115The preexec_fn, if supplied, will be called immediately before closing file\n\
1116descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001117WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
1118 It may trigger infrequent, difficult to debug deadlocks.\n\
1119\n\
1120If an error occurs in the child process before the exec, it is\n\
1121serialized and written to the errpipe_write fd per subprocess.py.\n\
1122\n\
1123Returns: the child process's PID.\n\
1124\n\
1125Raises: Only on an error in the parent process.\n\
1126");
1127
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001128/* module level code ********************************************************/
1129
1130PyDoc_STRVAR(module_doc,
1131"A POSIX helper for the subprocess module.");
1132
1133
1134static PyMethodDef module_methods[] = {
1135 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001136 {NULL, NULL} /* sentinel */
1137};
1138
1139
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001140static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +08001141 Py_VISIT(get_posixsubprocess_state(m)->disable);
1142 Py_VISIT(get_posixsubprocess_state(m)->enable);
1143 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001144 return 0;
1145}
1146
1147static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +08001148 Py_CLEAR(get_posixsubprocess_state(m)->disable);
1149 Py_CLEAR(get_posixsubprocess_state(m)->enable);
1150 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001151 return 0;
1152}
1153
1154static void _posixsubprocess_free(void *m) {
1155 _posixsubprocess_clear((PyObject *)m);
1156}
1157
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001158static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -06001159 PyModuleDef_HEAD_INIT,
1160 "_posixsubprocess",
1161 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001162 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -06001163 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001164 NULL,
1165 _posixsubprocess_traverse,
1166 _posixsubprocess_clear,
1167 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001168};
1169
1170PyMODINIT_FUNC
1171PyInit__posixsubprocess(void)
1172{
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001173 PyObject* m;
1174
1175 m = PyState_FindModule(&_posixsubprocessmodule);
1176 if (m != NULL) {
1177 Py_INCREF(m);
1178 return m;
1179 }
1180
1181 m = PyModule_Create(&_posixsubprocessmodule);
1182 if (m == NULL) {
1183 return NULL;
1184 }
1185
Hai Shif707d942020-03-16 21:15:01 +08001186 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1187 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1188 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001189
1190 PyState_AddModule(m, &_posixsubprocessmodule);
1191 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001192}