Merge pull request #296 from dkg/test-getrandom-library-init

test getrandom() in library initialization without FAKERANDOM_SEED
This commit is contained in:
Wolfgang Hommel
2021-02-23 22:08:43 +01:00
committed by GitHub
6 changed files with 52 additions and 3 deletions

3
.gitignore vendored
View File

@@ -2,6 +2,9 @@
*.so.1
timetest
test/getrandom_test
test/librandom.o
test/librandom.so
test/use_lib_random
src/libfaketime.dylib.1
src/libfaketime.1.dylib

View File

@@ -25,13 +25,22 @@ functest:
getrandom_test: getrandom_test.c
${CC} -o $@ ${CFLAGS} $<
randomtest: getrandom_test
randomtest: getrandom_test use_lib_random
./randomtest.sh
librandom.o: librandom.c
${CC} -c -o $@ -fpic ${CFLAGS} $<
librandom.so: librandom.o
${CC} -o $@ -shared ${CFLAGS} $<
use_lib_random: use_lib_random.c librandom.so
${CC} -L. -o $@ ${CFLAGS} $< -lrandom
clean:
@rm -f ${OBJ} timetest getrandom_test
@rm -f ${OBJ} timetest getrandom_test librandom.o librandom.so use_lib_random
distclean: clean
@echo
.PHONY: all test clean distclean
.PHONY: all test clean distclean randomtest

17
test/librandom.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <sys/random.h>
void func() {
fprintf(stderr, " called func()\n");
}
static __attribute__((constructor)) void rnd_init() {
unsigned int targ;
ssize_t ret = getrandom(&targ, sizeof(targ), 0);
if (ret == sizeof(targ)) {
fprintf(stderr, " getrandom() yielded 0x%08x\n", targ);
} else {
fprintf(stderr, " getrandom() failed with only %zd\n", ret);
}
}

6
test/librandom.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef __LIBRANDOM_H__
#define __LIBRANDOM_H__
extern void func();
#endif

View File

@@ -32,6 +32,14 @@ fi
rm -f run-base run0 run1 run2 run3
printf 'testing shared object with getrandom() in library constructor\n'
LD_LIBRARY_PATH=. ./use_lib_random
printf 'now with LD_PRELOAD and FAKERANDOM_SEED\n'
FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_random
# this demonstrates the crasher from https://github.com/wolfcw/libfaketime/issues/295
printf 'now with LD_PRELOAD without FAKERANDOM_SEED\n'
LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_random
if [ 0 = $error ]; then
printf 'getrandom interception test successful.\n'
fi

6
test/use_lib_random.c Normal file
View File

@@ -0,0 +1,6 @@
#include "librandom.h"
int main() {
func();
return 0;
}