From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 4 Mar 2019 04:26:04 -0500 Subject: [PATCH] use blocking getrandom and avoid urandom fallback Signed-off-by: anupritaisno1 --- libc/bionic/getentropy.cpp | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/libc/bionic/getentropy.cpp b/libc/bionic/getentropy.cpp index 9c93e713b..c9438ad2b 100644 --- a/libc/bionic/getentropy.cpp +++ b/libc/bionic/getentropy.cpp @@ -33,22 +33,6 @@ #include "private/ScopedFd.h" -static int getentropy_urandom(void* buffer, size_t buffer_size, int saved_errno) { - ScopedFd fd(TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_NOFOLLOW | O_CLOEXEC, 0))); - if (fd.get() == -1) return -1; - - size_t collected = 0; - while (collected < buffer_size) { - ssize_t count = TEMP_FAILURE_RETRY(read(fd.get(), static_cast(buffer) + collected, - buffer_size - collected)); - if (count == -1) return -1; - collected += count; - } - - errno = saved_errno; - return 0; -} - int getentropy(void* buffer, size_t buffer_size) { if (buffer_size > 256) { errno = EIO; @@ -59,16 +43,10 @@ int getentropy(void* buffer, size_t buffer_size) { size_t collected = 0; while (collected < buffer_size) { - long count = TEMP_FAILURE_RETRY(getrandom(static_cast(buffer) + collected, - buffer_size - collected, GRND_NONBLOCK)); + long count = TEMP_FAILURE_RETRY( + getrandom(static_cast(buffer) + collected, buffer_size - collected, 0)); if (count == -1) { - // EAGAIN: there isn't enough entropy right now. - // ENOSYS/EINVAL: getrandom(2) or GRND_NONBLOCK isn't supported. - // EFAULT: `buffer` is invalid. - // Try /dev/urandom regardless because it can't hurt, - // and we don't need to optimize the EFAULT case. - // See http://b/33059407 and http://b/67015565. - return getentropy_urandom(buffer, buffer_size, saved_errno); + return -1; } collected += count; }