Revert "[standalone] Use fewer syscalls to get entropy on Linux and Mac."
This reverts commit ea162825d3f0a366cb885e16fe5623051200deef.
Revert "[standalone] Fix Linux Crypto::GetRandomBytes on MSAN."
This reverts commit c3e5b7c268cbda93e6a171cb5217a7dbd49176c6.
Reason for revert: build failures on "Mac mac_ios_engine" Flutter bot:
```
../../../flutter/third_party/dart/runtime/bin/crypto_macos.cc:10:10: fatal error: 'sys/random.h' file not found
10 | #include <sys/random.h>
| ^~~~~~~~~~~~~~
1 error generated.
```
https://ci.chromium.org/ui/p/flutter/builders/try/Mac%20mac_ios_engine/50544/overview
TEST=ci
Change-Id: Ib0656cbf1268fc48f42097b09ca60c5bf2c1e2bd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452843
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
diff --git a/runtime/bin/crypto_linux.cc b/runtime/bin/crypto_linux.cc
index 16a0c86..cda1684 100644
--- a/runtime/bin/crypto_linux.cc
+++ b/runtime/bin/crypto_linux.cc
@@ -5,19 +5,17 @@
#include "platform/globals.h"
#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/syscall.h>
+#include <errno.h> // NOLINT
+#include <fcntl.h> // NOLINT
#include "bin/crypto.h"
#include "bin/fdutils.h"
-#include "platform/memory_sanitizer.h"
#include "platform/signal_blocker.h"
namespace dart {
namespace bin {
-static bool GetRandomFromDev(intptr_t count, uint8_t* buffer) {
+bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
ThreadSignalBlocker signal_blocker(SIGPROF);
intptr_t fd = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
open("/dev/urandom", O_RDONLY | O_CLOEXEC));
@@ -40,28 +38,6 @@
return true;
}
-bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
- intptr_t bytes_read = 0;
- do {
- ssize_t res;
- do {
- res = syscall(__NR_getrandom, buffer + bytes_read, count - bytes_read,
- /*flags=*/0);
- } while (res == -1 && errno == EINTR);
- if (res == -1) {
- if (errno == ENOSYS) {
- return GetRandomFromDev(count, buffer);
- }
- return false;
- }
- bytes_read += res;
- } while (bytes_read < count);
- // Not using the libc wrapper `getrandom`, which MSAN is missing an
- // interceptor for anyway.
- MSAN_UNPOISON(buffer, count);
- return true;
-}
-
} // namespace bin
} // namespace dart
diff --git a/runtime/bin/crypto_macos.cc b/runtime/bin/crypto_macos.cc
index c63f03a..24dfe9b 100644
--- a/runtime/bin/crypto_macos.cc
+++ b/runtime/bin/crypto_macos.cc
@@ -5,9 +5,8 @@
#include "platform/globals.h"
#if defined(DART_HOST_OS_MACOS)
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/random.h>
+#include <errno.h> // NOLINT
+#include <fcntl.h> // NOLINT
#include "bin/crypto.h"
#include "bin/fdutils.h"
@@ -17,18 +16,23 @@
namespace bin {
bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
+ intptr_t fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC));
+ if (fd < 0) {
+ return false;
+ }
intptr_t bytes_read = 0;
do {
- intptr_t chunk_size = count - bytes_read;
- if (chunk_size > 256) {
- chunk_size = 256;
- }
- int res = getentropy(buffer + bytes_read, chunk_size);
+ int res =
+ TEMP_FAILURE_RETRY(read(fd, buffer + bytes_read, count - bytes_read));
if (res < 0) {
+ int err = errno;
+ close(fd);
+ errno = err;
return false;
}
- bytes_read += chunk_size;
+ bytes_read += res;
} while (bytes_read < count);
+ close(fd);
return true;
}