Blame SOURCES/00204-CVE-2013-1752-ftplib.patch

ae2451
ae2451
# HG changeset patch
ae2451
# User Serhiy Storchaka <storchaka@gmail.com>
ae2451
# Date 1382277427 -10800
ae2451
# Node ID 44ac81e6d584758ee56a865a7c18d82505be0643
ae2451
# Parent  625ece68d79a27d376889579c414ed4b2d8a2649
ae2451
Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
ae2451
limiting the call to readline().  Original patch by Michał
ae2451
Jastrzębski and Giampaolo Rodola.
ae2451
ae2451
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
ae2451
--- a/Lib/ftplib.py
ae2451
+++ b/Lib/ftplib.py
ae2451
@@ -55,6 +55,8 @@ MSG_OOB = 0x1                           
ae2451
 
ae2451
 # The standard FTP server control port
ae2451
 FTP_PORT = 21
ae2451
+# The sizehint parameter passed to readline() calls
ae2451
+MAXLINE = 8192
ae2451
 
ae2451
 
ae2451
 # Exception raised when an error or invalid response is received
ae2451
@@ -101,6 +103,7 @@ class FTP:
ae2451
     debugging = 0
ae2451
     host = ''
ae2451
     port = FTP_PORT
ae2451
+    maxline = MAXLINE
ae2451
     sock = None
ae2451
     file = None
ae2451
     welcome = None
ae2451
@@ -180,7 +183,9 @@ class FTP:
ae2451
     # Internal: return one line from the server, stripping CRLF.
ae2451
     # Raise EOFError if the connection is closed
ae2451
     def getline(self):
ae2451
-        line = self.file.readline()
ae2451
+        line = self.file.readline(self.maxline + 1)
ae2451
+        if len(line) > self.maxline:
ae2451
+            raise Error("got more than %d bytes" % self.maxline)
ae2451
         if self.debugging > 1:
ae2451
             print '*get*', self.sanitize(line)
ae2451
         if not line: raise EOFError
ae2451
@@ -432,7 +437,9 @@ class FTP:
ae2451
         conn = self.transfercmd(cmd)
ae2451
         fp = conn.makefile('rb')
ae2451
         while 1:
ae2451
-            line = fp.readline()
ae2451
+            line = fp.readline(self.maxline + 1)
ae2451
+            if len(line) > self.maxline:
ae2451
+                raise Error("got more than %d bytes" % self.maxline)
ae2451
             if self.debugging > 2: print '*retr*', repr(line)
ae2451
             if not line:
ae2451
                 break
ae2451
@@ -485,7 +492,9 @@ class FTP:
ae2451
         self.voidcmd('TYPE A')
ae2451
         conn = self.transfercmd(cmd)
ae2451
         while 1:
ae2451
-            buf = fp.readline()
ae2451
+            buf = fp.readline(self.maxline + 1)
ae2451
+            if len(buf) > self.maxline:
ae2451
+                raise Error("got more than %d bytes" % self.maxline)
ae2451
             if not buf: break
ae2451
             if buf[-2:] != CRLF:
ae2451
                 if buf[-1] in CRLF: buf = buf[:-1]
ae2451
@@ -710,7 +719,9 @@ else:
ae2451
             fp = conn.makefile('rb')
ae2451
             try:
ae2451
                 while 1:
ae2451
-                    line = fp.readline()
ae2451
+                    line = fp.readline(self.maxline + 1)
ae2451
+                    if len(line) > self.maxline:
ae2451
+                        raise Error("got more than %d bytes" % self.maxline)
ae2451
                     if self.debugging > 2: print '*retr*', repr(line)
ae2451
                     if not line:
ae2451
                         break
ae2451
@@ -748,7 +759,9 @@ else:
ae2451
             conn = self.transfercmd(cmd)
ae2451
             try:
ae2451
                 while 1:
ae2451
-                    buf = fp.readline()
ae2451
+                    buf = fp.readline(self.maxline + 1)
ae2451
+                    if len(buf) > self.maxline:
ae2451
+                        raise Error("got more than %d bytes" % self.maxline)
ae2451
                     if not buf: break
ae2451
                     if buf[-2:] != CRLF:
ae2451
                         if buf[-1] in CRLF: buf = buf[:-1]
ae2451
@@ -905,7 +918,9 @@ class Netrc:
ae2451
         fp = open(filename, "r")
ae2451
         in_macro = 0
ae2451
         while 1:
ae2451
-            line = fp.readline()
ae2451
+            line = fp.readline(self.maxline + 1)
ae2451
+            if len(line) > self.maxline:
ae2451
+                raise Error("got more than %d bytes" % self.maxline)
ae2451
             if not line: break
ae2451
             if in_macro and line.strip():
ae2451
                 macro_lines.append(line)
ae2451
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
ae2451
--- a/Lib/test/test_ftplib.py
ae2451
+++ b/Lib/test/test_ftplib.py
ae2451
@@ -65,6 +65,7 @@ class DummyFTPHandler(asynchat.async_cha
ae2451
         self.last_received_data = ''
ae2451
         self.next_response = ''
ae2451
         self.rest = None
ae2451
+        self.next_retr_data = RETR_DATA
ae2451
         self.push('220 welcome')
ae2451
 
ae2451
     def collect_incoming_data(self, data):
ae2451
@@ -189,7 +190,7 @@ class DummyFTPHandler(asynchat.async_cha
ae2451
             offset = int(self.rest)
ae2451
         else:
ae2451
             offset = 0
ae2451
-        self.dtp.push(RETR_DATA[offset:])
ae2451
+        self.dtp.push(self.next_retr_data[offset:])
ae2451
         self.dtp.close_when_done()
ae2451
         self.rest = None
ae2451
 
ae2451
@@ -203,6 +204,11 @@ class DummyFTPHandler(asynchat.async_cha
ae2451
         self.dtp.push(NLST_DATA)
ae2451
         self.dtp.close_when_done()
ae2451
 
ae2451
+    def cmd_setlongretr(self, arg):
ae2451
+        # For testing. Next RETR will return long line.
ae2451
+        self.next_retr_data = 'x' * int(arg)
ae2451
+        self.push('125 setlongretr ok')
ae2451
+
ae2451
 
ae2451
 class DummyFTPServer(asyncore.dispatcher, threading.Thread):
ae2451
 
ae2451
@@ -558,6 +564,20 @@ class TestFTPClass(TestCase):
ae2451
         # IPv4 is in use, just make sure send_epsv has not been used
ae2451
         self.assertEqual(self.server.handler.last_received_cmd, 'pasv')
ae2451
 
ae2451
+    def test_line_too_long(self):
ae2451
+        self.assertRaises(ftplib.Error, self.client.sendcmd,
ae2451
+                          'x' * self.client.maxline * 2)
ae2451
+
ae2451
+    def test_retrlines_too_long(self):
ae2451
+        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
ae2451
+        received = []
ae2451
+        self.assertRaises(ftplib.Error,
ae2451
+                          self.client.retrlines, 'retr', received.append)
ae2451
+
ae2451
+    def test_storlines_too_long(self):
ae2451
+        f = StringIO.StringIO('x' * self.client.maxline * 2)
ae2451
+        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f)
ae2451
+
ae2451
 
ae2451
 class TestIPv6Environment(TestCase):
ae2451