]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: ocsp: Increment certificate_ocsp refcount in 'ssl_sock_copy_cert_key_and_c...
authorRemi Tricot-Le Breton <rlebreton@haproxy.com>
Thu, 26 Nov 2026 14:38:59 +0000 (15:38 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 26 Nov 2026 14:55:47 +0000 (15:55 +0100)
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.

src/ssl_ckch.c

index 77528283d40ab9a337c8b330486e17bf30c96a07..076ba5f3107ddc0457ab902fff06618de9c97025 100644 (file)
@@ -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;