From 9c46a60e6c3edc35d6369afdf5252a5c66aef5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?okhowang=28=E7=8E=8B=E6=B2=9B=E6=96=87=29?= Date: Thu, 3 Sep 2020 16:55:01 +0800 Subject: [PATCH] Use glibc's getrandom() instead of syscall when glibc > 2.25. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3432 Signed-off-by: okhowang(王沛文) --- ChangeLog.d/getrandom.txt | 2 ++ library/entropy_poll.c | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 ChangeLog.d/getrandom.txt diff --git a/ChangeLog.d/getrandom.txt b/ChangeLog.d/getrandom.txt new file mode 100644 index 000000000..87a3a6c13 --- /dev/null +++ b/ChangeLog.d/getrandom.txt @@ -0,0 +1,2 @@ +Changes + Use glibc's getrandom() instead of syscall when glibc > 2.25. diff --git a/library/entropy_poll.c b/library/entropy_poll.c index 4bf660e05..0f992f34c 100644 --- a/library/entropy_poll.c +++ b/library/entropy_poll.c @@ -18,9 +18,15 @@ */ #if defined(__linux__) +#if !defined(_GNU_SOURCE) /* Ensure that syscall() is available even when compiling with -std=c99 */ #define _GNU_SOURCE #endif +#include +#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25) +#define HAVE_SYS_RANDOM 1 +#endif +#endif #include "common.h" @@ -86,10 +92,16 @@ int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len /* * Test for Linux getrandom() support. - * Since there is no wrapper in the libc yet, use the generic syscall wrapper + * When the C library is GNU libc and its version is greater than 2.25, + * include sys/random.h to use getrandom(), + * otherwise use the generic use the generic syscall wrapper * available in GNU libc and compatible libc's (eg uClibc). */ -#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)) +#if HAVE_SYS_RANDOM +#include +#include +#define HAVE_GETRANDOM +#elif (defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__) #include #include #if defined(SYS_getrandom) @@ -155,7 +167,11 @@ int mbedtls_platform_entropy_poll( void *data, ((void) data); #if defined(HAVE_GETRANDOM) +#if HAVE_SYS_RANDOM + ret = getrandom(output, len, 0); +#else ret = getrandom_wrapper( output, len, 0 ); +#endif if( ret >= 0 ) { *olen = ret;