diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2019-08-11 18:46:07 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2019-08-11 19:00:35 +0200 |
commit | 9f2cf5f50a337d73058c43e88453cb2926b49fb3 (patch) | |
tree | f03a8542b9fe7367dea069ace86237e461fb0b73 | |
parent | b143b86e303a09afd5d69a06175f2dca98559a6d (diff) |
added fallback random source ("/dev/urandom") for systems with low entropy available and "/dev/random" activated
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/utils.c | 28 |
3 files changed, 31 insertions, 5 deletions
diff --git a/configure.ac b/configure.ac index 239a78b..80050d3 100644 --- a/configure.ac +++ b/configure.ac @@ -160,13 +160,14 @@ esac dnl `--with-randomdev`: Default value /dev/random use_customrng=no +use_rngfallback=no AC_MSG_CHECKING([for random device]) AC_ARG_WITH([rngdev], [AS_HELP_STRING([--with-rngdev], [Set an alternative random device. (default: /dev/random)])], [use_customrng=yes], [with_rngdev="/dev/random"]) case ${with_rngdev} in yes) with_rngdev="/dev/random" ;; - /dev/random) ;; + /dev/random) use_rngfallback=yes ;; /dev/urandom) ;; *) AC_MSG_ERROR([Unknown random device \`${with_rngdev}\` for --with-rngdev: Only \`/dev/random\` xor \`/dev/urandom\` allowed. This option is unused on Windows targets.]) ;; esac @@ -236,6 +237,7 @@ AM_CONDITIONAL([IS_WINDOWS], [test x"${use_msw}" = xyes]) AM_CONDITIONAL([HAVE_ICMPFILTER], [test x"${with_icmp_filter}" = xyes]) AM_CONDITIONAL([HAVE_ARC4RANDOM], [test x"${arc4random_enabled}" = xyes]) AM_CONDITIONAL([USE_CUSTOMRNG], [test x"${use_customrng}" = xyes]) +AM_CONDITIONAL([USE_RNGFALLBACK], [test x"${use_rngfallback}" = xyes]) dnl output config headers AC_CONFIG_HEADERS([src/config.h:src/config.h.in]) diff --git a/src/Makefile.am b/src/Makefile.am index 6f9ca23..97b4545 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,6 +30,10 @@ if USE_CUSTOMRNG ptunnel_ng_CFLAGS += -DUSE_CUSTOMRNG=1 endif +if USE_RNGFALLBACK +ptunnel_ng_CFLAGS += -DUSE_RNGFALLBACK=1 +endif + ptunnel_ng_SOURCES = \ md5.c \ challenge.c \ diff --git a/src/utils.c b/src/utils.c index 64f6f88..6752890 100644 --- a/src/utils.c +++ b/src/utils.c @@ -174,12 +174,32 @@ int pt_random(void) { } bytes_read = read(rng_fd, &rnd_val, sizeof rnd_val); if (bytes_read != sizeof rnd_val) { - if (bytes_read < 0) - pt_log(kLog_error, "FATAL: Read from random device failed: %s\n", + if (bytes_read < 0) { + pt_log(kLog_error, "Read from random device failed: %s\n", strerror(errno)); - else - pt_log(kLog_error, "FATAL: Read only %zd bytes (wanted %zd bytes)\n", + } else { + pt_log(kLog_info, "Read only %zd random bytes (wanted %zd bytes)\n", bytes_read, sizeof rnd_val); + } +#ifdef USE_RNGFALLBACK + /* use /dev/urandom if previous random device failed */ + static int fallback_rng_fd = -1; + if (fallback_rng_fd < 0) { + fallback_rng_fd = open("/dev/urandom", O_RDONLY); + if (fallback_rng_fd < 0) { + pt_log(kLog_error, "FATAL: Could not open fallback random device '%s': %s\n", + "/dev/urandom", strerror(errno)); + exit(EXIT_FAILURE); + } + } + if (bytes_read < 0) { + bytes_read = 0; + } + if (read(fallback_rng_fd, &rnd_val + bytes_read, sizeof rnd_val - bytes_read) == sizeof rnd_val - bytes_read) { + return rnd_val; + } +#endif + pt_log(kLog_error, "FATAL: No more RNG sources available\n"); exit(EXIT_FAILURE); } return rnd_val; |