Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-103987: fix crash in mmap module #103990

Merged
merged 39 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d5cf0dd
gh-103987: fix crash in mmap module
Agent-Hellboy Apr 29, 2023
af99baa
fix test
Agent-Hellboy Apr 29, 2023
6b7af10
fix test
Agent-Hellboy Apr 29, 2023
6ab6a70
fix test
Agent-Hellboy Apr 29, 2023
50cc20b
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 29, 2023
4d26d53
fix doc
Agent-Hellboy Apr 29, 2023
d8a1365
fix test
Agent-Hellboy Apr 29, 2023
428338f
add new test
Agent-Hellboy Apr 30, 2023
0a8213f
fix test
Agent-Hellboy Apr 30, 2023
b460efe
add checks in mmap_ass_subscript
Agent-Hellboy Apr 30, 2023
55bd26e
fix test
Agent-Hellboy Apr 30, 2023
7adecc4
fix test
Agent-Hellboy Apr 30, 2023
caab85b
Merge branch 'main' into fix-issue-103987
sunmy2019 May 12, 2023
d8b9897
Update Misc/NEWS.d/next/Library/2023-04-29-18-23-16.gh-issue-103987.s…
sunmy2019 May 12, 2023
6d50192
gh-103987: fix crash in mmap module
Agent-Hellboy Apr 29, 2023
3784382
fix test
Agent-Hellboy Apr 29, 2023
5e19249
fix test
Agent-Hellboy Apr 29, 2023
b0d1fcd
fix test
Agent-Hellboy Apr 29, 2023
1482ee7
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 29, 2023
8c76209
fix doc
Agent-Hellboy Apr 29, 2023
2018297
fix test
Agent-Hellboy Apr 29, 2023
2cea232
add new test
Agent-Hellboy Apr 30, 2023
e0de742
fix test
Agent-Hellboy Apr 30, 2023
5389a41
add checks in mmap_ass_subscript
Agent-Hellboy Apr 30, 2023
b5e37d7
fix test
Agent-Hellboy Apr 30, 2023
ed1715e
fix test
Agent-Hellboy Apr 30, 2023
49d1094
Update Misc/NEWS.d/next/Library/2023-04-29-18-23-16.gh-issue-103987.s…
sunmy2019 May 12, 2023
61cdd6e
Add new test
Agent-Hellboy May 12, 2023
5c8d709
Merge branch 'fix-issue-103987' of https://github.com/Agent-Hellboy/c…
Agent-Hellboy May 12, 2023
341b90a
Update Misc/NEWS.d/next/Library/2023-04-29-18-23-16.gh-issue-103987.s…
sunmy2019 May 12, 2023
6dece9f
Update Misc/NEWS.d/next/Library/2023-04-29-18-23-16.gh-issue-103987.s…
sunmy2019 May 12, 2023
5cf8f90
fix test
Agent-Hellboy May 12, 2023
93163d9
Merge branch 'fix-issue-103988' of https://github.com/Agent-Hellboy/c…
Agent-Hellboy May 12, 2023
3b61c74
Merge branch 'main' into fix-issue-103987
sunmy2019 May 17, 2023
f2d2d00
Merge branch 'main' into fix-issue-103987
sunmy2019 May 19, 2023
843c2eb
add more checks, fix test cases
sunmy2019 May 19, 2023
168f5b1
Update Misc/NEWS.d/next/Library/2023-04-29-18-23-16.gh-issue-103987.s…
sunmy2019 May 19, 2023
435ed41
remove trailing spaces
sunmy2019 May 19, 2023
320feac
Update Lib/test/test_mmap.py
sunmy2019 May 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,102 @@ def test_bad_file_desc(self):
# Try opening a bad file descriptor...
self.assertRaises(OSError, mmap.mmap, -2, 4096)

def test_unexpected_mmap_close_scenario_1(self):
# See gh-103987
with open(TESTFN, "wb+") as f:
data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()

class X:
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), n, access=mmap.ACCESS_READ)

self.assertEqual(m[1], 97)
with self.assertRaises(ValueError):
m[X()] # should not crash


def test_unexpected_mmap_close_scenario_2(self):
# See gh-103987
with open(TESTFN, "wb+") as f:
data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()

class X:
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), n, access=mmap.ACCESS_READ)

self.assertEqual(m[1], 97)
with self.assertRaises(ValueError):
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
m[X():5] # should not crash

def test_unexpected_mmap_close_scenario_3(self):
# See gh-103987
with open(TESTFN, "wb+") as f:
data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()

class X:
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), n, access=mmap.ACCESS_WRITE)


with self.assertRaises(ValueError):
m[X()] = 1 # should not crash

def test_unexpected_mmap_close_scenario_4(self):
# See gh-103987
with open(TESTFN, "wb+") as f:
data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()

class X:
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), n, access=mmap.ACCESS_WRITE)


with self.assertRaises(ValueError):
m[X():5] = b'abcd' # should not crash

def test_unexpected_mmap_close_scenario_5(self):
# See gh-103987
with open(TESTFN, "wb+") as f:
data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()

class X:
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), n, access=mmap.ACCESS_WRITE)


with self.assertRaises(ValueError):
m[X():5:1] = b'abcd' # should not crash

def test_tougher_find(self):
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# searching for data with embedded \0 bytes didn't work.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add more ``CHECK_VALID`` s in ``mmapmodule.c`` to avoid the file being invalidated by Python code.
sunmy2019 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
"mmap index out of range");
return NULL;
}
CHECK_VALID(NULL);
return PyLong_FromLong(Py_CHARMASK(self->data[i]));
}
else if (PySlice_Check(item)) {
Expand All @@ -953,6 +954,7 @@ mmap_subscript(mmap_object *self, PyObject *item)
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
CHECK_VALID(NULL);
slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step);

if (slicelen <= 0)
Expand All @@ -968,6 +970,8 @@ mmap_subscript(mmap_object *self, PyObject *item)

if (result_buf == NULL)
return PyErr_NoMemory();
CHECK_VALID(NULL);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the buffer have been invalidated here as a side effect of the PyMem_Malloc call above triggering GC? I seem to recall that's possible, but not 100% sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find any GC-related code in Object/obmalloc.c

for (cur = start, i = 0; i < slicelen;
cur += step, i++) {
result_buf[i] = self->data[cur];
Expand Down Expand Up @@ -1052,6 +1056,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
"in range(0, 256)");
return -1;
}
CHECK_VALID(-1);
self->data[i] = (char) v;
return 0;
}
Expand All @@ -1077,6 +1082,7 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
return -1;
}

CHECK_VALID(-1);
if (slicelen == 0) {
}
else if (step == 1) {
Expand Down