Skip to content

Commit 11190c4

Browse files
Fidget-Spinnerchilaxanserhiy-storchaka
authored
gh-92888: Fix memoryview bad __index__ use after free (GH-92946)
Co-authored-by: chilaxan <35645806+chilaxan@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <3659035+serhiy-storchaka@users.noreply.github.com>
1 parent a51742a commit 11190c4

File tree

3 files changed

+139
-19
lines changed

3 files changed

+139
-19
lines changed

Lib/test/test_memoryview.py

+101
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,107 @@ def test_pickle(self):
545545
with self.assertRaises(TypeError):
546546
pickle.dumps(m, proto)
547547

548+
def test_use_released_memory(self):
549+
# gh-92888: Previously it was possible to use a memoryview even after
550+
# backing buffer is freed in certain cases. This tests that those
551+
# cases raise an exception.
552+
size = 128
553+
def release():
554+
m.release()
555+
nonlocal ba
556+
ba = bytearray(size)
557+
class MyIndex:
558+
def __index__(self):
559+
release()
560+
return 4
561+
class MyFloat:
562+
def __float__(self):
563+
release()
564+
return 4.25
565+
class MyBool:
566+
def __bool__(self):
567+
release()
568+
return True
569+
570+
ba = None
571+
m = memoryview(bytearray(b'\xff'*size))
572+
with self.assertRaises(ValueError):
573+
m[MyIndex()]
574+
575+
ba = None
576+
m = memoryview(bytearray(b'\xff'*size))
577+
self.assertEqual(list(m[:MyIndex()]), [255] * 4)
578+
579+
ba = None
580+
m = memoryview(bytearray(b'\xff'*size))
581+
self.assertEqual(list(m[MyIndex():8]), [255] * 4)
582+
583+
ba = None
584+
m = memoryview(bytearray(b'\xff'*size)).cast('B', (64, 2))
585+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
586+
m[MyIndex(), 0]
587+
588+
ba = None
589+
m = memoryview(bytearray(b'\xff'*size)).cast('B', (2, 64))
590+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
591+
m[0, MyIndex()]
592+
593+
ba = None
594+
m = memoryview(bytearray(b'\xff'*size))
595+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
596+
m[MyIndex()] = 42
597+
self.assertEqual(ba[:8], b'\0'*8)
598+
599+
ba = None
600+
m = memoryview(bytearray(b'\xff'*size))
601+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
602+
m[:MyIndex()] = b'spam'
603+
self.assertEqual(ba[:8], b'\0'*8)
604+
605+
ba = None
606+
m = memoryview(bytearray(b'\xff'*size))
607+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
608+
m[MyIndex():8] = b'spam'
609+
self.assertEqual(ba[:8], b'\0'*8)
610+
611+
ba = None
612+
m = memoryview(bytearray(b'\xff'*size)).cast('B', (64, 2))
613+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
614+
m[MyIndex(), 0] = 42
615+
self.assertEqual(ba[8:16], b'\0'*8)
616+
ba = None
617+
m = memoryview(bytearray(b'\xff'*size)).cast('B', (2, 64))
618+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
619+
m[0, MyIndex()] = 42
620+
self.assertEqual(ba[:8], b'\0'*8)
621+
622+
ba = None
623+
m = memoryview(bytearray(b'\xff'*size))
624+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
625+
m[0] = MyIndex()
626+
self.assertEqual(ba[:8], b'\0'*8)
627+
628+
for fmt in 'bhilqnBHILQN':
629+
with self.subTest(fmt=fmt):
630+
ba = None
631+
m = memoryview(bytearray(b'\xff'*size)).cast(fmt)
632+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
633+
m[0] = MyIndex()
634+
self.assertEqual(ba[:8], b'\0'*8)
635+
636+
for fmt in 'fd':
637+
with self.subTest(fmt=fmt):
638+
ba = None
639+
m = memoryview(bytearray(b'\xff'*size)).cast(fmt)
640+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
641+
m[0] = MyFloat()
642+
self.assertEqual(ba[:8], b'\0'*8)
643+
644+
ba = None
645+
m = memoryview(bytearray(b'\xff'*size)).cast('?')
646+
with self.assertRaisesRegex(ValueError, "operation forbidden"):
647+
m[0] = MyBool()
648+
self.assertEqual(ba[:8], b'\0'*8)
548649

549650
if __name__ == "__main__":
550651
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``memoryview`` use after free when accessing the backing buffer in certain cases.
2+

Objects/memoryobject.c

+36-19
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ PyTypeObject _PyManagedBuffer_Type = {
193193
return -1; \
194194
}
195195

196+
/* See gh-92888. These macros signal that we need to check the memoryview
197+
again due to possible read after frees. */
198+
#define CHECK_RELEASED_AGAIN(mv) CHECK_RELEASED(mv)
199+
#define CHECK_RELEASED_INT_AGAIN(mv) CHECK_RELEASED_INT(mv)
200+
196201
#define CHECK_LIST_OR_TUPLE(v) \
197202
if (!PyList_Check(v) && !PyTuple_Check(v)) { \
198203
PyErr_SetString(PyExc_TypeError, \
@@ -381,8 +386,9 @@ copy_rec(const Py_ssize_t *shape, Py_ssize_t ndim, Py_ssize_t itemsize,
381386

382387
/* Faster copying of one-dimensional arrays. */
383388
static int
384-
copy_single(const Py_buffer *dest, const Py_buffer *src)
389+
copy_single(PyMemoryViewObject *self, const Py_buffer *dest, const Py_buffer *src)
385390
{
391+
CHECK_RELEASED_INT_AGAIN(self);
386392
char *mem = NULL;
387393

388394
assert(dest->ndim == 1);
@@ -1677,7 +1683,7 @@ pylong_as_zu(PyObject *item)
16771683
module syntax. This function is very sensitive to small changes. With this
16781684
layout gcc automatically generates a fast jump table. */
16791685
static inline PyObject *
1680-
unpack_single(const char *ptr, const char *fmt)
1686+
unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt)
16811687
{
16821688
unsigned long long llu;
16831689
unsigned long lu;
@@ -1689,6 +1695,8 @@ unpack_single(const char *ptr, const char *fmt)
16891695
unsigned char uc;
16901696
void *p;
16911697

1698+
CHECK_RELEASED_AGAIN(self);
1699+
16921700
switch (fmt[0]) {
16931701

16941702
/* signed integers and fast path for 'B' */
@@ -1767,7 +1775,7 @@ unpack_single(const char *ptr, const char *fmt)
17671775
/* Pack a single item. 'fmt' can be any native format character in
17681776
struct module syntax. */
17691777
static int
1770-
pack_single(char *ptr, PyObject *item, const char *fmt)
1778+
pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt)
17711779
{
17721780
unsigned long long llu;
17731781
unsigned long lu;
@@ -1784,6 +1792,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
17841792
ld = pylong_as_ld(item);
17851793
if (ld == -1 && PyErr_Occurred())
17861794
goto err_occurred;
1795+
CHECK_RELEASED_INT_AGAIN(self);
17871796
switch (fmt[0]) {
17881797
case 'b':
17891798
if (ld < SCHAR_MIN || ld > SCHAR_MAX) goto err_range;
@@ -1804,6 +1813,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18041813
lu = pylong_as_lu(item);
18051814
if (lu == (unsigned long)-1 && PyErr_Occurred())
18061815
goto err_occurred;
1816+
CHECK_RELEASED_INT_AGAIN(self);
18071817
switch (fmt[0]) {
18081818
case 'B':
18091819
if (lu > UCHAR_MAX) goto err_range;
@@ -1824,12 +1834,14 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18241834
lld = pylong_as_lld(item);
18251835
if (lld == -1 && PyErr_Occurred())
18261836
goto err_occurred;
1837+
CHECK_RELEASED_INT_AGAIN(self);
18271838
PACK_SINGLE(ptr, lld, long long);
18281839
break;
18291840
case 'Q':
18301841
llu = pylong_as_llu(item);
18311842
if (llu == (unsigned long long)-1 && PyErr_Occurred())
18321843
goto err_occurred;
1844+
CHECK_RELEASED_INT_AGAIN(self);
18331845
PACK_SINGLE(ptr, llu, unsigned long long);
18341846
break;
18351847

@@ -1838,12 +1850,14 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18381850
zd = pylong_as_zd(item);
18391851
if (zd == -1 && PyErr_Occurred())
18401852
goto err_occurred;
1853+
CHECK_RELEASED_INT_AGAIN(self);
18411854
PACK_SINGLE(ptr, zd, Py_ssize_t);
18421855
break;
18431856
case 'N':
18441857
zu = pylong_as_zu(item);
18451858
if (zu == (size_t)-1 && PyErr_Occurred())
18461859
goto err_occurred;
1860+
CHECK_RELEASED_INT_AGAIN(self);
18471861
PACK_SINGLE(ptr, zu, size_t);
18481862
break;
18491863

@@ -1852,6 +1866,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18521866
d = PyFloat_AsDouble(item);
18531867
if (d == -1.0 && PyErr_Occurred())
18541868
goto err_occurred;
1869+
CHECK_RELEASED_INT_AGAIN(self);
18551870
if (fmt[0] == 'f') {
18561871
PACK_SINGLE(ptr, d, float);
18571872
}
@@ -1865,6 +1880,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18651880
ld = PyObject_IsTrue(item);
18661881
if (ld < 0)
18671882
return -1; /* preserve original error */
1883+
CHECK_RELEASED_INT_AGAIN(self);
18681884
PACK_SINGLE(ptr, ld, _Bool);
18691885
break;
18701886

@@ -1882,6 +1898,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt)
18821898
p = PyLong_AsVoidPtr(item);
18831899
if (p == NULL && PyErr_Occurred())
18841900
goto err_occurred;
1901+
CHECK_RELEASED_INT_AGAIN(self);
18851902
PACK_SINGLE(ptr, p, void *);
18861903
break;
18871904

@@ -2042,7 +2059,7 @@ adjust_fmt(const Py_buffer *view)
20422059

20432060
/* Base case for multi-dimensional unpacking. Assumption: ndim == 1. */
20442061
static PyObject *
2045-
tolist_base(const char *ptr, const Py_ssize_t *shape,
2062+
tolist_base(PyMemoryViewObject *self, const char *ptr, const Py_ssize_t *shape,
20462063
const Py_ssize_t *strides, const Py_ssize_t *suboffsets,
20472064
const char *fmt)
20482065
{
@@ -2055,7 +2072,7 @@ tolist_base(const char *ptr, const Py_ssize_t *shape,
20552072

20562073
for (i = 0; i < shape[0]; ptr+=strides[0], i++) {
20572074
const char *xptr = ADJUST_PTR(ptr, suboffsets, 0);
2058-
item = unpack_single(xptr, fmt);
2075+
item = unpack_single(self, xptr, fmt);
20592076
if (item == NULL) {
20602077
Py_DECREF(lst);
20612078
return NULL;
@@ -2069,7 +2086,7 @@ tolist_base(const char *ptr, const Py_ssize_t *shape,
20692086
/* Unpack a multi-dimensional array into a nested list.
20702087
Assumption: ndim >= 1. */
20712088
static PyObject *
2072-
tolist_rec(const char *ptr, Py_ssize_t ndim, const Py_ssize_t *shape,
2089+
tolist_rec(PyMemoryViewObject *self, const char *ptr, Py_ssize_t ndim, const Py_ssize_t *shape,
20732090
const Py_ssize_t *strides, const Py_ssize_t *suboffsets,
20742091
const char *fmt)
20752092
{
@@ -2081,15 +2098,15 @@ tolist_rec(const char *ptr, Py_ssize_t ndim, const Py_ssize_t *shape,
20812098
assert(strides != NULL);
20822099

20832100
if (ndim == 1)
2084-
return tolist_base(ptr, shape, strides, suboffsets, fmt);
2101+
return tolist_base(self, ptr, shape, strides, suboffsets, fmt);
20852102

20862103
lst = PyList_New(shape[0]);
20872104
if (lst == NULL)
20882105
return NULL;
20892106

20902107
for (i = 0; i < shape[0]; ptr+=strides[0], i++) {
20912108
const char *xptr = ADJUST_PTR(ptr, suboffsets, 0);
2092-
item = tolist_rec(xptr, ndim-1, shape+1,
2109+
item = tolist_rec(self, xptr, ndim-1, shape+1,
20932110
strides+1, suboffsets ? suboffsets+1 : NULL,
20942111
fmt);
20952112
if (item == NULL) {
@@ -2123,15 +2140,15 @@ memoryview_tolist_impl(PyMemoryViewObject *self)
21232140
if (fmt == NULL)
21242141
return NULL;
21252142
if (view->ndim == 0) {
2126-
return unpack_single(view->buf, fmt);
2143+
return unpack_single(self, view->buf, fmt);
21272144
}
21282145
else if (view->ndim == 1) {
2129-
return tolist_base(view->buf, view->shape,
2146+
return tolist_base(self, view->buf, view->shape,
21302147
view->strides, view->suboffsets,
21312148
fmt);
21322149
}
21332150
else {
2134-
return tolist_rec(view->buf, view->ndim, view->shape,
2151+
return tolist_rec(self, view->buf, view->ndim, view->shape,
21352152
view->strides, view->suboffsets,
21362153
fmt);
21372154
}
@@ -2339,7 +2356,7 @@ memory_item(PyMemoryViewObject *self, Py_ssize_t index)
23392356
char *ptr = ptr_from_index(view, index);
23402357
if (ptr == NULL)
23412358
return NULL;
2342-
return unpack_single(ptr, fmt);
2359+
return unpack_single(self, ptr, fmt);
23432360
}
23442361

23452362
PyErr_SetString(PyExc_NotImplementedError,
@@ -2370,7 +2387,7 @@ memory_item_multi(PyMemoryViewObject *self, PyObject *tup)
23702387
ptr = ptr_from_tuple(view, tup);
23712388
if (ptr == NULL)
23722389
return NULL;
2373-
return unpack_single(ptr, fmt);
2390+
return unpack_single(self, ptr, fmt);
23742391
}
23752392

23762393
static inline int
@@ -2457,7 +2474,7 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
24572474
const char *fmt = adjust_fmt(view);
24582475
if (fmt == NULL)
24592476
return NULL;
2460-
return unpack_single(view->buf, fmt);
2477+
return unpack_single(self, view->buf, fmt);
24612478
}
24622479
else if (key == Py_Ellipsis) {
24632480
Py_INCREF(self);
@@ -2532,7 +2549,7 @@ memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
25322549
if (key == Py_Ellipsis ||
25332550
(PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) {
25342551
ptr = (char *)view->buf;
2535-
return pack_single(ptr, value, fmt);
2552+
return pack_single(self, ptr, value, fmt);
25362553
}
25372554
else {
25382555
PyErr_SetString(PyExc_TypeError,
@@ -2554,7 +2571,7 @@ memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
25542571
ptr = ptr_from_index(view, index);
25552572
if (ptr == NULL)
25562573
return -1;
2557-
return pack_single(ptr, value, fmt);
2574+
return pack_single(self, ptr, value, fmt);
25582575
}
25592576
/* one-dimensional: fast path */
25602577
if (PySlice_Check(key) && view->ndim == 1) {
@@ -2577,7 +2594,7 @@ memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
25772594
goto end_block;
25782595
dest.len = dest.shape[0] * dest.itemsize;
25792596

2580-
ret = copy_single(&dest, &src);
2597+
ret = copy_single(self, &dest, &src);
25812598

25822599
end_block:
25832600
PyBuffer_Release(&src);
@@ -2593,7 +2610,7 @@ memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
25932610
ptr = ptr_from_tuple(view, key);
25942611
if (ptr == NULL)
25952612
return -1;
2596-
return pack_single(ptr, value, fmt);
2613+
return pack_single(self, ptr, value, fmt);
25972614
}
25982615
if (PySlice_Check(key) || is_multislice(key)) {
25992616
/* Call memory_subscript() to produce a sliced lvalue, then copy
@@ -3194,7 +3211,7 @@ memoryiter_next(memoryiterobject *it)
31943211
if (ptr == NULL) {
31953212
return NULL;
31963213
}
3197-
return unpack_single(ptr, it->it_fmt);
3214+
return unpack_single(seq, ptr, it->it_fmt);
31983215
}
31993216

32003217
it->it_seq = NULL;

0 commit comments

Comments
 (0)