summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-23 11:10:49 -0500
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-23 11:14:37 -0500
commit8f2c856d8e94f90183ba138b6d84ebdb42fe7741 (patch)
treee7518c3400c1324beaaf55ee52e75061fda5348b /test
parentb4a822cd6a7f65e9300c998c689270375f2dd0d1 (diff)
downloadlibfaketime-8f2c856d8e94f90183ba138b6d84ebdb42fe7741.tar.gz
test getrandom() in library initialization without FAKERANDOM_SEED
Running "make randomtest" should demonstrates the segfault described in https://github.com/wolfcw/libfaketime/issues/295
Diffstat (limited to 'test')
-rw-r--r--test/Makefile15
-rw-r--r--test/librandom.c17
-rw-r--r--test/librandom.h6
-rwxr-xr-xtest/randomtest.sh8
-rw-r--r--test/use_lib_random.c6
5 files changed, 49 insertions, 3 deletions
diff --git a/test/Makefile b/test/Makefile
index 8ee538c..2a8ad9c 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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
diff --git a/test/librandom.c b/test/librandom.c
new file mode 100644
index 0000000..d11ba23
--- /dev/null
+++ b/test/librandom.c
@@ -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);
+ }
+}
diff --git a/test/librandom.h b/test/librandom.h
new file mode 100644
index 0000000..9037fce
--- /dev/null
+++ b/test/librandom.h
@@ -0,0 +1,6 @@
+#ifndef __LIBRANDOM_H__
+#define __LIBRANDOM_H__
+
+extern void func();
+
+#endif
diff --git a/test/randomtest.sh b/test/randomtest.sh
index 66e5b7a..5330419 100755
--- a/test/randomtest.sh
+++ b/test/randomtest.sh
@@ -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
diff --git a/test/use_lib_random.c b/test/use_lib_random.c
new file mode 100644
index 0000000..583608d
--- /dev/null
+++ b/test/use_lib_random.c
@@ -0,0 +1,6 @@
+#include "librandom.h"
+
+int main() {
+ func();
+ return 0;
+}