FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143195: fix UAF in `{bytearray,memoryview}.hex(sep)` via re-entrant `sep.__len__` by picnixz · Pull Request #143209 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions Lib/test/test_bytes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,19 @@ def make_case():
with self.assertRaises(BufferError):
ba.rsplit(evil)

def test_hex_use_after_free(self):
# Prevent UAF in bytearray.hex(sep) with re-entrant sep.__len__.
# Regression test for https://github.com/python/cpython/issues/143195.
ba = bytearray(b'\xAA')

class S(bytes):
def __len__(self):
ba.clear()
return 1

self.assertRaises(BufferError, ba.hex, S(b':'))


class AssortedBytesTest(unittest.TestCase):
#
# Test various combinations of bytes and bytearray
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_memoryview.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@ def test_issue22668(self):
self.assertEqual(c.format, "H")
self.assertEqual(d.format, "H")

def test_hex_use_after_free(self):
# Prevent UAF in memoryview.hex(sep) with re-entrant sep.__len__.
# Regression test for https://github.com/python/cpython/issues/143195.
ba = bytearray(b'A' * 1024)
mv = memoryview(ba)

class S(bytes):
def __len__(self):
mv.release()
ba.clear()
return 1

self.assertRaises(BufferError, mv.hex, S(b':'))


# Variations on source objects for the buffer: bytes-like objects, then arrays
# with itemsize > 1.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix use-after-free crashes in :meth:`bytearray.hex` and :meth:`memoryview.hex`
when the separator's :meth:`~object.__len__` mutates the original object.
Patch by Bénédikt Tran.
8 changes: 7 additions & 1 deletion Objects/bytearrayobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,13 @@ bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
{
char* argbuf = PyByteArray_AS_STRING(self);
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
// Prevent 'self' from being freed if computing len(sep) mutates 'self'
// in _Py_strhex_with_sep().
// See: https://github.com/python/cpython/issues/143195.
self->ob_exports++;
PyObject *res = _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
self->ob_exports--;
return res;
}

static PyObject *
Expand Down
8 changes: 7 additions & 1 deletion Objects/memoryobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,13 @@ memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
CHECK_RELEASED(self);

if (MV_C_CONTIGUOUS(self->flags)) {
return _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
// Prevent 'self' from being freed if computing len(sep) mutates 'self'
// in _Py_strhex_with_sep().
// See: https://github.com/python/cpython/issues/143195.
self->exports++;
PyObject *ret = _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
self->exports--;
return ret;
}

PyBytesWriter *writer = PyBytesWriter_Create(src->len);
Expand Down
Loading

Back | FazBrowse Home | New Git URL