Skip to content

Commit 0cd888b

Browse files
gpsheadsobolevnambv
authored
[3.11] gh-120384: gh-120298: Fix array-out-of-bounds & use after free list (GH-121345)
(cherry picked from commit 8334a1b) Co-authored-by: Nikita Sobolev <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 88f3f5b commit 0cd888b

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
@@ -229,6 +229,31 @@ def __eq__(self, other):
229229
list4 = [1]
230230
self.assertFalse(list3 == list4)
231231

232+
def test_lt_operator_modifying_operand(self):
233+
# See gh-120298
234+
class evil:
235+
def __lt__(self, other):
236+
other.clear()
237+
return NotImplemented
238+
239+
a = [[evil()]]
240+
with self.assertRaises(TypeError):
241+
a[0] < a
242+
243+
def test_list_index_modifing_operand(self):
244+
# See gh-120384
245+
class evil:
246+
def __init__(self, lst):
247+
self.lst = lst
248+
def __iter__(self):
249+
yield from self.lst
250+
self.lst.clear()
251+
252+
lst = list(range(5))
253+
operand = evil(lst)
254+
with self.assertRaises(ValueError):
255+
lst[::-1] = operand
256+
232257
@cpython_only
233258
def test_preallocation(self):
234259
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
@@ -2757,7 +2757,14 @@ list_richcompare(PyObject *v, PyObject *w, int op)
27572757
}
27582758

27592759
/* Compare the final item again using the proper operator */
2760-
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2760+
PyObject *vitem = vl->ob_item[i];
2761+
PyObject *witem = wl->ob_item[i];
2762+
Py_INCREF(vitem);
2763+
Py_INCREF(witem);
2764+
PyObject *result = PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2765+
Py_DECREF(vitem);
2766+
Py_DECREF(witem);
2767+
return result;
27612768
}
27622769

27632770
/*[clinic input]
@@ -2927,6 +2934,23 @@ list_subscript(PyListObject* self, PyObject* item)
29272934
}
29282935
}
29292936

2937+
static Py_ssize_t
2938+
adjust_slice_indexes(PyListObject *lst,
2939+
Py_ssize_t *start, Py_ssize_t *stop,
2940+
Py_ssize_t step)
2941+
{
2942+
Py_ssize_t slicelength = PySlice_AdjustIndices(Py_SIZE(lst), start, stop,
2943+
step);
2944+
2945+
/* Make sure s[5:2] = [..] inserts at the right place:
2946+
before 5, not before 2. */
2947+
if ((step < 0 && *start < *stop) ||
2948+
(step > 0 && *start > *stop))
2949+
*stop = *start;
2950+
2951+
return slicelength;
2952+
}
2953+
29302954
static int
29312955
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29322956
{
@@ -2939,22 +2963,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29392963
return list_ass_item(self, i, value);
29402964
}
29412965
else if (PySlice_Check(item)) {
2942-
Py_ssize_t start, stop, step, slicelength;
2966+
Py_ssize_t start, stop, step;
29432967

29442968
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
29452969
return -1;
29462970
}
2947-
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2948-
step);
2949-
2950-
if (step == 1)
2951-
return list_ass_slice(self, start, stop, value);
2952-
2953-
/* Make sure s[5:2] = [..] inserts at the right place:
2954-
before 5, not before 2. */
2955-
if ((step < 0 && start < stop) ||
2956-
(step > 0 && start > stop))
2957-
stop = start;
29582971

29592972
if (value == NULL) {
29602973
/* delete slice */
@@ -2963,6 +2976,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29632976
Py_ssize_t i;
29642977
int res;
29652978

2979+
Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
2980+
step);
2981+
2982+
if (step == 1)
2983+
return list_ass_slice(self, start, stop, value);
2984+
29662985
if (slicelength <= 0)
29672986
return 0;
29682987

@@ -3038,6 +3057,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
30383057
if (!seq)
30393058
return -1;
30403059

3060+
Py_ssize_t slicelength = adjust_slice_indexes(self, &start, &stop,
3061+
step);
3062+
3063+
if (step == 1) {
3064+
int res = list_ass_slice(self, start, stop, seq);
3065+
Py_DECREF(seq);
3066+
return res;
3067+
}
3068+
30413069
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
30423070
PyErr_Format(PyExc_ValueError,
30433071
"attempt to assign sequence of "

0 commit comments

Comments
 (0)