| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/rand.h> |
| 16 |
| 17 | #include <stdio.h> |
| 18 |
| 19 | #include <gtest/gtest.h> |
| 20 |
| 21 | #include <openssl/span.h> |
| 22 |
| 23 | #include "../bcm_support.h" |
| 24 | #include "../fipsmodule/rand/internal.h" |
| 25 | #include "../test/abi_test.h" |
| 26 | #include "../test/test_util.h" |
| 27 |
| 28 | #if defined(OPENSSL_THREADS) |
| 29 | #include <array> |
| 30 | #include <thread> |
| 31 | #include <vector> |
| 32 | #endif |
| 33 |
| 34 | #if !defined(OPENSSL_WINDOWS) |
| 35 | #include <errno.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 |
| 41 |
| 42 | // These tests are, strictly speaking, flaky, but we use large enough buffers |
| 43 | // that the probability of failing when we should pass is negligible. |
| 44 |
| 45 | TEST(RandTest, NotObviouslyBroken) { |
| 46 | static const uint8_t kZeros[256] = {0}; |
| 47 |
| 48 | uint8_t buf1[256], buf2[256]; |
| 49 | RAND_bytes(buf1, sizeof(buf1)); |
| 50 | RAND_bytes(buf2, sizeof(buf2)); |
| 51 |
| 52 | EXPECT_NE(Bytes(buf1), Bytes(buf2)); |
| 53 | EXPECT_NE(Bytes(buf1), Bytes(kZeros)); |
| 54 | EXPECT_NE(Bytes(buf2), Bytes(kZeros)); |
| 55 | } |
| 56 |
| 57 | #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_IOS) && \ |
| 58 | !defined(OPENSSL_FUCHSIA) && \ |
| 59 | !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) |
| 60 | static bool ForkAndRand(bssl::Span<uint8_t> out, bool fork_unsafe_buffering) { |
| 61 | int pipefds[2]; |
| 62 | if (pipe(pipefds) < 0) { |
| 63 | perror("pipe"); |
| 64 | return false; |
| 65 | } |
| 66 |
| 67 | // This is a multi-threaded process, but GTest does not run tests concurrently |
| 68 | // and there currently are no threads, so this should be safe. |
| 69 | pid_t child = fork(); |
| 70 | if (child < 0) { |
| 71 | perror("fork"); |
| 72 | close(pipefds[0]); |
| 73 | close(pipefds[1]); |
| 74 | return false; |
| 75 | } |
| 76 |
| 77 | if (child == 0) { |
| 78 | // This is the child. Generate entropy and write it to the parent. |
| 79 | close(pipefds[0]); |
| 80 | if (fork_unsafe_buffering) { |
| 81 | RAND_enable_fork_unsafe_buffering(-1); |
| 82 | } |
| 83 | RAND_bytes(out.data(), out.size()); |
| 84 | while (!out.empty()) { |
| 85 | ssize_t ret = write(pipefds[1], out.data(), out.size()); |
| 86 | if (ret < 0) { |
| 87 | if (errno == EINTR) { |
| 88 | continue; |
| 89 | } |
| 90 | perror("write"); |
| 91 | _exit(1); |
| 92 | } |
| 93 | out = out.subspan(static_cast<size_t>(ret)); |
| 94 | } |
| 95 | _exit(0); |
| 96 | } |
| 97 |
| 98 | // This is the parent. Read the entropy from the child. |
| 99 | close(pipefds[1]); |
| 100 | while (!out.empty()) { |
| 101 | ssize_t ret = read(pipefds[0], out.data(), out.size()); |
| 102 | if (ret <= 0) { |
| 103 | if (ret == 0) { |
| 104 | fprintf(stderr, "Unexpected EOF from child.\n"); |
| 105 | } else { |
| 106 | if (errno == EINTR) { |
| 107 | continue; |
| 108 | } |
| 109 | perror("read"); |
| 110 | } |
| 111 | close(pipefds[0]); |
| 112 | return false; |
| 113 | } |
| 114 | out = out.subspan(static_cast<size_t>(ret)); |
| 115 | } |
| 116 | close(pipefds[0]); |
| 117 |
| 118 | // Wait for the child to exit. |
| 119 | int status; |
| 120 | if (waitpid(child, &status, 0) < 0) { |
| 121 | perror("waitpid"); |
| 122 | return false; |
| 123 | } |
| 124 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 125 | fprintf(stderr, "Child did not exit cleanly.\n"); |
| 126 | return false; |
| 127 | } |
| 128 |
| 129 | return true; |
| 130 | } |
| 131 |
| 132 | TEST(RandTest, Fork) { |
| 133 | static const uint8_t kZeros[16] = {0}; |
| 134 |
| 135 | // Draw a little entropy to initialize any internal PRNG buffering. |
| 136 | uint8_t byte; |
| 137 | RAND_bytes(&byte, 1); |
| 138 |
| 139 | // Draw entropy in two child processes and the parent process. This test |
| 140 | // intentionally uses smaller buffers than the others, to minimize the chance |
| 141 | // of sneaking by with a large enough buffer that we've since reseeded from |
| 142 | // the OS. |
| 143 | // |
| 144 | // All child processes should have different PRNGs, including the ones that |
| 145 | // disavow fork-safety. Although they are produced by fork, they themselves do |
| 146 | // not fork after that call. |
| 147 | uint8_t bufs[5][16]; |
| 148 | ASSERT_TRUE(ForkAndRand(bufs[0], /*fork_unsafe_buffering=*/false)); |
| 149 | ASSERT_TRUE(ForkAndRand(bufs[1], /*fork_unsafe_buffering=*/false)); |
| 150 | ASSERT_TRUE(ForkAndRand(bufs[2], /*fork_unsafe_buffering=*/true)); |
| 151 | ASSERT_TRUE(ForkAndRand(bufs[3], /*fork_unsafe_buffering=*/true)); |
| 152 | RAND_bytes(bufs[4], sizeof(bufs[4])); |
| 153 |
| 154 | // All should be different and non-zero. |
| 155 | for (const auto &buf : bufs) { |
| 156 | EXPECT_NE(Bytes(buf), Bytes(kZeros)); |
| 157 | } |
| 158 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(bufs); i++) { |
| 159 | for (size_t j = 0; j < i; j++) { |
| 160 | EXPECT_NE(Bytes(bufs[i]), Bytes(bufs[j])) |
| 161 | << "buffers " << i << " and " << j << " matched"; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | #endif // !OPENSSL_WINDOWS && !OPENSSL_IOS && |
| 166 | // !OPENSSL_FUCHSIA && !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 167 |
| 168 | #if defined(OPENSSL_THREADS) |
| 169 | static void RunConcurrentRands(size_t num_threads) { |
| 170 | static const uint8_t kZeros[256] = {0}; |
| 171 |
| 172 | std::vector<std::array<uint8_t, 256>> bufs(num_threads); |
| 173 | std::vector<std::thread> threads(num_threads); |
| 174 |
| 175 | for (size_t i = 0; i < num_threads; i++) { |
| 176 | threads[i] = |
| 177 | std::thread([i, &bufs] { RAND_bytes(bufs[i].data(), bufs[i].size()); }); |
| 178 | } |
| 179 | for (size_t i = 0; i < num_threads; i++) { |
| 180 | threads[i].join(); |
| 181 | } |
| 182 |
| 183 | for (size_t i = 0; i < num_threads; i++) { |
| 184 | EXPECT_NE(Bytes(bufs[i]), Bytes(kZeros)); |
| 185 | for (size_t j = i + 1; j < num_threads; j++) { |
| 186 | EXPECT_NE(Bytes(bufs[i]), Bytes(bufs[j])); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 |
| 191 | // Test that threads may concurrently draw entropy without tripping TSan. |
| 192 | TEST(RandTest, Threads) { |
| 193 | constexpr size_t kFewerThreads = 10; |
| 194 | constexpr size_t kMoreThreads = 20; |
| 195 |
| 196 | // Draw entropy in parallel. |
| 197 | RunConcurrentRands(kFewerThreads); |
| 198 | // Draw entropy in parallel with higher concurrency than the previous maximum. |
| 199 | RunConcurrentRands(kMoreThreads); |
| 200 | // Draw entropy in parallel with lower concurrency than the previous maximum. |
| 201 | RunConcurrentRands(kFewerThreads); |
| 202 | } |
| 203 | #endif // OPENSSL_THREADS |
| 204 |
| 205 | #if defined(OPENSSL_X86_64) && defined(SUPPORTS_ABI_TEST) |
| 206 | TEST(RandTest, RdrandABI) { |
| 207 | if (!have_rdrand()) { |
| 208 | fprintf(stderr, "rdrand not supported. Skipping.\n"); |
| 209 | return; |
| 210 | } |
| 211 |
| 212 | uint8_t buf[32]; |
| 213 | CHECK_ABI_SEH(CRYPTO_rdrand, buf); |
| 214 | CHECK_ABI_SEH(CRYPTO_rdrand_multiple8_buf, nullptr, 0); |
| 215 | CHECK_ABI_SEH(CRYPTO_rdrand_multiple8_buf, buf, 8); |
| 216 | CHECK_ABI_SEH(CRYPTO_rdrand_multiple8_buf, buf, 16); |
| 217 | CHECK_ABI_SEH(CRYPTO_rdrand_multiple8_buf, buf, 24); |
| 218 | CHECK_ABI_SEH(CRYPTO_rdrand_multiple8_buf, buf, 32); |
| 219 | } |
| 220 | #endif // OPENSSL_X86_64 && SUPPORTS_ABI_TEST |
| 221 |