Skip to content

Free certificates, not just the list, in the OpenSSL credentials code - #233

Merged
michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
xu826Jamin:fix-openssl-certificate-leaks
Nov 25, 2026
Merged

michaelrsweet merged 1 commit into
OpenPrinting:masterfrom
xu826Jamin:fix-openssl-certificate-leaks

Conversation

@xu826Jamin

Copy link
Copy Markdown
Contributor

sk_X509_free() frees only the list, not the certificates in it. Five callers in cups/tls-openssl.c release a list returned by openssl_load_x509() (which owns its certificates) that way, so every certificate leaks. This changes them to sk_X509_pop_free(list, X509_free):

  • cupsAreCredentialsValidForName
  • cupsGetCredentialsExpiration
  • cupsGetCredentialsInfo
  • cupsGetCredentialsTrust (the sitecerts list and the certs list; cert is taken off the list with sk_X509_shift() and is still freed separately)

Five lines changed, one file.

How I found and checked it

Built master (dba4a4e) with --with-sanitizer=address --enable-debug against OpenSSL 3.0.13 and ran ./testcreds in cups/ (relinking the binary each time):

leaked at exit ASan errors test failures
master 313,791 bytes in 5,939 allocations 0 0
this change 41,818 bytes in 680 allocations 0 0

The remaining 41,818 bytes come from other functions in the same file (extension stacks and public-key references in cupsCreateCredentials and cupsSignCredentialsRequest); I can send those as a separate change if you want them. testhttp also passes. I did not test GnuTLS, LibreSSL or Valgrind.

I deliberately did not touch _httpFreeCredentials: _httpTLSStart hands the chain certificates to the SSL context, so freeing them there as well would double-free.

AI assistance (per DEVELOPING.md)

Assisted-by: Claude:claude-sonnet-5-5 AddressSanitizer

…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).
@michaelrsweet michaelrsweet self-assigned this Nov 25, 2026

@michaelrsweet michaelrsweet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@michaelrsweet michaelrsweet added bug Something isn't working platform issue Issue is specific to an OS or desktop labels Nov 25, 2026
@michaelrsweet michaelrsweet added this to the Stable milestone Nov 25, 2026
@michaelrsweet
michaelrsweet merged commit b94ee9e into OpenPrinting:master Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working platform issue Issue is specific to an OS or desktop

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants