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

View File

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