1 /* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #if !defined(_GNU_SOURCE)
16 #define _GNU_SOURCE // needed for syscall() on Linux.
17 #endif
18
19 #include <openssl/rand.h>
20
21 #include "internal.h"
22
23 #if defined(OPENSSL_RAND_URANDOM)
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #if defined(OPENSSL_LINUX)
33 #if defined(BORINGSSL_FIPS)
34 #include <linux/random.h>
35 #include <sys/ioctl.h>
36 #endif
37 #include <sys/syscall.h>
38
39 #if defined(OPENSSL_ANDROID)
40 #include <sys/system_properties.h>
41 #endif
42
43 #if !defined(OPENSSL_ANDROID)
44 #define OPENSSL_HAS_GETAUXVAL
45 #endif
46 // glibc prior to 2.16 does not have getauxval and sys/auxv.h. Android has some
47 // host builds (i.e. not building for Android itself, so |OPENSSL_ANDROID| is
48 // unset) which are still using a 2.15 sysroot.
49 //
50 // TODO(davidben): Remove this once Android updates their sysroot.
51 #if defined(__GLIBC_PREREQ)
52 #if !__GLIBC_PREREQ(2, 16)
53 #undef OPENSSL_HAS_GETAUXVAL
54 #endif
55 #endif
56 #if defined(OPENSSL_HAS_GETAUXVAL)
57 #include <sys/auxv.h>
58 #endif
59 #endif // OPENSSL_LINUX
60
61 #include <openssl/thread.h>
62 #include <openssl/mem.h>
63
64 #include "getrandom_fillin.h"
65 #include "../delocate.h"
66 #include "../../internal.h"
67
68
69 #if defined(USE_NR_getrandom)
70
71 #if defined(OPENSSL_MSAN)
72 void __msan_unpoison(void *, size_t);
73 #endif
74
boringssl_getrandom(void * buf,size_t buf_len,unsigned flags)75 static ssize_t boringssl_getrandom(void *buf, size_t buf_len, unsigned flags) {
76 ssize_t ret;
77 do {
78 ret = syscall(__NR_getrandom, buf, buf_len, flags);
79 } while (ret == -1 && errno == EINTR);
80
81 #if defined(OPENSSL_MSAN)
82 if (ret > 0) {
83 // MSAN doesn't recognise |syscall| and thus doesn't notice that we have
84 // initialised the output buffer.
85 __msan_unpoison(buf, ret);
86 }
87 #endif // OPENSSL_MSAN
88
89 return ret;
90 }
91
92 #endif // USE_NR_getrandom
93
94 // kHaveGetrandom in |urandom_fd| signals that |getrandom| or |getentropy| is
95 // available and should be used instead.
96 static const int kHaveGetrandom = -3;
97
98 // urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|.
DEFINE_BSS_GET(int,urandom_fd)99 DEFINE_BSS_GET(int, urandom_fd)
100
101 #if defined(USE_NR_getrandom)
102
103 // getrandom_ready is one if |getrandom| had been initialized by the time
104 // |init_once| was called and zero otherwise.
105 DEFINE_BSS_GET(int, getrandom_ready)
106
107 // extra_getrandom_flags_for_seed contains a value that is ORed into the flags
108 // for getrandom() when reading entropy for a seed.
109 DEFINE_BSS_GET(int, extra_getrandom_flags_for_seed)
110
111 // On Android, check a system property to decide whether to set
112 // |extra_getrandom_flags_for_seed| otherwise they will default to zero. If
113 // ro.oem_boringcrypto_hwrand is true then |extra_getrandom_flags_for_seed| will
114 // be set to GRND_RANDOM, causing all random data to be drawn from the same
115 // source as /dev/random.
116 static void maybe_set_extra_getrandom_flags(void) {
117 #if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID)
118 char value[PROP_VALUE_MAX + 1];
119 int length = __system_property_get("ro.boringcrypto.hwrand", value);
120 if (length < 0 || length > PROP_VALUE_MAX) {
121 return;
122 }
123
124 value[length] = 0;
125 if (OPENSSL_strcasecmp(value, "true") == 0) {
126 *extra_getrandom_flags_for_seed_bss_get() = GRND_RANDOM;
127 }
128 #endif
129 }
130
131 #endif // USE_NR_getrandom
132
DEFINE_STATIC_ONCE(rand_once)133 DEFINE_STATIC_ONCE(rand_once)
134
135 // init_once initializes the state of this module to values previously
136 // requested. This is the only function that modifies |urandom_fd|, which may be
137 // read safely after calling the once.
138 static void init_once(void) {
139 #if defined(USE_NR_getrandom)
140 int have_getrandom;
141 uint8_t dummy;
142 ssize_t getrandom_ret =
143 boringssl_getrandom(&dummy, sizeof(dummy), GRND_NONBLOCK);
144 if (getrandom_ret == 1) {
145 *getrandom_ready_bss_get() = 1;
146 have_getrandom = 1;
147 } else if (getrandom_ret == -1 && errno == EAGAIN) {
148 // We have getrandom, but the entropy pool has not been initialized yet.
149 have_getrandom = 1;
150 } else if (getrandom_ret == -1 && errno == ENOSYS) {
151 // Fallthrough to using /dev/urandom, below.
152 have_getrandom = 0;
153 } else {
154 // Other errors are fatal.
155 perror("getrandom");
156 abort();
157 }
158
159 if (have_getrandom) {
160 *urandom_fd_bss_get() = kHaveGetrandom;
161 maybe_set_extra_getrandom_flags();
162 return;
163 }
164 #endif // USE_NR_getrandom
165
166 // FIPS builds must support getrandom.
167 //
168 // Historically, only Android FIPS builds required getrandom, while Linux FIPS
169 // builds had a /dev/urandom fallback which used RNDGETENTCNT as a poor
170 // approximation for getrandom's blocking behavior. This is now removed, but
171 // avoid making assumptions on this removal until March 2023, in case it needs
172 // to be restored. This comment can be deleted after March 2023.
173 #if defined(BORINGSSL_FIPS)
174 perror("getrandom not found");
175 abort();
176 #endif
177
178 int fd;
179 do {
180 fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
181 } while (fd == -1 && errno == EINTR);
182
183 if (fd < 0) {
184 perror("failed to open /dev/urandom");
185 abort();
186 }
187
188 *urandom_fd_bss_get() = fd;
189 }
190
DEFINE_STATIC_ONCE(wait_for_entropy_once)191 DEFINE_STATIC_ONCE(wait_for_entropy_once)
192
193 static void wait_for_entropy(void) {
194 int fd = *urandom_fd_bss_get();
195 if (fd == kHaveGetrandom) {
196 // |getrandom| and |getentropy| support blocking in |fill_with_entropy|
197 // directly. For |getrandom|, we first probe with a non-blocking call to aid
198 // debugging.
199 #if defined(USE_NR_getrandom)
200 if (*getrandom_ready_bss_get()) {
201 // The entropy pool was already initialized in |init_once|.
202 return;
203 }
204
205 uint8_t dummy;
206 ssize_t getrandom_ret =
207 boringssl_getrandom(&dummy, sizeof(dummy), GRND_NONBLOCK);
208 if (getrandom_ret == -1 && errno == EAGAIN) {
209 // Attempt to get the path of the current process to aid in debugging when
210 // something blocks.
211 const char *current_process = "<unknown>";
212 #if defined(OPENSSL_HAS_GETAUXVAL)
213 const unsigned long getauxval_ret = getauxval(AT_EXECFN);
214 if (getauxval_ret != 0) {
215 current_process = (const char *)getauxval_ret;
216 }
217 #endif
218
219 fprintf(
220 stderr,
221 "%s: getrandom indicates that the entropy pool has not been "
222 "initialized. Rather than continue with poor entropy, this process "
223 "will block until entropy is available.\n",
224 current_process);
225
226 getrandom_ret =
227 boringssl_getrandom(&dummy, sizeof(dummy), 0 /* no flags */);
228 }
229
230 if (getrandom_ret != 1) {
231 perror("getrandom");
232 abort();
233 }
234 #endif // USE_NR_getrandom
235 return;
236 }
237 }
238
239 // fill_with_entropy writes |len| bytes of entropy into |out|. It returns one
240 // on success and zero on error. If |block| is one, this function will block
241 // until the entropy pool is initialized. Otherwise, this function may fail,
242 // setting |errno| to |EAGAIN| if the entropy pool has not yet been initialized.
243 // If |seed| is one, this function will OR in the value of
244 // |*extra_getrandom_flags_for_seed()| when using |getrandom|.
fill_with_entropy(uint8_t * out,size_t len,int block,int seed)245 static int fill_with_entropy(uint8_t *out, size_t len, int block, int seed) {
246 if (len == 0) {
247 return 1;
248 }
249
250 #if defined(USE_NR_getrandom) || defined(FREEBSD_GETRANDOM)
251 int getrandom_flags = 0;
252 if (!block) {
253 getrandom_flags |= GRND_NONBLOCK;
254 }
255 #endif
256
257 #if defined (USE_NR_getrandom)
258 if (seed) {
259 getrandom_flags |= *extra_getrandom_flags_for_seed_bss_get();
260 }
261 #endif
262
263 CRYPTO_init_sysrand();
264 if (block) {
265 CRYPTO_once(wait_for_entropy_once_bss_get(), wait_for_entropy);
266 }
267
268 // Clear |errno| so it has defined value if |read| or |getrandom|
269 // "successfully" returns zero.
270 errno = 0;
271 while (len > 0) {
272 ssize_t r;
273
274 if (*urandom_fd_bss_get() == kHaveGetrandom) {
275 #if defined(USE_NR_getrandom)
276 r = boringssl_getrandom(out, len, getrandom_flags);
277 #else // USE_NR_getrandom
278 fprintf(stderr, "urandom fd corrupt.\n");
279 abort();
280 #endif
281 } else {
282 do {
283 r = read(*urandom_fd_bss_get(), out, len);
284 } while (r == -1 && errno == EINTR);
285 }
286
287 if (r <= 0) {
288 return 0;
289 }
290 out += r;
291 len -= r;
292 }
293
294 return 1;
295 }
296
CRYPTO_init_sysrand(void)297 void CRYPTO_init_sysrand(void) {
298 CRYPTO_once(rand_once_bss_get(), init_once);
299 }
300
301 // CRYPTO_sysrand puts |requested| random bytes into |out|.
CRYPTO_sysrand(uint8_t * out,size_t requested)302 void CRYPTO_sysrand(uint8_t *out, size_t requested) {
303 if (!fill_with_entropy(out, requested, /*block=*/1, /*seed=*/0)) {
304 perror("entropy fill failed");
305 abort();
306 }
307 }
308
CRYPTO_sysrand_for_seed(uint8_t * out,size_t requested)309 void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
310 if (!fill_with_entropy(out, requested, /*block=*/1, /*seed=*/1)) {
311 perror("entropy fill failed");
312 abort();
313 }
314 }
315
CRYPTO_sysrand_if_available(uint8_t * out,size_t requested)316 int CRYPTO_sysrand_if_available(uint8_t *out, size_t requested) {
317 if (fill_with_entropy(out, requested, /*block=*/0, /*seed=*/0)) {
318 return 1;
319 } else if (errno == EAGAIN) {
320 OPENSSL_memset(out, 0, requested);
321 return 0;
322 } else {
323 perror("opportunistic entropy fill failed");
324 abort();
325 }
326 }
327
328 #endif // OPENSSL_RAND_URANDOM
329