Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8f86fed

Browse files
committed
Fix "invalid handle" failure in X509Cert's copy ctor on macOS
The certificate being copied from could be eligible for collection after its raw IntPtr handle is accessed but before the copy ctor actually gets around to reading from it, leading to use after free and recycling problems. The fix is simply to ensure the other certificate can't be collected until after we're done using the raw handle.
1 parent e1c0bf1 commit 8f86fed

File tree

1 file changed

+3
-1
lines changed
  • src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX

1 file changed

+3
-1
lines changed

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX/CertificatePal.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public static ICertificatePal FromOtherCert(X509Certificate cert)
5959
{
6060
Debug.Assert(cert.Pal != null);
6161

62-
return FromHandle(cert.Handle);
62+
ICertificatePal pal = FromHandle(cert.Handle);
63+
GC.KeepAlive(cert); // ensure cert's safe handle isn't finalized while raw handle is in use
64+
return pal;
6365
}
6466

6567
public static ICertificatePal FromBlob(

0 commit comments

Comments
 (0)