blob: a58159a277bea8fb703aa6a74f1db4502b2e7c7a [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 +010072static struct PyModuleDef _posixsubprocessmodule;
73
Gregory P. Smith8facece2012-01-21 14:01:08 -080074/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050075static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020076_pos_int_from_ascii(const char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080077{
78 int num = 0;
79 while (*name >= '0' && *name <= '9') {
80 num = num * 10 + (*name - '0');
81 ++name;
82 }
83 if (*name)
84 return -1; /* Non digit found, not a number. */
85 return num;
86}
87
88
David CARLIER13b865f2020-11-19 07:24:15 +000089#if defined(__FreeBSD__) || defined(__DragonFly__)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080090/* When /dev/fd isn't mounted it is often a static directory populated
David CARLIER13b865f2020-11-19 07:24:15 +000091 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD, OpenBSD and DragonFlyBSD.
Gregory P. Smith4842efc2012-01-21 21:01:24 -080092 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
93 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
94 * that properly supports /dev/fd.
95 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050096static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020097_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080098{
99 struct stat dev_stat;
100 struct stat dev_fd_stat;
101 if (stat("/dev", &dev_stat) != 0)
102 return 0;
103 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200104 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800105 if (dev_stat.st_dev == dev_fd_stat.st_dev)
106 return 0; /* / == /dev == /dev/fd means it is static. #fail */
107 return 1;
108}
109#endif
110
111
Gregory P. Smith8facece2012-01-21 14:01:08 -0800112/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500113static int
114_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800115{
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300116 Py_ssize_t seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800117 long prev_fd = -1;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300118 for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
119 PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
120 long iter_fd;
121 if (!PyLong_Check(py_fd)) {
122 return 1;
123 }
124 iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800125 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300126 /* Negative, overflow, unsorted, too big for a fd. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800127 return 1;
128 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800129 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800130 }
131 return 0;
132}
133
134
135/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500136static int
137_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800138{
139 /* Binary search. */
140 Py_ssize_t search_min = 0;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300141 Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800142 if (search_max < 0)
143 return 0;
144 do {
145 long middle = (search_min + search_max) / 2;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300146 long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
Gregory P. Smith8facece2012-01-21 14:01:08 -0800147 if (fd == middle_fd)
148 return 1;
149 if (fd > middle_fd)
150 search_min = middle + 1;
151 else
152 search_max = middle - 1;
153 } while (search_min <= search_max);
154 return 0;
155}
156
Victor Stinnerdaf45552013-08-28 00:53:59 +0200157static int
158make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
159{
160 Py_ssize_t i, len;
161
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300162 len = PyTuple_GET_SIZE(py_fds_to_keep);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200163 for (i = 0; i < len; ++i) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300164 PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200165 long fd = PyLong_AsLong(fdobj);
166 assert(!PyErr_Occurred());
167 assert(0 <= fd && fd <= INT_MAX);
168 if (fd == errpipe_write) {
169 /* errpipe_write is part of py_fds_to_keep. It must be closed at
170 exec(), but kept open in the child process until exec() is
171 called. */
172 continue;
173 }
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300174 if (_Py_set_inheritable_async_safe((int)fd, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200175 return -1;
176 }
177 return 0;
178}
179
Gregory P. Smith8facece2012-01-21 14:01:08 -0800180
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700181/* Get the maximum file descriptor that could be opened by this process.
182 * This function is async signal safe for use between fork() and exec().
183 */
184static long
185safe_get_max_fd(void)
186{
187 long local_max_fd;
188#if defined(__NetBSD__)
189 local_max_fd = fcntl(0, F_MAXFD);
190 if (local_max_fd >= 0)
191 return local_max_fd;
192#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700193#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
194 struct rlimit rl;
195 /* Not on the POSIX async signal safe functions list but likely
196 * safe. TODO - Someone should audit OpenBSD to make sure. */
197 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
198 return (long) rl.rlim_max;
199#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700200#ifdef _SC_OPEN_MAX
201 local_max_fd = sysconf(_SC_OPEN_MAX);
202 if (local_max_fd == -1)
203#endif
204 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
205 return local_max_fd;
206}
207
208
209/* Close all file descriptors in the range from start_fd and higher
210 * except for those in py_fds_to_keep. If the range defined by
211 * [start_fd, safe_get_max_fd()) is large this will take a long
212 * time as it calls close() on EVERY possible fd.
213 *
214 * It isn't possible to know for sure what the max fd to go up to
215 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800216 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500217static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700218_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800219{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700220 long end_fd = safe_get_max_fd();
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300221 Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800222 Py_ssize_t keep_seq_idx;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800223 /* As py_fds_to_keep is sorted we can loop through the list closing
luzpaza5293b42017-11-05 07:37:50 -0600224 * fds in between any in the keep list falling within our range. */
Gregory P. Smith8facece2012-01-21 14:01:08 -0800225 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300226 PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800227 int keep_fd = PyLong_AsLong(py_keep_fd);
228 if (keep_fd < start_fd)
229 continue;
Kyle Evansc230fde2020-10-11 13:54:11 -0500230 _Py_closerange(start_fd, keep_fd - 1);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800231 start_fd = keep_fd + 1;
232 }
233 if (start_fd <= end_fd) {
Kyle Evansc230fde2020-10-11 13:54:11 -0500234 _Py_closerange(start_fd, end_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800235 }
236}
237
238
239#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
240/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
241 * only to read a directory of short file descriptor number names. The kernel
242 * will return an error if we didn't give it enough space. Highly Unlikely.
243 * This structure is very old and stable: It will not change unless the kernel
244 * chooses to break compatibility with all existing binaries. Highly Unlikely.
245 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800246struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700247 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800248 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800249 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800250 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800251 char d_name[256]; /* Filename (null-terminated) */
252};
253
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700254/* Close all open file descriptors in the range from start_fd and higher
255 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800256 *
257 * This version is async signal safe as it does not make any unsafe C library
258 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
259 * to resort to making a kernel system call directly but this is the ONLY api
260 * available that does no harm. opendir/readdir/closedir perform memory
261 * allocation and locking so while they usually work they are not guaranteed
262 * to (especially if you have replaced your malloc implementation). A version
263 * of this function that uses those can be found in the _maybe_unsafe variant.
264 *
265 * This is Linux specific because that is all I am ready to test it on. It
266 * should be easy to add OS specific dirent or dirent64 structures and modify
267 * it with some cpp #define magic to work on other OSes as well if you want.
268 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500269static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700270_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800271{
272 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200273
Victor Stinner160e8192015-03-30 02:18:31 +0200274 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800275 if (fd_dir_fd == -1) {
276 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700277 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800278 return;
279 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800280 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800281 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800282 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
283 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800284 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800285 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800286 int offset;
Gregory P. Smith3015fb82018-11-12 22:01:22 -0800287#ifdef _Py_MEMORY_SANITIZER
Gregory P. Smith1584a002018-11-12 12:07:14 -0800288 __msan_unpoison(buffer, bytes);
289#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800290 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
291 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800292 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800293 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
294 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700295 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800296 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200297 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800298 }
299 }
300 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200301 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800302 }
303}
304
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700305#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800306
307#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
308
309
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700310/* Close all open file descriptors from start_fd and higher.
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300311 * Do not close any in the sorted py_fds_to_keep tuple.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800312 *
313 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800314 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800315 * likely to ever cause a problem is opendir() as it performs an internal
316 * malloc(). Practically this should not be a problem. The Java VM makes the
317 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
318 *
319 * readdir_r() is not used because it provides no benefit. It is typically
320 * implemented as readdir() followed by memcpy(). See also:
321 * http://womble.decadent.org.uk/readdir_r-advisory.html
322 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500323static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700324_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800325{
326 DIR *proc_fd_dir;
327#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700328 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800329 ++start_fd;
330 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800331 /* Close our lowest fd before we call opendir so that it is likely to
332 * reuse that fd otherwise we might close opendir's file descriptor in
333 * our loop. This trick assumes that fd's are allocated on a lowest
334 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200335 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800336 ++start_fd;
337#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800338
David CARLIER13b865f2020-11-19 07:24:15 +0000339#if defined(__FreeBSD__) || defined(__DragonFly__)
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800340 if (!_is_fdescfs_mounted_on_dev_fd())
341 proc_fd_dir = NULL;
342 else
343#endif
344 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800345 if (!proc_fd_dir) {
346 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700347 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800348 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800349 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800350#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800351 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800352#else
353 int fd_used_by_opendir = start_fd - 1;
354#endif
355 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800356 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800357 int fd;
358 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
359 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700360 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800361 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200362 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800363 }
364 errno = 0;
365 }
366 if (errno) {
367 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700368 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800369 }
370 closedir(proc_fd_dir);
371 }
372}
373
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700374#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800375
376#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
377
378
Alexey Izbyshev976da902020-10-24 03:47:01 +0300379#ifdef VFORK_USABLE
380/* Reset dispositions for all signals to SIG_DFL except for ignored
381 * signals. This way we ensure that no signal handlers can run
382 * after we unblock signals in a child created by vfork().
383 */
384static void
385reset_signal_handlers(const sigset_t *child_sigmask)
386{
387 struct sigaction sa_dfl = {.sa_handler = SIG_DFL};
388 for (int sig = 1; sig < _NSIG; sig++) {
389 /* Dispositions for SIGKILL and SIGSTOP can't be changed. */
390 if (sig == SIGKILL || sig == SIGSTOP) {
391 continue;
392 }
393
394 /* There is no need to reset the disposition of signals that will
395 * remain blocked across execve() since the kernel will do it. */
396 if (sigismember(child_sigmask, sig) == 1) {
397 continue;
398 }
399
400 struct sigaction sa;
401 /* C libraries usually return EINVAL for signals used
402 * internally (e.g. for thread cancellation), so simply
403 * skip errors here. */
404 if (sigaction(sig, NULL, &sa) == -1) {
405 continue;
406 }
407
408 /* void *h works as these fields are both pointer types already. */
409 void *h = (sa.sa_flags & SA_SIGINFO ? (void *)sa.sa_sigaction :
410 (void *)sa.sa_handler);
411 if (h == SIG_IGN || h == SIG_DFL) {
412 continue;
413 }
414
415 /* This call can't reasonably fail, but if it does, terminating
416 * the child seems to be too harsh, so ignore errors. */
417 (void) sigaction(sig, &sa_dfl, NULL);
418 }
419}
420#endif /* VFORK_USABLE */
421
422
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000423/*
Alexey Izbyshev976da902020-10-24 03:47:01 +0300424 * This function is code executed in the child process immediately after
425 * (v)fork to set things up and call exec().
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000426 *
427 * All of the code in this function must only use async-signal-safe functions,
428 * listed at `man 7 signal` or
429 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
430 *
431 * This restriction is documented at
432 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
Alexey Izbyshev976da902020-10-24 03:47:01 +0300433 *
434 * If this function is called after vfork(), even more care must be taken.
435 * The lack of preparations that C libraries normally take on fork(),
436 * as well as sharing the address space with the parent, might make even
437 * async-signal-safe functions vfork-unsafe. In particular, on Linux,
438 * set*id() and setgroups() library functions must not be called, since
439 * they have to interact with the library-level thread list and send
440 * library-internal signals to implement per-process credentials semantics
441 * required by POSIX but not supported natively on Linux. Another reason to
442 * avoid this family of functions is that sharing an address space between
443 * processes running with different privileges is inherently insecure.
444 * See bpo-35823 for further discussion and references.
445 *
446 * In some C libraries, setrlimit() has the same thread list/signalling
447 * behavior since resource limits were per-thread attributes before
448 * Linux 2.6.10. Musl, as of 1.2.1, is known to have this issue
449 * (https://www.openwall.com/lists/musl/2020/10/15/6).
450 *
451 * If vfork-unsafe functionality is desired after vfork(), consider using
452 * syscall() to obtain it.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000453 */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300454_Py_NO_INLINE static void
Benjamin Peterson91eef982012-01-22 20:04:46 -0500455child_exec(char *const exec_array[],
456 char *const argv[],
457 char *const envp[],
458 const char *cwd,
459 int p2cread, int p2cwrite,
460 int c2pread, int c2pwrite,
461 int errread, int errwrite,
462 int errpipe_read, int errpipe_write,
463 int close_fds, int restore_signals,
464 int call_setsid,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700465 int call_setgid, gid_t gid,
466 int call_setgroups, size_t groups_size, const gid_t *groups,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700467 int call_setuid, uid_t uid, int child_umask,
Alexey Izbyshev976da902020-10-24 03:47:01 +0300468 const void *child_sigmask,
Benjamin Peterson91eef982012-01-22 20:04:46 -0500469 PyObject *py_fds_to_keep,
470 PyObject *preexec_fn,
471 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000472{
Victor Stinner185fd332015-04-01 18:35:53 +0200473 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000474 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000475 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000476 /* Buffer large enough to hold a hex integer. We can't malloc. */
477 char hex_errno[sizeof(saved_errno)*2+1];
478
Victor Stinnerdaf45552013-08-28 00:53:59 +0200479 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
480 goto error;
481
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000482 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200483 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000484 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200485 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000486 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200487 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000488 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000489 POSIX_CALL(close(errpipe_read));
490
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200491 /* When duping fds, if there arises a situation where one of the fds is
492 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
Gregory P. Smithce344102018-09-10 17:46:22 -0700493 if (c2pwrite == 0) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200494 POSIX_CALL(c2pwrite = dup(c2pwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700495 /* issue32270 */
496 if (_Py_set_inheritable_async_safe(c2pwrite, 0, NULL) < 0) {
497 goto error;
498 }
499 }
500 while (errwrite == 0 || errwrite == 1) {
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200501 POSIX_CALL(errwrite = dup(errwrite));
Gregory P. Smithce344102018-09-10 17:46:22 -0700502 /* issue32270 */
503 if (_Py_set_inheritable_async_safe(errwrite, 0, NULL) < 0) {
504 goto error;
505 }
506 }
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200507
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000508 /* Dup fds for child.
509 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
510 would be a no-op (issue #10806). */
511 if (p2cread == 0) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300512 if (_Py_set_inheritable_async_safe(p2cread, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200513 goto error;
514 }
515 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000516 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200517
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000518 if (c2pwrite == 1) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300519 if (_Py_set_inheritable_async_safe(c2pwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200520 goto error;
521 }
522 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000523 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200524
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000525 if (errwrite == 2) {
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300526 if (_Py_set_inheritable_async_safe(errwrite, 1, NULL) < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200527 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000528 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200529 else if (errwrite != -1)
530 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000531
Gregory P. Smithce344102018-09-10 17:46:22 -0700532 /* We no longer manually close p2cread, c2pwrite, and errwrite here as
533 * _close_open_fds takes care when it is not already non-inheritable. */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000534
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000535 if (cwd)
536 POSIX_CALL(chdir(cwd));
537
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700538 if (child_umask >= 0)
539 umask(child_umask); /* umask() always succeeds. */
540
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000541 if (restore_signals)
542 _Py_RestoreSignals();
543
Alexey Izbyshev976da902020-10-24 03:47:01 +0300544#ifdef VFORK_USABLE
545 if (child_sigmask) {
546 reset_signal_handlers(child_sigmask);
Alexey Izbyshev473db472020-10-24 20:47:38 +0300547 if ((errno = pthread_sigmask(SIG_SETMASK, child_sigmask, NULL))) {
548 goto error;
549 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300550 }
551#endif
552
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000553#ifdef HAVE_SETSID
554 if (call_setsid)
555 POSIX_CALL(setsid());
556#endif
557
Patrick McLean2b2ead72019-09-12 10:15:44 -0700558#ifdef HAVE_SETGROUPS
559 if (call_setgroups)
560 POSIX_CALL(setgroups(groups_size, groups));
561#endif /* HAVE_SETGROUPS */
562
563#ifdef HAVE_SETREGID
564 if (call_setgid)
565 POSIX_CALL(setregid(gid, gid));
566#endif /* HAVE_SETREGID */
567
568#ifdef HAVE_SETREUID
569 if (call_setuid)
570 POSIX_CALL(setreuid(uid, uid));
571#endif /* HAVE_SETREUID */
572
573
Gregory P. Smith5591b022012-10-10 03:34:47 -0700574 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000575 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
576 /* This is where the user has asked us to deadlock their program. */
577 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
578 if (result == NULL) {
579 /* Stringifying the exception or traceback would involve
580 * memory allocation and thus potential for deadlock.
581 * We've already faced potential deadlock by calling back
582 * into Python in the first place, so it probably doesn't
583 * matter but we avoid it to minimize the possibility. */
584 err_msg = "Exception occurred in preexec_fn.";
585 errno = 0; /* We don't want to report an OSError. */
586 goto error;
587 }
588 /* Py_DECREF(result); - We're about to exec so why bother? */
589 }
590
Charles-François Natali249cdc32013-08-25 18:24:45 +0200591 /* close FDs after executing preexec_fn, which might open FDs */
592 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200593 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700594 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200595 }
596
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000597 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
598 /* given the executable_list generated by Lib/subprocess.py. */
599 saved_errno = 0;
600 for (i = 0; exec_array[i] != NULL; ++i) {
601 const char *executable = exec_array[i];
602 if (envp) {
603 execve(executable, argv, envp);
604 } else {
605 execv(executable, argv);
606 }
607 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
608 saved_errno = errno;
609 }
610 }
611 /* Report the first exec error, not the last. */
612 if (saved_errno)
613 errno = saved_errno;
614
615error:
616 saved_errno = errno;
617 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800618 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200619 less than PIPEBUF and we cannot do anything about an error anyways.
620 Use _Py_write_noraise() to retry write() if it is interrupted by a
621 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000622 if (saved_errno) {
623 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200624 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000625 cur = hex_errno + sizeof(hex_errno);
Serhiy Storchaka5ae4f492016-09-27 22:03:51 +0300626 while (saved_errno != 0 && cur != hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000627 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000628 saved_errno /= 16;
629 }
Victor Stinner185fd332015-04-01 18:35:53 +0200630 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
631 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700632 if (!reached_preexec) {
633 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200634 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700635 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000636 /* We can't call strerror(saved_errno). It is not async signal safe.
637 * The parent process will look the error message up. */
638 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200639 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
640 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000641 }
642}
643
644
Alexey Izbyshev976da902020-10-24 03:47:01 +0300645/* The main purpose of this wrapper function is to isolate vfork() from both
646 * subprocess_fork_exec() and child_exec(). A child process created via
647 * vfork() executes on the same stack as the parent process while the latter is
648 * suspended, so this function should not be inlined to avoid compiler bugs
649 * that might clobber data needed by the parent later. Additionally,
650 * child_exec() should not be inlined to avoid spurious -Wclobber warnings from
651 * GCC (see bpo-35823).
652 */
653_Py_NO_INLINE static pid_t
654do_fork_exec(char *const exec_array[],
655 char *const argv[],
656 char *const envp[],
657 const char *cwd,
658 int p2cread, int p2cwrite,
659 int c2pread, int c2pwrite,
660 int errread, int errwrite,
661 int errpipe_read, int errpipe_write,
662 int close_fds, int restore_signals,
663 int call_setsid,
664 int call_setgid, gid_t gid,
665 int call_setgroups, size_t groups_size, const gid_t *groups,
666 int call_setuid, uid_t uid, int child_umask,
667 const void *child_sigmask,
668 PyObject *py_fds_to_keep,
669 PyObject *preexec_fn,
670 PyObject *preexec_fn_args_tuple)
671{
672
673 pid_t pid;
674
675#ifdef VFORK_USABLE
676 if (child_sigmask) {
677 /* These are checked by our caller; verify them in debug builds. */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300678 assert(!call_setuid);
679 assert(!call_setgid);
680 assert(!call_setgroups);
681 assert(preexec_fn == Py_None);
682
683 pid = vfork();
684 } else
685#endif
686 {
687 pid = fork();
688 }
689
690 if (pid != 0) {
691 return pid;
692 }
693
694 /* Child process.
695 * See the comment above child_exec() for restrictions imposed on
696 * the code below.
697 */
698
699 if (preexec_fn != Py_None) {
700 /* We'll be calling back into Python later so we need to do this.
701 * This call may not be async-signal-safe but neither is calling
702 * back into Python. The user asked us to use hope as a strategy
703 * to avoid deadlock... */
704 PyOS_AfterFork_Child();
705 }
706
707 child_exec(exec_array, argv, envp, cwd,
708 p2cread, p2cwrite, c2pread, c2pwrite,
709 errread, errwrite, errpipe_read, errpipe_write,
710 close_fds, restore_signals, call_setsid,
711 call_setgid, gid, call_setgroups, groups_size, groups,
712 call_setuid, uid, child_umask, child_sigmask,
713 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
714 _exit(255);
715 return 0; /* Dead code to avoid a potential compiler warning. */
716}
717
718
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000719static PyObject *
Christian Heimes035deee2020-11-21 20:28:14 +0100720subprocess_fork_exec(PyObject *module, PyObject *args)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000721{
722 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200723 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000724 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000725 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000726 PyObject *preexec_fn_args_tuple = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700727 PyObject *groups_list;
728 PyObject *uid_object, *gid_object;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000729 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
730 int errpipe_read, errpipe_write, close_fds, restore_signals;
731 int call_setsid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700732 int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
733 uid_t uid;
734 gid_t gid, *groups = NULL;
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700735 int child_umask;
Alexey Izbyshevc0590c02020-10-26 03:09:32 +0300736 PyObject *cwd_obj, *cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000737 const char *cwd;
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +0300738 pid_t pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000739 int need_to_reenable_gc = 0;
740 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700741 Py_ssize_t arg_num, num_groups = 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200742 int need_after_fork = 0;
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700743 int saved_errno = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000744
745 if (!PyArg_ParseTuple(
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700746 args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300747 &process_args, &executable_list,
748 &close_fds, &PyTuple_Type, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000749 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000750 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
751 &errread, &errwrite, &errpipe_read, &errpipe_write,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700752 &restore_signals, &call_setsid,
Gregory P. Smithf3751ef2019-10-12 13:24:56 -0700753 &gid_object, &groups_list, &uid_object, &child_umask,
Patrick McLean2b2ead72019-09-12 10:15:44 -0700754 &preexec_fn))
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000755 return NULL;
756
Christian Heimes98d90f72019-08-27 23:36:56 +0200757 if ((preexec_fn != Py_None) &&
Victor Stinnerbe793732020-03-13 18:15:33 +0100758 (PyInterpreterState_Get() != PyInterpreterState_Main())) {
Christian Heimes98d90f72019-08-27 23:36:56 +0200759 PyErr_SetString(PyExc_RuntimeError,
760 "preexec_fn not supported within subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -0700761 return NULL;
762 }
763
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800764 if (close_fds && errpipe_write < 3) { /* precondition */
765 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
766 return NULL;
767 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800768 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
769 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000770 return NULL;
771 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000772
Victor Stinner252346a2020-05-01 11:33:44 +0200773 PyInterpreterState *interp = PyInterpreterState_Get();
774 const PyConfig *config = _PyInterpreterState_GetConfig(interp);
775 if (config->_isolated_interpreter) {
776 PyErr_SetString(PyExc_RuntimeError,
777 "subprocess not supported for isolated subinterpreters");
778 return NULL;
779 }
780
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000781 /* We need to call gc.disable() when we'll be calling preexec_fn */
782 if (preexec_fn != Py_None) {
Victor Stinner103d5e42021-04-28 19:09:29 +0200783 need_to_reenable_gc = PyGC_Disable();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000784 }
785
786 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200787 if (!exec_array)
788 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000789
790 /* Convert args and env into appropriate arguments for exec() */
791 /* These conversions are done in the parent process to avoid allocating
792 or freeing memory in the child process. */
793 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000794 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000795 /* Equivalent to: */
796 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000797 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200798 if (fast_args == NULL)
799 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000800 num_args = PySequence_Fast_GET_SIZE(fast_args);
801 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000802 if (converted_args == NULL)
803 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000804 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000805 PyObject *borrowed_arg, *converted_arg;
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300806 if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
807 PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
808 goto cleanup;
809 }
Gregory P. Smith68f52172010-03-15 06:07:42 +0000810 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000811 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
812 goto cleanup;
813 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
814 }
815
816 argv = _PySequence_BytesToCharpArray(converted_args);
817 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000818 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000819 if (!argv)
820 goto cleanup;
821 }
822
823 if (env_list != Py_None) {
824 envp = _PySequence_BytesToCharpArray(env_list);
825 if (!envp)
826 goto cleanup;
827 }
828
Victor Stinner0e59cc32010-04-16 23:49:32 +0000829 if (cwd_obj != Py_None) {
830 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
831 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000832 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000833 } else {
834 cwd = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000835 }
836
Patrick McLean2b2ead72019-09-12 10:15:44 -0700837 if (groups_list != Py_None) {
838#ifdef HAVE_SETGROUPS
839 Py_ssize_t i;
Jakub KulĂ­k0159e5e2020-12-29 13:58:27 +0100840 gid_t gid;
Patrick McLean2b2ead72019-09-12 10:15:44 -0700841
842 if (!PyList_Check(groups_list)) {
843 PyErr_SetString(PyExc_TypeError,
844 "setgroups argument must be a list");
845 goto cleanup;
846 }
847 num_groups = PySequence_Size(groups_list);
848
849 if (num_groups < 0)
850 goto cleanup;
851
852 if (num_groups > MAX_GROUPS) {
853 PyErr_SetString(PyExc_ValueError, "too many groups");
854 goto cleanup;
855 }
856
857 if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
858 PyErr_SetString(PyExc_MemoryError,
859 "failed to allocate memory for group list");
860 goto cleanup;
861 }
862
863 for (i = 0; i < num_groups; i++) {
864 PyObject *elem;
865 elem = PySequence_GetItem(groups_list, i);
866 if (!elem)
867 goto cleanup;
868 if (!PyLong_Check(elem)) {
869 PyErr_SetString(PyExc_TypeError,
870 "groups must be integers");
871 Py_DECREF(elem);
872 goto cleanup;
873 } else {
Patrick McLean2b2ead72019-09-12 10:15:44 -0700874 if (!_Py_Gid_Converter(elem, &gid)) {
875 Py_DECREF(elem);
876 PyErr_SetString(PyExc_ValueError, "invalid group id");
877 goto cleanup;
878 }
879 groups[i] = gid;
880 }
881 Py_DECREF(elem);
882 }
883 call_setgroups = 1;
884
885#else /* HAVE_SETGROUPS */
886 PyErr_BadInternalCall();
887 goto cleanup;
888#endif /* HAVE_SETGROUPS */
889 }
890
891 if (gid_object != Py_None) {
892#ifdef HAVE_SETREGID
893 if (!_Py_Gid_Converter(gid_object, &gid))
894 goto cleanup;
895
896 call_setgid = 1;
897
898#else /* HAVE_SETREGID */
899 PyErr_BadInternalCall();
900 goto cleanup;
901#endif /* HAVE_SETREUID */
902 }
903
904 if (uid_object != Py_None) {
905#ifdef HAVE_SETREUID
906 if (!_Py_Uid_Converter(uid_object, &uid))
907 goto cleanup;
908
909 call_setuid = 1;
910
911#else /* HAVE_SETREUID */
912 PyErr_BadInternalCall();
913 goto cleanup;
914#endif /* HAVE_SETREUID */
915 }
916
Gregory P. Smith163468a2017-05-29 10:03:41 -0700917 /* This must be the last thing done before fork() because we do not
918 * want to call PyOS_BeforeFork() if there is any chance of another
919 * error leading to the cleanup: code without calling fork(). */
920 if (preexec_fn != Py_None) {
921 preexec_fn_args_tuple = PyTuple_New(0);
922 if (!preexec_fn_args_tuple)
923 goto cleanup;
924 PyOS_BeforeFork();
925 need_after_fork = 1;
926 }
927
Alexey Izbyshev976da902020-10-24 03:47:01 +0300928 /* NOTE: When old_sigmask is non-NULL, do_fork_exec() may use vfork(). */
929 const void *old_sigmask = NULL;
930#ifdef VFORK_USABLE
931 /* Use vfork() only if it's safe. See the comment above child_exec(). */
932 sigset_t old_sigs;
933 if (preexec_fn == Py_None &&
Gregory P. Smithbe3c3a02020-10-24 12:07:35 -0700934 !call_setuid && !call_setgid && !call_setgroups) {
Alexey Izbyshev976da902020-10-24 03:47:01 +0300935 /* Block all signals to ensure that no signal handlers are run in the
936 * child process while it shares memory with us. Note that signals
937 * used internally by C libraries won't be blocked by
938 * pthread_sigmask(), but signal handlers installed by C libraries
939 * normally service only signals originating from *within the process*,
940 * so it should be sufficient to consider any library function that
941 * might send such a signal to be vfork-unsafe and do not call it in
942 * the child.
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000943 */
Alexey Izbyshev976da902020-10-24 03:47:01 +0300944 sigset_t all_sigs;
945 sigfillset(&all_sigs);
Alexey Izbyshev473db472020-10-24 20:47:38 +0300946 if ((saved_errno = pthread_sigmask(SIG_BLOCK, &all_sigs, &old_sigs))) {
Alexey Izbyshev473db472020-10-24 20:47:38 +0300947 goto cleanup;
948 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300949 old_sigmask = &old_sigs;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000950 }
Alexey Izbyshev976da902020-10-24 03:47:01 +0300951#endif
952
953 pid = do_fork_exec(exec_array, argv, envp, cwd,
954 p2cread, p2cwrite, c2pread, c2pwrite,
955 errread, errwrite, errpipe_read, errpipe_write,
956 close_fds, restore_signals, call_setsid,
957 call_setgid, gid, call_setgroups, num_groups, groups,
958 call_setuid, uid, child_umask, old_sigmask,
959 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
960
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700961 /* Parent (original) process */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000962 if (pid == -1) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700963 /* Capture errno for the exception. */
964 saved_errno = errno;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000965 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000966
Alexey Izbyshev976da902020-10-24 03:47:01 +0300967#ifdef VFORK_USABLE
968 if (old_sigmask) {
969 /* vfork() semantics guarantees that the parent is blocked
970 * until the child performs _exit() or execve(), so it is safe
971 * to unblock signals once we're here.
972 * Note that in environments where vfork() is implemented as fork(),
973 * such as QEMU user-mode emulation, the parent won't be blocked,
974 * but it won't share the address space with the child,
Alexey Izbyshev473db472020-10-24 20:47:38 +0300975 * so it's still safe to unblock the signals.
976 *
977 * We don't handle errors here because this call can't fail
978 * if valid arguments are given, and because there is no good
979 * way for the caller to deal with a failure to restore
980 * the thread signal mask. */
981 (void) pthread_sigmask(SIG_SETMASK, old_sigmask, NULL);
Alexey Izbyshev976da902020-10-24 03:47:01 +0300982 }
983#endif
984
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200985 if (need_after_fork)
986 PyOS_AfterFork_Parent();
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000987
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +0300988cleanup:
989 if (saved_errno != 0) {
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700990 errno = saved_errno;
991 /* We can't call this above as PyOS_AfterFork_Parent() calls back
992 * into Python code which would see the unreturned error. */
993 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smitha20b6ad2018-09-13 04:30:10 -0700994 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000995
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +0300996 Py_XDECREF(preexec_fn_args_tuple);
997 PyMem_RawFree(groups);
Alexey Izbyshevc0590c02020-10-26 03:09:32 +0300998 Py_XDECREF(cwd_obj2);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000999 if (envp)
1000 _Py_FreeCharPArray(envp);
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001001 Py_XDECREF(converted_args);
1002 Py_XDECREF(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001003 if (argv)
1004 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +02001005 if (exec_array)
1006 _Py_FreeCharPArray(exec_array);
Patrick McLean2b2ead72019-09-12 10:15:44 -07001007
Victor Stinner103d5e42021-04-28 19:09:29 +02001008 if (need_to_reenable_gc) {
1009 PyGC_Enable();
1010 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001011 Py_XDECREF(gc_module);
Alexey Izbyshevd3b4e062020-11-01 08:33:08 +03001012
1013 return pid == -1 ? NULL : PyLong_FromPid(pid);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001014}
1015
1016
1017PyDoc_STRVAR(subprocess_fork_exec_doc,
Orivej Desh77abf232019-09-20 17:01:10 +00001018"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001019 p2cread, p2cwrite, c2pread, c2pwrite,\n\
1020 errread, errwrite, errpipe_read, errpipe_write,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001021 restore_signals, call_setsid,\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001022 gid, groups_list, uid,\n\
Patrick McLean2b2ead72019-09-12 10:15:44 -07001023 preexec_fn)\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001024\n\
1025Forks a child process, closes parent file descriptors as appropriate in the\n\
1026child and dups the few that are needed before calling exec() in the child\n\
1027process.\n\
1028\n\
Orivej Desh77abf232019-09-20 17:01:10 +00001029If close_fds is true, close file descriptors 3 and higher, except those listed\n\
1030in the sorted tuple pass_fds.\n\
1031\n\
1032The preexec_fn, if supplied, will be called immediately before closing file\n\
1033descriptors and exec.\n\
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001034WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
1035 It may trigger infrequent, difficult to debug deadlocks.\n\
1036\n\
1037If an error occurs in the child process before the exec, it is\n\
1038serialized and written to the errpipe_write fd per subprocess.py.\n\
1039\n\
1040Returns: the child process's PID.\n\
1041\n\
1042Raises: Only on an error in the parent process.\n\
1043");
1044
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001045/* module level code ********************************************************/
1046
1047PyDoc_STRVAR(module_doc,
1048"A POSIX helper for the subprocess module.");
1049
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001050static PyMethodDef module_methods[] = {
1051 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001052 {NULL, NULL} /* sentinel */
1053};
1054
Christian Heimes035deee2020-11-21 20:28:14 +01001055static PyModuleDef_Slot _posixsubprocess_slots[] = {
Christian Heimes035deee2020-11-21 20:28:14 +01001056 {0, NULL}
1057};
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001058
1059static struct PyModuleDef _posixsubprocessmodule = {
luzpaza5293b42017-11-05 07:37:50 -06001060 PyModuleDef_HEAD_INIT,
Christian Heimes035deee2020-11-21 20:28:14 +01001061 .m_name = "_posixsubprocess",
1062 .m_doc = module_doc,
Victor Stinner103d5e42021-04-28 19:09:29 +02001063 .m_size = 0,
Christian Heimes035deee2020-11-21 20:28:14 +01001064 .m_methods = module_methods,
1065 .m_slots = _posixsubprocess_slots,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001066};
1067
1068PyMODINIT_FUNC
1069PyInit__posixsubprocess(void)
1070{
Christian Heimes035deee2020-11-21 20:28:14 +01001071 return PyModuleDef_Init(&_posixsubprocessmodule);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001072}