Skip to content

Commit ceaa4c3

Browse files
Agent-Hellboysunmy2019JelleZijlstra
authored
pythongh-103987: fix several crashes in mmap module (python#103990)
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 3bc94e6 commit ceaa4c3

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

Lib/test/test_mmap.py

+86-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ def test_move(self):
407407
m.move(0, 0, 1)
408408
m.move(0, 0, 0)
409409

410-
411410
def test_anonymous(self):
412411
# anonymous mmap.mmap(-1, PAGE)
413412
m = mmap.mmap(-1, PAGESIZE)
@@ -887,6 +886,92 @@ def test_resize_succeeds_with_error_for_second_named_mapping(self):
887886
self.assertEqual(m1[:data_length], data)
888887
self.assertEqual(m2[:data_length], data)
889888

889+
def test_mmap_closed_by_int_scenarios(self):
890+
"""
891+
gh-103987: Test that mmap objects raise ValueError
892+
for closed mmap files
893+
"""
894+
895+
class MmapClosedByIntContext:
896+
def __init__(self, access) -> None:
897+
self.access = access
898+
899+
def __enter__(self):
900+
self.f = open(TESTFN, "w+b")
901+
self.f.write(random.randbytes(100))
902+
self.f.flush()
903+
904+
m = mmap.mmap(self.f.fileno(), 100, access=self.access)
905+
906+
class X:
907+
def __index__(self):
908+
m.close()
909+
return 10
910+
911+
return (m, X)
912+
913+
def __exit__(self, exc_type, exc_value, traceback):
914+
self.f.close()
915+
916+
read_access_modes = [
917+
mmap.ACCESS_READ,
918+
mmap.ACCESS_WRITE,
919+
mmap.ACCESS_COPY,
920+
mmap.ACCESS_DEFAULT,
921+
]
922+
923+
write_access_modes = [
924+
mmap.ACCESS_WRITE,
925+
mmap.ACCESS_COPY,
926+
mmap.ACCESS_DEFAULT,
927+
]
928+
929+
for access in read_access_modes:
930+
with MmapClosedByIntContext(access) as (m, X):
931+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
932+
m[X()]
933+
934+
with MmapClosedByIntContext(access) as (m, X):
935+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
936+
m[X() : 20]
937+
938+
with MmapClosedByIntContext(access) as (m, X):
939+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
940+
m[X() : 20 : 2]
941+
942+
with MmapClosedByIntContext(access) as (m, X):
943+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
944+
m[20 : X() : -2]
945+
946+
with MmapClosedByIntContext(access) as (m, X):
947+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
948+
m.read(X())
949+
950+
with MmapClosedByIntContext(access) as (m, X):
951+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
952+
m.find(b"1", 1, X())
953+
954+
for access in write_access_modes:
955+
with MmapClosedByIntContext(access) as (m, X):
956+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
957+
m[X() : 20] = b"1" * 10
958+
959+
with MmapClosedByIntContext(access) as (m, X):
960+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
961+
m[X() : 20 : 2] = b"1" * 5
962+
963+
with MmapClosedByIntContext(access) as (m, X):
964+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
965+
m[20 : X() : -2] = b"1" * 5
966+
967+
with MmapClosedByIntContext(access) as (m, X):
968+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
969+
m.move(1, 2, X())
970+
971+
with MmapClosedByIntContext(access) as (m, X):
972+
with self.assertRaisesRegex(ValueError, "mmap closed or invalid"):
973+
m.write_byte(X())
974+
890975
class LargeMmapTests(unittest.TestCase):
891976

892977
def setUp(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :mod:`mmap`, fix several bugs that could lead to access to memory-mapped files after
2+
they have been invalidated.

Modules/mmapmodule.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ mmap_read_method(mmap_object *self,
284284

285285
CHECK_VALID(NULL);
286286
if (!PyArg_ParseTuple(args, "|O&:read", _Py_convert_optional_to_ssize_t, &num_bytes))
287-
return(NULL);
287+
return NULL;
288+
CHECK_VALID(NULL);
288289

289290
/* silently 'adjust' out-of-range requests */
290291
remaining = (self->pos < self->size) ? self->size - self->pos : 0;
@@ -325,6 +326,7 @@ mmap_gfind(mmap_object *self,
325326
end = self->size;
326327

327328
Py_ssize_t res;
329+
CHECK_VALID(NULL);
328330
if (reverse) {
329331
res = _PyBytes_ReverseFind(
330332
self->data + start, end - start,
@@ -388,7 +390,7 @@ mmap_write_method(mmap_object *self,
388390

389391
CHECK_VALID(NULL);
390392
if (!PyArg_ParseTuple(args, "y*:write", &data))
391-
return(NULL);
393+
return NULL;
392394

393395
if (!is_writable(self)) {
394396
PyBuffer_Release(&data);
@@ -401,6 +403,7 @@ mmap_write_method(mmap_object *self,
401403
return NULL;
402404
}
403405

406+
CHECK_VALID(NULL);
404407
memcpy(&self->data[self->pos], data.buf, data.len);
405408
self->pos += data.len;
406409
PyBuffer_Release(&data);
@@ -420,6 +423,7 @@ mmap_write_byte_method(mmap_object *self,
420423
if (!is_writable(self))
421424
return NULL;
422425

426+
CHECK_VALID(NULL);
423427
if (self->pos < self->size) {
424428
self->data[self->pos++] = value;
425429
Py_RETURN_NONE;
@@ -724,6 +728,7 @@ mmap_move_method(mmap_object *self, PyObject *args)
724728
if (self->size - dest < cnt || self->size - src < cnt)
725729
goto bounds;
726730

731+
CHECK_VALID(NULL);
727732
memmove(&self->data[dest], &self->data[src], cnt);
728733

729734
Py_RETURN_NONE;
@@ -847,6 +852,7 @@ mmap_madvise_method(mmap_object *self, PyObject *args)
847852
length = self->size - start;
848853
}
849854

855+
CHECK_VALID(NULL);
850856
if (madvise(self->data + start, length, option) != 0) {
851857
PyErr_SetFromErrno(PyExc_OSError);
852858
return NULL;
@@ -945,6 +951,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
945951
"mmap index out of range");
946952
return NULL;
947953
}
954+
CHECK_VALID(NULL);
948955
return PyLong_FromLong(Py_CHARMASK(self->data[i]));
949956
}
950957
else if (PySlice_Check(item)) {
@@ -955,6 +962,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
955962
}
956963
slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step);
957964

965+
CHECK_VALID(NULL);
958966
if (slicelen <= 0)
959967
return PyBytes_FromStringAndSize("", 0);
960968
else if (step == 1)
@@ -968,6 +976,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
968976

969977
if (result_buf == NULL)
970978
return PyErr_NoMemory();
979+
971980
for (cur = start, i = 0; i < slicelen;
972981
cur += step, i++) {
973982
result_buf[i] = self->data[cur];
@@ -1052,6 +1061,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
10521061
"in range(0, 256)");
10531062
return -1;
10541063
}
1064+
CHECK_VALID(-1);
10551065
self->data[i] = (char) v;
10561066
return 0;
10571067
}
@@ -1077,6 +1087,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
10771087
return -1;
10781088
}
10791089

1090+
CHECK_VALID(-1);
10801091
if (slicelen == 0) {
10811092
}
10821093
else if (step == 1) {

0 commit comments

Comments
 (0)