Skip to content

Release Android X509 chain certificate GREFs#128284

Draft
simonrozsival wants to merge 1 commit into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/android-x509-gref-leak-fix
Draft

Release Android X509 chain certificate GREFs#128284
simonrozsival wants to merge 1 commit into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/android-x509-gref-leak-fix

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

@simonrozsival simonrozsival commented May 16, 2026

Fixes a JNI global-reference leak in the Android X509 chain PAL.

AndroidCryptoNative_X509ChainGetCertificates returns caller-owned JNI global refs for the certificates in the built chain. Constructing X509Certificate2 from 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:

  • Local emulator stress repro previously reached 13k chain builds with bounded GREFs and no JNI overflow.

Copilot AI review requested due to automatic review settings May 16, 2026 13:01
@github-actions github-actions Bot added the area-crossgen2-coreclr only use for closed issues label May 16, 2026
@simonrozsival simonrozsival marked this pull request as draft May 16, 2026 13:03
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]>
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/android-x509-gref-leak-fix branch from 5853fff to cce39fd Compare May 16, 2026 13:05
@simonrozsival simonrozsival reopened this May 16, 2026
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/android-x509-gref-leak-fix branch from 5853fff to cce39fd Compare May 16, 2026 13:10
@simonrozsival simonrozsival removed the request for review from MichalStrehovsky May 16, 2026 13:10
@simonrozsival simonrozsival requested a review from vcsjones May 16, 2026 13:11
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants