Repository navigation
Closed
Labels
area-System.Securityos-macosmacOS aka OSXtracking-external-issueThe issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Milestone
Description
On macOS Monterey, I would expect this program to run forever.
using System.Security.Cryptography;
DSA dsa;
void Work()
{
while (true)
{
try
{
dsa = DSA.Create();
dsa.ImportParameters(new DSAParameters
{
P = Convert.FromBase64String(@"
nEx7rLmUg+FLq23XB/8rVFU3Txktd4NYVppGrJMdRKi0FktEj39g7vM33rA0g8Xf
BurQu9HkcblSR25E5beYrMbU8pJD1ZqmrltbnnlB+PHX5Pgbu91BCr2d5UjAIfiA
qIlnySMuV0XSqbb1A3qyWGIx3ATXBaXN9mm+paF2itE="),
Q = Convert.FromBase64String("vV0TbUwrTOkOoiyTJDxsaKWqWjE="),
G = Convert.FromBase64String(@"
XZESzrsgUFaS697sgeQEnFKrhh3S6C+gfVG2wL9JBv636QsEq2uxpOMl/1VQxjqx
Cys3x9YFOkdY1xYdk4ayhco6LYVr81X/lRUtx0YZxpaTt10XgcnlLwx772pYCcOH
UlyyGxq3GYCA1cglXtS80gPHIYieOqmUhvBHXMYBCAg="),
Y = Convert.FromBase64String(@"
e7NMNCxX/44GS2gUH+JyReWzdCUXcp6ax0PcF/XvIZ1mak74P8o8yqWseGa/10hR
CT92or4YBROsGtKqD/wqN0yJvVMkpPHHsWU9zs1Zt4CsQaZgUTw+vyjkw674OuyN
933pL+qQNvPuJcb/HK9ME2vSN/3Ki1lAqqKWuzcvggY="),
X = Convert.FromBase64String(@"DQrQZHBuIxyLlLqtNqOULp/tlH0="),
});
dsa.Dispose();
}
catch (Exception)
{
// Managed exceptions are okay, looking for crashes.
}
}
}
Thread t1 = new Thread(Work);
Thread t2 = new Thread(Work);
t1.Start();
t2.Start();
t1.Join();
t2.Join();It will quickly exit with status code 139, a segmentation fault.
I cannot reproduce this for RSA or ECDsa, only DSA.
The native stack trace is below. Unfortunately I've been struggling quite a bit to get dotnet-sos and dotnet-symbol to work on Apple Silicon, so no managed stack trace.
Details
frame #1: 0x00000001b2067ca8 Security`CssmManager::getModule(Security::Guid const&) + 28
frame #2: 0x00000001b20676a0 Security`CSSM_ModuleAttach + 104
frame #3: 0x00000001b2058c04 Security`Security::CssmClient::AttachmentImpl::activate() + 332
frame #4: 0x00000001b2089f50 Security`Security::CssmClient::KeyImpl::deactivate() + 180
frame #5: 0x00000001b21dd940 Security`Security::CssmClient::KeyImpl::~KeyImpl() + 84
frame #6: 0x00000001b2089e84 Security`Security::CssmClient::KeyImpl::~KeyImpl() + 16
frame #7: 0x00000001b21b6dd0 Security`Security::RefPointer::release_internal() + 112
frame #8: 0x00000001b2058498 Security`Security::RefPointer::release() + 56
frame #9: 0x00000001b21b6d34 Security`Security::RefPointer::~RefPointer() + 24
frame #10: 0x00000001b2290304 Security`Security::KeychainCore::KeyItem::~KeyItem() + 140
frame #11: 0x00000001b2089e5c Security`Security::KeychainCore::KeyItem::~KeyItem() + 16
frame #12: 0x00000001b22bed74 Security`SecCDSAKeyDestroy(__SecKey*) + 328
frame #13: 0x00000001b00fad78 CoreFoundation`_CFRelease + 232
frame #14: 0x0000000280f56944
frame #15: 0x00000002804ed554
frame #16: 0x00000002804ed25c
frame #17: 0x0000000280f56a28
frame #18: 0x00000002804ed094
frame #19: 0x00000001033af5c8 libcoreclr.dylib`CallDescrWorkerInternal + 132
frame #20: 0x000000010321fc2c libcoreclr.dylib`DispatchCallSimple(unsigned long*, unsigned int, unsigned long, unsigned int) + 284
frame #21: 0x00000001031a8cb4 libcoreclr.dylib`MethodTable::CallFinalizer(Object*) + 400
frame #22: 0x0000000103260068 libcoreclr.dylib`FinalizerThread::FinalizeAllObjects() + 364
frame #23: 0x0000000103260270 libcoreclr.dylib`FinalizerThread::FinalizerThreadWorker(void*) + 184
frame #24: 0x00000001031eaaa0 libcoreclr.dylib`ManagedThreadBase_DispatchOuter(ManagedThreadCallState*) + 260
frame #25: 0x00000001031eb074 libcoreclr.dylib`ManagedThreadBase::FinalizerBase(void (*)(void*)) + 36
frame #26: 0x00000001032603ec libcoreclr.dylib`FinalizerThread::FinalizerThreadStart(void*) + 88
frame #27: 0x00000001030fd380 libcoreclr.dylib`CorUnix::CPalThread::ThreadEntry(void*) + 380
frame #28: 0x00000001aff5426c libsystem_pthread.dylib`_pthread_start + 148
Metadata
Metadata
Assignees
Labels
area-System.Securityos-macosmacOS aka OSXtracking-external-issueThe issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Activity
bartonjs commentedon Jul 6, 2022
Maybe it's time to just mark
DSA.Create()as[UnsupportedOS("macOS")]and rip out DSASecurityTransforms.@GrabYourPitchforks, thoughts?
bartonjs commentedon Jul 6, 2022
ImportFromPem_Pkcs8_UnrelatedPrecedingPem (
runtime/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSAKeyPemTests.cs
Lines 73 to 94 in 0c4ee9e
vcsjones commentedon Jul 7, 2022
Oh, hm. Not sure how I misread that. Regardless, we can open a separate issue for that test failure then, if we want. But if we deprecate DSA on MacOS then it would have the same result.
filipnavara commentedon Jul 7, 2022
DSA is the only algorithm that uses the old CSSM keys (aside from X509Certificate impl. calling into it). Given the fact that it was always limited to one size of key and key creation didn't work I would be fine with deprecating it. That said, it would be nice to understand the problem so the X509 classes don't run into it.
filipnavara commentedon Jul 7, 2022
Native stack trace:
Managed stack trace:
The fact that this comes from finalization worries me. All the handles should have been disposed properly.
filipnavara commentedon Jul 7, 2022
Ah, I see now that the managed
catchis always triggered which explains why the handles are leaked. I didn't realize theDSAobject is shared between the threads.filipnavara commentedon Jul 7, 2022
For completeness where
eestackoutput:vcsjones commentedon Jul 7, 2022
Okay, multithreading and concurrency is not related. Just letting
DSAobjects get finalized will crash.Simpler repro (takes longer to crash, but still happens pretty quickly)
[-]SIGSEGV with disposing of DSA keys concurrently on macOS[/-][+]SIGSEGV when DSA keys are finalized on macOS[/+]bartonjs commentedon Jul 7, 2022
If both threads hit the line
_keys = newKeysat the same time, one of the twonewKeysvalues will be lost to the GC/finalizer.filipnavara commentedon Jul 7, 2022
"longer" is an understatement. Running that on .NET 7 didn't crash for 2 hours now 😅
17 remaining items
bartonjs commentedon Jul 7, 2022
At one or two methods I'd say we can just lock it. But at more than that it feels like we might want to wait for Apple's answer 😄
vcsjones commentedon Jul 7, 2022
I hope the answer is "no" but I am going to ask anyway because I am on an iPad for most of the week... this CSSM module map isn't shared between processes, is it?
Or, more specifically, if those threads turned in to separate processes, it would not reproduce?
Looking at my backtrace more in my "simpler" reproduction, it's still a multi threading issue - the finalizer is the other thread. So while there is no explicit threading going on, the finalizer and the main thread are racing this CSSM issue.
filipnavara commentedon Jul 7, 2022
Yep. The issue can happen with any place where the CSSM module is loaded/unloaded.
SecItemImportis the most obvious one but certainly not the only one. Any place whereSecKeyRefgets created (eg. materialized from X509 certificate) or released (eg. releasingSecIdentityRefindirectly releases associated key) is prone to potentially hit this.I had different workaround in mind - loading all CSSM modules as part of initialization and keeping them alive forever. That way the lack of the lock would not be observable because the hash map would no longer be modified. I abandoned this idea when I realized that there's more than 2 CSSM modules. The list of actually usable CSSM modules is likely finite (the public API is deprecated since macOS 10.7) and limited but it's not obvious how limited.
No, thankfully not.
It would not.
Correct. It is multi-threading issue.
CFReleasedoes a non-locked lookup in a hash map. The CSSM modules likely get unloaded with last key usage and loaded again when necessary.vcsjones commentedon Jul 7, 2022
For what it's worth, it looks like Chromium has noticed this issue:
https://chromium.googlesource.com/chromium/src/+/refs/heads/main/crypto/mac_security_services_lock.h#16
If you look at usages of
GetMacSecurityServicesLockthroughout Chromium, they do quite a bit of locking.filipnavara commentedon Jul 7, 2022
Thanks for opening this can of worms:
chromium/chromium@d169b90
bartonjs commentedon Jul 7, 2022
Playing on @filipnavara's earlier supposition about the last reference being the problem, then I wonder if we can do something like hold a DSA public+private keypair in a static from DSASecurityTransforms (maybe even going so far as to marking them as
SetHandleAsInvalid()so they don't ever release) would stop the exception.For @vcsjones' test, that'd be just hold one imported DSA key outside the loop, and then the crash may go away.
We might end up with one for DSA, one for ECDH, one for ECDSA, and one for RSA, but that's better than locking?
filipnavara commentedon Jul 7, 2022
That's basically the same idea as holding references to all CSSM modules, expressed in high-level API terminology.
(You may not need one key per type, perhaps even one would be enough but it's not immediately obvious which CSSM module does what)
vcsjones commentedon Jul 8, 2022
This still crashes almost instantly
Maybe the
SecKeyPairis still internally getting collected somehow, but first try seems to suggest that is not a sufficient fix.Even with my example using just the finalizer as the concurrent thread still crashes.
[-]SIGSEGV when DSA keys are finalized on macOS[/-][+]SIGSEGV when independent DSA keys are concurrently disposed[/+]