Skip to content

Commit 165d709

Browse files
miss-islingtonlazerggpshead
authored
[3.15] gh-158313: Clear the OpenSSL error queue when ssl raises OSError (GH-158333) (#162961)
gh-158313: Clear the OpenSSL error queue when ssl raises OSError (GH-158333) Clear the OpenSSL error queue when ssl raises OSError --------- (cherry picked from commit 9d0179d) Co-authored-by: Lazizbek Ergashev <lazerg2@gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 0e88df1 commit 165d709

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,6 +5420,81 @@ def test_got_eof(self):
54205420

54215421
self.assertEqual(sslsock.pending(), 0)
54225422

5423+
def test_oserror_does_not_leak_to_other_sockets(self):
5424+
# gh-158313: An OSError for one connection must not stay on the
5425+
# thread's OpenSSL error queue and be raised for another connection.
5426+
client_context, server_context, hostname = testing_context()
5427+
server_context.num_tickets = 0
5428+
5429+
def connect():
5430+
csock, ssock = socket.socketpair()
5431+
client = client_context.wrap_socket(
5432+
csock, server_hostname=hostname, do_handshake_on_connect=False)
5433+
server = server_context.wrap_socket(
5434+
ssock, server_side=True, do_handshake_on_connect=False)
5435+
thread = threading.Thread(target=server.do_handshake)
5436+
thread.start()
5437+
client.do_handshake()
5438+
thread.join()
5439+
return client, server
5440+
5441+
client, server = connect()
5442+
client2, server2 = connect()
5443+
with client, server, client2, server2:
5444+
server.close()
5445+
client.settimeout(support.SHORT_TIMEOUT)
5446+
with self.assertRaises(OSError):
5447+
while True:
5448+
client.send(b'x' * 1024)
5449+
5450+
client2.setblocking(False)
5451+
self.assertRaises(ssl.SSLWantReadError, client2.recv, 1)
5452+
5453+
def test_truncated_read_does_not_leak_to_other_sockets(self):
5454+
# gh-158313: If the peer sends FIN then RST without close_notify,
5455+
# OpenSSL 3.5 queues an unexpected EOF error, followed by a system
5456+
# error from its attempt to send an alert. SSL_get_error() reports
5457+
# SSL_ERROR_SSL, whereas a failed write reports SSL_ERROR_SYSCALL.
5458+
client_context, server_context, hostname = testing_context()
5459+
# Processing a session ticket would clear the error queue.
5460+
server_context.num_tickets = 0
5461+
5462+
listener = socket.create_server((HOST, 0))
5463+
self.addCleanup(listener.close)
5464+
5465+
def connect():
5466+
csock = socket.create_connection(listener.getsockname(),
5467+
timeout=support.SHORT_TIMEOUT)
5468+
ssock, _ = listener.accept()
5469+
ssock.settimeout(support.SHORT_TIMEOUT)
5470+
client = client_context.wrap_socket(
5471+
csock, server_hostname=hostname, do_handshake_on_connect=False)
5472+
server = server_context.wrap_socket(
5473+
ssock, server_side=True, do_handshake_on_connect=False)
5474+
thread = threading.Thread(target=server.do_handshake)
5475+
thread.start()
5476+
client.do_handshake()
5477+
thread.join()
5478+
return client, server
5479+
5480+
client, server = connect()
5481+
client2, server2 = connect()
5482+
with client, server, client2, server2:
5483+
server.sendall(b'part of a response')
5484+
server.shutdown(socket.SHUT_WR) # FIN, without close_notify
5485+
server.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
5486+
struct.pack('ii', 1, 0))
5487+
server.close() # RST
5488+
5489+
# How the truncation is reported depends on the platform and the
5490+
# OpenSSL version: EOF (b'') or OSError.
5491+
with contextlib.suppress(OSError):
5492+
while client.recv(1024):
5493+
pass
5494+
5495+
client2.setblocking(False)
5496+
self.assertRaises(ssl.SSLWantReadError, client2.recv, 1)
5497+
54235498

54245499
@unittest.skipUnless(has_tls_version('TLSv1_3') and ssl.HAS_PHA,
54255500
"Test needs TLS 1.3 PHA")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :mod:`ssl` leaving a system error on the OpenSSL error queue of the
2+
thread after raising :exc:`OSError`, which made later I/O on other SSL
3+
sockets in the same thread fail with the same error.

‎Modules/_ssl.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ PySSL_SetError(PySSLSocket *sslsock, _PySSLError err, PyObject *exc,
746746
type = state->PySSLCertVerificationErrorObject;
747747
}
748748
if (ERR_GET_LIB(e) == ERR_LIB_SYS) {
749+
ERR_clear_error();
749750
// A system error is being reported; reason is set to errno
750751
errno = ERR_GET_REASON(e);
751752
return PyErr_SetFromErrno(PyExc_OSError);
@@ -776,6 +777,7 @@ PySSL_SetError(PySSLSocket *sslsock, _PySSLError err, PyObject *exc,
776777
}
777778
#endif
778779
if (ERR_GET_LIB(e) == ERR_LIB_SYS) {
780+
ERR_clear_error();
779781
// A system error is being reported; reason is set to errno
780782
errno = ERR_GET_REASON(e);
781783
return PyErr_SetFromErrno(PyExc_OSError);

0 commit comments

Comments
 (0)