Skip to content
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

[System.Security] Fix CryptographicException from ProtectedData.Protect with multiple threads #2702

Merged
merged 1 commit into from Mar 1, 2016

Conversation

akoeplinger
Copy link
Member

The code in ManagedProtection.GetKey() instantiated a new RSACryptoServiceProvider singleton without proper locking to avoid multiple threads running the initialization concurrently.

Since RSACryptoServiceProvider (or rather KeyPairPersistence underneath) uses an XML file with a name derived from the parameters to store the data there could be a case where one thread writes while another tries to read and gets garbage. This results in a CryptographicException later on.

Added locking to prevent this.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=38933

…ct with multiple threads

The code in ManagedProtection.GetKey() instantiated a new RSACryptoServiceProvider singleton without proper
locking to avoid multiple threads running the initialization concurrently.
Since RSACryptoServiceProvider (or rather KeyPairPersistence underneath) uses an XML file with a name derived
from the parameters to store the data there could be a case where one thread writes while another tries to read
and gets garbage. This results in a CryptographicException later on.

Added locking to prevent this.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=38933
tritao added a commit that referenced this pull request Mar 1, 2016
[System.Security] Fix CryptographicException from ProtectedData.Protect with multiple threads
@tritao tritao merged commit 1232756 into mono:master Mar 1, 2016
@akoeplinger akoeplinger deleted the bug-38933 branch March 1, 2016 10:12
CspParameters csp = new CspParameters ();
csp.KeyContainerName = "DAPI";
user = new RSACryptoServiceProvider (1536, csp);
lock (user_lock) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you double check if user == null whichin the lock?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it doesn't really matter in this case since in the worst case we just create another object with the same parameters. I can add it if you prefer following the double-check pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants