From 00d6edf90cfb397d37b0107113fefa444a2a0a76 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Wed, 24 Feb 2021 15:24:45 -0500 Subject: [PATCH] Test repeated invocations of getrandom() A single program that invokes getrandom() repeatedly should end up with the same stream of bytes, regardless of how it chunks up the reading from the entropy source. This test already passses. I'm including it because it seems like a useful confirmation. --- .gitignore | 1 + test/Makefile | 2 +- test/randomtest.sh | 15 ++++++++++++--- test/repeat_random.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 test/repeat_random.c diff --git a/.gitignore b/.gitignore index afee928..14c308f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ test/lib*.o test/lib*.so test/use_lib_random test/use_lib_getpid +test/repeat_random src/libfaketime.dylib.1 src/libfaketime.1.dylib diff --git a/test/Makefile b/test/Makefile index 8f56489..8959b71 100644 --- a/test/Makefile +++ b/test/Makefile @@ -25,7 +25,7 @@ functest: %_test: %_test.c ${CC} -o $@ ${CFLAGS} $< -randomtest: getrandom_test use_lib_random librandom.so +randomtest: getrandom_test use_lib_random librandom.so repeat_random ./randomtest.sh getpidtest: use_lib_getpid libgetpid.so diff --git a/test/randomtest.sh b/test/randomtest.sh index 5330419..4290ec6 100755 --- a/test/randomtest.sh +++ b/test/randomtest.sh @@ -14,7 +14,7 @@ FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" ./getrandom_test > run3 if diff -u run-base run0 > /dev/null; then error=1 - printf >&2 'test run without the LD_PRELOAD matches a run without LD_PRELOAD' + printf >&2 'test run without the LD_PRELOAD matches a run without LD_PRELOAD\n' fi if diff -u run0 run1 > /dev/null; then @@ -30,8 +30,6 @@ if diff -u run2 run3 >/dev/null; then printf >&2 'test runs with different seeds produced the same data!\n' 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' @@ -40,6 +38,17 @@ FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_li printf 'now with LD_PRELOAD without FAKERANDOM_SEED\n' LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_random + +FAKERANDOM_SEED=0xDEADBEEFDEADBEEF LD_PRELOAD="$FTPL" ./repeat_random 3 5 > run4 +FAKERANDOM_SEED=0xDEADBEEFDEADBEEF LD_PRELOAD="$FTPL" ./repeat_random 5 3 > run5 + +if ! diff -u run4 run5; then + error=5 + printf >&2 '5 calls of getrandom(3) did not produce the same stream as 3 calls of getrandom(5)\n' +fi + +rm -f run-base run0 run1 run2 run3 run4 run5 + if [ 0 = $error ]; then printf 'getrandom interception test successful.\n' fi diff --git a/test/repeat_random.c b/test/repeat_random.c new file mode 100644 index 0000000..5a93433 --- /dev/null +++ b/test/repeat_random.c @@ -0,0 +1,40 @@ +#include +#include +#include + +void usage(const char* name) { + fprintf(stderr, + "Usage: %s REPS SIZE\n\n" + "Gather and print REPS blocks of SIZE bytes from getrandom()\n", + name); +} + +int main(int argc, const char **argv) { + int reps, size; + unsigned char *buf; + if (argc != 3) { + usage(argv[0]); + return 1; + } + reps = atoi(argv[1]); + size = atoi(argv[2]); + buf = malloc(size); + if (!buf) { + fprintf(stderr, "failure to allocate buffer of size %d\n", size); + return 1; + } + for (int i = 0; i < reps; i++) { + ssize_t resp = getrandom(buf, size, 0); + if (resp != size) { + fprintf(stderr, "tried to get %d bytes, got %zd\n", size, resp); + free(buf); + return 2; + } + for (int j = 0; j < size; j++) { + printf("%02x", buf[j]); + } + } + free(buf); + printf("\n"); + return 0; +};