Skip to content

Commit

Permalink
Use glibc's getrandom() instead of syscall when glibc > 2.15.
Browse files Browse the repository at this point in the history
Fixes Mbed-TLS#3432

Signed-off-by: okhowang(王沛文) <[email protected]>
  • Loading branch information
okhowang committed Sep 25, 2020
1 parent 7829748 commit e1d1719
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.d/getrandom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changes
Use glibc's getrandom() instead of syscall when glibc > 2.15.
20 changes: 18 additions & 2 deletions library/entropy_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <features.h>
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)
#define HAVE_SYS_RANDOM 1
#endif
#endif

#include "common.h"

Expand Down Expand Up @@ -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 <sys/random.h>
#include <errno.h>
#define HAVE_GETRANDOM
#elif (defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)
#include <unistd.h>
#include <sys/syscall.h>
#if defined(SYS_getrandom)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e1d1719

Please sign in to comment.