Skip to content

Commit

Permalink
arc4random: replace sysctl() with getrandom (on linux)
Browse files Browse the repository at this point in the history
Since sysctl() is deprecated for a long-long time, according to
sysctl(2):

    Since Linux 2.6.24, uses of this system call result in warnings in the kernel log.

Fixes: libevent#890
Suggested-by: Pierce Lopez
  • Loading branch information
azat committed Sep 2, 2019
1 parent 13b8fc3 commit 86f55b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ if(EVENT__HAVE_SYS_SOCKET_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
endif()

CHECK_INCLUDE_FILE(sys/random.h EVENT__HAVE_SYS_RANDOM_H)
if(EVENT__HAVE_SYS_RANDOM_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/random.h)
endif()

CHECK_INCLUDE_FILE(netinet/in.h EVENT__HAVE_NETINET_IN_H)
if(EVENT__HAVE_NETINET_IN_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in.h)
Expand Down Expand Up @@ -559,9 +564,8 @@ CHECK_SYMBOL_EXISTS("__FUNCTION__" "" EVENT__HAVE___FUNCTION__)
CHECK_SYMBOL_EXISTS(TAILQ_FOREACH sys/queue.h EVENT__HAVE_TAILQFOREACH)
CHECK_CONST_EXISTS(CTL_KERN sys/sysctl.h EVENT__HAVE_DECL_CTL_KERN)
CHECK_CONST_EXISTS(KERN_ARND sys/sysctl.h EVENT__HAVE_DECL_KERN_ARND)
CHECK_CONST_EXISTS(KERN_RANDOM sys/sysctl.h EVENT__HAVE_DECL_KERN_RANDOM)
CHECK_CONST_EXISTS(RANDOM_UUID sys/sysctl.h EVENT__HAVE_DECL_RANDOM_UUID)
CHECK_SYMBOL_EXISTS(F_SETFD fcntl.h EVENT__HAVE_SETFD)
CHECK_FUNCTION_EXISTS_EX(getrandom EVENT__HAVE_GETRANDOM)

CHECK_TYPE_SIZE(fd_mask EVENT__HAVE_FD_MASK)

Expand Down
30 changes: 13 additions & 17 deletions arc4random.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
#ifdef EVENT__HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef EVENT__HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#endif
#include <limits.h>
#include <stdlib.h>
Expand Down Expand Up @@ -167,17 +170,11 @@ arc4_seed_win32(void)
}
#endif

#if defined(EVENT__HAVE_SYS_SYSCTL_H) && defined(EVENT__HAVE_SYSCTL)
#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_RANDOM && EVENT__HAVE_DECL_RANDOM_UUID
#define TRY_SEED_SYSCTL_LINUX
#if defined(EVENT__HAVE_GETRANDOM)
#define TRY_SEED_GETRANDOM
static int
arc4_seed_sysctl_linux(void)
arc4_seed_getrandom(void)
{
/* Based on code by William Ahern, this function tries to use the
* RANDOM_UUID sysctl to get entropy from the kernel. This can work
* even if /dev/urandom is inaccessible for some reason (e.g., we're
* running in a chroot). */
int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
unsigned char buf[ADD_ENTROPY];
size_t len, n;
unsigned i;
Expand All @@ -188,7 +185,7 @@ arc4_seed_sysctl_linux(void)
for (len = 0; len < sizeof(buf); len += n) {
n = sizeof(buf) - len;

if (0 != sysctl(mib, 3, &buf[len], &n, NULL, 0))
if (0 == getrandom(&buf[len], n, 0))
return -1;
}
/* make sure that the buffer actually got set. */
Expand All @@ -202,8 +199,9 @@ arc4_seed_sysctl_linux(void)
evutil_memclear_(buf, sizeof(buf));
return 0;
}
#endif
#endif /* EVENT__HAVE_GETRANDOM */

#if defined(EVENT__HAVE_SYS_SYSCTL_H) && defined(EVENT__HAVE_SYSCTL)
#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_ARND
#define TRY_SEED_SYSCTL_BSD
static int
Expand Down Expand Up @@ -342,6 +340,10 @@ arc4_seed(void)
if (0 == arc4_seed_win32())
ok = 1;
#endif
#ifdef TRY_SEED_GETRANDOM
if (0 == arc4_seed_getrandom())
ok = 1;
#endif
#ifdef TRY_SEED_URANDOM
if (0 == arc4_seed_urandom())
ok = 1;
Expand All @@ -351,12 +353,6 @@ arc4_seed(void)
0 == arc4_seed_proc_sys_kernel_random_uuid())
ok = 1;
#endif
#ifdef TRY_SEED_SYSCTL_LINUX
/* Apparently Linux is deprecating sysctl, and spewing warning
* messages when you try to use it. */
if (!ok && 0 == arc4_seed_sysctl_linux())
ok = 1;
#endif
#ifdef TRY_SEED_SYSCTL_BSD
if (0 == arc4_seed_sysctl_bsd())
ok = 1;
Expand Down
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ AC_CHECK_HEADERS([ \
sys/timerfd.h \
sys/uio.h \
sys/wait.h \
sys/random.h \
errno.h \
])

Expand All @@ -256,6 +257,7 @@ AC_CHECK_HEADERS(sys/sysctl.h, [], [], [
#include <sys/param.h>
#endif
])

if test "x$ac_cv_header_sys_queue_h" = "xyes"; then
AC_MSG_CHECKING(for TAILQ_FOREACH in sys/queue.h)
AC_EGREP_CPP(yes,
Expand Down Expand Up @@ -328,7 +330,7 @@ if test "x$ac_cv_header_sys_time_h" = "xyes"; then
fi

if test "x$ac_cv_header_sys_sysctl_h" = "xyes"; then
AC_CHECK_DECLS([CTL_KERN, KERN_RANDOM, RANDOM_UUID, KERN_ARND], [], [],
AC_CHECK_DECLS([CTL_KERN, KERN_ARND], [], [],
[[#include <sys/types.h>
#include <sys/sysctl.h>]]
)
Expand Down Expand Up @@ -390,6 +392,7 @@ AC_CHECK_FUNCS([ \
usleep \
vasprintf \
getservbyname \
getrandom \
])
AM_CONDITIONAL(STRLCPY_IMPL, [test x"$ac_cv_func_strlcpy" = xno])

Expand Down
10 changes: 5 additions & 5 deletions event-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@
/* Define to 1 if you have the declaration of `KERN_ARND'. */
#define EVENT__HAVE_DECL_KERN_ARND @EVENT__HAVE_DECL_KERN_ARND@

/* Define to 1 if you have the declaration of `KERN_RANDOM'. */
#define EVENT__HAVE_DECL_KERN_RANDOM @EVENT__HAVE_DECL_KERN_RANDOM@

/* Define to 1 if you have the declaration of `RANDOM_UUID'. */
#define EVENT__HAVE_DECL_RANDOM_UUID @EVENT__HAVE_DECL_RANDOM_UUID@
/* Define to 1 if you have `getrandom' function. */
#define EVENT__HAVE_GETRANDOM @EVENT__HAVE_GETRANDOM@

/* Define if /dev/poll is available */
#cmakedefine EVENT__HAVE_DEVPOLL 1
Expand Down Expand Up @@ -370,6 +367,9 @@
/* Define to 1 if you have the <sys/stat.h> header file. */
#cmakedefine EVENT__HAVE_SYS_STAT_H 1

/* Define to 1 if you have the <sys/random.h> header file. */
#cmakedefine EVENT__HAVE_SYS_RANDOM_H 1

/* Define to 1 if you have the <sys/sysctl.h> header file. */
#cmakedefine EVENT__HAVE_SYS_SYSCTL_H 1

Expand Down

0 comments on commit 86f55b0

Please sign in to comment.