Skip to content

Commit a9e068f

Browse files
cmaloneyfatelei
andauthored
[3.13] gh-142560: prevent use-after-free in search-like methods by exporting buffer in bytearray (GH-142938) (GH-142986)
(cherry picked from commit 220f0b1) Co-authored-by: wangxiaolei <fatelei@gmail.com>
1 parent 8802556 commit a9e068f

File tree

3 files changed

+105
-41
lines changed

3 files changed

+105
-41
lines changed

Lib/test/test_bytes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,37 @@ def __index__(self):
18971897
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
18981898
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")
18991899

1900+
def test_search_methods_reentrancy_raises_buffererror(self):
1901+
# gh-142560: Raise BufferError if buffer mutates during search arg conversion.
1902+
class Evil:
1903+
def __init__(self, ba):
1904+
self.ba = ba
1905+
def __buffer__(self, flags):
1906+
self.ba.clear()
1907+
return memoryview(self.ba)
1908+
def __release_buffer__(self, view: memoryview) -> None:
1909+
view.release()
1910+
def __index__(self):
1911+
self.ba.clear()
1912+
return ord("A")
1913+
1914+
def make_case():
1915+
ba = bytearray(b"A")
1916+
return ba, Evil(ba)
1917+
1918+
for name in ("find", "count", "index", "rindex", "rfind"):
1919+
ba, evil = make_case()
1920+
with self.subTest(name):
1921+
with self.assertRaises(BufferError):
1922+
getattr(ba, name)(evil)
1923+
1924+
ba, evil = make_case()
1925+
with self.assertRaises(BufferError):
1926+
evil in ba
1927+
with self.assertRaises(BufferError):
1928+
ba.split(evil)
1929+
with self.assertRaises(BufferError):
1930+
ba.rsplit(evil)
19001931

19011932
class AssortedBytesTest(unittest.TestCase):
19021933
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix use-after-free in :class:`bytearray` search-like methods (:meth:`~bytearray.find`, :meth:`~bytearray.count`, :meth:`~bytearray.index`, :meth:`~bytearray.rindex`, and :meth:`~bytearray.rfind`) by marking the storage as exported which causes reallocation attempts to raise :exc:`BufferError`. For :func:`~operator.contains`, :meth:`~bytearray.split`, and :meth:`~bytearray.rsplit` the :ref:`buffer protocol <bufferobjects>` is used for this.

Objects/bytearrayobject.c

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
6464
assert(obj->ob_exports >= 0);
6565
}
6666

67+
typedef PyObject* (*_ba_bytes_op)(const char *buf, Py_ssize_t len,
68+
PyObject *sub, Py_ssize_t start,
69+
Py_ssize_t end);
70+
71+
static PyObject *
72+
_bytearray_with_buffer(PyByteArrayObject *self, _ba_bytes_op op, PyObject *sub,
73+
Py_ssize_t start, Py_ssize_t end)
74+
{
75+
PyObject *res;
76+
77+
/* Increase exports to prevent bytearray storage from changing during op. */
78+
self->ob_exports++;
79+
res = op(PyByteArray_AS_STRING(self), Py_SIZE(self), sub, start, end);
80+
self->ob_exports--;
81+
82+
return res;
83+
}
84+
6785
static int
6886
_canresize(PyByteArrayObject *self)
6987
{
@@ -1146,8 +1164,7 @@ bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
11461164
Py_ssize_t end)
11471165
/*[clinic end generated code: output=413e1cab2ae87da0 input=793dfad803e2952f]*/
11481166
{
1149-
return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1150-
sub, start, end);
1167+
return _bytearray_with_buffer(self, _Py_bytes_find, sub, start, end);
11511168
}
11521169

11531170
/*[clinic input]
@@ -1161,8 +1178,7 @@ bytearray_count_impl(PyByteArrayObject *self, PyObject *sub,
11611178
Py_ssize_t start, Py_ssize_t end)
11621179
/*[clinic end generated code: output=a21ee2692e4f1233 input=4deb529db38deda8]*/
11631180
{
1164-
return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1165-
sub, start, end);
1181+
return _bytearray_with_buffer(self, _Py_bytes_count, sub, start, end);
11661182
}
11671183

11681184
/*[clinic input]
@@ -1207,8 +1223,7 @@ bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
12071223
Py_ssize_t start, Py_ssize_t end)
12081224
/*[clinic end generated code: output=067a1e78efc672a7 input=8cbaf6836dbd2a9a]*/
12091225
{
1210-
return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1211-
sub, start, end);
1226+
return _bytearray_with_buffer(self, _Py_bytes_index, sub, start, end);
12121227
}
12131228

12141229
/*[clinic input]
@@ -1224,8 +1239,7 @@ bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub,
12241239
Py_ssize_t start, Py_ssize_t end)
12251240
/*[clinic end generated code: output=51bf886f932b283c input=eaa107468a158423]*/
12261241
{
1227-
return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1228-
sub, start, end);
1242+
return _bytearray_with_buffer(self, _Py_bytes_rfind, sub, start, end);
12291243
}
12301244

12311245
/*[clinic input]
@@ -1241,14 +1255,22 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
12411255
Py_ssize_t start, Py_ssize_t end)
12421256
/*[clinic end generated code: output=38e1cf66bafb08b9 input=81cf49d0af4d5bd0]*/
12431257
{
1244-
return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1245-
sub, start, end);
1258+
return _bytearray_with_buffer(self, _Py_bytes_rindex, sub, start, end);
12461259
}
12471260

12481261
static int
12491262
bytearray_contains(PyObject *self, PyObject *arg)
12501263
{
1251-
return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1264+
int ret = -1;
1265+
PyByteArrayObject *ba = _PyByteArray_CAST(self);
1266+
1267+
/* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
1268+
ba->ob_exports++;
1269+
ret = _Py_bytes_contains(PyByteArray_AS_STRING(ba),
1270+
PyByteArray_GET_SIZE(self),
1271+
arg);
1272+
ba->ob_exports--;
1273+
return ret;
12521274
}
12531275

12541276
/*[clinic input]
@@ -1271,8 +1293,7 @@ bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj,
12711293
Py_ssize_t start, Py_ssize_t end)
12721294
/*[clinic end generated code: output=a3d9b6d44d3662a6 input=76385e0b376b45c1]*/
12731295
{
1274-
return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1275-
subobj, start, end);
1296+
return _bytearray_with_buffer(self, _Py_bytes_startswith, subobj, start, end);
12761297
}
12771298

12781299
/*[clinic input]
@@ -1295,8 +1316,7 @@ bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj,
12951316
Py_ssize_t start, Py_ssize_t end)
12961317
/*[clinic end generated code: output=e75ea8c227954caa input=9b8baa879aa3d74b]*/
12971318
{
1298-
return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1299-
subobj, start, end);
1319+
return _bytearray_with_buffer(self, _Py_bytes_endswith, subobj, start, end);
13001320
}
13011321

13021322
/*[clinic input]
@@ -1539,26 +1559,32 @@ bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
15391559
Py_ssize_t maxsplit)
15401560
/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
15411561
{
1542-
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1543-
const char *s = PyByteArray_AS_STRING(self), *sub;
1544-
PyObject *list;
1545-
Py_buffer vsub;
1562+
PyObject *list = NULL;
1563+
1564+
/* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
1565+
self->ob_exports++;
1566+
const char *sbuf = PyByteArray_AS_STRING(self);
1567+
Py_ssize_t slen = PyByteArray_GET_SIZE((PyObject *)self);
15461568

15471569
if (maxsplit < 0)
15481570
maxsplit = PY_SSIZE_T_MAX;
15491571

1550-
if (sep == Py_None)
1551-
return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
1572+
if (sep == Py_None) {
1573+
list = stringlib_split_whitespace((PyObject*)self, sbuf, slen, maxsplit);
1574+
goto done;
1575+
}
15521576

1553-
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1554-
return NULL;
1555-
sub = vsub.buf;
1556-
n = vsub.len;
1577+
Py_buffer vsub;
1578+
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) {
1579+
goto done;
1580+
}
15571581

1558-
list = stringlib_split(
1559-
(PyObject*) self, s, len, sub, n, maxsplit
1560-
);
1582+
list = stringlib_split((PyObject*)self, sbuf, slen,
1583+
(const char *)vsub.buf, vsub.len, maxsplit);
15611584
PyBuffer_Release(&vsub);
1585+
1586+
done:
1587+
self->ob_exports--;
15621588
return list;
15631589
}
15641590

@@ -1650,26 +1676,32 @@ bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
16501676
Py_ssize_t maxsplit)
16511677
/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
16521678
{
1653-
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1654-
const char *s = PyByteArray_AS_STRING(self), *sub;
1655-
PyObject *list;
1656-
Py_buffer vsub;
1679+
PyObject *list = NULL;
1680+
1681+
/* Increase exports to prevent bytearray storage from changing during _Py_bytes_contains(). */
1682+
self->ob_exports++;
1683+
const char *sbuf = PyByteArray_AS_STRING(self);
1684+
Py_ssize_t slen = PyByteArray_GET_SIZE((PyObject *)self);
16571685

16581686
if (maxsplit < 0)
16591687
maxsplit = PY_SSIZE_T_MAX;
16601688

1661-
if (sep == Py_None)
1662-
return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
1689+
if (sep == Py_None) {
1690+
list = stringlib_rsplit_whitespace((PyObject*)self, sbuf, slen, maxsplit);
1691+
goto done;
1692+
}
16631693

1664-
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1665-
return NULL;
1666-
sub = vsub.buf;
1667-
n = vsub.len;
1694+
Py_buffer vsub;
1695+
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) {
1696+
goto done;
1697+
}
16681698

1669-
list = stringlib_rsplit(
1670-
(PyObject*) self, s, len, sub, n, maxsplit
1671-
);
1699+
list = stringlib_rsplit((PyObject*)self, sbuf, slen,
1700+
(const char *)vsub.buf, vsub.len, maxsplit);
16721701
PyBuffer_Release(&vsub);
1702+
1703+
done:
1704+
self->ob_exports--;
16731705
return list;
16741706
}
16751707

0 commit comments

Comments
 (0)