Skip to content

Conversation

@jkotas
Copy link
Member

@jkotas jkotas commented Sep 26, 2020

Fixes #42752

@ghost
Copy link

ghost commented Sep 26, 2020

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

@stephentoub
Copy link
Member

stephentoub commented Sep 26, 2020

What is the perf impact of this, on Linux and on macOS? I get that there's a desire for the Unix implementation to maintain undocumented behaviors that Windows happens to have, but if memory serves, the perf impact here especially on mac was substantial. I worry we're going to keep just going back and forth based on the latest issue filed. The latest issue cites a potential security pit of failure, but previous issues have cited essentially the same but for perf.

@jkotas
Copy link
Member Author

jkotas commented Sep 26, 2020

This change has no perf impact on Linux. This change is only changing failure mode on Linux. Before this change, we would fallback to weak random number generator when something went wrong. After this change, exception will be thrown in this case. It should never happen in practice (famous last words).

Yes, I expect that this be slower on macOS. I will try to get some numbers.

@jkotas
Copy link
Member Author

jkotas commented Sep 26, 2020

The NewGuid performance is roughly the same on macOS and Linux with the current version of the change.

On OSX 1015, this change makes Guid.NewGuid about 2.2 slower.
On OSX 1014, this change makes Guid.NewGuid about 4.6 slower.

This is due to underlying OS implementation changes. arc4random seems to have gotten much slower in OSX 1015. arc4random in OSX is actually implemented as wrapper over OSX core crypto APIs (see ifdef at the top of https://opensource.apple.com/source/Libc/Libc-1353.100.2/gen/FreeBSD/arc4random.c.auto.html), so it picks up performance impacts of core crypto hardening.

@stephentoub
Copy link
Member

On OSX 1015, this change makes Guid.NewGuid about 2.2 slower

That's much less than I was expecting. Thanks.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Thanks

@vcsjones
Copy link
Member

Just one question to help me understand...

arc4random seems to have gotten much slower in OSX 1015

But the numbers you posted show an improvement from 10.14 to 10.15.

@jkotas
Copy link
Member Author

jkotas commented Sep 26, 2020

Here are numbers that I have collected in #42777:

OSX 1014:
GetCryptographicallySecureRandomBytes((byte*)&g, sizeof(Guid)): 1.07us per call
GetNonCryptographicallySecureRandomBytes((byte*)&g, sizeof(Guid)): 0.23us per call

OSX 1015:
GetCryptographicallySecureRandomBytes((byte*)&g, sizeof(Guid)): 0.93us per call
GetNonCryptographicallySecureRandomBytes((byte*)&g, sizeof(Guid)): 0.43us per call

(Caveat: These numbers are for release build of the runtime + debug build of the libraries. They may be slightly smaller if everything is release build.)

@jkotas
Copy link
Member Author

jkotas commented Sep 26, 2020

If we wanted to make this better, we may look at:

@vcsjones
Copy link
Member

vcsjones commented Sep 26, 2020

As another data point, here are some numbers from my Mac on 10.16 11 Beta 8 built in release / release. It's a beta OS, so perhaps not valid.

Secure: 6384
NonSecure: 688
Secure: 6248
NonSecure: 689
Secure: 6318
NonSecure: 677
Secure: 6553
NonSecure: 704
Secure: 6318
NonSecure: 684

Or about 640ns for secure and 69ns for insecure.

@jkotas
Copy link
Member Author

jkotas commented Sep 26, 2020

Or about 640ns for secure and 69ns for insecure.

Hmm, I am wondering how the implementation of arc4random changed in the new macOS.

@am11
Copy link
Member

am11 commented Sep 27, 2020

@vcsjones, just curious, are those numbers from Big Sur x64 or arm64?

@vcsjones
Copy link
Member

@am11 x64. The ARM64 DTK agreement pretty strongly tells you to not post any benchmarks of any kind. (Or really even discuss it)😊

@jkotas jkotas force-pushed the random branch 2 times, most recently from 5f9b82c to 5955cb5 Compare September 27, 2020 17:05
@jkotas jkotas merged commit ae1b1be into dotnet:master Sep 28, 2020
@jkotas jkotas deleted the random branch September 28, 2020 01:52
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guid.NewGuid should guarantee a full 122 bits of entropy on non-Windows platforms

5 participants