From 01599b1bd03c076c10ef1fe18c3df96e801ba6c8 Mon Sep 17 00:00:00 2001 From: xu826Jamin Date: Tue, 24 Nov 2026 21:51:49 -0500 Subject: [PATCH] Free the certificates, not just the list, in the OpenSSL credentials code openssl_load_x509() returns a STACK_OF(X509) that owns the certificates it read, but five callers released it with sk_X509_free(), which frees only the list and leaks every certificate in it. Use sk_X509_pop_free(list, X509_free) in cupsAreCredentialsValidForName, cupsGetCredentialsExpiration, cupsGetCredentialsInfo and the two lists in cupsGetCredentialsTrust. "./testcreds" in an AddressSanitizer build leaked 313,791 bytes in 5,939 allocations before the change; this change removes most of that (41,818 bytes in 680 allocations remain, from other functions). --- cups/tls-openssl.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cups/tls-openssl.c b/cups/tls-openssl.c index a30e06743..ffabf5daf 100644 --- a/cups/tls-openssl.c +++ b/cups/tls-openssl.c @@ -95,7 +95,7 @@ cupsAreCredentialsValidForName( // Check the hostname against the primary certificate... result = openssl_validate_name(sk_X509_value(certs, 0), common_name); - sk_X509_free(certs); + sk_X509_pop_free(certs, X509_free); } return (result); @@ -681,7 +681,7 @@ cupsGetCredentialsExpiration( if ((certs = openssl_load_x509(credentials)) != NULL) { result = openssl_get_date(sk_X509_value(certs, 0), 1); - sk_X509_free(certs); + sk_X509_pop_free(certs, X509_free); } return (result); @@ -775,7 +775,7 @@ cupsGetCredentialsInfo( cupsHashData("md5", credentials, strlen(credentials), md5_digest, sizeof(md5_digest)); snprintf(buffer, bufsize, "%s (issued by %s) / %s / %s / %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", name, issuer, httpGetDateString(expiration, expdate, sizeof(expdate)), sigalg, md5_digest[0], md5_digest[1], md5_digest[2], md5_digest[3], md5_digest[4], md5_digest[5], md5_digest[6], md5_digest[7], md5_digest[8], md5_digest[9], md5_digest[10], md5_digest[11], md5_digest[12], md5_digest[13], md5_digest[14], md5_digest[15]); - sk_X509_free(certs); + sk_X509_pop_free(certs, X509_free); } DEBUG_printf("1cupsGetCredentialsInfo: Returning \"%s\".", buffer); @@ -962,7 +962,7 @@ cupsGetCredentialsTrust( X509_STORE_CTX_free(ctx); if (sitecerts) - sk_X509_free(sitecerts); + sk_X509_pop_free(sitecerts, X509_free); } X509_STORE_free(roots); @@ -1002,7 +1002,7 @@ cupsGetCredentialsTrust( } } - sk_X509_free(certs); + sk_X509_pop_free(certs, X509_free); X509_free(cert); DEBUG_printf("1cupsGetCredentialsTrust: Returning %d.", trust);