From a62f8ced7af51d1c4fd8abceb521bd24f362ab14 Mon Sep 17 00:00:00 2001 From: nia Date: Mon, 21 Oct 2019 20:33:48 +0100 Subject: [PATCH] netbsd: use KERN_ARND sysctl to get entropy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/libuv/libuv/pull/2528 Reviewed-By: Ben Noordhuis Reviewed-By: Saúl Ibarra Corretgé Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno --- docs/src/misc.rst | 1 + src/random.c | 2 ++ src/unix/netbsd.c | 23 +++++++++++++++++++++++ src/unix/random-devurandom.c | 8 ++++---- src/unix/random-sysctl-linux.c | 1 - 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/src/misc.rst b/docs/src/misc.rst index 8e167e3e..15d01633 100644 --- a/docs/src/misc.rst +++ b/docs/src/misc.rst @@ -679,6 +679,7 @@ API :man:`sysctl(2)`. - FreeBSD: `getrandom(2) _`, or `/dev/urandom` after reading from `/dev/random` once. + - NetBSD: `KERN_ARND` `sysctl(3) _` - macOS, OpenBSD: `getentropy(2) _` if available, or `/dev/urandom` after reading from `/dev/random` once. - AIX: `/dev/random`. diff --git a/src/random.c b/src/random.c index 8c4fe320..491bf703 100644 --- a/src/random.c +++ b/src/random.c @@ -40,6 +40,8 @@ static int uv__random(void* buf, size_t buflen) { rc = uv__random_getentropy(buf, buflen); if (rc == UV_ENOSYS) rc = uv__random_devurandom(buf, buflen); +#elif defined(__NetBSD__) + rc = uv__random_sysctl(buf, buflen); #elif defined(__FreeBSD__) || defined(__linux__) rc = uv__random_getrandom(buf, buflen); if (rc == UV_ENOSYS) diff --git a/src/unix/netbsd.c b/src/unix/netbsd.c index cfe2c6a4..690bd79e 100644 --- a/src/unix/netbsd.c +++ b/src/unix/netbsd.c @@ -234,3 +234,26 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { uv__free(cp_times); return 0; } + +int uv__random_sysctl(void* buf, size_t len) { + static int name[] = {CTL_KERN, KERN_ARND}; + size_t count, req; + unsigned char* p; + + p = buf; + while (len) { + req = len < 32 ? len : 32; + count = req; + + if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1) + return UV__ERR(errno); + + if (count != req) + return UV_EIO; /* Can't happen. */ + + p += count; + len -= count; + } + + return 0; +} diff --git a/src/unix/random-devurandom.c b/src/unix/random-devurandom.c index bfc40d20..9aa762e3 100644 --- a/src/unix/random-devurandom.c +++ b/src/unix/random-devurandom.c @@ -74,10 +74,10 @@ int uv__random_readpath(const char* path, void* buf, size_t buflen) { static void uv__random_devurandom_init(void) { char c; - /* Linux's and NetBSD's random(4) man page suggests applications should read - * at least once from /dev/random before switching to /dev/urandom in order - * to seed the system RNG. Reads from /dev/random can of course block - * indefinitely until entropy is available but that's the point. + /* Linux's random(4) man page suggests applications should read at least + * once from /dev/random before switching to /dev/urandom in order to seed + * the system RNG. Reads from /dev/random can of course block indefinitely + * until entropy is available but that's the point. */ status = uv__random_readpath("/dev/random", &c, 1); } diff --git a/src/unix/random-sysctl-linux.c b/src/unix/random-sysctl-linux.c index fb182ded..66ba8d74 100644 --- a/src/unix/random-sysctl-linux.c +++ b/src/unix/random-sysctl-linux.c @@ -40,7 +40,6 @@ struct uv__sysctl_args { }; -/* TODO(bnoordhuis) Use {CTL_KERN, KERN_ARND} on FreeBSD (and NetBSD?) */ int uv__random_sysctl(void* buf, size_t buflen) { static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/}; struct uv__sysctl_args args;