urandom.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #if !defined(_GNU_SOURCE)
  15. #define _GNU_SOURCE // needed for syscall() on Linux.
  16. #endif
  17. #include <openssl/rand.h>
  18. #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \
  19. !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY)
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #if defined(OPENSSL_LINUX)
  27. #if defined(BORINGSSL_FIPS)
  28. #include <linux/random.h>
  29. #include <sys/ioctl.h>
  30. #endif
  31. #include <sys/syscall.h>
  32. #endif
  33. #include <openssl/thread.h>
  34. #include <openssl/mem.h>
  35. #include "internal.h"
  36. #include "../delocate.h"
  37. #include "../../internal.h"
  38. #if defined(OPENSSL_LINUX)
  39. #if defined(OPENSSL_X86_64)
  40. #define EXPECTED_NR_getrandom 318
  41. #elif defined(OPENSSL_X86)
  42. #define EXPECTED_NR_getrandom 355
  43. #elif defined(OPENSSL_AARCH64)
  44. #define EXPECTED_NR_getrandom 278
  45. #elif defined(OPENSSL_ARM)
  46. #define EXPECTED_NR_getrandom 384
  47. #elif defined(OPENSSL_PPC64LE)
  48. #define EXPECTED_NR_getrandom 359
  49. #endif
  50. #if defined(EXPECTED_NR_getrandom)
  51. #define USE_NR_getrandom
  52. #if defined(__NR_getrandom)
  53. #if __NR_getrandom != EXPECTED_NR_getrandom
  54. #error "system call number for getrandom is not the expected value"
  55. #endif
  56. #else // __NR_getrandom
  57. #define __NR_getrandom EXPECTED_NR_getrandom
  58. #endif // __NR_getrandom
  59. #endif // EXPECTED_NR_getrandom
  60. #if !defined(GRND_NONBLOCK)
  61. #define GRND_NONBLOCK 1
  62. #endif
  63. #endif // OPENSSL_LINUX
  64. // rand_lock is used to protect the |*_requested| variables.
  65. DEFINE_STATIC_MUTEX(rand_lock);
  66. // The following constants are magic values of |urandom_fd|.
  67. static const int kUnset = 0;
  68. static const int kHaveGetrandom = -3;
  69. // urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
  70. // |rand_lock|.
  71. DEFINE_BSS_GET(int, urandom_fd_requested);
  72. // urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|.
  73. DEFINE_BSS_GET(int, urandom_fd);
  74. DEFINE_STATIC_ONCE(rand_once);
  75. #if defined(USE_NR_getrandom) || defined(BORINGSSL_FIPS)
  76. // message writes |msg| to stderr. We use this because referencing |stderr|
  77. // with |fprintf| generates relocations, which is a problem inside the FIPS
  78. // module.
  79. static void message(const char *msg) {
  80. ssize_t r;
  81. do {
  82. r = write(2, msg, strlen(msg));
  83. } while (r == -1 && errno == EINTR);
  84. }
  85. #endif
  86. // init_once initializes the state of this module to values previously
  87. // requested. This is the only function that modifies |urandom_fd| and
  88. // |urandom_buffering|, whose values may be read safely after calling the
  89. // once.
  90. static void init_once(void) {
  91. CRYPTO_STATIC_MUTEX_lock_read(rand_lock_bss_get());
  92. int fd = *urandom_fd_requested_bss_get();
  93. CRYPTO_STATIC_MUTEX_unlock_read(rand_lock_bss_get());
  94. #if defined(USE_NR_getrandom)
  95. uint8_t dummy;
  96. long getrandom_ret =
  97. syscall(__NR_getrandom, &dummy, sizeof(dummy), GRND_NONBLOCK);
  98. if (getrandom_ret == 1) {
  99. *urandom_fd_bss_get() = kHaveGetrandom;
  100. return;
  101. } else if (getrandom_ret == -1 && errno == EAGAIN) {
  102. message(
  103. "getrandom indicates that the entropy pool has not been initialized. "
  104. "Rather than continue with poor entropy, this process will block until "
  105. "entropy is available.\n");
  106. do {
  107. getrandom_ret =
  108. syscall(__NR_getrandom, &dummy, sizeof(dummy), 0 /* no flags */);
  109. } while (getrandom_ret == -1 && errno == EINTR);
  110. if (getrandom_ret == 1) {
  111. *urandom_fd_bss_get() = kHaveGetrandom;
  112. return;
  113. }
  114. }
  115. #endif // USE_NR_getrandom
  116. if (fd == kUnset) {
  117. do {
  118. fd = open("/dev/urandom", O_RDONLY);
  119. } while (fd == -1 && errno == EINTR);
  120. }
  121. if (fd < 0) {
  122. abort();
  123. }
  124. assert(kUnset == 0);
  125. if (fd == kUnset) {
  126. // Because we want to keep |urandom_fd| in the BSS, we have to initialise
  127. // it to zero. But zero is a valid file descriptor too. Thus if open
  128. // returns zero for /dev/urandom, we dup it to get a non-zero number.
  129. fd = dup(fd);
  130. close(kUnset);
  131. if (fd <= 0) {
  132. abort();
  133. }
  134. }
  135. #if defined(BORINGSSL_FIPS)
  136. // In FIPS mode we ensure that the kernel has sufficient entropy before
  137. // continuing. This is automatically handled by getrandom, which requires
  138. // that the entropy pool has been initialised, but for urandom we have to
  139. // poll.
  140. for (;;) {
  141. int entropy_bits;
  142. if (ioctl(fd, RNDGETENTCNT, &entropy_bits)) {
  143. message(
  144. "RNDGETENTCNT on /dev/urandom failed. We cannot continue in this "
  145. "case when in FIPS mode.\n");
  146. abort();
  147. }
  148. static const int kBitsNeeded = 256;
  149. if (entropy_bits >= kBitsNeeded) {
  150. break;
  151. }
  152. usleep(250000);
  153. }
  154. #endif
  155. int flags = fcntl(fd, F_GETFD);
  156. if (flags == -1) {
  157. // Native Client doesn't implement |fcntl|.
  158. if (errno != ENOSYS) {
  159. abort();
  160. }
  161. } else {
  162. flags |= FD_CLOEXEC;
  163. if (fcntl(fd, F_SETFD, flags) == -1) {
  164. abort();
  165. }
  166. }
  167. *urandom_fd_bss_get() = fd;
  168. }
  169. void RAND_set_urandom_fd(int fd) {
  170. fd = dup(fd);
  171. if (fd < 0) {
  172. abort();
  173. }
  174. assert(kUnset == 0);
  175. if (fd == kUnset) {
  176. // Because we want to keep |urandom_fd| in the BSS, we have to initialise
  177. // it to zero. But zero is a valid file descriptor too. Thus if dup
  178. // returned zero we dup it again to get a non-zero number.
  179. fd = dup(fd);
  180. close(kUnset);
  181. if (fd <= 0) {
  182. abort();
  183. }
  184. }
  185. CRYPTO_STATIC_MUTEX_lock_write(rand_lock_bss_get());
  186. *urandom_fd_requested_bss_get() = fd;
  187. CRYPTO_STATIC_MUTEX_unlock_write(rand_lock_bss_get());
  188. CRYPTO_once(rand_once_bss_get(), init_once);
  189. if (*urandom_fd_bss_get() == kHaveGetrandom) {
  190. close(fd);
  191. } else if (*urandom_fd_bss_get() != fd) {
  192. abort(); // Already initialized.
  193. }
  194. }
  195. #if defined(USE_NR_getrandom) && defined(OPENSSL_MSAN)
  196. void __msan_unpoison(void *, size_t);
  197. #endif
  198. // fill_with_entropy writes |len| bytes of entropy into |out|. It returns one
  199. // on success and zero on error.
  200. static char fill_with_entropy(uint8_t *out, size_t len) {
  201. while (len > 0) {
  202. ssize_t r;
  203. if (*urandom_fd_bss_get() == kHaveGetrandom) {
  204. #if defined(USE_NR_getrandom)
  205. do {
  206. r = syscall(__NR_getrandom, out, len, 0 /* no flags */);
  207. } while (r == -1 && errno == EINTR);
  208. #if defined(OPENSSL_MSAN)
  209. if (r > 0) {
  210. // MSAN doesn't recognise |syscall| and thus doesn't notice that we
  211. // have initialised the output buffer.
  212. __msan_unpoison(out, r);
  213. }
  214. #endif // OPENSSL_MSAN
  215. #else // USE_NR_getrandom
  216. abort();
  217. #endif
  218. } else {
  219. do {
  220. r = read(*urandom_fd_bss_get(), out, len);
  221. } while (r == -1 && errno == EINTR);
  222. }
  223. if (r <= 0) {
  224. return 0;
  225. }
  226. out += r;
  227. len -= r;
  228. }
  229. return 1;
  230. }
  231. // CRYPTO_sysrand puts |requested| random bytes into |out|.
  232. void CRYPTO_sysrand(uint8_t *out, size_t requested) {
  233. if (requested == 0) {
  234. return;
  235. }
  236. CRYPTO_once(rand_once_bss_get(), init_once);
  237. if (!fill_with_entropy(out, requested)) {
  238. abort();
  239. }
  240. #if defined(BORINGSSL_FIPS_BREAK_CRNG)
  241. // This breaks the "continuous random number generator test" defined in FIPS
  242. // 140-2, section 4.9.2, and implemented in rand_get_seed().
  243. OPENSSL_memset(out, 0, requested);
  244. #endif
  245. }
  246. #endif /* !OPENSSL_WINDOWS && !defined(OPENSSL_FUCHSIA) && \
  247. !BORINGSSL_UNSAFE_DETERMINISTIC_MODE && !OPENSSL_TRUSTY */