Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

urandom.c 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <openssl/rand.h>
  15. #if !defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <openssl/thread.h>
  22. #include <openssl/mem.h>
  23. #include "internal.h"
  24. #include "../internal.h"
  25. /* This file implements a PRNG by reading from /dev/urandom, optionally with a
  26. * buffer, which is unsafe across |fork|. */
  27. #define BUF_SIZE 4096
  28. /* rand_buffer contains unused, random bytes, some of which may have been
  29. * consumed already. */
  30. struct rand_buffer {
  31. size_t used;
  32. uint8_t rand[BUF_SIZE];
  33. };
  34. /* requested_lock is used to protect the |*_requested| variables. */
  35. static struct CRYPTO_STATIC_MUTEX requested_lock = CRYPTO_STATIC_MUTEX_INIT;
  36. /* urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
  37. * |requested_lock|. */
  38. static int urandom_fd_requested = -2;
  39. /* urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. */
  40. static int urandom_fd = -2;
  41. /* urandom_buffering_requested is set by |RAND_enable_fork_unsafe_buffering|.
  42. * It's protected by |requested_lock|. */
  43. static int urandom_buffering_requested = 0;
  44. /* urandom_buffering controls whether buffering is enabled (1) or not (0). This
  45. * is protected by |once|. */
  46. static int urandom_buffering = 0;
  47. static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
  48. /* init_once initializes the state of this module to values previously
  49. * requested. This is the only function that modifies |urandom_fd| and
  50. * |urandom_buffering|, whose values may be read safely after calling the
  51. * once. */
  52. static void init_once(void) {
  53. CRYPTO_STATIC_MUTEX_lock_read(&requested_lock);
  54. urandom_buffering = urandom_buffering_requested;
  55. int fd = urandom_fd_requested;
  56. CRYPTO_STATIC_MUTEX_unlock_read(&requested_lock);
  57. if (fd == -2) {
  58. do {
  59. fd = open("/dev/urandom", O_RDONLY);
  60. } while (fd == -1 && errno == EINTR);
  61. }
  62. if (fd < 0) {
  63. abort();
  64. }
  65. int flags = fcntl(fd, F_GETFD);
  66. if (flags == -1) {
  67. /* Native Client doesn't implement |fcntl|. */
  68. if (errno != ENOSYS) {
  69. abort();
  70. }
  71. } else {
  72. flags |= FD_CLOEXEC;
  73. if (fcntl(fd, F_SETFD, flags) == -1) {
  74. abort();
  75. }
  76. }
  77. urandom_fd = fd;
  78. }
  79. void RAND_set_urandom_fd(int fd) {
  80. fd = dup(fd);
  81. if (fd < 0) {
  82. abort();
  83. }
  84. CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
  85. urandom_fd_requested = fd;
  86. CRYPTO_STATIC_MUTEX_unlock_write(&requested_lock);
  87. CRYPTO_once(&once, init_once);
  88. if (urandom_fd != fd) {
  89. abort(); // Already initialized.
  90. }
  91. }
  92. void RAND_enable_fork_unsafe_buffering(int fd) {
  93. if (fd >= 0) {
  94. fd = dup(fd);
  95. if (fd < 0) {
  96. abort();
  97. }
  98. } else {
  99. fd = -2;
  100. }
  101. CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
  102. urandom_buffering_requested = 1;
  103. urandom_fd_requested = fd;
  104. CRYPTO_STATIC_MUTEX_unlock_write(&requested_lock);
  105. CRYPTO_once(&once, init_once);
  106. if (urandom_buffering != 1 || (fd >= 0 && urandom_fd != fd)) {
  107. abort(); // Already initialized.
  108. }
  109. }
  110. static struct rand_buffer *get_thread_local_buffer(void) {
  111. struct rand_buffer *buf =
  112. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF);
  113. if (buf != NULL) {
  114. return buf;
  115. }
  116. buf = OPENSSL_malloc(sizeof(struct rand_buffer));
  117. if (buf == NULL) {
  118. return NULL;
  119. }
  120. buf->used = BUF_SIZE; /* To trigger a |read_full| on first use. */
  121. if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF, buf,
  122. OPENSSL_free)) {
  123. OPENSSL_free(buf);
  124. return NULL;
  125. }
  126. return buf;
  127. }
  128. /* read_full reads exactly |len| bytes from |fd| into |out| and returns 1. In
  129. * the case of an error it returns 0. */
  130. static char read_full(int fd, uint8_t *out, size_t len) {
  131. ssize_t r;
  132. while (len > 0) {
  133. do {
  134. r = read(fd, out, len);
  135. } while (r == -1 && errno == EINTR);
  136. if (r <= 0) {
  137. return 0;
  138. }
  139. out += r;
  140. len -= r;
  141. }
  142. return 1;
  143. }
  144. /* read_from_buffer reads |requested| random bytes from the buffer into |out|,
  145. * refilling it if necessary to satisfy the request. */
  146. static void read_from_buffer(struct rand_buffer *buf,
  147. uint8_t *out, size_t requested) {
  148. size_t remaining = BUF_SIZE - buf->used;
  149. while (requested > remaining) {
  150. memcpy(out, &buf->rand[buf->used], remaining);
  151. buf->used += remaining;
  152. out += remaining;
  153. requested -= remaining;
  154. if (!read_full(urandom_fd, buf->rand, BUF_SIZE)) {
  155. abort();
  156. return;
  157. }
  158. buf->used = 0;
  159. remaining = BUF_SIZE;
  160. }
  161. memcpy(out, &buf->rand[buf->used], requested);
  162. buf->used += requested;
  163. }
  164. /* CRYPTO_sysrand puts |requested| random bytes into |out|. */
  165. void CRYPTO_sysrand(uint8_t *out, size_t requested) {
  166. if (requested == 0) {
  167. return;
  168. }
  169. CRYPTO_once(&once, init_once);
  170. if (urandom_buffering && requested < BUF_SIZE) {
  171. struct rand_buffer *buf = get_thread_local_buffer();
  172. if (buf != NULL) {
  173. read_from_buffer(buf, out, requested);
  174. return;
  175. }
  176. }
  177. if (!read_full(urandom_fd, out, requested)) {
  178. abort();
  179. }
  180. }
  181. #endif /* !OPENSSL_WINDOWS && !BORINGSSL_UNSAFE_FUZZER_MODE */