blob: 1abe2358aa10b7622b4b3ac7c2b8ffba3869ae94 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Kyle Evans79925792020-10-13 15:04:44 -05003#include "pycore_fileutils.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02004#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00006#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00007#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00008#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08009#ifdef HAVE_SYS_TYPES_H
10#include <sys/types.h>
11#endif
Gregory P. Smithf3751ef2019-10-12 13:24:56 -070012#if defined(HAVE_SYS_STAT_H)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080013#include <sys/stat.h>
14#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080015#ifdef HAVE_SYS_SYSCALL_H
16#include <sys/syscall.h>
17#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -070018#if defined(HAVE_SYS_RESOURCE_H)
19#include <sys/resource.h>
20#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080021#ifdef HAVE_DIRENT_H
22#include <dirent.h>
23#endif
Patrick McLean2b2ead72019-09-12 10:15:44 -070024#ifdef HAVE_GRP_H
25#include <grp.h>
26#endif /* HAVE_GRP_H */
27
28#include "posixmodule.h"
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000029
Gregory P. Smith3015fb82018-11-12 22:01:22 -080030#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -080031# include <sanitizer/msan_interface.h>
32#endif
33
Xavier de Gayec716f182016-06-15 11:35:29 +020034#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070035# include <sys/linux-syscalls.h>
36# define SYS_getdents64 __NR_getdents64
37#endif
38
Alexey Izbyshev976da902020-10-24 03:47:01 +030039#if defined(__linux__) && defined(HAVE_VFORK) && defined(HAVE_SIGNAL_H) && \
40 defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
Gregory P. Smithbe3c3a02020-10-24 12:07:35 -070041/* If this is ever expanded to non-Linux platforms, verify what calls are
42 * allowed after vfork(). Ex: setsid() may be disallowed on macOS? */
Alexey Izbyshev976da902020-10-24 03:47:01 +030043# include <signal.h>
44# define VFORK_USABLE 1
45#endif
46
Jakub Kulík6f9bc722018-12-31 03:16:40 +010047#if defined(__sun) && defined(__SVR4)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080048/* readdir64 is used to work around Solaris 9 bug 6395699. */
49# define readdir readdir64
50# define dirent dirent64
51# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080052/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080053# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080054# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080055# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080056#endif
57
David CARLIER13b865f2020-11-19 07:24:15 +000058#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__DragonFly__)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080059# define FD_DIR "/dev/fd"
60#else
61# define FD_DIR "/proc/self/fd"
62#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000063
Patrick McLean2b2ead72019-09-12 10:15:44 -070064#ifdef NGROUPS_MAX
65#define MAX_GROUPS NGROUPS_MAX
66#else
67#define MAX_GROUPS 64
68#endif
69
Victor Stinnerdaf45552013-08-28 00:53:59 +020070#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000071
Dino Viehland5a7d2e12019-09-10 12:01:20 +010072typedef struct {
73 PyObject* disable;
74 PyObject* enable;
75 PyObject* isenabled;
76} _posixsubprocessstate;
77
78static struct PyModuleDef _posixsubprocessmodule;
79
Hai Shif707d942020-03-16 21:15:01 +080080static inline _posixsubprocessstate*
81get_posixsubprocess_state(PyObject *module)
82{
83 void *state = PyModule_GetState(module);
84 assert(state != NULL);
85 return (_posixsubprocessstate *)state;
86}
87
88#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000089
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +030090/* If gc was disabled, call gc.enable(). Ignore errors. */
91static void
Martin Panterafdd5132015-11-30 02:21:41 +000092_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000093{
94 PyObject *result;
Martin Panterafdd5132015-11-30 02:21:41 +000095 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020096
Martin Panterafdd5132015-11-30 02:21:41 +000097 if (need_to_reenable_gc) {
98 PyErr_Fetch(&exctype, &val, &tb);
Petr Viktorinffd97532020-02-11 17:46:57 +010099 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100100 gc_module, _posixsubprocessstate_global->enable);
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +0300101 if (result == NULL) {
102 /* We might have created a child process at this point, we
103 * we have no good way to handle a failure to reenable GC
104 * and return information about the child process. */
105 PyErr_Print();
106 }
107 Py_XDECREF(result);
Martin Panterafdd5132015-11-30 02:21:41 +0000108 if (exctype != NULL) {
109 PyErr_Restore(exctype, val, tb);
110 }
Martin Panterafdd5132015-11-30 02:21:41 +0000111 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000112}
113
114
Gregory P. Smith8facece2012-01-21 14:01:08 -0800115/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500116static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200117_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800118{
119 int num = 0;
120 while (*name >= '0' && *name <= '9') {
121 num = num * 10 + (*name - '0');
122 ++name;
123 }
124 if (*name)
125 return -1; /* Non digit found, not a number. */
126 return num;
127}
128
129
David CARLIER13b865f2020-11-19 07:24:15 +0000130#if defined(__FreeBSD__) || defined(__DragonFly__)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800131/* When /dev/fd isn't mounted it is often a static directory populated
David CARLIER13b865f2020-11-19 07:24:15 +0000132 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD, OpenBSD and DragonFlyBSD.
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800133 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
134 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
135 * that properly supports /dev/fd.
136 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500137static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +0200138_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800139{
140 struct stat dev_stat;
141 struct stat dev_fd_stat;
142 if (stat("/dev", &dev_stat) != 0)
143 return 0;
144 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200145 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800146 if (dev_stat.st_dev == dev_fd_stat.st_dev)
147 return 0; /* / == /dev == /dev/fd means it is static. #fail */
148 return 1;
149}
150#endif
151
152
Gregory P. Smith8facece2012-01-21 14:01:08 -0800153/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500154static int
155_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800156{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300157 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800158 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300159 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
160 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
161 long iter_fd;
162 if (!PyLong_Check(py_fd)) {
163 return 1;
164 }
165 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800166 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300167 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800168 return 1;
169 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800170 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800171 }
172 return 0;
173}
174
175
176/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500177static int
178_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800179{
180 /* Binary search. */
181 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300182 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800183 if (search_max < 0)
184 return 0;
185 do {
186 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300187 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800188 if (fd == middle_fd)
189 return 1;
190 if (fd > middle_fd)
191 search_min = middle + 1;
192 else
193 search_max = middle - 1;
194 } while (search_min <= search_max);
195 return 0;
196}
197
Victor Stinnerdaf45552013-08-28 00:53:59 +0200198static int
199make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
200{
201 Py_ssize_t i, len;
202
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300203 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200204 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300205 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200206 long fd = PyLong_AsLong(fdobj);
207 assert(!PyErr_Occurred());
208 assert(0 <= fd && fd <= INT_MAX);
209 if (fd == errpipe_write) {
210 /* errpipe_write is part of py_fds_to_keep. It must be closed at
211 exec(), but kept open in the child process until exec() is
212 called. */
213 continue;
214 }
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300215 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200216 return -1;
217 }
218 return 0;
219}
220
Gregory P. Smith8facece2012-01-21 14:01:08 -0800221
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700222/* Get the maximum file descriptor that could be opened by this process.
223 * This function is async signal safe for use between fork() and exec().
224 */
225static long
226safe_get_max_fd(void)
227{
228 long local_max_fd;
229#if defined(__NetBSD__)
230 local_max_fd = fcntl(0, F_MAXFD);
231 if (local_max_fd >= 0)
232 return local_max_fd;
233#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700234#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
235 struct rlimit rl;
236 /* Not on the POSIX async signal safe functions list but likely
237 * safe. TODO - Someone should audit OpenBSD to make sure. */
238 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
239 return (long) rl.rlim_max;
240#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700241#ifdef _SC_OPEN_MAX
242 local_max_fd = sysconf(_SC_OPEN_MAX);
243 if (local_max_fd == -1)
244#endif
245 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
246 return local_max_fd;
247}
248
249
250/* Close all file descriptors in the range from start_fd and higher
251 * except for those in py_fds_to_keep. If the range defined by
252 * [start_fd, safe_get_max_fd()) is large this will take a long
253 * time as it calls close() on EVERY possible fd.
254 *
255 * It isn't possible to know for sure what the max fd to go up to
256 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800257 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500258static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700259_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800260{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700261 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300262 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800263 Py_ssize_t keep_seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800264 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600265 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800266 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300267 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800268 int keep_fd = PyLong_AsLong(py_keep_fd);
269 if (keep_fd < start_fd)
270 continue;
Kyle Evansc230fde2020-10-11 13:54:11 -0500271 _Py_closerange(start_fd, keep_fd - 1);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800272 start_fd = keep_fd + 1;
273 }
274 if (start_fd <= end_fd) {
Kyle Evansc230fde2020-10-11 13:54:11 -0500275 _Py_closerange(start_fd, end_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800276 }
277}
278
279
280#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
281/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
282 * only to read a directory of short file descriptor number names. The kernel
283 * will return an error if we didn't give it enough space. Highly Unlikely.
284 * This structure is very old and stable: It will not change unless the kernel
285 * chooses to break compatibility with all existing binaries. Highly Unlikely.
286 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800287struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700288 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800289 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800290 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800291 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800292 char d_name[256]; /* Filename (null-terminated) */
293};
294
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700295/* Close all open file descriptors in the range from start_fd and higher
296 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297 *
298 * This version is async signal safe as it does not make any unsafe C library
299 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
300 * to resort to making a kernel system call directly but this is the ONLY api
301 * available that does no harm. opendir/readdir/closedir perform memory
302 * allocation and locking so while they usually work they are not guaranteed
303 * to (especially if you have replaced your malloc implementation). A version
304 * of this function that uses those can be found in the _maybe_unsafe variant.
305 *
306 * This is Linux specific because that is all I am ready to test it on. It
307 * should be easy to add OS specific dirent or dirent64 structures and modify
308 * it with some cpp #define magic to work on other OSes as well if you want.
309 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500310static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700311_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312{
313 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200314
Victor Stinner160e8192015-03-30 02:18:31 +0200315 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800316 if (fd_dir_fd == -1) {
317 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700318 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800319 return;
320 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800321 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800322 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800323 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
324 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800325 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800326 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800327 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800328#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800329 __msan_unpoison(buffer, bytes);
330#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800331 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
332 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800333 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800334 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
335 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700336 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800337 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200338 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800339 }
340 }
341 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200342 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800343 }
344}
345
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700346#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800347
348#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
349
350
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700351/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300352 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800353 *
354 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800355 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800356 * likely to ever cause a problem is opendir() as it performs an internal
357 * malloc(). Practically this should not be a problem. The Java VM makes the
358 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
359 *
360 * readdir_r() is not used because it provides no benefit. It is typically
361 * implemented as readdir() followed by memcpy(). See also:
362 * http://womble.decadent.org.uk/readdir_r-advisory.html
363 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500364static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700365_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800366{
367 DIR *proc_fd_dir;
368#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700369 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800370 ++start_fd;
371 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800372 /* Close our lowest fd before we call opendir so that it is likely to
373 * reuse that fd otherwise we might close opendir's file descriptor in
374 * our loop. This trick assumes that fd's are allocated on a lowest
375 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200376 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800377 ++start_fd;
378#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800379
David CARLIER13b865f2020-11-19 07:24:15 +0000380#if defined(__FreeBSD__) || defined(__DragonFly__)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800381 if (!_is_fdescfs_mounted_on_dev_fd())
382 proc_fd_dir = NULL;
383 else
384#endif
385 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800386 if (!proc_fd_dir) {
387 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700388 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800389 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800390 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800391#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800392 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800393#else
394 int fd_used_by_opendir = start_fd - 1;
395#endif
396 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800397 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800398 int fd;
399 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
400 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700401 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800402 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200403 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800404 }
405 errno = 0;
406 }
407 if (errno) {
408 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700409 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800410 }
411 closedir(proc_fd_dir);
412 }
413}
414
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700415#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800416
417#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
418
419
Alexey Izbyshev976da902020-10-24 03:47:01 +0300420#ifdef VFORK_USABLE
421/* Reset dispositions for all signals to SIG_DFL except for ignored
422 * signals. This way we ensure that no signal handlers can run
423 * after we unblock signals in a child created by vfork().
424 */
425static void
426reset_signal_handlers(const sigset_t *child_sigmask)
427{
428 struct sigaction sa_dfl = {.sa_handler = SIG_DFL};
429 for (int sig = 1; sig < _NSIG; sig++) {
430 /* Dispositions for SIGKILL and SIGSTOP can't be changed. */
431 if (sig == SIGKILL || sig == SIGSTOP) {
432 continue;
433 }
434
435 /* There is no need to reset the disposition of signals that will
436 * remain blocked across execve() since the kernel will do it. */
437 if (sigismember(child_sigmask, sig) == 1) {
438 continue;
439 }
440
441 struct sigaction sa;
442 /* C libraries usually return EINVAL for signals used
443 * internally (e.g. for thread cancellation), so simply
444 * skip errors here. */
445 if (sigaction(sig, NULL, &sa) == -1) {
446 continue;
447 }
448
449 /* void *h works as these fields are both pointer types already. */
450 void *h = (sa.sa_flags & SA_SIGINFO ? (void *)sa.sa_sigaction :
451 (void *)sa.sa_handler);
452 if (h == SIG_IGN || h == SIG_DFL) {
453 continue;
454 }
455
456 /* This call can't reasonably fail, but if it does, terminating
457 * the child seems to be too harsh, so ignore errors. */
458 (void) sigaction(sig, &sa_dfl, NULL);
459 }
460}
461#endif /* VFORK_USABLE */
462
463
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000464/*
Alexey Izbyshev976da902020-10-24 03:47:01 +0300465 * This function is code executed in the child process immediately after
466 * (v)fork to set things up and call exec().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000467 *
468 * All of the code in this function must only use async-signal-safe functions,
469 * listed at `man 7 signal` or
470 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
471 *
472 * This restriction is documented at
473 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
Alexey Izbyshev976da902020-10-24 03:47:01 +0300474 *
475 * If this function is called after vfork(), even more care must be taken.
476 * The lack of preparations that C libraries normally take on fork(),
477 * as well as sharing the address space with the parent, might make even
478 * async-signal-safe functions vfork-unsafe. In particular, on Linux,
479 * set*id() and setgroups() library functions must not be called, since
480 * they have to interact with the library-level thread list and send
481 * library-internal signals to implement per-process credentials semantics
482 * required by POSIX but not supported natively on Linux. Another reason to
483 * avoid this family of functions is that sharing an address space between
484 * processes running with different privileges is inherently insecure.
485 * See bpo-35823 for further discussion and references.
486 *
487 * In some C libraries, setrlimit() has the same thread list/signalling
488 * behavior since resource limits were per-thread attributes before
489 * Linux 2.6.10. Musl, as of 1.2.1, is known to have this issue
490 * (https://www.openwall.com/lists/musl/2020/10/15/6).
491 *
492 * If vfork-unsafe functionality is desired after vfork(), consider using
493 * syscall() to obtain it.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000494 */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300495_Py_NO_INLINE static void
Benjamin Peterson91eef982012-01-22 20:04:46 -0500496child_exec(char *const exec_array[],
497 char *const argv[],
498 char *const envp[],
499 const char *cwd,
500 int p2cread, int p2cwrite,
501 int c2pread, int c2pwrite,
502 int errread, int errwrite,
503 int errpipe_read, int errpipe_write,
504 int close_fds, int restore_signals,
505 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700506 int call_setgid, gid_t gid,
507 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700508 int call_setuid, uid_t uid, int child_umask,
Alexey Izbyshev976da902020-10-24 03:47:01 +0300509 const void *child_sigmask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500510 PyObject *py_fds_to_keep,
511 PyObject *preexec_fn,
512 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000513{
Victor Stinner185fd332015-04-01 18:35:53 +0200514 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000515 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000516 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000517 /* Buffer large enough to hold a hex integer. We can't malloc. */
518 char hex_errno[sizeof(saved_errno)*2+1];
519
Victor Stinnerdaf45552013-08-28 00:53:59 +0200520 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
521 goto error;
522
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200524 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000525 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200526 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000527 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200528 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000529 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000530 POSIX_CALL(close(errpipe_read));
531
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200532 /* When duping fds, if there arises a situation where one of the fds is
533 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700534 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200535 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700536 /* issue32270 */
537 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
538 goto error;
539 }
540 }
541 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200542 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700543 /* issue32270 */
544 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
545 goto error;
546 }
547 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200548
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000549 /* Dup fds for child.
550 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
551 would be a no-op (issue #10806). */
552 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300553 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200554 goto error;
555 }
556 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000557 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200558
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000559 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300560 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200561 goto error;
562 }
563 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000564 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200565
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000566 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300567 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200568 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000569 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200570 else if (errwrite != -1)
571 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000572
Gregory P. Smithce344102018-09-10 17:46:22 -0700573 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
574 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000575
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000576 if (cwd)
577 POSIX_CALL(chdir(cwd));
578
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700579 if (child_umask >= 0)
580 umask(child_umask); /* umask() always succeeds. */
581
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000582 if (restore_signals)
583 _Py_RestoreSignals();
584
Alexey Izbyshev976da902020-10-24 03:47:01 +0300585#ifdef VFORK_USABLE
586 if (child_sigmask) {
587 reset_signal_handlers(child_sigmask);
Alexey Izbyshev473db472020-10-24 20:47:38 +0300588 if ((errno = pthread_sigmask(SIG_SETMASK, child_sigmask, NULL))) {
589 goto error;
590 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300591 }
592#endif
593
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000594#ifdef HAVE_SETSID
595 if (call_setsid)
596 POSIX_CALL(setsid());
597#endif
598
Patrick McLean2b2ead72019-09-12 10:15:44 -0700599#ifdef HAVE_SETGROUPS
600 if (call_setgroups)
601 POSIX_CALL(setgroups(groups_size, groups));
602#endif /* HAVE_SETGROUPS */
603
604#ifdef HAVE_SETREGID
605 if (call_setgid)
606 POSIX_CALL(setregid(gid, gid));
607#endif /* HAVE_SETREGID */
608
609#ifdef HAVE_SETREUID
610 if (call_setuid)
611 POSIX_CALL(setreuid(uid, uid));
612#endif /* HAVE_SETREUID */
613
614
Gregory P. Smith5591b022012-10-10 03:34:47 -0700615 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000616 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
617 /* This is where the user has asked us to deadlock their program. */
618 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
619 if (result == NULL) {
620 /* Stringifying the exception or traceback would involve
621 * memory allocation and thus potential for deadlock.
622 * We've already faced potential deadlock by calling back
623 * into Python in the first place, so it probably doesn't
624 * matter but we avoid it to minimize the possibility. */
625 err_msg = "Exception occurred in preexec_fn.";
626 errno = 0; /* We don't want to report an OSError. */
627 goto error;
628 }
629 /* Py_DECREF(result); - We're about to exec so why bother? */
630 }
631
Charles-François Natali249cdc32013-08-25 18:24:45 +0200632 /* close FDs after executing preexec_fn, which might open FDs */
633 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200634 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700635 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200636 }
637
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000638 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
639 /* given the executable_list generated by Lib/subprocess.py. */
640 saved_errno = 0;
641 for (i = 0; exec_array[i] != NULL; ++i) {
642 const char *executable = exec_array[i];
643 if (envp) {
644 execve(executable, argv, envp);
645 } else {
646 execv(executable, argv);
647 }
648 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
649 saved_errno = errno;
650 }
651 }
652 /* Report the first exec error, not the last. */
653 if (saved_errno)
654 errno = saved_errno;
655
656error:
657 saved_errno = errno;
658 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800659 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200660 less than PIPEBUF and we cannot do anything about an error anyways.
661 Use _Py_write_noraise() to retry write() if it is interrupted by a
662 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000663 if (saved_errno) {
664 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200665 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000666 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300667 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000668 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000669 saved_errno /= 16;
670 }
Victor Stinner185fd332015-04-01 18:35:53 +0200671 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
672 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700673 if (!reached_preexec) {
674 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200675 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700676 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000677 /* We can't call strerror(saved_errno). It is not async signal safe.
678 * The parent process will look the error message up. */
679 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200680 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
681 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000682 }
683}
684
685
Alexey Izbyshev976da902020-10-24 03:47:01 +0300686/* The main purpose of this wrapper function is to isolate vfork() from both
687 * subprocess_fork_exec() and child_exec(). A child process created via
688 * vfork() executes on the same stack as the parent process while the latter is
689 * suspended, so this function should not be inlined to avoid compiler bugs
690 * that might clobber data needed by the parent later. Additionally,
691 * child_exec() should not be inlined to avoid spurious -Wclobber warnings from
692 * GCC (see bpo-35823).
693 */
694_Py_NO_INLINE static pid_t
695do_fork_exec(char *const exec_array[],
696 char *const argv[],
697 char *const envp[],
698 const char *cwd,
699 int p2cread, int p2cwrite,
700 int c2pread, int c2pwrite,
701 int errread, int errwrite,
702 int errpipe_read, int errpipe_write,
703 int close_fds, int restore_signals,
704 int call_setsid,
705 int call_setgid, gid_t gid,
706 int call_setgroups, size_t groups_size, const gid_t *groups,
707 int call_setuid, uid_t uid, int child_umask,
708 const void *child_sigmask,
709 PyObject *py_fds_to_keep,
710 PyObject *preexec_fn,
711 PyObject *preexec_fn_args_tuple)
712{
713
714 pid_t pid;
715
716#ifdef VFORK_USABLE
717 if (child_sigmask) {
718 /* These are checked by our caller; verify them in debug builds. */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300719 assert(!call_setuid);
720 assert(!call_setgid);
721 assert(!call_setgroups);
722 assert(preexec_fn == Py_None);
723
724 pid = vfork();
725 } else
726#endif
727 {
728 pid = fork();
729 }
730
731 if (pid != 0) {
732 return pid;
733 }
734
735 /* Child process.
736 * See the comment above child_exec() for restrictions imposed on
737 * the code below.
738 */
739
740 if (preexec_fn != Py_None) {
741 /* We'll be calling back into Python later so we need to do this.
742 * This call may not be async-signal-safe but neither is calling
743 * back into Python. The user asked us to use hope as a strategy
744 * to avoid deadlock... */
745 PyOS_AfterFork_Child();
746 }
747
748 child_exec(exec_array, argv, envp, cwd,
749 p2cread, p2cwrite, c2pread, c2pwrite,
750 errread, errwrite, errpipe_read, errpipe_write,
751 close_fds, restore_signals, call_setsid,
752 call_setgid, gid, call_setgroups, groups_size, groups,
753 call_setuid, uid, child_umask, child_sigmask,
754 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
755 _exit(255);
756 return 0; /* Dead code to avoid a potential compiler warning. */
757}
758
759
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000760static PyObject *
761subprocess_fork_exec(PyObject* self, PyObject *args)
762{
763 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200764 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000765 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000766 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000767 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700768 PyObject *groups_list;
769 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000770 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
771 int errpipe_read, errpipe_write, close_fds, restore_signals;
772 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700773 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
774 uid_t uid;
775 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700776 int child_umask;
Alexey Izbyshevc0590c02020-10-26 03:09:32 +0300777 PyObject *cwd_obj, *cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000778 const char *cwd;
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +0300779 pid_t pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000780 int need_to_reenable_gc = 0;
781 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700782 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200783 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700784 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000785
786 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700787 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300788 &process_args, &executable_list,
789 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000790 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000791 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
792 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700793 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700794 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700795 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000796 return NULL;
797
Christian Heimes98d90f72019-08-27 23:36:56 +0200798 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100799 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200800 PyErr_SetString(PyExc_RuntimeError,
801 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700802 return NULL;
803 }
804
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800805 if (close_fds && errpipe_write < 3) { /* precondition */
806 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
807 return NULL;
808 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800809 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
810 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000811 return NULL;
812 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000813
Victor Stinner252346a2020-05-01 11:33:44 +0200814 PyInterpreterState *interp = PyInterpreterState_Get();
815 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
816 if (config->_isolated_interpreter) {
817 PyErr_SetString(PyExc_RuntimeError,
818 "subprocess not supported for isolated subinterpreters");
819 return NULL;
820 }
821
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000822 /* We need to call gc.disable() when we'll be calling preexec_fn */
823 if (preexec_fn != Py_None) {
824 PyObject *result;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200825
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000826 gc_module = PyImport_ImportModule("gc");
827 if (gc_module == NULL)
828 return NULL;
Petr Viktorinffd97532020-02-11 17:46:57 +0100829 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100830 gc_module, _posixsubprocessstate_global->isenabled);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000831 if (result == NULL) {
832 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000833 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000834 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000835 need_to_reenable_gc = PyObject_IsTrue(result);
836 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000837 if (need_to_reenable_gc == -1) {
838 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000839 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000840 }
Petr Viktorinffd97532020-02-11 17:46:57 +0100841 result = PyObject_CallMethodNoArgs(
Dino Viehland5a7d2e12019-09-10 12:01:20 +0100842 gc_module, _posixsubprocessstate_global->disable);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000843 if (result == NULL) {
844 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000845 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000846 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000847 Py_DECREF(result);
848 }
849
850 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200851 if (!exec_array)
852 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000853
854 /* Convert args and env into appropriate arguments for exec() */
855 /* These conversions are done in the parent process to avoid allocating
856 or freeing memory in the child process. */
857 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000858 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000859 /* Equivalent to: */
860 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000861 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200862 if (fast_args == NULL)
863 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000864 num_args = PySequence_Fast_GET_SIZE(fast_args);
865 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000866 if (converted_args == NULL)
867 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000868 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000869 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300870 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
871 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
872 goto cleanup;
873 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000874 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000875 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
876 goto cleanup;
877 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
878 }
879
880 argv = _PySequence_BytesToCharpArray(converted_args);
881 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000882 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000883 if (!argv)
884 goto cleanup;
885 }
886
887 if (env_list != Py_None) {
888 envp = _PySequence_BytesToCharpArray(env_list);
889 if (!envp)
890 goto cleanup;
891 }
892
Victor Stinner0e59cc32010-04-16 23:49:32 +0000893 if (cwd_obj != Py_None) {
894 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
895 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000896 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000897 } else {
898 cwd = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000899 }
900
Patrick McLean2b2ead72019-09-12 10:15:44 -0700901 if (groups_list != Py_None) {
902#ifdef HAVE_SETGROUPS
903 Py_ssize_t i;
904 unsigned long gid;
905
906 if (!PyList_Check(groups_list)) {
907 PyErr_SetString(PyExc_TypeError,
908 "setgroups argument must be a list");
909 goto cleanup;
910 }
911 num_groups = PySequence_Size(groups_list);
912
913 if (num_groups < 0)
914 goto cleanup;
915
916 if (num_groups > MAX_GROUPS) {
917 PyErr_SetString(PyExc_ValueError, "too many groups");
918 goto cleanup;
919 }
920
921 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
922 PyErr_SetString(PyExc_MemoryError,
923 "failed to allocate memory for group list");
924 goto cleanup;
925 }
926
927 for (i = 0; i < num_groups; i++) {
928 PyObject *elem;
929 elem = PySequence_GetItem(groups_list, i);
930 if (!elem)
931 goto cleanup;
932 if (!PyLong_Check(elem)) {
933 PyErr_SetString(PyExc_TypeError,
934 "groups must be integers");
935 Py_DECREF(elem);
936 goto cleanup;
937 } else {
938 /* In posixmodule.c UnsignedLong is used as a fallback value
939 * if the value provided does not fit in a Long. Since we are
940 * already doing the bounds checking on the Python side, we
941 * can go directly to an UnsignedLong here. */
942 if (!_Py_Gid_Converter(elem, &gid)) {
943 Py_DECREF(elem);
944 PyErr_SetString(PyExc_ValueError, "invalid group id");
945 goto cleanup;
946 }
947 groups[i] = gid;
948 }
949 Py_DECREF(elem);
950 }
951 call_setgroups = 1;
952
953#else /* HAVE_SETGROUPS */
954 PyErr_BadInternalCall();
955 goto cleanup;
956#endif /* HAVE_SETGROUPS */
957 }
958
959 if (gid_object != Py_None) {
960#ifdef HAVE_SETREGID
961 if (!_Py_Gid_Converter(gid_object, &gid))
962 goto cleanup;
963
964 call_setgid = 1;
965
966#else /* HAVE_SETREGID */
967 PyErr_BadInternalCall();
968 goto cleanup;
969#endif /* HAVE_SETREUID */
970 }
971
972 if (uid_object != Py_None) {
973#ifdef HAVE_SETREUID
974 if (!_Py_Uid_Converter(uid_object, &uid))
975 goto cleanup;
976
977 call_setuid = 1;
978
979#else /* HAVE_SETREUID */
980 PyErr_BadInternalCall();
981 goto cleanup;
982#endif /* HAVE_SETREUID */
983 }
984
Gregory P. Smith163468a2017-05-29 10:03:41 -0700985 /* This must be the last thing done before fork() because we do not
986 * want to call PyOS_BeforeFork() if there is any chance of another
987 * error leading to the cleanup: code without calling fork(). */
988 if (preexec_fn != Py_None) {
989 preexec_fn_args_tuple = PyTuple_New(0);
990 if (!preexec_fn_args_tuple)
991 goto cleanup;
992 PyOS_BeforeFork();
993 need_after_fork = 1;
994 }
995
Alexey Izbyshev976da902020-10-24 03:47:01 +0300996 /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */
997 const void *old_sigmask = NULL;
998#ifdef VFORK_USABLE
999 /* Use vfork() only if it's safe. See the comment above child_exec(). */
1000 sigset_t old_sigs;
1001 if (preexec_fn == Py_None &&
Gregory P. Smithbe3c3a02020-10-24 12:07:35 -07001002 !call_setuid && !call_setgid && !call_setgroups) {
Alexey Izbyshev976da902020-10-24 03:47:01 +03001003 /* Block all signals to ensure that no signal handlers are run in the
1004 * child process while it shares memory with us. Note that signals
1005 * used internally by C libraries won't be blocked by
1006 * pthread_sigmask(), but signal handlers installed by C libraries
1007 * normally service only signals originating from *within the process*,
1008 * so it should be sufficient to consider any library function that
1009 * might send such a signal to be vfork-unsafe and do not call it in
1010 * the child.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001011 */
Alexey Izbyshev976da902020-10-24 03:47:01 +03001012 sigset_t all_sigs;
1013 sigfillset(&all_sigs);
Alexey Izbyshev473db472020-10-24 20:47:38 +03001014 if ((saved_errno = pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs))) {
Alexey Izbyshev473db472020-10-24 20:47:38 +03001015 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
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001053 if (need_after_fork)
1054 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001055
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001056cleanup:
1057 if (saved_errno != 0) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001058 errno = saved_errno;
1059 /* We can't call this above as PyOS_AfterFork_Parent() calls back
1060 * into Python code which would see the unreturned error. */
1061 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -07001062 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001063
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001064 Py_XDECREF(preexec_fn_args_tuple);
1065 PyMem_RawFree(groups);
Alexey Izbyshevc0590c02020-10-26 03:09:32 +03001066 Py_XDECREF(cwd_obj2);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001067 if (envp)
1068 _Py_FreeCharPArray(envp);
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001069 Py_XDECREF(converted_args);
1070 Py_XDECREF(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001071 if (argv)
1072 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +02001073 if (exec_array)
1074 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -07001075
Martin Panterafdd5132015-11-30 02:21:41 +00001076 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001077 Py_XDECREF(gc_module);
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001078
1079 return pid == -1 ? NULL : PyLong_FromPid(pid);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001080}
1081
1082
1083PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +00001084"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001085 p2cread, p2cwrite, c2pread, c2pwrite,\n\
1086 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001087 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001088 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001089 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001090\n\
1091Forks a child process, closes parent file descriptors as appropriate in the\n\
1092child and dups the few that are needed before calling exec() in the child\n\
1093process.\n\
1094\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001095If close_fds is true, close file descriptors 3 and higher, except those listed\n\
1096in the sorted tuple pass_fds.\n\
1097\n\
1098The preexec_fn, if supplied, will be called immediately before closing file\n\
1099descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001100WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
1101 It may trigger infrequent, difficult to debug deadlocks.\n\
1102\n\
1103If an error occurs in the child process before the exec, it is\n\
1104serialized and written to the errpipe_write fd per subprocess.py.\n\
1105\n\
1106Returns: the child process's PID.\n\
1107\n\
1108Raises: Only on an error in the parent process.\n\
1109");
1110
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001111/* module level code ********************************************************/
1112
1113PyDoc_STRVAR(module_doc,
1114"A POSIX helper for the subprocess module.");
1115
1116
1117static PyMethodDef module_methods[] = {
1118 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001119 {NULL, NULL} /* sentinel */
1120};
1121
1122
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001123static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +08001124 Py_VISIT(get_posixsubprocess_state(m)->disable);
1125 Py_VISIT(get_posixsubprocess_state(m)->enable);
1126 Py_VISIT(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001127 return 0;
1128}
1129
1130static int _posixsubprocess_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +08001131 Py_CLEAR(get_posixsubprocess_state(m)->disable);
1132 Py_CLEAR(get_posixsubprocess_state(m)->enable);
1133 Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001134 return 0;
1135}
1136
1137static void _posixsubprocess_free(void *m) {
1138 _posixsubprocess_clear((PyObject *)m);
1139}
1140
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001141static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -06001142 PyModuleDef_HEAD_INIT,
1143 "_posixsubprocess",
1144 module_doc,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001145 sizeof(_posixsubprocessstate),
luzpaza5293b42017-11-05 07:37:50 -06001146 module_methods,
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001147 NULL,
1148 _posixsubprocess_traverse,
1149 _posixsubprocess_clear,
1150 _posixsubprocess_free,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001151};
1152
1153PyMODINIT_FUNC
1154PyInit__posixsubprocess(void)
1155{
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001156 PyObject* m;
1157
1158 m = PyState_FindModule(&_posixsubprocessmodule);
1159 if (m != NULL) {
1160 Py_INCREF(m);
1161 return m;
1162 }
1163
1164 m = PyModule_Create(&_posixsubprocessmodule);
1165 if (m == NULL) {
1166 return NULL;
1167 }
1168
Hai Shif707d942020-03-16 21:15:01 +08001169 get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1170 get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1171 get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
Dino Viehland5a7d2e12019-09-10 12:01:20 +01001172
1173 PyState_AddModule(m, &_posixsubprocessmodule);
1174 return m;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001175}