Skip to content

Commit b673be4

Browse files
[3.15] gh-155782: avoid a crash in ssl.SSLObject.group for session-less objects (GH-155829) (#159740)
gh-155782: avoid a crash in `ssl.SSLObject.group` for session-less objects (GH-155829) (cherry picked from commit a071788) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 64e7708 commit b673be4

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
@@ -1127,6 +1127,16 @@ def test_set_server_sigalgs(self):
11271127
if CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS:
11281128
self.assertIsNone(ctx.set_server_sigalgs('rsa_pss_rsae_sha256:?foo'))
11291129

1130+
@unittest.skipUnless(CAN_GET_SELECTED_OPENSSL_GROUP,
1131+
"SSL library doesn't support getting selected group")
1132+
def test_no_session_group_does_not_crash(self):
1133+
# Ensure that .group() does not crash because of OpenSSL itself.
1134+
# See https://github.com/python/cpython/issues/155782.
1135+
ctx = ssl.create_default_context()
1136+
ctx.check_hostname = False
1137+
obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
1138+
self.assertIsNone(obj.group())
1139+
11301140
def test_options(self):
11311141
# Test default SSLContext options
11321142
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
@@ -2224,7 +2224,9 @@ _ssl__SSLSocket_group_impl(PySSLSocket *self)
22242224
#if OPENSSL_VERSION_NUMBER >= 0x30200000L
22252225
const char *group_name;
22262226

2227-
if (self->ssl == NULL) {
2227+
// OpenSSL issue: SSL_get0_group_name(...) crashes if no session exists.
2228+
// See https://github.com/openssl/openssl/issues/32379.
2229+
if (self->ssl == NULL || SSL_get_session(self->ssl) == NULL) {
22282230
Py_RETURN_NONE;
22292231
}
22302232
group_name = SSL_get0_group_name(self->ssl);

0 commit comments

Comments
 (0)