Skip to content

Commit 285ae34

Browse files
committed
gh-155782: avoid a crash in ssl.SSLObject.group for session-less objects
1 parent c92e2fd commit 285ae34

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/test/test_ssl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,15 @@ def test_set_server_sigalgs(self):
10971097
if CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS:
10981098
self.assertIsNone(ctx.set_server_sigalgs('rsa_pss_rsae_sha256:?foo'))
10991099

1100+
@unittest.skipUnless(CAN_GET_SELECTED_OPENSSL_GROUP,
1101+
"SSL library doesn't support getting selected group")
1102+
def test_no_session_group_does_not_crash(self):
1103+
# Ensure that .group() does not crash because of OpenSSL itself.
1104+
# See https://github.com/python/cpython/issues/155782.
1105+
ctx = ssl.create_default_context()
1106+
obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
1107+
self.assertIsNone(obj.group())
1108+
11001109
def test_options(self):
11011110
# Test default SSLContext options
11021111
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:`SSLObject.group <ssl.SSLObject.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)