blob: b7cba30ee7618a1eb7002439bfb76e9ecd574b64 [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)
41# include <signal.h>
42# define VFORK_USABLE 1
43#endif
44
Jakub Kulík6f9bc722018-12-31 03:16:40 +010045#if defined(__sun) && defined(__SVR4)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080046/* readdir64 is used to work around Solaris 9 bug 6395699. */
47# define readdir readdir64
48# define dirent dirent64
49# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080050/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080051# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080052# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080053# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080054#endif
55
Gregory P. Smith4842efc2012-01-21 21:01:24 -080056#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
57# define FD_DIR "/dev/fd"
58#else
59# define FD_DIR "/proc/self/fd"
60#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000061
Patrick McLean2b2ead72019-09-12 10:15:44 -070062#ifdef NGROUPS_MAX
63#define MAX_GROUPS NGROUPS_MAX
64#else
65#define MAX_GROUPS 64
66#endif
67
Victor Stinnerdaf45552013-08-28 00:53:59 +020068#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000069
Dino Viehland5a7d2e12019-09-10 12:01:20 +010070typedef struct {
71 PyObject* disable;
72 PyObject* enable;
73 PyObject* isenabled;
74} _posixsubprocessstate;
75
76static struct PyModuleDef _posixsubprocessmodule;
77
Hai Shif707d942020-03-16 21:15:01 +080078static inline _posixsubprocessstate*
79get_posixsubprocess_state(PyObject *module)
80{
81 void *state = PyModule_GetState(module);
82 assert(state != NULL);
83 return (_posixsubprocessstate *)state;
84}
85
86#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000087
Martin Panterafdd5132015-11-30 02:21:41 +000088/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050089static int
Martin Panterafdd5132015-11-30 02:21:41 +000090_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000091{
92 PyObject *result;
Martin Panterafdd5132015-11-30 02:21:41 +000093 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020094
Martin Panterafdd5132015-11-30 02:21:41 +000095 if (need_to_reenable_gc) {
96 PyErr_Fetch(&exctype, &val, &tb);
Petr Viktorinffd97532020-02-11 17:46:57 +010097 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +010098 gc_module, _posixsubprocessstate_global->enable);
Martin Panterafdd5132015-11-30 02:21:41 +000099 if (exctype != NULL) {
100 PyErr_Restore(exctype, val, tb);
101 }
102 if (result == NULL) {
103 return 1;
104 }
105 Py_DECREF(result);
106 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000107 return 0;
108}
109
110
Gregory P. Smith8facece2012-01-21 14:01:08 -0800111/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500112static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200113_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800114{
115 int num = 0;
116 while (*name >= '0' && *name <= '9') {
117 num = num * 10 + (*name - '0');
118 ++name;
119 }
120 if (*name)
121 return -1; /* Non digit found, not a number. */
122 return num;
123}
124
125
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800126#if defined(__FreeBSD__)
127/* When /dev/fd isn't mounted it is often a static directory populated
128 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
129 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
130 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
131 * that properly supports /dev/fd.
132 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500133static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +0200134_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800135{
136 struct stat dev_stat;
137 struct stat dev_fd_stat;
138 if (stat("/dev", &dev_stat) != 0)
139 return 0;
140 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200141 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800142 if (dev_stat.st_dev == dev_fd_stat.st_dev)
143 return 0; /* / == /dev == /dev/fd means it is static. #fail */
144 return 1;
145}
146#endif
147
148
Gregory P. Smith8facece2012-01-21 14:01:08 -0800149/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500150static int
151_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800152{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300153 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800154 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300155 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
156 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
157 long iter_fd;
158 if (!PyLong_Check(py_fd)) {
159 return 1;
160 }
161 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800162 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300163 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800164 return 1;
165 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800166 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800167 }
168 return 0;
169}
170
171
172/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500173static int
174_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800175{
176 /* Binary search. */
177 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300178 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800179 if (search_max < 0)
180 return 0;
181 do {
182 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300183 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800184 if (fd == middle_fd)
185 return 1;
186 if (fd > middle_fd)
187 search_min = middle + 1;
188 else
189 search_max = middle - 1;
190 } while (search_min <= search_max);
191 return 0;
192}
193
Victor Stinnerdaf45552013-08-28 00:53:59 +0200194static int
195make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
196{
197 Py_ssize_t i, len;
198
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300199 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200200 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300201 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200202 long fd = PyLong_AsLong(fdobj);
203 assert(!PyErr_Occurred());
204 assert(0 <= fd && fd <= INT_MAX);
205 if (fd == errpipe_write) {
206 /* errpipe_write is part of py_fds_to_keep. It must be closed at
207 exec(), but kept open in the child process until exec() is
208 called. */
209 continue;
210 }
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300211 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200212 return -1;
213 }
214 return 0;
215}
216
Gregory P. Smith8facece2012-01-21 14:01:08 -0800217
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700218/* Get the maximum file descriptor that could be opened by this process.
219 * This function is async signal safe for use between fork() and exec().
220 */
221static long
222safe_get_max_fd(void)
223{
224 long local_max_fd;
225#if defined(__NetBSD__)
226 local_max_fd = fcntl(0, F_MAXFD);
227 if (local_max_fd >= 0)
228 return local_max_fd;
229#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700230#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
231 struct rlimit rl;
232 /* Not on the POSIX async signal safe functions list but likely
233 * safe. TODO - Someone should audit OpenBSD to make sure. */
234 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
235 return (long) rl.rlim_max;
236#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700237#ifdef _SC_OPEN_MAX
238 local_max_fd = sysconf(_SC_OPEN_MAX);
239 if (local_max_fd == -1)
240#endif
241 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
242 return local_max_fd;
243}
244
245
246/* Close all file descriptors in the range from start_fd and higher
247 * except for those in py_fds_to_keep. If the range defined by
248 * [start_fd, safe_get_max_fd()) is large this will take a long
249 * time as it calls close() on EVERY possible fd.
250 *
251 * It isn't possible to know for sure what the max fd to go up to
252 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800253 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500254static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700255_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800256{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700257 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300258 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800259 Py_ssize_t keep_seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800260 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600261 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800262 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300263 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800264 int keep_fd = PyLong_AsLong(py_keep_fd);
265 if (keep_fd < start_fd)
266 continue;
Kyle Evansc230fde2020-10-11 13:54:11 -0500267 _Py_closerange(start_fd, keep_fd - 1);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800268 start_fd = keep_fd + 1;
269 }
270 if (start_fd <= end_fd) {
Kyle Evansc230fde2020-10-11 13:54:11 -0500271 _Py_closerange(start_fd, end_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800272 }
273}
274
275
276#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
277/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
278 * only to read a directory of short file descriptor number names. The kernel
279 * will return an error if we didn't give it enough space. Highly Unlikely.
280 * This structure is very old and stable: It will not change unless the kernel
281 * chooses to break compatibility with all existing binaries. Highly Unlikely.
282 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800283struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700284 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800285 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800286 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800287 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800288 char d_name[256]; /* Filename (null-terminated) */
289};
290
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700291/* Close all open file descriptors in the range from start_fd and higher
292 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 *
294 * This version is async signal safe as it does not make any unsafe C library
295 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
296 * to resort to making a kernel system call directly but this is the ONLY api
297 * available that does no harm. opendir/readdir/closedir perform memory
298 * allocation and locking so while they usually work they are not guaranteed
299 * to (especially if you have replaced your malloc implementation). A version
300 * of this function that uses those can be found in the _maybe_unsafe variant.
301 *
302 * This is Linux specific because that is all I am ready to test it on. It
303 * should be easy to add OS specific dirent or dirent64 structures and modify
304 * it with some cpp #define magic to work on other OSes as well if you want.
305 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500306static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700307_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800308{
309 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200310
Victor Stinner160e8192015-03-30 02:18:31 +0200311 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 if (fd_dir_fd == -1) {
313 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700314 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800315 return;
316 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800317 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800318 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800319 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
320 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800321 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800322 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800323 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800324#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800325 __msan_unpoison(buffer, bytes);
326#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
328 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800329 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800330 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
331 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700332 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800333 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200334 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800335 }
336 }
337 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200338 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800339 }
340}
341
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700342#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343
344#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
345
346
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700347/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300348 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800349 *
350 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800351 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800352 * likely to ever cause a problem is opendir() as it performs an internal
353 * malloc(). Practically this should not be a problem. The Java VM makes the
354 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
355 *
356 * readdir_r() is not used because it provides no benefit. It is typically
357 * implemented as readdir() followed by memcpy(). See also:
358 * http://womble.decadent.org.uk/readdir_r-advisory.html
359 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500360static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700361_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800362{
363 DIR *proc_fd_dir;
364#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700365 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800366 ++start_fd;
367 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800368 /* Close our lowest fd before we call opendir so that it is likely to
369 * reuse that fd otherwise we might close opendir's file descriptor in
370 * our loop. This trick assumes that fd's are allocated on a lowest
371 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200372 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800373 ++start_fd;
374#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800375
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800376#if defined(__FreeBSD__)
377 if (!_is_fdescfs_mounted_on_dev_fd())
378 proc_fd_dir = NULL;
379 else
380#endif
381 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800382 if (!proc_fd_dir) {
383 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700384 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800385 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800386 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800387#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800388 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800389#else
390 int fd_used_by_opendir = start_fd - 1;
391#endif
392 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800393 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800394 int fd;
395 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
396 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700397 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800398 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200399 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800400 }
401 errno = 0;
402 }
403 if (errno) {
404 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700405 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800406 }
407 closedir(proc_fd_dir);
408 }
409}
410
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700411#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800412
413#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
414
415
Alexey Izbyshev976da902020-10-24 03:47:01 +0300416#ifdef VFORK_USABLE
417/* Reset dispositions for all signals to SIG_DFL except for ignored
418 * signals. This way we ensure that no signal handlers can run
419 * after we unblock signals in a child created by vfork().
420 */
421static void
422reset_signal_handlers(const sigset_t *child_sigmask)
423{
424 struct sigaction sa_dfl = {.sa_handler = SIG_DFL};
425 for (int sig = 1; sig < _NSIG; sig++) {
426 /* Dispositions for SIGKILL and SIGSTOP can't be changed. */
427 if (sig == SIGKILL || sig == SIGSTOP) {
428 continue;
429 }
430
431 /* There is no need to reset the disposition of signals that will
432 * remain blocked across execve() since the kernel will do it. */
433 if (sigismember(child_sigmask, sig) == 1) {
434 continue;
435 }
436
437 struct sigaction sa;
438 /* C libraries usually return EINVAL for signals used
439 * internally (e.g. for thread cancellation), so simply
440 * skip errors here. */
441 if (sigaction(sig, NULL, &sa) == -1) {
442 continue;
443 }
444
445 /* void *h works as these fields are both pointer types already. */
446 void *h = (sa.sa_flags & SA_SIGINFO ? (void *)sa.sa_sigaction :
447 (void *)sa.sa_handler);
448 if (h == SIG_IGN || h == SIG_DFL) {
449 continue;
450 }
451
452 /* This call can't reasonably fail, but if it does, terminating
453 * the child seems to be too harsh, so ignore errors. */
454 (void) sigaction(sig, &sa_dfl, NULL);
455 }
456}
457#endif /* VFORK_USABLE */
458
459
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460/*
Alexey Izbyshev976da902020-10-24 03:47:01 +0300461 * This function is code executed in the child process immediately after
462 * (v)fork to set things up and call exec().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000463 *
464 * All of the code in this function must only use async-signal-safe functions,
465 * listed at `man 7 signal` or
466 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
467 *
468 * This restriction is documented at
469 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
Alexey Izbyshev976da902020-10-24 03:47:01 +0300470 *
471 * If this function is called after vfork(), even more care must be taken.
472 * The lack of preparations that C libraries normally take on fork(),
473 * as well as sharing the address space with the parent, might make even
474 * async-signal-safe functions vfork-unsafe. In particular, on Linux,
475 * set*id() and setgroups() library functions must not be called, since
476 * they have to interact with the library-level thread list and send
477 * library-internal signals to implement per-process credentials semantics
478 * required by POSIX but not supported natively on Linux. Another reason to
479 * avoid this family of functions is that sharing an address space between
480 * processes running with different privileges is inherently insecure.
481 * See bpo-35823 for further discussion and references.
482 *
483 * In some C libraries, setrlimit() has the same thread list/signalling
484 * behavior since resource limits were per-thread attributes before
485 * Linux 2.6.10. Musl, as of 1.2.1, is known to have this issue
486 * (https://www.openwall.com/lists/musl/2020/10/15/6).
487 *
488 * If vfork-unsafe functionality is desired after vfork(), consider using
489 * syscall() to obtain it.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000490 */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300491_Py_NO_INLINE static void
Benjamin Peterson91eef982012-01-22 20:04:46 -0500492child_exec(char *const exec_array[],
493 char *const argv[],
494 char *const envp[],
495 const char *cwd,
496 int p2cread, int p2cwrite,
497 int c2pread, int c2pwrite,
498 int errread, int errwrite,
499 int errpipe_read, int errpipe_write,
500 int close_fds, int restore_signals,
501 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700502 int call_setgid, gid_t gid,
503 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700504 int call_setuid, uid_t uid, int child_umask,
Alexey Izbyshev976da902020-10-24 03:47:01 +0300505 const void *child_sigmask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500506 PyObject *py_fds_to_keep,
507 PyObject *preexec_fn,
508 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000509{
Victor Stinner185fd332015-04-01 18:35:53 +0200510 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000511 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000512 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000513 /* Buffer large enough to hold a hex integer. We can't malloc. */
514 char hex_errno[sizeof(saved_errno)*2+1];
515
Victor Stinnerdaf45552013-08-28 00:53:59 +0200516 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
517 goto error;
518
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000519 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200520 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000521 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200522 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200524 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000525 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000526 POSIX_CALL(close(errpipe_read));
527
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200528 /* When duping fds, if there arises a situation where one of the fds is
529 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700530 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200531 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700532 /* issue32270 */
533 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
534 goto error;
535 }
536 }
537 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200538 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700539 /* issue32270 */
540 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
541 goto error;
542 }
543 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200544
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000545 /* Dup fds for child.
546 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
547 would be a no-op (issue #10806). */
548 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300549 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200550 goto error;
551 }
552 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000553 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200554
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000555 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300556 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200557 goto error;
558 }
559 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000560 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200561
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000562 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300563 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200564 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000565 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200566 else if (errwrite != -1)
567 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000568
Gregory P. Smithce344102018-09-10 17:46:22 -0700569 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
570 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000571
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000572 if (cwd)
573 POSIX_CALL(chdir(cwd));
574
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700575 if (child_umask >= 0)
576 umask(child_umask); /* umask() always succeeds. */
577
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000578 if (restore_signals)
579 _Py_RestoreSignals();
580
Alexey Izbyshev976da902020-10-24 03:47:01 +0300581#ifdef VFORK_USABLE
582 if (child_sigmask) {
583 reset_signal_handlers(child_sigmask);
Alexey Izbyshev473db472020-10-24 20:47:38 +0300584 if ((errno = pthread_sigmask(SIG_SETMASK, child_sigmask, NULL))) {
585 goto error;
586 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300587 }
588#endif
589
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000590#ifdef HAVE_SETSID
591 if (call_setsid)
592 POSIX_CALL(setsid());
593#endif
594
Patrick McLean2b2ead72019-09-12 10:15:44 -0700595#ifdef HAVE_SETGROUPS
596 if (call_setgroups)
597 POSIX_CALL(setgroups(groups_size, groups));
598#endif /* HAVE_SETGROUPS */
599
600#ifdef HAVE_SETREGID
601 if (call_setgid)
602 POSIX_CALL(setregid(gid, gid));
603#endif /* HAVE_SETREGID */
604
605#ifdef HAVE_SETREUID
606 if (call_setuid)
607 POSIX_CALL(setreuid(uid, uid));
608#endif /* HAVE_SETREUID */
609
610
Gregory P. Smith5591b022012-10-10 03:34:47 -0700611 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000612 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
613 /* This is where the user has asked us to deadlock their program. */
614 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
615 if (result == NULL) {
616 /* Stringifying the exception or traceback would involve
617 * memory allocation and thus potential for deadlock.
618 * We've already faced potential deadlock by calling back
619 * into Python in the first place, so it probably doesn't
620 * matter but we avoid it to minimize the possibility. */
621 err_msg = "Exception occurred in preexec_fn.";
622 errno = 0; /* We don't want to report an OSError. */
623 goto error;
624 }
625 /* Py_DECREF(result); - We're about to exec so why bother? */
626 }
627
Charles-François Natali249cdc32013-08-25 18:24:45 +0200628 /* close FDs after executing preexec_fn, which might open FDs */
629 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200630 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700631 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200632 }
633
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000634 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
635 /* given the executable_list generated by Lib/subprocess.py. */
636 saved_errno = 0;
637 for (i = 0; exec_array[i] != NULL; ++i) {
638 const char *executable = exec_array[i];
639 if (envp) {
640 execve(executable, argv, envp);
641 } else {
642 execv(executable, argv);
643 }
644 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
645 saved_errno = errno;
646 }
647 }
648 /* Report the first exec error, not the last. */
649 if (saved_errno)
650 errno = saved_errno;
651
652error:
653 saved_errno = errno;
654 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800655 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200656 less than PIPEBUF and we cannot do anything about an error anyways.
657 Use _Py_write_noraise() to retry write() if it is interrupted by a
658 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000659 if (saved_errno) {
660 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200661 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000662 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300663 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000664 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000665 saved_errno /= 16;
666 }
Victor Stinner185fd332015-04-01 18:35:53 +0200667 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
668 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700669 if (!reached_preexec) {
670 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200671 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700672 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000673 /* We can't call strerror(saved_errno). It is not async signal safe.
674 * The parent process will look the error message up. */
675 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200676 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
677 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000678 }
679}
680
681
Alexey Izbyshev976da902020-10-24 03:47:01 +0300682/* The main purpose of this wrapper function is to isolate vfork() from both
683 * subprocess_fork_exec() and child_exec(). A child process created via
684 * vfork() executes on the same stack as the parent process while the latter is
685 * suspended, so this function should not be inlined to avoid compiler bugs
686 * that might clobber data needed by the parent later. Additionally,
687 * child_exec() should not be inlined to avoid spurious -Wclobber warnings from
688 * GCC (see bpo-35823).
689 */
690_Py_NO_INLINE static pid_t
691do_fork_exec(char *const exec_array[],
692 char *const argv[],
693 char *const envp[],
694 const char *cwd,
695 int p2cread, int p2cwrite,
696 int c2pread, int c2pwrite,
697 int errread, int errwrite,
698 int errpipe_read, int errpipe_write,
699 int close_fds, int restore_signals,
700 int call_setsid,
701 int call_setgid, gid_t gid,
702 int call_setgroups, size_t groups_size, const gid_t *groups,
703 int call_setuid, uid_t uid, int child_umask,
704 const void *child_sigmask,
705 PyObject *py_fds_to_keep,
706 PyObject *preexec_fn,
707 PyObject *preexec_fn_args_tuple)
708{
709
710 pid_t pid;
711
712#ifdef VFORK_USABLE
713 if (child_sigmask) {
714 /* These are checked by our caller; verify them in debug builds. */
715 assert(!call_setsid);
716 assert(!call_setuid);
717 assert(!call_setgid);
718 assert(!call_setgroups);
719 assert(preexec_fn == Py_None);
720
721 pid = vfork();
722 } else
723#endif
724 {
725 pid = fork();
726 }
727
728 if (pid != 0) {
729 return pid;
730 }
731
732 /* Child process.
733 * See the comment above child_exec() for restrictions imposed on
734 * the code below.
735 */
736
737 if (preexec_fn != Py_None) {
738 /* We'll be calling back into Python later so we need to do this.
739 * This call may not be async-signal-safe but neither is calling
740 * back into Python. The user asked us to use hope as a strategy
741 * to avoid deadlock... */
742 PyOS_AfterFork_Child();
743 }
744
745 child_exec(exec_array, argv, envp, cwd,
746 p2cread, p2cwrite, c2pread, c2pwrite,
747 errread, errwrite, errpipe_read, errpipe_write,
748 close_fds, restore_signals, call_setsid,
749 call_setgid, gid, call_setgroups, groups_size, groups,
750 call_setuid, uid, child_umask, child_sigmask,
751 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
752 _exit(255);
753 return 0; /* Dead code to avoid a potential compiler warning. */
754}
755
756
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000757static PyObject *
758subprocess_fork_exec(PyObject* self, PyObject *args)
759{
760 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200761 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000762 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000763 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000764 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700765 PyObject *groups_list;
766 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000767 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
768 int errpipe_read, errpipe_write, close_fds, restore_signals;
769 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700770 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
771 uid_t uid;
772 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700773 int child_umask;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000774 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000775 const char *cwd;
776 pid_t pid;
777 int need_to_reenable_gc = 0;
778 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700779 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200780 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700781 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000782
783 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700784 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300785 &process_args, &executable_list,
786 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000787 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000788 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
789 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700790 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700791 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700792 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000793 return NULL;
794
Christian Heimes98d90f72019-08-27 23:36:56 +0200795 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100796 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200797 PyErr_SetString(PyExc_RuntimeError,
798 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700799 return NULL;
800 }
801
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800802 if (close_fds && errpipe_write < 3) { /* precondition */
803 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
804 return NULL;
805 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800806 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
807 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000808 return NULL;
809 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000810
Victor Stinner252346a2020-05-01 11:33:44 +0200811 PyInterpreterState *interp = PyInterpreterState_Get();
812 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
813 if (config->_isolated_interpreter) {
814 PyErr_SetString(PyExc_RuntimeError,
815 "subprocess not supported for isolated subinterpreters");
816 return NULL;
817 }
818
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000819 /* We need to call gc.disable() when we'll be calling preexec_fn */
820 if (preexec_fn != Py_None) {
821 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200822
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000823 gc_module = PyImport_ImportModule("gc");
824 if (gc_module == NULL)
825 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100826 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100827 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000828 if (result == NULL) {
829 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000830 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000831 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000832 need_to_reenable_gc = PyObject_IsTrue(result);
833 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000834 if (need_to_reenable_gc == -1) {
835 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000836 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000837 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100838 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100839 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000840 if (result == NULL) {
841 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000842 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000843 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000844 Py_DECREF(result);
845 }
846
847 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200848 if (!exec_array)
849 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000850
851 /* Convert args and env into appropriate arguments for exec() */
852 /* These conversions are done in the parent process to avoid allocating
853 or freeing memory in the child process. */
854 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000855 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000856 /* Equivalent to: */
857 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000858 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200859 if (fast_args == NULL)
860 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000861 num_args = PySequence_Fast_GET_SIZE(fast_args);
862 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000863 if (converted_args == NULL)
864 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000865 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000866 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300867 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
868 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
869 goto cleanup;
870 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000871 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000872 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
873 goto cleanup;
874 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
875 }
876
877 argv = _PySequence_BytesToCharpArray(converted_args);
878 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000879 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000880 if (!argv)
881 goto cleanup;
882 }
883
884 if (env_list != Py_None) {
885 envp = _PySequence_BytesToCharpArray(env_list);
886 if (!envp)
887 goto cleanup;
888 }
889
Victor Stinner0e59cc32010-04-16 23:49:32 +0000890 if (cwd_obj != Py_None) {
891 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
892 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000893 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000894 } else {
895 cwd = NULL;
896 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000897 }
898
Patrick McLean2b2ead72019-09-12 10:15:44 -0700899 if (groups_list != Py_None) {
900#ifdef HAVE_SETGROUPS
901 Py_ssize_t i;
902 unsigned long gid;
903
904 if (!PyList_Check(groups_list)) {
905 PyErr_SetString(PyExc_TypeError,
906 "setgroups argument must be a list");
907 goto cleanup;
908 }
909 num_groups = PySequence_Size(groups_list);
910
911 if (num_groups < 0)
912 goto cleanup;
913
914 if (num_groups > MAX_GROUPS) {
915 PyErr_SetString(PyExc_ValueError, "too many groups");
916 goto cleanup;
917 }
918
919 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
920 PyErr_SetString(PyExc_MemoryError,
921 "failed to allocate memory for group list");
922 goto cleanup;
923 }
924
925 for (i = 0; i < num_groups; i++) {
926 PyObject *elem;
927 elem = PySequence_GetItem(groups_list, i);
928 if (!elem)
929 goto cleanup;
930 if (!PyLong_Check(elem)) {
931 PyErr_SetString(PyExc_TypeError,
932 "groups must be integers");
933 Py_DECREF(elem);
934 goto cleanup;
935 } else {
936 /* In posixmodule.c UnsignedLong is used as a fallback value
937 * if the value provided does not fit in a Long. Since we are
938 * already doing the bounds checking on the Python side, we
939 * can go directly to an UnsignedLong here. */
940 if (!_Py_Gid_Converter(elem, &gid)) {
941 Py_DECREF(elem);
942 PyErr_SetString(PyExc_ValueError, "invalid group id");
943 goto cleanup;
944 }
945 groups[i] = gid;
946 }
947 Py_DECREF(elem);
948 }
949 call_setgroups = 1;
950
951#else /* HAVE_SETGROUPS */
952 PyErr_BadInternalCall();
953 goto cleanup;
954#endif /* HAVE_SETGROUPS */
955 }
956
957 if (gid_object != Py_None) {
958#ifdef HAVE_SETREGID
959 if (!_Py_Gid_Converter(gid_object, &gid))
960 goto cleanup;
961
962 call_setgid = 1;
963
964#else /* HAVE_SETREGID */
965 PyErr_BadInternalCall();
966 goto cleanup;
967#endif /* HAVE_SETREUID */
968 }
969
970 if (uid_object != Py_None) {
971#ifdef HAVE_SETREUID
972 if (!_Py_Uid_Converter(uid_object, &uid))
973 goto cleanup;
974
975 call_setuid = 1;
976
977#else /* HAVE_SETREUID */
978 PyErr_BadInternalCall();
979 goto cleanup;
980#endif /* HAVE_SETREUID */
981 }
982
Gregory P. Smith163468a2017-05-29 10:03:41 -0700983 /* This must be the last thing done before fork() because we do not
984 * want to call PyOS_BeforeFork() if there is any chance of another
985 * error leading to the cleanup: code without calling fork(). */
986 if (preexec_fn != Py_None) {
987 preexec_fn_args_tuple = PyTuple_New(0);
988 if (!preexec_fn_args_tuple)
989 goto cleanup;
990 PyOS_BeforeFork();
991 need_after_fork = 1;
992 }
993
Alexey Izbyshev976da902020-10-24 03:47:01 +0300994 /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */
995 const void *old_sigmask = NULL;
996#ifdef VFORK_USABLE
997 /* Use vfork() only if it's safe. See the comment above child_exec(). */
998 sigset_t old_sigs;
999 if (preexec_fn == Py_None &&
1000 !call_setuid && !call_setgid && !call_setgroups && !call_setsid) {
1001 /* Block all signals to ensure that no signal handlers are run in the
1002 * child process while it shares memory with us. Note that signals
1003 * used internally by C libraries won't be blocked by
1004 * pthread_sigmask(), but signal handlers installed by C libraries
1005 * normally service only signals originating from *within the process*,
1006 * so it should be sufficient to consider any library function that
1007 * might send such a signal to be vfork-unsafe and do not call it in
1008 * the child.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001009 */
Alexey Izbyshev976da902020-10-24 03:47:01 +03001010 sigset_t all_sigs;
1011 sigfillset(&all_sigs);
Alexey Izbyshev473db472020-10-24 20:47:38 +03001012 if ((saved_errno = pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs))) {
1013 errno = saved_errno;
1014 PyErr_SetFromErrno(PyExc_OSError);
1015 goto cleanup;
1016 }
Alexey Izbyshev976da902020-10-24 03:47:01 +03001017 old_sigmask = &old_sigs;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001018 }
Alexey Izbyshev976da902020-10-24 03:47:01 +03001019#endif
1020
1021 pid = do_fork_exec(exec_array, argv, envp, cwd,
1022 p2cread, p2cwrite, c2pread, c2pwrite,
1023 errread, errwrite, errpipe_read, errpipe_write,
1024 close_fds, restore_signals, call_setsid,
1025 call_setgid, gid, call_setgroups, num_groups, groups,
1026 call_setuid, uid, child_umask, old_sigmask,
1027 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
1028
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001029 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001030 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001031 /* Capture errno for the exception. */
1032 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001033 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001034
Alexey Izbyshev976da902020-10-24 03:47:01 +03001035#ifdef VFORK_USABLE
1036 if (old_sigmask) {
1037 /* vfork() semantics guarantees that the parent is blocked
1038 * until the child performs _exit() or execve(), so it is safe
1039 * to unblock signals once we're here.
1040 * Note that in environments where vfork() is implemented as fork(),
1041 * such as QEMU user-mode emulation, the parent won't be blocked,
1042 * but it won't share the address space with the child,
Alexey Izbyshev473db472020-10-24 20:47:38 +03001043 * so it's still safe to unblock the signals.
1044 *
1045 * We don't handle errors here because this call can't fail
1046 * if valid arguments are given, and because there is no good
1047 * way for the caller to deal with a failure to restore
1048 * the thread signal mask. */
1049 (void) pthread_sigmask(SIG_SETMASK, old_sigmask, NULL);
Alexey Izbyshev976da902020-10-24 03:47:01 +03001050 }
1051#endif
1052
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001053 Py_XDECREF(cwd_obj2);
1054
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001055 if (need_after_fork)
1056 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001057 if (envp)
1058 _Py_FreeCharPArray(envp);
1059 if (argv)
1060 _Py_FreeCharPArray(argv);
1061 _Py_FreeCharPArray(exec_array);
1062
1063 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +00001064 if (_enable_gc(need_to_reenable_gc, gc_module)) {
1065 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001066 }
Christian Heimes0d3350d2020-06-12 18:18:43 +02001067 PyMem_RawFree(groups);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001068 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001069 Py_XDECREF(gc_module);
1070
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001071 if (pid == -1) {
1072 errno = saved_errno;
1073 /* We can't call this above as PyOS_AfterFork_Parent() calls back
1074 * into Python code which would see the unreturned error. */
1075 PyErr_SetFromErrno(PyExc_OSError);
1076 return NULL; /* fork() failed. */
1077 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001078
1079 return PyLong_FromPid(pid);
1080
1081cleanup:
1082 if (envp)
1083 _Py_FreeCharPArray(envp);
1084 if (argv)
1085 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +02001086 if (exec_array)
1087 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -07001088
1089 PyMem_RawFree(groups);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001090 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +00001091 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001092 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +00001093 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001094 Py_XDECREF(gc_module);
1095 return NULL;
1096}
1097
1098
1099PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +00001100"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001101 p2cread, p2cwrite, c2pread, c2pwrite,\n\
1102 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001103 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001104 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001105 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001106\n\
1107Forks a child process, closes parent file descriptors as appropriate in the\n\
1108child and dups the few that are needed before calling exec() in the child\n\
1109process.\n\
1110\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001111If close_fds is true, close file descriptors 3 and higher, except those listed\n\
1112in the sorted tuple pass_fds.\n\
1113\n\
1114The preexec_fn, if supplied, will be called immediately before closing file\n\
1115descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001116WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
1117 It may trigger infrequent, difficult to debug deadlocks.\n\
1118\n\
1119If an error occurs in the child process before the exec, it is\n\
1120serialized and written to the errpipe_write fd per subprocess.py.\n\
1121\n\
1122Returns: the child process's PID.\n\
1123\n\
1124Raises: Only on an error in the parent process.\n\
1125");
1126
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001127/* module level code ********************************************************/
1128
1129PyDoc_STRVAR(module_doc,
1130"A POSIX helper for the subprocess module.");
1131
1132
1133static PyMethodDef module_methods[] = {
1134 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001135 {NULL, NULL} /* sentinel */
1136};
1137
1138
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001139static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +08001140 Py_VISIT(get_posixsubprocess_state(m)->disable);
1141 Py_VISIT(get_posixsubprocess_state(m)->enable);
1142 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001143 return 0;
1144}
1145
1146static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +08001147 Py_CLEAR(get_posixsubprocess_state(m)->disable);
1148 Py_CLEAR(get_posixsubprocess_state(m)->enable);
1149 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001150 return 0;
1151}
1152
1153static void _posixsubprocess_free(void *m) {
1154 _posixsubprocess_clear((PyObject *)m);
1155}
1156
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001157static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -06001158 PyModuleDef_HEAD_INIT,
1159 "_posixsubprocess",
1160 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001161 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -06001162 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001163 NULL,
1164 _posixsubprocess_traverse,
1165 _posixsubprocess_clear,
1166 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001167};
1168
1169PyMODINIT_FUNC
1170PyInit__posixsubprocess(void)
1171{
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001172 PyObject* m;
1173
1174 m = PyState_FindModule(&_posixsubprocessmodule);
1175 if (m != NULL) {
1176 Py_INCREF(m);
1177 return m;
1178 }
1179
1180 m = PyModule_Create(&_posixsubprocessmodule);
1181 if (m == NULL) {
1182 return NULL;
1183 }
1184
Hai Shif707d942020-03-16 21:15:01 +08001185 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1186 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1187 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001188
1189 PyState_AddModule(m, &_posixsubprocessmodule);
1190 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001191}