[Python-checkins] cpython (3.3): Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to

georg.brandl python-checkins at python.org
Sun Oct 27 07:38:48 CET 2013


http://hg.python.org/cpython/rev/68029048c9c6
changeset:   86677:68029048c9c6
branch:      3.3
user:        Georg Brandl <georg at python.org>
date:        Sun Oct 27 07:23:53 2013 +0100
summary:
  Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
prevent readline() calls from consuming too much memory.  Patch by Jyrki
Pulliainen.

files:
  Lib/poplib.py           |  11 ++++++++++-
  Lib/test/test_poplib.py |   6 +++++-
  Misc/NEWS               |   4 ++++
  3 files changed, 19 insertions(+), 2 deletions(-)


diff --git a/Lib/poplib.py b/Lib/poplib.py
--- a/Lib/poplib.py
+++ b/Lib/poplib.py
@@ -32,6 +32,12 @@
 LF = b'\n'
 CRLF = CR+LF
 
+# maximal line length when calling readline(). This is to prevent
+# reading arbitrary lenght lines. RFC 1939 limits POP3 line length to
+# 512 characters, including CRLF. We have selected 2048 just to be on
+# the safe side.
+_MAXLINE = 2048
+
 
 class POP3:
 
@@ -107,7 +113,10 @@
     # Raise error_proto('-ERR EOF') if the connection is closed.
 
     def _getline(self):
-        line = self.file.readline()
+        line = self.file.readline(_MAXLINE + 1)
+        if len(line) > _MAXLINE:
+            raise error_proto('line too long')
+
         if self._debugging > 1: print('*get*', repr(line))
         if not line: raise error_proto('-ERR EOF')
         octets = len(line)
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -83,7 +83,7 @@
 
     def cmd_list(self, arg):
         if arg:
-            self.push('+OK %s %s' %(arg, arg))
+            self.push('+OK %s %s' % (arg, arg))
         else:
             self.push('+OK')
             asynchat.async_chat.push(self, LIST_RESP)
@@ -208,6 +208,10 @@
         foo = self.client.retr('foo')
         self.assertEqual(foo, expected)
 
+    def test_too_long_lines(self):
+        self.assertRaises(poplib.error_proto, self.client._shortcmd,
+                          'echo +%s' % ((poplib._MAXLINE + 10) * 'a'))
+
     def test_dele(self):
         self.assertOK(self.client.dele('foo'))
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,10 @@
 Library
 -------
 
+- Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
+  prevent readline() calls from consuming too much memory.  Patch by Jyrki
+  Pulliainen.
+
 - Issue #17997: Change behavior of ``ssl.match_hostname()`` to follow RFC 6125,
   for security reasons.  It now doesn't match multiple wildcards nor wildcards
   inside IDN fragments.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list