summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/rand.c
diff options
context:
space:
mode:
authorderaadt <>2014-12-08 21:45:20 +0000
committerderaadt <>2014-12-08 21:45:20 +0000
commit1e2b0be5bee045db1b0abb1f87801004db563bb8 (patch)
treebf17a76201de02c2ce50358a383001a6b6c5cc64 /src/lib/libc/stdlib/rand.c
parent4b0b4fc5d98edef87738071b3784e13573327bab (diff)
downloadopenbsd-1e2b0be5bee045db1b0abb1f87801004db563bb8.tar.gz
openbsd-1e2b0be5bee045db1b0abb1f87801004db563bb8.tar.bz2
openbsd-1e2b0be5bee045db1b0abb1f87801004db563bb8.zip
Change rand(), random(), drand48(), lrand48(), mrand48(), and srand48()
to returning strong random by default, source from arc4random(3). Parameters to the seeding functions are ignored, and the subsystems remain in strong random mode. If you wish the standardized deterministic mode, call srand_deterministic(), srandom_determistic(), srand48_deterministic(), seed48_deterministic() or lcong48_deterministic() instead. The re-entrant functions rand_r(), erand48(), nrand48(), jrand48() are unaffected by this change and remain in deterministic mode (for now). Verified as a good roadmap forward by auditing 8800 pieces of software. Roughly 60 pieces of software will need adaptation to request the deterministic mode. Violates POSIX and C89, which violate best practice in this century. ok guenther tedu millert
Diffstat (limited to 'src/lib/libc/stdlib/rand.c')
-rw-r--r--src/lib/libc/stdlib/rand.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c
index 6860dd4f71..618559fd9c 100644
--- a/src/lib/libc/stdlib/rand.c
+++ b/src/lib/libc/stdlib/rand.c
@@ -30,6 +30,7 @@
30#include <sys/types.h> 30#include <sys/types.h>
31#include <stdlib.h> 31#include <stdlib.h>
32 32
33static int rand_deterministic;
33static u_int next = 1; 34static u_int next = 1;
34 35
35int 36int
@@ -47,6 +48,8 @@ __warn_references(rand_r,
47int 48int
48rand(void) 49rand(void)
49{ 50{
51 if (rand_deterministic)
52 return (arc4random() % ((u_int)RAND_MAX + 1));
50 return (rand_r(&next)); 53 return (rand_r(&next));
51} 54}
52 55
@@ -58,10 +61,12 @@ __warn_references(rand,
58void 61void
59srand(u_int seed) 62srand(u_int seed)
60{ 63{
61 next = seed; 64 rand_deterministic = 0;
62} 65}
63 66
64#if defined(APIWARN) 67void
65__warn_references(srand, 68srand_deterministic(u_int seed)
66 "warning: srand() seed choices are invariably poor"); 69{
67#endif 70 rand_deterministic = 1;
71 next = seed;
72}