Changeset View
Changeset View
Standalone View
Standalone View
src/randomenv.cpp
Show All 39 Lines | |||||
#ifdef __MACH__ | #ifdef __MACH__ | ||||
#include <mach/clock.h> | #include <mach/clock.h> | ||||
#include <mach/mach.h> | #include <mach/mach.h> | ||||
#include <mach/mach_time.h> | #include <mach/mach_time.h> | ||||
#endif | #endif | ||||
#if HAVE_DECL_GETIFADDRS | #if HAVE_DECL_GETIFADDRS | ||||
#include <ifaddrs.h> | #include <ifaddrs.h> | ||||
#endif | #endif | ||||
#if HAVE_SYSCTL | |||||
#include <sys/sysctl.h> | |||||
#if HAVE_VM_VM_PARAM_H | |||||
#include <vm/vm_param.h> | |||||
#endif | |||||
#if HAVE_SYS_RESOURCES_H | |||||
#include <sys/resources.h> | |||||
#endif | |||||
#if HAVE_SYS_VMMETER_H | |||||
#include <sys/vmmeter.h> | |||||
#endif | |||||
#endif | |||||
//! Necessary on some platforms | //! Necessary on some platforms | ||||
extern char **environ; | extern char **environ; | ||||
namespace { | namespace { | ||||
void RandAddSeedPerfmon(CSHA512 &hasher) { | void RandAddSeedPerfmon(CSHA512 &hasher) { | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom | // Don't need this on Linux, OpenSSL automatically uses /dev/urandom | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | void AddPath(CSHA512 &hasher, const char *path) { | ||||
struct stat sb = {}; | struct stat sb = {}; | ||||
if (stat(path, &sb) == 0) { | if (stat(path, &sb) == 0) { | ||||
hasher.Write((const uint8_t *)path, strlen(path) + 1); | hasher.Write((const uint8_t *)path, strlen(path) + 1); | ||||
hasher << sb; | hasher << sb; | ||||
} | } | ||||
} | } | ||||
#endif | #endif | ||||
#if HAVE_SYSCTL | |||||
template <int... S> void AddSysctl(CSHA512 &hasher) { | |||||
int CTL[sizeof...(S)] = {S...}; | |||||
uint8_t buffer[65536]; | |||||
size_t siz = 65536; | |||||
int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0); | |||||
if (ret == 0 || (ret == -1 && errno == ENOMEM)) { | |||||
hasher << sizeof(CTL); | |||||
hasher.Write((const uint8_t *)CTL, sizeof(CTL)); | |||||
if (siz > sizeof(buffer)) { | |||||
siz = sizeof(buffer); | |||||
} | |||||
hasher << siz; | |||||
hasher.Write(buffer, siz); | |||||
} | |||||
} | |||||
#endif | |||||
} // namespace | } // namespace | ||||
void RandAddDynamicEnv(CSHA512 &hasher) { | void RandAddDynamicEnv(CSHA512 &hasher) { | ||||
RandAddSeedPerfmon(hasher); | RandAddSeedPerfmon(hasher); | ||||
// Various clocks | // Various clocks | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
FILETIME ftime; | FILETIME ftime; | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | #ifdef __linux__ | ||||
AddFile(hasher, "/proc/zoneinfo"); | AddFile(hasher, "/proc/zoneinfo"); | ||||
AddFile(hasher, "/proc/meminfo"); | AddFile(hasher, "/proc/meminfo"); | ||||
AddFile(hasher, "/proc/softirqs"); | AddFile(hasher, "/proc/softirqs"); | ||||
AddFile(hasher, "/proc/stat"); | AddFile(hasher, "/proc/stat"); | ||||
AddFile(hasher, "/proc/self/schedstat"); | AddFile(hasher, "/proc/self/schedstat"); | ||||
AddFile(hasher, "/proc/self/status"); | AddFile(hasher, "/proc/self/status"); | ||||
#endif | #endif | ||||
#if HAVE_SYSCTL | |||||
#ifdef CTL_KERN | |||||
#if defined(KERN_PROC) && defined(KERN_PROC_ALL) | |||||
AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher); | |||||
#endif | |||||
#endif | |||||
#ifdef CTL_HW | |||||
#ifdef HW_DISKSTATS | |||||
AddSysctl<CTL_HW, HW_DISKSTATS>(hasher); | |||||
#endif | |||||
#endif | |||||
#ifdef CTL_VM | |||||
#ifdef VM_LOADAVG | |||||
AddSysctl<CTL_VM, VM_LOADAVG>(hasher); | |||||
#endif | |||||
#ifdef VM_TOTAL | |||||
AddSysctl<CTL_VM, VM_TOTAL>(hasher); | |||||
#endif | |||||
#ifdef VM_METER | |||||
AddSysctl<CTL_VM, VM_METER>(hasher); | |||||
#endif | |||||
#endif | |||||
#endif | |||||
// Stack and heap location | // Stack and heap location | ||||
void *addr = malloc(4097); | void *addr = malloc(4097); | ||||
hasher << &addr << addr; | hasher << &addr << addr; | ||||
free(addr); | free(addr); | ||||
} | } | ||||
void RandAddStaticEnv(CSHA512 &hasher) { | void RandAddStaticEnv(CSHA512 &hasher) { | ||||
// Some compile-time static properties | // Some compile-time static properties | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | #ifdef __linux__ | ||||
AddFile(hasher, "/proc/version"); | AddFile(hasher, "/proc/version"); | ||||
#endif | #endif | ||||
AddFile(hasher, "/etc/passwd"); | AddFile(hasher, "/etc/passwd"); | ||||
AddFile(hasher, "/etc/group"); | AddFile(hasher, "/etc/group"); | ||||
AddFile(hasher, "/etc/hosts"); | AddFile(hasher, "/etc/hosts"); | ||||
AddFile(hasher, "/etc/resolv.conf"); | AddFile(hasher, "/etc/resolv.conf"); | ||||
AddFile(hasher, "/etc/timezone"); | AddFile(hasher, "/etc/timezone"); | ||||
AddFile(hasher, "/etc/localtime"); | AddFile(hasher, "/etc/localtime"); | ||||
#endif | |||||
/* TODO: sysctl's for OSX to fetch information not available from /proc */ | // For MacOS/BSDs, gather data through sysctl instead of /proc. Not all of | ||||
// these will exist on every system. | |||||
#if HAVE_SYSCTL | |||||
#ifdef CTL_HW | |||||
#ifdef HW_MACHINE | |||||
AddSysctl<CTL_HW, HW_MACHINE>(hasher); | |||||
#endif | |||||
#ifdef HW_MODEL | |||||
AddSysctl<CTL_HW, HW_MODEL>(hasher); | |||||
#endif | |||||
#ifdef HW_NCPU | |||||
AddSysctl<CTL_HW, HW_NCPU>(hasher); | |||||
#endif | |||||
#ifdef HW_PHYSMEM | |||||
AddSysctl<CTL_HW, HW_PHYSMEM>(hasher); | |||||
#endif | |||||
#ifdef HW_USERMEM | |||||
AddSysctl<CTL_HW, HW_USERMEM>(hasher); | |||||
#endif | |||||
#ifdef HW_MACHINE_ARCH | |||||
AddSysctl<CTL_HW, HW_MACHINE_ARCH>(hasher); | |||||
#endif | |||||
#ifdef HW_REALMEM | |||||
AddSysctl<CTL_HW, HW_REALMEM>(hasher); | |||||
#endif | |||||
#ifdef HW_CPU_FREQ | |||||
AddSysctl<CTL_HW, HW_CPU_FREQ>(hasher); | |||||
#endif | |||||
#ifdef HW_BUS_FREQ | |||||
AddSysctl<CTL_HW, HW_BUS_FREQ>(hasher); | |||||
#endif | |||||
#ifdef HW_CACHELINE | |||||
AddSysctl<CTL_HW, HW_CACHELINE>(hasher); | |||||
#endif | |||||
#endif | |||||
#ifdef CTL_KERN | |||||
#ifdef KERN_BOOTFILE | |||||
AddSysctl<CTL_KERN, KERN_BOOTFILE>(hasher); | |||||
#endif | |||||
#ifdef KERN_BOOTTIME | |||||
AddSysctl<CTL_KERN, KERN_BOOTTIME>(hasher); | |||||
#endif | |||||
#ifdef KERN_CLOCKRATE | |||||
AddSysctl<CTL_KERN, KERN_CLOCKRATE>(hasher); | |||||
#endif | |||||
#ifdef KERN_HOSTID | |||||
AddSysctl<CTL_KERN, KERN_HOSTID>(hasher); | |||||
#endif | |||||
#ifdef KERN_HOSTUUID | |||||
AddSysctl<CTL_KERN, KERN_HOSTUUID>(hasher); | |||||
#endif | |||||
#ifdef KERN_HOSTNAME | |||||
AddSysctl<CTL_KERN, KERN_HOSTNAME>(hasher); | |||||
#endif | |||||
#ifdef KERN_OSRELDATE | |||||
AddSysctl<CTL_KERN, KERN_OSRELDATE>(hasher); | |||||
#endif | |||||
#ifdef KERN_OSRELEASE | |||||
AddSysctl<CTL_KERN, KERN_OSRELEASE>(hasher); | |||||
#endif | |||||
#ifdef KERN_OSREV | |||||
AddSysctl<CTL_KERN, KERN_OSREV>(hasher); | |||||
#endif | |||||
#ifdef KERN_OSTYPE | |||||
AddSysctl<CTL_KERN, KERN_OSTYPE>(hasher); | |||||
#endif | |||||
#ifdef KERN_POSIX1 | |||||
AddSysctl<CTL_KERN, KERN_OSREV>(hasher); | |||||
#endif | |||||
#ifdef KERN_VERSION | |||||
AddSysctl<CTL_KERN, KERN_VERSION>(hasher); | |||||
#endif | |||||
#endif | |||||
#endif | #endif | ||||
// Env variables | // Env variables | ||||
if (environ) { | if (environ) { | ||||
for (size_t i = 0; environ[i]; ++i) { | for (size_t i = 0; environ[i]; ++i) { | ||||
hasher.Write((const uint8_t *)environ[i], strlen(environ[i])); | hasher.Write((const uint8_t *)environ[i], strlen(environ[i])); | ||||
} | } | ||||
} | } | ||||
Show All 10 Lines |