Fix a couple of bugs in the BSD sysctl arc4seed logic

Of course, FreeBSD has its own arc4random() implementation, so this should
never actually be needed.  Still, it's good to paint the underside of the
wagon.
This commit is contained in:
Nick Mathewson 2010-04-23 16:08:09 -04:00
parent a5bf43abb0
commit a47a4b7e7e
2 changed files with 8 additions and 7 deletions

View File

@ -201,16 +201,16 @@ arc4_seed_sysctl_linux(void)
} }
#endif #endif
#if _EVENT_HAVE_DECL_CTL_KERN && _EVENT_HAVE_DECL_KERN_ARAND #if _EVENT_HAVE_DECL_CTL_KERN && _EVENT_HAVE_DECL_KERN_ARND
#define TRY_SEED_SYSCTL_BSD #define TRY_SEED_SYSCTL_BSD
static int static int
arc4_seed_sysctl_bsd(void) arc4_seed_sysctl_bsd(void)
{ {
/* Based on code from William Ahern and from OpenBSD, this function /* Based on code from William Ahern and from OpenBSD, this function
* tries to use the KERN_ARAND syscall to get entropy from the kernel. * tries to use the KERN_ARND syscall to get entropy from the kernel.
* This can work even if /dev/urandom is inaccessible for some reason * This can work even if /dev/urandom is inaccessible for some reason
* (e.g., we're running in a chroot). */ * (e.g., we're running in a chroot). */
int mib[] = { CTL_KERN, KERN_ARAND }; int mib[] = { CTL_KERN, KERN_ARND };
unsigned char buf[ADD_ENTROPY]; unsigned char buf[ADD_ENTROPY];
size_t len, n; size_t len, n;
int i, any_set; int i, any_set;
@ -218,7 +218,7 @@ arc4_seed_sysctl_bsd(void)
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
len = sizeof(buf); len = sizeof(buf);
if (sysctl(mib, 2, rnd, &len, NULL, 0) == -1) { if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
for (len = 0; len < sizeof(buf); len += sizeof(unsigned)) { for (len = 0; len < sizeof(buf); len += sizeof(unsigned)) {
n = sizeof(unsigned); n = sizeof(unsigned);
if (n + len > sizeof(buf)) if (n + len > sizeof(buf))

View File

@ -159,8 +159,9 @@ if test "x$ac_cv_header_sys_time_h" = "xyes"; then
fi fi
if test "x$ac_cv_header_sys_sysctl_h" = "xyes"; then if test "x$ac_cv_header_sys_sysctl_h" = "xyes"; then
AC_CHECK_DECLS([CTL_KERN, KERN_RANDOM, RANDOM_UUID, KERN_ARAND], [], [], AC_CHECK_DECLS([CTL_KERN, KERN_RANDOM, RANDOM_UUID, KERN_ARND], [], [],
[[#include <sys/sysctl.h>]] [[#include <sys/types.h>
#include <sys/sysctl.h>]]
) )
fi fi