Skip to content

Commit ea16282

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[standalone] Use fewer syscalls to get entropy on Linux and Mac.
TEST=ci Change-Id: Ib4737145a34b1d5e4aeab4377f918745abe8e1fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452220 Commit-Queue: Ryan Macnak <rmacnak@google.com> Reviewed-by: Slava Egorov <vegorov@google.com>
1 parent 1dc08ea commit ea16282

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

runtime/bin/crypto_linux.cc

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include "platform/globals.h"
66
#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
77

8-
#include <errno.h> // NOLINT
9-
#include <fcntl.h> // NOLINT
8+
#include <errno.h>
9+
#include <fcntl.h>
10+
#include <sys/syscall.h>
1011

1112
#include "bin/crypto.h"
1213
#include "bin/fdutils.h"
@@ -15,7 +16,7 @@
1516
namespace dart {
1617
namespace bin {
1718

18-
bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
19+
static bool GetRandomFromDev(intptr_t count, uint8_t* buffer) {
1920
ThreadSignalBlocker signal_blocker(SIGPROF);
2021
intptr_t fd = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
2122
open("/dev/urandom", O_RDONLY | O_CLOEXEC));
@@ -38,6 +39,25 @@ bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
3839
return true;
3940
}
4041

42+
bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
43+
intptr_t bytes_read = 0;
44+
do {
45+
ssize_t res;
46+
do {
47+
res = syscall(__NR_getrandom, buffer + bytes_read, count - bytes_read,
48+
/*flags=*/0);
49+
} while (res == -1 && errno == EINTR);
50+
if (res == -1) {
51+
if (errno == ENOSYS) {
52+
return GetRandomFromDev(count, buffer);
53+
}
54+
return false;
55+
}
56+
bytes_read += res;
57+
} while (bytes_read < count);
58+
return true;
59+
}
60+
4161
} // namespace bin
4262
} // namespace dart
4363

runtime/bin/crypto_macos.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include "platform/globals.h"
66
#if defined(DART_HOST_OS_MACOS)
77

8-
#include <errno.h> // NOLINT
9-
#include <fcntl.h> // NOLINT
8+
#include <errno.h>
9+
#include <fcntl.h>
10+
#include <sys/random.h>
1011

1112
#include "bin/crypto.h"
1213
#include "bin/fdutils.h"
@@ -16,23 +17,18 @@ namespace dart {
1617
namespace bin {
1718

1819
bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
19-
intptr_t fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC));
20-
if (fd < 0) {
21-
return false;
22-
}
2320
intptr_t bytes_read = 0;
2421
do {
25-
int res =
26-
TEMP_FAILURE_RETRY(read(fd, buffer + bytes_read, count - bytes_read));
22+
intptr_t chunk_size = count - bytes_read;
23+
if (chunk_size > 256) {
24+
chunk_size = 256;
25+
}
26+
int res = getentropy(buffer + bytes_read, chunk_size);
2727
if (res < 0) {
28-
int err = errno;
29-
close(fd);
30-
errno = err;
3128
return false;
3229
}
33-
bytes_read += res;
30+
bytes_read += chunk_size;
3431
} while (bytes_read < count);
35-
close(fd);
3632
return true;
3733
}
3834

0 commit comments

Comments
 (0)