mirror of
https://github.com/cuberite/libevent.git
synced 2025-08-03 09:16:30 -04:00
arc4random: replace sysctl() with getrandom (on linux)
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: #890 Suggested-by: Pierce Lopez (cherry picked from commit 86f55b0420f864b518475f781ce7a3c619180b12)
This commit is contained in:
parent
45da7d9d44
commit
66ec78fddb
@ -364,6 +364,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)
|
||||
@ -557,9 +562,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)
|
||||
|
||||
|
30
arc4random.c
30
arc4random.c
@ -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>
|
||||
@ -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;
|
||||
@ -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. */
|
||||
@ -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
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
@ -248,6 +248,7 @@ AC_CHECK_HEADERS([ \
|
||||
sys/timerfd.h \
|
||||
sys/uio.h \
|
||||
sys/wait.h \
|
||||
sys/random.h \
|
||||
errno.h \
|
||||
])
|
||||
|
||||
@ -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,
|
||||
@ -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>]]
|
||||
)
|
||||
@ -389,6 +391,7 @@ AC_CHECK_FUNCS([ \
|
||||
usleep \
|
||||
vasprintf \
|
||||
getservbyname \
|
||||
getrandom \
|
||||
])
|
||||
AM_CONDITIONAL(STRLCPY_IMPL, [test x"$ac_cv_func_strlcpy" = xno])
|
||||
|
||||
|
@ -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
|
||||
@ -367,6 +364,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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user