Skip to content

Commit d6fddf3

Browse files
committed
- Issue python#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.
1 parent 4e95d60 commit d6fddf3

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Lib/ftplib.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
# The standard FTP server control port
5656
FTP_PORT = 21
57+
# The sizehint parameter passed to readline() calls
58+
MAXLINE = 8192
5759

5860

5961
# Exception raised when an error or invalid response is received
@@ -100,6 +102,7 @@ class FTP:
100102
debugging = 0
101103
host = ''
102104
port = FTP_PORT
105+
maxline = MAXLINE
103106
sock = None
104107
file = None
105108
welcome = None
@@ -179,7 +182,9 @@ def putcmd(self, line):
179182
# Internal: return one line from the server, stripping CRLF.
180183
# Raise EOFError if the connection is closed
181184
def getline(self):
182-
line = self.file.readline()
185+
line = self.file.readline(self.maxline + 1)
186+
if len(line) > self.maxline:
187+
raise Error("got more than %d bytes" % self.maxline)
183188
if self.debugging > 1:
184189
print '*get*', self.sanitize(line)
185190
if not line: raise EOFError
@@ -421,7 +426,9 @@ def retrlines(self, cmd, callback = None):
421426
conn = self.transfercmd(cmd)
422427
fp = conn.makefile('rb')
423428
while 1:
424-
line = fp.readline()
429+
line = fp.readline(self.maxline + 1)
430+
if len(line) > self.maxline:
431+
raise Error("got more than %d bytes" % self.maxline)
425432
if self.debugging > 2: print '*retr*', repr(line)
426433
if not line:
427434
break
@@ -473,7 +480,9 @@ def storlines(self, cmd, fp, callback=None):
473480
self.voidcmd('TYPE A')
474481
conn = self.transfercmd(cmd)
475482
while 1:
476-
buf = fp.readline()
483+
buf = fp.readline(self.maxline + 1)
484+
if len(buf) > self.maxline:
485+
raise Error("got more than %d bytes" % self.maxline)
477486
if not buf: break
478487
if buf[-2:] != CRLF:
479488
if buf[-1] in CRLF: buf = buf[:-1]

Lib/test/test_ftplib.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(self, conn):
4646
self.last_received_cmd = None
4747
self.last_received_data = ''
4848
self.next_response = ''
49+
self.next_retr_data = RETR_DATA
4950
self.push('220 welcome')
5051

5152
def collect_incoming_data(self, data):
@@ -162,7 +163,7 @@ def cmd_stor(self, arg):
162163

163164
def cmd_retr(self, arg):
164165
self.push('125 retr ok')
165-
self.dtp.push(RETR_DATA)
166+
self.dtp.push(self.next_retr_data)
166167
self.dtp.close_when_done()
167168

168169
def cmd_list(self, arg):
@@ -175,6 +176,11 @@ def cmd_nlst(self, arg):
175176
self.dtp.push(NLST_DATA)
176177
self.dtp.close_when_done()
177178

179+
def cmd_setlongretr(self, arg):
180+
# For testing. Next RETR will return long line.
181+
self.next_retr_data = 'x' * int(arg)
182+
self.push('125 setlongretr ok')
183+
178184

179185
class DummyFTPServer(asyncore.dispatcher, threading.Thread):
180186

@@ -362,6 +368,20 @@ def test_makepasv(self):
362368
# IPv4 is in use, just make sure send_epsv has not been used
363369
self.assertEqual(self.server.handler.last_received_cmd, 'pasv')
364370

371+
def test_line_too_long(self):
372+
self.assertRaises(ftplib.Error, self.client.sendcmd,
373+
'x' * self.client.maxline * 2)
374+
375+
def test_retrlines_too_long(self):
376+
self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
377+
received = []
378+
self.assertRaises(ftplib.Error,
379+
self.client.retrlines, 'retr', received.append)
380+
381+
def test_storlines_too_long(self):
382+
f = StringIO.StringIO('x' * self.client.maxline * 2)
383+
self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f)
384+
365385

366386
class TestIPv6Environment(TestCase):
367387

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
17+
limiting the call to readline(). Original patch by Michał
18+
Jastrzębski and Giampaolo Rodola.
19+
1620
- Issue #16039: CVE-2013-1752: Change use of readline in imaplib module to
1721
limit line length. Patch by Emil Lind.
1822

0 commit comments

Comments
 (0)