diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c index 1c4d6eebe5fe..4294de02f47e 100644 --- a/lib/libc/gen/arc4random.c +++ b/lib/libc/gen/arc4random.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -76,7 +77,7 @@ static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; static int rs_initialized; static struct arc4_stream rs; static pid_t arc4_stir_pid; -static int arc4_count; +static int* arc4_count; extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen); @@ -89,6 +90,12 @@ arc4_init(void) { int n; + if ((arc4_count = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, + MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) + abort(); + if (minherit(arc4_count, sizeof(int), INHERIT_ZERO) == -1) + abort(); + for (n = 0; n < 256; n++) rs.s[n] = n; rs.i = 0; @@ -162,7 +169,7 @@ arc4_stir(void) */ for (i = 0; i < 3072; i++) (void)arc4_getbyte(); - arc4_count = 1600000; + *arc4_count = 1600000; } static void @@ -170,7 +177,8 @@ arc4_stir_if_needed(void) { pid_t pid = getpid(); - if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) { + if (arc4_count == NULL || *arc4_count <= 0 || !rs_initialized || + arc4_stir_pid != pid) { arc4_stir_pid = pid; arc4_stir(); } @@ -224,7 +232,8 @@ arc4random(void) { u_int32_t val; _ARC4_LOCK(); - arc4_count -= 4; + if (arc4_count) + *arc4_count -= 4; arc4_stir_if_needed(); val = arc4_getword(); _ARC4_UNLOCK(); @@ -238,7 +247,7 @@ arc4random_buf(void *_buf, size_t n) _ARC4_LOCK(); arc4_stir_if_needed(); while (n--) { - if (--arc4_count <= 0) + if (--*arc4_count <= 0) arc4_stir(); buf[n] = arc4_getbyte(); }