Kaydet (Commit) d6fddf3d authored tarafından Barry Warsaw's avatar Barry Warsaw

- Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by

  limiting the call to readline().  Original patch by Michał
  Jastrzębski and Giampaolo Rodola.

with test fixes by Serhiy Storchaka.
üst 4e95d601
...@@ -54,6 +54,8 @@ MSG_OOB = 0x1 # Process data out of band ...@@ -54,6 +54,8 @@ MSG_OOB = 0x1 # Process data out of band
# The standard FTP server control port # The standard FTP server control port
FTP_PORT = 21 FTP_PORT = 21
# The sizehint parameter passed to readline() calls
MAXLINE = 8192
# Exception raised when an error or invalid response is received # Exception raised when an error or invalid response is received
...@@ -100,6 +102,7 @@ class FTP: ...@@ -100,6 +102,7 @@ class FTP:
debugging = 0 debugging = 0
host = '' host = ''
port = FTP_PORT port = FTP_PORT
maxline = MAXLINE
sock = None sock = None
file = None file = None
welcome = None welcome = None
...@@ -179,7 +182,9 @@ class FTP: ...@@ -179,7 +182,9 @@ class FTP:
# Internal: return one line from the server, stripping CRLF. # Internal: return one line from the server, stripping CRLF.
# Raise EOFError if the connection is closed # Raise EOFError if the connection is closed
def getline(self): def getline(self):
line = self.file.readline() line = self.file.readline(self.maxline + 1)
if len(line) > self.maxline:
raise Error("got more than %d bytes" % self.maxline)
if self.debugging > 1: if self.debugging > 1:
print '*get*', self.sanitize(line) print '*get*', self.sanitize(line)
if not line: raise EOFError if not line: raise EOFError
...@@ -421,7 +426,9 @@ class FTP: ...@@ -421,7 +426,9 @@ class FTP:
conn = self.transfercmd(cmd) conn = self.transfercmd(cmd)
fp = conn.makefile('rb') fp = conn.makefile('rb')
while 1: while 1:
line = fp.readline() line = fp.readline(self.maxline + 1)
if len(line) > self.maxline:
raise Error("got more than %d bytes" % self.maxline)
if self.debugging > 2: print '*retr*', repr(line) if self.debugging > 2: print '*retr*', repr(line)
if not line: if not line:
break break
...@@ -473,7 +480,9 @@ class FTP: ...@@ -473,7 +480,9 @@ class FTP:
self.voidcmd('TYPE A') self.voidcmd('TYPE A')
conn = self.transfercmd(cmd) conn = self.transfercmd(cmd)
while 1: while 1:
buf = fp.readline() buf = fp.readline(self.maxline + 1)
if len(buf) > self.maxline:
raise Error("got more than %d bytes" % self.maxline)
if not buf: break if not buf: break
if buf[-2:] != CRLF: if buf[-2:] != CRLF:
if buf[-1] in CRLF: buf = buf[:-1] if buf[-1] in CRLF: buf = buf[:-1]
......
...@@ -46,6 +46,7 @@ class DummyFTPHandler(asynchat.async_chat): ...@@ -46,6 +46,7 @@ class DummyFTPHandler(asynchat.async_chat):
self.last_received_cmd = None self.last_received_cmd = None
self.last_received_data = '' self.last_received_data = ''
self.next_response = '' self.next_response = ''
self.next_retr_data = RETR_DATA
self.push('220 welcome') self.push('220 welcome')
def collect_incoming_data(self, data): def collect_incoming_data(self, data):
...@@ -162,7 +163,7 @@ class DummyFTPHandler(asynchat.async_chat): ...@@ -162,7 +163,7 @@ class DummyFTPHandler(asynchat.async_chat):
def cmd_retr(self, arg): def cmd_retr(self, arg):
self.push('125 retr ok') self.push('125 retr ok')
self.dtp.push(RETR_DATA) self.dtp.push(self.next_retr_data)
self.dtp.close_when_done() self.dtp.close_when_done()
def cmd_list(self, arg): def cmd_list(self, arg):
...@@ -175,6 +176,11 @@ class DummyFTPHandler(asynchat.async_chat): ...@@ -175,6 +176,11 @@ class DummyFTPHandler(asynchat.async_chat):
self.dtp.push(NLST_DATA) self.dtp.push(NLST_DATA)
self.dtp.close_when_done() self.dtp.close_when_done()
def cmd_setlongretr(self, arg):
# For testing. Next RETR will return long line.
self.next_retr_data = 'x' * int(arg)
self.push('125 setlongretr ok')
class DummyFTPServer(asyncore.dispatcher, threading.Thread): class DummyFTPServer(asyncore.dispatcher, threading.Thread):
...@@ -362,6 +368,20 @@ class TestFTPClass(TestCase): ...@@ -362,6 +368,20 @@ class TestFTPClass(TestCase):
# IPv4 is in use, just make sure send_epsv has not been used # IPv4 is in use, just make sure send_epsv has not been used
self.assertEqual(self.server.handler.last_received_cmd, 'pasv') self.assertEqual(self.server.handler.last_received_cmd, 'pasv')
def test_line_too_long(self):
self.assertRaises(ftplib.Error, self.client.sendcmd,
'x' * self.client.maxline * 2)
def test_retrlines_too_long(self):
self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
received = []
self.assertRaises(ftplib.Error,
self.client.retrlines, 'retr', received.append)
def test_storlines_too_long(self):
f = StringIO.StringIO('x' * self.client.maxline * 2)
self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f)
class TestIPv6Environment(TestCase): class TestIPv6Environment(TestCase):
......
...@@ -13,6 +13,10 @@ Core and Builtins ...@@ -13,6 +13,10 @@ Core and Builtins
Library Library
------- -------
- Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
limiting the call to readline(). Original patch by Michał
Jastrzębski and Giampaolo Rodola.
- Issue #16039: CVE-2013-1752: Change use of readline in imaplib module to - Issue #16039: CVE-2013-1752: Change use of readline in imaplib module to
limit line length. Patch by Emil Lind. limit line length. Patch by Emil Lind.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment