diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index cbef37441dd..9b7100d7a29 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -234,6 +234,14 @@ class MmapTests(unittest.TestCase): flags=mmap.MAP_PRIVATE, prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE) + # Try writing with PROT_EXEC and without PROT_WRITE + prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0) + with open(TESTFN, "r+b") as f: + m = mmap.mmap(f.fileno(), mapsize, prot=prot) + self.assertRaises(TypeError, m.write, b"abcdef") + self.assertRaises(TypeError, m.write_byte, 0) + m.close() + def test_bad_file_desc(self): # Try opening a bad file descriptor... self.assertRaises(mmap.error, mmap.mmap, -2, 4096) diff --git a/Misc/NEWS b/Misc/NEWS index fcbcc882a97..0b5b74b9377 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -31,6 +31,10 @@ Core and Builtins Library ------- +- Issue #11391: Writing to a mmap object created with + ``mmap.PROT_READ|mmap.PROT_EXEC`` would segfault instead of raising a + TypeError. Patch by Charles-François Natali. + - Issue #11306: mailbox in certain cases adapts to an inability to open certain files in read-write mode. Previously it detected this by checking for EACCES, now it also checks for EROFS. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 02e0989fcc7..fdf39223567 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1106,17 +1106,22 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) prot = PROT_READ | PROT_WRITE; break; case ACCESS_DEFAULT: - /* use the specified or default values of flags and prot */ + /* map prot to access type */ + if ((prot & PROT_READ) && (prot & PROT_WRITE)) { + /* ACCESS_DEFAULT */ + } + else if (prot & PROT_WRITE) { + access = ACCESS_WRITE; + } + else { + access = ACCESS_READ; + } break; default: return PyErr_Format(PyExc_ValueError, "mmap invalid access parameter."); } - if (prot == PROT_READ) { - access = ACCESS_READ; - } - #ifdef HAVE_FSTAT # ifdef __VMS /* on OpenVMS we must ensure that all bytes are written to the file */