Information disclosure in mmap module - python 2.7.12
The mmap module's resize() function updated only the size variable and ignored the position variable, so shrinking a mapped file could leave the position greater than the size. A later read() or readline() call could then read data from an adjacent memory page, leaking information from the process's memory.
Originally disclosed on HackerOne
- Fixed in 56 days
- Paid in 56 days
- Disclosed after 3.0 years
First thing first, the report was sent to python's security mailing list on the 27.8.16 and was fixed by benjamin on the 5.10.16 (rev 144f10202076), and acknowledged be me today (8.10.16).
In a security audit I made to the mmap module in python (2.7.12), I have found a major information leak vulnerability. Here are the relevant details:
- mmap module uses size_t pos, size for boundary checks before read/write access to the mmaped file
- The resize() function, in both windows and linux, updates only the size variable, and ignores the pos variable
- resize() that shrinks the mapped file can lead to a situation in which pos > size, thus breaking the code's invariant
- A later call to read() or readline() can leverage this point into reading from a different memory page:
- read():
- The only check is an assert() that size >= pos
- Than an int variable is declared and used:
- n = self->size - self->pos; // n is now negative
- n is adjusted to be: n = PY_SSIZE_T_MAX;
- n is now a poor upper bound to the read operation that follows
- readline():
- no size checks are done, the checks are passed to the memchr() function
- memchr(start, '\n', self->size - self->pos)
- memchr() receives size_t as the length parameter, thus making the size bound negligible
- read will now "steal" data from another page: PyString_FromStringAndSize(start, (eol - start))
The exploitation will only work in case that there is an adjacent memory page, otherwise the read() / memchr() will cause a segfault while reading outside of the process memory mapped pages.
I have exploited the vulnerability and demonstrated it's possible effects on a windows 7 computer. On a linux machine there should be (probably) some small adjustments to the demo script. Attached are the attack script and it's result in a successful attack attempt.
As i mentioned at the beginning, the vulnerability was fixed and acknowledged to block the described attacks. Eyal.
Attachments

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the resize() function failed to update the position variable when shrinking the mapped file, breaking the invariant that position must not exceed size, and subsequent read operations used an incorrect size calculation that allowed reading beyond the mapped region.
Impact
An attacker could read data from an adjacent memory page, potentially disclosing sensitive information, but the exploit only works if an adjacent memory page exists; otherwise it causes a segfault.
What the attacker needed
Ability to run Python code using mmap; adjacent memory page must exist
The fix
The vulnerability was fixed, though the text does not describe how.
How it was found
- Create a memory-mapped file and set the position beyond the new size after shrinking.
- Call read() or readline() to trigger reading from an adjacent memory page.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
The mmap module lets a Python program treat a file like a block of memory. The code tracks two numbers: where you are reading (position) and how big the file is (size). When the file was made smaller, only the size was updated, so the position could point past the end. A read then went past the file and grabbed whatever was next in memory, which could be secret data.
What is Info disclosure?
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.