Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66bffd1

Browse files
bpo-30065: Fixed arguments validation in _posixsubprocess.fork_exec(). (#1110)
1 parent a79f4c2 commit 66bffd1

5 files changed

Lines changed: 41 additions & 26 deletions

File tree

Lib/multiprocessing/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _close_stdin():
386386

387387
def spawnv_passfds(path, args, passfds):
388388
import _posixsubprocess
389-
passfds = sorted(passfds)
389+
passfds = tuple(sorted(map(int, passfds)))
390390
errpipe_read, errpipe_write = os.pipe()
391391
try:
392392
return _posixsubprocess.fork_exec(

Lib/subprocess.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
12521252
fds_to_keep.add(errpipe_write)
12531253
self.pid = _posixsubprocess.fork_exec(
12541254
args, executable_list,
1255-
close_fds, sorted(fds_to_keep), cwd, env_list,
1255+
close_fds, tuple(sorted(map(int, fds_to_keep))),
1256+
cwd, env_list,
12561257
p2cread, p2cwrite, c2pread, c2pwrite,
12571258
errread, errwrite,
12581259
errpipe_read, errpipe_write,

Lib/test/test_capi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ class Z(object):
9898
def __len__(self):
9999
return 1
100100
self.assertRaises(TypeError, _posixsubprocess.fork_exec,
101-
1,Z(),3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17)
101+
1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
102102
# Issue #15736: overflow in _PySequence_BytesToCharpArray()
103103
class Z(object):
104104
def __len__(self):
105105
return sys.maxsize
106106
def __getitem__(self, i):
107107
return b'x'
108108
self.assertRaises(MemoryError, _posixsubprocess.fork_exec,
109-
1,Z(),3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17)
109+
1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
110110

111111
@unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
112112
def test_subprocess_fork_exec(self):
@@ -116,7 +116,7 @@ def __len__(self):
116116

117117
# Issue #15738: crash in subprocess_fork_exec()
118118
self.assertRaises(TypeError, _posixsubprocess.fork_exec,
119-
Z(),[b'1'],3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17)
119+
Z(),[b'1'],3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
120120

121121
@unittest.skipIf(MISSING_C_DOCSTRINGS,
122122
"Signature information for builtins requires docstrings")

Lib/test/test_subprocess.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ def test_fork_exec(self):
24182418
with self.assertRaises(TypeError):
24192419
_posixsubprocess.fork_exec(
24202420
args, exe_list,
2421-
True, [], cwd, env_list,
2421+
True, (), cwd, env_list,
24222422
-1, -1, -1, -1,
24232423
1, 2, 3, 4,
24242424
True, True, func)
@@ -2430,6 +2430,16 @@ def test_fork_exec(self):
24302430
def test_fork_exec_sorted_fd_sanity_check(self):
24312431
# Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
24322432
import _posixsubprocess
2433+
class BadInt:
2434+
first = True
2435+
def __init__(self, value):
2436+
self.value = value
2437+
def __int__(self):
2438+
if self.first:
2439+
self.first = False
2440+
return self.value
2441+
raise ValueError
2442+
24332443
gc_enabled = gc.isenabled()
24342444
try:
24352445
gc.enable()
@@ -2440,6 +2450,7 @@ def test_fork_exec_sorted_fd_sanity_check(self):
24402450
(18, 23, 42, 2**63), # Out of range.
24412451
(5, 4), # Not sorted.
24422452
(6, 7, 7, 8), # Duplicate.
2453+
(BadInt(1), BadInt(2)),
24432454
):
24442455
with self.assertRaises(
24452456
ValueError,

Modules/_posixsubprocess.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,17 @@ _is_fdescfs_mounted_on_dev_fd(void)
111111
static int
112112
_sanity_check_python_fd_sequence(PyObject *fd_sequence)
113113
{
114-
Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
114+
Py_ssize_t seq_idx;
115115
long prev_fd = -1;
116-
for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
117-
PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
118-
long iter_fd = PyLong_AsLong(py_fd);
116+
for (seq_idx = 0; seq_idx < PyTuple_GET_SIZE(fd_sequence); ++seq_idx) {
117+
PyObject* py_fd = PyTuple_GET_ITEM(fd_sequence, seq_idx);
118+
long iter_fd;
119+
if (!PyLong_Check(py_fd)) {
120+
return 1;
121+
}
122+
iter_fd = PyLong_AsLong(py_fd);
119123
if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
120-
/* Negative, overflow, not a Long, unsorted, too big for a fd. */
124+
/* Negative, overflow, unsorted, too big for a fd. */
121125
return 1;
122126
}
123127
prev_fd = iter_fd;
@@ -132,13 +136,12 @@ _is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
132136
{
133137
/* Binary search. */
134138
Py_ssize_t search_min = 0;
135-
Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
139+
Py_ssize_t search_max = PyTuple_GET_SIZE(fd_sequence) - 1;
136140
if (search_max < 0)
137141
return 0;
138142
do {
139143
long middle = (search_min + search_max) / 2;
140-
long middle_fd = PyLong_AsLong(
141-
PySequence_Fast_GET_ITEM(fd_sequence, middle));
144+
long middle_fd = PyLong_AsLong(PyTuple_GET_ITEM(fd_sequence, middle));
142145
if (fd == middle_fd)
143146
return 1;
144147
if (fd > middle_fd)
@@ -154,9 +157,9 @@ make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
154157
{
155158
Py_ssize_t i, len;
156159

157-
len = PySequence_Length(py_fds_to_keep);
160+
len = PyTuple_GET_SIZE(py_fds_to_keep);
158161
for (i = 0; i < len; ++i) {
159-
PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
162+
PyObject* fdobj = PyTuple_GET_ITEM(py_fds_to_keep, i);
160163
long fd = PyLong_AsLong(fdobj);
161164
assert(!PyErr_Occurred());
162165
assert(0 <= fd && fd <= INT_MAX);
@@ -213,14 +216,13 @@ static void
213216
_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
214217
{
215218
long end_fd = safe_get_max_fd();
216-
Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
219+
Py_ssize_t num_fds_to_keep = PyTuple_GET_SIZE(py_fds_to_keep);
217220
Py_ssize_t keep_seq_idx;
218221
int fd_num;
219222
/* As py_fds_to_keep is sorted we can loop through the list closing
220223
* fds inbetween any in the keep list falling within our range. */
221224
for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
222-
PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
223-
keep_seq_idx);
225+
PyObject* py_keep_fd = PyTuple_GET_ITEM(py_fds_to_keep, keep_seq_idx);
224226
int keep_fd = PyLong_AsLong(py_keep_fd);
225227
if (keep_fd < start_fd)
226228
continue;
@@ -306,7 +308,7 @@ _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
306308

307309

308310
/* Close all open file descriptors from start_fd and higher.
309-
* Do not close any in the sorted py_fds_to_keep list.
311+
* Do not close any in the sorted py_fds_to_keep tuple.
310312
*
311313
* This function violates the strict use of async signal safe functions. :(
312314
* It calls opendir(), readdir() and closedir(). Of these, the one most
@@ -562,8 +564,9 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
562564
#endif
563565

564566
if (!PyArg_ParseTuple(
565-
args, "OOpOOOiiiiiiiiiiO:fork_exec",
566-
&process_args, &executable_list, &close_fds, &py_fds_to_keep,
567+
args, "OOpO!OOiiiiiiiiiiO:fork_exec",
568+
&process_args, &executable_list,
569+
&close_fds, &PyTuple_Type, &py_fds_to_keep,
567570
&cwd_obj, &env_list,
568571
&p2cread, &p2cwrite, &c2pread, &c2pwrite,
569572
&errread, &errwrite, &errpipe_read, &errpipe_write,
@@ -574,10 +577,6 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
574577
PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
575578
return NULL;
576579
}
577-
if (PySequence_Length(py_fds_to_keep) < 0) {
578-
PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
579-
return NULL;
580-
}
581580
if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
582581
PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
583582
return NULL;
@@ -631,6 +630,10 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
631630
goto cleanup;
632631
for (arg_num = 0; arg_num < num_args; ++arg_num) {
633632
PyObject *borrowed_arg, *converted_arg;
633+
if (PySequence_Fast_GET_SIZE(fast_args) != num_args) {
634+
PyErr_SetString(PyExc_RuntimeError, "args changed during iteration");
635+
goto cleanup;
636+
}
634637
borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
635638
if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
636639
goto cleanup;

0 commit comments

Comments
 (0)