mod_ssl: fix leak of X509 reference in ssl_hook_Access_classic quick renegotiation path#649
Open
rootvector2 wants to merge 1 commit into
Open
Conversation
In ssl_hook_Access_classic(), when SSL renegotiation is performed via the quick path, SSL_get_peer_certificate() is called and its result is used to seed the certificate stack passed to X509_STORE_CTX_init(). SSL_get_peer_certificate() increments the X509 refcount and the caller owns the returned reference. Every other call site in this file releases the reference with X509_free(); this one did not, except in the narrow case where the original peer chain was empty (or NULL) and we built a fresh stack with sk_X509_push(), in which case the trailing sk_X509_pop_free() happens to release it. In the common path -- non-empty SSL_get_peer_cert_chain(ssl) -- the returned reference was never released, leaking one X509 reference per quick renegotiation for the lifetime of the SSL_CTX. The two early returns at "Cannot find certificate storage" and the X509_STORE_CTX_init failure path leaked unconditionally. Track the owned reference separately as peer_cert and release it in all exit paths. When ownership is transferred into the freshly-created stack via sk_X509_push(), peer_cert is cleared so sk_X509_pop_free() remains the sole owner and there is no double free.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While reading
ssl_hook_Access_classic(), I noticed that therenegotiate_quickbranch obtains a peer certificate withSSL_get_peer_certificate(ssl)and never releases it on the common path.The leak
SSL_get_peer_certificate()increments the X509 refcount and returns a reference the caller owns. Every other call site in this file releases it withX509_free()(lines 319, 615, 1531).The trailing
sk_X509_pop_free()only runs when we built the stack ourselves — i.e., only when the originalSSL_get_peer_cert_chain()was empty/NULL. In the common case where the peer chain already contains certificates, the reference is never freed, leaking one X509 refcount per quick renegotiation. The two early-return paths ("Cannot find certificate storage" and theX509_STORE_CTX_initfailure return added in bz 65902) leak unconditionally.This matches the documented OpenSSL contract for
SSL_get_peer_certificate(3):The fix
Track the owned reference as
peer_cert. When ownership is transferred into the freshly-created stack viask_X509_push(), clearpeer_certsosk_X509_pop_free()remains the sole owner and there is no double free. Releasepeer_certon every exit path.Only affects TLS ≤ 1.2 renegotiation (
ssl_hook_Access_classic); TLS 1.3 has no renegotiation and usesssl_hook_Access_modern.Notes for the reviewer
SSL_get_peer_certificate()sites in this file.CHANGESentry — happy to add one in the format the committer prefers.