Skip to content

Commit a071788

Browse files
authored
gh-155782: avoid a crash in ssl.SSLObject.group for session-less objects (#155829)
1 parent d42178a commit a071788

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,16 @@ def test_set_server_sigalgs(self):
11351135
if CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS:
11361136
self.assertIsNone(ctx.set_server_sigalgs('rsa_pss_rsae_sha256:?foo'))
11371137

1138+
@unittest.skipUnless(CAN_GET_SELECTED_OPENSSL_GROUP,
1139+
"SSL library doesn't support getting selected group")
1140+
def test_no_session_group_does_not_crash(self):
1141+
# Ensure that .group() does not crash because of OpenSSL itself.
1142+
# See https://github.com/python/cpython/issues/155782.
1143+
ctx = ssl.create_default_context()
1144+
ctx.check_hostname = False
1145+
obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
1146+
self.assertIsNone(obj.group())
1147+
11381148
def test_options(self):
11391149
# Test default SSLContext options
11401150
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`ssl`: prevent a crash in :meth:`~ssl.SSLSocket.group`
2+
for session-less objects on OpenSSL 3.2 and later. Patch by Bénédikt Tran.

‎Modules/_ssl.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,9 @@ _ssl__SSLSocket_group_impl(PySSLSocket *self)
21972197
#if OPENSSL_VERSION_NUMBER >= 0x30200000L
21982198
const char *group_name;
21992199

2200-
if (self->ssl == NULL) {
2200+
// OpenSSL issue: SSL_get0_group_name(...) crashes if no session exists.
2201+
// See https://github.com/openssl/openssl/issues/32379.
2202+
if (self->ssl == NULL || SSL_get_session(self->ssl) == NULL) {
22012203
Py_RETURN_NONE;
22022204
}
22032205
group_name = SSL_get0_group_name(self->ssl);

0 commit comments

Comments
 (0)