Skip to content

Commit 3eea546

Browse files
miss-islingtongpsheadsobolevnambv
authored
[3.10] gh-120384: gh-120298: Fix array-out-of-bounds & use after free list (GH-121345) (GH-140833)
(cherry picked from commit 8334a1b) (cherry picked from commit 0cd888b) Co-authored-by: Gregory P. Smith <[email protected]> Co-authored-by: Nikita Sobolev <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 9104fc6 commit 3eea546

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

Lib/test/list_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ def test_setslice(self):
192192

193193
self.assertRaises(TypeError, a.__setitem__)
194194

195+
def test_slice_assign_iterator(self):
196+
x = self.type2test(range(5))
197+
x[0:3] = reversed(range(3))
198+
self.assertEqual(x, self.type2test([2, 1, 0, 3, 4]))
199+
200+
x[:] = reversed(range(3))
201+
self.assertEqual(x, self.type2test([2, 1, 0]))
202+
195203
def test_delslice(self):
196204
a = self.type2test([0, 1])
197205
del a[1:2]

Lib/test/test_list.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ def __eq__(self, other):
201201
list4 = [1]
202202
self.assertFalse(list3 == list4)
203203

204+
def test_lt_operator_modifying_operand(self):
205+
# See gh-120298
206+
class evil:
207+
def __lt__(self, other):
208+
other.clear()
209+
return NotImplemented
210+
211+
a = [[evil()]]
212+
with self.assertRaises(TypeError):
213+
a[0] < a
214+
215+
def test_list_index_modifing_operand(self):
216+
# See gh-120384
217+
class evil:
218+
def __init__(self, lst):
219+
self.lst = lst
220+
def __iter__(self):
221+
yield from self.lst
222+
self.lst.clear()
223+
224+
lst = list(range(5))
225+
operand = evil(lst)
226+
with self.assertRaises(ValueError):
227+
lst[::-1] = operand
228+
204229
@cpython_only
205230
def test_preallocation(self):
206231
iterable = [0] * 10
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix use-after free in ``list_richcompare_impl`` which can be invoked via
2+
some specificly tailored evil input.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an array out of bounds crash in ``list_ass_subscript``, which could be
2+
invoked via some specificly tailored input: including concurrent modification
3+
of a list object, where one thread assigns a slice and another clears it.

Objects/listobject.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,7 +2707,14 @@ list_richcompare(PyObject *v, PyObject *w, int op)
27072707
}
27082708

27092709
/* Compare the final item again using the proper operator */
2710-
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2710+
PyObject *vitem = vl->ob_item[i];
2711+
PyObject *witem = wl->ob_item[i];
2712+
Py_INCREF(vitem);
2713+
Py_INCREF(witem);
2714+
PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2715+
Py_DECREF(vitem);
2716+
Py_DECREF(witem);
2717+
return result;
27112718
}
27122719

27132720
/*[clinic input]
@@ -2878,6 +2885,23 @@ list_subscript(PyListObject* self, PyObject* item)
28782885
}
28792886
}
28802887

2888+
static Py_ssize_t
2889+
adjust_slice_indexes(PyListObject *lst,
2890+
Py_ssize_t *start, Py_ssize_t *stop,
2891+
Py_ssize_t step)
2892+
{
2893+
Py_ssize_t slicelength = PySlice_AdjustIndices(Py_SIZE(lst), start, stop,
2894+
step);
2895+
2896+
/* Make sure s[5:2] = [..] inserts at the right place:
2897+
before 5, not before 2. */
2898+
if ((step < 0 && *start < *stop) ||
2899+
(step > 0 && *start > *stop))
2900+
*stop = *start;
2901+
2902+
return slicelength;
2903+
}
2904+
28812905
static int
28822906
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28832907
{
@@ -2890,22 +2914,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28902914
return list_ass_item(self, i, value);
28912915
}
28922916
else if (PySlice_Check(item)) {
2893-
Py_ssize_t start, stop, step, slicelength;
2917+
Py_ssize_t start, stop, step;
28942918

28952919
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
28962920
return -1;
28972921
}
2898-
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2899-
step);
2900-
2901-
if (step == 1)
2902-
return list_ass_slice(self, start, stop, value);
2903-
2904-
/* Make sure s[5:2] = [..] inserts at the right place:
2905-
before 5, not before 2. */
2906-
if ((step < 0 && start < stop) ||
2907-
(step > 0 && start > stop))
2908-
stop = start;
29092922

29102923
if (value == NULL) {
29112924
/* delete slice */
@@ -2914,6 +2927,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29142927
Py_ssize_t i;
29152928
int res;
29162929

2930+
Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
2931+
step);
2932+
2933+
if (step == 1)
2934+
return list_ass_slice(self, start, stop, value);
2935+
29172936
if (slicelength <= 0)
29182937
return 0;
29192938

@@ -2989,6 +3008,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29893008
if (!seq)
29903009
return -1;
29913010

3011+
Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3012+
step);
3013+
3014+
if (step == 1) {
3015+
int res = list_ass_slice(self, start, stop, seq);
3016+
Py_DECREF(seq);
3017+
return res;
3018+
}
3019+
29923020
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
29933021
PyErr_Format(PyExc_ValueError,
29943022
"attempt to assign sequence of "

0 commit comments

Comments
 (0)