Skip to content

Commit e7bf61d

Browse files
committed
CDRIVER-3978 mix time to avoid duplicate RAND_bytes for the same PIDs (#815)
1 parent 8a78b0e commit e7bf61d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libmongoc/src/mongoc/mongoc-rand-openssl.c

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
int
2929
_mongoc_rand_bytes (uint8_t *buf, int num)
3030
{
31+
#if OPENSSL_VERSION_NUMBER < 0x10101000L
32+
/* Versions of OpenSSL before 1.1.1 can potentially produce the same random
33+
* sequences in processes with the same PID. Rather than attempt to detect
34+
* PID changes (useful for parent/child forking but not if PIDs wrap), mix
35+
* the current time into the generator's state.
36+
* See also: https://wiki.openssl.org/index.php/Random_fork-safety */
37+
struct timeval tv;
38+
39+
bson_gettimeofday (&tv);
40+
RAND_add (&tv, sizeof(tv), 0.0);
41+
#endif
42+
3143
return RAND_bytes (buf, num);
3244
}
3345

0 commit comments

Comments
 (0)