GH-1076: Fix use of getrandom()

The availability and use of getrandom() actually caused unrandom and
deterministic results in terms of Zeek's random number generation.
This commit is contained in:
Jon Siwek 2020-07-21 14:24:50 -07:00
parent d7425b90d7
commit dba764386b
3 changed files with 52 additions and 10 deletions

View file

@ -0,0 +1 @@
F

View file

@ -1,13 +1,19 @@
#
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: zeek -b %INPUT do_seed=F >out.2
# @TEST-EXEC: unset ZEEK_SEED_FILE && zeek -b %INPUT real_random=T >out.3
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff out.2
# @TEST-EXEC: btest-diff out.3
const do_seed = T &redef;
const real_random = F &redef;
event zeek_init()
{
if ( real_random )
return;
local a = rand(1000);
local b = rand(1000);
local c = rand(1000);
@ -27,3 +33,38 @@ event zeek_init()
print e;
print f;
}
event zeek_init() &priority=-10
{
if ( ! real_random )
return;
local v1: vector of count = vector();
local v2: vector of count = vector();
local i = 0;
while ( i < 20 )
{
v1 += rand(65535);
i += 1;
}
i = 0;
while ( i < 20 )
{
v2 += rand(65535);
i += 1;
}
# Note: this is expected to be F with high probability, but
# technically could all be the same because, well, that's a
# valid "random" sequence, too
print all_set(v1 == v2);
if ( all_set(v1 == v2) )
{
print v1;
print v2;
}
}