Skip to content

Commit 0a79ce7

Browse files
authored
Merge pull request #361 from devnexen/randomization_changes
Changing the randomisation generation :
2 parents 1c50794 + 114e6ab commit 0a79ce7

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

cassconfig.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
#cmakedefine HAVE_BOOST_ATOMIC
77
#cmakedefine HAVE_NOSIGPIPE
88
#cmakedefine HAVE_SIGTIMEDWAIT
9+
#cmakedefine HAVE_ARC4RANDOM
10+
#cmakedefine HAVE_GETRANDOM
911

1012
#endif

cmake/modules/CppDriver.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ macro(CassFindSourceFiles)
511511
endmacro()
512512

513513
macro(CassConfigure)
514+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
515+
check_symbol_exists(GRND_NONBLOCK "linux/random.h" HAVE_GETRANDOM)
516+
else()
517+
check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM)
518+
endif()
514519
check_symbol_exists(SO_NOSIGPIPE "sys/socket.h;sys/types.h" HAVE_NOSIGPIPE)
515520
check_symbol_exists(sigtimedwait "signal.h" HAVE_SIGTIMEDWAIT)
516521
if (NOT WIN32 AND NOT HAVE_NOSIGPIPE AND NOT HAVE_SIGTIMEDWAIT)

src/random.cpp

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "random.hpp"
1818

19+
#include "cassconfig.hpp"
1920
#include "cassandra.h"
2021
#include "logger.hpp"
2122
#include "scoped_lock.hpp"
@@ -27,9 +28,14 @@
2728
#include <Windows.h>
2829
#include <WinCrypt.h>
2930
#else
31+
#if defined(HAVE_GETRANDOM)
32+
#include <syscall.h>
33+
#include <linux/random.h>
34+
#endif
3035
#include <errno.h>
3136
#include <fcntl.h>
3237
#include <unistd.h>
38+
#include <stdlib.h>
3339
#include <string.h>
3440
#include <sys/types.h>
3541
#include <sys/uio.h>
@@ -84,33 +90,54 @@ uint64_t Random::next(uint64_t max) {
8490
#endif
8591

8692
uint64_t get_random_seed(uint64_t seed) {
93+
#if defined(HAVE_ARC4RANDOM)
94+
arc4random_buf(&seed, sizeof(seed));
95+
#else
8796
static const char* device = "/dev/urandom";
88-
89-
int fd = open(device, O_RDONLY);
90-
91-
if (fd < 0) {
97+
ssize_t num_bytes;
98+
bool readurandom = true;
99+
#if defined(HAVE_GETRANDOM)
100+
num_bytes = static_cast<ssize_t>(syscall(SYS_getrandom, &seed,
101+
sizeof(seed), GRND_NONBLOCK));
102+
if (num_bytes < static_cast<ssize_t>(sizeof(seed))) {
92103
char buf[STRERROR_BUFSIZE_];
93104
char* err = STRERROR_R_(errno, buf, sizeof(buf));
94-
LOG_CRITICAL("Unable to open random device (%s): %s", device, err);
95-
return seed;
105+
LOG_WARN("Unable to read %u random bytes (%s): %u read",
106+
static_cast<unsigned int>(sizeof(seed)), err,
107+
static_cast<unsigned int>(num_bytes));
108+
} else {
109+
readurandom = false;
96110
}
97-
98-
ssize_t num_bytes = read(fd, reinterpret_cast<char*>(&seed), sizeof(seed));
99-
if (num_bytes < 0) {
100-
char buf[STRERROR_BUFSIZE_];
101-
char* err = STRERROR_R_(errno, buf, sizeof(buf));
102-
LOG_CRITICAL("Unable to read from random device (%s): %s", device, err);
103-
} else if (num_bytes != sizeof(seed)) {
104-
char buf[STRERROR_BUFSIZE_];
105-
char* err = STRERROR_R_(errno, buf, sizeof(buf));
106-
LOG_CRITICAL("Unable to read full seed value (expected: %u read: %u) "
107-
"from random device (%s): %s",
108-
static_cast<unsigned int>(sizeof(seed)),
109-
static_cast<unsigned int>(num_bytes),
110-
device, err);
111+
#endif // defined(HAVE_GETRANDOM)
112+
113+
if (readurandom) {
114+
int fd = open(device, O_RDONLY);
115+
116+
if (fd < 0) {
117+
char buf[STRERROR_BUFSIZE_];
118+
char* err = STRERROR_R_(errno, buf, sizeof(buf));
119+
LOG_CRITICAL("Unable to open random device (%s): %s", device, err);
120+
return seed;
121+
}
122+
123+
num_bytes = read(fd, reinterpret_cast<char*>(&seed), sizeof(seed));
124+
if (num_bytes < 0) {
125+
char buf[STRERROR_BUFSIZE_];
126+
char* err = STRERROR_R_(errno, buf, sizeof(buf));
127+
LOG_CRITICAL("Unable to read from random device (%s): %s", device, err);
128+
} else if (num_bytes != sizeof(seed)) {
129+
char buf[STRERROR_BUFSIZE_];
130+
char* err = STRERROR_R_(errno, buf, sizeof(buf));
131+
LOG_CRITICAL("Unable to read full seed value (expected: %u read: %u) "
132+
"from random device (%s): %s",
133+
static_cast<unsigned int>(sizeof(seed)),
134+
static_cast<unsigned int>(num_bytes),
135+
device, err);
136+
}
137+
138+
close(fd);
111139
}
112-
113-
close(fd);
140+
#endif // defined(HAVE_ARC4RANDOM)
114141

115142
return seed;
116143
}

0 commit comments

Comments
 (0)