Release Android X509 chain certificate GREFs#128284
Draft
simonrozsival wants to merge 1 commit into
Draft
Conversation
The Android X509 chain PAL returns JNI global references for chain certificates. Creating X509Certificate2 from those pointers duplicates the references for managed ownership, leaving the native-returned references caller-owned. Release those temporary references after conversion so repeated chain builds do not exhaust ART global refs. Co-authored-by: Copilot <[email protected]>
5853fff to
cce39fd
Compare
5853fff to
cce39fd
Compare
Contributor
|
Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones |
This was referenced May 16, 2026
bartonjs
approved these changes
May 16, 2026
vcsjones
reviewed
May 17, 2026
| for (int i = 0; i < res; i++) | ||
| { | ||
| // X509Certificate2 duplicates these JNI global refs; the native-returned refs remain caller-owned. | ||
| certs[i] = new X509Certificate2(certPtrs[i]); |
Member
There was a problem hiding this comment.
This is probably fine as a short-term fix, especially if you want to backport this, but I wonder if we should just add an overload of FromHandle on AndroidCertificatePal that does not duplicate the handle, then pass the instance of AndroidCertificatePal to new X509Certificate2. e.g.
AndroidCertificatePal pal = AndroidCertificatePal.FromHandleNoDuplicate(certPtrs[i]);
certs[i] = new X509Certificate2(pal);This way the PAL can take ownership of the handle passed in to it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a JNI global-reference leak in the Android X509 chain PAL.
AndroidCryptoNative_X509ChainGetCertificatesreturns caller-owned JNI global refs for the certificates in the built chain. ConstructingX509Certificate2from those pointers duplicates the refs for managed ownership, but the original returned refs were never released. This releases those temporary refs after the managed certificates have been created.Validation: