blob: ed498572a828b7af5f67b3a1f9f849d7f6c0ee95 [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);
584 POSIX_CALL(pthread_sigmask(SIG_SETMASK, child_sigmask, NULL));
585 }
586#endif
587
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000588#ifdef HAVE_SETSID
589 if (call_setsid)
590 POSIX_CALL(setsid());
591#endif
592
Patrick McLean2b2ead72019-09-12 10:15:44 -0700593#ifdef HAVE_SETGROUPS
594 if (call_setgroups)
595 POSIX_CALL(setgroups(groups_size, groups));
596#endif /* HAVE_SETGROUPS */
597
598#ifdef HAVE_SETREGID
599 if (call_setgid)
600 POSIX_CALL(setregid(gid, gid));
601#endif /* HAVE_SETREGID */
602
603#ifdef HAVE_SETREUID
604 if (call_setuid)
605 POSIX_CALL(setreuid(uid, uid));
606#endif /* HAVE_SETREUID */
607
608
Gregory P. Smith5591b022012-10-10 03:34:47 -0700609 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
611 /* This is where the user has asked us to deadlock their program. */
612 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
613 if (result == NULL) {
614 /* Stringifying the exception or traceback would involve
615 * memory allocation and thus potential for deadlock.
616 * We've already faced potential deadlock by calling back
617 * into Python in the first place, so it probably doesn't
618 * matter but we avoid it to minimize the possibility. */
619 err_msg = "Exception occurred in preexec_fn.";
620 errno = 0; /* We don't want to report an OSError. */
621 goto error;
622 }
623 /* Py_DECREF(result); - We're about to exec so why bother? */
624 }
625
Charles-François Natali249cdc32013-08-25 18:24:45 +0200626 /* close FDs after executing preexec_fn, which might open FDs */
627 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200628 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700629 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200630 }
631
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000632 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
633 /* given the executable_list generated by Lib/subprocess.py. */
634 saved_errno = 0;
635 for (i = 0; exec_array[i] != NULL; ++i) {
636 const char *executable = exec_array[i];
637 if (envp) {
638 execve(executable, argv, envp);
639 } else {
640 execv(executable, argv);
641 }
642 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
643 saved_errno = errno;
644 }
645 }
646 /* Report the first exec error, not the last. */
647 if (saved_errno)
648 errno = saved_errno;
649
650error:
651 saved_errno = errno;
652 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800653 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200654 less than PIPEBUF and we cannot do anything about an error anyways.
655 Use _Py_write_noraise() to retry write() if it is interrupted by a
656 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000657 if (saved_errno) {
658 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200659 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000660 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300661 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000662 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000663 saved_errno /= 16;
664 }
Victor Stinner185fd332015-04-01 18:35:53 +0200665 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
666 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700667 if (!reached_preexec) {
668 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200669 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700670 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000671 /* We can't call strerror(saved_errno). It is not async signal safe.
672 * The parent process will look the error message up. */
673 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200674 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
675 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000676 }
677}
678
679
Alexey Izbyshev976da902020-10-24 03:47:01 +0300680/* The main purpose of this wrapper function is to isolate vfork() from both
681 * subprocess_fork_exec() and child_exec(). A child process created via
682 * vfork() executes on the same stack as the parent process while the latter is
683 * suspended, so this function should not be inlined to avoid compiler bugs
684 * that might clobber data needed by the parent later. Additionally,
685 * child_exec() should not be inlined to avoid spurious -Wclobber warnings from
686 * GCC (see bpo-35823).
687 */
688_Py_NO_INLINE static pid_t
689do_fork_exec(char *const exec_array[],
690 char *const argv[],
691 char *const envp[],
692 const char *cwd,
693 int p2cread, int p2cwrite,
694 int c2pread, int c2pwrite,
695 int errread, int errwrite,
696 int errpipe_read, int errpipe_write,
697 int close_fds, int restore_signals,
698 int call_setsid,
699 int call_setgid, gid_t gid,
700 int call_setgroups, size_t groups_size, const gid_t *groups,
701 int call_setuid, uid_t uid, int child_umask,
702 const void *child_sigmask,
703 PyObject *py_fds_to_keep,
704 PyObject *preexec_fn,
705 PyObject *preexec_fn_args_tuple)
706{
707
708 pid_t pid;
709
710#ifdef VFORK_USABLE
711 if (child_sigmask) {
712 /* These are checked by our caller; verify them in debug builds. */
713 assert(!call_setsid);
714 assert(!call_setuid);
715 assert(!call_setgid);
716 assert(!call_setgroups);
717 assert(preexec_fn == Py_None);
718
719 pid = vfork();
720 } else
721#endif
722 {
723 pid = fork();
724 }
725
726 if (pid != 0) {
727 return pid;
728 }
729
730 /* Child process.
731 * See the comment above child_exec() for restrictions imposed on
732 * the code below.
733 */
734
735 if (preexec_fn != Py_None) {
736 /* We'll be calling back into Python later so we need to do this.
737 * This call may not be async-signal-safe but neither is calling
738 * back into Python. The user asked us to use hope as a strategy
739 * to avoid deadlock... */
740 PyOS_AfterFork_Child();
741 }
742
743 child_exec(exec_array, argv, envp, cwd,
744 p2cread, p2cwrite, c2pread, c2pwrite,
745 errread, errwrite, errpipe_read, errpipe_write,
746 close_fds, restore_signals, call_setsid,
747 call_setgid, gid, call_setgroups, groups_size, groups,
748 call_setuid, uid, child_umask, child_sigmask,
749 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
750 _exit(255);
751 return 0; /* Dead code to avoid a potential compiler warning. */
752}
753
754
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000755static PyObject *
756subprocess_fork_exec(PyObject* self, PyObject *args)
757{
758 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200759 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000760 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000761 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000762 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700763 PyObject *groups_list;
764 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000765 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
766 int errpipe_read, errpipe_write, close_fds, restore_signals;
767 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700768 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
769 uid_t uid;
770 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700771 int child_umask;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000772 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000773 const char *cwd;
774 pid_t pid;
775 int need_to_reenable_gc = 0;
776 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700777 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200778 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700779 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000780
781 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700782 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300783 &process_args, &executable_list,
784 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000785 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000786 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
787 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700788 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700789 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700790 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000791 return NULL;
792
Christian Heimes98d90f72019-08-27 23:36:56 +0200793 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100794 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200795 PyErr_SetString(PyExc_RuntimeError,
796 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700797 return NULL;
798 }
799
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800800 if (close_fds && errpipe_write < 3) { /* precondition */
801 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
802 return NULL;
803 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800804 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
805 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000806 return NULL;
807 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000808
Victor Stinner252346a2020-05-01 11:33:44 +0200809 PyInterpreterState *interp = PyInterpreterState_Get();
810 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
811 if (config->_isolated_interpreter) {
812 PyErr_SetString(PyExc_RuntimeError,
813 "subprocess not supported for isolated subinterpreters");
814 return NULL;
815 }
816
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000817 /* We need to call gc.disable() when we'll be calling preexec_fn */
818 if (preexec_fn != Py_None) {
819 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200820
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000821 gc_module = PyImport_ImportModule("gc");
822 if (gc_module == NULL)
823 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100824 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100825 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000826 if (result == NULL) {
827 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000828 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000829 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000830 need_to_reenable_gc = PyObject_IsTrue(result);
831 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000832 if (need_to_reenable_gc == -1) {
833 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000834 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000835 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100836 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100837 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000838 if (result == NULL) {
839 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000840 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000841 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000842 Py_DECREF(result);
843 }
844
845 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200846 if (!exec_array)
847 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000848
849 /* Convert args and env into appropriate arguments for exec() */
850 /* These conversions are done in the parent process to avoid allocating
851 or freeing memory in the child process. */
852 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000853 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000854 /* Equivalent to: */
855 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000856 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200857 if (fast_args == NULL)
858 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000859 num_args = PySequence_Fast_GET_SIZE(fast_args);
860 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000861 if (converted_args == NULL)
862 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000863 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000864 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300865 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
866 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
867 goto cleanup;
868 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000869 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000870 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
871 goto cleanup;
872 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
873 }
874
875 argv = _PySequence_BytesToCharpArray(converted_args);
876 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000877 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000878 if (!argv)
879 goto cleanup;
880 }
881
882 if (env_list != Py_None) {
883 envp = _PySequence_BytesToCharpArray(env_list);
884 if (!envp)
885 goto cleanup;
886 }
887
Victor Stinner0e59cc32010-04-16 23:49:32 +0000888 if (cwd_obj != Py_None) {
889 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
890 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000891 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000892 } else {
893 cwd = NULL;
894 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000895 }
896
Patrick McLean2b2ead72019-09-12 10:15:44 -0700897 if (groups_list != Py_None) {
898#ifdef HAVE_SETGROUPS
899 Py_ssize_t i;
900 unsigned long gid;
901
902 if (!PyList_Check(groups_list)) {
903 PyErr_SetString(PyExc_TypeError,
904 "setgroups argument must be a list");
905 goto cleanup;
906 }
907 num_groups = PySequence_Size(groups_list);
908
909 if (num_groups < 0)
910 goto cleanup;
911
912 if (num_groups > MAX_GROUPS) {
913 PyErr_SetString(PyExc_ValueError, "too many groups");
914 goto cleanup;
915 }
916
917 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
918 PyErr_SetString(PyExc_MemoryError,
919 "failed to allocate memory for group list");
920 goto cleanup;
921 }
922
923 for (i = 0; i < num_groups; i++) {
924 PyObject *elem;
925 elem = PySequence_GetItem(groups_list, i);
926 if (!elem)
927 goto cleanup;
928 if (!PyLong_Check(elem)) {
929 PyErr_SetString(PyExc_TypeError,
930 "groups must be integers");
931 Py_DECREF(elem);
932 goto cleanup;
933 } else {
934 /* In posixmodule.c UnsignedLong is used as a fallback value
935 * if the value provided does not fit in a Long. Since we are
936 * already doing the bounds checking on the Python side, we
937 * can go directly to an UnsignedLong here. */
938 if (!_Py_Gid_Converter(elem, &gid)) {
939 Py_DECREF(elem);
940 PyErr_SetString(PyExc_ValueError, "invalid group id");
941 goto cleanup;
942 }
943 groups[i] = gid;
944 }
945 Py_DECREF(elem);
946 }
947 call_setgroups = 1;
948
949#else /* HAVE_SETGROUPS */
950 PyErr_BadInternalCall();
951 goto cleanup;
952#endif /* HAVE_SETGROUPS */
953 }
954
955 if (gid_object != Py_None) {
956#ifdef HAVE_SETREGID
957 if (!_Py_Gid_Converter(gid_object, &gid))
958 goto cleanup;
959
960 call_setgid = 1;
961
962#else /* HAVE_SETREGID */
963 PyErr_BadInternalCall();
964 goto cleanup;
965#endif /* HAVE_SETREUID */
966 }
967
968 if (uid_object != Py_None) {
969#ifdef HAVE_SETREUID
970 if (!_Py_Uid_Converter(uid_object, &uid))
971 goto cleanup;
972
973 call_setuid = 1;
974
975#else /* HAVE_SETREUID */
976 PyErr_BadInternalCall();
977 goto cleanup;
978#endif /* HAVE_SETREUID */
979 }
980
Gregory P. Smith163468a2017-05-29 10:03:41 -0700981 /* This must be the last thing done before fork() because we do not
982 * want to call PyOS_BeforeFork() if there is any chance of another
983 * error leading to the cleanup: code without calling fork(). */
984 if (preexec_fn != Py_None) {
985 preexec_fn_args_tuple = PyTuple_New(0);
986 if (!preexec_fn_args_tuple)
987 goto cleanup;
988 PyOS_BeforeFork();
989 need_after_fork = 1;
990 }
991
Alexey Izbyshev976da902020-10-24 03:47:01 +0300992 /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */
993 const void *old_sigmask = NULL;
994#ifdef VFORK_USABLE
995 /* Use vfork() only if it's safe. See the comment above child_exec(). */
996 sigset_t old_sigs;
997 if (preexec_fn == Py_None &&
998 !call_setuid && !call_setgid && !call_setgroups && !call_setsid) {
999 /* Block all signals to ensure that no signal handlers are run in the
1000 * child process while it shares memory with us. Note that signals
1001 * used internally by C libraries won't be blocked by
1002 * pthread_sigmask(), but signal handlers installed by C libraries
1003 * normally service only signals originating from *within the process*,
1004 * so it should be sufficient to consider any library function that
1005 * might send such a signal to be vfork-unsafe and do not call it in
1006 * the child.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001007 */
Alexey Izbyshev976da902020-10-24 03:47:01 +03001008 sigset_t all_sigs;
1009 sigfillset(&all_sigs);
1010 pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs);
1011 old_sigmask = &old_sigs;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001012 }
Alexey Izbyshev976da902020-10-24 03:47:01 +03001013#endif
1014
1015 pid = do_fork_exec(exec_array, argv, envp, cwd,
1016 p2cread, p2cwrite, c2pread, c2pwrite,
1017 errread, errwrite, errpipe_read, errpipe_write,
1018 close_fds, restore_signals, call_setsid,
1019 call_setgid, gid, call_setgroups, num_groups, groups,
1020 call_setuid, uid, child_umask, old_sigmask,
1021 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
1022
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001023 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001024 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001025 /* Capture errno for the exception. */
1026 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001027 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001028
Alexey Izbyshev976da902020-10-24 03:47:01 +03001029#ifdef VFORK_USABLE
1030 if (old_sigmask) {
1031 /* vfork() semantics guarantees that the parent is blocked
1032 * until the child performs _exit() or execve(), so it is safe
1033 * to unblock signals once we're here.
1034 * Note that in environments where vfork() is implemented as fork(),
1035 * such as QEMU user-mode emulation, the parent won't be blocked,
1036 * but it won't share the address space with the child,
1037 * so it's still safe to unblock the signals. */
1038 pthread_sigmask(SIG_SETMASK, old_sigmask, NULL);
1039 }
1040#endif
1041
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001042 Py_XDECREF(cwd_obj2);
1043
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001044 if (need_after_fork)
1045 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001046 if (envp)
1047 _Py_FreeCharPArray(envp);
1048 if (argv)
1049 _Py_FreeCharPArray(argv);
1050 _Py_FreeCharPArray(exec_array);
1051
1052 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +00001053 if (_enable_gc(need_to_reenable_gc, gc_module)) {
1054 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001055 }
Christian Heimes0d3350d2020-06-12 18:18:43 +02001056 PyMem_RawFree(groups);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001057 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001058 Py_XDECREF(gc_module);
1059
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001060 if (pid == -1) {
1061 errno = saved_errno;
1062 /* We can't call this above as PyOS_AfterFork_Parent() calls back
1063 * into Python code which would see the unreturned error. */
1064 PyErr_SetFromErrno(PyExc_OSError);
1065 return NULL; /* fork() failed. */
1066 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001067
1068 return PyLong_FromPid(pid);
1069
1070cleanup:
1071 if (envp)
1072 _Py_FreeCharPArray(envp);
1073 if (argv)
1074 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +02001075 if (exec_array)
1076 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -07001077
1078 PyMem_RawFree(groups);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001079 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +00001080 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +00001081 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +00001082 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001083 Py_XDECREF(gc_module);
1084 return NULL;
1085}
1086
1087
1088PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +00001089"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001090 p2cread, p2cwrite, c2pread, c2pwrite,\n\
1091 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001092 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001093 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001094 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001095\n\
1096Forks a child process, closes parent file descriptors as appropriate in the\n\
1097child and dups the few that are needed before calling exec() in the child\n\
1098process.\n\
1099\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001100If close_fds is true, close file descriptors 3 and higher, except those listed\n\
1101in the sorted tuple pass_fds.\n\
1102\n\
1103The preexec_fn, if supplied, will be called immediately before closing file\n\
1104descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001105WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
1106 It may trigger infrequent, difficult to debug deadlocks.\n\
1107\n\
1108If an error occurs in the child process before the exec, it is\n\
1109serialized and written to the errpipe_write fd per subprocess.py.\n\
1110\n\
1111Returns: the child process's PID.\n\
1112\n\
1113Raises: Only on an error in the parent process.\n\
1114");
1115
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001116/* module level code ********************************************************/
1117
1118PyDoc_STRVAR(module_doc,
1119"A POSIX helper for the subprocess module.");
1120
1121
1122static PyMethodDef module_methods[] = {
1123 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001124 {NULL, NULL} /* sentinel */
1125};
1126
1127
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001128static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +08001129 Py_VISIT(get_posixsubprocess_state(m)->disable);
1130 Py_VISIT(get_posixsubprocess_state(m)->enable);
1131 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001132 return 0;
1133}
1134
1135static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +08001136 Py_CLEAR(get_posixsubprocess_state(m)->disable);
1137 Py_CLEAR(get_posixsubprocess_state(m)->enable);
1138 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001139 return 0;
1140}
1141
1142static void _posixsubprocess_free(void *m) {
1143 _posixsubprocess_clear((PyObject *)m);
1144}
1145
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001146static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -06001147 PyModuleDef_HEAD_INIT,
1148 "_posixsubprocess",
1149 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001150 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -06001151 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001152 NULL,
1153 _posixsubprocess_traverse,
1154 _posixsubprocess_clear,
1155 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001156};
1157
1158PyMODINIT_FUNC
1159PyInit__posixsubprocess(void)
1160{
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001161 PyObject* m;
1162
1163 m = PyState_FindModule(&_posixsubprocessmodule);
1164 if (m != NULL) {
1165 Py_INCREF(m);
1166 return m;
1167 }
1168
1169 m = PyModule_Create(&_posixsubprocessmodule);
1170 if (m == NULL) {
1171 return NULL;
1172 }
1173
Hai Shif707d942020-03-16 21:15:01 +08001174 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1175 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1176 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001177
1178 PyState_AddModule(m, &_posixsubprocessmodule);
1179 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001180}