| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#if !defined(_GNU_SOURCE) |
| 5 |
#define _GNU_SOURCE // needed for syscall() on Linux. |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <openssl/rand.h> |
| 9 |
|
| 10 |
#include "internal.h" |
| 11 |
|
| 12 |
#if defined(OPENSSL_RAND_URANDOM) |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <string.h> |
| 19 |
#include <time.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#if defined(OPENSSL_LINUX) |
| 23 |
#if defined(AWS_LC_URANDOM_NEEDS_U32) |
| 24 |
// On old Linux OS: unknown type name '__u32' when include <linux/random.h>. |
| 25 |
// If '__u32' is predefined, redefine will cause compiler error. |
| 26 |
typedef unsigned int __u32; |
| 27 |
#endif |
| 28 |
#include <linux/random.h> |
| 29 |
#include <sys/ioctl.h> |
| 30 |
#include <sys/param.h> |
| 31 |
#include <sys/syscall.h> |
| 32 |
|
| 33 |
#if defined(OPENSSL_ANDROID) |
| 34 |
#include <sys/system_properties.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#if !defined(OPENSSL_ANDROID) |
| 38 |
#define OPENSSL_HAS_GETAUXVAL |
| 39 |
#endif |
| 40 |
// glibc prior to 2.16 does not have getauxval and sys/auxv.h. Android has some |
| 41 |
// host builds (i.e. not building for Android itself, so |OPENSSL_ANDROID| is |
| 42 |
// unset) which are still using a 2.15 sysroot. |
| 43 |
// |
| 44 |
// TODO(davidben): Remove this once Android updates their sysroot. |
| 45 |
#if defined(__GLIBC_PREREQ) |
| 46 |
#if !__GLIBC_PREREQ(2, 16) |
| 47 |
#undef OPENSSL_HAS_GETAUXVAL |
| 48 |
#endif |
| 49 |
#endif |
| 50 |
#if defined(OPENSSL_HAS_GETAUXVAL) |
| 51 |
#include <sys/auxv.h> |
| 52 |
#endif |
| 53 |
#endif // OPENSSL_LINUX |
| 54 |
|
| 55 |
#include <openssl/thread.h> |
| 56 |
#include <openssl/mem.h> |
| 57 |
|
| 58 |
#include "getrandom_fillin.h" |
| 59 |
#include "../internal.h" |
| 60 |
|
| 61 |
#if defined(OPENSSL_MSAN) |
| 62 |
void __msan_unpoison(void *, size_t); |
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
#ifndef MIN |
| 67 |
#define AWSLC_MIN(X,Y) (((X) < (Y)) ? (X) : (Y)) |
| 68 |
#else |
| 69 |
#define AWSLC_MIN(X,Y) MIN(X,Y) |
| 70 |
#endif |
| 71 |
|
| 72 |
// One second in nanoseconds. |
| 73 |
#define ONE_SECOND INT64_C(1000000000) |
| 74 |
// 250 milliseconds in nanoseconds. |
| 75 |
#define MILLISECONDS_250 INT64_C(250000000) |
| 76 |
#define INITIAL_BACKOFF_DELAY 1 |
| 77 |
|
| 78 |
enum random_flavor_t { |
| 79 |
NOT_CHOSEN, |
| 80 |
USE_GETRANDOM, |
| 81 |
USE_DEV_URANDOM |
| 82 |
}; |
| 83 |
|
| 84 |
// random_flavor determines the randomness function used. This is either |
| 85 |
// getrandom or /dev/urandom. It's protected by |initialize_random_flavor_once|. |
| 86 |
// If both getrandom and /dev/urandom are available, we prefer getrandom. The |
| 87 |
// reasons are: |
| 88 |
// - getrandom has better blocking semantics than /dev/urandom; Former blocks |
| 89 |
// on initialization by default. |
| 90 |
// - getrandom doesn't require a file descriptor. |
| 91 |
// - Existence of vgetrandom can yield greater performance, see e.g. |
| 92 |
// https://www.phoronix.com/news/glibc-getrandom-vDSO-Merged. |
| 93 |
static enum random_flavor_t random_flavor = NOT_CHOSEN; |
| 94 |
|
| 95 |
enum random_state_t { |
| 96 |
STATE_NOT_READY, |
| 97 |
STATE_READY |
| 98 |
}; |
| 99 |
|
| 100 |
// random_flavor_state is |STATE_READY| if the entropy pool of |random_flavor| |
| 101 |
// has been (fully) initialized and |STATE_NOT_READY| otherwise. Initialized in |
| 102 |
// in this context means that the implementation of |random_flavor| has decided |
| 103 |
// that enough entropy/noise has been accumulated. It's protected by |
| 104 |
// |ensure_random_state_is_initialized_once|. |
| 105 |
// |
| 106 |
// If |random_flavor_state| has been initialized, we assume it stays |
| 107 |
// initialized forever. That is, we assume "entropy depletion" does not exist. |
| 108 |
// See e.g. https://www.nccgroup.com/us/research-blog/on-linux-s-random-number-generation/ |
| 109 |
// for an exposé on entropy depletion. |
| 110 |
// |
| 111 |
// If a consumer requests a non-blocking read of randomness from |
| 112 |
// |random_flavor|, the state |random_flavor_state| is not checked before |
| 113 |
// returning randomness. |
| 114 |
static enum random_state_t random_flavor_state = STATE_NOT_READY; |
| 115 |
|
| 116 |
// urandom_fd is a file descriptor to /dev/urandom. It's protected by |
| 117 |
// |initialize_random_flavor_once|. |
| 118 |
static int urandom_fd = -1; |
| 119 |
|
| 120 |
static CRYPTO_once_t initialize_random_flavor_once = CRYPTO_ONCE_INIT; |
| 121 |
static CRYPTO_once_t ensure_random_state_is_initialized_once = CRYPTO_ONCE_INIT; |
| 122 |
|
| 123 |
// getrandom_syscall_number_available returns 1 if the getrandom system call |
| 124 |
// number is defined, otherwise 0. |
| 125 |
static int getrandom_syscall_number_available(void) { |
| 126 |
#if defined(USE_NR_getrandom) |
| 127 |
return 1; |
| 128 |
#else |
| 129 |
return 0; |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
// do_backoff initiates exponential backoff. |backoff| holds the previous |
| 134 |
// backoff delay. Initial backoff delay is |INITIAL_BACKOFF_DELAY|. This |
| 135 |
// function will be called so rarely (if ever), that we keep it as a function |
| 136 |
// call and don't care about attempting to inline it. |
| 137 |
static void do_backoff(long *backoff) { |
| 138 |
|
| 139 |
// Exponential backoff. |
| 140 |
// |
| 141 |
// iteration delay |
| 142 |
// --------- ----------------- |
| 143 |
// 1 10 nsec |
| 144 |
// 2 100 nsec |
| 145 |
// 3 1,000 nsec |
| 146 |
// 4 10,000 nsec |
| 147 |
// 5 100,000 nsec |
| 148 |
// 6 1,000,000 nsec |
| 149 |
// 7 10,000,000 nsec |
| 150 |
// 8 99,999,999 nsec |
| 151 |
// 9 99,999,999 nsec |
| 152 |
// ... |
| 153 |
|
| 154 |
struct timespec sleep_time = {.tv_sec = 0, .tv_nsec = 0 }; |
| 155 |
|
| 156 |
// Cap backoff at 99,999,999 nsec, which is the maximum value the nanoseconds |
| 157 |
// field in |timespec| can hold. |
| 158 |
*backoff = AWSLC_MIN((*backoff) * 10, ONE_SECOND - 1); |
| 159 |
// |nanosleep| can mutate |sleep_time|. Hence, we use |backoff| for state. |
| 160 |
sleep_time.tv_nsec = *backoff; |
| 161 |
|
| 162 |
nanosleep(&sleep_time, &sleep_time); |
| 163 |
} |
| 164 |
|
| 165 |
static ssize_t wrapper_dev_urandom(void *buf, size_t buf_len, int block) { |
| 166 |
|
| 167 |
ssize_t ret = 0; |
| 168 |
size_t retry_counter = 0; |
| 169 |
long backoff = INITIAL_BACKOFF_DELAY; |
| 170 |
|
| 171 |
do { |
| 172 |
ret = read(urandom_fd, buf, buf_len); |
| 173 |
if ((ret == -1) && (errno != EINTR)) { |
| 174 |
if (!block || |
| 175 |
retry_counter >= MAX_BACKOFF_RETRIES) { |
| 176 |
break; |
| 177 |
} |
| 178 |
// We have observed extremely rare events in which a |read| on a |
| 179 |
// |urandom| fd failed with |errno| != |EINTR|. We regard this as an |
| 180 |
// intermittent error that is recoverable. Therefore, backoff to allow |
| 181 |
// recovery and to avoid creating a tight spinning loop. |
| 182 |
do_backoff(&backoff); |
| 183 |
retry_counter = retry_counter + 1; |
| 184 |
} |
| 185 |
} while (ret == -1); |
| 186 |
|
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
static ssize_t wrapper_getrandom(void *buf, size_t buf_len, int block) { |
| 191 |
|
| 192 |
ssize_t ret = -1; |
| 193 |
#if defined(USE_NR_getrandom) |
| 194 |
size_t retry_counter = 0; |
| 195 |
long backoff = INITIAL_BACKOFF_DELAY; |
| 196 |
|
| 197 |
do { |
| 198 |
ret = syscall(__NR_getrandom, buf, buf_len, block ? 0 : GRND_NONBLOCK); |
| 199 |
if ((ret == -1) && (errno != EINTR)) { |
| 200 |
// Don't block in non-block mode except if a signal handler interrupted |
| 201 |
// |getrandom|. |
| 202 |
if (!block || |
| 203 |
(retry_counter >= MAX_BACKOFF_RETRIES)) { |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
// We have observed extremely rare events in which a |read| on a |
| 208 |
// |urandom| fd failed with |errno| != |EINTR|. |getrandom| uses |urandom| |
| 209 |
// under the covers. Assuming transitivity, |getrandom| is therefore also |
| 210 |
// subject to the same rare error events. |
| 211 |
do_backoff(&backoff); |
| 212 |
retry_counter = retry_counter + 1; |
| 213 |
} |
| 214 |
} while (ret == -1); |
| 215 |
|
| 216 |
#if defined(OPENSSL_MSAN) |
| 217 |
if (ret > 0) { |
| 218 |
// MSAN doesn't recognise |syscall| and thus doesn't notice that we have |
| 219 |
// initialised the output buffer. |
| 220 |
__msan_unpoison(buf, ret); |
| 221 |
} |
| 222 |
#endif // OPENSSL_MSAN |
| 223 |
#endif // defined(USE_NR_getrandom) |
| 224 |
|
| 225 |
return ret; |
| 226 |
} |
| 227 |
|
| 228 |
// init_try_getrandom_once tests whether getrandom is supported. Returns 1 if |
| 229 |
// getrandom is supported, 0 otherwise. |
| 230 |
static int init_try_getrandom_once(void) { |
| 231 |
|
| 232 |
if (!getrandom_syscall_number_available()) { |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
uint8_t dummy = 0; |
| 237 |
ssize_t getrandom_ret = wrapper_getrandom(&dummy, sizeof(dummy), 0); |
| 238 |
|
| 239 |
if (getrandom_ret == 1) { |
| 240 |
// We have getrandom and entropy pool is also fully initialized. |
| 241 |
random_flavor = USE_GETRANDOM; |
| 242 |
random_flavor_state = STATE_READY; |
| 243 |
return 1; |
| 244 |
} else if (getrandom_ret == -1 && errno == EAGAIN) { |
| 245 |
// We have getrandom, but the entropy pool has not been initialized yet. |
| 246 |
random_flavor = USE_GETRANDOM; |
| 247 |
return 1; |
| 248 |
} else if (getrandom_ret == -1 && errno == ENOSYS) { |
| 249 |
// Kernel doesn't support getrandom. |
| 250 |
return 0; |
| 251 |
} else { |
| 252 |
// Other errors are fatal. |
| 253 |
perror("getrandom"); |
| 254 |
abort(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// init_try_urandom_once attempts to initialize /dev/urandom. Returns 1 if |
| 259 |
// successful, 0 otherwise. |
| 260 |
static int init_try_urandom_once(void) { |
| 261 |
|
| 262 |
int ret = 0; |
| 263 |
int fd = 0; |
| 264 |
|
| 265 |
do { |
| 266 |
fd = open("/dev/urandom", O_RDONLY); |
| 267 |
} while (fd == -1 && errno == EINTR); |
| 268 |
|
| 269 |
if (fd < 0) { |
| 270 |
perror("failed to open /dev/urandom"); |
| 271 |
goto out; |
| 272 |
} |
| 273 |
|
| 274 |
int flags = fcntl(fd, F_GETFD); |
| 275 |
if (flags == -1) { |
| 276 |
// Native Client doesn't implement |fcntl|. |
| 277 |
if (errno != ENOSYS) { |
| 278 |
perror("failed to get flags from urandom fd"); |
| 279 |
goto out; |
| 280 |
} |
| 281 |
} else { |
| 282 |
flags |= FD_CLOEXEC; |
| 283 |
if (fcntl(fd, F_SETFD, flags) == -1) { |
| 284 |
perror("failed to set FD_CLOEXEC on urandom fd"); |
| 285 |
goto out; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
random_flavor = USE_DEV_URANDOM; |
| 290 |
urandom_fd = fd; |
| 291 |
ret = 1; |
| 292 |
|
| 293 |
out: |
| 294 |
return ret; |
| 295 |
} |
| 296 |
|
| 297 |
// init_once initializes the state of this module to values previously |
| 298 |
// requested. Only function that mutates the chosen random function flavor. |
| 299 |
static void init_random_flavor_once(void) { |
| 300 |
|
| 301 |
// First determine if we can use getrandom. We prefer getrandom. |
| 302 |
if (init_try_getrandom_once()) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
// getrandom is not available. Fall-back to /dev/urandom. |
| 307 |
if (init_try_urandom_once()) { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
// This is a fatal error and there is no way to recover. |
| 312 |
abort(); |
| 313 |
} |
| 314 |
|
| 315 |
static void ensure_getrandom_is_initialized(void) { |
| 316 |
|
| 317 |
if (random_flavor_state == STATE_READY) { |
| 318 |
// The entropy pool was already initialized in |init_try_getrandom_once|. |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
uint8_t dummy = 0; |
| 323 |
ssize_t getrandom_ret = wrapper_getrandom(&dummy, sizeof(dummy), 0); |
| 324 |
|
| 325 |
if (getrandom_ret == -1 && errno == EAGAIN) { |
| 326 |
// Attempt to get the path of the current process to aid in debugging when |
| 327 |
// something blocks. |
| 328 |
const char *current_process = "<unknown>"; |
| 329 |
#if defined(OPENSSL_HAS_GETAUXVAL) |
| 330 |
const unsigned long getauxval_ret = getauxval(AT_EXECFN); |
| 331 |
if (getauxval_ret != 0) { |
| 332 |
current_process = (const char *)getauxval_ret; |
| 333 |
} |
| 334 |
#endif |
| 335 |
|
| 336 |
fprintf(stderr, |
| 337 |
"%s: getrandom indicates that the entropy pool has not been " |
| 338 |
"initialized. Rather than continue with poor entropy, this process " |
| 339 |
"will block until entropy is available.\n", |
| 340 |
current_process); |
| 341 |
|
| 342 |
getrandom_ret = wrapper_getrandom(&dummy, sizeof(dummy), 1); |
| 343 |
} |
| 344 |
|
| 345 |
if (getrandom_ret != 1) { |
| 346 |
perror("getrandom"); |
| 347 |
abort(); |
| 348 |
} |
| 349 |
|
| 350 |
random_flavor_state = STATE_READY; |
| 351 |
} |
| 352 |
|
| 353 |
static void ensure_dev_urandom_is_initialized(void) { |
| 354 |
|
| 355 |
// On platforms where urandom doesn't block at startup, we ensure that the |
| 356 |
// kernel has sufficient entropy before continuing. |
| 357 |
for (;;) { |
| 358 |
int entropy_bits = 0; |
| 359 |
if (ioctl(urandom_fd, RNDGETENTCNT, &entropy_bits)) { |
| 360 |
fprintf(stderr, |
| 361 |
"RNDGETENTCNT on /dev/urandom failed. We cannot determine if the kernel " |
| 362 |
"has enough entropy and must abort.\n"); |
| 363 |
abort(); |
| 364 |
} |
| 365 |
|
| 366 |
static const int kBitsNeeded = 256; |
| 367 |
if (entropy_bits >= kBitsNeeded) { |
| 368 |
break; |
| 369 |
} |
| 370 |
|
| 371 |
fprintf(stderr, |
| 372 |
"RNDGETENTCNT on /dev/urandom indicates that the entropy pool does not " |
| 373 |
"have enough entropy. Rather than continue with poor entropy, this " |
| 374 |
"process will block until entropy is available.\n"); |
| 375 |
|
| 376 |
struct timespec sleep_time = {.tv_sec = 0, .tv_nsec = MILLISECONDS_250 }; |
| 377 |
nanosleep(&sleep_time, &sleep_time); |
| 378 |
} |
| 379 |
|
| 380 |
random_flavor_state = STATE_READY; |
| 381 |
} |
| 382 |
|
| 383 |
// wait_for_entropy ensures the entropy pool has been initialized. If |
| 384 |
// initialization haven't happened yet, it will block the process. |
| 385 |
static void ensure_entropy_state_is_initd_once(void) { |
| 386 |
|
| 387 |
if (random_flavor == USE_GETRANDOM) { |
| 388 |
ensure_getrandom_is_initialized(); |
| 389 |
} else if (random_flavor == USE_DEV_URANDOM) { |
| 390 |
ensure_dev_urandom_is_initialized(); |
| 391 |
} else { |
| 392 |
// Indicates a state machine logical error which is fatal. |
| 393 |
abort(); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// fill_with_entropy writes |len| bytes of entropy into |out|. It returns one |
| 398 |
// on success and zero on error. If |block| is one, this function will block |
| 399 |
// until the entropy pool is initialized. Otherwise, this function may fail, |
| 400 |
// setting |errno| to |EAGAIN| if the entropy pool has not yet been initialized. |
| 401 |
static int fill_with_entropy(uint8_t *out, size_t len, int block, int seed) { |
| 402 |
|
| 403 |
if (len == 0) { |
| 404 |
return 1; |
| 405 |
} |
| 406 |
|
| 407 |
CRYPTO_once(&initialize_random_flavor_once, init_random_flavor_once); |
| 408 |
|
| 409 |
if (random_flavor == NOT_CHOSEN) { |
| 410 |
// This should never happen. It means there is a logical error somewhere. |
| 411 |
// Hard bail. |
| 412 |
abort(); |
| 413 |
} |
| 414 |
|
| 415 |
if (block) { |
| 416 |
// "Entropy depletion" is not a thing. So, it's enough to ensure the |
| 417 |
// entropy pool is initialized once. |
| 418 |
CRYPTO_once(&ensure_random_state_is_initialized_once, ensure_entropy_state_is_initd_once); |
| 419 |
|
| 420 |
if (random_flavor_state == STATE_NOT_READY) { |
| 421 |
// This should never happen. It means there is a logical error somewhere. |
| 422 |
// Hard bail. |
| 423 |
abort(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// Clear |errno| so it has defined value if |read| or |getrandom| |
| 428 |
// "successfully" returns zero. |
| 429 |
errno = 0; |
| 430 |
while (len > 0) { |
| 431 |
ssize_t ret = 0; |
| 432 |
|
| 433 |
if (random_flavor == USE_GETRANDOM) { |
| 434 |
ret = wrapper_getrandom(out, len, block); |
| 435 |
} else { |
| 436 |
ret = wrapper_dev_urandom(out, len, block); |
| 437 |
} |
| 438 |
|
| 439 |
if (ret <= 0) { |
| 440 |
return 0; |
| 441 |
} |
| 442 |
out += ret; |
| 443 |
len -= ret; |
| 444 |
} |
| 445 |
|
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
// CRYPTO_sysrand puts |requested| random bytes into |out|. |
| 450 |
void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 451 |
if (!fill_with_entropy(out, requested, /*block=*/1, /*seed=*/0)) { |
| 452 |
perror("entropy fill failed"); |
| 453 |
abort(); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
int CRYPTO_sysrand_if_available(uint8_t *out, size_t requested) { |
| 458 |
if (fill_with_entropy(out, requested, /*block=*/0, /*seed=*/0)) { |
| 459 |
return 1; |
| 460 |
} else if (errno == EAGAIN) { |
| 461 |
OPENSSL_memset(out, 0, requested); |
| 462 |
return 0; |
| 463 |
} else { |
| 464 |
perror("opportunistic entropy fill failed"); |
| 465 |
abort(); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
#endif // OPENSSL_RAND_URANDOM |