-
Notifications
You must be signed in to change notification settings - Fork 1k
Compat layer: fix memory leak in X509_get0_pubkey #11467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6554,6 +6554,31 @@ WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509) | |
| } | ||
| return key; | ||
| } | ||
|
|
||
|
|
||
| /* Get the certificate's public key without transferring ownership. | ||
| * | ||
| * The get0 form returns a pointer into the certificate which the caller must | ||
| * not free, It is released with the rest of the certificate's contents. | ||
| * | ||
| * Note: the first call on a given certificate should not race another; the | ||
| * cache is built without a lock, as elsewhere in this layer. | ||
| * | ||
| * @param [in] x509 Certificate. | ||
| * @return Public key on success, NULL on error. | ||
| */ | ||
| WOLFSSL_EVP_PKEY* wolfSSL_X509_get0_pubkey(WOLFSSL_X509* x509) | ||
| { | ||
| WOLFSSL_ENTER("wolfSSL_X509_get0_pubkey"); | ||
|
|
||
| if (x509 == NULL) | ||
| return NULL; | ||
|
|
||
| if (x509->pubKeyCache == NULL) | ||
| x509->pubKeyCache = wolfSSL_X509_get_pubkey(x509); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsynchronized lazy init of pubKeyCache races and leaks on shared X509 · Race conditions The getter writes Suggested fix: Fill the cache under the X509's existing ref/lock mutex, and free the losing allocation, or decode the key once when the certificate is created. |
||
|
|
||
| return x509->pubKeyCache; | ||
| } | ||
| #endif /* OPENSSL_EXTRA_X509_SMALL */ | ||
|
|
||
| /* End of smaller subset of X509 compatibility functions. Avoid increasing the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pubKeyCache is not invalidated when the certificate public key changes · Logic errors
wolfSSL_X509_set_pubkey(src/x509.c:16911-16913) replacespubKey.bufferandpubKeyOIDbut leavespubKeyCachealone. After that call,X509_get0_pubkeykeeps returning the old key, so code that verifies or compares with the returned key uses the wrong public key.Suggested fix: In
wolfSSL_X509_set_pubkey, freepubKeyCacheand set it to NULL after a successful update, and add a test that calls get0, then set_pubkey, then get0 again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please review