random(9): Restore historical [0,2^31-1] output range and related man

documention.

Commit SVN r364219 / Git 8a0edc914f changed random(9) to be a shim around
prng32(9) and inadvertently caused random(9) to begin returning numbers in the
range [0,2^32-1] instead of [0,2^31-1], where the latter has been the documented
range for decades.

The increased output range has been identified as the source of numerous bugs in
code written against the historical output range e.g. ipfw "prob" rules and
stats(3) are known to be affected, and a non-exhaustive audit of the tree
identified other random(9) consumers which are also likely affected.

As random(9) is deprecated and slated for eventual removal in 14.0, consumers
should gradually be audited and migrated to prng(9).

Submitted by:		Loic Prylli <lprylli@netflix.com>
Obtained from:		Netflix
Reviewed by:		cem, delphij, imp
MFC after:		1 day
MFC to:			stable/13, releng/13.0
Differential Revision:	https://reviews.freebsd.org/D29385
This commit is contained in:
Lawrence Stewart 2021-03-24 15:25:49 +11:00
parent 64c01719e4
commit dbbf3e3f37
2 changed files with 15 additions and 12 deletions

View File

@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\" "
.Dd December 26, 2019
.Dd March 22, 2021
.Dt RANDOM 9
.Os
.Sh NAME
@ -132,17 +132,13 @@ If the function is interrupted before the random device is seeded, no data is
returned.
.Pp
The deprecated
.Xr random 9
function will produce a sequence of pseudorandom numbers using a similar weak
linear congruential generator as
.Xr rand 3
(the 1988 Park-Miller LCG).
.Fn random
function will return a 31-bit value.
It is obsolete and scheduled to be removed in
.Fx 13.0 .
It is strongly advised that the
.Xr random 9
function not be used to generate random numbers.
See
.Fx 14.0 .
Consider
.Xr prng 9
instead and see
.Sx SECURITY CONSIDERATIONS .
.Sh RETURN VALUES
The
@ -167,6 +163,13 @@ the number of bytes placed in
.Fn read_random_uio
returns zero when successful,
otherwise an error code is returned.
.Pp
.Fn random
returns numbers
in the range from 0 to
.if t 2\u\s731\s10\d\(mi1.
.if n (2**31)\(mi1.
.Sh ERRORS
.Fn read_random_uio
may fail if:

View File

@ -45,5 +45,5 @@ __FBSDID("$FreeBSD$");
u_long
random(void)
{
return (prng32());
return (prng32() & 0x7fffffff);
}