Skip to content

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

Open
xu826Jamin wants to merge 1 commit into
OpenPrinting:masterfrom
xu826Jamin:fix-openssl-certificate-leaks
Open

xu826Jamin wants to merge 1 commit into
OpenPrinting:masterfrom
xu826Jamin:fix-openssl-certificate-leaks

Conversation

@xu826Jamin

Copy link
Copy Markdown

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).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant