Skip to content

Can Array.Clear() be used to zero out sensitive byte arrays? #48697

Answered by bartonjs
samuel-lucas6 asked this question in Q&A
Discussion options

You must be logged in to vote

Currently, none of C#, F#, or VB.NET permit the compiler to drop memory writes that it knows no one can read (from a purely deterministic functional flow), so it doesn't have the problem of memset-0+free in C.

Likewise, we don't permit the JIT to skip the writes in similar cases.

We added CryptographicOperations.ZeroMemory to .NET Core 2.1 just so that callers who want to guarantee that the memory write happens have a way to visibly assert that. Like Windows SecureZeroMemory we'll guarantee that no matter what compiler optimizations and runtime optimizations are permitted that we will forcibly clear the target memory.

So... Array.Clear works. Except for when it doesn't. If you are going t…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@samuel-lucas6
Comment options

@samuel-lucas6
Comment options

@PathogenDavid
Comment options

Answer selected by samuel-lucas6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants