@@ -56,7 +56,7 @@ fn get_rng_fd() -> Result<libc::c_int, Error> {
56
56
57
57
// On Linux, /dev/urandom might return insecure values.
58
58
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
59
- wait_until_rng_ready ( ) ?;
59
+ crate :: imp :: wait_until_rng_ready ( ) ?;
60
60
61
61
let fd = open_readonly ( FILE_PATH ) ?;
62
62
// The fd always fits in a usize without conflicting with FD_UNINIT.
@@ -74,61 +74,6 @@ fn get_rng_fd() -> Result<libc::c_int, Error> {
74
74
}
75
75
}
76
76
77
- // Polls /dev/random to make sure it is ok to read from /dev/urandom.
78
- //
79
- // Polling avoids draining the estimated entropy from /dev/random;
80
- // short-lived processes reading even a single byte from /dev/random could
81
- // be problematic if they are being executed faster than entropy is being
82
- // collected.
83
- //
84
- // OTOH, reading a byte instead of polling is more compatible with
85
- // sandboxes that disallow `poll()` but which allow reading /dev/random,
86
- // e.g. sandboxes that assume that `poll()` is for network I/O. This way,
87
- // fewer applications will have to insert pre-sandbox-initialization logic.
88
- // Often (blocking) file I/O is not allowed in such early phases of an
89
- // application for performance and/or security reasons.
90
- //
91
- // It is hard to write a sandbox policy to support `libc::poll()` because
92
- // it may invoke the `poll`, `ppoll`, `ppoll_time64` (since Linux 5.1, with
93
- // newer versions of glibc), and/or (rarely, and probably only on ancient
94
- // systems) `select`. depending on the libc implementation (e.g. glibc vs
95
- // musl), libc version, potentially the kernel version at runtime, and/or
96
- // the target architecture.
97
- //
98
- // BoringSSL and libstd don't try to protect against insecure output from
99
- // `/dev/urandom'; they don't open `/dev/random` at all.
100
- //
101
- // OpenSSL uses `libc::select()` unless the `dev/random` file descriptor
102
- // is too large; if it is too large then it does what we do here.
103
- //
104
- // libsodium uses `libc::poll` similarly to this.
105
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
106
- fn wait_until_rng_ready ( ) -> Result < ( ) , Error > {
107
- let fd = open_readonly ( b"/dev/random\0 " ) ?;
108
- let mut pfd = libc:: pollfd {
109
- fd,
110
- events : libc:: POLLIN ,
111
- revents : 0 ,
112
- } ;
113
- let _guard = DropGuard ( || unsafe {
114
- libc:: close ( fd) ;
115
- } ) ;
116
-
117
- loop {
118
- // A negative timeout means an infinite timeout.
119
- let res = unsafe { libc:: poll ( & mut pfd, 1 , -1 ) } ;
120
- if res >= 0 {
121
- debug_assert_eq ! ( res, 1 ) ; // We only used one fd, and cannot timeout.
122
- return Ok ( ( ) ) ;
123
- }
124
- let err = crate :: util_libc:: last_os_error ( ) ;
125
- match err. raw_os_error ( ) {
126
- Some ( libc:: EINTR ) | Some ( libc:: EAGAIN ) => continue ,
127
- _ => return Err ( err) ,
128
- }
129
- }
130
- }
131
-
132
77
struct Mutex ( UnsafeCell < libc:: pthread_mutex_t > ) ;
133
78
134
79
impl Mutex {
0 commit comments