diff options
author | deraadt <> | 2014-12-08 21:45:20 +0000 |
---|---|---|
committer | deraadt <> | 2014-12-08 21:45:20 +0000 |
commit | 1e2b0be5bee045db1b0abb1f87801004db563bb8 (patch) | |
tree | bf17a76201de02c2ce50358a383001a6b6c5cc64 /src/lib/libc/stdlib/rand.c | |
parent | 4b0b4fc5d98edef87738071b3784e13573327bab (diff) | |
download | openbsd-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.c | 15 |
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 | ||
33 | static int rand_deterministic; | ||
33 | static u_int next = 1; | 34 | static u_int next = 1; |
34 | 35 | ||
35 | int | 36 | int |
@@ -47,6 +48,8 @@ __warn_references(rand_r, | |||
47 | int | 48 | int |
48 | rand(void) | 49 | rand(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, | |||
58 | void | 61 | void |
59 | srand(u_int seed) | 62 | srand(u_int seed) |
60 | { | 63 | { |
61 | next = seed; | 64 | rand_deterministic = 0; |
62 | } | 65 | } |
63 | 66 | ||
64 | #if defined(APIWARN) | 67 | void |
65 | __warn_references(srand, | 68 | srand_deterministic(u_int seed) |
66 | "warning: srand() seed choices are invariably poor"); | 69 | { |
67 | #endif | 70 | rand_deterministic = 1; |
71 | next = seed; | ||
72 | } | ||