Skip to content

Commit 66ec78f

Browse files
committed
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: libevent#890 Suggested-by: Pierce Lopez (cherry picked from commit 86f55b0)
1 parent 45da7d9 commit 66ec78f

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ if(EVENT__HAVE_SYS_SOCKET_H)
364364
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
365365
endif()
366366

367+
CHECK_INCLUDE_FILE(sys/random.h EVENT__HAVE_SYS_RANDOM_H)
368+
if(EVENT__HAVE_SYS_RANDOM_H)
369+
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/random.h)
370+
endif()
371+
367372
CHECK_INCLUDE_FILE(netinet/in.h EVENT__HAVE_NETINET_IN_H)
368373
if(EVENT__HAVE_NETINET_IN_H)
369374
list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in.h)
@@ -557,9 +562,8 @@ CHECK_SYMBOL_EXISTS("__FUNCTION__" "" EVENT__HAVE___FUNCTION__)
557562
CHECK_SYMBOL_EXISTS(TAILQ_FOREACH sys/queue.h EVENT__HAVE_TAILQFOREACH)
558563
CHECK_CONST_EXISTS(CTL_KERN sys/sysctl.h EVENT__HAVE_DECL_CTL_KERN)
559564
CHECK_CONST_EXISTS(KERN_ARND sys/sysctl.h EVENT__HAVE_DECL_KERN_ARND)
560-
CHECK_CONST_EXISTS(KERN_RANDOM sys/sysctl.h EVENT__HAVE_DECL_KERN_RANDOM)
561-
CHECK_CONST_EXISTS(RANDOM_UUID sys/sysctl.h EVENT__HAVE_DECL_RANDOM_UUID)
562565
CHECK_SYMBOL_EXISTS(F_SETFD fcntl.h EVENT__HAVE_SETFD)
566+
CHECK_FUNCTION_EXISTS_EX(getrandom EVENT__HAVE_GETRANDOM)
563567

564568
CHECK_TYPE_SIZE(fd_mask EVENT__HAVE_FD_MASK)
565569

arc4random.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
#ifdef EVENT__HAVE_SYS_SYSCTL_H
6464
#include <sys/sysctl.h>
6565
#endif
66+
#ifdef EVENT__HAVE_SYS_RANDOM_H
67+
#include <sys/random.h>
68+
#endif
6669
#endif
6770
#include <limits.h>
6871
#include <stdlib.h>
@@ -167,17 +170,11 @@ arc4_seed_win32(void)
167170
}
168171
#endif
169172

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

191-
if (0 != sysctl(mib, 3, &buf[len], &n, NULL, 0))
188+
if (0 == getrandom(&buf[len], n, 0))
192189
return -1;
193190
}
194191
/* make sure that the buffer actually got set. */
@@ -202,8 +199,9 @@ arc4_seed_sysctl_linux(void)
202199
evutil_memclear_(buf, sizeof(buf));
203200
return 0;
204201
}
205-
#endif
202+
#endif /* EVENT__HAVE_GETRANDOM */
206203

204+
#if defined(EVENT__HAVE_SYS_SYSCTL_H) && defined(EVENT__HAVE_SYSCTL)
207205
#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_ARND
208206
#define TRY_SEED_SYSCTL_BSD
209207
static int
@@ -342,6 +340,10 @@ arc4_seed(void)
342340
if (0 == arc4_seed_win32())
343341
ok = 1;
344342
#endif
343+
#ifdef TRY_SEED_GETRANDOM
344+
if (0 == arc4_seed_getrandom())
345+
ok = 1;
346+
#endif
345347
#ifdef TRY_SEED_URANDOM
346348
if (0 == arc4_seed_urandom())
347349
ok = 1;
@@ -351,12 +353,6 @@ arc4_seed(void)
351353
0 == arc4_seed_proc_sys_kernel_random_uuid())
352354
ok = 1;
353355
#endif
354-
#ifdef TRY_SEED_SYSCTL_LINUX
355-
/* Apparently Linux is deprecating sysctl, and spewing warning
356-
* messages when you try to use it. */
357-
if (!ok && 0 == arc4_seed_sysctl_linux())
358-
ok = 1;
359-
#endif
360356
#ifdef TRY_SEED_SYSCTL_BSD
361357
if (0 == arc4_seed_sysctl_bsd())
362358
ok = 1;

configure.ac

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ AC_CHECK_HEADERS([ \
248248
sys/timerfd.h \
249249
sys/uio.h \
250250
sys/wait.h \
251+
sys/random.h \
251252
errno.h \
252253
])
253254

@@ -256,6 +257,7 @@ AC_CHECK_HEADERS(sys/sysctl.h, [], [], [
256257
#include <sys/param.h>
257258
#endif
258259
])
260+
259261
if test "x$ac_cv_header_sys_queue_h" = "xyes"; then
260262
AC_MSG_CHECKING(for TAILQ_FOREACH in sys/queue.h)
261263
AC_EGREP_CPP(yes,
@@ -328,7 +330,7 @@ if test "x$ac_cv_header_sys_time_h" = "xyes"; then
328330
fi
329331

330332
if test "x$ac_cv_header_sys_sysctl_h" = "xyes"; then
331-
AC_CHECK_DECLS([CTL_KERN, KERN_RANDOM, RANDOM_UUID, KERN_ARND], [], [],
333+
AC_CHECK_DECLS([CTL_KERN, KERN_ARND], [], [],
332334
[[#include <sys/types.h>
333335
#include <sys/sysctl.h>]]
334336
)
@@ -389,6 +391,7 @@ AC_CHECK_FUNCS([ \
389391
usleep \
390392
vasprintf \
391393
getservbyname \
394+
getrandom \
392395
])
393396
AM_CONDITIONAL(STRLCPY_IMPL, [test x"$ac_cv_func_strlcpy" = xno])
394397

event-config.h.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@
7575
/* Define to 1 if you have the declaration of `KERN_ARND'. */
7676
#define EVENT__HAVE_DECL_KERN_ARND @EVENT__HAVE_DECL_KERN_ARND@
7777

78-
/* Define to 1 if you have the declaration of `KERN_RANDOM'. */
79-
#define EVENT__HAVE_DECL_KERN_RANDOM @EVENT__HAVE_DECL_KERN_RANDOM@
80-
81-
/* Define to 1 if you have the declaration of `RANDOM_UUID'. */
82-
#define EVENT__HAVE_DECL_RANDOM_UUID @EVENT__HAVE_DECL_RANDOM_UUID@
78+
/* Define to 1 if you have `getrandom' function. */
79+
#define EVENT__HAVE_GETRANDOM @EVENT__HAVE_GETRANDOM@
8380

8481
/* Define if /dev/poll is available */
8582
#cmakedefine EVENT__HAVE_DEVPOLL 1
@@ -367,6 +364,9 @@
367364
/* Define to 1 if you have the <sys/stat.h> header file. */
368365
#cmakedefine EVENT__HAVE_SYS_STAT_H 1
369366

367+
/* Define to 1 if you have the <sys/random.h> header file. */
368+
#cmakedefine EVENT__HAVE_SYS_RANDOM_H 1
369+
370370
/* Define to 1 if you have the <sys/sysctl.h> header file. */
371371
#cmakedefine EVENT__HAVE_SYS_SYSCTL_H 1
372372

0 commit comments

Comments
 (0)