@@ -29,8 +29,8 @@ pub fn hashmap_random_keys() -> (u64, u64) {
29
29
mod imp {
30
30
use fs:: File ;
31
31
use io:: Read ;
32
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
32
33
use libc;
33
- use sys:: os:: errno;
34
34
35
35
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
36
36
fn getrandom ( buf : & mut [ u8 ] ) -> libc:: c_long {
@@ -40,71 +40,57 @@ mod imp {
40
40
}
41
41
42
42
#[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
43
- fn getrandom ( _buf : & mut [ u8 ] ) -> libc :: c_long { - 1 }
43
+ fn getrandom_fill_bytes ( _buf : & mut [ u8 ] ) -> bool { false }
44
44
45
+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
45
46
fn getrandom_fill_bytes ( v : & mut [ u8 ] ) -> bool {
47
+ use sync:: atomic:: { AtomicBool , Ordering } ;
48
+ use sys:: os:: errno;
49
+
50
+ static GETRANDOM_UNAVAILABLE : AtomicBool = AtomicBool :: new ( false ) ;
51
+ if GETRANDOM_UNAVAILABLE . load ( Ordering :: Relaxed ) {
52
+ return false ;
53
+ }
54
+
46
55
let mut read = 0 ;
47
56
while read < v. len ( ) {
48
57
let result = getrandom ( & mut v[ read..] ) ;
49
58
if result == -1 {
50
59
let err = errno ( ) as libc:: c_int ;
51
60
if err == libc:: EINTR {
52
61
continue ;
62
+ } else if err == libc:: ENOSYS {
63
+ GETRANDOM_UNAVAILABLE . store ( true , Ordering :: Relaxed ) ;
64
+ return false ;
53
65
} else if err == libc:: EAGAIN {
54
- return false
66
+ return false ;
55
67
} else {
56
68
panic ! ( "unexpected getrandom error: {}" , err) ;
57
69
}
58
70
} else {
59
71
read += result as usize ;
60
72
}
61
73
}
62
-
63
- return true
74
+ true
64
75
}
65
76
66
- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
67
- fn is_getrandom_available ( ) -> bool {
68
- use io;
69
- use sync:: atomic:: { AtomicBool , Ordering } ;
70
- use sync:: Once ;
71
-
72
- static CHECKER : Once = Once :: new ( ) ;
73
- static AVAILABLE : AtomicBool = AtomicBool :: new ( false ) ;
74
-
75
- CHECKER . call_once ( || {
76
- let mut buf: [ u8 ; 0 ] = [ ] ;
77
- let result = getrandom ( & mut buf) ;
78
- let available = if result == -1 {
79
- let err = io:: Error :: last_os_error ( ) . raw_os_error ( ) ;
80
- err != Some ( libc:: ENOSYS )
81
- } else {
82
- true
83
- } ;
84
- AVAILABLE . store ( available, Ordering :: Relaxed ) ;
85
- } ) ;
86
-
87
- AVAILABLE . load ( Ordering :: Relaxed )
88
- }
89
-
90
- #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
91
- fn is_getrandom_available ( ) -> bool { false }
92
-
93
77
pub fn fill_bytes ( v : & mut [ u8 ] ) {
94
78
// getrandom_fill_bytes here can fail if getrandom() returns EAGAIN,
95
79
// meaning it would have blocked because the non-blocking pool (urandom)
96
- // has not initialized in the kernel yet due to a lack of entropy the
80
+ // has not initialized in the kernel yet due to a lack of entropy. The
97
81
// fallback we do here is to avoid blocking applications which could
98
82
// depend on this call without ever knowing they do and don't have a
99
- // work around. The PRNG of /dev/urandom will still be used but not
100
- // over a completely full entropy pool
101
- if is_getrandom_available ( ) && getrandom_fill_bytes ( v) {
102
- return
83
+ // work around. The PRNG of /dev/urandom will still be used but over a
84
+ // possibly predictable entropy pool.
85
+ if getrandom_fill_bytes ( v) {
86
+ return ;
103
87
}
104
88
105
- let mut file = File :: open ( "/dev/urandom" )
106
- . expect ( "failed to open /dev/urandom" ) ;
107
- file. read_exact ( v) . expect ( "failed to read /dev/urandom" ) ;
89
+ // getrandom failed because it is permanently or temporarily (because
90
+ // of missing entropy) unavailable. Open /dev/urandom, read from it,
91
+ // and close it again.
92
+ let mut file = File :: open ( "/dev/urandom" ) . expect ( "failed to open /dev/urandom" ) ;
93
+ file. read_exact ( v) . expect ( "failed to read /dev/urandom" )
108
94
}
109
95
}
110
96
0 commit comments