Skip to content

Commit a81c494

Browse files
committed
Use sysctl for seeding on MacOS/BSD
1 parent 2554c1b commit a81c494

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed

configure.ac

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ if test x$TARGET_OS = xdarwin; then
788788
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip"])
789789
fi
790790

791-
AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h])
791+
AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
792792

793793
# FD_ZERO may be dependent on a declaration of memcpy, e.g. in SmartOS
794794
# check that it fails to build without memcpy, then that it builds with
@@ -948,6 +948,18 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
948948
[ AC_MSG_RESULT(no)]
949949
)
950950

951+
AC_MSG_CHECKING(for sysctl)
952+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
953+
#include <sys/sysctl.h>]],
954+
[[ static const int name[2] = {CTL_KERN, KERN_VERSION};
955+
#ifdef __linux__
956+
#error "Don't use sysctl on Linux, it's deprecated even when it works"
957+
#endif
958+
sysctl(name, 2, nullptr, nullptr, nullptr, 0); ]])],
959+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_SYSCTL, 1,[Define this symbol if the BSD sysctl() is available]) ],
960+
[ AC_MSG_RESULT(no)]
961+
)
962+
951963
AC_MSG_CHECKING(for sysctl KERN_ARND)
952964
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
953965
#include <sys/sysctl.h>]],

src/randomenv.cpp

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
#if HAVE_DECL_GETIFADDRS
4545
#include <ifaddrs.h>
4646
#endif
47+
#if HAVE_SYSCTL
48+
#include <sys/sysctl.h>
49+
#if HAVE_VM_VM_PARAM_H
50+
#include <vm/vm_param.h>
51+
#endif
52+
#if HAVE_SYS_RESOURCES_H
53+
#include <sys/resources.h>
54+
#endif
55+
#if HAVE_SYS_VMMETER_H
56+
#include <sys/vmmeter.h>
57+
#endif
58+
#endif
4759

4860
//! Necessary on some platforms
4961
extern char** environ;
@@ -149,6 +161,23 @@ void AddPath(CSHA512& hasher, const char *path)
149161
}
150162
#endif
151163

164+
#if HAVE_SYSCTL
165+
template<int... S>
166+
void AddSysctl(CSHA512& hasher)
167+
{
168+
int CTL[sizeof...(S)] = {S...};
169+
unsigned char buffer[65536];
170+
size_t siz = 65536;
171+
int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
172+
if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
173+
hasher << sizeof(CTL);
174+
hasher.Write((const unsigned char*)CTL, sizeof(CTL));
175+
if (siz > sizeof(buffer)) siz = sizeof(buffer);
176+
hasher << siz;
177+
hasher.Write(buffer, siz);
178+
}
179+
}
180+
#endif
152181

153182
} // namespace
154183

@@ -217,6 +246,30 @@ void RandAddDynamicEnv(CSHA512& hasher)
217246
AddFile(hasher, "/proc/self/status");
218247
#endif
219248

249+
#if HAVE_SYSCTL
250+
# ifdef CTL_KERN
251+
# if defined(KERN_PROC) && defined(KERN_PROC_ALL)
252+
AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
253+
# endif
254+
# endif
255+
# ifdef CTL_HW
256+
# ifdef HW_DISKSTATS
257+
AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
258+
# endif
259+
# endif
260+
# ifdef CTL_VM
261+
# ifdef VM_LOADAVG
262+
AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
263+
# endif
264+
# ifdef VM_TOTAL
265+
AddSysctl<CTL_VM, VM_TOTAL>(hasher);
266+
# endif
267+
# ifdef VM_METER
268+
AddSysctl<CTL_VM, VM_METER>(hasher);
269+
# endif
270+
# endif
271+
#endif
272+
220273
// Stack and heap location
221274
void* addr = malloc(4097);
222275
hasher << &addr << addr;
@@ -299,8 +352,81 @@ void RandAddStaticEnv(CSHA512& hasher)
299352
AddFile(hasher, "/etc/resolv.conf");
300353
AddFile(hasher, "/etc/timezone");
301354
AddFile(hasher, "/etc/localtime");
355+
#endif
302356

303-
/* TODO: sysctl's for OSX to fetch information not available from /proc */
357+
// For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of these
358+
// will exist on every system.
359+
#if HAVE_SYSCTL
360+
# ifdef CTL_HW
361+
# ifdef HW_MACHINE
362+
AddSysctl<CTL_HW, HW_MACHINE>(hasher);
363+
# endif
364+
# ifdef HW_MODEL
365+
AddSysctl<CTL_HW, HW_MODEL>(hasher);
366+
# endif
367+
# ifdef HW_NCPU
368+
AddSysctl<CTL_HW, HW_NCPU>(hasher);
369+
# endif
370+
# ifdef HW_PHYSMEM
371+
AddSysctl<CTL_HW, HW_PHYSMEM>(hasher);
372+
# endif
373+
# ifdef HW_USERMEM
374+
AddSysctl<CTL_HW, HW_USERMEM>(hasher);
375+
# endif
376+
# ifdef HW_MACHINE_ARCH
377+
AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher);
378+
# endif
379+
# ifdef HW_REALMEM
380+
AddSysctl<CTL_HW, HW_REALMEM>(hasher);
381+
# endif
382+
# ifdef HW_CPU_FREQ
383+
AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher);
384+
# endif
385+
# ifdef HW_BUS_FREQ
386+
AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher);
387+
# endif
388+
# ifdef HW_CACHELINE
389+
AddSysctl<CTL_HW, HW_CACHELINE>(hasher);
390+
# endif
391+
# endif
392+
# ifdef CTL_KERN
393+
# ifdef KERN_BOOTFILE
394+
AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher);
395+
# endif
396+
# ifdef KERN_BOOTTIME
397+
AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher);
398+
# endif
399+
# ifdef KERN_CLOCKRATE
400+
AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher);
401+
# endif
402+
# ifdef KERN_HOSTID
403+
AddSysctl<CTL_KERN, KERN_HOSTID>(hasher);
404+
# endif
405+
# ifdef KERN_HOSTUUID
406+
AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher);
407+
# endif
408+
# ifdef KERN_HOSTNAME
409+
AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher);
410+
# endif
411+
# ifdef KERN_OSRELDATE
412+
AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher);
413+
# endif
414+
# ifdef KERN_OSRELEASE
415+
AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher);
416+
# endif
417+
# ifdef KERN_OSREV
418+
AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
419+
# endif
420+
# ifdef KERN_OSTYPE
421+
AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher);
422+
# endif
423+
# ifdef KERN_POSIX1
424+
AddSysctl<CTL_KERN, KERN_OSREV>(hasher);
425+
# endif
426+
# ifdef KERN_VERSION
427+
AddSysctl<CTL_KERN, KERN_VERSION>(hasher);
428+
# endif
429+
# endif
304430
#endif
305431

306432
// Env variables

0 commit comments

Comments
 (0)