BIT-libpython-2026-19445: Use-after-free of a server-side SSLContext when sni_callback switches contexts
A remote, unauthenticated TLS client can make a server crash or call through a freed pointer if its snicallback assigns a different context to SSLSocket.context (the documented way to select a certificate per server name) and nothing else keeps the original ssl.SSLContext alive. Typical cases are servers that create an SSLContext per connection or replace it while connections are open; servers that wrap their listening socket with it are not affected.
Mitigation: keep a reference to every SSLContext that sets snicallback for the lifetime of the server. TLS clients are not affected.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Compensating control
For the lifetime of the server, keep a reference to every server-side SSLContext that sets sni_callback for SSLSocket.context, including contexts used when the callback switches contexts or when connections remain open.
Event History
Frequently Asked Questions
Which deployments are exposed to this issue?
Affected deployments are TLS servers whose sni_callback changes SSLSocket.context to another SSLContext and where no other reference keeps the original context alive. Servers that wrap their listening socket with the context are not affected, and TLS clients are not affected.
What does an attacker need to exploit it?
An attacker only needs to act as a remote, unauthenticated TLS client. Exploitation depends on the server using the affected SNI callback context-switching pattern and allowing the original SSLContext to be freed.
What can be done if patching is not immediately possible?
Keep a reference to every SSLContext that sets sni_callback for the lifetime of the server. This prevents the original context from being freed while it may still be used.