From: Remi Tricot-Le Breton Date: Thu, 26 Nov 2026 14:38:59 +0000 (+0100) Subject: BUG/MINOR: ocsp: Increment certificate_ocsp refcount in 'ssl_sock_copy_cert_key_and_c... X-Git-Url: https://git.kaiwu.me/http/%22../static/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=6117bf4bc64b7d443835f084a85459c1e55cefd5;p=haproxy.git BUG/MINOR: ocsp: Increment certificate_ocsp refcount in 'ssl_sock_copy_cert_key_and_chain' ssl_sock_copy_cert_key_and_chain() duplicates a ckch_data and duplicates its ocsp_cid via OCSP_CERTID_dup(). Since the response tree is keyed on the OCSP_CERTID itself, the copy points to the very same certificate_ocsp entry as the original ckch_data, but no additional reference was taken on it. This was an issue because ssl_sock_free_cert_key_and_chain_contents unconditionally decrements this certificate_ocsp's refcount_store while the matching increment was only performed in ssl_sock_load_ocsp(), If a duplicated ckch_data is freed (an aborted "set ssl cert" transaction for instance) while the original ckch_store is still alive, refcount_store can fall to 0 and the shared certificate_ocsp entry be freed, leaving the original ckch_store with a dangling pointer. The next OCSP response lookup or update would then use freed memory. This was reported by brhenc in GitHub issue #3591. This patch should be backported up to 2.8. --- diff --git a/src/ssl_ckch.c b/src/ssl_ckch.c index 77528283d..076ba5f31 100644 --- a/src/ssl_ckch.c +++ b/src/ssl_ckch.c @@ -936,6 +936,22 @@ struct ckch_data *ssl_sock_copy_cert_key_and_chain(struct ckch_data *src, dst->ocsp_issuer = src->ocsp_issuer; } dst->ocsp_cid = OCSP_CERTID_dup(src->ocsp_cid); + if (dst->ocsp_cid) { + /* Increment the corresponding certificate_ocsp reference + * counter to mirror the decrement happening in + * ssl_sock_free_cert_key_and_chain_contents */ + struct certificate_ocsp *ocsp = NULL; + unsigned char certid[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; + unsigned int certid_length = 0; + + if (ssl_ocsp_build_response_key(dst->ocsp_cid, (unsigned char*)certid, &certid_length) >= 0) { + HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); + ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, certid, OCSP_MAX_CERTID_ASN1_LENGTH); + if (ocsp) + ocsp->refcount_store++; + HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); + } + } #endif return dst;