arc4random does not reseed after using fork()

Johnothan King johnothanking@protonmail.com
Sun Jan 28 05:45:11 GMT 2024


> FYI: There was a thread in Nov 2023 about rand() which might be related.
> 
> rand is not ISO C compliant in Cygwin
> https://cygwin.com/pipermail/cygwin/2023-November/254735.html
> 
> conclusion of that thread:
> https://cygwin.com/pipermail/cygwin/2023-November/254764.html

I don't think that has much relevance here, as this is regarding
arc4random's seed after a fork, not rand's behavior in threads.
In any case I think I have found the cause of the bug. In
arc4random.c, _rs_stir_if_needed() attempts to detect a fork by
calling _rs_forkdetect(), which is located in arc4random.h. However,
that function is nothing more than a stub:
   static inline void
   _rs_forkdetect(void)
   {
   #ifdef _ARC4RANDOM_FORKDETECT
   	_ARC4RANDOM_FORKDETECT();
   #endif
   }
If _ARC4RANDOM_FORKDETECT is defined, _rs_forkdetect will attempt to
use it for detecting if a fork occured (the LibreSSL implementation
then zeros out `rs' with 'memset(rs, 0, sizeof(*rs));'.
Yet Cygwin has no such function or macro, so _rs_forkdetect does
nothing and _rs_stir subsequently cannot reseed after a fork() occurs.

- Johnothan King


More information about the Cygwin mailing list