Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
gh-119511: Fix OOM vulnerability in imaplib
The IMAP4 client could consume an arbitrary amount of memory when trying
to connent 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.
  • Loading branch information
serhiy-storchaka committed May 24, 2024
commit 6322049d1285f652e5a33144271200c1fe0c3746
10 changes: 10 additions & 0 deletions Lib/imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
# search command can be quite large, so we now use 1M.
_MAXLINE = 1000000

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

# Commands

Expand Down Expand Up @@ -315,6 +318,13 @@ def open(self, host='', port=IMAP4_PORT, timeout=None):

def read(self, size):
"""Read 'size' bytes from remote."""
cursize = min(size, _SAFE_BUF_SIZE)
data = self.file.read(cursize)
while cursize < size and len(data) == cursize:
delta = min(cursize, size - cursize)
data += self.file.read(delta)
Comment thread
terryjreedy marked this conversation as resolved.
cursize += delta
return data
return self.file.read(size)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you've missed erasing this part.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch!

Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated


Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,18 @@ def handle(self):
self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address)

def test_truncated_large_literal(self):
class BadHandler(SimpleIMAPHandler):
def handle(self):
self._send_textline('* OK {%d}' % size)
self._send_textline('IMAP4rev1')

for w in range(15, 64):
size = 1 << w
with self.reaped_server(BadHandler) as server:
self.assertRaises(imaplib.IMAP4.abort,
self.imap_class, *server.server_address)

@threading_helper.reap_threads
def test_simple_with_statement(self):
# simplest call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a vulnerability in the :mod:`imaplib` module, when connecting to a
malicious server could cause an arbitrary amount of memory to be consumed.