Skip to content

Commit

Permalink
test getrandom() in library initialization without FAKERANDOM_SEED
Browse files Browse the repository at this point in the history
Running "make randomtest" should demonstrates the segfault described
in wolfcw#295
  • Loading branch information
dkg committed Feb 23, 2021
1 parent b4a822c commit 8f2c856
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 17 additions & 0 deletions test/librandom.c
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions test/librandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __LIBRANDOM_H__
#define __LIBRANDOM_H__

extern void func();

#endif
8 changes: 8 additions & 0 deletions test/randomtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/use_lib_random.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "librandom.h"

int main() {
func();
return 0;
}

0 comments on commit 8f2c856

Please sign in to comment.