File CVE-2021-4189-ftplib-trust-PASV-resp.patch of Package python (Revision 381d91ea0ab10fc5235c75951d42564a)
Currently displaying revision 381d91ea0ab10fc5235c75951d42564a , Show latest
136
1
commit 0ab152c6b5d95caa2dc1a30fa96e10258b5f188e
2
Author: Gregory P. Smith <greg@krypto.org>
3
Date: Mon Mar 15 11:39:31 2021 -0700
4
5
bpo-43285 Make ftplib not trust the PASV response. (GH-24838)
6
7
bpo-43285: Make ftplib not trust the PASV response.
8
9
The IPv4 address value returned from the server in response to the PASV command
10
should not be trusted. This prevents a malicious FTP server from using the
11
response to probe IPv4 address and port combinations on the client network.
12
13
Instead of using the returned address, we use the IP address we're
14
already connected to. This is the strategy other ftp clients adopted,
15
and matches the only strategy available for the modern IPv6 EPSV command
16
where the server response must return a port number and nothing else.
17
18
For the rare user who _wants_ this ugly behavior, set a `trust_server_pasv_ipv4_address`
19
attribute on your `ftplib.FTP` instance to True.
20
21
---
22
Doc/whatsnew/2.7.rst | 10 +++
23
Lib/ftplib.py | 11 +++-
24
Lib/test/test_ftplib.py | 27 +++++++++-
25
Misc/NEWS.d/next/Security/2021-03-13-03-48-14.bpo-43285.g-Hah3.rst | 8 ++
26
4 files changed, 53 insertions(+), 3 deletions(-)
27
28
--- a/Doc/whatsnew/2.7.rst
29
+++ b/Doc/whatsnew/2.7.rst
30
31
when running tests.
32
33
34
+Post-EOS fixes
35
+==============
36
+
37
+A security fix alters the :class:`ftplib.FTP` behavior to not trust the
38
+IPv4 address sent from the remote server when setting up a passive data
39
+channel. We reuse the ftp server IP address instead. For unusual code
40
+requiring the old behavior, set a ``trust_server_pasv_ipv4_address``
41
+attribute on your FTP instance to ``True``. (See :issue:`43285`)
42
+
43
+
44
Python 3.1 Features
45
=======================
46
47
--- a/Lib/ftplib.py
48
+++ b/Lib/ftplib.py
49
50
sock = None
51
file = None
52
welcome = None
53
- passiveserver = 1
54
+ passiveserver = True
55
+ # Disables https://bugs.python.org/issue43285 security if set to True.
56
+ trust_server_pasv_ipv4_address = False
57
58
# Initialization method (called by class instantiation).
59
# Initialize host to localhost, port to standard ftp port
60
61
return sock
62
63
def makepasv(self):
64
+ """Internal: Does the PASV or EPSV handshake -> (address, port)"""
65
if self.af == socket.AF_INET:
66
- host, port = parse227(self.sendcmd('PASV'))
67
+ untrusted_host, port = parse227(self.sendcmd('PASV'))
68
+ if self.trust_server_pasv_ipv4_address:
69
+ host = untrusted_host
70
+ else:
71
+ host = self.sock.getpeername()[0]
72
else:
73
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
74
return host, port
75
--- a/Lib/test/test_ftplib.py
76
+++ b/Lib/test/test_ftplib.py
77
78
self.rest = None
79
self.next_retr_data = RETR_DATA
80
self.push('220 welcome')
81
+ # We use this as the string IPv4 address to direct the client
82
+ # to in response to a PASV command. To test security behavior.
83
+ # https://bugs.python.org/issue43285/.
84
+ self.fake_pasv_server_ip = '252.253.254.255'
85
86
def collect_incoming_data(self, data):
87
self.in_buffer.append(data)
88
89
sock.bind((self.socket.getsockname()[0], 0))
90
sock.listen(5)
91
sock.settimeout(10)
92
- ip, port = sock.getsockname()[:2]
93
+ port = sock.getsockname()[1]
94
+ ip = self.fake_pasv_server_ip
95
ip = ip.replace('.', ',')
96
p1, p2 = divmod(port, 256)
97
self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
98
99
# IPv4 is in use, just make sure send_epsv has not been used
100
self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
101
102
+ def test_makepasv_issue43285_security_disabled(self):
103
+ """Test the opt-in to the old vulnerable behavior."""
104
+ self.client.trust_server_pasv_ipv4_address = True
105
+ bad_host, port = self.client.makepasv()
106
+ self.assertEqual(
107
+ bad_host, self.server.handler_instance.fake_pasv_server_ip)
108
+ # Opening and closing a connection keeps the dummy server happy
109
+ # instead of timing out on accept.
110
+ socket.create_connection((self.client.sock.getpeername()[0], port),
111
+ timeout=TIMEOUT).close()
112
+
113
+ def test_makepasv_issue43285_security_enabled_default(self):
114
+ self.assertFalse(self.client.trust_server_pasv_ipv4_address)
115
+ trusted_host, port = self.client.makepasv()
116
+ self.assertNotEqual(
117
+ trusted_host, self.server.handler_instance.fake_pasv_server_ip)
118
+ # Opening and closing a connection keeps the dummy server happy
119
+ # instead of timing out on accept.
120
+ socket.create_connection((trusted_host, port), timeout=TIMEOUT).close()
121
+
122
def test_line_too_long(self):
123
self.assertRaises(ftplib.Error, self.client.sendcmd,
124
'x' * self.client.maxline * 2)
125
--- /dev/null
126
+++ b/Misc/NEWS.d/next/Security/2021-03-13-03-48-14.bpo-43285.g-Hah3.rst
127
128
+:mod:`ftplib` no longer trusts the IP address value returned from the server
129
+in response to the PASV command by default. This prevents a malicious FTP
130
+server from using the response to probe IPv4 address and port combinations
131
+on the client network.
132
+
133
+Code that requires the former vulnerable behavior may set a
134
+``trust_server_pasv_ipv4_address`` attribute on their
135
+:class:`ftplib.FTP` instances to ``True`` to re-enable it.
136