Skip to content

Commit 13df5d3

Browse files
[3.11] GH-95494: Fix transport EOF handling in OpenSSL 3.0 (GH-95495) (#103006)
GH-25309 enabled SSL_OP_IGNORE_UNEXPECTED_EOF by default, with a comment that it restores OpenSSL 1.1.1 behavior, but this wasn't quite right. That option causes OpenSSL to treat transport EOF as the same as close_notify (i.e. SSL_ERROR_ZERO_RETURN), whereas Python actually has distinct SSLEOFError and SSLZeroReturnError exceptions. (The latter is usually mapped to a zero return from read.) In OpenSSL 1.1.1, the ssl module would raise them for transport EOF and close_notify, respectively. In OpenSSL 3.0, both act like close_notify. Fix this by, instead, just detecting SSL_R_UNEXPECTED_EOF_WHILE_READING and mapping that to the other exception type. There doesn't seem to have been any unit test of this error, so fill in the missing one. This had to be done with the BIO path because it's actually slightly tricky to simulate a transport EOF with Python's fd based APIs. (If you instruct the server to close the socket, it gets confused, probably because the server's SSL object is still referencing the now dead fd?) (cherry picked from commit 420bbb7) Co-authored-by: David Benjamin <[email protected]>
1 parent b28f919 commit 13df5d3

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Lib/test/test_ssl.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ def data_file(*name):
154154
OP_SINGLE_ECDH_USE = getattr(ssl, "OP_SINGLE_ECDH_USE", 0)
155155
OP_CIPHER_SERVER_PREFERENCE = getattr(ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
156156
OP_ENABLE_MIDDLEBOX_COMPAT = getattr(ssl, "OP_ENABLE_MIDDLEBOX_COMPAT", 0)
157-
OP_IGNORE_UNEXPECTED_EOF = getattr(ssl, "OP_IGNORE_UNEXPECTED_EOF", 0)
158157

159158
# Ubuntu has patched OpenSSL and changed behavior of security level 2
160159
# see https://bugs.python.org/issue41561#msg389003
@@ -1200,8 +1199,7 @@ def test_options(self):
12001199
# SSLContext also enables these by default
12011200
default |= (OP_NO_COMPRESSION | OP_CIPHER_SERVER_PREFERENCE |
12021201
OP_SINGLE_DH_USE | OP_SINGLE_ECDH_USE |
1203-
OP_ENABLE_MIDDLEBOX_COMPAT |
1204-
OP_IGNORE_UNEXPECTED_EOF)
1202+
OP_ENABLE_MIDDLEBOX_COMPAT)
12051203
self.assertEqual(default, ctx.options)
12061204
with warnings_helper.check_warnings():
12071205
ctx.options |= ssl.OP_NO_TLSv1
@@ -2362,6 +2360,20 @@ def test_bio_read_write_data(self):
23622360
self.assertEqual(buf, b'foo\n')
23632361
self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
23642362

2363+
def test_transport_eof(self):
2364+
client_context, server_context, hostname = testing_context()
2365+
with socket.socket(socket.AF_INET) as sock:
2366+
sock.connect(self.server_addr)
2367+
incoming = ssl.MemoryBIO()
2368+
outgoing = ssl.MemoryBIO()
2369+
sslobj = client_context.wrap_bio(incoming, outgoing,
2370+
server_hostname=hostname)
2371+
self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
2372+
2373+
# Simulate EOF from the transport.
2374+
incoming.write_eof()
2375+
self.assertRaises(ssl.SSLEOFError, sslobj.read)
2376+
23652377

23662378
@support.requires_resource('network')
23672379
class NetworkedTests(unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
When built against OpenSSL 3.0, the :mod:`ssl` module had a bug where it
2+
reported unauthenticated EOFs (i.e. without close_notify) as a clean TLS-level
3+
EOF. It now raises :exc:`~ssl.SSLEOFError`, matching the behavior in previous
4+
versions of OpenSSL. The :attr:`~ssl.SSLContext.options` attribute on
5+
:class:`~ssl.SSLContext` also no longer includes
6+
:data:`~ssl.OP_IGNORE_UNEXPECTED_EOF` by default. This option may be set to
7+
specify the previous OpenSSL 3.0 behavior.

Modules/_ssl.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,16 @@ PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno)
665665
ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) {
666666
type = state->PySSLCertVerificationErrorObject;
667667
}
668+
#if defined(SSL_R_UNEXPECTED_EOF_WHILE_READING)
669+
/* OpenSSL 3.0 changed transport EOF from SSL_ERROR_SYSCALL with
670+
* zero return value to SSL_ERROR_SSL with a special error code. */
671+
if (ERR_GET_LIB(e) == ERR_LIB_SSL &&
672+
ERR_GET_REASON(e) == SSL_R_UNEXPECTED_EOF_WHILE_READING) {
673+
p = PY_SSL_ERROR_EOF;
674+
type = state->PySSLEOFErrorObject;
675+
errstr = "EOF occurred in violation of protocol";
676+
}
677+
#endif
668678
break;
669679
}
670680
default:
@@ -3133,10 +3143,6 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
31333143
#endif
31343144
#ifdef SSL_OP_SINGLE_ECDH_USE
31353145
options |= SSL_OP_SINGLE_ECDH_USE;
3136-
#endif
3137-
#ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
3138-
/* Make OpenSSL 3.0.0 behave like 1.1.1 */
3139-
options |= SSL_OP_IGNORE_UNEXPECTED_EOF;
31403146
#endif
31413147
SSL_CTX_set_options(self->ctx, options);
31423148

0 commit comments

Comments
 (0)