[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: random(3) in OpenBSD
|
From: |
Corinna Vinschen |
|
Subject: |
Re: random(3) in OpenBSD |
|
Date: |
Tue, 14 Nov 2023 22:55:08 +0100 |
Hi Bruno,
On Nov 14 19:33, Bruno Haible wrote:
> [CCing bug-gnulib]
>
> Hi Corinna,
>
> > Looking into all the BSD variants of random(), I found that OpenBSD
> > defaults the random() function to return non-deterministic output. It
> > works like this:
> >
> > - A global var random_deterministic defaults to 0.
> > - Calls to srandom() set random_deterministic to 0 and ignore their
> > input.
> > - Calls to srandom_deterministic() sets random_deterministic to 1 and
> > seeds the usual PRNG.
> > - initstate() and setstate() set random_deterministic to 1.
> > - random() returns either the next value from the PRNG if
> > random_deterministic is 1, or the next value generated by a call to
> > arc4random() if random_deterministic is 0. So by default, it
> > returns arc4random() values.
> >
> > With that behaviour, OpenBSD appears to stray from POSIX deliberately.
>
> Yup, I observed this too when I found that the gnulib MT test for random()
> yielded strange results on OpenBSD. [1]
>
> > I was wondering if you want to add this as incompatibility to your
> > document at
> > https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=doc/posix-functions/random.texi
> > perhaps.
>
> I documented it as an incompatibility for the srandom() function. But you are
> right, it's also an incompatibility for the random() function, since POSIX [2]
> says "random() shall produce by default a sequence of numbers that can be
> duplicated by calling srandom() with 1 as the seed".
Yes, that's the problem. However, I think your addition to random.texi
isn't quite right.
> +When @samp{srandom} was not called, this function returns a non-deterministic
> +sequence rather than a deterministic sequence of numbers on some platforms:
> +OpenBSD 7.4.
In fact, as I outlined above, srandom(3) does *not* initiate a seed for
the deterministic PRNG. It just sets the global var random_deterministic
to 0.
In turn, random(3) will call arc4random(), which typically generates
non-deterministic values from getentropy(3) or from /dev/random.
An application which requires deterministic, reproducible values from
a PRNG has to call initstate(), setstate(), or the OpenBSD-specific
function srandom_deterministic().
HTH,
Corinna