Skip to content

Commit ab86426

Browse files
authored
gh-105235: Prevent reading outside buffer during mmap.find() (#105252)
* Add a special case for s[-m:] == p in _PyBytes_Find * Add tests for _PyBytes_Find * Make sure that start <= end in mmap.find
1 parent 2d43bee commit ab86426

File tree

5 files changed

+161
-3
lines changed

5 files changed

+161
-3
lines changed

Lib/test/test_mmap.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,27 @@ def test_find_end(self):
299299
self.assertEqual(m.find(b'one', 1, -2), -1)
300300
self.assertEqual(m.find(bytearray(b'one')), 0)
301301

302+
for i in range(-n-1, n+1):
303+
for j in range(-n-1, n+1):
304+
for p in [b"o", b"on", b"two", b"ones", b"s"]:
305+
expected = data.find(p, i, j)
306+
self.assertEqual(m.find(p, i, j), expected, (p, i, j))
307+
308+
def test_find_does_not_access_beyond_buffer(self):
309+
try:
310+
flags = mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS
311+
PAGESIZE = mmap.PAGESIZE
312+
PROT_NONE = 0
313+
PROT_READ = mmap.PROT_READ
314+
except AttributeError as e:
315+
raise unittest.SkipTest("mmap flags unavailable") from e
316+
for i in range(0, 2049):
317+
with mmap.mmap(-1, PAGESIZE * (i + 1),
318+
flags=flags, prot=PROT_NONE) as guard:
319+
with mmap.mmap(-1, PAGESIZE * (i + 2048),
320+
flags=flags, prot=PROT_READ) as fm:
321+
fm.find(b"fo", -2)
322+
302323

303324
def test_rfind(self):
304325
# test the new 'end' parameter works as expected
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent out-of-bounds memory access during ``mmap.find()`` calls.

Modules/_testinternalcapi.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "interpreteridobject.h" // _PyInterpreterID_LookUp()
1515
#include "pycore_atomic_funcs.h" // _Py_atomic_int_get()
1616
#include "pycore_bitutils.h" // _Py_bswap32()
17+
#include "pycore_bytesobject.h" // _PyBytes_Find()
1718
#include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble
1819
#include "pycore_ceval.h" // _PyEval_AddPendingCall
1920
#include "pycore_fileutils.h" // _Py_normpath
@@ -443,6 +444,118 @@ test_edit_cost(PyObject *self, PyObject *Py_UNUSED(args))
443444
}
444445

445446

447+
static int
448+
check_bytes_find(const char *haystack0, const char *needle0,
449+
int offset, Py_ssize_t expected)
450+
{
451+
Py_ssize_t len_haystack = strlen(haystack0);
452+
Py_ssize_t len_needle = strlen(needle0);
453+
Py_ssize_t result_1 = _PyBytes_Find(haystack0, len_haystack,
454+
needle0, len_needle, offset);
455+
if (result_1 != expected) {
456+
PyErr_Format(PyExc_AssertionError,
457+
"Incorrect result_1: '%s' in '%s' (offset=%zd)",
458+
needle0, haystack0, offset);
459+
return -1;
460+
}
461+
// Allocate new buffer with no NULL terminator.
462+
char *haystack = PyMem_Malloc(len_haystack);
463+
if (haystack == NULL) {
464+
PyErr_NoMemory();
465+
return -1;
466+
}
467+
char *needle = PyMem_Malloc(len_needle);
468+
if (needle == NULL) {
469+
PyMem_Free(haystack);
470+
PyErr_NoMemory();
471+
return -1;
472+
}
473+
memcpy(haystack, haystack0, len_haystack);
474+
memcpy(needle, needle0, len_needle);
475+
Py_ssize_t result_2 = _PyBytes_Find(haystack, len_haystack,
476+
needle, len_needle, offset);
477+
PyMem_Free(haystack);
478+
PyMem_Free(needle);
479+
if (result_2 != expected) {
480+
PyErr_Format(PyExc_AssertionError,
481+
"Incorrect result_2: '%s' in '%s' (offset=%zd)",
482+
needle0, haystack0, offset);
483+
return -1;
484+
}
485+
return 0;
486+
}
487+
488+
static int
489+
check_bytes_find_large(Py_ssize_t len_haystack, Py_ssize_t len_needle,
490+
const char *needle)
491+
{
492+
char *zeros = PyMem_RawCalloc(len_haystack, 1);
493+
if (zeros == NULL) {
494+
PyErr_NoMemory();
495+
return -1;
496+
}
497+
Py_ssize_t res = _PyBytes_Find(zeros, len_haystack, needle, len_needle, 0);
498+
PyMem_RawFree(zeros);
499+
if (res != -1) {
500+
PyErr_Format(PyExc_AssertionError,
501+
"check_bytes_find_large(%zd, %zd) found %zd",
502+
len_haystack, len_needle, res);
503+
return -1;
504+
}
505+
return 0;
506+
}
507+
508+
static PyObject *
509+
test_bytes_find(PyObject *self, PyObject *Py_UNUSED(args))
510+
{
511+
#define CHECK(H, N, O, E) do { \
512+
if (check_bytes_find(H, N, O, E) < 0) { \
513+
return NULL; \
514+
} \
515+
} while (0)
516+
517+
CHECK("", "", 0, 0);
518+
CHECK("Python", "", 0, 0);
519+
CHECK("Python", "", 3, 3);
520+
CHECK("Python", "", 6, 6);
521+
CHECK("Python", "yth", 0, 1);
522+
CHECK("ython", "yth", 1, 1);
523+
CHECK("thon", "yth", 2, -1);
524+
CHECK("Python", "thon", 0, 2);
525+
CHECK("ython", "thon", 1, 2);
526+
CHECK("thon", "thon", 2, 2);
527+
CHECK("hon", "thon", 3, -1);
528+
CHECK("Pytho", "zz", 0, -1);
529+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "ab", 0, -1);
530+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "ba", 0, -1);
531+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bb", 0, -1);
532+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", "ab", 0, 30);
533+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba", "ba", 0, 30);
534+
CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb", "bb", 0, 30);
535+
#undef CHECK
536+
537+
// Hunt for segfaults
538+
// n, m chosen here so that (n - m) % (m + 1) == 0
539+
// This would make default_find in fastsearch.h access haystack[n].
540+
if (check_bytes_find_large(2048, 2, "ab") < 0) {
541+
return NULL;
542+
}
543+
if (check_bytes_find_large(4096, 16, "0123456789abcdef") < 0) {
544+
return NULL;
545+
}
546+
if (check_bytes_find_large(8192, 2, "ab") < 0) {
547+
return NULL;
548+
}
549+
if (check_bytes_find_large(16384, 4, "abcd") < 0) {
550+
return NULL;
551+
}
552+
if (check_bytes_find_large(32768, 2, "ab") < 0) {
553+
return NULL;
554+
}
555+
Py_RETURN_NONE;
556+
}
557+
558+
446559
static PyObject *
447560
normalize_path(PyObject *self, PyObject *filename)
448561
{
@@ -1328,6 +1441,7 @@ static PyMethodDef module_functions[] = {
13281441
{"reset_path_config", test_reset_path_config, METH_NOARGS},
13291442
{"test_atomic_funcs", test_atomic_funcs, METH_NOARGS},
13301443
{"test_edit_cost", test_edit_cost, METH_NOARGS},
1444+
{"test_bytes_find", test_bytes_find, METH_NOARGS},
13311445
{"normalize_path", normalize_path, METH_O, NULL},
13321446
{"get_getpath_codeobject", get_getpath_codeobject, METH_NOARGS, NULL},
13331447
{"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},

Modules/mmapmodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,17 @@ mmap_gfind(mmap_object *self,
342342

343343
Py_ssize_t res;
344344
CHECK_VALID_OR_RELEASE(NULL, view);
345-
if (reverse) {
345+
if (end < start) {
346+
res = -1;
347+
}
348+
else if (reverse) {
349+
assert(0 <= start && start <= end && end <= self->size);
346350
res = _PyBytes_ReverseFind(
347351
self->data + start, end - start,
348352
view.buf, view.len, start);
349353
}
350354
else {
355+
assert(0 <= start && start <= end && end <= self->size);
351356
res = _PyBytes_Find(
352357
self->data + start, end - start,
353358
view.buf, view.len, start);

Objects/bytesobject.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,8 +1272,25 @@ _PyBytes_Find(const char *haystack, Py_ssize_t len_haystack,
12721272
const char *needle, Py_ssize_t len_needle,
12731273
Py_ssize_t offset)
12741274
{
1275-
return stringlib_find(haystack, len_haystack,
1276-
needle, len_needle, offset);
1275+
assert(len_haystack >= 0);
1276+
assert(len_needle >= 0);
1277+
// Extra checks because stringlib_find accesses haystack[len_haystack].
1278+
if (len_needle == 0) {
1279+
return offset;
1280+
}
1281+
if (len_needle > len_haystack) {
1282+
return -1;
1283+
}
1284+
assert(len_haystack >= 1);
1285+
Py_ssize_t res = stringlib_find(haystack, len_haystack - 1,
1286+
needle, len_needle, offset);
1287+
if (res == -1) {
1288+
Py_ssize_t last_align = len_haystack - len_needle;
1289+
if (memcmp(haystack + last_align, needle, len_needle) == 0) {
1290+
return offset + last_align;
1291+
}
1292+
}
1293+
return res;
12771294
}
12781295

12791296
Py_ssize_t

0 commit comments

Comments
 (0)