summaryrefslogtreecommitdiff
path: root/src/libfaketime.c
diff options
context:
space:
mode:
authorWolfgang Hommel <wolfcw@users.noreply.github.com>2021-02-25 06:27:35 +0100
committerGitHub <noreply@github.com>2021-02-25 06:27:35 +0100
commit9337bccfcb7512ebf66bcbaf0f13f878cd53a6e8 (patch)
treea279b99fd6515b07e7af0083fff75d46a7078fa3 /src/libfaketime.c
parent3668fd9b0f7427de5c7b3c87c4c2b340aa18f67d (diff)
parent3a81c6becd99dba16813349e86fefa30d8cf05af (diff)
downloadlibfaketime-9337bccfcb7512ebf66bcbaf0f13f878cd53a6e8.tar.gz
Merge pull request #304 from dkg/cover-getentropy
better testing for interception of randomness from the kernel, including getentropy()
Diffstat (limited to 'src/libfaketime.c')
-rw-r--r--src/libfaketime.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/libfaketime.c b/src/libfaketime.c
index 856f5a5..66003e2 100644
--- a/src/libfaketime.c
+++ b/src/libfaketime.c
@@ -233,6 +233,7 @@ static int (*real_futimens) (int fd, const struct timespec times
#ifdef FAKE_RANDOM
static ssize_t (*real_getrandom) (void *buf, size_t buflen, unsigned int flags);
+static int (*real_getentropy) (void *buffer, size_t length);
#endif
#ifdef FAKE_PID
static pid_t (*real_getpid) ();
@@ -2466,6 +2467,7 @@ static void ftpl_init(void)
#ifdef FAKE_RANDOM
real_getrandom = dlsym(RTLD_NEXT, "getrandom");
+ real_getentropy = dlsym(RTLD_NEXT, "getentropy");
#endif
#ifdef FAKE_PID
@@ -3694,8 +3696,8 @@ inline static uint32_t fakerandom_msws(uint64_t s) {
return (char) x & 0xFF;
}
-
-ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
+/* return 0 if no FAKERANDOM_SEED was seen */
+static int bypass_randomness(void* buf, size_t buflen) {
char *seedstring = getenv("FAKERANDOM_SEED");
char *b = buf;
@@ -3704,9 +3706,14 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
for (size_t i = 0; i < buflen; i++) {
b[i] = fakerandom_msws(seed);
}
- return buflen;
+ return 1;
}
- else { /* if no FAKERANDOM_SEED was given, use the original function */
+ return 0;
+}
+ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
+ if (bypass_randomness(buf, buflen)) {
+ return buflen;
+ } else {
if (!initialized)
{
ftpl_init();
@@ -3714,6 +3721,15 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
return real_getrandom(buf, buflen, flags);
}
}
+int getentropy(void *buffer, size_t length) {
+ if (bypass_randomness(buffer, length)) {
+ return 0;
+ } else {
+ if (!initialized)
+ ftpl_init();
+ return real_getentropy(buffer, length);
+ }
+}
#endif
#ifdef FAKE_PID