[Mono-dev] Mutex Bug

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Thu Jul 3 03:32:38 UTC 2014


> From: Brandon Perry [mailto:bperry.volatile at gmail.com]
> 
> Do you *have* to write to the same file?

Yes.  Here is the situation:
https://tinhatrandom.org 
An open source, MIT licensed class library aimed at improving crypto random available to the application developer...

One of the things we need to do is this:  Assuming we have collected a lot of good entropy from various entropy sources, save a good strong random seed file on disk.  Later, any number of applications may be launched, potentially at the same time, which need to read & write the file, mutually exclusively.

The reason it needs to be one file, is because that's the whole point - If the user has already entered random keyboard input, random mouse input, and we gathered entropy from the internet, and RNGCryptoServiceProvider, and other sources, then every application launch shouldn't need to repeat that.  First one does it; everyone later benefits from it.

Right now, I believe, if I use FileMode.Open,  FileAccess.ReadWrite, and FileShare.None, that should work cross-platform to ensure only a single process may access the file at a time.  The process/thread that has the file lock can read the seed, write a new seed, and flush and close.  It will work reliably, I believe, for the first process that gets there, and to prevent any subsequent processes from accessing the same seed file before it is reseeded.  Any subsequent processes that fail to open the file (because another process already has it open) I think, will simply have to Sleep(1) and retry opening the file.  It's a little ghetto, but it should work AFAIK.


More information about the Mono-devel-list mailing list