Skip to content

Commit 3a6c6e0

Browse files
hugovkserhiy-storchakagpshead
authored andcommitted
[3.9] pythongh-119511: Fix a potential denial of service in imaplib (pythonGH-119514) (python#130248)
The IMAP4 client could consume an arbitrary amount of memory when trying to connect to a malicious server, because it read a "literal" data with a single read(size) call, and BufferedReader.read() allocates the bytes object of the specified size before reading. Now the IMAP4 client reads data by chunks, therefore the amount of used memory is limited by the amount of the data actually been sent by the server. (cherry picked from commit 735f25c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 66af90f commit 3a6c6e0

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Lib/imaplib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
# search command can be quite large, so we now use 1M.
5353
_MAXLINE = 1000000
5454

55+
# Data larger than this will be read in chunks, to prevent extreme
56+
# overallocation.
57+
_SAFE_BUF_SIZE = 1 << 20
5558

5659
# Commands
5760

@@ -306,7 +309,13 @@ def open(self, host = '', port = IMAP4_PORT):
306309

307310
def read(self, size):
308311
"""Read 'size' bytes from remote."""
309-
return self.file.read(size)
312+
cursize = min(size, _SAFE_BUF_SIZE)
313+
data = self.file.read(cursize)
314+
while cursize < size and len(data) == cursize:
315+
delta = min(cursize, size - cursize)
316+
data += self.file.read(delta)
317+
cursize += delta
318+
return data
310319

311320

312321
def readline(self):

Lib/test/test_imaplib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,21 @@ def handle(self):
857857
self.assertRaises(imaplib.IMAP4.error,
858858
self.imap_class, *server.server_address)
859859

860+
@reap_threads
861+
def test_truncated_large_literal(self):
862+
size = 0
863+
class BadHandler(SimpleIMAPHandler):
864+
def handle(self):
865+
self._send_textline('* OK {%d}' % size)
866+
self._send_textline('IMAP4rev1')
867+
868+
for exponent in range(15, 64):
869+
size = 1 << exponent
870+
with self.subTest(f"size=2e{size}"):
871+
with self.reaped_server(BadHandler) as server:
872+
with self.assertRaises(imaplib.IMAP4.abort):
873+
self.imap_class(*server.server_address)
874+
860875
@reap_threads
861876
def test_simple_with_statement(self):
862877
# simplest call
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Fix a potential denial of service in the :mod:`imaplib` module. When connecting
2+
to a malicious server, it could cause an arbitrary amount of memory to be
3+
allocated. On many systems this is harmless as unused virtual memory is only a
4+
mapping, but if this hit a virtual address size limit it could lead to a
5+
:exc:`MemoryError` or other process crash. On unusual systems or builds where
6+
all allocated memory is touched and backed by actual ram or storage it could've
7+
consumed resources doing so until similarly crashing.

0 commit comments

Comments
 (0)