Skip to content

Commit 7ff917f

Browse files
miss-islingtongpshead
authored andcommitted
[3.10] pythongh-87451: Apply CVE-2021-4189 PASV fix to ftplib.ftpcp() (pythonGH-149648) (python#149797)
pythongh-87451: Apply CVE-2021-4189 PASV fix to ftplib.ftpcp() (pythonGH-149648) ftpcp() called parse227() directly and passed the source server's self-reported PASV IPv4 address to the target server's PORT command, bypassing the CVE-2021-4189 fix that was applied only to FTP.makepasv(). A malicious source FTP server could use this to redirect the target server's data connection to an arbitrary host:port (SSRF). ftpcp() now uses the source server's actual peer address, honoring the existing trust_server_pasv_ipv4_address opt-out, the same as makepasv(). Thanks to Qi Ding at Aurascape AI for the report. (GHSA-w8c5-q2xf-gf7c) (cherry picked from commit eac4fe3) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> (cherry picked from commit 2bbcf3f)
1 parent c37e614 commit 7ff917f

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎Lib/ftplib.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,16 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
905905
type = 'TYPE ' + type
906906
source.voidcmd(type)
907907
target.voidcmd(type)
908-
sourcehost, sourceport = parse227(source.sendcmd('PASV'))
908+
# Don't trust the IPv4 address the source server advertises in its PASV
909+
# reply: a malicious source could otherwise point the target's data
910+
# connection at an arbitrary host (SSRF). A caller that needs the old
911+
# behavior can set trust_server_pasv_ipv4_address on the source FTP
912+
# object. See FTP.makepasv(), which applies the same rule.
913+
untrusted_host, sourceport = parse227(source.sendcmd('PASV'))
914+
if source.trust_server_pasv_ipv4_address:
915+
sourcehost = untrusted_host
916+
else:
917+
sourcehost = source.sock.getpeername()[0]
909918
target.sendport(sourcehost, sourceport)
910919
# RFC 959: the user must "listen" [...] BEFORE sending the
911920
# transfer request.

‎Lib/test/test_ftplib.py‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
except ImportError:
1919
ssl = None
2020

21-
from unittest import TestCase, skipUnless
21+
from unittest import mock, TestCase, skipUnless
2222
from test import support
2323
from test.support import socket_helper
2424
from test.support.socket_helper import HOST, HOSTv6
@@ -1136,6 +1136,40 @@ def testTimeoutDirectAccess(self):
11361136
ftp.close()
11371137

11381138

1139+
class TestFtpcpSecurity(TestCase):
1140+
"""ftpcp() must not trust the host a source server advertises in PASV.
1141+
1142+
A malicious source server can otherwise redirect the target server's
1143+
data connection to an arbitrary host:port (SSRF), so ftpcp() uses the
1144+
source server's actual peer address instead, the same as FTP.makepasv().
1145+
"""
1146+
1147+
def _make_pair(self, *, advertised_host, real_host, trust=False):
1148+
source = mock.Mock(spec=ftplib.FTP)
1149+
source.trust_server_pasv_ipv4_address = trust
1150+
source.sock.getpeername.return_value = (real_host, 21)
1151+
# PASV replies give the host as comma-separated octets, not dotted.
1152+
advertised = advertised_host.replace('.', ',')
1153+
source.sendcmd.side_effect = lambda cmd: (
1154+
f'227 Entering Passive Mode ({advertised},1,2).'
1155+
if cmd == 'PASV' else '150 ok')
1156+
target = mock.Mock(spec=ftplib.FTP)
1157+
target.sendcmd.return_value = '150 ok'
1158+
return source, target
1159+
1160+
def test_ftpcp_ignores_untrusted_pasv_host(self):
1161+
source, target = self._make_pair(advertised_host='10.0.0.5',
1162+
real_host='198.51.100.7')
1163+
ftplib.ftpcp(source, 'a', target, 'b')
1164+
target.sendport.assert_called_once_with('198.51.100.7', 258)
1165+
1166+
def test_ftpcp_trust_server_pasv_ipv4_address(self):
1167+
source, target = self._make_pair(advertised_host='10.0.0.5',
1168+
real_host='198.51.100.7', trust=True)
1169+
ftplib.ftpcp(source, 'a', target, 'b')
1170+
target.sendport.assert_called_once_with('10.0.0.5', 258)
1171+
1172+
11391173
class MiscTestCase(TestCase):
11401174
def test__all__(self):
11411175
blacklist = {'MSG_OOB', 'FTP_PORT', 'MAXLINE', 'CRLF', 'B_CRLF',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The :mod:`ftplib` module's undocumented ``ftpcp`` function no longer trusts
2+
the IPv4 address value returned from the source server in response to the
3+
``PASV`` command by default, completing the fix for CVE-2021-4189. As with
4+
:class:`ftplib.FTP`, the former behavior can be re-enabled by setting the
5+
``trust_server_pasv_ipv4_address`` attribute on the source :class:`ftplib.FTP`
6+
instance to ``True``. Thanks to Qi Deng at Aurascape AI for the report.

0 commit comments

Comments
 (0)