diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/config.h b/wasmtime-wasi/sandboxed-system-primitives/src/config.h index c44526290c..714e29d24f 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/src/config.h +++ b/wasmtime-wasi/sandboxed-system-primitives/src/config.h @@ -20,15 +20,15 @@ #define CONFIG_HAS_ARC4RANDOM_BUF 0 #endif -// On Linux, prefer to use getentropy, though it isn't available in +// On Linux, prefer to use getrandom, though it isn't available in // GLIBC before 2.25. #if defined(__linux__) && \ (!defined(__GLIBC__) || \ __GLIBC__ > 2 || \ (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)) -#define CONFIG_HAS_GETENTROPY 1 +#define CONFIG_HAS_GETRANDOM 1 #else -#define CONFIG_HAS_GETENTROPY 0 +#define CONFIG_HAS_GETRANDOM 0 #endif #if defined(__CloudABI__) diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/random.c b/wasmtime-wasi/sandboxed-system-primitives/src/random.c index 85c3acf700..9fc3e33be2 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/src/random.c +++ b/wasmtime-wasi/sandboxed-system-primitives/src/random.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include "random.h" @@ -25,10 +27,24 @@ void random_buf(void *buf, size_t len) { arc4random_buf(buf, len); } -#elif CONFIG_HAS_GETENTROPY +#elif CONFIG_HAS_GETRANDOM + +#include void random_buf(void *buf, size_t len) { - getentropy(buf, len); + for (;;) { + ssize_t x = getrandom(buf, len, 0); + if (x < 0) { + if (errno == EINTR) + continue; + fprintf(stderr, "getrandom failed: %s", strerror(errno)); + abort(); + } + if (x == len) + return; + buf = (void *)((unsigned char *)buf + x); + len -= x; + } } #else