arc4random does not reseed after using fork()

Johnothan King johnothanking@protonmail.com
Fri Jan 26 22:06:15 GMT 2024


Hello,

While testing ksh93u+m's recently added SRANDOM variable[1], I have
discovered a bug in Cygwin's arc4random function. After using fork(),
arc4random does not reseed itself, which causes the results to become
predictable[2]. Below is a test case C program exhibiting the bug:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
	pid_t child = fork();
	if(child==0)
	{
		printf("%u %u %u\n", arc4random(), arc4random(), arc4random());
		return 0;
	}
	else if(child==-1)
		return 1;
	waitpid(child, NULL, 0);
	printf("%u %u %u\n", arc4random(), arc4random(), arc4random());
	return 0;
}

Cygwin output:
3249037162 736770761 3917821637
3249037162 736770761 3917821637

Linux output (using glibc's arc4random):
746998953 2346785455 2324882761
2868775179 924299332 3954938398

[1]: https://github.com/ksh93/ksh/commit/00b296c
[2]: https://github.com/ksh93/ksh/issues/711

- Johnothan King


More information about the Cygwin mailing list