-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-119511: Fix OOM vulnerability in imaplib #119514
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6322049
gh-119511: Fix OOM vulnerability in imaplib
serhiy-storchaka 284a553
Update Lib/imaplib.py
serhiy-storchaka d7abc31
modernize the test, expand the NEWS entry.
gpshead 1c74be4
Merge branch 'main' into imaplib-oom
gpshead d71a7ee
use a subTest for sizes
gpshead f6a41df
wording tweak.
gpshead File filter
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
commit 6322049d1285f652e5a33144271200c1fe0c3746
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
cursize += delta | ||
return data | ||
return self.file.read(size) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you've missed erasing this part. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch!
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Security/2024-05-24-21-00-52.gh-issue-119511.jKrXQ8.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.